-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_states.py
161 lines (112 loc) · 5.42 KB
/
bot_states.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
from state_machine import BotState
from api.vector2 import Vector2
from api import orders
import random
from a_star import AStar
class BotStateIdle(BotState):
def __init__(self):
BotState.__init__(self, "IDLE")
def get_order(self, commander):
pass
class BotStateDefend(BotState):
def __init__(self):
BotState.__init__(self, "DEFEND")
def get_order(self, commander, bot):
exact_target = commander.level.findRandomFreePositionInBox((commander.game.team.flag.position - 5.0, commander.game.team.flag.position + 5.0))
pomastar = AStar(commander.gamedata['blockHeights'], commander.twodpom)
path = pomastar.get_path(bot.position.x,
bot.position.y,
exact_target.x,
exact_target.y)
try:
if len(path) > 10:
exact_target = Vector2(*path[len(path)/2])
target = commander.level.findRandomFreePositionInBox((exact_target-5.0, exact_target+5.0))
else:
target = commander.game.team.flag.position
# print target
if not target:
raise "no target found"
except:
print 'couldnt convert target to vec2'
target = flagScoreLocation = commander.game.team.flagScoreLocation
targetMin = target - Vector2(2.0, 2.0)
targetMax = target + Vector2(2.0, 2.0)
goal = commander.level.findRandomFreePositionInBox([targetMin, targetMax])
if not goal:
return None, None
if (goal - bot.position).length() > 8.0:
return (orders.Charge, commander.defender, goal), {'description' : 'rushing to defend'}
else:
return (orders.Defend, commander.defender, (commander.middle - bot.position)), {'description' : 'defending'}
class BotStateAttackFlag(BotState):
def __init__(self):
BotState.__init__(self, "Attack Flag")
def get_order(self, commander, bot):
if bot.flag:
# Tell the flag carrier to run home!
target = commander.game.team.flagScoreLocation
return (orders.Charge, bot, target), {'description' : 'run home'}
else:
target = commander.game.enemyTeam.flag.position
flank = commander.getFlankingPosition(bot, target)
if (target - flank).length() > (bot.position - target).length():
return (orders.Attack, bot, target), {'description' : 'attack from flank', 'lookAt':target}
else:
flank = commander.level.findNearestFreePosition(flank)
return (orders.Charge, bot, flank), { 'description' : 'running to flank'}
class BotStateRandomPatrol(BotState):
def __init__(self):
BotState.__init__(self, "Random Patrol")
def get_order(self, commander, bot):
try:
halfBox = 0.4 * min(commander.level.width, commander.level.height) * Vector2(1, 1)
exact_target = commander.level.findRandomFreePositionInBox((commander.middle - halfBox, commander.middle + halfBox))
path = commander.astar.get_path(bot.position.x,
bot.position.y,
exact_target.x,
exact_target.y)
try:
if len(path) > 10:
exact_target = Vector2(*path[len(path)/2])
target = commander.level.findRandomFreePositionInBox((exact_target-5.0, exact_target+5.0))
else:
target = commander.game.team.flagScoreLocation
# print target
if not target:
raise "no target found"
except:
print 'couldnt convert target to vec2'
target = flagScoreLocation = commander.game.team.flagScoreLocation
except:
halfBox = 0.4 * min(commander.level.width, commander.level.height) * Vector2(1, 1)
target = commander.level.findRandomFreePositionInBox((commander.middle - halfBox, commander.middle + halfBox))
# issue the order
if target:
return (orders.Attack, bot, target), {'description': 'random patrol'}
# issue the order
if target:
return (orders.Attack, bot, target), {'description': 'random patrol'}
else:
return None, None
class BotStateAttackBot(BotState):
def __init__(self):
BotState.__init__(self, "Attack Bot")
def get_order(self, commander, bot):
target = commander.level.findRandomFreePositionInBox((bot.position-5.0, bot.position+5.0))
if target:
return (orders.Attack, bot, target), {'lookAt' : random.choice([b.position for b in bot.visibleEnemies])}
else:
return None, None
class BotStateInterceptFlag(BotState):
def __init__(self):
BotState.__init__(self, "Intercept Flag")
def get_order(self, commander, bot):
targetPosition = (commander.game.team.flag.position + commander.game.enemyTeam.flagScoreLocation) * 0.5
targetMin = targetPosition - Vector2(6.0, 6.0)
targetMax = targetPosition + Vector2(6.0, 6.0)
goal = commander.level.findRandomFreePositionInBox([targetMin, targetMax])
if goal:
return (orders.Attack, bot, goal), {'description' : 'running to intercept', 'lookAt' : targetPosition}
else:
return None, None