-
Notifications
You must be signed in to change notification settings - Fork 0
/
mon_rand.pas
604 lines (533 loc) · 15.8 KB
/
mon_rand.pas
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
[INHERIT('MONCONST','MONTYPE','MONGLOBL','sys$library:Starlet',
'sys$library:pascal$lib_routines')]
PROGRAM Randoms(OUTPUT);
%include 'headers.txt'
(* -------------------------------------------------------------------------- *)
TYPE
hateptr = ^haterec;
haterec = RECORD
log : integer;
times : integer;
killed : integer;
died : integer;
next : hateptr;
END;
wantptr = ^wantrec;
wantrec = RECORD
objnum : integer;
times : integer;
next : wantptr;
end;
hymie = RECORD
hate : hateptr;
want : wantptr;
all : allmystats;
END;
RoomListPtr = ^RoomList;
RoomList = RECORD (* A listing of rooms a certain distance from *)
DirFrom : INTEGER; (* How did I get here (i.e. What direction) *)
RoomNumber : INTEGER; (* our origination room *)
Next : RoomListPtr; (* Joined in a linked list *)
Parent : RoomListPtr; (* This Nodes parent in the tree *)
Children : ARRAY[1..MaxExit] OF RoomListPtr;
END;
ExitType = RECORD
Where : INTEGER;
Kind : INTEGER;
Alias : String;
END;
RoomType = RECORD (* A listing of all of the rooms *)
Used : BYTE_BOOL;
Exits : ARRAY [1..MaxExit] of ExitType;
END;
AllRoomType = ARRAY[1..MaxRoom] of RoomType;
(* -------------------------------------------------------------------------- *)
VAR
Seed : [EXTERNAL] UNSIGNED;
RDEBUG : BYTE_BOOL := FALSE;
(* GlobalRandoms : ARRAY[1..100] OF RandRec; *)
(* Here : Room; *)
Passed : String;
RandomNumber : INTEGER;
Status : UNSIGNED;
AllRooms : AllRoomType;
(* ======================================================================== *)
(* Section header: Smart movement for randoms. *)
(* ======================================================================== *)
FUNCTION ReadRooms(VAR Rooms : AllRoomType) : BYTE_BOOL;
VAR
Index : IndexRec;
SomeRoom : RoomDesc;
ExitNum : INTEGER;
Status : BYTE_BOOL;
Loop : INTEGER;
BEGIN
Writeln('Reading in rooms...');
Status := GetIndex(I_ROOM, Index);
IF Status THEN
BEGIN
FOR Loop := 1 TO Index.Top DO
IF NOT Index.Free[Loop] THEN
BEGIN
Rooms[Loop].Used := FALSE;
IF GetRoomDesc(Loop, SomeRoom) THEN
FOR ExitNum := 1 TO MaxExit DO
BEGIN
Rooms[Loop].Exits[ExitNum].Where := SomeRoom.Exits[ExitNum].ToLoc;
Rooms[Loop].Exits[ExitNum].Alias := SomeRoom.Exits[ExitNum].Alias;
Rooms[Loop].Exits[ExitNum].Kind := SomeRoom.Exits[ExitNum].Kind;
END
ELSE
Rooms[Loop].Used := TRUE;
END
ELSE
Rooms[Loop].Used := TRUE;
Writeln('All rooms read in.');
END
ELSE
Writeln('Error reading in from room file.');
ReadRooms := Status;
END;
(* ======================================================================== *)
PROCEDURE AddToNewList(VAR NewList : RoomListPtr; VAR OldList : RoomListPtr;
ThisRoom : INTEGER; DirFrom : INTEGER; Dest : INTEGER);
VAR
Dummy : RoomListPtr;
Loop : INTEGER;
BEGIN
NEW(Dummy);
Dummy^.RoomNumber := Dest;
Dummy^.DirFrom := DirFrom;
Dummy^.Parent := OldList;
Dummy^.Next := NewList;
FOR Loop := 1 TO MaxExit DO
Dummy^.Children[Loop] := NIL;
IF OldList = NIL THEN
OldList := Dummy;
IF DirFrom <> 0 THEN
OldList^.Children[DirFrom] := Dummy;
NewList := Dummy;
END;
(* ======================================================================== *)
FUNCTION CanPass(ExitKind : INTEGER; Privd : BYTE_BOOL := FALSE) : BYTE_BOOL;
BEGIN
CASE ExitKind OF
EK_NoExit : CanPass := FALSE;
EK_Open : CanPass := TRUE;
EK_NeedKey : CanPass := FALSE;
EK_NeedNoKey : CanPass := TRUE;
EK_RandomFail : CanPass := TRUE;
EK_Acceptor : CanPass := FALSE;
EK_NeedObject : CanPass := FALSE;
EK_OpenClose : CanPass := FALSE;
EK_Password : CanPass := FALSE;
Otherwise CanPass := FALSE;
END;
END;
(* ======================================================================== *)
PROCEDURE KillTree(VAR List : RoomListPtr);
VAR
Loop : INTEGER;
BEGIN
IF List <> NIL THEN
BEGIN
FOR Loop := 1 TO MaxExit DO
KillTree(List^.Children[Loop]);
DISPOSE(List);
END;
END;
(* ======================================================================== *)
FUNCTION GetNextMove(Start : INTEGER; VAR List : RoomListPtr) : INTEGER;
BEGIN
IF List = NIL THEN
GetNextMove := 0
ELSE
BEGIN
IF (List^.Parent^.RoomNumber = Start) THEN
GetNextMove := List^.DirFrom
ELSE
GetNextMove := GetNextMove(Start, List^.Parent);
END;
END;
(* ======================================================================== *)
FUNCTION FindShortestPath(Start, Finish : INTEGER;
VAR Next : INTEGER) : INTEGER;
VAR
Top, OldList, NewList : RoomListPtr;
Rooms : AllRoomType;
Found : BYTE_BOOL;
Loop : INTEGER;
Dest : INTEGER;
S : String;
Count : INTEGER;
BEGIN
Next := -1;
FindShortestPath := -1;
Count := 0;
NewList := NIL;
OldList := NIL;
Rooms := AllRooms;
Rooms[Start].Used := TRUE;
AddToNewList(NewList,OldList,Start,0,Start);
Top := NewList;
Found := Start = Finish;
WHILE ((OldList <> NIL) AND (NOT FOUND)) DO
BEGIN
NewList := NIL;
WHILE ((OldList <> NIL) AND (NOT Found)) DO
BEGIN
Loop := 1;
WHILE ((Loop <= MaxExit) AND (NOT Found)) DO
BEGIN
Dest := Rooms[OldList^.RoomNumber].Exits[Loop].Where;
IF (Dest <> 0) THEN
BEGIN
IF (NOT Rooms[Dest].Used) THEN
BEGIN
IF CanPass(Rooms[Oldlist^.RoomNumber].Exits[Loop].Kind) THEN
BEGIN
IF (Dest = Finish) THEN
Found := TRUE;
AddToNewList(Newlist, Oldlist, Dest, Loop, Dest);
Rooms[Dest].Used := TRUE;
END;
END;
END;
IF NOT Found THEN Loop := Loop + 1;
END; (* While there is still an exit left *)
IF NOT Found THEN
OldList := OldList^.Next;
END; (* While there is something left in the list and we are not done *)
Count := Count + 1;
IF NOT Found THEN OldList := NewList
ELSE OldList := OldList^.Children[Loop];
END;
IF Found THEN
BEGIN
IF Count > 0 THEN Next := GetNextMove(Start, OldList)
ELSE Next := -2;
FindShortestPath := Count;
END;
KillTree(Top);
END;
(* ======================================================================== *)
PROCEDURE ActSearch(Slot, Targ : INTEGER; VAR Here : Room;
VAR AllStats : AllMyStats);
VAR
Tries, SSlot : INTEGER;
Found : BYTE_BOOL;
BEGIN
IF (Here.People[AllStats.Stats.Slot].Hiding = 0) THEN
BEGIN
Tries := 0;
REPEAT
Tries := Tries + 1;
SSlot := RND(10);
Found := (Here.People[SSlot].Kind <> 0) AND
(Here.People[SSlot].Hiding > 0) AND
(RND(MaxHide) >= Here.People[SSlot].Hiding);
IF Found THEN
BEGIN
Here.People[SSlot].Hiding := 0;
IF SaveRoom(AllStats.Stats.Location, Here) THEN
LogEvent(AllStats.Stats.Slot, 0, E_FOUNDYOU, SSlot, Targ,
0,0, AllStats.Stats.Name, AllStats.Stats.Location);
END;
UNTIL Found OR (Tries > 10);
Allstats.Tick.TkEvent := GetTicks + ROUND(AllStats.Stats.MoveSpeed/10);
END;
IF (NOT Found) THEN
LogEvent(AllStats.Stats.Slot, 0, E_SEARCH, 0,0, 0,0,
AllStats.Stats.Name, AllStats.Stats.Location);
END;
(* -------------------------------------------------------------------------- *)
PROCEDURE ActAttack(Slot, Targ : INTEGER; VAR Here : Room;
VAR AllStats : AllMyStats);
(* In this future, this should be modified to use the routines from *)
(* monattk *)
VAR
Damage : INTEGER;
Weapon : String := 'claws';
BEGIN
Damage := AllStats.MyHold.BaseDamage + RND(AllStats.MyHold.RandomDamage);
IF Here.People[AllStats.Stats.Slot].Hiding > 0 THEN
BEGIN
Here.People[AllStats.Stats.Slot].Hiding := 0;
LogEvent(AllStats.Stats.Slot, AllStats.Stats.Log, E_MSG, 0,0, 0,0, 'Surprise!',
AllStats.Stats.Location);
Damage := AllStats.MyHold.BaseDamage + AllStats.MyHold.RandomDamage;
END;
LogEvent(AllStats.Stats.Slot, AllStats.Stats.Log, E_ATTACK, Slot, Targ,
Damage, 0, Weapon, AllStats.Stats.Location);
Allstats.Tick.TkEvent := GetTicks + ROUND(AllStats.Stats.AttackSpeed/10);
END;
(* -------------------------------------------------------------------------- *)
FUNCTION LookUpLog(Log : INTEGER; Here : Room) : INTEGER;
VAR
Loop : INTEGER;
Temp : INTEGER := 0;
BEGIN
FOR Loop := 1 TO MaxPeople DO
IF Here.People[Loop].Kind = Log THEN
IF Temp = 0 THEN
Temp := Loop;
LookupLog := Temp;
END;
(* -------------------------------------------------------------------------- *)
PROCEDURE ActFighter(VAR Here : Room; Slot : INTEGER; Targ : INTEGER;
VAR AllStats : AllMyStats);
BEGIN
IF Slot <> 0 THEN
IF Here.People[Slot].Hiding > 0 THEN
ActSearch(Slot, Targ, Here, AllStats)
ELSE
ActAttack(Slot, Targ, Here, AllStats);
END;
(* -------------------------------------------------------------------------- *)
FUNCTION IsPlaying(Log : INTEGER) : BYTE_BOOL;
VAR
Indx : IndexRec;
BEGIN
IF GetIndex(I_ASLEEP, Indx) THEN
IsPlaying := NOT Indx.Free[Log]
ELSE
IsPlaying := FALSE;
END;
(* -------------------------------------------------------------------------- *)
PROCEDURE ActMonster(Targ : INTEGER; Dir : INTEGER; VAR AllStats : AllMyStats);
VAR
Here : Room;
HereDesc : RoomDesc;
Loc : IntArray;
Slot : INTEGER;
Next : INTEGER;
Num : INTEGER;
BEGIN
IF Targ = 0 THEN
BEGIN
CASE AllStats.Stats.Group OF
OTHERWISE AllStats.Tick.TkEvent := GetTicks + 20;
END;
END
ELSE
BEGIN
IF GetInt(N_LOCATION, Loc) THEN
BEGIN
IF Loc[Targ] = AllStats.Stats.Location THEN
BEGIN
IF GetRoom(AllStats.Stats.Location, Here) THEN
BEGIN
Slot := LookupLog(Targ, Here);
CASE AllStats.Stats.Group OF
RM_FIGHTER : ActFighter(Here, Slot, Targ, AllStats);
OTHERWISE ActFighter(Here, Slot, Targ, AllStats);
END;
END;
END
ELSE
BEGIN
IF Dir > 0 THEN
BEGIN
IF GetRoom(AllStats.Stats.Location, Here) AND
GetRoomDesc(AllStats.Stats.Location, HereDesc) THEN
DoGo(Direct[Dir], , AllStats);
END;
END;
END;
END;
END;
(* ------------------------------------------------------------------------ *)
(* ------------------------------------------------------------------------ *)
(* Following is the code to determine a randoms target. *)
(* AddTarget adds a person to a Randoms Hate List. *)
(* ------------------------------------------------------------------------ *)
PROCEDURE AddTarget(VAR This : Hymie; Log : INTEGER);
VAR
Walk, Back : hateptr;
Found, Done : BYTE_BOOL;
BEGIN
Walk := This.Hate;
Back := NIL;
Done := Walk = NIL;
Found := FALSE;
WHILE (NOT DONE) DO
BEGIN
IF Walk^.Log = Log THEN
BEGIN
Done := TRUE;
Found := TRUE;
Walk^.Times := Walk^.Times + 1;
END
ELSE
BEGIN
Back := Walk;
Walk := Walk^.Next;
END;
IF Walk = NIL THEN Done := TRUE;
END;
IF (NOT FOUND) THEN
BEGIN
New(Walk);
Walk^.Next := NIL;
Walk^.Log := Log;
Walk^.Times := 1;
IF Back = NIL THEN
This.Hate := Walk
ELSE
Back^.Next := Walk;
END;
END;
(* ------------------------------------------------------------------------ *)
(* Pick a target for this random, given his hate list. *)
(* ------------------------------------------------------------------------ *)
FUNCTION PickTarget(This : Hymie; VAR Dir : INTEGER) : INTEGER;
VAR
temp : hateptr;
level, dist : integer;
num, next, hate, times, loop : integer;
asleep : indexrec;
where : intarray;
me : integer;
BEGIN
me := this.all.stats.log;
GetIndex(I_ASLEEP, Asleep);
GetInt(N_LOCATION, Where);
hate := 0;
level := -1;
dist := 100;
for loop := 1 to asleep.top do
begin
if ((not (asleep.free[loop])) and (loop <> me)) then
begin
temp := this.hate;
times := 0;
while temp <> nil do
begin
if (temp^.log = loop) then
times := temp^.times;
temp := temp^.next;
end;
num := FindShortestPath(this.all.stats.location, where[loop], Next);
if (num >= 0) THEN
if (num < dist) then
(* if times > level then *)
begin
dist := num;
hate := loop;
level := times;
dir := next;
end;
end;
end;
picktarget := hate;
END;
(* -------------------------------------------------------------------------- *)
PROCEDURE HandEvent(e : anevent; var a : hymie);
VAR
TEMPI : INTEGER;
BEGIN
CASE e.action OF
E_ATTACK:
IF e.Targ = a.all.stats.slot THEN
BEGIN
Writeln('Someone tried to hit me! (', e.SendLog:0, ')');
AddTarget(a, e.SendLog);
a.All.Stats.LastHit := e.sendlog;
a.All.Stats.LastHitString := here.people[e.send].name+'''s '+e.msg;
tempi := a.all.stats.deaths;
TakeHit(e.param[1], TRUE, a.all);
if (tempi <> a.all.stats.deaths) then begin
a.all.stats.wealth := a.all.stats.bank;
writeln('I died...!!!!');
end;
END
ELSE
Writeln('Someone hit someone else..(', e.SendLog:0, ')->(',
e.TargLog:0, ')');
END;
END;
(* -------------------------------------------------------------------------- *)
PROCEDURE ReadEvent(var a : hymie);
VAR
ea : eventarray;
slot, myev : integer;
BEGIN
IF GetTicks >= a.all.tick.tkrandomevent THEN
BEGIN
a.all.tick.tkrandomevent := a.all.tick.tkrandomevent + 40;
RndEvent(a.all);
TimeUnwho(a.all.Stats.Log, a.all.stats.location, FALSE);
END;
slot := a.all.stats.location mod (numeventrec-1) + 1;
if (getevent(slot, ea, false)) then
begin
myev := a.all.stats.eventnum;
while ((ea.point <> a.all.stats.eventnum) and (myev = a.all.stats.eventnum)) do
begin
a.all.stats.eventnum := a.all.stats.eventnum + 1;
myev := myev + 1;
if (myev > numevents) then
myev := 1;
if a.all.stats.eventnum > numevents then
a.all.stats.eventnum := 1;
IF (ea.events[a.all.stats.eventnum].Loc = A.all.Stats.Location) AND
(ea.events[a.all.stats.eventnum].SendLog <> A.all.stats.Log) AND
((ea.events[a.all.stats.eventnum].TargLog = A.all.Stats.Log) or
(ea.events[a.all.stats.eventnum].TargLog = 0)) THEN
BEGIN
handevent(ea.events[a.all.stats.eventnum], a);
END;
end;
end;
END;
(* -------------------------------------------------------------------------- *)
VAR
TimerContext : UNSIGNED := 0;
PROCEDURE SetupGuts;
BEGIN
LIB$INIT_TIMER(TimerContext);
Seed := Clock;
END;
(* -------------------------------------------------------------------------- *)
PROCEDURE MoveRandoms(User : String := 'orc');
VAR
this : hymie;
NextEvent : INTEGER;
Targ, ack : integer;
Dir : INTEGER;
BEGIN
This.All.Stats.Log := -1;
Setup_Guts;
Finish_guts;
Init(This.All);
Writeln('Entered in Random Playing mode.');
NextEvent := MAXINT;
This.All.Stats.Name := user;
This.All.Stats.Userid := user;
IF ReadRooms(AllRooms) THEN
BEGIN
IF EnterUniverse(, This.All) THEN;
This.Hate := NIL;
This.Want := NIL;
ack := 0;
This.All.Stats.Brief := TRUE;
REPEAT
IF GetTicks > This.All.Tick.TkEvent THEN
BEGIN
Targ := PickTarget(This, Dir);
ActMonster(Targ, Dir, This.All);
ReadEvent(This);
END;
Wait((This.All.Tick.TkEvent-GetTicks)/10);
UNTIL FALSE;
END;
END;
(* -------------------------------------------------------------------------- *)
BEGIN
Status := LIB$Get_foreign(Passed.Body,,Passed.Length);
IF NOT ODD(Status) THEN
LIB$SIGNAL(Status);
Passed := lowcase(Passed);
if (passed.length = 0) THEN MoveRandoms
else MoveRandoms(passed);
END.