Repository navigation
Expand file tree
/
Copy pathmap.py
More file actions
174 lines (155 loc) · 6.22 KB
/
Copy pathmap.py
File metadata and controls
174 lines (155 loc) · 6.22 KB
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
import libtcodpy as libtcod
from rect import Rect
from object import Object
from components import Fighter, BasicMonster
from message import message
from constants import *
from envparse import env
import tiles
import random
color_dark_wall = libtcod.Color(0, 0, 100)
color_dark_ground = libtcod.Color(50, 50, 150)
color_dark_wall = libtcod.Color(0, 0, 100)
color_medium_wall = libtcod.Color(65, 55, 75)
color_light_wall = libtcod.Color(130, 110, 50)
color_dark_ground = libtcod.Color(50, 50, 150)
color_medium_ground = libtcod.Color(125, 115, 100)
color_light_ground = libtcod.Color(200, 180, 50)
DEBUG = env.bool('DEBUG', default=False)
class Tile:
# a tile of the map and its properties
def __init__(self, blocked, block_sight=None):
self.blocked = blocked
# by default, if a tile is blocked, it also blocks sight
if block_sight is None:
block_sight = blocked
self.block_sight = block_sight
self.explored = False
self.tunnel = False
self.xp_gain = 0
class Map:
def __init__(self, w, h, floor):
self.w = w
self.h = h
self.rooms = []
self.num_rooms = 0
self.floor = floor
self.fov_map = libtcod.map_new(self.w, self.h)
self.torch_left = 10000
self._objects = []
self.tiles = [[Tile(True)
for _ in range(self.h)]
for _ in range(self.w)]
def create_room(self, room):
# go through the tiles in the rectangle and make them passable
for x in range(room.x1 + 1, room.x2):
for y in range(room.y1 + 1, room.y2):
self.tiles[x][y].blocked = False
self.tiles[x][y].block_sight = False
self.tiles[room.x1 + 1][room.y1 + 1].blocked = True
self.tiles[room.x1 + 1][room.y1 + 1].block_sight = True
self.tiles[room.x1 + 1][room.y2 - 1].blocked = True
self.tiles[room.x1 + 1][room.y2 - 1].block_sight = True
self.tiles[room.x2 - 1][room.y1 + 1].blocked = True
self.tiles[room.x2 - 1][room.y1 + 1].block_sight = True
self.tiles[room.x2 - 1][room.y2 - 1].blocked = True
self.tiles[room.x2 - 1][room.y2 - 1].block_sight = True
def create_tunnel(self, x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
coords = []
if abs(dx) > abs(dy):
steps = abs(dx)
else:
steps = abs(dy)
for n in range(steps):
x = float(dx) / float(steps)
y = float(dy) / float(steps)
coords.append([x, y])
elected = coords[:]
random.shuffle(elected)
for index, coord in enumerate(elected):
if index % 2 == 0:
coord[0] *= 0
coord[1] *= 2
else:
coord[0] *= 2
coord[1] *= 0
current = [x1, y1]
for (x, y) in coords:
current[0] += x
current[1] += y
self.carve(*current)
def carve(self, x, y):
self.carve_tile(x, y)
self.carve_tile(x, y+1)
self.carve_tile(x+1, y)
self.carve_tile(x+1, y+1)
def carve_tile(self, x, y):
try:
x = int(x)
y = int(y)
self.tiles[x][y].blocked = False
self.tiles[x][y].block_sight = False
self.tiles[x][y].tunnel = True
except IndexError:
return False
def set_fov(self):
for y in range(self.h):
for x in range(self.w):
libtcod.map_set_properties(
self.fov_map, x, y, not self.tiles[x][y].block_sight, not self.tiles[x][y].blocked)
def fov_recompute(self, player):
libtcod.map_compute_fov(
self.fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO)
def tile_at(self, x, y):
return self.tiles[x][y]
def add_object(self, object):
object.map = self
self._objects.append(object)
def remove_object(self, object):
object.map = None
self._objects.remove(object)
def send_to_back(self, object):
self.remove_object(object)
self._objects.insert(0, object)
def draw(self, con, player):
min_x = max(0, player.x - SCREEN_WIDTH / 2 - 2)
max_x = min(player.x + SCREEN_WIDTH / 2 - 2, self.w)
min_y = max(0, player.y - PANEL_Y / 2 - 2)
max_y = min(player.y + PANEL_Y / 2 - 2, self.h)
for y in range(min_y, max_y):
for x in range(min_x, max_x):
visible = libtcod.map_is_in_fov(self.fov_map, x, y)
wall = self.tiles[x][y].block_sight
if not visible and not DEBUG:
# if it's not visible right now, the player can only see it if it's explored
if self.tiles[x][y].explored:
# it's out of the player's FOV
if wall:
libtcod.console_put_char_ex(
con, x, y, tiles.wall_tile, libtcod.darker_gray, libtcod.black)
else:
libtcod.console_put_char_ex(
con, x, y, tiles.floor_tile, libtcod.darker_gray, libtcod.black)
else:
# it's visible
distance = int(player.distance_to(x, y) * 16)
tint = libtcod.Color(255-distance, 249-distance, 249-distance)
if wall:
libtcod.console_put_char_ex(
con, x, y, tiles.wall_tile, tint, libtcod.black)
else:
libtcod.console_put_char_ex(
con, x, y, tiles.floor_tile, tint, libtcod.black)
# since it's visible, explore it
tile = self.tiles[x][y]
if not tile.explored:
player.fighter.grant_xp(tile.xp_gain)
tile.explored = True
for object in self._objects:
if object != player:
if DEBUG or libtcod.map_is_in_fov(self.fov_map, object.x, object.y):
distance = int(player.distance_to(object) * 16)
object.draw(con, distance)
player.draw(con, 0)