1 // Copyright (C) 2003, 2004, 2005 Ulf Lorenz
2 // Copyright (C) 2007, 2008 Ben Asselstine
3 // Copyright (C) 2007, 2008 Ole Laursen
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Library General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #include "NextTurnPbm.h"
22 #include "playerlist.h"
25 #include "stacklist.h"
26 #include "armysetlist.h"
28 #include "vectoredunitlist.h"
31 #include "QuestsManager.h"
32 #include "ai_fast.h" //remove me
37 #define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<flush<<endl;}
40 NextTurnPbm::NextTurnPbm(bool turnmode, bool random_turns)
41 :NextTurn(turnmode, random_turns)
43 Playerlist* plist = Playerlist::getInstance();
44 for (Playerlist::iterator i = plist->begin(); i != plist->end(); ++i) {
46 p->ending_turn.connect(sigc::mem_fun(this, &NextTurn::endTurn));
50 NextTurnPbm::~NextTurnPbm()
54 void NextTurnPbm::start()
56 //We need the playerlist a lot, so maintain a copy of it.
57 Playerlist* plist = Playerlist::getInstance();
59 //set first player as active if no active player exists
60 if (!plist->getActiveplayer())
68 // do various start-up tasks
71 continuing_turn = false;
77 // inform everyone about the next turn
78 snextTurn.emit(plist->getActiveplayer());
80 if (plist->getNoOfPlayers() <= 2)
82 if (plist->checkPlayers()) //end of game detected
86 splayerStart.emit(plist->getActiveplayer());
88 // let the player do his or her duties...
89 bool continue_loop = plist->getActiveplayer()->startTurn();
93 //Now do some cleanup at the end of the turn.
96 //...and initiate the next one.
99 //if it is the first player's turn now, a new round has started
100 if (Playerlist::getInstance()->getActiveplayer() ==
101 Playerlist::getInstance()->getFirstLiving())
104 if (plist->checkPlayers() == true)
106 if (plist->getNoOfPlayers() <= 1)
108 if (plist->getActiveplayer()->isDead())
119 void NextTurnPbm::endTurn()
121 // Finish off the player and transfers the control to the start function
126 if (Playerlist::getActiveplayer() == Playerlist::getInstance()->getFirstLiving())
133 void NextTurnPbm::startTurn()
135 //this function is called before a player starts his turn. Some
136 //items you could imagine to be placed here: healing/building
137 //units, check for joining heroes...
140 Player* p = Playerlist::getActiveplayer();
144 //calculate upkeep and income
145 p->calculateUpkeep();
146 p->calculateIncome();
148 //if turnmode is set, create/heal armies at player's turn
152 //if (p->getType() != Player::NETWORKED)
155 Citylist::getInstance()->collectTaxes(p);
157 guint32 num_cities = Citylist::getInstance()->countCities(p);
158 p->getStacklist()->collectTaxes(p, num_cities);
160 //vector armies (needs to preceed city's next turn)
161 VectoredUnitlist::getInstance()->nextTurn(p);
163 //pay upkeep for existing stacks
164 p->getStacklist()->payUpkeep(p);
166 //reset moves, and heal stacks
167 p->getStacklist()->nextTurn();
170 Citylist::getInstance()->nextTurn(p);
173 p->calculateUpkeep();
175 QuestsManager::getInstance()->nextTurn(p);
178 void NextTurnPbm::finishTurn()
180 //Put everything that has to be done before the next player starts
181 //his turn here. E.g. one could clear some caches.
182 Player *p = Playerlist::getActiveplayer();
183 p->getFogMap()->smooth();
187 void NextTurnPbm::finishRound()
189 Playerlist *plist = Playerlist::getInstance();
190 //Put everything that has to be done when a new round starts in here.
191 //E.g. increase the round number in GameScenario. (this is done with
192 //the snextRound signal, but useful for an example).
196 //do this for all players at once
198 for (Playerlist::iterator it = plist->begin(); it != plist->end(); it++)
203 //collect monies from cities
204 Citylist::getInstance()->collectTaxes(*it);
206 guint32 num_cities = Citylist::getInstance()->countCities(*it);
207 (*it)->getStacklist()->collectTaxes((*it), num_cities);
209 //vector armies (needs to preceed city's next turn)
210 VectoredUnitlist::getInstance()->nextTurn(*it);
212 //pay for existing armies
213 (*it)->getStacklist()->payUpkeep(*it);
215 //reset, and heal armies
216 (*it)->getStacklist()->nextTurn();
219 Citylist::getInstance()->nextTurn(*it);
224 // heal the stacks in the ruins
225 Ruinlist* rl = Ruinlist::getInstance();
226 for (Ruinlist::iterator it = rl->begin(); it != rl->end(); it++)
228 Stack* keeper = (*it)->getOccupant();
235 plist->randomizeOrder();