File: sdlmap.pp

package info (click to toggle)
gearhead 1.310-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 10,028 kB
  • sloc: pascal: 36,375; makefile: 78
file content (1631 lines) | stat: -rw-r--r-- 57,861 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
unit sdlmap;
{$MODE FPC}
{
	GearHead: Arena, a roguelike mecha CRPG
	Copyright (C) 2005 Joseph Hewitt

	This library is free software; you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation; either version 2.1 of the License, or (at
	your option) any later version.

	The full text of the LGPL can be found in license.txt.

	This library is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
	General Public License for more details. 

	You should have received a copy of the GNU Lesser General Public License
	along with this library; if not, write to the Free Software Foundation,
	Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
}

interface

uses SDL,SDL_ttf,sdlgfx,gears,gearutil,damage,locale,movement,ability,action,randmaps,effects,ghmecha,ui4gh,ghprop;

const
	{ DISPLAYSHOT CONSTANTS }
	SHOT_Hit = 0;
	SHOT_Dodge = 1;
	SHOT_Parry = 2;

	{ This constant is used by stairs and other portals. If a value }
	{ is addigned to it, the player character should appear on that }
	{ terrain after leaving the current level. }
	SCRIPT_Terrain_To_Seek: Integer = 0;


Function TeamColor( GB: GameBoardPtr; G: GearPtr ): TSDL_Color;
Function OnTheScreen( X , Y: Integer ): Boolean;
Function OnTheScreen( Mek: GearPtr ): Boolean;
Function NeedsRecentering( X,Y: Integer ): Boolean;

Function GearSpriteName( GB: GameBoardPtr; M: GearPtr ): String;


Function TeamColorString( GB: GameBoardPtr; M: GearPtr ): String;

Procedure RedrawTile( gb: GameBoardPtr; X,Y: Integer );
procedure RedrawTile( gb: GameBoardPtr; Mek: GearPtr );

procedure IndicateTile( GB: GameBoardPtr; X , Y , Z: Integer; Primary: Boolean );
procedure IndicateTile( GB: GameBoardPtr; Mek: GearPtr; Primary: Boolean );
procedure IndicateTile( GB: GameBoardPtr; X,Y: Integer );
Procedure MouseAtTile( GB: GameBoardPtr; X,Y: Integer );

Procedure RevealMek( GB: GameBoardPtr; Mek,Spotter: GearPtr );
Procedure VisionCheck( GB: GameBoardPtr; Mek: GearPtr );

Procedure DeployMek( GB: GameBoardPtr; Mek: GearPtr; PutOnMap: Boolean );
Procedure DeployMek( GB: GameBoardPtr; Mek,Pilot: GearPtr; Team: Integer );
Function LocateMekByUID( GB: GameBoardPtr; UID: Integer ): GearPtr;

Function TimeString( ComTime: LongInt ): String;
Procedure SDLCombatDisplay( GB: GameBoardPtr );

Procedure FocusOnMek( GB: GameBoardPtr; Mek: GearPtr );

Function DisplayEffectAnimations( GB: GameBoardPtr; N: Integer ): Boolean;

Procedure DisplayConsoleHistory( GB: GameBoardPtr );

Procedure WriteCampaign( Camp: CampaignPtr; var F: Text );
Function ReadCampaign( var F: Text ): CampaignPtr;

Function ProcessMovement( GB: GameBoardPtr; Mek: GearPtr ): Boolean;

Procedure PrepOpening;
Procedure RedrawOpening;

Function ScreenToMap( X,Y: Integer ): Point;
Function MouseMapPos: Point;

Procedure BeginTurn( GB: GameBoardPtr; M: GearPtr );

Procedure InitMapDisplay( GB: GameBoardPtr; PC: GearPtr );
Procedure FinalizeMapDisplay;

implementation

uses texutil,menugear,ghchars,sdlinfo;

Type
	Overlay_Description = Record
		Sprite: SensibleSpritePtr;
		F: Integer;	{ The frame to be displayed. }
		SubIcon: Integer;	{ Substitute icon to show on map border. }
		UseAlpha: Boolean;	{ Use alpha blending as appropriate. }
		name: String;		{ if NAMES_ABOVE_HEADS, print this. }
	end;

const
	{ This array tells whether or not a given terrain type requires an overlay. }
	Terrain_Toupee: Array [1..NumTerr] of Byte = (
		0, 1, 2, 3, 0,  0, 0, 0, 0, 0,
		0, 0, 0, 0, 0,  0, 0, 0, 4, 0,
		3, 3, 0, 0, 0,  0, 0, 0, 0, 0,
		0, 0, 0, 0, 0,  0, 0, 0, 0, 5,
		0, 0
	);

	NumThinWalls = 16;
	ThinWall_Earth = 1;
	ThinWall_RustySteel = 2;
	ThinWall_Stone = 3;
	ThinWall_Industrial = 4;
	ThinWall_Residential = 5;
    ThinWall_Wood = 6;
    ThinWall_Default = 7;
    ThinWall_Fortress = 8;
    ThinWall_Hospital = 9;
    ThinWall_Golden = 10;
    ThinWall_Commercial = 11;
    ThinWall_StainlessSteel = 12;
    ThinWall_Neon = 13;
    ThinWall_Restaurant = 14;
    ThinWall_Garage = 15;
    ThinWall_Organic = 16;
    

	Terrain_Image: Array [1..NumTerr] of SmallInt = (
		 1, 2, 3, 4, 5,  6, 7, 8, 9,10,
		11,12,-ThinWall_Residential,14,-Thinwall_Stone, 16,17,-ThinWall_Hospital,19,20,
		21,22,-Thinwall_Default,-ThinWall_Golden,25, 26,-ThinWall_Wood,28,-ThinWall_RustySteel,30,
		-ThinWall_Earth,-ThinWall_Commercial,-ThinWall_Fortress,-ThinWall_StainlessSteel,-ThinWall_Industrial, -ThinWall_Neon,-ThinWall_Restaurant,-ThinWall_Garage,39,40,
		41,-ThinWall_Organic
	);

	HalfTileWidth = 32;
	HalfTileHeight = 16;

	OriginX: LongInt = 80;	 { These constants tell what tile is being }
	OriginY: LongInt = -260; { displayed in the upper left corner of the }
				 { map area. }

	Terrain_Sprite_Name = 'big_terrain.png';
	Meta_Terrain_Sprite_Name = 'meta_terrain.png';
	Terrain_Toupee_Sprite_Name = 'iso_64b.png';
	Targeting_Srpite_Name = 'target64.png';
	Items_Sprite_Name = 'default_items.png';

	Default_Wreckage = 1;
{	Default_Dead_Thing = 2;}
	Default_Dead_Thing = 5;
	Default_Shadow = 3;
	Default_Unknown = 4;

	DefaultMaleSpriteName = 'cha_m_citizen.png';
	DefaultFemaleSpriteName = 'cha_f_citizen.png';
	DefaultMaleSpriteHead = 'cha_m_';
	DefaultFemaleSpriteHead = 'cha_f_';
    DefaultNonbinarySpriteHead = 'cha_*_';


	Strong_Hit_Sprite_Name = 'blast64.png';
	Weak_Hit_Sprite_Name = 'nodamage64.png';
	Parry_Sprite_Name = 'misc_parry.png';
	Miss_Sprite_Name = 'misc_miss.png';

	FROZEN_MAP_CONTINUE = 1;
	FROZEN_MAP_SENTINEL = -1;

	LowAlt = -3; { Lowest altitude to render. }
	HiAlt = 5; { Highest altitude to render. }
	Altitude_Height = 20; { Pixel height of each altitude layer. }

	NumOverlayLayers = 7;
	NumConstantLayers = 6;

	OVERLAY_Terrain = 0;
	OVERLAY_ThinWall = 1;
	OVERLAY_Shadow = 2;
	OVERLAY_Item = 3;
	OVERLAY_Metaterrain = 4;
	OVERLAY_Master = 5;
	OVERLAY_Toupee = 6;
	OVERLAY_Image = 7;

	SI_Ally = 3;
	SI_Neutral = 2;
	SI_Enemy = 1;

	NumOMM = 16;
	OM_North = 1;
	OM_East = 2;
	OM_South = 4;
	OM_West = 3;

	Map_Mid_X = 285;
	Map_Mid_Y = 229;

var
	OVERLAY_MAP: Array [ 1..XMax, 1..YMax, LowAlt..HiAlt, 0..NumOverlayLayers ] of Overlay_Description;
	MINI_MAP: Array [1..XMax, 1..YMax] of Byte;

	OFF_MAP_MODELS: Array [1..4,0..NumOMM] of Integer;

	Terrain_Sprite,Meta_Terrain_Sprite,Terrain_Toupee_Sprite,Targeting_Srpite,Items_Sprite: SensibleSpritePtr;
	Strong_Hit_Sprite,Weak_Hit_Sprite,Parry_Sprite,Miss_Sprite,Water_Sprite1,Water_Sprite2: SensibleSpritePtr;
	Thin_wall_Cap: SensibleSpritePtr;
	hill_1,hill_2,hill_3: SensibleSpritePtr;
	Off_Map_Model_Sprite: SensibleSpritePtr;
	Mini_Map_Sprite: SensibleSpritePtr;
    Door_Sprite: SensibleSpritePtr;

	Thin_Wall_Sprites: Array [1..NumThinWalls] of SensibleSpritePtr;

	Opening_Last_Anim_Phase: Uint32;
	Opening_X,Opening_Y: Integer;
	Opening_Phase,Opening_Count: Integer;

    ComDisplay_PC: GearPtr;
    MoreText_GB: GameBoardPtr;

Procedure ClearOverlayLayer( L: Integer );
	{ Clear sprite descriptions from the provided overlay layer. }
