forked from Akalay27/pythonRoguelike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.py
More file actions
571 lines (389 loc) · 16.1 KB
/
Copy pathmap.py
File metadata and controls
571 lines (389 loc) · 16.1 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
from util import *
import random
#import pygame
import math
from pathlib import Path
class Map(object):
MAX_HALLWAY_LENGTH = 30
MAX_ITERS = 4
MAX_ROOMS = 5
tileSize = 64
borderTypes = {"0000":0,"0011":1,"1001":2,"1100":7,"0110":6,"1010":3,"1000":13,"0010":8,"0101":5,"0100":10,"0001":15,"1111":12,"1011":11,"1110":16,"0111":17,"1101":18}
def __init__(self):
self.rooms = []
self.loadRooms("rooms.txt")
self.loadTextures("textures/tilemapTest1.png", 16)
self.generateMap()
self.generateTextures()
self.combineTiles()
def loadRooms(self,filename):
# seperate txt into array of room configurations
file = open("rooms.txt","r")
def loadRooms(self,filename):
self.roomConfigurations = []
with open(filename, 'r' ) as f:
lines = f.readlines()
loadedRoom = []
for line in lines:
if "&" not in line:
loadedRoom.append([i for i in line[:len(line)-1]])
else:
self.roomConfigurations.append(loadedRoom)
loadedRoom = []
def generateMap(self):
self.rooms = []
'''creates root room and generates all rooms of the map'''
self.numberOfRooms = 0
self.rooms = []
origin = self.Room(self,self.rooms)
origin.generateRoom()
#origin.setPosition(-5, -5, 1)
origin.createChildren()
def tileAt(self,x,y,room=None,bbox=None,fast=False):
'''returns Map.Tile at location, returns Map.Tile of type None if no tile found, optionally searching in one specific room or in a rectangular area'''
if not fast:
if room == None:
for r in self.rooms:
if bbox != None:
if not r.bbox.collidesWithRect(bbox): continue
if r.bbox.insideOf(x,y):
for t in r.tiles:
if t.x == x and t.y == y:
return t
else:
for t in room.tiles:
if t.x == x and t.y == y:
return t
else:
return self.getTileFromArray(x, y)
return Map.Tile(x,y, None)
def roomAt(self,x,y):
'''returns Map.Room at location, returns Map.Tile of type None if no tile found.'''
for room in self.rooms:
if room.bbox.insideOf(x,y):
return room
return Map.Tile(x,y, None)
def neighbors(self,x,y,room=None): #clockwise from +x
'''returns array of 8 neighbors around a Tile, starting from left (x+1,y) clockwise'''
neighbors = []
neighbors.append(self.tileAt(x+1,y,room))
neighbors.append(self.tileAt(x+1,y+1,room))
neighbors.append(self.tileAt(x,y+1,room))
neighbors.append(self.tileAt(x-1,y+1,room))
neighbors.append(self.tileAt(x-1,y,room))
neighbors.append(self.tileAt(x-1,y-1,room))
neighbors.append(self.tileAt(x,y-1,room))
neighbors.append(self.tileAt(x+1,y-1,room))
return neighbors
def combineTiles(self):
'''for drawing tiles on the screen, using a large 2d array instead of individual objects using search function'''
minX, maxX, minY, maxY = 0,0,0,0
for room in self.rooms:
for tile in room.tiles:
if tile.x < minX: minX = tile.x
if tile.x > maxX: maxX = tile.x
if tile.y < minY: minY = tile.y
if tile.y > maxY: maxY = tile.y
self.bbox = Rect(minX, minY, maxX, maxY)
self.allTiles = []
for y in range(minY,maxY+1):
row = []
for x in range(minX,maxX+1):
row.append(self.tileAt(x,y))
self.allTiles.append(row)
def getTileFromArray(self,x,y):
if x >= self.bbox.x1 and x <= self.bbox.x2 and y >= self.bbox.y1 and y <= self.bbox.y2:
return self.allTiles[y-self.bbox.y1][x-self.bbox.x1]
return Map.Tile(x,y, None)
def printMap(self):
'''prints the Map in ascii format'''
for y in range(-40,40):
for x in range(-40,40):
if (x,y) != (0,0):
if self.tileAt(x,y).type != None:
print(self.tileAt(x,y).type,end="")
else:
print(" ", end="")
else:
print("x",end="")
print("")
class Room:
'''holds Map tiles located inside of the Room and creates children rooms'''
def __init__ (self, map, rooms, iterationStep = 0):
self.map = map
self.roomArray = rooms
self.roomArray.append(self)
self.iterationStep = iterationStep
self.numberOfChildren = 1
def setVisibility(self,visible):
for tile in self.tiles:
tile.visible = visible
def generateRoom(self):
'''chooses room configuration from Map's room file'''
self.tiles = []
configIndex = random.randint(0,len(self.map.roomConfigurations)-1)
#print("CONFIG #{}".format(configIndex))
config = self.map.roomConfigurations[configIndex]
for y in range(len(config)):
for x in range(len(config[y])):
if config[y][x] != " ":
self.tiles.append(Map.Tile(x,y,int(config[y][x])))
self.calculateBBox()
doors = []
for t in self.tiles: #figure out direction of each door (so it generates outwards)
if t.type == Map.Tile.DOOR:
nb = self.map.neighbors(t.x, t.y,self)
if nb[4].type == Map.Tile.FLOOR:
doorDirection = 0
if nb[6].type == Map.Tile.FLOOR:
doorDirection = 1
if nb[0].type == Map.Tile.FLOOR:
doorDirection = 2
if nb[2].type == Map.Tile.FLOOR:
doorDirection = 3
t.direction = doorDirection
doors.append(t)
self.doors = doors
def getDoorCoordDir(self,direction,twoDirections=False):
'''returns +x,y-, -x,-y etc for each direction 0-3 clockwise. If twoDirections is true then only +x or +y'''
if twoDirections:
if direction == 0 or direction == 2:
moveDir = Point(1,0)
else:
moveDir = Point(0,1)
else:
if direction == 0:
moveDir = Point(1,0)
elif direction == 1:
moveDir = Point(0,1)
elif direction == 2:
moveDir = Point(-1,0)
else:
moveDir = Point(0,-1)
return moveDir
def calculateBBox(self,collideTest=False):
'''updates bounding box of the room. This must be called when moving or changing the room configuration collideTest expands bounding box up 2 and left, right, and bottom sides are expanded 1'''
self.bbox = Rect(min(t.x for t in self.tiles), min(t.y for t in self.tiles), max(t.x for t in self.tiles), max(t.y for t in self.tiles))
if collideTest:
self.bbox.y1-=2
def setPosition(self,x,y,doorDirection,selected=None):
'''sets the position of the room based on a door Tile facing a certain direction or a pre-selected door tile'''
if selected != None:
selectedDoor = selected
else:
#
possibleDoors = []
for doorTile in self.doors:
if doorTile.direction == doorDirection:
possibleDoors.append(doorTile)
if len(possibleDoors) == 0: return -1
selectedDoor = possibleDoors[random.randint(0,len(possibleDoors)-1)] #if there are 2 doors facing the right direction, then choose 1
moveX = x-selectedDoor.x
moveY = y-selectedDoor.y
for t in self.tiles: #shift entire room to new position
t.x+=moveX
t.y+=moveY
self.calculateBBox(collideTest=False)
return selectedDoor
def createChildren(self):
'''create iterations of room based on doors of Room'''
self.children = []
for doorTile in self.doors:
doorTile.type = Map.Tile.WALL
selectedDoors = []
# self.numberOfChildren = random.randint(1, len(self.doors))
# while (len(selectedDoors) < self.numberOfChildren):
# door = self.doors[random.randint(0,len(self.doors)-1)]
# if door not in selectedDoors:
# selectedDoors.append(door)
selectedDoors = self.doors
for doorTile in selectedDoors:
child = Map.Room(self.map,self.roomArray,self.iterationStep+1)
selectedDoor = -1
connectingDirection = (doorTile.direction+2)%4
while selectedDoor == -1: #get new configuration of room until the configuration contains a door facing the right direction
child.generateRoom()
selectedDoor = child.setPosition(doorTile.x, doorTile.y, connectingDirection)
moveDir = self.getDoorCoordDir(doorTile.direction)
invalidChild = False
for distance in range(random.randint(3,8),Map.MAX_HALLWAY_LENGTH+1): #move new child away from this room until it doesn't collide with anything else
newDoorPosition = Point(doorTile.x+moveDir.x*distance,doorTile.y+moveDir.y*distance)
child.setPosition(newDoorPosition.x, newDoorPosition.y, (doorTile.direction+2)%4, selectedDoor)
validPos = True
for room in self.roomArray:
if room != child:
if room.bbox.collidesWithRect(child.bbox):
validPos = False
if validPos == True:
break
if distance == Map.MAX_HALLWAY_LENGTH:
invalidChild = True
doorTile.type = Map.Tile.WALL
break
child.doors.remove(selectedDoor)
if not invalidChild: #if hallway connection intersects with anything then also invalidate child
if (manhattanDistance(doorTile.toPoint(),selectedDoor.toPoint()) > 1):
hallway = Map.Hallway(self.map, self.roomArray)
hallway.generateHallway(doorTile,selectedDoor)
for r in self.roomArray:
if r != hallway:
if r.bbox.collidesWithRect(hallway.bbox):
invalidChild = True
doorTile.type = Map.Tile.WALL
self.roomArray.remove(hallway)
break
if invalidChild:
self.roomArray.remove(child)
continue
else:
doorTile.type = Map.Tile.DOOR
self.children.append(child)
self.map.numberOfRooms+=1
#make 2 doors instead of 1
secondOffset = self.getDoorCoordDir((doorTile.direction-1)%4,twoDirections=True)
secondDoor, secondDoorChild = self.map.tileAt(doorTile.x+secondOffset.x,doorTile.y+secondOffset.y), self.map.tileAt(selectedDoor.x+secondOffset.x,selectedDoor.y+secondOffset.y)
secondDoor.type, secondDoorChild.type = Map.Tile.DOOR, Map.Tile.DOOR
secondDoor.direction, secondDoorChild.direction = doorTile.direction, selectedDoor.direction
#make doors close off also better way of limiting rooms please
if self.map.numberOfRooms > Map.MAX_ROOMS:
for door in child.doors: #remove other doors of child if child isn't going to have more rooms
if door != selectedDoor:
door.type = Map.Tile.WALL
return
for child in self.children:
child.createChildren()
class Hallway(Room):
"""Room connecting rooms together"""
def generateHallway(self,tStart,tEnd): #make walls connecting rooms together, NOT including walls adjacent to doors.
self.tiles = []
moveDir = self.getDoorCoordDir(tStart.direction,twoDirections=True)
if tStart.x > tEnd.x or tStart.y > tEnd.y:
tStart, tEnd = tEnd, tStart
steps = manhattanDistance(tStart.toPoint(), tEnd.toPoint())
sideDirection = self.getDoorCoordDir(((tStart.direction-1)%4),twoDirections=True)
for i in range(1,steps):
stepPosition = Point(tStart.x+moveDir.x*i, tStart.y+moveDir.y*i)
leftWall = Map.Tile(stepPosition.x+sideDirection.x*2, stepPosition.y+sideDirection.y*2, Map.Tile.WALL)
rightWall = Map.Tile(stepPosition.x-sideDirection.x, stepPosition.y-sideDirection.y, Map.Tile.WALL)
floorLeft = Map.Tile(stepPosition.x+sideDirection.x, stepPosition.y+sideDirection.y, Map.Tile.FLOOR)
floorRight = Map.Tile(stepPosition.x, stepPosition.y, Map.Tile.FLOOR)
self.tiles.append(floorLeft)
self.tiles.append(floorRight)
self.tiles.append(leftWall)
self.tiles.append(rightWall)
self.calculateBBox()
def loadTextures(self,filename,size):
img = pygame.image.load(filename)
textures = []
imgWidth, imgHeight = img.get_size()
for y in range(0,imgHeight,size):
for x in range(0,imgWidth,size):
tex = pygame.transform.scale(img.subsurface(pygame.Rect(x,y,size,size)),(self.tileSize,self.tileSize))
textures.append(tex)
self.textures = textures
def generateTextures(self):
for room in self.rooms:
for tile in room.tiles:
nb = self.neighbors(tile.x,tile.y)
if tile.type == Map.Tile.FLOOR or tile.type == Map.Tile.DOOR and tile.wallType == 0:
tile.texture = self.textures[10]
if tile.type == Map.Tile.WALL:
wallSeg2 = self.tileAt(tile.x, tile.y-1)
wallSeg3 = self.tileAt(tile.x, tile.y-2)
if wallSeg2.type == None:
room.tiles.append(wallSeg2)
if wallSeg3.type == None:
room.tiles.append(wallSeg3)
#if below the floor tiles, then only occlude 1 tile in front. Otherwise occlude 2 like normal
if nb[2].type == Map.Tile.FLOOR or nb[2].type == Map.Tile.DOOR: #front facing wall case
tile.texture = self.textures[random.randint(5,9)]
wallSeg2.texture = self.textures[random.randint(0,4)]
wallSeg3.texture = self.getBorderTexture(nb)
wallSeg2.wallType, wallSeg3.wallType, tile.wallType = 2 , 3, 1
y = 1
while self.tileAt(tile.x,tile.y-y).type == Map.Tile.WALL:
nextAbove = self.tileAt(tile.x,tile.y-2-y)
if self.roomAt(nextAbove.x,nextAbove.y) != room:
break
nextAbove.texture = self.getBorderTexture(self.neighbors(tile.x,tile.y-y))
nextAbove.wallType = 3
y+=1
elif nb[6].type == Map.Tile.FLOOR and (nb[2].type == None or nb[2].type == Map.Tile.WALL) and tile.wallType != 3 and wallSeg2.wallType != 3: #bottom of room case
wallSeg2.texture = self.getBorderTexture(self.neighbors(tile.x,tile.y))
tile.texture = self.getBorderTexture(self.neighbors(tile.x,tile.y+1))
wallSeg2.wallType = 3
tile.wallType = 3
elif tile.wallType == 0: #vertical sides of rooms, find bottom tile of the row and then iterate upwards
tile.texture = self.getBorderTexture(nb)
lowestTile = tile
ly = 0
while self.tileAt(tile.x,tile.y+ly) == Map.Tile.WALL:
lowestTile = self.tileAt(tile.x,tile.y+ly)
ly+=1
y = 0
offset = 1
while self.tileAt(lowestTile.x,lowestTile.y-y).type == Map.Tile.WALL and self.tileAt(lowestTile.x,lowestTile.y-y).wallType != 3:
nextAbove = self.tileAt(lowestTile.x,lowestTile.y-offset-y)
if nextAbove.type == Map.Tile.FLOOR:
offset = 1
if nextAbove.type == None:
room.tiles.append(nextAbove)
room.calculateBBox()
if self.roomAt(nextAbove.x,nextAbove.y) != room or self.neighbors(nextAbove.x,nextAbove.y-2)[6] == Map.Tile.FLOOR:
break
nextAbove.texture = self.getBorderTexture(self.neighbors(lowestTile.x,lowestTile.y-y))
nextAbove.wallType = 3
y+=1
print(y)
if y > 0:
offset = 2
room.calculateBBox()
# elif tile.wallType == 0:
# if (nb[0].type == Map.Tile.FLOOR and nb[4].type == None) or (nb[0].type == None or nb[4].type == Map.Tile.FLOOR):
# tile.texture = self.getBorderTexture(self.neighbors(tile.x,tile.y+1))
# else:
# tile.texture = self.getBorderTexture(self.neighbors(tile.x,tile.y+2))
for room in self.rooms:
for tile in room.tiles:
if tile.wallType in (1,2):
tile.type = Map.Tile.WALL
#set top wall segment later when all top wall segments are defined
#if tile.type == Map.Tile.FLOOR:
def getBorderTexture(self,nb):
option = ""
for t in (nb[0],nb[2],nb[4],nb[6]):
if t.type == Map.Tile.WALL or t.type == None:
option = option + "0"
else:
option = option + "1"
#return self.textures[self.borderTypes[option]] //WHEN THERE ARE NO BORDER TEXTURES
return self.textures[12]
def draw(self,game,background=True):
tileRangeY = math.ceil(game.CANVAS_HEIGHT/self.tileSize/2)+1
tileRangeX = math.ceil(game.CANVAS_WIDTH/self.tileSize/2)+1
cameraTilePos = Point(int(game.cameraPos.x/self.tileSize), int(game.cameraPos.y/self.tileSize))
for y in range(cameraTilePos.y-tileRangeY,cameraTilePos.y+tileRangeY):
for x in range(cameraTilePos.x-tileRangeX,cameraTilePos.x+tileRangeX):
tile = self.tileAt(x, y,fast=True)
if tile.visible:
if (tile.wallType == 3) and not background or background:
drawPos = game.getCameraPoint(Point(x*self.tileSize,y*self.tileSize))
if tile.texture != None:
game.screen.blit(tile.texture,(drawPos[0],drawPos[1]))
# if room == None and tile == None:
class Tile:
DOOR = 2
WALL = 1
FLOOR = 0
def __init__ (self,x,y,t):
self.x = x
self.y = y
self.type = t
self.direction = -1
self.texture = None
self.wallType = 0 #only for setting correct border tile
self.visible = False
def __str__ (self):
return "Tile of type {} at {}, {}".format(self.type,self.x,self.y)
def toPoint(self):
return Point(self.x, self.y)