-
Notifications
You must be signed in to change notification settings - Fork 4
/
entityeffect.py
575 lines (467 loc) · 25.1 KB
/
entityeffect.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
import random
import trigger
from animation import animate_point
import colors
from compositecore import Leaf
import gametime
from graphic import GraphicChar
import icon
import messenger
from statusflags import StatusFlags
# TODO: Idea replace effect types with spoofchildren and add time to live to spoof children.
# Effect types in execution order
class EffectTypes(object):
EFFECT_REMOVER = 0
STATUS_REMOVER = 1
ADD_SPOOF_CHILD = 2
REMOVE_CHILD = 3
BLOCKER = 4
STATUS_ADDER = 5
TELEPORT = 6
HEAL = 7
UI = 8
DAMAGE = 9
EQUIPMENT = 10
ALLTYPES = [EFFECT_REMOVER, STATUS_REMOVER, ADD_SPOOF_CHILD, REMOVE_CHILD, BLOCKER, STATUS_ADDER,
TELEPORT, HEAL, UI, DAMAGE, EQUIPMENT]
class EffectStackID(object):
SLIME_DISSOLVE = "slime_dissolve"
class EffectQueue(Leaf):
def __init__(self):
super(EffectQueue, self).__init__()
self.component_type = "effect_queue"
self._effect_queue = [None for _ in range(len(EffectTypes.ALLTYPES))]
for effect_type in EffectTypes.ALLTYPES:
self._effect_queue[effect_type] = []
@property
def effects(self):
return [effect for effect_group in self._effect_queue for effect in effect_group]
def add(self, effect):
if effect.meld_id:
if not any(effect.meld_id == e.meld_id for e in self.effects):
self._add(effect)
else:
[e for e in self.effects][0].meld(effect)
else:
self._add(effect)
def _add(self, effect):
self._effect_queue[effect.effect_type].append(effect)
effect.queue = self
def remove(self, effect):
self._effect_queue[effect.effect_type].remove(effect)
effect.queue = None
def remove_effects_with_id(self, effect_id):
for index in range(len(self._effect_queue)):
self._effect_queue[index] = [effect for effect in self._effect_queue[index]
if not effect.effect_id == effect_id]
# TODO this is old lookup if can remove.
def remove_status_adder_of_status(self, id_to_remove):
for index in range(len(self.queue.effects)):
self.queue.effects[index] = [effect for effect in self.queue.effects[index]
if not effect.effect_id == id_to_remove]
def update(self, time):
for effect_type_queue in EffectTypes.ALLTYPES:
for effect in self._effect_queue[effect_type_queue]:
effect.update(time)
class EntityEffect(object):
def __init__(self, source_entity, time_to_live, effect_type, meld_id=None, effect_id=None):
self.source_entity = source_entity
self.time_to_live = time_to_live
self.effect_type = effect_type
self.is_blocked = False
self.meld_id = meld_id
self.effect_id = effect_id
self.queue = None
self.time_alive = 0
def update(self, time_spent):
pass
def meld(self, other_effect):
pass
@property
def target_entity(self):
return self.queue.parent
def is_new_round(self, time_spent):
turns_alive = int(self.time_alive / gametime.single_turn)
new_turns_alive = int((self.time_alive + time_spent) / gametime.single_turn)
return new_turns_alive > turns_alive
def tick(self, time_spent):
self.time_alive += time_spent
self.time_to_live = self.time_to_live - time_spent
if self.time_to_live < 1:
self._on_remove_effect()
self.queue.remove(self)
def _on_remove_effect(self):
pass
class StatusRemover(EntityEffect):
def __init__(self, source_entity, status_type_to_remove, time_to_live=1):
super(StatusRemover, self).__init__(source_entity,
time_to_live,
EffectTypes.STATUS_REMOVER)
self.status_type_to_remove = status_type_to_remove
def update(self, time_spent):
self.queue.remove_status_adder_of_status(self.status_type_to_remove)
self.tick(time_spent)
class EffectRemover(EntityEffect):
def __init__(self, source_entity, effect_id_to_remove, time_to_live=1, message=None):
super(EffectRemover, self).__init__(source_entity, time_to_live,
EffectTypes.EFFECT_REMOVER)
self.the_message = message
self.effect_id_to_remove = effect_id_to_remove
def update(self, time_spent):
old_size = len(self.queue.effects)
self.queue.remove_effects_with_id(self.effect_id_to_remove)
if not old_size == len(self.queue.effects):
self.message()
self.tick(time_spent)
def message(self):
messenger.msg.send_visual_message(self.the_message % {"source_entity": self.source_entity.description.long_name,
"target_entity": self.target_entity.description.long_name},
self.target_entity.position.value)
class HeartStop(EntityEffect):
def __init__(self, source_entity, time_to_live=1, message=messenger.HEART_STOP_MESSAGE):
super(HeartStop, self).__init__(source_entity, time_to_live, EffectTypes.STATUS_REMOVER)
self.message = message
def send_message(self):
messenger.msg.send_visual_message(self.message % {"source_entity": self.source_entity.description.long_name,
"target_entity": self.target_entity.description.long_name},
self.target_entity.position.value)
def update(self, time_spent):
if self.is_new_round(time_spent):
gray_heart = GraphicChar(None, colors.GRAY, icon.HEART)
self.target_entity.char_printer.append_graphic_char_temporary_frames([gray_heart])
self.send_message()
self.tick(time_spent)
def _on_remove_effect(self):
self.target_entity.health_modifier.kill()
class StatusAdder(EntityEffect):
def __init__(self, source_entity, status_flag, time_to_live=1):
super(StatusAdder, self).__init__(source_entity, time_to_live,
EffectTypes.STATUS_ADDER)
self.status_flag = status_flag
def update(self, time_spent):
status_flags = StatusFlags([self.status_flag])
status_flags.to_be_removed = True
self.target_entity.set_child(status_flags)
self.tick(time_spent)
class Teleport(EntityEffect):
def __init__(self, source_entity, time_to_live=1):
super(Teleport, self).__init__(source_entity, time_to_live, EffectTypes.TELEPORT)
def update(self, time_spent):
positions = (self.target_entity.dungeon_level.value.
get_walkable_positions(self.target_entity,
self.target_entity.position.value))
random_positions = random.sample(positions, len(positions))
for position in random_positions:
teleport_successful = self.target_entity.mover.try_move(position)
if teleport_successful:
self.target_entity.game_state.value.dungeon_needs_redraw = True
break
self.tick(time_spent)
class AttackEntityEffect(EntityEffect):
def __init__(self, source_entity, damage, damage_types, accuracy, crit_chance=0, crit_multiplier=2,
hit_message=messenger.HIT_MESSAGE, miss_message=messenger.MISS_MESSAGE,
crit_message=messenger.CRIT_MESSAGE, hit_trigger_effect=[], meld_id=None, time_to_live=1,
attack_effects=[]):
super(AttackEntityEffect, self).__init__(source_entity=source_entity,
effect_type=EffectTypes.DAMAGE,
meld_id=meld_id,
time_to_live=time_to_live)
self.accuracy = accuracy
self.damage = damage
self.damage_types = damage_types
self.miss_message = miss_message
self.hit_message = hit_message
self.attack_effects = attack_effects
self.hit_trigger_effect = hit_trigger_effect
self.crit_chance = crit_chance
self.crit_multiplier = crit_multiplier
self.crit_message = crit_message
def send_miss_message(self):
messenger.msg.send_visual_message(self.miss_message %
{"source_entity": self.source_entity.description.long_name,
"target_entity": self.target_entity.description.long_name},
self.target_entity.position.value)
def send_hit_message(self, message_template, damage_caused):
source_entity_name = self.source_entity.description.long_name if self.source_entity else None
target_entity_name = self.target_entity.description.long_name if self.target_entity else None
m = message_template % {"source_entity": source_entity_name,
"target_entity": target_entity_name,
"damage": str(int(damage_caused))}
messenger.msg.send_visual_message(m, self.target_entity.position.value)
def is_a_hit(self):
return self.target_entity.dodger.is_a_hit(self.accuracy) or self.target_entity.has("sleeping")
def is_a_crit(self):
return self.crit_chance > random.random() or self.target_entity.has("sleeping")
def hit_target(self):
is_crit = self.is_a_crit()
damage = self.damage
if is_crit:
damage = self.damage * self.crit_multiplier
animate_point(self.target_entity.game_state.value, self.target_entity.position.value,
[GraphicChar(None, colors.RED, "X")])
damage_after_armor = self.target_entity.armor_checker.get_damage_after_armor(damage, self.damage_types)
damage_after_resist = self.target_entity.resistance_checker.get_damage_after_resistance(damage_after_armor,
self.damage_types)
damage_caused = self.target_entity.health_modifier.hurt(damage_after_resist, entity=self.source_entity)
self.execute_effects()
if is_crit:
self.send_hit_message(self.crit_message, damage_caused)
else:
self.send_hit_message(self.hit_message, damage_caused)
def update(self, time_spent):
if self.target_entity.resistance_checker.is_immune(self.damage_types):
pass
self.on_attacked_effects()
if self.is_a_hit():
self.hit_target()
else:
self.send_miss_message()
self.tick(time_spent)
def execute_effects(self):
for effect in self.attack_effects:
if effect.roll_to_hit():
effect.attack_effect(self.source_entity, self.target_entity)
def on_attacked_effects(self):
for t in self.target_entity.get_children_with_tag(trigger.ON_ATTACKED_TRIGGER_TAG):
t.trigger(target_entity=self.source_entity, source_entity=self.target_entity)
class UndodgeableAttackEntityEffect(AttackEntityEffect):
def __init__(self, source_entity, damage, damage_types, hit_message=messenger.HIT_MESSAGE,
meld_id=None, time_to_live=1):
super(UndodgeableAttackEntityEffect, self).__init__(source_entity, damage, damage_types, -1,
hit_message=hit_message,
meld_id=meld_id,
time_to_live=time_to_live)
def is_a_hit(self):
return True
class HealthRegain(EntityEffect):
def __init__(self, source_entity, health, turn_interval, time_to_live, meld_id=None):
super(HealthRegain, self).__init__(source_entity=source_entity, effect_type=EffectTypes.HEAL,
meld_id=meld_id, time_to_live=time_to_live)
self.health = health
self.time_interval = turn_interval * gametime.single_turn
self.time_until_next_heal = self.time_interval
def update(self, time_spent):
if self.time_until_next_heal <= 0:
self.target_entity.health_modifier.heal(self.health)
self.time_until_next_heal = self.time_interval
self.time_until_next_heal -= time_spent
self.tick(time_spent)
class StatusIconEntityEffect(EntityEffect):
def __init__(self, source_entity, status_description, time_to_live, meld_id=None):
super(StatusIconEntityEffect, self).__init__(source_entity=source_entity, effect_type=EffectTypes.UI,
meld_id=meld_id, time_to_live=time_to_live)
self.status_description = status_description
def update(self, time_spent):
if self.status_description and self.target_entity.has("status_bar"):
self.target_entity.status_bar.add(self.status_description)
self.tick(time_spent)
class DamageOverTimeEffect(EntityEffect):
def __init__(self, source_entity, damage, damage_types, turn_interval, turns_to_live,
damage_message, status_description=None, meld_id=None):
super(DamageOverTimeEffect, self).__init__(source_entity=source_entity, effect_type=EffectTypes.DAMAGE,
meld_id=meld_id, time_to_live=turns_to_live * gametime.single_turn)
self.damage = damage
self.damage_types = damage_types
self.time_interval = turn_interval * gametime.single_turn
self.damage_message = damage_message
self.time_until_next_damage = self.time_interval
self.status_description = status_description
def send_damage_message(self, damage_caused):
message_arguments = {}
if self.source_entity and self.source_entity.has("description"):
message_arguments["source_entity"] = self.source_entity.description.long_name
if self.target_entity and self.target_entity.has("description"):
message_arguments["target_entity"] = self.target_entity.description.long_name
message_arguments["damage"] = str(damage_caused)
m = self.damage_message % message_arguments
messenger.msg.send_visual_message(m, self.target_entity.position.value)
def damage_target(self):
damage_after_armor = self.target_entity.armor_checker.get_damage_after_armor(self.damage, self.damage_types)
damage_after_resist = self.target_entity.resistance_checker.get_damage_after_resistance(damage_after_armor,
self.damage_types)
damage_caused = self.target_entity.health_modifier.hurt(damage_after_resist, entity=self.source_entity,
damage_types=self.damage_types)
return damage_caused
def update_status_icon(self):
if self.status_description and self.target_entity.has("status_bar"):
self.target_entity.status_bar.add(self.status_description)
def update(self, time_spent):
if not self.target_entity.resistance_checker.is_immune(self.damage_types):
if self.time_until_next_damage <= 0:
damage_caused = self.damage_target()
self.send_damage_message(damage_caused)
self.time_until_next_damage = self.time_interval
self.update_status_icon()
self.time_until_next_damage -= time_spent
self.tick(time_spent)
class BleedEffect(DamageOverTimeEffect):
MAX_DAMAGE_PER_TURN = 3
def __init__(self, source_entity, damage, damage_types, turn_interval, turns_to_live,
damage_message, status_description):
super(BleedEffect, self).__init__(source_entity, damage, damage_types, turn_interval, turns_to_live,
damage_message, status_description=status_description, meld_id="bleed_effect")
def meld(self, other_effect):
self.time_to_live = max(other_effect.time_to_live, self.time_to_live)
self.damage = min(self.damage + 1, BleedEffect.MAX_DAMAGE_PER_TURN)
self.status_description.graphic_char.icon = str(self.damage)
class UndodgeableDamagAndBlockSameEffect(EntityEffect):
def __init__(self, source_entity, damage, damage_types,
damage_message, meld_id, status_description=None, time_to_live=1):
super(UndodgeableDamagAndBlockSameEffect, self).__init__(source_entity=source_entity,
effect_type=EffectTypes.DAMAGE,
time_to_live=time_to_live,
meld_id=meld_id)
self.damage = damage
self.damage_types = damage_types
self.damage_message = damage_message
self.status_description = status_description
def send_damage_message(self, damage_caused):
messenger.msg.send_visual_message(
self.damage_message % {"source_entity": self.source_entity.description.long_name,
"target_entity": self.target_entity.description.long_name,
"damage": str(damage_caused)},
self.target_entity.position.value)
def update(self, time_spent):
if not self.target_entity.resistance_checker.is_immune(self.damage_types):
if self.time_alive == 0:
damage_after_armor = self.target_entity.armor_checker.get_damage_after_armor(self.damage,
self.damage_types)
damage_after_resist = self.target_entity.resistance_checker.get_damage_after_resistance(
damage_after_armor, self.damage_types)
damage_caused = self.target_entity.health_modifier.hurt(damage_after_resist, entity=self.source_entity)
self.send_damage_message(damage_caused)
self.update_status_icon()
self.tick(time_spent)
def update_status_icon(self):
if self.status_description and self.target_entity.has("status_bar"):
self.target_entity.status_bar.add(self.status_description)
class Heal(EntityEffect):
def __init__(self, source_entity, health, heal_message=messenger.HEAL_MESSAGE, time_to_live=1):
super(Heal, self).__init__(source_entity=source_entity,
effect_type=EffectTypes.HEAL,
time_to_live=time_to_live)
self.health = health
self.heal_message = heal_message
def message(self):
messenger.msg.send_visual_message(
self.heal_message % {"source_entity": self.source_entity.description.long_name,
"target_entity": self.target_entity.description.long_name,
"health": str(self.health)},
self.target_entity.position.value)
def update(self, time_spent):
self.target_entity.health_modifier.heal(self.health)
self.message()
self.tick(time_spent)
class AddSpoofChild(EntityEffect):
def __init__(self, source_entity, spoof_child, time_to_live, message_effect=None, meld_id=None, effect_id=None,
status_description=None):
super(AddSpoofChild, self).__init__(source_entity=source_entity,
effect_type=EffectTypes.ADD_SPOOF_CHILD,
time_to_live=time_to_live,
meld_id=meld_id,
effect_id=effect_id)
self.status_description = status_description
self.message_effect = message_effect
self.spoof_child = spoof_child
def update(self, time_spent):
self.target_entity.add_spoof_child(self.spoof_child)
if self.message_effect:
self.message()
self.update_status_icon()
self.tick(time_spent)
def update_status_icon(self):
if self.status_description and self.target_entity.has("status_bar"):
self.target_entity.status_bar.add(self.status_description)
def message(self):
messenger.msg.send_visual_message(
self.message_effect % {"source_entity": self.source_entity.description.long_name,
"target_entity": self.target_entity.description.long_name},
self.target_entity.position.value)
class RemoveChildEffect(EntityEffect):
def __init__(self, source_entity, component_type, time_to_live, meld_id=None):
super(RemoveChildEffect, self).__init__(source_entity=source_entity,
effect_type=EffectTypes.REMOVE_CHILD,
time_to_live=time_to_live,
meld_id=meld_id)
self.component_type = component_type
def update(self, time_spent):
self.target_entity.remove_component_of_type(self.component_type)
self.tick(time_spent)
class Equip(EntityEffect):
def __init__(self, source_entity, item, equip_message=messenger.EQUIP_MESSAGE):
super(Equip, self).__init__(source_entity=source_entity,
effect_type=EffectTypes.EQUIPMENT,
time_to_live=1)
self.item = item
self.equip_message = equip_message
def message(self):
messenger.msg.send_visual_message(
self.equip_message % {"source_entity": self.source_entity.description.long_name,
"target_entity": self.target_entity.description.long_name,
"item": self.item.description.long_name},
self.target_entity.position.value)
def update(self, time_spent):
equipment = self.queue.target_entity.equipment
equip_succeeded = equipment.try_equip(self.item)
if equip_succeeded:
self.message()
if self.queue.target_entity.inventory.has_item(self.item):
self.queue.target_entity.inventory.remove_item(self.item)
self.tick(time_spent)
class StepEffect(EntityEffect):
def __init__(self, source_entity, item):
super(StepEffect, self).__init__(source_entity=source_entity,
effect_type=EffectTypes.EQUIPMENT,
time_to_live=1)
self.item = item
def update(self, time_spent):
pass
class Unequip(EntityEffect):
def __init__(self, source_entity, equipment_slot):
super(Unequip, self).__init__(source_entity=source_entity,
effect_type=EffectTypes.EQUIPMENT,
time_to_live=1)
self.item = source_entity.equipment.get(equipment_slot)
self.equipment_slot = equipment_slot
self.unequip_message = messenger.UNEQUIP_MESSAGE
def message(self):
messenger.msg.send_visual_message(
self.unequip_message % {"source_entity": self.source_entity.description.long_name,
"target_entity": self.target_entity.description.long_name,
"item": self.item.description.long_name},
self.target_entity.position.value)
def update(self, time_spent):
equipment = self.target_entity.equipment
if equipment.can_unequip_to_inventory(self.equipment_slot):
underlip_succeeded = equipment.unequip_to_inventory(self.equipment_slot)
if underlip_succeeded:
self.message()
self.tick(time_spent)
class ReEquip(EntityEffect):
def __init__(self, source_entity, equipment_slot, item):
super(ReEquip, self).__init__(source_entity=source_entity,
effect_type=EffectTypes.EQUIPMENT,
time_to_live=1)
self.equipment_slot = equipment_slot
self.item = item
def message(self):
message = "%s equips %s." % (self.source_entity.description.long_name,
self.item.description.long_name)
messenger.msg.send_visual_message(message,
self.target_entity.position.value)
def update(self, time_spent):
old_item = None
if self.source_entity.equipment.slot_is_equiped(self.equipment_slot):
old_item = self.source_entity.equipment.unequip(self.equipment_slot)
equipment = self.target_entity.equipment
equip_succeeded = equipment.try_equip(self.item)
if equip_succeeded:
self.message()
self._item_flash_animation(self.target_entity, self.item)
if self.source_entity.inventory.has_item(self.item):
self.source_entity.inventory.remove_item(self.item)
if not old_item is None:
self.source_entity.inventory.try_add(old_item)
self.tick(time_spent)
def _item_flash_animation(self, entity, item):
entity.char_printer.append_graphic_char_temporary_frames([item.graphic_char])