-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps.py
249 lines (218 loc) · 8.66 KB
/
maps.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
from globalconst import *
import libtcodpy as libtcod
import entity, sys
class Tile(object):
def __init__(self, terrain = "land"):
self.terrain = terrain
self.owned_by = None
self.char = None
self.fg_color = None
self.info = "No info available"
self.temp = 0
self.rain = 0
class Map(object):
def __init__(self, owner, width = MAP_WIDTH, height = MAP_HEIGHT, gen = True):
self.size = self.width, self.height = width, height
self.owner = owner
self.generated = False
self.tiles = [[ Tile()
for y in range(self.height) ]
for x in range(self.width) ]
self.image = libtcod.image_new(self.width * 2, self.height * 2)
if gen:
self.generate()
def generate(self):
self.owner.log.message("Generating map...", debug = True)
noise = libtcod.noise_new(2, 0.5, 2.0)
heightmap = libtcod.heightmap_new(2*self.width, 2*self.height)
maxi = 0
mini = 0
self.tiles = [[ Tile()
for y in range(self.height) ]
for x in range(self.width) ]
self.owner.log.message("-- creating heightmap...", debug = True)
for x in range(self.width*2):
for y in range(self.height*2):
f = [3 * float(x) / (2*self.width), 3 * float(y) / (2*self.height)]
value = (libtcod.noise_get_fbm(noise, f, 5, libtcod.NOISE_PERLIN))/2
if value > maxi:
maxi = value
if value < mini:
mini = value
libtcod.heightmap_set_value(heightmap, x, y, value)
# print "-- erode the map"
# libtcod.heightmap_rain_erosion(heightmap, self.width*2*self.height*2*2,0.1,0.2)
self.owner.log.message("-- normalize heights...", debug = True)
self.heightmap = libtcod.heightmap_new(self.width*2, self.height*2)
for x in range(self.width*2):
for y in range(self.height*2):
value = libtcod.heightmap_get_value(heightmap, x, y)
if value < 0:
value += 1
mini2 = mini + 1
coeff = (value - mini2)/(1-mini2)
libtcod.heightmap_set_value(self.heightmap, x, y, -coeff)
else:
value = value / maxi
libtcod.heightmap_set_value(self.heightmap, x, y, value)
self.owner.log.message("-- setting up tiles", debug = True)
for x in range(self.width):
for y in range(self.height):
h = libtcod.heightmap_get_value(self.heightmap, x*2, y*2)
if h >= 0.05:
self.tiles[x][y].terrain = "land"
else:
self.tiles[x][y].terrain = "water"
# self.owner.log.message("-- creating temperature map", debug = True)
# noise2 = libtcod.noise_new(2, 0.5, 2.0)
# temp_max = 0
# temp_min = 1
# for x in range(self.width):
# for y in range(self.height):
# f = [3 * float(x) / (self.width), 3 * float(y) / (self.height)]
# value = (libtcod.noise_get_fbm(noise2, f, 5, libtcod.NOISE_PERLIN))/2
# value = (value + 1)/2
# if value < temp_min:
# temp_min = value
# if value > temp_max:
# temp_max = value
# self.tiles[x][y].temp = value
# temp_max = temp_max - temp_min
# height_factor = 0.5
# for x in range(self.width):
# for y in range(self.height):
# temp = (self.tiles[x][y].temp - temp_min)/temp_max
# h = libtcod.heightmap_get_value(self.heightmap, x*2, y*2)
# if h > 0:
# factor = (-h)*height_factor
# temp = temp + factor
# temp = min(1, temp)
# temp = max(0, temp)
# self.tiles[x][y].temp = temp
self.owner.log.message("-- creating rainfall map", debug = True)
noise3 = libtcod.noise_new(2, 0.5, 2.0)
self.rainmap = libtcod.heightmap_new(self.width*2, self.height*2)
rain_max = 0
rain_min = 1
for x in range(self.width*2):
for y in range(self.height*2):
f = [5 * float(x) / (self.width*2), 5 * float(y) / (self.height*2)]
value = (libtcod.noise_get_fbm(noise3, f, 5, libtcod.NOISE_PERLIN))/2
value = (value + 1)/2
if value < rain_min:
rain_min = value
if value > rain_max:
rain_max = value
self.tiles[x/2][y/2].rain = value
libtcod.heightmap_set_value(self.rainmap, x, y, value)
rain_max = rain_max - rain_min
for x in range(self.width*2):
for y in range(self.height*2):
libtcod.heightmap_set_value(self.rainmap, x, y, (libtcod.heightmap_get_value(self.rainmap, x, y) - rain_min)/rain_max)
self.tiles[x/2][y/2].rain = (self.tiles[x/2][y/2].rain - rain_min)/rain_max
self.owner.log.message("Terrain complete", debug = True)
self.owner.log.message("Painting terrain", debug = True)
deep = libtcod.Color(1, 10, 27)
mid = libtcod.Color(38, 50, 60)
shallow = libtcod.Color(51, 83, 120)
water_idx = [0, 70, 210, 255]
water_cols = [deep, deep, mid, shallow]
water_colormap = libtcod.color_gen_map(water_cols, water_idx)
mountaintop = libtcod.Color(145, 196, 88)
grass = libtcod.Color(40, 62, 19)
foothill = libtcod.Color(57, 81, 34)
sand = libtcod.Color(215, 185, 115)
watersedge = libtcod.Color(19, 97, 101)
land_idx = [0, 15, 20, 128, 255]
land_cols = [watersedge, sand, grass, foothill, mountaintop]
land_colormap = libtcod.color_gen_map(land_cols, land_idx)
# Apply height-based colours
for x in range(self.width*2):
for y in range(self.height*2):
value = libtcod.heightmap_get_value(self.heightmap, x, y)
if value < 0:
index = int(-value * 255)
libtcod.image_put_pixel(self.image, x, y, water_colormap[index])
else:
index = int(value * 255)
libtcod.image_put_pixel(self.image, x, y, land_colormap[index])
# Adjust colours for desert-plains-forest
for x in range(self.width*2):
for y in range(self.height*2):
if libtcod.heightmap_get_value(self.heightmap, x, y) > 0:
rain = libtcod.heightmap_get_value(self.rainmap, x, y)
cur_col = libtcod.image_get_pixel(self.image, x, y)
cols = [libtcod.light_sepia, cur_col, cur_col, cur_col * 1.1]
col_idx = [0, 100, 165, 255]
col_map = libtcod.color_gen_map(cols, col_idx)
index = int(rain*255)
if index > 165 and libtcod.heightmap_get_value(self.heightmap, x, y) > 0.15:
self.tiles[x/2][y/2].biome = "forest"
self.tiles[x/2][y/2].char = 'T'
self.tiles[x/2][y/2].fg_color = grass * 0.7
libtcod.image_put_pixel(self.image, x, y, col_map[index])
self.owner.log.message("-- apply normal shadows", debug = True)
for x in range(self.width*2):
for y in range(self.height*2):
normal = libtcod.heightmap_get_normal(self.heightmap, x, y, 0)
nx = normal[0]
ny = normal[1]
avg = (nx + ny)/2
if avg > 0:
avg = 1
else:
avg = avg + 1
avg = min(avg/2 + 0.5, 1)
col = libtcod.image_get_pixel(self.image, x, y) * avg
libtcod.image_put_pixel(self.image, x, y, col)
self.owner.log.message("Placing cities", debug=True)
self.owner.entities = []
max_cities = 10
num_cities = 0
for i in range(max_cities):
x = libtcod.random_get_int(0, 0, self.width - 1)
y = libtcod.random_get_int(0, 0, self.height - 1)
if self.tiles[x][y].terrain == "land":
city = entity.City(self.owner, x, y, '#', libtcod.Color(libtcod.random_get_int(0, 0, 255), libtcod.random_get_int(0, 0, 255), libtcod.random_get_int(0, 0, 255)))
self.owner.entities.append(city)
num_cities += 1
self.owner.log.message("-- placed " + str(num_cities) + " cities")
self.owner.log.message("Map generated", debug = True)
self.generated = True
def draw(self, con, mode):
if mode == "normal":
libtcod.image_blit_2x(self.image, con, 0, 0)
for x in range(self.width):
for y in range(self.height):
tile = self.tiles[x][y]
if tile.char is not None:
libtcod.console_set_default_foreground(con, tile.fg_color)
libtcod.console_put_char(con, x, y, tile.char, libtcod.BKGND_NONE)
elif mode == "tiles":
for x in range(self.width):
for y in range(self.height):
if self.tiles[x][y].terrain == "land":
libtcod.console_set_default_background(con, libtcod.Color(40, 62, 19))
libtcod.console_put_char(con, x, y, ' ', libtcod.BKGND_SET)
elif self.tiles[x][y].terrain == "water":
libtcod.console_set_default_background(con, libtcod.Color(38, 50, 60))
libtcod.console_put_char(con, x, y, ' ', libtcod.BKGND_SET)
elif mode == "temps":
cols = [libtcod.green, libtcod.yellow, libtcod.red]
col_idx = [0, 50, 100]
col_map = libtcod.color_gen_map(cols, col_idx)
for x in range(self.width):
for y in range(self.height):
libtcod.console_set_default_foreground(con, col_map[int(self.tiles[x][y].temp*100)])
libtcod.console_put_char(con, x, y, 'o', libtcod.BKGND_NONE)
elif mode == "rain":
cols = [libtcod.yellow, libtcod.green, libtcod.blue]
col_idx = [0, 50, 100]
col_map = libtcod.color_gen_map(cols, col_idx)
for x in range(self.width):
for y in range(self.height):
libtcod.console_set_default_foreground(con, col_map[int(self.tiles[x][y].rain*100)])
libtcod.console_put_char(con, x, y, 'o', libtcod.BKGND_NONE)
else:
print "Invalid drawing mode passed to Map.draw: " + mode
sys.exit(1)