Example #1
0
def DoTurn2(pw):
  global turn_number
  turn_number += 1
  log.debug('turn number: ' + str(turn_number))
  
  try:
    if turn_number == 1:
      if FirstTurn.doFirstTurn(pw):
        return
    
    orders = []
    
    for myPlanet in pw.MyPlanets():
      
      closestPlanet = None
      #find the closest planet that we can take over
      for planet in pw.NotMyPlanets():
        
        if FleetIsEnRoute(myPlanet.PlanetID(), planet.PlanetID(), pw.MyFleets()):
          continue
        
        try:
          if planet.Owner() == 0:
            minFleetSize = planet.NumShips() + 1
          else:
            minFleetSize = planet.NumShips() + 1 + planet.GrowthRate() * pw.Distance(myPlanet.PlanetID(), planet.PlanetID())
        except Exception, e:
          log.exc(e)
          
        if minFleetSize >= myPlanet.NumShips(): # we can't take it over with just one planet
          continue
        distance = pw.Distance(myPlanet.PlanetID(), planet.PlanetID())
        worthiness = PlanetWorthinessToTakeOver(myPlanet, planet, minFleetSize, distance)
        log.debug('worthiness: ' + str(worthiness))
        
        if closestPlanet is None or worthiness > closestPlanet[1]:
          closestPlanet = (planet, worthiness, minFleetSize)
          log.debug('assigned, closestPlanet = ' + str(closestPlanet))
          
      log.debug('closest planet for planet: ' + str(myPlanet.PlanetID()) + ' : ' + str(closestPlanet))
      if closestPlanet is None:
        log.debug('from planet' + str(myPlanet.PlanetID()) + ', no closest planet to attack')
      else:
        log.debug('planet ' + str(myPlanet.PlanetID()) + ' will attack planet ' + str(closestPlanet[0].PlanetID()) + ' with worthiness' + str(closestPlanet[1]) + ', with a fleet of ' + str(closestPlanet[2]))
        orders.append((myPlanet.PlanetID(), closestPlanet[0].PlanetID(), closestPlanet[2]))
        
    filterOrders(orders, pw.Distance)
    
    log.debug('Starting to issue all orders')
    for order in orders:
      try:
        pw.IssueOrder(order[0], order[1], order[2])
      except Exception, e:
        log.exc(e)
Example #2
0
def DoTurn3(pw):
  global turn_number
  turn_number += 1
  try:
    #if turn_number == 1:
      #if FirstTurn.doFirstTurn(pw):
      #  return
    
    #for each of my planets
      #for each of the other planets
      
        #find the total number of fleets when we could get there from our planet, then determine the best planet to attack
    
    orders = []
    allGains = {}
    availableShips = {}
    
    for my_planet in pw.MyPlanets():
      
      availableShips[my_planet.PlanetID()] = max(0, my_planet.NumShips() - extra_ships)
      
      gains = []
      for other_planet in pw.Planets():
        if other_planet.GrowthRate() == 0: # not worth taking over
          continue
        if my_planet.PlanetID() == other_planet.PlanetID(): #same planet
          continue
        
        shipsAtArriveTime, time = pw.GetShipsAtArriveTime(my_planet.PlanetID(), other_planet.PlanetID())
        if shipsAtArriveTime[1] == 1:
          # we will have control of that planet, don't bother with it
          pass
        else:
          # should we try to take over?
          gain = 0 if time >= turns_ahead else (turns_ahead - time) * other_planet.GrowthRate() * (1 if shipsAtArriveTime == 0 else 2)
          
          gains.append((shipsAtArriveTime[0] + extra_ships, gain, other_planet.PlanetID()))
      
      allGains[my_planet.PlanetID()] = sorted(gains)
    
    log.debug('got all gains')
    
    orders = getBestOrders2(allGains, availableShips, pw)
    log.debug('got orders from getBestOrders1(): \n' + '\n'.join([str(x) for x in orders]))
    
    log.debug('Starting to issue all orders')
    for order in orders:
      try:
        pw.IssueOrder(order[0], order[1], order[2])
      except Exception, e:
        log.exc(e)
    log.debug('done issueing orders')
