-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_inst.py
1200 lines (968 loc) · 28.7 KB
/
game_inst.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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from const import *
from items import *
from json_obj import *
from board import Board
from entity import Entity
from monster import Monster
from player import Player
from spell import Spell
from threading import Thread
from messages import MessageLog
from noise_event import NoiseEvent
from utils import *
from projectile import Projectile
import curses, textwrap, math, pickle, time
from inventory_ui import *
def _check_type(typ, types, type_name):
if typ not in types:
valid_types = sorted(types.keys())
error = ValueError(f"invalid {type_name} type {typ!r}")
error.add_note(f"Valid {type_name} types are: {', '.join(valid_types)}")
raise error
class Game:
_INST = None
@classmethod
def get_instance(cls):
if cls._INST:
return cls._INST
obj = object.__new__(cls)
cls._INST = obj
return obj
def __new__(cls):
return cls.get_instance()
def __init__(self):
self._board = Board(50, 18)
self._player = Player()
self.screen = None
self.monsters = []
self.msg_log = MessageLog(MESSAGE_LOG_CAPACITY)
self.window_init = True
self.mon_types = {}
self.eff_types = {}
self.weap_types = {}
self.armor_types = {}
self.level = 1
self.subtick_timer = 0
self.tick = 0
self.select_mon = None
self.noise_events = []
self.revealed = set()
self.delay = False
self.last_save_turn = -999
self.last_save_time = time.time()
self.projectile = None
def __getstate__(self):
d = self.__dict__.copy()
for field in ["screen", "mon_types", "eff_types", "weap_types", "armor_types"]:
del d[field]
return d
def __setstate__(self, state):
self.__dict__.update(state)
def autosave(self):
time_diff = time.time() - self.last_save_time
if time_diff >= 1.5:
t = Thread(target=self.save)
t.start()
def save(self):
player = self.get_player()
if not player.is_alive():
self.delete_saved_game()
return
try:
saved_data = pickle.dumps(self)
except:
pass
else:
with open(SAVED_GAME_PATH, "wb") as file:
file.write(saved_data)
self.last_save_time = time.time()
def load(self):
obj = pickle.load(open(SAVED_GAME_PATH, "rb"))
self.__dict__.update(obj.__dict__)
def has_saved_game(self):
import os
return os.path.exists(SAVED_GAME_PATH)
def delete_saved_game(self):
if self.has_saved_game():
import os
os.remove(SAVED_GAME_PATH)
def add_noise_event(self, pos, loudness, src=None):
if loudness > 0:
self.noise_events.append(NoiseEvent(pos, loudness, src))
def set_mon_select(self, m):
self.select_mon = m
def clear_mon_select(self):
self.select_mon = None
def check_mon_type(self, typ):
_check_type(typ, self.mon_types, "monster")
def check_effect_type(self, name):
_check_type(name, self.eff_types, "effect")
def check_weapon_type(self, typ):
_check_type(typ, self.weap_types, "weapon")
def check_armor_type(self, typ):
_check_type(typ, self.armor_types, "armor")
def load_monsters(self):
self.mon_types = load_monster_types()
def load_effects(self):
self.eff_types = load_effect_types()
def load_weapons(self):
self.weap_types = load_weapon_types()
def load_armors(self):
self.armor_types = load_armor_types()
def get_mon_type(self, typ):
self.check_mon_type(typ)
return self.mon_types[typ]
def get_weapon_type(self, typ):
self.check_weapon_type(typ)
return self.weap_types[typ]
def get_armor_type(self, typ):
self.check_armor_type(typ)
return self.armor_types[typ]
def get_effect_type(self, name):
self.check_effect_type(name)
return self.eff_types[name]
def get_all_monster_types(self):
for typ in self.mon_types.values():
yield typ
def add_message(self, text, typ="neutral"):
self.msg_log.add_message(text, typ)
def init_colors(self):
curses.start_color()
assert curses.has_colors()
curses.use_default_colors()
for i in range(curses.COLORS-1):
curses.init_pair(i + 1, i + 1, -1)
def init_game(self):
screen = curses.initscr()
self.screen = screen
self.init_colors()
curses.noecho()
curses.curs_set(False)
Entity.g = self
Spell.g = self
screen.addstr("Loading game, please wait...")
screen.refresh()
self.load_json_data()
if not self.check_saved_game():
self.init_player()
self.generate_level()
self.add_message("Welcome to the Dungeon Roguelike game! Press '?' to view controls.")
def confirm_load_save(self):
screen = self.screen
screen.erase()
screen.addstr(0, 0, "Continue Saved Game")
screen.addstr(2, 0, "You have a saved game in progress. Would you like to load the saved game?")
screen.addstr(3, 0, "(Press Y to load game, or N to start a new one)")
screen.refresh()
while True:
char = chr(self.getch())
if char.lower() == "y":
self.load()
return True
elif char.lower() == "n":
self.delete_saved_game()
return False
def check_saved_game(self):
if self.has_saved_game():
return self.confirm_load_save()
else:
return False
def load_json_data(self):
self.load_monsters()
self.load_effects()
self.load_weapons()
self.load_armors()
def next_level(self):
player = self.get_player()
self.level += 1
if player.debug_wizard:
self.level = self.input_int("As the wizard of debugging, you choose which level to go to. Which level number would you like to teleport to?")
self.generate_level()
self.refresh_mon_pos_cache()
self.save()
self.draw_board()
def generate_level(self):
board = self.get_board()
self.monsters.clear()
self.revealed.clear()
board.clear_los_cache()
board.clear_collision_cache()
board.procgen_level()
self.place_player()
self.place_monsters()
self.place_items()
def choose_mon_spawn_pos(self):
board = self.get_board()
player = self.get_player()
force_outside_fov = x_in_y(3, 4) and x_in_y(8, self.level + 8)
for tries in range(150):
pos = board.random_pos()
if board.passable(pos) and not self.entity_at(pos):
if not (force_outside_fov and player.sees_pos(pos)):
return pos
return None
def create_weapon(self, id):
typ = self.get_weapon_type(id)
return Weapon.from_type(typ)
def create_armor(self, id):
typ = self.get_armor_type(id)
return Armor.from_type(typ)
def place_items(self):
board = self.get_board()
potions = [
[InvisibilityPotion, 25],
[HealingPotion, 130],
[EnlargementPotion, 20],
[ShrinkingPotion, 20],
[SpeedPotion, 30],
[ForesightPotion, 12]
]
for _ in range(rng(1, 5)):
pos = board.random_passable()
typ = random_weighted(potions)
board.place_item_at(pos, typ())
weapons = [
["club", 100],
["dagger", 30],
["greatclub", 70],
["handaxe", 15],
["battleaxe", 10],
["scimitar", 6],
["greatsword", 4],
["greataxe", 5]
]
for _ in range(rng(1, 5)):
if one_in(2):
pos = board.random_passable()
name = random_weighted(weapons)
board.place_item_at(pos, self.create_weapon(name))
armors = [
["leather", 100],
["hide", 100],
["chain_shirt", 45],
["scale_mail", 45],
["half_plate", 10],
["ring_mail", 27],
["chain_mail", 18],
["plate", 10]
]
for _ in range(rng(2, 4)):
if one_in(2):
pos = board.random_passable()
name = random_weighted(armors)
board.place_item_at(pos, self.create_armor(name))
scrolls = [
[TeleportScroll, 20],
[FogScroll, 20],
[ThunderScroll, 20]
]
for _ in range(rng(1, 3)):
if one_in(5):
pos = board.random_passable()
typ = random_weighted(scrolls)
board.place_item_at(pos, typ())
if x_in_y(3, 8):
wand_types = [
WandFlame,
WandConfuse
]
pos = board.random_passable()
board.place_item_at(pos, random.choice(wand_types)())
if one_in(5):
pos = board.random_passable()
board.place_item_at(pos, Shield())
if not one_in(5):
num = rng(0, rng(0, rng(0, 9)))
for _ in range(num):
pos = board.random_passable()
board.place_item_at(pos, Dart())
def place_monsters(self):
eligible_types = {}
highest = 0
special = not x_in_y(10, self.level) and one_in(7)
levels = WeightedList()
for typ in self.get_all_monster_types():
if self.level >= typ.level:
w = 3
if typ.level > 1:
w = min(w, self.level - typ.level + 1)
if typ.level not in eligible_types:
levels.add(typ.level, w)
eligible_types[typ.level] = []
eligible_types[typ.level].append(typ)
assert len(eligible_types) > 0
num_monsters = rng(5, 10)
num_monsters += rng(0, round((self.level-1)**(2/3)))
packs = 0
while num_monsters > 0:
typ = random.choice(eligible_types[levels.pick()])
min_level = typ.level
if "PACK_TRAVEL" in typ.flags and x_in_y(self.level, self.level + 6) and one_in(6 + packs * 3):
pack_num = rng(3, 5)
if self.spawn_pack(typ.id, pack_num):
num_monsters -= pack_num
packs += 1
else:
num_monsters -= 1
self.place_monster(typ.id)
def spawn_pack(self, typ, num):
self.check_mon_type(typ)
board = self.get_board()
pos = self.choose_mon_spawn_pos()
if not pos:
return False
candidates = []
for p in board.points_in_radius(pos, 6):
if not board.passable(p):
continue
if self.entity_at(p):
continue
if one_in(2) or board.has_line_of_sight(p, pos):
candidates.append(p)
if not candidates:
return False
random.shuffle(candidates)
candidates.sort(key=lambda p: p.distance(pos))
num = min(len(candidates), num)
for i in range(num):
self.place_monster_at(typ, candidates[i])
def deinit_window(self):
if not self.window_init:
return
screen = self.screen
screen.nodelay(False)
screen.clear()
curses.nocbreak()
curses.echo()
curses.curs_set(True)
curses.endwin()
import os
os.system("cls" if os.name == "nt" else "clear")
self.window_init = False
def init_player(self):
board = self.get_board()
player = self.get_player()
self.place_player()
player.recalc_max_hp()
board.player = player
def place_player(self):
board = self.get_board()
player = self.get_player()
while True:
pos = board.random_passable()
for p in board.points_in_radius(pos, 1):
if p != pos and player.can_move_to(p):
player.move_to(pos)
board.set_collision_cache(pos, player)
return
def spawn_monster_at(self, m, pos):
board = self.get_board()
if m.move_to(pos):
board.set_collision_cache(pos, m)
self.monsters.append(m)
return True
return False
def place_monster_at(self, typ_id, pos):
typ = self.get_mon_type(typ_id)
m = Monster.from_type(typ)
if self.spawn_monster_at(m, pos):
return m
else:
return None
def place_monster(self, typ_id):
typ = self.get_mon_type(typ_id)
board = self.get_board()
pos = self.choose_mon_spawn_pos()
if not pos:
return None
return self.place_monster_at(typ_id, pos)
def entity_at(self, pos):
board = self.get_board()
return board.get_collision_cache(pos)
def monster_at(self, pos):
entity = self.entity_at(pos)
if entity and entity.is_monster():
return entity
return None
def monsters_in_radius(self, pos, radius):
board = self.get_board()
for pos in board.points_in_radius(pos, radius):
if (mon := self.monster_at(pos)):
yield mon
def once_every_num_turns(self, num):
return self.tick % num == 0
def get_board(self):
return self._board
def get_player(self):
return self._player
def get_monsters(self):
return [mon for mon in self.monsters if mon.is_alive()]
def process_noise_events(self):
if not self.noise_events:
return
for noise in self.noise_events:
for m in self.monsters:
if not (noise.src and noise.src is m):
m.on_hear_noise(noise)
player = self.get_player()
player.on_hear_noise(noise)
self.noise_events.clear()
def do_turn(self):
board = self.get_board()
player = self.get_player()
used = player.energy_used
if used <= 0:
return
self.autosave()
board.tick_fields(used)
while player.energy_used > 0:
amount = min(player.energy_used, 100)
player.do_turn(amount)
self.subtick_timer += used
player.energy += used
monsters = self.get_monsters()
for m in monsters:
m.energy += used
self.process_noise_events()
while self.subtick_timer >= 100:
self.subtick_timer -= 100
self.tick += 1
player.tick()
for m in monsters:
if m.is_alive():
m.tick()
remaining = monsters.copy()
random.shuffle(remaining)
remaining.sort(key=lambda m: m.energy, reverse=True)
self.refresh_mon_pos_cache()
while len(remaining) > 0:
nextremain = []
for m in remaining:
if not m.is_alive():
continue
if m.energy <= 0:
continue
m.do_turn()
if m.energy > 0:
nextremain.append(m)
remaining = nextremain
self.remove_dead()
self.process_noise_events()
self.refresh_mon_pos_cache()
def items_at(self, pos):
board = self.get_board()
return board.get_tile(pos).items
def refresh_mon_pos_cache(self):
board = self.get_board()
player = self.get_player()
board.clear_collision_cache()
board.set_collision_cache(player.pos, player)
for m in self.monsters:
if m.is_alive():
board.set_collision_cache(m.pos, m)
def remove_dead(self):
for m in reversed(self.monsters):
if not m.is_alive():
self.monsters.remove(m)
def place_stairs(self):
board = self.get_board()
for _ in range(5):
pos = board.random_passable()
if not self.items_at(pos):
break
tile = board.get_tile(pos)
tile.stair = 1
def getch(self, wait=True):
screen = self.screen
screen.nodelay(not wait)
if wait:
curses.flushinp()
code = screen.getch()
return code
def draw_symbol(self, row, col, symbol, color=0):
screen = self.screen
rows, cols = screen.getmaxyx()
if 0 <= row < rows and 0 <= col < cols:
screen.addstr(row, col, symbol, color)
def draw_string(self, row, col, string, color=0):
screen = self.screen
rows, cols = screen.getmaxyx()
if col >= cols or not (0 <= row < rows):
return
diff = cols - (col + len(string))
if diff < 0:
string = string[:diff]
screen.addstr(row, col, string, color)
def draw_walls(self, offset_y):
board = self.get_board()
player = self.get_player()
height = board.height
width = board.width
player = self.get_player()
for pos in board.iter_square(0, 0, width-1, height-1):
tile = board.get_tile(pos)
if not tile.revealed:
continue
seen = player.sees_pos(pos)
color = 0 if seen else curses.color_pair(COLOR_GRAY)
if tile.wall:
symbol = WALL_SYMBOL
elif seen and board.field_at(pos):
symbol = "8"
color = curses.color_pair(COLOR_SILVER) | curses.A_REVERSE
elif seen and tile.items:
item = tile.items[-1]
symbol = item.symbol
color = item.display_color()
elif tile.stair:
symbol = STAIR_SYMBOL
else:
symbol = "." if seen else " "
if seen:
color = curses.color_pair(COLOR_GRAY)
self.draw_symbol(pos.y + offset_y, pos.x, symbol, color)
def maybe_draw_projectile(self, offset_y):
if not self.projectile:
return
player = self.get_player()
proj = self.projectile
if player.sees_pos(proj):
self.draw_symbol(proj.y + offset_y, proj.x, "*")
def draw_stats(self):
board = self.get_board()
height = board.height
width = board.width
player = self.get_player()
bar = "HP: " + display_bar(player.HP, player.MAX_HP, 20)
p = f"({player.HP}/{player.MAX_HP})"
bar += " " + p
if player.HP <= player.MAX_HP // 4:
color = COLOR_RED
elif player.HP <= player.MAX_HP // 2:
color = COLOR_YELLOW
else:
color = COLOR_GREEN
self.draw_string(0, 0, bar, curses.color_pair(color))
xp_needed = player.xp_to_next_level()
xp_curr = player.xp
xp_str = f"XP Level: {player.xp_level} - {xp_curr}/{xp_needed} | Depth: {self.level}"
self.draw_string(1, 0, xp_str)
strings = [
f"STR {player.STR}",
f"DEX {player.DEX}",
f"CON {player.CON}",
f"INT {player.INT}",
f"WIS {player.WIS}",
f"CHA {player.CHA}"
]
for i, string in enumerate(strings):
self.draw_string(i, width + 6, string)
offset = len(strings) + 1
ev = player.calc_evasion() - 10
ev = round(ev, 1)
stealth = round(player.stealth_mod(), 1)
ev_str = f"+{ev}" if ev >= 0 else str(ev)
stealth_str = f"+{stealth}" if stealth >= 0 else str(stealth)
wield_str = player.weapon.name
dmg_str = str(player.weapon.damage)
armor_str = player.armor.name if player.armor else ""
prot_str = f"Protection: {player.get_armor()}"
strings2 = [
f"Stealth: {stealth_str}",
f"Evasion: {ev_str}",
f"Wield: {wield_str}",
f"Damage: {dmg_str}",
armor_str,
prot_str,
"Using Shield" if player.shield else ""
]
for i, string in enumerate(strings2):
self.draw_string(i + offset, width + 6, string)
offset += len(strings2) + 1
status = sorted(list(player.get_all_status_effects()))
if status:
for i, string in enumerate(status):
eff = self.get_effect_type(string)
color = curses.color_pair(MSG_TYPES[eff.type])
self.draw_string(i + offset, width + 6, string, color)
if i >= 12:
break
def draw_monsters(self, offset_y):
player = self.get_player()
for m in player.visible_monsters():
pos = m.pos
symbol = m.symbol
color = m.display_color()
if m is self.select_mon:
color = curses.color_pair(COLOR_GREEN) | curses.A_REVERSE
self.draw_symbol(pos.y + offset_y, pos.x, symbol, color)
pos = player.pos
self.draw_symbol(pos.y + offset_y, pos.x, PLAYER_SYMBOL, curses.A_REVERSE)
def draw_messages(self, offset_y):
screen = self.screen
messages = self.msg_log.get_messages(8)
board = self.get_board()
y = board.height + offset_y
rows, _ = screen.getmaxyx()
cols = board.width + 4
groups = []
total_lines = 0
for message, type in reversed(messages):
msg_lines = textwrap.wrap(message, width=cols)
groups.append([(line, type) for line in msg_lines])
total_lines += len(msg_lines)
if total_lines >= 8:
break
groups.reverse()
displayed = []
for group in groups:
displayed.extend(group)
displayed = displayed[-8:]
for i in range(len(displayed)):
msg, type = displayed[i]
color = curses.color_pair(MSG_TYPES[type])
self.draw_string(y + i, 0, msg, color)
def reveal_seen_tiles(self):
board = self.get_board()
player = self.get_player()
for pos in player.fov:
board.reveal_tile_at(pos)
def draw_board(self):
screen = self.screen
screen.erase()
offset_y = 2
board = self.get_board()
player = self.get_player()
if board.recalc_sight:
player.calc_fov()
board.recalc_sight = False
self.reveal_seen_tiles()
self.draw_walls(offset_y)
self.draw_monsters(offset_y)
self.maybe_draw_projectile(offset_y)
self.draw_stats()
self.draw_messages(offset_y+1)
screen.move(20 + offset_y, 0)
screen.refresh()
def maybe_refresh(self):
player = self.get_player()
if not player.is_resting or self.once_every_num_turns(5):
self.draw_board()
def input_text(self, msg=""):
curses.curs_set(True)
curses.echo()
screen = self.screen
if msg:
self.add_message(msg, "input")
self.draw_board()
screen.move(20, 0)
result = screen.getstr()
curses.curs_set(False)
curses.noecho()
return result.decode().rstrip()
def input_int(self, msg=""):
while True:
txt = self.input_text(msg)
try:
return int(txt)
except ValueError:
self.add_message("Integers only, please.", "input")
def confirm(self, question):
while True:
txt = self.input_text(question + " (Y/N)")
if txt in ["Y", "N"]:
return txt == "Y"
else:
self.add_message("Please enter an uppercase Y or N.", "input")
def process_input(self):
self.maybe_refresh()
player = self.get_player()
board = self.get_board()
if player.is_resting:
if player.HP >= player.MAX_HP:
self.add_message("HP restored.", "good")
player.is_resting = False
self.save()
elif player.can_rest():
player.use_energy(100)
else:
player.interrupt()
return True
if player.has_status("Paralyzed"):
player.use_energy(100)
return True
player.handle_activities()
if player.activity:
player.use_energy(100)
return True
self.draw_board()
code = self.getch()
char = chr(code)
if char == "w":
return player.move_dir(0, -1)
if char == "s":
return player.move_dir(0, 1)
elif char == "a":
return player.move_dir(-1, 0)
elif char == "d":
return player.move_dir(1, 0)
elif char == ">":
return player.descend()
elif char == "r":
if player.can_rest():
self.add_message("You begin resting.")
player.is_resting = True
return True
elif char == "v":
return self.view_monsters()
elif char == "p":
return player.pickup()
elif char == "u":
self.add_message("The 'u'se menu has been moved to the 'i'nventory menu.", "info")
elif char == "i":
return self.open_player_inventory()
elif char == " ":
player.use_energy(100)
return True
elif char == "Q":
if self.confirm("Are you sure to want to close the game? (Your game will be saved so you can come back to it later.)"):
self.save()
self.deinit_window()
exit()
elif char == "?":
self.help_menu()
return False
def help_menu(self):
menu = GameTextMenu(self)
menu.add_text("Help Menu")
menu.add_line()
menu.add_text("wasd keys - Move around")
menu.add_text("> - Descend stairs")
menu.add_text("r - rest and recover HP")
menu.add_text("v - view info of nearby monsters")
menu.add_text("p - pick up item")
menu.add_text("i - open inventory menu")
menu.add_text("SPACE - wait 1 turn")
menu.add_text("Q - Quits and saves the game")
menu.add_text("? - Shows this menu again")
menu.add_line()
menu.add_text("Press Enter to close this menu")
menu.display()
menu.wait_for_enter()
def select_monster(self, monsters, prompt):
monsters = monsters.copy()
if not monsters:
return None
monsters.sort(key=lambda m: m.pos.y)
monsters.sort(key=lambda m: m.pos.x)
cursor = 0
self.add_message(f"{prompt} (Use the a and d keys to navigate, then press Enter to select)")
mon = None
while True:
self.set_mon_select(monsters[cursor])
self.draw_board()
code = self.getch()
char = chr(code)
if char == "a":
cursor -= 1
elif char == "d":
cursor += 1
elif code == 10:
mon = monsters[cursor]
break
cursor %= len(monsters)
self.clear_mon_select()
return mon
def select_monster_menu(self, monsters, check_fov=True):
player = self.get_player()
monsters = monsters.copy()
if check_fov:
monsters = [mon for mon in monsters if player.sees(monster)]
if not monsters:
return False
#Sort by y position first, then sort by x position
monsters.sort(key=lambda m: m.pos.y)
monsters.sort(key=lambda m: m.pos.x)
self.add_message("View info of which monster? (Use the a and d keys to navigate, then press Enter to select)")
cursor = 0
mon = None
min_dist = 999
nearest = None
for i, m in enumerate(monsters):
dist = player.distance(m)
if dist < min_dist:
min_dist = dist
nearest = m
cursor = i
while True:
self.set_mon_select(monsters[cursor])
self.draw_board()
code = self.getch()
char = chr(code)
if char == "a":
cursor -= 1
elif char == "d":
cursor += 1