~ubuntu-branches/ubuntu/maverick/lordsawar/maverick

« back to all changes in this revision

Viewing changes to src/AI_Analysis.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2010-04-10 09:29:33 UTC
  • mfrom: (1.1.9 upstream) (5.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100410092933-23uq4dxig30kmtcw
Tags: 0.1.8-1
* New upstream release.
* Add misc:Depends for -data package.
* Bump Standards Version to 3.8.4. (No changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include "Threatlist.h"
25
25
#include "Threat.h"
26
26
#include "playerlist.h"
 
27
#include "stackreflist.h"
27
28
#include "stacklist.h"
28
29
#include "ruinlist.h"
29
30
#include "army.h"
30
31
#include "AICityInfo.h"
 
32
#include "armysetlist.h"
31
33
 
32
34
using namespace std;
33
35
 
43
45
{
44
46
    d_stacks = 0;
45
47
    d_threats = new Threatlist();
46
 
    d_stacks = new Stacklist(owner->getStacklist());
 
48
    d_stacks = new StackReflist(owner->getStacklist());
47
49
 
48
50
    examineCities();
49
51
    examineRuins();
73
75
  if (instance)
74
76
    {
75
77
        instance->d_threats->deleteStack(id);
76
 
        instance->d_stacks->flRemove(id);
 
78
        instance->d_stacks->removeStack(id);
77
79
    }
78
80
}
79
81
 
83
85
    {
84
86
        debug("delete stack from ai_analysis")
85
87
        instance->d_threats->deleteStack(s->getId());
86
 
        instance->d_stacks->flRemove(s->getId());
 
88
        instance->d_stacks->removeStack(s->getId());
87
89
        debug("stack " << s << " died")
88
90
    }
89
91
}
90
92
 
 
93
float AI_Analysis::assessArmyStrength(const Army *army)
 
94
{
 
95
  return (float)army->getStat(Army::STRENGTH);
 
96
}
 
97
 
91
98
float AI_Analysis::assessStackStrength(const Stack *stack)
92
99
{
93
 
    debug("assessing stack with id "<<stack->getId())
94
 
    
95
100
    if (!instance)
96
101
        return stack->size() * 5.0;
97
102
 
100
105
        // our stack, so we can look inside it
101
106
        float total = 0.0;
102
107
        for (Stack::const_iterator it = stack->begin(); it != stack->end(); it++)
103
 
            total += (*it)->getStat(Army::STRENGTH);
 
108
          total += assessArmyStrength(*it);
 
109
            
104
110
 
105
111
        return total;
106
112
    }
107
113
    else
108
 
    {
 
114
      {
109
115
        // enemy stack, no cheating!
110
116
        // if we were smarter, we would remember all stacks we had seen before and return a better number here.
111
117
        // We don't assume a too high average strength
112
 
        if (stack->getStrongestArmy()->getStat(Army::STRENGTH) < 5)
113
 
            return stack->size() * stack->getStrongestArmy()->getStat(Army::STRENGTH);
 
118
        guint32 as = stack->getOwner()->getArmyset();
 
119
        guint32 type_id = stack->getStrongestArmy()->getTypeId();
 
120
        ArmyProto *strongest = Armysetlist::getInstance()->getArmy(as, type_id);
 
121
        //if the strongest army has a strength of 4 or less,
 
122
        //we assume that all army units in the stack have the same strength.
 
123
        if (strongest->getStrength() < 5)
 
124
          return stack->size() * strongest->getStrength();
 
125
        //otherwise we round everything down to an average of 5 strength.
114
126
        return stack->size() * 5.0;
115
 
    }
 
127
      }
116
128
}
117
129
 
118
130
const Threatlist* AI_Analysis::getThreatsInOrder()
130
142
 
131
143
void AI_Analysis::getCityWorstDangers(float dangers[3])
132
144
{
133
 
    std::map<string, AICityInfo *>::iterator it;
 
145
    std::map<guint32, AICityInfo *>::iterator it;
134
146
 
135
147
 
136
148
    // i wanto to have a result array with the first worst dangers
161
173
    return;
162
174
}
163
175
 
 
176
int AI_Analysis::getNumberOfDefendersInCity(City *city)
 
177
{
 
178
  AICityMap::iterator it = d_cityInfo.find(city->getId());
 
179
  if (it == d_cityInfo.end())
 
180
    return 0;
 
181
 
 
182
  return (*it).second->getDefenderCount();
 
183
}
 
184
 
164
185
float AI_Analysis::getCityDanger(City *city)
165
186
{
166
 
    // city does not exist in the map
167
 
    if (d_cityInfo.find(city->getName()) == d_cityInfo.end())
168
 
        return 0.0;
169
 
    
170
 
    debug("Threats to " << city->getName() << " are " << d_cityInfo[city->getName()]->getThreats()->toString())
171
 
    return d_cityInfo[city->getName()]->getDanger();
 
187
  AICityMap::iterator it = d_cityInfo.find(city->getId());
 
188
  // city does not exist in the map
 
189
  if (it == d_cityInfo.end())
 
190
    return 0.0;
 
191
 
 
192
  debug("Threats to " << city->getName() << " are " << d_cityInfo[city->getId()]->getThreats()->toString())
 
193
    return (*it).second->getDanger();
172
194
}
173
195
 
174
196
void AI_Analysis::reinforce(City *city, Stack *stack, int movesToArrive)
175
197
{
176
 
    if (d_cityInfo.find(city->getName()) == d_cityInfo.end())
177
 
        return;
 
198
  AICityMap::iterator it = d_cityInfo.find(city->getId()) ;
 
199
  if (it == d_cityInfo.end())
 
200
    return;
178
201
 
179
 
    AICityInfo *info = d_cityInfo[city->getName()];
180
 
    info->addReinforcements(assessStackStrength(stack) / (float) movesToArrive);
 
202
  (*it).second->addReinforcements(assessStackStrength(stack) / (float) movesToArrive);
181
203
}
182
204
 
183
205
float AI_Analysis::reinforcementsNeeded(City *city)
184
206
{
185
 
    if (d_cityInfo.find(city->getName()) == d_cityInfo.end())
186
 
        return -1000.0;
 
207
  AICityMap::iterator it = d_cityInfo.find(city->getId());
 
208
  if (it == d_cityInfo.end())
 
209
    return -1000.0;
187
210
 
188
 
    const AICityInfo* info = d_cityInfo[city->getName()];
189
 
    return info->getDanger() - info->getReinforcements();
 
211
  return (*it).second->getDanger() - (*it).second->getReinforcements();
190
212
}
191
213
 
192
214
void AI_Analysis::examineCities()
214
236
 
215
237
        Stacklist *sl = player->getStacklist();
216
238
        for (Stacklist::iterator sit = sl->begin(); sit != sl->end(); ++sit)
217
 
        {
218
 
            d_threats->addStack(*sit);
219
 
        }
 
239
          d_threats->addStack(*sit);
220
240
    }
221
241
}
222
242
 
245
265
        {
246
266
            AICityInfo *info = new AICityInfo(city);
247
267
            d_threats->findThreats(info);
248
 
            d_cityInfo[city->getName()] = info;
 
268
            d_cityInfo[city->getId()] = info;
249
269
        }
250
270
    }
251
271
}
252
272
 
 
273
void AI_Analysis::changeOwnership (Player * old_player, Player * new_player)
 
274
{
 
275
  if (instance)
 
276
    instance->d_threats->changeOwnership(old_player, new_player);
 
277
}
 
278
 
253
279
// End of file