-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.py
146 lines (129 loc) · 7 KB
/
World.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
import pygame
from Tile import Tile
class World:
def __init__(self, background_width, background_height, world_width, world_height, tiles, tile_type_dict):
self.world_width = world_width
self.world_height = world_height
self.tiles = tiles
self.background = pygame.Surface((background_width, background_height))
self.tile_type_dict = tile_type_dict
self.batteries = None
def tile_rotated_absolute(self, tile, rotation_direction):
if tile in ["batteryMain", "batteryNotMain", "batteryOff", ""]:
return tile
tile_append = tile[-1]
tile = tile[0:-1]
compression = {("ud", "lr"): "hor",
("ul", "ur", "dl", "dr"): "turn",
("ulr", "udl", "udr", "dlr"): "3turn",
("udlr",): "4turn"}
decompression = {("hor", "left"): "lr", # Straight tiles
("hor", "right"): "lr",
("hor", "up"): "ud",
("hor", "down"): "ud",
("turn", "left"): "dl", # Turn tiles
("turn", "right"): "ur",
("turn", "up"): "ul",
("turn", "down"): "dr",
("3turn", "left"): "udl", # 3Turn tiles
("3turn", "right"): "udr",
("3turn", "up"): "ulr",
("3turn", "down"): "dlr",
("4turn", "down"): "udlr", # 4Turn tile
("4turn", "left"): "udlr",
("4turn", "up"): "udlr",
("4turn", "right"): "udlr"
}
for (key, value) in compression.items():
if tile in key:
return decompression[(value, rotation_direction)] + tile_append
return "error"
def tile_rotated_relative(self, tile, clockwise):
if tile in ["batteryMain", "batteryNotMain", "batteryOff", ""]:
return tile
clockwiseMoves = {"left": "up",
"up": "right",
"right": "down",
"down": "left"}
tile_direction = self.get_tile_direction(tile)
new_direction = clockwiseMoves[tile_direction]
if not clockwise:
new_direction = clockwiseMoves[clockwiseMoves[new_direction]]
return self.tile_rotated_absolute(tile, new_direction)
def get_tile_direction(self, tile: str):
tile = tile[0:-1]
return {"ud": "up", # Horizontal tiles
"lr": "right",
"ul": "up", # Turn tiles
"ur": "right",
"dr": "down",
"dl": "left",
"ulr": "up", # 3turn tiles
"udr": "right",
"dlr": "down",
"udl": "left",
"udlr": "up" # 4turn tiles
}[tile]
def update_batteries_and_connections(self):
for row in self.tiles:
for tile in row:
if 'p' in tile.tile_type.name:
tile.tile_type = self.tile_type_dict[tile.tile_type.name[0:-1] + 'n']
elif tile.tile_type.name == "batteryNotMain":
tile.tile_type = self.tile_type_dict["batteryOff"]
for battery in self.batteries:
if battery.x == tile.tx and battery.y == tile.ty:
battery.on = False
break
return_value = (0, 0)
for row in self.tiles:
for tile in row:
if tile.tile_type.name == "batteryMain":
battery_locations = self.find_connected_battery_locations(tile.tx, tile.ty)
for (mx, my) in battery_locations:
if self.tiles[mx][my].tile_type.name == "batteryEnd":
return_value = (mx, my)
if self.tiles[mx][my].tile_type.name == "batteryMain" or self.tiles[mx][my].tile_type.name == "batteryNotMain":
continue
if self.tiles[mx][my].tile_type.name == "batteryOff":
self.tiles[mx][my] = Tile(mx * 50, my * 50, self.tile_type_dict["batteryNotMain"], tile.world_surface)
for battery in self.batteries:
if battery.x == mx and battery.y == my:
battery.on = True
break
conductors = self.find_connected_battery_locations_and_conductors(tile.tx, tile.ty)
for (mx, my) in conductors:
if "battery" in self.tiles[mx][my].tile_type.name:
continue
if 'n' in self.tiles[mx][my].tile_type.name:
self.tiles[mx][my].tile_type = self.tile_type_dict[self.tiles[mx][my].tile_type.name[0:-1] + 'p']
return return_value
def find_connected_battery_locations(self, x, y):
checked_tile_locations = []
self.find_connected_battery_locations_recursive(x, y, checked_tile_locations)
final_list = []
for (x, y) in checked_tile_locations:
if "battery" in self.tiles[x][y].tile_type.name:
final_list.append((x, y))
return final_list
def find_connected_battery_locations_and_conductors(self, x, y):
checked_tile_locations = []
self.find_connected_battery_locations_recursive(x, y, checked_tile_locations)
return checked_tile_locations
def find_connected_battery_locations_recursive(self, x, y, checked_tile_locations):
if (x, y) in checked_tile_locations:
return
my_tile = self.tiles[x][y]
checked_tile_locations.append((x, y))
# tiles that can conduct power
if 'n' in my_tile.tile_type.name or 'p' in my_tile.tile_type.name or "battery" in my_tile.tile_type.name:
if "up" in my_tile.tile_type.accessible_to and y - 1 >= 0 and "down" in self.tiles[x][y - 1].tile_type.accessible_from:
self.find_connected_battery_locations_recursive(x, y - 1, checked_tile_locations)
if "left" in my_tile.tile_type.accessible_to and x - 1 >= 0 and "right" in self.tiles[x - 1][y].tile_type.accessible_from:
self.find_connected_battery_locations_recursive(x - 1, y, checked_tile_locations)
if "right" in my_tile.tile_type.accessible_to and x + 1 < self.world_width and "left" in self.tiles[x + 1][y].tile_type.accessible_from:
self.find_connected_battery_locations_recursive(x + 1, y, checked_tile_locations)
if "down" in my_tile.tile_type.accessible_to and y + 1 < self.world_height and "up" in self.tiles[x][y + 1].tile_type.accessible_from:
self.find_connected_battery_locations_recursive(x, y + 1, checked_tile_locations)
else:
return