Skip to content

Commit

Permalink
Merge pull request #45 from PixPanz/sophie
Browse files Browse the repository at this point in the history
base_client works now!
  • Loading branch information
GrimQuagle authored Jan 9, 2021
2 parents b47eae2 + 803cecb commit b1edc25
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 34 deletions.
15 changes: 14 additions & 1 deletion base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,24 @@ def team_name(self):
return 'Team Name'

# This is where your AI will decide what to do
def take_turn(self, turn, actions, world, truckCopy, timeCopy):
def take_turn(self, turn, actions, world, truck, time):
"""
This is where your AI will decide what to do.
:param turn: The current turn of the game.
:param actions: This is the actions object that you will add effort allocations or decrees to.
:param world: Generic world information
"""

if(truck.active_contract == None):
# Select contract
actions.set_action(ActionType.select_contract, 0)
elif(truck.body.current_gas < .2):
# Buy gas
actions.set_action(ActionType.buy_gas)
elif(truck.current_node.roads[0] != None):
# Move to next node
actions.set_action(ActionType.select_route, truck.current_node.roads[0])



pass
4 changes: 4 additions & 0 deletions game/common/road.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ def from_json(self, data):
self.road_type = data['road_type']
self.road_name = data['road_name']
self.length = data['length']

def is_equal(self, other):
return (isinstance(other, self.__class__) and self.road_name == other.road_name and self.road_type == other.road_type
and self.length == other.length)
54 changes: 27 additions & 27 deletions game/controllers/action_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ def __init__(self):
self.contract_list = list()

def handle_actions(self, player, obj=None):
player_action = player.action
player_action = player.action._chosen_action
# Without a contract truck has no node to move to, ensure a contract is always active
if player.truck.active_contract is not None or player_action == ActionType.select_contract:
#Call the appropriate method for this action

if(player_action == ActionType.select_contract):
#Checks if contract_list is empty. If so, we have a problem
if(len(self.contract_list) == 0): raise ValueError("Contract list cannot be empty")
Expand All @@ -38,33 +39,35 @@ def handle_actions(self, player, obj=None):

elif(player_action == ActionType.select_route):
#Moves the player to the node given in the action_parameter
self.move(player, player_action.action_parameter)
#self.move(player, player_action.action.action_parameter)
self.move(player)

else:
if(player_action == ActionType.buy_gas):
self.buy_gas(player)
if(player_action == ActionType.buy_gas):
self.buy_gas(player)

elif(player_action == ActionType.upgrade):
self.upgrade_level(self, player, obj)
elif(player_action == ActionType.upgrade):
self.upgrade_level(self, player, obj)

elif(player_action == ActionType.choose_speed):
#This is an ActionType because the user client cannot directly influence truck values.
player.truck.set_current_speed(player.action_parameter)
elif(player_action == ActionType.choose_speed):
#This is an ActionType because the user client cannot directly influence truck values.
player.truck.set_current_speed(player.action_parameter)

else:
self.print("Action aborted: no active contract!")
else:
self.print("Action aborted: no active contract!")

# Action Methods ---------------------------------------------------------
def move(self, player):
road = player.action.action_parameter

def move(self, player, road):
self.current_location = player.truck.current_node
time_taken = 0
luck = 1
fuel_efficiency = GameStats.costs_and_effectiveness[ObjectType.tires]['fuel_efficiency'][player.truck.tires]
if(isinstance(player.truck.addons, RabbitFoot)):
luck = 1 - GameStats.costs_and_effectiveness[ObjectType.rabbitFoot]['effectiveness'][player.truck.addons.level]

for route in self.current_location.roads:
if route is road: #May need to be redone
if route.is_equal(road): #May need to be redone
player.truck.current_node = self.current_location.next_node
self.event_controller.trigger_event(road, player, player.truck)
time_taken = (road.length / player.truck.get_current_speed()) * luck
Expand All @@ -76,24 +79,21 @@ def move(self, player, road):

# Retrieve by index and store in Player, then clear the list
def select_contract(self, player):
if len(self.contract_list) < int(player.action.contract_index):
player.truck.active_contract = self.contract_list[int(player.action.contract_index)]
player.truck.current_node = player.truck.active_contract.game_map.current_node
self.contract_list.clear()
else:
self.print("Contract list index was out of bounds")
player.truck.active_contract = self.contract_list[int(player.action.action_parameter)]
player.truck.current_node = player.truck.active_contract.game_map.current_node
self.contract_list.clear()

def buy_gas(self, player):
gasPrice = round(random.uniform(1, 5), 2) # gas price per percent
if(player.truck.money > 0):
percentRemain = player.truck.max_gas - round(player.truck.gas, 2)
maxPercent = round((player.truck.money / gasPrice) / 100, 2)
if(player.money > 0):
percentRemain = player.truck.body.max_gas - round(player.truck.body.current_gas, 2)
maxPercent = round((player.money / gasPrice) / 100, 2)
if(percentRemain < maxPercent):
player.truck.money -= percentRemain * gasPrice
player.truck.gas = player.truck.max_gas
player.money -= percentRemain * gasPrice
player.truck.body.current_gas = player.truck.body.max_gas
else:
player.truck.money = 0
player.truck.money += maxPercent
player.money = 0
player.money += maxPercent

def upgrade_body(self, player, objEnum, typ):
if objEnum is ObjectType.tank:
Expand Down
6 changes: 3 additions & 3 deletions game/controllers/event_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def __init__(self):
super().__init__()

def trigger_event(self, road, player, truck):
for i in range(len(GameStats.event_weights[road.road_type])-1):
GameStats.event_weights[road.road_type][i] -= truck.event_type_bonus[GameStats.possible_event_types[road.road_type][i]]
GameStats.event_weights[road.road_type][-1] += truck.event_type_bonus[GameStats.possible_event_types[road.road_type][i]]
#for i in range(len(GameStats.event_weights[road.road_type])-1):
#GameStats.event_weights[road.road_type][i] -= truck.event_type_bonus[GameStats.possible_event_types[road.road_type][i]]
#GameStats.event_weights[road.road_type][-1] += truck.event_type_bonus[GameStats.possible_event_types[road.road_type][i]]
# Picks random event type from those possible on given road
chosen_event_type = random.choices(GameStats.possible_event_types[road.road_type], weights=GameStats.event_weights[road.road_type], k=1)[0]
mods = self.negation(truck, chosen_event_type)
Expand Down
7 changes: 5 additions & 2 deletions game/controllers/master_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ def turn_logic(self, client, turn):
self.action_controller.handle_actions(client)
#client.time -= 10
if client.time <= 0:
self.print("Game is ending because time has run out.")
print("Game is ending because time has run out.")
self.game_over = True
if client.truck.health <= 0:
self.print("Game is ending because health has run out.")
print("Game is ending because health has run out.")
self.game_over = True
if client.truck.body.current_gas <= 0:
print("Game is ending because gas has run out.")
self.game_over = True


Expand Down
2 changes: 1 addition & 1 deletion game/utils/create_game_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def create_game_map(node_count, length):
roads = []
for j in range(random.randint(2,3)):
roads.append(Road("Route "+str(i)+"-"+str(j),
random.randint(0, 6), av_road_length + random.randint(-1 * road_deviation, road_deviation)))
random.randint(1, 6), av_road_length + random.randint(-1 * road_deviation, road_deviation)))
temp_node = Node(str(i), roads, None)
g_map.insert_node(temp_node)

Expand Down

0 comments on commit b1edc25

Please sign in to comment.