-
Notifications
You must be signed in to change notification settings - Fork 18
/
gearutil.pp
1861 lines (1629 loc) · 56.2 KB
/
gearutil.pp
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
unit gearutil;
{
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 gears;
Function IsMasterGear(G: GearPtr): Boolean;
Procedure InitGear(Part: GearPtr);
Function MasterSize(Part: GearPtr): Integer;
function FindMaster( Part: GearPtr ): GearPtr;
function FindModule( Part: GearPtr ): GearPtr;
function ModifiersSkillBonus( Part: GearPtr; Skill: Integer ): Integer;
Function CharaSkillRank( PC: GearPtr; Skill: Integer ): Integer;
function InGoodModule( Part: GearPtr ): Boolean;
Function ScaleDP( DP , Scale , Material: Integer ): Integer;
Function UnscaledMaxDamage( Part: GearPtr ): Integer;
Function GearMaxDamage(Part: GearPtr): Integer;
Function GearMaxArmor(Part: GearPtr): Integer;
Function GenericName( Part: GearPtr ): String;
Function GearName(Part: GearPtr): String;
Function FullGearName(Part: GearPtr): String;
Function GearMass( Master: GearPtr ): LongInt;
Function IntrinsicMass( Master: GearPtr ): LongInt;
Function EquipmentMass( Master: GearPtr ): LongInt;
Function MakeMassString( BaseMass: LongInt; Scale: Integer ): String;
Function MassString( Master: GearPtr ): String;
Function GearDepth( Part: GearPtr ): Integer;
Function ComponentComplexity( Part: GearPtr ): Integer;
Function SubComComplexity( Part: GearPtr ): Integer;
Function IsLegalSlot( Slot, Equip: GearPtr ): Boolean;
Function IsLegalSubcom( Part, Equip: GearPtr ): Boolean;
Function CanBeInstalled( Part , Equip: GearPtr ): Boolean;
Procedure CheckGearRange( Part: GearPtr );
Function EquipGear( Slot , Part: GearPtr ): GearPtr;
Function SeekGear( Master: GearPtr; G,S: Integer; CheckInv: Boolean ): GearPtr;
Function SeekGear( Master: GearPtr; G,S: Integer ): GearPtr;
Function SeekCurrentLevelGear( Master: GearPtr; G,S: Integer ): GearPtr;
Function GearEncumberance( Mek: GearPtr ): Integer;
Function IntrinsicMVTVMod( Mek: GearPtr ): Integer;
Function EquipmentMVTVMod( Mek: GearPtr ): Integer;
Function BaseMVTVScore( Mek: GearPtr ): Integer;
Function BaseGearValue( Master: GearPtr ): Int64;
Function GearValue( Master: GearPtr ): LongInt;
function SeekGearByName( LList: GearPtr; Name: String ): GearPtr;
function SeekGearByDesig( LList: GearPtr; Name: String ): GearPtr;
function SeekGearByIDTag( LList: GearPtr; G,S,V: LongInt ): GearPtr;
function SeekGearByG( LList: GearPtr; G: Integer ): GearPtr;
function SeekSubsByG( LList: GearPtr; G: Integer ): GearPtr;
function MaxIDTag( LList: GearPtr; G,S: Integer ): LongInt;
function CStat( PC: GearPtr; Stat: Integer ): Integer;
Procedure WriteCGears( var F: Text; G: GearPtr );
Function ReadCGears( var F: Text ): GearPtr;
Function IsExternalPart( Master,Part: GearPtr ): Boolean;
implementation
uses ghchars,ghcpit,ghguard,ghholder,ghmecha,ghmodule,ghmovers,
ghintrinsic,ghprop,ghsensor,ghsupport,
ghswag,ghweapon,texutil;
Const
SaveFileContinue = 0;
SaveFileSentinel = -1;
GMMODE_AddAll = 0;
GMMODE_Intrinsic = 1;
GMMODE_Equipment = 2;
Storage_Armor_Bonus = 2;
Function IsMasterGear(G: GearPtr): Boolean;
{This function checks gear G to see whether or not it counst}
{as a Master Gear. Currently the only gears which count}
{as masters are Mecha and Characters.}
var
it: Boolean;
begin
if G <> Nil then begin
if (G^.G = GG_Mecha) or (G^.G = GG_Character) or (G^.G = GG_Prop) then it := true
else it := false;
end else it := False;
IsMasterGear := it;
end;
Procedure InitGear(Part: GearPtr);
{ Part has just been created. G, S, and V have been defined but }
{ nothing else. Initialize its fields to the default values.}
var
T: Integer;
begin
{Error check- make sure we haven't just been sent a Nil.}
if Part = Nil then exit;
{ Clear all stats, to prevent nasty errors. }
for t := 1 to NumGearStats do Part^.Stat[ T ] := 0;
{For gears which are not master gears, take the scale}
{from its parent.}
if not IsMasterGear(Part) then begin
if Part^.Parent <> NIl then begin
Part^.Scale := Part^.Parent^.Scale;
{ In addition, sub-components inherit material from }
{ their parents. }
if IsSubCom( Part ) then begin
SetNAtt( Part^.NA , NAG_GearOps , NAS_Material , NAttValue( Part^.Parent^.NA , NAG_GearOps , NAS_Material ) );
end;
end;
end;
{Do the type-specific initialization routines here.}
case Part^.G of
GG_Mecha: InitMecha(Part);
GG_Character: InitChar(Part);
GG_Weapon: InitWeapon(Part);
GG_Ammo: InitAmmo(Part);
GG_Module: InitModule(Part);
GG_MetaTerrain: InitMetaTerrain(Part);
end;
end;
Function MasterSize(Part: GearPtr): Integer;
{Determine the size of the Master for the current gear.}
{If the Master is a mecha, this will be its Value field.}
{If its a Character, this will be its Body stat.}
{If no Master can be found, return Nil.}
var
it: Integer;
begin
while (Part <> Nil) and not IsMasterGear(Part) do begin
Part := Part^.Parent;
end;
if Part = Nil then it := 0
else if Part^.G = GG_Mecha then it := Part^.V
else if Part^.G = GG_Character then begin
{ The main purpose of this for character gears is to }
{ determine the size of the character's limbs. }
it := ( Part^.Stat[ STAT_Body ] + 2 ) div 3;
if it < 1 then it := 1
else if it > 10 then it := 10;
end;
MasterSize := it;
end;
function FindMaster( Part: GearPtr ): GearPtr;
{ Locate the master of PART. Return NIL if there is no master. }
begin
{ Move the pointer up to either root level or the first Master parent. }
while ( Part <> Nil ) and ( not IsMasterGear(Part) ) do Part := Part^.Parent;
FindMaster := Part;
end;
function FindModule( Part: GearPtr ): GearPtr;
{ Locate the module of PART. Return NIL if there is no master. }
begin
{ Move the pointer up to either root level or the first Master parent. }
while ( Part <> Nil ) and ( Part^.G <> GG_Module ) do Part := Part^.Parent;
FindModule := Part;
end;
function ModifiersSkillBonus( Part: GearPtr; Skill: Integer ): Integer;
{ Determine the total skill bonus gained from MODIFIER }
{ gears installed in PART. }
var
MD: GearPtr; { Modifier gears. }
it: Integer;
begin
MD := Part^.SubCom;
it := 0;
while MD <> Nil do begin
if ( MD^.G = GG_Modifier ) and ( MD^.S = GS_SkillModifier ) then begin
if ( MD^.Stat[ STAT_SkillToModify ] = Skill ) and ( MD^.Stat[ STAT_SkillModBonus ] > it ) then it := MD^.Stat[ STAT_SkillModBonus ];
end;
MD := MD^.Next;
end;
ModifiersSkillBonus := it;
end;
Function CharaSkillRank( PC: GearPtr; Skill: Integer ): Integer;
{ Return the PC's rank in this skill. }
{ Note that PC _MUST_ be the actual PC!!! This does not work on }
{ mecha, or props, or anything else!!! }
{ Also note that this function does not check the presence of tools. }
var
it: Integer;
begin
if PC <> Nil then begin
it := NAttValue( PC^.NA , NAG_Skill , Skill ) + ModifiersSkillBonus( PC , Skill );
{ Normally to check for a talent we'd use the HAStALENT function, }
{ but since that isn't available here and we know we're dealing with }
{ the PC and also because you can't be granted talents from items or }
{ mecha, I'll just check the NAtt to see if it's present. }
if ( it = 0 ) and ( NAttValue( PC^.NA , NAG_Talent , NAS_JackOfAll ) <> 0 ) then it := 1;
CharaSkillRank := it;
end else begin
CharaSkillRank := 0;
end;
end;
function InGoodModule( Part: GearPtr ): Boolean;
{ Check PART to make sure that it is mounted in a good module. }
var
Ms,Md: GearPtr; { Master and Module }
begin
Ms := FindMaster( Part );
Md := FindModule( Part );
{ If no master can be found, this function returns FALSE. }
if ( Ms = Nil ) then Exit( False );
if Ms^.G = GG_Mecha then begin
if Md = Nil then begin
{ This gear must be located in the general }
{ inventory, since it isn't in a module. Items }
{ in the general inventory can't be used until }
{ equipped. }
InGoodModule := False;
end else begin
{ With both the master and the module, look up }
{ this combo in the Form X Module array. }
InGoodModule := FormXModule[ Ms^.S , Md^.S ] and IsSubCom( Md );
end;
end else begin
{ For characters and whatever else, every module is a }
{ good module. If there's no module, i.e. the item is in }
{ the general inventory, that's not good. }
InGoodModule := ( Md <> Nil ) and IsSubCom( Md );
end;
end;
Function ScaleDP( DP , Scale , Material: Integer ): Integer;
{ Modify the damage point score DP for scale and construction. }
var
T: Integer;
begin
if DP > 0 then begin
if Scale < 1 then begin
if Material = NAV_Meat then DP := DP * 2
else DP := DP * 3;
end else begin
if Material = NAV_Meat then DP := DP * 4
else if Material = NAV_BioTech then DP := DP * 6
else if Material = NAV_Metal then DP := DP * 5;
if Scale > 1 then begin
for t := 2 to Scale do DP := DP * 5;
end;
end;
end;
ScaleDP := DP;
end;
Function UnscaledMaxDamage( Part: GearPtr ): Integer;
{ Return the maxdamage rating of this part, unadjusted for }
{ scale or construction. }
var
it: Integer;
begin
case Part^.G of
GG_Module: it := ModuleBaseDamage(Part);
GG_Mecha: it := -1;
GG_Character: it := CharBaseDamage(Part , CStat( Part , STAT_Body ) );
GG_Cockpit: it := -1;
GG_Weapon: it := WeaponBaseDamage(Part);
GG_Ammo: it := AmmoBaseDamage(Part);
GG_MoveSys: it := MovesysBaseDamage(Part);
GG_Holder: it := 1;
GG_Sensor: it := SensorBaseDamage( Part );
GG_Support: it := SupportBaseDamage( Part );
GG_Shield: it := -1;
GG_ExArmor: it := -1;
GG_Swag: it := 1;
GG_Prop: it := Part^.V;
GG_MetaTerrain: it := Part^.V;
GG_Electronics: it := ElecBaseDamage( Part );
GG_Usable: it := UsableDamage( Part );
GG_RepairFuel: it := 0;
GG_Consumable: it := 0;
GG_Modifier: it := 0;
GG_WeaponAddOn: it := 1;
else it := -1;
end;
UnscaledMaxDamage := it;
end;
Function GearMaxDamage(Part: GearPtr): Integer;
{Calculate how much damage this particular part can take}
{before being destroyed.}
var
it: Integer;
begin
{ Start with the unscaled mass damage. }
it := UnscaledMaxDamage( Part );
{Modify damage for scale and construction.}
it := ScaleDP( it , PART^.Scale , NAttValue( Part^.NA , NAG_GearOps , NAS_Material ) );
GearMaxDamage := it;
end;
Function GearMaxArmor(Part: GearPtr): Integer;
{Calculate how much armor protection PART has. This should}
{include both intrinsic armor and equipped armor.}
var
M: GearPtr;
it: Integer;
begin
{Error Check}
if Part = Nil then Exit(0);
{Modules and Cockpits have armor ratings.}
if Part^.G = GG_Module then begin
it := Part^.Stat[STAT_Armor];
if Part^.S = GS_Storage then it := it + Storage_Armor_Bonus;
end else if ( Part^.G = GG_Cockpit ) or ( Part^.G = GG_Support ) then it := Part^.Stat[STAT_Armor]
else if ( Part^.G = GG_Shield ) then it := Part^.V * 2
else if ( Part^.G = GG_ExArmor ) then it := Part^.V
else if ( Part^.G = GG_MetaTerrain ) then it := Part^.V
else it := 0;
{ If this is an armored part, perform additional checks now. }
if it > 0 then begin
{ Modify armor for a GroundHugger or Arachnoid }
M := FindMaster( Part );
if ( M <> Nil ) and ( M^.G = GG_Mecha ) then begin
if M^.S = GS_GroundHugger then it := it + 2
else if M^.S = GS_Arachnoid then it := it + 1;
end;
{Modify it for scale.}
it := ScaleDP( it , Part^.Scale , NAV_Metal );
end;
GearMaxArmor := it;
end;
Function GenericName( Part: GearPtr ): String;
{ Return a generic name for this part. }
begin
case Part^.G of
GG_Module: GenericName := ModuleName(Part);
GG_Mecha: GenericName := MechaName(Part);
GG_Character: GenericName := 'Character';
GG_Cockpit: GenericName := 'Cockpit';
GG_Weapon: GenericName := WeaponName(Part);
GG_Ammo: GenericName := AmmoName(Part);
GG_MoveSys: GenericName := MoveSysName(Part);
GG_Holder: GenericName := HolderName( Part );
GG_Sensor: GenericName := SensorName( Part );
GG_Support: GenericName := SupportName( Part );
GG_Shield: GenericName := ShieldName( Part );
GG_ExArmor: GenericName := ArmorName( Part );
GG_Scene: GenericName := 'Scene ' + BStr( Part^.S );
GG_Swag: GenericName := SwagName( Part );
GG_Prop: GenericName := 'Prop';
GG_MetaTerrain: GenericName := 'Scenery';
GG_Electronics: GenericName := ElecName( Part );
GG_Usable: GenericName := UsableName( Part );
GG_RepairFuel: GenericName := RepairFuelName( Part );
GG_Consumable: GenericName := 'Food';
GG_WeaponAddOn: GenericName := 'Weapon Accessory';
else GenericName := 'Platonic Form';
end;
end;
Function GearName(Part: GearPtr): String;
{Determine the name of Part. If Part has a NAME attribute,}
{this is easy. If not, locate a default name based upon}
{Part's type.}
var
it: String;
begin
{Error check- make sure we aren't trying to find a name}
{for nothing.}
if Part = Nil then Exit( '' );
it := SAttValue(Part^.SA,'NAME');
if it = '' then it := GenericName( Part );
if Part^.g = GG_AbsolutelyNothing then it := '~!' + it;
GearName := it;
end;
Function FullGearName(Part: GearPtr): String;
{ Return the name + designation for this gear. }
var
it: String;
begin
it := SAttValue( Part^.SA , 'DESIG' );
if it <> '' then it := it + ' ';
FullGearName := it + GearName( Part );
end;
Function ComponentMass( Part: GearPtr ): LongInt;
{Calculate the unscaled mas of PART, ignoring for the}
{moment its subcomponents.}
const
Mass_MAX = 2147483647;
Mass_MIN = -2147483648;
var
it,MAV: Int64;
begin
Case Part^.G of
GG_Module: it := ModuleBaseMass(Part);
GG_Cockpit: it := CockpitBaseMass(Part);
GG_Weapon: it := WeaponBaseMass(Part);
GG_Ammo: it := AmmoBaseMass(Part);
GG_MoveSys: it := MovesysBaseMass(Part);
GG_Holder: it := 1;
GG_Sensor: it := SensorBaseMass( Part );
GG_Support: it := SupportBaseMass( Part );
GG_Shield: it := ShieldBaseMass( Part );
GG_ExArmor: it := ArmorBaseMass( Part );
GG_Swag: it := SwagBaseMass( Part );
GG_Prop: it := Part^.V;
GG_MetaTerrain: it := Part^.V;
GG_Electronics: it := ElecBaseMass( Part );
GG_Usable: it := UsableDamage( Part );
GG_Consumable: it := FoodMass( Part );
GG_WeaponAddOn: it := 1;
{If a component type is not listed above, it has no mass.}
else it := 0
end;
{ Reduce component mass by mass adjustment value. }
MAV := NAttValue( Part^.NA , NAG_GearOps , NAS_MassAdjust );
it := it + MAV;
{ Mass adjustment can't result in a negative mass. }
if it < 0 then it := 0;
if it < Mass_MIN then begin
it := Mass_MIN;
end else if Mass_MAX < it then begin
it := Mass_MAX;
end;
ComponentMass := it;
end;
Function TrackMass( Part: GearPtr; Scale: Integer; Mode: Byte; AddThis: Boolean ): LongInt;
{Calculate the mass of this list of gears, including all}
{subcomponents.}
const
Mass_MAX = 2147483647;
Mass_MIN = -2147483648;
var
it,W: Int64;
t: Integer;
begin
{Initialize the total Mass to 0.}
it := 0;
{Loop through all components.}
while Part <> Nil do begin
{We will only add the mass of components which are}
{in the same scale as the master gear.}
if Part^.Scale >= Scale then begin
W := ComponentMass(Part);
{Increase mass for overscale gears.}
if Part^.Scale > Scale then begin
for t := 1 to Part^.Scale - Scale do W := W * 5;
end;
{Add the mass to the total.}
if AddThis then it := it + W;
end;
{Check for subcomponents and invcomponents.}
if Mode = GMMODE_AddAll then begin
if Part^.SubCom <> Nil then it := it + TrackMass(Part^.SubCom,Scale,Mode,True);
if Part^.InvCom <> Nil then it := it + TrackMass(Part^.InvCom,Scale,Mode,True);
end else if Mode = GMMODE_Intrinsic then begin
{ Calculate only the mass of pure SubComs. }
if Part^.SubCom <> Nil then it := it + TrackMass(Part^.SubCom,Scale,Mode,True);
end else begin
{ Calculate only the mass of InvComs. }
if Part^.SubCom <> Nil then it := it + TrackMass(Part^.SubCom,Scale,Mode,AddThis);
if Part^.InvCom <> Nil then it := it + TrackMass(Part^.InvCom,Scale,Mode,True);
end;
{Go to the next part in the series.}
Part := Part^.Next;
end;
{Return the value.}
if it < Mass_MIN then begin
it := Mass_MIN;
end else if Mass_MAX < it then begin
it := Mass_MAX;
end;
TrackMass := it;
end;
Function GearMass( Master: GearPtr ): LongInt;
{Calculate the mass of MASTER, including all of its}
{subcomponents.}
const
Mass_MAX = 2147483647;
Mass_MIN = -2147483648;
var
tmp: Int64;
begin
{The formula to work out the total mass of this gear}
{is basic mass + SubCom mass + InvCom mass.}
if ( Master = Nil ) or ( Master^.G < 0 ) then begin
GearMass := 0;
end else begin
tmp := ComponentMass(Master);
tmp := tmp + TrackMass(Master^.SubCom,Master^.Scale,GMMODE_AddAll,True);
tmp := tmp + TrackMass(Master^.InvCom,Master^.Scale,GMMODE_AddAll,True);
if tmp < Mass_MIN then begin
tmp := Mass_MIN;
end else if Mass_MAX < tmp then begin
tmp := Mass_MAX;
end;
GearMass := tmp;
end;
end;
Function IntrinsicMass( Master: GearPtr ): LongInt;
{ Return the mass of MASTER and all its subcomponents. Do not }
{ calculate the mass of inventory components. }
const
Mass_MAX = 2147483647;
Mass_MIN = -2147483648;
var
tmp: Int64;
begin
tmp := ComponentMass(Master);
tmp := tmp + TrackMass(Master^.SubCom,Master^.Scale,GMMODE_Intrinsic,True);
if tmp < Mass_MIN then begin
tmp := Mass_MIN;
end else if Mass_MAX < tmp then begin
tmp := Mass_MAX;
end;
IntrinsicMass := tmp;
end;
Function EquipmentMass( Master: GearPtr ): LongInt;
{ Return the mass of all inventory components of MASTER. Do not }
{ include the mass of intrinsic components. }
const
Mass_MAX = 2147483647;
Mass_MIN = -2147483648;
var
tmp: Int64;
begin
tmp := TrackMass(Master^.SubCom,Master^.Scale,GMMODE_Equipment,False);
tmp := tmp + TrackMass(Master^.InvCom,Master^.Scale,GMMODE_Equipment,True);
if tmp < Mass_MIN then begin
tmp := Mass_MIN;
end else if Mass_MAX < tmp then begin
tmp := Mass_MAX;
end;
EquipmentMass := tmp;
end;
Function MakeMassString( BaseMass: LongInt; Scale: Integer ): String;
{ Given a mass value and a scale, create a string to express }
{ said mass to the player. }
var
msg: String;
T: Integer;
begin
if Scale >= 2 then begin
for t := 3 to Scale do BaseMass := BaseMass * 5;
msg := BStr( BaseMass div 2 ) + '.' + BStr( ( BaseMass mod 2 ) * 5 ) + 't';
end else if Scale = 1 then begin
msg := BStr( BaseMass div 10 ) + '.' + BStr( BaseMass mod 10 ) + 't';
end else if Scale = 0 then begin
msg := BStr( BaseMass div 2 ) + '.' + BStr( ( BaseMass mod 2 ) * 5 ) + 'kg';
end else begin
msg := BStr( BaseMass ) + '!';
end;
MakeMassString := Msg;
end;
Function MassString( Master: GearPtr ): String;
{ Return a string describing how heavy this gear is, based upon its }
{ scale. }
var
BaseMass: LongInt;
begin
BaseMass := GearMass( Master );
MassString := MakeMassString( BaseMass , Master^.Scale );
end;
Function GearDepth( Part: GearPtr ): Integer;
{ Calculate the depth of PART. If PART is a root level component, }
{ depth is 0. }
var
D: Integer;
begin
{ Initialize D. }
D := 0;
{ Ascend up the structure to the root level. For each level }
{ we have to go up, increase D by one. }
while Part^.Parent <> Nil do begin
Part := Part^.Parent;
Inc( D );
end;
GearDepth := D;
end;
Function ComponentComplexity( Part: GearPtr ): Integer;
{ Return the basic complexity value of this part, and only }
{ this part. }
begin
if ( Part = Nil ) or ( Part^.G < 0 ) then begin
ComponentComplexity := 0;
end else begin
case Part^.G of
GG_Module: ComponentComplexity := ModuleComplexity( Part );
GG_Weapon: ComponentComplexity := WeaponComplexity( Part );
GG_MoveSys: ComponentComplexity := Part^.V;
else ComponentComplexity := 1;
end;
{ If the part is integral, and not a module, reduce complexity by 1 }
{ down to a minimum value of 1. }
if ( ComponentComplexity > 1 ) and ( Part^.G <> GG_Module ) and PartHasIntrinsic( Part , NAS_Integral ) then Dec( ComponentComplexity );
end;
end;
Function SubComComplexity( Part: GearPtr ): Integer;
{ Return the overall complexity of all of PART's subcomponents. }
var
it: Integer;
S: GearPtr;
begin
it := 0;
S := Part^.SubCom;
while S <> Nil do begin
if S^.Scale >= Part^.Scale then begin
it := it + ComponentComplexity( S );
end else begin
Inc( it );
end;
S := S^.Next;
end;
SubComComplexity := it;
end;
Function IsLegalSlot( Slot, Equip: GearPtr ): Boolean;
{ Check EQUIP to see if it can be installed as a sub-component }
{ of SLOT. Return TRUE if it can, FALSE if it can't. }
{ This procedure only checks to make sure the slot is legal; }
{ it doesn't check whether the slot is already occupied or }
{ anything else. }
begin
if ( Slot = Nil ) or ( Equip = Nil ) then begin
{ If either of the provided gears don't really exist, }
{ this can't very well be a legal installation, can it? }
IsLegalSlot := False;
end else if Slot^.G < 0 then begin
{ Virtal slots can hold anything. }
IsLegalSlot := True;
end else if IsMasterGear( Slot ) or ( Slot^.G = GG_MetaTerrain ) then begin
{ The inventory components of MASTER gears can hold just }
{ about anything, since they represent the "general }
{ inventory" from most RPGs. The only restrictions have }
{ to do with scale and weight. }
if Equip^.Scale > Slot^.Scale then IsLegalSlot := False
else if Equip^.G = GG_MetaTerrain then IsLegalSlot := False
else IsLegalSlot := True;
end else if AStringHasBString( SAttValue( Equip^.SA , 'TYPE' ) , 'CYBER' ) then begin
{ Gears marked as "cyber" can only be internally mounted. }
IsLegalSlot := False;
end else if Slot^.G = GG_Holder then begin
{ Call the ghholder unit InvChecker to see what it says. }
IsLegalSlot := IsLegalHolderInv( Slot , Equip );
end else if ( Slot^.G = GG_Weapon ) then begin
IsLegalSlot := IsLegalWeaponInv( Slot , Equip );
end else if Slot^.G = GG_Module then begin
{ Call the ghmodule unit InvChecker to see what it says. }
if Equip^.G = GG_ExArmor then begin
IsLegalSlot := IsLegalModuleInv( Slot , Equip ) and ArmorFItsMaster( Equip , FindMaster( Slot ) );
end else begin
IsLegalSlot := IsLegalModuleInv( Slot , Equip );
end;
end else begin
{ No other slots may hold equipment. }
IsLegalSlot := False;
end;
end;
Procedure CheckGearInv( Part: GearPtr );
{ Check through a gear's Inv components, and delete any illegal }
{ gears found. A gear is legal if it meets two requirements- }
{ it must be legal according to the IsLegalSlot procedure above, }
{ and it must be the only gear with a given G value installed at }
{ this location. }
var
LG,LG2: GearPtr; { Loop Gear }
MG: GearPtr; { Multiplicity Gear }
N: Integer; { A number. That's all. }
begin
LG := Part^.InvCom;
while LG <> Nil do begin
{ We need to save the location of the next gear, }
{ since LG itself might get deleted. }
LG2 := LG^.Next;
if not IsLegalSlot( Part , LG ) then begin
{ LG failed the legality check. Delete it. }
RemoveGear( Part^.InvCom , LG );
end else if ( Part^.G <> GG_MetaTerrain ) and not IsMasterGear( Part ) then begin
{ Perform the multiplicity test. }
N := 0;
MG := Part^.InvCom;
while MG <> Nil do begin
if MG^.G = LG^.G then Inc( N );
MG := MG^.Next;
end;
{ There's more than one gear here. Get rid of it. }
if N > 1 then RemoveGear( Part^.InvCom , LG );
end;
LG := LG2;
end;
end;
Function IsLegalSubcom( Part, Equip: GearPtr ): Boolean;
{ Check EQUIP and see if it can be a legal subcomponent of PART. }
{ The first rule is that EQUIP must be of a scale less than or }
{ equal to PART; the second rule is that it must meet whatever }
{ conditions are set by PART's type. }
{ Note that this procedure only checks the legality of installation; }
{ it does not do a multiplicity test or anything else. }
begin
if ( Part = Nil ) or ( Equip = Nil ) then begin
{ If either of the provided gears don't really exist, }
{ this can't very well be a legal installation, can it? }
IsLegalSubcom := False;
end else if Part^.G < 0 then begin
{ Virtal slots can hold anything. }
IsLegalSubcom := True;
end else if Equip^.Scale > Part^.Scale then begin
{ Can't mount a gear of larger scale than the part. }
IsLegalSubcom := False;
end else begin
case Part^.G of
GG_Mecha: IsLegalSubcom := IsLegalMechaSubCom( Part, Equip );
GG_Module: IsLegalSubCom := IsLegalModuleSub( Part, Equip );
GG_Character: IsLegalSubCom := IsLegalCharSub( Part , Equip );
GG_Cockpit: IsLegalSubCom := IsLegalCPitSub( Part , Equip );
GG_Shield: IsLegalSubCom := IsLegalShieldSub( Part , Equip );
GG_ExArmor: IsLegalSubCom := IsLegalArmorSub( Part , Equip );
GG_Weapon: IsLegalSubCom := IsLegalWeaponSub( Part , Equip );
GG_Prop: IsLegalSubCom := True;
GG_MetaTerrain: IsLegalSubCom := True;
GG_WeaponAddOn: IsLegalSubCom := Equip^.G = GG_Weapon;
else IsLegalSubcom := False
end;
end;
end;
Function MaximumInstancesAllowed( Slot: GearPtr; Equip_G,Equip_S: Integer ): Integer;
{ Return the maximum number of (G,S) gears that can be }
{ installed in SLOT, or 0 if as many as wanted can be installed. }
{ Note that the results of this function are undefined if the }
{ part cannot be legally installed in the slot in the first place. }
begin
if Equip_G = GG_MoveSys then begin
MaximumInstancesAllowed := 1;
end else if Equip_G = GG_Support then begin
MaximumInstancesAllowed := 1;
end else if ( Equip_G = GG_Module ) and ( Equip_S = GS_Body ) then begin
MaximumInstancesAllowed := 1;
end else if Equip_G = GG_Ammo then begin
MaximumInstancesAllowed := 1;
end else if Equip_G = GG_Holder then begin
if ( Slot^.G = GG_Module ) and ( Slot^.S = GS_Body ) then begin
{ Body modules may have up to two mounting points. }
MaximumInstancesAllowed := 2;
end else begin
{ All other locations may have only one holder of each type. }
MaximumInstancesAllowed := 1;
end;
end else begin
MaximumInstancesAllowed := 0;
end;
end;
Function NumberOfMatches( Parent,Exclude: GearPtr; G,S: Integer ): Integer;
{ Count up the number of gears present which match }
{ the descriptors G, S. }
{ Don't count part EXCLUDE. }
var
N: Integer;
PSC: GearPtr; { Part SubCom }
begin
N := 0;
PSC := Parent^.SubCom;
while PSC <> Nil do begin
if ( PSC^.G = G ) and ( PSC^.S = S ) and ( PSC <> Exclude ) then Inc( N );
PSC := PSC^.Next;
end;
NumberOfMatches := N;
end;
Function MultiplicityCheck( Slot, Item: GearPtr ): Boolean;
{ Certain gears may only be installed a set number of times. }
{ For instance, an arm may only have one hand, etc... This }
{ function centralizes the multiplicity check. Return TRUE if }
{ ITEM can be installed in SLOT, or FALSE otherwise. }
var
it: Boolean;
N: Integer;
CyberSlot: String;
Function CyberMatches: Integer;
{ Return the number of subcoms of SLOT which bear the same }
{ cyberslot as ITEM, excluding ITEM itself. }
var
N: Integer;
PSC: GearPtr; { Part SubCom }
begin
N := 0;
PSC := Slot^.SubCom;
CyberSlot := UpCase( CyberSlot );
while PSC <> Nil do begin
if ( UpCase( SAttValue( PSC^.SA , SATT_CyberSlot ) ) = CyberSlot ) and ( PSC <> Item ) then Inc( N );
PSC := PSC^.Next;
end;
CyberMatches := N;
end;
begin
{ Start by assuming TRUE. }
it := True;
{ Check the MaximumInstancesAllowed. }
N := MaximumInstancesAllowed( Slot , Item^.G , Item^.S );
if N > 0 then begin
it := ( NumberOfMatches( Slot , Item , Item^.G , Item^.S ) < N );
end;
{ Check Cyberslot. }
if it then begin
CyberSlot := SAttValue( Item^.SA , SATT_CyberSlot );
if CyberSlot <> '' then begin
it := CyberMatches = 0;
end;
end;
{ Return the result. }
MultiplicityCheck := it;
end;
Procedure CheckGearSubs( Part: GearPtr );
{ Examine the subcomponents of this gear to make sure everything }
{ is nice and legal. }
{ First do a legality check for each subcom, then do a }
{ multiplicity test if the subcom is a type which requires that. }
var
LG,LG2: GearPtr; { Loop Gear }
begin
LG := Part^.SubCom;
while LG <> Nil do begin
{ We need to save the location of the next gear, }
{ since LG itself might get deleted. }
LG2 := LG^.Next;
if not IsLegalSubCom( Part , LG ) then begin
{ LG failed the legality check. Delete it. }
RemoveGear( Part^.SubCom , LG );
end else begin
{ *** MULTIPLICITY CHECK *** }
if not MultiplicityCheck( Part , LG ) then begin
RemoveGear( Part^.SubCom , LG );
end;
end;
LG := LG2;
end;
end;
Function CanBeInstalled( Part , Equip: GearPtr ): Boolean;
{ Return TRUE if the part can be equipped, or FALSE }
{ otherwise. }
var
it: Boolean;
begin
it := IsLegalSubCom( Part , Equip );
if it then begin
it := MultiplicityCheck( Part , Equip );
if it and ( Equip^.G = GG_Module ) and ( Part^.G = GG_Mecha ) then begin
it := FormXModule[ Part^.S , Equip^.S ];
end;
end;
CanBeInstalled := it;
end;
Procedure CheckGearRange( Part: GearPtr );
{ Check the G , S , V , Stat , SubCom , and InvCom values of }
{ this gear to make sure everything is all nice and legal. }
begin
if Part^.G = GG_Mecha then CheckMechaRange( Part )
else if Part^.G = GG_Module then CheckModuleRange( Part )
else if Part^.G = GG_Cockpit then CheckCPitRange( Part )
else if Part^.G = GG_Weapon then CheckWeaponRange( Part )
else if Part^.G = GG_Ammo then CheckAmmoRange( Part )
else if Part^.G = GG_Holder then CheckHolderRange( Part )
else if Part^.G = GG_MoveSys then CheckMoverRange( Part )
else if Part^.G = GG_Sensor then CheckSensorRange( Part )
else if Part^.G = GG_Electronics then CheckElecRange( Part )
else if Part^.G = GG_Support then CheckSupportRange( Part )
else if Part^.G = GG_Shield then CheckShieldRange( Part )
else if Part^.G = GG_ExArmor then CheckArmorRange( Part )
else if Part^.G = GG_Swag then CheckSwagRange( Part )
else if Part^.G = GG_Prop then CheckPropRange( Part )
else if Part^.G = GG_Usable then CheckUsableRange( Part )
else if Part^.G = GG_RepairFuel then CheckRepairFuelRange( Part )
else if Part^.G = GG_Consumable then CheckFoodRange( Part )
else if Part^.G = GG_Modifier then CheckModifierRange( Part )
else if Part^.G = GG_WeaponAddOn then CheckWeaponAddOnRange( Part );
{ Next, check the children of this gear to make sure everything }
{ there is all nice and legal. }
{ Note that the children of the gear don't have to be checked }
{ if this gear is one of the virtual types; see gears.pp for }
{ more information about that. }
if Part^.G >= 0 then begin
CheckGearInv( Part );
CheckGearSubs( Part );
end;
end;
Function EquipGear( Slot , Part: GearPtr ): GearPtr;
{ This procedure will attempt to place PART as an inventory }
{ component of SLOT. It will return a gear to place in the }
{ general inventory, which should usually be either the previously }
{ equipped item or PART itself if installation here is illegal. }
{ This function may return NIL. PRECONDITION: Part must be }
{ properly delinked. }
var