Exemple #1
0
 def __init__(self, game):
     self.game = game
     self.game_map = game.game_map
     self.me = game.me
     self.GV = GlobalVariablesSingleton.getInstance()
     self.NR_OF_PLAYERS = self.GV.NR_OF_PLAYERS
     self.GF = GlobalFunctions(self.game)
 def __init__(self, game):
     self.game = game
     self.game_map = game.game_map
     self.me = game.me
     GV = GlobalVariablesSingleton.getInstance()
     self.ENABLE_BACKUP = GV.ENABLE_BACKUP
     self.ENABLE_COMBAT = GV.ENABLE_COMBAT
     self.previous_state = GV.previous_state
     self.ship_state = GV.ship_state
     self.ship_path = GV.ship_path
     self.ship_dest = GV.ship_dest
     self.NR_OF_PLAYERS = GV.NR_OF_PLAYERS
 def __init__(self, game):
     self.game = game
     self.game_map = game.game_map
     self.me = game.me
     GV = GlobalVariablesSingleton.getInstance()
     self.ship_dest = GV.ship_dest
     self.ship_state = GV.ship_state
     self.ship_path = GV.ship_path
     self.previous_state = GV.previous_state
     self.NR_OF_PLAYERS = GV.NR_OF_PLAYERS
     self.GF = GlobalFunctions(self.game)
     self.inv_ship_dest = {}
     self.dropoff_distribution = {}
 def __init__(self, game, has_moved, command_queue, statemachine):
     self.game = game
     self.game_map = game.game_map
     self.me = game.me
     GV = GlobalVariablesSingleton.getInstance()
     self.ship_obj = GV.ship_obj
     self.ship_dest = GV.ship_dest
     self.previous_state = GV.previous_state
     self.ship_path = GV.ship_path
     self.ship_state = GV.ship_state
     self.previous_position = GV.previous_position
     self.has_moved = has_moved
     self.command_queue = command_queue
     self.NR_OF_PLAYERS = GV.NR_OF_PLAYERS
     self.GF = GlobalFunctions(self.game)
     self.SM = statemachine
Exemple #5
0
    def __init__(self, game, return_percentage):
        self.game = game
        self.game_map = game.game_map
        self.me = game.me
        self.return_percentage = return_percentage

        self.prcntg_halite_left = game.game_map.prcntg_halite_left
        GV = GlobalVariablesSingleton.getInstance()
        self.ENABLE_BACKUP = GV.ENABLE_BACKUP
        self.ENABLE_COMBAT = GV.ENABLE_COMBAT
        self.ship_path = GV.ship_path
        self.ship_state = GV.ship_state
        self.ship_dest = GV.ship_dest
        self.fleet_leader = GV.fleet_leader
        self.previous_state = GV.previous_state
        self.NR_OF_PLAYERS = GV.NR_OF_PLAYERS
        self.GF = GlobalFunctions(self.game)
        self.DP = DestinationProcessor(self.game)
Exemple #6
0
 def __init__(self, game):
     self.game = game
     self.game_map = game.game_map
     self.me = game.me
     GV = GlobalVariablesSingleton(game)
     self.GF = GlobalFunctions(self.game)
     self.ENABLE_BACKUP = GV.ENABLE_BACKUP
     self.ENABLE_COMBAT = GV.ENABLE_COMBAT
     self.ENABLE_INSPIRE = GV.ENABLE_INSPIRE
     self.ship_state = GV.ship_state
     self.ship_path = GV.ship_path
     self.ship_dest = GV.ship_dest
     self.previous_position = GV.previous_position
     self.previous_state = GV.previous_state
     self.fleet_leader = GV.fleet_leader
     self.ship_obj = GV.ship_obj
     self.NR_OF_PLAYERS = GV.NR_OF_PLAYERS
     self.crashed_positions = []
     self.cluster_centers = []
     self.clusters_determined = False
     self.crashed_ship_positions = []
     self.dropoff_last_built = 0
