-
Notifications
You must be signed in to change notification settings - Fork 119
/
ai.cpp
1104 lines (959 loc) · 29.7 KB
/
ai.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
// _________ __ __
// / _____// |_____________ _/ |______ ____ __ __ ______
// \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
// / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
// /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
// \/ \/ \//_____/ \/
// ______________________ ______________________
// T H E W A R B E G I N S
// Stratagus - A free fantasy real time strategy game engine
//
/**@name ai.cpp - The computer player AI main file. */
//
// (c) Copyright 2000-2007 by Lutz Sammer, Ludovic Pollet, and
// Jimmy Salmon
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; only version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//@{
//----------------------------------------------------------------------------
// Documentation
//----------------------------------------------------------------------------
/**
** @page AiModule Module - AI
**
** @section aibasics What is it?
**
** Stratagus uses a very simple scripted AI. There are no optimizations
** yet. The complete AI was written on one weekend.
** Until no AI specialist joins, I keep this AI.
**
** @subsection aiscripted What is scripted AI?
**
** The AI script tells the engine build 4 workers, than build 3 footman,
** than attack the player, than sleep 100 frames.
**
** @section API The AI API
**
** @subsection aimanage Management calls
**
** Manage the inititialse and cleanup of the AI players.
**
** ::InitAiModule()
**
** Initialise all global varaibles and structures.
** Called before AiInit, or before game loading.
**
** ::AiInit(::Player)
**
** Called for each player, to setup the AI structures
** Player::Aiin the player structure. It can use Player::AiName to
** select different AI's.
**
** ::CleanAi()
**
** Called to release all the memory for all AI structures.
** Must handle self which players contains AI structures.
**
** ::SaveAi(::FILE *)
**
** Save the AI structures of all players to file.
** Must handle self which players contains AI structures.
**
**
** @subsection aipcall Periodic calls
**
** This functions are called regular for all AI players.
**
** ::AiEachCycle(::Player)
**
** Called each game cycle, to handle quick checks, which needs
** less CPU.
**
** ::AiEachSecond(::Player)
**
** Called each second, to handle more CPU intensive things.
**
**
** @subsection aiecall Event call-backs
**
** This functions are called, when some special events happens.
**
** ::AiHelpMe()
**
** Called if a unit owned by the AI is attacked.
**
** ::AiUnitKilled()
**
** Called if a unit owned by the AI is killed.
**
** ::AiNeedMoreSupply()
**
** Called if an trained unit is ready, but not enough food is
** available for it.
**
** ::AiWorkComplete()
**
** Called if a unit has completed its work.
**
** ::AiCanNotBuild()
**
** Called if the AI unit can't build the requested unit-type.
**
** ::AiCanNotReach()
**
** Called if the AI unit can't reach the building place.
**
** ::AiTrainingComplete()
**
** Called if AI unit has completed training a new unit.
**
** ::AiUpgradeToComplete()
**
** Called if AI unit has completed upgrade to new unit-type.
**
** ::AiResearchComplete()
**
** Called if AI unit has completed research of an upgrade or spell.
*/
/*----------------------------------------------------------------------------
-- Includes
----------------------------------------------------------------------------*/
#include "stratagus.h"
#include "ai.h"
#include "ai_local.h"
#include "actions.h"
#include "action/action_attack.h"
#include "commands.h"
#include "iolib.h"
#include "map.h"
#include "pathfinder.h"
#include "player.h"
#include "script.h"
#include "unit.h"
#include "unit_manager.h"
#include "unittype.h"
#include "upgrade.h"
/*----------------------------------------------------------------------------
-- Variables
----------------------------------------------------------------------------*/
int AiSleepCycles; /// Ai sleeps # cycles
std::vector<CAiType *> AiTypes; /// List of all AI types.
AiHelper AiHelpers; /// AI helper variables
PlayerAi *AiPlayer; /// Current AI player
/*----------------------------------------------------------------------------
-- Lowlevel functions
----------------------------------------------------------------------------*/
/**
** Execute the AI Script.
*/
static void AiExecuteScript()
{
if (AiPlayer->Script.empty()) {
return;
}
lua_getglobal(Lua, "_ai_scripts_");
lua_pushstring(Lua, AiPlayer->Script.c_str());
lua_rawget(Lua, -2);
LuaCall(0, 1);
lua_pop(Lua, 1);
}
/**
** Check if everything is fine, send new requests to resource manager.
*/
static void AiCheckUnits()
{
// Count the already made build requests.
int counter[UnitTypeMax];
AiGetBuildRequestsCount(*AiPlayer, counter);
const int *unit_types_count = AiPlayer->Player->UnitTypesAiActiveCount;
// Look if some unit-types are missing.
int n = AiPlayer->UnitTypeRequests.size();
for (int i = 0; i < n; ++i) {
const unsigned int t = AiPlayer->UnitTypeRequests[i].Type->Slot;
const int x = AiPlayer->UnitTypeRequests[i].Count;
// Add equivalent units
int e = unit_types_count[t];
if (t < AiHelpers.Equiv().size()) {
for (unsigned int j = 0; j < AiHelpers.Equiv()[t].size(); ++j) {
e += unit_types_count[AiHelpers.Equiv()[t][j]->Slot];
}
}
const int requested = x - e - counter[t];
if (requested > 0) { // Request it.
AiAddUnitTypeRequest(*AiPlayer->UnitTypeRequests[i].Type, requested);
counter[t] += requested;
}
counter[t] -= x;
}
AiPlayer->Force.CheckUnits(counter);
// Look if some upgrade-to are missing.
n = AiPlayer->UpgradeToRequests.size();
for (int i = 0; i < n; ++i) {
const unsigned int t = AiPlayer->UpgradeToRequests[i]->Slot;
const int x = 1;
// Add equivalent units
int e = unit_types_count[t];
if (t < AiHelpers.Equiv().size()) {
for (unsigned int j = 0; j < AiHelpers.Equiv()[t].size(); ++j) {
e += unit_types_count[AiHelpers.Equiv()[t][j]->Slot];
}
}
const int requested = x - e - counter[t];
if (requested > 0) { // Request it.
AiAddUpgradeToRequest(*AiPlayer->UpgradeToRequests[i]);
counter[t] += requested;
}
counter[t] -= x;
}
// Look if some researches are missing.
n = (int)AiPlayer->ResearchRequests.size();
for (int i = 0; i < n; ++i) {
if (UpgradeIdAllowed(*AiPlayer->Player, AiPlayer->ResearchRequests[i]->ID) == 'A') {
AiAddResearchRequest(AiPlayer->ResearchRequests[i]);
}
}
}
/*----------------------------------------------------------------------------
-- Functions
----------------------------------------------------------------------------*/
/**
** Save state of player AI.
**
** @param file Output file.
** @param plynr Player number.
** @param ai Player AI.
*/
static void SaveAiPlayer(CFile &file, int plynr, const PlayerAi &ai)
{
file.printf("DefineAiPlayer(%d,\n", plynr);
file.printf(" \"ai-type\", \"%s\",\n", ai.AiType->Name.c_str());
file.printf(" \"script\", \"%s\",\n", ai.Script.c_str());
file.printf(" \"script-debug\", %s,\n", ai.ScriptDebug ? "true" : "false");
file.printf(" \"sleep-cycles\", %lu,\n", ai.SleepCycles);
// All forces
for (size_t i = 0; i < ai.Force.Size(); ++i) {
file.printf(" \"force\", {%d, %s%s%s", (int) i,
ai.Force[i].Completed ? "\"complete\"," : "\"recruit\",",
ai.Force[i].Attacking ? " \"attack\"," : "",
ai.Force[i].Defending ? " \"defend\"," : "");
file.printf(" \"role\", ");
switch (ai.Force[i].Role) {
case AiForceRoleAttack:
file.printf("\"attack\",");
break;
case AiForceRoleDefend:
file.printf("\"defend\",");
break;
default:
file.printf("\"unknown-%d\",", ai.Force[i].Role);
break;
}
file.printf("\n \"types\", { ");
const size_t unitTypesCounst = ai.Force[i].UnitTypes.size();
for (size_t j = 0; j != unitTypesCounst; ++j) {
const AiUnitType &aut = ai.Force[i].UnitTypes[j];
file.printf("%d, \"%s\", ", aut.Want, aut.Type->Ident.c_str());
}
file.printf("},\n \"units\", {");
const size_t unitsCount = ai.Force[i].Units.size();
for (size_t j = 0; j != unitsCount; ++j) {
const CUnit &aiunit = *ai.Force[i].Units[j];
file.printf(" %d, \"%s\",", UnitNumber(aiunit),
aiunit.Type->Ident.c_str());
}
file.printf("},\n \"state\", %d, \"goalx\", %d, \"goaly\", %d,",
ai.Force[i].State, ai.Force[i].GoalPos.x, ai.Force[i].GoalPos.y);
file.printf("},\n");
}
file.printf(" \"reserve\", {");
for (int i = 0; i < MaxCosts; ++i) {
file.printf("\"%s\", %d, ", DefaultResourceNames[i].c_str(), ai.Reserve[i]);
}
file.printf("},\n");
file.printf(" \"used\", {");
for (int i = 0; i < MaxCosts; ++i) {
file.printf("\"%s\", %d, ", DefaultResourceNames[i].c_str(), ai.Used[i]);
}
file.printf("},\n");
file.printf(" \"needed\", {");
for (int i = 0; i < MaxCosts; ++i) {
file.printf("\"%s\", %d, ", DefaultResourceNames[i].c_str(), ai.Needed[i]);
}
file.printf("},\n");
file.printf(" \"collect\", {");
for (int i = 0; i < MaxCosts; ++i) {
file.printf("\"%s\", %d, ", DefaultResourceNames[i].c_str(), ai.Collect[i]);
}
file.printf("},\n");
file.printf(" \"need-mask\", {");
for (int i = 0; i < MaxCosts; ++i) {
if (ai.NeededMask & (1 << i)) {
file.printf("\"%s\", ", DefaultResourceNames[i].c_str());
}
}
file.printf("},\n");
if (ai.NeedSupply) {
file.printf(" \"need-supply\",\n");
}
// Requests
if (!ai.FirstExplorationRequest.empty()) {
file.printf(" \"exploration\", {");
const size_t FirstExplorationRequestCount = ai.FirstExplorationRequest.size();
for (size_t i = 0; i != FirstExplorationRequestCount; ++i) {
const AiExplorationRequest &ptr = ai.FirstExplorationRequest[i];
file.printf("{%d, %d, %d}, ", ptr.pos.x, ptr.pos.y, ptr.Mask);
}
file.printf("},\n");
}
file.printf(" \"last-exploration-cycle\", %lu,\n", ai.LastExplorationGameCycle);
file.printf(" \"last-can-not-move-cycle\", %lu,\n", ai.LastCanNotMoveGameCycle);
file.printf(" \"unit-type\", {");
const size_t unitTypeRequestsCount = ai.UnitTypeRequests.size();
for (size_t i = 0; i != unitTypeRequestsCount; ++i) {
file.printf("\"%s\", ", ai.UnitTypeRequests[i].Type->Ident.c_str());
file.printf("%d, ", ai.UnitTypeRequests[i].Count);
}
file.printf("},\n");
file.printf(" \"upgrade\", {");
const size_t upgradeToRequestsCount = ai.UpgradeToRequests.size();
for (size_t i = 0; i != upgradeToRequestsCount; ++i) {
file.printf("\"%s\", ", ai.UpgradeToRequests[i]->Ident.c_str());
}
file.printf("},\n");
file.printf(" \"research\", {");
const size_t researchRequestsCount = ai.ResearchRequests.size();
for (size_t i = 0; i != researchRequestsCount; ++i) {
file.printf("\"%s\", ", ai.ResearchRequests[i]->Ident.c_str());
}
file.printf("},\n");
//
// Building queue
//
file.printf(" \"building\", {");
const size_t UnitTypeBuiltCount = ai.UnitTypeBuilt.size();
for (size_t i = 0; i != UnitTypeBuiltCount; ++i) {
const AiBuildQueue &queue = ai.UnitTypeBuilt[i];
/* rb - for backward compatibility of save format we have to put it first */
if (queue.Pos.x != -1) {
file.printf("\"onpos\", %d, %d, ", queue.Pos.x, queue.Pos.y);
}
/* */
file.printf("\"%s\", %d, %d", queue.Type->Ident.c_str(), queue.Made, queue.Want);
if (i < UnitTypeBuiltCount - 1) {
file.printf(",\n");
}
}
file.printf("},\n");
file.printf(" \"repair-building\", %u\n", ai.LastRepairBuilding);
file.printf(")\n\n");
}
/**
** Save state of AI to file.
**
** @param file Output file.
*/
void SaveAi(CFile &file)
{
file.printf("\n--- -----------------------------------------\n");
for (int i = 0; i < PlayerMax; ++i) {
if (Players[i].Ai) {
SaveAiPlayer(file, i, *Players[i].Ai);
}
}
DebugPrint("FIXME: Saving lua function definition isn't supported\n");
}
/**
** Setup all at start.
**
** @param player The player structure pointer.
*/
void AiInit(CPlayer &player)
{
PlayerAi *pai = new PlayerAi;
if (!pai) {
fprintf(stderr, "Out of memory.\n");
exit(0);
}
pai->Player = &player;
DebugPrint("%d - looking for class %s\n" _C_ player.Index _C_ player.AiName.c_str());
//MAPTODO print the player name (player->Name) instead of the pointer
// Search correct AI type.
if (AiTypes.empty()) {
DebugPrint("AI: Got no scripts at all! You need at least one dummy fallback script.\n");
DebugPrint("AI: Look at the DefineAi() documentation.\n");
Exit(0);
}
size_t i;
CAiType *ait = NULL;
for (i = 0; i < AiTypes.size(); ++i) {
ait = AiTypes[i];
if (!ait->Race.empty() && ait->Race != PlayerRaces.Name[player.Race]) {
continue;
}
if (!player.AiName.empty() && ait->Name != player.AiName) {
continue;
}
break;
}
if (i == AiTypes.size()) {
DebugPrint("AI: Found no matching ai scripts at all!\n");
// FIXME: surely we can do something better than exit
exit(0);
}
if (player.AiName.empty()) {
DebugPrint("AI: not found!!!!!!!!!!\n");
DebugPrint("AI: Using fallback:\n");
}
DebugPrint("AI: %s:%s with %s:%s\n" _C_ PlayerRaces.Name[player.Race].c_str() _C_
!ait->Race.empty() ? ait->Race.c_str() : "All" _C_ player.AiName.c_str() _C_ ait->Class.c_str());
pai->AiType = ait;
pai->Script = ait->Script;
pai->Collect[GoldCost] = 50;
pai->Collect[WoodCost] = 50;
pai->Collect[OilCost] = 0;
player.Ai = pai;
}
/**
** Initialize global structures of the AI
*/
void InitAiModule()
{
AiResetUnitTypeEquiv();
}
/**
** Cleanup the AI in order to enable to restart a game.
*/
void CleanAi()
{
for (int p = 0; p < PlayerMax; ++p) {
delete Players[p].Ai;
Players[p].Ai = NULL;
}
}
/**
** Free all AI resources.
*/
void FreeAi()
{
CleanAi();
// Free AiTypes.
for (unsigned int i = 0; i < AiTypes.size(); ++i) {
CAiType *aitype = AiTypes[i];
delete aitype;
}
AiTypes.clear();
// Free AiHelpers.
AiHelpers.Train().clear();
AiHelpers.Build().clear();
AiHelpers.Upgrade().clear();
AiHelpers.Research().clear();
AiHelpers.Repair().clear();
AiHelpers.UnitLimit().clear();
AiHelpers.Equiv().clear();
AiHelpers.Refinery().clear();
AiHelpers.Depots().clear();
AiResetUnitTypeEquiv();
}
/*----------------------------------------------------------------------------
-- Support functions
----------------------------------------------------------------------------*/
/**
** Remove unit-type from build list.
**
** @param pai Computer AI player.
** @param type Unit-type which is now available.
** @return True, if unit-type was found in list.
*/
static int AiRemoveFromBuilt2(PlayerAi *pai, const CUnitType &type)
{
std::vector<AiBuildQueue>::iterator i;
for (i = pai->UnitTypeBuilt.begin(); i != pai->UnitTypeBuilt.end(); ++i) {
Assert((*i).Want);
if (&type == (*i).Type && (*i).Made) {
--(*i).Made;
if (!--(*i).Want) {
pai->UnitTypeBuilt.erase(i);
}
return 1;
}
}
return 0;
}
/**
** Remove unit-type from build list.
**
** @param pai Computer AI player.
** @param type Unit-type which is now available.
*/
static void AiRemoveFromBuilt(PlayerAi *pai, const CUnitType &type)
{
if (AiRemoveFromBuilt2(pai, type)) {
return;
}
// This could happen if an upgrade is ready, look for equivalent units.
int equivalents[UnitTypeMax + 1];
const int equivalentsCount = AiFindUnitTypeEquiv(type, equivalents);
for (int i = 0; i < equivalentsCount; ++i) {
if (AiRemoveFromBuilt2(pai, *UnitTypes[equivalents[i]])) {
return;
}
}
if (pai->Player == ThisPlayer) {
DebugPrint("My guess is that you built something under ai me. naughty boy!\n");
return;
}
fprintf(stderr, "Can't reduce %s from build list.\n", type.Ident.c_str());
}
/**
** Reduce made unit-type from build list.
**
** @param pai Computer AI player.
** @param type Unit-type which is now available.
** @return True if the unit-type could be reduced.
*/
static bool AiReduceMadeInBuilt2(PlayerAi &pai, const CUnitType &type)
{
std::vector<AiBuildQueue>::iterator i;
for (i = pai.UnitTypeBuilt.begin(); i != pai.UnitTypeBuilt.end(); ++i) {
if (&type == (*i).Type && (*i).Made) {
(*i).Made--;
return true;
}
}
return false;
}
/**
** Reduce made unit-type from build list.
**
** @param pai Computer AI player.
** @param type Unit-type which is now available.
*/
void AiReduceMadeInBuilt(PlayerAi &pai, const CUnitType &type)
{
if (AiReduceMadeInBuilt2(pai, type)) {
return;
}
// This could happen if an upgrade is ready, look for equivalent units.
int equivs[UnitTypeMax + 1];
const unsigned int equivnb = AiFindUnitTypeEquiv(type, equivs);
for (unsigned int i = 0; i < equivnb; ++i) {
if (AiReduceMadeInBuilt2(pai, *UnitTypes[equivs[i]])) {
return;
}
}
if (pai.Player == ThisPlayer) {
DebugPrint("My guess is that you built something under ai me. naughty boy!\n");
return;
}
fprintf(stderr, "Can't reduce %s from build list.\n", type.Ident.c_str());
}
/*----------------------------------------------------------------------------
-- Callback Functions
----------------------------------------------------------------------------*/
/**
** Called if a Unit is Attacked
**
** @param attacker Pointer to attacker unit.
** @param defender Pointer to unit that is being attacked.
*/
void AiHelpMe(const CUnit *attacker, CUnit &defender)
{
/* Friendly Fire - typical splash */
if (!attacker || attacker->Player->Index == defender.Player->Index) {
//FIXME - try react somehow
return;
}
DebugPrint("%d: %d(%s) attacked at %d,%d\n" _C_
defender.Player->Index _C_ UnitNumber(defender) _C_
defender.Type->Ident.c_str() _C_ defender.tilePos.x _C_ defender.tilePos.y);
// Don't send help to scouts (zeppelin,eye of vision).
if (!defender.Type->CanAttack && defender.Type->UnitType == UnitTypeFly) {
return;
}
// Summoned unit, don't help
if (defender.Summoned) {
return;
}
PlayerAi &pai = *defender.Player->Ai;
AiPlayer = &pai;
// If unit belongs to an attacking force, check if force members can help.
if (defender.GroupId) {
AiForce &aiForce = pai.Force[defender.GroupId - 1];
// Unit belongs to an force, check if brothers in arms can help
for (unsigned int i = 0; i < aiForce.Units.size(); ++i) {
CUnit &aiunit = *aiForce.Units[i];
if (&defender == &aiunit) {
continue;
}
// if brother is idle or attack no-agressive target and
// can attack our attacker then ask for help
// FIXME ad support for help from Coward type units
if (aiunit.IsAgressive() && CanTarget(*aiunit.Type, *attacker->Type)
&& aiunit.CurrentOrder()->GetGoal() != attacker) {
bool shouldAttack = aiunit.IsIdle() && aiunit.Threshold == 0;
if (aiunit.CurrentAction() == UnitActionAttack) {
const COrder_Attack &orderAttack = *static_cast<COrder_Attack *>(aiunit.CurrentOrder());
const CUnit *oldGoal = orderAttack.GetGoal();
if (oldGoal == NULL || (ThreatCalculate(defender, *attacker) < ThreatCalculate(defender, *oldGoal)
&& aiunit.MapDistanceTo(defender) <= aiunit.Stats->Variables[ATTACKRANGE_INDEX].Max)) {
shouldAttack = true;
}
}
if (shouldAttack) {
CommandAttack(aiunit, attacker->tilePos, const_cast<CUnit *>(attacker), FlushCommands);
COrder *savedOrder = COrder::NewActionAttack(aiunit, attacker->tilePos);
if (aiunit.CanStoreOrder(savedOrder) == false) {
delete savedOrder;
savedOrder = NULL;
} else {
aiunit.SavedOrder = savedOrder;
}
}
}
}
if (!aiForce.Defending && aiForce.State > 0) {
DebugPrint("%d: %d(%s) belong to attacking force, don't defend it\n" _C_
defender.Player->Index _C_ UnitNumber(defender) _C_ defender.Type->Ident.c_str());
// unit belongs to an attacking force,
// so don't send others force in such case.
// FIXME: there may be other attacking the same place force who can help
return;
}
}
// Send defending forces, also send attacking forces if they are home/traning.
// This is still basic model where we suspect only one base ;(
const Vec2i &pos = attacker->tilePos;
for (unsigned int i = 0; i < pai.Force.Size(); ++i) {
AiForce &aiForce = pai.Force[i];
if (aiForce.Size() > 0
&& ((aiForce.Role == AiForceRoleDefend && !aiForce.Attacking)
|| (aiForce.Role == AiForceRoleAttack && !aiForce.Attacking && !aiForce.State))) { // none attacking
aiForce.Defending = true;
aiForce.Attack(pos);
}
}
}
/**
** Called if a unit is killed.
**
** @param unit Pointer to unit.
*/
void AiUnitKilled(CUnit &unit)
{
DebugPrint("%d: %d(%s) killed\n" _C_
unit.Player->Index _C_ UnitNumber(unit) _C_ unit.Type->Ident.c_str());
Assert(unit.Player->Type != PlayerTypes::PlayerPerson);
if (unit.GroupId) {
AiForce &force = unit.Player->Ai->Force[unit.GroupId - 1];
force.Remove(unit);
if (force.Size() == 0) {
force.Attacking = false;
if (!force.Defending && force.State > 0) {
DebugPrint("%d: Attack force #%lu was destroyed, giving up\n"
_C_ unit.Player->Index _C_(long unsigned int)(&force - & (unit.Player->Ai->Force[0])));
force.Reset(true);
}
}
}
unit.CurrentOrder()->AiUnitKilled(unit);
}
/**
** Called if work complete (Buildings).
**
** @param unit Pointer to unit that builds the building.
** @param what Pointer to unit building that was built.
*/
void AiWorkComplete(CUnit *unit, CUnit &what)
{
if (unit) {
DebugPrint("%d: %d(%s) build %s at %d,%d completed\n" _C_
what.Player->Index _C_ UnitNumber(*unit) _C_ unit->Type->Ident.c_str() _C_
what.Type->Ident.c_str() _C_ unit->tilePos.x _C_ unit->tilePos.y);
} else {
DebugPrint("%d: building %s at %d,%d completed\n" _C_
what.Player->Index _C_ what.Type->Ident.c_str() _C_ what.tilePos.x _C_ what.tilePos.y);
}
Assert(what.Player->Type != PlayerTypes::PlayerPerson);
AiRemoveFromBuilt(what.Player->Ai, *what.Type);
}
/**
** Called if building can't be build.
**
** @param unit Pointer to unit what builds the building.
** @param what Pointer to unit-type.
*/
void AiCanNotBuild(const CUnit &unit, const CUnitType &what)
{
DebugPrint("%d: %d(%s) Can't build %s at %d,%d\n" _C_
unit.Player->Index _C_ UnitNumber(unit) _C_ unit.Type->Ident.c_str() _C_
what.Ident.c_str() _C_ unit.tilePos.x _C_ unit.tilePos.y);
Assert(unit.Player->Type != PlayerTypes::PlayerPerson);
AiReduceMadeInBuilt(*unit.Player->Ai, what);
}
/**
** Called if building place can't be reached.
**
** @param unit Pointer to unit what builds the building.
** @param what Pointer to unit-type.
*/
void AiCanNotReach(CUnit &unit, const CUnitType &what)
{
Assert(unit.Player->Type != PlayerTypes::PlayerPerson);
AiReduceMadeInBuilt(*unit.Player->Ai, what);
}
/**
** Try to move a unit that's in the way
*/
static void AiMoveUnitInTheWay(CUnit &unit)
{
static Vec2i dirs[8] = {Vec2i(-1, -1), Vec2i(-1, 0), Vec2i(-1, 1), Vec2i(0, 1), Vec2i(1, 1), Vec2i(1, 0), Vec2i(1, -1), Vec2i(0, -1)};
CUnit *movableunits[16];
Vec2i movablepos[16];
int movablenb;
AiPlayer = unit.Player->Ai;
// No more than 1 move per 10 cycle ( avoid stressing the pathfinder )
if (GameCycle <= AiPlayer->LastCanNotMoveGameCycle + 10) {
return;
}
const CUnitType &unittype = *unit.Type;
const Vec2i u0 = unit.tilePos;
const Vec2i u1(u0.x + unittype.TileWidth - 1, u0.y + unittype.TileHeight - 1);
movablenb = 0;
// Try to make some unit moves around it
for (CUnitManager::Iterator it = UnitManager->begin(); it != UnitManager->end(); ++it) {
CUnit &blocker = **it;
if (blocker.IsUnusable()) {
continue;
}
if (!blocker.CanMove() || blocker.Moving) {
continue;
}
if (blocker.Player != unit.Player && blocker.Player->IsAllied(*unit.Player) == false) {
continue;
}
const CUnitType &blockertype = *blocker.Type;
if (blockertype.UnitType != unittype.UnitType) {
continue;
}
const Vec2i b0 = blocker.tilePos;
const Vec2i b1(b0.x + blockertype.TileWidth - 1, b0.y + blockertype.TileHeight - 1);
if (&unit == &blocker) {
continue;
}
// Check for collision
if (unit.MapDistanceTo(blocker) >= unit.Type->TileWidth + 1) {
continue;
}
// Move blocker in a rand dir
int r = SyncRand() & 7;
int trycount = 8;
while (trycount > 0) {
r = (r + 1) & 7;
--trycount;
const Vec2i pos = blocker.tilePos + blocker.Type->TileWidth * dirs[r];
// Out of the map => no !
if (!Map.Info.IsPointOnMap(pos)) {
continue;
}
// move to blocker ? => no !
if (pos == u0) {
continue;
}
if (Map.Field(pos)->UnitCache.size() > 0) {
continue;
}
movableunits[movablenb] = &blocker;
movablepos[movablenb] = pos;
++movablenb;
trycount = 0;
}
if (movablenb >= 16) {
break;
}
}
// Don't move more than 1 unit.
if (movablenb) {
const int index = SyncRand() % movablenb;
COrder *savedOrder = NULL;
if (movableunits[index]->IsIdle() == false) {
if (unit.CanStoreOrder(unit.CurrentOrder())) {
savedOrder = unit.CurrentOrder()->Clone();
}
}
CommandMove(*movableunits[index], movablepos[index], FlushCommands);
if (savedOrder != NULL) {
unit.SavedOrder = savedOrder;
}
AiPlayer->LastCanNotMoveGameCycle = GameCycle;
}
}
/**
** Called if a unit can't move. Try to move unit in the way
**
** @param unit Pointer to unit what builds the building.
*/
void AiCanNotMove(CUnit &unit)
{
const Vec2i &goalPos = unit.pathFinderData->input.GetGoalPos();
const int gw = unit.pathFinderData->input.GetGoalSize().x;
const int gh = unit.pathFinderData->input.GetGoalSize().y;
AiPlayer = unit.Player->Ai;
if (PlaceReachable(unit, goalPos, gw, gh, 0, 255, false)) {
// Path probably closed by unit here
AiMoveUnitInTheWay(unit);
}
}
/**
** Called if the AI needs more farms.
**
** @param player player which need supply.
*/
void AiNeedMoreSupply(const CPlayer &player)
{
Assert(player.Type != PlayerTypes::PlayerPerson);
player.Ai->NeedSupply = true;
}
/**
** Called if training of a unit is completed.
**
** @param unit Pointer to unit making.
** @param what Pointer to new ready trained unit.
*/
void AiTrainingComplete(CUnit &unit, CUnit &what)
{
DebugPrint("%d: %d(%s) training %s at %d,%d completed\n" _C_
unit.Player->Index _C_ UnitNumber(unit) _C_ unit.Type->Ident.c_str() _C_
what.Type->Ident.c_str() _C_ unit.tilePos.x _C_ unit.tilePos.y);
Assert(unit.Player->Type != PlayerTypes::PlayerPerson);
AiRemoveFromBuilt(unit.Player->Ai, *what.Type);
unit.Player->Ai->Force.RemoveDeadUnit();
unit.Player->Ai->Force.Assign(what);
}
/**
** Called if upgrading of an unit is completed.
**
** @param unit Pointer to unit working.
** @param what Pointer to the new unit-type.
*/
void AiUpgradeToComplete(CUnit &unit, const CUnitType &what)
{
DebugPrint("%d: %d(%s) upgrade-to %s at %d,%d completed\n" _C_
unit.Player->Index _C_ UnitNumber(unit) _C_ unit.Type->Ident.c_str() _C_
what.Ident.c_str() _C_ unit.tilePos.x _C_ unit.tilePos.y);
Assert(unit.Player->Type != PlayerTypes::PlayerPerson);
}
/**
** Called if reseaching of an unit is completed.
**
** @param unit Pointer to unit working.
** @param what Pointer to the new upgrade.
*/
void AiResearchComplete(CUnit &unit, const CUpgrade *what)
{
DebugPrint("%d: %d(%s) research %s at %d,%d completed\n" _C_
unit.Player->Index _C_ UnitNumber(unit) _C_ unit.Type->Ident.c_str() _C_
what->Ident.c_str() _C_ unit.tilePos.x _C_ unit.tilePos.y);
Assert(unit.Player->Type != PlayerTypes::PlayerPerson);
// FIXME: upgrading knights -> paladins, must rebuild lists!
}
/**
** This is called for each player, each game cycle.