-
Notifications
You must be signed in to change notification settings - Fork 0
/
area.py
213 lines (188 loc) · 7.38 KB
/
area.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
import yaml
import pygame
from random import randint, choice
from numpy import array, empty
from constants import *
from character import Character
from location import Location
import a_star
class Area:
def __init__(self, world, area, gate=1):
self.tiles = {}
self.area = area
self.map = None
self.gates = {}
self.transition = None
self.characters = []
# hash to cache paths
self.paths = {}
self.load(world, area)
def recalculate_free_locations(self):
self.free_locations = {}
for i in range(self.w):
for j in range(self.h):
loc = Location(i, j)
if self.can_pass(None, loc):
self.free_locations[loc] = True
return True
def place_hero_in_gate(self, hero, gate):
self.place_character(hero, self.gates[gate]['tiles'][0], is_hero=True)
def remove_hero(self):
hero = self.characters.pop(0)
loc = hero.location()
self.map[loc.x][loc.y]['character'] = None
self.free_locations[l] = True
def is_character_hero(self, character):
return character == self.characters[0]
def place_character(self, character, loc, is_hero=False):
"""Places a character in the map for the first time"""
#FIXME: check for already existing characters
character.set_location(loc)
if is_hero:
self.characters.insert(0, character)
else:
self.characters.append(character)
self.map[loc.x][loc.y]['character'] = character
del self.free_locations[loc]
def move_character(self, character, loc, moves_left):
"""Moves the character in the map"""
#FIXME: check for already existing characters
del self.map[character.loc.x][character.loc.y]['character']
self.free_locations[character.loc] = True
character.set_location(loc)
self.map[loc.x][loc.y]['character'] = character
del self.free_locations[character.loc]
self.invalidate_paths()
if self.is_character_hero(character):
gate = self.has_gate(loc)
if gate and not moves_left:
self.transition = self.gates[gate]
def invalidate_paths(self):
self.paths = {}
def load(self, world, area):
f = open('data/worlds/%s/%s' % (world, area))
data = yaml.load(f)
f.close()
for i in data['import']:
self.load_import(world, i)
self.load_map(world, data['map'])
# FIXME: probably this will be removed when we take into account units
# that can be in water or blocking areas
self.recalculate_free_locations()
for g in data['gates']:
gate = int(g['id'])
dest = g['dest']
self.gates[gate]['next'] = dest
for u in data['units']:
if u['type'] == 'unique':
pass
elif u['type'] == 'fill':
# FIXME: should probably be non-blocking grids instead of just
# w*h
units = (self.w * self.h) / u['density']
unit_types = u['unit_types']
sum_prob = sum([ut['prob'] for ut in unit_types])
for i in range(units):
rnd = randint(0, sum_prob - 1)
s = 0
for ut in unit_types:
s = s + ut['prob']
if rnd < s:
c = Character.load_from_data(ut['data'])
self.place_character(c, self.random_free_location(c))
break
def load_import(self, world, name):
f = open('data/worlds/%s/%s' % (world, name))
data = yaml.load(f)
f.close()
for t in data['tiles']:
prob_sum = 0
for i in t['flat']:
i['image'] = pygame.image.load('images/%s.png' % i['file'])
prob_sum = prob_sum + i['prob']
t['flat_prob_sum'] = prob_sum
prob_sum = 0
if t.has_key('volume'):
for i in t['volume']:
i['image'] = pygame.image.load('images/%s.png' % i['file'])
prob_sum = prob_sum + i['prob']
t['volume_prob_sum'] = prob_sum
self.tiles[t['char']] = t
def select_image(self, image_list, prob_max):
rnd = randint(0, prob_max - 1)
s = 0
for img in image_list:
s = s + img['prob']
if rnd < s:
return img['image']
def load_map(self, world, name):
f = open('data/worlds/%s/%s' % (world, name))
lines = [l.rstrip("\n") for l in f.readlines()]
f.close()
y = len(lines)
x = len(lines[0])/2
self.map = empty((x,y), dtype=object)
for j in range(y):
for i in range(x):
c = lines[j][i*2]
extra = lines[j][i*2+1]
t = self.tiles[c]
self.map[i][j] = {
'char': c,
'tile': t
}
if extra != ' ':
if extra.isdigit():
gate = int(extra)
self.map[i][j]['gate'] = gate
if not self.gates.has_key(gate):
self.gates[gate] = {}
if not self.gates[gate].has_key('tiles'):
self.gates[gate]['tiles'] = []
self.gates[gate]['tiles'].append(Location(i,j))
self.map[i][j]['flat_image'] = self.select_image(t['flat'],
t['flat_prob_sum'])
if t.has_key('volume'):
self.map[i][j]['volume_image'] = \
self.select_image(t['volume'], t['volume_prob_sum'])
self.w, self.h = self.map.shape
def random_free_location(self, unit):
loc = choice(self.free_locations.keys())
return loc
def can_move(self, unit, loc):
#FIXME: probably we want to remove this trivial check from here
# by ensuring this method is always called on a proper loc
if loc.x < 0 or loc.x >= self.w:
return False
if loc.y < 0 or loc.y >= self.h:
return False
if self.map[loc.x][loc.y]['tile']['type'] != 'normal':
return False
if self.map[loc.x][loc.y].has_key('character'):
return False
path = self.find_path(unit, loc)
if not path:
return False
return path
def can_pass(self, unit, loc):
# FIXME: for now we don't use unit, go over uses of can_pass if
# we actually start requiring it
if self.map[loc.x][loc.y]['tile']['type'] != 'normal':
return False
if self.map[loc.x][loc.y].has_key('character'):
return False
return True
def has_gate(self, loc):
if self.map[loc.x][loc.y].has_key('gate'):
return self.map[loc.x][loc.y]['gate']
else:
return False
def find_path(self, unit, loc):
#FIXME: remember to invalidate paths
path_id = (unit, loc)
if not self.paths.has_key(path_id):
self.paths[path_id] = a_star.find_path(self, unit, loc)
return self.paths[path_id]
def update(self, proportion):
for c in self.characters:
c.update(self, proportion)