var
	X,Y,Z: Integer;
begin
	for X := 1 to XMax do begin
		for Y := 1 to YMax do begin
			for Z := LowAlt to HiAlt do begin
				Overlay_Map[ X , Y , Z , L ].Sprite := Nil;
				Overlay_Map[ X , Y , Z , L ].SubIcon := 0;
				Overlay_Map[ X , Y , Z , L ].UseAlpha := False;
				Overlay_Map[ X , Y , Z , L ].Name := '';
			end;
		end;
	end;
end;

Procedure AddInstantOverlay( X,Y,Z,L,F: Integer; SS: SensibleSpritePtr );
	{ Add an overlay image safely to the display. }
begin
	if not OnTheMap( X , Y ) then Exit;
	if ( Z < LowAlt ) or ( Z > HiAlt ) then Exit;
	if ( L < 0 ) or ( L > NumOverlayLayers ) then Exit;
	Overlay_MAP[ X , Y , Z , L ].Sprite := SS;
	Overlay_MAP[ X , Y , Z , L ].F := F;
end;

Procedure AddOverlayIfClear( X,Y,Z,L,F: Integer; SS: SensibleSpritePtr );
	{ If the requested overlay slot is empty, fill it. Otherwise leave it alone. }
begin
	if not OnTheMap( X , Y ) then Exit;
	if Overlay_Map[ X , Y , Z , L ].Sprite = Nil then AddInstantOverlay( X,Y,Z,L,F,SS );
end;


Procedure AddOverlay( X,Y,Z,L: Integer; Name,Color,GName: String; W,H,F: Integer );
	{ Add an overlay image safely to the display. }
var
	SS: SensibleSpritePtr;
begin
	if not OnTheMap( X , Y ) then Exit;
	if ( Z < LowAlt ) or ( Z > HiAlt ) then Exit;
	if ( L < 0 ) or ( L > NumOverlayLayers ) then Exit;

	SS := ConfirmSprite( Name , Color , W , H );
	Overlay_MAP[ X , Y , Z , L ].Sprite := SS;
	Overlay_MAP[ X , Y , Z , L ].F := F;
	Overlay_MAP[ X , Y , Z , L ].Name := GName;
end;

Function TeamColor( GB: GameBoardPtr; G: GearPtr ): TSDL_Color;
	{ Select a good color based upon the team this }
	{ gear belongs to. }
var
	color: TSDL_Color;
	T: LongInt;
begin
	if ( G = Nil ) or ( GB = Nil ) then begin
		{ No gear provided - Neutral Gray. }
		color := NeutralGrey;

	end else if not GearOperational( G ) then begin
		{ Nonfunctioning gear. Color = DarkGrey. }
		color := DarkGrey;

	end else begin
		T := NAttValue( G^.NA , NAG_Location , NAS_Team );

		if T = NAV_DefPlayerTeam then begin
			{ Player team. Color = Blue. }
			color := PlayerBlue;

		end else if AreEnemies( GB , NAV_DefPlayerTeam , T ) then begin
			{ Enemy team. Color = Red. }
			color := EnemyRed;

		end else if AreAllies( GB , NAV_DefPlayerTeam , T ) then begin
			{ Ally team. Color = Purple. }
			color := AllyPurple;

		end else begin
			{ Neutral team. Color = Brown. }
			color := NeutralBrown;

		end;
	end;
	TeamColor := Color;
end;

Function RelativeX( X,Y: Integer ): LongInt;
	{ Return the relative position of tile X,Y. The UpLeft corner }
	{ of tile [1,1] is the origin of our display. }
begin
	RelativeX := ( (X-1) * HalfTileWidth ) - ( (Y-1) * HalfTileWidth );
end;

Function ScreenX( X,Y: Integer ): LongInt;
	{ Return the screen coordinates of map column X. }
begin
	ScreenX := RelativeX( X , Y ) + ZONE_Map.X + OriginX;
end;

Function RelativeY( X,Y: Integer ): LongInt;
	{ Return the relative position of tile X,Y. The UpLeft corner }
	{ of tile [1,1] is the origin of our display. }
begin
	RelativeY := ( (Y-1) * HalfTileHeight ) + ( (X-1) * HalfTileHeight );
end;

Function ScreenY( X,Y: Integer ): Integer;
	{ Return the screen coordinates of map row Y. }
begin
	ScreenY := RelativeY( X , Y ) + ZONE_Map.Y + OriginY;
end;

