-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.pb
497 lines (441 loc) · 14.8 KB
/
main.pb
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
; Lost Labyrinth VI: Portals 0.3.0
; main file
; written in PureBasic 4.30 (http://www.purebasic.com)
; created: 08.10.2008 Frank Malota <malota@web.de>
; modified: 26.01.2009 Frank Malota <malota@web.de>
; total number of hours of programming: 128
; --------------------------------------------
; Switch to PureBasic 6.0 and Github
; modified: 202206 Peter Eismann
; total number of hours of programming: 10
;-
;- Gold = e.g.: <field x="16" y="11">9</field>
;-
; include files
EnableExplicit
XIncludeFile "constants.pb"
XIncludeFile "error_handling.pb"
XIncludeFile "misc.pb"
XIncludeFile "preferences.pb"
XIncludeFile "messages.pb"
XIncludeFile "map_handling.pb"
XIncludeFile "menu.pb"
XIncludeFile "graphics.pb"
XIncludeFile "character.pb"
XIncludeFile "monster.pb"
XIncludeFile "powers.pb"
XIncludeFile "items.pb"
XIncludeFile "sound.pb"
;- debug fuction
XIncludeFile "debug.pb"
;- set current directory to program file; uncomment before compiling final version
; SetCurrentDirectory(GetPathPart(ProgramFilename()))
;- set preferences
load_preferences(@preferences)
;-save_preferences(@preferences)
;- load message list
load_message_list()
;- initialize keyboard & mouse input
If InitKeyboard() = 0
error_message("Can't open keyboard for input!")
EndIf
If InitMouse() = 0
error_message("Can't open mouse for input!")
EndIf
;- init graphics engine
UsePNGImageDecoder()
UseJPEGImageEncoder()
UseJPEGImageDecoder()
If InitSprite() = 0
isSoundSupported = #False
error_message("Can't open sprite environment!")
EndIf
;- init sound
If InitSound() = 0
error_message("Can't InitSound!")
EndIf
UseOGGSoundDecoder()
load_sound()
;- open program window
If preferences\fullscreen = 0
open_windowed_screen()
Else
open_fullscreen()
EndIf
;- load resources
load_sprites()
;- initialize misc dbs
load_abilities_db()
load_monster_db()
load_power_db()
load_item_db()
ability_db_resolve_names()
item_db_resolve_names()
;- load savegame or create new character
If FileSize("savegame\character.xml") > 0
load_character(@current_character)
load_map(@current_map, "savegame\" + GetFilePart(current_character\map_filename$))
EndIf
;- define variables used in main loop
Define step_nr.w = 0
Define direction.b = 0
Define x.w = 0
Define y.w = 0
Define main_window_event.l = 0 ; id for event that occured in main window
Define update_screen.b = 1 ; flag; if set, update screen
Define character_moved.b = 0 ; flag: character has moved
Define game_paused.b = 0 ; flag: 1=game is paused
Define turn_ends.b = 0 ; flag; 1=current turn ends
Define goto_main_menu.b = 1; flag: 1=goto main screen
Define character_died.b = 0 ; flag: 1=character has died
Define save_game_at_exit.b = 1 ; flag: save game when exiting
Define key_lock.b = 0 ; flag: 1=keyboard locked
Define update_equipment_boni.b = 0 ; flag; if set, update boni provided by equipment
Define blocked_by_monster.b = 0 ; flag; 1=character is blocked by a monster
Global dx.b = 0 ; xpos modification when moving character
Global dy.b = 0 ; ypos modification when moving character
Define modify_value.w = 0 ; modifier for attributes
Define i.w = 0 ; default counter
;- main loop
While program_ends = 0
;- check for keyboard input
ExamineKeyboard()
character_moved = 0
dx = 0
dy = 0
;- main menu
If goto_main_menu = 1
update_screen = 1
goto_main_menu = 0
game_paused = 0
program_ends = 0
character_died = 0
Select main_menu()
Case message_list$(#MESSAGE_MENU_SPLASH_NEW_GAME):
If program_ends = 0
delete_savegame()
init_character(@current_character)
experience(3, 1)
character_gets_starting_equipment()
load_map(@current_map, "maps\" + GetFilePart(current_character\map_filename$))
save_character(@current_character)
save_map(@current_map, "savegame\" + GetFilePart(current_map\filename$))
EndIf
Case message_list$(#MESSAGE_MENU_SPLASH_CONTINUE_GAME):
If program_ends = 0
load_character(@current_character)
load_map(@current_map, "savegame\" + GetFilePart(current_character\map_filename$))
EndIf
Case message_list$(#MESSAGE_MENU_SPLASH_EXIT):
program_ends = 1
EndSelect
EndIf
;- check for windows events
If preferences\fullscreen = 0
Repeat
main_window_event = WindowEvent()
If main_window_event = #PB_Event_CloseWindow
program_ends = 1
EndIf
Until main_window_event = 0
EndIf
;- escape key: goto main menu
If KeyboardReleased(#PB_Key_Escape)
ClearScreen(0)
draw_map_screen(@current_map)
draw_frame_main_screen()
draw_right_panel()
draw_character()
If preferences\fade_effect = 1
DisplayTransparentSprite(#SPRITE_FADEOUT_TOTAL, 0, 0, 150)
EndIf
FlipBuffers()
screenshot("savegame\screenshot.jpg")
save_map(@current_map, "savegame\" + GetFilePart(current_map\filename$))
save_character(@current_character)
goto_main_menu = 1
game_paused = 1
EndIf
;- ALT-X: end program without saving
If KeyboardPushed(#PB_Key_LeftAlt) And KeyboardPushed(#PB_Key_X)
program_ends = 1
EndIf
;- up: move character up
If KeyboardPushed(key_binding(#ACTION_UP)\keycode) And game_paused = 0
If movement_left() > 0
dx = 0
dy = -1
EndIf
current_character\facing = 0
update_screen = 1
EndIf
;- down: move character down
If KeyboardPushed(key_binding(#ACTION_DOWN)\keycode) And game_paused = 0
If movement_left() > 0
dx = 0
dy = 1
EndIf
current_character\facing = 2
update_screen = 1
EndIf
;- right: move character right
If KeyboardPushed(key_binding(#ACTION_RIGHT)\keycode) And game_paused = 0
If movement_left() > 0
dx = 1
dy = 0
EndIf
current_character\facing = 1
update_screen = 1
EndIf
;- left: move character left
If KeyboardPushed(key_binding(#ACTION_LEFT)\keycode) And game_paused = 0
If movement_left() > 0
dx = -1
dy = 0
EndIf
current_character\facing = 3
update_screen = 1
EndIf
;- move character
If (dx <> 0 Or dy <> 0) And game_paused = 0
update_screen = 1
blocked_by_monster = 0
If current_character\xpos + dx >= 0 And current_character\xpos + dx < #MAP_DIMENSION_X And current_character\ypos + dy >= 0 And current_character\ypos + dy < #MAP_DIMENSION_Y
If field_contains_monster(current_character\xpos + dx, current_character\ypos + dy)
blocked_by_monster = 1
If sneaking_val(1) > 0 And backstabbing_val(1) = 0; character tries to sneak
If Random(99) >= sneaking_val(1) Or monster_is_alerted(current_character\xpos + dx, current_character\ypos + dy)
play_sound("oh_no")
alert_monster(current_character\xpos + dx, current_character\ypos + dy)
message(message_list$(#MESSAGE_MONSTER_NOTICES_YOU), "")
turn_ends = 1
Else
blocked_by_monster = 0
play_sound("evasion")
message(message_list$(#MESSAGE_YOU_SNEAK_UNNOTICED_BY), "")
EndIf
Else
character_attacks_monster(@current_map, current_character\xpos + dx, current_character\ypos + dy)
turn_ends = 1
EndIf
EndIf
If blocked_by_monster = 0
trigger_map_event(@current_map, current_character\xpos + dx, current_character\ypos + dy)
trigger_map_message(@current_map, current_character\xpos + dx, current_character\ypos + dy)
If field_is_blocked(@current_map, current_character\xpos + dx, current_character\ypos + dy) = 0
current_character_change_frame()
If dx > 0 Or dy > 0
current_character\xpos = current_character\xpos + dx
current_character\ypos = current_character\ypos + dy
For i=30 To 2 Step -#SETTING_MAP_SCREEN_SCROLLING_STEP
draw_main_screen_complete(@current_map, dx * i, dy * i)
current_character_change_frame()
Next
Else
For i=2 To 30 Step #SETTING_MAP_SCREEN_SCROLLING_STEP
draw_main_screen_complete(@current_map, Abs(dx) * i, Abs(dy) * i)
current_character_change_frame()
Next
current_character\xpos = current_character\xpos + dx
current_character\ypos = current_character\ypos + dy
EndIf
character_moved = 1
EndIf
EndIf
EndIf
EndIf
;- ALT-Enter: toggle between windowed screen and fullscreen
If KeyboardPushed(#PB_Key_LeftAlt) And KeyboardPushed(#PB_Key_Return)
toggle_fullscreen(@current_map\tileset)
update_screen = 1
EndIf
;- Character info
If KeyboardPushed(#PB_Key_C) And game_paused = 0
character_info_screen()
FlipBuffers()
game_paused = 1
EndIf
;- use power
If KeyboardPushed(#PB_Key_U) And game_paused = 0 And key_lock = 0
If use_power() = 1
turn_ends = 1
EndIf
update_screen = 1
key_lock = 1
EndIf
; pickup items
If KeyboardPushed(#PB_Key_P) And game_paused = 0 And key_lock = 0
pickup_items(current_character\xpos, current_character\ypos)
key_lock = 1
update_screen = 1
EndIf
;- open inventory
If KeyboardPushed(#PB_Key_I) And game_paused = 0 And key_lock = 0
open_inventory()
update_screen = 1
key_lock = 1
EndIf
;- end turn
If KeyboardPushed(#PB_Key_E) And game_paused = 0 And key_lock = 0
turn_ends = 1
update_screen = 1
key_lock = 1
EndIf
;- T-Key: trigger test
If KeyboardPushed(#PB_Key_T) And key_lock = 0
scramble_item_db_tiles("Potion")
EndIf
;- check for field effects after character has moved
If character_moved = 1 And game_paused = 0
If enter_field() = 1
turn_ends = 1
EndIf
If detect_hidden_fields() = 1
draw_main_screen_complete(@current_map, 0,0)
Delay(1000)
EndIf
If field_is_open_portal(@current_map, current_character\xpos, current_character\ypos) = 1
enter_portal()
update_screen = 1
Else
If field_contains_dungeon_entrance(current_character\xpos, current_character\ypos)
enter_dungeon()
update_screen = 1
EndIf
EndIf
For i = 1 To #MAX_NUMBER_OF_ABILITIES - 1
If ability_db(i)\change_per_move <> 0
modify_attribute(i, ability_db(i)\change_per_move)
EndIf
If turn_ends = 0 And ability_db(i)\turn_ends_if_lower > -32000
If attribute_current_val(i) < ability_db(i)\turn_ends_if_lower
turn_ends = 1
EndIf
EndIf
Next
EndIf
;- remove game paused status
If KeyboardPushed(#PB_Key_Return) And game_paused = 1
game_paused = 0
update_screen = 1
EndIf
;- release key lock if no key is pressed
If KeyboardReleased(#PB_Key_All)
key_lock = 0
EndIf
;- end of turn
If turn_ends = 1
; adjust attributes at end of turn
For i = 1 To #MAX_NUMBER_OF_ABILITIES - 1
If ability_db(i)\modify_attribute > 0
modify_value = attribute_current_val(i)
If modify_value <> 0
If ability_db(i)\modify_divisor > 1
modify_value = (modify_value + ability_db(i)\modify_divisor - 1)/ ability_db(i)\modify_divisor
EndIf
If ability_db(i)\modify_multiplier <> 1
modify_value = modify_value * ability_db(i)\modify_multiplier
EndIf
EndIf
If modify_value <> 0
modify_attribute(ability_db(i)\modify_attribute, modify_value)
If ability_db(i)\modify_sound$ <> ""
play_sound(ability_db(i)\modify_sound$)
EndIf
If ability_db(i)\modify_animation$ <> ""
animation(ability_db(i)\modify_animation$, 6, 6, Abs(modify_value))
EndIf
EndIf
EndIf
If ability_db(i)\change_per_turn <> 0
If Random(99) < ability_db(i)\change_per_turn_chance
modify_attribute(i, ability_db(i)\change_per_turn)
EndIf
EndIf
If current_character\ability[i]\power_adjustment <> 0 Or current_character\ability[i]\adjustment_duration <> 0
current_character\ability[i]\adjustment_duration = current_character\ability[i]\adjustment_duration - 1
If current_character\ability[i]\adjustment_duration < 1
current_character\ability[i]\power_adjustment = 0
current_character\ability[i]\adjustment_duration = 0
current_character\ability[i]\adjustment_name$ = ""
current_character\ability[i]\adjustment_power_id = 0
EndIf
EndIf
Next
; reduce cooldown for all powers
For i = 1 To #MAX_NUMBER_OF_POWERS - 1
If current_character\power[i]\cooldown > 0
current_character\power[i]\cooldown = max(0, current_character\power[i]\cooldown - 1)
EndIf
Next
move_all_monster()
monster_attacks_character(current_character\xpos, current_character\ypos - 1)
monster_attacks_character(current_character\xpos + 1, current_character\ypos)
monster_attacks_character(current_character\xpos, current_character\ypos + 1)
monster_attacks_character(current_character\xpos - 1, current_character\ypos)
monster_turn_ends()
spawn_random_monster(@current_map)
; fuel of equipped items is reduced
ForEach inventory()
If inventory()\equipped = 1
If item_db(inventory()\type)\fuel > 0
inventory()\fuel = inventory()\fuel - item_db(inventory()\type)\fuel_consumed_per_turn
If inventory()\fuel < 1
inventory()\fuel = 0
inventory()\equipped = 0
update_equipment_boni = 1
EndIf
EndIf
EndIf
Next
For i = 1 To #MAX_NUMBER_OF_ABILITIES - 1
If ability_db(i)\restore_at_end_of_turn = 1
restore_attribute(i)
EndIf
Next
turn_ends = 0
update_screen = 1
EndIf
;- character death
If character_died() And program_ends = 0
play_sound("deathcry")
fade_map_screen_out(@current_map)
ClearScreen(0)
draw_map_screen(@current_map)
draw_frame_main_screen()
draw_right_panel()
draw_character(0)
If preferences\fade_effect = 1
DisplayTransparentSprite(#SPRITE_FADEOUT_TOTAL, 0, 0, 150)
EndIf
FlipBuffers()
screenshot("savegame\screenshot.jpg")
If startLastPoint = #False
delete_savegame()
EndIf
character_died = 1
goto_main_menu = 1
game_paused = 1
update_screen = 0
EndIf
;- update boni provided by equipment
If update_equipment_boni = 1
update_equipment_boni()
update_equipment_boni = 0
EndIf
;- update screen
If update_screen = 1
draw_main_screen_complete(@current_map, 0,0)
update_screen = 0
EndIf
Wend
;- save current game
If FileSize("savegame\character.xml") > 0
save_map(@current_map, "savegame\" + GetFilePart(current_map\filename$))
save_character(@current_character)
EndIf
End
; IDE Options = PureBasic 6.10 LTS beta 9 (Windows - x64)
; CursorPosition = 31
; Executable = Portals_3.0_64bit.exe
; CompileSourceDirectory