-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathai_controller.c
More file actions
2830 lines (2606 loc) · 99.4 KB
/
ai_controller.c
File metadata and controls
2830 lines (2606 loc) · 99.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
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 "controller/ai_controller.h"
#include "controller/controller.h"
#include "formats/pilot.h"
#include "game/game_state.h"
#include "game/objects/har.h"
#include "game/objects/projectile.h"
#include "game/objects/scrap.h"
#include "game/scenes/arena.h"
#include "resources/af_loader.h"
#include "resources/ids.h"
#include "utils/allocator.h"
#include "utils/c_array_util.h"
#include "utils/log.h"
#include "utils/random.h"
#include "utils/vec.h"
#include <math.h>
/* times thrown before we AI learns its lesson */
#define MAX_TIMES_THROWN 3
/* times shot before we AI learns its lesson */
#define MAX_TIMES_SHOT 4
/* base likelihood to change movement action (lower is more likely) */
#define BASE_ACT_CHANCE 5
/* base timer before we can consider changing movement action */
#define BASE_ACT_TIMER 28
/* base likelihood to jump while moving forwards (lower is more likely) */
#define BASE_FWD_JUMP_CHANCE 5
/* base likelihood to jump while moving backwards (lower is more likely) */
#define BASE_BACK_JUMP_CHANCE 5
/* base likelihood to jump while standing still (lower is more likely) */
#define BASE_STILL_JUMP_CHANCE 40
/* number of move ticks before bailing on tactic */
#define TACTIC_MOVE_TIMER_MAX 30
/* number of attack attempt ticks before bailing on tactic */
#define TACTIC_ATTACK_TIMER_MAX 3
/* number of jump attack attempt ticks before bailing on tactic */
#define TACTIC_JUMP_ATTACK_TIMER_MAX 12
/* likelihood of attempting a random attack/tactic (lower is more likely) */
#define RANDOM_ATTACK_CHANCE 10
#define BACK (o->direction == OBJECT_FACE_RIGHT ? ACT_LEFT : ACT_RIGHT)
#define DOWNBACK (o->direction == OBJECT_FACE_RIGHT ? ACT_LEFT : ACT_RIGHT) | ACT_DOWN
#define UPBACK (o->direction == OBJECT_FACE_RIGHT ? ACT_LEFT : ACT_RIGHT) | ACT_UP
#define FORWARD (o->direction == OBJECT_FACE_RIGHT ? ACT_RIGHT : ACT_LEFT)
#define DOWNFORWARD (o->direction == OBJECT_FACE_RIGHT ? ACT_RIGHT : ACT_LEFT) | ACT_DOWN
#define UPFORWARD (o->direction == OBJECT_FACE_RIGHT ? ACT_RIGHT : ACT_LEFT) | ACT_UP
typedef struct {
int max_hit_dist;
int min_hit_dist;
int value;
int attempts;
int consecutive;
int last_dist;
} move_stat;
typedef struct {
int tactic_type;
int last_tactic;
int move_type;
int move_timer;
int attack_type;
int attack_id;
int attack_timer;
int attack_on;
int chain_hit_on;
int chain_hit_tactic;
} tactic_state;
typedef struct {
int difficulty;
int act_timer;
int cur_act;
int input_lag; // number of ticks to wait per input
int input_lag_timer;
// move stats
af_move *selected_move;
int last_move_id;
int move_str_pos;
move_stat move_stats[70];
int blocked;
int thrown; // times thrown by enemy
int shot; // times shot by enemy
// tactical state
tactic_state *tactic;
sd_pilot *pilot;
// all projectiles currently on screen (vector of projectile object*)
vector active_projectiles;
} ai;
enum
{
TACTIC_ESCAPE = 1, // escape from enemy
TACTIC_TURTLE, // block attacks
TACTIC_GRAB, // charge and grab enemy
TACTIC_SPAM, // spam the same attack
TACTIC_SHOOT, // shoot a projectile
TACTIC_TRIP, // trip enemy
TACTIC_QUICK, // quick attack
TACTIC_CLOSE, // close with the enemy
TACTIC_FLY, // fly towards the enemy
TACTIC_PUSH, // spam power moves to push them back
TACTIC_COUNTER // block then attack
};
enum
{
MOVE_CLOSE = 1, // close distance
MOVE_AVOID, // gain distance
MOVE_JUMP, // jump towards
MOVE_HIGH_JUMP, // high-jump towards
MOVE_BLOCK // hold block
};
enum
{
ATTACK_ID = 1, // attack by id
ATTACK_TRIP, // trip attack
ATTACK_GRAB, // grab/throw attack
ATTACK_LIGHT, // light/quick attack
ATTACK_HEAVY, // heavy/power attack
ATTACK_JUMP, // jumping attack
ATTACK_RANGED, // ranged attack
ATTACK_CHARGE, // charge attack
ATTACK_PUSH, // push attack
ATTACK_RANDOM, // random attack
};
enum
{
RANGE_CRAMPED = 0,
RANGE_CLOSE,
RANGE_MID,
RANGE_FAR
};
enum
{
MOVE_DIR_STILL,
MOVE_DIR_FWD,
MOVE_DIR_BACK
};
int char_to_act(str *ch, int direction, int *position) {
int action = 0;
switch(str_at(ch, *position)) {
case '8':
action = ACT_UP;
break;
case '2':
action = ACT_DOWN;
break;
case '6':
if(direction == OBJECT_FACE_LEFT) {
action = ACT_LEFT;
} else {
action = ACT_RIGHT;
}
break;
case '4':
if(direction == OBJECT_FACE_LEFT) {
action = ACT_RIGHT;
} else {
action = ACT_LEFT;
}
break;
case '7':
if(direction == OBJECT_FACE_LEFT) {
action = ACT_UP | ACT_RIGHT;
} else {
action = ACT_UP | ACT_LEFT;
}
break;
case '9':
if(direction == OBJECT_FACE_LEFT) {
action = ACT_UP | ACT_LEFT;
} else {
action = ACT_UP | ACT_RIGHT;
}
break;
case '1':
if(direction == OBJECT_FACE_LEFT) {
action = ACT_DOWN | ACT_RIGHT;
} else {
action = ACT_DOWN | ACT_LEFT;
}
break;
case '3':
if(direction == OBJECT_FACE_LEFT) {
action = ACT_DOWN | ACT_LEFT;
} else {
action = ACT_DOWN | ACT_RIGHT;
}
break;
case 'K':
action = ACT_KICK;
break;
case 'P':
action = ACT_PUNCH;
break;
case '5':
return ACT_STOP;
default:
break;
}
// it's possible there's a kick/punch, or both, chained on here, so check the next 2 characters
for(int i = 0; i < 2 && (*position) > 0; i++) {
// this is a lookahead, so use - 1 to look at the previous character in the string (move strings are in reverse
// order)
switch(str_at(ch, (*position) - 1)) {
case 'K':
log_debug("adding in extra kick to %d at string %s position %d", action, str_c(ch), *position - 1);
action |= ACT_KICK;
// decrement our string position!
(*position)--;
break;
case 'P':
log_debug("adding in extra punch to %d at string %s position %d", action, str_c(ch), *position - 1);
action |= ACT_PUNCH;
// decrement our string position!
(*position)--;
break;
default:
// don't keep going
return action;
}
}
return action;
}
/**
* \brief Chain an array of controller commands in sequence.
*
* \param ctrl Controller instance.
* \param commands An array of controller commands to chain.
* \param n_commands Number of elements in commands array.
* \param ev The current controller event.
*
* \return Void.
*/
void chain_controller_cmd(controller *ctrl, int commands[], size_t n_commands, ctrl_event **ev) {
for(size_t i = 0; i < n_commands; i++) {
controller_cmd(ctrl, commands[i], ev);
}
}
/**
* \brief Convenience method to roll '1 in x' chance.
*
* \param roll_x An integer indicating number of numbers in roll.
*
* \return A boolean indicating whether the roll passed.
*/
bool roll_chance(int roll_x) {
return roll_x <= 1 ? true : rand_int(roll_x) == 1;
}
/**
* \brief Roll chance for pilot preference.
*
* \param pref_val The value of the pilot preference (-100 to 100)
*
* \return A boolean indicating whether the preference is confirmed.
*/
bool roll_pref(int pref_val) {
int rand_roll = rand_int(200);
int pref_thresh = pref_val + 100;
return rand_roll <= pref_thresh;
}
/**
* \brief Determine whether the AI is smart enough to usually go ahead with an action.
*
* \param a The AI instance.
*
* \return A boolean indicating whether the AI is smart enough.
*/
bool smart_usually(const ai *a) {
if(a->difficulty >= 6) {
// at highest difficulty 92% chance to be smart
return !roll_chance(12);
} else if(a->difficulty >= 3) {
return roll_chance(7 - a->difficulty);
} else {
return false;
}
}
/**
* \brief Determine whether the AI is dumb enough to usually go ahead with an action.
*
* \param a The AI instance.
*
* \return A boolean indicating whether the AI is dumb enough.
*/
bool dumb_usually(const ai *a) {
if(a->difficulty == 1) {
// at lowest difficulty 92% chance to be dumb
return !roll_chance(12);
}
if(a->difficulty <= 2) {
return roll_chance(a->difficulty + 1);
} else {
return false;
}
}
/**
* \brief Determine whether the AI is smart enough to sometimes go ahead with an action.
*
* \param a The AI instance.
*
* \return A boolean indicating whether the AI is smart enough.
*/
bool smart_sometimes(const ai *a) {
if(a->difficulty >= 2) {
return roll_chance(10 - a->difficulty);
} else {
return false;
}
}
/**
* \brief Determine whether the AI is dumb enough to sometimes go ahead with an action.
*
* \param a The AI instance.
*
* \return A boolean indicating whether the AI is dumb enough.
*/
bool dumb_sometimes(const ai *a) {
if(a->difficulty <= 2) {
return roll_chance(a->difficulty + 2);
} else {
return false;
}
}
/**
* \brief Determine whether AI will proceed with an action using exponentially scaling roll.
*
* \param a The AI instance.
*
* \return A boolean indicating whether the AI should proceed with an action.
*/
bool diff_scale(const ai *a) {
int roll = rand_int(36);
return roll <= (a->difficulty * a->difficulty);
}
/**
* \brief Determine whether AI will learn from this moment.
*
* \param a The AI instance.
*
* \return A boolean indicating whether the AI should learn.
*/
bool learning_moment(const ai *a) {
float roll = (float)rand_int(diff_scale(a) ? 8 : 15);
return roll <= a->pilot->learning;
}
/**
* \brief Determine whether AI will forget something.
*
* \param a The AI instance.
*
* \return A boolean indicating whether the AI should forget.
*/
bool forgetful(const ai *a) {
float roll = (float)rand_int(diff_scale(a) ? 3 : 2);
return roll <= a->pilot->forget;
}
/**
* \brief Determine the current range classification.
*
* \param ctrl The AI controller instance.
*
* \return A boolean indicating range classification.
*/
int get_enemy_range(const controller *ctrl) {
object *o = game_state_find_object(ctrl->gs, ctrl->har_obj_id);
har *h = object_get_userdata(o);
object *o_enemy =
game_state_find_object(ctrl->gs, game_state_get_player(ctrl->gs, h->player_id == 1 ? 0 : 1)->har_obj_id);
int range_units = fixedpt_toint(fixedpt_abs(o_enemy->pos.fx - o->pos.fx)) / 30;
switch(range_units) {
case 0:
case 1:
return RANGE_CRAMPED;
case 2:
return RANGE_CLOSE;
case 3:
case 4:
return RANGE_MID;
default:
return RANGE_FAR;
}
}
bool enemy_is_stunned_or_stasis(const controller *ctrl) {
object *o = game_state_find_object(ctrl->gs, ctrl->har_obj_id);
har *h = object_get_userdata(o);
object *o_enemy =
game_state_find_object(ctrl->gs, game_state_get_player(ctrl->gs, h->player_id == 1 ? 0 : 1)->har_obj_id);
har *h_enemy = object_get_userdata(o_enemy);
return h_enemy->state == STATE_STUNNED || h_enemy->in_stasis_ticks;
}
/**
* \brief Convenience method to check whether the provided move is a special move.
*
* \param move The move instance.
*
* \return A boolean indicating whether the move is a special move.
*/
bool is_special_move(const af_move *move) {
if(str_equal_c(&move->move_string, "K") || str_equal_c(&move->move_string, "K1") ||
str_equal_c(&move->move_string, "K2") || str_equal_c(&move->move_string, "K3") ||
str_equal_c(&move->move_string, "K4") || str_equal_c(&move->move_string, "K6") ||
str_equal_c(&move->move_string, "P") || str_equal_c(&move->move_string, "P1") ||
str_equal_c(&move->move_string, "P2") || str_equal_c(&move->move_string, "P3") ||
str_equal_c(&move->move_string, "P4") || str_equal_c(&move->move_string, "P6")) {
return false;
}
return true;
}
/**
* \brief Convenience method to check whether a HAR has projectiles.
*
* \param har_id An integer identifying the HAR.
*
* \return Boolean indicating whether HAR supports projectiles.
*/
bool har_has_projectiles(int har_id) {
switch(har_id) {
case HAR_JAGUAR:
case HAR_SHADOW:
case HAR_ELECTRA:
case HAR_SHREDDER:
case HAR_CHRONOS:
case HAR_NOVA:
return true;
}
return false;
}
/**
* \brief Convenience method to check whether a HAR has a charge attack.
*
* \param har_id An integer identifying the HAR.
*
* \return Boolean indicating whether HAR has a charge attack.
*/
bool har_has_charge(int har_id) {
switch(har_id) {
case HAR_JAGUAR:
case HAR_SHADOW:
case HAR_KATANA:
case HAR_FLAIL:
case HAR_THORN:
case HAR_PYROS:
case HAR_ELECTRA:
case HAR_SHREDDER:
case HAR_CHRONOS:
case HAR_GARGOYLE:
return true;
}
return false;
}
/**
* \brief Convenience method to check whether a HAR has a push attack.
*
* \param har_id An integer identifying the HAR.
*
* \return Boolean indicating whether HAR has a push attack.
*/
bool har_has_push(int har_id) {
switch(har_id) {
case HAR_JAGUAR:
case HAR_KATANA:
case HAR_FLAIL:
case HAR_THORN:
case HAR_PYROS:
case HAR_ELECTRA:
case HAR_NOVA:
return true;
}
return false;
}
/**
* \brief Determine whether the AI would like to use the specified tactic.
*
* \param ctrl Controller instance.
* \param tactic_type An integer identifying the tactic.
*
* \return Boolean indicating whether the AI would like to use the tactic..
*/
bool likes_tactic(const controller *ctrl, int tactic_type) {
ai *a = ctrl->data;
object *o = game_state_find_object(ctrl->gs, ctrl->har_obj_id);
har *h = object_get_userdata(o);
sd_pilot *pilot = a->pilot;
if((a->tactic->last_tactic == tactic_type && roll_chance(2)) || h->state == STATE_JUMPING) {
return false;
}
bool enemy_close = h->close;
int enemy_range = get_enemy_range(ctrl);
bool wall_close = h->is_wallhugging;
switch(tactic_type) {
case TACTIC_SHOOT:
if(har_has_projectiles(h->id) && roll_pref(pilot->att_sniper) && enemy_range > RANGE_CRAMPED &&
(h->id != HAR_SHREDDER || ((enemy_range <= RANGE_MID && smart_usually(a)) ||
dumb_sometimes(a)) // shredder prefers to be close-mid range
)) {
return true;
}
break;
case TACTIC_CLOSE:
if(enemy_range > RANGE_CRAMPED && (har_has_charge(h->id) || roll_chance(4)) &&
roll_pref(pilot->att_hyper)) {
return true;
}
break;
case TACTIC_QUICK:
if(enemy_range > RANGE_CRAMPED && enemy_range < RANGE_FAR &&
((roll_pref(pilot->att_sniper) && roll_chance(3)) || (roll_pref(pilot->att_hyper) && roll_chance(6)) ||
(roll_pref(pilot->att_normal) && roll_chance(8)))) {
return true;
}
break;
case TACTIC_GRAB:
if((a->thrown <= MAX_TIMES_THROWN || roll_chance(2)) &&
((roll_pref(pilot->att_hyper) && roll_chance(3)) ||
((h->id == HAR_FLAIL || h->id == HAR_THORN) && roll_chance(3)))) {
return true;
}
break;
case TACTIC_TURTLE:
if(a->thrown <= MAX_TIMES_THROWN && ((roll_pref(pilot->att_def) && roll_chance(3)))) {
return true;
}
break;
case TACTIC_COUNTER:
if(a->thrown < MAX_TIMES_THROWN && roll_pref(pilot->att_def) && roll_chance(3)) {
return true;
}
break;
case TACTIC_ESCAPE:
if((roll_pref(pilot->att_jump) && roll_chance(3)) || (roll_pref(pilot->att_def) && roll_chance(5))) {
return true;
}
break;
case TACTIC_FLY:
if((roll_pref(a->pilot->att_jump) || (a->shot > MAX_TIMES_SHOT && learning_moment(a)) ||
(h->id == HAR_GARGOYLE || h->id == HAR_PYROS)) &&
((wall_close && roll_chance(2)) || roll_chance(4))) {
return true;
}
break;
case TACTIC_PUSH:
if((enemy_range <= RANGE_CLOSE ||
((h->id == HAR_THORN || h->id == HAR_KATANA) && enemy_range <= RANGE_MID)) &&
((har_has_push(h->id) && smart_usually(a)) &&
((roll_pref(pilot->att_hyper) && roll_chance(2)) || (roll_pref(pilot->att_def) && roll_chance(4)) ||
(wall_close && roll_chance(5))))) {
return true;
}
break;
case TACTIC_TRIP:
if(enemy_range <= RANGE_MID &&
((roll_pref(pilot->att_def) && roll_chance(4)) || (roll_pref(pilot->att_sniper) && roll_chance(6)))) {
return true;
}
break;
case TACTIC_SPAM:
if((enemy_close || dumb_usually(a)) && (wall_close || roll_chance(6)) && roll_pref(pilot->att_normal)) {
return true;
}
break;
}
return false;
}
/**
* \brief Queue the specified tactic in AI tactical state object.
*
* \param ctrl Controller instance.
* \param tactic_type An integer identifying the tactic.
*
* \return Void.
*/
void queue_tactic(controller *ctrl, int tactic_type) {
ai *a = ctrl->data;
object *o = game_state_find_object(ctrl->gs, ctrl->har_obj_id);
har *h = object_get_userdata(o);
a->tactic->last_tactic = a->tactic->tactic_type > 0 ? a->tactic->tactic_type : 0;
a->tactic->tactic_type = tactic_type;
// log when we queue a tactic
switch(tactic_type) {
case TACTIC_GRAB:
log_debug("HAR %d queued tactic: GRAB", h->id);
break;
case TACTIC_TRIP:
log_debug("HAR %d queued tactic: TRIP", h->id);
break;
case TACTIC_QUICK:
log_debug("HAR %d queued tactic: QUICK", h->id);
break;
case TACTIC_CLOSE:
log_debug("HAR %d queued tactic: CLOSE", h->id);
break;
case TACTIC_FLY:
log_debug("HAR %d queued tactic: FLY", h->id);
break;
case TACTIC_SHOOT:
log_debug("HAR %d queued tactic: SHOOT", h->id);
break;
case TACTIC_PUSH:
log_debug("HAR %d queued tactic: PUSH", h->id);
break;
case TACTIC_SPAM:
log_debug("HAR %d queued tactic: SPAM", h->id);
break;
case TACTIC_ESCAPE:
log_debug("HAR %d queued tactic: ESCAPE", h->id);
break;
case TACTIC_TURTLE:
log_debug("HAR %d queued tactic: TURTLE", h->id);
break;
case TACTIC_COUNTER:
log_debug("HAR %d queued tactic: COUNTER", h->id);
break;
}
bool enemy_close = h->close;
bool wall_close = h->is_wallhugging;
int enemy_range = get_enemy_range(ctrl);
bool do_charge = false;
// set move tactic
switch(tactic_type) {
// aggressive tactics
case TACTIC_GRAB:
case TACTIC_TRIP:
case TACTIC_QUICK:
case TACTIC_CLOSE:
if(enemy_close) {
a->tactic->move_type = 0;
} else if((tactic_type == TACTIC_CLOSE || (tactic_type == TACTIC_QUICK && roll_chance(3))) &&
smart_usually(a) && har_has_charge(h->id)) {
// smart AI will try to use charge attacks
a->tactic->move_type = 0;
do_charge = true;
} else if(smart_usually(a) && roll_pref(a->pilot->pref_jump)) {
// smart AI that likes to jump will close via jump
a->tactic->move_type = MOVE_JUMP;
} else {
a->tactic->move_type = MOVE_CLOSE;
}
break;
// jumping tactics
case TACTIC_FLY:
a->tactic->move_type = MOVE_HIGH_JUMP;
break;
// ranged tactics
case TACTIC_SHOOT:
a->tactic->move_type = (enemy_range == RANGE_CRAMPED && !wall_close) ? MOVE_AVOID : 0;
break;
// stalling tactics
case TACTIC_PUSH:
case TACTIC_SPAM:
a->tactic->move_type = 0;
break;
// evasive tactics
case TACTIC_ESCAPE:
a->tactic->move_type = wall_close ? MOVE_JUMP : MOVE_AVOID;
break;
// goading tactics
case TACTIC_TURTLE:
if(enemy_range == RANGE_CRAMPED) {
// at this range they might grab/throw so we need to use escape tactic
a->tactic->move_type = wall_close ? MOVE_JUMP : MOVE_AVOID;
} else {
a->tactic->move_type = MOVE_BLOCK;
}
break;
case TACTIC_COUNTER:
a->tactic->move_type = enemy_range > RANGE_CRAMPED ? MOVE_BLOCK : 0;
break;
}
// set tactic move timer
if(a->tactic->move_type > 0) {
a->tactic->move_timer = TACTIC_MOVE_TIMER_MAX;
}
if(do_charge) {
// set charge attack
a->tactic->attack_type = ATTACK_CHARGE;
a->tactic->attack_id = 0;
} else {
// set attack tactics
switch(tactic_type) {
// aggressive tactics
case TACTIC_GRAB:
a->tactic->attack_type = ATTACK_GRAB;
a->tactic->attack_id = 0;
break;
case TACTIC_TRIP:
a->tactic->attack_type = ATTACK_TRIP;
a->tactic->attack_id = 0;
// if we are jumping we wait for land to trip
if(a->tactic->move_type == MOVE_JUMP) {
a->tactic->attack_on = HAR_EVENT_LAND;
}
break;
case TACTIC_QUICK:
a->tactic->attack_type = ATTACK_LIGHT;
a->tactic->attack_id = 0;
break;
case TACTIC_FLY:
// smart AI will try for a jumping attack
a->tactic->attack_type = smart_usually(a) ? ATTACK_JUMP : 0;
a->tactic->attack_id = 0;
break;
case TACTIC_SHOOT:
a->tactic->attack_type = ATTACK_RANGED;
a->tactic->attack_id = 0;
break;
case TACTIC_PUSH:
if(har_has_push(h->id)) { // && smart_sometimes(a)
a->tactic->attack_type = ATTACK_PUSH;
} else {
a->tactic->attack_type = ATTACK_HEAVY;
}
a->tactic->attack_id = 0;
break;
case TACTIC_SPAM:
if(a->last_move_id > 0) {
a->tactic->attack_type = ATTACK_ID;
a->tactic->attack_id = a->last_move_id;
} else {
a->tactic->attack_type = ATTACK_LIGHT;
a->tactic->attack_id = 0;
}
break;
case TACTIC_COUNTER:
a->tactic->attack_type = roll_chance(3) ? ATTACK_TRIP : ATTACK_HEAVY;
// we only wait for block if they're not in range to grab/throw
if(enemy_range > RANGE_CRAMPED) {
a->tactic->attack_on = HAR_EVENT_BLOCK;
}
break;
case TACTIC_CLOSE:
a->tactic->attack_type = ATTACK_RANDOM;
a->tactic->attack_id = 0;
break;
case TACTIC_ESCAPE:
case TACTIC_TURTLE:
a->tactic->attack_type = 0;
a->tactic->attack_id = 0;
}
}
// set tactic attack timer
if(a->tactic->attack_type > 0) {
if(a->tactic->move_type == MOVE_JUMP || a->tactic->move_type == MOVE_HIGH_JUMP) {
a->tactic->attack_timer = TACTIC_JUMP_ATTACK_TIMER_MAX;
} else {
a->tactic->attack_timer = TACTIC_ATTACK_TIMER_MAX;
}
}
}
/**
* \brief Consider each of the provided tactics in order and queue one we like.
*
* \param ctrl Controller instance.
* \param tactics An array of tactics to consider.
*
* \return Void.
*/
void chain_consider_tactics(controller *ctrl, int tactics[], size_t n_tactics) {
for(size_t i = 0; i < n_tactics; i++) {
if(likes_tactic(ctrl, tactics[i])) {
queue_tactic(ctrl, tactics[i]);
return;
}
}
}
void reset_tactic_state(ai *a) {
a->tactic->last_tactic = a->tactic->tactic_type ? a->tactic->tactic_type : 0;
a->tactic->tactic_type = 0;
a->tactic->move_type = 0;
a->tactic->move_timer = 0;
a->tactic->attack_type = 0;
a->tactic->attack_id = 0;
a->tactic->attack_timer = 0;
a->tactic->attack_on = 0;
a->tactic->chain_hit_on = 0;
a->tactic->chain_hit_tactic = 0;
}
/**
* \brief Reset pilot personality. This is currently hard-coded.
*
* \param pilot The pilot details.
*
* \return Void.
*/
void reset_pilot_personality(sd_pilot *pilot) {
// log_debug("\033[90mReset pilot personality: %d", pilot->pilot_id);
switch(pilot->pilot_id) {
case 0:
// crystal
pilot->att_normal = 30;
pilot->att_hyper = 10;
pilot->att_jump = 10;
pilot->att_sniper = 20;
pilot->ap_throw = 100;
pilot->ap_special = 75;
pilot->ap_jump = -30;
pilot->ap_high = -50;
pilot->ap_low = -50;
pilot->ap_middle = -50;
pilot->pref_jump = -10;
pilot->pref_fwd = 30;
pilot->pref_back = 10;
pilot->learning = 1.5f;
pilot->forget = 0.25f;
break;
case 1:
// steffan
pilot->att_normal = 40;
pilot->att_hyper = 60;
pilot->att_jump = 30;
pilot->ap_throw = 25;
pilot->ap_special = 20;
pilot->ap_high = -75;
pilot->ap_low = 75;
pilot->ap_middle = 50;
pilot->pref_jump = 6;
pilot->pref_fwd = 20;
pilot->pref_back = -9;
pilot->learning = 1.0f;
pilot->forget = 0.4f;
break;
case 2:
// milano
pilot->att_normal = 20;
pilot->att_hyper = 30;
pilot->att_jump = 40;
pilot->att_sniper = 20;
pilot->ap_throw = -50;
pilot->ap_special = -50;
pilot->ap_jump = -50;
pilot->ap_high = 50;
pilot->ap_low = 50;
pilot->ap_middle = 50;
pilot->pref_jump = 8;
pilot->pref_fwd = 30;
pilot->pref_back = -3;
pilot->learning = 0.9f;
pilot->forget = 0.1f;
break;
case 3:
// christian
pilot->att_normal = 20;
pilot->att_hyper = 15;
pilot->att_def = 30;
pilot->att_sniper = 10;
pilot->ap_throw = 30;
pilot->ap_special = 25;
pilot->ap_jump = 30;
pilot->ap_low = -25;
pilot->ap_middle = 20;
pilot->pref_jump = 2;
pilot->pref_fwd = 10;
pilot->pref_back = -10;
pilot->learning = 2.5f;
pilot->forget = 0.35f;
break;
case 4:
// shirro
pilot->att_normal = 15;
pilot->att_hyper = 5;
pilot->att_jump = 5;
pilot->att_def = 20;
pilot->att_sniper = 4;
pilot->ap_throw = 75;
pilot->ap_special = 50;
pilot->ap_jump = -50;
pilot->ap_high = -50;
pilot->ap_low = -50;
pilot->ap_middle = -50;
pilot->pref_jump = -20;
pilot->pref_fwd = 10;
pilot->pref_back = 10;
pilot->learning = 2.0f;
pilot->forget = 0.2f;
break;
case 5:
// jean-paul
pilot->att_normal = 20;
pilot->att_hyper = 10;
pilot->att_jump = 20;
pilot->att_def = 30;
pilot->att_sniper = 45;
pilot->ap_throw = -50;
pilot->ap_special = 75;
pilot->ap_jump = 100;
pilot->ap_high = -50;
pilot->ap_low = 100;
pilot->ap_middle = -50;
pilot->pref_fwd = 20;
pilot->learning = 1.2f;
pilot->forget = 0.07f;
break;
case 6:
// ibrahim
pilot->att_normal = 40;
pilot->att_hyper = 5;
pilot->att_jump = 5;
pilot->att_def = 50;
pilot->att_sniper = 7;
pilot->ap_special = 50;
pilot->ap_jump = -50;
pilot->ap_high = 50;
pilot->ap_low = 50;
pilot->ap_middle = 50;
pilot->pref_jump = 2;
pilot->pref_fwd = 10;
pilot->pref_back = -10;
pilot->learning = 2.5f;
pilot->forget = 0.05f;
break;
case 7:
// angel
pilot->att_normal = 40;
pilot->att_hyper = 60;
pilot->att_jump = 30;
pilot->ap_throw = 25;
pilot->ap_special = 20;
pilot->ap_jump = 100;
pilot->ap_high = -75;
pilot->ap_low = 75;
pilot->ap_middle = 50;
pilot->pref_jump = 40;
pilot->pref_fwd = 40;
pilot->pref_back = -9;
pilot->learning = 3.0f;
pilot->forget = 0.15f;
break;
case 8:
// cossette
pilot->att_normal = 50;
pilot->att_hyper = 5;
pilot->att_jump = 5;
pilot->att_def = 5;
pilot->att_sniper = 5;
pilot->ap_throw = 25;
pilot->ap_special = -50;
pilot->ap_jump = -50;
pilot->ap_high = -25;
pilot->ap_low = 10;
pilot->ap_middle = -50;
pilot->pref_jump = -10;
pilot->pref_back = 10;
pilot->learning = 0.7f;
pilot->forget = 0.2f;
break;
case 9:
// raven
pilot->att_normal = 30;
pilot->att_hyper = 40;
pilot->ap_throw = 100;