Function OnTheScreen( X , Y: Integer ): Boolean;
	{ This function returns TRUE if the specified point is visible }
	{ on screen, FALSE if it isn't. }
var
	SX,SY: LongInt;		{ Find Screen X and Screen Y and see if it's in the map area. }
begin
	SX := ScreenX( X , Y );
	SY := ScreenY( X , Y );
	if ( SX >= ( ZONE_Map.X - 64 ) ) and ( SX <= (ZONE_Map.X+ZONE_Map.w + 64) ) and ( SY >= ( ZONE_Map.Y - 64 ) ) and ( SY <= (ZONE_Map.Y + ZONE_Map.h + 64) ) then begin
		OnTheScreen := True;
	end else begin
		OnTheScreen := False;
	end;
end;

Function OnTheScreen( Mek: GearPtr ): Boolean;
	{ Check to see whether or not the specified mek is visible on screen. }
begin
	OnTheScreen := OnTheScreen( NAttValue( Mek^.NA , NAG_Location , NAS_X ) , NAttValue( Mek^.NA , NAG_Location , NAS_Y ) );
end;

Procedure RecenterDisplay( X , Y: Integer );
	{ Change the display so that point X,Y will be on screen. }
begin
	OriginX := ( ZONE_Map.w div 2 ) - RelativeX( X , Y ) - 32;
	OriginY := ( ZONE_Map.h div 2 ) - RelativeY( X , Y ) - 64;
end;

Function NeedsRecentering( X,Y: Integer ): Boolean;
	{ Check point X,Y to see whether or not the screen needs to be }
	{ centered upon it. }
begin
	NeedsRecentering := False;
end;

Procedure DrawOffMap( Quad,N: Integer );
	{ Draw an off-map model as indicated by the OFF_MAP_MODEL array. }
var
	MyDest: TSDL_Rect;
begin
	if Quad = OM_East then begin
		MyDest.Y := ZONE_Map.Y + ( ZONE_Map.H * ( N + 1 ) ) div ( NumOMM + 2 );
		MyDest.X := ZONE_Map.X + ZONE_Map.W - 16;
	end else if Quad = OM_West then begin
		MyDest.Y := ZONE_Map.Y + ( ZONE_Map.H * ( N + 1 ) ) div ( NumOMM + 2 );
		MyDest.X := ZONE_Map.X + 8;
	end else if Quad = OM_South then begin
		MyDest.X := ZONE_Map.X + ( ( ZONE_Map.W * ( N + 1 ) ) div ( NumOMM + 2 ) );
		MyDest.Y := ZONE_Map.Y + 8;
	end else begin
		MyDest.X := ZONE_Map.X + ( ( ZONE_Map.W * ( N + 1 ) ) div ( NumOMM + 2 ) );
		MyDest.Y := ZONE_Map.Y + ZONE_Map.H - 16;
	end;
	DrawSprite( Off_Map_Model_Sprite , MyDest , OFF_MAP_MODELS[ Quad , N ] - 1 + ( Animation_Phase div 5 mod 2 ) * 3 );
end;

Procedure RenderMap;
	{ Display the map and all its contents in the correct screen zone, }
	{ but don't do a Flip afterwards. }
var
	X,Y,Z,T,Quad: Integer;
	MyDest: TSDL_Rect;
begin
	{ Set the clip area. }
    ZONE_Map.X := 0;
    ZONE_Map.Y := 0;
    ZONE_Map.W := Game_Screen^.W;
    ZONE_Map.H := Game_Screen^.H;
	ClrZone( ZONE_Map );
	SDL_SetClipRect( Game_Screen , @ZONE_Map );

	{ Clear the OFF_MAP_MODELS. }
	for X := 1 to 4 do begin
		for Y := 0 to NumOMM do begin
			OFF_MAP_MODELS[ X , Y ] := 0;
		end;
	end;

	{ Go through each tile on the map, displaying terrain and }
	{ other contents. }
	for X := 1 to XMax do begin
		for Y := 1 to YMax do begin
			if OnTheScreen( X , Y ) then begin
				for Z := LowAlt to HiAlt do begin
					for t := 0 to NumOverlayLayers do begin
						if OVERLAY_MAP[ X ,Y , Z , T ].Sprite <> Nil then begin
							MyDest.X := ScreenX( X , Y );
							MyDest.Y := ScreenY( X , Y ) - Altitude_Height * Z;
							if OVERLAY_MAP[ X ,Y , Z , T ].Sprite^.H > 64 then MyDest.Y := MyDest.Y - 32;
							if Use_Alpha_Blending and OVERLAY_MAP[ X ,Y , Z , T ].UseAlpha and ( Abs( MyDest.Y - Map_Mid_Y ) < 64 ) and ( Abs( MyDest.X - Map_Mid_X ) < 128 ) then begin
								DrawAlphaSprite( OVERLAY_MAP[ X ,Y , Z , T ].Sprite , MyDest , OVERLAY_MAP[ X ,Y , Z , T ].F );
							end else begin
								DrawSprite( OVERLAY_MAP[ X ,Y , Z , T ].Sprite , MyDest , OVERLAY_MAP[ X ,Y , Z , T ].F );
							end;
							if ( OVERLAY_MAP[ X ,Y , Z , T ].name <> '' ) and NAMES_ABOVE_HEADS then begin
								MyDest.X := ScreenX( X , Y ) + HalfTileWidth;
								MyDest.Y := ScreenY( X , Y ) - Altitude_Height * Z - 10;
								QuickTinyText( OVERLAY_MAP[ X ,Y , Z , T ].name , MyDest , StdWhite );
							end;
						end;
					end;
				end; { For Z... }
			end else if OVERLAY_MAP[ X , Y , 0 , OVERLAY_Master ].SubIcon > 0 then begin
				{ This image is off the map, but has a substitute image }
				{ so it should be indicated on the edge. }
				{ Add a note to the OFF_MAP_MODELS array. }
				{ Figure out its relative coordinates, with the center of the map }
				{ as the origin. }
				MyDest.X := ScreenX( X , Y ) - ( ZONE_Map.X + ZONE_Map.W div 2 );
				MyDest.Y := ScreenY( X , Y ) - ( ZONE_Map.Y + ZONE_Map.H div 2 );

				{ Use W to save the segment total length, and H to store the }
				{ relative length. }

				Quad := 1;
				if MyDest.Y <= MyDest.X then Quad := Quad + 1;
				if MyDest.Y <= -MyDest.X then Quad := Quad + 2;

				if ( Quad = 1 ) or ( Quad = 4 ) then begin
					MyDest.W := Abs( MyDest.Y ) * 2;
					MyDest.H := MyDest.X + Abs( MyDest.Y );
				end else begin
					MyDest.W := Abs( MyDest.X ) * 2;
					MyDest.H := MyDest.Y + Abs( MyDest.X );
				end;

				OFF_MAP_MODELS[ Quad , ( MyDest.H * NumOMM ) div MyDest.W ] := OVERLAY_MAP[ X , Y , 0 , OVERLAY_Master ].SubIcon;
			end; { if OnTheScreen... }

		end;
	end;

	{ Display the OFF_MAP_MODELS along the appropriate map edges. }
	for t := 0 to NumOMM do begin
		if Off_Map_Models[ OM_North , T ] > 0 then DrawOffMap( OM_North , T );
		if Off_Map_Models[ OM_South , T ] > 0 then DrawOffMap( OM_South , T );
		if Off_Map_Models[ OM_West , T ] > 0 then DrawOffMap( OM_West , T );
		if Off_Map_Models[ OM_East , T ] > 0 then DrawOffMap( OM_East , T );
	end;

	{ Display the MINI_MAP }
	if Display_Mini_Map then begin
		for x := 1 to XMax do begin
			for y := 1 to YMax do begin
				MyDest.X := ZONE_Map.X + 8 + X*3;
				MyDest.Y := ZONE_Map.Y + 8 + Y*3;
				if ( Mini_Map[ X , Y ] > 0 ) and ( Mini_Map[ X , Y ] < 10 ) then begin
					DrawSprite( Mini_Map_Sprite , MyDest , Mini_Map[ X , Y ] + ( Animation_Phase div 5 mod 2 ) );
				end else begin
					DrawSprite( Mini_Map_Sprite , MyDest , Mini_Map[ X , Y ] );
				end;
			end;
		end;
	end;

	{ Restore the clip area. }
	SDL_SetClipRect( Game_Screen , Nil );
end;

Function GearSpriteName( GB: GameBoardPtr; M: GearPtr ): String;
	{ Locate the sprite name for this gear. If no sprite name is defined, }
	{ set the default sprite name for the gear type & store it as a string }
	{ attribute so we won't need to do this calculation later. }
const
	FORM_DEFAULT: Array [1..NumForm] of String = (
	'btr_buruburu.png','zoa_scylla.png','ghu_ultari.png',
	'ara_kojedo.png', 'aer_wraith.png', 'orn_wasp.png',
	'ger_harpy.png', 'aer_bluebird.png', 'gca_rover.png'
	);
	mini_sprite = 'cha_pilot.png';
var
	it: String;
	FList: SAttPtr;
    gen: Integer;
begin
	{ If this model is an out-of-scale character, return the mini-sprite. }
	if ( M^.G = GG_Character ) and (GB <> Nil) and ( M^.Scale < GB^.Scale ) then Exit( mini_sprite );

	it := SAttValue( M^.SA , 'SDL_SPRITE' );
	if it = '' then begin
		if M^.G = GG_Character then begin
            gen := NAttValue( M^.NA , NAG_CharDescription , NAS_Gender );
			if gen = NAV_Male then begin
				it := DefaultMaleSpriteHead;
			end else if gen = NAV_Female then begin
				it := DefaultFemaleSpriteHead;
            end else begin
                it := DefaultNonbinarySpriteHead;
			end;
			it := it + LowerCase( SAttValue( M^.SA , 'JOB' ) ) + '.*';
			FList := CreateFileList( Graphics_Directory + it );
			if FList <> Nil then begin
				it := SelectRandomSAtt( FList )^.Info;
				DisposeSAtt( FList );
			end else begin
				if gen = NAV_Male then begin
					it := DefaultMaleSpriteName;
				end else if gen = NAV_Female then begin
					it := DefaultFemaleSpriteName;
                end else if random(2) = 1 then it := DefaultMaleSpriteName
				else it := DefaultFemaleSpriteName;
			end;
		end else if ( M^.G = GG_Mecha ) and ( M^.S >= 0 ) and ( M^.S < NumForm ) then begin
			it := FORM_DEFAULT[ M^.S + 1 ];
		end else begin
			it := Items_Sprite_Name;
		end;
		SetSAtt( M^.SA , 'SDL_SPRITE <' + it + '>' );
	end;
	GearSpriteName := it;
end;

Function TeamColorString( GB: GameBoardPtr; M: GearPtr ): String;
	{ Determine the color string for this model. }
var
	it: String;
	T: Integer;
	Team: GearPtr;
begin
	it := SAttValue( M^.SA , 'SDL_COLORS' );
	if ( it = '' ) and ( GB <> Nil ) then begin
		T := NAttValue( M^.NA , NAG_Location , NAS_Team );
		Team := LocateTeam( GB , T );
		if Team <> Nil then it := SAttValue( Team^.SA , 'TEAM_COLORS' );

		if it = '' then begin
			if M^.G = GG_Character then begin
				if T = NAV_DefPlayerTeam then begin
					it := '66 121 179';
				end else if AreEnemies( GB , T , NAV_DefPlayerTeam ) then begin
					it := '180 10 120';
				end else if AreAllies( GB , T , NAV_DefPlayerTeam ) then begin
					it := '66 121 119';
				end else begin
					it := '175 175 171';
				end;
			end else begin
				if T = NAV_DefPlayerTeam then begin
					it := '66 121 179 210 215 80 205 25 0';
				end else if AreEnemies( GB , T , NAV_DefPlayerTeam ) then begin
					it := '180 10 120  125 125 125 170 205 75';
				end else if AreAllies( GB , T , NAV_DefPlayerTeam ) then begin
					it := '66 121 119 190 190 190 0 205 0';
				end else begin
					it := '175 175 171 100 100 120 0 200 200';
				end;
			end;
		end;

		if M^.G = GG_Character then begin
            it := it + ' ' + RandomColorString( CS_Skin ) + ' ' + RandomColorString( CS_Hair );
        end;

		SetSAtt( M^.SA , 'SDL_COLORS <' + it + '>' );

    end else if it = '' then begin
        it := RandomColorString(CS_PrimaryMecha)+' '+RandomColorString(CS_SecondaryMecha)+' '+RandomColorString(CS_Detailing);
		SetSAtt( M^.SA , 'SDL_COLORS <' + it + '>' );
	end;
	TeamColorString := it;
end;

procedure NFDisplayMap( gb: GameBoardPtr );
	{ Actually display the map. }
	Function EffectiveWall( X , Y : Integer ): Boolean;
		{ Return TRUE if X,Y is a wall, a threshold, or not on the map. }
	begin
		if Not OnTheMap( X , Y ) then begin
			EffectiveWall := True;
		end else if GB^.Map[ X , Y ].terr = Terrain_Threshold then begin
			EffectiveWall := True;
		end else if TerrMan[ GB^.Map[ X , Y ].terr ].Pass < -99 then begin
			EffectiveWall := True;
		end else begin
			EffectiveWall := False;
		end;
	end;
	Function WallFrame( X , Y : Integer ): Integer;
		{ Given 16 different thinwall frames, determine which one is right }
		{ for this tile. }
	var
		F: Integer;
	begin
		F := 0;
		if EffectiveWall( X - 1 , Y ) then F := 1;
		if EffectiveWall( X , Y - 1 ) then F := F + 2;
		if EffectiveWall( X + 1 , Y ) then F := F + 4;
		if EffectiveWall( X , Y + 1 ) then F := F + 8;
		WallFrame := F;
	end;
	Function WallCapFrame( X , Y : Integer ): Integer;
		{ Given 16 different thinwall frames, determine which one is right }
		{ for this tile. }
	var
		F: Integer;
	begin
		F := 0;
		if EffectiveWall( X - 1 , Y - 1 ) and EffectiveWall( X - 1 , Y ) and EffectiveWall( X , Y - 1 ) then F := 1;
		if EffectiveWall( X + 1 , Y - 1 ) and EffectiveWall( X + 1 , Y ) and EffectiveWall( X , Y - 1 ) then F := F + 2;
		if EffectiveWall( X + 1 , Y + 1 ) and EffectiveWall( X + 1 , Y ) and EffectiveWall( X , Y + 1 ) then F := F + 4;
		if EffectiveWall( X - 1 , Y + 1 ) and EffectiveWall( X - 1 , Y ) and EffectiveWall( X , Y + 1 ) then F := F + 8;
		WallCapFrame := F;
	end;
	Function EffectiveFloor( X , Y: Integer ): Boolean;
		{ Return TRUE if the listed tile is on the map and a non-threshold floor. }
	begin
		EffectiveFloor := OnTheMap( X , Y ) and ( GB^.Map[ X , Y ].terr <> terrain_threshold ) and ( TerrMan[ GB^.Map[ X , Y ].terr ].Obscurement = 0 );
	end;
	Function WallFloorFrame( X , Y: Integer ): Integer;
		{ The thin walls don't come with their own floors. So, we'll pick a floor style }
		{ from the surrounding terrain. }
	begin
		if EffectiveFloor( X + 1 , Y + 1 ) then WallFloorFrame := GB^.Map[ X + 1 , Y + 1 ].terr - 1
		else if EffectiveFloor( X , Y + 1 ) then WallFloorFrame := GB^.Map[ X , Y + 1 ].terr - 1
		else if EffectiveFloor( X + 1 , Y ) then WallFloorFrame := GB^.Map[ X + 1 , Y ].terr - 1
		else WallFloorFrame := 5;
	end;
	Function EffectiveHill( X , Y , MinZ : Integer ): Boolean;
		{ Return TRUE if X,Y is a hill, or not on the map. }
	begin
		if Not OnTheMap( X , Y ) then begin
			EffectiveHill := True;
		end else begin
			EffectiveHill := TerrMan[ GB^.Map[ X , Y ].terr ].Altitude >= MinZ;
		end;
	end;
	Function HillFrame( X , Y : Integer ): Integer;
		{ Given 16 different thinwall frames, determine which one is right }
		{ for this tile. }
	var
		F: Integer;
	begin
		F := 0;
		if EffectiveHill( X - 1 , Y , TerrMan[ GB^.Map[ X , Y ].terr ].Altitude ) then F := 1;
		if EffectiveHill( X , Y - 1 , TerrMan[ GB^.Map[ X , Y ].terr ].Altitude ) then F := F + 2;
		if EffectiveHill( X + 1 , Y , TerrMan[ GB^.Map[ X , Y ].terr ].Altitude ) then F := F + 4;
		if EffectiveHill( X , Y + 1 , TerrMan[ GB^.Map[ X , Y ].terr ].Altitude ) then F := F + 8;
		HillFrame := F;
	end;

var
	X,Y,Z,T: Integer;
	M: GearPtr;
begin
	{ Clear the overlay grids. We'll be calculating everything fresh. }
	for t := 0 to NumConstantLayers do ClearOverlayLayer( T );

	{ Draw the terrain itself first. }
	for x := 1 to XMax do begin
		for Y := 1 to YMax do begin
			if GB^.Map[ X , Y ].Visible then begin
				Mini_Map[ X , Y ] := GB^.Map[ X , Y ].terr + 9;
				if GB^.Map[ X , Y ].terr = 8 then begin
					AddInstantOverlay( X , Y , 0 , OVERLAY_Terrain , HillFrame( X , Y ) , Hill_1 );
					Overlay_Map[ X , Y , 0 , OVERLAY_Terrain ].UseAlpha := True;
				end else if GB^.Map[ X , Y ].terr = 9 then begin
					AddInstantOverlay( X , Y , 0 , OVERLAY_Terrain , HillFrame( X , Y ) , Hill_2 );
					Overlay_Map[ X , Y , 0 , OVERLAY_Terrain ].UseAlpha := True;
				end else if GB^.Map[ X , Y ].terr = 10 then begin
					AddInstantOverlay( X , Y , 0 , OVERLAY_Terrain , HillFrame( X , Y ) , Hill_3 );
					Overlay_Map[ X , Y , 0 , OVERLAY_Terrain ].UseAlpha := True;
				end else if Terrain_Image[ GB^.Map[ X , Y ].terr ] < 0 then begin
                    T := WallCapFrame( X , Y );
                    if T < 15 then begin
				        AddInstantOverlay( X , Y , 0 , OVERLAY_Terrain , WallFloorFrame( X , Y ) , Terrain_Sprite );
				        AddInstantOverlay( X , Y , 0 , OVERLAY_ThinWall , WallFrame( X , Y ) , Thin_Wall_Sprites[ -Terrain_Image[ GB^.Map[ X , Y ].terr ] ] );
				        AddInstantOverlay( X , Y , 0 , OVERLAY_Toupee , T , Thin_Wall_Cap );
				        Overlay_Map[ X , Y , 0 , OVERLAY_ThinWall ].UseAlpha := True;
				        Overlay_Map[ X , Y , 0 , OVERLAY_Toupee ].UseAlpha := True;
                    end;

                end else if GB^.Map[ X , Y ].terr = 4 then begin
					AddInstantOverlay( X , Y , 0 , OVERLAY_Terrain , (Animation_Phase div 5 + ( (x+y) mod 2 ) * 4 ) mod 8 , Water_Sprite1 );
					AddInstantOverlay( X , Y , 0 , OVERLAY_Toupee , (Animation_Phase div 5 + ( (x+y) mod 2 ) * 4 ) mod 8 , Water_Sprite2 );
                end else if GB^.Map[ X , Y ].terr = 21 then begin
					AddInstantOverlay( X , Y , 0 , OVERLAY_Terrain , (Animation_Phase div 5 + ( (x+y) mod 2 ) * 4 ) mod 8 + 8 , Water_Sprite1 );
					AddInstantOverlay( X , Y , 0 , OVERLAY_Toupee , (Animation_Phase div 5 + ( (x+y) mod 2 ) * 4 ) mod 8 + 8 , Water_Sprite2 );
                end else if GB^.Map[ X , Y ].terr = 22 then begin
					AddInstantOverlay( X , Y , 0 , OVERLAY_Terrain , (Animation_Phase div 5 + ( (x+y) mod 2 ) * 4 ) mod 8 + 16 , Water_Sprite1 );
					AddInstantOverlay( X , Y , 0 , OVERLAY_Toupee , (Animation_Phase div 5 + ( (x+y) mod 2 ) * 4 ) mod 8 + 16 , Water_Sprite2 );

				end else begin
					AddInstantOverlay( X , Y , 0 , OVERLAY_Terrain , GB^.Map[ X , Y ].terr - 1 , Terrain_Sprite );
					if Terrain_Toupee[ GB^.Map[ X , Y ].terr ] <> 0 then AddInstantOverlay( X , Y , 0 , OVERLAY_Toupee , Terrain_Toupee[ GB^.Map[ X , Y ].terr ] - 1 , Terrain_Toupee_Sprite );
				end;
			end else begin
				Mini_Map[ X , Y ] := 0;
				AddInstantOverlay( X , Y , 0 , OVERLAY_Terrain , DEFAULT_Unknown , Items_Sprite );
			end;
		end;
	end;

	{ Next add the items to the map. }
	M := GB^.Meks;
	while M <> Nil do begin
		X := NAttValue( M^.NA , NAG_Location , NAS_X );
		Y := NAttValue( M^.NA , NAG_Location , NAS_Y );
		Z := MekAltitude( GB , M );
		if IsMasterGear(M) and NotDestroyed( M ) then begin
			if OnTheMap( X , Y ) and MekVisible( GB , M ) then begin
				if M^.G = GG_Prop then begin
					AddOverlay( X , Y , Z , OVERLAY_Master , GearSpriteName( GB , M ) , '' , GearName( M ) , 64 , 64 , NAttValue( M^.NA , NAG_Display , NAS_PrimaryFrame ) );
				end else begin
					AddOverlay( X , Y , Z , OVERLAY_Master , GearSpriteName( GB , M ) , TeamColorString( GB , M ) , PilotName( M ) , 64 , 64 , ( NAttValue( M^.NA , NAG_Location , NAS_D ) + 1 ) mod 8 );
				end;

				{ Record what substitute icon to use if this model is off the map. }
				if AreAllies( GB , NAV_DefPlayerTeam , NAttValue( M^.NA , NAG_Location , NAS_Team ) ) then begin
					OVERLAY_MAP[ X , Y , 0 , OVERLAY_Master ].SubIcon := SI_Ally;
					Mini_Map[ X , Y ] := 5;
				end else if AreEnemies( GB , NAV_DefPlayerTeam , NAttValue( M^.NA , NAG_Location , NAS_Team ) ) then begin
					OVERLAY_MAP[ X , Y , 0 , OVERLAY_Master ].SubIcon := SI_Enemy;
					Mini_Map[ X , Y ] := 1;
				end else begin
					OVERLAY_MAP[ X , Y , 0 , OVERLAY_Master ].SubIcon := SI_Neutral;
					Mini_Map[ X , Y ] := 3;
				end;

				{ Also record a shadow. }
				AddInstantOverlay( X , Y , TerrMan[ GB^.Map[ X , Y ].terr ].altitude , OVERLAY_Shadow , Default_Shadow , Items_Sprite );
			end;
		end else begin
			if OnTheMap( X , Y ) and GB^.Map[X,Y].Visible then begin
				if ( M^.G = GG_Mecha ) or ( M^.G = GG_Prop ) then begin
					AddInstantOverlay( X , Y , Z , OVERLAY_Item , Default_Wreckage , Items_Sprite );
				end else if M^.G = GG_Character then begin
					AddInstantOverlay( X , Y , Z , OVERLAY_Item , Default_Dead_Thing , Items_Sprite );
				end else if M^.G = GG_MetaTerrain then begin
					if NotDestroyed( M ) and MekVisible( GB , M ) then 
						if M^.S = GS_MetaCloud then begin
							for t := Z to HiAlt do begin
								AddOverlayIfClear( X , Y , T , OVERLAY_Metaterrain , NAttValue( M^.NA , NAG_Display , NAS_PrimaryFrame ) , Meta_Terrain_Sprite );
							end;
						end else if ( M^.S = GS_MetaFire ) and ( Z < 1 ) then begin
							for t := Z to 1 do begin
								AddInstantOverlay( X , Y , T , OVERLAY_Metaterrain , NAttValue( M^.NA , NAG_Display , NAS_PrimaryFrame ) , Meta_Terrain_Sprite );
							end;
                        end else if ( M^.S = GS_MetaDoor ) then begin
                            { New for 2016- thin doors to go with the thin walls. Yay! }
                            t := 0;
                            if EffectiveWall( X + 1, Y ) then t := t + 1;
                            if M^.stat[ STAT_Pass ] = 0 then t := t + 2;
							AddInstantOverlay( X , Y , Z , OVERLAY_Metaterrain , t , Door_Sprite );
						end else begin
							AddInstantOverlay( X , Y , Z , OVERLAY_Metaterrain , NAttValue( M^.NA , NAG_Display , NAS_PrimaryFrame ) , Meta_Terrain_Sprite );
						end;
				end else begin
					AddOverlay( X , Y , Z , OVERLAY_Item , GearSpriteName( GB , M ) , '' , '' , 64 , 64 , NAttValue( M^.NA , NAG_Display , NAS_PrimaryFrame ) );
				end;
			end;
		end;
		M := M^.Next;
	end;

	{ Call the main map rendering routine. }
	RenderMap;
end;

Procedure RedrawTile( gb: GameBoardPtr; X,Y: Integer );
	{ Just call the above procedure with a Hilight value of FASLE. }
var
	T: Integer;
begin
	if not OnTheMap( X , Y ) then Exit;
	for t := LowAlt to HiAlt do begin
		Overlay_MAP[ X , Y , T , OVERLAY_Image ].Sprite := Nil;
	end;
end;

procedure RedrawTile( gb: GameBoardPtr; Mek: GearPtr );
	{ Display the tile containing the listed gear. }
	{ If the tile is not currently visible, the map will be recentered. }
var
	P: Point;
begin
	P := GearCurrentLocation( Mek );
	if OnTheMap( p.X , P.Y ) then RedrawTile( gb , P.X , P.Y );
end;

procedure IndicateTile( GB: GameBoardPtr; X , Y , Z: Integer; Primary: Boolean );
	{Indicate the desired tile with a rectangle of the requested color. }
begin
	{ Check the range of the coordinates we've been given. }
	if not OnTheMap( X , Y ) then exit;
	if ( Z < LowAlt ) or ( Z > HiAlt ) then Z := 0;

	Overlay_MAP[ X , Y , Z , OVERLAY_IMAGE ].Sprite := Targeting_Srpite;
	Overlay_MAP[ X , Y , Z , OVERLAY_IMAGE ].F := 0;

	if Primary then begin
		RecenterDisplay( X , Y );
	end;
end;

procedure IndicateTile( GB: GameBoardPtr; Mek: GearPtr; Primary: Boolean );
	{ Indicate the tile containing the listed gear. }
var
	team: Integer;
begin
	team := NAttValue( Mek^.NA , NAG_Location , NAS_Team );
	if MekVisible( GB , Mek ) and ( OnTheScreen( Mek ) or ( Team = NAV_DefPlayerTeam ) ) then IndicateTile( GB , NAttValue( Mek^.NA , NAG_Location , NAS_X ) , NAttValue( Mek^.NA , NAG_Location , NAS_Y ) , MekAltitude( GB , Mek ) , Primary );
end;

procedure IndicateTile( GB: GameBoardPtr; X,Y: Integer );
	{ Indicate a tile. }
begin
	if OnTheMap( X , Y ) then begin
		IndicateTile( GB , X , Y , TerrMan[ GB^.Map[ X , Y ].terr ].Altitude , True );
	end;
end;

Procedure MouseAtTile( GB: GameBoardPtr; X,Y: Integer );
	{ The mouse is apparently hovering over this tile. Draw the mouse cursor here. }
begin
	ClearOverlayLayer( OVERLAY_IMAGE );
	if OnTheMap( X , Y ) then begin
		Overlay_MAP[ X , Y , 0 , OVERLAY_IMAGE ].Sprite := Targeting_Srpite;
		Overlay_MAP[ X , Y , 0 , OVERLAY_IMAGE ].F := 1;

	end;
end;


Procedure RevealMek( GB: GameBoardPtr; Mek,Spotter: GearPtr );
	{ This mek has been spotted. Light it up. }
var
	team: Integer;
begin
	team := NAttValue( Spotter^.NA , NAG_Location , NAS_Team );
	SetNAtt( Mek^.NA , NAG_Visibility , Team , NAV_Spotted );
end;

Procedure CheckVisibleArea( GB: GameBoardPtr; Mek: GearPtr );
	{ Expand the visual area around this model. }
var
	P: Point;
	X,Y,MZ,R,Obs: Integer;
begin
	P := GearCurrentLocation( Mek );
	R := MappingRange( Mek , GB^.Scale );
	MZ := MekAltitude( GB , Mek );

	{ Look through every tile within range. If it's on the map and }
	{ not yet revealed, do a check to see if it should be. }
	for X := ( P.X - R ) to ( P.X + R ) do begin
		for Y := ( P.Y - R ) to ( P.Y + R ) do begin
			if OnTheMap( X , Y ) and not GB^.Map[X,Y].Visible then begin
				{ This tile will be revealed if Range + Obscurement }
				{ is less than or equal to the mapping radius. }
				Obs := CalcObscurement( X , Y , TerrMan[ GB^.Map[ X , Y ].Terr ].Altitude , P.X , P.Y , MZ , GB );
				if (( Range( P.X , P.Y , X , Y ) + Obs ) <= R ) and ( Obs <> -1 ) then begin
					GB^.Map[X,Y].Visible := True;
				end;
			end;
		end;
	end;
end;

Procedure VisionCheck( GB: GameBoardPtr; Mek: GearPtr );
	{ Perform a sensor check for MEK. It might spot hidden enemy }
	{ units; it may get spotted by enemy units itself. }
var
	M2: GearPtr;
begin
	if ( Mek = Nil ) or ( not GearOperational( Mek ) ) then exit;

	{ Start by assuming that the mek will be hidden after this. }
	{ Strip all of its visibility tokens. }
	StripNAtt( Mek , NAG_Visibility );

	M2 := GB^.Meks;
	while M2 <> Nil do begin
		{ We are only interested in this other mek if it's an }
		{ enemy of the one we're checking. }
		if OnTheMap( M2 ) then begin
			if not MekCanSeeTarget( gb , Mek , M2 ) then begin

				{ If this enemy mecha has not yet been spotted, }
				{ there's a chance it will become visible. }
				if IsMasterGear( M2 ) and CheckLOS( GB , Mek , M2 ) then begin
					{ M2 has just been spotted. }
					RevealMek( GB , M2 , Mek );
				end;
			end;

			{ There is also a chance that M2 might spot Mek. }
			if IsMasterGear( M2 ) and GearOperational( M2 ) and not MekCanSeeTarget( GB , M2 , Mek ) then begin
				if CheckLOS( GB , M2 , Mek ) then RevealMek( GB , Mek , M2 );
			end;
		end;

		M2 := M2^.Next;
	end;

	if (NAttValue( Mek^.NA , NAG_Location , NAS_Team ) = NAV_DefPlayerTeam) or (NAttValue( Mek^.NA , NAG_Location , NAS_Team ) = NAV_LancemateTeam) then begin
		CheckVisibleArea( GB , Mek );
	end;
end;

Procedure DeployMek( GB: GameBoardPtr; Mek: GearPtr; PutOnMap: Boolean );
	{ Stick the provided MEK onto the game board. Assign a UID, }
	{ and set the default orders for MEK's team. }
	{ PRECONDITION: Mek must be unlinked. }
var
	Team: GearPtr;
	P: Point;
begin
	if ( GB = Nil ) or ( Mek = Nil ) then Exit;

	{ Find the team for this model. }
	Team := LocateTeam( GB , NAttValue( Mek^.NA , NAG_Location , NAS_Team ) );

	{ Gear up the mek. }
	if PutOnMap then GearUp( Mek );

	{ Determine the X and Y values for everything. }
	{ If, according to the PutOnMap parameter, we aren't supposed to put }
	{ this model on the map, set X and Y to 0. }
	{ If the model has X and Y already defined within the map boundaries, }
	{ place it on the map at its specified location. }
	{ Otherwise, determine a good spot to place this mek based upon its }
	{ assigned team. }
	if not PutOnMap then begin
		SetNAtt( mek^.NA , NAG_Location , NAS_X , 0 );
		SetNAtt( mek^.NA , NAG_Location , NAS_Y , 0 );

	end else if OnTheMap( Mek ) and ( SAttValue( Mek^.SA , 'HOME' ) = '' ) then begin
		{ Just set a random direction. }
		SetNAtt( mek^.NA , NAG_Location , NAS_D , Random( 8 ) );

	end else begin
		P := FindDeploymentSpot( GB , Mek );
		SetNAtt( mek^.NA , NAG_Location , NAS_X , P.X );
		SetNAtt( mek^.NA , NAG_Location , NAS_Y , P.Y );
		SetNAtt( mek^.NA , NAG_Location , NAS_D , Random( 8 ) );
	end;


	{ Assign a unique ID for this model. }
	SetNAtt( mek^.NA , NAG_EpisodeData, NAS_UID, MaxIdTag( GB^.Meks , NAG_EpisodeData, NAS_UID ) + 1 );

	{ Stick mek on board. }
	Mek^.Next := gb^.Meks;
	gb^.Meks := Mek;

	{ Set default orders. }
	if Team <> Nil then begin
		SetNAtt( Mek^.NA , NAG_EpisodeData , NAS_Orders , Team^.Stat[ STAT_TeamOrders ] );
	end;
end;

Procedure DeployMek( GB: GameBoardPtr; Mek,Pilot: GearPtr; Team: Integer );
	{ Insert the supplied pilot into the supplied mecha, then insert both }
	{ into the provided scenario. Supply UIDs to mek and pilot. }
	{ PRECONDITION: Mek and Pilot are both unlinked gears. }
begin
	if ( GB = Nil ) or ( Mek = Nil ) or ( Pilot = Nil ) then Exit;

	{ Set the correct values for everything. }
	SetNAtt( mek^.NA , NAG_Location , NAS_Team , Team );

	SetNAtt( Pilot^.NA , NAG_Location , NAS_Team , Team );
	SetNAtt( mek^.NA , NAG_EpisodeData, NAS_UID, MaxIdTag( GB^.Meks , NAG_EpisodeData, NAS_UID ) + 2 );

	{ Locate cockpit, and install pilot. }
	if not BoardMecha( Mek , Pilot ) then begin
		{ PILOT couldn't be installed in MEK. }
		{ Stick PILOT off the map. }
		DeployMek( GB , Pilot, False );
	end;

	DeployMek( gb , Mek , True );
end;

Function LocateMekByUID( GB: GameBoardPtr; UID: Integer ): GearPtr;
	{ Search through the list of mecha associated with this scenario, and }
	{ return the mecha with the given Unique ID. Return Nil if it can't }
	{ be found. }
begin
	{ Error check - UID 0 is impossible. }
	if UID = 0 then Exit( Nil );

	{ Return whatever we found. }
	LocateMekByUID := SeekGearByIDTag( GB^.Meks , NAG_EpisodeData , NAS_UID , UID );
end;

Function TimeString( ComTime: LongInt ): String;
	{ Create a string to express the time listed in COMTIME. }
var
	msg: String;
	S,M,H,D: LongInt;	{ Seconds, Minutes, Hours, Days }
begin
	S := ComTime mod 60;
	M := ( ComTime div 60 ) mod 60;
	H := ( ComTime div AP_Hour ) mod 24;
	D := ComTime div AP_Day;

	msg := Bstr( H ) + ':' + WideStr( M , 2 ) + ':' + WideStr( S , 2 ) + MsgString( 'CLOCK_days' ) + BStr( D );
	TimeString := msg;
end;

Procedure SDLCombatDisplay( GB: GameBoardPtr );
	{ Do each of the standard GearFitr display elements. }
var
	pmsg: PChar;
	MyImage: PSDL_Surface;
begin
	NFDisplayMap( GB );
    SetupWizardDisplay();
	CMessage( TimeString( GB^.ComTime ) , ZONE_Clock , NeutralGrey );

	{ Update the console. }
	RedrawConsole;

    { Show the PC info. }
    if ComDisplay_PC <> Nil then DisplayPCInfo( ComDisplay_PC, GB );
end;

Procedure FocusOnMek( GB: GameBoardPtr; Mek: GearPtr );
	{ Recenter the display on the indicated mecha. }
var
	P: Point;
begin
	if ( Mek <> Nil ) and ( GB <> Nil ) then begin
		P := GearCurrentLocation( Mek );
		RecenterDisplay( P.X , P.Y );
	end;
end;

Function ProcessShotAnimation( GB: GameBoardPtr; var AnimList,AnimOb: GearPtr ): Boolean;
	{ Process this shot. Return TRUE if the missile }
	{ is visible on the screen, FALSE otherwise. }
	{ V = Timer }
	{ Stat 1 , 2 , 3 -> X1 , Y1 , Z1 }
	{ Stat 4 , 5 , 6 -> X2 , Y2 , Z2 }
const
	X1 = 1;
	Y1 = 2;
	Z1 = 3;
	X2 = 4;
	Y2 = 5;
	Z2 = 6;
var
	P: Point;
begin
	{ Increase the counter, and find the next spot. }
	Inc( AnimOb^.V );
	P := SolveLine( AnimOb^.Stat[ X1 ] , AnimOb^.Stat[ Y1 ] , AnimOb^.Stat[ Z1 ] , AnimOb^.Stat[ X2 ] , AnimOb^.Stat[ Y2 ] , AnimOb^.Stat[ Z2 ] , AnimOb^.V );

	{ If this is the destination point, then we're done. }
	if ( P.X = AnimOb^.Stat[ X2 ] ) and ( P.Y = AnimOb^.Stat[ Y2 ] ) then begin
		RemoveGear( AnimList , ANimOb );
		P.X := 0;

	{ If this is not the destination point, draw the missile. }
	end else begin
		{Display bullet...}
		AddInstantOverlay( P.X , P.Y , P.Z , OVERLAY_IMAGE , 1 , Strong_Hit_Sprite );
	end;

	ProcessShotAnimation := OnTheScreen( P.X , P.Y );
end;

Function ProcessPointAnimation( GB: GameBoardPtr; var AnimList,AnimOb: GearPtr ): Boolean;
	{ Process this effect. Return TRUE if the blast }
	{ is visible on the screen, FALSE otherwise. }
	{ V = Timer }
	{ Stat 1 , 2 , 3 -> X , Y , Z }
const
	X = 1;
	Y = 2;
	Z = 3;
var
	it: Boolean;
begin
	if AnimOb^.V < 10 then begin
		case AnimOb^.S of
		GS_DamagingHit: begin
				AddInstantOverlay( AnimOb^.Stat[ X ] , AnimOb^.Stat[ Y ] , AnimOb^.Stat[ Z ] , OVERLAY_IMAGE , AnimOb^.V , Strong_Hit_Sprite );

				end;
		GS_ArmorDefHit: begin
				AddInstantOverlay( AnimOb^.Stat[ X ] , AnimOb^.Stat[ Y ] , AnimOb^.Stat[ Z ] , OVERLAY_IMAGE , AnimOb^.V , Weak_Hit_Sprite );

				end;

		GS_Parry:	begin
				AddInstantOverlay( AnimOb^.Stat[ X ] , AnimOb^.Stat[ Y ] , AnimOb^.Stat[ Z ] , OVERLAY_IMAGE , ( AnimOb^.V + 1 ) div 2 , Parry_Sprite );
				Inc( AnimOb^.V );
				end;

		GS_Dodge:	begin
				AddInstantOverlay( AnimOb^.Stat[ X ] , AnimOb^.Stat[ Y ] , AnimOb^.Stat[ Z ] , OVERLAY_IMAGE , ( AnimOb^.V + 1 ) div 2 , Miss_Sprite );
				Inc( AnimOb^.V );
				end;

		GS_Backlash:	begin
				AddInstantOverlay( AnimOb^.Stat[ X ] , AnimOb^.Stat[ Y ] , AnimOb^.Stat[ Z ] , OVERLAY_IMAGE , AnimOb^.V , Strong_Hit_Sprite );

				end;
		GS_AreaAttack:	begin
				AddInstantOverlay( AnimOb^.Stat[ X ] , AnimOb^.Stat[ Y ] , AnimOb^.Stat[ Z ] , OVERLAY_IMAGE , AnimOb^.V , Strong_Hit_Sprite );

				end;
		end;


		{ Increment the counter. }
		Inc( AnimOb^.V );

		it := OnTheScreen( AnimOb^.Stat[ X ] , AnimOb^.Stat[ Y ] );
	end else begin

		RemoveGear( AnimList , AnimOb );
		it := False;
	end;

	ProcessPointAnimation := it;
end;

Procedure ProcessAnimations( GB: GameBoardPtr; var AnimList: GearPtr );
	{ Display all the queued animations, deleting them as we go along. }
var
	AnimOb,A2: GearPtr;
	DelayThisFrame,PointDelay: Boolean;
begin
	{ Keep processing until we run out of animation objects. }
	while AnimList <> Nil do begin
		{ Erase all current image overlays. }
		ClearOverlayLayer( OVERLAY_Image );

		AnimOb := AnimList;

		{ Assume there'll be no animation delay, unless }
		{ otherwise requested. }
		DelayThisFrame := False;

		while AnimOb <> Nil do begin
			A2 := AnimOb^.Next;

			{ Call a routine based upon the type of }
			{ animation requested. }
			case AnimOb^.S of

			GS_Shot: PointDelay := ProcessShotAnimation( GB , AnimList , AnimOb ) or DelayThisFrame;
			GS_DamagingHit: PointDelay := ProcessPointAnimation( GB , AnimList , AnimOb );
			GS_ArmorDefHit: PointDelay := ProcessPointAnimation( GB , AnimList , AnimOb );
			GS_Parry: PointDelay := ProcessPointAnimation( GB , AnimList , AnimOb );
			GS_Dodge: PointDelay := ProcessPointAnimation( GB , AnimList , AnimOb );
			GS_Backlash: PointDelay := ProcessPointAnimation( GB , AnimList , AnimOb );
			GS_AreaAttack: PointDelay := ProcessPointAnimation( GB , AnimList , AnimOb );

			{ If no routine was found to deal with the animation }
			{ requested, just delete the gear. }
			else RemoveGear( AnimList , AnimOb );
			end;

			DelayThisFrame := DelayThisFrame or PointDelay;

			{ Move to the next animation. }
			AnimOb := A2;
		end;

		{ Delay the animations, if appropriate. }
		if DelayThisFrame then begin
	        RenderMap();
            SetupWizardDisplay();
	        CMessage( TimeString( GB^.ComTime ) , ZONE_Clock , NeutralGrey );
	        RedrawConsole;
            if ComDisplay_PC <> Nil then DisplayPCInfo( ComDisplay_PC, GB );
			GHFlip;
			if ( FrameDelay > 0 ) then SDL_Delay(FrameDelay);
		end;
	end;

	ClearOverlayLayer( OVERLAY_Image );
end;

Function DisplayEffectAnimations( GB: GameBoardPtr; N: Integer ): Boolean;
	{ Display all the animations stored for sequence slice N. }
	{ Return TRUE if an animation was found, or FALSE otherwise. }
var
	A: SAttPtr;
	T: Integer;
	AnimFound: Boolean;
	AnimList,AnimItem: GearPtr;
	AnimLabel,AnimCode: String;
begin
	A := ATTACK_History;
	AnimFound := False;
	AnimList := Nil;

	AnimLabel := SATT_Anim_Direction + BStr( N ) + '_';

	{ Start by creating the animation list. }
	while A <> Nil do begin
		if HeadMatchesString( AnimLabel , A^.Info ) then begin
			AnimFound := True;

			{ Insert animation handling code here. }
			AnimItem := AddGear( AnimList , Nil );
			AnimCode := RetrieveAString( A^.Info );
			AnimItem^.S := ExtractValue( AnimCode );

			T := 1;
			while ( AnimCode <> '' ) and ( T <= NumGearStats ) do begin
				AnimItem^.Stat[ T ] := ExtractValue( AnimCode );
				Inc( T );
			end;
		end;
		A := A^.Next;
	end;

	{ Process each animation. }
	ProcessAnimations( GB , AnimList );

	DisplayEffectAnimations := AnimFound;
end;


Procedure MoreTextRedraw;
begin
    SDLCombatDisplay( MoreText_GB );
end;

Procedure DisplayConsoleHistory( GB: GameBoardPtr );
	{ Display the console history, then restore the display. }
var
	SL: SAttPtr;
begin
    MoreText_GB := GB;
	MoreText( Console_History , MoreHighFirstLine( Console_History ), @MoreTextRedraw );
end;

Procedure WriteCampaign( Camp: CampaignPtr; var F: Text );
	{ Output the supplied campaign and all appropriate data to disk. }
var
	Frz: FrozenLocationPtr;
begin
	{ Output GameBoard. }
	writeln( F , Camp^.GB^.ComTime );
	writeln( F , Camp^.GB^.Scale );
	WriteMap( Camp^.GB^.Map , F );

	{ Can't output the scene gear directly, since it'll be outputted }
	{ with the rest of SOURCE later on. Output its reference number. }
	writeln( F , FindGearIndex( Camp^.Source , Camp^.GB^.Scene ) );

	{ Output map contents. }
	WriteCGears( F , Camp^.GB^.Meks );

	{ Output frozen maps. }
	Frz := Camp^.Maps;
	while Frz <> Nil do begin
		writeln( F , FROZEN_MAP_CONTINUE );
		writeln( F , Frz^.Name );
		WriteMap( Frz^.Map , F );
		Frz := Frz^.Next;
	end;

	{ Output frozen map sentinel marker. }
	writeln( F , FROZEN_MAP_SENTINEL );

	{ Output SOURCE. }
	WriteCGears( F , Camp^.Source );
end;

Function ReadCampaign( var F: Text ): CampaignPtr;
	{ Input the campaign and all appropriate data from disk. }
var
	Camp: CampaignPtr;
	SceneIndex: LongInt;
	N: Integer;
	Frz: FrozenLocationPtr;
begin
	{ Allocate the campaign and the gameboard. }
	Camp := NewCampaign;
	Camp^.GB := NewMap;

	readln( F , Camp^.GB^.ComTime );
	Camp^.ComTime := Camp^.GB^.ComTime;
	readln( F , Camp^.GB^.Scale );
	Camp^.GB^.Map := ReadMap( F );

	{ Read the index of this game board's SCENE gear, and }
	{ remember to set it in the GB structure after SOURCE is loaded. }
	readln( F , SceneIndex );

	{ Read the list of map contents. }
	Camp^.GB^.Meks := ReadCGears( F );

	{ Read the frozen maps. }
	repeat
		ReadLn( F , N );

		if N = FROZEN_MAP_CONTINUE then begin
			Frz := CreateFrozenLocation( Camp^.Maps );
			ReadLn( F , Frz^.Name );
			Frz^.Map := ReadMap( F );
		end;
	until N = FROZEN_MAP_SENTINEL;

	{ Read the source, and set the gameboard's scene. }
	Camp^.Source := ReadCGears( F );
	Camp^.GB^.Scene := LocateGearByNumber( Camp^.Source , SceneIndex );

	{ Return the restored campaign structure. }
	ReadCampaign := Camp;
end;

Function ProcessMovement( GB: GameBoardPtr; Mek: GearPtr ): Boolean;
	{ Call the LOCALE movement routine, then update the display }
	{ here if need be. }
var
	result,Team: Integer;
	X,Y: LongInt;
	MSV: Boolean;	{ Mek Started Visible. }
	Msg: String;
begin
	{ Store the initial position of the mek. }
	X := NAttValue( Mek^.NA , NAG_Location , NAS_X );
	Y := NAttValue( Mek^.NA , NAG_Location , NAS_Y );

	MSV := MekVisible( GB , Mek ) and OnTheScreen( X , Y );

	{ Call the movement procedure, and store the result. }
	result := EnactMovement( GB , Mek );

	{ Depending upon what happened, update the display. }
	if result > 0 then begin
		{ Update the display. }

		{ Check for previously unseen enemies. }
		if OnTheMap( NAttValue( Mek^.NA , NAG_Location , NAS_X ) , NAttValue( Mek^.NA , NAG_Location , NAS_Y ) ) then VisionCheck( GB , Mek )
		{ Print message if mek has fled the battle. }
		else begin
			DialogMSG( PilotName( Mek ) + ' has left this area.');

			{ Set trigger here. }
			Team := NAttValue( Mek^.NA , NAG_Location , NAS_Team );
			SetTrigger( GB , TRIGGER_NumberOfUnits + BStr( Team ) );
		end;

	end else if result = EMR_Crash then begin
		if Mek^.G = GG_Character then begin
			msg := ReplaceHash( MsgString( 'PROCESSMOVEMENT_Fall' ) , GearName( Mek ) );
		end else begin
			msg := ReplaceHash( MsgString( 'PROCESSMOVEMENT_Crash' ) , GearName( Mek ) );
		end;
		DialogMsg( ReplaceHash( msg , BStr( DAMAGE_DamageDone ) ) );
	end;
	ProcessMovement := ( Result <> 0 ) and ( MSV or ( OnTheScreen( Mek ) and MekVisible( GB , Mek ) ) );
end;

Procedure PrepOpening;
	{ Prepare some graphics for the opening of the game. }
const
	FormPat: Array [1..NumForm] of String[6] = (
		'btr_*','zoa_*','ghu_*','ara_*','aer_*',
		'orn_*','ger_*','hov_*','gca_*'
	);
var
	GB: GameBoardPtr;
	Scene: GearPtr;
	X,Y,T: Integer;
	Sprite_Names: SAttPtr;
	name: String;
	SS: SensibleSpritePtr;
begin
	{ Clear the sprite list just to conserve memory. }
	CleanSpriteList;

	{ Start with a random forest map. }
	Scene := NewGear( Nil );
	Scene^.G := GG_Scene;
	Scene^.Stat[ 1 ] := 1;
	GB := RandomMap( Scene );

    ZONE_Map.X := 0;
    ZONE_Map.Y := 0;
    ZONE_Map.W := Game_Screen^.W;
    ZONE_Map.H := Game_Screen^.H;

	{ Clear the overlays... }
	for t := 0 to NumOverlayLayers do ClearOverlayLayer( T );

	{ Copy this map to the overlays... }
	for x := 1 to XMax do begin
		for y := 1 to YMax do begin
			GB^.Map[ X , Y ].Visible := True;
		end;
	end;
	NFDisplayMap( GB );


	OriginX := 80;		{ Set the origin to default value. }
	OriginY := -260;

	{ Next we're going to add some mecha sprites. }
	{ First, locate all the mecha sprite filenames... }
	Sprite_Names := Nil;
	for t := 1 to NumForm do begin
		ExpandFileList( Sprite_Names , Graphics_Directory + FormPat[ T ] );
	end;

	{ Add one sprite per 10x10 area of the map. }
	for t := 0 to 24 do begin
		name := SelectRandomSAtt( Sprite_Names )^.Info;
		SS := ConfirmSprite( name , RandomColorString(CS_PrimaryMecha)+' '+RandomColorString(CS_SecondaryMecha)+' '+RandomColorString(CS_Detailing) , 64 , 64 );
		if ( SS <> Nil ) and ( SS^.Img <> Nil ) then begin
			{ The first part of the name will tell us what }
			{ kind of a mecha we're dealing with. This is }
			{ important to know since we want flyers to fly }
			{ and other types to stay close to the ground. }
			name := copy( name , 1 , 3 );

			X := ( T mod 5 ) * 10 + Random( 10 ) + 1;
			Y := ( T div 5 ) * 10 + Random( 10 ) + 1;

			if (name='ger') or (name='aer') or (name='orn') or (name='hov') then begin
				AddInstantOverlay( X , Y , 5 , OVERLAY_Master , Random( 5 ) , SS );
				AddInstantOverlay( X , Y , TerrMan[ GB^.Map[ X , Y ].terr ].altitude , OVERLAY_Shadow , Default_Shadow , Items_Sprite );
			end else begin
				AddInstantOverlay( X , Y , TerrMan[ GB^.Map[ X , Y ].terr ].altitude , OVERLAY_Master , Random( 5 ) , SS );
				AddInstantOverlay( X , Y , TerrMan[ GB^.Map[ X , Y ].terr ].altitude , OVERLAY_Shadow , Default_Shadow , Items_Sprite );
			end;
		end;
	end;

	{ Finally, get rid of the random map. }
	{ This will also get rid of the attached scene. Bonus! }
	DisposeMap( GB );

	DisposeSAtt( Sprite_Names );

	Opening_Phase := 1;
	Opening_Count := 0;
	Opening_Last_Anim_Phase := Animation_Phase;
end;

Procedure RedrawOpening;
	{ Redraw the screen for the opening display. }
const
	NumLeg = 6;
	Leg_Points: Array [1..NumLeg,1..2] of Point = (
		((X:15;Y:15),(X:25;Y:15)),
		((X:25;Y:15),(X:25;Y:35)),
		((X:25;Y:35),(X:35;Y:35)),
		((X:35;Y:35),(X:35;Y:25)),
		((X:35;Y:25),(X:15;Y:25)),
		((X:15;Y:25),(X:15;Y:15))
	);
var
	X0,Y0,X1,Y1: Integer;
	P: Point;
	Normally_Use_Alpha: Boolean;
begin
    { To start with, recenter the menu zone. }
    {ZONE_TitleScreenMenu.x := Game_Screen^.w div 2 - ZONE_TitleScreenMenu.w div 2;
    ZONE_TitleScreenMenu.y := Game_Screen^.h div 2 - ZONE_TitleScreenMenu.h div 2;}

	if Opening_Last_Anim_Phase <> Animation_Phase then begin
		Opening_Last_Anim_Phase := Animation_Phase;
		Inc( Opening_Count );
		Inc( Opening_Count );

		X0 := ( ZONE_Map.w div 2 ) - RelativeX( Leg_Points[ Opening_Phase , 1 ].X , Leg_Points[ Opening_Phase , 1 ].Y );
		Y0 := ( ZONE_Map.h div 2 ) - RelativeY( Leg_Points[ Opening_Phase , 1 ].X , Leg_Points[ Opening_Phase , 1 ].Y );
		X1 := ( ZONE_Map.w div 2 ) - RelativeX( Leg_Points[ Opening_Phase , 2 ].X , Leg_Points[ Opening_Phase , 2 ].Y );
		Y1 := ( ZONE_Map.h div 2 ) - RelativeY( Leg_Points[ Opening_Phase , 2 ].X , Leg_Points[ Opening_Phase , 2 ].Y );

		P := SolveLine( X0 , Y0 , X1 , Y1 , Opening_Count );

		ORIGINX := P.X;
		ORIGINY := P.Y;

		if ( OriginX = X1 ) and ( OriginY = Y1 ) then begin
			Opening_Phase := Opening_Phase + 1;
			if Opening_Phase > NumLeg then Opening_Phase := 1;
			Opening_Count := 0;
		end;
	end;
	Normally_Use_Alpha := Use_Alpha_Blending;
	Use_Alpha_Blending := False;
	RenderMap;
	Use_Alpha_Blending := Normally_Use_Alpha;
end;

Function ScreenToMap( X,Y: Integer ): Point;
	{ X and Y are screen coordinates. Convert these to map coordinates. }
var
	AX,AY,DX,TX,TY: Integer;	{ Absolute X , Absolute Y }
	P: Point;
begin
	AX := X - ZONE_Map.X - OriginX;
	AY := Y - ZONE_Map.Y - OriginY;

	P.Y := AY div HalfTileHeight - 1;
	P.X := 1;
	AX := AX + ( P.Y - 1 ) * HalfTileWidth;
	DX := AX div ( HalfTileWidth * 2 );

	TX := AX mod ( HalfTileWidth * 2 );
	TY := AY mod HalfTileHeight;

	if ( TX < HalfTileWidth ) and ( TX < ( 31 - 2 * TY ) ) then Dec( P.X )
	else if ( TX > HalfTileWidth ) and ( ( TX - 31 ) > TY ) then Dec( P.Y );

	P.Y := P.Y - DX;
	P.X := P.X + DX;

	ScreenToMap := P;
end;

Function MouseMapPos: Point;
	{ Return the map position of the mouse. }
begin
	MouseMapPos := ScreenToMap( Mouse_X , Mouse_Y );
end;

Procedure BeginTurn( GB: GameBoardPtr; M: GearPtr );
	{ Time to start the turn. }
var
	A: Char;
begin
	SetupMemoDisplay;
	CMessage( 'Begin ' + PilotName( M ) + ' turn' , ZONE_MemoText.GetRect() , InfoHilight );
	GHFlip;
	EndOfGameMoreKey;
end;

Procedure InitMapDisplay( GB: GameBoardPtr; PC: GearPtr );
    { Do anything that needs to be done to prepare this map to be displayed. }
begin
    ComDisplay_PC := PC;
end;

Procedure FinalizeMapDisplay;
    { Do anything that needs to be done after we're finished looking at this map. }
begin
    ComDisplay_PC := Nil;
end;

initialization
	Terrain_Sprite := ConfirmSprite( Terrain_Sprite_Name , '' , 64 , 96 );
	Meta_Terrain_Sprite := ConfirmSprite( Meta_Terrain_Sprite_Name , '' , 64 , 96 );
	Terrain_Toupee_Sprite := ConfirmSprite( Terrain_Toupee_Sprite_Name , '' , 64 , 64 );
	Targeting_Srpite := ConfirmSprite( Targeting_Srpite_Name , '' , 64 , 64 );
	Items_Sprite := ConfirmSprite( Items_Sprite_Name , '' , 64 , 64 );
	Strong_Hit_Sprite := ConfirmSprite( Strong_Hit_Sprite_Name , '' , 64 , 64 );
	Weak_Hit_Sprite := ConfirmSprite( Weak_Hit_Sprite_Name , '' , 64 , 64 );
	Parry_Sprite := ConfirmSprite( Parry_Sprite_Name , '' , 64 , 64 );
	Miss_Sprite := ConfirmSprite( Miss_Sprite_Name , '' , 64 , 64 );

    Water_Sprite1 := ConfirmSprite( 'terrain_water1.png', '', 64, 64 );
    Water_Sprite2 := ConfirmSprite( 'terrain_water2.png', '', 64, 64 );
    SDL_SetAlpha( Water_Sprite1^.Img , SDL_SRCAlpha , 150 );
    SDL_SetAlpha( Water_Sprite2^.Img , SDL_SRCAlpha , 150 );

	Thin_Wall_Sprites[ ThinWall_Earth ] := ConfirmSprite( 'wall_earth.png' , '' , 64 , 96 );
	Thin_Wall_Sprites[ ThinWall_RustySteel ] := ConfirmSprite( 'wall_rustysteel.png' , '' , 64 , 96 );
	Thin_Wall_Sprites[ ThinWall_Stone ] := ConfirmSprite( 'wall_stone.png' , '' , 64 , 96 );
	Thin_Wall_Sprites[ ThinWall_Industrial ] := ConfirmSprite( 'wall_industrial.png' , '' , 64 , 96 );
	Thin_Wall_Sprites[ ThinWall_Residential ] := ConfirmSprite( 'wall_residential.png' , '' , 64 , 96 );
	Thin_Wall_Sprites[ ThinWall_Wood ] := ConfirmSprite( 'wall_wood.png' , '' , 64 , 96 );
	Thin_Wall_Sprites[ ThinWall_Default ] := ConfirmSprite( 'wall_default.png' , '' , 64 , 96 );
	Thin_Wall_Sprites[ ThinWall_Fortress ] := ConfirmSprite( 'wall_fortress.png' , '' , 64 , 96 );

	Thin_Wall_Sprites[ ThinWall_Hospital ] := ConfirmSprite( 'wall_hospital.png' , '' , 64 , 64 );
	Thin_Wall_Sprites[ ThinWall_Golden ] := ConfirmSprite( 'wall_gold.png' , '' , 64 , 64 );
	Thin_Wall_Sprites[ ThinWall_Commercial ] := ConfirmSprite( 'wall_commercial.png' , '' , 64 , 64 );
	Thin_Wall_Sprites[ ThinWall_StainlessSteel ] := ConfirmSprite( 'wall_stainless.png' , '' , 64 , 64 );
	Thin_Wall_Sprites[ ThinWall_Neon ] := ConfirmSprite( 'wall_neon.png' , '' , 64 , 64 );
	Thin_Wall_Sprites[ ThinWall_Restaurant ] := ConfirmSprite( 'wall_restaurant.png' , '' , 64 , 64 );
	Thin_Wall_Sprites[ ThinWall_Garage ] := ConfirmSprite( 'wall_garage.png' , '' , 64 , 64 );
	Thin_Wall_Sprites[ ThinWall_Organic ] := ConfirmSprite( 'wall_organic.png' , '' , 64 , 64 );

	Thin_wall_Cap := ConfirmSprite( 'wall_cap.png' , '' , 64 , 96 );
    {SDL_SetAlpha( Thin_wall_Cap^.Img , SDL_SRCAlpha , 128 );}

	Hill_1 := ConfirmSprite( 'hill_1.png' , '' , 64 , 96 );
	Hill_2 := ConfirmSprite( 'hill_2.png' , '' , 64 , 96 );
	Hill_3 := ConfirmSprite( 'hill_3.png' , '' , 64 , 96 );
	Off_Map_Model_Sprite := ConfirmSprite( 'off_map.png' , '' , 16 , 16 );

    Door_Sprite := ConfirmSprite( 'terrain_door.png', '', 64, 64 );

	Mini_Map_Sprite := ConfirmSprite( 'minimap.png' , '' , 3 , 3 );
	if Use_Alpha_Blending then SDL_SetAlpha( Mini_Map_Sprite^.Img , SDL_SRCAlpha , Alpha_Level );

end.