File: wmonster.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 (550 lines) | stat: -rw-r--r-- 15,672 bytes parent folder | download | duplicates (5)
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
unit WMonster;
{
	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,locale;

const
	{ This is the minimum point value for meks when calling the STOCKSCENE }
	{ procedure. }
	MinPointValue = 65000;


Function GenerateMonster( MTV,Scale: Integer; MDesc: String ): GearPtr;
Procedure RestockRandomMonsters( GB: GameBoardPtr );
Function GenerateMechaList( MPV: LongInt ): SAttPtr;
Function PurchaseForces( ShoppingList: SAttPtr; UPV: LongInt ): GearPtr;
Procedure SelectEnemyForces( GB: GameBoardPtr; MekList: SAttPtr; UPV: LongInt );
Procedure StockSceneWithEnemies( Scene: GearPtr; UPV: longInt; TeamID: Integer );
Procedure StockSceneWithMonsters( Scene: GearPtr; MPV,TeamID: Integer; MDesc: String );


implementation

{$IFDEF SDLMODE}
uses dos,ability,action,gearutil,ghchars,ghparser,texutil,sdlmap;
{$ELSE}
uses dos,ability,action,gearutil,ghchars,ghparser,texutil,conmap;
{$ENDIF}

Function MatchWeight( S, M: String ): Integer;
	{ Return a value showing how well the monster M matches the }
	{ quoted source S. }
var
	Trait: String;
	it: Integer;
begin
	it := 0;

	while M <> '' do begin
		Trait := ExtractWord( M );

		if AStringHasBString( S , Trait ) then begin
			if it = 0 then it := 1
			else it := it * 2;
		end;
	end;

	MatchWeight := it;
end;

Function GenerateMonster( MTV,Scale: Integer; MDesc: String ): GearPtr;
	{ Generate a monster with no greater than NTV threat value, }
	{ which corresponds to MDesc. }
var
	ShoppingList,ShoppingItem: NAttPtr;
	Total: LongInt;
	WM: GearPtr;
	N,Match: Integer;
begin
	ShoppingList := Nil;
	WM := WMonList;
	N := 1;
	Total := 0;
	while WM <> Nil do begin
		{ If this monster matches our criteria, add its number to the list. }
		if ( WM^.V <= MTV ) and ( WM^.Scale <= Scale ) then begin
			Match := MatchWeight( MDesc , SAttValue( WM^.SA , 'TYPE' ) );
			SetNAtt( ShoppingList , 0 , N , Match );
			Total := Total + Match;
		end;

		{ Move to the next monster, and increase the monster index. }
		WM := WM^.Next;
		Inc( N );
	end;

	if Total > 0 then begin
		Match := Random( Total );
		ShoppingItem := ShoppingList;
		while Match > ShoppingItem^.V do begin
			Match := Match - ShoppingItem^.V;
			ShoppingItem := ShoppingItem^.Next;
		end;
		N := ShoppingItem^.S;

		{ Return the selected monster. }
		GenerateMonster := CloneGear( RetrieveGearSib( WMonList , N ) );
	end else begin
		{ Return a random monster. }
		GenerateMonster := CloneGear( SelectRandomGear( WMonList ) );
	end;
end;

Procedure AddRandomMonsters( GB: GameBoardPtr; Team: GearPtr; Threat: Integer );
	{ Place some wandering monsters on the map. }
var
	ShoppingList,ShoppingItem: NAttPtr;
	Total: LongInt;
	WM: GearPtr;
	WMonType: String;
	Gen,N,Match: Integer;
	MTV: LongInt;	{ Maximum Threat Value }
begin
	{ Check the TEAM gear to see what kinds of monsters we're looking for. }
	WMonType := SAttValue( Team^.SA , 'TYPE' );

	{ Create the shopping list. }
	{ Remove those that don't match the description, that are too big }
	{ for this game board, and that have a Threat Value which is too high. }
	MTV := Team^.Stat[ STAT_WanderMon ] div 2;
	if MTV < 1 then MTV := 1;

	ShoppingList := Nil;
	WM := WMonList;
	N := 1;
	Total := 0;
	while WM <> Nil do begin
		{ If this monster matches our criteria, add its number to the list. }
		if ( WM^.V <= MTV ) and ( WM^.Scale <= GB^.Scale ) then begin
			Match := MatchWeight( WMonType , SAttValue( WM^.SA , 'TYPE' ) );
			SetNAtt( ShoppingList , N , N , Match );
			Total := Total + Match;

		end;



		{ Move to the next monster, and increase the monster index. }
		WM := WM^.Next;
		Inc( N );
	end;


	{ If no monsters were loaded, exit the procedure. }
	if ShoppingList = Nil then Exit;

	{ Decide upon how many monsters to add. }
	Gen := Random( 5 );

	while ( Gen > 0 ) and ( Threat > 0 ) do begin
		{ Choose a random monster to add. }
		Match := Random( Total );
		ShoppingItem := ShoppingList;
		while Match > 0 do begin
			Match := Match - ShoppingItem^.V;
			if Match > 0 then ShoppingItem := ShoppingItem^.Next;
		end;
		N := ShoppingItem^.S;

		{ Deploy the monster on the map by cloning it. }
		WM := CloneGear( RetrieveGearSib( WMonList , N ) );
		SetNAtt( WM^.NA , NAG_Location , NAS_Team , Team^.S );

		{ Give the monster some money. }
		if WM^.V > 0 then begin
			SetNAtt( WM^.NA , NAG_Experience , NAS_Credits , Random( WM^.V + 3 ) );
		end;
		DeployMek( GB , WM , True );

		{ Reduce the generation counter and the threat points. }
		Threat := Threat - WM^.V;
		Dec( Gen );
	end;

	DisposeNAtt( ShoppingList );
end;

Procedure RestockRandomMonsters( GB: GameBoardPtr );
	{ Replenish this level's supply of random monsters. }
var
	Team: GearPtr;
	TPV: LongInt;
begin
	{ Error check - make sure the scene is defined. }
	if ( GB = Nil ) or ( GB^.Scene = Nil ) then Exit;

	{ Search through the scene gear for teams which need random }
	{ monsters. If they don't have enough PV, add some monsters. }
	Team := GB^.Scene^.SubCom;
 	while Team <> Nil do begin
		{ if this gear is a team, and it has a wandering monster }
		{ allocation set, add some monsters. }
		if ( Team^.G = GG_Team ) and ( Team^.STat[ STAT_WanderMon ] > 0 ) then begin
			{ Calculate total point value of this team's units. }
			TPV := TeamTV( GB^.Meks , Team^.S );

			if TPV < Team^.Stat[ STAT_WanderMon ] then begin
				AddRandomMonsters( GB , Team , Team^.Stat[ STAT_WanderMon ] - TPV );
			end;
		end;

		{ Move to the next gear. }
		Team := Team^.Next;
	end;

end;

Function GenerateMechaList( MPV: LongInt ): SAttPtr;
	{ Build a list of mechas from the DESIGN diectory which have }
	{ a maximum point value of MPV or less. }
	{ Format for the description string is: pv index <filename> }
	{ where PV = Point Value, Index = Root Gear Number (since a }
	{ design file may contain more than one mecha), and filename }
	{ is the filename stored as an alligator string. }
var
	SRec: SearchRec;
	it,current: SAttPtr;
	DList,Mek: GearPtr;
	F: Text;
	N,MinValFound: LongInt;	{ The lowest value found so far. }
	MVInfo: String;		{ Info on the mek with the lowest value. }
begin
	it := Nil;
	MinValFound := 0;
	MVInfo := '';

	{ Start the search process going... }
	FindFirst( Design_Directory + Default_Search_Pattern , AnyFile , SRec );

	{ As long as there are files which match our description, }
	{ process them. }
	While DosError = 0 do begin
		{ Load this mecha design file from disk. }
		Assign( F , Design_Directory + SRec.Name );
		reset(F);
		DList := ReadGear(F);
		Close(F);

		{ Search through it for mecha. }
		Mek := DList;
		N := 1;
		while Mek <> Nil do begin
			if ( Mek^.G = GG_Mecha ) then begin
				if ( GearValue( Mek ) <= MPV ) then begin
					Current := CreateSAtt( it );
					Current^.Info := BStr( GearValue( Mek ) ) + ' ' + BStr( N ) + ' <' + SRec.Name + '>';
				end else if ( GearValue( Mek ) < MinValFound ) or ( MinValFound = 0 ) then begin
					MVInfo := BStr( GearValue( Mek ) ) + ' ' + BStr( N ) + ' <' + SRec.Name + '>';
					MinValFound := GearValue( Mek );
				end;
			end;
			Mek := Mek^.Next;
			Inc( N );
		end;

		{ Dispose of the list. }
		DisposeGear( DList );

		{ Look for the next file in the directory. }
		FindNext( SRec );
	end;

	{ Error check- we don't want to return an empty list, }
	{ but we will if we have to. }
	if ( it = Nil ) and ( MVInfo <> '' ) then begin
		Current := CreateSAtt( it );
		Current^.Info := MVInfo;
	end;

	GenerateMechaList := it;
end;

Function PurchaseForces( ShoppingList: SAttPtr; UPV: LongInt ): GearPtr;
	{ Pick a number of random meks with point value at least }
	{ equal to UPV. Add pilots to these meks. }

	Function ObtainMekFromFile( S: String ): GearPtr;
		{ Using the description string S, locate and load }
		{ a mek from disk. }
	var
		N: LongInt;
		F: Text;
		FList,Mek: GearPtr;
	begin
		{ Load the design file. }
		Assign(F, Design_Directory + RetrieveAString( S ) );
		reset(F);
		FList := ReadGear(F);
		Close(F);

		{ Get the number of the mek we want. }
		N := ExtractValue( S );

		{ Clone the mecha we want. }
		Mek := CloneGear( RetrieveGearSib( FList , N ) );

		{ Get rid of the design record. }
		DisposeGear( FList );

		{ Return the mek obtained. }
		ObtainMekFromFile := Mek;
	end;

	Function SelectNextMecha: String;
		{ Select a mecha file to load. Try to make it appropriate }
		{ to the point value of the encounter. }
	var
		M1,M2: STring;
		T: Integer;
		V,V2: LongInt;
	begin
		{ Select a mecha at random, and find out its point value. }
		M1 := SelectRandomSAtt( ShoppingList )^.Info;
		V := ExtractValue( M1 );

		{ If the PV of this mecha seems a bit low, }
		{ look for a more expensive model and maybe pick that }
		{ one instead. }
		t := 3;
		while ( t > 0 ) and ( V < ( UPV div 5 ) ) do begin
			M2 := SelectRandomSAtt( ShoppingList )^.Info;
			V2 := ExtractValue( M2 );
			if V2 > V then begin
				M1 := M2;
				V := V2;
			end;

			Dec( T );
		end;

		{ Return the info string selected. }
		SelectNextMecha := M1;
	end;
var
	MPV: LongInt;
	Lvl: LongInt;		{ Pilot level. }
	StPt,SkPt: LongInt;	{ Stat points and skill points of the pilot. }
	Mek,MList,CP: GearPtr;
begin
	{ Initialize our list to Nil. }
	MList := Nil;

	{ Keep processing until we run out of points. }
	while ( UPV > 0 ) and ( ShoppingList <> Nil ) do begin
		{ Select a mek at random. }
		{ Load & Clone the mek. }
		Mek := ObtainMekFromFile( SelectNextMecha );
		MPV := GearValue( Mek );

		{ Select a pilot skill level. }
		{ Set default values. }
		StPt := 90;
		SkPt := 3;

		if ( MPV > UPV ) or ( Random(10) = 1 ) then begin
			{ Level will be between 0 and -5 }
			Lvl := Random( 6 );
			StPt := StPt - ( Lvl * 3 );
			SkPt := SkPt - ( Lvl div 2 );
			MPV := ( MPV * ( 10 - Lvl ) ) div 10;

		end else if Random( MPV ) < Random( UPV ) then begin
			{ Level will be between 0 and 20 }
			Lvl := Random( 21 );

			{ Make sure we don't go overboard. }
			while ( ( ( MPV * ( 5 + Lvl ) ) div 5 ) > UPV ) and ( Lvl > 0 ) do begin
				Dec( Lvl );
			end;

			StPt := StPt + Lvl;
			SkPt := SkPt + ( Lvl div 2 );
			MPV := ( MPV * ( 5 + Lvl ) ) div 5;
		end;

		{ Add this mecha to our list. }
		Mek^.Next := MList;
		MList := Mek;

		{ Insert pilot in this mecha. }
		CP := SeekGear( Mek , GG_CockPit , 0 );
		if CP <> Nil then begin
			InsertSubCom( CP , RandomPilot( StPt , SkPt ) );
		end;

		{ Reduce UPV by an appropriate amount. }
		UPV := UPV - MPV;
	end;

	PurchaseForces := MList;
end;

Procedure SelectEnemyForces( GB: GameBoardPtr; MekList: SAttPtr; UPV: LongInt );
	{ This procedure will buy a number of mecha, provide them with pilots, }
	{ and stick them on the map. }
var
	MList,Mek: GearPtr;
begin
	{ Purchase the mecha. }
	MList := PurchaseForces( MekList , UPV );

	{ Stick the mecha on the map. }
	while MList <> Nil do begin
		{ Delink the first mecha from the list. }
		Mek := MList;
		DelinkGear( MList , Mek );

		{ Set its team to the enemy team. }
		SetNAtt( Mek^.NA , NAG_Location , NAS_Team , NAV_DefEnemyTeam );

		{ Place it on the map. }
		DeployMek( GB , Mek , True );
	end;
end;

Procedure StockSceneWithSoldiers( Scene: GearPtr; UPV: LongInt; TeamID: Integer );
	{ Fill this team with people, but instead of mechas just give }
	{ them some random equipment. }
var
	F: Text;
	EquipList,NPC: GearPtr;
	EPV,AvgPointValue: LongInt;
	StPt,SkPt,Lvl: Integer;
begin
	Assign( F , PC_Equipment_File );
	Reset( F );
	EquipList := ReadGear( F );
	Close( F );

	AvgPointValue := 800;
	{ Use Lvl temporarily to store the maximum number of combatants we want. }
	lvl := 10 + Random( 20 );
	if ( UPV div AvgPointValue ) > lvl then AvgPointValue := UPV div lvl;

	While UPV > 0 do begin
		StPt := 90;
		SkPt := 5;
		EPV := AvgPointValue + Random( 500 ) - Random( 500 );
		if EPV > UPV then EPV := UPV
		else if EPV < 500 then EPV := 500;

		if ( EPV < 1000 ) or ( Random( 5 ) = 1 ) then begin
			Lvl := -Random( 5 );
			StPt := StPt + 2*Lvl;
			SkPt := SkPt + Lvl;
			EPV := EPV - ( 500 - lvl * lvl * 20 );
			UPV := UPV - ( 500 - lvl * lvl * 20 );
		end else if ( EPV > 1500 ) and ( Random( 5 ) <> 1 ) then begin
			repeat
				Lvl := Random( 10 );
			until ( 500 + lvl * lvl * 150 ) < ( EPV div 2 );
			StPt := StPt + Lvl;
			SkPt := SkPt + Lvl;
			EPV := EPV - ( 500 + lvl * lvl * 150 );
			UPV := UPV - ( 500 + lvl * lvl * 150 );
		end else begin
			EPV := EPV - 500;
			UPV := UPV - 500;
		end;

		NPC := RandomSoldier( StPt , SkPt );
		ExpandCharacter( NPC );

		SelectCombatEquipment( NPC , EquipList , EPV );
		UPV := UPV - EPV;

		{ Set its team to the ID provided. }
		SetNAtt( NPC^.NA , NAG_Location , NAS_Team , TeamID );

		{ Place it in the scene. }
		InsertInvCom( Scene , NPC );
	end;

	DisposeGear( EquipList );
end;

Procedure StockSceneWithMeks( Scene: GearPtr; UPV: longInt; TeamID: Integer );
	{ This scene requires a number of mecha to be added. Purchase an }
	{ appropriate value of mecha, then stick them in the scene. }
var
	ShoppingList: SAttPtr;
	MaxPointValue: LongInt;
	MList,Mek: GearPtr;
begin
	{ Generate the shopping list, then purchase mecha. }
	MaxPointValue := UPV div 2;
	if MaxPointValue < MinPointValue then MaxPointValue := MinPointValue;
	ShoppingList := GenerateMechaList( UPV );
	MList := PurchaseForces( ShoppingList , UPV );
	DisposeSAtt( ShoppingList );

	{ Stick the mecha in the scene. }
	while MList <> Nil do begin
		{ Delink the first mecha from the list. }
		Mek := MList;
		DelinkGear( MList , Mek );

		{ Set its team to the ID provided. }
		SetNAtt( Mek^.NA , NAG_Location , NAS_Team , TeamID );

		{ Place it in the scene. }
		InsertInvCom( Scene , Mek );
	end;

end;

Procedure StockSceneWithEnemies( Scene: GearPtr; UPV: longInt; TeamID: Integer );
	{ Put some enemies in the scene. }
begin
	if Scene^.V = 0 then begin
		StockSceneWithSoldiers( Scene , UPV , TeamID );
	end else begin
		StockSceneWithMeks( Scene , UPV , TeamID );
	end;
end;

Procedure StockSceneWithMonsters( Scene: GearPtr; MPV,TeamID: Integer; MDesc: String );
	{ Place some monsters in this scene. }
var
	M: GearPtr;
begin
	while MPV > 0 do begin
		{ Grab a monster. }
		M := GenerateMonster( MPV , Scene^.V , MDesc );

		{ Reduce the PV by the monster's threat value. }
		MPV := MPV - M^.V;

		{ Set the team to the correct value. }
		{ Set its team to the ID provided. }
		SetNAtt( M^.NA , NAG_Location , NAS_Team , TeamID );

		{ Stick the monster in the scene. }
		InsertInvCom( Scene , M );
	end;

end;


end.