Example #3
0
def PlanetWorthinessToTakeOver(myPlanet, otherPlanet, minFleetSize, distance):
  try:
    enemyLoss = int(otherPlanet.Owner() == 2) * otherPlanet.GrowthRate()
    growthRate = otherPlanet.GrowthRate()
    
    log.debug('enemy loss: ' + str(enemyLoss))
    log.debug('growth rate: ' + str(growthRate))
    log.debug('min fleet size: ' + str(minFleetSize))
    
    result = 1.0 * (growthRate + enemyLoss) / (minFleetSize * ( 1.0 + distance)**2.0)
    
    log.debug('result of planet worthiness: ' + str(result))
    
    return result
  except Exception, e:
    log.exc(e)
    log.error('due to error, PlanetWorthinessToTakeOver() will return 0')
    return 0.0
Example #4
0
def doFirstTurn(pw):
  try:
    log.debug('Start of doFirstTurn()')
    myPlanets = pw.MyPlanets()
    if len(myPlanets) != 1:
      raise Exception("should always have 1 planet on turn on")
    myPlanet = myPlanets[0]
    if myPlanet.NumShips() != 100:
      raise Exception("should always have 100 ships on initial planet")
      
    availableShips = 99 # leave 5 behind minimum
      
    planets = []
    #try to get as much growth as possible, while also minimizing distance to planets, (and possible fleet loss)
    for planet in pw.NeutralPlanets():
      growthRate = planet.GrowthRate()
      distance = pw.Distance(myPlanet.PlanetID(), planet.PlanetID())
      minShips = planet.NumShips() + 1
      
      planets.append((minShips, growthRate, distance, planet))
    
    #log.debug('before sort: ' + '\n'.join([str(p) for p in planets]))
    planets.sort(cmp = distanceSort)
    #log.debug('after sort: ' + '\n'.join([str(p) for p in planets]))
    
    #not sure if the next line should be commented, or changed in some way
    planets = planets[:len(planets)/3]
      
    #log.debug('planets to actually consider: ' + '\n'.join([str(p) for p in planets]))

    planetsToConquer = getPlanetsToGoToFirst(planets, availableShips)
    
    for planet in planetsToConquer:
        log.debug('issueing order: planetId: ' + str(planet[0].PlanetID()) + ' with ' + str(planet[1]) + ' ships')
        pw.IssueOrder(myPlanet.PlanetID(), planet[0].PlanetID(), planet[1])
        
    log.debug('end of doFirstTurn()')
    return True
  except Exception, e:
    log.exc(e)
    return False
Example #5
0
def filterOrders(orders, distanceFunc):
  try:
    destinations = {}
    removeIndeces =[]
    for index, (source, destination, fleet) in enumerate(orders):
      if destinations.has_key(destination):
        distance1 = distanceFunc(source, destination)
        distance2 = distanceFunc(destinations[destination][0][0], destinations[destination][0][1])
        if distance1 < distance2:
          removeIndeces.append(destinations[destination][1])
        else:
          removeIndeces.append(index)          
      else:
        destinations[destination] = ((source, destination, fleet), index)
    
    log.debug('orders: ' + str(orders))
    log.debug('remove indeces: ' + str(removeIndeces))
    for index in sorted(removeIndeces, reverse = True):
      orders.pop(index)
    
    log.debug('orders after: ' + str(orders))
          
  except Exception, e:
    log.exc(e)
