This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.py
468 lines (363 loc) · 15.3 KB
/
start.py
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
from os.path import isdir, isfile
from sys import exit, argv
from time import sleep
from pathlib import Path
from random import choice
from argparse import ArgumentParser
import pygame
import state
from media import play_video, play_sound, is_sound_playing, load_bmp, scale_point
from savegame import savegame, loadgame
from parser import game_parser
from compiler import compile_lines
from engine import run_statement, run_setflag
from cursor import load_cursors
pgv = pygame.__version__
if len(pgv) < 2 or pgv[:2] != '2.':
print("pygame %s was found, but 2.x is required. To upgrade, run: pip3 install pygame --user --upgrade" % pgv)
exit(-1)
def render_cursor_hand(mask, x, y, c):
#print(c)
if mask is None:
return False
if c == "NULL" or c == 0:
return False
(bmp, ox, oy) = mask
xm = x - ox
ym = y - oy
if xm < 0 or ym < 0:
return False
mask = pygame.mask.from_surface(bmp)
msize = mask.get_size()
if (xm >= msize[0] or ym >= msize[1]):
pass
elif mask.get_at((xm, ym)) == 1:
pygame.mouse.set_cursor(*state.cursors[c])
return True
return False
def render_dossier():
(bmp, x, y) = state.dossier_previous_sheet
state.screen.blit(bmp, [x, y])
(bmp, x, y) = state.dossier_next_sheet
state.screen.blit(bmp, [x, y])
csus = state.dossier_current_suspect
cshe = state.dossier_current_sheet
bmp = load_bmp(state.dossiers[csus][cshe])
state.screen.blit(bmp, [x, y])
(bmp, x, y) = state.dossier_previous_suspect
state.screen.blit(bmp, [x, y])
(bmp, x, y) = state.dossier_next_suspect
state.screen.blit(bmp, [x, y])
pygame.display.flip()
def set_cursor(x, y):
# save/load masks
if render_cursor_hand(state.load_game, x, y, "kExit"):
return
if render_cursor_hand(state.save_game, x, y, "kExit"):
return
# dossier related masks
if render_cursor_hand(state.dossier_next_sheet, x, y, "kExit"):
return
if render_cursor_hand(state.dossier_previous_sheet, x, y, "kExit"):
return
if render_cursor_hand(state.dossier_next_suspect, x, y, "kExit"):
return
if render_cursor_hand(state.dossier_previous_suspect, x, y, "kExit"):
return
# sound areas
for (bmp, _) in state.sareas:
if render_cursor_hand((bmp, 0, 0), x, y, "kExit"):
return
# general masks
for (bmp, ox, oy, _, _, c) in state.masks:
if render_cursor_hand((bmp, ox, oy), x, y, c):
return
# exits
current_exit_size = None
selected_exit = None
selected_cursor = None
for (xs, ys, xe, ye, e, c) in state.exits:
if (x>=xs and x<=xe):
if (y>=ys and y<=ye):
exit_size = (xs-xe)*(ys-ye)
#print("matched", new_setting, exit_size)
assert(exit_size > 0)
if current_exit_size is None or exit_size < current_exit_size:
selected_exit = e
selected_cursor = c
current_exit_size = exit_size
if selected_exit is not None and selected_exit != "NULL" and selected_exit != 0:
if c != "NULL" and c != 0:
pygame.mouse.set_cursor(*state.cursors[selected_cursor])
return
# default cursor
pygame.mouse.set_system_cursor(pygame.SYSTEM_CURSOR_ARROW)
def check_for_events():
# incoming phone call
if state.mode == 0 and "kPhone" in state.sounds and len(state.sounds["kPhone"]) > 0:
filename = state.phone_path + state.phone_sound
if (not is_sound_playing()):
play_sound(filename, 0)
(x,y) = pygame.mouse.get_pos()
set_cursor(x,y)
for event in pygame.event.get():
# Did the user click the window close button?
if event.type == pygame.QUIT:
exit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return True
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x,y = event.pos
# General masks
for (bmp, ox, oy, new_setting, v, _) in state.masks:
xm = x - ox
ym = y - oy
if xm < 0 or ym < 0:
return False
mask = pygame.mask.from_surface(bmp)
msize = mask.get_size()
if (xm >= msize[0] or ym >= msize[1]):
continue
print("mask", mask.get_at((xm, ym)))
if mask.get_at((xm, ym)) == 1:
oscreen = state.screen.copy()
state.screen.blit(bmp, [ox, oy])
pygame.display.flip()
sleep(0.2)
state.screen.blit(oscreen, [0, 0])
pygame.display.flip()
if new_setting != "NULL" and new_setting != 0:
assert(new_setting in state.settings)
state.next_setting = new_setting
if v != "NULL" and v != 0:
assert(v in state.definitions['variables'])
run_setflag(v, "TRUE")
return True
# Sound Areas
for (bmp, s) in state.sareas:
xm = x
ym = y
mask = pygame.mask.from_surface(bmp)
msize = mask.get_size()
if (xm >= msize[0] or ym >= msize[1]):
continue
print("sound area", mask.get_at((xm, ym)))
if mask.get_at((xm, ym)) == 1:
if s == "kPoliceRadio":
ss = list(state.sounds["kPoliceRadio"].items())
if len(ss) > 0:
(s, _) = choice(list(state.sounds["kPoliceRadio"].items()))
filename = state.police_radio_path + s
play_sound(filename, 0)
state.played_sounds.append(s)
del state.sounds["kPoliceRadio"][s]
elif s == "kAMRadio":
ss = list(state.sounds["kAMRadio"].items())
if len(ss) > 0:
(s, _) = choice(list(state.sounds["kAMRadio"].items()))
filename = state.am_radio_path + s
play_sound(filename, 0)
del state.sounds["kAMRadio"][s]
elif s == "kPhone":
ss = list(state.sounds["kPhone"].items())
if len(ss) > 0:
(s, (v, x)) = choice(list(state.sounds["kPhone"].items()))
filename = state.phone_path + s
play_sound(filename, 0)
if v != "NULL" and v != 0:
assert(v in state.definitions['variables'])
run_setflag(v, x)
state.played_sounds.append(s)
del state.sounds["kPhone"][s]
else:
print("Invalid sound added")
assert(False)
return True
# Load/Save game
if state.save_game is not None:
(bmp, ox, oy) = state.save_game
xm = x - ox
ym = y - oy
if xm < 0 or ym < 0:
return False
mask = pygame.mask.from_surface(bmp)
msize = mask.get_size()
if (xm >= msize[0] or ym >= msize[1]):
continue
if mask.get_at((xm, ym)) == 1:
savegame()
return True
if state.load_game is not None:
(bmp, ox, oy) = state.load_game
xm = x - ox
ym = y - oy
if xm < 0 or ym < 0:
return False
mask = pygame.mask.from_surface(bmp)
msize = mask.get_size()
if (xm >= msize[0] or ym >= msize[1]):
continue
if mask.get_at((xm, ym)) == 1:
loadgame()
return True
# Dossiers handling
if state.dossier_next_sheet is not None:
(bmp, ox, oy) = state.dossier_next_sheet
xm = x - ox
ym = y - oy
if xm < 0 or ym < 0:
return False
mask = pygame.mask.from_surface(bmp)
msize = mask.get_size()
if (xm >= msize[0] or ym >= msize[1]):
continue
print("dossier_next_sheet mask", mask.get_at((xm, ym)))
nd = state.dossier_current_sheet + 1
cs = state.dossier_current_suspect
if mask.get_at((xm, ym)) == 1:
if nd < len(state.dossiers[cs]) and state.dossiers[cs][nd] is not None:
state.dossier_current_sheet = nd
render_dossier()
return False
if state.dossier_previous_sheet is not None:
(bmp, ox, oy) = state.dossier_previous_sheet
xm = x - ox
ym = y - oy
if xm < 0 or ym < 0:
return False
mask = pygame.mask.from_surface(bmp)
msize = mask.get_size()
if (xm >= msize[0] or ym >= msize[1]):
continue
print("dossier_previous_sheet mask", mask.get_at((xm, ym)))
pd = state.dossier_current_sheet - 1
cs = state.dossier_current_suspect
if mask.get_at((xm, ym)) == 1:
if pd >= 0 and state.dossiers[cs][pd] is not None:
state.dossier_current_sheet = pd
render_dossier()
return False
if state.dossier_next_suspect is not None:
(bmp, ox, oy) = state.dossier_next_suspect
xm = x - ox
ym = y - oy
if xm < 0 or ym < 0:
return False
mask = pygame.mask.from_surface(bmp)
msize = mask.get_size()
if (xm >= msize[0] or ym >= msize[1]):
continue
print("dossier_next_suspect mask", mask.get_at((xm, ym)))
ns = state.dossier_current_suspect + 1
if mask.get_at((xm, ym)) == 1:
if ns < len(state.dossiers):
state.dossier_current_suspect = ns
state.dossier_current_sheet = 0
render_dossier()
return False
if state.dossier_previous_suspect is not None:
(bmp, ox, oy) = state.dossier_previous_suspect
xm = x - ox
ym = y - oy
if xm < 0 or ym < 0:
return False
mask = pygame.mask.from_surface(bmp)
msize = mask.get_size()
if (xm >= msize[0] or ym >= msize[1]):
continue
print("dossier_next_suspect mask", mask.get_at((xm, ym)))
pd = state.dossier_current_suspect - 1
if mask.get_at((xm, ym)) == 1:
if pd >= 0:
state.dossier_current_suspect = pd
state.dossier_current_sheet = 0
render_dossier()
return False
current_exit_size = None
for (xs, ys, xe, ye, new_setting, _) in state.exits:
if (x>=xs and x<=xe):
if (y>=ys and y<=ye):
exit_size = (xs-xe)*(ys-ye)
#print("matched", new_setting, exit_size)
assert(exit_size > 0)
if current_exit_size is None or exit_size < current_exit_size:
if new_setting == "NULL" or new_setting == 0:
state.next_setting = None
else:
state.next_setting = new_setting
current_exit_size = exit_size
if current_exit_size is not None:
return True
return False
if __name__ == '__main__':
# Arguments
parser = ArgumentParser(description='re-private-eye')
parser.add_argument("CDROM", help="Path to CDROM", type=str)
parser.add_argument("-height", help="Screen height", type=int, default=1024)
parser.add_argument("-width", help="Screen height", type=int, default=768)
options = parser.parse_args()
state.height, state.width = options.height, options.width
state.cdrom_path = options.CDROM
if not isdir(state.cdrom_path):
print(state.cdrom_path, "does not exists")
exit(-1)
pygame.init()
pygame.font.init() # you have to call this at the start,
state.font = pygame.font.SysFont(pygame.font.get_default_font(), 70)
# Set up the drawing window
state.screen = pygame.display.set_mode((state.height, state.width))#, pygame.FULLSCREEN)
state.gorigin = [0, 0]
pygame.display.set_caption("Private Eye (1996) re-implementation")
data = None
if isfile('assets/GAME.DAT'):
with open('assets/GAME.DAT') as f:
data = f.read()
if isfile('assets/GAME.TXT'):
with open('assets/GAME.TXT') as f:
data = f.read()
if data is None:
print("Cannot find full game (assets/GAME.DATA) or demo (assets/GAME.TXT)")
exit(-1)
state.save_path = Path(__file__).parent.absolute()
state.cursors = load_cursors()
print("Parsing game assets..")
ls = game_parser.parse(data)
print("Compiling assets..")
compile_lines(ls)
print("Executing game..")
state.next_setting = "kIntro"
while True:
if check_for_events():
pass
elif (state.video_to_play != None):
filename, next_setting = state.video_to_play
play_video(filename, check_for_events)
state.next_setting = next_setting
if state.next_setting != None:
state.current_setting = state.next_setting
print("CURRENT SETTING:", state.current_setting, state.mode)
state.exits = []
state.masks = []
state.sareas = []
state.dossier_next_sheet = None
state.dossier_previous_sheet = None
state.dossier_next_suspect = None
state.dossier_previous_suspect = None
state.save_game = None
state.load_game = None
state.video_to_play = None
state.next_setting = None
if (state.current_setting in state.settings):
if state.settings[state.current_setting] == []:
print("WARNING", state.current_setting, "is empty")
break
for st in state.settings[state.current_setting]:
#print("EXECUTING",st.pretty())
run_statement(st)
else:
raise SyntaxError('I can\'t find setting: %s' % state.current_setting)
#brighten = 32
#screen.fill((brighten, brighten, brighten), special_flags=pygame.BLEND_RGB_ADD)
pygame.display.flip()
pygame.quit()