/
Places.py
786 lines (687 loc) · 39.4 KB
/
Places.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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
from Displayable import DisplayableObject, AnimatedSpriteObject
from GameObject import GameObject, Door
__author__ = 'Tangil'
"""
Town, places and other map functions
# TODO: Optimize the walls: some walls show up as regular floor.
"""
import random
import Util
import pygame
import Constants
import GameData
class Building(object):
UNKNOWN = "unknown"
GATE = "gate"
TRADING_POST = "trading_post"
BANK = "bank"
TAVERN = "tavern"
MERCENARY_GUILD = "mercenary_guild"
BLACKSMITH = "blacksmith"
STABLES = "stables"
TOWNHOUSE = "townhouse"
def __init__(self, town):
self.decoration_list = {}
self.town = town
self.name = Building.UNKNOWN
return
def action_pane(self):
pass
class TradingPost(Building):
def __init__(self, town):
super().__init__(town)
self.name = Building.TRADING_POST
self.gold = random.randint(1, 200)
self.goods_available = [
GameObject(Util.MName().new(), GameObject.JUNK, weight=random.randint(1, 10), volume=random.randint(1, 5),
regular_value=random.randint(2, 10), displayable_object=None)
for x in range(random.randint(1, 5))]
self.decoration_list = {
"1x1": [("Decor", True, (0, 64)), ("Decor", True, (16, 64)), ("Decor", True, (32, 64)),
("Decor", True, (0, 112)), ("Decor", True, (16, 112), (16, 128)), ("Decor", True, (32, 112))],
"1x3": [("Decor", True, (0, 160))]
}
return
def action_pane(self):
pass
class Town(object):
"""
A Town is the main place of the game. A town hosts buildings, hosts NPCs, hosts gameobjects.
"""
def __init__(self, building_number, make_map=False, render_map=False):
self.name = Util.MName().new()
self.things_id_list = []
self.available_paths = []
self.buildings = [TradingPost(self), TradingPost(self)]
size = (50, 50)
if 4 < building_number <= 6:
size = (65, 65)
elif 6 < building_number:
size = (80, 80)
self.tile_map = TownTileMap(self, size, make_map=make_map, render_map=render_map)
return
def __str__(self):
return self.name
def add_path(self, path):
self.available_paths.append(path)
return
def build_tile_map(self):
size = (50, 50)
if 4 < len(self.buildings) <= 6:
size = (65, 65)
elif 6 < len(self.buildings):
size = (80, 80)
self.tile_map = TownTileMap(self, size, make_map=True, render_map=True)
return
def register_thing(self, a_thing):
"""
Add an object (NPC, GameObject...) in the current town list. Only the id of the object is added...
:param a_thing: the object to add (NPC, GameObject)
:return: Nothing
"""
if not a_thing.id in self.things_id_list:
self.things_id_list.append(a_thing.id)
a_thing.town = self
if hasattr(a_thing, "displayable_object"):
if a_thing.displayable_object and a_thing.displayable_object.position_on_tile:
self.tile_map.map[a_thing.displayable_object.position_on_tile].register_thing(a_thing)
return
def unregister_thing(self, a_thing):
"""
Add an object (NPC, GameObject...) in the current town list. Only the id of the object is added...
:param a_thing: the object to add (NPC, GameObject)
:return: Nothing
"""
if a_thing.id in self.things_id_list:
self.things_id_list.remove(a_thing.id)
a_thing.town = None
if hasattr(a_thing, "displayable_object"):
if a_thing.displayable_object and a_thing.displayable_object.position_on_tile:
self.tile_map.map[a_thing.displayable_object.position_on_tile].unregister_thing(a_thing)
return
class Path(object):
""" A Path links two towns. Note that due to Geography, path from A to B may be different from B to A...
"""
def __init__(self, origin, destination, days):
self.origin_town = origin
self.destination_town = destination
self.days = days
def __str__(self):
return "to {} in {} days.".format(self.destination_town, str(self.days))
class TownGraph(object):
def __init__(self, towns):
self.towns = towns
if len(towns) <= 3:
num_paths = random.randint(len(towns) - 1, (len(towns) * (len(towns) - 1)) / 2)
else:
num_paths = random.randint(len(towns) - 1, len(towns) * 2)
# internal purpose...
self._edges = []
self._edge_set = set()
self.build_graph(num_paths)
for edge in self._edges:
edge[0].add_path(Path(edge[0], edge[1], random.randint(1, 10)))
edge[1].add_path(Path(edge[1], edge[0], random.randint(1, 10)))
def build_graph(self, num_paths):
# Building algorithm process using random walk from https://gist.github.com/bwbaugh/4602818
# Create two partitions, source and target. Initially store all nodes in S.
source, target = set(self.towns), set()
# Pick a random node, and mark it as visited and the current node.
current_node = random.sample(source, 1).pop()
source.remove(current_node)
target.add(current_node)
# Create a random connected graph.
while source:
# Randomly pick the next node from the neighbors of the current node.
# As we are generating a connected graph, we assume a complete graph.
neighbor_node = random.sample(self.towns, 1).pop()
# If the new node hasn't been visited, add the edge from current to new.
if neighbor_node not in target:
edge = (current_node, neighbor_node)
self.add_edge(edge)
source.remove(neighbor_node)
target.add(neighbor_node)
# Set the new node as the current node.
current_node = neighbor_node
# Add random edges until the number of desired edges is reached.
self.add_random_edges(num_paths)
def add_edge(self, edge):
"""Add the edge if the graph type allows it."""
if edge not in self._edge_set:
self._edges.append(edge)
self._edge_set.add(edge)
self._edge_set.add(edge[::-1]) # add other direction to set.
return True
return False
def make_random_edge(self):
"""Generate a random edge between any two nodes in the graph."""
random_edge = tuple(random.sample(self.towns, 2))
return random_edge
def add_random_edges(self, total_edges):
"""Add random edges until the number of desired edges is reached."""
while len(self._edges) < total_edges:
self.add_edge(self.make_random_edge())
def __str__(self):
result = "Number of towns: {}\n".format(len(self.towns))
for town in self.towns:
result += "Town: {}\n".format(town)
for path in town.available_paths:
result += "{}\n".format(str(path))
result += "\n"
return result
class Tile(object):
UNKNOWN = "unknown"
FLOOR = "floor"
WALL = "wall"
PATH = "path"
GRASS = "grass"
DIRT = "dirt"
WATER = "water"
ROCK = "rock"
def __init__(self, position, floor_type, tile_map_owner, decoration_type=None):
self.floor_type = floor_type
self.room = None
self.position = position
self.tile_map_owner = tile_map_owner
self.name_of_things_on_tile = []
return
def register_thing(self, a_thing):
if isinstance(a_thing, str):
self.name_of_things_on_tile.append(a_thing)
else:
self.name_of_things_on_tile.append(a_thing.id)
return
def unregister_thing(self, a_thing):
if isinstance(a_thing, str):
if a_thing in self.name_of_things_on_tile:
self.name_of_things_on_tile.remove(a_thing)
elif a_thing.id in self.name_of_things_on_tile:
self.name_of_things_on_tile.remove(a_thing.id)
return
@property
def blocking(self):
for game_id in self.name_of_things_on_tile:
if hasattr(GameData.game_dict[game_id], "blocking") and GameData.game_dict[game_id].blocking:
return True
return self.floor_type in (Tile.WATER, Tile.WALL, Tile.ROCK)
@property
def has_things(self):
return len(self.name_of_things_on_tile) > 0
def get_object_id(self, object_type=None):
"""
Fetch the first object id that is located here that matches the type
:return: the object id if there is something, None otherwise
"""
for an_id in self.name_of_things_on_tile:
if object_type and isinstance(GameData.game_dict[an_id], object_type):
return an_id
elif not object_type:
return an_id
return None
def __str__(self):
part1 = "{}x{} Floor characteristices: {} [blocking: {}]".format(
self.position[0], self.position[1], self.floor_type, self.blocking
)
part2 = ""
if self.name_of_things_on_tile:
part2 = "\n\t Currently at this location: "
for an_id in self.name_of_things_on_tile:
if hasattr(GameData.game_dict[an_id], "name"):
part2 += "\n\t\t{}".format(GameData.game_dict[an_id].name)
else:
part2 += "\n\t\t{}".format(an_id)
return part1+part2
class TileMap(object):
def __init__(self, size, make_map=False, render_map=False, style=Constants.DAWNLIKE_STYLE):
self.max_x = size[0]
self.max_y = size[1]
self.map = {}
self.surface_memory = None
for x in range(self.max_x):
for y in range(self.max_y):
self.map[(x, y)] = Tile((x, y), Tile.UNKNOWN, self)
if make_map:
self.make_map()
if render_map:
self.render(style)
def make_map(self):
pass
def render(self, style=Constants.DAWNLIKE_STYLE):
pass
def compute_tile_weight(self, x, y, terrain_type):
count = 0
if y - 1 >= 0 and self.map[(x, y - 1)].floor_type == terrain_type:
count += 1
if x + 1 < self.max_x and self.map[(x + 1, y)].floor_type == terrain_type:
count += 2
if y + 1 < self.max_y and self.map[(x, y + 1)].floor_type == terrain_type:
count += 4
if x - 1 >= 0 and self.map[(x - 1, y)].floor_type == terrain_type:
count += 8
return count
class TownTileMap(TileMap):
def __init__(self, town, size, make_map=False, render_map=False, style=Constants.DAWNLIKE_STYLE):
super().__init__(size, make_map=False, render_map=False)
self.town = town
self.surface_memory = None
self.rooms = []
self.default_start_player_position = (0, 0)
if make_map or render_map:
self.make_map()
if render_map:
self.render(style)
def get_place_in_building(self, building_name):
default = (0, 0)
for room in self.rooms:
if room.building.name == building_name:
for trials in range(100):
place = random.choice(room.places)
if self.map[place].floor_type == Tile.FLOOR and not self.map[place].blocking:
return place
return default
print("Warning: room type not found!!")
return default
def make_map(self):
def prepare_ground():
# Reset all!
for x in range(self.max_x):
for y in range(self.max_y):
self.map[(x, y)] = Tile((x, y), Tile.UNKNOWN, self)
# First: prepare the land with Dirt and Grass
# Use the IslandMaze algo... http://www.evilscience.co.uk/?p=53
def check_cell(x_val, y_val):
if 0 <= x_val < self.max_x and 0 <= y_val < self.max_y:
if self.map[(x_val, y_val)].floor_type == Tile.GRASS:
return True
return False
def examine_neighbours(x_val, y_val):
count = 0
for var_x in (-1, 0, 1):
for var_y in (-1, 0, 1):
if check_cell(x_val + var_x, y_val + var_y):
count += 1
return count
for x in range(self.max_x):
for y in range(self.max_y):
if random.randint(0, 100) < 55:
self.map[(x, y)].floor_type = Tile.GRASS
else:
self.map[(x, y)].floor_type = Tile.DIRT
# Pick random cells
for i in range(4000):
random_x = random.randint(0, self.max_x - 1)
random_y = random.randint(0, self.max_y - 1)
if examine_neighbours(random_x, random_y) > 4:
self.map[(random_x, random_y)].floor_type = Tile.GRASS
else:
self.map[(random_x, random_y)].floor_type = Tile.DIRT
def add_shape(center_x, center_y, terrain_type):
x = center_x
y = center_y
for an_iteration in range(100):
if random.randint(1, 4) == 1 and x-1 >= 0:
x -= 1
self.map[(x, y)].floor_type = terrain_type
if random.randint(1, 4) == 1 and x+1 < self.max_x:
x += 1
self.map[(x, y)].floor_type = terrain_type
if random.randint(1, 4) == 1 and y-1 >= 0:
y -= 1
self.map[(x, y)].floor_type = terrain_type
if random.randint(1, 4) == 1 and y+1 < self.max_y:
y += 1
self.map[(x, y)].floor_type = terrain_type
class Room(object):
def __init__(self, town, width, height, top_x, top_y, building):
self.town = town
self.connected_by_path = False
self.places = []
self.doors = []
self.building = building
for x_coord in range(top_x, width + top_x):
for y_coord in range(top_y, height + top_y):
self.places.append((x_coord, y_coord))
additions = random.randint(1, 4)
for i in range(additions):
# Add an extra room: pick a coordinate, and build from that
origin = random.choice(self.places)
addition_width = max(width + random.randint(-3, 0), 3)
addition_height = max(height + random.randint(-3, 0), 3)
for x_coord in range(origin[0], addition_width + origin[0]):
for y_coord in range(origin[1], addition_height + origin[1]):
self.places.append((x_coord, y_coord))
def can_place(self, tile_map):
# Step 1: make sure the ground is not blocked
for place in self.places:
if place not in tile_map.keys() or tile_map[place].blocking:
return False
# Step 2: leave a two block path around the building
for place in self.places:
(x, y) = place
for x_var in (-2, -1, 0, 1, 2):
for y_var in (-2, -1, 0, 1, 2):
test_place = (x + x_var, y + y_var)
if test_place not in self.places and (test_place not in tile_map.keys()
or tile_map[test_place].blocking):
return False
return True
def compute_tile_weight(self, x, y, terrain_type_list, map, max_x, max_y):
count = 0
if y - 1 >= 0 and map[(x, y - 1)].floor_type in terrain_type_list:
count += 1
if x + 1 < max_x and map[(x + 1, y)].floor_type in terrain_type_list:
count += 2
if y + 1 < max_y and map[(x, y + 1)].floor_type in terrain_type_list:
count += 4
if x - 1 >= 0 and map[(x - 1, y)].floor_type in terrain_type_list:
count += 8
return count
def carve(self, tile_map, max_x, max_y):
for place in self.places:
tile_map[place].floor_type = Tile.FLOOR
tile_map[place].room = self.building
for place in self.places:
if self.compute_tile_weight(place[0], place[1], (Tile.FLOOR, Tile.WALL),
tile_map, max_x, max_y) != 15:
tile_map[place].floor_type = Tile.WALL
def smooth_walls(self, tile_map, max_x, max_y):
#TODO smooth wall is buggy
additional_walls = []
for place in self.places:
if tile_map[place].floor_type == Tile.FLOOR and \
self.compute_tile_weight(place[0], place[1], (Tile.WALL),
tile_map, max_x, max_y) == 3 and tile_map[
place[0] + 1, place[1] - 1].floor_type == Tile.FLOOR:
additional_walls.append(place)
elif tile_map[place].floor_type == Tile.FLOOR and \
self.compute_tile_weight(place[0], place[1], (Tile.WALL),
tile_map, max_x, max_y) == 6 and tile_map[
place[0] + 1, place[1] + 1].floor_type == Tile.FLOOR:
additional_walls.append(place)
#for place in additional_walls:
# tile_map[place].floor_type = Tile.WALL
def place_door(self, tile_map, max_x, max_y):
#nb_doors = random.randint(1, 3)
nb_doors = 1
nb_placed = 0
while nb_placed < nb_doors:
place = random.choice(self.places)
weight = self.compute_tile_weight(place[0], place[1],
(Tile.FLOOR, Tile.WALL), tile_map, max_x, max_y)
if weight in (7, 11, 14, 13):
orientation = Door.ORIENTATION_HORIZONTAL
if weight in (7, 13):
orientation = Door.ORIENTATION_VERTICAL
self.doors.append((place, orientation))
nb_placed += 1
def add_deco(self, tile_map, max_x, max_y):
"""
Add the decorative object. Note that the decoration is a GameObject!
:param tile_map:
:param max_x:
:param max_y:
:return: nothing
"""
list_doors = [door_def[0] for door_def in self.doors]
for i in range(20):
place = random.choice(self.places)
weight = self.compute_tile_weight(place[0], place[1], [Tile.FLOOR], tile_map, max_x, max_y)
near_door = (place[0], place[1] - 1) in list_doors or \
(place[0], place[1] + 1) in list_doors or \
(place[0] - 1, place[1]) in list_doors or \
(place[0] + 1, place[1]) in list_doors
if not (near_door or not (tile_map[place].floor_type == Tile.FLOOR) or tile_map[
place].has_things) and weight == 15:
a_deco = random.choice(self.building.decoration_list["1x1"])
decoration = GameObject(a_deco[0], GameObject.DECORATION,
town=self.town,
weight=random.randint(1, 10),
volume=random.randint(1, 5),
regular_value=random.randint(2, 10),
displayable_object=DisplayableObject(movable=False, blocking=a_deco[1],
position_on_tile=place,
graphical_representation=
AnimatedSpriteObject(
Constants.DAWNLIKE_STYLE,
"Objects", "Decor",
a_deco[2])))
GameData.register_object(decoration)
need_rebuild = True
while need_rebuild:
self.rooms = []
# Main process - Ground
print("Generating ground")
prepare_ground()
# Main process - Lake
print("Generating lake and rocks")
for k in range(random.randint(1, 3)):
add_shape(random.randint(0, self.max_x - 1), random.randint(0, self.max_y - 1), Tile.WATER)
add_shape(random.randint(0, self.max_x - 1), random.randint(0, self.max_y - 1), Tile.ROCK)
# Main process - Room
room_placed = []
self.rooms = []
trials = 0
print("Placing rooms")
while len(room_placed) < len(self.town.buildings) and trials < 100:
a_room = Room(self.town,
10 + random.randint(-3, 1),
10 + random.randint(-3, 1),
random.randint(self.max_x // 5, self.max_x - self.max_x // 5),
random.randint(self.max_y // 5, self.max_y - self.max_y // 5),
self.town.buildings[len(room_placed)])
if a_room.can_place(self.map):
a_room.carve(self.map, self.max_x, self.max_y)
a_room.smooth_walls(self.map, self.max_x, self.max_y)
a_room.place_door(self.map, self.max_x, self.max_y)
room_placed.append(a_room)
trials += 1
if trials >= 100:
print("Unable to place town in the allocated time - Regenerating")
else:
# Successfully placed all the rooms
need_rebuild = False
print("Successfully built after {} trials".format(trials))
print("Settings doors in rooms".format(trials))
# a door is an open floor
for room in room_placed:
for door in room.doors:
self.map[door[0]].floor_type = Tile.FLOOR
random.shuffle(room_placed)
for index, room in enumerate(room_placed[:-1]):
door1 = (room.doors[0])[0]
door2 = (room_placed[index + 1].doors[0])[0]
astar = Util.AStar(Util.SQ_MapHandler(self.map, self.max_x, self.max_y))
p = astar.findPath(Util.SQ_Location(door1[0], door1[1]), Util.SQ_Location(door2[0], door2[1]))
if not p:
# impossible to connect the room: reject and restart from scratch
print("One path not made... Regenerating".format(trials))
need_rebuild = True
else:
possible_starts = []
for n in p.nodes:
if self.map[(n.location.x, n.location.y)].floor_type not in (
Tile.FLOOR, Tile.PATH
):
self.map[(n.location.x, n.location.y)].floor_type = Tile.PATH
possible_starts.append((n.location.x, n.location.y))
if len(possible_starts) > 0:
self.default_start_player_position = random.choice(possible_starts)
# finish building it - Now redecorating and carving really the door
for room in room_placed:
room.add_deco(self.map, self.max_x, self.max_y)
# and adding the room to the official list of room
for room in room_placed:
self.rooms.append(room)
def render(self, style):
def build_floor_tile_dawnlike(source_file, origin_x, origin_y, destination_tile_size):
file_tile_size = (16, 16)
x_dev = file_tile_size[0]
y_dev = file_tile_size[1]
tile = [
source_file.subsurface(pygame.Rect((origin_x + 5 * x_dev, origin_y + 0 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 3 * x_dev, origin_y + 2 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 4 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 0 * x_dev, origin_y + 2 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 3 * x_dev, origin_y + 0 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 3 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 0 * x_dev, origin_y + 0 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 0 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 6 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 2 * x_dev, origin_y + 2 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 5 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 1 * x_dev, origin_y + 2 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 2 * x_dev, origin_y + 0 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 2 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 1 * x_dev, origin_y + 0 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 1 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy()
]
scaled_tile = []
for a_tile in tile:
scaled_tile.append(pygame.transform.smoothscale(a_tile, destination_tile_size))
return scaled_tile
def build_wall_tile_dawnlike(source_file, origin_x, origin_y, destination_tile_size):
file_tile_size = (16, 16)
x_dev = file_tile_size[0]
y_dev = file_tile_size[1]
tile = [
source_file.subsurface(pygame.Rect((origin_x + 1 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(
pygame.Rect((origin_x + 1 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 0 * x_dev, origin_y + 2 * y_dev), file_tile_size)).copy(),
source_file.subsurface(
pygame.Rect((origin_x + 0 * x_dev, origin_y + 2 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 0 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 0 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 0 * x_dev, origin_y + 0 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 3 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 2 * x_dev, origin_y + 2 * y_dev), file_tile_size)).copy(),
source_file.subsurface(
pygame.Rect((origin_x + 2 * x_dev, origin_y + 2 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 1 * x_dev, origin_y + 0 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 4 * x_dev, origin_y + 2 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 2 * x_dev, origin_y + 0 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 5 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 4 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 4 * x_dev, origin_y + 2 * y_dev), file_tile_size)).copy()
]
scaled_tile = []
for a_tile in tile:
scaled_tile.append(pygame.transform.smoothscale(a_tile, destination_tile_size))
return scaled_tile
def build_floor_tile_oryx(source_file, origin_x, origin_y, destination_tile_size):
file_tile_size = (24, 24)
x_dev = file_tile_size[0]
y_dev = file_tile_size[1]
tile = [
source_file.subsurface(pygame.Rect((origin_x + 0 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 6 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 1 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 9 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 4 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 5 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 7 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 14 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 3 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 10 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 2 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 15 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 8 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 13 * x_dev, origin_y + 1 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 2 * x_dev, origin_y + 0 * y_dev), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 1 * x_dev, origin_y + 0 * y_dev), file_tile_size)).copy()
]
scaled_tile = []
for a_tile in tile:
scaled_tile.append(pygame.transform.smoothscale(a_tile, destination_tile_size))
return scaled_tile
def build_wall_tile_oryx(source_file, origin_x, origin_y, destination_tile_size):
file_tile_size = (24, 24)
x_dev = file_tile_size[0]
tile = [
source_file.subsurface(pygame.Rect((origin_x + 9 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 15 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 10 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 18 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 13 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 14 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 16 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 23 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 12 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 19 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 11 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 24 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 17 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 22 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 21 * x_dev, origin_y), file_tile_size)).copy(),
source_file.subsurface(pygame.Rect((origin_x + 20 * x_dev, origin_y), file_tile_size)).copy()
]
scaled_tile = []
for a_tile in tile:
scaled_tile.append(pygame.transform.smoothscale(a_tile, destination_tile_size))
return scaled_tile
if not self.surface_memory:
self.surface_memory = pygame.Surface((self.max_x * Constants.TILE_SIZE[0],
self.max_y * Constants.TILE_SIZE[1]))
if style == Constants.DAWNLIKE_STYLE:
wall_source_file_d = pygame.image.load(Constants.DAWNLIKE_IMAGE_RESOURCE_FOLDER + 'Objects/Wall.png').convert_alpha()
floor_source_file_d = pygame.image.load(Constants.DAWNLIKE_IMAGE_RESOURCE_FOLDER + 'Objects/Floor.png').convert_alpha()
door_closed_source_file = pygame.image.load(Constants.DAWNLIKE_IMAGE_RESOURCE_FOLDER + 'Objects/Door0.png').convert_alpha()
dirt_image = build_floor_tile_dawnlike(floor_source_file_d, 0, 288, Constants.TILE_SIZE)
floor_image = build_floor_tile_dawnlike(floor_source_file_d, 112, 288, Constants.TILE_SIZE)
grass_image = build_floor_tile_dawnlike(floor_source_file_d, 112, 96, Constants.TILE_SIZE)
water_image = build_floor_tile_dawnlike(floor_source_file_d, 224, 288, Constants.TILE_SIZE)
rock_image = build_floor_tile_dawnlike(floor_source_file_d, 224, 96, Constants.TILE_SIZE)
path_image = build_floor_tile_dawnlike(floor_source_file_d, 0, 96, Constants.TILE_SIZE)
wall_image = build_wall_tile_dawnlike(wall_source_file_d, 112, 48, Constants.TILE_SIZE)
for x in range(self.max_x):
for y in range(self.max_y):
destination_pos = (x * Constants.TILE_SIZE[0], y * Constants.TILE_SIZE[1])
if self.map[(x, y)].floor_type == Tile.GRASS:
self.surface_memory.blit(grass_image[self.compute_tile_weight(x, y, Tile.GRASS)],
destination_pos)
elif self.map[(x, y)].floor_type == Tile.WATER:
self.surface_memory.blit(water_image[self.compute_tile_weight(x, y, Tile.WATER)],
destination_pos)
elif self.map[(x, y)].floor_type == Tile.ROCK:
self.surface_memory.blit(rock_image[self.compute_tile_weight(x, y, Tile.ROCK)],
destination_pos)
elif self.map[(x, y)].floor_type == Tile.WALL:
self.surface_memory.blit(wall_image[self.compute_tile_weight(x, y, Tile.WALL)],
destination_pos)
elif self.map[(x, y)].floor_type == Tile.PATH:
self.surface_memory.blit(path_image[self.compute_tile_weight(x, y, Tile.PATH)],
destination_pos)
elif self.map[(x, y)].floor_type == Tile.DIRT:
self.surface_memory.blit(dirt_image[self.compute_tile_weight(x, y, Tile.DIRT)],
destination_pos)
elif self.map[(x, y)].floor_type == Tile.FLOOR:
self.surface_memory.blit(floor_image[self.compute_tile_weight(x, y, Tile.FLOOR)],
destination_pos)
else:
source_file_o = pygame.image.load(Constants.ORYX_IMAGE_RESOURCE_FOLDER + 'oryx_16bit_fantasy_world_trans.png').convert_alpha()
water_image = rock_image = dirt_image = path_image = floor_image = grass_image = build_floor_tile_oryx(source_file_o, 696, 384, Constants.TILE_SIZE)
wall_image = build_wall_tile_oryx(source_file_o, 24, 336, Constants.TILE_SIZE)
for x in range(self.max_x):
for y in range(self.max_y):
destination_pos = (x * Constants.TILE_SIZE[0], y * Constants.TILE_SIZE[1])
if self.map[(x, y)].floor_type == Tile.GRASS:
self.surface_memory.blit(grass_image[self.compute_tile_weight(x, y, Tile.GRASS)],
destination_pos)
elif self.map[(x, y)].floor_type == Tile.WATER:
self.surface_memory.blit(water_image[self.compute_tile_weight(x, y, Tile.WATER)],
destination_pos)
elif self.map[(x, y)].floor_type == Tile.ROCK:
self.surface_memory.blit(rock_image[self.compute_tile_weight(x, y, Tile.ROCK)],
destination_pos)
elif self.map[(x, y)].floor_type == Tile.WALL:
self.surface_memory.blit(wall_image[self.compute_tile_weight(x, y, Tile.WALL)],
destination_pos)
elif self.map[(x, y)].floor_type == Tile.PATH:
self.surface_memory.blit(path_image[self.compute_tile_weight(x, y, Tile.PATH)],
destination_pos)
elif self.map[(x, y)].floor_type == Tile.DIRT:
self.surface_memory.blit(dirt_image[self.compute_tile_weight(x, y, Tile.DIRT)],
destination_pos)
elif self.map[(x, y)].floor_type == Tile.FLOOR:
self.surface_memory.blit(floor_image[self.compute_tile_weight(x, y, Tile.FLOOR)],
destination_pos)