1 // Copyright (C) 2004 John Farrell
2 // Copyright (C) 2004, 2005, 2006 Ulf Lorenz
3 // Copyright (C) 2004, 2005, 2006 Andrea Paternesi
4 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
5 // Copyright (C) 2007, 2008 Ole Laursen
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Library General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include "playerlist.h"
28 #include "armysetlist.h"
29 #include "stacklist.h"
31 #include "AI_Analysis.h"
32 #include "AI_Allocation.h"
33 #include "AI_Diplomacy.h"
35 #include "xmlhelper.h"
36 #include "armyprodbase.h"
37 #include "armyproto.h"
45 #define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<flush<<endl;}
48 AI_Smart::AI_Smart(string name, unsigned int armyset, Gdk::Color color, int width, int height, int player_no)
49 :RealPlayer(name, armyset, color, width, height, Player::AI_SMART, player_no),
54 AI_Smart::AI_Smart(const Player& player)
55 :RealPlayer(player),d_mustmakemoney(0)
60 AI_Smart::AI_Smart(XML_Helper* helper)
61 :RealPlayer(helper),d_mustmakemoney(0)
69 bool AI_Smart::startTurn()
73 if (getStacklist()->getHeroes().size() == 0 &&
74 Citylist::getInstance()->countCities(this) == 1)
75 AI_maybeBuyScout(Citylist::getInstance()->getFirstCity(this));
77 debug("Player " << getName() << " starts a turn.")
79 AI_Diplomacy diplomacy (this);
81 diplomacy.considerCuspOfWar();
83 if (getGold() < 500 && getUpkeep() > getIncome())
91 AI_setupVectoring(10, 3, 20);
96 AI_Analysis *analysis = new AI_Analysis(this);
97 const Threatlist *threats = analysis->getThreatsInOrder();
98 City *first_city = Citylist::getInstance()->getFirstCity(this);
99 bool build_capacity = false;
102 Vector<int> pos = first_city->getPos();
103 City *first_neutral =
104 Citylist::getInstance()->getNearestNeutralCity(pos);
107 if (dist (pos, first_neutral->getPos()) <= 50)
108 build_capacity = true;
112 build_capacity = true;
117 AI_Allocation *allocation = new AI_Allocation(analysis, threats, this);
118 allocation->sbusy.connect
119 (sigc::mem_fun (sbusy, &sigc::signal<void>::emit));
120 int moveCount = allocation->move(first_city, build_capacity);
125 // stop when no more stacks move
133 d_stacklist->setActivestack(0);
135 diplomacy.makeProposals();
142 void AI_Smart::abortTurn()
144 abort_requested = true;
147 else if (Playerlist::getInstance()->countPlayersAlive() == 1)
151 void AI_Smart::invadeCity(City* c)
153 CityDefeatedAction action = CITY_DEFEATED_OCCUPY;
154 AI_invadeCityQuestPreference(c, action);
157 int pillaged_army_type = -1;
158 std::list<guint32> sacked_army_types;
161 case CITY_DEFEATED_OCCUPY:
164 case CITY_DEFEATED_PILLAGE:
165 cityPillage(c, gold, &pillaged_army_type);
167 case CITY_DEFEATED_RAZE:
170 case CITY_DEFEATED_SACK:
171 citySack(c, gold, &sacked_army_types);
175 if (c->getNoOfProductionBases() == 0)
176 maybeBuyProduction(c, true);
178 // Update its production
182 void AI_Smart::heroGainsLevel(Hero * a)
184 Army::Stat stat = Army::STRENGTH;
185 doHeroGainsLevel(a, stat);
187 Action_Level* item = new Action_Level();
188 item->fillData(a, stat);
192 int AI_Smart::maybeBuyProduction(City *c, bool quick)
194 Armysetlist *al = Armysetlist::getInstance();
199 freeslot = c->getFreeSlot();
204 armytype = chooseArmyTypeToBuy(c, quick);
205 //does this armytype beat the one we're currently producing?
206 //is the one we're buying any better ?
211 int slot = c->getActiveProductionSlot();
214 else if (scoreBestArmyType(al->getArmy(getArmyset(), armytype)) >
215 scoreBestArmyType(c->getProductionBase(slot)))
220 debug("armytype i want to produce " << armytype);
222 bool couldbuy = cityBuyProduction(c, freeslot, armytype);
224 if (armytype >= 0 && couldbuy)
226 debug("YES I COULD BUY! type=" << armytype)
233 int AI_Smart::setQuickProduction(City *c)
238 // we try to determine the most attractive basic production
239 for (guint32 i = 0; i < c->getMaxNoOfProductionBases(); i++)
241 if (c->getArmytype(i) == -1) // no production in this slot
244 const ArmyProdBase *proto = c->getProductionBase(i);
245 int score = scoreQuickArmyType(proto);
246 if (score > best_score)
253 if (select != c->getActiveProductionSlot())
255 cityChangeProduction(c, select);
256 debug(getName() << " Set production to " << select << " in " << c->getName())
259 return c->getActiveProductionSlot();
262 int AI_Smart::setBestProduction(City *c)
267 // we try to determine the most attractive basic production
268 for (guint32 i = 0; i < c->getMaxNoOfProductionBases(); i++)
270 if (c->getArmytype(i) == -1) // no production in this slot
273 const ArmyProdBase *proto = c->getProductionBase(i);
274 int score = scoreBestArmyType(proto);
275 if (score > best_score)
282 if (select != c->getActiveProductionSlot())
284 cityChangeProduction(c, select);
285 debug(getName() << " Set production to slot " << select << " in " << c->getName())
288 return c->getActiveProductionSlot();
291 int AI_Smart::chooseArmyTypeToBuy(City *c, bool quick)
293 int bestScore, bestIndex;
296 const Armysetlist* al = Armysetlist::getInstance();
298 size = al->getSize(getArmyset());
303 debug("size " << size)
305 for (unsigned int i = 0; i < size; i++)
307 const ArmyProto *proto = NULL;
309 proto=al->getArmy(getArmyset(), i);
311 if (proto->getNewProductionCost() == 0)
314 if ((int)proto->getNewProductionCost() > d_gold)
317 if (c->hasProductionBase(proto->getTypeId(), getArmyset())==false)
321 score = scoreQuickArmyType(proto);
323 score = scoreBestArmyType(proto);
324 if (score >= bestScore)
335 int AI_Smart::scoreQuickArmyType(const ArmyProdBase *a)
337 //go get the best 1 turn army with the highest strength
338 int strength = a->getStrength();
340 int production = (5 - a->getProduction()) * 10;
341 return strength + production;
344 int AI_Smart::scoreQuickArmyType(const ArmyProto *a)
346 //go get the best 1 turn army with the highest strength
347 int strength = a->getStrength();
349 int production = (5 - a->getProduction()) * 10;
350 return strength + production;
353 int AI_Smart::scoreBestArmyType(const ArmyProto *a)
355 int production = a->getProduction();
358 //this treats armies with turns of 7 or higher unfairly
359 int max_strength = 60 / production * a->getStrength();
362 switch (a->getArmyBonus())
364 case Army::ADD1STRINCITY: city_bonus += 5; break;
365 case Army::ADD2STRINCITY: city_bonus += 10; break;
368 int any_other_bonus = 0;
369 if (a->getArmyBonus() && city_bonus == 0)
370 any_other_bonus += 2;
373 if (a->getMaxMoves() > 10)
375 if (a->getMaxMoves() >= 20)
378 return max_strength + city_bonus + move_bonus + any_other_bonus;
381 int AI_Smart::scoreBestArmyType(const ArmyProdBase *a)
383 //this treats armies with turns of 7 or higher unfairly
384 int max_strength = 60 / a->getProduction() * a->getStrength();
387 switch (a->getArmyBonus())
389 case Army::ADD1STRINCITY: city_bonus += 5; break;
390 case Army::ADD2STRINCITY: city_bonus += 10; break;
393 int any_other_bonus = 0;
394 if (a->getArmyBonus() && city_bonus == 0)
395 any_other_bonus += 2;
398 if (a->getMaxMoves() > 10)
400 if (a->getMaxMoves() >= 20)
403 return max_strength + city_bonus + move_bonus + any_other_bonus;
406 bool AI_Smart::cityNewlyTaken(City *city, guint32 turns) const
409 std::list<History*> h = getHistoryForCityId(city->getId());
410 for (std::list<History*>::reverse_iterator i = h.rbegin(); i != h.rend(); i++)
412 if ((*i)->getType() == History::START_TURN)
414 else if ((*i)->getType() == History::CITY_WON)
423 void AI_Smart::setProduction(City *city)
425 if (city->countDefenders() < 3 || cityNewlyTaken(city) == true)
427 int slot = setQuickProduction(city);
430 slot = maybeBuyProduction(city, true);
432 cityChangeProduction(city, slot);
437 setBestProduction(city);
441 void AI_Smart::examineCities()
443 debug("Examinating Cities to see what we can do");
444 Citylist* cl = Citylist::getInstance();
445 for (Citylist::iterator it = cl->begin(); it != cl->end(); ++it)
448 if (city->getOwner() == this && city->isBurnt() == false)
451 //do we have enough money to create all these new-fangled army units?
452 int profit = getIncome() - getUpkeep();
453 int total_gp_to_spend = getGold() + profit;
454 //now we get to spend this amount on the city production.
455 //we'll turn off the cities we can't afford.
456 std::list<City*> cities = cl->getNearestFriendlyCities(this);
457 for (std::list<City*>::iterator it = cities.begin(); it != cities.end(); it++)
460 if (total_gp_to_spend <= 0)
461 cityChangeProduction(c, -1);
464 const ArmyProdBase *prodbase = c->getActiveProductionBase();
467 total_gp_to_spend -= prodbase->getProductionCost();
468 if (total_gp_to_spend <= 0)
469 cityChangeProduction(c, -1);
475 bool AI_Smart::chooseTreachery (Stack *stack, Player *player, Vector <int> pos)
477 bool performTreachery = true;
478 return performTreachery;
481 bool AI_Smart::chooseHero(HeroProto *hero, City *city, int gold)
486 Reward *AI_Smart::chooseReward(Ruin *ruin, Sage *sage, Stack *stack)
488 //always pick the money.
489 for (Sage::iterator it = sage->begin(); it != sage->end(); it++)
490 if ((*it)->getType() == Reward::GOLD)
492 return sage->front();
495 Army::Stat AI_Smart::chooseStat(Hero *hero)
497 if (hero && hero->getStat(Army::STRENGTH) > 7)
499 return Army::STRENGTH;
502 bool AI_Smart::chooseQuest(Hero *hero)