forked from fiddlerwoaroof/yinjar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
levels.py
145 lines (118 loc) · 3.7 KB
/
levels.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
from algorithms import djikstra
import libtcodpy as libtcod
import maps
import debug
class Level(object):
levels = {}
color_dark_wall = libtcod.Color(60, 60, 60)
color_light_wall = libtcod.Color(127,127,127)
color_dark_ground = libtcod.Color(150,150,150)
color_light_ground = libtcod.Color(200,200,200)
def register(self, num):
self.levels[num] = self
self.number = num
return self
def get_djikstra(self, x,y):
if (x,y) not in self.djikstra_cache:
print 'new (%s, %s)' % (x,y)
dj = self.djikstra_cache[x,y] = djikstra.DjikstraMap()
dj.set_goals( (x,y), weight=0)
dj.load_map(self.map.map.data)
dj = self.djikstra_cache[x,y]
dj.iter(5)
return dj
def __init__(self, width, height, con, item_types=None, monster_types=None):
self.clear_cells = set()
self.djikstra_cache = {}
self.objects = []
self.map = maps.Map(width, height, con, self)
self.fov_map = libtcod.map_new(self.map.width, self.map.height)
self.con = con
self.player = None
if item_types is None: item_types = {}
self.item_types = item_types
if monster_types is None: item_types = {}
self.monster_types = monster_types
def setup(self, max_rooms, min_size, max_size, max_num_monsters, max_num_items):
self.map.populate_map(
max_rooms, min_size, max_size,
self.monster_types, max_num_monsters,
self.item_types, max_num_items,
)
def iter_objects(self):
return iter(self.objects)
def add_object(self, obj):
self.objects.append(
obj.enter_level(self)
)
return obj
def claim_object(self, obj):
self.objects.remove(obj)
return obj
def enter(self, player):
#self.map.enter(player)
if self.player is not None and self.player.level is not None:
self.player.level.leave(self.player)
self.player = player
self.player.pos = self.map.map_entrance
self.objects.append(player)
return self
def leave(self, player):
#self.map.leave(player)
self.objects.remove(player)
self.player = None
def send_to_back(self, obj):
self.objects.remove(obj)
self.objects.insert(0,obj)
fov_algo = libtcod.FOV_DIAMOND
fov_light_walls = True
def recompute_fov(self, clear_all=False):
x,y = self.map.map_entrance
if self.player is not None:
x,y = self.player.pos
libtcod.map_compute_fov(
self.fov_map, x,y,
player.Player.torch_radius, self.fov_light_walls, self.fov_algo
)
for x,y, cell in self.map.iter_cells_with_coords():
visible = libtcod.map_is_in_fov(self.fov_map, x,y)
if visible and not cell.explored:
cell.explored = True
color = libtcod.black
#if True:
if cell.explored:
wall = cell.block_sight
walkable = not cell.blocked
if wall or walkable:
color = {
True: {True: self.color_light_wall, False: self.color_light_ground},
False: {True: self.color_dark_wall, False: self.color_dark_ground}
}[visible][wall]
elif not walkable:
color = libtcod.Color(100,100,200)
if cell.explored or clear_all:
libtcod.console_set_char_background(self.con, x, y, color, libtcod.BKGND_SET)
def init_fov(self):
libtcod.map_clear(self.fov_map)
#self.fov_map = libtcod.map_new(self.map.width, self.map.height)
for x,y,cell in self.map.iter_cells_with_coords():
libtcod.map_set_properties(self.fov_map, x,y,
not cell.block_sight,
not cell.blocked
)
def is_visible(self, x,y):
if x < 0 or y < 0:
raise ValueError(' (%s,%s) not in map ' % (x,y))
elif x >= self.map.width or y >= self.map.height:
raise ValueError(' (%s,%s) not in map ' % (x,y))
return libtcod.map_is_in_fov(self.fov_map, x,y)
def is_blocked(self, x,y):
if x < 0 or x > self.map.width:
result = True
elif y < 0 or y > self.map.height:
result = True
else:
result = self.map.is_blocked(x,y)
return result
import game
import player