-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
2501 lines (2224 loc) · 109 KB
/
game.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
"""
Start the actual Game
"""
import time
import random
from functools import reduce
from core.configs import get_value
from core.display import Text
from core.files import get_files
from core.save import get_saves_path, save_to_file, load_from_file, rename_file
from core.tools import weighted_choice, cap_all_words
from game_objects import dict_to_attack, LOCATIONS_LIST, WEAPONS_LIST, EVERYTHING_LIST, all_attacks, NPC, \
ambrosia, starting_health, treasure, cerastes, gold_ingot, spartae, minotaur,\
bestiary, anthology, cook_book, finely_cooked_meal, cooked_meal, mead, atlas, monks_dogs, dagger, hrunting, \
hrothgars_gift, grendel, adamantine_blade \
# TODO: Store lines out of screen past configurable char limit in variables
header_color = get_value("customization", "header_color")
header_size = get_value("customization", "header_size")
number_color = get_value("customization", "number_color")
prompt_color = get_value("customization", "prompt_color")
input_color = get_value("customization", "input_color")
path_font_size = get_value("customization", "path_font_size")
path_font = get_value("customization", "path_font")
choice_font = get_value("customization", "choice_font")
choice_font_size = get_value("customization", "choice_font_size")
location_color = get_value("game_customization", "location_color")
weapon_color = get_value("game_customization", "weapon_color")
attack_color = get_value("game_customization", "attack_color")
inventory_color = get_value("game_customization", "inventory_color")
npc_color = get_value("game_customization", "npc_color")
dead_font = get_value("game_customization", "dead_font")
dead_font_size = get_value("game_customization", "dead_font_size")
dead_color = get_value("game_customization", "dead_color")
default_color = get_value("display", "default_color")
multiattack_color = get_value("game_customization", "multiattack_color")
cooldown_color = get_value("game_customization", "cooldown_color")
self_color = get_value("game_customization", "self_color")
treasure_color = get_value("game_customization", "treasure_color")
starting_health = get_value("game_objects", "starting_health")
extension = get_value("save", "extension")
tutorials = get_value("tutorials")
# Game Object Classes
class GameAction(object):
"""Stores object traits
:type text_list: None | list[Text] | str
"""
def __init__(self, text_list=None):
if isinstance(text_list, str):
text_list = [Text(text_list)]
self.text_list = text_list
def set_text(self, text):
old_text = self.get_text_object()
old_text.set_text(text)
old_text.update_whole()
self.text_list = [old_text]
def set_text_list(self, text_list):
self.text_list = text_list
def get_text_string(self):
"""Returns entire text list as a single string"""
if isinstance(self.text_list, type(None)):
raise Exception("Text should not be None")
string = ""
for text in self.text_list:
string += text.get_whole()
return string
def get_text_object(self):
"""Returns Text object with intact attributes and all text from list"""
if isinstance(self.text_list, type(None)):
raise Exception("Text should not be None")
string = ""
for text in self.text_list:
string += text.get_text()
text_object = self.text_list[0].copy(True)
text_object.set_text(string)
return text_object
def get_text_list(self):
text_list = []
for text in self.text_list:
text_list.append(text.copy(True))
return text_list
class GameObject(GameAction):
"""Stores information related to in-game actions
:type text_list: None | list[Text] | str
:type _actions: list[GameAction]
:type _objects: list[GameObjects]
"""
def __init__(self, text_list=None, _objects=None, _actions=None,
attributes=None):
GameAction.__init__(self, text_list)
self.contained_objects = _objects
self.contained_actions = _actions
if isinstance(self.contained_objects, type(None)):
self.contained_objects = []
if isinstance(self.contained_actions, type(None)):
self.contained_actions = []
if isinstance(attributes, type(None)):
self.attributes = {}
else:
self.attributes = attributes
self.tooltip = self.get_tooltip()
def copy(self):
new_obj = GameObject(self.text_list, self.contained_objects,
self.contained_actions, self.attributes)
return new_obj
def show_attacks_desc(self):
if not isinstance(self.text_list, type(None)):
for text in self.text_list:
text.set_tooltip(self.tooltip)
def hide_attacks_desc(self):
if not isinstance(self.text_list, type(None)):
self.tooltip = self.get_tooltip()
for text in self.text_list:
text.set_tooltip([self.get_tooltip()[0]])
def update_tooltip(self):
"""Adds objects and actions to the tooltip"""
for text in self.text_list:
text.tooltip.append(Text("", None, new_line=True))
text.tooltip.append(Text("", None, new_line=True))
text.tooltip.append(
Text("Objects:", None, new_line=True, color=header_color,
bold=True))
for _object in self.contained_objects:
text.tooltip.append(_object.get_text_list()[0])
text.tooltip.append(Text(" "))
text.tooltip.append(Text("", None, new_line=True))
text.tooltip.append(Text("", None, new_line=True))
text.tooltip.append(
Text("Actions:", None, new_line=True, color=header_color,
bold=True))
for action in self.contained_actions:
text.tooltip.append(action.get_text_list()[0])
text.tooltip.append(Text(" "))
def set_tooltip(self, tooltip):
for text in self.text_list:
text.tooltip = tooltip
def get_tooltip(self):
for text in self.text_list:
if not isinstance(text.tooltip, type(None)):
return text.tooltip
def add_action(self, action):
self.contained_actions.append(action)
def set_actions(self, actions):
self.contained_actions = actions
def clear_actions(self):
self.contained_actions = []
def get_actions(self):
return self.contained_actions
def add_object(self, _object):
self.contained_objects.append(_object)
def remove_object(self, _object):
self.contained_objects.remove(_object)
def remove_action(self, _action):
self.contained_actions.remove(_action)
def add_objects(self, _objects):
for _object in _objects:
self.contained_objects.append(_object)
def clear_objects(self):
self.contained_objects = []
def get_objects(self):
return self.contained_objects
def get_object_names(self):
names = []
for _object in self.contained_objects:
names.append(_object.get_text_string())
return names
def get_action_names(self):
names = []
for _action in self.contained_actions:
names.append(_action.get_text_string())
return names
def find_object(self, _name):
for _object in self.contained_objects:
if _object.get_text_string().strip() == _name:
return _object
def find_action(self, _name):
for _action in self.contained_actions:
if _action.get_text_string().strip() == _name:
return _action
def add_attribute(self, key, value):
self.attributes[key] = value
def remove_attribute(self, key):
self.attributes.pop(key)
def get_attribute(self, key):
return self.attributes[key]
def get_attributes(self):
return self.attributes
# Save Classes
class GameSave(object):
"""Stores all game values"""
def __init__(self, name="", add_event=None):
self.name = name
self.cryptic = False
self.temp_cryptic = False
self.camo = False
self.camo_plus = False
self.orpheus = False
self.shield = False
self.wolf = False
self.fleece = False
self.sack = False
self.sleep = 0
self.dead = False
self.path = [
GameObject([Text("Game", color=input_color)],
[
GameObject(
[Text("Inventory", color=inventory_color)]),
GameObject(
[Text("Surroundings", color=inventory_color)]),
GameObject(
[Text("Stats", color=inventory_color)],
attributes={
"Character": NPC("yourself"),
},
),
GameObject(
[Text("Attacks", color=inventory_color)],
),
]
)
]
if isinstance(add_event, type(None)):
raise Exception("Game Save object needs add_event")
else:
self.add_event = add_event
self.path[0].find_object("Stats").add_object(
GameObject([self.get_you_word(tooltip=[Text("")], strip=True)],
_actions=[GameAction([Text("Use")])]))
self.update_weapons()
self.update_attacks(True)
# Equip first attack by default
self.equip_default()
self.current_objects = None # For showing/hiding objects
self.update_stats_tooltip()
self.new_location = True
self.name_nums = {}
self.type_nums = {}
self.uniques = []
self.boosts = []
self.accurate = 0
self.viewed_npc = None
self.herot = False
self.hotkey_action = None
self.current_path = None
def notify_switch(self):
self.add_event([Text("Equipped attack not available, switched to ",
color=prompt_color), Text(
game_save.get_equipped().get_attribute("Attack").name + ".",
tooltip=game_save.get_equipped().get_attribute("Attack").gen_desc(),
new_line=True)])
def update_attacks(self, first=False):
"""Clears actions and adds starting actions"""
self.path[0].find_object("Attacks").clear_objects()
current_attacks = []
for attack in self.get_character().get_attacks():
if attack.get_name() not in current_attacks:
if attack.name not in current_attacks:
if attack.current_cooldown > 0:
attack_color = cooldown_color
elif attack.current_cooldown < 0:
attack_color = multiattack_color
elif "Self" in attack.attributes:
attack_color = self_color
else:
attack_color = default_color
self.path[0].find_object("Attacks").add_object(GameObject(
[Text(attack.get_name(), tooltip=attack.gen_desc(
None, False), color=attack_color)],
_actions=[GameAction("Equip")],
attributes={"Attack": attack}))
current_attacks.append(attack.get_name())
for weapon in self.path[0].find_object("Inventory").get_objects():
for attack in weapon.get_attribute("Item").get_attacks():
if "Cryptic" in attack.get_attributes() and not self.cryptic:
continue
attack_name = weapon.get_attribute(
"Item").get_name() + ":" + attack.get_name()
if attack_name not in current_attacks:
if attack.current_cooldown > 0:
attack_color = cooldown_color
elif attack.current_cooldown < 0:
attack_color = multiattack_color
elif "Self" in attack.attributes:
attack_color = self_color
else:
attack_color = default_color
flower = False
if "Flower" in weapon.get_attribute("Item"). \
get_attributes() and not self.cryptic:
flower = True
self.path[0].find_object("Attacks").add_object(GameObject(
[Text(attack_name, tooltip=attack.gen_desc(
weapon.get_attribute("Item"), False, flower=flower),
color=attack_color)],
_actions=[GameAction("Equip")],
attributes={"Attack": attack, "Weapon": weapon}))
current_attacks.append(attack_name)
inv = list([weap.get_attribute("Item") for weap in
self.get_path()[0].find_object(
"Inventory").get_objects()])
equipped = True
if "Equipped" not in self.path[0].find_object(
"Attacks").get_attributes():
equipped = False
found = True
if equipped and "Weapon" in self.get_equipped().get_attributes():
weapon = self.get_equipped().get_attribute("Weapon").get_attribute(
"Item")
attack = self.get_equipped().get_attribute("Attack")
if weapon not in inv: # If weapon is lost, set all identical weapons to same cooldown for continued multiuse
found = False
for weap in inv: # Update all cooldowns of same weapons/attacks
if weap.get_name() == weapon.get_name():
found = True
for atta in weap.get_attacks():
if atta.get_name() == attack.get_name():
atta.current_cooldown = attack.current_cooldown
weapon = weap
if found:
for atta_obj in self.get_path()[0].find_object(
"Attacks").get_objects(): # Find same weapon/attack to equip instead
temp_atta = atta_obj.get_attribute("Attack")
if temp_atta.get_name() == attack.get_name():
if "Weapon" in atta_obj.get_attributes():
if atta_obj.get_attribute(
"Weapon").get_attribute(
"Item").get_name() == weapon.get_name():
# Same attack name, same weapon name
if temp_atta != attack:
# Different attack
self.path[0].find_object(
"Attacks").add_attribute("Equipped",
atta_obj)
self.update_attacks_tooltip()
break
if not equipped or not found:
self.equip_default()
if not first:
self.open_path()
self.notify_switch()
return True
return False
def open_path(self, pth="Attacks"):
# pth = cap_all_words(pth, [">", "-", ":"])
if pth == "Game":
self.set_path_index(0)
elif pth == "Back":
if len(self.get_path()) > 1:
self.remove_path_index(-1)
else:
self.set_path_index(0)
for step in pth.split(">"):
if step in self.get_path()[-1].get_object_names():
self.append_path(self.get_path()[-1].find_object(step))
elif step in self.get_path()[-1].get_action_names():
return self.get_path()[-1].find_action(step)
def update_attacks_tooltip(self):
text_list = [Text("Equipped Attack", color=header_color,
font_size=header_size, new_line=True),
Text("", new_line=True)]
attack = self.get_equipped().get_attribute("Attack")
if "Weapon" in self.get_equipped().get_attributes():
weapon = self.get_equipped().get_attribute("Weapon").get_attribute(
"Item")
else:
weapon = None
flower = False
if not isinstance(weapon, type(None)):
if "Flower" in weapon.get_attributes() and not self.cryptic:
flower = True
text_list += attack.gen_desc(weapon, flower=flower)
self.get_path()[0].find_object("Attacks").set_tooltip(text_list)
def rename_flowers(self):
flowers = []
for item in self.get_path()[0].find_object("Inventory").get_objects():
flowers.append(item)
for item in self.get_path()[0].find_object(
"Surroundings").get_objects():
flowers.append(item)
for item in flowers:
if "Item" in item.get_attributes():
flower_item = item.get_attribute("Item")
if "Flower" in flower_item.get_attributes():
if not self.cryptic:
new_name = flower_item.get_attributes()["Flower"]
if new_name != "Herb" and new_name != "Shirt-of-Nessus":
new_name += "-Flower"
item.hide_attacks_desc()
else:
new_name = flower_item.get_type()
item.show_attacks_desc()
flower_item.set_name(new_name)
item.set_text(new_name)
def equip_default(self):
self.path[0].find_object("Attacks").add_attribute(
"Equipped", self.path[0].find_object(
"Attacks").get_objects()[0])
self.update_attacks_tooltip()
def get_equipped(self):
return self.get_path()[0].find_object("Attacks").get_attribute(
"Equipped")
def update_weapons(self):
"""Clears the inventory and adds starting weapons"""
self.path[0].find_object("Inventory").clear_objects()
for weapon in self.get_character().get_weapons():
if "Treasure" in weapon.get_attributes():
color = treasure_color
else:
color = weapon_color
self.path[0].find_object("Inventory").add_object(GameObject(
[Text(weapon.get_name(), color=color,
tooltip=weapon.gen_desc())], attributes={
"Item": weapon,
}, _actions=[GameAction("Drop")]))
def get_character(self):
return self.path[0].find_object("Stats").get_attribute(
"Character")
def append_path(self, _object):
self.path.append(_object)
def remove_path_index(self, ind):
self.path.pop(ind)
def add_health_boost(self, healths, turns, name_word):
"""Additive effect"""
self.boosts.append(
{"Healths": healths, "Turns": turns, "Teller": name_word})
self.boost_health(len(self.boosts) - 1, True)
def add_attack_boost(self, attacks, turns, name_word):
"""Multiplicative effect"""
self.boosts.append(
{"Attacks": attacks, "Turns": turns, "Teller": name_word})
def boost_health(self, ind, on=True):
current_boost = self.boosts[ind]
health = self.get_health().copy()
for stat in current_boost["Healths"]:
if stat not in health:
health[stat] = 0
if on:
health[stat] += current_boost["Healths"][stat]
else:
health[stat] -= (current_boost["Healths"][stat] - 1)
self.set_health(health)
def boost_attacks(self):
attacks_dict = {}
for attacks in list(filter(lambda x: "Attacks" in x, self.boosts)):
for attack in attacks["Attacks"]:
if attack not in attacks_dict:
attacks_dict[attack] = 0
attacks_dict[attack] += attacks["Attacks"][attack]
return attacks_dict
def check_boosts(self):
if self.accurate > 0:
self.accurate -= 1
if self.accurate == 0:
self.add_event(
[Text("The effects of hearing ", color=prompt_color),
self.accurate_word,
Text("'s story have worn off.", color=prompt_color,
new_line=True)])
ind = 0
for boost in self.boosts:
boost["Turns"] -= 1
if boost["Turns"] <= 0:
if "Healths" in boost:
self.boost_health(ind, False)
self.add_event(
[Text("The effects of hearing ", color=prompt_color),
boost["Teller"],
Text("'s story have worn off.", color=prompt_color,
new_line=True)])
self.boosts.pop(ind)
ind += 1
def canterbury(self, npc, treasures):
name_parts = npc.get_name().split("-")
if name_parts[-1].isnumeric():
name_parts.pop()
name = reduce(lambda x, y: x + "-" + y, name_parts)
if not name == "The-Franklin":
for item in treasures:
npc.weapons.append(item.get_attribute("Item"))
desc = None
if name == "The-Knight":
desc = "inspires you to persevere through pain."
self.add_health_boost({"Sharp": 100000}, 30,
npc.get_word(True, True))
elif name == "The-Squire":
desc = "inspires you to stay strong."
self.add_health_boost({"Brute": 100000}, 30,
npc.get_word(True, True))
elif name == "The-Plowman":
desc = "inspires you to be more righteous."
self.add_health_boost({"Divine": 100000}, 30,
npc.get_word(True, True))
elif name == "The-Reeve":
desc = "inspires you to be quick on your feet."
self.add_health_boost({"Agility": 100000}, 30,
npc.get_word(True, True))
elif name == "The-Franklin":
desc = "inspires you to be friendlier."
self.add_health_boost({"Charisma": 1}, 30,
npc.get_word(True, True))
self.add_event([npc.get_word(True, False),
Text("gave you back your payment.",
color=prompt_color, new_line=True)])
for _treasure in treasures:
self.add_inv_item(_treasure)
elif name == "The-Sailor":
desc = "inspires you to be one with nature."
self.add_attack_boost({"Tame": 2}, 30,
npc.get_word(True, True))
bestiary_word = Text(bestiary.get_name(), color=weapon_color,
tooltip=bestiary.gen_desc())
self.add_event(
[npc.get_word(), Text("handed you a ", color=prompt_color),
bestiary_word, Text(".", color=prompt_color, new_line=True)])
self.add_inv_item(GameObject([bestiary_word],
attributes={"Item": bestiary}))
elif name == "The-Parson":
desc = "inspires you to spread the word of God."
self.add_attack_boost({"Divine": 2}, 30,
npc.get_word(True, True))
elif name == "The-Clerk":
anthology_word = Text(anthology.get_name(), color=weapon_color,
tooltip=anthology.gen_desc())
self.add_event(
[npc.get_word(), Text("handed you a ", color=prompt_color),
anthology_word, Text(".", color=prompt_color, new_line=True)])
self.add_inv_item(GameObject([anthology_word],
attributes={"Item": anthology}))
elif name == "The-Cook":
cook_word = Text(cook_book.get_name(), color=weapon_color,
tooltip=cook_book.gen_desc())
self.add_event(
[npc.get_word(), Text("handed you a ", color=prompt_color),
cook_word, Text(".", color=prompt_color, new_line=True)])
self.add_inv_item(GameObject([cook_word],
attributes={"Item": cook_book}))
elif name == "The-Wife-of-Bath":
atlas_word = Text(atlas.get_name(), color=weapon_color,
tooltip=atlas.gen_desc())
self.add_event(
[npc.get_word(), Text("handed you a ", color=prompt_color),
atlas_word, Text(".", color=prompt_color, new_line=True)])
self.add_inv_item(GameObject([atlas_word],
attributes={"Item": atlas}))
elif name == "The-Yeoman":
desc = "inspires you to focus in combat."
self.accurate = 30
self.accurate_word = npc.get_word(True, True)
elif name == "The-Monk":
game_save.gen_npc(monks_dogs)
game_save.gen_npc(monks_dogs)
self.add_event(
[npc.get_word(True, True),
Text("'s dogs attack you while he tells you his story.",
color=prompt_color, new_line=True)])
elif name == "The-Friar":
health = self.get_health()
health["Divine"] = 1
self.set_health(health)
self.add_event([npc.get_word(True),
Text("absolves your sins.", color=prompt_color,
new_line=True)])
elif name == "The-Merchant":
success, items = self.get_payment(1, True)
for item in items:
npc.weapons.append(item.get_attribute("Item"))
elif name == "The-Lawyer":
success, items = self.get_payment(-1, True)
for item in items:
npc.weapons.append(item.get_attribute("Item"))
if not isinstance(desc, type(None)):
self.add_event([npc.get_word(True, True), Text("'s story ",
color=prompt_color),
Text(desc, color=prompt_color, new_line=True)])
self.update_surroundings()
def get_payment(self, num, trick=False):
treasures = []
treasure_words = []
for _object in self.get_path()[0].find_object(
"Inventory").get_objects():
item = _object.get_attribute("Item")
if "Treasure" in item.get_attributes():
treasures.append(_object)
treasure_words.append(
Text(item.get_name(), color=treasure_color,
tooltip=item.gen_desc()))
if len(treasures) == num:
break
if len(treasures) == num or num == -1:
treasure_string = []
i = 0
for word in treasure_words:
if i == 0:
treasure_string.append(word)
elif i == len(treasure_words) - 1:
treasure_string.append(
Text(", and ", color=prompt_color))
treasure_string.append(word)
else:
treasure_string.append(
Text(", ", color=prompt_color))
treasure_string.append(word)
i += 1
if trick:
sentence = "He takes your "
else:
sentence = "You hand over "
self.add_event(
[Text(sentence, color=prompt_color),
*treasure_string,
Text(".", color=prompt_color,
new_line=True)])
for item in treasures:
self.remove_inv_item(item)
return True, treasures
else:
if not trick:
self.add_event([Text("You don't have enough treasure.",
color=prompt_color, new_line=True)])
return False, treasures
def drop_empty(self, weapon_object, attack_object):
self.add_event([Text("Equipped attack is out of ammo.", color=prompt_color,
new_line=True)])
weapon_object = attack_object.get_attribute("Weapon")
self.remove_inv_item(weapon_object)
self.update_surroundings()
def add_inv_item(self, item):
self.path[0].find_object("Inventory").add_object(item)
self.path[0].find_object("Inventory").get_objects()[-1].set_actions(
[
GameAction("Drop")])
health = self.get_health()
health["Agility"] -= 5
self.set_health(health) # Reduce agility
def remove_inv_item(self, item):
if item not in self.path[0].find_object("Inventory").get_objects():
for _item in self.path[0].find_object(
"Inventory").get_objects():
if item.get_text_string() == _item.get_text_string():
self.path[0].find_object("Inventory").remove_object(
_item)
break
else:
self.path[0].find_object("Inventory").remove_object(item)
health = self.get_health()
health["Agility"] += 5
self.set_health(health) # Increase agility
def add_ground_item(self, item, npc=False):
self.path[0].find_object("Surroundings").add_object(item)
if not npc:
self.path[0].find_object("Surroundings").get_objects()[
-1].set_actions([GameAction("Pick-up")])
def remove_ground_item(self, item):
self.path[0].find_object("Surroundings").remove_object(item)
def add_main_action(self, action):
self.path[0].add_action(action)
def clear_main_actions(self):
self.path[0].clear_actions()
def hide_objects(self):
self.path[0].set_text_list([Text("Menu", color=input_color,
font_size=path_font_size,
font_name=path_font)])
self.current_objects = self.path[-1].get_objects()[:]
self.path[-1].clear_objects()
def show_objects(self):
self.path[0].set_text_list([Text("Game", color=input_color,
font_size=path_font_size,
font_name=path_font)])
self.path[-1].add_objects(self.current_objects)
self.current_objects = None
def set_path_index(self, ind):
self.path = self.path[:ind + 1]
def get_path(self):
return self.path
def get_path_text_list(self, string=False):
path_text_list = []
for _object in self.get_path():
if not string:
path_obj = _object.get_text_object()
path_obj.set_font(choice_font, choice_font_size)
path_text_list.append(path_obj)
else:
path_text_list.append(_object.get_text_string())
if type(_object) == GameObject:
if not string:
path_text_list.append(Text(">", color=input_color,
font_size=choice_font_size,
font_name=choice_font))
else:
path_text_list.append(">")
if string:
return "".join(path_text_list)
return path_text_list
def check_caught(self):
"""Check if cannot leave"""
for npc in self.get_npcs():
if npc.target == "You":
if random.randint(0, npc.get_health()["Agility"]) > \
self.get_health()["Agility"]:
return npc # You were caught
return False
def gen_location(self, location_name=None):
"""Chooses a random location to spawn in"""
self.path[0].find_object("Surroundings").clear_objects()
self.path[0].find_object("Surroundings").clear_actions()
available_locations = list(filter(lambda
x: x.get_name() != "Strange-Cavern" and x.get_name() != "Treasure-Hall",
LOCATIONS_LIST))
if "Grendel" in self.uniques:
available_locations = list(
filter(lambda x: x.get_name() != "Treasure-Hall",
LOCATIONS_LIST))
if "Grendel's-Mother" in self.uniques:
available_locations = LOCATIONS_LIST
probabilities = [l.probability for l in available_locations]
if isinstance(location_name, type(None)):
location = weighted_choice(available_locations, probabilities)
else: # Given name, find location
location = None
for _location in available_locations:
if _location.get_type() == location_name:
location = _location
break
location.gen_name()
self.path[0].find_object("Surroundings").add_attribute(
"location", location)
self.path[0].find_object("Surroundings").add_action(GameAction(
"Leave-Location"))
self.path[0].find_object("Surroundings").add_action(GameAction(
"Wait"))
self.path[0].find_object("Surroundings").set_tooltip(
[Text("Location", color=header_color, bold=True, new_line=True,
font_size=header_size),
Text(location.get_name(), new_line=True),
Text("", new_line=True),
*location.gen_desc()])
# Gen items
objects = []
for _weapon in location.gen_objects(location.weapons):
objects.append(GameObject([Text(_weapon.get_name(),
color=weapon_color,
tooltip=_weapon.gen_desc())],
_actions=[GameAction("Pick-up")],
attributes={"Item": _weapon}))
tmax = location.treasure["Max"]
tmin = location.treasure["Min"]
tprob = location.treasure["Probability"]
treasures = []
if random.randint(0, 100) <= tprob:
for i in range(0, random.randint(tmin, tmax)):
treasure_item = treasure.copy(False)
treasures.append(GameObject([Text(
treasure_item.get_name(), color=treasure_color,
tooltip=treasure_item.gen_desc())], _actions=[
GameAction("Pick-up")], attributes={"Treasure": True,
"Item": treasure_item}))
self.gen_npcs()
if location.get_name() == "Herot":
if self.herot:
self.gen_npc(grendel)
self.path[0].find_object("Surroundings").add_objects(objects)
self.path[0].find_object("Surroundings").add_objects(treasures)
self.new_location = True
self.rename_flowers()
self.reset_cooldowns()
def gen_npcs(self, wander=False):
if not wander:
self.name_nums = {}
self.type_nums = {}
# Gen NPCs
location = self.get_path()[0].find_object(
"Surroundings").get_attribute("location")
if "Boss" not in location.get_attributes():
npc_word_list = []
npc_amounts = {}
for npc in location.gen_objects(location.npcs, wander, self.type_nums):
if self.gen_npc(npc):
npc = self.get_path()[0].find_object(
"Surroundings").get_objects()[-1].get_attribute(
"NPC")
npc_word_list.append(npc.get_word())
return npc_word_list
elif not wander:
# Only spawn one random creature
npcs = location.gen_objects(location.npcs)
living_npcs = []
for npc in npcs:
if "Unique" in npc.get_attributes():
if npc.get_type() in self.uniques:
# Already been killed
continue
living_npcs.append(npc)
while len(living_npcs) > 0:
if self.gen_npc(random.choice(living_npcs)):
npc = self.get_path()[0].find_object(
"Surroundings").get_objects()[-1].get_attribute(
"NPC")
return [npc.get_word()]
return []
def gen_npc(self, _npc, new_name=True):
"""Creates an npc and adds to surroundings"""
npc = _npc.copy()
if npc.get_type() in self.uniques and new_name:
return False
if new_name:
npc.gen_name(self.name_nums, self.type_nums)
else:
npc.determine_number(self.name_nums, self.type_nums)
weapon_objects = []
for _weapon in npc.get_weapons():
if "Treasure" in _weapon.get_attributes():
color = treasure_color
else:
color = weapon_color
weapon_objects.append(GameObject([
Text(_weapon.get_name(), color=color,
tooltip=_weapon.gen_desc())],
attributes={"Item": _weapon}))
actions = [GameAction("Attack")]
if "Story-Teller" in npc.get_attributes():
if isinstance(npc.target, type(None)):
actions.append(GameAction([Text("Pay", color=treasure_color,
new_line=True)]))
if "Hrothgar" in npc.get_attributes() and isinstance(npc.target,
type(None)):
actions.append(GameAction([Text("Talk", new_line=True)]))
self.add_ground_item(
GameObject([npc.get_word(False)], _actions=actions,
_objects=weapon_objects,
attributes={"NPC": npc}), npc=True)
return True
def get_npcs(self):
"""Returns NPCs in current surroundings"""
npcs = []
for _object in self.path[0].find_object(
"Surroundings").get_objects():
if "NPC" in _object.get_attributes():
npcs.append(_object.get_attribute("NPC"))
return npcs
def get_health(self):
"""Return Health Stats of self"""
return self.get_character().get_health()
def set_health(self, stats):
"""Sets the health stats for self"""
self.get_character().set_health(stats)
self.update_stats_tooltip()
def update_stats_tooltip(self):
"""Updates stats tooltip with current info"""
desc = [Text("Health", color=header_color, bold=True,
new_line=True, font_size=header_size)]
for stat in self.get_health():
desc += [Text(stat + ": ", color=header_color),
Text(str(self.get_health()[stat]), new_line=True,
color=number_color)]
self.path[0].find_object("Stats").set_tooltip(desc)
self.path[0].find_object("Stats").find_object("You").set_tooltip(
desc)
def get_you_word(self, caps=True, tooltip=None, strip=False,
yourself=False):
if caps:
word = "You "
else:
word = "you "
if yourself:
word = "yourself"
if strip:
word = word.strip()
if isinstance(tooltip, type(None)):
tooltip = self.get_stats_tooltip()[:]
return Text(word, tooltip=tooltip,
color=self_color)
def get_stats_tooltip(self):
return self.path[0].find_object("Stats").get_tooltip()
def update_surroundings(self):