-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUnitHandler.cpp
1394 lines (1147 loc) · 39.1 KB
/
UnitHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <sstream>
#include "IncCREG.h"
#include "IncEngine.h"
#include "IncExternAI.h"
#include "IncGlobalAI.h"
CR_BIND(CUnitHandler, (NULL));
CR_REG_METADATA(CUnitHandler, (
CR_MEMBER(IdleUnits),
CR_MEMBER(BuildTasks),
CR_MEMBER(TaskPlans),
CR_MEMBER(AllUnitsByCat),
CR_MEMBER(AllUnitsByType),
CR_MEMBER(Factories),
CR_MEMBER(NukeSilos),
CR_MEMBER(MetalExtractors),
CR_MEMBER(Limbo),
CR_MEMBER(BuilderTrackers),
CR_MEMBER(metalMaker),
CR_MEMBER(ai),
CR_MEMBER(taskPlanCounter),
CR_RESERVED(16)
));
CUnitHandler::CUnitHandler(AIClasses* ai) {
this->ai = ai;
taskPlanCounter = 1;
lastCapturedUnitFrame = -1;
lastCapturedUnitID = -1;
IdleUnits.resize(CAT_LAST);
BuildTasks.resize(CAT_LAST);
TaskPlans.resize(CAT_LAST);
AllUnitsByCat.resize(CAT_LAST);
if (ai) {
AllUnitsByType.resize(ai->cb->GetNumUnitDefs() + 1);
metalMaker = new CMetalMaker(ai);
}
}
CUnitHandler::~CUnitHandler() {
for (std::list<BuilderTracker*>::iterator i = BuilderTrackers.begin(); i != BuilderTrackers.end(); i++) {
delete *i;
}
}
void CUnitHandler::IdleUnitUpdate(int frame) {
std::list<integer2> limboremoveunits;
for (std::list<integer2>::iterator i = Limbo.begin(); i != Limbo.end(); i++) {
if (i->y > 0) {
i->y--;
} else {
if (ai->cb->GetUnitDef(i->x) == NULL) {
// ignore dead unit
} else {
IdleUnits[ai->ut->GetCategory(i->x)].push_back(i->x);
}
limboremoveunits.push_back(*i);
}
}
if (limboremoveunits.size()) {
for (std::list<integer2>::iterator i = limboremoveunits.begin(); i != limboremoveunits.end(); i++) {
Limbo.remove(*i);
}
}
// make sure that all the builders are in action (hack?)
if (frame % 15 == 0) {
for (std::list<BuilderTracker*>::iterator i = BuilderTrackers.begin(); i != BuilderTrackers.end(); i++) {
if ((*i)->idleStartFrame != -2) {
// the brand new builders must be filtered still
bool ans = VerifyOrder(*i);
const int builderID = (*i)->builderID;
const CCommandQueue* myCommands = ai->cb->GetCurrentUnitCommands(builderID);
Command c;
if (myCommands->size() > 0) {
c = myCommands->front();
}
// two sec delay is ok
if (((*i)->commandOrderPushFrame + LAG_ACCEPTANCE) < frame) {
if (!ans) {
float3 pos = ai->cb->GetUnitPos(builderID);
std::stringstream msg;
msg << "[CUnitHandler::IdleUnitUpdate()][frame=" << frame << "]\n";
msg << "\tfailed to verify order for builder " << builderID;
msg << " with " << (myCommands->size()) << " remaining commands\n";
ai->GetLogger()->Log(msg.str());
ClearOrder(*i, false);
// due to the Legacy AI Wrapper inner workings,
// the command-queue we fetched above is now invalid,
// because it got changed in ClearOrder()
myCommands = ai->cb->GetCurrentUnitCommands(builderID);
if (!myCommands->empty()) {
DecodeOrder(*i, true);
} else {
IdleUnitAdd(builderID, frame);
}
}
}
}
}
}
}
void CUnitHandler::UnitMoveFailed(int unitID) {
unitID = unitID;
}
// called when unit nanoframe first created
// (CEconomyTracker deals with UnitFinished())
void CUnitHandler::UnitCreated(int unitID) {
UnitCategory ucat = ai->ut->GetCategory(unitID);
const UnitDef* udef = ai->cb->GetUnitDef(unitID);
if (ucat != CAT_LAST) {
assert(ai->GetUnit(unitID)->isDead);
ai->GetUnit(unitID)->isDead = false;
AllUnitsByCat[ucat].push_back(unitID);
AllUnitsByType[udef->id].push_back(unitID);
if (ucat == CAT_FACTORY) {
FactoryAdd(unitID);
}
BuildTaskCreate(unitID);
if (ucat == CAT_BUILDER) {
// add the new builder
BuilderTracker* builderTracker = new BuilderTracker();
builderTracker->builderID = unitID;
builderTracker->buildTaskId = 0;
builderTracker->taskPlanId = 0;
builderTracker->factoryId = 0;
builderTracker->stuckCount = 0;
// under construction
builderTracker->commandOrderPushFrame = -2;
builderTracker->categoryMaker = CAT_LAST;
// wait for the first idle call, as this unit might be under construction
builderTracker->idleStartFrame = -2;
BuilderTrackers.push_back(builderTracker);
}
if (ucat == CAT_MMAKER) {
MMakerAdd(unitID);
}
if (ucat == CAT_MEX) {
MetalExtractorAdd(unitID);
}
if (ucat == CAT_NUKE) {
NukeSiloAdd(unitID);
}
}
if (udef->isCommander && udef->canDGun) {
ai->dgunConHandler->AddController(unitID);
} else {
ai->GetUnit(unitID)->SetFireState(2);
}
}
void CUnitHandler::UnitDestroyed(int unitID) {
UnitCategory category = ai->ut->GetCategory(unitID);
const UnitDef* unitDef = ai->cb->GetUnitDef(unitID);
if (category != CAT_LAST) {
assert(!ai->GetUnit(unitID)->isDead);
ai->GetUnit(unitID)->isDead = true;
AllUnitsByType[unitDef->id].remove(unitID);
AllUnitsByCat[category].remove(unitID);
IdleUnitRemove(unitID);
BuildTaskRemove(unitID);
if (category == CAT_DEFENCE) {
ai->dm->RemoveDefense(ai->cb->GetUnitPos(unitID), unitDef);
}
if (category == CAT_MMAKER) {
MMakerRemove(unitID);
}
if (category == CAT_FACTORY) {
FactoryRemove(unitID);
}
if (category == CAT_BUILDER) {
// remove the builder
for (std::list<BuilderTracker*>::iterator i = BuilderTrackers.begin(); i != BuilderTrackers.end(); i++) {
if ((*i)->builderID == unitID) {
if ((*i)->buildTaskId)
BuildTaskRemove(*i);
if ((*i)->taskPlanId)
TaskPlanRemove(*i);
if ((*i)->factoryId)
FactoryBuilderRemove(*i);
BuilderTracker* builderTracker = *i;
BuilderTrackers.erase(i);
delete builderTracker;
break;
}
}
}
if (category == CAT_MEX) {
MetalExtractorRemove(unitID);
}
if (category == CAT_NUKE) {
NukeSiloRemove(unitID);
}
}
}
void CUnitHandler::IdleUnitAdd(int unit, int frame) {
UnitCategory category = ai->ut->GetCategory(unit);
if (category != CAT_LAST) {
const CCommandQueue* myCommands = ai->cb->GetCurrentUnitCommands(unit);
if (myCommands->empty()) {
if (category == CAT_BUILDER) {
BuilderTracker* builderTracker = GetBuilderTracker(unit);
// add clear here
ClearOrder(builderTracker, true);
if (builderTracker->idleStartFrame == -2) {
// it was in the idle list already?
IdleUnitRemove(builderTracker->builderID);
}
builderTracker->idleStartFrame = -2;
if (builderTracker->commandOrderPushFrame == -2) {
// make sure that if the unit was just built
// it will have some time to leave the factory
builderTracker->commandOrderPushFrame = frame + 30 * 3;
}
}
integer2 myunit(unit, LIMBOTIME);
Limbo.remove(myunit);
Limbo.push_back(myunit);
} else {
// the unit has orders still, so should not be idle
if (category == CAT_BUILDER) {
if (false) {
// KLOOTNOTE: somehow we are reaching this branch
// on initialization when USE_CREG is not defined,
// mycommands->size() returns garbage?
//
// BuilderTracker* builderTracker = GetBuilderTracker(unit);
// DecodeOrder(builderTracker, true);
}
}
}
}
}
bool CUnitHandler::VerifyOrder(BuilderTracker* builderTracker) {
// if it's without orders then try to find the lost command (TODO)
const CCommandQueue* mycommands = ai->cb->GetCurrentUnitCommands(builderTracker->builderID);
bool commandFound = false;
bool hit = false;
if (!mycommands->empty()) {
// it has orders
const Command* c = &mycommands->front();
if (mycommands->size() == 2) {
// it might have a reclaim order, or terrain change order
// take command nr. 2
c = &mycommands->back();
}
if (builderTracker->buildTaskId != 0) {
hit = true;
// test that this builder is on repair on this unit
BuildTask* buildTask = GetBuildTask(builderTracker->buildTaskId);
commandFound =
((c->id == CMD_REPAIR ) &&
(c->params[0] == builderTracker->buildTaskId)) ||
((c->id == -buildTask->def->id) &&
(c->params[0] == buildTask->pos.x ) &&
(c->params[2] == buildTask->pos.z ));
if (!commandFound) {
return false;
}
}
if (builderTracker->taskPlanId != 0) {
assert(!hit);
hit = true;
TaskPlan* taskPlan = GetTaskPlan(builderTracker->taskPlanId);
if ((c->id == -taskPlan->def->id)
&& (c->params[0] == taskPlan->pos.x)
&& (c->params[2] == taskPlan->pos.z))
commandFound = true;
else
return false;
}
if (builderTracker->factoryId != 0) {
assert(!hit);
hit = true;
if (c->id == CMD_GUARD && c->params[0] == builderTracker->factoryId)
commandFound = true;
else
return false;
}
if (!commandFound) {
hit = true;
commandFound = (c->id == CMD_RECLAIM || c->id == CMD_MOVE || c->id == CMD_REPAIR);
}
if (hit && commandFound) {
// it's on the right job
return true;
}
}
else {
if (builderTracker->idleStartFrame == -2) {
return true;
}
}
return false;
}
// use this only if the unit does not have any orders at the moment
void CUnitHandler::ClearOrder(BuilderTracker* builderTracker, bool reportError) {
bool hit = false;
const int frame = ai->cb->GetCurrentFrame();
const int builderID = builderTracker->builderID;
const int buildTaskID = builderTracker->buildTaskId;
const int factoryID = builderTracker->factoryId;
const CCommandQueue* q = ai->cb->GetCurrentUnitCommands(builderID);
assert(q->empty() || !reportError);
if (buildTaskID != 0) {
hit = true;
BuildTask* buildTask = GetBuildTask(buildTaskID);
std::stringstream msg;
msg << "[CUnitHandler::ClearOrder()][frame=" << frame << "]\n";
msg << "\tbuilder " << builderID << " is reported idle but";
msg << " still has a build-task with ID " << (buildTaskID) << "\n";
ai->GetLogger()->Log(msg.str());
if (buildTask->builderTrackers.size() > 1) {
BuildTaskRemove(builderTracker);
} else {
// only builder of this thing, and now idle
BuildTaskRemove(builderTracker);
}
}
if (builderTracker->taskPlanId != 0) {
assert(!hit);
hit = true;
const TaskPlan* taskPlan = GetTaskPlan(builderTracker->taskPlanId);
const std::string& taskName = taskPlan->def->humanName;
std::stringstream msg;
msg << "[CUnitHandler::ClearOrder()][frame=" << frame << "]\n";
msg << "\tbuilder " << builderID << " is reported idle but";
msg << " still has a task-plan named \"" << (taskName) << "\"\n";
ai->GetLogger()->Log(msg.str());
// mask this build-spot as bad
ai->dm->MaskBadBuildSpot(taskPlan->pos);
// TODO: remove all builders from this plan
if (reportError) {
std::list<BuilderTracker*> builderTrackers = taskPlan->builderTrackers;
std::list<BuilderTracker*>::iterator i;
for (i = builderTrackers.begin(); i != builderTrackers.end(); i++) {
TaskPlanRemove(*i);
ai->GetUnit((*i)->builderID)->Stop();
}
} else {
TaskPlanRemove(builderTracker);
}
}
if (factoryID != 0) {
assert(!hit);
hit = true;
std::stringstream msg;
msg << "[CUnitHandler::ClearOrder()][frame=" << frame << "]\n";
msg << "\tbuilder " << builderID << " is reported idle but";
msg << " still has a factory ID of " << factoryID << "\n";
ai->GetLogger()->Log(msg.str());
// remove the builder from its job
FactoryBuilderRemove(builderTracker);
}
assert(builderTracker->buildTaskId == 0);
assert(builderTracker->taskPlanId == 0);
assert(builderTracker->factoryId == 0);
}
void CUnitHandler::DecodeOrder(BuilderTracker* builderTracker, bool reportError) {
const int frame = ai->cb->GetCurrentFrame();
const int builderID = builderTracker->builderID;
const CCommandQueue* builderQ = ai->cb->GetCurrentUnitCommands(builderID);
if (builderQ->size() > 0) {
// builder has orders
const Command* c = &builderQ->front();
const int n = c->params.size();
const int cID = c->id;
if (builderQ->size() == 2 && cID == CMD_MOVE) {
// it might have a move order before the real order,
// take command nr. 2 if nr. 1 is a move order
c = &builderQ->back();
}
if (reportError) {
std::stringstream msg;
msg << "[CUnitHandler::DecodeOrder()][frame=" << frame << "]\n";
msg << "\tbuilder " << builderID << " claimed idle, but has";
msg << " command " << cID << " with " << n << " parameters";
msg << " (params[0]: " << ((n > 0)? c->params[0]: -1) << ")\n";
ai->GetLogger()->Log(msg.str());
}
if (cID < 0) {
assert(n >= 3);
// it's building a unit
float3 newUnitPos;
newUnitPos.x = c->params[0];
newUnitPos.y = c->params[1];
newUnitPos.z = c->params[2];
const UnitDef* newUnitDef = ai->ut->unitTypes[-cID].def;
// make sure that no BuildTasks exists there
BuildTask* buildTask = BuildTaskExist(newUnitPos, newUnitDef);
if (buildTask) {
BuildTaskAddBuilder(buildTask, builderTracker);
} else {
// make a new TaskPlan (or join an existing one)
TaskPlanCreate(builderID, newUnitPos, newUnitDef);
}
}
if (cID == CMD_REPAIR) {
assert(n >= 1);
// it's repairing, find the unit being repaired
int guardingID = int(c->params[0]);
bool found = false;
UnitCategory cat = ai->ut->GetCategory(guardingID);
std::list<BuildTask>::iterator i;
if (cat == CAT_LAST) {
return;
}
for (i = BuildTasks[cat].begin(); i != BuildTasks[cat].end(); i++) {
if (i->id == guardingID) {
// whatever the old order was, update it now
bool hit = false;
if (builderTracker->buildTaskId != 0) {
hit = true;
// why is this builder idle?
BuildTask* buildTask = GetBuildTask(builderTracker->buildTaskId);
if (buildTask->builderTrackers.size() > 1) {
BuildTaskRemove(builderTracker);
} else {
// only builder of this thing, and now idle?
BuildTaskRemove(builderTracker);
}
}
if (builderTracker->taskPlanId != 0) {
assert(!hit);
hit = true;
TaskPlanRemove(builderTracker);
}
if (builderTracker->factoryId != 0) {
assert(!hit);
hit = true;
FactoryBuilderRemove(builderTracker);
}
BuildTask* bt = &*i;
BuildTaskAddBuilder(bt, builderTracker);
found = true;
}
}
if (!found) {
// not found, just make a custom order
builderTracker->idleStartFrame = -1;
}
}
} else {
// error: this function needs a builder with orders
// should not be possible because IdleUnitUpdate()
// calls us only if a unit's command-queue is NOT
// empty?
// assert(false);
std::stringstream msg;
msg << "[CUnitHandler::DecodeOrder()][frame=" << frame << "]\n";
msg << "\tbuilder " << builderID << " should not have an empty queue!\n";
ai->GetLogger()->Log(msg.str());
}
}
void CUnitHandler::IdleUnitRemove(int unit) {
UnitCategory category = ai->ut->GetCategory(unit);
if (category != CAT_LAST) {
IdleUnits[category].remove(unit);
if (category == CAT_BUILDER) {
BuilderTracker* builderTracker = GetBuilderTracker(unit);
builderTracker->idleStartFrame = -1;
if (builderTracker->commandOrderPushFrame == -2) {
// bad
}
// update the order start frame
builderTracker->commandOrderPushFrame = ai->cb->GetCurrentFrame();
// assert(builderTracker->buildTaskId == 0);
// assert(builderTracker->taskPlanId == 0);
// assert(builderTracker->factoryId == 0);
}
std::list<integer2>::iterator tempunit;
bool found = false;
for (std::list<integer2>::iterator i = Limbo.begin(); i != Limbo.end(); i++) {
if (i->x == unit) {
tempunit = i;
found = true;
}
}
if (found) {
Limbo.erase(tempunit);
}
}
}
int CUnitHandler::GetIU(UnitCategory category) {
assert(IdleUnits[category].size() > 0);
int unitID = IdleUnits[category].front();
// move the returned unitID to the back
// of the list, so CBuildUp::FactoryCycle()
// doesn't keep examining the same factory
// if a build order fails and we have more
// than one idle factory
IdleUnits[category].pop_front();
IdleUnits[category].push_back(unitID);
return unitID;
}
int CUnitHandler::NumIdleUnits(UnitCategory category) {
assert(category < CAT_LAST);
IdleUnits[category].sort();
IdleUnits[category].unique();
return IdleUnits[category].size();
}
void CUnitHandler::MMakerAdd(int unit) {
metalMaker->Add(unit);
}
void CUnitHandler::MMakerRemove(int unit) {
metalMaker->Remove(unit);
}
void CUnitHandler::MMakerUpdate(int frame) {
metalMaker->Update(frame);
}
void CUnitHandler::BuildTaskCreate(int id) {
const UnitDef* newUnitDef = ai->cb->GetUnitDef(id);
const UnitCategory category = ai->ut->GetCategory(id);
const float3 pos = ai->cb->GetUnitPos(id);
if ((!newUnitDef->movedata || category == CAT_DEFENCE) && !newUnitDef->canfly && category != CAT_LAST) {
// this needs to change, so that it can make more stuff
if (category >= CAT_LAST) {
return;
}
BuildTask bt;
bt.id = -1;
std::list<TaskPlan>::iterator i;
for (i = TaskPlans[category].begin(); i != TaskPlans[category].end(); i++) {
if (pos.distance2D(i->pos) < 1.0f && newUnitDef == i->def) {
bt.category = category;
bt.id = id;
bt.pos = i->pos;
bt.def = newUnitDef;
std::list<BuilderTracker*> moveList;
for (std::list<BuilderTracker*>::iterator builder = i->builderTrackers.begin(); builder != i->builderTrackers.end(); builder++) {
moveList.push_back(*builder);
}
for (std::list<BuilderTracker*>::iterator builder = moveList.begin(); builder != moveList.end(); builder++) {
TaskPlanRemove(*builder);
BuildTaskAddBuilder(&bt, *builder);
}
// there can not be more than one found TaskPlan
break;
}
}
if (bt.id == -1) {
// buildtask creation error (can happen if builder manages
// to restart a dead building, or a human has taken control),
// make one anyway
std::stringstream msg;
msg << "[CUnitHandler::BuildTaskCreate()][frame=" << ai->cb->GetCurrentFrame() << "]\n";
msg << "\tBuildTask Creation Error for task with ID " << id << "\n";
ai->GetLogger()->Log(msg.str());
if (category == CAT_DEFENCE) {
ai->dm->AddDefense(pos, newUnitDef);
}
bt.category = category;
bt.id = id;
bt.pos = pos;
bt.def = newUnitDef;
// if we have any friendly builders
for (std::list<BuilderTracker*>::iterator i = BuilderTrackers.begin(); i != BuilderTrackers.end(); i++) {
BuilderTracker* builderTracker = *i;
// check what builder is doing
const CCommandQueue* cq = ai->cb->GetCurrentUnitCommands(builderTracker->builderID);
if (!cq->empty()) {
Command c = cq->front();
const bool b0 = (c.id == -newUnitDef->id && c.params[0] == pos.x && c.params[2] == pos.z); // at this pos
const bool b1 = (c.id == CMD_REPAIR && c.params[0] == id); // at this unit (id)
const bool b2 = (c.id == CMD_GUARD && c.params[0] == id); // at this unit (id)
const bool b3 = b0 || b1 || b2;
if (b3) {
if (builderTracker->buildTaskId != 0) {
BuildTask* buildTask = GetBuildTask(builderTracker->buildTaskId);
if (buildTask->builderTrackers.size() > 1) {
BuildTaskRemove(builderTracker);
} else {
// only builder of this thing, and now idle
BuildTaskRemove(builderTracker);
}
}
if (builderTracker->taskPlanId != 0) {
GetTaskPlan(builderTracker->taskPlanId);
TaskPlanRemove(builderTracker);
}
if (builderTracker->factoryId != 0) {
FactoryBuilderRemove(builderTracker);
}
// this builder is now free
if (builderTracker->idleStartFrame == -2) {
IdleUnitRemove(builderTracker->builderID);
}
// add it to this task
BuildTaskAddBuilder(&bt, builderTracker);
msg.str("");
msg << "\tadded builder " << builderTracker->builderID << " to";
msg << " build-task with ID " << builderTracker->buildTaskId << "\n";
ai->GetLogger()->Log(msg.str());
}
}
}
// add the task anyway
BuildTasks[category].push_back(bt);
}
else {
if (category == CAT_DEFENCE)
ai->dm->AddDefense(pos,newUnitDef);
BuildTasks[category].push_back(bt);
}
}
}
void CUnitHandler::BuildTaskRemove(int id) {
UnitCategory category = ai->ut->GetCategory(id);
if (category < CAT_LAST) {
std::list<BuildTask>::iterator killtask;
bool found = false;
for (std::list<BuildTask>::iterator i = BuildTasks[category].begin(); i != BuildTasks[category].end(); i++) {
if (i->id == id) {
killtask = i;
assert(!found);
found = true;
}
}
if (found) {
// remove the builders from this BuildTask
std::list<BuilderTracker*> removeList;
for (std::list<BuilderTracker*>::iterator builder = killtask->builderTrackers.begin(); builder != killtask->builderTrackers.end(); builder++) {
removeList.push_back(*builder);
}
for (std::list<BuilderTracker*>::iterator builder = removeList.begin(); builder != removeList.end(); builder++) {
BuildTaskRemove(*builder);
}
BuildTasks[category].erase(killtask);
}
}
}
BuilderTracker* CUnitHandler::GetBuilderTracker(int builderID) {
for (std::list<BuilderTracker*>::iterator i = BuilderTrackers.begin(); i != BuilderTrackers.end(); i++) {
if ((*i)->builderID == builderID) {
return (*i);
}
}
return NULL;
}
void CUnitHandler::BuildTaskRemove(BuilderTracker* builderTracker) {
if (builderTracker->buildTaskId == 0) {
assert(false);
return;
}
UnitCategory category = ai->ut->GetCategory(builderTracker->buildTaskId);
// TODO: Hack fix
if (category == CAT_LAST) {
return;
}
assert(category >= 0);
assert(category < CAT_LAST);
assert(builderTracker->buildTaskId != 0);
assert(builderTracker->taskPlanId == 0);
assert(builderTracker->factoryId == 0);
bool found = false;
bool found2 = false;
for (std::list<BuildTask>::iterator i = BuildTasks[category].begin(); i != BuildTasks[category].end(); i++) {
if (i->id == builderTracker->buildTaskId){
// killtask = i;
assert(!found);
for (std::list<int>::iterator builder = i->builders.begin(); builder != i->builders.end(); builder++) {
if (builderTracker->builderID == *builder) {
assert(!found2);
i->builders.erase(builder);
builderTracker->buildTaskId = 0;
found2 = true;
break;
}
}
for (std::list<BuilderTracker*>::iterator builder = i->builderTrackers.begin(); builder != i->builderTrackers.end(); builder++) {
if (builderTracker == *builder) {
assert(!found);
i->builderTrackers.erase(builder);
builderTracker->buildTaskId = 0;
// give it time to change command
builderTracker->commandOrderPushFrame = ai->cb->GetCurrentFrame();
found = true;
break;
}
}
}
}
assert(found);
}
void CUnitHandler::BuildTaskAddBuilder(BuildTask* buildTask, BuilderTracker* builderTracker) {
buildTask->builders.push_back(builderTracker->builderID);
buildTask->builderTrackers.push_back(builderTracker);
buildTask->currentBuildPower += ai->cb->GetUnitDef(builderTracker->builderID)->buildSpeed;
assert(builderTracker->buildTaskId == 0);
assert(builderTracker->taskPlanId == 0);
assert(builderTracker->factoryId == 0);
builderTracker->buildTaskId = buildTask->id;
}
BuildTask* CUnitHandler::GetBuildTask(int buildTaskId) {
for (int k = 0; k < CAT_LAST; k++) {
for (std::list<BuildTask>::iterator i = BuildTasks[k].begin(); i != BuildTasks[k].end(); i++) {
if (i->id == buildTaskId)
return &*i;
}
}
// this better not happen
assert(false);
return 0;
}
BuildTask* CUnitHandler::BuildTaskExist(float3 pos, const UnitDef* builtdef) {
UnitCategory category = ai->ut->GetCategory(builtdef);
if (category >= CAT_LAST) {
return NULL;
}
for (std::list<BuildTask>::iterator i = BuildTasks[category].begin(); i != BuildTasks[category].end(); i++) {
if (i->pos.distance2D(pos) < 1 && ai->ut->GetCategory(i->def) == category) {
return &*i;
}
}
return NULL;
}
bool CUnitHandler::BuildTaskAddBuilder(int builderID, UnitCategory category) {
assert(category < CAT_LAST);
assert(builderID >= 0);
assert(ai->GetUnit(builderID) != NULL);
CUNIT* u = ai->GetUnit(builderID);
BuilderTracker* builderTracker = GetBuilderTracker(builderID);
const UnitDef* builderDef = ai->cb->GetUnitDef(builderID);
const int frame = ai->cb->GetCurrentFrame();
// make sure this builder is free
const bool b1 = (builderTracker->taskPlanId == 0);
const bool b2 = (builderTracker->buildTaskId == 0);
const bool b3 = (builderTracker->factoryId == 0);
const bool b4 = builderDef->canAssist;
const bool b5 = (category == CAT_FACTORY && frame >= 18000);
if (!b1 || !b2 || !b3 || !b4) {
if (b5) {
// note that FactoryBuilderAdd() asserts b1 through b4
// immediately after BuildTaskAddBuilder() is tried and
// fails in BuildUp(), so at least those must be true
// (and so should b5 in most of the *A mods)
std::stringstream msg;
msg << "[CUnitHandler::BuildTaskAddBuilder()][frame=" << frame << "]\n";
msg << "\tbuilder " << builderID << " not able to be added to CAT_FACTORY build-task\n";
msg << "\tb1: " << b1 << ", b2: " << b2 << ", b3: " << b3;
msg << ", b4: " << b4 << ", b5: " << b5;
ai->GetLogger()->Log(msg.str());
}
return false;
}
// see if there are any BuildTasks that it can join
if (BuildTasks[category].size() > 0) {
float largestTime = 0.0f;
std::list<BuildTask>::iterator task;
std::list<BuildTask>::iterator bestTask;
for (task = BuildTasks[category].begin(); task != BuildTasks[category].end(); task++) {
float buildTime = ai->math->ETT(*task) - ai->math->ETA(builderID, ai->cb->GetUnitPos(task->id));
if (buildTime > largestTime) {
largestTime = buildTime;
bestTask = task;
}
}
if (largestTime > 0.0f) {
BuildTaskAddBuilder(&*bestTask, builderTracker);
u->Repair(bestTask->id);
return true;
}
}
// see if there any joinable TaskPlans
if (TaskPlans[category].size() > 0) {
float largestTime = 0.0f;
std::list<TaskPlan>::iterator plan;
std::list<TaskPlan>::iterator bestPlan;
for (plan = TaskPlans[category].begin(); plan != TaskPlans[category].end(); plan++) {
float buildTime = (plan->def->buildTime / plan->currentBuildPower) - ai->math->ETA(builderID, plan->pos);
// must test if this builder can make this unit/building too
if (buildTime > largestTime) {
const std::vector<int>* canBuildList = &ai->ut->unitTypes[builderDef->id].canBuildList;
const int buildListSize = canBuildList->size();
for (int j = 0; j < buildListSize; j++) {
if (canBuildList->at(j) == plan->def->id) {
largestTime = buildTime;
bestPlan = plan;
break;
}
}
}
}
if (largestTime > 10.0f) {
assert(builderID >= 0);
// bad, CUNIT::Build() uses TaskPlanCreate()
// should we really give build orders here?
// return u->Build(bestPlan->pos, bestPlan->def, -1);
// TaskPlanCreate(builderID, bestPlan->pos, bestPlan->def);
return true;
}
}
if (b5) {
std::stringstream msg;
msg << "[CUnitHandler::BuildTaskAddBuilder()][frame=" << frame << "]\n";
msg << "\tno joinable CAT_FACTORY build-tasks or task-plans for builder " << builderID;
ai->GetLogger()->Log(msg.str());
}
return false;
}
void CUnitHandler::TaskPlanCreate(int builder, float3 pos, const UnitDef* builtdef) {
UnitCategory category = ai->ut->GetCategory(builtdef);
// HACK
if (category >= CAT_LAST) {
return;
}
// find this builder
BuilderTracker* builderTracker = GetBuilderTracker(builder);
// make sure this builder is free
bool b1 = (builderTracker->taskPlanId == 0);
bool b2 = (builderTracker->buildTaskId == 0);
bool b3 = (builderTracker->factoryId == 0);
if (!b1 || !b2 || !b3) {
return;
}
bool existingTP = false;
for (std::list<TaskPlan>::iterator i = TaskPlans[category].begin(); i != TaskPlans[category].end(); i++) {
if (pos.distance2D(i->pos) < 20.0f && builtdef == i->def) {
// make sure there are no other TaskPlans
if (!existingTP) {
existingTP = true;
TaskPlanAdd(&*i, builderTracker);
} else {
std::stringstream msg;
msg << "[CUnitHandler::TaskPlanCreate()][frame=" << ai->cb->GetCurrentFrame() << "]\n";
msg << "\ttask-plan for \"" << builtdef->humanName << "\" already present";
msg << " at position <" << pos.x << ", " << pos.y << ", " << pos.z << ">\n";
ai->GetLogger()->Log(msg.str());