-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathFinding.py
288 lines (196 loc) · 8.33 KB
/
PathFinding.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
import pygame
from pathfinding.core.grid import Grid
from pathfinding.finder.a_star import AStarFinder
import csv
import random
import json
import Map
import ImageUtil
from Constants import *
import City
import Company
import datetime
import Notification
# 0 1 2 3 4 5 6
# STRUCTURE: [(currentCordX, currentCoordY), typeID, targetPosNode, Capacity, AmountFill, path, pathindex]
LiveVehicles = []
# 0 1 2(0=TL,1=TR,2=BR,3=BL) 3 4
# STRUCTURE: [id, (name, capacity, images, cost, monthly-cost)]
VehicleTypes = {}
finder = AStarFinder()
CurrentTick = 0
MaxTick = 20
LastBillTime = None
def initVehicleTypes():
global VehicleTypes
with open("vehicle.json") as jsonFile:
tempData = json.load(jsonFile)
for val in tempData:
images = (tempData[val]["image-name-TL"], tempData[val]["image-name-TR"], tempData[val]["image-name-BR"], tempData[val]["image-name-BL"])
VehicleTypes[tempData[val]["id"]] = (tempData[val]["name"], tempData[val]["capacity"], images, tempData[val]["cost"], tempData[val]["monthly-cost"])
def createVehicle(id):
global VehicleTypes
global LiveVehicles
startCoords = getNewRoadTarget()
randomTarget = getNewRoadTarget()
path = getPath(startCoords, randomTarget)
vehicle = [(startCoords[0], startCoords[1]), id, randomTarget, VehicleTypes[id][0], 0, path, 0]
LiveVehicles.append(vehicle)
def tick():
global LiveVehicles
global CurrentTick
global VehicleTypes
global MaxTick
global Mon
global LastBillTime
TotalMonthlyCost = 0
if(CurrentTick >= MaxTick):
for vehicle in LiveVehicles:
currentpos = vehicle[0]
path = vehicle[5]
pathindex = vehicle[6]
endpos = vehicle[2]
vehicleType = VehicleTypes[vehicle[1]]
#print("Current Pos: " + str(currentpos) + " End Pos: " + str(endpos) + " Path Length: " + str(len(path)) + " Path Index: " + str(pathindex))
TotalMonthlyCost += vehicleType[4]
#ADD GARBAGE IF NEXT TO HOUSE
if(vehicle[4] < vehicleType[1]):
if(Map.isHouseTile(currentpos[0]+1,currentpos[1]) or Map.isHouseTile(currentpos[0]-1, currentpos[1]) or Map.isHouseTile(currentpos[0], currentpos[1]-1) or Map.isHouseTile(currentpos[0], currentpos[1]+1)):
#print("HOUSE NEXT TO")
rand = random.randint(0, RANDOM_CHANCE_OF_GARBAGE_GET)
if(rand == 0 and City.AmoutOfTrash >= 1 ):
vehicle[4] += 1
Company.Money += 10
City.AmoutOfTrash -= 1
#print("Trash: " + str(vehicle[4]) + " CITY TRASH: " + str(City.AmoutOfTrash))
#IF IT HAS REACHED THE END OF ITS RANDOM PATH GENERATE A NEW ONE AND DUMP TRASH IF HAS SOME
if((currentpos[0] == endpos[0] and currentpos[1] == endpos[1]) or endpos[0] == None or len(path) <= 0 or pathindex >= len(path)):
#IF AT LANDFILL
if(Map.getTile(currentpos[0], currentpos[1]) == Map.TILES["landfill"]):
landfill = Map.Landfillgroups[Map.LandfillTiles[(currentpos[0],currentpos[1])]]
spaceLeft = landfill[1] - landfill[0]
if spaceLeft >= vehicle[4]:
landfill[0] += vehicle[4]
vehicle[4] = 0
else:
landfill[0] += spaceLeft
vehicle[4] -= spaceLeft
endpos = None
path = None
if(vehicle[4] >= vehicleType[1]):
newfill = getNewLandfillTarget()
if(newfill == None):
vehicle[2] = getNewRoadTarget()
#print("NO Landfill")
Notification.addNotification("Citizens worry as full garbage trucks roam the city without a landfill to go to!")
else:
vehicle[2] = newfill
#print("yay landfill!")
else:
vehicle[2] = getNewRoadTarget()
startpos = currentpos
path = getPath(startpos, vehicle[2])
if(len(path) == 0):
Notification.addNotification("Truck drivers begin a trip to insanity as they can't find a road to a landfill!")
#print(path)
vehicle[5] = path
vehicle[6] = 0
Map.testTruckEnd = endpos
continue
#SET POSIITON TO NEXT POOSITION IN PATH
vehicle[0] = [path[pathindex][0], path[pathindex][1]]
vehicle[6] += 1
#MONTHLY COSTS
currentTime = datetime.datetime.now()
if(LastBillTime == None):
LastBillTime = currentTime
if((currentTime - LastBillTime).total_seconds() >= SECONDS_PER_MONTH):
Company.Money -= TotalMonthlyCost
LastBillTime = currentTime
CurrentTick = 0
else:
CurrentTick += 1
def render(screen):
global LiveVehicles
for vehicle in LiveVehicles:
renderVehicle(vehicle, screen)
def renderVehicle(vehicle, screen):
currentpos = vehicle[0]
images = VehicleTypes[vehicle[1]][2]
image = ImageUtil.get_image(images[3])
isoPos = Map.getScreenPositionOfCoord(currentpos[0], currentpos[1])
if(vehicle[6] < len(vehicle[5])):
nextpos = vehicle[5][vehicle[6]]
#pygame.draw.rect(screen, (255,0,0), [isoPos[0]-25, isoPos[1]-25, 50, 50])
if(nextpos[0] > currentpos[0]):
image = ImageUtil.get_image(images[1])
elif(nextpos[0] < currentpos[0]):
image = ImageUtil.get_image(images[3])
elif(nextpos[1] > currentpos[1]):
image = ImageUtil.get_image(images[2])
elif(nextpos[1] < currentpos[1]):
image = ImageUtil.get_image(images[0])
isoPos[0] += (TILE_WIDTH/2 - image.get_width()/2)
isoPos[1] -= image.get_height()/2
screen.blit(image, isoPos)
"""
returns vehicle if there is one in that tile
if not it returns false
"""
def tileHasVehicle(x, y):
global LiveVehicles
for vehicle in LiveVehicles:
if(vehicle[0][0] == x and vehicle[0][1] == y):
return vehicle
return False
#---------------------------------PATH FINDING FUNCTIONS-------------------------------------
"""
gets the path from start pos to end on PATHMAP. returns a list of positions (AKA move to this pos next)
"""
def getPath(start, end):
RoadMap = Map.PATHMAP
global finder
grid = Grid(matrix = RoadMap)
start = grid.node(start[0], start[1])
end = grid.node(end[0], end[1])
path, runs = finder.find_path(start, end, grid)
#print(grid.grid_str(path=path, start=start, end=end))
return path
"""
Returns a random spot on PATHMAP where there is a road
"""
def getNewRoadTarget():
RoadMap = Map.PATHMAP
returns = [0,0]
while RoadMap[returns[0]][returns[1]] != '1':
returns = random.choice([(j,i) for i, row in enumerate(RoadMap) for j, val in enumerate(row) if val=='1'])
return returns
def getNewLandfillTarget():
RoadMap = Map.PATHMAP
returns = [0,0]
if(any('2' in sub for sub in RoadMap)):
print("there is a 2")
returns = random.choice([(j,i) for i, row in enumerate(RoadMap) for j, val in enumerate(row) if val=='2'])
else:
return None
return returns
#--------- OLD CODE USED FOR TESTING
"""
def run():
matrix = None
with open("maps/pathfindingtestmap.csv") as csvmap:
reader = csv.reader(csvmap)
matrix = list(reader)
grid = Grid(matrix = matrix)
start = grid.node(1,4)
end = grid.node(26,13)
print(len(matrix))
while(matrix[start.x][start.y] != '1'):
start = grid.node(random.randint(0, len(matrix[0]))-1, random.randint(0, len(matrix))-1)
finder = AStarFinder()
path, runs = finder.find_path(start, end, grid)
print("RUNS: ", runs, "path length: ", len(path))
print(grid.grid_str(path=path, start=start, end=end))
print(path[0])
print(path[1])
"""