forked from phu004/JavaRTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombatManagerAI.java
More file actions
785 lines (600 loc) · 32.4 KB
/
Copy pathcombatManagerAI.java
File metadata and controls
785 lines (600 loc) · 32.4 KB
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
package enemyAI;
import core.baseInfo;
import core.gameData;
import core.mainThread;
import core.vector;
import entity.goldMine;
import entity.solidObject;
import entity.techCenter;
//this agent makes all the high level combat decisions
public class combatManagerAI {
public baseInfo theBaseInfo;
public int frameAI;
public int currentState;
public final int booming = 0;
public final int aggressing = 1;
public final int defending = 2;
public goldMine[] goldMines;
float distanceToTarget;
public vector gatherPoint, attackDirection, attackPosition;
public solidObject unNeutralizedEntity;
public solidObject[] team;
public int numberOfUnitInCombatRadius;
public int numberOfUnitOutsideCombatRadius;
public float unitInCombactRadiusPercentage;
public solidObject[] unitInCombatRadius;
public solidObject[] unitOutsideCombatRadius;
public solidObject[] troopsControlledByCombatAI;
public solidObject[] playerUnitInMinimap;
public vector playerForceCenter;
public vector adjustedAttackDirection;
public int withdrawUnitOutsideCombatRadiusCooldown;
public float maxPlayerForceStrengthRoundAttacker;
public float combatCenterX, combatCenterZ;
public float myRallyPointX, myRallyPointZ;
public boolean rallyPointChanged;
public float unrevealedPlayerForceStrength;
public int noPlayerActivityCountdown;
public boolean staticDefenseAhead;
public boolean staticDefenseNearAttackPosition;
public boolean dealWithMajorThreat;
public boolean unitCountLow;
public int attackTime, standardAttackTime, rushAttackTime;
public int stateSwitchingCooldown;
public combatManagerAI(){
this.theBaseInfo = mainThread.ec.theBaseInfo;
standardAttackTime = 500;
if(mainThread.ec.difficulty == 2)
standardAttackTime = 630;
rushAttackTime = 250 + gameData.getRandom()/5;
goldMines = mainThread.theAssetManager.goldMines;
gatherPoint = new vector(-1,-1,-1);
attackDirection = new vector(0,0,0);
attackPosition = new vector(0,0,0);
playerForceCenter = new vector(0,0,0);
adjustedAttackDirection = new vector(0,0,0);
}
//check if player has expansions
//if player has expansion(s) then find the one that is least defended
//compare player force with its own force
//if AI thinks its has a comparable or greater force than the player, attack player's expansion and switch to aggressing mode
//if AI is under significant threat, switch to defending mode
public void processAI(){
frameAI = mainThread.ec.frameAI;
if(mainThread.ec.theMapAwarenessAI.numberOfPlayerUnitsOnMinimap != 0)
noPlayerActivityCountdown=150;
else if(noPlayerActivityCountdown > 0)
noPlayerActivityCountdown--;
//assume player force gets stronger as time goes by
if(unrevealedPlayerForceStrength < 0)
unrevealedPlayerForceStrength = 0;
if(noPlayerActivityCountdown > 0 && mainThread.ec.theMapAwarenessAI.totalNumberOfPlayerUnits > 0) {
if(frameAI < 360)
unrevealedPlayerForceStrength+=0.075f;
else
unrevealedPlayerForceStrength+=0.1f;
}
if(withdrawUnitOutsideCombatRadiusCooldown > 0){
withdrawUnitOutsideCombatRadiusCooldown --;
}else{
maxPlayerForceStrengthRoundAttacker = 0;
}
if(stateSwitchingCooldown > 0)
stateSwitchingCooldown--;
team = mainThread.ec.theUnitProductionAI.troopsControlledByCombatAI;
numberOfUnitInCombatRadius = mainThread.ec.theUnitProductionAI.numberOfUnitInCombatRadius;
numberOfUnitOutsideCombatRadius = mainThread.ec.theUnitProductionAI.numberOfUnitOutsideCombatRadius;
unitInCombatRadius = mainThread.ec.theUnitProductionAI.unitInCombatRadius;
unitOutsideCombatRadius = mainThread.ec.theUnitProductionAI.unitOutsideCombatRadius;
troopsControlledByCombatAI = mainThread.ec.theUnitProductionAI.troopsControlledByCombatAI;
playerUnitInMinimap = mainThread.ec.theMapAwarenessAI.playerUnitInMinimap;
combatCenterX = mainThread.ec.theUnitProductionAI.combatAICenterX;
combatCenterZ = mainThread.ec.theUnitProductionAI.combatAICenterZ;
boolean frontalTroopIverwhelmed = false;
boolean shouldAttack = false;
boolean playerHasBecomeStrongerThanAIDuringMarching = false;
rallyPointChanged = false;
if(myRallyPointX != mainThread.ec.theUnitProductionAI.rallyPoint.x){
myRallyPointX = mainThread.ec.theUnitProductionAI.rallyPoint.x;
myRallyPointZ = mainThread.ec.theUnitProductionAI.rallyPoint.z;
rallyPointChanged = true;
}
int numberOfLightTanks_AI = mainThread.ec.theUnitProductionAI.numberOfLightTanksControlledByCombatAI;
int numberOfRocketTanks_AI = mainThread.ec.theUnitProductionAI.numberOfRocketTanksControlledByCombatAI;
int numberOfStealthTanks_AI = mainThread.ec.theUnitProductionAI.numberOfStealthTanksControlledByCombatAI;
int numberOfHeavyTanks_AI = mainThread.ec.theUnitProductionAI.numberOfHeavyTanksControlledByCombatAI;
unitCountLow = (numberOfLightTanks_AI + numberOfRocketTanks_AI + numberOfStealthTanks_AI + numberOfHeavyTanks_AI * 2 < 9) && frameAI > 480;
if(currentState == booming){
//enemy AI compares its own force with player's force, then make a decision whether it should attack or not
attackTime = standardAttackTime;
if(mainThread.ec.theMapAwarenessAI.canRushPlayer && mainThread.ec.difficulty > 0)
attackTime = rushAttackTime;
int targetPlayerExpension = mainThread.ec.theMapAwarenessAI.targetPlayerExpension;
if(frameAI > attackTime) {
if(targetPlayerExpension == 0 || targetPlayerExpension == 1 || targetPlayerExpension == 6 || targetPlayerExpension == 7)
if(frameAI < 700)
shouldAttack = checkIfAIHasBiggerForce(0.5f);
else
shouldAttack = checkIfAIHasBiggerForce(0.75f);
else
shouldAttack = checkIfAIHasBiggerForce(1.2f);
if(mainThread.ec.theUnitProductionAI.numberOfCombatUnit > 75)
shouldAttack = true;
}
if(shouldAttack){
if(targetPlayerExpension != -1){
currentState = aggressing;
attackDirection.set(goldMines[targetPlayerExpension].centre.x - combatCenterX, 0, goldMines[targetPlayerExpension].centre.z - combatCenterZ);
attackDirection.unit();
attackPosition.set(goldMines[targetPlayerExpension].centre);
attackPosition.add(attackDirection);
return;
}else{
//if no enemy structure found around gold mines, set attack position to a revealed enemy building or unit
solidObject[] playerStructures = mainThread.ec.theMapAwarenessAI.playerStructures;
solidObject[] playerUnitInMinimap = mainThread.ec.theMapAwarenessAI.playerUnitInMinimap;
attackDirection.set(0,0,0);
for(int i = 0; i < playerStructures.length; i++){
if(playerStructures[i] != null && playerStructures[i].currentHP > 0){
currentState = aggressing;
attackDirection.set(playerStructures[i].centre.x - combatCenterX, 0, playerStructures[i].centre.z - combatCenterZ);
attackDirection.unit();
attackPosition.set(playerStructures[i].centre);
attackPosition.add(attackDirection);
return;
}
}
if(attackDirection.x == 0 && attackDirection.z ==0){
for(int i = 0; i < playerUnitInMinimap.length; i++){
if(playerUnitInMinimap[i] != null && playerUnitInMinimap[i].currentHP > 0){
currentState = aggressing;
attackDirection.set(playerUnitInMinimap[i].centre.x - combatCenterX, 0, playerUnitInMinimap[i].centre.z - combatCenterZ);
attackDirection.unit();
attackPosition.set(playerUnitInMinimap[i].centre);
attackPosition.add(attackDirection);
return;
}
}
}
//if AI couldn't find a player base nor player's unit then set the attack position to the player spawning point
currentState = aggressing;
attackDirection.set(goldMines[0].centre.x - combatCenterX, 0, goldMines[0].centre.z - combatCenterZ);
attackDirection.unit();
attackPosition.set(goldMines[0].centre);
attackPosition.add(attackDirection);
}
}else {
//check if defenceManager found a major threat
if(mainThread.ec.theDefenseManagerAI.majorThreatLocation.x != 0) {
if(unitCountLow)
return;
currentState = aggressing;
attackDirection.set(mainThread.ec.theDefenseManagerAI.majorThreatLocation.x - combatCenterX, 0, mainThread.ec.theDefenseManagerAI.majorThreatLocation.z - combatCenterZ);
attackDirection.unit();
attackPosition.set(mainThread.ec.theDefenseManagerAI.majorThreatLocation);
return;
}
//check if there are any player units/structures near the combat center
solidObject[] playerUnitInMinimap = mainThread.ec.theMapAwarenessAI.playerUnitInMinimap;
solidObject[] playerStructures = mainThread.ec.theMapAwarenessAI.playerStructures;
for(int i = 0; i < playerUnitInMinimap.length; i++) {
if(playerUnitInMinimap[i] != null && playerUnitInMinimap[i].currentHP > 0) {
double d = Math.sqrt((combatCenterX - playerUnitInMinimap[i].centre.x)*(combatCenterX - playerUnitInMinimap[i].centre.x) + (combatCenterZ - playerUnitInMinimap[i].centre.z)*(combatCenterZ - playerUnitInMinimap[i].centre.z));
if(d < 7){
currentState = aggressing;
attackDirection.set(playerUnitInMinimap[i].centre.x - combatCenterX, 0, playerUnitInMinimap[i].centre.z - combatCenterZ);
attackDirection.unit();
attackPosition.set(playerUnitInMinimap[i].centre);
return;
}
}
}
for(int i = 0; i < playerStructures.length; i++) {
if(playerStructures[i] != null && playerStructures[i].currentHP > 0) {
double d = Math.sqrt((combatCenterX - playerStructures[i].centre.x)*(combatCenterX - playerStructures[i].centre.x) + (combatCenterZ - playerStructures[i].centre.z)*(combatCenterZ - playerStructures[i].centre.z));
if(d < 7){
currentState = aggressing;
attackDirection.set(playerStructures[i].centre.x - combatCenterX, 0, playerStructures[i].centre.z - combatCenterZ);
attackDirection.unit();
attackPosition.set(playerStructures[i].centre);
return;
}
}
}
}
if(currentState != aggressing) {
if(rallyPointChanged || Math.abs(myRallyPointX - combatCenterX) > 1 || Math.abs(myRallyPointZ - combatCenterZ) > 1){
for(int i =0 ; i < troopsControlledByCombatAI.length; i++){
if(troopsControlledByCombatAI[i] != null && troopsControlledByCombatAI[i].currentHP > 0){
if(Math.abs(troopsControlledByCombatAI[i].destinationX - myRallyPointX) > 0.25 || Math.abs(troopsControlledByCombatAI[i].destinationY - myRallyPointZ) > 0.25) {
if(troopsControlledByCombatAI[i].secondaryDestinationX != myRallyPointX || troopsControlledByCombatAI[i].secondaryDestinationY != myRallyPointZ) {
troopsControlledByCombatAI[i].attackMoveTo(myRallyPointX, myRallyPointZ);
troopsControlledByCombatAI[i].currentCommand = solidObject.attackMove;
troopsControlledByCombatAI[i].secondaryCommand = solidObject.attackMove;
}
}
}
}
}
}
}else if(currentState == aggressing && stateSwitchingCooldown == 0){
//check if a major threat is found other than the current attack position
if(mainThread.ec.theDefenseManagerAI.majorThreatLocation.x != 0){
float xPos = mainThread.ec.theDefenseManagerAI.majorThreatLocation.x;
float zPos = mainThread.ec.theDefenseManagerAI.majorThreatLocation.z;
float d1 = (attackPosition.x - combatCenterX)*(attackPosition.x - combatCenterX) + (attackPosition.z - combatCenterZ)*(attackPosition.z - combatCenterZ);
float d2 = (xPos - combatCenterX)*(xPos - combatCenterX) + (zPos - combatCenterZ)*(zPos - combatCenterZ);
if(d2 -2 <= d1) {
attackPosition.set(xPos, 0, zPos);
}
dealWithMajorThreat = true;
}else {
if(dealWithMajorThreat == true) {
currentState = booming;
dealWithMajorThreat = false;
return;
}
}
attackDirection.set(attackPosition.x - combatCenterX, 0, attackPosition.z - combatCenterZ);
distanceToTarget = attackDirection.getLength();
attackDirection.unit();
//check if the target position has been neutralized
solidObject[] playerUnitInMinimap = mainThread.ec.theMapAwarenessAI.playerUnitInMinimap;
solidObject[] playerStructures = mainThread.ec.theMapAwarenessAI.playerStructures;
unNeutralizedEntity = null;
//look for revealed player building structures
for(int i = 0; i < playerStructures.length; i++){
if(playerStructures[i] != null){
if((playerStructures[i].centre.x - attackPosition.x)*(playerStructures[i].centre.x - attackPosition.x) + (playerStructures[i].centre.z - attackPosition.z)*(playerStructures[i].centre.z - attackPosition.z) < 16){
unNeutralizedEntity = playerStructures[i];
attackPosition.set(playerStructures[i].centre);
break;
}
}
}
//if there is no player structure found, then look for player units
boolean needResetAttackPosition = false;
if(unNeutralizedEntity == null){
for(int i = 0; i < playerUnitInMinimap.length; i++){
if(playerUnitInMinimap[i] != null){
unNeutralizedEntity = playerUnitInMinimap[i];
needResetAttackPosition = true;
if((playerUnitInMinimap[i].centre.x - attackPosition.x)*(playerUnitInMinimap[i].centre.x - attackPosition.x) + (playerUnitInMinimap[i].centre.z - attackPosition.z)*(playerUnitInMinimap[i].centre.z - attackPosition.z) < 16){
needResetAttackPosition = false;
break;
}
}
}
}
if(needResetAttackPosition) {
attackPosition.set(unNeutralizedEntity.centre);
}
//if front portion of the troops are under attack, set the attack position to the attacker
for(int i = 0; i < numberOfUnitInCombatRadius; i++){
if(unitInCombatRadius[i].underAttackCountDown > 0){
attackPosition.set(unitInCombatRadius[i].attacker.centre);
unNeutralizedEntity = unitInCombatRadius[i].attacker;
break;
}
}
//check if the front portion of the troops are overwhelmed by player force
float playerForceStrengthNearCombatCenter = checkPlayerForceStrengthAroundOnePoint(playerUnitInMinimap, combatCenterX + attackDirection.x, combatCenterZ + attackDirection.z, 4);
if((playerForceStrengthNearCombatCenter+1)/(getAIForceStrength(unitInCombatRadius)+1) > 2f){
frontalTroopIverwhelmed = true;
}
//check if the player force has become stronger than the AI during the marching towards attack position
//System.out.println("distanceToTarget: " + distanceToTarget);
if(checkIfAIHasBiggerForce(1.5f) == false && distanceToTarget > 8 && !(mainThread.ec.theMapAwarenessAI.playerForceNearBase && dealWithMajorThreat)){
playerHasBecomeStrongerThanAIDuringMarching = true;
}
//check if the troops is near a concentration of player's static defense.
//If true, then check if AI has enough troops to deal with the static defense.
staticDefenseAhead = false;
int staticDefenseThreshold = 0;
if(frameAI > 600 && mainThread.ec.theUnitProductionAI.numberOfUnitInCombatRadius > 15)
staticDefenseThreshold = 4;
double distanceToTower = 999;
for(int i = 0; i < mainThread.ec.theMapAwarenessAI.playerStaticDefenseLocations.length; i++) {
if(mainThread.ec.theMapAwarenessAI.playerStaticDefenseSize[i] > staticDefenseThreshold) {
float xPos = mainThread.ec.theMapAwarenessAI.playerStaticDefenseLocations[i].x;
float zPos = mainThread.ec.theMapAwarenessAI.playerStaticDefenseLocations[i].z;
distanceToTower = Math.sqrt((xPos - combatCenterX)*(xPos - combatCenterX) + (zPos - combatCenterZ)*(zPos - combatCenterZ));
if(distanceToTower < 4.5) {
staticDefenseAhead = true;
break;
}
}
}
if(mainThread.ec.difficulty < 1)
staticDefenseAhead = false;
//if a rush tactics is denied by the player (e.g player builds static defenses around natural), then briefly suspend the attacking force (wait for rocket tanks to take out the static defenses)
if(frameAI < standardAttackTime && mainThread.ec.theMapAwarenessAI.canRushPlayer && distanceToTower < 2 && mainThread.ec.difficulty > 0){
if(Math.abs(attackPosition.x - myRallyPointX) > 12 || Math.abs(attackPosition.z - myRallyPointZ) > 12) {
for(int i = 0; i < troopsControlledByCombatAI.length; i++) {
if(troopsControlledByCombatAI[i] != null && troopsControlledByCombatAI[i].currentHP > 0 && troopsControlledByCombatAI[i].type != 1) {
troopsControlledByCombatAI[i].moveTo(myRallyPointX, myRallyPointZ);
troopsControlledByCombatAI[i].currentCommand = solidObject.move;
troopsControlledByCombatAI[i].secondaryCommand = solidObject.StandBy;
}
}
currentState = booming;
stateSwitchingCooldown = 8;
return;
}
}
staticDefenseNearAttackPosition = false;
for(int i = 0; i < mainThread.ec.theMapAwarenessAI.playerStaticDefenseLocations.length; i++) {
if(mainThread.ec.theMapAwarenessAI.playerStaticDefenseSize[i] > 0) {
if(mainThread.ec.theMapAwarenessAI.playerStaticDefenseStrength[i] > 6) {
float xPos = mainThread.ec.theMapAwarenessAI.playerStaticDefenseLocations[i].x;
float zPos = mainThread.ec.theMapAwarenessAI.playerStaticDefenseLocations[i].z;
float d = (xPos - attackPosition.x)*(xPos - attackPosition.x) + (zPos - attackPosition.z)*(zPos - attackPosition.z);
if(d < 16) {
staticDefenseNearAttackPosition = true;
break;
}
}
}
}
//send units to attack-move to target position
if(!playerHasBecomeStrongerThanAIDuringMarching && !frontalTroopIverwhelmed && (unNeutralizedEntity != null || distanceToTarget > 2)){
//if the tail portion of the troops are under attack, send them back to base instead of throwing them into player's trap
float AIForceStrengthOutsideCombatRadius = getAIForceStrength(unitOutsideCombatRadius);
maxPlayerForceStrengthRoundAttacker = 0;
for(int i = 0; i < numberOfUnitOutsideCombatRadius; i++){
if(unitOutsideCombatRadius[i].underAttackCountDown > 0 && unitOutsideCombatRadius[i].attacker != null){
//check the number of hostile units around the attacker
playerForceStrengthNearCombatCenter = checkPlayerForceStrengthAroundOnePoint(playerUnitInMinimap, unitOutsideCombatRadius[i].attacker.centre.x, unitOutsideCombatRadius[i].attacker.centre.z, 4);
if(playerForceStrengthNearCombatCenter > maxPlayerForceStrengthRoundAttacker)
maxPlayerForceStrengthRoundAttacker = playerForceStrengthNearCombatCenter;
}
//check if there are too many hostile units
if(maxPlayerForceStrengthRoundAttacker > AIForceStrengthOutsideCombatRadius){
if(withdrawUnitOutsideCombatRadiusCooldown == 0)
withdrawUnitOutsideCombatRadiusCooldown = 30;
break;
}
}
if(withdrawUnitOutsideCombatRadiusCooldown > 0){
team = unitInCombatRadius; //exclude the tail portion of the troops from the attack force
//retreat tail portion of the troops to rally point
//but if the player's force is near the rally point, send tail portion of the troops to defend the rally point
float x = mainThread.ec.theMapAwarenessAI.mainPlayerForceLocation.x;
float z = mainThread.ec.theMapAwarenessAI.mainPlayerForceLocation.z;
double d = Math.sqrt((x-myRallyPointX)*(x-myRallyPointX) + (z-myRallyPointZ)*(z-myRallyPointZ));
for(int i = 0; i < unitOutsideCombatRadius.length; i++){
if(unitOutsideCombatRadius[i] != null && unitOutsideCombatRadius[i].currentHP > 0){
if(d > 3.5) {
unitOutsideCombatRadius[i].attackMoveTo(mainThread.ec.theUnitProductionAI.rallyPoint.x, mainThread.ec.theUnitProductionAI.rallyPoint.z);
}else {
unitOutsideCombatRadius[i].attackMoveTo(x, z);
}
unitOutsideCombatRadius[i].currentCommand = solidObject.attackMove;
unitOutsideCombatRadius[i].secondaryCommand = solidObject.attackMove;
}
}
}
if(unNeutralizedEntity != null){
attackDirection.set(unNeutralizedEntity.centre.x - combatCenterX, 0, unNeutralizedEntity.centre.z - combatCenterZ);
distanceToTarget = attackDirection.getLength();
attackDirection.unit();
}
//make sure the attack position is a valid point on the map
if(attackPosition.x < 1.5)
attackPosition.x = 1.5f;
if(attackPosition.x > 30.5)
attackPosition.x = 30.5f;
if(attackPosition.y < 1.5)
attackPosition.y = 1.5f;
if(attackPosition.y > 30.5)
attackPosition.y = 30.5f;
everyoneAttackTargetPosition();
}else{
//if target position has been neutralized, change status to booming
currentState = booming;
float gatherPointX, gatherPointZ;
//if the change of state is caused by heavy causality of the AI player then move move every unit back to rally point
if(frontalTroopIverwhelmed || playerHasBecomeStrongerThanAIDuringMarching){
gatherPointX = mainThread.ec.theUnitProductionAI.rallyPoint.x;
gatherPointZ = mainThread.ec.theUnitProductionAI.rallyPoint.z;
}else {
//if the AI really couldn't find any targets then gather every units at the current combat center
gatherPointX = combatCenterX;
gatherPointZ = combatCenterZ;
}
for(int i = 0; i < numberOfUnitOutsideCombatRadius; i++){
//send the tail portion of the troops to rally point
unitOutsideCombatRadius[i].attackMoveTo(gatherPointX, gatherPointZ);
unitOutsideCombatRadius[i].currentCommand = solidObject.attackMove;
unitOutsideCombatRadius[i].secondaryCommand = solidObject.attackMove;
}
for(int i = 0; i < numberOfUnitInCombatRadius; i++){
//send the tail portion of the troops to rally point
if(unitInCombatRadius[i].attackStatus != solidObject.isAttacking){
unitInCombatRadius[i].attackMoveTo(gatherPointX, gatherPointZ);
unitInCombatRadius[i].currentCommand = solidObject.attackMove;
unitInCombatRadius[i].secondaryCommand = solidObject.attackMove;
}
}
}
}
}
//attack a target location with all the forces
public void everyoneAttackTargetPosition(){
//Find a gather location for an attack. The attacking units will first try to gather around a location that in between the target location and the attack force's center point.
//When the majority attacking units arrive around the gather point, the attack will proceed towards the target location.
//The purpose of doing this is to keep the attack units together when across long distance,
float teamRadius = (float)Math.sqrt(mainThread.ec.theUnitProductionAI.numberOfCombatUnit)/2.5f;
if(distanceToTarget < 3 + teamRadius && unNeutralizedEntity != null && !staticDefenseAhead){
//adjust the attack location for better engagement
playerForceCenter.set(0,0,0);
int numOfPlayerUnitsInMinimap = 0;
for(int i = 0; i < playerUnitInMinimap.length; i++){
if(playerUnitInMinimap[i] != null && playerUnitInMinimap[i].currentHP > 0){
float playerCenterX = playerUnitInMinimap[i].centre.x;
float playerCenterZ = playerUnitInMinimap[i].centre.z;
if((playerCenterX - combatCenterX)*(playerCenterX - combatCenterX) + (playerCenterZ - combatCenterZ)*(playerCenterZ - combatCenterZ) < 9){
playerForceCenter.x+=playerCenterX;
playerForceCenter.z+=playerCenterZ;
numOfPlayerUnitsInMinimap++;
}
}
}
if(numOfPlayerUnitsInMinimap >0){
playerForceCenter.x/=numOfPlayerUnitsInMinimap;
playerForceCenter.z/=numOfPlayerUnitsInMinimap;
}else{
playerForceCenter.x= attackPosition.x;
playerForceCenter.z= attackPosition.z;
}
adjustedAttackDirection.set(playerForceCenter.x - combatCenterX, 0, playerForceCenter.z - combatCenterZ);
adjustedAttackDirection.unit();
adjustedAttackDirection.scale(20);
for(int i = 0; i < mainThread.ec.theUnitProductionAI.numberOfCombatUnit; i++){
if(team[i] != null && team[i].currentHP > 0){
if(!((team[i].secondaryDestinationX == attackPosition.x && team[i].secondaryDestinationY == attackPosition.z)
|| (team[i].secondaryDestinationX == unNeutralizedEntity.centre.x && team[i].secondaryDestinationY == unNeutralizedEntity.centre.z))){
if(team[i].attackStatus != solidObject.isAttacking){
float x = team[i].centre.x;
float z = team[i].centre.z;
boolean farFromAttackPosition = Math.abs(x - attackPosition.x) > 2.5 || Math.abs(z - attackPosition.z) > 2.5;
if(!farFromAttackPosition)
team[i].attackMoveTo(playerForceCenter.x + adjustedAttackDirection.x, playerForceCenter.z + adjustedAttackDirection.z);
else
team[i].attackMoveTo(attackPosition.x, attackPosition.z);
team[i].currentCommand = solidObject.attackMove;
team[i].secondaryCommand = solidObject.attackMove;
}
team[i].secondaryDestinationX = attackPosition.x;
team[i].secondaryDestinationY = attackPosition.z;
}
}
}
}else{
for(int i = 0; i < 3; i++){
gatherPoint.set(combatCenterX + attackDirection.x*(teamRadius+1*i), 0, combatCenterZ + attackDirection.z*(teamRadius+ 1*i));
//if the gather point is inside a water body them move the gather point forward
int tileIndex = (int)(gatherPoint.x*64)/16 + (127 - ((int)(gatherPoint.z*64))/16)*128;
if(tileIndex >= mainThread.gridMap.tiles.length || tileIndex < 0)
break;
if(mainThread.gridMap.tiles[tileIndex][0] != null && mainThread.gridMap.tiles[tileIndex][0].type == 4)
continue;
else
break;
}
boolean playerForceIsMuchWeakerThanAI = checkIfAIHasBiggerForce(0.5f);
for(int i = 0; i < mainThread.ec.theUnitProductionAI.numberOfCombatUnit; i++){
if(team[i] != null && team[i].currentHP > 0){
if(mainThread.ec.difficulty > 0) {
//stop chasing player unit if it has got out of sight
if(team[i].targetObject != null && team[i].targetObject.currentHP >0) {
int targetPositionIndex = (int)(team[i].targetObject.centre.x*64)/16 + (127 - (int)(team[i].targetObject.centre.z*64)/16)*128;
if(team[i].attackStatus != solidObject.isAttacking && team[i].underAttackCountDown == 0 && (!mainThread.ec.visionMap[targetPositionIndex] || team[i].targetObject.isCloaked))
team[i].targetObject = null;
}
//marching forward
if(team[i].targetObject == null || team[i].targetObject.currentHP <=0){
if(staticDefenseAhead) {
if(team[i].type == 1) {
team[i].attackMoveTo(team[i].centre.x + attackDirection.x*teamRadius, team[i].centre.z + attackDirection.z*teamRadius);
}else {
team[i].attackMoveTo(combatCenterX, combatCenterZ);
}
}else if(!(team[i].currentMovementStatus == solidObject.hugRight || team[i].currentMovementStatus == solidObject.hugLeft)){
double d = Math.sqrt((team[i].centre.x - combatCenterX)*(team[i].centre.x - combatCenterX) + (team[i].centre.z - combatCenterZ)*(team[i].centre.z - combatCenterZ))*3;
if(d > teamRadius){
if(staticDefenseNearAttackPosition || !playerForceIsMuchWeakerThanAI || mainThread.ec.theMapAwarenessAI.playerAssetDestoryedCountDown == 0)
team[i].attackMoveTo(gatherPoint.x, gatherPoint.z);
else
team[i].attackMoveTo(attackPosition.x, attackPosition.z);
}else{
team[i].attackMoveTo(team[i].centre.x + attackDirection.x*teamRadius, team[i].centre.z + attackDirection.z*teamRadius);
}
}
team[i].currentCommand = solidObject.attackMove;
team[i].secondaryCommand = solidObject.attackMove;
}
}else {
team[i].attackMoveTo(attackPosition.x, attackPosition.z);
team[i].currentCommand = solidObject.attackMove;
team[i].secondaryCommand = solidObject.attackMove;
}
}
}
}
//make sure idle units are send to attack unNeutralized target
if(unNeutralizedEntity != null){
for(int i = 0; i < mainThread.ec.theUnitProductionAI.numberOfCombatUnit; i++){
if(team[i] != null && team[i].currentHP > 0 && !(team[i].type!= 1 && staticDefenseAhead)){
if(team[i].currentCommand == solidObject.StandBy || (team[i].targetObject == null && (team[i].secondaryDestinationX != unNeutralizedEntity.centre.x || team[i].secondaryDestinationY != unNeutralizedEntity.centre.z))){
float d = (team[i].centre.x - attackPosition.x)*(team[i].centre.x - attackPosition.x) + (team[i].centre.z - attackPosition.z)*(team[i].centre.z - attackPosition.z);
if(d < 9){
team[i].attackMoveTo(unNeutralizedEntity.centre.x, unNeutralizedEntity.centre.z);
team[i].currentCommand = solidObject.attackMove;
team[i].secondaryCommand = solidObject.attackMove;
}
}
}
}
}
}
public float checkPlayerForceStrengthAroundOnePoint(solidObject[] listOfUnits, float x, float z, double distanceThreshold){
float playerForceStrength = 0;
for(int j = 0; j < listOfUnits.length; j++){
if(listOfUnits[j] != null && listOfUnits[j].currentHP > 0){
double d = (listOfUnits[j].centre.x - x)*(listOfUnits[j].centre.x - x)
+ (listOfUnits[j].centre.z - z)*(listOfUnits[j].centre.z - z);
if(d < distanceThreshold){
if(listOfUnits[j].type == 0 || listOfUnits[j].type == 1)
playerForceStrength+=1;
else if(listOfUnits[j].type == 6)
playerForceStrength+=1.5f;
else if(listOfUnits[j].type == 7)
playerForceStrength+=3;
}
}
}
return playerForceStrength;
}
public float getAIForceStrength(solidObject[] listOfUnits){
float AIForceStrength = 0;
for(int j = 0; j < listOfUnits.length; j++){
if(listOfUnits[j] != null && listOfUnits[j].currentHP > 0){
if(listOfUnits[j].type == 0 || listOfUnits[j].type == 1)
AIForceStrength+=1;
else if(listOfUnits[j].type == 6)
AIForceStrength+=1.5f;
else if(listOfUnits[j].type == 7)
AIForceStrength+=3;
}
}
return AIForceStrength;
}
public boolean checkIfAIHasBiggerForce(float ratio){
int numberOfLightTanks_AI = mainThread.ec.theUnitProductionAI.numberOfLightTanksControlledByCombatAI;
int numberOfRocketTanks_AI = mainThread.ec.theUnitProductionAI.numberOfRocketTanksControlledByCombatAI;
int numberOfStealthTanks_AI = mainThread.ec.theUnitProductionAI.numberOfStealthTanksControlledByCombatAI;
int numberOfHeavyTanks_AI = mainThread.ec.theUnitProductionAI.numberOfHeavyTanksControlledByCombatAI;
int numberOfLightTanks_player = mainThread.ec.theMapAwarenessAI.numberOfLightTanks_player;
int numberOfRocketTanks_player = mainThread.ec.theMapAwarenessAI.numberOfRocketTanks_player;
int numberOfStealthTanks_player = mainThread.ec.theMapAwarenessAI.numberOfStealthTanks_player;
int numberOfHeavyTanks_player = mainThread.ec.theMapAwarenessAI.numberOfHeavyTanks_player;
float m3 = 1.5f;
if(techCenter.stealthTankResearched_enemy == true && numberOfHeavyTanks_player < 4 && numberOfStealthTanks_AI > numberOfStealthTanks_player * 2)
m3+=0.5f;
if(techCenter.stealthTankResearched_enemy == true && mainThread.ec.theMapAwarenessAI.playerArmyCanBeCounteredWithStealthTanks){
m3+=0.5f;
}
float m1 = 1;
if(mainThread.ec.theMapAwarenessAI.playerArmyCanBeCounteredWithLightTanks){
m1 = 1.2f;
if(techCenter.lightTankResearched_enemy == true){
m1=1.75f;
}
}
double enemyAIForceStrength = m1*numberOfLightTanks_AI + 0.75f*numberOfRocketTanks_AI + m3*(numberOfStealthTanks_AI-mainThread.ec.theBaseExpentionAI.numberOfStealthTankScout) + 3* numberOfHeavyTanks_AI;
double playerForceStrength = unrevealedPlayerForceStrength + numberOfLightTanks_player + 0.75f*numberOfRocketTanks_player + 1.5*numberOfStealthTanks_player + 3* numberOfHeavyTanks_player;
//System.out.println("unrevealedPlayerForceStrength" + unrevealedPlayerForceStrength + " " + "enemyAIForceStrength " + enemyAIForceStrength + " " + "playerForceStrength" + playerForceStrength);
return enemyAIForceStrength > 0 && playerForceStrength/enemyAIForceStrength < ratio;
}
}