-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmodule_scripts_form.py
More file actions
5986 lines (5638 loc) · 273 KB
/
module_scripts_form.py
File metadata and controls
5986 lines (5638 loc) · 273 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
# Formations AI for Mount&Blade by Motomataru
# rel. 12/26/10
# MV: Comcatenated original formAI_scripts_mb.py and formations_scripts_mb.py (search for "#Formations Scripts" to find where they start)
from header_common import *
from header_operations import *
from module_constants import *
from header_mission_templates import *
from header_items import *
from header_parties import *
from header_skills import *
from header_triggers import *
from header_terrain_types import *
from header_music import *
from ID_animations import *
from module_info import wb_compile_switch as is_a_wb_script
####################################################################################################################
# scripts is a list of script records.
# Each script record contns the following two fields:
# 1) Script id: The prefix "script_" will be inserted when referencing scripts.
# 2) Operation block: This must be a valid operation block. See header_operations.py for reference.
####################################################################################################################
formAI_scripts = [
# # AI with Formations Scripts
# script_calculate_decision_numbers by motomataru
# Input: AI team, size relative to battle in %
# Output: reg0 - battle presence plus level bump, reg1 - level bump (team avg level / 3)
("calculate_decision_numbers", [
(store_script_param, ":team_no", 1),
(store_script_param, ":battle_presence", 2),
(try_begin),
(call_script, "script_battlegroup_get_level", ":team_no", grc_everyone),
(store_div, reg1, reg0, 3),
(store_add, reg0, ":battle_presence", reg1), #decision w.r.t. all enemy teams
(try_end)
]),
# script_team_field_ranged_tactics by motomataru #Kham - Edited
# Input: AI team, size relative to largest team in %, size relative to battle in %
# Output: none
("team_field_ranged_tactics", [
(store_script_param, ":team_no", 1),
(store_script_param, ":rel_army_size", 2),
(store_script_param, ":battle_presence", 3),
(call_script, "script_battlegroup_get_size", ":team_no", grc_archers),
(set_fixed_point_multiplier, 100),
#Shit, Elves! Tactic - kham
(try_begin),
(gt, "$g_ally_party", 0),
(store_faction_of_party, ":ally_elf", "$g_ally_party"),
(try_end),
(try_begin),
(store_faction_of_party, ":enemy_elf", "$g_enemy_party"),
(store_faction_of_party, ":player_elf", "p_main_party"),
(team_get_leader, ":elf_leader", ":team_no"),
(gt, ":elf_leader", 0),
(agent_get_troop_id, ":agent_troop", ":elf_leader"),
(store_troop_faction, ":team_is_elf", ":agent_troop"),
(assign, ":shit_elves", 0),
(try_begin),
(gt, ":ally_elf", 0),
(is_between, ":ally_elf", "fac_lorien", "fac_dale"),
(assign, ":shit_elves", 1),
(else_try),
(is_between, ":player_elf", "fac_lorien","fac_dale"),
(assign, ":shit_elves", 1),
(else_try),
(is_between, ":enemy_elf", "fac_lorien","fac_dale"),
(assign, ":shit_elves", 1),
(try_end),
(try_end),
(try_begin),
(gt, reg0, 0),
(call_script, "script_battlegroup_get_position", Archers_Pos, ":team_no", grc_archers),
(call_script, "script_team_get_position_of_enemies", Enemy_Team_Pos, ":team_no"),
(call_script, "script_point_y_toward_position", Archers_Pos, Enemy_Team_Pos),
(call_script, "script_get_nearest_enemy_battlegroup_location", Nearest_Enemy_Battlegroup_Pos, ":team_no", Archers_Pos),
(assign, ":distance_to_enemy", reg0),
(call_script, "script_calculate_decision_numbers", ":team_no", ":battle_presence"),
(assign, ":decision_index", reg0),
(assign, ":level_bump", reg1),
(try_begin),
(gt, ":decision_index", 86), #outpower enemies more than 6:1?
(team_give_order, ":team_no", grc_archers, mordr_charge),
(else_try),
(ge, "$battle_phase", BP_Jockey),
(try_begin),
(eq, ":team_no", 0),
(assign, reg0, "$team0_archers_have_ammo"),
(else_try),
(eq, ":team_no", 1),
(assign, reg0, "$team1_archers_have_ammo"),
(else_try),
(eq, ":team_no", 2),
(assign, reg0, "$team2_archers_have_ammo"),
(else_try),
(eq, ":team_no", 3),
(assign, reg0, "$team3_archers_have_ammo"),
(try_end),
#Special orders for archers to support melee go here, JL
(this_or_next|eq, "$arc_charge_activated", 1), #are archers ordered to charge? JL
(eq, reg0, 0), #running out of ammo?
(team_give_order, ":team_no", grc_archers, mordr_charge),
#(display_message, "@Archers are charging, Archer AI disabled!"),##################
(else_try),
#(display_message, "@Archer form AI started"), #####################
(gt, "$cur_casualties", 0),
(eq, "$cur_casualties", "$prev_casualties"), #no new casualties since last function call?
(gt, ":decision_index", Advance_More_Point),
(le, ":distance_to_enemy", AI_long_range), #closer than reposition?
(team_give_order, ":team_no", grc_archers, mordr_advance),
#hold somewhere
(else_try),
(store_add, ":decision_index", ":rel_army_size", ":level_bump"), #decision w.r.t. largest enemy team
(assign, ":move_archers", 0),
(try_begin),
(eq, "$battle_phase", BP_Setup),
(assign, ":move_archers", 1),
(else_try),
(eq, "$battle_phase", BP_Fight), #changed ge to eq -JL
(try_begin),
(neg|is_between, ":distance_to_enemy", AI_charge_distance, AI_long_range),
(assign, ":move_archers", 1),
(else_try),
(lt, ":decision_index", Hold_Point), #probably coming from a defensive position (see below)
(gt, ":distance_to_enemy", AI_firing_distance),
(eq, "$FormAI_AI_no_defense", 0), #player hasn't set disallow defense option?
(this_or_next|is_between, ":team_is_elf", "fac_lorien", "fac_dale"),
(eq, ":shit_elves", 0), #There are no elven teams
(assign, ":move_archers", 1),
(try_end),
(else_try),
(this_or_next|eq, "$FormAI_AI_no_defense", 0), #player hasn't set disallow defense option OR?
(ge, ":decision_index", Hold_Point), #not starting in a defensive position (see below)
(this_or_next|is_between, ":team_is_elf", "fac_lorien", "fac_dale"),
(eq, ":shit_elves", 0), #There are no elven teams
(call_script, "script_battlegroup_get_size", ":team_no", grc_infantry),
(try_begin),
(this_or_next|eq, reg0, 0),
(gt, ":distance_to_enemy", AI_long_range),
(assign, ":move_archers", 1),
(else_try), #don't outstrip infantry when closing
(call_script, "script_battlegroup_get_position", Infantry_Pos, ":team_no", grc_infantry),
(get_distance_between_positions, ":infantry_to_enemy", Infantry_Pos, Nearest_Enemy_Battlegroup_Pos),
(val_sub, ":infantry_to_enemy", ":distance_to_enemy"),
(le, ":infantry_to_enemy", 1500),
(assign, ":move_archers", 1),
(try_end),
(try_end),
(try_begin),
(gt, ":move_archers", 0),
(try_begin),
(eq, ":team_no", 0),
(assign, ":team_start_pos", Team0_Starting_Point),
(else_try),
(eq, ":team_no", 1),
(assign, ":team_start_pos", Team1_Starting_Point),
(else_try),
(eq, ":team_no", 2),
(assign, ":team_start_pos", Team2_Starting_Point),
(else_try),
(eq, ":team_no", 3),
(assign, ":team_start_pos", Team3_Starting_Point),
(try_end),
(try_begin),
(lt, ":decision_index", Hold_Point), #outnumbered?
(eq, "$FormAI_AI_no_defense", 0), #player hasn't set disallow defense option?
(this_or_next|is_between, ":team_is_elf", "fac_lorien", "fac_dale"),
(eq, ":shit_elves", 0), #There are no elven teams
(lt, "$battle_phase", BP_Fight),
(store_div, ":distance_to_move", ":distance_to_enemy", 6), #middle of rear third of battlefield
(assign, ":hill_search_radius", ":distance_to_move"),
(else_try),
(assign, ":from_start_pos", 0),
(try_begin),
(eq, "$battle_phase", BP_Fight), #changed ge to eq -JL
(assign, ":from_start_pos", 1),
(else_try),
(gt, "$battle_phase", BP_Setup),
(call_script, "script_point_y_toward_position", ":team_start_pos", Archers_Pos),
(position_get_rotation_around_z, reg0, ":team_start_pos"),
(position_get_rotation_around_z, reg1, Archers_Pos),
(val_sub, reg0, reg1),
(neg|is_between, reg0, -45, 45),
(assign, ":from_start_pos", 1),
(try_end),
(try_begin),
(gt, ":from_start_pos", 0),
(copy_position, Archers_Pos, ":team_start_pos"),
(call_script, "script_point_y_toward_position", Archers_Pos, Enemy_Team_Pos),
(call_script, "script_get_nearest_enemy_battlegroup_location", Nearest_Enemy_Battlegroup_Pos, ":team_no", Archers_Pos),
(assign, ":distance_to_enemy", reg0),
(try_end),
(try_begin),
(eq, "$battle_phase", BP_Setup),
(assign, ":shot_distance", AI_long_range),
(else_try),
(assign, ":shot_distance", AI_firing_distance),
(try_begin),
(eq, ":team_no", 0),
(assign, reg0, "$team0_percent_ranged_throw"),
(else_try),
(eq, ":team_no", 1),
(assign, reg0, "$team1_percent_ranged_throw"),
(else_try),
(eq, ":team_no", 2),
(assign, reg0, "$team2_percent_ranged_throw"),
(else_try),
(eq, ":team_no", 3),
(assign, reg0, "$team3_percent_ranged_throw"),
(try_end),
(store_sub, reg1, AI_firing_distance, AI_charge_distance), #if default constants are used -> 4500
(val_add, reg1, 200), #add two meters to prevent automatically provoking melee from forward enemy infantry
(val_mul, reg1, reg0),
(val_div, reg1, 100),
(val_sub, ":shot_distance", reg1),
(try_end),
(store_sub, ":distance_to_move", ":distance_to_enemy", ":shot_distance"),
(store_div, ":hill_search_radius", ":shot_distance", 3), #limit so as not to run into enemy
(try_begin),
(lt, "$battle_phase", BP_Fight),
(try_begin),
(this_or_next|eq, "$battle_phase", BP_Setup),
(lt, ":battle_presence", Advance_More_Point), #expect to meet halfway?
(val_div, ":distance_to_move", 2),
(try_end),
(try_end),
(try_end),
(position_move_y, Archers_Pos, ":distance_to_move", 0),
(try_begin),
(lt, "$battle_phase", BP_Fight),
(copy_position, pos1, Archers_Pos),
(store_div, reg0, ":hill_search_radius", 100),
(call_script, "script_find_high_ground_around_pos1_corrected", Archers_Pos, reg0),
(try_end),
(try_end),
(team_give_order, ":team_no", grc_archers, mordr_hold),
(team_set_order_position, ":team_no", grc_archers, Archers_Pos),
(try_end),
(try_end)
]),
] + (is_a_wb_script==1 and [
# WB Only as it has some operations that are WB only - Kham
# script_team_field_melee_tactics by motomataru KHAM - Edited
# Input: AI team, size relative to largest team in %, size relative to battle in %
# Output: none
("team_field_melee_tactics", [
(store_script_param, ":team_no", 1),
# (store_script_param, ":rel_army_size", 2),
(store_script_param, ":battle_presence", 3),
(call_script, "script_calculate_decision_numbers", ":team_no", ":battle_presence"),
(set_fixed_point_multiplier, 100),
(store_mission_timer_a, ":timer"),
##JL code for checking disengagement by reinforcements:
(try_begin),
(eq, "$formai_disengage", 0), #if we have no disengagement ordered yet then
(store_normalized_team_count, ":num_attackers", 1), #added by JL to use for determining reinforcements (and possibly future other factors)
(store_normalized_team_count, ":num_defenders", 0), #added by JL to use for determining reinforcements (and possibly future other factors)
(try_begin), #for attackers:
(lt, ":num_attackers", 6), #if number of attackers are 5 or less
(eq, "$att_reinforcements_arrived", 0), #and attacker reinforcements have not arrived
(assign, "$att_reinforcements_needed", 1), #set attacker reinforcements are needed
(try_end),
(try_begin),
(ge, ":num_attackers", 6), #if number of attacker or defenders are more than 5
(eq, "$att_reinforcements_needed", 1), # and attacker reinforcements were needed
(assign, "$att_reinforcements_arrived", 1), #set attacker reinforcements to have arrived
(assign, "$att_reinforcements_needed", 0), #set attacker reinforcements are not needed
(assign, "$formai_disengage", 1), #set flag that disengagement is ok
#(display_message, "@Attacker reinforcements have arrived"), #############
(try_end),
(try_begin), #for defenders:
(lt, ":num_defenders",6), #if number of defenders are 5 or less
(eq, "$def_reinforcements_arrived", 0), #and defender reinforcements have not arrived
(assign, "$def_reinforcements_needed", 1), #set defender reinforcements are needed
(try_end),
(try_begin),
(ge, ":num_defenders", 6), #if number of defenders are more than 5
(eq, "$def_reinforcements_needed", 1), # and defender reinforcements were needed
(assign, "$def_reinforcements_arrived", 1), #set defender reinforcements to have arrived
(assign, "$def_reinforcements_needed", 0), #set defender reinforcements are not needed
(assign, "$formai_disengage", 1), #set flag that disengagement is ok
#(display_message, "@Defender reinforcements have arrived"), #############
(try_end),
(try_end),
##End JL code
#mop up if outnumber enemies more than 6:1
(try_begin),
(gt, reg0, 86),
(call_script, "script_formation_end", ":team_no", grc_infantry),
(team_give_order, ":team_no", grc_infantry, mordr_charge),
(call_script, "script_formation_end", ":team_no", grc_cavalry),
(team_give_order, ":team_no", grc_cavalry, mordr_charge),
(else_try),
#find closest distance of enemy to infantry, cavalry troops
(assign, ":inf_closest_dist", Far_Away),
(assign, ":inf_closest_non_cav_dist", Far_Away),
(assign, ":cav_closest_dist", Far_Away),
(assign, ":num_enemies_in_melee", 0),
(assign, ":num_enemies_supporting_melee", 0),
(assign, ":num_enemy_infantry", 0),
(assign, ":num_enemy_cavalry", 0),
(assign, ":num_enemy_others", 0),
(assign, ":num_enemy_elves", 0),
(assign, ":sum_level_enemy_infantry", 0),
(assign, ":x_enemy", 0),
(assign, ":y_enemy", 0),
(try_for_agents, ":enemy_agent"),
(agent_is_alive, ":enemy_agent"),
(agent_is_human, ":enemy_agent"),
(agent_is_active, ":enemy_agent"),
(agent_get_team, ":enemy_team_no", ":enemy_agent"),
(teams_are_enemies, ":enemy_team_no", ":team_no"),
# (agent_slot_eq, ":enemy_agent", slot_agent_is_running_away, 0),
(gt, ":enemy_agent", 0), #for some reason, -1 is being shown here...
(agent_get_troop_id, ":enemy_troop", ":enemy_agent"),
(troop_get_type, ":enemy_race", ":enemy_troop"),
(neq, ":enemy_race", tf_troll), #disregard trolls
(try_begin),
(is_between, ":enemy_race", tf_elf_begin, tf_elf_end),
(val_add, ":num_enemy_elves", 1),
(try_end),
(agent_get_class, ":enemy_class_no", ":enemy_agent"),
(try_begin),
(eq, ":enemy_class_no", grc_infantry),
(val_add, ":num_enemy_infantry", 1),
(agent_get_troop_id, ":enemy_troop", ":enemy_agent"),
(store_character_level, ":enemy_level", ":enemy_troop"),
(val_add, ":sum_level_enemy_infantry", ":enemy_level"),
(else_try),
(eq, ":enemy_class_no", grc_cavalry),
(val_add, ":num_enemy_cavalry", 1),
(else_try),
(val_add, ":num_enemy_others", 1),
(try_end),
(agent_get_position, pos0, ":enemy_agent"),
(position_get_x, ":value", pos0),
(val_add, ":x_enemy", ":value"),
(position_get_y, ":value", pos0),
(val_add, ":y_enemy", ":value"),
(assign, ":enemy_in_melee", 0),
(assign, ":enemy_supporting_melee", 0),
(try_for_agents, ":cur_agent"),
(agent_is_alive, ":cur_agent"),
(agent_is_human, ":cur_agent"),
(agent_is_active, ":cur_agent"),
(agent_get_team, ":cur_team_no", ":cur_agent"),
(eq, ":cur_team_no", ":team_no"),
#(agent_slot_eq, ":cur_agent", slot_agent_is_running_away, 0),
(agent_get_class, ":cur_class_no", ":cur_agent"),
(try_begin),
(eq, ":cur_class_no", grc_infantry), #this block will not be traversed if there is no Infantry ... JL
(agent_get_position, pos1, ":cur_agent"),
(get_distance_between_positions, ":distance_of_enemy", pos0, pos1),
(try_begin),
(gt, ":inf_closest_dist", ":distance_of_enemy"),
(assign, ":inf_closest_dist", ":distance_of_enemy"),
(copy_position, Nearest_Enemy_Troop_Pos, pos0),
(assign, ":enemy_nearest_troop_distance", ":distance_of_enemy"),
(assign, ":enemy_nearest_agent", ":enemy_agent"),
(try_end),
(try_begin),
(neq, ":enemy_class_no", grc_cavalry),
(gt, ":inf_closest_non_cav_dist", ":distance_of_enemy"),
(assign, ":inf_closest_non_cav_dist", ":distance_of_enemy"),
(copy_position, Nearest_Non_Cav_Enemy_Troop_Pos, pos0),
(assign, ":enemy_nearest_non_cav_troop_distance", ":distance_of_enemy"),
(assign, ":enemy_nearest_non_cav_agent", ":enemy_agent"),
(try_end),
(try_begin),
(lt, ":distance_of_enemy", 150),
(assign, ":enemy_in_melee", 1),
(try_end),
(try_begin),
(lt, ":distance_of_enemy", 350),
(assign, ":enemy_supporting_melee", 1),
(try_end),
(else_try),
(eq, ":cur_class_no", grc_cavalry),
(agent_get_position, pos1, ":cur_agent"),
(get_distance_between_positions, ":distance_of_enemy", pos0, pos1),
(try_begin),
(gt, ":cav_closest_dist", ":distance_of_enemy"),
(assign, ":cav_closest_dist", ":distance_of_enemy"),
(try_end),
(try_end),
(try_end),
(try_begin),
(eq, ":enemy_in_melee", 1),
(val_add, ":num_enemies_in_melee", 1),
(try_end),
(try_begin),
(eq, ":enemy_supporting_melee", 1),
(val_add, ":num_enemies_supporting_melee", 1),
(try_end),
(try_end),
(store_add, ":num_enemies", ":num_enemy_infantry", ":num_enemy_cavalry"),
(val_add, ":num_enemies", ":num_enemy_others"),
(gt, ":num_enemies", 0),
#JL get percentage of enemy others (archers, commpanions)
(assign, ":perc_enemy_others", ":num_enemy_others"), #assign enemy others percentage JL
(val_mul, ":perc_enemy_others", 100), #multiply by 100 to get percent JL
(val_div, ":perc_enemy_others", ":num_enemies"), #divide by total enemies to get ratio in percent JL
#Kham - Get % of Enemy Elves - Infantry charges if enemy elves > 20% of enemy composition
(store_mul, ":perc_enemy_elves", ":num_enemy_elves", 100),
(val_div, ":perc_enemy_elves", ":num_enemies"),
(init_position, Enemy_Team_Pos),
(val_div, ":x_enemy", ":num_enemies"),
(position_set_x, Enemy_Team_Pos, ":x_enemy"),
(val_div, ":y_enemy", ":num_enemies"),
(position_set_y, Enemy_Team_Pos, ":y_enemy"),
(position_set_z_to_ground_level, Enemy_Team_Pos),
(call_script, "script_battlegroup_get_size", ":team_no", grc_archers),
(assign, ":num_archers", reg0),
#(display_message, "@{reg0} archers"),
(try_begin),
(eq, ":num_archers", 0),
(assign, ":enemy_from_archers", Far_Away),
(assign, ":archer_order", mordr_charge),
(else_try),
(call_script, "script_battlegroup_get_position", Archers_Pos, ":team_no", grc_archers),
(call_script, "script_point_y_toward_position", Archers_Pos, Enemy_Team_Pos),
(call_script, "script_get_nearest_enemy_battlegroup_location", pos0, ":team_no", Archers_Pos),
(assign, ":enemy_from_archers", reg0),
(team_get_movement_order, ":archer_order", ":team_no", grc_archers),
(try_end),
(call_script, "script_battlegroup_get_size", ":team_no", grc_infantry),
(assign, ":num_infantry", reg0),
#(display_message, "@{reg0} infantry"),
(try_begin),
(eq, ":num_infantry", 0),
(assign, ":enemy_from_infantry", Far_Away),
(else_try),
(call_script, "script_battlegroup_get_position", Infantry_Pos, ":team_no", grc_infantry),
(call_script, "script_get_nearest_enemy_battlegroup_location", pos0, ":team_no", Infantry_Pos),
(assign, ":enemy_from_infantry", reg0),
(try_end),
(call_script, "script_battlegroup_get_size", ":team_no", grc_cavalry),
(assign, ":num_cavalry", reg0),
#(display_message, "@{reg0} cavalry"),
(try_begin),
(eq, ":num_cavalry", 0),
(assign, ":enemy_from_cavalry", Far_Away),
(else_try),
(call_script, "script_battlegroup_get_position", Cavalry_Pos, ":team_no", grc_cavalry),
(call_script, "script_get_nearest_enemy_battlegroup_location", pos0, ":team_no", Cavalry_Pos),
(assign, ":enemy_from_cavalry", reg0),
(try_end),
(try_begin),
(lt, "$battle_phase", BP_Fight),
(this_or_next|le, ":enemy_from_infantry", AI_charge_distance),
(this_or_next|le, ":enemy_from_cavalry", AI_charge_distance),
(le, ":enemy_from_archers", AI_charge_distance),
(assign, "$battle_phase", BP_Fight),
(else_try),
(lt, "$battle_phase", BP_Jockey),
(this_or_next|le, ":inf_closest_dist", AI_long_range),
(le, ":cav_closest_dist", AI_long_range),
(assign, "$battle_phase", BP_Jockey),
(try_end),
#(team_get_leader, ":team_leader", ":team_no"),
(call_script, "script_team_get_nontroll_leader", ":team_no"),
(assign, ":team_leader", reg0),
(gt, ":team_leader", 0),
#infantry AI
#(assign, ":place_leader_by_infantry", 0), #Kham - removed all these
# JL DISENGAGEMENT OF ARCHERS:
(try_begin),
(gt, ":num_archers", 0), #if there are any archers
(eq, "$arc_charge_activated", 1), #and archers are in charge mode
(ge, ":enemy_from_archers", AI_charge_distance), #and enemies are not immediately near
(assign, "$arc_charge_activated", 0), #then deactivate inf charge mode
#(display_message, "@Archers ordered to resume shooting."), #######################
(try_end),
#JL ADVANCE Archers closer to infantry if they are too far away:
(try_begin),
(gt, ":num_archers", 0), #if there are any archers
(gt, ":num_infantry", 0), #and if there is infantry
(get_distance_between_positions, ":archer_dist_to_inf", Infantry_Pos, Archers_Pos),
(gt, ":archer_dist_to_inf", 4000), #allied inf are not within 40m
(gt, ":enemy_from_archers", 4000), #and enemies are not within 40m
(ge, ":enemy_from_archers", ":enemy_from_infantry"), #and enemies are closer or equally close to infantry than they are to archers
(team_give_order, ":team_no", grc_archers, mordr_charge), #charge the archers forward
#(display_message, "@Archers ordered to charge to keep close with infantry."), #######################
(try_end),
(assign, ":charge_against_elves", 0),
(try_begin),
(gt, ":perc_enemy_elves", 20), #if there are more than 20% enemy elves
(ge, ":timer", 90), #and it has been a 1.5 mins
(assign, ":charge_against_elves", 1), #activate infantry charge against elves
(try_end),
(try_begin),
(le, ":num_infantry", 0),
(assign, ":infantry_order", ":archer_order"),
(else_try),
(try_begin), ## JL CHARGE ACTIVATION to force inf Charge Mode when inf AI has been deemed to need charge mode (--for use if inf charge has been ordered earlier in the code or from archers code)
(eq, "$inf_charge_activated", 1), #if inf AI has been told to Charge
(eq, "$inf_charge_ongoing", 0), #and switch is false
(call_script, "script_formation_end", ":team_no", grc_infantry), #end any formations
(team_give_order, ":team_no", grc_infantry, mordr_charge), #do a Native Charge
(assign, "$inf_charge_ongoing", 1), #switch set to true so this function is not needed to be called again until charge mode has been disabled and a new charge has been set
#(display_message, "@Infantry orders are Charge, Formations Inf AI disabled"), ################
(try_end),
(try_begin), ## JL RULES OF DISENGAGEMENT: to Remove AI Infantry Charge Mode when infantry is not in melee any more
(eq, ":num_enemies_supporting_melee", 0), #infantry is currently not in melee
(gt, ":enemy_from_infantry", 1500), #JL Kham - and nearest enemy is further away than 20m - Changed to 15m
(eq, "$inf_charge_ongoing", 1), # flag to see if we have a regular charge ongoing
(assign, "$inf_charge_activated", 0), #then deactivate inf charge mode
(assign, "$inf_charge_ongoing", 0), #and reset switch to open up for future charge orders
#(display_message, "@Infantry AI is not in melee and decides to resume maneuver"), #######################
(try_end),
#JL Control to Continue form AI
(eq, "$inf_charge_activated", 0), #If infantry charge has not been activated then continue Inf AI, JL
(store_mul, ":percent_level_enemy_infantry", ":sum_level_enemy_infantry", 100),
(val_div, ":percent_level_enemy_infantry", ":num_enemies"),
(try_begin),
(teams_are_enemies, ":team_no", "$fplayer_team_no"),
(assign, ":combined_level", 0),
(assign, ":combined_team_size", 0),
(assign, ":combined_num_infantry", ":num_infantry"),
(else_try),
(call_script, "script_battlegroup_get_level", "$fplayer_team_no", grc_infantry),
(assign, ":combined_level", reg0),
(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_everyone),
(assign, ":combined_team_size", reg0),
(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_infantry),
(store_add, ":combined_num_infantry", ":num_infantry", reg0),
(try_end),
(store_mul, ":percent_level_infantry", ":combined_num_infantry", 100),
(call_script, "script_battlegroup_get_level", ":team_no", grc_infantry),
(assign, ":level_infantry", reg0),
(val_add, ":combined_level", reg0),
(val_mul, ":percent_level_infantry", ":combined_level"),
(call_script, "script_battlegroup_get_size", ":team_no", grc_everyone),
(val_add, ":combined_team_size", reg0),
(val_div, ":percent_level_infantry", ":combined_team_size"),
(assign, ":infantry_order", mordr_charge),
(try_begin), #enemy far away AND ranged not charging
(gt, ":enemy_from_archers", AI_charge_distance),
(gt, ":inf_closest_dist", AI_charge_distance),
#(neq, ":archer_order", mordr_charge), #InVain: disabled this, doesn't make sense and makes factions without archers (Dunland) charge from the start
(try_begin), #fighting not started OR not enough infantry
(this_or_next|le, "$battle_phase", BP_Jockey),
(lt, ":percent_level_infantry", ":percent_level_enemy_infantry"),
(assign, ":infantry_order", mordr_hold),
(try_end),
(try_end),
#if low level troops outnumber enemies in melee by 2:1, attempt to whelm
(try_begin),
(le, ":level_infantry", 12),
(gt, ":num_enemies_in_melee", 0),
(store_mul, reg0, ":num_enemies_supporting_melee", 2),
(is_between, reg0, 1, ":num_infantry"),
(get_distance_between_positions, reg0, Infantry_Pos, Nearest_Enemy_Troop_Pos),
(le, reg0, AI_charge_distance),
(call_script, "script_formation_end", ":team_no", grc_infantry),
(team_give_order, ":team_no", grc_infantry, mordr_charge),
#(display_message, "@Infantry decided to whelm."), #############
(else_try), #Kham - Make infantry charge when enemy is close
(get_distance_between_positions, reg0, Infantry_Pos, Nearest_Enemy_Troop_Pos),
(this_or_next|le, ":enemy_from_infantry", 1300), # Start charge if enemy infantry formation is close
(this_or_next|le, ":enemy_from_archers", 1300), # Start charge if enemy archer group is close.
(le, reg0, 900), # Start charge if any enemies are really close.
(call_script, "script_formation_end", ":team_no", grc_infantry),
(team_get_movement_order, reg0, ":team_no", grc_infantry),
(team_give_order, ":team_no", grc_infantry, mordr_hold_fire), # Hold fire while charging to keep infantry together.
(try_begin),
(neq, reg0, mordr_charge),
(team_give_order, ":team_no", grc_infantry, mordr_charge),
(try_end), #Kham - changes end
#(display_message, "@DEBUG: Infantry decides to charge!"),
#else attempt to form formation somewhere
(else_try),
(try_begin),
(eq, ":team_no", 0),
(try_begin),
(eq, "$team0_default_formation", formation_default),
(call_script, "script_get_default_formation", 0),
(assign, "$team0_default_formation", reg0),
(try_end),
(assign, ":infantry_formation", "$team0_default_formation"),
(else_try),
(eq, ":team_no", 1),
(try_begin),
(eq, "$team1_default_formation", formation_default),
(call_script, "script_get_default_formation", 1),
(assign, "$team1_default_formation", reg0),
(try_end),
(assign, ":infantry_formation", "$team1_default_formation"),
(else_try),
(eq, ":team_no", 2),
(try_begin),
(eq, "$team2_default_formation", formation_default),
(call_script, "script_get_default_formation", 2),
(assign, "$team2_default_formation", reg0),
(try_end),
(assign, ":infantry_formation", "$team2_default_formation"),
(else_try),
(eq, ":team_no", 3),
(try_begin),
(eq, "$team3_default_formation", formation_default),
(call_script, "script_get_default_formation", 3),
(assign, "$team3_default_formation", reg0),
(try_end),
(assign, ":infantry_formation", "$team3_default_formation"),
(try_end),
(call_script, "script_classify_agent", ":enemy_nearest_agent"),
(assign, ":enemy_nearest_troop_division", reg0),
(agent_get_class, ":enemy_nearest_troop_class", ":enemy_nearest_agent"),
(agent_get_team, ":enemy_nearest_troop_team", ":enemy_nearest_agent"),
#(team_get_leader, ":enemy_leader", ":enemy_nearest_troop_team"),
(call_script, "script_team_get_nontroll_leader", ":enemy_nearest_troop_team"),
(assign, ":enemy_leader", reg0),
(gt, ":enemy_leader", 0),
(store_mul, ":percent_enemy_cavalry", ":num_enemy_cavalry", 100),
(val_div, ":percent_enemy_cavalry", ":num_enemies"),
(try_begin),
(neq, ":infantry_formation", formation_none),
(try_begin),
(gt, ":percent_enemy_cavalry", 66),
(assign, ":infantry_formation", formation_square),
#(display_message, "@Infantry decided to form square"), #############
(else_try),
(neq, ":enemy_nearest_troop_class", grc_cavalry),
(neq, ":enemy_nearest_troop_class", grc_archers),
(neq, ":enemy_nearest_agent", ":enemy_leader"),
(call_script, "script_battlegroup_get_size", ":enemy_nearest_troop_team", ":enemy_nearest_troop_division"),
(gt, reg0, ":num_infantry"), #got fewer troops?
(call_script, "script_battlegroup_get_level", ":team_no", grc_infantry),
(assign, ":average_level", reg0),
(call_script, "script_battlegroup_get_level", ":enemy_nearest_troop_team", ":enemy_nearest_troop_division"),
(gt, ":average_level", reg0), #got better troops?
(assign, ":infantry_formation", formation_shield),
#(display_message, "@Infantry decided to form shield"), #############
(try_end),
(try_end),
#hold near archers?
(try_begin),
(eq, ":infantry_order", mordr_hold),
(gt, ":num_archers", 0),
(copy_position, pos1, Archers_Pos),
(position_move_x, pos1, "$formai_rand8", 0), #changed -100 to rand8 (-100 to 100) JL
(try_begin),
(this_or_next|eq, ":enemy_nearest_troop_division", grc_cavalry),
(gt, ":percent_level_infantry", ":percent_level_enemy_infantry"),
(position_move_y, pos1, "$formai_rand2", 0), #move ahead of archers in anticipation of charges -- changed 1000 to rand2 JL
(else_try),
(position_move_y, pos1, "$formai_rand5", 0), #changed -1000 to rand5 JL
(try_end),
(assign, ":spacing", 1),
#(display_message, "@Infantry decided to hold near archers"), #############
#advance to nearest (preferably unmounted) enemy
(else_try),
#(display_message, "@Infantry decides to advance"), #############
(try_begin),
(eq, ":num_enemies_in_melee", 0), #not engaged?
(gt, ":enemy_from_archers", "$formai_rand4"), #changed AI_charge_distance to rand4 (10-22m) JL
(lt, ":percent_enemy_cavalry", 100),
(assign, ":distance_to_enemy_troop", ":enemy_nearest_non_cav_troop_distance"),
(copy_position, pos60, Nearest_Non_Cav_Enemy_Troop_Pos),
(agent_get_team, ":enemy_non_cav_team", ":enemy_nearest_non_cav_agent"),
#(team_get_leader, reg0, ":enemy_non_cav_team"),
(call_script, "script_team_get_nontroll_leader", ":enemy_non_cav_team"),
(try_begin),
(eq, reg0, -1),
(assign, ":distance_to_enemy_group", Far_Away),
(else_try),
(call_script, "script_classify_agent", ":enemy_nearest_non_cav_agent"),
(call_script, "script_battlegroup_get_position", pos0, ":enemy_non_cav_team", reg0),
(get_distance_between_positions, ":distance_to_enemy_group", Infantry_Pos, pos0),
(try_end),
(else_try),
(assign, ":distance_to_enemy_troop", ":enemy_nearest_troop_distance"),
(copy_position, pos60, Nearest_Enemy_Troop_Pos),
(try_begin),
(eq, ":enemy_nearest_agent", ":enemy_leader"),
(assign, ":distance_to_enemy_group", Far_Away),
(else_try),
(call_script, "script_battlegroup_get_position", pos0, ":enemy_nearest_troop_team", ":enemy_nearest_troop_division"),
(get_distance_between_positions, ":distance_to_enemy_group", Infantry_Pos, pos0),
(try_end),
(try_end),
(try_begin), #attack troop if its unit is far off
(gt, ":distance_to_enemy_group", "$formai_rand4"),
(this_or_next|neq, ":enemy_nearest_agent", ":enemy_leader"), #added by JL to ignore leader
(neq,"$cur_casualties","$prev_casualties"), # added by JL to have the AI not ignore the leader (5 secs) if there have been any recent losses
(copy_position, pos0, pos60),
(assign, ":distance_to_move", ":distance_to_enemy_troop"),
#(display_message, "@Infantry decides to MOVE to attack enemy troop"), #############
(else_try), #attack unit
(this_or_next|neq, ":enemy_nearest_agent", ":enemy_leader"), #added by JL to ignore leader
(neq,"$cur_casualties","$prev_casualties"), # added by JL to have the AI not ignore the leader (5 secs) if there have been any recent losses
(assign, ":distance_to_move", ":distance_to_enemy_group"),
#(display_message, "@Infantry decides to attack enemy troop"), #############
#wedge pushes through to last enemy infantry rank
(try_begin),
(eq, ":infantry_formation", formation_wedge),
(val_add, ":distance_to_move", formation_minimum_spacing),
# #non-wedge stops before first rank of enemy
# (else_try),
# (store_mul, reg0, formation_minimum_spacing, 2),
# (val_sub, ":distance_to_move", reg0),
#(display_message, "@Infantry decided to move to attack in wedge formation"), #############
(try_end),
(try_end),
(copy_position, pos1, Infantry_Pos),
(call_script, "script_point_y_toward_position", pos1, pos0),
(try_begin),
(le, ":distance_to_move", 1600),
(position_move_y, pos1, ":distance_to_move"),
(else_try),
(position_move_y, pos1, 1500),
(try_end),
(call_script, "script_get_centering_amount", ":infantry_formation", ":num_infantry", 0),
(position_move_x, pos1, reg0),
(store_div, reg0, formation_minimum_spacing, 2),
(position_move_x, pos1, reg0), #combat tendency of fighting formations to rotate left
(assign, ":spacing", 0),
(try_end),
(copy_position, pos61, pos1), #copy for possible leader positioning
(position_copy_rotation, pos61, pos1),
(try_begin),
(neq, ":infantry_formation", formation_none),
(ge, ":num_infantry", formation_min_foot_troops),
(call_script, "script_set_formation_position", ":team_no", grc_infantry, pos1),
(call_script, "script_form_infantry", ":team_no", ":team_leader", ":spacing", ":infantry_formation"),
(team_give_order, ":team_no", grc_infantry, mordr_hold),
#(assign, ":place_leader_by_infantry", 1), #Kham
(else_try),
(call_script, "script_formation_end", ":team_no", grc_infantry),
(team_give_order, ":team_no", grc_infantry, ":infantry_order"),
(team_set_order_position, ":team_no", grc_infantry, pos61),
(team_get_gap_distance, ":gap_distance", ":team_no", grc_infantry), #InVain: Make factions without formation stand closer
(gt, ":gap_distance", 2),
(team_give_order, ":team_no", grc_infantry, mordr_stand_closer),
#(eq, ":infantry_order", mordr_hold), #Kham
#(assign, ":place_leader_by_infantry", 1), #Kham
(try_end),
(try_end),
#GENERAL RULES OF ENGAGEMENT for Infantry, by JL:
#If Engaged in melee and order is charge, then full Charge Infantry (without affecting cavalry) and maybe affecting archers
(try_begin),
(gt, ":num_enemies_supporting_melee", 0), #If melee is occurring
(try_begin), #then if
(this_or_next|eq, ":infantry_order", mordr_charge), #infantry order is to charge
(eq, ":archer_order", mordr_charge), #or archer orders are to charge
(assign, "$inf_charge_activated",1), #then Charge infantry
#(display_message,"@Infantry ordered to charge because infantry is in melee."), ##############
(gt, ":num_archers",0),
(eq, "$arc_charge_activated", 0),
(call_script, "script_calculate_decision_numbers", ":team_no", ":battle_presence"), #call to see the odds JL
(le, reg0, 56), #AI is not overwhelmingly strong, so they need to throw in extra support in close combat
(le, ":enemy_from_archers", "$formai_rand3"), #and enemy is 20-30 meters from archers
(team_give_order, ":team_no", grc_archers, mordr_charge), #do a Native Charge
(assign, "$arc_charge_activated", 1), #then Charge archers
#(display_message,"@Archers ordered to charge to support melee!"), ##############
(try_end),
(try_end),
#If Infantry is Holding and Nearest Enemy is within Self Defence range, enemy is not leader or troops have fallen then Charge (charge infantry and cavalry).
(try_begin),
(eq, ":infantry_order", mordr_hold), #if Orders are Hold
(le, ":enemy_nearest_troop_distance", "$formai_rand4"), #and any enemy is within self defence distance
(this_or_next|neq, ":enemy_nearest_agent", ":enemy_leader"), #nearest target is not the player
(neq, "$cur_casualties","$prev_casualties2"), #troops have fallen during last second
(assign, "$inf_charge_activated", 1), #then Charge Infantry
#(assign, "$charge_activated", 1), #and Charge Cavalry
#(display_message,"@Infantry ordered general charge because enemy is too near and casualties inflicted."), ##############
(try_end),
#JL If enemy constitutes more than 40% then charge towards archers
(try_begin),
(this_or_next|eq, "$formai_rand7", 35), #JL if the odds have been lowered to 35 then there are lots of archers and infantry needs to close in with them quickly
(eq, ":charge_against_elves", 1), #and Charge against elves is activated
(call_script, "script_formation_end", ":team_no", grc_infantry), #JL Kham added
(team_give_order, ":team_no", grc_infantry, mordr_charge), #charge the infantry forward towards cavalry and enemy
(try_end),
##JL ACTIVATION of Inf Charge Mode when Inf AI has been deemed to need charge mode. Put here to activate charge immediately (not wait until next trigger).
(try_begin),
(eq, "$inf_charge_activated", 1), #if inf AI has been told to Charge
(eq, "$inf_charge_ongoing", 0), #and switch is false
(call_script, "script_formation_end", ":team_no", grc_infantry), #end any formations
(team_give_order, ":team_no", grc_infantry, mordr_charge), #do a Native Charge
(assign, "$inf_charge_ongoing", 1), #switch set to true so this function is not needed to be called again until charge mode has been disabled and a new charge has been set
#(display_message, "@Infantry orders are Charge, Formations Inf AI disabled"), ################
(try_end),
(try_end), #end of Infantry AI
#cavalry AI
(call_script, "script_battlegroup_get_size", ":team_no", grc_cavalry),
(assign, ":num_cavalry", reg0),
(call_script, "script_calculate_decision_numbers", ":team_no", ":battle_presence"), #added by JL to control grand charge and move around threat charge
(assign, ":grand_charge", reg0), #added by JL
(val_sub, ":grand_charge", 5), #added JL 6/14/2011 Grand charge lowered so that it will prefer to do flank attacks with cav.
(assign, ":around_charge", reg0), #added by JL
#JL: don't place leader with infantry if there are more than 1 cavalry on the map:
#(try_begin),
#(gt, ":num_cavalry", 1),
#(assign, ":place_leader_by_infantry", 0), #JL, added on 2/20/2011 Kham - removed
#(try_end),
#JL Patrol Mode Control
(store_random_in_range, ":chance", 1, 101), #chance value that is used to assign cavalry patrols and extra aggressivness while on patrol. JL
(try_begin),
(ge, ":chance", 97), #There is 3% chance that patrol mode will activate each second
(neq, "$formai_patrol_mode", 1), #and if it is not already active
(assign, "$formai_patrol_mode", 1), #assign patrol mode to 1
(try_end), #End of Patrol Mode Control
#(val_sub, ":around_charge", 5), #if needed use to increase the odds needed to execute the move around threat and charge routine. JL
##JL ACTIVATION OF Cavalry CHARGE:
(try_begin), ## Added by JL to force charge when charge is active and cavalry exists
(gt, ":num_cavalry", 0),
(eq, "$charge_activated", 1), #if Cav AI has been told to Charge
(eq, "$charge_ongoing", 0), #switch check
(call_script, "script_formation_end", ":team_no", grc_cavalry),
(team_give_order, ":team_no", grc_cavalry, mordr_charge),
(assign, "$charge_ongoing", 1), #setting switch so this function is not called again until charge mode is turned off
# (display_message, "@Cavarly Orders are now in Charge Mode; Formations Cav AI is disabled"),
(try_end),
#cavalry AI
(try_begin),
(gt, ":num_cavalry", 0),
(call_script, "script_battlegroup_get_size", ":team_no", grc_everyone), # added by JL
(assign, ":perc_cav", ":num_cavalry"), #JL
(val_mul, ":perc_cav", 100), #get percent JL
(val_div, ":perc_cav", reg0), #cavalry/everyone*100 JL : used for when executing certain tactics is allowed
#JL CHECK IF Infantry should move forward in charge movement to support an already charging Cavalry:
(try_begin),
(eq, "$charge_ongoing", 1), #if Cav AI is in Charge mode
(this_or_next|gt, ":enemy_from_archers", "$formai_rand3"), #and if enemy is at least 20-30 meters away from archers
(lt, ":perc_cav", 60), #or if there are less than 60% cavalry on the field
(get_distance_between_positions, ":inf_dist_to_cav", Infantry_Pos, Cavalry_Pos), #get the distance between infantry and cavalry
(this_or_next|le, ":enemy_from_infantry", "$formai_rand3"), #if enemy is less than or equal to 20-30m from infantry
(le, ":inf_dist_to_cav", 8000), #or allied cav is within 80m JL to make infantry support cavalry better (was 40m)
(le, ":enemy_from_cavalry", ":enemy_from_infantry"), #and enemies are closer or equally close to cavalry than they are to infantry
(call_script, "script_formation_end", ":team_no", grc_infantry), #Kham
(team_give_order, ":team_no", grc_infantry, mordr_charge), #charge the infantry forward towards cavalry and enemy
(try_end), #End JL check
#get distance to nearest enemy battlegroup(s)
(call_script, "script_battlegroup_get_level", ":team_no", grc_cavalry),
(assign, ":average_level", reg0),
(assign, ":nearest_threat_distance", Far_Away),
(assign, ":nearest_target_distance", Far_Away),
(assign, ":num_targets", 0),
(assign, ":num_threats", 0), # added by JL
(try_for_range, ":enemy_team_no", 0, 4),
(call_script, "script_battlegroup_get_size", ":enemy_team_no", grc_everyone),
(gt, reg0, 0),
(teams_are_enemies, ":enemy_team_no", ":team_no"),
(try_begin),
(eq, ":enemy_team_no", "$fplayer_team_no"),
(assign, ":num_groups", 9),
(else_try),
(assign, ":num_groups", 3),
(try_end),
(try_for_range, ":enemy_battle_group", 0, ":num_groups"),
(call_script, "script_battlegroup_get_size", ":enemy_team_no", ":enemy_battle_group"),
(assign, ":size_enemy_battle_group", reg0),
(gt, ":size_enemy_battle_group", 0),
(call_script, "script_battlegroup_get_position", pos0, ":enemy_team_no", ":enemy_battle_group"),
(get_distance_between_positions, ":distance_of_enemy", Cavalry_Pos, pos0),
(try_begin), #threat or target?
(call_script, "script_battlegroup_get_weapon_length", ":enemy_team_no", ":enemy_battle_group"),
(assign, ":decision_index", reg0),
(call_script, "script_battlegroup_get_level", ":enemy_team_no", ":enemy_battle_group"),
(val_mul, ":decision_index", reg0),
(val_mul, ":decision_index", ":size_enemy_battle_group"),
(val_div, ":decision_index", ":average_level"),
(val_div, ":decision_index", ":num_cavalry"),
(try_begin),
(neq, ":enemy_battle_group", grc_cavalry),
(val_div, ":decision_index", 2), #double count cavalry vs. foot soldiers
(try_end),
(gt, ":decision_index", 100),
(try_begin),
(val_add, ":num_threats", 1), #JL
(gt, ":nearest_threat_distance", ":distance_of_enemy"),
(copy_position, Nearest_Threat_Pos, pos0),
(assign, ":nearest_threat_distance", ":distance_of_enemy"),
(try_end),
(else_try),
(val_add, ":num_targets", 1),
(gt, ":nearest_target_distance", ":distance_of_enemy"),
(copy_position, Nearest_Target_Pos, pos0),
(assign, ":nearest_target_distance", ":distance_of_enemy"),
(try_end),
(try_end),
(try_end),
(try_begin), #added by JL to eliminate {0,0} position bug when no target is detected
(eq, ":num_targets", 0),
(copy_position, Nearest_Target_Pos, Nearest_Threat_Pos),
(assign, ":nearest_target_distance", ":nearest_threat_distance"),
(try_end),
(try_begin), #added by JL to eliminate a {0,0} Position bug when no threat is detected
(eq, ":num_threats", 0),
(copy_position, Nearest_Threat_Pos, Nearest_Target_Pos),
(assign, ":nearest_threat_distance", ":nearest_target_distance"),
(try_end),
(try_begin), #JL Kham - added to eliminate a {0,0} Position bug when no threat and no target is detected (e.g. the player has only companions or troops in other divisions)
(eq, ":num_threats", 0),
(eq, ":num_targets", 0),
(gt, ":num_cavalry", 0),
(assign, "$charge_activated", 1),
(call_script, "script_formation_end", ":team_no", grc_cavalry),
(team_give_order, ":team_no", grc_cavalry, mordr_charge),
(assign, ":nearest_target_distance", AI_charge_distance),
(assign, ":nearest_threat_distance", AI_charge_distance),
(try_end),
##JL KHAM TEST -- Distances are no good in some cases (especially before casualties have occurred ...):
#(assign,reg10, ":cav_closest_dist"), (assign,reg11, ":nearest_target_distance"),(display_message, "@Enemy cavalry: {reg10} Target dist is: {reg11}"), ##########
(get_distance_between_positions, reg0, Nearest_Target_Pos, Nearest_Threat_Pos),
(store_div, reg1, AI_charge_distance, 2),
(try_begin), #ignore target too close to threat
(le, reg0, reg1),
(gt, reg0, 0), #JL added to fix when there is no target and vice versa Kham
(assign, ":nearest_target_guarded", 1),
(else_try),
(assign, ":nearest_target_guarded", 0),
(try_end),
(assign, ":cavalry_order", mordr_charge),