-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathontology.py
927 lines (780 loc) · 32.7 KB
/
ontology.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
'''
Video game description language -- ontology of concepts.
@author: Tom Schaul
'''
from random import choice, random
from math import sqrt
import pygame
from tools import triPoints, unitVector, vectNorm, oncePerStep
from ai import AStarWorld
# ---------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------
GREEN = (0, 200, 0)
BLUE = (0, 0, 200)
RED = (200, 0, 0)
GRAY = (90, 90, 90)
WHITE = (250, 250, 250)
BROWN = (140, 120, 100)
BLACK = (0, 0, 0)
ORANGE = (250, 160, 0)
YELLOW = (250, 250, 0)
PINK = (250, 200, 200)
GOLD = (250, 212, 0)
LIGHTRED = (250, 50, 50)
LIGHTORANGE = (250, 200, 100)
LIGHTBLUE = (50, 100, 250)
LIGHTGREEN = (50, 250, 50)
LIGHTGRAY = (150, 150, 150)
DARKGRAY = (30, 30, 30)
DARKBLUE = (20, 20, 100)
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
BASEDIRS = [UP, LEFT, DOWN, RIGHT]
# ---------------------------------------------------------------------
# Types of physics
# ---------------------------------------------------------------------
class GridPhysics():
""" Define actions and key-mappings for grid-world dynamics. """
def passiveMovement(self, sprite):
if sprite.speed is None:
speed = 1
else:
speed = sprite.speed
if speed != 0 and hasattr(sprite, 'orientation'):
sprite._updatePos(sprite.orientation, speed * self.gridsize[0])
def activeMovement(self, sprite, action, speed=None):
if speed is None:
if sprite.speed is None:
speed = 1
else:
speed = sprite.speed
if speed != 0 and action is not None:
sprite._updatePos(action, speed * self.gridsize[0])
def distance(self, r1, r2):
""" Grid physics use Hamming distances. """
return (abs(r1.top - r2.top)
+ abs(r1.left - r2.left))
class ContinuousPhysics(GridPhysics):
gravity = 0.
friction = 0.02
def passiveMovement(self, sprite):
if sprite.speed != 0 and hasattr(sprite, 'orientation'):
sprite._updatePos(sprite.orientation, sprite.speed)
if self.gravity > 0 and sprite.mass > 0:
self.activeMovement(sprite, (0, self.gravity * sprite.mass))
sprite.speed *= (1 - self.friction)
def activeMovement(self, sprite, action, speed=None):
""" Here the assumption is that the controls determine the direction of
acceleration of the sprite. """
if speed is None:
speed = sprite.speed
v1 = action[0] / float(sprite.mass) + sprite.orientation[0] * speed
v2 = action[1] / float(sprite.mass) + sprite.orientation[1] * speed
sprite.orientation = unitVector((v1, v2))
sprite.speed = vectNorm((v1, v2)) / vectNorm(sprite.orientation)
def distance(self, r1, r2):
""" Continuous physics use Euclidean distances. """
return sqrt((r1.top - r2.top) ** 2
+ (r1.left - r2.left) ** 2)
class NoFrictionPhysics(ContinuousPhysics):
friction = 0
class GravityPhysics(ContinuousPhysics):
gravity = 0.5
# ---------------------------------------------------------------------
# Sprite types
# ---------------------------------------------------------------------
from core import VGDLSprite, Resource
class Immovable(VGDLSprite):
""" A gray square that does not budge. """
color = GRAY
is_static = True
class Passive(VGDLSprite):
""" A square that may budge. """
color = RED
class ResourcePack(Resource):
""" Can be collected, and in that case adds/increases a progress bar on the collecting sprite.
Multiple resource packs can refer to the same type of base resource. """
is_static = True
class Flicker(VGDLSprite):
""" A square that persists just a few timesteps. """
color = RED
limit = 1
def __init__(self, **kwargs):
self._age = 0
VGDLSprite.__init__(self, **kwargs)
def update(self, game):
VGDLSprite.update(self, game)
if self._age > self.limit:
killSprite(self, None, game)
self._age += 1
class Spreader(Flicker):
""" Spreads to its four canonical neighbor positions, and replicates itself there,
if these are unoccupied. """
spreadprob = 1.
def update(self, game):
Flicker.update(self, game)
if self._age == 2:
for u in BASEDIRS:
if random() < self.spreadprob:
game._createSprite([self.name], (self.lastrect.left + u[0] * self.lastrect.size[0],
self.lastrect.top + u[1] * self.lastrect.size[1]))
class SpriteProducer(VGDLSprite):
""" Superclass for all sprites that may produce other sprites, of type 'stype'. """
stype = None
class Portal(SpriteProducer):
is_static = True
color = BLUE
class SpawnPoint(SpriteProducer):
prob = None
total = None
color = BLACK
cooldown = None
is_static = True
def __init__(self, cooldown=1, prob=1, total=None, **kwargs):
SpriteProducer.__init__(self, **kwargs)
if prob:
self.prob = prob
self.is_stochastic = (prob > 0 and prob < 1)
if cooldown:
self.cooldown = cooldown
if total:
self.total = total
self.counter = 0
def update(self, game):
if (game.time % self.cooldown == 0 and random() < self.prob):
game._createSprite([self.stype], (self.rect.left, self.rect.top))
self.counter += 1
if self.total and self.counter >= self.total:
killSprite(self, None, game)
class RandomNPC(VGDLSprite):
""" Chooses randomly from all available actions each step. """
speed = 1
is_stochastic = True
def update(self, game):
VGDLSprite.update(self, game)
self.physics.activeMovement(self, choice(BASEDIRS))
class OrientedSprite(VGDLSprite):
""" A sprite that maintains the current orientation. """
draw_arrow = False
orientation = RIGHT
def _draw(self, game):
""" With a triangle that shows the orientation. """
VGDLSprite._draw(self, game)
if self.draw_arrow:
col = (self.color[0], 255 - self.color[1], self.color[2])
pygame.draw.polygon(game.screen, col, triPoints(self.rect, unitVector(self.orientation)))
class Conveyor(OrientedSprite):
""" A static object that used jointly with the 'conveySprite' interaction to move
other sprites around."""
is_static = True
color = BLUE
strength = 1
draw_arrow = True
class Missile(OrientedSprite):
""" A sprite that constantly moves in the same direction. """
speed = 1
class OrientedFlicker(OrientedSprite, Flicker):
""" Preserves directionality """
draw_arrow = True
speed = 0
class Walker(Missile):
""" Keep moving in the current horizontal direction. If stopped, pick one randomly. """
airsteering = False
is_stochastic = True
def update(self, game):
if self.airsteering or self.lastdirection[0] == 0:
if self.orientation[0] > 0:
d = 1
elif self.orientation[0] < 0:
d = -1
else:
d = choice([-1, 1])
self.physics.activeMovement(self, (d, 0))
Missile.update(self, game)
class WalkJumper(Walker):
prob = 0.1
strength = 10
def update(self, game):
if self.lastdirection[0] == 0:
if self.prob < random():
self.physics.activeMovement(self, (0, -self.strength))
Walker.update(self, game)
class RandomInertial(OrientedSprite, RandomNPC):
physicstype = ContinuousPhysics
class RandomMissile(Missile):
def __init__(self, **kwargs):
Missile.__init__(self, orientation=choice(BASEDIRS),
speed=choice([0.1, 0.2, 0.4]), **kwargs)
class ErraticMissile(Missile):
""" A missile that randomly changes direction from time to time.
(with probability 'prob' per timestep). """
def __init__(self, prob=0.1, **kwargs):
Missile.__init__(self, orientation=choice(BASEDIRS), **kwargs)
self.prob = prob
self.is_stochastic = (prob > 0 and prob < 1)
def update(self, game):
Missile.update(self, game)
if random() < self.prob:
self.orientation = choice(BASEDIRS)
class Bomber(SpawnPoint, Missile):
color = ORANGE
is_static = False
def update(self, game):
Missile.update(self, game)
SpawnPoint.update(self, game)
class Chaser(RandomNPC):
""" Pick an action that will move toward the closest sprite of the provided target type. """
stype = None
fleeing = False
def _closestTargets(self, game):
bestd = 1e100
res = []
for target in game.getSprites(self.stype):
d = self.physics.distance(self.rect, target.rect)
if d < bestd:
bestd = d
res = [target]
elif d == bestd:
res.append(target)
return res
def _movesToward(self, game, target):
""" Find the canonical direction(s) which move toward
the target. """
res = []
basedist = self.physics.distance(self.rect, target.rect)
for a in BASEDIRS:
r = self.rect.copy()
r = r.move(a)
newdist = self.physics.distance(r, target.rect)
if self.fleeing and basedist < newdist:
res.append(a)
if not self.fleeing and basedist > newdist:
res.append(a)
return res
def update(self, game):
VGDLSprite.update(self, game)
options = []
for target in self._closestTargets(game):
options.extend(self._movesToward(game, target))
if len(options) == 0:
options = BASEDIRS
self.physics.activeMovement(self, choice(options))
class Fleeing(Chaser):
""" Just reversing directions"""
fleeing = True
class AStarChaser(RandomNPC):
""" Move towards the character using A* search. """
stype = None
fleeing = False
drawpath = None
walkableTiles = None
neighborNodes = None
def _movesToward(self, game, target):
""" Find the canonical direction(s) which move toward
the target. """
res = []
basedist = self.physics.distance(self.rect, target.rect)
for a in BASEDIRS:
r = self.rect.copy()
r = r.move(a)
newdist = self.physics.distance(r, target.rect)
if self.fleeing and basedist < newdist:
res.append(a)
if not self.fleeing and basedist > newdist:
res.append(a)
return res
def _draw(self, game):
""" With a triangle that shows the orientation. """
RandomNPC._draw(self, game)
if self.walkableTiles:
col = pygame.Color(0, 0, 255, 100)
for sprite in self.walkableTiles:
pygame.draw.rect(game.screen, col, sprite.rect)
if self.neighborNodes:
#logToFile("len(neighborNodes)=%s" %len(self.neighborNodes))
col = pygame.Color(0, 255, 255, 80)
for node in self.neighborNodes:
pygame.draw.rect(game.screen, col, node.sprite.rect)
if self.drawpath:
col = pygame.Color(0, 255, 0, 120)
for sprite in self.drawpath[1:-1]:
pygame.draw.rect(game.screen, col, sprite.rect)
def _setDebugVariables(self, world, path):
'''
Sets the variables required for debug drawing of the paths
resulting from the A-Star search.
'''
path_sprites = [node.sprite for node in path]
self.walkableTiles = world.get_walkable_tiles()
self.neighborNodes = world.neighbor_nodes_of_sprite(self)
self.drawpath = path_sprites
def update(self, game):
VGDLSprite.update(self, game)
world = AStarWorld(game)
path = world.getMoveFor(self)
# Uncomment below to draw debug paths.
# self._setDebugVariables(world,path)
if len(path)>1:
move = path[1]
nextX, nextY = world.get_sprite_tile_position(move.sprite)
nowX, nowY = world.get_sprite_tile_position(self)
movement = None
if nowX == nextX:
if nextY > nowY:
#logToFile('DOWN')
movement = DOWN
else:
#logToFile('UP')
movement = UP
else:
if nextX > nowX:
#logToFile('RIGHT')
movement = RIGHT
else:
#logToFile('LEFT')
movement = LEFT
self.physics.activeMovement(self, movement)
# ---------------------------------------------------------------------
# Avatars: player-controlled sprite types
# ---------------------------------------------------------------------
from core import Avatar
class MovingAvatar(VGDLSprite, Avatar):
""" Default avatar, moves in the 4 cardinal directions. """
color = WHITE
speed = 1
is_avatar = True
alternate_keys=False
def declare_possible_actions(self):
from pygame.locals import K_LEFT, K_RIGHT, K_UP, K_DOWN
actions = {}
actions["UP"] = K_UP
actions["DOWN"] = K_DOWN
actions["LEFT"] = K_LEFT
actions["RIGHT"] = K_RIGHT
return actions
def _readAction(self, game):
actions = self._readMultiActions(game)
if actions:
return actions[0]
else:
return None
def _readMultiActions(self, game):
""" Read multiple simultaneously pressed button actions. """
from pygame.locals import K_LEFT, K_RIGHT, K_UP, K_DOWN, K_a, K_s, K_d, K_w
res = []
if self.alternate_keys:
if game.keystate[K_d]: res += [RIGHT]
elif game.keystate[K_a]: res += [LEFT]
if game.keystate[K_w]: res += [UP]
elif game.keystate[K_s]: res += [DOWN]
else:
if game.keystate[K_RIGHT]: res += [RIGHT]
elif game.keystate[K_LEFT]: res += [LEFT]
if game.keystate[K_UP]: res += [UP]
elif game.keystate[K_DOWN]: res += [DOWN]
return res
def update(self, game):
VGDLSprite.update(self, game)
action = self._readAction(game)
if action:
self.physics.activeMovement(self, action)
class HorizontalAvatar(MovingAvatar):
""" Only horizontal moves. """
def declare_possible_actions(self):
from pygame.locals import K_LEFT, K_RIGHT
actions = {}
actions["LEFT"] = K_LEFT
actions["RIGHT"] = K_RIGHT
return actions
def update(self, game):
VGDLSprite.update(self, game)
action = self._readAction(game)
if action in [RIGHT, LEFT]:
self.physics.activeMovement(self, action)
class VerticalAvatar(MovingAvatar):
""" Only vertical moves. """
def declare_possible_actions(self):
from pygame.locals import K_UP, K_DOWN
actions = {}
actions["UP"] = K_UP
actions["DOWN"] = K_DOWN
return actions
def update(self, game):
VGDLSprite.update(self, game)
action = self._readAction(game)
if action in [UP, DOWN]:
self.physics.activeMovement(self, action)
class FlakAvatar(HorizontalAvatar, SpriteProducer):
""" Hitting the space button creates a sprite of the
specified type at its location. """
def declare_possible_actions(self):
from pygame.locals import K_SPACE
actions = HorizontalAvatar.declare_possible_actions(self)
actions["SPACE"] = K_SPACE
return actions
color = GREEN
def update(self, game):
HorizontalAvatar.update(self, game)
self._shoot(game)
def _shoot(self, game):
from pygame.locals import K_SPACE
if self.stype and game.keystate[K_SPACE]:
game._createSprite([self.stype], (self.rect.left, self.rect.top))
class OrientedAvatar(OrientedSprite, MovingAvatar):
""" Avatar retains its orientation, but moves in cardinal directions. """
draw_arrow = True
def update(self, game):
tmp = self.orientation
self.orientation = (0, 0)
VGDLSprite.update(self, game)
action = self._readAction(game)
if action:
self.physics.activeMovement(self, action)
d = self.lastdirection
if sum(map(abs, d)) > 0:
# only update if the sprite moved.
self.orientation = d
else:
self.orientation = tmp
class RotatingAvatar(OrientedSprite, MovingAvatar):
""" Avatar retains its orientation, and moves forward/backward or rotates
relative to that. """
draw_arrow = True
speed = 0
def update(self, game):
actions = self._readMultiActions(game)
if UP in actions:
self.speed = 1
elif DOWN in actions:
self.speed = -1
if LEFT in actions:
i = BASEDIRS.index(self.orientation)
self.orientation = BASEDIRS[(i + 1) % len(BASEDIRS)]
elif RIGHT in actions:
i = BASEDIRS.index(self.orientation)
self.orientation = BASEDIRS[(i - 1) % len(BASEDIRS)]
VGDLSprite.update(self, game)
self.speed = 0
class RotatingFlippingAvatar(RotatingAvatar):
""" Uses a different action set: DOWN makes it spin around 180 degrees.
Optionally, a noise level can be specified
"""
noiseLevel = 0
def update(self, game):
actions = self._readMultiActions(game)
if len(actions) > 0 and self.noiseLevel > 0:
# pick a random one instead
if random() < self.noiseLevel*4:
actions = [choice([UP, LEFT, DOWN, RIGHT])]
if UP in actions:
self.speed = 1
elif DOWN in actions:
i = BASEDIRS.index(self.orientation)
self.orientation = BASEDIRS[(i + 2) % len(BASEDIRS)]
elif LEFT in actions:
i = BASEDIRS.index(self.orientation)
self.orientation = BASEDIRS[(i + 1) % len(BASEDIRS)]
elif RIGHT in actions:
i = BASEDIRS.index(self.orientation)
self.orientation = BASEDIRS[(i - 1) % len(BASEDIRS)]
VGDLSprite.update(self, game)
self.speed = 0
@property
def is_stochastic(self):
return self.noiseLevel > 0
class NoisyRotatingFlippingAvatar(RotatingFlippingAvatar):
noiseLevel = 0.1
class ShootAvatar(OrientedAvatar, SpriteProducer):
""" Produces a sprite in front of it (e.g., Link using his sword). """
ammo=None
def __init__(self, stype=None, **kwargs):
self.stype = stype
OrientedSprite.__init__(self, **kwargs)
def update(self, game):
OrientedAvatar.update(self, game)
if self._hasAmmo():
self._shoot(game)
def _hasAmmo(self):
if self.ammo is None:
return True
elif self.ammo in self.resources:
return self.resources[self.ammo] > 0
return False
def _reduceAmmo(self):
if self.ammo is not None and self.ammo in self.resources:
self.resources[self.ammo] -= 1
def _shoot(self, game):
from pygame.locals import K_SPACE
if self.stype and game.keystate[K_SPACE]:
u = unitVector(self.orientation)
newones = game._createSprite([self.stype], (self.lastrect.left + u[0] * self.lastrect.size[0],
self.lastrect.top + u[1] * self.lastrect.size[1]))
if len(newones) > 0 and isinstance(newones[0], OrientedSprite):
newones[0].orientation = unitVector(self.orientation)
self._reduceAmmo()
class AimedAvatar(ShootAvatar):
""" Can change the direction of firing, but not move. """
speed=0
angle_diff=0.05
def update(self, game):
VGDLSprite.update(self, game)
self._aim(game)
self._shoot(game)
def _aim(self, game):
action = self._readAction(game)
if action in [UP, DOWN]:
if action == DOWN:
angle = self.angle_diff
else:
angle = -self.angle_diff
from math import cos, sin
self.orientation = unitVector((self.orientation[0]*cos(angle)-self.orientation[1]*sin(angle),
self.orientation[0]*sin(angle)+self.orientation[1]*cos(angle)))
class AimedFlakAvatar(AimedAvatar):
""" Can move left and right """
only_active=True
speed=None
def update(self, game):
AimedAvatar.update(self, game)
action = self._readAction(game)
if action in [RIGHT, LEFT]:
self.physics.activeMovement(self, action)
class InertialAvatar(OrientedAvatar):
speed = 1
physicstype = ContinuousPhysics
def update(self, game):
MovingAvatar.update(self, game)
class MarioAvatar(InertialAvatar):
""" Mario can have two states: in contact with the ground, or in parabolic flight. """
physicstype = GravityPhysics
draw_arrow = False
strength = 10
airsteering = False
def update(self, game):
action = self._readAction(game)
if action is None:
action = (0, 0)
from pygame.locals import K_SPACE
if game.keystate[K_SPACE] and self.orientation[1] == 0:
action = (action[0] * sqrt(self.strength), -self.strength)
elif self.orientation[1] == 0 or self.airsteering:
action = (action[0] * sqrt(self.strength), 0)
else:
action = (0, 0)
self.physics.activeMovement(self, action)
VGDLSprite.update(self, game)
# ---------------------------------------------------------------------
# Termination criteria
# ---------------------------------------------------------------------
from core import Termination
class Timeout(Termination):
def __init__(self, limit=0, win=False):
self.limit = limit
self.win = win
def isDone(self, game):
if game.time >= self.limit:
return True, self.win
else:
return False, None
class SpriteCounter(Termination):
""" Game ends when the number of sprites of type 'stype' hits 'limit' (or below). """
def __init__(self, limit=0, stype=None, win=True):
self.limit = limit
self.stype = stype
self.win = win
def isDone(self, game):
if game.numSprites(self.stype) <= self.limit:
return True, self.win
else:
return False, None
class MultiSpriteCounter(Termination):
""" Game ends when the sum of all sprites of types 'stypes' hits 'limit'. """
def __init__(self, limit=0, win=True, **kwargs):
self.limit = limit
self.win = win
self.stypes = kwargs.values()
def isDone(self, game):
if sum([game.numSprites(st) for st in self.stypes]) == self.limit:
return True, self.win
else:
return False, None
# ---------------------------------------------------------------------
# Effect types (invoked after an event).
# ---------------------------------------------------------------------
def killSprite(sprite, partner, game):
""" Kill command """
game.kill_list.append(sprite)
def cloneSprite(sprite, partner, game):
game._createSprite([sprite.name], (sprite.rect.left, sprite.rect.top))
def transformTo(sprite, partner, game, stype='wall'):
newones = game._createSprite([stype], (sprite.rect.left, sprite.rect.top))
if len(newones) > 0:
if isinstance(sprite, OrientedSprite) and isinstance(newones[0], OrientedSprite):
newones[0].orientation = sprite.orientation
killSprite(sprite, partner, game)
def stepBack(sprite, partner, game):
""" Revert last move. """
sprite.rect = sprite.lastrect
def undoAll(sprite, partner, game):
""" Revert last moves of all sprites. """
for s in game:
s.rect = s.lastrect
def bounceForward(sprite, partner, game):
""" The partner sprite pushed, so if possible move in the opposite direction. """
sprite.physics.activeMovement(sprite, unitVector(partner.lastdirection))
game._updateCollisionDict(sprite)
def conveySprite(sprite, partner, game):
""" Moves the partner in target direction by some step size. """
tmp = sprite.lastrect
v = unitVector(partner.orientation)
sprite.physics.activeMovement(sprite, v, speed=partner.strength)
sprite.lastrect = tmp
game._updateCollisionDict(sprite)
def windGust(sprite, partner, game):
""" Moves the partner in target direction by some step size, but stochastically
(step, step-1 and step+1 are equally likely) """
s = choice([partner.strength, partner.strength + 1, partner.strength - 1])
if s != 0:
tmp = sprite.lastrect.copy()
v = unitVector(partner.orientation)
sprite.physics.activeMovement(sprite, v, speed=s)
sprite.lastrect = tmp
game._updateCollisionDict(sprite)
def slipForward(sprite, partner, game, prob=0.5):
""" Slip forward in the direction of the current orientation, sometimes."""
if prob > random():
tmp = sprite.lastrect
v = unitVector(sprite.orientation)
sprite.physics.activeMovement(sprite, v, speed=1)
sprite.lastrect = tmp
game._updateCollisionDict(sprite)
def attractGaze(sprite, partner, game, prob=0.5):
""" Turn the orientation to the value given by the partner. """
if prob > random():
sprite.orientation = partner.orientation
def turnAround(sprite, partner, game):
sprite.rect = sprite.lastrect
sprite.lastmove = sprite.cooldown
sprite.physics.activeMovement(sprite, DOWN)
sprite.lastmove = sprite.cooldown
sprite.physics.activeMovement(sprite, DOWN)
reverseDirection(sprite, partner, game)
game._updateCollisionDict(sprite)
def reverseDirection(sprite, partner, game):
sprite.orientation = (-sprite.orientation[0], -sprite.orientation[1])
def flipDirection(sprite, partner, game):
sprite.orientation = choice(BASEDIRS)
def bounceDirection(sprite, partner, game, friction=0):
""" The centers of the objects determine the direction"""
stepBack(sprite, partner, game)
inc = sprite.orientation
snorm = unitVector((-sprite.rect.centerx + partner.rect.centerx,
- sprite.rect.centery + partner.rect.centery))
dp = snorm[0] * inc[0] + snorm[1] * inc[1]
sprite.orientation = (-2 * dp * snorm[0] + inc[0], -2 * dp * snorm[1] + inc[1])
sprite.speed *= (1. - friction)
def wallBounce(sprite, partner, game, friction=0):
""" Bounce off orthogonally to the wall. """
if not oncePerStep(sprite, game, 'lastbounce'):
return
sprite.speed *= (1. - friction)
stepBack(sprite, partner, game)
if abs(sprite.rect.centerx - partner.rect.centerx) > abs(sprite.rect.centery - partner.rect.centery):
sprite.orientation = (-sprite.orientation[0], sprite.orientation[1])
else:
sprite.orientation = (sprite.orientation[0], -sprite.orientation[1])
def wallStop(sprite, partner, game, friction=0):
""" Stop just in front of the wall, removing that velocity component,
but possibly sliding along it. """
if not oncePerStep(sprite, game, 'laststop'):
return
stepBack(sprite, partner, game)
if abs(sprite.rect.centerx - partner.rect.centerx) > abs(sprite.rect.centery - partner.rect.centery):
sprite.orientation = (0, sprite.orientation[1] * (1. - friction))
else:
sprite.orientation = (sprite.orientation[0] * (1. - friction), 0)
sprite.speed = vectNorm(sprite.orientation) * sprite.speed
sprite.orientation = unitVector(sprite.orientation)
def killIfSlow(sprite, partner, game, limitspeed=1):
""" Take a decision based on relative speed. """
if sprite.is_static:
relspeed = partner.speed
elif partner.is_static:
relspeed = sprite.speed
else:
relspeed = vectNorm((sprite._velocity()[0] - partner._velocity()[0],
sprite._velocity()[1] - partner._velocity()[1]))
if relspeed < limitspeed:
killSprite(sprite, partner, game)
def killIfFromAbove(sprite, partner, game):
""" Kills the sprite, only if the other one is higher and moving down. """
if (sprite.lastrect.top > partner.lastrect.top
and partner.rect.top > partner.lastrect.top):
killSprite(sprite, partner, game)
def killIfAlive(sprite, partner, game):
""" Perform the killing action, only if no previous collision effect has removed the partner. """
if partner not in game.kill_list:
killSprite(sprite, partner, game)
def collectResource(sprite, partner, game):
""" Adds/increments the resource type of sprite in partner """
assert isinstance(sprite, Resource)
r = sprite.resourceType
partner.resources[r] = max(0, min(partner.resources[r]+sprite.value, game.resources_limits[r]))
def changeResource(sprite, partner, game, resource, value=1):
""" Increments a specific resource type in sprite """
sprite.resources[resource] = max(0, min(sprite.resources[resource]+value, game.resources_limits[resource]))
def spawnIfHasMore(sprite, partner, game, resource, stype, limit=1):
""" If 'sprite' has more than a limit of the resource type given, it spawns a sprite of 'stype'. """
if sprite.resources[resource] >= limit:
game._createSprite([stype], (sprite.rect.left, sprite.rect.top))
def killIfHasMore(sprite, partner, game, resource, limit=1):
""" If 'sprite' has more than a limit of the resource type given, it dies. """
if sprite.resources[resource] >= limit:
killSprite(sprite, partner, game)
def killIfOtherHasMore(sprite, partner, game, resource, limit=1):
""" If 'partner' has more than a limit of the resource type given, sprite dies. """
if partner.resources[resource] >= limit:
killSprite(sprite, partner, game)
def killIfHasLess(sprite, partner, game, resource, limit=1):
""" If 'sprite' has less than a limit of the resource type given, it dies. """
if sprite.resources[resource] <= limit:
killSprite(sprite, partner, game)
def killIfOtherHasLess(sprite, partner, game, resource, limit=1):
""" If 'partner' has less than a limit of the resource type given, sprite dies. """
if partner.resources[resource] <= limit:
killSprite(sprite, partner, game)
def wrapAround(sprite, partner, game, offset=0):
""" Move to the edge of the screen in the direction the sprite is coming from.
Plus possibly an offset. """
if sprite.orientation[0] > 0:
sprite.rect.left = offset * sprite.rect.size[1]
elif sprite.orientation[0] < 0:
sprite.rect.left = game.screensize[0] - sprite.rect.size[0] * (1 + offset)
if sprite.orientation[1] > 0:
sprite.rect.top = offset * sprite.rect.size[1]
elif sprite.orientation[1] < 0:
sprite.rect.top = game.screensize[1] - sprite.rect.size[1] * (1 + offset)
sprite.lastmove = 0
def pullWithIt(sprite, partner, game):
""" The partner sprite adds its movement to the sprite's. """
if not oncePerStep(sprite, game, 'lastpull'):
return
tmp = sprite.lastrect
v = unitVector(partner.lastdirection)
sprite._updatePos(v, partner.speed * sprite.physics.gridsize[0])
if isinstance(sprite.physics, ContinuousPhysics):
sprite.speed = partner.speed
sprite.orientation = partner.lastdirection
sprite.lastrect = tmp
def teleportToExit(sprite, partner, game):
e = choice(game.sprite_groups[partner.stype])
sprite.rect = e.rect
sprite.lastmove = 0
# this allows us to determine whether the game has stochastic elements or not
stochastic_effects = [teleportToExit, windGust, slipForward, attractGaze, flipDirection]
# this allows is to determine which effects might kill a sprite
kill_effects = [killSprite, killIfSlow, transformTo, killIfOtherHasLess, killIfOtherHasMore, killIfHasMore, killIfHasLess,
killIfFromAbove, killIfAlive]