/
myPlayerBrain.py
executable file
·414 lines (358 loc) · 19.2 KB
/
myPlayerBrain.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"""
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE"
* As long as you retain this notice you can do whatever you want with this
* stuff. If you meet an employee from Windward some day, and you think this
* stuff is worth it, you can buy them a beer in return. Windward Studios
* ----------------------------------------------------------------------------
"""
import random as rand
import traceback
import simpleAStar
from framework import sendOrders, playerPowerSend
NAME = "EStorm"
SCHOOL = "Harvey Mudd College"
class MyPlayerBrain(object):
"""The Python AI class. This class must have the methods setup and gameStatus."""
def __init__(self, name=NAME):
self.name = name #The name of the player.
#The player's avatar (looks in the same directory that this module is in).
#Must be a 32 x 32 PNG file.
try:
avatar = open("MyAvatar.png", "rb")
avatar_str = b''
for line in avatar:
avatar_str += line
avatar = avatar_str
except IOError:
avatar = None # avatar is optional
self.avatar = avatar
def setup(self, gMap, me, allPlayers, companies, passengers, client, stores, powerUpDeck, framework):
"""
Called at the start of the game; initializes instance variables.
gMap -- The game map.
me -- Your Player object.
allPlayers -- List of all Player objects (including you).
companies -- The companies on the map.
passengers -- The passengers that need a lift.
client -- TcpClient to use to send orders to the server.
stores -- All the coffee stores.
powerUpDeck -- The powerups this player has in their hand (may have to wait before playing it).
powerUpHand -- The powerups this player can draw.
myPassenger -- The passenger currently in my limo, none to start.
MAX_TRIPS_BEFORE_REFILL -- The maximum number of trips allowed before a refill is required.
"""
self.framework = framework
self.gameMap = gMap
self.players = allPlayers
self.me = me
self.companies = companies
self.passengers = passengers
self.client = client
self.stores = stores
self.powerUpDeck = powerUpDeck
self.powerUpHand = []
self.powerUpHand2 = []
self.myPassenger = None
self.MAX_TRIPS_BEFORE_REFILL = 3
self.pickup = pickup = self.allPickups(me, passengers)
# get the path from where we are to the dest.
path = self.calculatePathPlus1(me, pickup[0].lobby.busStop)
sendOrders(self, "ready", path, pickup)
def setOkToPlay(self):
for p in self.powerUpHand2:
p.okToPlay = True
def gameStatus(self, status, playerStatus):
"""
Called to send an update message to this A.I. We do NOT have to send a response.
status -- The status message.
playerStatus -- The player this status is about. THIS MAY NOT BE YOU.
players -- The status of all players.
passengers -- The status of all passengers.
"""
if self.me.score < 0.5:
self.setOkToPlay()
# bugbug - Framework.cs updates the object's in this object's Players,
# Passengers, and Companies lists. This works fine as long as this app
# is single threaded. However, if you create worker thread(s) or
# respond to multiple status messages simultaneously then you need to
# split these out and synchronize access to the saved list objects.
try:
# bugbug - we return if not us because the below code is only for
# when we need a new path or our limo hits a bus stop. If you want
# to act on other players arriving at bus stops, you need to
# remove this. But make sure you use self.me, not playerStatus for
# the Player you are updating (particularly to determine what tile
# to start your path from).
if playerStatus != self.me:
if(status == "PASSENGER_DELIVERED_AND_PICKED_UP" or
status == "PASSENGER_PICKED_UP"):
if(playerStatus.limo.passenger == self.me.pickup[0] and self.me.limo.coffeeServings > 0):
pickup = self.allPickups(self.me, self.passengers)
print "Changing targets because someone else picked up ours"
print "Now picking up"
print pickup[0]
ptDest = pickup[0].lobby.busStop
self.displayOrders(ptDest)
# get the path from where we are to the dest.
path = self.calculatePathPlus1(self.me, ptDest)
sendOrders(self, "move", path, pickup)
elif (self.me.limo.coffeeServings <= 0):
print "Gotta get more coffee"
path = self.calculatePathPlus1(self.me, self.findClosestStore().busStop)
pickup = self.allPickups(self.me, self.passengers)
sendOrders(self, "move", path, pickup)
return
ptDest = None
pickup = []
if (status == "UPDATE" and self.me.limo.coffeeServings > 0):
self.maybePlayPowerUp()
if(self.me.limo.passenger): #if passenger in limo
if(self.enemyAtDestination(self.me.limo.passenger)):
print "enemy deposited at destination, changing passenger"
pickup = self.allPickups(self.me, self.passengers)
ptDest = pickup[0].lobby.busStop
self.displayOrders(ptDest)
# get the path from where we are to the dest.
path = self.calculatePathPlus1(self.me, ptDest)
sendOrders(self, "move", path, pickup)
elif (self.me.limo.coffeeServings <= 0): #no passenger in limo
print "No passenger in limo. Heading for "
print self.me.pickup[0]
elif (self.me.limo.coffeeServings <= 0):
print "Gotta get more coffee"
path = self.calculatePathPlus1(self.me, self.findClosestStore().busStop)
pickup = self.allPickups(self.me, self.passengers)
sendOrders(self, "move", path, pickup)
return
self.displayStatus(status, playerStatus)
print "status:"
print status
if (status == "PASSENGER_NO_ACTION" or status == "NO_PATH"):
if self.me.limo.passenger is None:
pickup = self.allPickups(self.me, self.passengers)
ptDest = pickup[0].lobby.busStop
print "Now picking up"
print pickup[0]
else:
ptDest = self.me.limo.passenger.destination.busStop
elif (status == "PASSENGER_DELIVERED" or
status == "PASSENGER_ABANDONED"):
pickup = self.allPickups(self.me, self.passengers)
ptDest = pickup[0].lobby.busStop
self.setOkToPlay()
elif status == "PASSENGER_REFUSED_ENEMY":
pickup = self.allPickups(self.me, self.passengers)
ptDest = pickup[0].lobby.busStop
self.setOkToPlay()
elif (status == "PASSENGER_DELIVERED_AND_PICKED_UP" or
status == "PASSENGER_PICKED_UP"):
pickup = self.allPickups(self.me, self.passengers)
ptDest = self.me.limo.passenger.destination.busStop
self.setOkToPlay()
# coffee store override
if(status == "PASSENGER_DELIVERED_AND_PICKED_UP" or status == "PASSENGER_DELIVERED" or status == "PASSENGER_ABANDONED"):
if(self.me.limo.coffeeServings <= 0):
ptDest = self.findClosestStore().busStop
self.setOkToPlay()
elif(status == "PASSENGER_REFUSED_NO_COFFEE" or status == "PASSENGER_DELIVERED_AND_PICK_UP_REFUSED"):
ptDest = self.findClosestStore().busStop
self.setOkToPlay()
elif(status == "COFFEE_STORE_CAR_RESTOCKED"):
pickup = self.allPickups(self.me, self.passengers)
if len(pickup) != 0:
ptDest = pickup[0].lobby.busStop
self.setOkToPlay()
if(ptDest == None):
return
self.displayOrders(ptDest)
# get the path from where we are to the dest.
path = self.calculatePathPlus1(self.me, ptDest)
sendOrders(self, "move", path, pickup)
except Exception as e:
print traceback.format_exc()
raise e
def displayOrders(self, ptDest):
msg = None
potentialStores = [s for s in self.stores if s.busStop == ptDest]
store = None
if len(potentialStores) > 0:
store = potentialStores[0]
if store is not None:
storename = store.name
if "Gloria Jean" in storename:
storename = "Gloria Jean's Coffees"
if "Peet" in storename:
storename = "Peet's Coffee and Tea"
if "Tully" in storename:
storename = "Tully's Coffee"
msg = "Heading toward {0} at {1}".format(storename, ptDest)
else:
potentialCompanies = [c for c in self.companies if c.busStop == ptDest]
company = None
if len(potentialCompanies) > 0:
company = potentialCompanies[0]
if company is not None:
msg = "Heading toward {0} at {1}".format(company.name, ptDest)
if msg is not None:
print(msg)
def calculatePathPlus1 (self, me, ptDest):
path = simpleAStar.calculatePath(self.gameMap, me.limo.tilePosition, ptDest)
# add in leaving the bus stop so it has orders while we get the message
# saying it got there and are deciding what to do next.
if len(path) > 1:
path.append(path[-2])
return path
def checkForRaces(self):
playersWithoutPassengers = filter(lambda p: p.guid != self.me.guid and p.limo.passenger is None, self.players)
if (self.me.limo.passenger == None):
for player in playersWithoutPassengers:
if ((player.limo.path != []) and (self.me.limo.path != [])):
if (player.limo.path[-1] == self.me.limo.path[-1]):
return player
return None
def maybePlayPowerUp(self):
# not enough, draw
if len(self.powerUpHand2) < self.me.maxCardsInHand and len(self.powerUpDeck) > 0:
for card in self.powerUpDeck:
if(len(self.powerUpHand2) >= self.me.maxCardsInHand):
break
# select a card
self.powerUpDeck.remove(card)
self.powerUpHand2.append(card)
playerPowerSend(self, "DRAW", card)
#return
print "Length of hand", len(self.powerUpHand2)
print "Max length", self.me.maxCardsInHand
# can we play one?
okToPlayHand = filter(lambda p: p.okToPlay, self.powerUpHand2)
print "Ok to play len", len(okToPlayHand)
if len(okToPlayHand) == 0:
return
for powerUp in okToPlayHand:
print "Looking at powerUp ", powerUp.card
if powerUp.card == "ALL_OTHER_CARS_QUARTER_SPEED":
playerPowerSend(self, "PLAY", powerUp)
self.powerUpHand2.remove(powerUp)
elif powerUp.card == "MOVE_PASSENGER":
currPassenger = self.me.limo.passenger
if currPassenger != None:
# Could try waiting until right before we arrive at destination to use
# Although if there are multiple enemies, could stop us from getting rid
# of all of them
for e in currPassenger.enemies:
if e.lobby == currPassenger.destination:
powerUp.passenger = e
playerPowerSend(self, "PLAY", powerUp)
self.powerUpHand2.remove(powerUp)
break
elif powerUp.card == "STOP_CAR":
# Check for races:
playersWithoutPassengers = filter(lambda p: p.guid != self.me.guid and p.limo.passenger is None, self.players)
racingPlayer = self.checkForRaces()
if racingPlayer != None:
powerUp.player = racingPlayer
print "stopping", racingPlayer, " their destination:", racingPlayer.limo.path[-1], "our dest", self.limo.path[-1]
# Stop best player if we have not found a race
else:
bestScore = 0
bestPlayer = None
for player in self.players:
if player.totalScore > bestScore:
bestScore = player.totalScore
bestPlayer = player
powerUp.player = bestPlayer
playerPowerSend(self, "PLAY", powerUp)
self.powerUpHand2.remove(powerUp)
elif powerUp.card == "CHANGE_DESTINATION":
playersWithPassengers = filter(lambda p: p.guid != self.me.guid and p.limo.passenger is not None, self.players)
for player in playersWithPassengers:
# If a player is between 3 and 10 spaces away from destination
if (10 > abs(player.limo.tilePosition[0] - player.limo.path[-1][0]) > 3) or (10 > abs(player.limo.tilePosition[1] - player.limo.path[-1][1]) > 3):
powerUp.player = player
playerPowerSend(self, "PLAY", powerUp)
self.powerUpHand2.remove(powerUp)
break
else:
playerPowerSend(self, "DISCARD", powerUp)
self.powerUpHand2.remove(powerUp)
#playerPowerSend(self, "PLAY", powerUp)
return
# A power-up was played. It may be an error message, or success.
def powerUpStatus(self, status, playerPowerUp, cardPlayed):
# redo the path if we got relocated
if((status == "POWER_UP_PLAYED") and ((cardPlayed.card == "RELOCATE_ALL_CARS") or ((cardPlayed.card == "CHANGE_DESTINATION") and (cardPlayed.player.guid == self.me.guid)))):
self.gameStatus("NO_PATH", self.me)
return
def displayStatus(self, status, plyrStatus):
msg = ""
# Sometimes, myPassenger or myPassenger.lobby is None. If you want to figure this
# out on your own, have at it, but it really only affects the messages displayed below.
if(status == "PASSENGER_DELIVERED"):
if self.myPassenger.name is not None or self.myPassenger.lobby is not None:
msg = "{0} delivered to {1}\n".format(self.myPassenger.name, self.myPassenger.lobby.name)
self.myPassenger = None
elif(status == "PASSENGER_ABANDONED"):
if self.myPassenger is not None or self.myPassenger.lobby is not None:
msg = "{0} abandoned at {1}\n".format(self.myPassenger.name, self.myPassenger.lobby.name)
self.myPassenger = None
elif(status == "PASSENGER_REFUSED_ENEMY"):
msg = "{0} refused to exit at {1} - enemy there".format(plyrStatus.limo.passenger.name, plyrStatus.limo.passenger.destination.name)
elif(status == "PASSENGER_DELIVERED_AND_PICKED_UP"):
msg = "{0} delivered at {1} and {2} picked up".format(self.myPassenger.name, self.myPassenger.lobby.name, plyrStatus.limo.passenger.name)
self.myPassenger = plyrStatus.limo.passenger
elif(status == "PASSENGER_PICKED_UP"):
msg = "{0} picked up".format(plyrStatus.limo.passenger.name)
self.myPassenger = plyrStatus.limo.passenger
elif(status == "PASSENGER_REFUSED_NO_COFFEE"):
msg = "Passenger refused to board limo, no coffee"
elif(status == "PASSENGER_DELIVERED_AND_PICK_UP_REFUSED"):
msg = "{0} delivered at {1}, new passenger refused to board limo, no coffee".format(self.myPassenger.name, self.myPassenger.lobby.name)
elif(status == "COFFEE_STORE_CAR_RESTOCKED"):
msg = "Coffee restocked!"
if(msg != ""):
print (msg)
return
def allPickups (self, me, passengers):
# pickup is a list of the possible passengers to pick up.
pickup = [p for p in passengers if (not p in me.passengersDelivered and
p != me.limo.passenger and
p.car is None and
p.lobby is not None and p.destination is not None and
not any(i for i in p.enemies if i in p.destination.passengers))]
total = 0
for passenger in pickup:
self.calculateScore(passenger)
total += passenger.score
rand.shuffle(pickup)
select = rand.random()*total
for passenger in pickup:
select -= passenger.score
if (select <= 0):
pickup.insert(0, pickup.pop(pickup.index(passenger)))
break
return pickup
def calculateScore(self, passenger):
pathScore = self.scorePath(simpleAStar.calculatePath(self.gameMap, self.me.limo.tilePosition, passenger.lobby.busStop))
pathScore += self.scorePath(simpleAStar.calculatePath(self.gameMap, passenger.lobby.busStop, passenger.destination.busStop))
passenger.score = passenger.pointsDelivered/float(pathScore)
if self.enemyAtDestination(passenger):
passenger.score = 0
def scorePath(self, path):
return len(path)
def findClosestStore(self):
"""Finds closest coffee store"""
closestStore = self.stores[0]
score = self.scorePath(self.calculatePathPlus1(self.me, closestStore.busStop))
for x in self.stores:
newScore = self.scorePath(self.calculatePathPlus1(self.me, x.busStop))
if newScore < score:
score = newScore
closestStore = x
return closestStore
def enemyAtDestination(self, passenger):
destination = passenger.destination
for enemy in passenger.enemies:
if destination == enemy.lobby and destination:
return True
return False