Exemple #7
0
    def mainloop(self):
        while True:
            self.game.update_frame(self.ENABLE_INSPIRE)
            self.game_map = self.game.game_map
            self.me = self.game.me
            GlobalVariablesSingleton.getInstance().turn_start = time.time()
            self.clear_dictionaries()  # of crashed or transformed ships
            command_queue = []

            if self.game.turn_number == 1:
                self.game_map.HALITE_STOP = GC.INITIAL_HALITE_STOP
                self.game_map.c = [GC.A, GC.B, GC.C, GC.D, GC.E,
                                   GC.F]  # set the heuristic constants
            for s in self.me.get_ships():
                if s.id not in self.ship_state:
                    self.ship_state[s.id] = "exploring"

            GlobalVariablesSingleton.getInstance(
            ).ENABLE_COMBAT = not self.have_less_ships(
                0.8) and self.NR_OF_PLAYERS == 2
            self.ENABLE_COMBAT = GlobalVariablesSingleton.getInstance(
            ).ENABLE_COMBAT
            GlobalVariablesSingleton.getInstance(
            ).ENABLE_BACKUP = self.ENABLE_COMBAT
            self.ENABLE_BACKUP = GlobalVariablesSingleton.getInstance(
            ).ENABLE_BACKUP
            GlobalVariablesSingleton.getInstance(
            ).ENABLE_INSPIRE = not self.have_less_ships(0.9)
            self.ENABLE_INSPIRE = GlobalVariablesSingleton.getInstance(
            ).ENABLE_INSPIRE

            # initialize shipyard halite, inspiring stuff and other
            self.game_map.init_map(self.me, list(self.game.players.values()),
                                   self.ENABLE_BACKUP)
            logging.info(f"Map initialized.{self.GF.time_left()} left")
            if self.game.turn_number == 1:
                TOTAL_MAP_HALITE = self.game_map.total_halite

            self.game_map.prcntg_halite_left = self.game_map.total_halite / TOTAL_MAP_HALITE
            # if clusters_determined and not cluster_centers:
            if self.game.turn_number >= GC.SPAWN_TURN:
                self.game_map.HALITE_STOP = self.game_map.prcntg_halite_left * GC.INITIAL_HALITE_STOP

            if self.crashed_ship_positions and self.game.turn_number < GC.CRASH_TURN and self.ENABLE_BACKUP:
                self.process_backup_sending()

            # Dijkstra the graph based on all dropoffs
            self.game_map.create_graph(self.GF.get_dropoff_positions())

            if self.game.turn_number == GC.DETERMINE_CLUSTER_TURN:
                self.clusters_determined = True
                self.cluster_centers = ClusterProcessor(
                    game).clusters_with_classifier()

            if self.game.turn_number == GC.CRASH_SELECTION_TURN:
                GC.CRASH_TURN = self.select_crash_turn()

            self.return_percentage = GC.BIG_PERCENTAGE if self.game.turn_number < GC.PERCENTAGE_SWITCH else GC.SMALL_PERCENTAGE
            if self.game_map.prcntg_halite_left < 0.15 and self.game_map.percentage_occupied >= GC.BUSY_PERCENTAGE:
                self.return_percentage = GC.BUSY_RETURN_AMOUNT / constants.MAX_HALITE

            if self.should_build():
                self.process_building()

            # has_moved ID->True/False, moved or not
            # ships priority queue of (importance, ship)
            ships, self.has_moved = self.ship_priority_q()
            # True if a ship moves into the shipyard this turn
            move_into_shipyard = False
            # whether a dropoff has been built this turn so that wouldnt use too much
            # halite
            dropoff_built = False
            SM = StateMachine(self.game, self.return_percentage)
            MP = MoveProcessor(self.game, self.has_moved, command_queue, SM)

            while ships:  # go through all ships
                ship = heappop(ships)[1]
                if self.has_moved[ship.id]:
                    continue
                if self.GF.time_left() < 0.15:
                    logging.info("STANDING STILL TOO SLOW")
                    command_queue.append(ship.stay_still())
                    self.ship_state[ship.id] = "collecting"
                    continue
                if ship.id not in self.previous_position:  # if new ship the
                    self.previous_position[ship.id] = self.me.shipyard.position

                # setup state
                # if ship hasnt received a destination yet
                if ship.id not in self.ship_dest or not ship.id in self.ship_state:
                    DestinationProcessor(self.game).find_new_destination(
                        self.game_map.halite_priority, ship)
                    self.previous_state[ship.id] = "exploring"
                    self.ship_state[ship.id] = "exploring"  # explore

                # transition
                SM.state_transition(ship)

                # logging.info("SHIP {}, STATE {}, DESTINATION {}".format(
                # ship.id, self.ship_state[ship.id], self.ship_dest[ship.id]))

                # if ship is dropoff builder
                if self.is_builder(ship):
                    # if enough halite and havent built a dropoff this turn
                    if (ship.halite_amount + self.me.halite_amount
                        ) >= constants.DROPOFF_COST and not dropoff_built:
                        command_queue.append(ship.make_dropoff())
                        self.dropoff_last_built = game.turn_number
                        GC.SPAWN_TURN += 10
                        dropoff_built = True
                    else:  # cant build
                        # wait in the position
                        self.ship_state[ship.id] = "waiting"
                        self.game_map[ship.position].mark_unsafe(ship)
                        command_queue.append(ship.move(Direction.Still))
                else:  # not associated with building a dropoff, so move regularly
                    move = MP.produce_move(ship)
                    if move is not None:
                        MP.move_ship(ship, move)

                self.clear_dictionaries()  # of crashed or transformed ships
                # This ship has made a move
                self.has_moved[ship.id] = True

            surrounded_shipyard = self.game_map.is_surrounded(
                self.me.shipyard.position)
            total_ships = max(
                1,
                sum([
                    self.get_ship_amount(pID)
                    for pID in self.game.players.keys()
                ]))
            halite_per_ship = self.game_map.total_halite / total_ships
            if self.NR_OF_PLAYERS == 4:
                if self.game_map.width <= 40:
                    VALUE_PER_SHIP = 1
                else:
                    VALUE_PER_SHIP = 1.5
            else:
                VALUE_PER_SHIP = 0.8

            if not dropoff_built and self.me.halite_amount >= constants.SHIP_COST and self.max_enemy_ships() + 6 > len(
                    self.me.get_ships()) and self.game.turn_number <= GC.SPAWN_TURN\
                    and halite_per_ship >= VALUE_PER_SHIP * constants.SHIP_COST and self.game_map.prcntg_halite_left > (1 - 0.65) and \
                    not (self.game_map[
                             self.me.shipyard].is_occupied or surrounded_shipyard or "waiting" in self.ship_state.values()):
                if not ("build" in self.ship_state.values()
                        and self.me.halite_amount <=
                        (constants.SHIP_COST + constants.DROPOFF_COST)):
                    command_queue.append(self.me.shipyard.spawn())

            logging.info(self.GF.time_left())
            self.game.end_turn(command_queue)
 def time_left():
     return 2 - (time.time() - GlobalVariablesSingleton.getInstance().turn_start)