2020-03-02 07:53:52 -08:00
|
|
|
import random
|
|
|
|
import time
|
2020-03-04 11:23:23 -08:00
|
|
|
import math
|
|
|
|
|
|
|
|
MSIZE = 80
|
2020-03-02 07:53:52 -08:00
|
|
|
|
|
|
|
def rand(*args):
|
|
|
|
random.seed(time.clock())
|
|
|
|
return random.randint(*args)
|
|
|
|
|
|
|
|
class Room():
|
|
|
|
def __init__(self, x, y, width, height):
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
|
|
|
|
self.left = x
|
|
|
|
self.right = x + width
|
|
|
|
self.top = y
|
|
|
|
self.bottom = y + height
|
|
|
|
|
2020-03-04 11:23:23 -08:00
|
|
|
self.cx = math.floor(self.x + (width / 2))
|
|
|
|
self.cy = math.floor(self.y + (height / 2))
|
|
|
|
|
|
|
|
def intersects(self, other):
|
|
|
|
return self.left <= other.right and self.right >= other.left and self.top <= other.bottom and self.bottom >= other.top
|
|
|
|
|
|
|
|
class Generator():
|
2020-03-02 07:53:52 -08:00
|
|
|
rooms = []
|
2020-03-04 11:23:23 -08:00
|
|
|
board = []
|
|
|
|
|
|
|
|
def __init__(self, width, height = None):
|
|
|
|
self.width = width
|
|
|
|
self.height = height or width
|
|
|
|
|
|
|
|
for y in range(self.height):
|
|
|
|
self.board.append([])
|
|
|
|
for _ in range(self.width):
|
|
|
|
self.board[y].append(0)
|
|
|
|
|
|
|
|
self.place_rooms()
|
|
|
|
self.place_corridors()
|
|
|
|
|
|
|
|
def place_rooms(self):
|
2020-03-04 13:07:11 -08:00
|
|
|
for _ in range(rand(10, 30)):
|
|
|
|
width = rand(4, 10)
|
|
|
|
height = rand(4, 10)
|
2020-03-02 07:53:52 -08:00
|
|
|
x = rand(0, 60)
|
|
|
|
y = rand(0, 60)
|
|
|
|
|
2020-03-04 11:23:23 -08:00
|
|
|
if x + width > self.width:
|
|
|
|
x = self.width - width
|
|
|
|
|
|
|
|
if y + height > self.height:
|
|
|
|
y = self.height - height
|
|
|
|
|
|
|
|
collides = False
|
|
|
|
room = Room(x, y, width, height)
|
|
|
|
|
|
|
|
for other_room in self.rooms:
|
|
|
|
if room.intersects(other_room):
|
|
|
|
collides = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if not collides:
|
|
|
|
self.place_room(room)
|
2020-03-02 07:53:52 -08:00
|
|
|
|
2020-03-04 11:23:23 -08:00
|
|
|
def place_room(self, room):
|
|
|
|
for row in range(room.height):
|
|
|
|
for col in range(room.width):
|
|
|
|
y = room.y + row
|
|
|
|
x = room.x + col
|
|
|
|
|
|
|
|
self.board[y][x] = 1
|
|
|
|
|
|
|
|
self.rooms.append(room)
|
2020-03-02 07:53:52 -08:00
|
|
|
|
2020-03-04 11:23:23 -08:00
|
|
|
def place_corridors(self):
|
|
|
|
for i in range(0, len(self.rooms) - 1):
|
|
|
|
room1 = self.rooms[i]
|
|
|
|
room2 = self.rooms[i + 1]
|
2020-03-02 07:53:52 -08:00
|
|
|
|
2020-03-04 11:23:23 -08:00
|
|
|
if rand(0, 2) == 0:
|
|
|
|
if room1.cx <= room2.cx:
|
|
|
|
self.horiz_corridor(room1.cx, room2.cx, room1.cy)
|
|
|
|
else:
|
|
|
|
self.horiz_corridor(room2.cx, room1.cx, room1.cy)
|
|
|
|
if room1.cy <= room2.cy:
|
|
|
|
self.vert_corridor(room1.cy, room2.cy, room2.cx)
|
|
|
|
else:
|
|
|
|
self.vert_corridor(room2.cy, room1.cy, room2.cx)
|
|
|
|
else:
|
|
|
|
if room1.cy <= room2.cy:
|
|
|
|
self.vert_corridor(room1.cy, room2.cy, room2.cx)
|
|
|
|
else:
|
|
|
|
self.vert_corridor(room2.cy, room1.cy, room2.cx)
|
|
|
|
if room1.cx <= room2.cx:
|
|
|
|
self.horiz_corridor(room1.cx, room2.cx, room1.cy)
|
|
|
|
else:
|
|
|
|
self.horiz_corridor(room2.cx, room1.cx, room1.cy)
|
2020-03-02 07:53:52 -08:00
|
|
|
|
2020-03-04 11:23:23 -08:00
|
|
|
def horiz_corridor(self, x1, x2, y):
|
|
|
|
for row in range(y - 1, y + 2):
|
|
|
|
for col in range(x1 - 1, x2 + 2):
|
|
|
|
self.board[row][col] = 1
|
|
|
|
|
|
|
|
def vert_corridor(self, y1, y2, x):
|
|
|
|
for row in range(y1, y2 + 2):
|
|
|
|
for col in range(x - 1, x + 2):
|
|
|
|
self.board[row][col] = 1
|
|
|
|
|
|
|
|
def get_map(self):
|
2020-03-04 12:32:22 -08:00
|
|
|
# Map object
|
2020-03-04 11:23:23 -08:00
|
|
|
map = {
|
|
|
|
"tiles": [
|
|
|
|
"floor_cobble.png",
|
|
|
|
"wall_cobble_down.png",
|
|
|
|
"wall_cobble_right.png",
|
|
|
|
"wall_cobble_up.png",
|
|
|
|
"wall_cobble_left.png",
|
|
|
|
"wall_cobble_corner_nw_inner.png",
|
|
|
|
"wall_cobble_corner_ne_inner.png",
|
|
|
|
"wall_cobble_corner_se_inner.png",
|
|
|
|
"wall_cobble_corner_sw_inner.png",
|
|
|
|
"wall_cobble_corner_nw_outer.png",
|
|
|
|
"wall_cobble_corner_ne_outer.png",
|
|
|
|
"wall_cobble_corner_se_outer.png",
|
|
|
|
"wall_cobble_corner_sw_outer.png"
|
|
|
|
],
|
|
|
|
"renderLayers": []
|
|
|
|
}
|
|
|
|
|
2020-03-04 12:32:22 -08:00
|
|
|
# Generate walls
|
|
|
|
bounds = []
|
|
|
|
for y in range(self.height):
|
|
|
|
bounds.append([])
|
|
|
|
for x in range(self.width):
|
|
|
|
wall = 0
|
|
|
|
if self.board[y][x] > 0:
|
|
|
|
for x2 in range(x - 1, x + 2):
|
|
|
|
for y2 in range(y - 1, y + 2):
|
|
|
|
if self.board[y2][x2] == 0:
|
|
|
|
wall += 1
|
|
|
|
bounds[y].append(wall)
|
|
|
|
map["boundaries"] = bounds
|
|
|
|
|
|
|
|
# Floor layer
|
2020-03-04 11:23:23 -08:00
|
|
|
layer1 = self.board.copy()
|
|
|
|
map["renderLayers"].append(layer1)
|
|
|
|
|
2020-03-04 12:32:22 -08:00
|
|
|
# Wall layer
|
|
|
|
layer2 = []
|
|
|
|
for y in range(self.height):
|
|
|
|
layer2.append([])
|
|
|
|
for _ in range(self.width):
|
|
|
|
layer2[y].append(0)
|
|
|
|
|
2020-03-04 11:23:23 -08:00
|
|
|
for y in range(1, self.height - 1):
|
|
|
|
for x in range(1, self.width - 1):
|
|
|
|
if self.board[y][x] == 1:
|
2020-03-04 12:32:22 -08:00
|
|
|
tile = 0
|
2020-03-04 11:23:23 -08:00
|
|
|
if self.board[y][x - 1] == 1 and self.board[y][x + 1] == 1:
|
|
|
|
if self.board[y - 1][x] == 0:
|
|
|
|
tile = 2
|
|
|
|
elif self.board[y + 1][x] == 0:
|
|
|
|
tile = 4
|
|
|
|
|
|
|
|
if self.board[y - 1][x] == 1 and self.board[y + 1][x] == 1:
|
|
|
|
if self.board[y][x - 1] == 0:
|
|
|
|
tile = 3
|
|
|
|
elif self.board[y][x + 1] == 0:
|
|
|
|
tile = 5
|
|
|
|
|
|
|
|
layer2[y][x] = tile
|
|
|
|
|
2020-03-04 12:32:22 -08:00
|
|
|
for y in range(1, self.height - 1):
|
|
|
|
for x in range(1, self.width - 1):
|
|
|
|
if bounds[y][x] > 0 and layer2[y][x] == 0:
|
|
|
|
tile = 0
|
|
|
|
if bounds[y + 1][x] > 0 and bounds[y][x + 1] > 0:
|
|
|
|
if bounds[y][x] > 1:
|
|
|
|
tile = 6
|
|
|
|
else:
|
|
|
|
tile = 10
|
|
|
|
elif bounds[y + 1][x] > 0 and bounds[y][x - 1] > 0:
|
|
|
|
if bounds[y][x] > 1:
|
|
|
|
tile = 7
|
|
|
|
else:
|
|
|
|
tile = 11
|
|
|
|
elif bounds[y - 1][x] > 0 and bounds[y][x - 1] > 0:
|
|
|
|
if bounds[y][x] > 1:
|
|
|
|
tile = 8
|
|
|
|
else:
|
|
|
|
tile = 12
|
|
|
|
elif bounds[y - 1][x] > 0 and bounds[y][x + 1] > 0:
|
|
|
|
if bounds[y][x] > 1:
|
|
|
|
tile = 9
|
|
|
|
else:
|
|
|
|
tile = 13
|
2020-03-04 11:23:23 -08:00
|
|
|
|
2020-03-04 12:32:22 -08:00
|
|
|
layer2[y][x] = tile
|
2020-03-04 11:23:23 -08:00
|
|
|
|
2020-03-04 12:32:22 -08:00
|
|
|
map["renderLayers"].append(layer2)
|
2020-03-04 11:23:23 -08:00
|
|
|
|
|
|
|
return map
|