-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenemy.py
296 lines (273 loc) · 11.6 KB
/
enemy.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
import math
import random
from copy import deepcopy
from time import sleep
from uuid import uuid4
import displayer as view
import effect_interface as ei
from ansi_tags import ansiprint
from definitions import State
from entities import Damage
from message_bus_tools import Message, Registerable, bus
from card_catalog import Card
from player import Player
from effect_catalog import Effect
class Action():
'''A single action that an enemy takes'''
def __init__(self, action_type, ) -> None:
pass
def execute(self):
pass
class Enemy(Registerable):
registers = [Message.START_OF_TURN, Message.END_OF_TURN, Message.ON_DEATH_OR_ESCAPE]
player = None
def __init__(self, health_range: list, block: int, name: str, powers: list[Effect] | None = None):
self.uid = uuid4()
if not powers:
powers = []
actual_health = random.randint(health_range[0], health_range[1])
self.health = actual_health
self.max_health = actual_health
self.block = block
self.name = name
self.third_person_ref = (
f"{self.name}'s" # Python f-strings suck so I have to use this
)
self.past_moves = ["place"] * 3
self.intent: str = ""
self.next_move: list[tuple[str, str, tuple] | tuple[str, tuple]] = ""
self.state = State.ALIVE
self.buffs = powers
self.debuffs = []
self.stolen_gold = 0
self.awake_turns = 0
self.mode = ""
self.flames = -1
self.upgrade_burn = False
self.active_turns = 1
def __str__(self):
return "Enemy"
def __repr__(self):
status = f"{self.name} (<red>{self.health} </red>/ <red>{self.max_health}</red> | <light-blue>{self.block} Block</light-blue>)"
for effect in self.buffs + self.debuffs:
status += " | " + effect.get_name()
if self.flames > 0:
status += f" | <yellow>{self.flames} Flames</yellow>"
status += " | Intent: " + self.intent.replace('Σ', '')
return status
def set_intent(self):
pass
def execute_move(self, player: Player, enemies: list["Enemy"]):
moves = 1
display_name = "DEFAULT: UNKNOWN"
for action in self.next_move:
if moves == 1 and len(action) > 2:
display_name, action, parameters = action
else:
action, parameters = action
if action not in ("Attack", "Buff", "Debuff", "Remove Effect", "Status", "Block"):
self.misc_move(enemies)
sleep(1)
view.clear()
return
ansiprint(f"<bold>{display_name}</bold>\n" if moves == 1 else "", end="")
sleep(0.6)
if action == "Attack":
dmg = parameters[0]
times = parameters[1] if len(parameters) > 1 else 1
self.attack(dmg, times, target=player)
elif action == "Buff":
buff = parameters[0]
amount = parameters[1] if len(parameters) > 1 else 1
target = parameters[2] if len(parameters) > 2 else self
ei.apply_effect(self, self, buff, amount)
elif action == "Debuff":
debuff = parameters[0]
amount = parameters[1] if len(parameters) > 1 else 1
target = parameters[2] if len(parameters) > 2 else player
ei.apply_effect(target, self, debuff, amount)
elif action == "Remove Effect":
effect_name = parameters[0]
effect_type = parameters[1]
self.remove_effect(effect_name, effect_type)
elif action == "Status":
assert (len(parameters) >= 3), f"Status action requires 3 parameters: given {parameters}"
status = parameters[0]
amount = parameters[1]
location = parameters[2].lower()
self.status(status, amount, location, player=player)
elif action == "Block":
block = parameters[0]
target = parameters[1] if len(parameters) > 1 else None
self.blocking(block, target)
sleep(0.2)
moves += 1
if display_name == "Inferno" and self.flames > -1:
self.upgrade_burn = True
self.flames = 0
sleep(0.5)
self.past_moves.append(display_name)
self.active_turns += 1
if self.flames > -1:
self.flames += 1
def misc_move(self, enemies):
if len(self.next_move[0]) > 2:
name, func_name, parameters = self.next_move[0]
else:
name, func_name = self.next_move[0]
ansiprint(f"<bold>{name}</bold>")
sleep(0.6)
if func_name == "Cowardly":
ansiprint("<italic>Hehe. Thanks for the money.<italic>")
self.state = State.ESCAPED
ansiprint(f"<italic><red>{self.name} has escaped</red></italic>")
elif func_name == "Sleeping":
sleeptalk = parameters[0]
ansiprint(f"<italic>{sleeptalk}</italic>")
elif func_name == "Stunned":
ansiprint("<italic>Stunned!</italic>")
elif func_name == "Summon":
enemies = tuple(parameters[0])
amount = int(parameters[1]) if len(parameters) > 1 else 1
choice = bool(parameters[2]) if len(parameters) > 2 else False
self.summon(enemies, amount, choice)
elif func_name == "Explode":
pass
elif func_name == "Rebirth":
for debuff in self.debuffs:
if debuff not in ei.NON_STACKING_EFFECTS:
self.debuffs[debuff] = 0
else:
self.debuffs[debuff] = False
self.buffs["Curiosity"] = False
self.buffs["Unawakened"] = False
elif func_name == "Revive":
self.health = math.floor(self.health * 0.5)
ansiprint(f"<bold>{self.name}</bold> revived!")
elif func_name == "Charging":
message = parameters[0]
ansiprint(f"{message}")
elif func_name == "Split":
split_into = {
"Slime Boss": (
Enemy(self.health, 0, "Acid Slime(L)"),
Enemy(self.health, 0, "Spike Slime (L)"),
),
"Acid Slime (L)": (
Enemy(self.health, 0, "Acid Slime(M)"),
Enemy(self.health, 0, "Acid Slime(M)"),
),
"Spike Slime (L)": (
Enemy(self.health, 0, "Spike Slime (M)"),
Enemy(self.health, 0, "Spike Slime (M)"),
),
}
for _ in range(2):
enemies.append(split_into[self.name])
ansiprint(f"{self.name} split into 2 {split_into[self.name].name}s")
self.active_turns += 1
def die(self):
"""
Dies.
"""
print(f"{self.name} has died.")
self.state = State.DEAD
for effect in self.buffs + self.debuffs:
effect.unsubscribe()
bus.publish(Message.ON_DEATH_OR_ESCAPE, (self))
def debuff_and_buff_check(self):
"""
Not finished
"""
def move_spam_check(self, target_move, max_count) -> bool:
"""Returns False if the move occurs [max_count] times in a row. Otherwise returns True"""
enough_moves = len(self.past_moves) >= max_count
return not(enough_moves and all(move == target_move for move in self.past_moves[-max_count:]))
def attack(self, dmg: int, times: int, target: Player):
for _ in range(times):
if target.state == State.DEAD:
ansiprint(f"{self.name} stopped attacking: {target.name} is already dead.")
return
modifiable_dmg = Damage(dmg)
bus.publish(Message.BEFORE_ATTACK, (self, target, modifiable_dmg)) # allows for damage modification from relics/effects
dmg = modifiable_dmg.damage
if dmg <= target.block:
target.block -= dmg
dmg = 0
ansiprint("<light-blue>Blocked</light-blue>")
elif dmg > target.block:
dmg -= target.block
dmg = max(0, dmg)
ansiprint(f"{self.name} dealt {dmg}(<light-blue>{target.block} Blocked</light-blue>) damage to you.")
target.block = 0
target.health -= dmg
bus.publish(Message.ON_PLAYER_HEALTH_LOSS, None)
if target.health <= 0:
target.die()
bus.publish(Message.AFTER_ATTACK, (self, target, dmg))
sleep(1)
def remove_effect(self, effect_name, effect_type):
if effect_name not in ei.ALL_EFFECTS:
raise ValueError(f"{effect_name} is not a member of any debuff or buff list.")
effect_types = {"Buffs": self.buffs, "Debuffs": self.debuffs}
if effect_name not in ei.NON_STACKING_EFFECTS:
effect_types[effect_type][effect_name] = 0
else:
effect_types[effect_type][effect_name] = False
def blocking(self, block: int, target: "Enemy" = None, context: str=None):
if not target:
target = self
target.block += block
if context:
ansiprint(f"{target.name} gained {block} <blue>Block</blue> from {context}")
else:
ansiprint(f"{target.name} gained {block} <blue>Block</blue>")
sleep(1)
def status(self, status_card: Card, amount: int, location: str, player: Player):
locations = {
"draw pile": player.draw_pile,
"discard pile": player.discard_pile,
"hand": player.hand,
}
pile = locations[location]
status_card = status_card()
for _ in range(amount):
upper_bound = len(location) - 1 if len(location) > 0 else 1
insert_index = random.randint(0, upper_bound)
pile.insert(insert_index, deepcopy(status_card))
ansiprint(f"{player.name} gained {amount} {status_card.name} \nPlaced into {location}")
sleep(1)
def summon(self, enemy, amount: int, random_enemy: bool, enemies):
if len(enemy) == 1:
enemy = enemy[0]
for _ in range(amount):
chosen_enemy = random.choice(enemy) if random_enemy else enemy
enemies.append(chosen_enemy)
ansiprint(f"<bold>{chosen_enemy.name}</bold> summoned!")
def start_turn(self):
ansiprint(f"{self.name}'s current state: {self.state}")
if self.state == State.ALIVE:
for effect in self.buffs + self.debuffs:
if effect.subscribed is False:
effect.register(bus)
ansiprint(f"<underline><bold>{self.name}</bold></underline>:")
if "Block" not in self.intent: # Checks the last move(its intent hasn't been updated yet) used to see if the enemy Blocked last turn
self.block = 0
ei.tick_effects(self)
print()
self.set_intent()
def take_turn(self, player: Player, enemies: list["Enemy"]):
if self.state == State.ALIVE:
self.execute_move(player, enemies)
def callback(self, message, data):
global bus
if message == Message.START_OF_TURN:
self.start_turn()
elif message == Message.END_OF_TURN:
player, enemies = data
self.take_turn(player, enemies)
elif message == Message.ON_DEATH_OR_ESCAPE:
# This is meant to react to OTHER entities dying, not itself
dead_entity = data
if dead_entity != self:
ansiprint(f"{self.name} observes {dead_entity.name}'s death.")