Repository navigation
Expand file tree
/
Copy pathcadence.lua
More file actions
754 lines (739 loc) · 21.8 KB
/
Copy pathcadence.lua
File metadata and controls
754 lines (739 loc) · 21.8 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
-- Native-animation cadence: preserve native cycles and delay at safe poses.
local M = {}
local constants = require('constants')
local configuration = require('configuration')
local native_crews = constants.native_reload_crews
-- Raw unit types, independent of the base/fortification profile selection.
M.attack_states = {[6]=10, [22]=6, [23]=6, [39]=4, [40]=4, [41]=4,
[61]=4, [70]=6, [72]=6, [76]=6, [77]=4}
M.start_states = {[6]=10, [22]=4, [23]=4, [39]=8, [40]=8, [41]=8,
[61]=8, [70]=4, [72]=4, [74]=4, [76]=4, [77]=8}
local function native_profile(profile)
return profile.auto_targeting == false or (profile.interval and profile.sync_to_animation ~= false)
end
local function enabled(config, name)
if config == nil then return true end
return configuration.any_profile(config.units[name] or {}, native_profile)
end
function M.required(config)
for name in pairs(native_crews) do
if enabled(config, name) then return true end
end
return false
end
function M.resolve(locate, config)
local result = {}
for _, item in ipairs({
{6, '0F BE 80 ? ? ? ? 83 C4 08 3B C5 89 86 ? ? ? ? 7E 14', 3, 22},
{39, '80 BA ? ? ? ? 17 0F 85', 2, 23},
{40, '80 BA ? ? ? ? 1B 0F 85', 2, 27},
{41, '80 BA ? ? ? ? 16 75', 2, 22},
{61, '80 BA ? ? ? ? 0C 75 65', 2, 12},
{77, '0F BE 80 ? ? ? ? 3B C5 89 86 ? ? ? ? 7E 4A', 3, 13},
{22, '0F BE 82 ? ? ? ? 85 C0 89 86 ? ? ? ? 7E 6D 0F BF 96 ? ? ? ? 8D 84 C2 79 01 00 00', 3, 6},
{70, '0F BE 81 ? ? ? ? 83 CA FF 85 C0 89 86 ? ? ? ? 7E 4B', 3, 22},
{72, '0F BE 81 ? ? ? ? 85 C0 89 86 ? ? ? ? 7E 6D 0F BF 8E ? ? ? ? 8D 84 C1 C1 01 00 00', 3, 12},
{76, '0F BE 81 ? ? ? ? 85 C0 89 86 ? ? ? ? 7E 14 0F BF 96 ? ? ? ? 8D 84 C2 39 01 00 00', 3, 20},
{74, '0F BE 89 ? ? ? ? 89 8E ? ? ? ? 8B 8E ? ? ? ? 3B CB', 3, 27},
}) do
if enabled(config, constants.unit_names[item[1]]) then
local address = locate(item[2])
local script = core.readInteger(address + item[3])
assert(script >= 0x400000 and script < 0x4000000, 'invalid native release script')
local found
-- Script index zero is the phase's initial pose. The animation pass
-- advances before the first update: slingers release at index 1.
for index = 1, 39 do
if core.readByte(script + index) == item[4] then found = index; break end
end
assert(found and found > 0, 'unsupported native release script')
result[item[1]] = found
end
end
-- Crossbows compare the cycle itself, not the rendered frame; all three
-- elevation scripts share this gate. Resolve its immediate from the code.
if enabled(config, 'European crossbowman') then
local crossbow = locate('B9 ? ? ? ? 89 8E ? ? ? ? C7 86 ? ? ? ? 00 00 00 00 0F B7 86 ? ? ? ? 83 CA FF 66 85 C0 75 0F')
assert(core.readByte(crossbow + 0x78) == 0x39
and core.readByte(crossbow + 0x79) == 0x8E, 'unsupported crossbow release gate')
result[23] = core.readInteger(crossbow + 1)
assert(result[23] == 2, 'unsupported crossbow release cycle')
end
return result
end
function M.resolve_catapult_rest(locate, release_cycle)
if not release_cycle then return nil end
-- Final native reload entry: engine pose 13 (arm down), engineer pose 41.
-- The old release gate waited at firing pose 22, with the arm raised.
local site = locate('8B 1D ? ? ? ? 8B C3 69 C0 90 04 00 00 8B 88 ? ? ? ? 0F BE 89 ? ? ? ? 3B CD 89 88 ? ? ? ? 7F 17 5F 5E') + 8
local script = core.readInteger(site + 15)
assert(script >= 0x400000 and script < 0x4000000, 'invalid catapult reload script')
local finish
for index = 1, 39 do
if core.readByte(script + index) == 0 then finish = index; break end
end
assert(core.readByte(site + 0x4B) == 0x0F and core.readByte(site + 0x4C) == 0xBE
and core.readByte(site + 0x4D) == 0x8A, 'unsupported catapult engineer script')
local crew = core.readInteger(site + 0x4E)
assert(crew >= 0x400000 and crew < 0x4000000
and finish and finish > 1 and core.readByte(script + finish - 1) == 13
and core.readByte(crew + finish - 1) == 41 and core.readByte(crew + finish) == 0,
'unsupported catapult lowered pose')
local speed = site - 0x8C
local attack = speed + 14 + core.readInteger(speed + 10)
assert(attack >= 0x400000 and attack < 0x700000
and core.readByte(speed) == 0xB9 and core.readInteger(speed + 1) == 2
and core.readInteger(speed + 5) == 0x0FC13B66
and core.readByte(speed + 9) == 0x85
and core.readInteger(attack) == 0x00043D66
and core.readSmallInteger(attack + 10) % 65536 == 0x8E89
and core.readInteger(attack + 12) == core.readInteger(speed + 16),
'unsupported catapult firing speed')
return {cycle=finish-1, lead=release_cycle * (core.readInteger(speed + 1) + 1)}
end
function M.resolve_trebuchet_rest(locate, release_cycle)
if not release_cycle then return nil end
-- The trebuchet's firing phase already starts its swing. The loaded pose
-- is the LAST reload-script entry, before state 2 advances to state 4.
local site = locate('69 DB 90 04 00 00 8B 83 ? ? ? ? 0F BE 80 ? ? ? ? 3B C2 89 83 ? ? ? ? 7F 1A 5F 5E')
local script = core.readInteger(site + 15)
assert(script >= 0x400000 and script < 0x4000000, 'invalid trebuchet reload script')
local finish
for index = 1, 99 do
if core.readByte(script + index) == 0 then finish = index; break end
end
assert(core.readByte(site + 0x65) == 0x0F and core.readByte(site + 0x66) == 0xBE
and core.readByte(site + 0x67) == 0x88, 'unsupported trebuchet body script')
local body = core.readInteger(site + 0x68)
assert(body >= 0x400000 and body < 0x4000000, 'invalid trebuchet body script')
assert(finish and finish > 1 and core.readByte(body + finish - 1) == 23
and core.readByte(body + finish) == 0,
'unsupported trebuchet loaded pose')
-- ECX=2 is loaded before the state dispatch and written to the firing
-- phase's frame delay. Validate that path before deriving its lead time.
local speed = locate('B9 ? ? ? ? 66 3B C1 0F 85 ? ? ? ? C7 86 ? ? ? ? 05 00 00 00 89 96 ? ? ? ? 66 89 96 ? ? ? ?')
local attack = speed + 14 + core.readInteger(speed + 10)
assert(attack >= 0x400000 and attack < 0x700000
and core.readInteger(speed + 1) == 2
and core.readInteger(attack) == 0x00043D66
and core.readSmallInteger(attack + 10) % 65536 == 0x8E89
and core.readInteger(attack + 12) == core.readInteger(speed + 16),
'unsupported trebuchet firing speed')
return {cycle=finish-1, lead=release_cycle * (core.readInteger(speed + 1) + 1)}
end
-- Classify the already admitted native aim: 1 human order, 2 corrected siege
-- automatic aim, 0 legacy automatic selection. Share this decision between
-- validation, native dispatch and stagger continuation; keep coordinates/UIDs
-- with the native unit record rather than a second target cache.
M.context_code = [[
nativeContext:
push dword [esp+4]
call MANUALORDER
add esp, 4
test eax, eax
jnz nc_done
mov ecx, [esp+8]
cmp dword [NATIVECYCLET+ecx*4], 0
je nc_done
cmp dword [TURNBEFORET+ecx*4], 0
je nc_done
mov ecx, [esp+4]
imul ecx, ecx, 0x490
movzx ecx, word [ecx+UNITARRAY+0x8E]
cmp dword [NATIVESTARTT+ecx*4], 8
jne nc_done
mov eax, 2
nc_done:
ret
]]
-- The native target ID/UID pair survives a configured automatic aim. A lost
-- target must return to native idle/aiming before another volley; searching at
-- release could replace the point after the engine has already turned.
M.accepted_code = [[
acceptedTargetValid:
mov eax, [esp+4]
imul eax, eax, 0x490
add eax, UNITARRAY
movzx ecx, word [eax+0x344]
cmp ecx, 1
jl at_valid ; ground/wall point, no unit identity
cmp ecx, MAXUNITS
jae at_valid ; native no-unit sentinel
imul ecx, ecx, 0x490
add ecx, UNITARRAY
cmp word [ecx+0x8C], 2
jne at_invalid
cmp dword [ecx+0x3C8], 0
jle at_invalid
mov edx, [ecx+0x98]
cmp edx, [eax+0xA0]
jne at_invalid
at_valid:
mov eax, 1
ret
at_invalid:
xor eax, eax
ret
]]
-- Tick cached interval/crew eligibility is used by the later animation pass.
-- This prevents movement hysteresis from advancing twice in one simulation tick.
M.release_code = [[
nativeRelease:
pushad
mov ebp, esp
mov eax, [ebp+36]
mov edx, [ebp+40]
mov ecx, eax
imul ecx, ecx, 0x490
mov ebx, [NATIVECYCLET+edx*4]
cmp ebx, [ecx+UNITARRAY+0x2B0]
jne nr_done
cmp dword [ecx+UNITARRAY+0x50], 0
je nr_done
cmp dword [NATIVESEENT+eax*4], 0
jne nr_done
mov dword [NATIVESEENT+eax*4], 1
cmp dword [NATIVEBLOCKT+eax*4], 0
jne nr_refund
cmp dword [COOLDOWNT+eax*4], 0
jg nr_refund
cmp dword [PENDINGT+eax*4], 0
jg nr_refund
mov ecx, [NATIVEINTT+eax*4]
cmp ecx, 0
jle nr_refund
mov [S_ID], eax
mov [S_PROFILE], edx
mov [S_INTV], ecx
mov dword [S_ONESHOT], 0
push eax
call CHECKATTACHED
add esp, 4
push dword [S_PROFILE]
push dword [S_ID]
call NATIVECONTEXT
add esp, 8
test eax, eax
jz nr_search
mov eax, [ebp+44] ; native code already acquired and charged this shot
nr_search:
push eax ; zero retains automatic AI/search behavior
mov edx, [S_PROFILE]
call AUTOVOLLEY
add esp, 4
cmp dword [S_FIRED], 0
jne nr_done
nr_refund:
; The native catapult/trebuchet charged a stone before reaching dispatch.
; A target can disappear between the hold check and dispatch. Refund that
; charge if no configured shot was released. Mangonels have no such charge.
mov eax, [ebp+36]
imul eax, eax, 0x490
add eax, UNITARRAY
movzx ecx, word [eax+0x8E]
cmp ecx, 39
je nr_stone
cmp ecx, 40
jne nr_done
nr_stone:
inc word [eax+0x362]
nr_done:
popad
ret
]]
-- Enter the native aiming state from idle. Siege state 8 owns turning and
-- enters reload only after alignment; starting at reload (2) skips rotation.
-- Preserve movement/cow states already in progress.
M.idle_code = [[
nativeIdle:
pushad
mov eax, [S_ID]
imul eax, eax, 0x490
add eax, UNITARRAY
cmp word [eax+0x2C0], 2
jne ni_phase
cmp word [eax+0x3B0], 0
jne ni_done
push dword [S_PROFILE]
push dword [S_ID]
call NATIVECONTEXT
add esp, 8
cmp eax, 2
jne ni_done
push dword [S_ID]
call ACCEPTEDVALID
add esp, 4
test eax, eax
jnz ni_done
; The old target vanished during reload. No stone has been charged. Let
; native idle acquire a fresh target and pass through aiming state 8.
mov eax, [S_ID]
imul eax, eax, 0x490
add eax, UNITARRAY
mov word [eax+0x2C0], 0
mov dword [eax+0x2B0], 0
ni_phase:
cmp word [eax+0x8E], 74
je ni_horse
cmp word [eax+0x2C0], 0
jne ni_done
cmp word [eax+0x3B0], 0
jne ni_done
jmp ni_target
ni_horse:
cmp word [eax+0x424], 0
jne ni_done
movzx ecx, word [eax+0x2C0]
cmp ecx, 0
je ni_target
cmp ecx, 4
je ni_target
cmp ecx, 101
jne ni_done
ni_target:
mov ecx, [S_ID]
cmp dword [PENDINGT+ecx*4], 0
je ni_acquire
push ecx
call MANUALORDER
add esp, 4
test eax, eax
jnz ni_start ; queued native shots already own their aim
ni_acquire:
push dword [S_PROFILE]
push dword [S_ID]
call NATIVETARGET
add esp, 8
test eax, eax
jz ni_done
ni_start:
mov eax, [S_ID]
imul eax, eax, 0x490
add eax, UNITARRAY
cmp word [eax+0x8E], 74
jne ni_ordinary
mov word [eax+0x424], 4
cmp word [eax+0x2C0], 0
jne ni_done
ni_ordinary:
movzx ecx, word [eax+0x8E]
mov ecx, [NATIVESTARTT+ecx*4]
mov word [eax+0x2C0], cx
mov dword [eax+0x2B0], 0
ni_done:
popad
ret
]]
-- Failed target scans use the existing saved per-unit wait counter. Native
-- cadence does not use the legacy animation wait, so the two never overlap.
M.target_code = [[
nativeTarget:
pushad
mov ebp, esp
mov ebx, [ebp+36]
xor eax, eax
cmp dword [SYNCWAITT+ebx*4], 0
jg nt_done
push ebx
call MANUALORDER
add esp, 4
mov edi, eax ; snapshot before an automatic scan changes order
push dword [ebp+40]
push ebx
call PICKTARGET
add esp, 8
test eax, eax
jz nt_restore
mov ecx, [S_UNITPTR]
movzx ecx, word [ecx+0x8E]
cmp ecx, 6
je nt_hunter
cmp dword [NATIVESTARTT+ecx*4], 8
je nt_siege
cmp dword [NATIVESTARTT+ecx*4], 4
jne nt_restore
; Configured infantry targets must reach the native wind-up and LOS/UID
; checks. Otherwise a stale prior target can discard a valid loaded shot.
cmp eax, 2
je nt_ground
mov ecx, [S_UNITPTR]
mov word [ecx+0x39C], 3
jmp nt_done
nt_siege:
cmp eax, 2
jne nt_siegeunit
test edi, edi
jnz nt_done ; PICKTARGET already acquired this human order
jmp nt_ground ; newly selected automatic building/wall target
nt_siegeunit:
; A configured automatic unit target supplies the same tile fields used by
; the original siege aiming state. Retain the selected UID as for infantry.
; Human explicit orders use mode 2 and never take this path.
mov ecx, [S_UNITPTR]
movsx edx, word [ecx+0xBE]
sar edx, 3
mov word [ecx+0x3E8], dx
movsx edx, word [ecx+0xC0]
sar edx, 3
mov word [ecx+0x3EA], dx
mov word [ecx+0x39C], 3
jmp nt_done
nt_ground:
push ebx
mov ecx, UNITSTATE
call ACQUIRE
test eax, eax
jnz nt_done
jmp nt_restore
nt_hunter:
; Turn toward the temporary aim without overwriting the hunter's work order.
; The native tile-facing helper also respects the current camera rotation.
cmp eax, 2
jne nt_hunterface
push ebx
mov ecx, UNITSTATE
call ACQUIRE
test eax, eax
jz nt_restore
mov eax, 2
nt_hunterface:
push eax
mov ecx, [S_UNITPTR]
movsx edx, word [ecx+0xC0]
sar edx, 3
push edx
movsx edx, word [ecx+0xBE]
sar edx, 3
push edx
push ebx
mov ecx, UNITSTATE
call FACEPOINT
pop eax
nt_restore:
push eax
call RESTORETARGET
pop eax
test eax, eax
jnz nt_done
mov edx, [ebp+40]
mov ecx, RETRYTICKS
cmp dword [PRELOADT+edx*4], 0
je nt_cap
mov ecx, [PRELPOLLT+edx*4]
nt_cap:
cmp ecx, [NATIVEINTT+ebx*4]
jle nt_wait
mov ecx, [NATIVEINTT+ebx*4]
nt_wait:
mov [SYNCWAITT+ebx*4], ecx
nt_done:
mov [ebp+28], eax
popad
ret
]]
M.configured_hook = [[
configuredAnimationHold:
pushfd
pushad
mov ebx, [CURUNIT]
cmp ebx, 1
jl ca_pass
cmp ebx, MAXUNITS
jae ca_pass
if HASMANUALONLY = 0
cmp dword [NATIVEINTT+ebx*4], -1
je ca_pass
end if
push ebx
call PROFILE
add esp, 4
cmp eax, MAXPROFILES
jae ca_pass
imul esi, ebx, 0x490
add esi, UNITARRAY
cmp word [esi+0x8E], 74
je ca_pass ; bow routine owns a separate temporary clock
cmp dword [AUTOTARGETT+eax*4], 0
jne ca_scheduled
cmp dword [AIONLYT+eax*4], 0
je ca_manual
push eax
push ebx
call ISAIOWNED
add esp, 4
mov ecx, eax
pop eax
test ecx, ecx
jz ca_pass
ca_manual:
push eax
push ebx
call MANUALORDER
add esp, 4
mov ecx, eax
pop eax
test ecx, ecx
jnz ca_scheduled
; A retained native target can bypass acquisition during wind-up. Reuse
; the release/loaded-pose gate, without installing an interval or a timer.
push eax
push 1
push 0
movzx ecx, word [esi+0x8E]
push dword [RELEASECYCLET+ecx*4]
push ebx
call SHOULDHOLD
add esp, 16
mov ecx, eax
pop eax
test ecx, ecx
jnz ca_hold
ca_scheduled:
cmp dword [NATIVEINTT+ebx*4], -1
je ca_pass
; Turn human siege orders at native frame boundaries, including cooldown.
; This reads the existing order and calls the native direction owner. It
; never reacquires a shot, consumes RNG or replaces native target policy.
cmp dword [TURNBEFORET+eax*4], 0
je ca_cadence
cmp dword [PENDINGT+ebx*4], 0
jg ca_cadence
imul esi, ebx, 0x490
add esi, UNITARRAY
movzx ecx, word [esi+0x8E]
cmp dword [NATIVESTARTT+ecx*4], 8
jne ca_cadence
cmp word [esi+0x3B0], 0
jne ca_cadence
cmp word [esi+0x2C0], 2
je ca_turnclock
cmp word [esi+0x2C0], 4
jne ca_cadence
ca_turnclock:
mov ecx, [esi+0x40]
inc ecx
mov edx, [esi+0x3C]
add edx, [esi+0x44]
cmp ecx, edx
jle ca_cadence
push eax
push ebx
call MANUALORDER
add esp, 4
test eax, eax
pop eax
jz ca_cadence
movzx ecx, word [esi+0x39C]
cmp ecx, 22
je ca_cadence ; native cow phases already turn
push eax ; effective profile
cmp ecx, 4
jne ca_turnpoint
movzx edx, word [esi+0x39E]
cmp edx, 1
jl ca_turnskip
cmp edx, MAXUNITS
jae ca_turnskip
imul ecx, edx, 0x490
mov ecx, [ecx+UNITARRAY+0x98]
cmp ecx, [esi+0x3A0]
jne ca_turnskip ; do not follow a recycled native target slot
push edx
push ebx
mov ecx, UNITSTATE
call FACEUNIT
jmp ca_turned
ca_turnpoint:
movsx edx, word [esi+0x3EA]
movsx edi, word [esi+0x3E8]
cmp ecx, 9
jne ca_facexy
; Native siege state 8 aims at a building's centre, whereas the generic
; native building-facing function aims at its corner. Supply that centre
; to the existing point-facing API, retaining the native UID check.
movzx ecx, word [esi+0x336]
test ecx, ecx
jz ca_turnskip
cmp ecx, MAXBLD
jae ca_turnskip
imul ecx, ecx, 0x32C
add ecx, BLDBASE
mov eax, [ecx+0xD8]
cmp eax, [esi+0x3A0]
jne ca_turnskip
mov eax, [ecx+0xF8]
cdq
sub eax, edx
sar eax, 1
movsx edi, word [ecx+0xEE]
movsx edx, word [ecx+0xF0]
add edi, eax
add edx, eax
ca_facexy:
push edx
push edi
push ebx
mov ecx, UNITSTATE
call FACEPOINT
ca_turned:
test eax, eax
jz ca_turnskip
; Reuse the native animation clock for the six-tick aiming step. Holding
; the phase/cycle keeps the loaded pose and all progress through reloading.
mov edx, [esi+0x3C]
add edx, [esi+0x44]
sub edx, 5
mov [esi+0x40], edx
pop eax
jmp ca_hold
ca_turnskip:
pop eax
ca_cadence:
mov edi, eax ; retain the effective profile across gate calls
mov ecx, [NATIVECYCLET+eax*4]
test ecx, ecx
jz ca_pass
push dword [NATIVEBLOCKT+ebx*4]
push dword [COOLDOWNT+ebx*4]
push ecx
push ebx
call SHOULDHOLD
add esp, 16
test eax, eax
jnz ca_hold
; At a loaded/release transition, use a synthetic block to validate the
; selected target before native code starts the swing or consumes ammunition.
mov ecx, [NATIVECYCLET+edi*4]
push 1
push 0
push ecx
push dword [CURUNIT]
call SHOULDHOLD
add esp, 16
test eax, eax
jz ca_pass
cmp dword [PENDINGT+ebx*4], 0
jg ca_hold
push edi
push ebx
call NATIVECONTEXT
add esp, 8
cmp eax, 2
je ca_pass
; Automatic siege aim was admitted before native state 8 turned the body.
; Do not pick another target while loaded or at release: after Halt this
; would replace the old aim without another native aiming transition.
push edi
push dword [CURUNIT]
call NATIVETARGET
add esp, 8
test eax, eax
jz ca_hold
ca_pass:
popad
popfd
add dword [eax+esi+0x654], ebx
jmp RESUME
ca_hold:
popad
popfd
mov dword [eax+esi+0x664], 0
mov ecx, [CURUNIT]
jmp ANIMATIONDONE
]]
-- shouldHold(unitID, releaseCycle, remaining, blocked) -> bool
-- remaining is a shot-to-shot cooldown, decremented once per simulation tick.
-- blocked covers hold-fire/crew/target/queued-volley gating. Caller has resolved
-- a supported native attack profile. Catapults/trebuchets wait at the reload pose
-- and start the swing early enough to release on time. Retain the release gate
-- for late eligibility changes. Do not freeze movement, recoil or native cows.
M.hold_code = [[
shouldHold:
push ebx
mov eax, [esp+8]
cmp eax, 1
jl nh_no
cmp eax, MAXUNITS
jae nh_no
imul eax, eax, 0x490
add eax, UNITARRAY
cmp word [eax+0x3B0], 0
jne nh_no
movzx ecx, word [eax+0x8E]
cmp ecx, MAXTYPES
jae nh_no
xor ebx, ebx ; remaining ticks allowed before this transition
cmp ecx, 39
jne nh_trebuchet
cmp word [eax+0x2C0], 2
jne nh_attack
cmp dword [eax+0x2B0], CATRESTCYCLE
jne nh_no
mov ebx, CATRELEASELEAD
jmp nh_transition
nh_trebuchet:
cmp ecx, 40
jne nh_attack
cmp word [eax+0x2C0], 2
jne nh_attack
cmp dword [eax+0x2B0], TREBRESTCYCLE
jne nh_no
mov ebx, TREBRELEASELEAD
jmp nh_transition
nh_attack:
mov ecx, [NATIVEATTACKT+ecx*4]
cmp word [eax+0x2C0], cx
jne nh_no
mov ecx, [esp+12]
cmp ecx, 1
jl nh_no
dec ecx
cmp ecx, [eax+0x2B0]
jne nh_no
nh_transition:
mov ecx, [eax+0x40]
inc ecx
mov edx, [eax+0x3C]
add edx, [eax+0x44]
cmp ecx, edx
jle nh_no
cmp dword [esp+20], 0
jne nh_yes
cmp dword [esp+16], ebx
jle nh_no
nh_yes:
mov eax, 1
pop ebx
ret
nh_no:
xor eax, eax
pop ebx
ret
]]
-- At the original animation ticker increment: EAX=id*stride, ESI=UnitState,
-- EBX=1. The caller supplies already-resolved RELEASECYCLE and gating arrays.
M.animation_hook = [[
animationHold:
pushfd
pushad
mov eax, [CURUNIT]
push dword [BLOCKEDT+eax*4]
push dword [COOLDOWNT+eax*4]
push RELEASECYCLE
push eax
call SHOULDHOLD
add esp, 16
test eax, eax
jz ah_pass
popad
popfd
mov dword [eax+esi+0x664], 0
mov ecx, [CURUNIT]
jmp ANIMATIONDONE
ah_pass:
popad
popfd
add dword [eax+esi+0x654], ebx
jmp RESUME
]]
return M