-
Notifications
You must be signed in to change notification settings - Fork 0
/
NPCBehavior.py
121 lines (97 loc) · 3.37 KB
/
NPCBehavior.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
import random
import GameState
STATE_DISABLED = 0
STATE_IDLE = 1
STATE_CHASE = 2
STATE_ATTACK = 3
STATE_FLEE = 4
STATE_DEAD = 5
#npc needs hide function
#
class NPCBehavior:
def __init__(self,npc):
assert(npc != None)
self.npc = npc
self.state = None
self.nextState = STATE_IDLE
self.target = None
self.noodleTick = 0
self.pathList = []
def updateState(self):
if self.nextState:
self.state = self.nextState
self.nextState = None
def activity(self):
if not PLAYER_ON_SAME_MAP:
return
self.noodleTick += 1
if(self.noodleTick > 360)
self.noodleTick = 0
self.updateState()
else:
return
assert(self.npc != None)
assert(self.state != None)
if self.state == STATE_CHASE:
if self.npc.sqDistanceFrom(self.target) < 33*33:
self.nextState = STATE_ATTACK
if pathLists.empty():
self.nextState = STATE_IDLE
self.target = None
else:
moveTo(pathLists.pop())
elif self.state = STATE_ATTACK:
if self.npc.sqDistanceFrom(self.target) >= 33*33:
self.nextState = STATE_CHASE
elif self.state == STATE_IDLE:
self.target = GameState.player
self.nextState = STATE_CHASE
def moveTick(self):
def rethinkPath(self,x,y):
paths = []
visited = []
foundPath = explore(self.npc.getTilePos(),target.getTilePos(),paths,visited)
if foundPath:
self.pathList = paths
else:
self.pathList = []
def impassable(self,x,y):
return (GameState.getCurrentAtMap[x][y] != '.')
def explore(self,cur,end,pathList,visited):
if cur == end:
return True
else:
visited.append(cur)
print cur,"has been visited"
# current direction priority with respect to end position
left = [0,(cur[0]-1,cur[1])]
right = [0,(cur[0]+1,cur[1])]
up = [0,(cur[0],cur[1]+1)]
down = [0,(cur[0],cur[1]-1)]
xdif = end[1] - cur[1]
ydif = end[0] - cur[0]
if xdif > 0:
left[0] += 1
elif xdif < 0:
right[0] += 1
if ydif > 0:
down[0] += 1
elif xdif < 0:
up[0] += 1
pTiles = sorted([left,right,up,down], key=itemgetter(0), reverse=True)
result = []
for nextTile in pTiles:
if impassable(nextTile[1],visited):
result.append((False,nextTile[1]))
print " ",nextTile[1],"impassable"
else:
good = explore(nextTile[1],end,pathList,visited)
result.append((good,nextTile[1]))
print " ",nextTile[1],"passable"
if good:
break
for explored_tile in result:
if explored_tile[0] == True:
pathList.append(explored_tile[1])
return True
return False