]> ruin.nu Git - hbs.git/blob - bs/fleetview.cpp
now shows the lost score for the planet and the capping each tick
[hbs.git] / bs / fleetview.cpp
1 /***************************************************************************
2                           fleetview.cpp  -  description
3                              -------------------
4     begin                : Sun Mar 10 2002
5     copyright            : (C) 2002 by Michael Andreen
6     email                : whale@linux.nu
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include "fleetview.h"
19
20 #include "ui/roidseditview.h"
21 #include "ui/resourceview.h"
22 #include "planet.h"
23
24 #include <qvalidator.h>
25
26 #include <iostream>
27 #include <map>
28
29 using namespace std;
30
31 FleetView::FleetView(const Fleet* fleet, bool friendly, QWidget *parent, const char *name ) : QWidget(parent,name)
32 {
33         m_iMinTick = 0;
34         m_iMaxTick = 0;
35         m_iResultMode = 0;
36
37         m_Modified = false;
38         const Planet* planet = 0;
39         if((planet = dynamic_cast<const Planet*>(fleet)))
40         {
41                         m_Fleet = new Planet(*planet);
42                         m_bHome = true;
43         }
44         else
45         {
46                 m_Fleet = new Fleet(*fleet);
47                 m_bHome = false;
48         }
49         
50         m_Fleet->printFleet();
51         m_bFriendly = friendly;
52
53         //m_ChangesMapper = new QSignalMapper(this);
54
55         
56         findNames();
57         int rows = m_Names.size() / 3 + m_Names.size() % 3;
58         m_MainLayout = new QVBoxLayout(this,2);
59         m_MainLayout->addSpacing(2);
60         m_UnitsLayout = new QGridLayout(m_MainLayout, rows, 6);
61         buildUnitTable();
62         //QSpacerItem* space = new QSpacerItem(1, 1);
63         
64
65         m_MainLayout->addStretch(10);
66
67         if (m_bHome)
68         {
69                 m_RoidsEditView = new RoidsEditView(this);
70                 m_MainLayout->addWidget(m_RoidsEditView);
71                 connect(m_RoidsEditView, SIGNAL(changed(const QString&, int)), SLOT(slotRoidsChanged(const QString&, int)));
72         }
73         m_ResourceView = new ResourceView(this);
74         m_MainLayout->addWidget(m_ResourceView);
75         fillTable();
76 }
77
78 FleetView::~FleetView(){
79 }
80
81 /////////////////////////////////////////////////////////////////////
82 //
83 void FleetView::viewFleet(const Fleet* fleet, bool friendly)
84 {
85         if (m_Fleet->race() != fleet->race())
86         {
87                 findNames();
88         }
89         delete m_Fleet;
90         if(m_bHome)
91         {
92                 m_Fleet = new Planet(*dynamic_cast<const Planet*>(fleet));
93         }
94         else
95         {
96                 m_Fleet = new Fleet(*fleet);
97         }
98         //m_Fleet->printFleet();
99         fillTable();
100 }
101
102 /////////////////////////////////////////////////////////////////////
103 //
104 void FleetView::findNames()
105 {
106         m_Names.clear();
107
108         const UnitList& units = Fleet::Units(); 
109         vector<int> races = m_Fleet->RacesAllowed();
110
111         for (UnitList::const_iterator i = units.begin(); i != units.end(); ++i)
112         {
113                 for (vector<int>::iterator j = races.begin(); j != races.end(); ++j)
114                 {
115                         if ((*j) == i->second.race())
116                         {
117                                 m_Names.push_back(i->first.c_str());
118                                 break;
119                         }
120                 }
121         }
122 }
123
124 /////////////////////////////////////////////////////////////////////
125 //
126 void FleetView::buildUnitTable()
127 {
128         int rows = 0;
129         QIntValidator* val = new QIntValidator(this);
130         val->setBottom(0);
131
132         for (vector<QString>::iterator i = m_Names.begin(); i != m_Names.end(); ++rows)
133         {
134                 for ( int cols = 0; i != m_Names.end() && cols < 3; ++i, ++cols)
135                 {
136                         m_UnitsLabel[(*i)] = new QLabel((*i), this);
137                         m_UnitsLayout->addWidget(m_UnitsLabel[(*i)], rows, cols*3);
138
139                         m_UnitsEdit[(*i)] = new QLineEdit(this,(*i));
140       m_UnitsEdit[(*i)]->setValidator(val);
141                         m_UnitsLayout->addWidget(m_UnitsEdit[(*i)], rows, cols*3+1);
142
143                         m_UnitsLostSurvivedView[(*i)] = new QLineEdit(this);
144                         m_UnitsLostSurvivedView[(*i)]->setFocusPolicy(QWidget::NoFocus);
145                         m_UnitsLostSurvivedView[(*i)]->setReadOnly(true);
146                         m_UnitsLayout->addWidget(m_UnitsLostSurvivedView[(*i)], rows, cols*3+2);
147
148                         connect(m_UnitsEdit[(*i)], SIGNAL(textChanged(const QString&)),this, SLOT(unitsChanged(const QString&)));
149                 }
150         }
151
152 }
153
154 /////////////////////////////////////////////////////////////////////
155 //
156 const Fleet* FleetView::fleet()
157 {
158         return m_Fleet;
159 }
160
161 /////////////////////////////////////////////////////////////////////
162 //
163 bool FleetView::isHome()
164 {
165         return m_bHome;
166 }
167
168 /////////////////////////////////////////////////////////////////////
169 //
170 bool FleetView::isFriendly()
171 {
172         return m_bFriendly;
173 }
174
175 /////////////////////////////////////////////////////////////////////
176 //
177 void FleetView::unitsChanged(const QString& number)
178 {
179         m_Fleet->setFleet(sender()->name(), number.toInt());
180         m_Modified = true;
181         emit fleetChanged(m_Fleet);
182 }
183
184 /////////////////////////////////////////////////////////////////////
185 //
186 bool FleetView::isModified()
187 {
188         return m_Modified;
189 }
190
191 /////////////////////////////////////////////////////////////////////
192 //
193 void FleetView::fillTable()
194 {
195         for(vector<QString>::iterator i = m_Names.begin(); i != m_Names.end(); ++i)
196         {
197                 m_UnitsEdit[(*i)]->setText(QString("%1").arg(m_Fleet->fleet(i->latin1(), 0)));
198         }
199         Planet* pl;
200         if ((pl = dynamic_cast<Planet*>(m_Fleet)) && m_bHome)
201         {
202                 m_RoidsEditView->setValue(tr("score"),pl->planetScore());
203                 m_RoidsEditView->setValue(tr("metal"),pl->roids(tr("metal").latin1()));
204                 m_RoidsEditView->setValue(tr("crystal"),pl->roids(tr("crystal").latin1()));
205                 m_RoidsEditView->setValue(tr("eonium"),pl->roids(tr("eonium").latin1()));
206                 m_RoidsEditView->setValue(tr("uninit"),pl->roids(tr("uninit").latin1()));
207         }
208         slotViewTickRange();
209 }
210
211 /////////////////////////////////////////////////////////////////////
212 //
213 void FleetView::slotRoidsChanged(const QString& type, int value)
214 {
215         Planet* pl;
216         if ((pl = dynamic_cast<Planet*>(m_Fleet)))
217         {
218                 if (type == tr("score"))
219                         pl->setPlanetScore(value);
220                 else
221                 {
222                         pl->setRoids(type.latin1(), value);
223                 }
224                 emit fleetChanged(pl);
225         }
226 }
227
228 /////////////////////////////////////////////////////////////////////
229 //
230 void FleetView::slotViewTickRange(int min = -1, int max = -1)
231 {
232         if (min > -1)
233                 m_iMinTick = min;
234         if (max > -1)
235                 m_iMaxTick = max;
236
237         int before = 0;
238         int after  = 0;
239         for(vector<QString>::iterator i = m_Names.begin(); i != m_Names.end(); ++i)
240         {
241                 int show;
242                 if (m_iResultMode == 1) //Blocked
243                 {
244                         show = m_Fleet->blockedFleet(i->latin1(), m_iMaxTick);
245                 }
246                 else if (m_iResultMode == 2) //Survived
247                 {
248                         show = m_Fleet->fleet(i->latin1(), m_iMaxTick);
249                 }
250                 else
251                 {
252                         before = m_Fleet->fleet(i->latin1(), m_iMinTick);
253                         after = m_Fleet->fleet(i->latin1(), m_iMaxTick);
254                         show = after - before;
255                 }
256                 m_UnitsLostSurvivedView[(*i)]->setText(QString("%1").arg( show ));
257         }
258
259         int lost;
260         
261         int init = 0;
262
263         before = m_Fleet->resource(tr("metal").latin1(),m_iMinTick);
264         after = m_Fleet->resource(tr("metal").latin1(),m_iMaxTick);
265         lost = after - before;
266         init += lost;
267         m_ResourceView->setLines(tr("metal"),lost);
268
269         before = m_Fleet->resource(tr("crystal").latin1(),m_iMinTick);
270         after = m_Fleet->resource(tr("crystal").latin1(),m_iMaxTick);
271         lost = after - before;
272         init += lost;
273         m_ResourceView->setLines(tr("crystal"),lost);
274
275         before = m_Fleet->resource(tr("eonium").latin1(),m_iMinTick);
276         after = m_Fleet->resource(tr("eonium").latin1(),m_iMaxTick);
277         lost = after - before;
278         init += lost;
279         m_ResourceView->setLines(tr("eonium"),lost);
280
281         before = m_Fleet->resource(tr("uninit").latin1(),m_iMinTick);
282         after = m_Fleet->resource(tr("uninit").latin1(),m_iMaxTick);
283         lost = after - before;
284         m_ResourceView->setLines(tr("uninit"),lost);
285
286         before = m_Fleet->score(m_iMinTick);
287         after = m_Fleet->score(m_iMaxTick);
288         lost = after - before;
289         m_ResourceView->setLines(tr("score"),after);
290         m_ResourceView->setLines(tr("lost"),lost);
291         float cost =  (before - after) / float(init) / 1000;
292         m_ResourceView->setLines(tr("cost"),cost);
293         
294
295
296         Planet* pl;
297         if ((pl = dynamic_cast<Planet*>(m_Fleet)))
298         {
299
300                 before = pl->roids(tr("metal").latin1(),m_iMinTick);
301                 after = pl->roids(tr("metal").latin1(),m_iMaxTick);
302                 lost = after - before;
303                 m_RoidsEditView->slotSetLost(tr("metal"),lost);
304
305                 before = pl->roids(tr("crystal").latin1(),m_iMinTick);
306                 after = pl->roids(tr("crystal").latin1(),m_iMaxTick);
307                 lost = after - before;
308                 m_RoidsEditView->slotSetLost(tr("crystal"),lost);
309
310                 before = pl->roids(tr("eonium").latin1(),m_iMinTick);
311                 after = pl->roids(tr("eonium").latin1(),m_iMaxTick);
312                 lost = after - before;
313                 m_RoidsEditView->slotSetLost(tr("eonium"),lost);
314
315                 before = pl->roids(tr("uninit").latin1(),m_iMinTick);
316                 after = pl->roids(tr("uninit").latin1(),m_iMaxTick);
317                 lost = after - before;
318                 m_RoidsEditView->slotSetLost(tr("uninit"),lost);
319
320                 before = pl->planetScore(m_iMinTick);
321                 after = pl->planetScore(m_iMaxTick);
322                 lost = after - before;
323                 m_RoidsEditView->slotSetLost(tr("scorelost"),lost);
324
325                 m_RoidsEditView->slotSetLost(tr("capping"),pl->capping(m_iMaxTick));
326
327         }
328 }
329
330 //////////////////////////////////////////////////////////////////////////
331 //
332 void FleetView::slotSetResultMode(int i)
333 {
334         m_iResultMode = i;
335         slotViewTickRange();
336 }