-
Notifications
You must be signed in to change notification settings - Fork 2
/
cwords.pp
416 lines (349 loc) · 9.13 KB
/
cwords.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
unit cwords;
{This unit is brought to you by the letter "C".}
{It handles two different "C" words - Clouds and}
{Computers.}
interface
uses crt,texmodel,texmaps;
Type
CloudDesc = Record
name: String;
color: Byte;
Obscurement: SmallInt;
Pass: Boolean;
end;
CloudPtr = ^Cloud;
Cloud = Record
Kind: Integer;
Duration: LongInt; {The ComTime at which the cloud dissipates.}
M: ModelPtr;
Next: CloudPtr;
end;
MPUDesc = Record
name: String;
color: Byte;
SecPass: Integer;
end;
MPUPtr = ^MPU;
MPU = Record {This is the record that describes computer}
{terminals. The name, MPU, comes from the}
{anime series Cowboy Bebop.}
Kind: Integer;
M: ModelPtr;
Attr: String; { MPU Attributes }
Next: MPUPtr;
end;
Const
NumCloud = 3;
{ NOTE: A cloud with an obscurement value of -1 will completely block }
{ line of sight, and cannot even be fired through. Such a cloud should }
{ have Pass set to False, or bad things may result. }
CloudMan: Array [1..NumCloud] of CloudDesc = (
( name: 'Etherial Mist';
color: LightCyan;
Obscurement: 3;
pass: True; ),
( name: 'Smoke Screen';
color: LightGray;
Obscurement: 3;
pass: True; ),
( name: 'Force Field';
color: Yellow;
Obscurement: -1;
pass: False; )
);
NumMPU = 4;
MPUMan: Array [1..NumMPU] of MPUDesc = (
( name: 'Info Terminal';
color: Yellow;
SecPass: 2; ),
( name: 'Medical Unit';
color: Red;
SecPass: 12; ),
( name: 'Primary Server "Morgan"';
color: LightMagenta;
SecPass: 9; ),
( name: 'Primary Server "DesCartes"';
color: Green;
SecPass: 10; )
);
MKIND_Cloud = 3;
MKIND_MPU = 4;
CloudGFX = '*';
MPUGFX = '&';
Function AddCloud(var CList: CloudPtr; gb: GameBoardPtr; C,X,Y: Integer; D: LongInt): Cloudptr;
Procedure DisposeCloud(var LList: CloudPtr);
Procedure CleanCloud( gb: GameBoardPtr; var LList: CloudPtr);
Procedure RemoveCloud(var LList,LMember: CloudPtr; gb: GameBoardPtr);
Function LocateCloud(M: ModelPtr; C: CloudPtr): CloudPtr;
Function AddMPU(var CList: MPUPtr; gb: GameBoardPtr; C,X,Y: Integer): MPUptr;
Procedure DisposeMPU(var LList: MPUPtr);
Procedure RemoveMPU(var LList,LMember: MPUPtr; gb: GameBoardPtr);
Function LocateMPU(M: ModelPtr; C: MPUPtr): MPUPtr;
Procedure WriteClouds(CL: CloudPtr; var F: Text);
Function ReadClouds(var F: Text; gb: GameBoardPtr): CloudPtr;
Procedure WriteMPU(CL: MPUPtr; var F: Text);
Function ReadMPU(var F: Text; gb: GameBoardPtr): MPUPtr;
implementation
Function AddCloud(var CList: CloudPtr; gb: GameBoardPtr; C,X,Y: Integer; D: LongInt): Cloudptr;
{Add a cloud to the map at location X,Y.}
var
it: CloudPtr;
begin
{Allocate memory for IT.}
New(it);
if it = Nil then Exit(Nil);
{Attach IT to the list.}
it^.Next := CList;
CList := it;
it^.Kind := C;
it^.Duration := D;
it^.M := GAddModel(gb,CloudGFX,CloudMan[C].color,White,CloudMan[C].Pass,X,Y,MKIND_Cloud);
if it^.M = Nil then begin
Dispose(it);
it := Nil;
end
else begin
{Set the obscurement for this model.}
it^.M^.Obs := CloudMan[C].Obscurement;
end;
AddCloud := it;
end;
Procedure DisposeCloud(var LList: CloudPtr);
{Dispose of the list, freeing all associated system resources.}
{The models associated with each cloud will have to be removed}
{somewhere else, since this unit ain't messing with them.}
var
LTemp: CloudPtr;
begin
while LList <> Nil do begin
LTemp := LList^.Next;
Dispose(LList);
LList := LTemp;
end;
end;
Procedure CleanCloud( gb: GameBoardPtr; var LList: CloudPtr);
{Dispose of the list, freeing all associated system resources.}
{The models associated with each cloud will also be removed.}
var
LTemp: CloudPtr;
begin
while LList <> Nil do begin
LTemp := LList^.Next;
RemoveModel( LList^.M , GB^.MList , GB^.Mog );
Dispose(LList);
LList := LTemp;
end;
end;
Procedure RemoveCloud(var LList,LMember: CloudPtr; gb: GameBoardPtr);
{Locate and extract member LMember from list LList.}
{Then, dispose of LMember.}
var
a,b: CloudPtr;
begin
{Initialize A and B}
B := LList;
A := Nil;
{Locate LMember in the list. A will thereafter be either Nil,}
{if LMember if first in the list, or it will be equal to the}
{element directly preceding LMember.}
while (B <> LMember) and (B <> Nil) do begin
A := B;
B := B^.next;
end;
if B = Nil then begin
{Major FUBAR. The member we were trying to remove can't}
{be found in the list.}
writeln('ERROR- RemoveLink asked to remove a link that doesnt exist.');
end
else if A = Nil then begin
{There's no element before the one we want to remove,}
{i.e. it's the first one in the list.}
LList := B^.Next;
{Get rid of the model.}
GRemoveModel(B^.M,gb);
Dispose(B);
B := Nil;
end
else begin
{We found the attribute we want to delete and have another}
{one standing before it in line. Go to work.}
A^.next := B^.next;
{Get rid of the model.}
GRemoveModel(B^.M,gb);
Dispose(B);
B := Nil;
end;
end;
Function LocateCloud(M: ModelPtr; C: CloudPtr): CloudPtr;
{Given model M, locate the cloud that is being referred to.}
{Return Nil if no such cloud can be found.}
var
it: CloudPtr;
begin
it := Nil;
while (C <> Nil) and (it = Nil) do begin
if C^.M = M then it := C;
C := C^.Next;
end;
LocateCloud := it;
end;
Function AddMPU(var CList: MPUPtr; gb: GameBoardPtr; C,X,Y: Integer): MPUptr;
{Add a MPU to the map at location X,Y.}
var
it: MPUPtr;
begin
{Allocate memory for IT.}
New(it);
if it = Nil then Exit(Nil);
{Attach IT to the list.}
it^.Next := CList;
CList := it;
it^.Kind := C;
it^.M := GAddModel(gb,MPUGFX,MPUMan[C].color,White,False,X,Y,MKIND_MPU);
if it^.M = Nil then begin
Dispose(it);
it := Nil;
end;
AddMPU := it;
end;
Procedure DisposeMPU(var LList: MPUPtr);
{Dispose of the list, freeing all associated system resources.}
{The models associated with each MPU will have to be removed}
{somewhere else, since this unit ain't messing with them.}
var
LTemp: MPUPtr;
begin
while LList <> Nil do begin
LTemp := LList^.Next;
Dispose(LList);
LList := LTemp;
end;
end;
Procedure RemoveMPU(var LList,LMember: MPUPtr; gb: GameBoardPtr);
{Locate and extract member LMember from list LList.}
{Then, dispose of LMember.}
var
a,b: MPUPtr;
begin
{Initialize A and B}
B := LList;
A := Nil;
{Locate LMember in the list. A will thereafter be either Nil,}
{if LMember if first in the list, or it will be equal to the}
{element directly preceding LMember.}
while (B <> LMember) and (B <> Nil) do begin
A := B;
B := B^.next;
end;
if B = Nil then begin
{Major FUBAR. The member we were trying to remove can't}
{be found in the list.}
writeln('ERROR- RemoveLink asked to remove a link that doesnt exist.');
end
else if A = Nil then begin
{There's no element before the one we want to remove,}
{i.e. it's the first one in the list.}
LList := B^.Next;
{Get rid of the model.}
GRemoveModel(B^.M,gb);
Dispose(B);
B := Nil;
end
else begin
{We found the attribute we want to delete and have another}
{one standing before it in line. Go to work.}
A^.next := B^.next;
{Get rid of the model.}
GRemoveModel(B^.M,gb);
Dispose(B);
B := Nil;
end;
end;
Function LocateMPU(M: ModelPtr; C: MPUPtr): MPUPtr;
{Given model M, locate the MPU that is being referred to.}
{Return Nil if no such MPU can be found.}
var
it: MPUPtr;
begin
it := Nil;
while (C <> Nil) and (it = Nil) do begin
if C^.M = M then it := C;
C := C^.Next;
end;
LocateMPU := it;
end;
Procedure WriteClouds(CL: CloudPtr; var F: Text);
{Save the linked list of clouds to the file F.}
begin
while CL <> Nil do begin
writeln(F,CL^.kind);
{Record the position of the cloud}
writeln(F,CL^.M^.X);
writeln(F,CL^.M^.Y);
writeln(F,CL^.duration);
CL := CL^.Next;
end;
writeln(F,-1);
end;
Function ReadClouds(var F: Text; gb: GameBoardPtr): CloudPtr;
{Read a list of clouds from disk.}
var
CList: CloudPtr;
N,X,Y,D: Integer; {Cloud info.}
begin
{Initialize the list to NIL.}
CList := Nil;
{Keep reading data until we get a termination value, -1.}
repeat
ReadLn(F,N);
{If this isn't the termination character, add this cloud to}
{the list.}
if N <> -1 then begin
{Read the rest of the cloud data.}
ReadLn(F,X);
ReadLn(F,Y);
ReadLn(F,D);
{Add this cloud to the list.}
AddCloud(CList,gb,N,X,Y,D);
end;
until N = -1;
ReadClouds := CList;
end;
Procedure WriteMPU(CL: MPUPtr; var F: Text);
{Save the linked list of computers to the file F.}
begin
while CL <> Nil do begin
writeln(F,CL^.kind);
{Record the position of the computer}
writeln(F,CL^.M^.X);
writeln(F,CL^.M^.Y);
writeln( F , CL^.Attr );
CL := CL^.Next;
end;
writeln(F,-1);
end;
Function ReadMPU(var F: Text; gb: GameBoardPtr): MPUPtr;
{Read a list of computers from disk.}
var
CList,Current: MPUPtr;
N,X,Y: Integer; {Computer info.}
begin
{Initialize the list to NIL.}
CList := Nil;
{Keep reading data until we get a termination value, -1.}
repeat
ReadLn(F,N);
{If this isn't the termination character, add this cloud to}
{the list.}
if N <> -1 then begin
{Read the rest of the cloud data.}
ReadLn(F,X);
ReadLn(F,Y);
{Add this computer to the list.}
Current := AddMPU(CList,gb,N,X,Y);
ReadLn( F , Current^.Attr );
end;
until N = -1;
ReadMPU := CList;
end;
end.