-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.py
296 lines (243 loc) · 12.2 KB
/
Engine.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
import numpy as np
import random
from Team import Team
from graphicsEngine import graphicsEngine
from minmaxAI import minmaxAI
from dumbAI import dumbAI
from randAI import randAI
from neurnetAI import neurnetAI
from maxnetAI import maxnetAI
from SARSA import SARSA
from Utility import Utility
utilities = Utility()
class Engine(object):
def __init__(self, armySizeArray, controllerArray, nnet_train = False):
self.graphics = graphicsEngine()
armySizeArray = np.asarray(armySizeArray)
self.Controllers = controllerArray
self.nnet_train = nnet_train
self.t = 1 # This is for the neural network training
self.teamArray = []
totalSoldiers = 0
for i in range(len(armySizeArray)):
spawn = [20 * (len(armySizeArray) - 2) * (-1)**i, 20 * (len(armySizeArray) -1) * (-1)**i]
self.teamArray.append(Team(i, armySizeArray[i], spawn, totalSoldiers))
totalSoldiers += armySizeArray[i]
self.turnOrder = []
totalArmies = np.sum(armySizeArray)
armySizeArray -= 1 #Set up for pulling armies out of team arrays
while len(self.turnOrder) < totalArmies:
for i in range(len(self.teamArray)):
if armySizeArray[i] < 0:
continue
self.turnOrder.append(self.teamArray[i][armySizeArray[i]])
armySizeArray[i] -= 1
self.adjustHeadingsForStartOfGame()
self.Walls = self.generateWalls(2, 30, [[-20,-20],[20,20]])
self.graphics.drawState(self.Walls, self.teamArray[0], self.teamArray[1]) #TODO: more than 2 team compatibility
def reset(self, armySizeArray, controllerArray, allowRandom = False):
for controller in self.Controllers:
controller.free_space()
if allowRandom:
randomSpawn = random.randint(0,1) #Random spawns occur when value != 0
else:
randomSpawn = 0
armySizeArray = np.asarray(armySizeArray)
self.Controllers = controllerArray
self.teamArray = []
totalSoldiers = 0
for i in range(len(armySizeArray)):
spawn = [20 * (len(armySizeArray) - 2) * (-1)**i, 20 * (len(armySizeArray) -1) * (-1)**i]
self.teamArray.append(Team(i, armySizeArray[i], spawn, totalSoldiers, randomSpawn))
totalSoldiers += armySizeArray[i]
self.turnOrder = []
totalArmies = np.sum(armySizeArray)
armySizeArray -= 1 #Set up for pulling armies out of team arrays
while len(self.turnOrder) < totalArmies:
for i in range(len(self.teamArray)):
if armySizeArray[i] < 0:
continue
self.turnOrder.append(self.teamArray[i][armySizeArray[i]])
armySizeArray[i] -= 1
self.adjustHeadingsForStartOfGame()
self.graphics.drawState(self.Walls, self.teamArray[0], self.teamArray[1]) #TODO: more than 2 team compatibility
def gameLoop(self):
elapsedTurns = 0
initialStrength = utilities.get_team_strengths(self.teamArray)
while((not self.checkEndState()) and elapsedTurns < 10):
print("\n\n=====TURN {}=====".format(elapsedTurns))
packet = self.createDataPacket() #Minmax algorithm uses relative direction so need to use a packet that has relative direction
utilities.printPacket(packet)
controller = self.Controllers[packet[0][0]]
Move = controller.get_move(packet, self.Walls)
print(Move[:2])
magnitude = Move[0]
direction = Move[1]
self.turnOrder[0].move(magnitude, direction, self.Walls)
isDead = self.calculateCombat(self.turnOrder[0])
if not isDead:
self.prepNextTurn()
self.graphics.drawState(self.Walls, self.teamArray[0], self.teamArray[1]) #TODO: more than 2 team compatibility
elapsedTurns += 1
#Return results of battle
endStrength = utilities.get_team_strengths(self.teamArray)
return [initialStrength, endStrength]
def checkEndState(self):
strengths = utilities.get_team_strengths(self.teamArray)
numCombatantTeams = 0
for s in strengths:
if s > 0:
numCombatantTeams += 1
if numCombatantTeams > 1:
return False
return True
def calculateCombat(self, unit):
def calculateDamage(attDirection, attStr, defStr, Bonus, defOutOfRange):
#Bonus is the attacking bonus for flanking
if not Bonus:
bonus = 0
else:
bonus = .75 * np.floor(attDirection / 15)# * (1 - (min(defHead, attDirection) / max(defHead, attDirection)))
defDmgTaken = np.ceil(attStr / defStr + bonus)
if not defOutOfRange:
attDmgTaken = np.ceil(defStr / attStr - .5 * bonus) #Flanks are 75% effective at protecting attackers
else:
attDmgTaken = 0
if attDmgTaken < 0:
attDmgTaken = 0
return defDmgTaken, attDmgTaken
def checkCombatModifiers(unit1, unit2):
lineOfSightBlocked, _ = utilities.checkForIntersect(self.Walls, unit1.location, unit2.location)
#Check if unit1 is attacking unit2
if utilities.get_relative_direction(unit1, unit2) < utilities.get_relative_direction(unit2, unit1):
Attacker = True
else:
Attacker = False
dist = utilities.get_distance(unit1.location, unit2.location)
if unit1.fireRange > dist and unit2.fireRange > dist:
#They're both in range of each other
Range = False
else:
Range = True #One outranges the other
#Attacker is the one that outranges
if unit1.fireRange > unit2.fireRange:
Attacker = True
else:
Attacker = False
if not Attacker and utilities.get_relative_direction(unit1, unit2) > 30 and utilities.get_relative_direction(unit2, unit1) < 90:
Bonus = True
elif Attacker and utilities.get_relative_direction(unit2, unit1) > 30 and utilities.get_relative_direction(unit1, unit2) < 90:
Bonus = True
else:
Bonus = False
return lineOfSightBlocked, Attacker, Bonus, Range
for i in range(len(self.turnOrder)):
if unit == self.turnOrder[i] or unit.get_unitTeam() == self.turnOrder[i].get_unitTeam():
continue
if self.turnOrder[i].strength <= 0:
continue
if unit.strength <= 0:
break
dist = utilities.get_distance(unit.location, self.turnOrder[i].location)
if (dist < unit.fireRange or dist < self.turnOrder[i].fireRange):
lineOfSightBlocked, Attacker, Bonus, Range = checkCombatModifiers(unit, self.turnOrder[i])
if lineOfSightBlocked:
continue
if Attacker:
attDirection = utilities.get_relative_direction(self.turnOrder[i], unit)
dmgDone, dmgTaken = calculateDamage(attDirection,
unit.strength, self.turnOrder[i].strength,
Bonus, Range)
else:
attDirection = utilities.get_relative_direction(unit, self.turnOrder[i])
dmgTaken, dmgDone = calculateDamage(attDirection,
self.turnOrder[i].strength, unit.strength,
Bonus, Range)
unit.strength -= dmgTaken
self.turnOrder[i].strength -= dmgDone
unitIsDead = False #The unit that is taking its turn
for army in self.turnOrder:
if army.strength <= 0:
if army == self.turnOrder[0]:
unitIsDead = True
army.strength = 0
self.turnOrder.remove(army)
return unitIsDead
def createDataPacket(self):
#unit is the currently selected unit and is the focus of the packet
unit = self.turnOrder[0]
dataPacket = [unit.get_packet() + [0, 0]]
i = 1
while len(dataPacket) < len(self.turnOrder):
temp = self.turnOrder[i].get_packet()
temp += [utilities.get_distance(unit.location, self.turnOrder[i].location),
utilities.get_absolute_direction(unit, self.turnOrder[i])]
dataPacket.append(temp)
i += 1
return dataPacket
def prepNextTurn(self):
self.turnOrder.append(self.turnOrder[0])
self.turnOrder.remove(self.turnOrder[0])
def save_model(self):
self.Brain.save_model()
#Sets the unit's headings to the average direction of the opponents.
def adjustHeadingsForStartOfGame(self):
for unit in self.turnOrder:
directionArray = []
for otherUnit in self.turnOrder:
if otherUnit == unit:
continue
elif otherUnit.get_unitTeam() == unit.get_unitTeam():
continue
else:
direction = utilities.get_absolute_direction(unit, otherUnit)
if direction > 270:
direction -= 360
directionArray.append(direction)
directionArray = np.asarray(directionArray)
newHeading = np.average(directionArray) + random.randint(-15,15)
unit.set_heading(int(newHeading))
def generateWalls(self, numWalls, maxLength, spawnArea):
wallContainer = []
while len(wallContainer) < numWalls:
firstPoint = (random.randint(spawnArea[0][0], spawnArea[1][0]), random.randint(spawnArea[0][1], spawnArea[1][1]))
length = random.randint(10, maxLength)
direction = random.randint(0,359)
dX, dY = utilities.get_dXdY(10, direction, length) #Repurposed this function. Gets the change in x and y for the direction and length
secondPoint = (firstPoint[0] + dX, firstPoint[1] + dY)
if len(wallContainer) > 0:
intersect, _ = utilities.checkForIntersect(wallContainer, firstPoint, secondPoint)
if intersect:
continue
wallContainer.append([firstPoint, secondPoint])
return wallContainer
def doOneTurn(self):
packet = self.createDataPacket() #Minmax algorithm uses relative direction so need to use a packet that has relative direction
controller = self.Controllers[packet[0][0]]
#Minmax
if controller == "minmax":
AIMove = self.minmaxAI.get_move(packet)[1:]
#dumbAI
elif controller == "dumbai":
AIMove = self.dumbAI.get_move(packet)
#random ai
elif controller == "randai":
AIMove = self.randAI.get_move()
#Maxnet AI
elif controller == "maxnet":
AIMove = self.maxnetAI.get_move(packet)
#Neural network
elif controller == "neurnet":
AIMove = self.neurnetAI.get_move(packet)
magnitude = AIMove[0]
direction = AIMove[1]
self.turnOrder[0].move(magnitude, direction)
isDead = self.calculateCombat(self.turnOrder[0])
if not isDead:
self.prepNextTurn()
return [packet, AIMove]
if __name__ == '__main__':
random.seed(5446489)
e = Engine([1,1], ['minmax', 'sarsa'])
x = e.gameLoop()
print(x)