1 // Copyright (C) 2002, 2003, 2004, 2005, 2006 Ulf Lorenz
2 // Copyright (C) 2003 Michael Bartl
3 // Copyright (C) 2004, 2006 Andrea Paternesi
4 // Copyright (C) 2004 John Farrell
5 // Copyright (C) 2006, 2007, 2008, 2009 Ben Asselstine
6 // Copyright (C) 2007, 2008 Ole Laursen
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Library General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30 #include "playerlist.h"
31 #include "armysetlist.h"
32 #include "stacklist.h"
35 #include "templelist.h"
39 #include "Threatlist.h"
41 #include "xmlhelper.h"
42 #include "AI_Diplomacy.h"
44 #include "GameScenarioOptions.h"
46 #include "vectoredunitlist.h"
47 #include "PathCalculator.h"
48 #include "stacktile.h"
49 #include "armyprodbase.h"
50 #include "QuestsManager.h"
56 #define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<flush<<endl;}
59 AI_Fast::AI_Fast(string name, guint32 armyset, Gdk::Color color, int width, int height, int player_no)
60 :RealPlayer(name, armyset, color, width, height, Player::AI_FAST, player_no), d_join(true),
61 d_maniac(false), d_analysis(0), d_diplomacy(0)
65 AI_Fast::AI_Fast(const Player& player)
66 :RealPlayer(player), d_join(true), d_maniac(false), d_analysis(0),
72 AI_Fast::AI_Fast(XML_Helper* helper)
73 :RealPlayer(helper), d_analysis(0), d_diplomacy(0)
75 helper->getData(d_join, "join");
76 helper->getData(d_maniac, "maniac");
87 bool AI_Fast::save(XML_Helper* helper) const
91 retval &= helper->openTag(Player::d_tag);
92 retval &= helper->saveData("join", d_join);
93 retval &= helper->saveData("maniac", d_maniac);
94 retval &= Player::save(helper);
95 retval &= helper->closeTag();
100 void AI_Fast::abortTurn()
102 abort_requested = true;
105 else if (Playerlist::getInstance()->countPlayersAlive() == 1)
109 bool AI_Fast::startTurn()
114 if (getStacklist()->getHeroes().size() == 0 &&
115 Citylist::getInstance()->countCities(this) == 1)
116 AI_maybeBuyScout(Citylist::getInstance()->getFirstCity(this));
119 debug(getName() << ": AI_Fast::start_turn")
120 debug("being in " <<(d_maniac?"maniac":"normal") <<" mode")
121 debug((d_join?"":"not ") <<"joining armies")
123 d_analysis = new AI_Analysis(this);
124 d_diplomacy = new AI_Diplomacy(this);
126 d_diplomacy->considerCuspOfWar();
130 if (getUpkeep() > getIncome() * ratio)
134 debug("examining cities");
135 Citylist *cl = Citylist::getInstance();
136 for (Citylist::iterator cit = cl->begin(); cit != cl->end(); ++cit)
139 if (c->getOwner() != this || c->isBurnt())
141 if (c->getActiveProductionSlot() == -1)
142 setBestProduction(c);
146 debug("setting up vectoring");
148 AI_setupVectoring(18, 3, 30);
152 debug("trying to complete quests");
153 //try to complete our quests
154 std::vector<Quest*> q = QuestsManager::getInstance()->getPlayerQuests(this);
155 for (std::vector<Quest*>::iterator it = q.begin(); it != q.end(); it++)
158 if (quest->isPendingDeletion())
160 Stack *s = getStacklist()->getArmyStackById(quest->getHeroId());
163 Vector<int> dest = AI_getQuestDestination(quest, s);
164 if (dest == Vector<int>(-1,-1))
166 d_stacklist->setActivestack(s);
167 s->getPath()->calculate(s, dest);
168 bool stack_moved = stackMove(s);
169 if (d_stacklist->getActivestack() && stack_moved)
170 GameMap::groupStacks(s);
173 while (computerTurn() == true)
178 //are there any stacks with paths that can move?
179 for (Stacklist::reverse_iterator it = d_stacklist->rbegin(); it != d_stacklist->rend(); it++)
182 if (s->getPath()->size() > 0 && s->enoughMoves())
184 int mp = s->getPath()->calculate(s, s->getLastPointInPath());
187 debug ("AI_FAST stack " << s->getId() << " can still potentially move");
188 debug ("moving from (" << s->getPos().x << "," << s->getPos().y
189 << ") to (" <<s->getFirstPointInPath().x << "," <<
190 s->getFirstPointInPath().y << ") with " << s->getMoves() <<" left");
195 //are there any stacks without paths that still have some moves?
196 else if (s->getPath()->size() == 0 && s->getMoves() > 1)
210 d_stacklist->setActivestack(0);
212 // Declare war with enemies, make peace with friends
213 if (GameScenarioOptions::s_diplomacy)
214 d_diplomacy->makeProposals();
221 int AI_Fast::scoreArmyType(const ArmyProdBase *a)
223 int max_strength = a->getStrength();
226 if (a->getProduction() == 1)
228 else if (a->getProduction() == 2)
234 if (a->getUpkeep() < 5)
236 else if (a->getUpkeep() < 10)
242 if (a->getProductionCost() < 5)
244 else if (a->getProductionCost() < 10)
249 //we prefer armies that move farther
251 if (a->getMaxMoves() > 10)
253 if (a->getMaxMoves() >= 20)
256 return max_strength + move_bonus + production + upkeep + newcost;
259 int AI_Fast::setBestProduction(City *c)
264 // we try to determine the most attractive basic production
265 for (guint32 i = 0; i < c->getMaxNoOfProductionBases(); i++)
267 if (c->getArmytype(i) == -1) // no production in this slot
270 const ArmyProdBase *proto = c->getProductionBase(i);
271 if (scoreArmyType(proto) > score)
274 score = scoreArmyType(proto);
279 if (select != c->getActiveProductionSlot())
281 cityChangeProduction(c, select);
282 debug(getName() << " Set production to slot " << select << " in " << c->getName())
285 return c->getActiveProductionSlot();
288 void AI_Fast::invadeCity(City* c)
290 CityDefeatedAction action = CITY_DEFEATED_OCCUPY;
291 bool quest_preference = AI_invadeCityQuestPreference(c, action);
292 debug("Invaded city " <<c->getName());
294 if (quest_preference == false)
296 if (getIncome() < getUpkeep())
297 action = CITY_DEFEATED_OCCUPY;
299 action = CITY_DEFEATED_RAZE;
301 action = CITY_DEFEATED_OCCUPY;
304 int pillaged_army_type = -1;
305 std::list<guint32> sacked_army_types;
308 case CITY_DEFEATED_OCCUPY:
310 setBestProduction(c);
312 case CITY_DEFEATED_PILLAGE:
313 cityPillage(c, gold, &pillaged_army_type);
315 setBestProduction(c);
317 case CITY_DEFEATED_RAZE:
320 case CITY_DEFEATED_SACK:
321 citySack(c, gold, &sacked_army_types);
323 setBestProduction(c);
328 void AI_Fast::heroGainsLevel(Hero * a)
330 debug("Army raised a level, id = " <<a->getId())
333 // increase the strength attack (uninnovative, but enough here)
334 Army::Stat stat = Army::STRENGTH;
335 doHeroGainsLevel(a, stat);
337 Action_Level* item = new Action_Level();
338 item->fillData(a, stat);
342 Stack *AI_Fast::findNearOwnStackToJoin(Stack *s, int max_distance)
345 std::list<Stack*> stks;
346 stks = GameMap::getNearbyFriendlyStacks(s->getPos(), max_distance);
347 if (stks.size() <= 0)
349 PathCalculator pc(s);
350 Stack* target = NULL;
351 for (std::list<Stack*>::iterator it = stks.begin(); it != stks.end(); it++)
356 //is this a stack that is co-located?
357 if (s->getPos() == (*it)->getPos())
360 //does the destination have few enough army units to join?
361 if (GameMap::canJoin(s, (*it)) == false)
364 //is the tile distance under the threshold?
365 int distance = dist(s->getPos(), (*it)->getPos());
367 if (distance <= max_distance)
369 //can we actually get there?
370 int mp = pc.calculate((*it)->getPos());
373 if (mp < min_mp || min_mp == -1)
385 bool AI_Fast::computerTurn()
387 bool stack_moved = false;
388 // we have configurable behaviour in two ways:
389 // 1. join => merges close stacks
390 // 2. maniac => attack at any costs, raze everything in the path
392 // So the basic algorithm is like
394 // if !maniac, and close to a temple, visit it
395 // if (army_to_join close && d_join)
397 // if (!maniac && army_damaged)
400 // find next enemy city
402 // find next enemy unit with preference to cities
405 // return true if any stack moved
407 // we are using reversed order because new stacks come behind old stacks
408 // and we want the freshly created stacks join the veterans and not the other
410 //d_stacklist->dump();
412 std::list<Vector<int> > points = d_stacklist->getPositions();
413 for (std::list<Vector<int> >::iterator it = points.begin();
414 it != points.end(); it++)
416 Stack *s = GameMap::getFriendlyStack(*it);
420 d_stacklist->setActivestack(s);
422 //go to a temple or ruin
425 bool stack_died = false;
426 bool blessed = false;
427 if (s->isOnCity() == false && s->hasHero() == false)
429 stack_moved = AI_maybeVisitTempleForBlessing
430 (s, s->getMoves(), s->getMoves() + 7, 50.0,
431 blessed, stack_died);
434 s = d_stacklist->getActivestack();
437 GameMap::groupStacks(s);
442 else if (s->isOnCity() == false && s->hasHero() == true)
444 stack_moved = AI_maybeVisitTempleForQuest
445 (s, s->getMoves(), s->getMoves() + 15, stack_died);
450 stack_moved = AI_maybeVisitRuin
451 (s, s->getMoves(), s->getMoves() + 15, stack_died);
455 s = d_stacklist->getActivestack();
458 GameMap::groupStacks(s);
468 bool stack_died = false;
469 bool picked_up = false;
471 stack_moved = AI_maybePickUpItems(s, s->getMoves(),
473 picked_up, stack_died);
476 s = d_stacklist->getActivestack();
477 if (picked_up && stack_moved)
478 stack_moved = false; //do this so we move it later on
479 else if (stack_moved)
483 debug(">>>> What to do with stack " <<s->getId() <<" at (" <<s->getPos().x
484 <<"," <<s->getPos().y <<") containing " <<s->size() << " armies ?")
486 // join armies if close
487 if (d_join && s->isFull() == false)
489 Stack* target = NULL;
490 target = findNearOwnStackToJoin(s, 5);
494 debug("Joining with stack " <<target->getId() <<" at (" <<target->getPos().x <<"," <<target->getPos().y <<")")
495 s->getPath()->calculate(s, target->getPos());
496 stack_moved |= stackMove(s);
497 //in case we lost our stack
498 if (!d_stacklist->getActivestack())
500 if (s->getPos() == target->getPos())
502 GameMap::groupStacks(s);
509 // second step: try to resupply
512 City *target = Citylist::getInstance()->getNearestFriendlyCity(s->getPos());
513 if (s->isFull() == false && target)
515 debug("Restocking in " <<target->getName())
516 // try to move to the north west part of the city (where the units
517 // move after production), otherwise just wait and stand around
519 if (target->contains(s->getPos()) == false)
521 debug("Stack is not in " << target->getName() << " yet" <<endl);
522 int mp = s->getPath()->calculateToCity(s, target);
525 stack_moved |= stackMove(s);
527 // the stack could have joined another stack waiting there
528 if (!d_stacklist->getActivestack())
532 GameMap::groupStacks(s);
538 else if (s->getPos() != target->getPos())
540 debug("Stack is inside " << target->getName() << endl);
541 //if we're not in the upper right corner
542 s->getPath()->calculate(s, target->getPos());
543 //go there, and take as many as we can
544 Stack *new_stack = NULL;
545 stack_moved |= stackSplitAndMove(s, new_stack);
546 //in case we lost our stack
547 if (!d_stacklist->getActivestack())
551 GameMap::groupStacks(s);
553 GameMap::groupStacks(target->getPos());
559 //otherwise just stay put in the city
560 GameMap::groupStacks(s);
565 // third step: non-maniac players attack only enemy cities
569 PathCalculator pc(s, true, 10, -1);
570 guint32 moves1 = 0, turns1 = 0, moves2 = 0, turns2 = 0;
571 Path *target1_path = NULL;
572 Path *target2_path = NULL;
573 Citylist *cl = Citylist::getInstance();
574 City *target1 = cl->getNearestEnemyCity(s->getPos());
575 City *target2 = cl->getNearestForeignCity(s->getPos());
577 target1_path = pc.calculateToCity(target1, moves1, turns1);
579 target1_path = new Path();
583 return false; //it's game over and we're still moving
585 target2_path = pc.calculateToCity(target2, moves2, turns2);
587 //no enemies? then go for the nearest foreign city.
588 //if diplomacy isn't on and we hit this, then it's game over
592 //is the enemy city far enough away that a foreign city
594 else if (target1_path->size() / 13 > target2_path->size())
601 if (target == target2)
603 if (GameScenarioOptions::s_diplomacy == true)
604 d_diplomacy->needNewEnemy(target->getOwner());
605 // try to wait a turn until we're at war
610 if (!target) // strange situation
612 cerr << "yet another bad situation!!\n";
617 debug("Attacking " << target->getName() << " (" <<
618 target->getPos().x <<","<< target->getPos().y << ")")
619 int moves = s->getPath()->calculateToCity(s, target);
620 debug("Moves to enemy city: " << moves);
624 stack_moved |= stackMove(s);
625 s = d_stacklist->getActivestack();
626 if (!d_stacklist->getActivestack())
628 //if we didn't get there
629 if (target->getOwner() != s->getOwner())
631 //and the target city is empty
632 if (target->countDefenders() == 0)
634 //attack it if we can reach it.
635 Stack *new_stack = NULL;
636 int moved = stackSplitAndMove(s, new_stack);
637 stack_moved |= moved;
640 GameMap::groupStacks(s);
641 GameMap::groupStacks(target->getPos());
649 // an enemy city is completely surrouned by other stacks, or the way is blocked by a signle enemy stack
650 //let us presume this is temporary and just leave the stack here
651 //for some reason we can't set parked on this thing
652 //and have it realize it, after we return true.
654 printf("crap, it happened with a stack at %d,%d\n", s->getPos().x, s->getPos().y);
655 printf("moves is %d\n", moves);
656 printf("Destination was %d,%d (%s)\n", target->getPos().x, target->getPos().y, target->getName().c_str());
661 // a stack has died ->restart
662 if (!d_stacklist->getActivestack())
670 // fourth step: maniac players attack everything that is close if they can
671 // reach it or cities otherwise.
674 const Threatlist* threats = d_analysis->getThreatsInOrder(s->getPos());
675 Threatlist::const_iterator tit = threats->begin();
676 const Threat* target = 0;
678 // prefer weak forces (take strong if neccessary) and stop after 10
680 for (int i = 0; tit != threats->end() && i < 10; tit++, i++)
682 // in a first step, we only look at enemy stacks
683 if ((*tit)->isCity() || (*tit)->isRuin())
686 // ignore stacks out of reach
687 Vector<int> threatpos = (*tit)->getClosestPoint(s->getPos());
688 if (threatpos == Vector<int>(-1, -1))
691 guint32 mp = s->getPath()->calculate(s, threatpos);
692 if ((int)mp <= 0 || mp > s->getMoves())
700 // now we need to choose. If we found a target, attack it, otherwise
701 // attack the closest city.
702 Vector<int> pos = Vector<int>(-1,-1);
705 pos = target->getClosestPoint(s->getPos());
706 debug("Maniac mode, found target at (" <<pos.x <<"," <<pos.y <<")")
710 Citylist *cl = Citylist::getInstance();
711 City *enemy_city = cl->getNearestForeignCity(s->getPos());
714 pos = enemy_city->getPos();
715 debug("Maniac, found no targets, attacking city " << enemy_city->getName() << " at (" <<pos.x <<"," <<pos.y <<")")
719 if (pos == Vector<int>(-1,-1))
722 int mp = s->getPath()->calculate(s, pos);
725 //printf ("stack %d at %d,%d moving %d with %d moves\n",
726 //s->getId(), s->getPos().x, s->getPos().y,
727 //mp, s->getMoves());
728 bool moved = stackMove(s);
729 //printf("result of move: %d\n", moved);
730 stack_moved |= moved;
731 //in case we lost our stack
732 if (!d_stacklist->getActivestack())
734 s = d_stacklist->getActivestack();
738 printf ("we're going the wrong way (mp is %d)!!\n", mp);
739 printf ("this means we couldn't calculate a path from %d,%d to %d,%d\n", s->getPos().x, s->getPos().y, pos.x, pos.y);
741 Citylist *cl = Citylist::getInstance();
742 City *friendly_city = cl->getNearestFriendlyCity(s->getPos());
745 mp = s->getPath()->calculate(s, friendly_city->getPos());
748 stack_moved |= stackMove(s);
749 //in case we lost our stack
750 if (!d_stacklist->getActivestack())
754 stack_moved |= false;
758 //we can't find anyplace to move to!
760 stack_moved |= false;
764 if (!d_stacklist->getActivestack())
775 bool AI_Fast::chooseTreachery (Stack *stack, Player *player, Vector <int> pos)
777 bool performTreachery = true;
778 return performTreachery;
781 bool AI_Fast::chooseHero(HeroProto *hero, City *city, int gold)
786 Reward *AI_Fast::chooseReward(Ruin *ruin, Sage *sage, Stack *stack)
788 //always pick the money.
789 for (Sage::iterator it = sage->begin(); it != sage->end(); it++)
790 if ((*it)->getType() == Reward::GOLD)
792 return sage->front();
795 Army::Stat AI_Fast::chooseStat(Hero *hero)
797 return Army::STRENGTH;
800 bool AI_Fast::chooseQuest(Hero *hero)