initial commit, lordsawar source, slightly modified
[lordsawar] / src / player.cpp
1 // Copyright (C) 2000, 2001, 2002, 2003 Michael Bartl
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Ulf Lorenz
3 // Copyright (C) 2004, 2005 Andrea Paternesi
4 // Copyright (C) 2004 John Farrell
5 // Copyright (C) 2005 Bryan Duff
6 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
7 // Copyright (C) 2007, 2008 Ole Laursen
8 //
9 //  This program is free software; you can redistribute it and/or modify
10 //  it under the terms of the GNU General Public License as published by
11 //  the Free Software Foundation; either version 3 of the License, or
12 //  (at your option) any later version.
13 //
14 //  This program is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 //  GNU Library General Public License for more details.
18 //
19 //  You should have received a copy of the GNU General Public License
20 //  along with this program; if not, write to the Free Software
21 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
22 //  02110-1301, USA.
23
24 #include <stdlib.h>
25 #include <assert.h>
26 #include <algorithm>
27 #include <fstream>
28 #include <sstream>
29 #include <sigc++/functors/mem_fun.h>
30
31 #include "player.h"
32 #include "playerlist.h"
33 #include "stacklist.h"
34 #include "citylist.h"
35 #include "templelist.h"
36 #include "city.h"
37 #include "path.h"
38 #include "armysetlist.h"
39 #include "real_player.h"
40 #include "ai_dummy.h"
41 #include "ai_fast.h"
42 #include "ai_smart.h"
43 #include "network_player.h"
44 #include "GameMap.h"
45 #include "counter.h"
46 #include "army.h"
47 #include "hero.h"
48 #include "heroproto.h"
49 #include "herotemplates.h"
50 #include "Configuration.h"
51 #include "GameScenarioOptions.h"
52 #include "action.h"
53 #include "network-action.h"
54 #include "history.h"
55 #include "network-history.h"
56 #include "AI_Analysis.h"
57 #include "AI_Allocation.h"
58 #include "FogMap.h"
59 #include "QuestsManager.h"
60 #include "signpost.h"
61 #include "vectoredunit.h"
62 #include "ucompose.hpp"
63 #include "armyprodbase.h"
64 #include "Triumphs.h"
65 #include "Backpack.h"
66 #include "MapBackpack.h"
67 #include "PathCalculator.h"
68 #include "stacktile.h"
69 #include "templelist.h"
70 #include "temple.h"
71 #include "QCityOccupy.h"
72 #include "QCitySack.h"
73 #include "QCityRaze.h"
74 #include "QPillageGold.h"
75 #include "Quest.h"
76 #include "QKillHero.h"
77 #include "QEnemyArmies.h"
78 #include "QEnemyArmytype.h"
79 #include "callback-enums.h"
80
81 using namespace std;
82
83 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<flush<<endl;}
84 #define debug(x)
85
86 std::string Player::d_tag = "player";
87
88 Player::Player(string name, guint32 armyset, Gdk::Color color, int width,
89                int height, Type type, int player_no)
90     :d_color(color), d_name(name), d_armyset(armyset), d_gold(1000),
91     d_dead(false), d_immortal(false), d_type(type), d_upkeep(0), d_income(0),
92     d_observable(true), surrendered(false), abort_requested(false)
93 {
94     if (player_no != -1)
95         d_id = player_no;
96     else
97         d_id = fl_counter->getNextId();
98     d_stacklist = new Stacklist();
99     debug("type of " << d_name << " is " << type)
100         
101     d_fogmap = new FogMap(width, height);
102
103     //initial fight order is the order in which the armies appear
104     //in the default.xml file.
105     guint32 size = Armysetlist::getInstance()->getSize(d_armyset);
106     for (unsigned int i = 0; i < size; i++)
107     {
108       d_fight_order.push_back(i);
109     }
110
111     for (unsigned int i = 0 ; i < MAX_PLAYERS; i++)
112     {
113       d_diplomatic_state[i] = AT_PEACE;
114       d_diplomatic_proposal[i] = NO_PROPOSAL;
115       d_diplomatic_score[i] = DIPLOMACY_STARTING_SCORE;
116     }
117     d_diplomatic_rank = 0;
118     d_diplomatic_title = std::string("");
119
120     d_triumphs = new Triumphs();
121 }
122
123 Player::Player(const Player& player)
124     :d_color(player.d_color), d_name(player.d_name), d_armyset(player.d_armyset),
125     d_gold(player.d_gold), d_dead(player.d_dead), d_immortal(player.d_immortal),
126     d_type(player.d_type), d_id(player.d_id), 
127     d_fight_order(player.d_fight_order), d_upkeep(player.d_upkeep), 
128     d_income(player.d_income), d_observable(player.d_observable),
129     surrendered(player.surrendered),abort_requested(player.abort_requested)
130 {
131     // as the other player is propably dumped somehow, we need to deep copy
132     // everything.
133     d_stacklist = new Stacklist();
134     for (Stacklist::iterator it = player.d_stacklist->begin(); 
135          it != player.d_stacklist->end(); it++)
136     {
137         Stack* mine = new Stack(**it);
138         // change the stack's loyalty
139         mine->setPlayer(this);
140         d_stacklist->add(mine);
141     }
142
143     // copy actions
144     std::list<Action*>::const_iterator ait;
145     for (ait = player.d_actions.begin(); ait != player.d_actions.end(); ait++)
146         d_actions.push_back(Action::copy(*ait));
147
148     // copy events
149     std::list<History*>::const_iterator pit;
150     for (pit = player.d_history.begin(); pit != player.d_history.end(); pit++)
151         d_history.push_back(History::copy(*pit));
152
153     // copy fogmap; TBD
154     d_fogmap = new FogMap(*player.getFogMap());
155
156     // copy diplomatic states
157     for (unsigned int i = 0 ; i < MAX_PLAYERS; i++)
158       {
159         d_diplomatic_state[i] = player.d_diplomatic_state[i];
160         d_diplomatic_proposal[i] = player.d_diplomatic_proposal[i];
161         d_diplomatic_score[i] = player.d_diplomatic_score[i];
162       }
163     d_diplomatic_rank = player.d_diplomatic_rank;
164     d_diplomatic_title = player.d_diplomatic_title;
165
166     d_triumphs = new Triumphs(*player.getTriumphs());
167 }
168
169 Player::Player(XML_Helper* helper)
170     :d_stacklist(0), d_fogmap(0), surrendered(false), abort_requested(false)
171 {
172     helper->getData(d_id, "id");
173     helper->getData(d_name, "name");
174     helper->getData(d_gold, "gold");
175     helper->getData(d_dead, "dead");
176     helper->getData(d_immortal, "immortal");
177     std::string type_str;
178     helper->getData(type_str, "type");
179     d_type = playerTypeFromString(type_str);
180     helper->getData(d_upkeep, "upkeep");
181     helper->getData(d_income, "income");
182     helper->getData(d_color, "color");
183     helper->getData(d_armyset, "armyset");
184
185     // Read in Fight Order.  One ranking per army type.
186     std::string fight_order;
187     std::stringstream sfight_order;
188     guint32 val;
189     helper->getData(fight_order, "fight_order");
190     sfight_order.str(fight_order);
191     guint32 size = Armysetlist::getInstance()->getSize(d_armyset);
192     for (unsigned int i = 0; i < size; i++)
193     {
194             sfight_order >> val;
195             d_fight_order.push_back(val);
196     }
197
198     // Read in Diplomatic States.  One state per player.
199     std::string diplomatic_states;
200     std::stringstream sdiplomatic_states;
201     helper->getData(diplomatic_states, "diplomatic_states");
202     sdiplomatic_states.str(diplomatic_states);
203     for (unsigned int i = 0; i < MAX_PLAYERS; i++)
204     {
205             sdiplomatic_states >> val;
206             d_diplomatic_state[i] = DiplomaticState(val);
207     }
208
209     helper->getData(d_diplomatic_rank, "diplomatic_rank");
210     helper->getData(d_diplomatic_title, "diplomatic_title");
211
212     // Read in Diplomatic Proposals.  One proposal per player.
213     std::string diplomatic_proposals;
214     std::stringstream sdiplomatic_proposals;
215     helper->getData(diplomatic_proposals, "diplomatic_proposals");
216     sdiplomatic_proposals.str(diplomatic_proposals);
217     for (unsigned int i = 0; i < MAX_PLAYERS; i++)
218     {
219             sdiplomatic_proposals>> val;
220             d_diplomatic_proposal[i] = DiplomaticProposal(val);
221     }
222
223     // Read in Diplomatic Scores.  One score per player.
224     std::string diplomatic_scores;
225     std::stringstream sdiplomatic_scores;
226     helper->getData(diplomatic_scores, "diplomatic_scores");
227     sdiplomatic_scores.str(diplomatic_scores);
228     for (unsigned int i = 0; i < MAX_PLAYERS; i++)
229     {
230             sdiplomatic_scores >> val;
231             d_diplomatic_score[i] = val;
232     }
233     helper->getData(d_observable, "observable");
234
235     helper->registerTag(Action::d_tag, sigc::mem_fun(this, &Player::load));
236     helper->registerTag(History::d_tag, sigc::mem_fun(this, &Player::load));
237     helper->registerTag(Stacklist::d_tag, sigc::mem_fun(this, &Player::load));
238     helper->registerTag(FogMap::d_tag, sigc::mem_fun(this, &Player::load));
239     helper->registerTag(Triumphs::d_tag, sigc::mem_fun(this, &Player::load));
240
241 }
242
243 Player::~Player()
244 {
245     if (d_stacklist)
246     {
247         //d_stacklist->flClear();
248         delete d_stacklist;
249     }
250     if (d_fogmap)
251         delete d_fogmap;
252
253     clearActionlist();
254     clearHistorylist();
255     d_fight_order.clear();
256 }
257
258 Player* Player::create(std::string name, guint32 armyset, Gdk::Color color, int width, int height, Type type)
259 {
260   switch(type)
261   {
262   case HUMAN:
263     return (new RealPlayer(name, armyset, color, width, height));
264   case AI_FAST:
265     return (new AI_Fast(name, armyset, color, width, height));
266   case AI_DUMMY:
267     return (new AI_Dummy(name, armyset, color, width, height));
268   case AI_SMART:
269     return (new AI_Smart(name, armyset, color, width, height));
270   case NETWORKED:
271     return (new NetworkPlayer(name, armyset, color, width, height));
272   }
273
274   return 0;
275 }
276
277 Player* Player::create(Player* orig, Type type)
278 {
279     switch(type)
280     {
281         case HUMAN:
282             return new RealPlayer(*orig);
283         case AI_FAST:
284             return new AI_Fast(*orig);
285         case AI_DUMMY:
286             return new AI_Dummy(*orig);
287         case AI_SMART:
288             return new AI_Smart(*orig);
289         case NETWORKED:
290             return new NetworkPlayer(*orig);
291     }
292
293     return 0;
294 }
295
296 void Player::initTurn()
297 {
298   clearActionlist();
299   History_StartTurn* item = new History_StartTurn();
300   addHistory(item);
301   Action_InitTurn* action = new Action_InitTurn();
302   addAction(action);
303 }
304
305 void Player::setColor(Gdk::Color c)
306 {
307     d_color = c;
308 }
309
310 void Player::addGold(int gold)
311 {
312     d_gold += gold;
313     schangingStats.emit();
314 }
315
316 void Player::withdrawGold(int gold)
317 {
318     d_gold -= gold;
319     if (d_gold < 0)
320       d_gold = 0; /* bankrupt.  should we start turning off city production? */
321     schangingStats.emit();
322 }
323
324 std::string Player::getName(bool translate) const
325 {
326     if (translate)
327         return __(d_name);
328
329     return d_name;
330 }
331
332 void Player::dumpActionlist() const
333 {
334     for (list<Action*>::const_iterator it = d_actions.begin();
335         it != d_actions.end(); it++)
336     {
337         cerr <<(*it)->dump() << endl;
338     }    
339 }
340
341 void Player::dumpHistorylist() const
342 {
343     for (list<History*>::const_iterator it = d_history.begin();
344         it != d_history.end(); it++)
345     {
346         cerr <<(*it)->dump() << endl;
347     }    
348 }
349
350 void Player::clearActionlist()
351 {
352     for (list<Action*>::iterator it = d_actions.begin();
353         it != d_actions.end(); it++)
354     {
355       delete (*it);
356     }
357     d_actions.clear();
358 }
359
360 void Player::clearHistorylist()
361 {
362     for (list<History*>::iterator it = d_history.begin();
363         it != d_history.end(); it++)
364     {
365         delete (*it);
366     }
367     d_history.clear();
368 }
369
370 void Player::addStack(Stack* stack)
371 {
372     stack->setPlayer(this);
373     d_stacklist->add(stack);
374 }
375
376 bool Player::deleteStack(Stack* stack)
377 {
378   if (isComputer() == true)
379     {
380       AI_Analysis::deleteStack(stack->getId());
381       AI_Allocation::deleteStack(stack);
382     }
383     return d_stacklist->flRemove(stack);
384 }
385
386 void Player::kill()
387 {
388   doKill();
389 }
390
391 void Player::doKill()
392 {
393     if (d_immortal)
394         // ignore it
395         return;
396
397     d_observable = false;
398     History_PlayerVanquished* item;
399     item = new History_PlayerVanquished();
400     addHistory(item);
401
402     d_dead = true;
403     //drop the bags of stuff that the heroes might be carrying
404     std::list<Hero*> h = getHeroes();
405     for (std::list<Hero*>::iterator it = h.begin(); it != h.end(); it++)
406       {
407         Stack *s = d_stacklist->getArmyStackById((*it)->getId());
408         if (s)
409           stackDisband(s);
410       }
411     //get rid of all of the other stacks.
412     d_stacklist->flClear();
413
414     // Since in some cases the player can be killed rather innocently
415     // (using reactions), we also need to clear the player's traces in the
416     // single cities
417     Citylist* cl = Citylist::getInstance();
418     for (Citylist::iterator it = cl->begin(); it != cl->end(); it++)
419         if ((*it)->getOwner() == this && (*it)->isBurnt() == false)
420             Playerlist::getInstance()->getNeutral()->takeCityInPossession(*it);
421
422     d_diplomatic_rank = 0;
423     d_diplomatic_title = std::string("");
424 }
425
426 bool Player::save(XML_Helper* helper) const
427 {
428     bool retval = true;
429
430     retval &= helper->saveData("id", d_id);
431     retval &= helper->saveData("name", d_name);
432     retval &= helper->saveData("color", d_color);
433     retval &= helper->saveData("armyset", d_armyset);
434     retval &= helper->saveData("gold", d_gold);
435     retval &= helper->saveData("dead", d_dead);
436     retval &= helper->saveData("immortal", d_immortal);
437     std::string type_str = playerTypeToString(Player::Type(d_type));
438     retval &= helper->saveData("type", type_str);
439     debug("type of " << d_name << " is " << d_type)
440     retval &= helper->saveData("upkeep", d_upkeep);
441     retval &= helper->saveData("income", d_income);
442
443     // save the fight order, one ranking per army type
444     std::stringstream fight_order;
445     for (std::list<guint32>::const_iterator it = d_fight_order.begin();
446          it != d_fight_order.end(); it++)
447       {
448         fight_order << (*it) << " ";
449       }
450     retval &= helper->saveData("fight_order", fight_order.str());
451
452     // save the diplomatic states, one state per player
453     std::stringstream diplomatic_states;
454     for (unsigned int i = 0; i < MAX_PLAYERS; i++)
455       {
456         diplomatic_states << d_diplomatic_state[i] << " ";
457       }
458     retval &= helper->saveData("diplomatic_states", diplomatic_states.str());
459
460     retval &= helper->saveData("diplomatic_rank", d_diplomatic_rank);
461     retval &= helper->saveData("diplomatic_title", d_diplomatic_title);
462
463     // save the diplomatic proposals, one proposal per player
464     std::stringstream diplomatic_proposals;
465     for (unsigned int i = 0; i < MAX_PLAYERS; i++)
466       {
467         diplomatic_proposals << d_diplomatic_proposal[i] << " ";
468       }
469     retval &= helper->saveData("diplomatic_proposals", 
470                                diplomatic_proposals.str());
471
472     // save the diplomatic scores, one score per player
473     std::stringstream diplomatic_scores;
474     for (unsigned int i = 0; i < MAX_PLAYERS; i++)
475       {
476         diplomatic_scores << d_diplomatic_score[i] << " ";
477       }
478     retval &= helper->saveData("diplomatic_scores", diplomatic_scores.str());
479
480     retval &= helper->saveData("observable", d_observable);
481
482     //save the actionlist
483     for (list<Action*>::const_iterator it = d_actions.begin();
484             it != d_actions.end(); it++)
485         retval &= (*it)->save(helper);
486     
487     //save the pasteventlist
488     for (list<History*>::const_iterator it = d_history.begin();
489             it != d_history.end(); it++)
490         retval &= (*it)->save(helper);
491
492     retval &= d_stacklist->save(helper);
493     retval &= d_fogmap->save(helper);
494     retval &= d_triumphs->save(helper);
495
496     return retval;
497 }
498
499 Player* Player::loadPlayer(XML_Helper* helper)
500 {
501     Type type;
502     std::string type_str;
503     helper->getData(type_str, "type");
504     type = playerTypeFromString(type_str);
505
506     switch (type)
507     {
508         case HUMAN:
509             return new RealPlayer(helper);
510         case AI_FAST:
511             return new AI_Fast(helper);
512         case AI_SMART:
513             return new AI_Smart(helper);
514         case AI_DUMMY:
515             return new AI_Dummy(helper);
516         case NETWORKED:
517             return new NetworkPlayer(helper);
518     }
519
520     return 0;
521 }
522
523 bool Player::load(string tag, XML_Helper* helper)
524 {
525     if (tag == Action::d_tag)
526     {
527         Action* action;
528         action = Action::handle_load(helper);
529         d_actions.push_back(action);
530     }
531     if (tag == History::d_tag)
532     {
533         History* history;
534         history = History::handle_load(helper);
535         d_history.push_back(history);
536     }
537
538     if (tag == Stacklist::d_tag)
539         d_stacklist = new Stacklist(helper);
540
541     if (tag == FogMap::d_tag)
542         d_fogmap = new FogMap(helper);
543
544     if (tag == Triumphs::d_tag)
545         d_triumphs = new Triumphs(helper);
546
547     return true;
548 }
549
550 void Player::addAction(Action *action)
551 {
552   d_actions.push_back(action);
553   NetworkAction *copy = new NetworkAction(action, getId());
554   acting.emit(copy);
555   //free'd in game-server
556 }
557
558 void Player::addHistory(History *history)
559 {
560   d_history.push_back(history);
561   NetworkHistory *copy = new NetworkHistory(history, getId());
562   history_written.emit(copy);
563   //free'd in game-server
564 }
565
566
567 guint32 Player::getScore() const
568 {
569   //go get our last published score in the history
570   guint32 score = 0;
571   std::list<History*>::const_iterator it = d_history.begin();
572   for (; it != d_history.end(); it++)
573     {
574       if ((*it)->getType() == History::SCORE)
575         score = static_cast<History_Score*>(*it)->getScore();
576     }
577   return score;
578 }
579
580 void Player::calculateUpkeep()
581 {
582     d_upkeep = 0;
583     Stacklist *sl = getStacklist();
584     for (Stacklist::iterator i = sl->begin(), iend = sl->end(); i != iend; ++i)
585       d_upkeep += (*i)->getUpkeep();
586 }
587
588 void Player::calculateIncome()
589 {
590     d_income = 0;
591     Citylist *cl = Citylist::getInstance();
592     for (Citylist::iterator i = cl->begin(), iend = cl->end(); i != iend; ++i)
593       {
594         if ((*i)->getOwner() == this)
595           d_income += (*i)->getGold();
596       }
597 }
598
599 void Player::doSetFightOrder(std::list<guint32> order)
600 {
601   d_fight_order = order;
602 }
603
604 void Player::setFightOrder(std::list<guint32> order) 
605 {
606   doSetFightOrder(order);
607   
608   Action_FightOrder * item = new Action_FightOrder();
609   item->fillData(order);
610   addAction(item);
611 }
612
613 bool Player::doStackSplitArmy(Stack *s, Army *a, Stack *& new_stack)
614 {
615   new_stack = s->splitArmy(a);
616   if (new_stack != NULL)
617     {
618       addStack(new_stack);
619       return true;
620     }
621   return false;
622 }
623
624
625 bool Player::doStackSplitArmies(Stack *stack, std::list<guint32> armies,
626                                 Stack *& new_stack)
627 {
628   new_stack = stack->splitArmies(armies);
629   if (new_stack != NULL)
630     {
631       addStack(new_stack);
632       return true;
633     }
634   return false;
635 }
636
637 Stack *Player::stackSplitArmies(Stack *stack, std::list<guint32> armies)
638 {
639   Stack *new_stack = NULL;
640   bool retval = doStackSplitArmies(stack, armies, new_stack);
641   if (retval == true)
642     {
643       Action_Split* item = new Action_Split();
644       item->fillData(stack, new_stack);
645       addAction(item);
646     }
647   return new_stack;
648 }
649
650 Stack *Player::stackSplitArmy(Stack *stack, Army *a)
651 {
652   Stack *new_stack = NULL;
653   bool retval = doStackSplitArmy(stack, a, new_stack);
654   if (retval == true)
655     {
656       Action_Split* item = new Action_Split();
657       item->fillData(stack, new_stack);
658       addAction(item);
659     }
660   return new_stack;
661 }
662
663 void Player::doStackJoin(Stack* receiver, Stack* joining)
664 {
665     receiver->join(joining);
666    deleteStack(joining); 
667     //d_stacklist->flRemove(joining);
668     
669     d_stacklist->setActivestack(receiver);
670 }
671
672 bool Player::stackJoin(Stack* receiver, Stack* joining)
673 {
674
675     if ((receiver == 0) || (joining == 0))
676         return false;
677     debug("Player::stackJoin("<<receiver->getId()<<","<<joining->getId()<<")");
678
679     assert (receiver->getPos() == joining->getPos());
680     if (GameMap::canJoin(joining, receiver) == false)
681       return false;
682     
683     Action_Join* item = new Action_Join();
684     item->fillData(receiver, joining);
685     addAction(item);
686
687     doStackJoin(receiver, joining);
688  
689     supdatingStack.emit(0);
690     return true;
691 }
692
693 bool Player::stackSplitAndMove(Stack* s, Stack *& new_stack)
694 {
695   if (s->hasPath() == false)
696     return false;
697   Vector<int> pos = s->getLastReachablePointInPath();
698   if (pos == Vector<int>(-1,-1))
699     return false;
700   Stack *join = GameMap::getFriendlyStack(pos);
701   if (join)
702     return stackSplitAndMoveToJoin(s, join, new_stack);
703   else
704     return stackSplitAndMoveToAttack(s, new_stack);
705 }
706
707 bool Player::stackSplitAndMoveToJoin(Stack* s, Stack *join, Stack *& new_stack)
708 {
709   //the stack can't get there, but maybe part of the stack can.
710   if (s->hasPath() == false)
711     return false;
712
713   std::list<guint32> ids;
714   ids = s->determineReachableArmies(s->getLastPointInPath());
715   if (ids.size() == 0)
716     return false;
717   //if they're all reachable and we can join, just move them
718   if (ids.size() == s->size() && GameMap::canJoin(s, join) == true)
719     return stackMove(s);
720
721   //let's take who we can fit.
722   if (ids.size() > join->getMaxArmiesToJoin())
723     {
724       int diff = ids.size() - join->getMaxArmiesToJoin();
725       for (int i = 0; i < diff; i++)
726         ids.pop_front();
727     }
728
729   if (ids.size() == 0)
730     return false;
731   //okay, ids.size armies can make the move.  but can that tile accept it?
732   new_stack = stackSplitArmies(s, ids);
733   if (new_stack)
734     {
735       setActivestack(new_stack);
736       return stackMove(new_stack);
737       //if (getActivestack() != NULL)
738         //GameMap::groupStacks(new_stack);
739     }
740   return false;
741 }
742
743 bool Player::stackSplitAndMoveToAttack(Stack* s, Stack *& new_stack)
744 {
745   //the stack can't get there, but maybe part of the stack can.
746   if (s->getPath()->empty())
747     return false;
748
749   std::list<guint32> ids;
750   ids = s->determineReachableArmies(s->getLastPointInPath());
751   if (ids.size() == 0)
752     return false;
753   if (ids.size() == s->size())
754     return stackMove(s);
755
756   new_stack = stackSplitArmies(s, ids);
757   if (new_stack)
758     {
759       setActivestack(new_stack);
760       return stackMove(new_stack);
761     }
762   return false;
763 }
764
765 bool Player::stackMove(Stack* s)
766 {
767     debug("Player::stackMove(Stack*)")
768
769     if (s->getPath()->empty())
770     {
771         return false;
772     }
773
774     MoveResult *result = stackMove(s, s->getLastPointInPath(), true);
775     bool ret = result->didSomething();//result->moveSucceeded();
776     delete result;
777     result = 0;
778     return ret;
779 }
780
781     
782 bool Player::nextStepOnEnemyStackOrCity(Stack *s) const
783 {
784   Vector<int> dest = s->getFirstPointInPath();
785   if (dest != Vector<int>(-1,-1))
786     {
787       if (GameMap::getEnemyStack(dest))
788         return true;
789       if (GameMap::getEnemyCity(dest))
790         return true;
791     }
792   return false;
793 }
794
795 MoveResult *Player::stackMove(Stack* s, Vector<int> dest)
796 {
797   if (dest == Vector<int>(-1,-1))
798     return stackMove(s, dest, true);
799   else
800     return stackMove(s, dest, false);
801 }
802
803 MoveResult *Player::stackMove(Stack* s, Vector<int> dest, bool follow)
804 {
805
806     debug("Player::stack_move()");
807     //if follow is set to true, follow an already calculated way, else
808     //calculate it here
809     if (!follow)
810     {
811         s->getPath()->calculate(s, dest);
812     }
813
814     if (s->getPath()->empty())
815       {
816         MoveResult *result = new MoveResult;
817         result->setReachedEndOfPath(true);
818         return result;
819       }
820
821     int stepCount = 0;
822     int moves_left = s->getPath()->getMovesExhaustedAtPoint();
823     while (1)
824       {
825         if (abortRequested())
826           {
827             MoveResult *result = new MoveResult;
828             result->fillData(s, stepCount);
829             result->setMoveAborted(true);
830             return result;
831           }
832         if (s->getPath()->size() <= 1)
833           break;
834         if (nextStepOnEnemyStackOrCity(s) == true)
835           break;
836
837         bool step = false;
838         step = stackMoveOneStep(s);
839         if (!step)
840           step = stackMoveOneStepOverTooLargeFriendlyStacks(s);
841         if (step)
842           {
843             stepCount++;
844             moves_left--;
845             supdatingStack.emit(0);
846             if (moves_left == 1)
847               break;
848           }
849         else
850           break;
851       }
852
853     //the idea here is that we're one move away from our destination.
854     //but in some cases we've already reached the end of the path
855     //because a fight has to happen.
856
857     //did we jump over a too large friendly stack to an enemy stack or city?
858   
859     //alright, we've walked up to the last place in the path.
860     if (s->getPath()->size() >= 1 && s->enoughMoves())
861     //now look for fight targets, joins etc.
862     {
863     
864         Vector<int> pos = s->getFirstPointInPath();
865         City* city = GameMap::getCity(pos);
866         Stack* target =GameMap::getStack(pos);
867
868
869         //first fight_city to avoid ambiguity with fight_army
870         if (city && (city->getOwner() != this) && (!city->isBurnt()))
871           {
872             bool treachery = false;
873             if (this->getDiplomaticState (city->getOwner()) != AT_WAR)
874               {
875                 if (streacheryStack.emit (s, city->getOwner(), 
876                                           city->getPos()) == false)
877                   {
878                     //we decided not to be treacherous
879                     s->getPath()->clear();
880                     MoveResult *moveResult = new MoveResult;
881                     moveResult->setConsideredTreachery(true);
882                     moveResult->fillData(s, stepCount);
883                     return moveResult;
884                   }
885                 else
886                   treachery = true;
887               }
888             MoveResult *moveResult = new MoveResult;
889             moveResult->setTreachery(treachery);
890             moveResult->setConsideredTreachery(treachery);
891             if (stackMoveOneStep(s))
892               {
893                 stepCount++;
894               }
895             else
896               {
897                 moveResult->fillData(s, stepCount);
898                 shaltedStack.emit(s);
899                 return moveResult;
900               }
901
902             moveResult->fillData(s, stepCount);
903             Fight::Result result;
904             vector<Stack*> def_in_city = city->getDefenders();
905             if (!def_in_city.empty())
906             {
907                 // This is a hack to circumvent the limitations of stackFight.
908                 if (!target)
909                   target = def_in_city[0];
910  
911                 result = stackFight(&s, &target);
912             }
913             else
914                 result = Fight::ATTACKER_WON;
915
916             moveResult->setFightResult(result);
917
918             // We may only take the city if we have defeated all defenders
919             if (result == Fight::ATTACKER_WON)
920             {
921                 adjustDiplomacyFromConqueringCity(city);
922                 conquerCity(city, s);
923                 invadeCity(city); //let AIs determine what to do with city
924                 shaltedStack.emit(s);
925             }
926             
927             cityfight_finished(city, result);
928             supdatingStack.emit(0);
929             
930             return moveResult;
931         }
932         
933         //another friendly stack => share the tile if we're human
934         else if (target && target->getOwner() == this /*&& 
935                  getType() == Player::HUMAN*/)
936           {
937             MoveResult *moveResult = new MoveResult;
938             bool moved = false;
939             if (stackMoveOneStep(s))
940               {
941                 moved = true;
942                 stepCount++;
943               }
944             else
945               moveResult->setTooLargeStackInTheWay(true);
946               
947             supdatingStack.emit(0);
948             shaltedStack.emit(d_stacklist->getActivestack());
949     
950             moveResult->fillData(s, stepCount);
951             return moveResult;
952          }
953         
954         //enemy stack => fight
955         else if (target)
956         {
957           bool treachery = false;
958           if (this->getDiplomaticState (target->getOwner()) == AT_PEACE)
959             {
960               if (streacheryStack.emit (s, target->getOwner(), 
961                                         target->getPos()) == false)
962                 {
963                   s->getPath()->clear();
964                   MoveResult *moveResult = new MoveResult;
965                   moveResult->setConsideredTreachery(true);
966                   moveResult->fillData(s, stepCount);
967                   return moveResult;
968                 }
969               else
970                 treachery = true;
971             }
972             MoveResult *moveResult = new MoveResult;
973             moveResult->setTreachery(treachery);
974             moveResult->setConsideredTreachery(treachery);
975         
976             moveResult->fillData(s, stepCount);
977             Fight::Result result = stackFight(&s, &target);
978             moveResult->setFightResult(result);
979             if (!target)
980               {
981                 if (stackMoveOneStep(s))
982                   stepCount++;
983                 moveResult->fillData(s, stepCount);
984               }
985             
986             supdatingStack.emit(0);
987             if (result == Fight::ATTACKER_WON)
988               shaltedStack.emit(s);
989             return moveResult;
990         }
991         
992         //else
993         if (stackMoveOneStep(s))
994           {
995             supdatingStack.emit(0);
996             stepCount++;
997           }
998
999         shaltedStack.emit(s);
1000     
1001         MoveResult *moveResult = new MoveResult;
1002         moveResult->fillData(s, stepCount);
1003         return moveResult;
1004     }
1005     else if (s->getPath()->size() >= 1 && s->enoughMoves() == false)
1006     {
1007     
1008         MoveResult *moveResult = new MoveResult;
1009         moveResult->fillData(s, stepCount);
1010       /* if we can't attack a city, don't remember it in the stack's path. */
1011         Vector<int> pos = s->getFirstPointInPath();
1012         City* city = GameMap::getCity(pos);
1013         if (city && city->getOwner() != this)
1014           s->clearPath();
1015     
1016         return moveResult;
1017     }
1018
1019     MoveResult *moveResult = new MoveResult;
1020     moveResult->setStepCount(stepCount);
1021     return moveResult;
1022 }
1023
1024            
1025 bool Player::stackMoveOneStepOverTooLargeFriendlyStacks(Stack *s)
1026 {
1027   if (!s)
1028     return false;
1029
1030   if (!s->enoughMoves())
1031     return false;
1032
1033   if (s->getPath()->size() <= 1)
1034     return false;
1035
1036   Vector<int> dest = s->getFirstPointInPath();
1037   Stack *another_stack = GameMap::getStack(dest);
1038   if (!another_stack)
1039     return false;
1040
1041   if (another_stack->getOwner() != s->getOwner())
1042     return false;
1043
1044   if (d_stacklist->canJumpOverTooLargeStack(s) == false)
1045     return false;
1046
1047   Action_Move* item = new Action_Move();
1048   item->fillData(s, dest);
1049   addAction(item);
1050
1051   s->moveOneStep(true);
1052   return true;
1053 }
1054
1055 bool Player::stackMoveOneStep(Stack* s)
1056 {
1057   if (!s)
1058     return false;
1059
1060   sbusy.emit();
1061
1062   if (!s->enoughMoves())
1063     return false;
1064
1065   Vector<int> dest = s->getFirstPointInPath();
1066   
1067   Stack *another_stack = GameMap::getStack(dest);
1068   if (another_stack)
1069     {
1070       if (another_stack->getOwner() == s->getOwner())
1071         {
1072           if (GameMap::canJoin(s,another_stack) == false)
1073             return false;
1074         }
1075       else
1076         {
1077           //if we're attacking, then jump onto the square with the enemy.
1078           if (s->getPath()->size() != 1)
1079             return false;
1080         }
1081
1082     }
1083   Action_Move* item = new Action_Move();
1084   item->fillData(s, dest);
1085   addAction(item);
1086
1087   s->moveOneStep();
1088
1089   return true;
1090 }
1091
1092 void Player::cleanupAfterFight(std::list<Stack*> &attackers,
1093                                std::list<Stack*> &defenders)
1094 {
1095   // get attacker and defender heroes and more...
1096   std::vector<guint32> attackerHeroes, defenderHeroes;
1097     
1098   getHeroes(attackers, attackerHeroes);
1099   getHeroes(defenders, defenderHeroes);
1100
1101   // here we calculate also the total XP to add when a player have a battle
1102   // clear dead defenders
1103   debug("clean dead defenders");
1104   double defender_xp = removeDeadArmies(defenders, attackerHeroes);
1105
1106   // and dead attackers
1107   debug("clean dead attackers");
1108   double attacker_xp = removeDeadArmies(attackers, defenderHeroes);
1109
1110   debug("after fight: attackers empty? " << attackers.empty()
1111         << "(" << attackers.size() << ")");
1112
1113   if (!attackers.empty() && defender_xp != 0)
1114     updateArmyValues(attackers, defender_xp);
1115     
1116   if (attacker_xp != 0)
1117     updateArmyValues(defenders, attacker_xp);
1118
1119   supdatingStack.emit(0);
1120 }
1121
1122 Fight::Result Player::stackFight(Stack** attacker, Stack** defender) 
1123 {
1124     debug("stackFight: player = " << getName()<<" at position "
1125           <<(*defender)->getPos().x<<","<<(*defender)->getPos().y);
1126
1127     // save the defender's player for future use
1128     Player* pd = (*defender)->getOwner();
1129
1130     // I suppose, this should be always true, but one can never be sure
1131     bool attacker_active = *attacker == d_stacklist->getActivestack();
1132
1133     Fight fight(*attacker, *defender);
1134     fight.battle(GameScenarioOptions::s_intense_combat);
1135     
1136     fight_started.emit(fight);
1137     // cleanup
1138     
1139     // add a fight item about the combat
1140     Action_Fight* item = new Action_Fight();
1141     item->fillData(&fight);
1142     addAction(item);
1143
1144     std::list<Stack *> attackers = fight.getAttackers(),
1145       defenders = fight.getDefenders();
1146
1147     cleanupAfterFight(attackers, defenders);
1148     
1149     // Set the attacker and defender stack to 0 if neccessary. This is a great
1150     // help for the functions calling stackFight (e.g. if a stack attacks
1151     // another stack and destroys it without winning the battle, it may take the
1152     // position of this stack)
1153
1154     // First, the attacker...
1155     bool exists =
1156         std::find(d_stacklist->begin(), d_stacklist->end(), *attacker)
1157         != d_stacklist->end();
1158     
1159     if (!exists)
1160     {
1161         (*attacker) = 0;
1162         if (attacker_active)
1163             d_stacklist->setActivestack(0);
1164     }
1165
1166     // ...then the defender.
1167     exists = false;
1168     if (pd)
1169       exists = 
1170         std::find(pd->getStacklist()->begin(), pd->getStacklist()->end(), 
1171                   *defender) != pd->getStacklist()->end();
1172     else
1173         exists = true;
1174     if (!exists)
1175         (*defender) = 0;
1176
1177     return fight.getResult();
1178 }
1179
1180 /*
1181  *
1182  * To help factor in the advantage of hero experience/strength and 
1183  * ruin-monster strength as well as the stack strength, I think you'll 
1184  * find it'll be easier to calculate in terms of the odds of failure [than
1185  * the odds of success].  A new hero (minimum strength) with nothing in 
1186  * the stack to help him might have 10-20% odds of failure at a wimpy ruin.
1187  * The same novice hero facing a dragon in the ruin might have 50% odds of 
1188  * failure.  So a rule of thumb would be to start with a 25% chance of
1189  * failure.  The odds would be doubled by the worst monster and halved by 
1190  * the easiest.  I agree that a strength-9 hero with 8 in the stack should i
1191  * definitely be at 99%.  A reasonable formula might be:
1192  *
1193  * OddsOfFailure = BaseOdds * MonsterFactor * StackFactor * HeroFactor,
1194  *
1195  * with
1196  *        BaseOdds = 0.10
1197  * and
1198  *        MonsterFactor = 2, 1 or 0.5 depending on hard vs. easy
1199  * and
1200  *        StackFactor = (9 - SizeOfStack)/8,
1201  * and
1202  *        HeroFactor = (10-StrengthOfHero)/5.
1203  */
1204 Fight::Result ruinfight (Stack **attacker, Stack **defender)
1205 {
1206   Stack *loser;
1207   Fight::Result result;
1208   guint32 hero_strength, monster_strength;
1209   hero_strength = (*attacker)->getFirstHero()->getStat(Army::STRENGTH, true);
1210   monster_strength = (*defender)->getStrongestArmy()->getStat(Army::STRENGTH, true);
1211   float base_factor = 0.28;
1212   float stack_factor = ((float)(MAX_STACK_SIZE + 1) - (*attacker)->size()) / (float)MAX_STACK_SIZE;
1213   float hero_factor = (10.0 - hero_strength) / 5.0;
1214   float monster_factor;
1215   if (monster_strength >= 8)
1216     monster_factor = 2.0;
1217   else if (monster_strength >= 6)
1218     monster_factor = 1.0;
1219   else
1220     monster_factor = 0.5;
1221   float fail = base_factor * monster_factor * stack_factor * hero_factor;
1222
1223   if (rand() % 100 > (int)(fail * 100.0))
1224     {
1225       result = Fight::ATTACKER_WON;
1226       loser = *defender;
1227       for (Stack::iterator sit = loser->begin(); sit != loser->end();)
1228         {
1229           (*sit)->setHP (0);
1230           sit++;
1231         }
1232     }
1233   else
1234     {
1235       result = Fight::DEFENDER_WON;
1236       loser = *attacker;
1237       loser->getFirstHero()->setHP(0); /* only the hero dies */
1238     }
1239         
1240   return result;
1241 }
1242
1243 Fight::Result Player::stackRuinFight (Stack **attacker, Stack **defender)
1244 {
1245     Fight::Result result = Fight::DRAW;
1246     if (*defender == NULL)
1247       return Fight::ATTACKER_WON;
1248     debug("stackRuinFight: player = " << getName()<<" at position "
1249           <<(*defender)->getPos().x<<","<<(*defender)->getPos().y);
1250
1251     ruinfight_started.emit(*attacker, *defender);
1252     result = ruinfight (attacker, defender);
1253     ruinfight_finished.emit(result);
1254
1255     // cleanup
1256     
1257     // add a ruin fight item about the combat
1258     //Action_RuinFight* item = new Action_RuinFight();
1259     //item->fillData(*attacker, *defender, result);
1260     //addAction(item);
1261     /* FIXME: do we need an Action_RuinFight? */
1262
1263     // get attacker and defender heroes and more...
1264     std::list<Stack*> attackers;
1265     attackers.push_back(*attacker);
1266     std::list<Stack*> defenders;
1267     defenders.push_back(*defender);
1268
1269     cleanupAfterFight(attackers, defenders);
1270
1271     return result;
1272 }
1273
1274 bool Player::treachery (Stack *stack, Player *player, Vector <int> pos)
1275 {
1276   return streachery.emit(stack, player, pos);
1277 }
1278
1279 Reward* Player::stackSearchRuin(Stack* s, Ruin* r)
1280 {
1281   Reward *retReward = NULL;
1282   debug("Player::stack_search_ruin");
1283
1284   //throw out impossible actions
1285   if ((s->getPos().x != r->getPos().x) ||
1286       (s->getPos().y != r->getPos().y))
1287   {
1288     cerr <<  "Error: searching stack and ruin to be searched not on same position\n" ;
1289     exit(-1);
1290   }
1291
1292   if (r->isSearched())
1293     return NULL;
1294
1295   // start the action item
1296   Action_Ruin* item = new Action_Ruin();
1297   item->fillData(r, s);
1298
1299   Stack* keeper = r->getOccupant();
1300
1301   if (keeper)
1302   {
1303     stackRuinFight(&s, &keeper);
1304
1305     // did the explorer not win?
1306     if (keeper && !keeper->empty())
1307     {
1308       item->setSearched(false);
1309       addAction(item);
1310
1311       return NULL;
1312     }
1313
1314     r->setOccupant(0);
1315     if (keeper)
1316       delete keeper;
1317   }
1318
1319   if (r->hasSage())
1320   {
1321     History_FoundSage* history = new History_FoundSage();
1322     history->fillData(dynamic_cast<Hero *>(s->getFirstHero()));
1323     addHistory(history);
1324   }
1325   else
1326   {
1327     if (r->getReward() == NULL)
1328       r->populateWithRandomReward();
1329   }
1330
1331   retReward = r->getReward();
1332
1333   r->setSearched(true);
1334   r->setOwner(s->getOwner());
1335
1336   item->setSearched(true);
1337   addAction(item);
1338
1339   History_HeroRuinExplored *history_item = new History_HeroRuinExplored();
1340   history_item->fillData(dynamic_cast<Hero*>(s->getFirstHero()), r);
1341   addHistory(history_item);
1342
1343   supdatingStack.emit(0);
1344   return retReward;
1345 }
1346
1347 int Player::doStackVisitTemple(Stack *s, Temple *t)
1348 {
1349   // you have your stack blessed (+1 strength)
1350   int count = s->bless();
1351
1352   supdatingStack.emit(0);
1353   
1354   return count;
1355 }
1356
1357 int Player::stackVisitTemple(Stack* s, Temple* t)
1358 {
1359   debug("Player::stackVisitTemple");
1360
1361   assert(s && t->getPos().x == s->getPos().x && t->getPos().y == s->getPos().y);
1362
1363   Action_Temple* item = new Action_Temple();
1364   item->fillData(t, s);
1365   addAction(item);
1366   
1367   return doStackVisitTemple(s, t);
1368 }
1369
1370 Quest* Player::doHeroGetQuest(Hero *hero, Temple* t, bool except_raze)
1371 {
1372   QuestsManager *qm = QuestsManager::getInstance();
1373
1374   std::vector<Quest*> quests = qm->getPlayerQuests(Playerlist::getActiveplayer());
1375   if (quests.size() > 0 && GameScenarioOptions::s_play_with_quests == GameParameters::ONE_QUEST_PER_PLAYER)
1376     return NULL;
1377
1378   Quest* q=0;
1379   if (hero)
1380     {
1381       q = qm->createNewQuest (hero->getId(), except_raze);
1382     }
1383
1384   // couldn't assign a quest for various reasons
1385   if (!q)
1386     return 0;
1387   return q;
1388 }
1389
1390 Quest* Player::heroGetQuest(Hero *hero, Temple* t, bool except_raze)
1391 {
1392   debug("Player::stackGetQuest")
1393
1394   Quest *q = doHeroGetQuest(hero, t, except_raze);
1395   if (q == NULL)
1396     return q;
1397
1398   // Now fill the action item
1399   Action_Quest* action = new Action_Quest();
1400   action->fillData(q);
1401   addAction(action);
1402
1403   // and record it for posterity
1404   History_HeroQuestStarted * history = new History_HeroQuestStarted();
1405   history->fillData(hero);
1406   addHistory(history);
1407   return q;
1408 }
1409
1410 float Player::stackFightAdvise(Stack* s, Vector<int> tile, 
1411                                bool intense_combat)
1412 {
1413   float percent = 0.0;
1414         
1415   City* city = GameMap::getCity(tile);
1416   Stack* target = GameMap::getEnemyStack(tile);
1417                 
1418   if (!target && city)
1419     {
1420       vector<Stack*> def_in_city = city->getDefenders();
1421       if (def_in_city.empty())
1422         return 100.0;
1423       target = def_in_city[0];
1424     }
1425
1426   //what chance is there that stack will defeat defenders?
1427     
1428   for (unsigned int i = 0; i < 100; i++)
1429     {
1430       Fight fight(s, target, Fight::FOR_KICKS);
1431       fight.battle(intense_combat);
1432       if (fight.getResult() == Fight::ATTACKER_WON)
1433         percent += 1.0;
1434     }
1435
1436   advice_asked.emit(percent);
1437   return percent;
1438 }
1439
1440 void Player::adjustDiplomacyFromConqueringCity(City *city)
1441 {
1442   Player *defender = city->getOwner();
1443   
1444   // See if this is the last city for that player, and alter the 
1445   // diplomatic scores.
1446   if (Citylist::getInstance()->countCities(defender) == 1)
1447   {
1448     if (defender->getDiplomaticRank() < getDiplomaticRank())
1449       deteriorateDiplomaticRelationship (2);
1450     else if (defender->getDiplomaticRank() > getDiplomaticRank())
1451       improveDiplomaticRelationship (2, defender);
1452   }
1453 }
1454
1455 void Player::calculateLoot(Player *looted, guint32 &added, guint32 &subtracted)
1456 {
1457   Player *defender = looted;
1458   int gold = 0;
1459
1460   // if the attacked city isn't neutral, loot some gold
1461   if (defender != Playerlist::getInstance()->getNeutral())
1462   {
1463     Citylist *clist = Citylist::getInstance();
1464     int amt = (defender->getGold() / (2 * (clist->countCities (defender)+1)) * 2);
1465     // give (Enemy-Gold/(2Enemy-Cities)) to the attacker 
1466     // and then take away twice that from the defender.
1467     // the idea here is that some money is taken in the invasion
1468     // and other monies are lost forever
1469     // NOTE: +1 because the looted player just lost a city
1470     subtracted = amt;
1471     amt /= 2;
1472     added = amt;
1473     gold = amt;
1474   }
1475
1476   return;
1477 }
1478
1479 void Player::doConquerCity(City *city, Stack *stack)
1480 {
1481   takeCityInPossession(city);
1482   
1483   History_CityWon *item = new History_CityWon();
1484   item->fillData(city);
1485   addHistory(item);
1486   if (stack->hasHero())
1487   {
1488     History_HeroCityWon *another = new History_HeroCityWon();
1489     Hero *hero = dynamic_cast<Hero *>(stack->getFirstHero());
1490     another->fillData(hero, city);
1491     addHistory(another);
1492   }
1493 }
1494
1495 void Player::conquerCity(City *city, Stack *stack)
1496 {
1497   
1498   Action_ConquerCity *action = new Action_ConquerCity();
1499   action->fillData(city, stack);
1500   addAction(action);
1501
1502   Player *looted = city->getOwner();
1503   doConquerCity(city, stack);
1504   if (getType() != Player::NETWORKED)
1505     lootCity(city, looted);
1506 }
1507
1508 void Player::lootCity(City *city, Player *looted)
1509 {
1510   guint32 added = 0;
1511   guint32 subtracted = 0;
1512   calculateLoot(looted, added, subtracted);
1513   sinvadingCity.emit(city, added);
1514   doLootCity(looted, added, subtracted);
1515   Action_Loot *item = new Action_Loot();
1516   item->fillData(this, looted, added, subtracted);
1517   addAction(item);
1518   return;
1519 }
1520
1521 void Player::doLootCity(Player *looted, guint32 added, guint32 subtracted)
1522 {
1523   addGold(added);
1524   looted->withdrawGold(subtracted);
1525   return;
1526 }
1527
1528 void Player::takeCityInPossession(City* c)
1529 {
1530   c->conquer(this);
1531
1532   //set the production to the cheapest armytype
1533   c->setActiveProductionSlot(-1);
1534   if (c->getArmytype(0) != -1)
1535     c->setActiveProductionSlot(0);
1536
1537   supdatingCity.emit(c);
1538 }
1539
1540 void Player::doCityOccupy(City *c)
1541 {
1542   assert (c->getOwner() == this);
1543   
1544   soccupyingCity.emit(c, getActivestack());
1545   QuestsManager::getInstance()->cityOccupied(c, getActivestack());
1546 }
1547
1548 void Player::cityOccupy(City* c)
1549 {
1550   debug("cityOccupy");
1551   doCityOccupy(c);
1552
1553   Action_Occupy* item = new Action_Occupy();
1554   item->fillData(c);
1555   addAction(item);
1556 }
1557
1558 void Player::doCityPillage(City *c, int& gold, int* pillaged_army_type)
1559 {
1560   gold = 0;
1561   if (pillaged_army_type)
1562     *pillaged_army_type = -1;
1563   
1564   // get rid of the most expensive army type and trade it in for 
1565   // half it's cost
1566   // it is presumed that the last army type is the most expensive
1567
1568   if (c->getNoOfProductionBases() > 0)
1569     {
1570       unsigned int i;
1571       unsigned int max_cost = 0;
1572       int slot = -1;
1573       for (i = 0; i < c->getNoOfProductionBases(); i++)
1574         {
1575           const ArmyProdBase *a = c->getProductionBase(i);
1576           if (a != NULL)
1577             {
1578               if (a->getNewProductionCost() == 0)
1579                 {
1580                   slot = i;
1581                   break;
1582                 }
1583               if (a->getNewProductionCost() > max_cost)
1584                 {
1585                   max_cost = a->getNewProductionCost();
1586                   slot = i;
1587                 }
1588             }
1589         }
1590       if (slot > -1)
1591         {
1592           const ArmyProdBase *a = c->getProductionBase(slot);
1593           if (pillaged_army_type)
1594             *pillaged_army_type = a->getTypeId();
1595           if (a->getNewProductionCost() == 0)
1596             gold += 1500;
1597           else
1598             gold += a->getNewProductionCost() / 2;
1599           c->removeProductionBase(slot);
1600         }
1601   //*pillaged_army_type = 10;
1602   //gold = 300;
1603       addGold(gold);
1604       Stack *s = getActivestack();
1605       //printf ("%s emitting %p, %p, %d, %d\n", getName().c_str(), c, s, gold, *pillaged_army_type);
1606       spillagingCity.emit(c, s, gold, *pillaged_army_type);
1607       QuestsManager::getInstance()->cityPillaged(c, s, gold);
1608     }
1609
1610   //takeCityInPossession(c);
1611 }
1612
1613 void Player::cityPillage(City* c, int& gold, int* pillaged_army_type)
1614 {
1615   debug("Player::cityPillage");
1616   
1617   Action_Pillage* item = new Action_Pillage();
1618   item->fillData(c);
1619   addAction(item);
1620
1621   doCityPillage(c, gold, pillaged_army_type);
1622 }
1623
1624 void Player::doCitySack(City* c, int& gold, std::list<guint32> *sacked_types)
1625 {
1626   gold = 0;
1627   //trade in all of the army types except for one
1628   //presumes that the army types are listed in order of expensiveness
1629
1630   if (c->getNoOfProductionBases() > 1)
1631     {
1632       const ArmyProdBase *a;
1633       unsigned int i, max = 0;
1634       for (i = 0; i < c->getNoOfProductionBases(); i++)
1635         {
1636           a = c->getProductionBase(i);
1637           if (a)
1638             max++;
1639         }
1640
1641       i = c->getNoOfProductionBases() - 1;
1642       while (max > 1)
1643         {
1644           a = c->getProductionBase(i);
1645           if (a != NULL)
1646             {
1647               sacked_types->push_back(a->getTypeId());
1648               if (a->getNewProductionCost() == 0)
1649                 gold += 1500;
1650               else
1651                 gold += a->getNewProductionCost() / 2;
1652               c->removeProductionBase(i);
1653               max--;
1654             }
1655           i--;
1656         }
1657     }
1658
1659   addGold(gold);
1660   Stack *s = getActivestack();
1661   ssackingCity.emit(c, s, gold, *sacked_types);
1662   printf("notifying quests manager of city sacking!\n");
1663   QuestsManager::getInstance()->citySacked(c, s, gold);
1664   printf("done\n");
1665   //takeCityInPossession(c);
1666 }
1667
1668 void Player::citySack(City* c, int& gold, std::list<guint32> *sacked_types)
1669 {
1670   debug("Player::citySack");
1671
1672   Action_Sack* item = new Action_Sack();
1673   item->fillData(c);
1674   addAction(item);
1675
1676   doCitySack(c, gold, sacked_types);
1677 }
1678
1679 void Player::doCityRaze(City *c)
1680 {
1681   History_CityRazed* history = new History_CityRazed();
1682   history->fillData(c);
1683   addHistory(history);
1684
1685   c->conquer(this);
1686   c->setBurnt(true);
1687
1688   supdatingCity.emit(c);
1689
1690   srazingCity.emit(c, getActivestack());
1691   QuestsManager::getInstance()->cityRazed(c, getActivestack());
1692 }
1693
1694 void Player::cityRaze(City* c)
1695 {
1696   debug("Player::cityRaze");
1697
1698   Action_Raze* action = new Action_Raze();
1699   action->fillData(c);
1700   addAction(action);
1701
1702   doCityRaze(c);
1703 }
1704
1705 void Player::doCityBuyProduction(City* c, int slot, int type)
1706 {
1707   const Armysetlist* al = Armysetlist::getInstance();
1708   guint32 as = c->getOwner()->getArmyset();
1709
1710   c->removeProductionBase(slot);
1711   c->addProductionBase(slot, new ArmyProdBase(*al->getArmy(as, type)));
1712
1713   // and do the rest of the neccessary actions
1714   withdrawGold(al->getArmy(as, type)->getNewProductionCost());
1715 }
1716
1717 bool Player::cityBuyProduction(City* c, int slot, int type)
1718 {
1719   const Armysetlist* al = Armysetlist::getInstance();
1720   guint32 as = c->getOwner()->getArmyset();
1721
1722   // sort out unusual values (-1 is allowed and means "scrap production")
1723   if ((type <= -1) || (type >= (int)al->getSize(as)))
1724     return false;
1725
1726   // return if we don't have enough money
1727   if ((type != -1) && ((int)al->getArmy(as, type)->getNewProductionCost() > d_gold))
1728     return false;
1729
1730   // return if the city already has the production
1731   if (c->hasProductionBase(type, as))
1732     return false;
1733
1734   // can't put it in that slot
1735   if (slot >= (int)c->getMaxNoOfProductionBases())
1736     return false;
1737   
1738   Action_Buy* item = new Action_Buy();
1739   item->fillData(c, slot, al->getArmy(as, type));
1740   addAction(item);
1741
1742   doCityBuyProduction(c, slot, type);
1743
1744   return true;
1745 }
1746
1747 void Player::doCityChangeProduction(City* c, int slot)
1748 {
1749   c->setActiveProductionSlot(slot);
1750 }
1751
1752 bool Player::cityChangeProduction(City* c, int slot)
1753 {
1754   doCityChangeProduction(c, slot);
1755   
1756   Action_Production* item = new Action_Production();
1757   item->fillData(c, slot);
1758   addAction(item);
1759
1760   return true;
1761 }
1762
1763 void Player::doGiveReward(Stack *s, Reward *reward)
1764 {
1765   switch (reward->getType())
1766     {
1767     case Reward::GOLD:
1768       addGold(dynamic_cast<Reward_Gold*>(reward)->getGold());
1769       break;
1770     case Reward::ALLIES:
1771         {
1772           const ArmyProto *a = dynamic_cast<Reward_Allies*>(reward)->getArmy();
1773
1774           Reward_Allies::addAllies(s->getOwner(), s->getPos(), a,
1775                              dynamic_cast<Reward_Allies*>(reward)->getNoOfAllies());
1776   
1777         }
1778       break;
1779     case Reward::ITEM:
1780       static_cast<Hero*>(s->getFirstHero())->getBackpack()->addToBackpack
1781         (dynamic_cast<Reward_Item*>(reward)->getItem());
1782       break;
1783     case Reward::RUIN:
1784         {
1785           //assign the hidden ruin to this player
1786           Ruin *r = dynamic_cast<Reward_Ruin*>(reward)->getRuin();
1787           r->setHidden(true);
1788           r->setOwner(this);
1789           r->deFog(this);
1790         }
1791       break;
1792     case Reward::MAP:
1793         {
1794           Reward_Map *map = dynamic_cast<Reward_Map*>(reward);
1795           d_fogmap->alterFog(map->getSightMap());
1796         }
1797       break;
1798     }
1799 }
1800
1801 bool Player::giveReward(Stack *s, Reward *reward)
1802 {
1803   debug("Player::give_reward");
1804
1805   doGiveReward(s, reward);
1806   
1807   Action_Reward* item = new Action_Reward();
1808   item->fillData(s, reward);
1809   addAction(item);
1810
1811   if (reward->getType() == Reward::RUIN)
1812     {
1813       Ruin *r = dynamic_cast<Reward_Ruin*>(reward)->getRuin();
1814       History_HeroRewardRuin* history_item = new History_HeroRewardRuin();
1815       history_item->fillData(dynamic_cast<Hero*>(s->getFirstHero()), r);
1816       addHistory(history_item);
1817     }
1818   //FIXME: get rid of this reward now that we're done with it
1819   //but we need to show it still... (in the case of quest completions)
1820
1821   return true;
1822 }
1823
1824 bool Player::doStackDisband(Stack* s)
1825 {
1826     getStacklist()->setActivestack(0);
1827     bool found = d_stacklist->flRemove(s);
1828     supdatingStack.emit(0);
1829     return found;
1830 }
1831
1832 bool Player::stackDisband(Stack* s)
1833 {
1834     debug("Player::stackDisband(Stack*)")
1835     if (!s)
1836       s = getActivestack();
1837     
1838     Action_Disband* item = new Action_Disband();
1839     item->fillData(s);
1840     addAction(item);
1841
1842     return doStackDisband(s);
1843 }
1844
1845 void Player::doHeroDropItem(Hero *h, Item *i, Vector<int> pos)
1846 {
1847   GameMap::getInstance()->getTile(pos)->getBackpack()->addToBackpack(i);
1848   h->getBackpack()->removeFromBackpack(i);
1849 }
1850
1851 bool Player::heroDropItem(Hero *h, Item *i, Vector<int> pos)
1852 {
1853   doHeroDropItem(h, i, pos);
1854   
1855   Action_Equip* item = new Action_Equip();
1856   item->fillData(h, i, Action_Equip::GROUND, pos);
1857   addAction(item);
1858   
1859   return true;
1860 }
1861
1862 bool Player::heroDropAllItems(Hero *h, Vector<int> pos)
1863 {
1864   while (h->getBackpack()->empty() == false)
1865     heroDropItem(h, h->getBackpack()->front(), pos);
1866   return true;
1867 }
1868
1869 bool Player::doHeroDropAllItems(Hero *h, Vector<int> pos)
1870 {
1871   while (h->getBackpack()->empty() == false)
1872     doHeroDropItem(h, h->getBackpack()->front(), pos);
1873   return true;
1874 }
1875
1876 void Player::doHeroPickupItem(Hero *h, Item *i, Vector<int> pos)
1877 {
1878   bool found = GameMap::getInstance()->getTile(pos)->getBackpack()->removeFromBackpack(i);
1879   if (found)
1880     h->getBackpack()->addToBackpack(i);
1881 }
1882
1883 bool Player::heroPickupItem(Hero *h, Item *i, Vector<int> pos)
1884 {
1885   doHeroPickupItem(h, i, pos);
1886   
1887   Action_Equip* item = new Action_Equip();
1888   item->fillData(h, i, Action_Equip::BACKPACK, pos);
1889   addAction(item);
1890   
1891   return true;
1892 }
1893
1894 bool Player::heroPickupAllItems(Hero *h, Vector<int> pos)
1895 {
1896   MapBackpack *backpack = GameMap::getInstance()->getTile(pos)->getBackpack();
1897   while (backpack->empty() == false)
1898     heroPickupItem(h, backpack->front(), pos);
1899   return true;
1900 }
1901
1902 bool Player::heroCompletesQuest(Hero *h)
1903 {
1904   // record it for posterity
1905   History_HeroQuestCompleted* item = new History_HeroQuestCompleted();
1906   item->fillData(h);
1907   addHistory(item);
1908   return true;
1909 }
1910
1911 void Player::doResign()
1912 {
1913   //disband all stacks
1914   getStacklist()->flClear();
1915
1916   //raze all cities
1917   Citylist *cl = Citylist::getInstance();
1918   for (Citylist::iterator it = cl->begin(); it != cl->end(); it++)
1919     {
1920       if ((*it)->getOwner() == this)
1921         {
1922           (*it)->setBurnt(true);
1923           History_CityRazed* history = new History_CityRazed();
1924           history->fillData((*it));
1925           addHistory(history);
1926         }
1927     }
1928   withdrawGold(getGold()); //empty the coffers!
1929
1930   getStacklist()->setActivestack(0);
1931   supdatingStack.emit(0);
1932 }
1933
1934 void Player::resign() 
1935 {
1936   doResign();
1937   
1938   Action_Resign* item = new Action_Resign();
1939   item->fillData();
1940   addAction(item);
1941 }
1942
1943 void Player::doSignpostChange(Signpost *s, std::string message)
1944 {
1945   s->setName(message);
1946 }
1947
1948 bool Player::signpostChange(Signpost *s, std::string message)
1949 {
1950   if (!s)
1951     return false;
1952   
1953   doSignpostChange(s, message);
1954   
1955   Action_ModifySignpost* item = new Action_ModifySignpost();
1956   item->fillData(s, message);
1957   addAction(item);
1958   return true;
1959 }
1960
1961 void Player::doCityRename(City *c, std::string name)
1962 {
1963   c->setName(name);
1964 }
1965
1966 bool Player::cityRename(City *c, std::string name)
1967 {
1968   if (!c)
1969     return false;
1970
1971   doCityRename(c, name);
1972   
1973   Action_RenameCity* item = new Action_RenameCity();
1974   item->fillData(c, name);
1975   addAction(item);
1976   return true;
1977 }
1978
1979 void Player::doRename(std::string name)
1980 {
1981   setName(name);
1982 }
1983
1984 void Player::rename(std::string name)
1985 {
1986   doRename(name);
1987   Action_RenamePlayer * item = new Action_RenamePlayer();
1988   item->fillData(name);
1989   addAction(item);
1990   return;
1991 }
1992
1993 void Player::doVectorFromCity(City * c, Vector<int> dest)
1994 {
1995   c->setVectoring(dest);
1996 }
1997
1998 bool Player::vectorFromCity(City * c, Vector<int> dest)
1999 {
2000   if (dest != Vector<int>(-1,-1))
2001     {
2002       std::list<City*> cities;
2003       cities = Citylist::getInstance()->getCitiesVectoringTo(dest);
2004       if (cities.size() >= MAX_CITIES_VECTORED_TO_ONE_CITY)
2005         return false;
2006     }
2007   doVectorFromCity(c, dest);
2008   
2009   Action_Vector* item = new Action_Vector();
2010   item->fillData(c, dest);
2011   addAction(item);
2012   return true;
2013 }
2014
2015 bool Player::doChangeVectorDestination(Vector<int> src, Vector<int> dest,
2016                                        std::list<City*> &vectored)
2017 {
2018   //DEST can be a flag.
2019   //SRC can be a flag too.
2020   //Note: we don't actually have a way in the gui to change the vectoring 
2021   //from the planted standard (flag).
2022   bool retval = true;
2023   //sanity checks:
2024   //disallow changing vectoring from or to a city that isn't ours
2025   //disallow vectoring to something that isn't our city or our planted 
2026   //standard.
2027   Citylist *cl = Citylist::getInstance();
2028   City *src_city = GameMap::getCity(src);
2029   if (src_city == NULL)
2030     {
2031       //maybe it's a flag we're changing the vector destination from.
2032       if (GameMap::getInstance()->findPlantedStandard(this) != src)
2033         return false;
2034     }
2035   else
2036     {
2037       if (src_city->getOwner() != this)
2038         return false;
2039     }
2040   City *dest_city = GameMap::getCity(dest);
2041   if (dest_city == NULL)
2042     {
2043       if (GameMap::getInstance()->findPlantedStandard(this) != dest)
2044         return false;
2045     }
2046   else
2047     {
2048       if (dest_city->getOwner() != this)
2049         return false;
2050     }
2051
2052   //check to see if the destination has enough room to accept all of the
2053   //cities we want to send to it.
2054   std::list<City*> sources = cl->getCitiesVectoringTo(src);
2055   std::list<City*> alreadyvectored = cl->getCitiesVectoringTo(dest);
2056
2057   if (alreadyvectored.size() + sources.size() > MAX_CITIES_VECTORED_TO_ONE_CITY)
2058     return false;
2059
2060   //okay, do the vectoring changes.
2061   std::list<City*>::iterator it = sources.begin();
2062   for (; it != sources.end(); it++)
2063     retval &= (*it)->changeVectorDestination(dest);
2064   vectored = sources;
2065   return retval;
2066 }
2067
2068 bool Player::changeVectorDestination(Vector<int> src, Vector<int> dest)
2069 {
2070   std::list<City*> vectored;
2071   bool retval = doChangeVectorDestination(src, dest, vectored);
2072   if (retval == false)
2073     return retval;
2074
2075   std::list<City*>::iterator it = vectored.begin();
2076   for (; it != vectored.end(); it++)
2077     {
2078       Action_Vector* item = new Action_Vector();
2079       item->fillData((*it), dest);
2080       addAction(item);
2081     }
2082   return true;
2083 }
2084
2085 bool Player::heroPlantStandard(Stack* s)
2086 {
2087   debug("Player::heroPlantStandard(Stack*)");
2088   if (!s)
2089     s = getActivestack();
2090   
2091   for (Stack::iterator it = s->begin(); it != s->end(); it++)
2092   {
2093     if ((*it)->isHero())
2094     {
2095       Hero *hero = dynamic_cast<Hero*>((*it));
2096       Item *item = hero->getBackpack()->getPlantableItem(this);
2097       if (item)
2098         {
2099           //drop the item, and plant it
2100           doHeroPlantStandard(hero, item, s->getPos());
2101                   
2102           Action_Plant * i = new Action_Plant();
2103           i->fillData(hero, item);
2104           addAction(i);
2105           return true;
2106         }
2107     }
2108   }
2109   return true;
2110 }
2111
2112 void Player::doHeroPlantStandard(Hero *hero, Item *item, Vector<int> pos)
2113 {
2114   item->setPlanted(true);
2115   GameMap *gm = GameMap::getInstance();
2116   gm->getTile(pos)->getBackpack()->addToBackpack(item);
2117   hero->getBackpack()->removeFromBackpack(item);
2118 }
2119
2120 void Player::getHeroes(const std::list<Stack*> stacks, std::vector<guint32>& dst)
2121 {
2122     std::list<Stack*>::const_iterator it;
2123     for (it = stacks.begin(); it != stacks.end(); it++)
2124         (*it)->getHeroes(dst);
2125 }
2126
2127 double Player::removeDeadArmies(std::list<Stack*>& stacks,
2128                                 std::vector<guint32>& culprits)
2129 {
2130     double total=0;
2131     Player *owner = NULL;
2132     if (stacks.empty() == 0)
2133     {
2134         owner = (*stacks.begin())->getOwner();
2135         debug("Owner = " << owner);
2136         if (owner)
2137             debug("Owner of the stacks: " << owner->getName()
2138                   << ", his stacklist = " << owner->getStacklist());
2139     }
2140     for (unsigned int i = 0; i < culprits.size(); i++)
2141         debug("Culprit: " << culprits[i]);
2142
2143     std::list<Stack*>::iterator it;
2144     for (it = stacks.begin(); it != stacks.end(); )
2145     {
2146     
2147         debug("Stack: " << (*it))
2148         for (Stack::iterator sit = (*it)->begin(); sit != (*it)->end();)
2149         {
2150             debug("Army: " << (*sit))
2151             if ((*sit)->getHP() <= 0)
2152             {
2153                 //Tally up the triumphs
2154                 if ((*sit)->getAwardable()) //hey a special died
2155                   d_triumphs->tallyTriumph((*sit)->getOwner(), 
2156                                            Triumphs::TALLY_SPECIAL);
2157                 else if ((*sit)->isHero() == false)
2158                   d_triumphs->tallyTriumph((*sit)->getOwner(), 
2159                                            Triumphs::TALLY_NORMAL);
2160                 if ((*sit)->getStat(Army::SHIP, false)) //hey it was on a boat
2161                   d_triumphs->tallyTriumph((*sit)->getOwner(), 
2162                                            Triumphs::TALLY_SHIP);
2163                 debug("Army: " << (*sit)->getName())
2164                 debug("Army: " << (*sit)->getXpReward())
2165                 if ((*sit)->isHero())
2166                 {
2167                   d_triumphs->tallyTriumph((*sit)->getOwner(), 
2168                                            Triumphs::TALLY_HERO);
2169                   Hero *hero = dynamic_cast<Hero*>((*sit));
2170                   guint32 count = hero->getBackpack()->countPlantableItems();
2171                   for (guint32 i = 0; i < count; i++)
2172                     d_triumphs->tallyTriumph((*sit)->getOwner(), 
2173                                              Triumphs::TALLY_FLAG);
2174
2175                   //one of our heroes died
2176                   //drop hero's stuff
2177                   Hero *h = static_cast<Hero *>(*sit);
2178                   //now record the details of the death
2179                   GameMap *gm = GameMap::getInstance();
2180                   Maptile *tile = gm->getTile((*it)->getPos());
2181                   if (tile->getBuilding() == Maptile::RUIN)
2182                     {
2183                       History_HeroKilledSearching* item;
2184                       item = new History_HeroKilledSearching();
2185                       item->fillData(h);
2186                       h->getOwner()->addHistory(item);
2187                       doHeroDropAllItems (h, (*it)->getPos());
2188                     }
2189                   else if (tile->getBuilding() == Maptile::CITY)
2190                     {
2191                       City* c = GameMap::getCity((*it)->getPos());
2192                       History_HeroKilledInCity* item;
2193                       item = new History_HeroKilledInCity();
2194                       item->fillData(h, c);
2195                       h->getOwner()->addHistory(item);
2196                       doHeroDropAllItems (h, (*it)->getPos());
2197                     }
2198                   else //somewhere else
2199                     {
2200                       History_HeroKilledInBattle* item;
2201                       item = new History_HeroKilledInBattle();
2202                       item->fillData(h);
2203                       h->getOwner()->addHistory(item);
2204                       doHeroDropAllItems (h, (*it)->getPos());
2205                     }
2206                 }
2207                 //Add the XP bonus to the total of the battle;
2208                 total+=(*sit)->getXpReward();
2209                 //tell the quest manager that someone died
2210                 //(maybe it was a hero, or a target that's an army)
2211                 QuestsManager::getInstance()->armyDied(*sit, culprits);
2212                 // here we destroy the army, so we send
2213                 // the signal containing the fight data
2214                 debug("sending sdyingArmy!")
2215                   sdyingArmy.emit(*sit, culprits);
2216                 sit = (*it)->flErase(sit);
2217                 continue;
2218             }
2219
2220             // heal this army to full hitpoints
2221             (*sit)->heal((*sit)->getStat(Army::HP));
2222
2223             sit++;
2224         }
2225
2226         debug("Is stack empty?")
2227
2228           if ((*it)->empty())
2229             {
2230               if (owner)
2231                 {
2232                   debug("Removing this stack from the owner's stacklist");
2233                   bool found = owner->deleteStack(*it);
2234                   assert (found == true);
2235                 }
2236               else // there is no owner - like for the ruin's occupants
2237                 debug("No owner for this stack - do stacklist too");
2238
2239               debug("Removing from the vector too (the vector had "
2240                     << stacks.size() << " elt)");
2241               it = stacks.erase(it);
2242             }
2243           else
2244             it++;
2245     }
2246     debug("after removeDead: size = " << stacks.size());
2247     return total;
2248 }
2249
2250 void Player::doHeroGainsLevel(Hero *hero, Army::Stat stat)
2251 {
2252   hero->gainLevel(stat);
2253 }
2254
2255
2256 void Player::updateArmyValues(std::list<Stack*>& stacks, double xp_sum)
2257 {
2258   std::list<Stack*>::iterator it;
2259   double numberarmy = 0;
2260
2261   for (it = stacks.begin(); it != stacks.end(); it++)
2262     numberarmy += (*it)->size();
2263
2264   for (it = stacks.begin(); it != stacks.end(); )
2265     {
2266       debug("Stack: " << (*it))
2267
2268         for (Stack::iterator sit = (*it)->begin(); sit != (*it)->end();)
2269           {
2270             Army *army = *sit;
2271             debug("Army: " << army)
2272
2273               // here we adds XP
2274               army->gainXp((double)((xp_sum)/numberarmy));
2275             debug("Army gets " << (double)((xp_sum)/numberarmy) << " XP")
2276
2277               // here we adds 1 to number of battles
2278               army->setBattlesNumber(army->getBattlesNumber()+1);
2279             debug("Army battles " <<  army->getBattlesNumber())
2280
2281               // medals only go to non-ally armies.
2282               if ((*it)->hasHero() && army->isHero() == false && 
2283                   army->getAwardable() == false)
2284                 {
2285                   if((army->getBattlesNumber())>10 && 
2286                      !(army->getMedalBonus(2)))
2287                     {
2288                       army->setMedalBonus(2,true);
2289                       // We must recalculate the XPValue of this unit since it 
2290                       // got a medal
2291                       army->setXpReward(army->getXpReward()+1);
2292                       // We get the medal bonus here
2293                       army->setStat(Army::STRENGTH, army->getStat(Army::STRENGTH, false)+1);
2294                       // Emit signal
2295                       snewMedalArmy.emit(army, 2);
2296                     }
2297
2298                   debug("Army hits " <<  army->getNumberHasHit())
2299
2300                     // Only give medals if the unit has attacked often enough, else
2301                     // medals lose the flair of something special; a value of n 
2302                     // means roughly to hit an equally strong unit around n 
2303                     // times. (note: one hit! An attack can consist of up to 
2304                     // strength hits)
2305                     if((army->getNumberHasHit()>50) && !army->getMedalBonus(0))
2306                       {
2307                         army->setMedalBonus(0,true);
2308                         // We must recalculate the XPValue of this unit since it
2309                         // got a medal
2310                         army->setXpReward(army->getXpReward()+1);
2311                         // We get the medal bonus here
2312                         army->setStat(Army::STRENGTH, army->getStat(Army::STRENGTH, false)+1);
2313                         // Emit signal
2314                         snewMedalArmy.emit(army, 0);
2315                       }
2316
2317                   debug("army being hit " <<  army->getNumberHasBeenHit())
2318
2319                     // Gives the medal for good defense. The more negative the 
2320                     // number the more blows the unit evaded. n means roughly 
2321                     // avoid n hits from an equally strong unit. Since we want 
2322                     // to punish the case of the unit hiding among many others, 
2323                     // we set this value quite high.
2324                     if((army->getNumberHasBeenHit() < -100) && !army->getMedalBonus(1))
2325                       {
2326                         army->setMedalBonus(1,true);
2327                         // We must recalculate the XPValue of this unit since it 
2328                         // got a medal
2329                         army->setXpReward(army->getXpReward()+1);
2330                         // We get the medal bonus here
2331                         army->setStat(Army::STRENGTH, army->getStat(Army::STRENGTH, false)+1);
2332                         // Emit signal
2333                         snewMedalArmy.emit(army, 1);
2334                       }
2335                   debug("Army hits " <<  army->getNumberHasHit())
2336
2337                     for(int i=0;i<3;i++)
2338                       {
2339                         debug("MEDAL[" << i << "]==" << army->getMedalBonus(i))
2340                       }
2341                 }
2342
2343             // We reset the hit values after the battle
2344             army->setNumberHasHit(0);
2345             army->setNumberHasBeenHit(0);
2346
2347             if (army->isHero() && getType() != Player::NETWORKED)
2348               {
2349                 Hero *h = dynamic_cast<Hero*>(army);
2350                 while(h->canGainLevel())
2351                   {
2352                     // Units not associated to a player never raise levels.
2353                     if (h->getOwner() == 
2354                         Playerlist::getInstance()->getNeutral())
2355                       break;
2356
2357                     //Here this for is to check if army must raise 2 or more 
2358                     //levels per time depending on the XP and level itself
2359
2360                     h->getOwner()->heroGainsLevel(h);
2361                   }
2362                 debug("Hero new XP=" << h->getXP())
2363               }
2364             sit++;
2365           }
2366       it++;
2367     }
2368 }
2369
2370 Hero* Player::doRecruitHero(HeroProto* herotemplate, City *city, int cost, int alliesCount, const ArmyProto *ally)
2371 {
2372   Hero *newhero = new Hero(*herotemplate);
2373   newhero->setOwner(this);
2374   GameMap::getInstance()->addArmy(city, newhero);
2375
2376   if (alliesCount > 0)
2377     {
2378       Reward_Allies::addAllies(this, city->getPos(), ally, alliesCount);
2379       hero_arrives_with_allies.emit(alliesCount);
2380     }
2381
2382   if (cost == 0)
2383     {
2384       // Initially give the first hero the player's standard.
2385       std::string name = String::ucompose(_("%1 Standard"), getName());
2386       Item *battle_standard = new Item (name, true, this);
2387       battle_standard->addBonus(Item::ADD1STACK);
2388       newhero->getBackpack()->addToBackpack(battle_standard, 0);
2389     }
2390   withdrawGold(cost);
2391   supdatingStack.emit(0);
2392   return newhero;
2393 }
2394
2395 void Player::recruitHero(HeroProto* heroproto, City *city, int cost, int alliesCount, const ArmyProto *ally)
2396 {
2397   //alright, we may have picked another sex for the hero.
2398   HeroProto *h;
2399   std::string name = heroproto->getName();
2400   Hero::Gender g = Hero::Gender(heroproto->getGender());
2401   h = HeroTemplates::getInstance()->getRandomHero(g, getId());
2402   h->setGender(g);
2403   h->setName(name);
2404   Action_RecruitHero *action = new Action_RecruitHero();
2405   action->fillData(h, city, cost, alliesCount, ally);
2406   addAction(action);
2407
2408   Hero *hero = doRecruitHero(h, city, cost, alliesCount, ally);
2409   if (hero)
2410     {
2411       History_HeroEmerges *item = new History_HeroEmerges();
2412       item->fillData(hero, city);
2413       addHistory(item);
2414     }
2415 }
2416
2417 void Player::doDeclareDiplomacy (DiplomaticState state, Player *player)
2418 {
2419   Playerlist *pl = Playerlist::getInstance();
2420   if (pl->getNeutral() == player)
2421     return;
2422   if (player == this)
2423     return;
2424   if (state == d_diplomatic_state[player->getId()])
2425     return;
2426   d_diplomatic_state[player->getId()] = state;
2427 }
2428
2429 void Player::declareDiplomacy (DiplomaticState state, Player *player)
2430 {
2431   doDeclareDiplomacy(state, player);
2432
2433   Action_DiplomacyState * item = new Action_DiplomacyState();
2434   item->fillData(player, state);
2435   addAction(item);
2436
2437   // FIXME: update diplomatic scores? 
2438 }
2439
2440 void Player::doProposeDiplomacy (DiplomaticProposal proposal, Player *player)
2441 {
2442   if (GameScenarioOptions::s_diplomacy == false)
2443     return;
2444   Playerlist *pl = Playerlist::getInstance();
2445   if (pl->getNeutral() == player)
2446     return;
2447   if (player == this)
2448     return;
2449   if (proposal == d_diplomatic_proposal[player->getId()])
2450     return;
2451   if (proposal == PROPOSE_PEACE)
2452     {
2453       std::string s = _("Peace negotiated with ") + player->getName();
2454       if (getDiplomaticState(player) == AT_PEACE ||
2455           getDiplomaticProposal(player) == PROPOSE_PEACE)
2456         schangingStatus.emit(s);
2457     }
2458   else if (proposal == PROPOSE_WAR)
2459     {
2460       std::string s = _("War declared with ") + player->getName();
2461       if (getDiplomaticState(player) == AT_WAR ||
2462           getDiplomaticProposal(player) == PROPOSE_WAR)
2463       schangingStatus.emit(s);
2464     }
2465   d_diplomatic_proposal[player->getId()] = proposal;
2466 }
2467
2468 void Player::proposeDiplomacy (DiplomaticProposal proposal, Player *player)
2469 {
2470   doProposeDiplomacy(proposal, player);
2471
2472   Action_DiplomacyProposal * item = new Action_DiplomacyProposal();
2473   item->fillData(player, proposal);
2474   addAction(item);
2475
2476   // FIXME: update diplomatic scores? 
2477 }
2478
2479 Player::DiplomaticState Player::negotiateDiplomacy (Player *player)
2480 {
2481   DiplomaticState state = getDiplomaticState(player);
2482   DiplomaticProposal them = player->getDiplomaticProposal(this);
2483   DiplomaticProposal me = getDiplomaticProposal(player);
2484   DiplomaticProposal winning_proposal;
2485
2486   /* Check if we both want the status quo. */
2487   if (me == NO_PROPOSAL && them == NO_PROPOSAL)
2488     return state;
2489
2490   /* Okay, we both want a change from the status quo. */
2491
2492   /* In the absense of a new proposal, the status quo is the proposal. */
2493   if (me == NO_PROPOSAL)
2494     {
2495       switch (state)
2496         {
2497         case AT_PEACE: me = PROPOSE_PEACE; break;
2498         case AT_WAR_IN_FIELD: me = PROPOSE_WAR_IN_FIELD; break;
2499         case AT_WAR: me = PROPOSE_WAR; break;
2500         }
2501     }
2502   if (them == NO_PROPOSAL)
2503     {
2504       switch (state)
2505         {
2506         case AT_PEACE: them = PROPOSE_PEACE; break;
2507         case AT_WAR_IN_FIELD: them = PROPOSE_WAR_IN_FIELD; break;
2508         case AT_WAR: them = PROPOSE_WAR; break;
2509         }
2510     }
2511
2512   /* Check if we have agreement. */
2513   if (me == PROPOSE_PEACE && them == PROPOSE_PEACE)
2514     return AT_PEACE;
2515   else if (me == PROPOSE_WAR_IN_FIELD && them == PROPOSE_WAR_IN_FIELD)
2516     return AT_WAR_IN_FIELD;
2517   else if (me == PROPOSE_WAR && them == PROPOSE_WAR)
2518     return AT_WAR;
2519
2520   /* Still we don't have an agreement.  
2521      Unfortunately the greater violence is the new diplomatic state. 
2522      Because there are two different proposals and the proposal with
2523      greater violence will be the new status quo, there can't 
2524      possibly be peace at this juncture.  */
2525
2526   winning_proposal = me;
2527   if (them > me)
2528     winning_proposal = them;
2529
2530   switch (winning_proposal)
2531     {
2532     case PROPOSE_WAR_IN_FIELD: return AT_WAR_IN_FIELD; break;
2533     case PROPOSE_WAR: return AT_WAR; break;
2534     default: return AT_PEACE; break; //impossible
2535     }
2536
2537 }
2538
2539 Player::DiplomaticState Player::getDiplomaticState (Player *player) const
2540 {
2541   if (player == Playerlist::getInstance()->getNeutral())
2542     return AT_WAR;
2543   if (player == this)
2544     return AT_PEACE;
2545   return d_diplomatic_state[player->getId()];
2546 }
2547
2548 Player::DiplomaticProposal Player::getDiplomaticProposal (Player *player) const
2549 {
2550   if (player == Playerlist::getInstance()->getNeutral())
2551     return PROPOSE_WAR;
2552   if (player == this)
2553     return NO_PROPOSAL;
2554   return d_diplomatic_proposal[player->getId()];
2555 }
2556
2557 guint32 Player::getDiplomaticScore (Player *player) const
2558 {
2559   Playerlist *pl = Playerlist::getInstance();
2560   if (pl->getNeutral() == player)
2561     return 8;
2562   return d_diplomatic_score[player->getId()];
2563 }
2564
2565 void Player::alterDiplomaticRelationshipScore (Player *player, int amount)
2566 {
2567   if (amount > 0)
2568     {
2569       if (d_diplomatic_score[player->getId()] + amount > DIPLOMACY_MAX_SCORE)
2570         d_diplomatic_score[player->getId()] = DIPLOMACY_MAX_SCORE;
2571       else
2572         d_diplomatic_score[player->getId()] += amount;
2573     }
2574   else if (amount < 0)
2575     {
2576       if ((guint32) (amount * -1) > d_diplomatic_score[player->getId()])
2577         d_diplomatic_score[player->getId()] = DIPLOMACY_MIN_SCORE;
2578       else
2579         d_diplomatic_score[player->getId()] += amount;
2580     }
2581 }
2582
2583 void Player::improveDiplomaticRelationship (Player *player, guint32 amount)
2584 {
2585   Playerlist *pl = Playerlist::getInstance();
2586   if (pl->getNeutral() == player || player == this)
2587     return;
2588
2589   alterDiplomaticRelationshipScore (player, amount);
2590
2591   Action_DiplomacyScore* item = new Action_DiplomacyScore();
2592   item->fillData(player, amount);
2593   addAction(item);
2594 }
2595
2596 void Player::deteriorateDiplomaticRelationship (Player *player, guint32 amount)
2597 {
2598   Playerlist *pl = Playerlist::getInstance();
2599   if (pl->getNeutral() == player || player == this)
2600     return;
2601
2602   alterDiplomaticRelationshipScore (player, -amount);
2603
2604   Action_DiplomacyScore* item = new Action_DiplomacyScore();
2605   item->fillData(player, -amount);
2606   addAction(item);
2607 }
2608
2609 void Player::deteriorateDiplomaticRelationship (guint32 amount)
2610 {
2611   Playerlist *pl = Playerlist::getInstance();
2612   for (Playerlist::iterator it = pl->begin(); it != pl->end(); ++it)
2613     {
2614       if ((*it)->isDead())
2615         continue;
2616       if (pl->getNeutral() == (*it))
2617         continue;
2618       if (*it == this)
2619         continue;
2620       (*it)->deteriorateDiplomaticRelationship (this, amount);
2621     }
2622 }
2623
2624 void Player::improveDiplomaticRelationship (guint32 amount, Player *except)
2625 {
2626   Playerlist *pl = Playerlist::getInstance();
2627   for (Playerlist::iterator it = pl->begin(); it != pl->end(); ++it)
2628     {
2629       if ((*it)->isDead())
2630         continue;
2631       if (pl->getNeutral() == (*it))
2632         continue;
2633       if (*it == this)
2634         continue;
2635       if (except && *it == except)
2636         continue;
2637       (*it)->improveDiplomaticRelationship (this, amount);
2638     }
2639 }
2640
2641 void Player::deteriorateAlliesRelationship(Player *player, guint32 amount,
2642                                            Player::DiplomaticState state)
2643 {
2644   Playerlist *pl = Playerlist::getInstance();
2645   for (Playerlist::iterator it = pl->begin(); it != pl->end(); ++it)
2646     {
2647       if ((*it)->isDead())
2648         continue;
2649       if (pl->getNeutral() == (*it))
2650         continue;
2651       if (*it == this)
2652         continue;
2653       if (getDiplomaticState(*it) == state)
2654         (*it)->deteriorateDiplomaticRelationship (player, amount);
2655     }
2656 }
2657
2658 void Player::improveAlliesRelationship(Player *player, guint32 amount,
2659                                        Player::DiplomaticState state)
2660 {
2661   Playerlist *pl = Playerlist::getInstance();
2662   for (Playerlist::iterator it = pl->begin(); it != pl->end(); ++it)
2663     {
2664       if ((*it)->isDead())
2665         continue;
2666       if (pl->getNeutral() == (*it))
2667         continue;
2668       if (*it == this)
2669         continue;
2670       if (player->getDiplomaticState(*it) == state)
2671         (*it)->improveDiplomaticRelationship (this, amount);
2672     }
2673 }
2674
2675 void Player::AI_maybeBuyScout(City *c)
2676 {
2677   bool one_turn_army_exists = false;
2678   //do we already have something that can be produced in one turn?
2679   for (unsigned int i = 0; i < c->getMaxNoOfProductionBases(); i++)
2680     {
2681       if (c->getArmytype(i) == -1)    // no production in this slot
2682         continue;
2683
2684       const ArmyProdBase *proto = c->getProductionBase(i);
2685       if (proto->getProduction() == 1)
2686         {
2687           one_turn_army_exists = true;
2688           break;
2689         }
2690     }
2691   if (one_turn_army_exists == false)
2692     {
2693       const Armysetlist* al = Armysetlist::getInstance();
2694       int free_slot = c->getFreeSlot();
2695       if (free_slot == -1)
2696         free_slot = 0;
2697       ArmyProto *scout = al->getScout(getArmyset());
2698       cityBuyProduction(c, free_slot, scout->getTypeId());
2699     }
2700 }
2701
2702 bool Player::AI_maybePickUpItems(Stack *s, int max_dist, int max_mp, 
2703                                  bool &picked_up, bool &stack_died)
2704 {
2705   int min_dist = -1;
2706   bool stack_moved = false;
2707   Vector<int> item_tile(-1, -1);
2708
2709   // do we not have a hero?
2710   if (s->hasHero() == false)
2711     return false;
2712
2713   //ok, which bag of stuff is closest?
2714   std::vector<Vector<int> > tiles = GameMap::getInstance()->getItems();
2715   std::vector<Vector<int> >::iterator it = tiles.begin();
2716   for(; it != tiles.end(); it++)
2717     {
2718       Vector<int> tile = *it;
2719       //don't consider bags of stuff that are inside enemy cities
2720       City *c = GameMap::getCity(tile);
2721       if (c)
2722         {
2723           if (c->getOwner() != s->getOwner())
2724             continue;
2725         }
2726
2727       int distance = dist (tile, s->getPos());
2728       if (distance < min_dist || min_dist == -1)
2729         {
2730           min_dist = distance;
2731           item_tile = tile;
2732         }
2733     }
2734
2735   //if no bags of stuff, or the bag is too far away
2736   if (min_dist == -1 || min_dist > max_dist)
2737     return false;
2738
2739   //are we not standing on it?
2740   if (s->getPos() != item_tile)
2741     {
2742       //can we really reach it?
2743       Vector<int> old_dest(-1,-1);
2744       if (s->getPath()->size())
2745         old_dest = s->getLastPointInPath();
2746       guint32 mp = s->getPath()->calculate(s, item_tile);
2747       if ((int)mp > max_mp)
2748         {
2749           //nope.  unreachable.  set in our old path.
2750           if (old_dest != Vector<int>(-1,-1))
2751             s->getPath()->calculate(s, old_dest);
2752           return false;
2753         }
2754       stack_moved = stackMove(s);
2755       //maybe we died -- an enemy stack was guarding the bag.
2756       if (!d_stacklist->getActivestack())
2757         {
2758           stack_died = true;
2759           return true;
2760         }
2761       s = d_stacklist->getActivestack();
2762     }
2763
2764   //are we standing on it now?
2765   if (s->getPos() == item_tile)
2766     {
2767       Hero *hero = static_cast<Hero*>(s->getFirstHero());
2768       if (hero)
2769         picked_up = heroPickupAllItems(hero, s->getPos());
2770     }
2771
2772   return stack_moved;
2773 }
2774
2775 bool Player::AI_maybeVisitTempleForQuest(Stack *s, int dist, int max_mp, 
2776                                          bool &stack_died)
2777 {
2778   bool stack_moved = false;
2779   Templelist *tl = Templelist::getInstance();
2780
2781   //if this stack doesn't have a hero then we can't get a quest with this stack.
2782   if (s->hasHero() == false)
2783     return false;
2784
2785   //if the player already has a hero who has a quest, then we can't get a
2786   //quest with this stack when playing one quest per player.
2787   if (QuestsManager::getInstance()->getPlayerQuests(this).size() > 0 &&
2788       GameScenarioOptions::s_play_with_quests == 
2789       GameParameters::ONE_QUEST_PER_PLAYER)
2790     return false;
2791
2792   Temple *temple = tl->getNearestVisibleTemple(s->getPos(), dist);
2793   if (!temple)
2794     return false;
2795
2796   //if we're not there yet
2797   if (temple->contains(s->getPos()) == false)
2798     {
2799       //can we really reach it?
2800       Vector<int> old_dest(-1,-1);
2801       if (s->getPath()->size())
2802         old_dest = s->getLastPointInPath();
2803       guint32 mp = s->getPath()->calculate(s, temple->getPos());
2804       if ((int)mp > max_mp)
2805         {
2806           //nope.  unreachable.  set in our old path.
2807           if (old_dest != Vector<int>(-1,-1))
2808             s->getPath()->calculate(s, old_dest);
2809           return false;
2810         }
2811       stack_moved = stackMove(s);
2812
2813       //maybe we died -- an enemy stack was guarding the temple
2814       if (!d_stacklist->getActivestack())
2815         {
2816           stack_died = true;
2817           return true;
2818         }
2819       s = d_stacklist->getActivestack();
2820     }
2821
2822   //are we there yet?
2823   if (temple->contains(s->getPos()) == true)
2824     svisitingTemple.emit(temple, s);
2825
2826   return stack_moved;
2827 }
2828
2829 bool Player::AI_maybeVisitRuin(Stack *s, int dist, int max_mp, 
2830                                          bool &stack_died)
2831 {
2832   bool stack_moved = false;
2833   Ruinlist *rl = Ruinlist::getInstance();
2834
2835   //if this stack doesn't have a hero then we can't search the ruin.
2836   if (s->hasHero() == false)
2837     return false;
2838
2839   Ruin *ruin = rl->getNearestUnsearchedRuin(s->getPos(), dist);
2840   if (!ruin)
2841     return false;
2842
2843   //if we're not there yet
2844   if (ruin->contains(s->getPos()) == false)
2845     {
2846       //can we really reach it?
2847       Vector<int> old_dest(-1,-1);
2848       if (s->getPath()->size())
2849         old_dest = s->getLastPointInPath();
2850       guint32 mp = s->getPath()->calculate(s, ruin->getPos());
2851       if ((int)mp > max_mp)
2852         {
2853           //nope.  unreachable.  set in our old path.
2854           if (old_dest != Vector<int>(-1,-1))
2855             s->getPath()->calculate(s, old_dest);
2856           return false;
2857         }
2858       stack_moved = stackMove(s);
2859
2860       //maybe we died -- an enemy stack was guarding the temple
2861       if (!d_stacklist->getActivestack())
2862         {
2863           stack_died = true;
2864           return true;
2865         }
2866       s = d_stacklist->getActivestack();
2867     }
2868
2869   //are we there yet?
2870   if (ruin->contains(s->getPos()) == true)
2871     ssearchingRuin.emit(ruin, s);
2872
2873   return stack_moved;
2874 }
2875 bool Player::AI_maybeVisitTempleForBlessing(Stack *s, int dist, int max_mp, 
2876                                             double percent_can_be_blessed, 
2877                                             bool &blessed, bool &stack_died)
2878 {
2879   bool stack_moved = false;
2880   Templelist *tl = Templelist::getInstance();
2881
2882   Temple *temple = tl->getNearestVisibleAndUsefulTemple(s, percent_can_be_blessed, dist);
2883   if (!temple)
2884     return false;
2885
2886   //if we're not there yet
2887   if (s->getPos() != temple->getPos())
2888     {
2889       //can we really reach it?
2890       Vector<int> old_dest(-1,-1);
2891       if (s->getPath()->size())
2892         old_dest = s->getLastPointInPath();
2893       guint32 mp = s->getPath()->calculate(s, temple->getPos());
2894       if ((int)mp > max_mp)
2895         {
2896           //nope.  unreachable.  set in our old path.
2897           if (old_dest != Vector<int>(-1,-1))
2898             s->getPath()->calculate(s, old_dest);
2899           return false;
2900         }
2901       stack_moved = stackMove(s);
2902
2903       //maybe we died -- an enemy stack was guarding the temple
2904       if (!d_stacklist->getActivestack())
2905         {
2906           stack_died = true;
2907           return true;
2908         }
2909       s = d_stacklist->getActivestack();
2910     }
2911
2912   int num_blessed = 0;
2913   //are we there yet?
2914   if (s->getPos() == temple->getPos())
2915     {
2916       num_blessed = stackVisitTemple(s, temple);
2917     }
2918
2919   blessed = num_blessed > 0;
2920   return stack_moved;
2921 }
2922
2923 bool Player::safeFromAttack(City *c, guint32 safe_mp, guint32 min_defenders)
2924 {
2925   //if there isn't an enemy city nearby to the source
2926   // calculate mp to nearest enemy city
2927   //   needs to be less than 18 mp with a scout
2928   //does the source city contain at least 3 defenders?
2929
2930   City *enemy_city = Citylist::getInstance()->getNearestEnemyCity(c->getPos());
2931   if (enemy_city)
2932     {
2933       PathCalculator pc(c->getOwner(), c->getPos());
2934       int mp = pc.calculate(enemy_city->getPos());
2935       if (mp <= 0 || mp >= (int)safe_mp)
2936         {
2937           if (c->countDefenders() >= min_defenders)
2938             return true;
2939         }
2940     }
2941
2942   return false;
2943 }
2944
2945 bool Player::AI_maybeDisband(Stack *s, int safe_mp, bool &stack_killed)
2946 {
2947   bool disbanded = false;
2948   //see if we're near to enemy stacks
2949   PathCalculator pc(s);
2950   if (GameMap::getEnemyStacks(pc.getReachablePositions(safe_mp)).size() > 0)
2951     return false;
2952
2953   //upgroup the whole stack if it doesn't contain a hero
2954   if (s->hasHero() == false)
2955     {
2956       stack_killed = stackDisband (s);
2957       return stack_killed;
2958     }
2959
2960   //ungroup the lucky ones not being disbanded
2961   for (Stack::reverse_iterator i = s->rbegin(); i != s->rend(); i++)
2962     {
2963       if ((*i)->isHero() == false)
2964         {
2965           Stack *new_stack = stackSplitArmy(s, *i);
2966           if (new_stack)
2967             {
2968             if (stackDisband(new_stack))
2969               disbanded = true;
2970             }
2971         }
2972     }
2973   return disbanded;
2974 }
2975
2976 bool Player::AI_maybeDisband(Stack *s, City *city, guint32 min_defenders, 
2977                              int safe_mp, bool &stack_killed)
2978 {
2979   bool disbanded = false;
2980   //is the city in danger from a city?
2981   if (safeFromAttack(city, safe_mp, 0) == false)
2982     return false;
2983
2984   if (city->countDefenders() - s->size() >= min_defenders)
2985     {
2986       if (s->hasHero())
2987         min_defenders = s->size() + 1;
2988       else
2989         {
2990           stack_killed = stackDisband(s);
2991           return stack_killed;
2992         }
2993     }
2994
2995   //okay, we need to disband part of our stack
2996
2997   //before we move, ungroup the lucky ones not being disbanded
2998   unsigned int count = 0;
2999   for (Stack::reverse_iterator i = s->rbegin(); i != s->rend(); i++)
3000     {
3001       if (count == min_defenders)
3002         break;
3003       if ((*i)->isHero() == false)
3004         {
3005           Stack *new_stack = stackSplitArmy(s, *i);
3006           if (new_stack)
3007             {
3008               count++;
3009               if (stackDisband(new_stack))
3010                 disbanded = true;
3011             }
3012         }
3013     }
3014   return disbanded;
3015 }
3016
3017 bool Player::AI_maybeVector(City *c, guint32 safe_mp, guint32 min_defenders,
3018                             City *target, City **vector_city)
3019 {
3020   assert (c->getOwner() == this);
3021   if (vector_city)
3022     *vector_city = NULL;
3023   Citylist *cl = Citylist::getInstance();
3024
3025   //is this city producing anything that we can vector?
3026   if (c->getActiveProductionSlot() == -1)
3027     return false;
3028
3029   //is it safe to vector from this city?
3030   bool safe = safeFromAttack(c, 18, 3);
3031
3032   if (!safe)
3033     return false;
3034
3035   //get the nearest city to the enemy city that can accept vectored units
3036   City *near_city = cl->getNearestFriendlyVectorableCity(target->getPos());
3037   if (!near_city)
3038     return false;
3039   assert (near_city->getOwner() == this);
3040   if (GameMap::getCity(near_city->getPos()) != near_city)
3041     {
3042       printf("nearCity is %s (%d)\n", near_city->getName().c_str(), near_city->getId());
3043       printf("it is located at %d,%d\n", near_city->getPos().x, near_city->getPos().y);
3044       City *other = GameMap::getCity(near_city->getPos());
3045       if (other)
3046         {
3047       printf("the OTHER nearCity is %s (%d)\n", other->getName().c_str(), other->getId());
3048       printf("it is located at %d,%d\n", other->getPos().x, other->getPos().y);
3049         }
3050       else
3051         printf("no city there!\n");
3052       assert (1 == 0);
3053     }
3054
3055   //if it's us then it's easier to just walk.
3056   if (near_city == c)
3057     return false;
3058
3059   //is that city already vectoring?
3060   if (near_city->getVectoring() != Vector<int>(-1, -1))
3061     return false;
3062
3063   //can i just walk there faster?
3064
3065   //find mp from source to target city
3066   const ArmyProdBase *proto = c->getActiveProductionBase();
3067   PathCalculator pc1(c->getOwner(), c->getPos(), proto);
3068   int mp_from_source_city = pc1.calculate(target->getPos());
3069
3070   //find mp from nearer vectorable city to target city
3071   PathCalculator pc2(c->getOwner(), near_city->getPos(), proto);
3072   int mp_from_near_city = pc2.calculate(target->getPos());
3073
3074   guint32 max_moves_per_turn = proto->getMaxMoves();
3075
3076   double turns_to_move_from_source_city = 
3077     (double)mp_from_source_city / (double)max_moves_per_turn;
3078   double turns_to_move_from_near_city = 
3079     (double)mp_from_near_city / (double)max_moves_per_turn;
3080   turns_to_move_from_near_city += 1.0; //add extra turn to vector
3081
3082   //yes i can walk there faster, so don't vector
3083   if (turns_to_move_from_source_city <= turns_to_move_from_near_city)
3084     return false;
3085
3086   //great.  now do the vectoring.
3087   c->changeVectorDestination(near_city->getPos());
3088
3089   if (vector_city)
3090     *vector_city = near_city;
3091   return true;
3092 }
3093
3094 void Player::AI_setupVectoring(guint32 safe_mp, guint32 min_defenders,
3095                                guint32 mp_to_front)
3096 {
3097   Citylist *cl = Citylist::getInstance();
3098   //turn off vectoring where it isn't safe anymore
3099   //turn off vectoring for destinations that are far away from the
3100   //nearest enemy city
3101
3102
3103   for (Citylist::iterator cit = cl->begin(); cit != cl->end(); ++cit)
3104     {
3105       sbusy.emit();
3106       City *c = *cit;
3107       if (c->getOwner() != this || c->isBurnt())
3108         continue;
3109       Vector<int> dest = c->getVectoring();
3110       if (dest == Vector<int>(-1, -1))
3111         continue;
3112       if (safeFromAttack(c, safe_mp, min_defenders) == false)
3113         {
3114           //City *target_city = Citylist::getInstance()->getObjectAt(dest);
3115           //debug("stopping vectoring from " << c->getName() <<" to " << target_city->getName() << " because it's not safe to anymore!\n")
3116           c->setVectoring(Vector<int>(-1,-1));
3117           continue;
3118         }
3119
3120       City *enemy_city = cl->getNearestEnemyCity(dest);
3121       if (!enemy_city)
3122         {
3123           //City *target_city = Citylist::getInstance()->getObjectAt(dest);
3124           //debug("stopping vectoring from " << c->getName() <<" to " << target_city->getName() << " because there aren't any more enemy cities!\n")
3125           c->setVectoring(Vector<int>(-1,-1));
3126           continue;
3127         }
3128
3129       PathCalculator pc(this, dest, NULL);
3130       int mp = pc.calculate(enemy_city->getPos());
3131       if (mp <= 0 || mp > (int)mp_to_front)
3132         {
3133
3134           //City *target_city = Citylist::getInstance()->getObjectAt(dest);
3135           //debug("stopping vectoring from " << c->getName() <<" to " << target_city->getName() << " because it's too far away from an enemy city!\n")
3136           c->setVectoring(Vector<int>(-1,-1));
3137           continue;
3138         }
3139     }
3140
3141   for (Citylist::iterator cit = cl->begin(); cit != cl->end(); ++cit)
3142     {
3143       sbusy.emit();
3144       City *c = *cit;
3145       if (c->getOwner() != this || c->isBurnt())
3146         continue;
3147       City *enemy_city = cl->getNearestEnemyCity(c->getPos());
3148       if (!enemy_city)
3149         continue;
3150       City *vector_city = NULL;
3151       //if the city isn't already vectoring
3152       if (c->getVectoring() == Vector<int>(-1,-1))
3153         {
3154           bool vectored = AI_maybeVector(c, safe_mp, min_defenders, enemy_city, 
3155                                          &vector_city);
3156           if (vectored)
3157             debug("begin vectoring from " << c->getName() <<" to " << vector_city->getName() << "!\n");
3158         }
3159     }
3160 }
3161
3162 const Army * Player::doCityProducesArmy(City *city, Vector<int> &pos)
3163 {
3164   int cost = city->getActiveProductionBase()->getProductionCost();
3165   if (cost > d_gold)
3166     return NULL;
3167   withdrawGold(cost);
3168   const Army *a = city->armyArrives(pos);
3169   return a;
3170 }
3171
3172 bool Player::cityProducesArmy(City *city)
3173 {
3174   assert(city->getOwner() == this);
3175   Action_Produce *item = new Action_Produce();
3176   Vector<int> pos;
3177   const Army *army = doCityProducesArmy(city, pos);
3178   if (army)
3179     {
3180       const ArmyProdBase *source_army;
3181       source_army = city->getProductionBaseBelongingTo(army);
3182       if (city->getVectoring() == Vector<int>(-1, -1))
3183         item->fillData(source_army, city, false, pos, army->getId());
3184       else
3185         item->fillData(source_army, city, true, city->getVectoring(), army->getId());
3186       addAction(item);
3187     }
3188   return true;
3189 }
3190
3191 Army* Player::doVectoredUnitArrives(VectoredUnit *unit)
3192 {
3193   Army *army = unit->armyArrives();
3194   return army;
3195 }
3196
3197 bool Player::vectoredUnitArrives(VectoredUnit *unit)
3198 {
3199   Action_ProduceVectored *item = new Action_ProduceVectored();
3200   item->fillData(unit->getArmy(), unit->getDestination(), unit->getPos());
3201   addAction(item);
3202   Army *army = doVectoredUnitArrives(unit);
3203   if (!army)
3204     {
3205       printf("this was supposed to be impossible because of operations on the vectoredunitlist after the city is conquered.\n");
3206       printf("whooops... this vectored unit failed to show up.\n");
3207       City *dest = GameMap::getCity(unit->getDestination());
3208       printf("the unit was being vectored to: %s, from %s by %s\n", 
3209              dest->getName().c_str(), 
3210              GameMap::getCity(unit->getPos())->getName().c_str(), getName().c_str());
3211       printf("Army is a %s, turns is %d + 1\n", unit->getArmy()->getName().c_str(), unit->getArmy()->getProduction());
3212
3213       
3214       int turn = -1;
3215   std::list<History*> h = dest->getOwner()->getHistoryForCityId(dest->getId());
3216   std::list<History*>::const_iterator pit;
3217   for (pit = h.begin(); pit != h.end(); pit++)
3218     {
3219       switch ((*pit)->getType())
3220         {
3221         case History::START_TURN:
3222             {
3223               turn++;
3224               break;
3225             }
3226         case History::CITY_WON:
3227             {
3228               History_CityWon *event;
3229               event = dynamic_cast<History_CityWon*>(*pit);
3230               //printf("on turn %d, player %s took %s\n", turn, dest->getOwner()->getName().c_str(), dest->getName().c_str());
3231               break;
3232             }
3233         case History::CITY_RAZED:
3234             {
3235               History_CityRazed *event;
3236               event = dynamic_cast<History_CityRazed*>(*pit);
3237               printf("on turn %d, player %s razed %s\n", turn, dest->getOwner()->getName().c_str(), dest->getName().c_str());
3238               break;
3239             }
3240         default:
3241           break;
3242         }
3243     }
3244       printf("was the destination city owned by us way back then?\n");
3245       exit (1);
3246     }
3247
3248   return true;
3249 }
3250
3251 std::list<Action_Produce *> Player::getUnitsProducedThisTurn() const
3252 {
3253   std::list<Action_Produce *> actions;
3254   std::list<Action *>::const_reverse_iterator it = d_actions.rbegin();
3255   for (; it != d_actions.rend(); it++)
3256     {
3257       if ((*it)->getType() == Action::PRODUCE_UNIT)
3258         actions.push_back(dynamic_cast<Action_Produce*>(*it));
3259       else if ((*it)->getType() == Action::INIT_TURN)
3260         break;
3261     }
3262   return actions;
3263 }
3264 std::list<Action *> Player::getReportableActions() const
3265 {
3266   std::list<Action *> actions;
3267   std::list<Action *>::const_iterator it = d_actions.begin();
3268   for (; it != d_actions.end(); it++)
3269     {
3270       if ((*it)->getType() == Action::PRODUCE_UNIT ||
3271           (*it)->getType() == Action::PRODUCE_VECTORED_UNIT ||
3272           (*it)->getType() == Action::CITY_DESTITUTE)
3273         actions.push_back(*it);
3274     }
3275   return actions;
3276 }
3277
3278 void Player::cityTooPoorToProduce(City *city, int slot)
3279 {
3280   cityChangeProduction(city, -1);
3281   const ArmyProdBase *a = city->getProductionBase(slot);
3282   Action_CityTooPoorToProduce *action = new Action_CityTooPoorToProduce();
3283   action->fillData(city, a);
3284   addAction(action);
3285 }
3286
3287 void Player::pruneActionlist()
3288 {
3289   pruneActionlist(d_actions);
3290 }
3291
3292 void Player::pruneCityProductions(std::list<Action*> actions)
3293 {
3294   //remove duplicate city production actions
3295
3296   //enumerate the ones we want
3297   std::list<Action_Production*> keepers;
3298   std::list<Action*>::reverse_iterator ait;
3299   for (ait = actions.rbegin(); ait != actions.rend(); ait++)
3300     {
3301       if ((*ait)->getType() != Action::CITY_PROD)
3302         continue;
3303       //if this city isn't already in the keepers list, then add it.
3304
3305       Action_Production *action = static_cast<Action_Production*>(*ait);
3306       bool found = false;
3307       std::list<Action_Production*>::const_iterator it;
3308       for (it = keepers.begin(); it != keepers.end(); it++)
3309         {
3310           if (action->getCityId() == (*it)->getCityId())
3311             {
3312               found = true;
3313               break;
3314             }
3315         }
3316       if (found == false)
3317         keepers.push_back(action);
3318
3319     }
3320
3321   //now delete all city production events that aren't in keepers
3322   int total = 0;
3323   std::list<Action*>::iterator bit;
3324   for (bit = actions.begin(); bit != actions.end(); bit++)
3325     {
3326       if ((*bit)->getType() != Action::CITY_PROD)
3327         continue;
3328       if (find (keepers.begin(), keepers.end(), (*bit)) == keepers.end())
3329         {
3330           total++;
3331           actions.erase (bit);
3332           bit = actions.begin();
3333           continue;
3334         }
3335     }
3336   //if (total)
3337   //printf ("pruned %d city production actions.\n", total);
3338 }
3339
3340 void Player::pruneCityVectorings(std::list<Action*> actions)
3341 {
3342   //remove duplicate city vectoring actions
3343
3344   //enumerate the ones we want
3345   std::list<Action_Vector*> keepers;
3346   std::list<Action*>::reverse_iterator ait;
3347   for (ait = actions.rbegin(); ait != actions.rend(); ait++)
3348     {
3349       if ((*ait)->getType() != Action::CITY_VECTOR)
3350         continue;
3351       //if this city isn't already in the keepers list, then add it.
3352
3353       Action_Vector *action = static_cast<Action_Vector *>(*ait);
3354       bool found = false;
3355       std::list<Action_Vector*>::const_iterator it;
3356       for (it = keepers.begin(); it != keepers.end(); it++)
3357         {
3358           if (action->getCityId() == (*it)->getCityId())
3359             {
3360               found = true;
3361               break;
3362             }
3363         }
3364       if (found == false)
3365         keepers.push_back(action);
3366
3367     }
3368
3369   //now delete all city vector events that aren't in keepers
3370   int total = 0;
3371   std::list<Action*>::iterator bit;
3372   for (bit = actions.begin(); bit != actions.end(); bit++)
3373     {
3374       if ((*bit)->getType() != Action::CITY_VECTOR)
3375         continue;
3376       if (find (keepers.begin(), keepers.end(), (*bit)) == keepers.end())
3377         {
3378           total++;
3379           actions.erase (bit);
3380           bit = actions.begin();
3381           continue;
3382         }
3383     }
3384   //if (total)
3385   //printf ("pruned %d city vector actions.\n", total);
3386 }
3387
3388 void Player::pruneActionlist(std::list<Action*> actions)
3389 {
3390   pruneCityProductions(actions);
3391   pruneCityVectorings(actions);
3392
3393 }
3394
3395 std::string Player::playerTypeToString(const Player::Type type)
3396 {
3397   switch (type)
3398     {
3399     case Player::HUMAN:
3400       return "Player::HUMAN";
3401     case Player::AI_FAST:
3402       return "Player::AI_FAST";
3403     case Player::AI_DUMMY:
3404       return "Player::AI_DUMMY";
3405     case Player::AI_SMART:
3406       return "Player::AI_SMART";
3407     case Player::NETWORKED:
3408       return "Player::NETWORKED";
3409     }
3410   return "Player::HUMAN";
3411 }
3412
3413 Player::Type Player::playerTypeFromString(const std::string str)
3414 {
3415   if (str.size() > 0 && isdigit(str.c_str()[0]))
3416     return Player::Type(atoi(str.c_str()));
3417   if (str == "Player::HUMAN")
3418     return Player::HUMAN;
3419   else if (str == "Player::AI_FAST")
3420     return Player::AI_FAST;
3421   else if (str == "Player::AI_DUMMY")
3422     return Player::AI_DUMMY;
3423   else if (str == "Player::AI_SMART")
3424     return Player::AI_SMART;
3425   else if (str == "Player::NETWORKED")
3426     return Player::NETWORKED;
3427   return Player::HUMAN;
3428 }
3429
3430 bool Player::hasAlreadyInitializedTurn() const
3431 {
3432   for (list<Action*>::const_iterator it = d_actions.begin();
3433        it != d_actions.end(); it++)
3434     if ((*it)->getType() == Action::INIT_TURN)
3435       return true;
3436   return false;
3437 }
3438
3439 bool Player::hasAlreadyEndedTurn() const
3440 {
3441   for (list<Action*>::const_iterator it = d_actions.begin();
3442        it != d_actions.end(); it++)
3443     if ((*it)->getType() == Action::END_TURN)
3444       return true;
3445   return false;
3446 }
3447
3448 std::list<History*> Player::getHistoryForThisTurn() const
3449 {
3450   std::list<History*> history;
3451   for (list<History*>::const_reverse_iterator it = d_history.rbegin();
3452        it != d_history.rend(); it++)
3453     {
3454       history.push_front(*it);
3455       if ((*it)->getType() == History::START_TURN)
3456         break;
3457     }
3458   return history;
3459 }
3460
3461 guint32 Player::countEndTurnHistoryEntries() const
3462 {
3463   guint32 count = 0;
3464   for (list<History*>::const_iterator it = d_history.begin();
3465        it != d_history.end(); it++)
3466     {
3467       if ((*it)->getType() == History::END_TURN)
3468         count++;
3469     }
3470   return count;
3471 }
3472
3473 void Player::loadPbmGame() 
3474 {
3475   for (list<Action*>::const_iterator it = d_actions.begin();
3476        it != d_actions.end(); it++)
3477     {
3478       NetworkAction *copy = new NetworkAction(*it, getId());
3479       acting.emit(copy);
3480     }
3481   std::list<History*> history = getHistoryForThisTurn();
3482   for (list<History*>::const_iterator it = history.begin();
3483        it != history.end(); it++)
3484     {
3485       NetworkHistory *copy = new NetworkHistory(*it, getId());
3486       history_written.emit(copy);
3487     }
3488 }
3489
3490 void Player::saveNetworkActions(XML_Helper *helper) const
3491 {
3492   for (list<Action*>::const_iterator it = d_actions.begin();
3493        it != d_actions.end(); it++)
3494     {
3495       NetworkAction *copy = new NetworkAction(*it, getId());
3496       copy->save(helper);
3497     }
3498 }
3499
3500 bool Player::searchedRuin(Ruin *r) const
3501 {
3502   if (!r)
3503     return false;
3504   for (list<History*>::const_iterator it = d_history.begin();
3505        it != d_history.end(); it++)
3506     {
3507       if ((*it)->getType() == History::HERO_RUIN_EXPLORED)
3508         {
3509           History_HeroRuinExplored *event = 
3510             dynamic_cast<History_HeroRuinExplored*>(*it);
3511           if (event->getRuinId() == r->getId())
3512             return true;
3513         }
3514     }    
3515   return false;
3516 }
3517
3518 bool Player::conqueredCity(City *c) const
3519 {
3520   if (!c)
3521     return false;
3522   for (list<History*>::const_iterator it = d_history.begin();
3523        it != d_history.end(); it++)
3524     {
3525       if ((*it)->getType() == History::CITY_WON)
3526         {
3527           History_CityWon *event = dynamic_cast<History_CityWon*>(*it);
3528           if (event->getCityId() == c->getId())
3529             return true;
3530         }
3531     }    
3532   return false;
3533 }
3534
3535 std::list<Vector<int> > Player::getStackTrack(Stack *s) const
3536 {
3537   std::list<Vector<int> > points;
3538   Vector<int> delta = Vector<int>(0,0);
3539   for (list<Action*>::const_iterator it = d_actions.begin();
3540        it != d_actions.end(); it++)
3541     {
3542       if ((*it)->getType() == Action::STACK_MOVE)
3543         {
3544           Action_Move *action = dynamic_cast<Action_Move*>(*it);
3545           if (action->getStackId() == s->getId())
3546             {
3547               if (points.size() == 0)
3548                 delta = action->getPositionDelta();
3549               points.push_back(action->getEndingPosition());
3550             }
3551         }
3552     }
3553   if (points.size() >= 1)
3554     {
3555       Vector<int> pos = points.front() + delta;
3556       if (pos != points.front())
3557         points.push_front(pos);
3558     }
3559   return points;
3560 }
3561         
3562 std::list<History *>Player::getHistoryForCityId(guint32 id) const
3563 {
3564   std::list<History*> events;
3565   std::list<History*>::const_iterator pit;
3566   for (pit = d_history.begin(); pit != d_history.end(); pit++)
3567     {
3568       switch ((*pit)->getType())
3569         {
3570         case History::START_TURN:
3571             {
3572               events.push_back(*pit);
3573               break;
3574             }
3575         case History::CITY_WON:
3576             {
3577               History_CityWon *event;
3578               event = dynamic_cast<History_CityWon*>(*pit);
3579               if (event->getCityId() == id)
3580                 events.push_back(*pit);
3581               break;
3582             }
3583         case History::CITY_RAZED:
3584             {
3585               History_CityRazed *event;
3586               event = dynamic_cast<History_CityRazed*>(*pit);
3587               if (event->getCityId() == id)
3588                 events.push_back(*pit);
3589               break;
3590             }
3591         default:
3592           break;
3593         }
3594     }
3595   return events;
3596 }
3597
3598 std::list<History *>Player::getHistoryForHeroId(guint32 id) const
3599 {
3600   std::string hero_name = "";
3601   std::list<History*> events;
3602   std::list<History*>::const_iterator pit;
3603   for (pit = d_history.begin(); pit != d_history.end(); pit++)
3604     {
3605       switch ((*pit)->getType())
3606         {
3607         case History::HERO_EMERGES:
3608             {
3609               History_HeroEmerges *event;
3610               event = dynamic_cast<History_HeroEmerges *>(*pit);
3611               if (event->getHeroId() == id)
3612                 {
3613                   hero_name = event->getHeroName();
3614                   events.push_back(*pit);
3615                 }
3616               break;
3617             }
3618         case History::FOUND_SAGE:
3619             {
3620               History_FoundSage *event;
3621               event = dynamic_cast<History_FoundSage*>(*pit);
3622               if (event->getHeroName() == hero_name)
3623                 events.push_back(*pit);
3624               break;
3625             }
3626         case History::HERO_QUEST_STARTED:
3627             {
3628               History_HeroQuestStarted *event;
3629               event = dynamic_cast<History_HeroQuestStarted*>(*pit);
3630               if (event->getHeroName() == hero_name)
3631                 events.push_back(*pit);
3632               break;
3633             }
3634         case History::HERO_QUEST_COMPLETED:
3635             {
3636               History_HeroQuestCompleted *event;
3637               event = dynamic_cast<History_HeroQuestCompleted*>(*pit);
3638               if (event->getHeroName() == hero_name)
3639                 events.push_back(*pit);
3640               break;
3641             }
3642         case History::HERO_KILLED_IN_CITY:
3643             {
3644               History_HeroKilledInCity *event;
3645               event = dynamic_cast<History_HeroKilledInCity*>(*pit);
3646               if (event->getHeroName() == hero_name)
3647                 events.push_back(*pit);
3648               break;
3649             }
3650         case History::HERO_KILLED_IN_BATTLE:
3651             {
3652               History_HeroKilledInBattle *event;
3653               event = dynamic_cast<History_HeroKilledInBattle*>(*pit);
3654               if (event->getHeroName() == hero_name)
3655                 events.push_back(*pit);
3656               break;
3657             }
3658         case History::HERO_KILLED_SEARCHING:
3659             {
3660               History_HeroKilledSearching*event;
3661               event = dynamic_cast<History_HeroKilledSearching*>(*pit);
3662               if (event->getHeroName() == hero_name)
3663                 events.push_back(*pit);
3664               break;
3665             }
3666         case History::HERO_CITY_WON:
3667             {
3668               History_HeroCityWon *event;
3669               event = dynamic_cast<History_HeroCityWon*>(*pit);
3670               if (event->getHeroName() == hero_name)
3671                 events.push_back(*pit);
3672               break;
3673             }
3674         case History::HERO_FINDS_ALLIES:
3675             {
3676               History_HeroFindsAllies *event;
3677               event = dynamic_cast<History_HeroFindsAllies*>(*pit);
3678               if (event->getHeroName() == hero_name)
3679                 events.push_back(*pit);
3680               break;
3681             }
3682         default:
3683           break;
3684         }
3685     }
3686   return events;
3687 }
3688
3689 void Player::setSurrendered(bool surr)
3690 {
3691   surrendered = surr;
3692 }
3693 std::list<Hero*> Player::getHeroes() const
3694 {
3695   return d_stacklist->getHeroes();
3696 }
3697 guint32 Player::countArmies() const
3698 {
3699   return d_stacklist->countArmies();
3700 }
3701 guint32 Player::countAllies() const
3702 {
3703   return d_stacklist->countAllies();
3704 }
3705 Stack * Player::getActivestack() const
3706 {
3707   return d_stacklist->getActivestack();
3708 }
3709 void Player::setActivestack(Stack *s)
3710 {
3711   d_stacklist->setActivestack(s);
3712 }
3713         
3714 Vector<int> Player::getPositionOfArmyById(guint32 id) const
3715 {
3716   return d_stacklist->getPosition(id);
3717 }
3718
3719 void Player::immobilize()
3720 {
3721   d_stacklist->drainAllMovement();
3722 }
3723
3724 guint32 Player::getCostOfUnitsProducedThisTurn() const
3725 {
3726   guint32 gold = 0;
3727   std::list<Action_Produce *> units = getUnitsProducedThisTurn();
3728   for (std::list<Action_Produce *>::const_iterator it = units.begin(); it != units.end(); it++)
3729     gold +=(*it)->getArmy()->getProductionCost();
3730     
3731   return gold;
3732
3733 }
3734           
3735 void Player::clearStacklist()
3736 {
3737   d_stacklist->flClear();
3738 }
3739
3740 void Player::clearFogMap()
3741 {
3742   d_fogmap->fill(FogMap::OPEN);
3743 }
3744
3745
3746 std::list<Action *> Player::getActionsThisTurn(int type) const
3747 {
3748   std::list<Action *> actions;
3749   std::list<Action *>::const_iterator it = d_actions.begin();
3750   for (; it != d_actions.end(); it++)
3751     {
3752       if ((*it)->getType() == Action::Type(type))
3753         actions.push_back(*it);
3754     }
3755   return actions;
3756 }
3757 std::list<Action *> Player::getFightsThisTurn() const
3758 {
3759   return getActionsThisTurn(Action::STACK_FIGHT);
3760 }
3761
3762 int Player::countFightsThisTurn() const
3763 {
3764   return getFightsThisTurn().size();
3765 }
3766
3767 std::list<Action *> Player::getMovesThisTurn() const
3768 {
3769   return getActionsThisTurn(Action::STACK_MOVE);
3770 }
3771
3772 int Player::countMovesThisTurn() const
3773 {
3774   return getMovesThisTurn().size();
3775 }
3776         
3777 int Player::countDestituteCitiesThisTurn() const
3778 {
3779   return getActionsThisTurn(Action::CITY_DESTITUTE).size();
3780 }
3781
3782 Vector<int> Player::AI_getQuestDestination(Quest *quest, Stack *stack) const
3783 {
3784   Playerlist *pl = Playerlist::getInstance();
3785   Vector<int> dest = Vector<int>(-1,-1);
3786   switch (quest->getType())
3787     {
3788     case Quest::KILLHERO:
3789         {
3790           QuestKillHero *q = dynamic_cast<QuestKillHero*>(quest);
3791           guint32 hero_id = q->getVictim();
3792           Stack *enemy = NULL;
3793           for (Playerlist::iterator it = pl->begin(); it != pl->end(); it++)
3794             {
3795               if (*it == this)
3796                 continue;
3797               enemy = (*it)->getStacklist()->getArmyStackById(hero_id);
3798               if(enemy)
3799                 break;
3800             }
3801           if (enemy)
3802             dest = enemy->getPos();
3803
3804         }
3805       break;
3806     case Quest::KILLARMYTYPE:
3807         {
3808           QuestEnemyArmytype *q = dynamic_cast<QuestEnemyArmytype*>(quest);
3809           guint32 army_type = q->getArmytypeToKill();
3810           std::list<Stack*> s = 
3811             GameMap::getNearbyEnemyStacks(stack->getPos(), GameMap::getWidth());
3812           for (std::list<Stack*>::iterator i = s.begin(); i != s.end(); i++)
3813             {
3814               if ((*i)->hasArmyType(army_type) == true)
3815                 {
3816                   dest = (*i)->getPos();
3817                   break;
3818                 }
3819             }
3820         }
3821       break;
3822     case Quest::KILLARMIES:
3823         {
3824           QuestEnemyArmies *q = dynamic_cast<QuestEnemyArmies*>(quest);
3825           Player *enemy = pl->getPlayer(q->getVictimPlayerId());
3826           std::list<Stack*> s = 
3827             GameMap::getNearbyEnemyStacks(stack->getPos(), GameMap::getWidth());
3828           for (std::list<Stack*>::iterator i = s.begin(); i != s.end(); i++)
3829             {
3830               if ((*i)->getOwner() != enemy)
3831                 continue;
3832               dest = (*i)->getPos();
3833             }
3834         }
3835       break;
3836
3837     case Quest::PILLAGEGOLD:
3838     case Quest::CITYSACK:
3839     case Quest::CITYRAZE:
3840     case Quest::CITYOCCUPY:
3841       //attack the nearest enemy city.
3842         {
3843           City *c = Citylist::getInstance()->getClosestEnemyCity(stack);
3844           if (c)
3845             dest = c->getNearestPos(stack->getPos());
3846         }
3847       break;
3848     }
3849   return dest;
3850 }
3851
3852 bool Player::AI_invadeCityQuestPreference(City *c, CityDefeatedAction &action) const
3853 {
3854   bool found = false;
3855   std::vector<Quest*> q = QuestsManager::getInstance()->getPlayerQuests(this);
3856   for (std::vector<Quest*>::iterator i = q.begin(); i != q.end(); i++)
3857     {
3858       switch ((*i)->getType())
3859         {
3860         case Quest::CITYOCCUPY:
3861             {
3862               QuestCityOccupy* qu = dynamic_cast<QuestCityOccupy*>(*i);
3863               if (qu->getCityId() == c->getId())
3864                 {
3865                   action = CITY_DEFEATED_OCCUPY;
3866                   found = true;
3867                 }
3868             }
3869           break;
3870         case Quest::CITYSACK:
3871             {
3872               QuestCitySack * qu = dynamic_cast<QuestCitySack*>(*i);
3873               if (qu->getCityId() == c->getId())
3874                 {
3875                   action = CITY_DEFEATED_SACK;
3876                   found = true;
3877                 }
3878             }
3879           break;
3880         case Quest::CITYRAZE:
3881             {
3882               QuestCityRaze* qu = dynamic_cast<QuestCityRaze*>(*i);
3883               if (qu->getCityId() == c->getId())
3884                 {
3885                   action = CITY_DEFEATED_RAZE;
3886                   found = true;
3887                 }
3888             }
3889           break;
3890         case Quest::PILLAGEGOLD:
3891           action = CITY_DEFEATED_SACK;
3892           found = true;
3893           break;
3894         }
3895     }
3896   return found;
3897 }
3898
3899 /*
3900  *
3901  * what are the chances of a hero showing up?
3902  *
3903  * 1 in 6 if you have enough gold, where "enough gold" is...
3904  *
3905  * ... 1500 if the player already has a hero, then:  1500 is generally 
3906  * enough to buy all the heroes.  I forget the exact distribution of 
3907  * hero prices but memory says from 1000 to 1500.  (But, if you don't 
3908  * have 1500 gold, and the price is less, you still get the offer...  
3909  * So, calculate price, compare to available gold, then decided whether 
3910  * or not to offer...)
3911  *
3912  * ...500 if all your heroes are dead: then prices are cut by about 
3913  * a factor of 3.
3914  */
3915 bool Player::maybeRecruitHero ()
3916 {
3917   bool accepted = false;
3918   
3919   City *city = NULL;
3920   int gold_needed = 0;
3921   if (Citylist::getInstance()->countCities(this) == 0)
3922     return false;
3923   //give the player a hero if it's the first round.
3924   //otherwise we get a hero based on chance
3925   //a hero costs a random number of gold pieces
3926   if (GameScenarioOptions::s_round == 1 && getHeroes().size() == 0)
3927     gold_needed = 0;
3928   else
3929     {
3930       bool exists = false;
3931       if (getHeroes().size() > 0)
3932           exists = true; 
3933
3934       gold_needed = (rand() % 500) + 1000;
3935       if (exists == false)
3936         gold_needed /= 2;
3937     }
3938
3939   if ((((rand() % 6) == 0 && gold_needed < getGold()) || gold_needed == 0))
3940     {
3941       HeroProto *heroproto = 
3942         HeroTemplates::getInstance()->getRandomHero(getId());
3943       if (gold_needed == 0)
3944         {
3945           //we do it this way because maybe quickstart is on.
3946           Citylist* cl = Citylist::getInstance();
3947           for (Citylist::iterator it = cl->begin(); it != cl->end(); ++it)
3948             if (!(*it)->isBurnt() && (*it)->getOwner() == this &&
3949                 (*it)->getCapitalOwner() == this && (*it)->isCapital())
3950               {
3951                 city = *it;
3952                 break;
3953               }
3954           if (!city) //no capital cities
3955             city = Citylist::getInstance()->getFirstCity(this);
3956         }
3957       else
3958         {
3959           std::vector<City*> cities;
3960           Citylist* cl = Citylist::getInstance();
3961           for (Citylist::iterator it = cl->begin(); it != cl->end(); ++it)
3962             if (!(*it)->isBurnt() && (*it)->getOwner() == this)
3963               cities.push_back((*it));
3964           if (cities.empty())
3965             return false;
3966           city = cities[rand() % cities.size()];
3967         }
3968
3969       if (srecruitingHero.empty())
3970         accepted = true;
3971       else if (city)
3972         accepted = srecruitingHero.emit(heroproto, city, gold_needed);
3973
3974       if (accepted) {
3975         /* now maybe add a few allies */
3976         int alliesCount;
3977         if (gold_needed > 1300)
3978           alliesCount = 3;
3979         else if (gold_needed > 1000)
3980           alliesCount = 2;
3981         else if (gold_needed > 800)
3982           alliesCount = 1;
3983         else
3984           alliesCount = 0;
3985
3986         const ArmyProto *ally = 0;
3987         if (alliesCount > 0)
3988         {
3989           ally = Reward_Allies::randomArmyAlly();
3990           if (!ally)
3991             alliesCount = 0;
3992         }
3993         
3994         recruitHero(heroproto, city, gold_needed, alliesCount, ally);
3995       }
3996     }
3997   return accepted;
3998 }
3999 // End of file