1 // Copyright (C) 2003, 2004, 2005 Ulf Lorenz
2 // Copyright (C) 2007, 2008, 2009 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 "NextTurnHotseat.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 NextTurnHotseat::NextTurnHotseat(bool turnmode, bool random_turns)
41 :NextTurn(turnmode, random_turns)
43 continuing_turn = false;
45 Playerlist* plist = Playerlist::getInstance();
46 for (Playerlist::iterator i = plist->begin(); i != plist->end(); ++i) {
48 p->ending_turn.connect(sigc::mem_fun(this, &NextTurn::endTurn));
52 NextTurnHotseat::~NextTurnHotseat()
56 void NextTurnHotseat::start()
58 //We need the playerlist a lot, so maintain a copy of it.
60 //set first player as active if no active player exists
61 if (!Playerlist::getActiveplayer())
68 // do various start-up tasks
71 continuing_turn = false;
77 // inform everyone about the next turn
78 snextTurn.emit(Playerlist::getActiveplayer());
80 splayerStart.emit(Playerlist::getActiveplayer());
82 // let the player do his or her duties...
83 bool continue_loop = Playerlist::getActiveplayer()->startTurn();
84 if (!continue_loop || d_stop)
87 //Now do some cleanup at the end of the turn.
90 //...and initiate the next one.
93 //if it is the first player's turn now, a new round has started
94 if (Playerlist::getInstance()->getActiveplayer() ==
95 Playerlist::getInstance()->getFirstLiving())
98 if (Playerlist::getInstance()->checkPlayers() == true)
100 if (Playerlist::getInstance()->getNoOfPlayers() <= 1)
102 if (Playerlist::getActiveplayer()->isDead())
111 void NextTurnHotseat::endTurn()
113 // Finish off the player and transfers the control to the start function
116 if (Playerlist::getInstance()->checkPlayers() == true)
120 if (Playerlist::getInstance()->getNoOfPlayers() <= 1)
125 //this is problematic when the first player dies.
126 //end the turn if all living players have ended their turn.
127 if (Playerlist::getInstance()->isEndOfRound() == true)
136 void NextTurnHotseat::startTurn()
138 //this function is called before a player starts his turn. Some
139 //items you could imagine to be placed here: healing/building
140 //units, check for joining heroes...
143 Player* p = Playerlist::getActiveplayer();
147 //calculate upkeep and income
148 p->calculateUpkeep();
149 p->calculateIncome();
151 //if turnmode is set, create/heal armies at player's turn
155 //if (p->getType() != Player::NETWORKED)
158 Citylist::getInstance()->collectTaxes(p);
160 guint32 num_cities = Citylist::getInstance()->countCities(p);
161 p->getStacklist()->collectTaxes(p, num_cities);
163 //vector armies (needs to preceed city's next turn)
164 VectoredUnitlist::getInstance()->nextTurn(p);
166 //pay upkeep for existing stacks
167 p->getStacklist()->payUpkeep(p);
169 //reset moves, and heal stacks
170 p->getStacklist()->nextTurn();
173 Citylist::getInstance()->nextTurn(p);
176 p->calculateUpkeep();
178 QuestsManager::getInstance()->nextTurn(p);
181 void NextTurnHotseat::finishTurn()
183 //Put everything that has to be done before the next player starts
184 //his turn here. E.g. one could clear some caches.
185 Player *p = Playerlist::getActiveplayer();
186 p->getFogMap()->smooth();
190 void NextTurnHotseat::finishRound()
192 Playerlist *plist = Playerlist::getInstance();
193 //Put everything that has to be done when a new round starts in here.
194 //E.g. increase the round number in GameScenario. (this is done with
195 //the snextRound signal, but useful for an example).
199 //do this for all players at once
201 for (Playerlist::iterator it = plist->begin(); it != plist->end(); it++)
206 //collect monies from cities
207 Citylist::getInstance()->collectTaxes(*it);
209 guint32 num_cities = Citylist::getInstance()->countCities(*it);
210 (*it)->getStacklist()->collectTaxes((*it), num_cities);
212 //vector armies (needs to preceed city's next turn)
213 VectoredUnitlist::getInstance()->nextTurn(*it);
215 //pay for existing armies
216 (*it)->getStacklist()->payUpkeep(*it);
218 //reset, and heal armies
219 (*it)->getStacklist()->nextTurn();
222 Citylist::getInstance()->nextTurn(*it);
227 // heal the stacks in the ruins
228 Ruinlist* rl = Ruinlist::getInstance();
229 for (Ruinlist::iterator it = rl->begin(); it != rl->end(); it++)
231 Stack* keeper = (*it)->getOccupant();
238 plist->randomizeOrder();