initial commit, lordsawar source, slightly modified
[lordsawar] / src / ai_dummy.cpp
1 // Copyright (C) 2002, 2003, 2004, 2005 Ulf Lorenz
2 // Copyright (C) 2003 Michael Bartl
3 // Copyright (C) 2006 Andrea Paternesi
4 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
5 // Copyright (C) 2007, 2008 Ole Laursen
6 //
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.
11 //
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.
16 //
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 
20 //  02110-1301, USA.
21
22 #include <stdlib.h>
23 #include <algorithm>
24 #include "ai_dummy.h"
25 #include "playerlist.h"
26 #include "armysetlist.h"
27 #include "stacklist.h"
28 #include "citylist.h"
29 #include "city.h"
30 #include <fstream>
31 #include "path.h"
32 #include "action.h"
33 #include "xmlhelper.h"
34 #include "history.h"
35 #include "GameScenarioOptions.h"
36 #include "Sage.h"
37
38 #define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
39 //#define debug(x)
40
41 using namespace std;
42
43 AI_Dummy::AI_Dummy(std::string name, guint32 armyset, Gdk::Color color, int width, int height, int player_no)
44     :RealPlayer(name, armyset, color, width, height, Player::AI_DUMMY, player_no)
45 {
46 }
47
48 AI_Dummy::AI_Dummy(const Player& player)
49     :RealPlayer(player)
50 {
51     d_type = AI_DUMMY;
52 }
53
54 AI_Dummy::AI_Dummy(XML_Helper* helper)
55     :RealPlayer(helper)
56 {
57 }
58
59 AI_Dummy::~AI_Dummy()
60 {
61 }
62
63 void AI_Dummy::abortTurn()
64 {
65   abort_requested = true;
66   if (surrendered)
67     aborted_turn.emit();
68 }
69
70 void AI_Dummy::setDefensiveProduction(City *city)
71 {
72   if (city->getActiveProductionSlot() == -1 &&
73       city->countDefenders() < MAX_ARMIES_PRODUCED_IN_NEUTRAL_CITY)
74     {
75       if (rand() % 2 == 0)
76         {
77           int idx = rand() % city->getMaxNoOfProductionBases();
78           cityChangeProduction(city, idx);
79         }
80     }
81   else
82     {
83       std::list<Action_Produce *> actions = getUnitsProducedThisTurn();
84       std::list<Action_Produce *>::iterator it = actions.begin();
85       for (; it != actions.end(); it++)
86         {
87           if ((*it)->getCityId() == city->getId())
88             {
89               cityChangeProduction(city, -1);
90               break;
91             }
92         }
93     }
94       //if production is stopped, then start it.
95       //if an army arrived this turn, stop production.
96 }
97 void AI_Dummy::examineCities()
98 {
99     debug("Examinating Cities to see what we can do")
100     Citylist* cl = Citylist::getInstance();
101     for (Citylist::iterator it = cl->begin(); it != cl->end(); ++it)
102       {
103         City *city = (*it);
104         if ((city->isFriend(this)) && (city->isBurnt()==false))
105           setDefensiveProduction(city);
106       }
107 }
108
109 bool AI_Dummy::startTurn()
110 {
111       
112   if (GameScenarioOptions::s_neutral_cities == GameParameters::DEFENSIVE)
113     {
114       //setup production defensive style.
115       if (d_gold > 100)
116         examineCities();
117       else
118         {
119           // stop the presses.
120           Citylist* cl = Citylist::getInstance();
121           for (Citylist::iterator it = cl->begin(); it != cl->end(); ++it)
122             {
123               City *city = (*it);
124               if ((city->isFriend(this)) && (city->isBurnt()==false))
125                 city->setActiveProductionSlot(-1);
126             }
127         }
128
129     }
130   //this is a dummy AI (neutral player) so there is not much point in
131   //doing anything
132   if (abort_requested)
133     aborted_turn.emit();
134   return true;
135 }
136
137 void AI_Dummy::invadeCity(City* c)
138 {
139   //dummy ai player should never invade an enemy city, but if it happens, we
140   //make sure there is no inconsistency
141   cityOccupy(c);
142 }
143
144 void AI_Dummy::heroGainsLevel(Hero * a)
145 {
146   Army::Stat stat = Army::STRENGTH;
147   doHeroGainsLevel(a, stat);
148
149   Action_Level* item = new Action_Level();
150   item->fillData(a, stat);
151   addAction(item);
152 }
153
154 bool AI_Dummy::chooseHero(HeroProto *hero, City *city, int gold)
155 {
156   //neutral players never accept heroes.
157   return false;
158 }
159         
160 Reward *AI_Dummy::chooseReward(Ruin *ruin, Sage *sage, Stack *stack)
161 {
162   //neutrals don't search ruins, but let's not return null.
163   return sage->front();
164 }
165
166 bool AI_Dummy::chooseTreachery (Stack *stack, Player *player, Vector <int> pos)
167 {
168   //neutrals don't leave the castle.
169   bool performTreachery = true;
170   return performTreachery;
171 }
172
173 Army::Stat AI_Dummy::chooseStat(Hero *hero)
174 {
175   //neutrals don't have heroes.
176   return Army::STRENGTH;
177 }
178
179 bool AI_Dummy::chooseQuest(Hero *hero)
180 {
181   //neutrals don't have heroes.
182   return true;
183 }
184 // End of file