~ubuntu-branches/ubuntu/natty/spring/natty

« back to all changes in this revision

Viewing changes to AI/Skirmish/KAIK/UnitHandler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Ritchie
  • Date: 2010-09-23 18:56:03 UTC
  • mfrom: (3.1.9 experimental)
  • Revision ID: james.westby@ubuntu.com-20100923185603-st97s5chplo42y7w
Tags: 0.82.5.1+dfsg1-1ubuntu1
* Latest upstream version for online play
* debian/control: Replace (rather than conflict) spring-engine
  - spring-engine will be a dummy package (LP: #612905)
  - also set maintainer to MOTU

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include <sstream>
2
2
 
3
3
#include "IncCREG.h"
 
4
#include "IncEngine.h"
4
5
#include "IncExternAI.h"
5
6
#include "IncGlobalAI.h"
6
7
 
113
114
        }
114
115
}
115
116
 
116
 
void CUnitHandler::UnitMoveFailed(int unit) {
117
 
        unit = unit;
 
117
void CUnitHandler::UnitMoveFailed(int unitID) {
 
118
        unitID = unitID;
118
119
}
119
120
 
120
121
// called when unit nanoframe first created
124
125
        const UnitDef* udef = ai->cb->GetUnitDef(unitID);
125
126
 
126
127
        if (ucat != CAT_LAST) {
 
128
                assert(ai->GetUnit(unitID)->isDead);
 
129
                ai->GetUnit(unitID)->isDead = false;
 
130
 
127
131
                AllUnitsByCat[ucat].push_back(unitID);
128
132
                AllUnitsByType[udef->id].push_back(unitID);
129
133
 
164
168
        if (udef->isCommander && udef->canDGun) {
165
169
                ai->dgunConHandler->AddController(unitID);
166
170
        } else {
167
 
                ai->MyUnits[unitID]->SetFireState(2);
 
171
                ai->GetUnit(unitID)->SetFireState(2);
168
172
        }
169
173
}
170
174
 
171
 
void CUnitHandler::UnitDestroyed(int unit) {
172
 
        UnitCategory category = ai->ut->GetCategory(unit);
173
 
        const UnitDef* unitDef = ai->cb->GetUnitDef(unit);
 
175
void CUnitHandler::UnitDestroyed(int unitID) {
 
176
        UnitCategory category = ai->ut->GetCategory(unitID);
 
177
        const UnitDef* unitDef = ai->cb->GetUnitDef(unitID);
174
178
 
175
179
        if (category != CAT_LAST) {
176
 
                AllUnitsByType[unitDef->id].remove(unit);
177
 
                AllUnitsByCat[category].remove(unit);
178
 
                IdleUnitRemove(unit);
179
 
                BuildTaskRemove(unit);
 
180
                assert(!ai->GetUnit(unitID)->isDead);
 
181
                ai->GetUnit(unitID)->isDead = true;
 
182
 
 
183
                AllUnitsByType[unitDef->id].remove(unitID);
 
184
                AllUnitsByCat[category].remove(unitID);
 
185
                IdleUnitRemove(unitID);
 
186
                BuildTaskRemove(unitID);
180
187
 
181
188
                if (category == CAT_DEFENCE) {
182
 
                        ai->dm->RemoveDefense(ai->cb->GetUnitPos(unit), unitDef);
 
189
                        ai->dm->RemoveDefense(ai->cb->GetUnitPos(unitID), unitDef);
183
190
                }
184
191
                if (category == CAT_MMAKER) {
185
 
                        MMakerRemove(unit);
 
192
                        MMakerRemove(unitID);
186
193
                }
187
194
                if (category == CAT_FACTORY) {
188
 
                        FactoryRemove(unit);
 
195
                        FactoryRemove(unitID);
189
196
                }
190
197
 
191
198
                if (category == CAT_BUILDER) {
192
199
                        // remove the builder
193
200
                        for (std::list<BuilderTracker*>::iterator i = BuilderTrackers.begin(); i != BuilderTrackers.end(); i++) {
194
 
                                if ((*i)->builderID == unit) {
 
201
                                if ((*i)->builderID == unitID) {
195
202
                                        if ((*i)->buildTaskId)
196
203
                                                BuildTaskRemove(*i);
197
204
                                        if ((*i)->taskPlanId)
208
215
                }
209
216
 
210
217
                if (category == CAT_MEX) {
211
 
                        MetalExtractorRemove(unit);
 
218
                        MetalExtractorRemove(unitID);
212
219
                }
213
220
                if (category == CAT_NUKE) {
214
 
                        NukeSiloRemove(unit);
 
221
                        NukeSiloRemove(unitID);
215
222
                }
216
223
        }
217
224
}
386
393
                // TODO: remove all builders from this plan
387
394
                if (reportError) {
388
395
                        std::list<BuilderTracker*> builderTrackers = taskPlan->builderTrackers;
389
 
                        for (std::list<BuilderTracker*>::iterator i = builderTrackers.begin(); i != builderTrackers.end(); i++) {
 
396
                        std::list<BuilderTracker*>::iterator i;
 
397
 
 
398
                        for (i = builderTrackers.begin(); i != builderTrackers.end(); i++) {
390
399
                                TaskPlanRemove(*i);
391
 
                                ai->MyUnits[(*i)->builderID]->Stop();
 
400
                                ai->GetUnit((*i)->builderID)->Stop();
392
401
                        }
393
402
                } else {
394
403
                        TaskPlanRemove(builderTracker);
745
754
        }
746
755
}
747
756
 
748
 
BuilderTracker* CUnitHandler::GetBuilderTracker(int builder) {
 
757
BuilderTracker* CUnitHandler::GetBuilderTracker(int builderID) {
749
758
        for (std::list<BuilderTracker*>::iterator i = BuilderTrackers.begin(); i != BuilderTrackers.end(); i++) {
750
 
                if ((*i)->builderID == builder) {
 
759
                if ((*i)->builderID == builderID) {
751
760
                        return (*i);
752
761
                }
753
762
        }
754
763
 
755
 
        // this better not happen
756
 
        assert(false);
757
 
        return 0;
 
764
        return NULL;
758
765
}
759
766
 
760
767
void CUnitHandler::BuildTaskRemove(BuilderTracker* builderTracker) {
853
860
bool CUnitHandler::BuildTaskAddBuilder(int builderID, UnitCategory category) {
854
861
        assert(category < CAT_LAST);
855
862
        assert(builderID >= 0);
856
 
        assert(ai->MyUnits[builderID] != NULL);
 
863
        assert(ai->GetUnit(builderID) != NULL);
857
864
 
858
 
        CUNIT* u = ai->MyUnits[builderID];
 
865
        CUNIT* u = ai->GetUnit(builderID);
859
866
        BuilderTracker* builderTracker = GetBuilderTracker(builderID);
 
867
 
860
868
        const UnitDef* builderDef = ai->cb->GetUnitDef(builderID);
861
869
        const int frame = ai->cb->GetCurrentFrame();
862
870
 
1188
1196
}
1189
1197
 
1190
1198
bool CUnitHandler::FactoryBuilderAdd(int builderID) {
1191
 
        bool b = (ai->MyUnits[builderID]->def())->canAssist;
 
1199
        CUNIT* unit = ai->GetUnit(builderID);
1192
1200
        BuilderTracker* builderTracker = GetBuilderTracker(builderID);
1193
 
        return (b && FactoryBuilderAdd(builderTracker));
 
1201
        return ((unit->def()->canAssist) && FactoryBuilderAdd(builderTracker));
1194
1202
}
1195
1203
 
1196
1204
bool CUnitHandler::FactoryBuilderAdd(BuilderTracker* builderTracker) {
1199
1207
        assert(builderTracker->factoryId == 0);
1200
1208
 
1201
1209
        for (std::list<Factory>::iterator i = Factories.begin(); i != Factories.end(); i++) {
1202
 
                CUNIT* u = ai->MyUnits[i->id];
 
1210
                CUNIT* u = ai->GetUnit(i->id);
1203
1211
 
1204
1212
                // don't assist hubs (or factories that cannot be assisted)
1205
1213
                if ((u->def())->canBeAssisted && !u->isHub()) {
1208
1216
                        // HACK: get the sum of the heuristic costs of every
1209
1217
                        // builder that is already assisting this factory
1210
1218
                        for (std::list<int>::iterator j = i->supportbuilders.begin(); j != i->supportbuilders.end(); j++) {
1211
 
                                if ((ai->MyUnits[*j]->def())->isCommander) {
 
1219
                                if ((ai->GetUnit(*j)->def())->isCommander) {
1212
1220
                                        continue;
1213
1221
                                }
1214
1222
 
1226
1234
                                builderTracker->factoryId = i->id;
1227
1235
                                i->supportbuilders.push_back(builderTracker->builderID);
1228
1236
                                i->supportBuilderTrackers.push_back(builderTracker);
1229
 
                                ai->MyUnits[builderTracker->builderID]->Guard(i->id);
 
1237
                                ai->GetUnit(builderTracker->builderID)->Guard(i->id);
1230
1238
                                return true;
1231
1239
                        }
1232
1240
                }
1344
1352
                                                        msg << "\" to builder " << builderID << "\n";
1345
1353
                                                L(ai, msg.str());
1346
1354
 
1347
 
                                                ai->MyUnits[builderID]->Build_ClosestSite(task->newBuildingDef, task->oldBuildingPos);
 
1355
                                                ai->GetUnit(builderID)->Build_ClosestSite(task->newBuildingDef, task->oldBuildingPos);
1348
1356
                                        }
1349
1357
                                } else {
1350
1358
                                        // give a reclaim order for the original structure
1355
1363
                                                        msg << "\" to builder " << builderID << "\n";
1356
1364
                                                L(ai, msg.str());
1357
1365
 
1358
 
                                                ai->MyUnits[builderID]->Reclaim(oldBuildingID);
 
1366
                                                ai->GetUnit(builderID)->Reclaim(oldBuildingID);
1359
1367
                                        }
1360
1368
                                }
1361
1369
                        }