File: arenahq.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 (1474 lines) | stat: -rw-r--r-- 41,757 bytes parent folder | download | duplicates (3)
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
unit ArenaHQ;

{
	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

{$IFDEF SDLMODE}
uses sdlgfx;
{$ENDIF}

const
	NAV_StartingCash = 250000;	{ This is how much money a unit starts with. }


Procedure CreateNewUnit;
Procedure LoadUnit;

{$IFDEF SDLMODE}
Procedure StartRPGCampaign( RD: RedrawProcedureType );
Procedure DesignDirBrowser( RD: RedrawProcedureType );
{$ELSE}
Procedure StartRPGCampaign;
Procedure DesignDirBrowser;
{$ENDIF}

implementation

{$IFDEF SDLMODE}
uses ability,arenaplay,damage,gears,gearutil,ghchars,ghparser,
     locale,navigate,pcaction,randchar,randmaps,texutil,wmonster,
     sdlinfo,sdlmap,sdlmenus,ui4gh,backpack;
{$ELSE}
uses ability,arenaplay,damage,gears,gearutil,ghchars,ghparser,
     locale,navigate,pcaction,randchar,randmaps,texutil,wmonster,
     coninfo,congfx,conmap,conmenus,context,ui4gh;
{$ENDIF}

{$IFDEF SDLMODE}
var
	HQRD_Redraw: RedrawProcedureType;
    HQRD_Source: GearPtr;
    HQRD_Menu: RPGMenuPtr;
{$ENDIF}

Procedure SaveUnit( U: GearPtr );
	{ Save this unit to disk, in the "SaveGame" directory. }
var
	FName: String;		{ Filename for the character. }
	F: Text;		{ The file to write to. }
begin
	FName := Save_Unit_Base + GearName(U) + Default_File_Ending;
	Assign( F , FName );
	Rewrite( F );
	WriteCGears( F , U );
	Close( F );
end;

Function FindMechasPilot( U , Mek: GearPtr ): GearPtr;
	{ Search unit U to locate whatever pilot is assigned to mecha Mek. }
	{ If no such pilot is found, clear Mek's PILOT attribute and }
	{ return Nil. }
var
	pc,mpc: GearPtr;
	name: String;
begin
	{ Begin by finding the pilot's name. }
	name := SAttValue( Mek^.SA , 'pilot' );

	{ Search through the unit's Sub looking for a character of }
	{ this name. }
	pc := U^.SubCom;
	mpc := Nil;
	while ( pc <> Nil ) and ( mpc = Nil ) do begin
		if pc^.G = GG_Character then begin
			if GearName( PC ) = name then mpc := pc;
		end;
		pc := pc^.Next;
	end;

	{ If the required pilot could not be found, }
	{ delete this mecha's PILOT attribute. }
	if mpc = Nil then begin
		SetSAtt( Mek^.SA , 'pilot <>' );
	end;

	FindMechasPilot := mpc;
end;

Function CreateHQPilotMenu( U: GearPtr ): RPGMenuPtr;
	{ Allocate and fill out a menu containing all the pilots }
	{ which belong to this unit. }
var
	RPM: RPGMenuPtr;
	P: GearPtr;
	N: Integer;
	msg: String;
begin
{$IFNDEF SDLMODE}
	RPM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_HQPilots );
{$ENDIF}
	{ Add an entry for each pilot. }
	P := U^.SubCom;
	N := 1;
	while P <> Nil do begin
		msg := GearName( P );
		if FindPilotsMecha( U^.InvCom , P ) <> Nil then msg := msg + ' +';
		AddRPGMenuItem( RPM , msg , N );
		Inc( N );
		P := P^.Next;
	end;

	{ Return the finished menu. }
	CreateHQPilotMenu := RPM;
end;

Function CreateHQMechaMenu( U: GearPtr ): RPGMenuPtr;
	{ Allocate and fill out a menu containing all the pilots }
	{ which belong to this unit. }
var
	RPM: RPGMenuPtr;
	P,Pilot: GearPtr;
	N: Integer;
	msg: String;
begin
{$IFNDEF SDLMODE}
	RPM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_HQMecha );
{$ENDIF}
	{ Add an entry for each mek. }
	P := U^.InvCom;
	N := 1;
	while P <> Nil do begin
		{ Only add mechas to the menu - not items or salvage. }
		if P^.G = GG_Mecha then begin
			msg := GearName( P );
			Pilot := FindMechasPilot( U , P );
			if Pilot <> Nil then msg := msg + ' (' + GearName( Pilot ) + ')';
			AddRPGMenuItem( RPM , msg , N );
		end;
		Inc( N );
		P := P^.Next;
	end;

	{ Return the finished menu. }
	CreateHQMechaMenu := RPM;
end;

Procedure UpdateHQDisplay( U: GearPtr );
	{ Redraw the pilot and mecha lists. }
var
	DSM: RPGMenuPtr;	{ Display Menu }
begin
	DSM := CreateHQPilotMenu( U );
{$IFNDEF SDLMODE}
	if DSM^.NumItem > 0 then DisplayMenu( DSM )
	else ClrZone(ZONE_HQPilots);
{$ENDIF}
	DisposeRPGMenu( DSM );

	DSM := CreateHQMechaMenu( U );
{$IFNDEF SDLMODE}
	if DSM^.NumItem > 0 then DisplayMenu( DSM )
	else ClrZone(ZONE_HQMecha);
{$ENDIF}
	DisposeRPGMenu( DSM );

	{ Display how many credits the unit has. }
	ClrZone( ZONE_Clock );
	CMessage( '$' + BStr( NAttValue( U^.NA , NAG_Experience , NAS_Credits ) ) , ZONE_Clock , PlayerBlue );
end;

procedure PurchaseGear( U,Part: GearPtr );
	{ The unit may or may not want to buy PART. }
	{ Show the price of this gear, and ask whether or not the }
	{ player wants to make this purchase. }
var
	YNMenu: RPGMenuPtr;
	Cost, ShopRk: LongInt;
begin
	Cost := GearValue( Part );
	ShopRk := SkillValue( U , 21 );

	if ShopRk > 10 then begin
		{ Every point of shopping skill that the unit has over 10 }
		{ gives a 2% discount to whatever is being purchased. }
		ShopRk := ( ShopRk - 10 ) * 2;
		if ShopRk > 40 then ShopRk := 40;

		Cost := ( Cost * (100 - ShopRk ) ) div 100;
	end;
	if Cost < 1 then Cost := 1;

	YNMenu := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu2 );
	AddRPGMenuItem( YNMenu , 'Buy ' + GearName( Part ) + ' ($' + BStr( Cost ) + ')' , 1 );
	AddRPGMenuItem( YNMenu , 'Search Again' , -1 );

{$IFDEF SDLMODE}
	if SelectMenu( YNMenu , Nil ) = 1 then begin
{$ELSE}
	CMessage( 'COST: ' + BStr( Cost ) , ZONE_Menu1 , InfoHilight );
	if SelectMenu( YNMenu ) = 1 then begin
{$ENDIF}
		if NAttValue( U^.NA , NAG_Experience , NAS_Credits ) >= Cost then begin
			{ Copy the gear, then stick it in inventory. }
			Part := CloneGear( Part );
			InsertInvCom( U , Part );

			{ Reduce the buyer's cash by the cost of the gear. }
			AddNAtt( U^.NA , NAG_Experience , NAS_Credits , -Cost );

			{ Update the display. }
			UpdateHQDisplay( U );

			DialogMSG( 'You have purchased ' + GearName( Part ) + '.' );
		end else begin
			{ Not enough cash to buy... }
			DialogMSG( 'You don''t have enough money to buy ' + GearName( Part ) + '.' );
		end;

	end;

	DisposeRPGMenu( YNMenu );
end;

procedure SellGear( U,Part: GearPtr );
	{ The unit may or may not want to sell PART. }
	{ Shothe price of this gear, and ask whether or not the }
	{ player wants to make this purchase. }
var
	YNMenu: RPGMenuPtr;
	Cost, ShopRk: LongInt;
begin
	Cost := ( GearValue( Part ) * PercentDamaged( Part ) ) div 100;
	if Destroyed( Part ) then Cost := Cost div 3;
	ShopRk := SkillValue( U , 21 );

	if ShopRk > 10 then begin
		{ Every point of shopping skill that the unit has }
		{ gives a 1% bonus to the money gained. }
		if ShopRk > 40 then ShopRk := 40;

		Cost := ( Cost * (20 + ShopRk ) ) div 100;
	end;
	if Cost < 1 then Cost := 1;

	YNMenu := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu2 );
	AddRPGMenuItem( YNMenu , 'Sell ' + GearName( Part ) + ' ($' + BStr( Cost ) + ')' , 1 );
	AddRPGMenuItem( YNMenu , 'Search Again' , -1 );

{$IFDEF SDLMODE}
	if SelectMenu( YNMenu , Nil ) = 1 then begin
{$ELSE}
	CMessage( 'VALUE: ' + BStr( Cost ) , ZONE_Menu1 , InfoHilight );
	if SelectMenu( YNMenu ) = 1 then begin
{$ENDIF}
		{ Increase the buyer's cash by the price of the gear. }
		AddNAtt( U^.NA , NAG_Experience , NAS_Credits , Cost );

		{ Update the display. }
		UpdateHQDisplay( U );

		DialogMSG( 'You have sold ' + GearName( Part ) + ' for $' + BStr( Cost ) + '.' );

		RemoveGear( Part^.Parent^.InvCom , Part );
	end;

	DisposeRPGMenu( YNMenu );
end;

Function RecurseFix( U,Part: GearPtr ): Integer;
	{ Recurse through the bits of the PART, looking for broken }
	{ things. }
var
	SPart: GearPtr;
	Roll,LowRoll: Integer;
begin
	LowRoll := PitFix( Part , U );

	SPart := Part^.SubCom;
	while SPart <> Nil do begin
		Roll := RecurseFix( U , SPart );
		if Roll < LowRoll then LowRoll := Roll;
		SPart := SPart^.Next;
	end;

	SPart := Part^.InvCom;
	while SPart <> Nil do begin
		Roll := RecurseFix( U , SPart );
		if Roll < LowRoll then LowRoll := Roll;
		SPart := SPart^.Next;
	end;

	RecurseFix := LowRoll;
end;

Procedure FixEntireUnit( U: GearPtr );
	{ Attempt to repair everything in the unit. }
var
	Part: GearPtr;
	C0,C1: LongInt;		{ Cash at start. }
	R,LowRoll: Integer;	{ The worst repair result generated. }
begin
	C0 := NAttValue( U^.NA , NAG_Experience , NAS_Credits );
	LowRoll := 10;

	{ Administer medical treatment to all characters. }
	Part := U^.SubCom;
	while Part <> Nil do begin
		R := RecurseFix( U , Part );
		if R < LowRoll then LowRoll := R;
		Part := Part^.Next;
	end;

	{ Administer repair to all assigned mecha. }
	Part := U^.InvCom;
	while Part <> Nil do begin
		if ( Part^.G = GG_Mecha ) and ( FindMechasPilot( U , Part ) <> Nil ) then begin
			R := RecurseFix( U , Part );
			if R < LowRoll then LowRoll := R;
		end;
		Part := Part^.Next;
	end;

	C1 := NAttValue( U^.NA , NAG_Experience , NAS_Credits );
	if C1 < C0 then begin
		DialogMSG( 'Recovery from the combat cost $' + BStr( C0 - C1 ) + '.' );
	end;
	if LowRoll < 1 then begin
		DialogMSG( 'There have been some problems...' );
	end;
end;

Procedure FixSingleGear( U,Mek: GearPtr );
	{ Attempt to repair everything in the unit. }
var
	C0,C1: LongInt;		{ Cash at start. }
	Roll: Integer;
begin
	C0 := NAttValue( U^.NA , NAG_Experience , NAS_Credits );

	Roll := RecurseFix( U , Mek );

    {$IFNDEF SDLMODE}
	DisplayGearInfo( Mek );
    {$ENDIF}

	C1 := NAttValue( U^.NA , NAG_Experience , NAS_Credits );
	if C1 < C0 then begin
		DialogMSG( 'Restoring ' + GearName( Mek ) + ' cost $' + BStr( C0 - C1 ) + '.' );
	end;
	if Roll < 1 then begin
		DialogMSG( 'There have been some problems...' );
	end;
end;

procedure AddPilotToUnit( U: GearPtr );
	{ Browse the disk for a character file. If one is selected, }
	{ display the character's stats and ask whether or not to hire }
	{ this character. If hired, add the character to the unit, }
	{ save the game, then delete the character's individual file. }
var
	PC,Temp: GearPtr;
	PCMenu,YNMenu: RPGMenuPtr;
	F: Text;
	FName: String;
begin
	{ Create the YNMenu here. It'll be the same throughout the }
	{ hiring process. }
	YNMenu := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu );
	AddRPGMenuItem( YNMenu , 'Hire Character' , 1 );
	AddRPGMenuItem( YNMenu , 'Search Again' , -1 );

	DialogMSG('Select character file.');

	{ Keep querying for characters until cancel is selected. }
	repeat
		{ Create the PC menu. }
		PCMenu := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu );
		BuildFileMenu( PCMenu , Save_Character_Base + Default_Search_Pattern );
		RPMSortAlpha( PCMenu );
		AddRPGMenuItem( PCMenu , '  Exit' , -1 );

		{ Select a file, then dispose of the menu. }
		{ Don't need to worry about the menu being empty because }
		{ of the EXIT item. }
{$IFDEF SDLMODE}
		FName := SelectFile( PCMenu , Nil );
{$ELSE}
		FName := SelectFile( PCMenu );
{$ENDIF}
		DisposeRPGMenu( PCMenu );

		{ If a file was selected, load it and see if the player }
		{ wants to keep it. }
		if FName <> '' then begin
			{ Load the character file. }
			Assign( F , Save_Game_Directory + FName );
			reset(F);
			PC := ReadCGears(F);
			Close(F);

			{ ERROR CHECK - make sure the file that was loaded }
			{ is in fact a valid, singular character. }
			if ( PC <> Nil ) then begin
				{ Display the character's stats. }

				{ Ask the player what to do with this character. }
{$IFDEF SDLMODE}
				if SelectMenu( YNMenu , Nil ) = 1 then begin
{$ELSE}
				DisplayGearInfo( PC );
				if SelectMenu( YNMenu ) = 1 then begin
{$ENDIF}
					{ Add the character to the unit. }
					while PC <> Nil do begin
						Temp := PC;
						DelinkGear( PC , Temp );

						if Temp^.G = GG_Character then begin
							InsertSubCom( U , Temp );
						end else begin
							InsertInvCom( U , Temp );
						end;
					end;

					{ Saving the game is done before deleting }
					{ the character file so that if there's a }
					{ problem in saving, at least the original }
					{ character file will be intact. }
					SaveUnit( U );
					Assign( F , Save_Game_Directory + FName );
					Erase(F);

					UpdateHQDisplay( U );
				end else begin
					{ Just get rid of the character. }
					DisposeGear( PC );
				end;
                {$IFNDEF SDLMODE}
				ClrZone( ZONE_Info );
                {$ENDIF}

			end else begin
				{ PC isn't a valid character. Get rid of it. }
				DialogMSG( 'ERROR - Corrupt save file.' );
				DisposeGear( PC );
			end;
		end;
	until FName = '';

	{ Get rid of the Yes/No menu. }
	DisposeRPGMenu( YNMenu );
end;

Function SelectOneGear( List: GearPtr ): GearPtr;
	{ Choose one of the sibling gears from LIST. }
var
	BrowseMenu: RPGMenuPtr;
	Part: GearPtr;
	N: Integer;
	msg: String;
begin
	{ Create the menu. }
	BrowseMenu := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu );

	{ Add each of the gears to the menu. }
	Part := List;
	N := 1;
	while Part <> Nil do begin
		msg := SAttValue( Part^.SA , 'desig' );
		if msg <> '' then msg := msg + ' ' + GearName( Part )
		else msg := GearName( Part );
		AddRPGMenuItem( BrowseMenu , msg , N );
		Inc( N );
		Part := Part^.Next;
	end;
	RPMSortAlpha( BrowseMenu );
	AddRPGMenuItem( BrowseMenu , '  Cancel' , -1 );

	{ Select a gear. }
{$IFDEF SDLMODE}
	N := SelectMenu( BrowseMenu, Nil );
{$ELSE}
	N := SelectMenu( BrowseMenu );
{$ENDIF}
	DisposeRPGMenu( BrowseMenu );
	SelectOneGear := RetrieveGearSib( List , N );
end;

procedure BuyMechsForUnit( U: GearPtr );
	{ Create a list of mecha which are within this unit's price }
	{ range, then allow the user to browse the list and maybe }
	{ purchase some. }
var
	MekMenu: RPGMenuPtr;
	fname: String;
	m1,mek: GearPtr;	{ The start of the mecha file, }
			{ and the mek being considered for purchase. }
	F: Text;
begin
	{ Create the mecha menu. }
	MekMenu := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu );
	BuildFileMenu( MekMenu , Design_Directory + Default_Search_Pattern );
	RPMSortAlpha( MekMenu );
	AddRPGMenuItem( MekMenu , '  Exit' , -1 );

	DialogMSG( 'Select design file.' );

	repeat
		{ Prompt the user for a file selection. }
{$IFDEF SDLMODE}
		fname := SelectFile( MekMenu , Nil );
{$ELSE}
		fname := SelectFile( MekMenu );
{$ENDIF}

		if fname <> '' then begin
			{ Load this design file, then allow the player }
			{ to select any of the gears it contains. }
			Assign(F, Design_Directory + fname );
			reset(F);
			m1 := ReadGear(F);
			Close(F);

			{ Error check- make sure something was actually loaded. }
			if ( m1 <> Nil ) then begin
				{ If there were multiple designs in this file, }
				{ allow the player to browse through them. }
				{ If there was only one design, leap straight to it. }
				if M1^.Next = Nil then Mek := M1
				else Mek := SelectOneGear( M1 );

				{ Check to make sure that Mek isn't Nil. }
				if Mek <> Nil then begin
                    {$IFNDEF SDLMODE}
					DisplayGearInfo( Mek );
                    {$ENDIF}

					PurchaseGear( U , Mek );

					{ Update the display. }
					UpdateHQDisplay( U );
				end;

				DisposeGear( m1 );
			end else begin
				DialogMsg( 'ERROR - Corrupt design file.' );
			end;
		end;
	until fname = '';

	{ Get rid of dynamic resources. }
	DisposeRPGMenu( MekMenu );
end;

procedure ExamineUnitMecha( U: GearPtr );
	{ Allow the player to browse through the unit's mechas; }
	{ selecting a mecha will bring up its info display and }
	{ a sub-menu allowing the mek to be repaired, sold, or }
	{ assigned to a pilot. }
	Procedure GetPilotForMek( M: GearPtr );
		{ Select a pilot for this mecha, then associate the two. }
	var
		PMenu: RPGMenuPtr;
		N: Integer;
	begin
{$IFNDEF SDLMODE}
		CMessage( 'SELECT CHARACTER' , ZONE_Menu1 , InfoHilight );
		DialogMSG( 'Select a pilot for ' + GearName( M ) + '.' );
{$ENDIF}
		PMenu := CreateHQPilotMenu( U );
		if PMenu^.NumItem > 0 then begin
{$IFDEF SDLMODE}
			N := SelectMenu( PMenu , Nil );
{$ELSE}
			N := SelectMenu( PMenu );
{$ENDIF}
			if N <> -1 then begin
				AssociatePilotMek( U^.InvCom , RetrieveGearSib( U^.SubCom , N ) , M );
			end;
		end;
		DisposeRPGMenu( PMenu );

		{ Update the display. }
		UpdateHQDisplay( U );
	end;
var
	MekMenu,OpMenu: RPGMenuPtr;
	Mek: GearPtr;
	MN,N: Integer;
begin
	{ Create the needed menus. }
	MekMenu := CreateHQMechaMenu( U );
	OpMenu := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu2 );
	AddRPGMenuItem( OpMenu , 'Assign Pilot' , 1 );
	AddRPGMenuItem( OpMenu , 'Sell this Mecha' , -2 );
	AddRPGMenuItem( OpMenu , 'Repair Mecha' , 3 );	
	AddRPGMenuItem( OpMenu , 'Exit' , -1 );

	{ Error check- this unit better have some meks purchased already. }
	if MekMenu^.NumItem > 0 then begin
		MN := 1;
		repeat
{$IFNDEF SDLMODE}
			CMessage( 'SELECT  MECHA  TO  EXAMINE' , ZONE_Menu1 , MenuSelect );
			DrawZoneBorder( ZONE_Menu2 , PlayerBlue );
{$ENDIF}

			{ MN stands for Mek Number. }
			SetItemByValue( MekMenu , MN );
{$IFDEF SDLMODE}
			MN := SelectMenu( MekMenu , Nil );
{$ELSE}
			MN := SelectMenu( MekMenu );
{$ENDIF}

			{ If a mek was selected, go to the options menu. }
			if MN <> -1 then begin
				{ Find out what mek the player selected, }
				{ and display its info. }
				Mek := RetrieveGearSib( U^.InvCom , MN );
{$IFNDEF SDLMODE}
				DisplayGearInfo( Mek );

				{ Restore the display. }
				UpdateHQDisplay( U );
{$ENDIF}
				{ Bring up the options menu. }
				SetItemByValue( OpMenu , 1 );
				repeat
{$IFDEF SDLMODE}
					N := SelectMenu( OpMenu , Nil );
{$ELSE}
					ClrZone( ZONE_Menu1 );
					CMessage( GearName( Mek ) , ZONE_Menu1 , InfoHilight );
					N := SelectMenu( OpMenu );
{$ENDIF}
					if N = 1 then GetPilotForMek( Mek )
					else if N = -2 then SellGear( U , Mek )
					else if N = 3 then FixSingleGear( U , Mek );
				until N < 0;

				{ Refresh the mecha menu. }
				DisposeRPGMenu( MekMenu );
				MekMenu := CreateHQMechaMenu( U );

			end;

		until ( MN = -1 ) or ( MekMenu^.NumItem = 0 );

		{ Restore the display. }
		UpdateHQDisplay( U );
	end else begin
		DialogMSG( 'Your unit does not currently have any meks.' );
	end;

	{ Free dynamic resources. }
	DisposeRPGMenu( MekMenu );
	DisposeRPGMenu( OpMenu );
end;

procedure ExamineUnitPilots( U: GearPtr );
	{ Allow the player to browse through the unit's characters; }
	{ selecting a char will bring up its info display and a }
	{ sub-menu allowing the pilot to be removed from the unit, }
	{ treated for injuries / status conditions, assigned a mecha }
	{ to use in combat, or trained. }
	Procedure GetMekForPilot( P: GearPtr );
		{ Select a pilot for this mecha, then associate the two. }
	var
		MekMenu: RPGMenuPtr;
		N: Integer;
	begin
{$IFNDEF SDLMODE}
		CMessage( 'SELECT MECHA' , ZONE_Menu1 , InfoHilight );
		DialogMSG( 'Select a mecha for ' + GearName( P ) + '.' );
{$ENDIF}
		MekMenu := CreateHQMechaMenu( U );
		if MekMenu^.NumItem > 0 then begin
{$IFDEF SDLMODE}
			N := SelectMenu( MekMenu , Nil );
{$ELSE}
			N := SelectMenu( MekMenu );
{$ENDIF}
			if N <> -1 then begin
				AssociatePilotMek( U^.InvCom , P , RetrieveGearSib( U^.InvCom , N ) );
			end;
		end;
		DisposeRPGMenu( MekMenu );

		{ Update the display. }
		UpdateHQDisplay( U );
	end;

	Procedure QuitUnit( PC: GearPtr );
		{ This character wants to quit. Make it so. }
	begin
		DelinkGear( U^.SubCom , PC );
		SaveChar( PC );
		SaveUnit( U );
		DisposeGear( PC );
	end;

	Procedure ViewBiography( PC: GearPtr );
		{ Display the biography text which was generated for }
		{ this character. }
	var
		msg: String;
	begin
		msg := SAttValue( PC^.SA , 'Bio1' );
{$IFNDEF SDLMODE}
		CMessage( 'BIOGRAPHY' , ZONE_Menu1 , InfoHilight );
		GameMsg( msg , ZONE_Menu2 , InfoGreen );
{$ENDIF}
		{ Wait for a keypress before exiting. }
		RPGKey;
	end;
var
	PCMenu,OpMenu: RPGMenuPtr;
	PC: GearPtr;
	PN,N: Integer;
begin
	{ Create the needed menus. }
	PCMenu := CreateHQPilotMenu( U );
	OpMenu := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu2 );
	AddRPGMenuItem( OpMenu , 'View Biography' , 3 );
	AddRPGMenuItem( OpMenu , 'Assign Mecha for Pilot' , 1 );
	AddRPGMenuItem( OpMenu , 'Do Training' , 2 );
	AddRPGMenuItem( OpMenu , 'Quit This Team' , -2 );
	AddRPGMenuItem( OpMenu , 'Exit' , -1 );

	{ Error check- this unit better have some chars hired already. }
	if PCMenu^.NumItem > 0 then begin
		PN := 1;
		repeat
{$IFNDEF SDLMODE}
			CMessage( 'SELECT  CHARACTER  TO  EXAMINE' , ZONE_Menu1 , MenuSelect );
			DrawZoneBorder( ZONE_Menu2 , PlayerBlue );
{$ENDIF}

			{ PN stands for PC Number. }
			SetItemByValue( PCMenu , PN );
{$IFDEF SDLMODE}
			PN := SelectMenu( PCMenu , Nil );
{$ELSE}
			PN := SelectMenu( PCMenu );
{$ENDIF}

			{ If a char was selected, go to the options menu. }
			if PN <> -1 then begin
				{ Find out what PC the player selected, }
				{ and display its info. }
				PC := RetrieveGearSib( U^.SubCom , PN );
{$IFNDEF SDLMODE}
				DisplayGearInfo( PC );

				{ Restore the display. }
				UpdateHQDisplay( U );
				ClrZone( ZONE_Menu1 );
{$ENDIF}

				{ Bring up the options menu. }
				SetItemByValue( OpMenu , 3 );
				repeat
{$IFDEF SDLMODE}
					N := SelectMenu( OpMenu , Nil );
{$ELSE}
					CMessage( GearName( PC ) , ZONE_Menu1 , InfoHilight );
					N := SelectMenu( OpMenu );
{$ENDIF}
					Case N of
						-2: QuitUnit( PC );
						1: GetMekForPilot( PC );
						2: DoTraining( Nil , PC );
						3: ViewBiography( PC );
					end;
				until N < 0;

				{ Refresh the pilots menu. }
				DisposeRPGMenu( PCMenu );
				PCMenu := CreateHQPilotMenu( U );

			end;

		until ( PN = -1 ) or ( PCMenu^.NumItem = 0 );

		{ Restore the display. }
		UpdateHQDisplay( U );
	end else begin
		DialogMSG( 'Your unit does not currently have any characters.' );
	end;

	{ Free dynamic resources. }
	DisposeRPGMenu( PCMenu );
	DisposeRPGMenu( OpMenu );
end;

procedure EnterCombat( HQCamp: CampaignPtr );
	{ This is the HQ combat wrapper. Prompt for mission difficulcy, }
	{ then select the Meks/Pilots who will be doing the mission. }
	{ Extract meks/pilots from the unit. }
	{ Deploy them on the map. }
	{ Select enemy forces based upon difficulcy level selected. }
	{ Call the combat procedure. }
	{ Disassemble the gameboard meks list. }
	{ Strip all location, scenario, weaponinfo NAtts. }
	{ Decide the fate of all characters and mechs; delete dead chars. }
	{ Insert surviving PCs and salvage into the unit. }
	{ Deallocate NPCs and wasted meks. }
	{ Save the game. }
const
	TPV_MAX = 2147483647;
	TPV_MIN = -2147483648;
var
	ECM: RPGMenuPtr;	{ Enter Combat Menu }
	Diff: Integer;		{ Difficulcy Level }
	Outcome: Integer;	{ Who won the battle? }
	Mek: GearPtr;		{ Mecha Pointer of Many Uses }
	Pilot: GearPtr;
	MList: GearPtr;		{ The list of meks which will take part }
	N: Integer;		{ A menu input code }
	msg: String;
	TPV: Int64;
	SA: SAttPtr;
	XPV: Integer;
begin
	{ Create the difficulcy selector menu. }
	ECM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu2 );
{$IFNDEF SDLMODE}
	CMessage( 'SELECT DIFFICULCY LEVEL' , ZONE_Menu1 , InfoGreen );
{$ENDIF}
	AddRPGMenuItem( ECM , 'Easy' , 1 );
	AddRPGMenuItem( ECM , 'Regular' , 3 );
	AddRPGMenuItem( ECM , 'Hard' , 6 );
	AddRPGMenuItem( ECM , 'Suicidal' , 10 );

	{ Input the difficulcy level, and dispose of the menu right away. }
{$IFDEF SDLMODE}
	Diff := SelectMenu( ECM , Nil );
{$ELSE}
	Diff := SelectMenu( ECM );
{$ENDIF}
	DisposeRPGMenu( ECM );
{$IFNDEF SDLMODE}
	ClrZone( ZONE_Menu1 );
{$ENDIF}

	{ If the selection was not cancelled, continue with the procedure. }
	if Diff = -1 then exit;

	{ Select the list of mechas to use on this mission. }
{$IFNDEF SDLMODE}
	CMessage( 'SELECT MECHA' , ZONE_Menu1 , InfoGreen );
{$ENDIF}
	MList := Nil;
	repeat
		{ Create the mecha menu. This has to be re-created with }
		{ each iteration, since there are mecha which will be }
		{ extracted from the primary list. }
		ECM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu2 );
		Mek := HQCamp^.Source^.InvCom;
		N := 1;
		while Mek <> Nil do begin
			if ( Mek^.G = GG_Mecha ) and ( FindMechasPilot( HQCamp^.Source , Mek ) <> Nil ) then begin
				msg := GearName( Mek ) + ' (' + GearName( FindMechasPilot( HQCamp^.Source , Mek ) ) + ')';
				AddRPGMenuItem( ECM , msg , N );
			end;
			Mek := Mek^.Next;
			Inc( N );
		end;

		{ Get input from the menu, if there are any mechas left. }
		if ECM^.NumItem > 0 then begin
{$IFDEF SDLMODE}
			N := SelectMenu( ECM , Nil );
{$ELSE}
			N := SelectMenu( ECM );
{$ENDIF}
			if N > -1 then begin
				Mek := RetrieveGearSib( HQCamp^.Source^.InvCom , N );
				DelinkGear( HQCamp^.Source^.InvCom , Mek );
				Mek^.Next := MList;
				MList := Mek;
			end;
		end else N := -1;

		DisposeRPGMenu( ECM );
	until N = -1;

	{ If no mechas were selected for the mission, exit. }
	if MList = Nil then exit;

	{ Generate a random scenario. }
	HQCamp^.GB := RandomMap( Nil );

	{ Deploy the chosen mechas, along with their pilots, into the }
	{ scenario. }
	TPV := 0;
	while MList <> Nil do begin
		Mek := MList;
		MList := MList^.Next;
		Mek^.Next := Nil;

		TPV := TPV + GearValue( Mek );
		Pilot := FindMechasPilot( HQCamp^.Source , Mek );
		DelinkGear( HQCamp^.Source^.SubCom , Pilot );
		DeployMek( HQCamp^.GB , Mek , Pilot , NAV_DefPlayerTeam );
	end;
	if TPV < TPV_MIN then begin
		TPV := TPV_MIN;
	end else if TPV_MAX < TPV then begin
		TPV := TPV_MAX;
	end;

	{ Add a number of random enemies to the scenario. }
	{ Generate a shopping list of mecha found in the Design/ drawer. }
	{ Meks with Point Values larger than the value provided will be }
	{ filtered out. }
	SA := GenerateMechaList( TPV div 2 );

	{ Determine how many points of mecha to buy. This is determined by the }
	{ point value of the player meks (TPV) and the difficulcy level }
	{ selected (Diff). Yes, I'm reassigning TPV to now represent the enemy }
	{ point value... bad programming style. }
	TPV := ( TPV * Diff ) div 2;
	if TPV < TPV_MIN then begin
		TPV := TPV_MIN;
	end else if TPV_MAX < TPV then begin
		TPV := TPV_MAX;
	end;

	{ Call the SelectEnemyForces procedure from ArenaPlay. This will }
	{ choose mecha designs from the list generated & give pilots to them. }
	SelectEnemyForces( HQCamp^.GB , SA , TPV );

	{ Get rid of the mecha shopping list, since we don't need it any more. }
	DisposeSAtt( SA );

	{ Call the combat procedure. }
	Outcome := CombatMain( HQCamp );
	rpgkey;

	{ Determine the base number of XPs to give survivors. }
	if Outcome > 0 then XPV := 20 + ( Diff * 5 )
	else if Outcome = 0 then XPV := 0
	else XPV := 7 + ( Diff * 3 );

	{ Disassemble the gameboard's mecha list, sticking all the player }
	{ meks and characters back into their proper slots. }
	MList := HQCamp^.GB^.Meks;
	HQCamp^.GB^.Meks := Nil;

	{ Process each mek in turn. }
	{ While sorting through all the meks and pilots that took part }
	{ in this battle, also give out experience to the player's characters. }
	while MList <> Nil do begin
		Mek := MList;
		DelinkGear( MList , Mek );

		if Mek^.G = GG_Mecha then begin
			if NAttValue( Mek^.NA , NAG_Location , NAS_Team ) = NAV_DefPlayerTeam then begin
				repeat
					Pilot := ExtractPilot( Mek );
					if Pilot <> Nil then begin

						if NotDestroyed( Pilot ) then begin
							StripNAtt( Pilot , -1 );
							StripNAtt( Pilot , -2 );
							StripNAtt( Pilot , -3 );
							StripNAtt( Pilot , -5 );
							StripNAtt( Pilot , NAG_EpisodeData );
							InsertSubCom( HQCamp^.Source , Pilot );

							{ Give XP for successful mission. }
							if NotDestroyed( Mek ) then begin
								DoleExperience( Pilot , XPV );
							end else begin
								DoleExperience( Pilot , XPV div 2 );
							end;
						end else begin
							{ The pilot died. Perform a decent burial/deallocation. }
							DisposeGear( Pilot );

							{ Set PILOT to be equal to MEK, since we don't want the loop to exit right now. }
							Pilot := Mek;
						end;
					end;
				until Pilot = Nil;

				if Destroyed( Mek ) and ( Outcome < 1 ) then begin
					{ If the mek was destroyed, and the player didn't win the game, }
					{ this mek is captured and lost. }
					DisposeGear( Mek );
				end else if NotDestroyed( Mek ) or ( PercentDamaged( Mek ) > 50 ) then begin
					StripNAtt( Mek , -1 );
					StripNAtt( Mek , -2 );
					StripNAtt( Mek , -3 );
					StripNAtt( Mek , -5 );
					StripNAtt( Mek , NAG_EpisodeData );

					InsertInvCom( HQCamp^.Source , Mek );
				end else begin
					DisposeGear( Mek );
				end;
			end else if Outcome = 1 then begin
				{ The player won the game, so he may take }
				{ this mecha as salvage. }
				if OnTheMap( Mek ) and (( PercentDamaged( Mek ) > 75 ) or NotDestroyed( Mek ) ) then begin
					{ Remove the enemy pilot, of course. }
					repeat
						Pilot := ExtractPilot( Mek );
						if Pilot <> Nil then begin
							DisposeGear( Pilot );
							{ Set PILOT to be equal to MEK, since we don't want the loop to exit right now. }
							Pilot := Mek;
						end;
					until Pilot = Nil;

					StripNAtt( Mek , -1 );
					StripNAtt( Mek , -2 );
					StripNAtt( Mek , -3 );
					StripNAtt( Mek , -5 );
					StripNAtt( Mek , NAG_EpisodeData );

					InsertInvCom( HQCamp^.Source , Mek );
				end else DisposeGear( Mek );
			end else DisposeGear( Mek );

		end else if Mek^.G = GG_Character then begin
			if NAttValue( Mek^.NA , NAG_Location , NAS_Team ) = NAV_DefPlayerTeam then begin
				if NotDestroyed( Mek ) then begin
					StripNAtt( Mek , -1 );
					StripNAtt( Mek , -2 );
					StripNAtt( Mek , -3 );
					StripNAtt( Mek , -5 );
					StripNAtt( Mek , NAG_EpisodeData );
					InsertSubCom( HQCamp^.Source , Mek );

					{ Give experience, which is reduced if the pilot had to eject. }
					DoleExperience( Mek , XPV div 2 );
				end else DisposeGear( Mek );

			end else DisposeGear( Mek );
		end else begin
			DisposeGear( Mek );
		end;
	end;

	{ Now that the meks are out, dispose of the scenario itself. }
	DisposeMap( HQCamp^.GB );

	{ Pay the unit for their actions. }
	if Outcome > 0 then TPV := TPV div 50
	else if Outcome = 0 then TPV := 0
	else TPV := TPV div 150;
	AddNAtt( HQCamp^.Source^.NA , NAG_Experience , NAS_Credits , TPV );

	{ Save the game, and update the display. }
	SaveUnit( HQCamp^.Source );

	{ Repair all meks and treat all wounded pilots. }
	SetupHQDisplay;
	DialogMSG( 'You earned $' + BStr(TPV) + ' for this mission.' );
	FixEntireUnit( HQCamp^.Source );
end;

procedure HQMain( HQCamp: CampaignPtr );
	{ This is the central headquarters procedure. From here }
	{ everything else can be branched to. }
var
	RPM: RPGMenuPtr;
	n: Integer;
begin
	{ Create the HQ Menu }
	RPM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu );
	AddRPGMenuItem( RPM , 'Examine Characters' , 5 );
	AddRPGMenuItem( RPM , 'Examine Mecha' , 1 );
	AddRPGMenuItem( RPM , 'Purchase Hardware' , 2 );
	AddRPGMenuItem( RPM , 'Hire Character' , 3 );
	AddRPGMenuItem( RPM , 'Create New Character' , 4 );
	AddRPGMenuItem( RPM , 'Enter Combat' , 6 );
	AddRPGMenuItem( RPM , 'Exit to Main' , 0 );
	RPM^.mode := RPMNoCancel;

	{ Set up the display. }
	SetupHQDisplay;

	{ Keep processing until an EXIT is encountered. }
	repeat
		UpdateHQDisplay( HQCamp^.Source );
{$IFDEF SDLMODE}
		N := SelectMenu( RPM , Nil );
{$ELSE}
		N := SelectMenu( RPM );
{$ENDIF}

		case N of
			5: ExamineUnitPilots( HQCamp^.Source );
			1: ExamineUnitMecha( HQCamp^.Source );
			2: BuyMechsForUnit( HQCamp^.Source );
			3: AddPilotToUnit( HQCamp^.Source );
			4: GenerateNewPC;
			6: EnterCombat( HQCamp );
			0: SaveUnit( HQCamp^.Source );
		end;
	until N <= 0;

	{ Free all dynamic resources. }
	DisposeRPGMenu( RPM );
	DisposeCampaign( HQCamp );
end;

Procedure CreateNewUnit;
	{ Start a new unit from scratch. Give it however much starting }
	{ cash and head on up to the HQMain procedure. }
var
	HQCamp: CampaignPtr;
	Name: String;
begin
	HQCamp := NewCampaign;
	HQCamp^.Source := NewGear( Nil );
	HQCamp^.Source^.G := GG_Unit;
	Name := 'New Unit';
	SetNAtt( HQCamp^.Source^.NA , NAG_Experience , NAS_Credits , NAV_StartingCash );

{$IFDEF SDLMODE}
	Name := GetStringFromUser( 'enter a name for your new unit' , Nil );
{$ELSE}
	Name := GetStringFromUser( 'enter a name for your new unit' );
{$ENDIF}
	if Name <> '' then begin
		SetSAtt( HQCamp^.Source^.SA , 'name <'+name+'>');
		SaveUnit( HQCamp^.Source );
		HQMain( HQCamp );

	end else DisposeCampaign( HQCamp );
end;

Procedure LoadUnit;
	{ Select a previously saved unit from the menu. If no unit is }
	{ found, jump to the CreateNewUnit procedure above. }
var
	RPM: RPGMenuPtr;
	uname: String;		{ Unit Name }
	HQCamp: CampaignPtr;
	F: Text;		{ A File }
begin
	{ Create a menu listing all the units in the SaveGame directory. }
	RPM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu );
	BuildFileMenu( RPM , Save_Unit_Base + Default_Search_Pattern );

	{ If any units are found, allow the player to load one. }
	{ Otherwise, go straight to the NEW UNIT procedure. }
	if RPM^.NumItem > 0 then begin
		RPMSortAlpha( RPM );
		DialogMSG('Select unit file to load.');
{$IFDEF SDLMODE}
		uname := SelectFile( RPM , Nil );
{$ELSE}
		uname := SelectFile( RPM );
{$ENDIF}
		if uname <> '' then begin
			HQCamp := NewCampaign;
			Assign(F, Save_Game_Directory + uname );
			reset(F);
			HQCamp^.Source := ReadCGears(F);
			Close(F);
			HQMain( HQCamp );
		end;
	end else CreateNewUnit;

	DisposeRPGMenu( RPM );
end;

Procedure EnterCampaign( PC: GearPtr );
	{ Actually start this character in a campaign. }
var
	RPM: RPGMenuPtr;
	uname: String;
	TCamp: CampaignPtr;
	Part: GearPtr;
	F: Text;
begin
	{ Allocate the campaign structure, and load the adventure }
	{ from disk. }
	TCamp := NewCampaign;
	RPM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu );
	BuildFileMenu( RPM , Adventure_File_Base + Default_Search_Pattern );

	if RPM^.NumItem > 1 then begin
		RPMSortAlpha( RPM );
		DialogMsg( MsgString( 'SelectCampaignFile' ) );

{$IFDEF SDLMODE}
		uname := SelectFile( RPM , Nil );
{$ELSE}
		uname := SelectFile( RPM );
{$ENDIF}
		DisposeRPGMenu( RPM );
	end else if RPM^.NumItem = 1 then begin
		uname := RPM^.FirstItem^.msg;
	end else begin
		uname := '';
	end;

	if uname <> '' then begin
		Assign( F , Series_Directory + uname );
		Reset( F );
		TCamp^.Source := ReadGear( F );
		Close( F );
	end;

	if ( PC <> Nil ) and ( TCamp^.Source <> Nil ) then begin
		Part := PC;
		while Part <> Nil do begin
			if ( Part^.G = GG_Character ) and ( Part^.SubCom = Nil ) then begin
				ExpandCharacter( Part );
			end;
			Part := Part^.Next;
		end;

		Navigator( TCamp , TCamp^.Source^.SubCom , PC );
	end;

	DisposeCampaign( TCamp );
	DisposeGear( PC );
end;

{$IFDEF SDLMODE}
Procedure StartRPGCampaign( RD: RedrawProcedureType );
{$ELSE}
Procedure StartRPGCampaign;
{$ENDIF}
	{ Load & run the teaser adventure. }
var
	RPM: RPGMenuPtr;
	uname: String;
	PC: GearPtr;
	F: Text;
begin
	PC := Nil;

	{ Create a menu listing all the units in the SaveGame directory. }
{$IFDEF SDLMODE}
	RPM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_TitleScreenMenu );
{$ELSE}
	RPM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu );
{$ENDIF}
	BuildFileMenu( RPM , Save_Character_Base + Default_Search_Pattern );

	if RPM^.NumItem > 0 then begin
		RPMSortAlpha( RPM );
		AddRPGMenuItem( RPM , MsgString( 'STARTRPG_NewChar' ) , -2 );
		DialogMSG('Select character file.');
{$IFDEF SDLMODE}
		uname := SelectFile( RPM , RD );
{$ELSE}
		uname := SelectFile( RPM );
{$ENDIF}
		if uname = MsgString( 'STARTRPG_NewChar' ) then begin
			EnterCampaign( CharacterCreator );
		end else if uname <> '' then begin
			Assign(F, Save_Game_Directory + uname );
			reset(F);
			PC := ReadCGears(F);
			Close(F);

			{ Erase character upon entry. }
			Assign( F , Save_Game_Directory + uName );
			Erase(F);

			EnterCampaign( PC );
		end;

	end else begin
		{ The menu was empty... make a new PC! }
		EnterCampaign( CharacterCreator );

	end;

	DisposeRPGMenu( RPM );
end;

Procedure ViewMechaDesign( Part: GearPtr );
	{ Take a look at an individual mecha. }
var
	A: Char;
	msg: String;
begin
{$IFDEF SDLMODE}
	msg := SAttValue( Part^.SA , 'DESC' );
	if ( msg <> '' ) or ( Part^.G <> GG_Mecha ) then begin
		repeat
			RedrawOpening;
			CMessage( msg , ZONE_Menu.GetRect() , InfoGreen );
			GHFlip;
			A := RPGKey;
		until ( A = ' ' ) or ( A = #27 ) or ( A = RPK_MouseButton );
	end;
	msg := MechaDescription( Part );
	if Part^.G = GG_Mecha then begin
		repeat
			RedrawOpening;
			CMessage( msg , ZONE_Menu.GetRect() , InfoGreen );
			GHFlip;
			A := RPGKey;
		until ( A = ' ' ) or ( A = #27 ) or ( A = RPK_MouseButton );
	end;
{$ELSE}
	DisplayGearInfo( Part );
	msg := SAttValue( Part^.SA , 'DESC' );
	if ( msg <> '' ) or ( Part^.G <> GG_Mecha ) then begin
		GameMsg( msg , ZONE_Menu , InfoGreen );
		EndOfGameMoreKey;
	end;
	if Part^.G = GG_Mecha then begin
		GameMsg( MechaDescription( Part ) , ZONE_Menu , InfoGreen );
		EndOfGameMoreKey;
	end;
	ClrZone( ZONE_Menu );
	ClrZone( ZONE_Info );
{$ENDIF}
end;

{$IFDEF SDLMODE}

Procedure DDBRedraw;
    { SDL redrawer for the design dir browser. }
var
    Part: GearPtr;
begin
    HQRD_Redraw();
    InfoBox( ZONE_FHQMenu.GetRect() );
    InfoBox( ZONE_FHQInfo.GetRect() );

	Part := RetrieveGearSib( HQRD_Source , CurrentMenuItemValue( HQRD_Menu ) );
	if Part <> Nil then begin
    	LongformGearInfo( Part , Nil, ZONE_FHQInfo );
	end;
end;

Procedure DesignDirBrowser( RD: RedrawProcedureType );
	{ Browse the mecha files on disk. }
	{ NOTE: This procedure must be called from the Arena opening menu, so that }
	{ the RedrawOpening procedure is properly initialized. }
var
    N: Integer;
	part: GearPtr;
begin
    HQRD_Redraw := RD;
    HQRD_Source := AggregatePattern( '*.txt', Design_Directory );
	HQRD_Menu := CreateRPGMenu( MenuItem , MenuSelect , ZONE_FHQMenu );

    part := HQRD_Source;
    n := 1;
    while part <> Nil do begin
        if part^.G = GG_Mecha then begin
            AddRPGMenuItem( HQRD_Menu, FullGearName( part ), n );
        end;
        Inc( n );
        part := part^.Next;
    end;

	RPMSortAlpha( HQRD_Menu );
	AddRPGMenuItem( HQRD_Menu , '  Exit' , -1 );

	repeat
		N := SelectMenu( HQRD_Menu , @DDBRedraw );

		if N > -1 then begin
			part := RetrieveGearSib( HQRD_Source , N );
			if Part <> Nil then begin
                MechaPartBrowser( Part, RD );
			end;
		end;
	until N = -1;
	DisposeRPGMenu( HQRD_Menu );
    DisposeGear( HQRD_Source );
    CleanSpriteList();
end;

{$ELSE}
Procedure BrowseDesignFile( List: GearPtr );
	{ Choose one of the sibling gears from LIST and display its properties. }
	{ NOTE: This procedure must be called from the DesignDirBrowser, which must }
	{ be called directly from the arena opening menu, so that the OpeningDisplay }
	{ procedure is properly initialized. }
var
	BrowseMenu: RPGMenuPtr;
	Part: GearPtr;
	N: Integer;
begin
	{ Create the menu. }
	BrowseMenu := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu );

	{ Add each of the gears to the menu. }
	Part := List;
	N := 1;
	while Part <> Nil do begin
		AddRPGMenuItem( BrowseMenu , FullGearName( Part ) , N );
		Inc( N );
		Part := Part^.Next;
	end;
	RPMSortAlpha( BrowseMenu );
	AddRPGMenuItem( BrowseMenu , '  Cancel' , -1 );

	repeat
		{ Select a gear. }
		N := SelectMenu( BrowseMenu );
		if N > -1 then begin
			Part := RetrieveGearSib( List , N );
			ViewMechaDesign( Part );
		end;
	until N = -1;

	DisposeRPGMenu( BrowseMenu );
end;


Procedure DesignDirBrowser;
	{ Browse the mecha files on disk. }
	{ NOTE: This procedure must be called from the Arena opening menu, so that }
	{ the RedrawOpening procedure is properly initialized. }
var
	MekMenu: RPGMenuPtr;
	fname: String;
	part: GearPtr;
begin
	MekMenu := CreateRPGMenu( MenuItem , MenuSelect , ZONE_Menu );
	BuildFileMenu( MekMenu , Design_Directory + Default_Search_Pattern );
	RPMSortAlpha( MekMenu );
	AddRPGMenuItem( MekMenu , '  Exit' , -1 );

	repeat
		fname := SelectFile( MekMenu );

		if fname <> '' then begin
			part := LoadFile( fname , Design_Directory );
			if Part <> Nil then begin
				if Part^.Next = Nil then begin
					{ Only one mecha in this file. Just view it. }
					ViewMechaDesign( Part );
				end else begin
					{ Multiple mecha in this file. Better write another }
					{ procedure... }
					BrowseDesignFile( Part );
				end;
				DisposeGear( Part );
			end;
		end;
	until fname = '';
	DisposeRPGMenu( MekMenu );
end;
{$ENDIF}

end.