-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fight_encounter.py
333 lines (264 loc) · 12.2 KB
/
fight_encounter.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
import random
import copy
import utilities
from character import Character
import enemy
from enemy import Enemy
from enemies import enemies
from summon import Summon
from summons import summons
from elements import Elements
from items.item import ItemType
import ability
import spell
import skill
import dice
class Fight:
def __init__(self, _enemies: [enemy.Enemy], characters: [Character], level: int):
self.enemies = _enemies
self.characters = characters
self.level = level
self.description = 'You see:'
for e in self.enemies:
self.description += f'\n {e.name}'
self.inits = self.characters + self.enemies
self.update_turn_order()
self.elements_strong = []
self.elements_weak = []
@property
def states(self):
return self.elements_strong + self.elements_weak
def update_turn_order(self):
self.inits.sort(key=lambda x: x.init + x.bonus_init, reverse=True)
def add_enemy(self, enemy_str: str):
e = copy.deepcopy(enemies[enemy_str])
e.scale(self.level)
self.enemies.append(e)
self.inits.append(e)
self.description += f'\n {e.name}'
# Note: don't update turn order here, this could cause some enemies to go twice
return e.name
def remove_character(self, character: Character):
self.characters.remove(character)
self.inits.remove(character)
return self.unsummon_all_by_character(character)
def remove_enemy(self, enemy_to_remove: Enemy):
self.enemies.remove(enemy_to_remove)
self.inits.remove(enemy_to_remove)
def summon(self, summon: Summon, owner: Character):
summon.owner = owner.name
summon.name = f'{owner.name}\'s {summon.name}'
summon.scale(self.level)
self.characters.append(summon)
self.inits.append(summon)
return f'{summon.owner} summoned {summon.name}.'
def unsummon(self, summon: Summon) -> str:
self.characters.remove(summon)
self.inits.remove(summon)
return f'{summon.owner}\'s {summon.name} vanished.'
def unsummon_all_by_character(self, owner: Character) -> bool:
i = 0
for s in [x for x in self.characters if isinstance(x, Summon) and x.owner == owner.name]:
i += 1
self.characters.remove(s)
self.inits.remove(s)
return True if i > 0 else False
def unsummon_all(self):
for s in [x for x in self.characters if isinstance(x, Summon)]:
self.characters.remove(s)
self.inits.remove(s)
def activate(self, element: Elements):
if element in self.elements_weak:
self.elements_strong.append(element)
self.elements_weak.remove(element)
elif element not in self.elements_weak and element not in self.elements_strong:
self.elements_strong.append(element)
def consume(self, element: Elements):
if element in self.elements_weak:
self.elements_weak.remove(element)
elif element in self.elements_strong:
self.elements_strong.remove(element)
def end_of_turn(self):
check = len(self.elements_weak) + len(self.elements_strong)
self.elements_weak = self.elements_strong.copy()
self.elements_strong = []
if check > 0:
return True
return False
def use_ability(self, char: Character, ab, target):
out = f'{char.name} used {ab.name} on {target.name}.'
targets = [target]
i = ab.area
while i > 0:
if ab is spell.Spell and not ab.targets_enemies:
if self.characters.index(target) + i <= len(self.characters) - 1:
targets.append(self.characters[self.characters.index(target) + i])
if self.characters.index(target) - i >= 0:
targets.insert(0, self.characters[self.characters.index(target) - i])
else:
if self.enemies.index(target) + i <= len(self.enemies) - 1:
targets.append(self.enemies[self.enemies.index(target) + i])
if self.enemies.index(target) - i >= 0:
targets.insert(0, self.enemies[self.enemies.index(target) - i])
i -= 1
crit = False
chance = None
if isinstance(ab, skill.Skill):
chance = char.eq_weapon.base_crit_chance
elif isinstance(ab, spell.Spell):
chance = ab.base_crit_chance
if random.random() <= chance:
crit = True
out += f' CRITICAL HIT!'
for _target in targets:
for effect in ab.effects:
if effect.type == ability.EffectType.damage_health:
dmgs = _target.take_damage(char.deal_damage(effect, critical=crit, multi=1.0 if isinstance(ab, spell.Spell) else ab.multiplier), char.get_ele_pens())
shock = False
confusion = False
for dmg in dmgs:
ele = Elements(dmg[1])
out += f'\n{_target.name} suffered {dmg[0]} {ele.name} damage.'
shock = True if ele == Elements.electricity else False
confusion = True if ele == Elements.water else False
if shock:
target.shock += 1
if confusion:
target.confusion += 1
elif effect.type == ability.EffectType.burn:
if target.apply_burn(effect.effect_turns, effect.dot_value,
char.dot_effect + char.bonus_dot_effect, char.dot_duration + char.bonus_dot_duration):
out += f'\n{target.name} is burning.'
else:
out += f'\n{target.name} is already seriously burning.'
elif effect.type == ability.EffectType.bleed:
if target.apply_bleed(effect.effect_turns, effect.dot_value,
char.dot_effect + char.bonus_dot_effect, char.dot_duration + char.bonus_dot_duration):
out += f'\n{target.name} is bleeding.'
else:
out += f'\n{target.name} is bleeding more severely.'
elif effect.type == ability.EffectType.restore_health:
heal = _target.restore_health(dice.roll(dice.count(char.level), effect.dice_value, crit), char)
out += f'\n{_target.name} regained {heal} health.'
elif effect.type == ability.EffectType.restore_stamina:
stam = _target.restore_stamina(dice.roll(dice.count(char.level), effect.dice_value, crit), char)
out += f'\n{_target.name} regained {stam} stamina.'
elif effect.type == ability.EffectType.restore_mana:
mana = _target.restore_mana(dice.roll(dice.count(char.level), effect.dice_value, crit), char)
out += f'\n{_target.name} regained {mana} mana.'
elif effect.type in [ability.EffectType.buff, ability.EffectType.debuff]:
amt = dice.roll(char.level, effect.dice_value)
amt = -amt if effect.type == ability.EffectType.debuff else amt
overwrite = target.apply_status_effect(effect.status_effect_name, effect.stat, amt,
effect.effect_turns)
out += f'\n{_target.name} has been affected by {effect.status_effect_name}.'
if overwrite is not None:
out += f' The existing {overwrite} was replaced.'
else:
raise Exception(f'{char.name} used ability {ab.name} with unsupported effect type {effect.type}')
if isinstance(ab, spell.Spell):
for summ_str in ab.summon:
s = copy.deepcopy(summons[summ_str])
out += '\n' + self.summon(s, char)
for stat in ab.cost.keys():
cost = ab.cost[stat]
if stat == 'h':
char.current_health -= cost
elif stat == 's':
char.current_stamina -= cost
elif stat == 'm':
char.current_mana -= cost
else:
raise Exception(f'{char.name} used ability {ab.name} with unsupported cost {cost} {stat}')
for state in ab.consumes:
self.consume(state)
out += f'\n{state.name.capitalize()} has been consumed.'
for state in ab.activates:
self.activate(state)
out += f'\n{state.name.capitalize()} has been infused.'
char.save()
return out
def estimate_damage_from_enemy_action(self, user, character, action):
amt = 0
characters = action.get_aoe_targets(self.characters, character)
for c in characters:
amt += c.estimate_damage_from_enemy_action(user, action)
return amt
def display_active_elements(self):
if len(self.elements_strong) + len(self.elements_weak) > 0:
out = 'Strong: '
for ele in self.elements_strong:
out += f'{utilities.get_elemental_symbol(ele)} '
out += '\nWeak: '
for ele in self.elements_weak:
out += f'{utilities.get_elemental_symbol(ele)} '
else:
out = 'No elements are infused in the environment.'
return out
def display_ally_menu(self, ally):
out = 'Allies:'
i = 1
for c in self.characters:
if c != ally:
if isinstance(c, Character):
out += f'\n{i} - {c.name} {c.current_health}h {c.current_stamina}s {c.current_mana}m'
elif isinstance(c, Summon):
out += f'\n{i} - {c.owner}\'s {c.name} {c.current_health}h'
else:
out += f'\n{i} - YOU {c.current_health}h {c.current_stamina}s {c.current_mana}m'
return out
@staticmethod
def display_action_menu(char: Character):
if char.has_consumables():
return '1 - Ability\n2 - Item\n3 - Recover'
else:
return '1 - Ability\n2 - Item (None)\n3 - Recover'
@staticmethod
def display_ability_cost(cost: dict):
out = ''
if cost['h'] > 0:
out += f'{cost["h"]}h '
if cost['s'] > 0:
out += f'{cost["s"]}s '
if cost['m'] > 0:
out += f'{cost["m"]}m'
return out
@staticmethod
def display_ability_menu(character):
out = 'Abilities:'
for i in range(1, len(character.ability_slots) + 1):
if character.ability_slots[str(i)] is not None:
_ability = utilities.get_ability_by_name(character.ability_slots[str(i)])
cost = f'{Fight.display_ability_cost(_ability.cost)}'
ability_type = 'Error'
if isinstance(_ability, skill.Skill):
ability_type = 'Skill'
elif isinstance(_ability, spell.Spell):
ability_type = 'Spell'
activates = ''
consumes = ''
if len(_ability.activates) > 0:
activates += ' Activates: '
for ele in _ability.activates:
activates += utilities.get_elemental_symbol(ele) + ' '
if len(_ability.consumes) > 0:
consumes += ' Consumes: '
for ele in _ability.consumes:
consumes += utilities.get_elemental_symbol(ele) + ' '
out += f'\n {str(i)} - {_ability.name} ({ability_type}) - {cost}{consumes}{activates}'
return out
@staticmethod
def display_item_menu(character):
indices_counter = 0
display_counter = 1
display_string = 'Consumables in inventory:'
indices = []
for item in character.inventory:
if item.itype == ItemType.potion.value:
display_string += f'\n{display_counter} - {item.name} ({item.uses})'
display_counter += 1
indices.append(indices_counter)
indices_counter += 1
if indices_counter == 0:
display_string += '\n None'
return display_string, indices