-
Notifications
You must be signed in to change notification settings - Fork 0
/
w2p.cpp
5980 lines (5660 loc) · 186 KB
/
w2p.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <windows.h>
#include <stdint.h>
#include "patch.h"//stole that file from Fois plugins sources because why not
#include "defs.h"//some addresses and constants
#include "names.h"//some constants
byte a_custom = 0;//custom game
byte reg[4] = { 0 };//region
int* unit[1610];//array for units
int* unitt[1610];//temp array for units
int* capt[1610];//array for captured units
int units = 0;//number of units in array
int captk = 0;//number of units in captured array
byte ua[255] = { 255 };//attackers array
byte ut[255] = { 255 };//targets array only unit ua[i] can deal damage to unit ut[i]
byte runes[9] = { 0 };//runestone special abilities flags
bool first_step = false;//first step of trigger
bool table = false;//show win/lose table
bool agr = false;//allied comps aggro if attacked
bool cpt = false;//can buildings be captured on low hp
bool pcpt = false;//only peons can capture low hp build
bool thcpt = false;//if th captured on low hp, capture all units of that player
bool ucpt = false;//units capture
bool steal = false;//if can steal resources
bool aport = false;//only allies can teleport in portal
bool mport = false;//portal can teleport only if have mage(or dk) nearby
bool b3rune = false;//can build runestone
bool b3port = false;//can build portal
bool b3cirl = false;//can build circle of power
bool b3mine = false;//can build gold mine
bool b3forest = false;//can build forest
bool apn = false;//can build attack peons
bool manaburn = false;//if demons have manaburn
byte heros[16] = { 0 };//heroes ids
bool herosb[16] = { false };//heroes buildable ids
char hhero[180] = { 0 };//buttons for build human heroes array
char ohero[180] = { 0 };//buttons for build orc heroes array
bool A_runestone = false;//runestone activated
bool A_portal = false;//portals activated
bool A_transport = false;//transport activated
bool A_autoheal = false;//paladins autoheal activated
bool blood_f = false;//blood fixed
bool more_res = false;//can get more resources
bool path_fixed = false;//path fix
bool ai_fixed = false;//ai fix
bool peon_steal = false;//peons can steal from other peons
bool saveload_fixed = false;//saveload ai break bug fix
byte m_slow_aura[255] = { 255 };//units that have slow aura
byte m_death_aura[255] = { 255 };//units that have death aura
byte m_sneak[255] = { 255 };//units that have hide ability
byte m_devotion[255] = { 255 };//units that have defence aura
byte m_vampire[255] = { 255 };//units that have vampire aura
byte m_prvnt[255] = { 255 };//units that have prevent loss
struct Vizs
{
byte x = 0;
byte y = 0;
byte p = 0;
byte s = 0;
};
Vizs vizs_areas[2000];
int vizs_n = 0;
char sheep_build[] = "\x0\x0\x73\x0\xf0\x40\x44\x0\xd0\xe6\x40\x0\x1\x39\x28\x1\x0\x0\x0\x0";
char demon_build[] = "\x0\x0\x25\x0\xf0\x40\x44\x0\xd0\xe6\x40\x0\x1\x38\x18\x1\x0\x0\x0\x0";
char build_3[] = "\x0\x0\x52\x0\xf0\x40\x44\x0\x80\x6b\x43\x0\x0\x66\x31\x1\x0\x0\x0\x0\x1\x0\x50\x0\xf0\x40\x44\x0\x80\x6b\x43\x0\x0\x65\x3e\x1\x0\x0\x0\x0\x2\x0\x51\x0\xf0\x40\x44\x0\x80\x6b\x43\x0\x0\x64\x46\x1\x0\x0\x0\x0\x3\x0\x4A\x0\xf0\x40\x44\x0\x80\x6b\x43\x0\x0\x5c\x38\x1\x0\x0\x0\x0\x4\x0\x67\x0\xf0\x40\x44\x0\x80\x6b\x43\x0\x0\x5c\x91\x1\x0\x0\x0\x0\x8\x0\x5b\x0\xf0\x40\x44\x0\x70\xa6\x44\x0\x0\x6e\x97\x1\x0\x0\x0\x0";
char peon_build3[] = "\x0\x0\x54\x0\xf0\x40\x44\x0\x90\x66\x43\x0\x0\x0\x88\x1\x2\x0\x0\x0\x1\x0\xa7\x0\xf0\x40\x44\x0\xf0\x61\x43\x0\x0\x0\x89\x1\x1\x0\x0\x0\x2\x0\x77\x0\xf0\x40\x44\x0\x40\x69\x43\x0\x0\x0\x8a\x1\x8\x0\x0\x0\x3\x0\x55\x0\xf0\x40\x44\x0\x70\x6a\x43\x0\x0\x0\x8b\x1\x0\x1\x0\x0\x4\x0\x56\x0\x60\x51\x44\x0\x10\x6a\x43\x0\x0\x0\x8c\x1\x40\x0\x0\x0\x5\x0\x5a\x0\xa0\x51\x44\x0\x30\x6b\x43\x0\x0\x0\x91\x1\x80\x0\x0\x0\x6\x0\x57\x0\xe0\x4d\x44\x0\x70\xa6\x44\x0\x0\x71\x8e\x1\x0\x0\x0\x0\x7\x0\x58\x0\x50\x50\x44\x0\x70\xa6\x44\x0\x0\x72\x8f\x1\x0\x0\x0\x0\x8\x0\x6d\x0\xf0\x40\x44\x0\x70\xa6\x44\x0\x0\x6d\x38\x1\x0\x0\x0\x0";
char oth_build[] = "\x0\x0\x1\x0\xa0\x44\x44\x0\xd0\xe6\x40\x0\x1\x3\xd\x1\x0\x0\x0\x0\x1\x0\x43\x0\xb0\x42\x44\x0\x10\xe7\x40\x0\x0\x59\x3e\x1\x0\x0\x0\x0\x1\x0\x45\x0\xb0\x42\x44\x0\x10\xe7\x40\x0\x1\x5b\x3c\x1\x0\x0\x0\x0\x2\x0\x4f\x0\xf0\x40\x44\x0\x70\xa6\x44\x0\x0\x6c\x90\x1\x0\x0\x0\x0\x3\x0\xb5\x0\xf0\x40\x44\x0\xd0\xe6\x40\x0\x1\x11\x8a\x1\x0\x0\x0\x0";
char hth_build[] = "\x2\x0\x4f\x0\xf0\x40\x44\x0\x70\xa6\x44\x0\x0\x6b\x90\x1\x0\x0\x0\x0\x3\x0\xb4\x0\xf0\x40\x44\x0\xd0\xe6\x40\x0\x1\x10\x8a\x1\x0\x0\x0\x0";
char churc[] = "\x0\x0\xa\x0\xc0\x43\x44\x0\x90\xe6\x40\x0\x14\x21\x72\x1\x0\x0\x0\x0\x0\x0\x6b\x0\x10\x44\x44\x0\x90\xe6\x40\x0\x1\x23\x81\x1\x0\x0\x0\x0\x1\x0\x6e\x0\x10\x44\x44\x0\x90\xe6\x40\x0\x3\x24\x83\x1\x0\x0\x0\x0\x3\x0\x6d\x0\xf0\x40\x44\x0\xf0\x40\x44\x0\x0\x0\x6c\x1\x0\x0\x0\x0";
void show_message(byte time, char* text)
{
((void (*)(char*, int, int))F_MAP_MSG)(text, 15, time * 10);//original war2 show msg func
}
void clear_chat()
{
for (int i = 0; i < 12; i++)
{
int* p = (int*)CHAT_POINTER;
p = (int*)(*((int*)((uintptr_t)p)));
p -= 100 / 4 * i;
char buf[] = "\x0";//just set 0 in all messages to empty
PATCH_SET((char*)p, buf);
}
}
int get_val(int adress, int player)
{
return (int)(*(WORD*)(adress + player * 2));//player*2 cause all vals is WORD
}
bool cmp_args(byte m, byte v, byte c)
{//compare bytes
bool f = false;
switch (m)
{
case CMP_EQ:f = (v == c); break;
case CMP_NEQ:f = (v != c); break;
case CMP_BIGGER_EQ:f = (v >= c); break;
case CMP_SMALLER_EQ:f = (v <= c); break;
case CMP_BIGGER:f = (v > c); break;
case CMP_SMALLER:f = (v < c); break;
default: f = false; break;
}
return f;
}
bool cmp_args2(byte m, WORD v, WORD c)
{//compare words
bool f = false;
switch (m)
{
case CMP_EQ:f = (v == c); break;
case CMP_NEQ:f = (v != c); break;
case CMP_BIGGER_EQ:f = (v >= c); break;
case CMP_SMALLER_EQ:f = (v <= c); break;
case CMP_BIGGER:f = (v > c); break;
case CMP_SMALLER:f = (v < c); break;
default: f = false; break;
}
return f;
}
bool cmp_args4(byte m, int v, int c)
{//comapre 4 bytes (for resources)
bool f = false;
switch (m)
{
case CMP_EQ:f = (v == c); break;
case CMP_NEQ:f = (v != c); break;
case CMP_BIGGER_EQ:f = (v >= c); break;
case CMP_SMALLER_EQ:f = (v <= c); break;
case CMP_BIGGER:f = (v > c); break;
case CMP_SMALLER:f = (v < c); break;
default: f = false; break;
}
return f;
}
void lose(bool t)
{
if (t == true)
{
char buf[] = "\x0";//if need to show table
PATCH_SET((char*)LOSE_SHOW_TABLE, buf);
}
else
{
char buf[] = "\x3b";
PATCH_SET((char*)LOSE_SHOW_TABLE, buf);
}
if (!first_step)
{
char l[] = "\x2\x0\x0\x0";
PATCH_SET((char*)(ENDGAME_STATE + (4 * (*(byte*)LOCAL_PLAYER))), l);
((void (*)())F_LOSE)();//original lose func
}
else
{
patch_setdword((DWORD*)0x004C0D38, (DWORD)F_LOSE);
}
}
void win(bool t)
{
if (t == true)
{
char buf[] = "\xEB";//if need to show table
PATCH_SET((char*)WIN_SHOW_TABLE, buf);
}
else
{
char buf[] = "\x74";
PATCH_SET((char*)WIN_SHOW_TABLE, buf);
}
if (!first_step)
{
char l[] = "\x3\x0\x0\x0";
PATCH_SET((char*)(ENDGAME_STATE + (4 * (*(byte*)LOCAL_PLAYER))), l);
((void (*)())F_WIN)();//original win func
}
else
{
patch_setdword((DWORD*)0x004C0D38, (DWORD)F_WIN);
}
}
void lose2(bool t, byte vid)
{
lose(t);
((void (*)(int, byte))F_VIDEO)(vid, 1);//original war2 func that show video by id
}
void win2(bool t, byte vid)
{
win(t);
((void (*)(int, byte))F_VIDEO)(vid, 1);//original war2 func that show video by id
}
void tile_remove_trees(int x, int y)
{
((void (*)(int, int))F_TILE_REMOVE_TREES)(x, y);
}
void tile_remove_rocks(int x, int y)
{
((void (*)(int, int))F_TILE_REMOVE_ROCKS)(x, y);
}
void tile_remove_walls(int x, int y)
{
((void (*)(int, int))F_TILE_REMOVE_WALLS)(x, y);
}
bool stat_byte(byte s)
{//chech if unit stat is 1 or 2 byte
bool f = (s == S_DRAW_X) || (s == S_DRAW_Y) || (s == S_X) || (s == S_Y) || (s == S_HP)
|| (s == S_INVIZ) || (s == S_SHIELD) || (s == S_BLOOD) || (s == S_HASTE)
|| (s == S_AI_SPELLS) || (s == S_NEXT_FIRE)
|| (s == S_LAST_HARVEST_X) || (s == S_LAST_HARVEST_Y)
|| (s == S_BUILD_PROGRES) || (s == S_BUILD_PROGRES_TOTAL)
|| (s == S_RESOURCES) || (s == S_ORDER_X) || (s == S_ORDER_Y)
|| (s == S_RETARGET_X1) || (s == S_RETARGET_Y1) || (s == S_RETARGET_X2) || (s == S_RETARGET_Y2);
return !f;
}
bool cmp_stat(int* p, int v, byte pr, byte cmp)
{
//p - unit
//v - value
//pr - property
//cmp - compare method
bool f = false;
if (stat_byte(pr))
{
byte ob = v % 256;
char buf[3] = { 0 };
buf[0] = ob;
buf[1] = *((byte*)((uintptr_t)p + pr));
if (cmp_args(cmp, buf[1], buf[0]))
{
f = true;
}
}
else
{
if (cmp_args2(cmp, *((WORD*)((uintptr_t)p + pr)), (WORD)v))
{
f = true;
}
}
return f;
}
void set_stat(int* p, int v, byte pr)
{
if (stat_byte(pr))
{
char buf[] = "\x0";
buf[0] = v % 256;
PATCH_SET((char*)((uintptr_t)p + pr), buf);
}
else
{
char buf[] = "\x0\x0";
buf[0] = v % 256;
buf[1] = (v / 256) % 256;
PATCH_SET((char*)((uintptr_t)p + pr), buf);
}
}
void unit_convert(byte player, int who, int tounit, int a)
{
//original war2 func converts units
((void (*)(byte, int, int, int))F_UNIT_CONVERT)(player, who, tounit, a);
}
void unit_create(int x, int y, int id, byte owner, byte n)
{
while (n > 0)
{
n -= 1;
int* p = (int*)(UNIT_SIZE_TABLE + 4 * id);//unit sizes table
((void (*)(int, int, int, byte, int*))F_UNIT_CREATE)(x, y, id, owner, p);//original war2 func creates unit
//just call n times to create n units
}
}
void unit_kill(int* u)
{
((void (*)(int*))F_UNIT_KILL)(u);//original war2 func kills unit
}
void unit_remove(int* u)
{
byte f = *((byte*)((uintptr_t)u + S_FLAGS3));
f |= SF_HIDDEN;
set_stat(u, f, S_FLAGS3);
unit_kill(u);//hide unit then kill
}
void unit_cast(int* u)//unit autocast
{
((void (*)(int*))F_AI_CAST)(u);//original war2 ai cast spells
}
void set_region(int x1, int y1, int x2, int y2)
{
if (x1 < 0)x1 = 0;
if (x1 > 127)x1 = 127;
if (y1 < 0)y1 = 0;
if (y1 > 127)y1 = 127;
if (x2 < 0)x2 = 0;
if (x2 > 127)x2 = 127;
if (y2 < 0)y2 = 0;
if (y2 > 127)y2 = 127;
reg[0] = x1 % 256;
reg[1] = y1 % 256;
reg[2] = x2 % 256;
reg[3] = y2 % 256;
}
bool in_region(byte x, byte y, byte x1, byte y1, byte x2, byte y2)
{
//dnt know why but without this big monstrous ussless code gam crash
byte tmp;
tmp = x % 256;
x = tmp;
tmp = y % 256;
y = tmp;
tmp = x1 % 256;
x1 = tmp;
tmp = y1 % 256;
y1 = tmp;
tmp = x2 % 256;
x2 = tmp;
tmp = y2 % 256;
y2 = tmp;
if (x < 0)x = 0;
if (x > 127)x = 127;
if (y < 0)y = 0;
if (y > 127)y = 127;
if (x1 < 0)x1 = 0;
if (x1 > 127)x1 = 127;
if (y1 < 0)y1 = 0;
if (y1 > 127)y1 = 127;
if (x2 < 0)x2 = 0;
if (x2 > 127)x2 = 127;
if (y2 < 0)y2 = 0;
if (y2 > 127)y2 = 127;
if (x2 < x1)
{
tmp = x1;
x1 = x2;
x2 = tmp;
}
if (y2 < y1)
{
tmp = y1;
y1 = y2;
y2 = tmp;
}
//just check if coords inside region
return ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2));
}
bool check_unit_dead(int* p)
{
bool dead = false;
if (p)
{
if ((*((byte*)((uintptr_t)p + S_FLAGS3))
& (SF_DEAD | SF_DIEING | SF_UNIT_FREE)) != 0)
dead = true;
}
else
dead = true;
return dead;
}
bool check_unit_complete(int* p)//for buildings
{
bool f = false;
if (p)
{
if ((*((byte*)((uintptr_t)p + S_FLAGS3)) & SF_COMPLETED) != 0)//flags3 last bit
f = true;
}
else
f = false;
return f;
}
bool check_unit_hidden(int* p)
{
bool f = false;
if (p)
{
if ((*((byte*)((uintptr_t)p + S_FLAGS3)) & SF_HIDDEN) != 0)//flags3 4 bit
f = true;
}
else
f = true;
return f;
}
bool check_unit_preplaced(int* p)
{
bool f = false;
if (p)
{
if ((*((byte*)((uintptr_t)p + S_FLAGS3)) & SF_PREPLACED) != 0)//flags3
f = true;
}
else
f = false;
return f;
}
bool check_unit_near_death(int* p)
{
bool dead = false;
if (p)
{
if (((*((byte*)((uintptr_t)p + S_FLAGS3)) & SF_DIEING) != 0)
&& ((*((byte*)((uintptr_t)p + S_FLAGS3)) & (SF_DEAD | SF_UNIT_FREE)) == 0))
dead = true;
}
else
dead = true;
return dead;
}
bool check_peon_loaded(int* p, byte r)
{
bool f = false;
if (p)
{
if (r == 0)
{
if (((*((byte*)((uintptr_t)p + S_PEON_FLAGS)) & PEON_LOADED) != 0)
&& ((*((byte*)((uintptr_t)p + S_PEON_FLAGS)) & PEON_HARVEST_GOLD) != 0))
f = true;
}
if (r == 1)
{
if (((*((byte*)((uintptr_t)p + S_PEON_FLAGS)) & PEON_LOADED) != 0)
&& ((*((byte*)((uintptr_t)p + S_PEON_FLAGS)) & PEON_HARVEST_LUMBER) != 0))
f = true;
}
if (r == 2)
{
if (((*((byte*)((uintptr_t)p + S_PEON_FLAGS)) & PEON_LOADED) != 0))
f = true;
}
}
return f;
}
void find_all_units(byte id)
{
//CAREFUL with this function - ALL units get into massive
//even if their memory was cleared already
//all units by id will go in array
units = 0;
int* p = (int*)UNITS_MASSIVE;//pointer to units
p = (int*)(*p);
int k = *(int*)UNITS_NUMBER;
while (k > 0)
{
bool f = *((byte*)((uintptr_t)p + S_ID)) == (byte)id;
if (f)
{
unit[units] = p;
units++;
}
p = (int*)((int)p + 0x98);
k--;
}
}
void find_all_alive_units(byte id)
{
//all units by id will go in array
units = 0;
for (int i = 0; i < 16; i++)
{
int* p = (int*)(UNITS_LISTS + 4 * i);//pointer to units list for each player
if (p)
{
p = (int*)(*p);
while (p)
{
bool f = *((byte*)((uintptr_t)p + S_ID)) == (byte)id;
if (id == ANY_BUILDING)
f = *((byte*)((uintptr_t)p + S_ID)) >= U_FARM;//buildings
if (id == ANY_MEN)
f = *((byte*)((uintptr_t)p + S_ID)) < U_FARM;//all nonbuildings
if (id == ANY_UNITS)
f = true;//all ALL units
if (id == ANY_BUILDING_2x2)//small buildings
{
byte sz = *((byte*)UNIT_SIZE_TABLE + *((byte*)((uintptr_t)p + S_ID)) * 4);
f = sz == 2;
}
if (id == ANY_BUILDING_3x3)//med buildings
{
byte sz = *((byte*)UNIT_SIZE_TABLE + *((byte*)((uintptr_t)p + S_ID)) * 4);
f = sz == 3;
}
if (id == ANY_BUILDING_4x4)//big buildings
{
byte sz = *((byte*)UNIT_SIZE_TABLE + *((byte*)((uintptr_t)p + S_ID)) * 4);
f = sz == 4;
}
if (f)
{
if (!check_unit_dead(p))
{
unit[units] = p;
units++;
}
}
p = (int*)(*((int*)((uintptr_t)p + S_NEXT_UNIT_POINTER)));
}
}
}
}
void sort_complete()
{
//only completed units stay in array
int k = 0;
for (int i = 0; i < units; i++)
{
if (check_unit_complete(unit[i]))
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_in_region()
{
//only units in region stay in array
int k = 0;
WORD x = 0, y = 0;
for (int i = 0; i < units; i++)
{
x = *((WORD*)((uintptr_t)unit[i] + S_DRAW_X)) / 32;
y = *((WORD*)((uintptr_t)unit[i] + S_DRAW_Y)) / 32;
if (in_region((byte)x, (byte)y, reg[0], reg[1], reg[2], reg[3]))
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_not_in_region()
{
//only units not in region stay in array
int k = 0;
WORD x = 0, y = 0;
for (int i = 0; i < units; i++)
{
x = *((WORD*)((uintptr_t)unit[i] + S_DRAW_X)) / 32;
y = *((WORD*)((uintptr_t)unit[i] + S_DRAW_Y)) / 32;
if (!in_region((byte)x, (byte)y, reg[0], reg[1], reg[2], reg[3]))
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_target_in_region()
{
//only units that have order coords in region stay in array
int k = 0;
byte x = 0, y = 0;
for (int i = 0; i < units; i++)
{
x = *((byte*)((uintptr_t)unit[i] + S_ORDER_X));
y = *((byte*)((uintptr_t)unit[i] + S_ORDER_Y));
if (in_region(x, y, reg[0], reg[1], reg[2], reg[3]))
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_stat(byte pr, int v, byte cmp)
{
//only units stay in array if have property compared to value is true
int k = 0;
for (int i = 0; i < units; i++)
{
if (cmp_stat(unit[i], v, pr, cmp))
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_tp_flag()
{
//if not teleported by portal
int k = 0;
for (int i = 0; i < units; i++)
{
if ((*((byte*)((uintptr_t)unit[i] + S_FLAGS3)) & SF_TELEPORT) == 0)//unused in actual game flag
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_hidden()
{
//only not hidden units stay in array
int k = 0;
for (int i = 0; i < units; i++)
{
if (!check_unit_hidden(unit[i]))
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_self(int* u)
{
//unit remove self from array
int k = 0;
for (int i = 0; i < units; i++)
{
if (!(unit[i] == u))
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_full_hp()
{
//if hp not full
int k = 0;
for (int i = 0; i < units; i++)
{
byte id = *((byte*)((uintptr_t)unit[i] + S_ID));//unit id
WORD mhp = *(WORD*)(UNIT_HP_TABLE + 2 * id);//max hp
WORD hp = *((WORD*)((uintptr_t)unit[i] + S_HP));//unit hp
if (hp < mhp)//hp not full
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_fleshy()
{
//only fleshy units stay in array
int k = 0;
for (int i = 0; i < units; i++)
{
byte id = *((byte*)((uintptr_t)unit[i] + S_ID));//unit id
if ((*(int*)(UNIT_GLOBAL_FLAGS + id * 4) & IS_FLESHY) != 0)//fleshy global flag
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_order_hp()
{
//order array by hp from low to high
for (int i = 0; i < units; i++)
{
int sm = i;
for (int j = i + 1; j < units; j++)
{
WORD hpsm = *((WORD*)((uintptr_t)unit[sm] + S_HP));//unit hp
WORD hpj = *((WORD*)((uintptr_t)unit[j] + S_HP));//unit hp
if (hpj < hpsm)
{
sm = j;
}
}
int* tmp = unit[i];
unit[i] = unit[sm];
unit[sm] = tmp;
}
}
void sort_preplaced()
{
int k = 0;
for (int i = 0; i < units; i++)
{
if (!check_unit_preplaced(unit[i]))
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_near_death()
{
int k = 0;
for (int i = 0; i < units; i++)
{
if (check_unit_near_death(unit[i]))
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_attack_can_hit(int* p)
{
//only units stay in array that *p can attack them
int k = 0;
for (int i = 0; i < units; i++)
{
int a = 0;
a = ((int(*)(int*, int*))F_ATTACK_CAN_HIT)(p, unit[i]);//attack can hit original war2 function
if (a != 0)
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_attack_can_hit_range(int* p)
{
//only units stay in array that *p can attack them and have passable terrain in attack range
int k = 0;
for (int i = 0; i < units; i++)
{
int a = 0;
a = ((int(*)(int*, int*))F_ATTACK_CAN_HIT)(p, unit[i]);//attack can hit
if (a != 0)
{
byte id = *((byte*)((uintptr_t)unit[i] + S_ID));
byte szx = *(byte*)(UNIT_SIZE_TABLE + 4 * id);
byte szy = *(byte*)(UNIT_SIZE_TABLE + 4 * id + 2);
byte idd = *((byte*)((uintptr_t)p + S_ID));
byte rng = *(byte*)(UNIT_RANGE_TABLE + idd);
byte ms = *(byte*)MAP_SIZE;
byte xx = *((byte*)((uintptr_t)unit[i] + S_X));
byte yy = *((byte*)((uintptr_t)unit[i] + S_Y));
if (xx < rng)xx = 0;
else xx -= rng;
if (yy < rng)yy = 0;
else yy -= rng;
byte cl = *((byte*)((uintptr_t)p + S_MOVEMENT_TYPE));//movement type
WORD mt = *(WORD*)(GLOBAL_MOVEMENT_TERRAIN_FLAGS + 2 * cl);//movement terrain flags
bool f = false;
for (int x = xx; (x < szx + xx + rng * 2 + 1) && (x < 127); x++)
{
for (int y = yy; (y < szy + yy + rng * 2 + 1) && (x < 127); y++)
{
int aa = 1;
if ((cl == 0) || (cl == 3))//land and docked transport
{
aa = ((int (*)(int, int, int))F_XY_PASSABLE)(x, y, (int)mt);//original war2 func if terrain passable with that movement type
}
if ((x % 2 == 0) && (y % 2 == 0))//air and water
{
if ((cl == 1) || (cl == 2))
{
aa = ((int (*)(int, int, int))F_XY_PASSABLE)(x, y, (int)mt);
}
}
if (aa == 0)f = true;
}
}
if (f)
{
unitt[k] = unit[i];
k++;
}
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_rune_near()
{
int k = 0;
for (int i = 0; i < units; i++)
{
byte x = *((byte*)((uintptr_t)unit[i] + S_X));
byte y = *((byte*)((uintptr_t)unit[i] + S_Y));
bool f = false;
for (int r = 0; r < 50; r++)//max runes 50
{
WORD d = *(WORD*)(RUNEMAP_TIMERS + 2 * r);
if (d != 0)
{
byte xx = *(byte*)(RUNEMAP_X + r);
byte yy = *(byte*)(RUNEMAP_Y + r);
if (xx == x)
{
if (yy > y)
{
if ((yy - y) == 1)f = true;
}
else
{
if ((y - yy) == 1)f = true;
}
}
if (yy == y)
{
if (xx > x)
{
if ((xx - x) == 1)f = true;
}
else
{
if ((x - xx) == 1)f = true;
}
}
}
}
if (!f)
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_peon_loaded(byte r)
{
int k = 0;
for (int i = 0; i < units; i++)
{
if (check_peon_loaded(unit[i], r))
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void sort_peon_not_loaded(byte r)
{
int k = 0;
for (int i = 0; i < units; i++)
{
if (!check_peon_loaded(unit[i], r))
{
unitt[k] = unit[i];
k++;
}
}
units = k;
for (int i = 0; i < units; i++)
{
unit[i] = unitt[i];
}
}
void set_stat_all(byte pr, int v)
{
for (int i = 0; i < units; i++)
{
set_stat(unit[i], v, pr);//set stat to all units in array
}
}
void set_tp_flag(bool f)
{
for (int i = 0; i < units; i++)
{
//set if unit can be teleported by portal (that flag unused in actual game)
if (f)
*((byte*)((uintptr_t)unit[i] + S_FLAGS3)) |= SF_TELEPORT;
else
*((byte*)((uintptr_t)unit[i] + S_FLAGS3)) &= ~SF_TELEPORT;
}
}
void kill_all()
{
for (int i = 0; i < units; i++)
{
unit_kill(unit[i]);//just kill all in array
}
units = 0;
}
void remove_all()
{
for (int i = 0; i < units; i++)
{
unit_remove(unit[i]);//just kill all in array
}
units = 0;
}
void cast_all()
{