-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp4_brains.py
executable file
·73 lines (58 loc) · 1.86 KB
/
p4_brains.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
import random
from slug_machine import SlugStateMachine
# EXAMPLE STATE MACHINE
class MantisBrain:
def __init__(self, body):
self.body = body
self.state = 'idle'
self.target = None
def handle_event(self, message, details):
if self.state is 'idle':
if message == 'timer':
# go to a random point, wake up sometime in the next 10 seconds
world = self.body.world
x, y = random.random()*world.width, random.random()*world.height
self.body.go_to((x,y))
self.body.set_alarm(random.random()*10)
elif message == 'collide' and details['what'] == 'Slug':
# a slug bumped into us; get curious
self.state = 'curious'
self.body.set_alarm(1) # think about this for a sec
self.body.stop()
self.target = details['who']
elif self.state == 'curious':
if message == 'timer':
# chase down that slug who bumped into us
if self.target:
if random.random() < 0.5:
self.body.stop()
self.state = 'idle'
else:
self.body.follow(self.target)
self.body.set_alarm(1)
elif message == 'collide' and details['what'] == 'Slug':
# we meet again!
slug = details['who']
slug.amount -= 0.01 # take a tiny little bite
class SlugBrain:
def __init__(self, body):
self.body = body
self.stateMachine = SlugStateMachine(body)
def handle_event(self, message, details):
# TODO: IMPLEMENT THIS METHOD
# (Use helper methods and classes to keep your code organized where
# approprioate.)
self.stateMachine.transition(message, details)
pass
world_specification = {
'worldgen_seed': 13, # comment-out to randomize
'nests': 2,
'obstacles': 25,
'resources': 5,
'slugs': 5,
'mantises': 5,
}
brain_classes = {
'mantis': MantisBrain,
'slug': SlugBrain,
}