Example #6
0
def DoTurn5(pw):
  global wait_for_enemy
  global turn_number
  turn_number += 1
  try:
    
    if turn_number == 1:
      distance_to_enemy = pw.Distance(pw.MyPlanets()[0].PlanetID(), pw.EnemyPlanets()[0].PlanetID())
      if distance_to_enemy < distance_to_wait_first_turn:
        wait_for_enemy = True
        
    if wait_for_enemy:
      if len(pw.Fleets()) > 0:
        wait_for_enemy = False
      else:
        return
    
    #for each of my planets
      #for each of the other planets
      
        #find the total number of fleets when we could get there from our planet, then determine the best planet to attack
    
    orders = []
    allGains = {}
    stayHomeGain = getAvailableShips(pw)
    availableShips = {}
    
    log.debug('starting to get planet gains')
    for my_planet in pw.MyPlanets():
      
      gains = []
      for other_planet in pw.Planets():
        gainsDict = getPlanetGains(my_planet, other_planet, pw)
        #allGains[my_planet.PlanetID()] = getPlanetGains(my_planet, other_planet, pw)
        
        for ships in gainsDict:
          if gainsDict[ships] > 0:
            gains.append((ships, gainsDict[ships], other_planet.PlanetID()))
            
        log.debug('gains for planet ' + str(my_planet.PlanetID()) + ' : \n' + '\n'.join([(str(x) + ' : ' + str(gainsDict[x])) for x in gainsDict if gainsDict[x] > 0]))
      
        availableShips[my_planet.PlanetID()] = max(0, my_planet.NumShips() - extra_ships)
      
      allGains[my_planet.PlanetID()] = gains
    log.debug('done getting planet gains')
    log.debug('gains: ' + str(allGains))
    
    """
    allGains = {}
    for my_planet in pw.MyPlanets():
      gains = []
      
      #add the gains attained by leaving ships here
      for ships in stayHomeGain[my_planet.PlanetID()]:
        gains.append((ships, stayHomeGain[my_planet.PlanetID()][ships], my_planet.PlanetID()))
        
      for other_planet in pw.Planets():
        if other_planet.GrowthRate() == 0: # not worth taking over
          continue
        if my_planet.PlanetID() == other_planet.PlanetID(): #same planet
          continue
        
        availableShips[my_planet.PlanetID()] = max(0, my_planet.NumShips() - extra_ships)
        
        shipsAtArriveTime, time = pw.GetShipsAtArriveTime(my_planet.PlanetID(), other_planet.PlanetID())
        if shipsAtArriveTime[1] == 1:
          # we will have control of that planet, don't bother with it
          pass
        else:
          # should we try to take over?
          gain = 0 if time >= turns_ahead else (turns_ahead - time) * other_planet.GrowthRate() * (1 if shipsAtArriveTime == 0 else 2)
          
          gains.append((shipsAtArriveTime[0] + extra_ships, gain, other_planet.PlanetID()))
      
      allGains[my_planet.PlanetID()] = sorted(gains)
    """
    log.debug('got all gains')
    
    orders = getBestOrders3(allGains, availableShips, pw)
    
    log.debug('got orders from getBestOrders3(): \n' + '\n'.join([str(x) for x in orders]))
    log.debug(str(orders))
    log.debug('Starting to issue all orders')
    for order in orders:
      try:
        pw.IssueOrder(order[0], order[1], order[2])
      except Exception, e:
        log.exc(e)
    log.debug('done issueing orders')
Example #7
0
    orders = getBestOrders3(allGains, availableShips, pw)
    #orders += addStreamOrders(orders, availableShips, pw)
    #if turn_number == 1:
    #  orders = optimizeOrdersForTurn1(orders)
    log.debug('got orders from getBestOrders3(): \n' + '\n'.join([str(x) for x in orders]))
    
    log.debug('Starting to issue all orders')
    for order in orders:
      try:
        pw.IssueOrder(order[0], order[1], order[2])
      except Exception, e:
        log.exc(e)
    log.debug('done issueing orders')
    
  except Exception, e:
    log.exc(e)

def DoTurn5(pw):
  global wait_for_enemy
  global turn_number
  turn_number += 1
  try:
    
    if turn_number == 1:
      distance_to_enemy = pw.Distance(pw.MyPlanets()[0].PlanetID(), pw.EnemyPlanets()[0].PlanetID())
      if distance_to_enemy < distance_to_wait_first_turn:
        wait_for_enemy = True
        
    if wait_for_enemy:
      if len(pw.Fleets()) > 0:
        wait_for_enemy = False