-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainClasses.py
1598 lines (1443 loc) · 68.3 KB
/
mainClasses.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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from abc import ABC, abstractmethod
import random
import copy
import math
import pygame
import pygame.gfxdraw
import TimeClass as Time
from datetime import datetime, timedelta
pygame.init()
COLOURS = {"background": (16, 51, 158),
"land": (63, 104, 81), # asli = (155, 118, 83)
# "land": (110, 83, 61),
"blackInside": (45, 45, 45),
"whiteOutline": (255, 255, 255),
"lines": ((9, 254, 25),
(230, 80, 40),
(254, 201, 9),
(59, 161, 210),
(55, 135, 48),
(242, 145, 173),
(140, 84, 42))}
CIRCLE = 0
TRIANGLE = 1
SQUARE = 2
DIAMOND = 3
TRAPEZOID = 4
PARLLELOGRAM = 5
PENTAGON = 6
HEXAGON = 7
STAR = 8
CONTAINER = 0
LINE = 1
BOAT = 2
TRUCK = 3
# units are in world pixels
STOP_REMOVAL_DISTANCE = 10 # distance mouse must be to a stop to remove it from a line
STOP_ADDITION_DISTANCE = 5 # distance mouse must be to a stop to add it to a line
ENDPOINT_SEGMENT_DISTANCE = 60 # distance mouse must be to grab an endpoint segment
STOP_DISTANCE = 50 # minimum spacing between any two given stops
LOSE_DURATION = 45 # amount of time stop has to overcrowd to cause the game to be over
RESOURCE_GAIN_DELAY = 90 # time between each resource gain event
def _isValidSpawn(x, y, stops, mapSurface):
# Returns True or False depending on whether or not the given
# point (x, y) is a valid stop location on the given map
land_color = COLOURS.get("land")
current_color = tuple(mapSurface.get_at((x, y))[:3])
if (land_color[0]-5 <= current_color[0] <= land_color[0]+5 and
land_color[1]-5 <= current_color[1] <= land_color[1]+5 and
land_color[2]-5 <= current_color[2] <= land_color[2]+5):
for stop in stops:
if stop.withinRadius(x, y, STOP_DISTANCE):
return False
return True
return False
def findDistance(point1, point2):
""" ((num, num), (num, num)) -> float
Returns the shortest distance between the two points
(x1, y1) and (x2, y2).
"""
x1, y1 = point1
x2, y2 = point2
return math.sqrt((x1-x2)**2 + (y1-y2)**2)
def getViewCoords(x, y, offset):
""" (num, num, list) -> (num, num)
Returns (x, y) the point in view space that corresponds to the
given coordinates "x" and "y" in world space, using the offset
provided.
Offset is [[world scale x, world scale y],
[world translation x, world translation y]]
"""
return [(x-offset[1][0])*offset[0][0], (y-offset[1][1])*offset[0][1]]
class Weather(ABC):
@abstractmethod
def __init__(self, worldSurface, radius, speed_radius, spawn_interval):
self.worldSurface = worldSurface
self.radius = radius
self.speed_radius = speed_radius
self.spawn_interval = spawn_interval
@abstractmethod
def spawn(self):
pass
class Windy(Weather):
def __init__(self, worldSurface, radius, speed_radius, spawn_interval):
super().__init__(worldSurface, radius, speed_radius, spawn_interval)
self.color = (255, 255, 255, 10)
self.x = 0
self.y = 0
self.last_draw_time = 0
self.__windy = pygame.image.load("assets/weather/windy.png")
self.__windy = pygame.transform.scale(self.__windy, (100, 100))
self.__windy_x = 0
self.__windy_y = 0
def spawn(self, targetSurface, offset):
now = pygame.time.get_ticks()
time_since_last_draw = now - self.last_draw_time
# spawn a new circle if enough time has passed
if time_since_last_draw >= self.spawn_interval:
# spawn 60% in the middle
if random.random() <= 0.6:
self.x = targetSurface.get_width() // 2
self.y = targetSurface.get_height() // 2
else:
self.x = random.randint(
self.radius, min(800, targetSurface.get_width()) - self.radius)
self.y = random.randint(
self.radius, min(600, targetSurface.get_height()) - self.radius)
# spawn a new circle if enough time has passed
if time_since_last_draw >= self.spawn_interval:
self.last_draw_time = now
# convert the world coordinates of the circle to view coordinates
circleView = getViewCoords(self.x, self.y, offset)
# draw the circle onto the targetSurface
pygame.draw.circle(targetSurface, self.color,
circleView, self.radius)
# draw the windy ihn the middle of the circle
self.__windy_x = circleView[0] - self.__windy.get_width() // 2
self.__windy_y = circleView[1] - self.__windy.get_height() // 2
targetSurface.blit(self.__windy, (self.__windy_x, self.__windy_y))
class Rainy(Weather):
def __init__(self, worldSurface, radius, speed_radius, spawn_interval):
super().__init__(worldSurface, radius, speed_radius, spawn_interval)
self.color = (148, 149, 153)
self.x = 0
self.y = 0
self.last_draw_time = 0
self.__rainy = pygame.image.load("assets/weather/rain.png")
self.__rainy = pygame.transform.scale(self.__rainy, (100, 100))
self.__rainy_x = 0
self.__rainy_y = 0
def spawn(self, targetSurface, offset):
now = pygame.time.get_ticks()
time_since_last_draw = now - self.last_draw_time
# spawn a new circle if enough time has passed
if time_since_last_draw >= self.spawn_interval:
self.x = random.randint(
self.radius, min(800, targetSurface.get_width()) - self.radius)
self.y = random.randint(
self.radius, min(600, targetSurface.get_height()) - self.radius)
self.last_draw_time = now
# convert the world coordinates of the circle to view coordinates
circleView = getViewCoords(self.x, self.y, offset)
# draw the circle onto the targetSurface
pygame.draw.circle(targetSurface, self.color,
circleView, self.radius)
# draw the light rain in the middle of the circle
self.__rainy_x = circleView[0] - self.__rainy.get_width() // 2
self.__rainy_y = circleView[1] - self.__rainy.get_height() // 2
targetSurface.blit(self.__rainy, (self.__rainy_x, self.__rainy_y))
# spawn after 1 minutes
class Storm(Rainy, Windy):
def __init__(self, first_minutes, **kwargs):
super().__init__(**kwargs)
self.color = (148, 149, 153)
self.x = 0
self.y = 0
self.last_draw_time = datetime.min
self.__storm = pygame.image.load("assets/weather/strom.png")
self.__storm = pygame.transform.scale(self.__storm, (100, 100))
self.__storm_x = 0
self.__storm_y = 0
self.first_minutes = first_minutes
self.first_minute_passed = False
self.warning_displayed = False
self.warning_time = None
self.game_start_time = datetime.now()
# override spawn method from Rainy and Windy
def spawn(self, targetSurface, offset):
now = datetime.now()
if not self.first_minute_passed:
# spawn after first minutes have passed
if now - self.game_start_time >= timedelta(minutes=self.first_minutes):
self.first_minute_passed = True
self.warning_displayed = False
self.warning_time = None
else:
return
time_since_last_draw = now - self.last_draw_time
if time_since_last_draw >= timedelta(milliseconds=self.spawn_interval):
# 40% spawn in the middle
if random.random() <= 0.4:
self.x = targetSurface.get_width() // 2
self.y = targetSurface.get_height() // 2
else:
self.x = random.randint(
self.radius, min(800, targetSurface.get_width()) - self.radius)
self.y = random.randint(
self.radius, min(600, targetSurface.get_height()) - self.radius)
self.last_draw_time = now
circleView = getViewCoords(self.x, self.y, offset)
pygame.draw.circle(targetSurface, self.color,
circleView, self.radius)
self.__storm_x = circleView[0] - 50
self.__storm_y = circleView[1] - 50
targetSurface.blit(self.__storm, (self.__storm_x, self.__storm_y))
# display warning message for 5 seconds
if not self.warning_displayed and now - self.game_start_time >= timedelta(minutes=self.first_minutes):
self.warning_displayed = True
self.warning_time = now + timedelta(seconds=5)
if self.warning_time is not None and now <= self.warning_time:
font = pygame.font.SysFont(None, 40)
text = font.render(
"Warning: Badai Mendekat!", True, (255, 0, 0))
text_rect = text.get_rect(
center=(targetSurface.get_width() // 2, 50))
targetSurface.blit(text, text_rect)
class World(object):
def __init__(self, mapSurface, stopSize=30, cargoSize=10):
self.stops = []
self.lines = []
self.boats = []
self.containers = []
self.boatSpeed = 1
self._map = mapSurface
self.stopSize = stopSize
self.cargoSize = cargoSize
self.width = mapSurface.get_width()
self.height = mapSurface.get_height()
self.validStopDistanceX = 250
self.validStopDistanceY = int(self.validStopDistanceX
* (float(self.height)/self.width))
# give the player some starting equipment
self.resources = [1, 2, 3, 10]
self.totalTrucks = self.resources[TRUCK]
self.iconHitboxes = [None] * 4
self.cargosMoved = 0
def boat_slow_storms(self, storms):
for boat in self.boats:
speed_reduction = self.boatSpeed
for storm in storms:
dist = math.sqrt((boat._x - storm.x)**2 +
(boat._y - storm.y)**2)
if dist <= storm.speed_radius:
speed_reduction = min(speed_reduction, self.boatSpeed / 6)
boat._speed = speed_reduction # update the boat's speed
def boat_speed_windys(self, windys):
for boat in self.boats:
for windy in windys:
dist = math.sqrt((boat._x - windy.x) ** 2 +
(boat._y - windy.y) ** 2)
if dist <= windy.speed_radius and boat._speed < self.boatSpeed * 3:
boat._speed = self.boatSpeed * 3
def boat_slow_rain(self, circle):
for boat in self.boats:
dist = math.sqrt((boat._x - circle.x)** 2 +
(boat._y - circle.y)**2)
if dist <= circle.speed_radius and boat._speed > self.boatSpeed / 3:
boat._speed = self.boatSpeed / 3
def addRandomStop(self, shape, stopSurfaces):
""" (int, list) -> bool, bool
Creates a stop of the given shape at a random but valid
location on the map.
Returns two booleans: the first dictates if the map area
was expanded, and the second tells whether or not the
maximum map area has been reached.
"""
# makes shape in random valid location
count = 0
x = random.randint(self.width/2-self.validStopDistanceX+self.cargoSize*6,
self.width/2+self.validStopDistanceX-self.cargoSize*6)
y = random.randint(self.height/2-self.validStopDistanceY+self.stopSize*3,
self.height/2+self.validStopDistanceY-self.stopSize*3)
# try 15 times to generate a valid stop
while (not _isValidSpawn(x, y, self.stops, self._map)) and count < 15:
x = random.randint(self.width/2-self.validStopDistanceX+self.cargoSize*6,
self.width/2+self.validStopDistanceX-self.cargoSize*6)
y = random.randint(self.height/2-self.validStopDistanceY+self.stopSize*3,
self.height/2+self.validStopDistanceY-self.stopSize*3)
count = count+1
if count < 15:
timer = Time.Time(Time.MODE_STOPWATCH,
Time.FORMAT_TOTAL_SECONDS, 0)
self.stops.append(Stop(x, y, shape, stopSurfaces, timer))
return False, False
self.validStopDistanceX = self.validStopDistanceX+50
if self.validStopDistanceX >= self.width/2:
self.validStopDistanceX = self.width/2
self.validStopDistanceY = int(self.validStopDistanceX
* (float(self.height)/self.width))
return False, True
self.validStopDistanceY = int(self.validStopDistanceX
* (float(self.height)/self.width))
return True, False
def switchRandomStop(self, shapeRange, existingStops, worldSurface):
""" (int) -> int
Picks a random stop (circle, triangle, or square) and
changes the shape of the stop to a special one.
Returns -1 if no conversion was made or the shape the stop
was converted to as an int.
"""
# try 10 times to get a random shape that isn't used
count = 0
newShape = random.choice(shapeRange)
while newShape in existingStops and count < 10:
newShape = random.choice(shapeRange)
count = count+1
if count == 10:
return -1
# try 10 times to find a non-special stop to switch
count = 0
newStop = self.stops[random.randint(0, len(self.stops)-1)]
while newStop.shape > SQUARE and count < 10:
newStop = self.stops[random.randint(0, len(self.stops)-1)]
count = count+1
if count == 10:
return -1
newStop.shape = newShape
for line in newStop.lines:
line.update(worldSurface, False)
return newShape
def createNewLine(self, mouseObject, stop):
""" (MousePosition, Stop) -> int
Creates a new line with a starting mouse segment anchored
at the provided stop and MousePosition object.
Returns the index of the line.
"""
availableLines = [0, 1, 2, 3, 4, 5, 6]
for line in self.lines:
availableLines.remove(line.LINE_NUMBER)
if len(availableLines) == 0:
return -1
newLine = availableLines[0]
line = Line(newLine)
self.lines.insert(newLine, line)
line.isMoving = True
line.createMouseSegments(-1, mouseObject, None, stop)
return newLine
def getClickedLine(self, colour):
""" (tuple) -> int
"colour" is a tuple of length 3 (R, G, B).
Returns the index of the line with the colour given, or
-1 if no line has that colour.
"""
# there must be a check to see if the colour is in the line list first because
# the Tuple.index method throws an exception if it is not in it
if colour in COLOURS.get("lines"):
return COLOURS.get("lines").index(colour)
return -1
def getSegmentFromWorld(self, mouseObject, offset):
# as opposed to getting a segment from a specific line
clickedSegments = []
for i, _ in enumerate(self.lines):
for j, item in enumerate(self.lines[i].segments):
if item.rect.collidepoint(mouseObject.getView(offset)):
clickedSegments.append([i, j])
if len(clickedSegments) > 1:
lowestDistance = [1000000, [-1, -1]]
for segment in clickedSegments:
distanceScore = (self.lines[segment[0]].segments[segment[1]]
.getDistanceScore(mouseObject.getWorld()))
if distanceScore < lowestDistance[0]:
lowestDistance = [distanceScore, segment]
return lowestDistance[1]
if len(clickedSegments) == 1:
return clickedSegments[0]
return -1
def getLineByHitbox(self, mouseObject, offset):
# as opposed to getting the clicked line by colour
segment = self.getSegmentFromWorld(mouseObject, offset)
if segment != -1:
return segment[0]
return -1
def getClickedIcon(self, mouseView):
for i, item in enumerate(self.iconHitboxes):
if item.collidepoint(mouseView):
return i
return -1
def getClickedBoatLine(self, colour):
# get the line that the clicked boat is on
for i, item in enumerate(self.lines):
if colour == item.BRIGHTER_COLOUR:
return i
return -1
def getClickedBoat(self, mouseView, line):
# get a boat or container on a line
boats = self.lines[line].boats
for boat in boats:
if boat.rect.collidepoint(mouseView):
return boat, "boat"
for container in boat.containers:
if container.rect.collidepoint(mouseView):
return container, "container"
for abandonedLine in self.lines[line].abandonedChildren:
for boat in abandonedLine.boats:
if boat.rect.collidepoint(mouseView):
return boat, "boat"
for container in boat.containers:
if container.rect.collidepoint(mouseView):
return container, "container"
return -1
def removeLine(self, index):
# remove a line and everything on it
childLines = self.lines[index].abandonedChildren
for i in range(len(childLines)-1, -1, -1):
for j in range(len(childLines[i].boats)-1, -1, -1):
for container in childLines[i].boats[j].containers:
self.resources[CONTAINER] = self.resources[CONTAINER]+1
self.containers.remove(container)
childLines[i].boats[j].containers.remove(container)
container.remove()
self.boats.remove(childLines[i].boats[j])
self.resources[BOAT] = self.resources[BOAT]+1
childLines[i].boats[j].remove()
childLines[i].boats.pop(j)
self.lines[index].abandonedChildren.pop(i)
self.lines.pop(index)
self.resources[LINE] = self.resources[LINE]+1
class Stop(object):
def __init__(self, x, y, shape, surfaces, timer):
self._STOP_SURFACES = surfaces
self.X = x
self.Y = y
self.shape = shape
self.cargos = []
self.timer = timer
self.usingTimer = False
self.timer.toggleActive()
self.boats = [] # boats stopped at the stop
self.lines = [] # lines that pass through this stop
def __eq__(self, other):
# overrided definition of Stop == Stop
# if the coordinates of both stops are equal, the
# stops are considered equal
return self.X == other.X and self.Y == other.Y
def withinRadius(self, x, y, radius):
""" (int, int, int) -> bool
Returns if the point at (x, y) is less than "radius" pixels
away (in world space) from the stop "self"
"""
return (x-self.X)**2 + (y-self.Y)**2 < radius**2
def getPosition(self):
""" (None) -> num, num
Returns the world position of the stop as a tuple (x, y).
"""
return self.X, self.Y
def draw(self, targetSurface, size, cargoSize, offset):
""" (pygame.Surface, int, int, list) -> None
Draws the stop "self" onto "targetSurface", as well as any
cargos at that stop.
Offset is a list containing the scale for x and y as well
as the translation for x and y required to transform world
coordinates into view coordinates.
"""
# convert the world coordinates of the stop to view coordinates
stopView = getViewCoords(self.X, self.Y, offset)
stopView[0] = stopView[0]-size/2
stopView[1] = stopView[1]-size/2
targetSurface.blit(self._STOP_SURFACES[self.shape], stopView)
self.rect = pygame.Rect(stopView[0], stopView[1], size, size)
if self.usingTimer:
self.timer.tick()
width = self._STOP_SURFACES[self.shape].get_width()*2
stop = pygame.Surface((width, width))
stop.blit(self._STOP_SURFACES[self.shape],
(width/2-size/2, width/2-size/2))
# draw the red fill the changes the stop colour
# the pygame.draw.arc() function leaves some pixels empty which causes it to look
# bad, but there is no other (easy and simple) way to do this
pie = pygame.Surface((width, width))
width = max(pie.get_width(), pie.get_height())
pygame.draw.arc(pie,
(255, 45, 45),
pie.get_rect(),
math.pi/2.0,
math.pi/2.0 +
(2*math.pi*(max(self.timer.time, 0)/LOSE_DURATION)),
int(width/2))
stop.blit(pie, (0, 0), None, pygame.BLEND_MIN)
stop.set_colorkey((0, 0, 0))
targetSurface.blit(stop, (stopView[0]-size/2, stopView[1]-size/2))
self.value = max(self.timer.time, 0)/LOSE_DURATION
# gambar ! if the stop about to be full
if self.value > 0.1 and self.value < 0.99:
font = pygame.font.SysFont("monospace", 30)
label = font.render("!", 1, (255, 45, 45))
targetSurface.blit(label,
(stopView[0]-size/2, stopView[1]-size/2))
# pojok kiri atas
font = pygame.font.SysFont("monospace", 30)
label = font.render("!", 1, (255, 45, 45))
targetSurface.blit(label, (0, 0))
for i, item in enumerate(self.cargos):
# draw the cargo to the side of the stop, in rows of 6
# (so if a 7th cargo spawns, it'll appear in another row)
item.draw(targetSurface,
cargoSize,
stopView[0] + size*1.4 +
(i % 6)*cargoSize,
stopView[1] + (i/6)*cargoSize)
def addRandomCargo(self, shapes, cargoSurfaces):
""" (int, list) -> None
Creates a cargo of the given shape (given by an
integer 0-8) at this stop.
"""
shapes = list(shapes)
shapes.remove(self.shape)
self.cargos.append(
Cargo(random.choice(shapes), cargoSurfaces))
def processBoat(self, boat, boatsToMove):
# load or unload cargos that can move
for container in boat.containers:
if container.movingClone in boatsToMove:
return self.moveCargo(boat, True)
if boat in boatsToMove or boat.movingClone in boatsToMove:
return self.moveCargo(boat, True)
return self.moveCargo(boat, False)
def isValidTransfer(self, path, transfer):
# if the transfer's line has already been visited,
# ignore it
for step in path:
if transfer[1] == step[1]:
return False
return True
def findPath(self, currentLine, cargo, path):
# recursively finds the first path to the target stop
# not the fastest path but it doesn't matter
# for a game like this
if cargo.SHAPE in currentLine.stopNums:
index = currentLine.stopNums.index(cargo.SHAPE)
path.append([index, currentLine])
return path
for transfer in currentLine.transfers:
if self.isValidTransfer(path, transfer):
newPath = list(path)
newPath.append(transfer)
foundPath = self.findPath(transfer[1], cargo, newPath)
if foundPath != -1:
return foundPath
return -1
def findValidCargo(self, boat):
for i, item in enumerate(self.cargos):
# first, see if the stop it wants to go to
# is reachable by boat without the boat needing
# to reverse direction
foundPath = len(item.path) > 0
if (boat.direction == 1
and (item.SHAPE in boat.line.stopNums[boat.segmentNum:]
or (foundPath
and item.path[1][0] > boat.segmentNum
and boat in item.path[0][1].boats))):
return i
if (boat.direction == -1
and (item.SHAPE in boat.line.stopNums[:boat.segmentNum+1]
or (foundPath
and item.path[1][0] <= boat.segmentNum
and boat in item.path[0][1].boats))):
return i
# then, check all lines directly accessible to the cargo/stop
if not foundPath:
for line in self.lines:
if item.SHAPE in line.stopNums:
foundPath = True # do nothing, wait until the right boat comes
# finally, if all else fails, find a path along the whole network
if not foundPath:
for line in self.lines:
path = [[-1, line]]
path = self.findPath(line, item, list(path))
if path != -1:
item.path = path
if (boat.direction == 1
and item.path[1][0] > boat.segmentNum):
return i
if (boat.direction == -1
and item.path[1][0] <= boat.segmentNum):
return i
return -1
def moveCargo(self, boat, shouldUnload):
# move a single cargo
index = -1
for i, item in enumerate(boat.cargos):
if (self.shape == item.SHAPE
or (len(item.path) > 0
and item.path[1][1] in self.lines)):
index = i
if index > -1:
# if a cargo was found that can be moved off the boat, move it
if len(boat.cargos[index].path) > 0:
boat.cargos[index].path.pop(0)
if boat.cargos[index].path[-1][1] in self.lines:
boat.cargos[index].path = []
self.cargos.append(boat.cargos.pop(index))
return 0
boat.cargos.pop(index)
return 1 # one cargo has been moved
elif shouldUnload:
# if the boat or container should be moved to another line, it
# can't have any cargos on it, so unload move them off
if len(boat.cargos) > 0:
self.cargos.append(boat.cargos.pop())
return 0
# self.cargos.append(boat.cargos.pop())
# return 0
index = self.findValidCargo(boat)
# if a cargo was found that can be moved onto the boat, move it
if (index > -1
and len(boat.cargos) < (len(boat.containers)*6)+6):
boat.cargos.append(self.cargos.pop(index))
else:
# no cargos can be moved
if boat.setMoving(True):
self.boats.remove(boat)
return 0
class Cargo(object):
def __init__(self, shape, surfaces):
self.__CARGO_SURFACES = surfaces
self.SHAPE = shape
self.path = []
def draw(self, targetSurface, size, x, y):
centerX = x - size/2
centerY = y - size/2
targetSurface.blit(self.__CARGO_SURFACES[self.SHAPE],
(centerX, centerY))
class Line(object):
def __init__(self, lineNumber):
# lineNumber is 0-6 (inclusive, for 7 lines maximum)
self.LINE_NUMBER = lineNumber
self._COLOUR = COLOURS.get("lines")[lineNumber]
self.BRIGHTER_COLOUR = (min(self._COLOUR[0]+50, 255),
min(self._COLOUR[1]+50, 255),
min(self._COLOUR[2]+50, 255))
self.DARKER_COLOUR = (max(self._COLOUR[0]-50, 0),
max(self._COLOUR[1]-50, 0),
max(self._COLOUR[2]-50, 0))
# holds lines that were split from this parent line
# and are considered "abandoned". all boats on a
# segment that was split into a child line get
# moved to the child line and the moment they leave,
# the abandoned line is deleted
self.abandonedChildren = []
self.isAbandoned = False
self.segments = []
self.stopNums = [] # numeric values of the shape of stops on the line
self.transfers = [] # for cargo pathfinding (along with stopNums)
# temporary list used to edit the line before commiting changes
self.tempSegments = []
self._newStops = []
self._removedStops = []
self._abandonedSegments = []
self.mouseSegments = []
self.isMoving = False # if the mouse is moving the line
self.boats = []
def draw(self, targetSurface, width, offset):
for segment in self.tempSegments+self.segments:
if segment.isAbandoned:
segment.draw(targetSurface, self.DARKER_COLOUR, width, offset)
else:
segment.draw(targetSurface, self._COLOUR, width, offset)
if self.isMoving:
for mouseSegment in self.mouseSegments:
mouseSegment.draw(targetSurface, self.BRIGHTER_COLOUR, offset)
def _abandonSegment(self, segmentIndex):
self.tempSegments[segmentIndex].isAbandoned = True
self._abandonedSegments.append(self.tempSegments[segmentIndex])
def getClickedSegment(self, mouseView, mouseObject):
# determine which segment of the line is being clicked on
intersectingSegments = []
for i, item in enumerate(self.segments):
if item.rect.collidepoint(mouseView):
intersectingSegments.append(i)
# if only one approximate rectangle is clicked,
# pick that segment. if multiple approximate rectangles
# are clicked, use a more precise detection:
if len(intersectingSegments) > 1:
# holds the lowest score and the index of
# the segment with that score
lowestDistance = [10000000, -1]
for i, item in enumerate(intersectingSegments):
distanceScore = (self.segments[item]
.getDistanceScore(mouseObject.getWorld()))
if distanceScore < lowestDistance[0]:
lowestDistance = [distanceScore, item]
intersectingSegments[0] = lowestDistance[1]
if len(intersectingSegments) > 0:
# sometimes the rectangles pygame returns do not cover
# the full line, so make sure there is an intersection
# in the list before returning
return intersectingSegments[0]
return -1
def createMouseSegments(self, segment, mouseObject, stop1, stop2):
self.tempSegments = list(self.segments)
if (segment == 0
and findDistance(mouseObject.getWorld(),
stop1.getPosition()) < ENDPOINT_SEGMENT_DISTANCE):
self.mouseSegments.append(MouseSegment(stop1,
mouseObject,
-len(self.segments),
"before"))
elif (segment == len(self.segments)-1
and findDistance(mouseObject.getWorld(),
stop2.getPosition()) < ENDPOINT_SEGMENT_DISTANCE):
self.mouseSegments.append(MouseSegment(stop2,
mouseObject,
segment,
"after"))
else:
self._abandonSegment(segment)
self.mouseSegments.append(MouseSegment(stop1,
mouseObject,
segment-1,
"after"))
self.mouseSegments.append(MouseSegment(stop2,
mouseObject,
-len(self.segments) +
segment+1,
"before"))
def update(self, worldSurface, updateTransfers):
# commit changes made during editing and fix values that changed
# updateTransfers = True: update everything, and update the lines that
# get marked as transfers from this line with updateTransfers = False
# updateTransfers = False: only update the line, no boats, and
# do not continue to call update on lines that get marked as transfers
if len(self.segments) > 0:
if self in self.segments[0].firstPoint.lines:
self.segments[0].firstPoint.lines.remove(self)
for i, item in enumerate(self.segments):
if self in item.lastPoint.lines:
item.lastPoint.lines.remove(self)
self.transfers = []
if updateTransfers:
self.updateBoatIndices()
for i in range(len(self._abandonedSegments)-1, -1, -1):
self._abandonedSegments.pop(i)
for i in range(len(self.tempSegments)-1, -1, -1):
if self.tempSegments[i].isAbandoned:
self.tempSegments.pop(i)
self.segments = list(self.tempSegments)
# fix indices and update the stop number list as well each stop
if len(self.segments) > 0:
self.stopNums = [self.segments[0].firstPoint.shape]
if self not in self.segments[0].firstPoint.lines:
self.segments[0].firstPoint.lines.append(self)
for i, item in enumerate(self.segments):
item.index = i
self.tempSegments[i].index = i
item.checkOverWater(worldSurface)
self.stopNums.append(item.lastPoint.shape)
if self not in item.lastPoint.lines:
item.lastPoint.lines.append(self)
# update transfers
if len(self.segments) > 0:
for line in self.segments[0].firstPoint.lines:
if line is not self and [0, line] not in self.transfers:
self.transfers.append([0, line])
if updateTransfers:
line.update(worldSurface, False)
for i, item in enumerate(self.segments):
for line in item.lastPoint.lines:
if line is not self and [i+1, line] not in self.transfers:
self.transfers.append([i+1, line])
if updateTransfers:
line.update(worldSurface, False)
# clear new stops
for i in range(len(self._newStops)-1, -1, -1):
self._newStops.pop(i)
# clear removed stops
for i in range(len(self._removedStops)-1, -1, -1):
self._removedStops.pop(i)
# clear mouse segments
for i in range(len(self.mouseSegments)-1, -1, -1):
self.mouseSegments.pop(i)
def contains(self, stop):
# see if a stop is within a line
inLine = False
for segment in self.tempSegments:
if stop == segment.firstPoint or stop == segment.lastPoint:
inLine = True
return inLine
def find(self, stop, source):
# see if a stop is within the list source
segments = []
for i, item in enumerate(source):
if (item.firstPoint == stop
or item.lastPoint == stop):
segments.append(i)
return segments
def processMouseSegments(self, stops, mouseObject, offset, worldSurface):
for mouseSegment in self.mouseSegments:
mouseSegment.update(mouseObject.getView(offset), offset)
for stop in stops:
mouseWorld = mouseObject.getWorld()
if (not self.contains(stop)
and (stop not in self._removedStops)
# expand the result of mouseWorld into the
# function call. radius has to be assigned by
# name since we are expanding a tuple into
# the function call
and stop.withinRadius(*mouseWorld,
radius=STOP_ADDITION_DISTANCE)):
# if the mouse segment meets the conditions for adding a stop, add one
self._insertSegment(mouseSegment, stop, worldSurface)
elif (self.contains(stop)
and (stop not in self._newStops)
and (stop not in self._removedStops)
and stop.withinRadius(*mouseWorld,
radius=STOP_REMOVAL_DISTANCE)):
# same as before but for removing
self._removeStop(stop)
def _findNextActiveStop(self, stopsToSearch):
# find the next non-abandoned stop in the given list
for stop in stopsToSearch:
if not self.tempSegments[stop].isAbandoned:
return stop
return
def _removeStop(self, stop):
matchedSegments = self.find(stop, self.tempSegments)
validRemoval = False
for mouseSegment in self.mouseSegments:
if stop == mouseSegment.firstPoint:
validRemoval = True
if not validRemoval:
return
if len(matchedSegments) == 2 and len(self.mouseSegments) == 2:
# removing middle stops
if not self.tempSegments[matchedSegments[0]].isAbandoned:
self._abandonSegment(matchedSegments[0])
if not self.tempSegments[matchedSegments[1]].isAbandoned:
self._abandonSegment(matchedSegments[1])
# find the next active stop after the removed point
self._updateMouseSegment("after", matchedSegments[1], 1)
# find the next active stop before the removed point
self._updateMouseSegment("before", matchedSegments[0], 0)
elif (len(matchedSegments) == 1
or (len(matchedSegments) == 2
and len(self.mouseSegments) == 1)):
# removing end stops
if (len(matchedSegments) == 2
and self.tempSegments[matchedSegments[0]].isAbandoned):
matchedSegments.pop(0)
if not self.tempSegments[matchedSegments[0]].isAbandoned:
self._abandonSegment(matchedSegments[0])
if len(self.mouseSegments) == 2:
# remove the extra unwanted mouse segment
if matchedSegments[0] == 0:
if (self.mouseSegments[0].direction == "before"
or self.mouseSegments[0].firstPoint != stop):
self.mouseSegments.pop(1)
else:
self.mouseSegments.pop(0)
elif matchedSegments[0] == len(self.tempSegments)-1:
if self.mouseSegments[0].direction == "after":
self.mouseSegments.pop(1)
else:
self.mouseSegments.pop(0)
if self.mouseSegments[0].direction == "before":
self._updateMouseSegment("after", matchedSegments[0], 0)
elif self.mouseSegments[0].direction == "after":
self._updateMouseSegment("before", matchedSegments[0], 0)
self._removedStops.append(stop)
def _updateMouseSegment(self, direction, matchedSegment, mouseIndex):
# corrects the mouse segment after removal of stops
if direction == "after":
nextStop = self._findNextActiveStop(
list(range(-len(self.tempSegments)+matchedSegment, 0)))
if nextStop is None:
self.mouseSegments[mouseIndex].firstPoint = self.tempSegments[-1].lastPoint
self.mouseSegments[mouseIndex].index = 0
else:
self.mouseSegments[mouseIndex].firstPoint = self.tempSegments[nextStop].firstPoint
self.mouseSegments[mouseIndex].index = nextStop
elif direction == "before":
nextStop = self._findNextActiveStop(
list(range(matchedSegment, -1, -1)))
if nextStop is None:
self.mouseSegments[mouseIndex].firstPoint = self.tempSegments[0].firstPoint
self.mouseSegments[mouseIndex].index = -1
else:
self.mouseSegments[mouseIndex].firstPoint = self.tempSegments[nextStop].lastPoint
self.mouseSegments[mouseIndex].index = nextStop
def _insertSegment(self, mouseSegment, stop, worldSurface):
if len(self.tempSegments) == 0:
if stop != mouseSegment.firstPoint:
segment = Segment(mouseSegment.firstPoint,
stop,
0)
segment.checkOverWater(worldSurface)
self.tempSegments.append(segment)
mouseSegment.index = mouseSegment.index+1
elif mouseSegment.direction == "before":
if mouseSegment.index == 0:
segment = Segment(stop,
self.tempSegments[-1].lastPoint,
-1)
segment.checkOverWater(worldSurface)
self.tempSegments.append(segment)
else:
segment = Segment(stop,
self.tempSegments[mouseSegment.index].firstPoint,
mouseSegment.index-1)
segment.checkOverWater(worldSurface)
self.tempSegments.insert(mouseSegment.index,
segment)
mouseSegment.index = mouseSegment.index-1
elif mouseSegment.direction == "after":
if mouseSegment.index == -1:
segment = Segment(self.tempSegments[0].firstPoint,
stop,
0)
segment.checkOverWater(worldSurface)
self.tempSegments.insert(0,
segment)
else:
segment = Segment(self.tempSegments[mouseSegment.index].lastPoint,
stop,
mouseSegment.index+1)
segment.checkOverWater(worldSurface)
self.tempSegments.insert(mouseSegment.index+1,
segment)