-
Notifications
You must be signed in to change notification settings - Fork 1
/
character.py
426 lines (386 loc) · 14.5 KB
/
character.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
from skills import SKILLS, SKILL_NAMES
from equipment import Equipment, RARITY
from effect import Effect, Buff, Debuff
from plat import nl
from util import pd
STAT_ORDER = ["Strength", "Intellect", "Speed", "Stamina", "Defense",
"Magic Defense"]
TRAITS = {"Beefy": "Increase physical damage inflicted",
"Wizardry": "Increase magical damage inflicted",
"Perseverance": "Chance to survive fatal attacks in combat",
"Scholar": "On level up, chance to choose new skills",
"Self-Improvement": "On level up, chance to choose new passives",
"Quick Learner": "Improved experience gain",
"Merchant Warrior": "Improved gold gain",
"Clarity of Mind": "SP regeneration over time",
"Regeneration": "HP regeneration over time",
"Stocky": "Reduce physical damage taken",
"Mental Toughness": "Reduce magical damage taken",
"Combobreaker!": "Chance to prevent multiple consecutive enemy turns",
"Libra": "See monster stats",
"Sneaky": "Chance to skip non-elite battles (in tower only)",
}
GREEN_HP = "rgb(0,160,0)"
YELLOW_HP = "rgb(200,200,0)"
ORANGE_HP = "rgb(224,136,20)"
RED_HP = "rgb(255,0,0)"
# TRAITS:
# TODO: There are more on the sheet
# -- Increased Stats
# -- Lightning Strike (chance to automatically go again after an attack)
# -- Dodge (chance to avoid all damage from an attack)
class Character(object):
def __init__(self, game):
self.game = game
# Weapon, Hat, Shirt, Pants, Accessory
self.equipment = [None, None, None, None, None]
self.items = []
self.skills = []
self.stats = {"Strength": 20, "Stamina": 20, "Defense": 20, "Speed": 20,
"Intellect": 20, "Magic Defense": 20}
self.gold = 100
self.name = "Hero"
self.base_hp = 20
self.base_sp = 20
self.max_hp = 5 * self.stats["Stamina"] + self.base_hp
self.max_sp = self.base_sp + self.stats["Intellect"]
self.current_hp = self.max_hp
self.current_sp = self.max_sp
self.level = 1
self.exp = 0
self.materials = [0] * len(RARITY) # __:opov
self.buffs = []
self.debuffs = []
self.runes = 1
self.traits = {}
def colored_hp(self):
hp_percent = self.current_hp * 100 // self.max_hp
if hp_percent > 75:
color = GREEN_HP
elif hp_percent > 50:
color = YELLOW_HP
elif hp_percent > 25:
color = ORANGE_HP
else:
color = RED_HP
return "<span style=\"color: {}\">{} / {}</span>".format(color, int(self.current_hp), self.max_hp)
@classmethod
def debug_character(cls, game, level, choice_text):
character = Character(game)
for i in range(1, level):
character.level_up([])
trait_choices = character.get_trait_choices()
choice = ""
while choice not in TRAITS:
choice = game.rng.choice(trait_choices)
character.learn_trait(choice)
skill_choices = character.get_skill_choices()
choice = ""
while choice not in SKILL_NAMES + ["Improve stats"]:
choice = game.rng.choice(skill_choices)
chance = 10.0 / i
if game.rng.random() < chance:
character.learn_skill(choice)
else:
character.learn_skill("Improve stats")
character.make_debug_equipment(level, choice_text)
character.level = level
return character
def make_debug_equipment(self, level, choice):
for i in range(len(self.equipment)):
self.equip(Equipment.get_new_gear(self.game, level, i, choice, 3))
def add_item(self, item):
if len(self.items) >= 3:
return False
self.items.append(item)
return True
def use_item(self, index, monster, logs):
item = self.items.pop(index)
item.apply(self, monster, logs)
def add_buff(self, new_buff):
Buff.add_buff(self.buffs, new_buff)
self.recalculate_maxes()
def add_debuff(self, new_debuff):
Debuff.add_debuff(self.debuffs, new_debuff)
self.recalculate_maxes()
def pass_time(self, time_passed):
remaining_buffs = []
for buff in self.buffs:
if buff.turn_by_turn():
for _ in range(time_passed):
buff.pass_time(1)
effect = Effect.get_combined_impact("HP Restore", self.buffs,
self.debuffs)
self.restore_hp(effect)
if not buff.active():
break
else:
buff.pass_time(time_passed)
if buff.active():
remaining_buffs.append(buff)
self.buffs = remaining_buffs
restored_hp = self.traits.get("Regeneration", 0) * 3 * time_passed
restored_sp = self.traits.get("Clarity of Mind", 0) * time_passed
self.restore_hp(restored_hp)
self.restore_sp(restored_sp)
self.recalculate_maxes()
remaining_debuffs = []
for debuff in self.debuffs:
debuff.pass_time(time_passed)
if debuff.active():
remaining_debuffs.append(debuff)
self.debuffs = remaining_debuffs
self.recalculate_maxes()
def gain_gold(self, amount):
amount_gained = amount * (1.00 + (0.08 * self.traits.get("Merchant Warrior", 0)))
amount_gained = int(amount_gained)
self.gold += amount_gained
return amount_gained
def line_of_credit(self):
return self.gold >= 0 and self.traits.get("Merchant Warrior", 0) >= 4
def can_afford(self, amount):
if self.gold >= amount:
return True
if self.line_of_credit():
return True
return False
def spend_gold(self, amount, logs):
self.gold -= amount
def make_initial_equipment(self, choice):
for i in range(len(self.equipment)):
self.equip(Equipment.get_new_gear(self.game, 1, i, choice))
def __str__(self):
pieces = []
pieces.append(nl("Character:"))
pieces.append(nl("HP: {} / {}").format(self.current_hp, self.max_hp))
pieces.append(nl("SP: {} / {}").format(self.current_sp, self.max_sp))
pieces.append("Level: {} ".format(self.level))
pieces.append(nl("({} / {})").format(self.exp, self.next_level_exp()))
for stat in STAT_ORDER:
pieces.append("{}: {} ({}) ".format(stat, self.get_effective_stat(stat),
self.stats[stat]))
pieces.append(nl())
pieces.append(nl("Equipment:"))
for piece in self.equipment:
pieces.append(nl(str(piece)))
pieces.append("Traits: ")
if sum(self.traits.values()) == 0:
pieces.append("None")
else:
for trait in self.traits.keys():
if self.traits.get(trait, 0) > 0:
pieces.append("{}: {} ".format(trait, self.traits.get(trait, 0)))
pieces.append(nl())
pieces.append("Skills: ")
if not self.skills:
pieces.append("None")
else:
for skill in self.skills:
pieces.append("{}: {} ".format(skill.get_name(), skill.level))
pieces.append(nl())
pieces.append("Materials: ")
if sum(self.materials) == 0:
pieces.append(nl("None"))
else:
for i in range(len(self.materials)):
if self.materials[i] > 0:
pieces.append("{}: {} ".format(RARITY[i], self.materials[i]))
pieces.append(nl())
pieces.append("Buffs: ")
pieces.append(", ".join(str(buff) for buff in self.buffs))
if self.buffs:
pieces.append(nl())
else:
pieces.append(nl("None"))
pieces.append("Items: ")
if self.items:
pieces.append(", ".join(item.get_name() for item in self.items))
pieces.append(nl())
else:
pieces.append(nl("None"))
pieces.append("Corrupted runes: {}".format(self.runes))
return "".join(pieces)
def restore_hp(self, amount=None):
# NB: Amount may be float if it comes from an HP Restore buff like Renew.
old_current = self.current_hp
if amount is None:
self.current_hp = self.max_hp
else:
self.current_hp = min(self.max_hp, int(self.current_hp + amount))
return self.current_hp - old_current
def restore_sp(self, amount=None):
old_current = self.current_sp
if amount is None:
self.current_sp = self.max_sp
else:
self.current_sp = min(self.max_sp, self.current_sp + amount)
return self.current_sp - old_current
def rest(self):
hp_gained = self.max_hp/10
return self.restore_hp(hp_gained)
def apply_death(self, logs, penalty=True):
logs.append("You have been defeated.")
self.restore_hp()
self.restore_sp()
self.debuffs = []
if penalty:
logs.append("You were found by a passerby and brought back to town.")
if self.gold >= 0:
lost_gold = int((self.gold / 2) * (.8 ** self.traits.get("Merchant Warrior", 0)))
self.gold -= lost_gold
logs.append("You lost {} gold.".format(lost_gold))
self.buffs = []
def get_effective_stat(self, stat):
value = self.stats[stat]
for piece in self.equipment:
if piece:
value += piece.get_stat_value(stat)
effect = Effect.get_combined_impact(stat, self.buffs, self.debuffs)
value = int(value * effect)
return value
def equip(self, item):
slot = item.slot
removed = self.equipment[slot]
self.equipment[slot] = item
self.recalculate_maxes()
return removed
def get_damage(self):
# 0 is weapon
return self.equipment[0].get_damage()
def get_damage_type(self):
return self.equipment[0].get_damage_type()
def next_level_exp(self):
return int(self.level * 100 * (1.01 ** self.level))
def recalculate_maxes(self):
new_max_hp = self.get_effective_stat("Stamina") * 5 + self.base_hp
difference = new_max_hp - self.max_hp
self.current_hp += max(0, difference) # Don't lose HP for equipping
self.max_hp = new_max_hp
self.current_hp = min(self.current_hp, self.max_hp) # Unless max_hp drops
new_max_sp = self.get_effective_stat("Intellect") + self.base_sp
#difference = new_max_sp - self.max_sp
#self.current_sp += max(0, difference) # Don't lose HP for equipping
self.max_sp = new_max_sp
self.current_sp = min(self.current_sp, self.max_sp) # Unless max_sp drops
def level_up(self, logs):
for stat in self.stats.keys():
increase = self.game.rng.randint(1, 3)
if increase > 0:
self.stats[stat] += increase
logs.append("You have gained {} {}.".format(increase, stat))
hp_gain = self.game.rng.randint(10, 20)
sp_gain = self.game.rng.randint(5, 10)
self.base_hp += hp_gain
self.base_sp += sp_gain
logs.append("You have gained {} HP.".format(hp_gain))
logs.append("You have gained {} SP.".format(sp_gain))
self.recalculate_maxes()
def train_xp(self, level, logs):
return self.gain_exp(level * 25, level, logs, level_adjust=False)
def train_stats(self, logs):
stat = self.game.rng.choice(list(self.stats.keys()))
self.stats[stat] += 1
logs.append("Gained +1 {}.".format(stat))
def gain_materials(self, materials):
for i in range(len(materials)):
self.materials[i] += materials[i]
def get_trait_choices(self):
choices = [""]
reroll_trait_level = self.traits.get("Self-Improvement", 0)
reroll_chance = float(reroll_trait_level) / (reroll_trait_level + 1)
if self.game.rng.random() < reroll_chance:
choices.append("Get New Traits")
while len(choices) < 4:
best_roll, best_trait = 0.0, None
for trait in TRAITS.keys():
rerolls = max(1, int((self.traits.get(trait, 0) + 1) ** .5))
roll = self.game.rng.min_random_n_times(rerolls)
if trait == "Libra" and self.traits.get(trait, 0) > 0: # Only one libra level
roll = 0.0
if roll > best_roll:
best_roll, best_trait = roll, trait
if best_trait not in choices:
choices.append(best_trait)
return choices
def learn_trait(self, trait):
if trait == "Get New Traits":
return False
assert trait in TRAITS
self.traits[trait] = self.traits.get(trait, 0) + 1
return True
def get_skill_choices(self):
# TODO: Have one of the skills be a new one, even if character has all
# three skills? In this case, we'd have to replace one of the existing
# skills, which might get tricky.
choices = []
choices.append("Improve stats")
# If we have three skills already, we can just choose from those
if len(self.skills) == 3:
choices.extend(skill.get_name() for skill in self.skills)
else:
reroll_skill_level = self.traits.get("Scholar", 0)
reroll_chance = float(reroll_skill_level) / (reroll_skill_level + 1)
if self.game.rng.random() < reroll_chance:
choices.append("Get New Skills")
while len(choices) < 4:
best_roll, best_skill = 0.0, None
for skill_name in SKILL_NAMES:
current_skill = self.have_skill(skill_name)
if current_skill is None:
rerolls = 1
else:
rerolls = int(current_skill.level ** .5)
roll = self.game.rng.min_random_n_times(rerolls)
if roll > best_roll:
best_roll, best_skill = roll, skill_name
if best_skill not in choices:
choices.append(best_skill)
return choices
def have_skill(self, name):
for skill in self.skills:
if skill.get_name() == name:
return skill
return None
def learn_skill(self, skill_name):
if skill_name == "Improve stats":
for stat in self.stats.keys():
self.stats[stat] += 1
self.recalculate_maxes()
return True
if skill_name == "Get New Skills":
return False
assert skill_name in SKILL_NAMES
current_skill = self.have_skill(skill_name)
if current_skill:
current_skill.level += 1
return True
else:
assert len(self.skills) < 3
new_skill = None
for skill in SKILLS:
skill_instance = skill(self.game)
if skill_instance.get_name() == skill_name:
new_skill = skill_instance
self.skills.insert(0, new_skill)
return True
def gain_exp(self, exp, encounter_level, logs, level_adjust=True):
exp_gained = int(exp * (1.0 + (0.03 * self.traits.get("Quick Learner", 0))))
level_difference = encounter_level - self.level
if level_adjust:
exp_gained = int(exp_gained * (1.03 ** level_difference))
xp_buff = Effect.get_combined_impact("XP Gain", self.buffs, self.debuffs)
total_xp_gain = int(exp_gained * xp_buff)
self.exp += total_xp_gain
added_xp = total_xp_gain - exp_gained
if added_xp:
logs.append("You have gained {} XP ({} from buffs).".format(exp_gained, pd(added_xp)))
else:
logs.append("You have gained {} XP.".format(exp_gained))
levels_gained = 0
while self.exp >= self.next_level_exp():
self.exp -= self.next_level_exp()
self.level += 1
logs.append("You have reached level {}!".format(self.level))
self.level_up(logs)
levels_gained += 1
return levels_gained