forked from bitcraft/pygoap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
152 lines (109 loc) · 3.57 KB
/
test.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
"""
lets make a drunk pirate.
scenerio:
the pirate begins by idling
soon....he spies a woman
he should attempt to get drunk and sleep with her...
...any way he knows how.
"""
__version__ = ".010"
from pygoap.agent import GoapAgent
from pygoap.environment import ObjectBase
from pygoap.tiledenvironment import TiledEnvironment
from pygoap.goals import *
import os, imp, sys
from pygame.locals import *
import logging
logging.basicConfig(level=00)
stdout = sys.stdout
global_actions = {}
def load_commands(agent, path):
mod = imp.load_source("actions", os.path.join(path, "actions.py"))
global_actions = dict([ (c.__name__, c()) for c in mod.exported_actions ])
for k, v in global_actions.items():
print "testing action {}...".format(v)
[ agent.add_action(a) for a in global_actions.values() ]
def is_female(precept):
try:
thing = precept.thing
except AttributeError:
return False
else:
if isinstance(thing, Human):
return thing.gender == "Female"
class Human(GoapAgent):
def __init__(self, gender, name="welp"):
super(Human, self).__init__()
self.gender = gender
self.name = name
def __repr__(self):
return "<Human: %s>" % self.gender
def run_once():
import pygame
pygame.init()
screen = pygame.display.set_mode((480, 480))
pygame.display.set_caption('Pirate Island')
screen_buf = pygame.Surface((240, 240))
# make our little cove
formosa = TiledEnvironment("formosa.tmx")
time = 0
interactive = 1
run = True
while run:
stdout.write("=============== STEP {} ===============\n".format(time))
formosa.run(1)
if time == 1:
pirate = Human("Male", "jack")
load_commands(pirate, os.path.join("npc", "pirate"))
pirate.add_goal(SimpleGoal(is_drunk=True))
formosa.add(pirate)
formosa.set_position(pirate, (formosa, (0,0)))
elif time == 3:
rum = ObjectBase("rum")
formosa.add(rum)
formosa.set_position(rum, (formosa, (0,2)))
elif time == 6:
wench = Human("Female", "wench")
formosa.add(wench)
screen_buf.fill((0,128,255))
formosa.render(screen_buf)
pygame.transform.scale2x(screen_buf, screen)
pygame.display.flip()
stdout.write("\nPRESS ANY KEY TO CONTINUE".format(time))
stdout.flush()
# wait for a keypress
try:
if interactive:
event = pygame.event.wait()
else:
event = pygame.event.poll()
while event:
if event.type == QUIT:
run = False
break
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
run = False
break
if not interactive: break
if event.type == KEYUP: break
if interactive:
event = pygame.event.wait()
else:
event = pygame.event.poll()
except KeyboardInterrupt:
run = False
stdout.write("\n\n");
time += 1
if time == 32: run = False
if __name__ == "__main__":
import cProfile
import pstats
try:
cProfile.run('run_once()', "pirate.prof")
except KeyboardInterrupt:
pass
p = pstats.Stats("pirate.prof")
p.strip_dirs()
p.sort_stats('time').print_stats(20, "^((?!pygame).)*$")
p.sort_stats('time').print_stats(20)