-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.cpp
More file actions
3055 lines (2746 loc) · 124 KB
/
menu.cpp
File metadata and controls
3055 lines (2746 loc) · 124 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
/*
* menu.cpp - Menu system, level resource loading
* Addresses: Menu_Init_And_Loop=0045D950, Load_Level_Resources=0041A020
*
* Handle_Menu_State (004611D0) is in gameloop.cpp (thin SEH wrapper).
* Menu_Init_And_Loop initializes resources and enters menu sub-state.
* The actual menu rendering is driven by FUN_00425fe0 in gameplay state.
*/
#include "tou.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* ===== Menu_Init_And_Loop (0045D950) ===== */
/* Initializes sound, switches resolution if needed, loads level resources,
* and sets up the menu sub-state. Returns 1 on success, 0 on failure.
*
* Despite the name, this is NOT a loop — it's an init function.
* The "loop" is the main message loop in WinMain, with Game_State_Manager
* dispatching to FUN_00425fe0 when g_GameState=0/1 and g_SubState=4 (MENU). */
int Menu_Init_And_Loop(void)
{
int iVar1;
DAT_00487824 = 0;
FUN_0040e130(); /* Init/start menu music */
/* Clamp desired mode to valid range. The config (options.cfg) may reference
* modes enumerated by the original game's fullscreen DDraw init; our windowed
* mode decomp has fewer modes in the table. Out-of-range indices read garbage
* from the mode table, producing insane resolutions that hang the system. */
if (g_NumDisplayModes > 0 && (int)(unsigned int)DAT_00487640[2] >= g_NumDisplayModes) {
DAT_00487640[2] = 0;
}
/* Resolution switch: if current mode != desired mode, try to change.
* Original uses pointer arithmetic from &g_NumDisplayModes, but our decomp
* can't guarantee contiguous layout. Use direct array indexing instead. */
if (DAT_00487640[1] != DAT_00487640[2]) {
int modeIdx = (unsigned int)DAT_00487640[2];
DAT_00483724[1] = DAT_00487640[2];
DAT_00487640[1] = DAT_00487640[2];
g_DisplayWidth = g_ModeWidths[modeIdx];
g_DisplayHeight = g_ModeHeights[modeIdx];
Release_DirectDraw_Surfaces();
iVar1 = Init_DirectDraw(g_DisplayWidth, g_DisplayHeight);
/* If DDraw init fails, cycle through modes until one works */
while (iVar1 == 0) {
Release_DirectDraw_Surfaces();
DAT_00487640[2] = DAT_00487640[2] + 1;
if (g_NumDisplayModes <= (int)(unsigned int)DAT_00487640[2]) {
DAT_00487640[2] = 0;
}
modeIdx = (unsigned int)DAT_00487640[2];
g_DisplayWidth = g_ModeWidths[modeIdx];
g_DisplayHeight = g_ModeHeights[modeIdx];
DAT_00483724[1] = DAT_00487640[2];
DAT_00487640[1] = DAT_00487640[2];
Release_DirectDraw_Surfaces();
iVar1 = Init_DirectDraw(g_DisplayWidth, g_DisplayHeight);
}
}
/* Load level data and resources */
iVar1 = Load_Level_Resources();
if (iVar1 != 0) {
DAT_00489e9c = 0;
DAT_00487784 = 0;
/* Place turrets if flag set */
if (DAT_00483834 != 0) {
FUN_004102b0();
}
/* Place troopers if flag set */
if (DAT_00483835 != 0) {
FUN_0041bc50();
}
/* Assign water tile colors (partial FUN_00417460) */
Assign_Water_Tile_Colors();
/* Set up menu display sub-state */
g_SubState = 0;
if (g_SubState2 == 0) {
g_SubState = 4; /* MENU_DISPLAY */
g_NeedsRedraw = 1;
}
g_TimerStart = timeGetTime();
g_TimerAux = 0;
return 1;
}
return 0;
}
/* ===== Load_Level_Resources (0041A020) ===== */
/* Loads level file, initializes entity arrays, and prepares the game world.
* Returns 1 on success, 0 on failure.
*
* Level selection uses the indirection table at g_ConfigBlob[4..0x323]
* (200 ints mapping slot index → actual level index in DAT_00485090).
* DAT_0048693c holds the current slot, masked to 0xFF. */
int Load_Level_Resources(void)
{
int result;
int levelIdx = 0;
char *levelName;
/* Set up flags for local mode.
* Team mode / player count come from config blob (options.cfg). */
DAT_0048396d = 0; /* not a generated map */
DAT_00483960 = 0; /* no swap file */
DAT_004892e4 = 0; /* no random mirror */
DAT_004892e5 = 0; /* no difficulty override */
DAT_0048764a = 0; /* not network mode */
DAT_00489d7c[0] = '\0'; /* clear error buffer */
/* Random mirror: g_ConfigBlob[1] enables X-flip randomization */
if (g_ConfigBlob[1] == 1) {
DAT_004892e4 = (char)(rand() & 1);
}
/* Select level from indirection table.
* g_ConfigBlob[4..0x323] = 200 ints mapping slot → level index.
* DAT_0048693c & 0xFF = current slot number. */
levelName = DAT_00486938; /* fallback: previously set name (if any) */
if (DAT_0048764a == '\0') {
/* Local mode: look up level from indirection table */
int *levelOrder = (int *)&g_ConfigBlob[4]; /* DAT_00481f5c */
levelIdx = levelOrder[DAT_0048693c & 0xFF];
/* Bounds check against available levels */
if (levelIdx < 0 || levelIdx >= DAT_00485088) {
LOG("[LEVEL] WARNING: level index %d out of range (0-%d), clamping\n",
levelIdx, DAT_00485088 - 1);
levelIdx = 0;
}
if (DAT_00485ea0[levelIdx] == '\x02') {
/* GG theme (type 2): procedural level generation.
* Copy theme name to DAT_0048396e for matching, then call generator. */
levelName = (char *)DAT_00485090[levelIdx];
strcpy(DAT_0048396e, levelName);
/* Compute random map dimensions based on size preset g_ConfigBlob[2] */
int gg_w, gg_h;
char sizePreset = (char)g_ConfigBlob[2];
if (sizePreset == 0) {
gg_w = rand() % 800 + 300;
gg_h = rand() % 800 + 300;
} else if (sizePreset == 1) {
gg_w = rand() % 0x4B0 + 400;
gg_h = rand() % 0x4B0 + 400;
} else if (sizePreset == 2) {
gg_w = rand() % 0x4B0 + 0x400;
gg_h = rand() % 0x4B0 + 0x400;
} else {
gg_w = 7000;
gg_h = 7000;
}
result = FUN_004143e0(gg_w, gg_h);
if (result == 0) {
LOG("[LEVEL] GG generation FAILED: %s\n", DAT_00489d7c);
return 0;
}
DAT_00486938 = levelName;
/* Skip Load_Level_File — GG generator already set up the map */
goto gg_level_ready;
}
levelName = (char *)DAT_00485090[levelIdx];
}
if (!levelName || levelName[0] == '\0') {
LOG("[LEVEL] ERROR: No level name available\n");
return 0;
}
DAT_00486938 = levelName;
/* NOTE: Do NOT write to DAT_0048693c here. The original binary uses it as
* a read-only slot index in this function. The low byte is the round counter
* managed by the round-end state machine; bytes 1+ hold per-player scores.
* Writing levelIdx as a full int would clobber score data AND corrupt the
* slot counter if the indirection table reorders levels. */
result = Load_Level_File(levelName);
if (result == 0) {
LOG("[LEVEL] Load_Level_File FAILED: %s\n", DAT_00489d7c);
return 0;
}
/* Load per-level sky image (.SWP) if sky rendering is enabled.
* The level config blob sets DAT_00483960=1 for levels with sky. */
if (DAT_00483960 == '\x01') {
Load_SWP_Sky(levelName);
}
gg_level_ready:
/* Set up player count from config (DAT_0048227c: [0]=total, [1]=human) */
if (DAT_00489240 == 0) {
DAT_00489244 = (int)DAT_0048227c[1]; /* human count */
DAT_00489240 = (int)DAT_0048227c[0]; /* total count */
if (DAT_00489240 < DAT_00489244) {
DAT_00489244 = DAT_00489240;
}
if (DAT_00489240 == 0) {
DAT_00489240 = 2; /* default: 2 players */
DAT_00489244 = 1; /* 1 human */
}
}
/* Player data (DAT_00487810) and ship stats (DAT_0048780c) are now
* allocated in Init_Memory_Pools, matching the original binary. */
/* Load ship types from config blob (0=Peru..8=Dest, 9=random) */
for (int st = 0; st < DAT_00489240 && st < 80; st++) {
DAT_0048236e[st] = g_ConfigBlob[0x416 + st];
}
/* Mark human/CPU flags and team assignments for all players */
if (DAT_00487810 != 0) {
for (int i = 0; i < DAT_00489240; i++) {
int poff = i * 0x598;
*(char *)(DAT_00487810 + poff + 0x480) = (i < DAT_00489244) ? 1 : 0;
*(char *)(DAT_00487810 + poff + 0x2C) = g_ConfigBlob[0x3C6 + i]; /* team */
}
}
/* FUN_0041b010() - Ship/player init */
FUN_0041b010();
/* FUN_004249c0() - Ship sprite loading (fatal on failure in original) */
result = FUN_004249c0();
if (result != 1) {
sprintf(DAT_00489d7c, "Could not load ships!");
return 0;
}
/* Clear extended entity state (matches original loop) */
if (DAT_004892e8) {
for (int i = 0; i < 0x51400; i += 0x80) {
*(int *)((char *)DAT_004892e8 + i + 0x34) = 0;
}
}
/* Player stat scaling: scale ship damage/health/speed, configure entity table.
* Original calls this BEFORE FUN_0041bfe0 (entity spawning). */
FUN_0041a370();
/* Allocate wall segment array if not already allocated */
if (!DAT_00489e80) {
DAT_00489e80 = Mem_Alloc(16 * 0x20);
if (DAT_00489e80) {
g_MemoryTracker += 16 * 0x20;
}
}
/* Entity spawning - populates all entity arrays from level data */
FUN_0041bfe0();
/* Check for entity spawning errors (original checks DAT_00489d7c after FUN_0041bfe0) */
if (DAT_00489d7c[0] != '\0') {
return 0;
}
/* Edge detection: find walkable tiles adjacent to solid */
FUN_0041d2e0();
/* Allocate large game state buffer if needed */
if (!DAT_00487aa4) {
DAT_00487aa4 = Mem_Alloc(0x14000); /* 4 * 0x4000 + overhead */
if (DAT_00487aa4) {
g_MemoryTracker += 0x14000;
}
}
/* Clear game state buffer (original clears before FUN_0041aea0) */
if (DAT_00487aa4) {
memset(DAT_00487aa4, 0, 0x10000);
}
/* Player spawn init: find spawn positions for all players */
FUN_0041aea0();
/* Difficulty constants: map config indices to tick limits */
FUN_0041bed0();
g_SurfaceReady = 2;
/* Team initialization: count active teams */
FUN_00451500();
/* Visibility map: full rebuild for init */
FUN_00449040('\x01');
return 1;
}
/* ===== FUN_004102b0 - Turret Placement Scanner (004102B0) ===== */
/* Three-pass tilemap scanner: count turret-capable tiles, record positions
* with random team, erase turrets from tilemap, then init each turret. */
void FUN_004102b0(void)
{
int iVar1, iVar2;
unsigned int uVar3;
int iVar4, iVar5;
int shift = (unsigned char)DAT_00487a18 & 0x1f;
/* Pass 1: count turret-capable tiles (step by 3 in X, -3 in Y) */
DAT_00489280 = 0;
if (DAT_00481f48 != 0) {
Mem_Free((void *)DAT_00481f48);
}
for (iVar5 = (int)DAT_004879f4 - 1; iVar5 > 0; iVar5 -= 3) {
iVar1 = 0;
if (0 < (int)DAT_004879f0) {
do {
unsigned char tile = *(unsigned char *)((int)DAT_0048782c + (iVar5 << shift) + iVar1);
if (*(char *)((int)DAT_00487928 + (unsigned int)tile * 0x20 + 4) != 0) {
DAT_00489280++;
}
iVar1 += 3;
} while (iVar1 < (int)DAT_004879f0);
}
}
/* Allocate turret array (8 bytes per entry + 10000 overhead) */
DAT_00489280 += 10000;
DAT_00481f48 = (int)Mem_Alloc(DAT_00489280 * 8);
DAT_0048927c = 0;
g_MemoryTracker += DAT_00489280 * 8;
/* Pass 2: record turret positions */
for (iVar5 = (int)DAT_004879f4 - 1; iVar5 > 0; iVar5 -= 3) {
iVar4 = 0;
if (0 < (int)DAT_004879f0) {
do {
iVar2 = (iVar5 << shift);
unsigned char tile = *(unsigned char *)((int)DAT_0048782c + iVar2 + iVar4);
if (*(char *)((int)DAT_00487928 + (unsigned int)tile * 0x20 + 4) != 0) {
*(int *)(DAT_00481f48 + DAT_0048927c * 8) = iVar2 + iVar4;
*(char *)(DAT_00481f48 + DAT_0048927c * 8 + 5) = 0;
*(char *)(DAT_00481f48 + DAT_0048927c * 8 + 6) = 0;
uVar3 = (unsigned int)rand() & 0x80000003;
if ((int)uVar3 < 0) {
uVar3 = (uVar3 - 1 | 0xFFFFFFFC) + 1;
}
*(char *)(DAT_00481f48 + DAT_0048927c * 8 + 4) = (char)uVar3;
*(char *)(DAT_00481f48 + DAT_0048927c * 8 + 7) = 0;
DAT_0048927c++;
}
iVar4 += 3;
} while (iVar4 < (int)DAT_004879f0);
}
}
/* Pass 3: erase turret tiles from tilemap and background */
iVar5 = 0;
if (0 < (int)DAT_004879f4) {
do {
iVar4 = 0;
if (0 < (int)DAT_004879f0) {
do {
iVar2 = (iVar5 << shift) + (int)DAT_0048782c;
unsigned char tile = *(unsigned char *)(iVar2 + iVar4);
if (*(char *)((int)DAT_00487928 + (unsigned int)tile * 0x20 + 4) != 0) {
*(unsigned char *)(iVar2 + iVar4) = 0;
*(unsigned short *)((int)DAT_00481f50 + ((iVar5 << shift) + iVar4) * 2) = 0;
}
iVar4++;
} while (iVar4 < (int)DAT_004879f0);
}
iVar5++;
} while (iVar5 < (int)DAT_004879f4);
}
/* Initialize each turret */
iVar5 = 0;
DAT_00489284 = 0;
if (0 < DAT_0048927c) {
do {
FUN_004104c0(iVar5);
iVar5++;
} while (iVar5 < DAT_0048927c);
}
}
/* ===== FUN_0041bc50 - Trooper Spawn Placement (0041BC50) ===== */
/* Scans tilemap for wall-edge spawn points. For each tile within 7-tile margin:
* if tile is wall-type AND tile above is open → record spawn point.
* Max 50000 entries, 0x1C bytes each. */
void FUN_0041bc50(void)
{
int shift = (unsigned char)DAT_00487a18 & 0x1f;
int stride_diff = DAT_00487a00 - (int)DAT_004879f0;
/* Allocate 1.4MB for trooper spawn data */
DAT_00487820 = Mem_Alloc(1400000);
g_MemoryTracker += 1400000;
int iVar3 = 0;
int iVar7 = (7 << shift) + 7;
DAT_004892c8 = 0;
DAT_004892cc = 1;
int row = 7;
if (7 < (int)DAT_004879f4 - 7) {
do {
int col = 7;
if (7 < (int)DAT_004879f0 - 7) {
unsigned int angle = 0x38;
do {
unsigned char tile_cur = *(unsigned char *)((int)DAT_0048782c + iVar7);
unsigned char tile_above = *(unsigned char *)((int)DAT_0048782c - DAT_00487a00 + iVar7);
/* Current tile must be wall-type */
int is_wall = (*(char *)((int)DAT_00487928 + (unsigned int)tile_cur * 0x20 + 4) == 1)
|| tile_cur == 0xF || tile_cur == 0x10
|| tile_cur == 0x11 || tile_cur == 0x13 || tile_cur == 0x12;
/* Tile above must be open (not wall, not special, type byte 4 == 0, byte 0xB == 0) */
int above_open = (tile_above != 0xF && tile_above != 0x11
&& tile_above != 0x13 && tile_above != 0x12
&& tile_above != 0x10
&& *(char *)((int)DAT_00487928 + (unsigned int)tile_above * 0x20 + 4) == 0
&& *(char *)((int)DAT_00487928 + (unsigned int)tile_above * 0x20 + 0xB) == 0);
if (is_wall && above_open) {
*(int *)((int)DAT_00487820 + iVar3 * 0x1c) = col;
*(int *)((int)DAT_00487820 + DAT_004892c8 * 0x1c + 4) = row;
*(int *)((int)DAT_00487820 + DAT_004892c8 * 0x1c + 8) = 0;
*(int *)((int)DAT_00487820 + DAT_004892c8 * 0x1c + 0xc) = 0;
*(int *)((int)DAT_00487820 + DAT_004892c8 * 0x1c + 0x18) = 0;
/* Get facing angle from math LUT */
*(int *)((int)DAT_00487820 + DAT_004892c8 * 0x1c + 0x14) =
(*(int *)((int)DAT_00487ab0 + (angle & 0x7FF) * 4) >> 10) & 0x7FF;
iVar3 = DAT_004892c8 + 1;
DAT_004892c8 = iVar3;
if (iVar3 > 49999) goto done;
}
iVar7++;
angle += 8;
col++;
} while (col < (int)DAT_004879f0 - 7);
}
iVar7 += stride_diff + 14;
row++;
} while (row < (int)DAT_004879f4 - 7);
}
done:
/* Set terminator flags */
if (DAT_004892c8 > 0) {
*(char *)((int)DAT_00487820 + 0x10) = 0;
*(char *)((int)DAT_00487820 + DAT_004892c8 * 0x1c - 0xb) = 0;
}
FUN_00453230();
FUN_004533d0();
}
/* ===== FUN_0041bed0 - Difficulty Constants (0041BED0) ===== */
/* Maps config indices to scaled game constants (round length, etc.) */
void FUN_0041bed0(void)
{
int table1[14] = { 0, 5, 10, 0x14, 0x1e, 0x28, 0x32, 0x3c,
0x78, 0xb4, 300, 600, 900, 0x708 };
int table2[8] = { 0, 2, 5, 10, 0xf, 0x19, 0x32, 0x78 };
unsigned char idx = (unsigned char)DAT_00483740;
if (idx > 13) {
idx = 13;
DAT_00483740 = 13;
}
DAT_004892a8 = table1[idx] * 0x3f + 1;
if (idx == 0) {
DAT_004892a8 = 0;
}
if (DAT_0048373f > 7) {
DAT_0048373f = 7;
}
DAT_004892ac = table2[(unsigned char)DAT_0048373f] * 0x3f;
}
/* ===== FUN_00451500 - Team Initialization (00451500) ===== */
/* Scans players for active teams. If 0 teams have players, sets victory flag.
* If exactly 1 team, records that team. */
void FUN_00451500(void)
{
int team_active[4] = {0, 0, 0, 0};
if (DAT_00489240 > 0) {
int off = 0;
for (int i = 0; i < DAT_00489240; i++) {
/* Check if player has lives (+0x28) */
if (*(int *)(DAT_00487810 + off + 0x28) != 0) {
unsigned char team = *(unsigned char *)(DAT_00487810 + off + 0x2c);
if (team < 4) {
team_active[team] = 1;
}
}
off += 0x598;
}
}
int count = 0;
int last_team = 0xFF;
for (int i = 0; i < 4; i++) {
if (team_active[i] != 0) {
count++;
last_team = i;
}
}
if (count == 0) {
DAT_004892a4 = (char)0xFF;
DAT_004892a5 = 1;
return;
}
if (count == 1) {
DAT_004892a4 = (char)(last_team + 1);
}
}
/* ===== FUN_0041a370 - Player Stat Scaling (0041A370) ===== */
/* Scales ship stats (damage, health, speed) using config multipliers.
* Configures entity table entries based on game mode.
* Sets turret/trooper/team flags. */
void FUN_0041a370(void)
{
/* Part 1: Call entity config helper if misc config byte 3 == 0 */
if (((unsigned char *)&DAT_00483750)[3] == 0) {
FUN_00460cf0('\x01', 5);
FUN_00460cf0('\x0f', 5);
}
/* Part 2: Scale ship stats per player */
if (DAT_00489240 > 0) {
int off = 0;
for (int i = 0; i < DAT_00489240; i++) {
/* Scale damage: /20 then * multiplier byte */
unsigned int damage = *(unsigned int *)((char *)DAT_0048780c + off + 0x28);
damage /= 0x14;
damage *= ((unsigned int)(DAT_00483748 >> 16) & 0xFF);
*(unsigned int *)((char *)DAT_0048780c + off + 0x28) = damage;
/* Scale health: * multiplier / 20, clamp 1..200 */
unsigned int health = (unsigned int)*(unsigned char *)((char *)DAT_0048780c + off + 0x24);
health = (health * (unsigned int)((unsigned int)DAT_00483748 >> 24)) / 0x14;
if (health > 200) health = 200;
else if (health == 0) health = 1;
*((char *)DAT_0048780c + off + 0x24) = (char)health;
/* Scale speed: * multiplier / 20, clamp 1..200 */
unsigned int speed = (unsigned int)*(unsigned char *)((char *)DAT_0048780c + off + 0x23);
speed = (speed * (unsigned int)(DAT_0048374c & 0xFF)) / 0x14;
if (speed > 200) speed = 200;
else if (speed == 0) speed = 1;
*((char *)DAT_0048780c + off + 0x23) = (char)speed;
off += 0x40;
}
}
/* Part 3: Configure entity table based on game mode (DAT_00483754 byte 0) */
if (DAT_00487928) {
switch (DAT_00483754[0]) {
case 0:
*(char *)((int)DAT_00487928 + 0x4b) = 0;
*(char *)((int)DAT_00487928 + 0x4c) = 1;
*(char *)((int)DAT_00487928 + 0x46) = 1;
*(char *)((int)DAT_00487928 + 0x4d) = 1;
*(char *)((int)DAT_00487928 + 0x26b) = 0;
*(char *)((int)DAT_00487928 + 0x26c) = 1;
*(char *)((int)DAT_00487928 + 0x266) = 1;
*(char *)((int)DAT_00487928 + 0x26d) = 1;
break;
case 1:
*(char *)((int)DAT_00487928 + 0x4b) = 0;
*(char *)((int)DAT_00487928 + 0x4c) = 0;
*(char *)((int)DAT_00487928 + 0x46) = 1;
*(char *)((int)DAT_00487928 + 0x4d) = 1;
*(char *)((int)DAT_00487928 + 0x26b) = 0;
*(char *)((int)DAT_00487928 + 0x26c) = 0;
*(char *)((int)DAT_00487928 + 0x266) = 1;
*(char *)((int)DAT_00487928 + 0x26d) = 1;
break;
case 2:
*(char *)((int)DAT_00487928 + 0x46) = 0;
*(char *)((int)DAT_00487928 + 0x4b) = 0;
*(char *)((int)DAT_00487928 + 0x4c) = 1;
*(char *)((int)DAT_00487928 + 0x4d) = 0;
*(char *)((int)DAT_00487928 + 0x266) = 0;
*(char *)((int)DAT_00487928 + 0x26b) = 0;
*(char *)((int)DAT_00487928 + 0x26c) = 1;
*(char *)((int)DAT_00487928 + 0x26d) = 0;
break;
case 3:
*(char *)((int)DAT_00487928 + 0x46) = 0;
*(char *)((int)DAT_00487928 + 0x4b) = 1;
*(char *)((int)DAT_00487928 + 0x4c) = 0;
*(char *)((int)DAT_00487928 + 0x4d) = 0;
*(char *)((int)DAT_00487928 + 0x266) = 0;
*(char *)((int)DAT_00487928 + 0x26b) = 1;
*(char *)((int)DAT_00487928 + 0x26c) = 0;
*(char *)((int)DAT_00487928 + 0x26d) = 0;
break;
}
}
/* Part 4: Set turret/trooper/team flags */
/* DAT_00483860[0x106] = DAT_00483964 byte 2 in original binary */
if ((DAT_0048372b == 0 || DAT_00483860[0x106] != 0) && DAT_0048372b != 2) {
DAT_00483834 = 0;
} else {
DAT_00483834 = DAT_0048372a + 1;
if (DAT_00483834 != 0) {
DAT_00483836 = 0;
goto after_team;
}
}
DAT_00483836 = 2;
after_team:
/* Trooper flag: enabled only for game type 1 (TDM) with no turrets */
if (DAT_00483729 == 1 && DAT_00483834 == 0) {
DAT_00483835 = 1;
} else {
DAT_00483835 = 0;
}
/* Part 5: Entity table tuning based on turret mode
* DAT_00483836 == 2: turrets DISABLED → stronger entities (compensate)
* DAT_00483836 != 2: turrets ENABLED → weaker entities + extended range init */
if (DAT_00487928) {
if (DAT_00483836 == 2) {
/* Turrets disabled: higher entity spawn thresholds and stats */
*(char *)((int)DAT_00487928 + 0x1ee) = 0x40;
*(char *)((int)DAT_00487928 + 0x20e) = 0x40;
*(char *)((int)DAT_00487928 + 0x26e) = 0x40;
*(char *)((int)DAT_00487928 + 0x22e) = 0x40;
*(char *)((int)DAT_00487928 + 0x24e) = 0x40;
*(char *)((int)DAT_00487928 + 0x80f) = 0xf;
*(char *)((int)DAT_00487928 + 0x82f) = 0xf;
*(char *)((int)DAT_00487928 + 0x84f) = 0xf;
*(char *)((int)DAT_00487928 + 0x86f) = 0xf;
*(char *)((int)DAT_00487928 + 0x88f) = 0xf;
*(char *)((int)DAT_00487928 + 0x8af) = 0xf;
*(char *)((int)DAT_00487928 + 0x8cf) = 0xf;
*(char *)((int)DAT_00487928 + 0x8ef) = 0xf;
*(char *)((int)DAT_00487928 + 0x812) = 0x11;
*(char *)((int)DAT_00487928 + 0x832) = 0x11;
*(char *)((int)DAT_00487928 + 0x852) = 0x11;
*(char *)((int)DAT_00487928 + 0x872) = 0x11;
*(char *)((int)DAT_00487928 + 0x892) = 0x11;
*(char *)((int)DAT_00487928 + 0x8b2) = 0x11;
*(char *)((int)DAT_00487928 + 0x8d2) = 0x11;
*(char *)((int)DAT_00487928 + 0x8f2) = 0x11;
} else {
/* Turrets enabled: lower entity stats + extended entity range init */
*(char *)((int)DAT_00487928 + 0x1ee) = 0;
*(char *)((int)DAT_00487928 + 0x20e) = 0;
*(char *)((int)DAT_00487928 + 0x26e) = 0;
*(char *)((int)DAT_00487928 + 0x22e) = 0;
*(char *)((int)DAT_00487928 + 0x24e) = 0;
*(char *)((int)DAT_00487928 + 0x80f) = 1;
*(char *)((int)DAT_00487928 + 0x82f) = 1;
*(char *)((int)DAT_00487928 + 0x84f) = 1;
*(char *)((int)DAT_00487928 + 0x86f) = 1;
*(char *)((int)DAT_00487928 + 0x88f) = 1;
*(char *)((int)DAT_00487928 + 0x8af) = 1;
*(char *)((int)DAT_00487928 + 0x8cf) = 1;
*(char *)((int)DAT_00487928 + 0x8ef) = 1;
*(char *)((int)DAT_00487928 + 0x812) = 0xc;
*(char *)((int)DAT_00487928 + 0x832) = 0xc;
*(char *)((int)DAT_00487928 + 0x852) = 0xc;
*(char *)((int)DAT_00487928 + 0x872) = 0xc;
*(char *)((int)DAT_00487928 + 0x892) = 0xc;
*(char *)((int)DAT_00487928 + 0x8b2) = 0xc;
*(char *)((int)DAT_00487928 + 0x8d2) = 0xc;
*(char *)((int)DAT_00487928 + 0x8f2) = 0xc;
/* Extended entity range: tiles 100-115 fire rate and damage */
for (int j = 0xc80; j < 0xe80; j += 0x20) {
*(char *)((int)DAT_00487928 + j + 0xf) = 1;
*(char *)((int)DAT_00487928 + j + 0x12) = 0xc;
}
}
}
/* Part 6: Compute damage scaling constants */
/* DAT_00483860[0x107] = DAT_00483964 byte 3 in original binary */
DAT_00483824 = (unsigned int)DAT_00483860[0x107] * (DAT_00483748 & 0xFF) * 0x17;
DAT_00483828 = DAT_00483824 / 3;
/* NOTE: Do NOT override DAT_00483828 from config blob here.
* In the original, FUN_0041a8c0 calls Load_Options_Config (blob reload)
* but does NOT call Sync_Config_From_Blob, so the computed gravity value
* persists. Our FUN_0041a8c0 does Sync_To → Save → Load → Sync_From
* which round-trips the value correctly.
* DAT_00483828 provides upward buoyancy for f2c particles (bubbles)
* and gravity for entity/projectile physics. */
/* Stat scaling applied */
}
/* ===== FUN_0041d2e0 - Edge Detection (0041D2E0) ===== */
/* Scans tilemap for tile boundaries: walkable tiles adjacent to solid tiles.
* Records edge positions in DAT_00489e84 (fixed-point 14.18 format). */
void FUN_0041d2e0(void)
{
int shift = (unsigned char)DAT_00487a18 & 0x1f;
int stride_diff = DAT_00487a00 - (int)DAT_004879f0;
int tilemap = (int)DAT_0048782c;
int etable = (int)DAT_00487928;
int row = 7;
int count = 0;
DAT_00489254 = 0;
int idx = (7 << shift) + 7;
if (7 < (int)DAT_004879f4 - 7) {
do {
int col = 7;
if (7 < (int)DAT_004879f0 - 7) {
do {
/* Check if current tile is walkable (type table +0x18 != 0) */
unsigned char tile = *(unsigned char *)(tilemap + idx);
if (*(char *)(etable + (unsigned int)tile * 0x20 + 0x18) != 0) {
/* Check 4 neighbors for solid tile (type byte 0 == 1) */
unsigned char t_left = *(unsigned char *)(tilemap + idx - 1);
unsigned char t_above = *(unsigned char *)(tilemap - DAT_00487a00 + idx);
unsigned char t_right = *(unsigned char *)(tilemap + idx + 1);
unsigned char t_below = *(unsigned char *)(tilemap + DAT_00487a00 + idx);
int has_solid = (*(char *)(etable + (unsigned int)t_left * 0x20) == 1)
|| (*(char *)(etable + (unsigned int)t_above * 0x20) == 1)
|| (*(char *)(etable + (unsigned int)t_right * 0x20) == 1)
|| (*(char *)(etable + (unsigned int)t_below * 0x20) == 1);
if (has_solid && count < 5000) {
*(int *)((int)DAT_00489e84 + count * 0x10) = col << 18;
*(int *)((int)DAT_00489e84 + DAT_00489254 * 0x10 + 4) = row << 18;
*(char *)((int)DAT_00489e84 + DAT_00489254 * 0x10 + 8) =
*(char *)(etable + (unsigned int)*(unsigned char *)(tilemap + idx) * 0x20 + 0x19);
count = DAT_00489254 + 1;
DAT_00489254 = count;
}
}
idx++;
col++;
} while (col < (int)DAT_004879f0 - 7);
}
idx += stride_diff + 14;
row++;
} while (row < (int)DAT_004879f4 - 7);
}
}
/* ===== FUN_0041aea0 - Player Spawn Init (0041AEA0) ===== */
/* For each player, finds a valid spawn point using FUN_0044dfb0. Sets initial
* position, health, and alive/dead status. */
void FUN_0041aea0(void)
{
if (DAT_00489240 <= 0) return;
int poff = 0;
int soff = 0; /* ship stats table offset (0x40 per entry) */
for (int i = 0; i < DAT_00489240; i++) {
*(char *)(DAT_00487810 + poff + 0x26) = 0;
int result = FUN_0044dfb0(i);
while (result == 0 && *(unsigned char *)(DAT_00487810 + poff + 0x26) < 0x28) {
*(unsigned char *)(DAT_00487810 + poff + 0x26) =
*(unsigned char *)(DAT_00487810 + poff + 0x26) + 1;
result = FUN_0044dfb0(i);
}
if (*(char *)(DAT_00487810 + poff + 0x26) == 0x28) {
/* Failed to find spawn — mark dead */
*(char *)(DAT_00487810 + poff + 0x24) = 1;
*(char *)(DAT_00487810 + poff + 0x25) = 1;
*(char *)(DAT_00487810 + poff + 0x47c) = 0;
*(char *)(DAT_00487810 + poff + 0x47d) = 0;
*(int *)(DAT_00487810 + poff) = 0;
*(int *)(DAT_00487810 + poff + 4) = 0;
} else {
/* Spawn success — mark alive */
*(char *)(DAT_00487810 + poff + 0x26) = 0;
*(char *)(DAT_00487810 + poff + 0x24) = 0;
*(char *)(DAT_00487810 + poff + 0x25) = 0;
*(char *)(DAT_00487810 + poff + 0x47c) = 1;
*(char *)(DAT_00487810 + poff + 0x47d) = 0;
}
/* Copy position to previous position */
*(int *)(DAT_00487810 + poff + 0x08) = *(int *)(DAT_00487810 + poff);
*(int *)(DAT_00487810 + poff + 0x0c) = *(int *)(DAT_00487810 + poff + 4);
/* Set health from ship stats table */
if (DAT_0048780c) {
*(int *)(DAT_00487810 + poff + 0x20) = *(int *)((char *)DAT_0048780c + soff + 0x28);
}
/* Clear velocity */
*(int *)(DAT_00487810 + poff + 0x10) = 0;
*(int *)(DAT_00487810 + poff + 0x14) = 0;
/* Clear button state */
*(unsigned int *)(DAT_00487810 + poff + 0xB8) = 0;
*(unsigned int *)(DAT_00487810 + poff + 0xBC) = 0;
/* Set heading to 0 (pointing right) */
*(int *)(DAT_00487810 + poff + 0x18) = 0;
/* Apply start weapon from loadout grid (right-click selection).
* Only on the first round of a match — subsequent levels keep the
* weapon the player selected during gameplay. */
if ((unsigned char)DAT_0048693c == 0) {
*(int *)(DAT_00487810 + poff + 0x34) = (int)g_StartWeapon[i];
}
/* Default key bindings (overwritten by FUN_0041a8c0 from config blob).
* These serve as initial fallback values during level loading. */
if (i == 0) {
*(unsigned char *)(DAT_00487810 + poff + 0xAC) = 0xCB; /* LEFT arrow: Turn Left */
*(unsigned char *)(DAT_00487810 + poff + 0xAD) = 0xCD; /* RIGHT arrow: Turn Right */
*(unsigned char *)(DAT_00487810 + poff + 0xAE) = 0xC8; /* UP arrow: Thrust */
*(unsigned char *)(DAT_00487810 + poff + 0xAF) = 0x36; /* RSHIFT: Fire Primary */
*(unsigned char *)(DAT_00487810 + poff + 0xB0) = 0x9D; /* RCTRL: Fire Secondary */
*(unsigned char *)(DAT_00487810 + poff + 0xB1) = 0x35; /* NUMPAD /: Special/Detonate */
*(unsigned char *)(DAT_00487810 + poff + 0xB2) = 0xD0; /* DOWN arrow: Brake */
} else {
/* Player 2+ defaults: WASD + space/lctrl/lshift */
*(unsigned char *)(DAT_00487810 + poff + 0xAC) = 0x1E; /* A: Turn Left */
*(unsigned char *)(DAT_00487810 + poff + 0xAD) = 0x20; /* D: Turn Right */
*(unsigned char *)(DAT_00487810 + poff + 0xAE) = 0x11; /* W: Thrust */
*(unsigned char *)(DAT_00487810 + poff + 0xAF) = 0x39; /* SPACE: Fire Primary */
*(unsigned char *)(DAT_00487810 + poff + 0xB0) = 0x1D; /* LCTRL: Fire Secondary */
*(unsigned char *)(DAT_00487810 + poff + 0xB1) = 0x2A; /* LSHIFT: Special/Detonate */
*(unsigned char *)(DAT_00487810 + poff + 0xB2) = 0x1F; /* S: Brake */
}
/* Mark as human player (0 = human, nonzero = AI) */
*(char *)(DAT_00487810 + poff + 0xDD) = 0;
/* Initialize energy to max */
*(int *)(DAT_00487810 + poff + 0x98) = DAT_00483830;
/* Check underwater tile: if tile type byte 4 == 1, set water flag */
int px = *(int *)(DAT_00487810 + poff) >> 18;
int py = *(int *)(DAT_00487810 + poff + 4) >> 18;
int shift = (unsigned char)DAT_00487a18 & 0x1f;
int tile_offset = (py << shift) + px;
unsigned char tile = *(unsigned char *)((int)DAT_0048782c + tile_offset);
if (*(char *)((int)DAT_00487928 + (unsigned int)tile * 0x20 + 4) == 1) {
*(char *)(DAT_00487810 + poff + 0x49f) = 0x3f;
} else {
*(char *)(DAT_00487810 + poff + 0x49f) = 0;
}
poff += 0x598;
soff += 0x40;
}
}
/* ===== FUN_00449040 - Visibility Map Scanner (00449040) ===== */
/* Incremental LOS grid scanner. param==0: process 200 cells per call.
* param==1: process entire grid (init-time full rebuild).
* For opaque tiles, casts rays in 4 diagonal directions (19 tiles each). */
void FUN_00449040(char param)
{
int cells;
if (param == 0) {
cells = 200;
} else {
cells = (DAT_00487a08 - 2) * (DAT_00487a04 - 2);
}
int shadow_buf = (int)DAT_00489ea4;
int shift = (unsigned char)DAT_00487a18 & 0x1f;
int tilemap = (int)DAT_0048782c;
int etable = (int)DAT_00487928;
if (shadow_buf == 0 || tilemap == 0 || etable == 0) return;
while (cells > 0) {
/* Advance cursor */
DAT_004892dc++;
if (DAT_004892dc >= DAT_00487a04 - 1) {
DAT_004892e0++;
DAT_004892dc = 0;
if (DAT_004892e0 >= DAT_00487a08 - 1) {
DAT_004892e0 = 0;
}
}
int grid_x = DAT_004892dc * 18;
int grid_y = DAT_004892e0 * 18;
int tile_idx = (grid_y << shift) + grid_x;
int shadow_idx = DAT_00487a04 * DAT_004892e0 + DAT_004892dc;
unsigned char tile = *(unsigned char *)(tilemap + tile_idx);
int is_opaque = (*(char *)(etable + (unsigned int)tile * 0x20 + 5) == 1);
if (is_opaque) {
/* Ray 1: diagonal down-right (+1, +stride) for 19 tiles */
if (grid_x < (int)DAT_004879f0 - 22 && grid_y < (int)DAT_004879f4 - 22) {
int ri = 0, rp = tile_idx;
do {
if (*(char *)(etable + (unsigned int)*(unsigned char *)(tilemap + rp) * 0x20 + 5) != 1) {
/* Clear ray bit */
if ((*(unsigned char *)(shadow_buf + shadow_idx) & 2) != 0)
*(unsigned char *)(shadow_buf + shadow_idx) ^= 2;
if ((*(unsigned char *)(shadow_buf + DAT_00487a04 + 1 + shadow_idx) & 0x20) != 0)
*(unsigned char *)(shadow_buf + DAT_00487a04 + 1 + shadow_idx) ^= 0x20;
goto ray2;
}
ri++;
rp += 1 + DAT_00487a00;
} while (ri < 19);
/* All opaque: set ray bit */
*(unsigned char *)(shadow_buf + shadow_idx) |= 2;
*(unsigned char *)(shadow_buf + DAT_00487a04 + 1 + shadow_idx) |= 0x20;
}
ray2:
/* Ray 2: right (+1, 0) for 19 tiles */
if (grid_x < (int)DAT_004879f0 - 18) {
int ri = 0;
unsigned char *rp = (unsigned char *)(tilemap + tile_idx);
do {
if (*(char *)(etable + (unsigned int)*rp * 0x20 + 5) != 1) {
if ((*(unsigned char *)(shadow_buf + shadow_idx) & 4) != 0)
*(unsigned char *)(shadow_buf + shadow_idx) ^= 4;
if ((*(unsigned char *)(shadow_buf + 1 + shadow_idx) & 0x40) != 0)
*(unsigned char *)(shadow_buf + 1 + shadow_idx) ^= 0x40;
goto ray3;
}
rp++;
ri++;
} while (ri < 19);
*(unsigned char *)(shadow_buf + shadow_idx) |= 4;
*(unsigned char *)(shadow_buf + 1 + shadow_idx) |= 0x40;
}
ray3:
/* Ray 3: down (0, +stride) for 19 tiles */
if (grid_y < (int)DAT_004879f4 - 22) {
int ri = 0, rp = tile_idx;
do {
if (*(char *)(etable + (unsigned int)*(unsigned char *)(tilemap + rp) * 0x20 + 5) != 1) {
if ((*(unsigned char *)(shadow_buf + shadow_idx) & 1) != 0)
*(unsigned char *)(shadow_buf + shadow_idx) ^= 1;
if ((*(unsigned char *)(shadow_buf + DAT_00487a04 + shadow_idx) & 0x10) != 0)
*(unsigned char *)(shadow_buf + DAT_00487a04 + shadow_idx) ^= 0x10;
goto ray4;
}
rp += DAT_00487a00;
ri++;
} while (ri < 19);
*(unsigned char *)(shadow_buf + shadow_idx) |= 1;
*(unsigned char *)(shadow_buf + DAT_00487a04 + shadow_idx) |= 0x10;
}
ray4:
/* Ray 4: diagonal up-right (+1, -stride) for 19 tiles */
if (grid_x < (int)DAT_004879f0 - 22 && DAT_004892e0 > 0) {
int ri = 0, rp = tile_idx;
do {
if (*(char *)(etable + (unsigned int)*(unsigned char *)(tilemap + rp) * 0x20 + 5) != 1) {
if ((*(unsigned char *)(shadow_buf + shadow_idx) & 8) != 0)
*(unsigned char *)(shadow_buf + shadow_idx) ^= 8;
unsigned char *nb = (unsigned char *)(shadow_buf - DAT_00487a04 + 1 + shadow_idx);
if ((*nb & 0x80) != 0)
*nb ^= 0x80;
goto next_cell;
}
rp += 1 - DAT_00487a00;
ri++;
} while (ri < 19);
*(unsigned char *)(shadow_buf + shadow_idx) |= 8;
*(unsigned char *)(shadow_buf - DAT_00487a04 + 1 + shadow_idx) |= 0x80;
}
} else {
/* Non-opaque tile: clear all visibility bits */
*(unsigned char *)(shadow_buf + shadow_idx) = 0;
unsigned char *b;
b = (unsigned char *)(shadow_buf + DAT_00487a04 + 1 + shadow_idx);