/
spawner.py
99 lines (81 loc) · 3.61 KB
/
spawner.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
import logging
import random
from compositecore import Composite
from mover import Mover
from stats import GamePieceTypes
import dungeontrash
import item
import monster
import rng
from statusflags import StatusFlags
from text import Description
def place_piece_on_random_walkable_tile(piece, dungeon_level):
walker = piece
if not piece.has("status_flags"):
walker = dummy_player
positions = dungeon_level.get_random_walkable_positions_in_dungeon(walker)
random.shuffle(positions)
for position in positions:
tile = dungeon_level.get_tile(position)
if not tile.get_dungeon_feature() and piece.mover.try_move(position, dungeon_level):
return True
return False
def spawn_rat_man(dungeon_level, game_state):
rat = monster.new_ratman(game_state)
spawn_succeeded = place_piece_on_random_walkable_tile(rat, dungeon_level)
if not spawn_succeeded:
logging.info("could not spawn rat-man")
return False
return True
def spawn_corpse_turn_into_entity(entity_killed, entity_factory):
return spawn_corpse_turn_into_entity_on_position(entity_killed.position.value,
entity_killed.dungeon_level.last_dungeon_level,
entity_killed.game_state.value,
entity_factory)
def spawn_corpse_turn_into_entity_on_position(position, dungeon_level, game_state, entity_factory):
corpse = dungeontrash.CorpseTurnIntoEntity(game_state, entity_factory)
spawn_succeeded = corpse.mover.replace_move(position, dungeon_level)
if not spawn_succeeded:
logging.info("could not spawn corpse.")
return False
return True
def spawn_corpse_of_entity(entity_killed):
return spawn_corpse_on_position(entity_killed.position.value,
entity_killed.dungeon_level.last_dungeon_level,
entity_killed.game_state.value)
def spawn_corpse_on_position(position, dungeon_level, game_state):
corpse = dungeontrash.Corpse(game_state)
spawn_succeeded = corpse.mover.replace_move(position, dungeon_level)
if not spawn_succeeded:
logging.info("could not spawn corpse.")
return False
return True
def place_health_potions(dungeon_level, game_state):
health_potions_to_spawn = 0
for _ in range(2):
if rng.coin_flip():
health_potions_to_spawn += 1
if dungeon_level.depth == 0:
health_potions_to_spawn += 2
health_potions_to_spawn += 1
for _ in range(health_potions_to_spawn):
potion = item.new_health_potion(game_state)
place_piece_on_random_walkable_tile_not_on_item_or_feature(potion, dungeon_level)
#print "HP pots spawned: ", health_potions_to_spawn
def place_piece_on_random_walkable_tile_not_on_item_or_feature(piece, dungeon_level):
walker = piece
if not piece.has("status_flags"):
walker = dummy_player
for position in dungeon_level.get_random_walkable_positions_in_dungeon(walker):
tile = dungeon_level.get_tile(position)
if (tile.has_piece_of_type(GamePieceTypes.ITEM) or
tile.has_piece_of_type(GamePieceTypes.DUNGEON_FEATURE)):
continue
move_succeeded = piece.mover.try_move(position, dungeon_level)
if move_succeeded:
return True
return False
dummy_player = Composite()
dummy_player.set_child(StatusFlags([StatusFlags.CAN_OPEN_DOORS]))
dummy_player.set_child(Mover())
dummy_player.set_child(Description("player_dummy", "Just a dummy used for instead of player for calculations."))