-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPROJT.PAS
executable file
·381 lines (330 loc) · 11.1 KB
/
GPROJT.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
PROGRAM GameProject;
{ NOTE: Please run on DOSBox with 52000 cycles or more }
USES CRT, DOS;
CONST
GameFieldWidth = 60;
GameFieldHeight = 48;
LightCalculationInterval = 36; { 21 with rad 8, 36 with rad 14}
MonsterLimit = 25;
TYPE
Vector = RECORD
X, Y: INTEGER;
END;
PlayerRecord = RECORD
Position: VECTOR;
DeltaPosition: VECTOR;
Health: INTEGER;
Bearing: REAL;
Radius, FOV: INTEGER;
END;
Tile = RECORD
Character: CHAR;
Color, BackgroundColor: BYTE;
END;
Time = RECORD
Hour, Minute, Second, HSecond: WORD;
END;
GameField = ARRAY[1..GameFieldWidth, 1..GameFieldHeight] OF Tile;
MonsterVector = ARRAY[1..MonsterLimit] OF Vector;
CONST
{ Tiles }
{ Functional Tiles }
EnvironmentTile: Tile = (Character: CHR(219); Color: DarkGray; BackgroundColor: Black);
WallTile: Tile = (Character: CHR(219); Color: Yellow; BackgroundColor: Black);
MonsterTile: Tile = (Character: CHR(254); Color: LightRed; BackgroundColor: Red);
PlayerTile: Tile = (Character: CHR(254); Color: LightGreen; BackgroundColor: Blue);
DarkTile: Tile = (Character: CHR(219); Color: Black; BackgroundColor: Black);
{ Representative Tiles }
EmptyTile: Tile = (Character: CHR(0); Color: Blink; BackgroundColor: Magenta);
LightTile: Tile = (Character: CHR(0); Color: White; BackgroundColor: Black);
VAR
{ Utility }
Ch: CHAR;
I, J{, K, L}: INTEGER;
{ Statekeeping }
GameRunning: BOOLEAN;
OriginalMode: INTEGER;
Cycles, WorkingCycles: WORD;
LastTime, CurrentTime: TIME;
{ Other }
Player: PlayerRecord;
Field: GameField;
TorchCache: GameField;
Monsters: MonsterVector;
MonsterCount: WORD;
{ -- PROCEDURES & FUNCTIONS --}
FUNCTION KeyDown: CHAR;
VAR
Ch: CHAR;
BEGIN
IF KEYPRESSED THEN
KeyDown := READKEY
ELSE
KeyDown := CHR(0);
END;
FUNCTION Radians(Degrees: REAL): REAL;
BEGIN
Radians := Degrees * (Pi/180.0);
END;
PROCEDURE UpdateCurrentTime;
BEGIN
GetTime(CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second, CurrentTime.HSecond);
END;
FUNCTION TimestampFromTime(TheTime: TIME): LONGINT;
VAR
Result: LONGINT;
BEGIN
TimestampFromTime := (TheTime.Hour * 60 * 60 * 100) +
(TheTime.Minute * 60 * 100) +
(TheTime.Second * 100) +
(TheTime.HSecond);
END;
PROCEDURE Render(X, Y: INTEGER); FORWARD;
PROCEDURE RenderGhost(X, Y: INTEGER; ActiveTile: Tile); FORWARD;
PROCEDURE LoadLevel(Path: STRING); FORWARD;
PROCEDURE InitGame;
BEGIN
CLRSCR;
OriginalMode := LastMode;
TEXTMODE(CO80 + Font8x8); {Make the whole thing square}
{ Initialise Player }
WITH Player DO BEGIN
WITH Position DO BEGIN
X := 30;
Y := 30;
END;
WITH DeltaPosition DO BEGIN
X := 0;
Y := 0;
END;
Health := 3;
Bearing := -90.0;
Radius := 14;
FOV := 100;
END;
{ Initialise game field }
GOTOXY(1, 49);
TEXTCOLOR(WHITE);
TEXTBACKGROUND(BLACK);
LoadLevel('LEV0.LEV');
{ Initialise torch cache }
FOR I := 1 TO GameFieldHeight DO
FOR J := 1 TO GameFieldWidth DO BEGIN
TorchCache[J, I] := DarkTile;
RenderGhost(J, I, DarkTile);
END;
RenderGhost(Player.Position.X, Player.Position.Y, PlayerTile);
END;
PROCEDURE LoadLevel(Path: STRING);
VAR
LevelFile: TEXT;
Ch: CHAR;
I, J: INTEGER;
BEGIN
ASSIGN(LevelFile, Path);
RESET(LevelFile);
MonsterCount := 0;
FOR J := 1 TO GameFieldHeight DO BEGIN
FOR I := 1 TO GameFieldWidth DO BEGIN
READ(LevelFile, Ch);
CASE Ch OF
'-' : Field[I, J] := EnvironmentTile;
'#' : Field[I, J] := WallTile;
'M' : BEGIN
Field[I, J] := MonsterTile;
MonsterCount := MonsterCount + 1;
Monsters[MonsterCount].X := I;
Monsters[MonsterCount].Y := J;
END;
'@' : BEGIN
Player.Position.X := I;
Player.Position.Y := J;
Field[I, J] := EnvironmentTile;
END;
END;
END;
READ(LevelFile, Ch);
END;
CLOSE(LevelFile);
END;
PROCEDURE DeinitGame;
BEGIN
TEXTMODE(OriginalMode);
END;
PROCEDURE ResetPlayerDelta;
BEGIN
Player.DeltaPosition.X := 0;
Player.DeltaPosition.Y := 0;
END;
PROCEDURE UpdatePlayer; FORWARD;
PROCEDURE UpdateLight; FORWARD;
PROCEDURE DetectInput;
VAR
V: VECTOR;
BEGIN
CASE KeyDown OF
'q':
GameRunning := FALSE;
CHR(75), 'a': BEGIN {LEFT}
Player.Bearing := Player.Bearing - 22.5;
UpdateLight;
END;
CHR(72): BEGIN {UP}
V.X := ROUND(COS(Radians(Player.Bearing)));
V.Y := ROUND(SIN(Radians(Player.Bearing)));
Player.DeltaPosition.X := V.X;
Player.DeltaPosition.Y := V.Y;
Player.Position.X := Player.Position.X + V.X;
Player.Position.Y := Player.Position.Y + V.Y;
UpdatePlayer;
END;
CHR(77), 'd': BEGIN {RIGHT}
Player.Bearing := Player.Bearing + 22.5;
UpdateLight;
END;
CHR(80): BEGIN {DOWN}
V.X := ROUND(COS(Radians(Player.Bearing + 180.0)));
V.Y := ROUND(SIN(Radians(Player.Bearing + 180.0)));
Player.DeltaPosition.X := V.X;
Player.DeltaPosition.Y := V.Y;
Player.Position.X := Player.Position.X + V.X;
Player.Position.Y := Player.Position.Y + V.Y;
UpdatePlayer;
END;
END;
END;
FUNCTION TilesEqual(T1, T2: Tile): BOOLEAN;
BEGIN
TilesEqual := (T1.Character = T2.Character) AND (T1.Color = T2.Color) AND (T1.BackgroundColor = T2.BackgroundColor);
END;
PROCEDURE UpdateLight;
VAR
Look: VECTOR;
I, J: INTEGER;
NewTorchCache: GameField;
BEGIN
FOR I := 1 TO GameFieldHeight DO
FOR J := 1 TO GameFieldWidth DO BEGIN
NewTorchCache[J, I] := EmptyTile;
END;
{ Recalculate light }
FOR J := 0 TO LightCalculationInterval DO BEGIN
FOR I := 1 TO Player.Radius DO BEGIN
Look.X := ROUND(COS(Radians(
(Player.Bearing + ((Player.FOV/LightCalculationInterval)*J)) - (Player.FOV/2))) * I)
+ Player.Position.X;
Look.Y := ROUND(SIN(Radians(
(Player.Bearing + ((Player.FOV/LightCalculationInterval)*J)) - (Player.FOV/2))) * I)
+ Player.Position.Y;
IF (Look.X >= 1) AND (Look.X <= GameFieldWidth) AND (Look.Y >= 1) AND (Look.Y <= GameFieldHeight) THEN BEGIN
NewTorchCache[Look.X, Look.Y] := LightTile;
IF NOT(TilesEqual(NewTorchCache[Look.X, Look.Y], TorchCache[Look.X, Look.Y])) THEN
Render(Look.X, Look.Y);
IF NOT TilesEqual(Field[Look.X, Look.Y], EnvironmentTile) THEN
I := Player.Radius;
END;
END;
END;
{ Remove light from where it was }
FOR J := 1 TO GameFieldHeight DO
FOR I := 1 TO GameFieldWidth DO BEGIN
IF TilesEqual(NewTorchCache[I, J], EmptyTile)
AND TilesEqual(TorchCache[I, J], LightTile) THEN BEGIN
RenderGhost(I, J, DarkTile);
END;
END;
{ Fix player sprite }
RenderGhost(Player.Position.X, Player.Position.Y, PlayerTile);
TorchCache := NewTorchCache;
END;
PROCEDURE UpdatePlayer;
VAR
OldPosition: VECTOR;
BEGIN
WITH OldPosition DO BEGIN
X := Player.Position.X - Player.DeltaPosition.X;
Y := Player.Position.Y - Player.DeltaPosition.Y;
END;
IF TilesEqual(Field[Player.Position.X, Player.Position.Y], EnvironmentTile) AND
((Player.Position.X >= 1) AND (Player.Position.Y >= 1)) AND
((Player.Position.X <= GameFieldWidth) AND (Player.Position.Y <= GameFieldHeight)) THEN
BEGIN
RenderGhost(Player.Position.X, Player.Position.Y, PlayerTile);
RenderGhost(OldPosition.X, OldPosition.Y, DarkTile);
UpdateLight;
END
ELSE BEGIN
Player.Position := OldPosition;
END;
ResetPlayerDelta;
END;
{ Incomplete }
{PROCEDURE UpdateMonsters;
VAR
I: INTEGER;
CycleCounter: REAL;
BEGIN
TEXTCOLOR(WHITE);
TEXTBACKGROUND(BLACK);
GOTOXY(11, 50);
IF Cycles > 0 THEN
CycleCounter := WorkingCycles/Cycles
ELSE
CycleCounter := 0.0;
WRITE('| ', MonsterCount, ' / ', MonsterLimit, ' MONSTERS ', CycleCounter:2:7);
FOR I := 1 TO MonsterCount DO BEGIN
END;
END;}
{ Procedure for rendering/refreshing a tile at some coordinate }
PROCEDURE Render(X, Y: INTEGER);
VAR
ActiveTile: Tile;
BEGIN
GOTOXY(X, Y);
ActiveTile := Field[X, Y];
TextColor(ActiveTile.Color);
TextBackground(ActiveTile.BackgroundColor);
WRITE(ActiveTile.Character);
END;
{ Procedure for rendering a tile with a predefined style at some coordinate }
PROCEDURE RenderGhost(X, Y: INTEGER; ActiveTile: Tile);
BEGIN
GOTOXY(X, Y);
TextColor(ActiveTile.Color);
TextBackground(ActiveTile.BackgroundColor);
WRITE(ActiveTile.Character);
END;
{ -- MAIN PROGRAM -- }
BEGIN
GameRunning := TRUE;
InitGame;
UpdateLight;
Cycles := 0;
WorkingCycles := 0;
UpdateCurrentTime;
LastTime := CurrentTime;
WHILE GameRunning DO BEGIN
DetectInput;
{UpdateMonsters;}
{ Calculate how many cycles are run per second }
IF CurrentTime.Second = LastTime.Second THEN BEGIN
WorkingCycles := WorkingCycles + 1;
TEXTCOLOR(WHITE);
TEXTBACKGROUND(BLACK);
GOTOXY(9, 50);
WRITE('A');
END ELSE BEGIN
LastTime := CurrentTime;
Cycles := WorkingCycles;
WorkingCycles := 0;
TEXTCOLOR(WHITE);
TEXTBACKGROUND(BLACK);
GOTOXY(1, 50);
WRITE(Cycles:7);
GOTOXY(9, 50);
WRITE('R');
END;
UpdateCurrentTime;
END;
DeinitGame;
END.