/
cloud.py
316 lines (254 loc) · 11.9 KB
/
cloud.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
import random
from Status import FIRE_STATUS_DESCRIPTION, FROST_SLOW_STATUS_DESCRIPTION
from actor import Actor
from attacker import DamageTypes
import colors
from compositecommon import EntityShareTileEffect, PoisonEntityEffectFactory, frost_effect_factory
from compositecore import Composite, Leaf
import direction
from entityeffect import UndodgeableDamagAndBlockSameEffect, AddSpoofChild
import gametime
import geometry
from graphic import CharPrinter, GraphicChar
import icon
import messenger
from mover import Mover
from position import Position, DungeonLevel
import rng
from stats import DataTypes, DataPoint, GamePieceTypes, DataPointBonusSpoof
from statusflags import StatusFlags, Flags
from text import Description
import turn
class CloudTypes:
EXPLOSION = "explosion"
STEAM = "steam"
FIRE = "fire"
FROST = "frost"
DUST = "dust"
POISON = "poison"
def set_cloud_components(game_state, cloud, density):
cloud.set_child(DataPoint(DataTypes.ENERGY, -gametime.single_turn))
cloud.set_child(DataPoint(DataTypes.GAME_PIECE_TYPE, GamePieceTypes.CLOUD))
cloud.set_child(DataPoint(DataTypes.GAME_STATE, game_state))
cloud.set_child(CharPrinter())
cloud.set_child(Position())
cloud.set_child(DungeonLevel())
cloud.set_child(Mover())
cloud.set_child(GraphicChar(None, None, 178))
cloud.set_child(DataPoint(DataTypes.DENSITY, density))
cloud.set_child(DataPoint(DataTypes.MOVEMENT_SPEED, gametime.single_turn))
cloud.set_child(StatusFlags([StatusFlags.FLYING]))
cloud.set_child(CloudChangeAppearanceShareTileEffect())
def new_steam_cloud(game_state, density):
cloud = Composite()
set_cloud_components(game_state, cloud, density)
cloud.graphic_char.color_fg = colors.WHITE
cloud.set_child(CloudActor())
cloud.set_child(DataPoint(DataTypes.CLONE_FUNCTION, new_steam_cloud))
cloud.set_child(DataPoint(DataTypes.CLOUD_TYPE, CloudTypes.STEAM))
return cloud
def new_poison_cloud(game_state, density):
cloud = Composite()
set_cloud_components(game_state, cloud, density)
cloud.graphic_char.color_fg = colors.GREEN
cloud.set_child(CloudActor())
cloud.set_child(DataPoint(DataTypes.CLONE_FUNCTION, new_poison_cloud))
cloud.set_child(DataPoint(DataTypes.CLOUD_TYPE, CloudTypes.POISON))
cloud.set_child(PoisonCloudShareTileEffect())
return cloud
def new_frost_cloud(game_state, density):
cloud = Composite()
set_cloud_components(game_state, cloud, density)
cloud.graphic_char.color_fg = colors.LIGHT_BLUE
cloud.set_child(CloudActor())
cloud.set_child(DataPoint(DataTypes.CLONE_FUNCTION, new_frost_cloud))
cloud.set_child(DataPoint(DataTypes.CLOUD_TYPE, CloudTypes.FROST))
cloud.set_child(FrostCloudShareTileEffect())
return cloud
def new_dust_cloud(game_state, density):
cloud = Composite()
set_cloud_components(game_state, cloud, density)
cloud.graphic_char.color_fg = colors.LIGHT_ORANGE
cloud.set_child(CloudActor())
cloud.set_child(DataPoint(DataTypes.CLONE_FUNCTION, new_dust_cloud))
cloud.set_child(DustLowerHitOfEntityShareTileEffect())
cloud.set_child(DataPoint(DataTypes.CLOUD_TYPE, CloudTypes.DUST))
return cloud
def new_explosion_cloud(game_state, density):
explosion = Composite()
set_cloud_components(game_state, explosion, density)
explosion.graphic_char.color_fg = colors.YELLOW
explosion.set_child(Description("Explosion", "Don't go near it."))
explosion.set_child(DisappearCloudActor())
explosion.set_child(ExplosionDamageShareTileEffect())
explosion.set_child(DataPoint(DataTypes.CLONE_FUNCTION, new_explosion_cloud))
explosion.set_child(DataPoint(DataTypes.CLOUD_TYPE, CloudTypes.EXPLOSION))
return explosion
def set_non_flying_cloud_components(cloud):
cloud.set_child(StatusFlags([]))
def new_fire_cloud(game_state, density):
fire = Composite()
set_cloud_components(game_state, fire, density)
set_non_flying_cloud_components(fire)
fire.graphic_char.icon = icon.FIRE
fire.graphic_char.color_fg = colors.RED
fire.set_child(Description("Fire", "Don't get burnt."))
fire.set_child(SpreadToFlammableDisappearActor())
fire.set_child(FireDamageShareTileEffect())
fire.set_child(DataPoint(DataTypes.CLONE_FUNCTION, new_fire_cloud))
fire.set_child(DataPoint(DataTypes.CLOUD_TYPE, CloudTypes.FIRE))
return fire
class FireDamageShareTileEffect(EntityShareTileEffect):
def __init__(self):
super(FireDamageShareTileEffect, self).__init__()
self.component_type = "fire_damage_share_tile_effect"
self.damage_types = [DamageTypes.FIRE]
def effect(self, **kwargs):
target_entity = kwargs["target_entity"]
source_entity = kwargs["source_entity"]
damage_mid = 5
damage_var = 3
damage = rng.random_variance(damage_mid, damage_var)
if not target_entity.has("effect_queue"):
return
damage_effect = UndodgeableDamagAndBlockSameEffect(source_entity, damage, self.damage_types,
messenger.HURT_BY_FIRE, "fire_damage",
time_to_live=gametime.single_turn,
status_description=FIRE_STATUS_DESCRIPTION)
target_entity.effect_queue.add(damage_effect)
class AddEntityEffectShareTile(EntityShareTileEffect):
"""
Abstract subclasses should define entity effect.
"""
def __init__(self):
super(AddEntityEffectShareTile, self).__init__()
def effect(self, **kwargs):
target_entity = kwargs["target_entity"]
if not target_entity.has("effect_queue"):
return
target_entity.effect_queue.add(self.entity_effect)
class AddSpoofChildShareEntityEffect(EntityShareTileEffect):
def __init__(self):
super(AddSpoofChildShareEntityEffect, self).__init__()
def effect(self, **kwargs):
target_entity = kwargs["target_entity"]
if not target_entity.has("effect_queue"):
return
target_entity.effect_queue.add(AddSpoofChild(self.parent, self.spoof_child_factory(), 1))
def spoof_child_factory(self):
pass
class CloudChangeAppearanceShareTileEffect(AddSpoofChildShareEntityEffect):
def __init__(self):
super(CloudChangeAppearanceShareTileEffect, self).__init__()
self.component_type = "cloud_change_appearance_share_tile_effect"
def spoof_child_factory(self):
return GraphicChar(self.parent.graphic_char.color_fg, colors.GRAY_D, None)
class DustLowerHitOfEntityShareTileEffect(AddSpoofChildShareEntityEffect):
def __init__(self):
super(DustLowerHitOfEntityShareTileEffect, self).__init__()
self.component_type = "dust_lower_hit_of_entity_share_tile_effect"
def spoof_child_factory(self):
return DataPointBonusSpoof("hit", -10)
class PoisonCloudShareTileEffect(AddEntityEffectShareTile):
def __init__(self):
super(PoisonCloudShareTileEffect, self).__init__()
self.component_type = "poison_share_tile_effect"
self.poison_effect_factory = PoisonEntityEffectFactory(None, random.randrange(8, 14), 2, random.randrange(10, 20))
@property
def entity_effect(self):
return self.poison_effect_factory()
class FrostCloudShareTileEffect(AddEntityEffectShareTile):
def __init__(self):
super(FrostCloudShareTileEffect, self).__init__()
self.component_type = "frost_share_tile_effect"
@property
def entity_effect(self):
slow_turns = random.randrange(8, 20)
return AddSpoofChild(None, frost_effect_factory(), slow_turns * gametime.single_turn, meld_id="frost",
status_description=FROST_SLOW_STATUS_DESCRIPTION)
class ExplosionDamageShareTileEffect(EntityShareTileEffect):
def __init__(self):
super(ExplosionDamageShareTileEffect, self).__init__()
self.component_type = "explosion_damage_share_tile_effect"
def effect(self, **kwargs):
target_entity = kwargs["target_entity"]
source_entity = kwargs["source_entity"]
damage_mid = 20
damage_var = 10
damage = rng.random_variance(damage_mid, damage_var)
if not target_entity.has("effect_queue"):
return
damage_effect = UndodgeableDamagAndBlockSameEffect(source_entity, damage, [DamageTypes.PHYSICAL],
messenger.HURT_BY_EXPLOSION, "explosion_damage",
time_to_live=gametime.single_turn)
target_entity.effect_queue.add(damage_effect)
class DisappearCloudActor(Actor):
def __init__(self):
super(DisappearCloudActor, self).__init__()
def tick(self):
self.parent.energy.value += self.energy_recovery
while self.parent.energy.value > 0:
self.parent.energy.value -= self.act()
turn.current_turn += 1
def act(self):
self._before_act()
self.parent.density.value -= 1
if self.parent.density.value <= 0:
self.parent.mover.try_remove_from_dungeon()
return gametime.single_turn
def _before_act(self):
pass
class SpreadToFlammableDisappearActor(DisappearCloudActor):
def _before_act(self):
neighbours = [geometry.add_2d(offset, self.parent.position.value) for offset in direction.AXIS_DIRECTIONS]
random.shuffle(neighbours)
for neighbour in neighbours:
if self.point_has_flammable(neighbour) and self.try_spread_to_position(neighbour):
break
return gametime.single_turn
def point_has_flammable(self, position):
return any(True for df in self.parent.dungeon_level.value.get_tile_or_unknown(position).get_dungeon_features()
if df.has(Flags.FLAMMABLE))
def try_spread_to_position(self, position):
flame = self.parent.clone_function.value(self.parent.game_state.value, self.parent.density.value + 1)
if flame.mover.try_move(position, self.parent.dungeon_level.value):
flammables = [df for df in self.parent.dungeon_level.value.get_tile_or_unknown(position).get_dungeon_features()
if df.has(Flags.FLAMMABLE)]
for flammable in flammables:
flammable.mover.try_remove_from_dungeon()
return True
return False
class CloudActor(Actor):
def __init__(self):
super(CloudActor, self).__init__()
def _float_to_position(self, position, density):
original_cloud = self.parent.dungeon_level.value.get_tile_or_unknown(position).get_first_cloud()
if original_cloud is None:
new_cloud = self.parent.clone_function.value(self.parent.game_state.value, density)
new_cloud.mover.try_move(position, self.parent.dungeon_level.value)
self.parent.density.value -= density
elif original_cloud.cloud_type.value == self.parent.cloud_type.value:
original_cloud.density.value += density
self.parent.density.value -= density
def tick(self):
self.parent.energy.value += self.energy_recovery
while self.parent.energy.value > 0:
self.parent.energy.value -= self.act()
turn.current_turn += 1
def act(self):
self.spread()
return self.parent.movement_speed.value
def spread(self):
minimal_cloud_size = 2
if self.parent.density.value < minimal_cloud_size:
self.parent.mover.try_remove_from_dungeon()
return gametime.single_turn
density_per_tile = max(self.parent.density.value / 4, 1)
neighbours = [geometry.add_2d(offset, self.parent.position.value) for offset in direction.AXIS_DIRECTIONS]
random.shuffle(neighbours)
for neighbour in neighbours:
self._float_to_position(neighbour, density_per_tile)
if self.parent.density.value < density_per_tile:
break
if self.parent.density.value < minimal_cloud_size:
break