示例#1
0
def Assault(g, doupgrade, fleetstr, source, dest, owner):
  f = game.ParseFleet(fleetstr)
  print f

  built = 0

  # find a list of potential fleet builders
  print "looking for fleet builders..."
  total_fleets = 0
  fleet_builders = []
  for p in g.planets:
    if source == None or source.inside(p.location):
      p.load()
      count = p.how_many_can_build(f)
      if count > 0:
        print "planet " + str(p) + " can build " + str(count) + " fleets"
        p.distance_to_target = dest.distance(p.location)
        fleet_builders.append(p)
        total_fleets += count

  # sort fleet builders by distance to target
  fleet_builders = sorted(fleet_builders, key=lambda planet: planet.distance_to_target)

  print "found " + str(len(fleet_builders)) + " fleet building planets capable of building " + str(total_fleets) + " fleets"

  # load the sectors around the target point
  print "looking for owned planets at target location..."
  sect = g.load_sectors(dest.bounding_box())
  #print sect
  targets = []
  for p in sect["planets"]["owned"]:
    print p
    if p.owner == owner:
      routename = "assault%s" %(str(p.planetid))
      r = g.find_route(routename)
      if r == None:
        targets.append(p)

  print "found " + str(len(targets)) + " target planets"

  g.load_fleet_cache()
  build = 0
  if len(targets) > 0:
    print "building assault fleets"
    done = False
    for p in fleet_builders:
      if done:
        break

      # for this builder, find the closest unowned planets
      for t in targets:
        t.distance_to_target = game.distance_between(p.location, t.location)
      targets = sorted(targets, key=lambda planet: planet.distance_to_target)

      count = p.how_many_can_build(f);

      print "planet " + str(p) + " can build " + str(count) + " fleets"
      while not done and count > 0 and p.can_build(f):
        t = targets[0]
        print "looking to build to " + str(t) + " distance: " + str(t.distance_to_target)
        if doupgrade:
          fleet = p.build_fleet(f)
          if fleet:
            #fleet.move_to_planet(t)
            routename = "assault%s" %(str(t.planetid))
            r = g.find_route(routename)
            if r == None:
              print "building route"
              points = MakePoints(t.location)
              #print points

              r = g.create_route("assault%s" % (str(t.planetid)), True, points);
              #print r

            print "moving fleet to new route"
            if fleet.move_to_route(r) == False:
              print "error moving fleet"
          else:
            print " failed to build fleet"
            count = 0
            break

        # cull this target from the list
        targets.remove(t)
        built += 1
        count -= 1
        if len(targets) == 0:
          done = True

  if built > 0:
    if doupgrade:
      print "built %d fleets" % built
    else:
      print "would have built %d fleets" % built

  g.write_planet_cache()
  g.write_fleet_cache()
示例#2
0
def RedirAssaults(g, doupgrade):

  g.load_fleet_cache()
  g.load_planet_cache()
  g.load_routes()

  # make a list of assault routes
  assaultroutesowned = []
  assaultroutesnotowned = []
  for r in g.routes.keys():
    route = g.routes[r]
    assaultmatch = re.search(r'\Aassault([0-9]+)', route.name)
    if assaultmatch == None:
      continue

    #print "assault route %s" % route

    # get the planet id from the route name
    num = int(assaultmatch.group(1))
    route.assaultplanetid = num

    # see if it's one of our planets
    p = g.get_planet(num)

    if p:
      route.assaultplanet = p
      assaultroutesowned.append(route)
    else:
      assaultroutesnotowned.append(route)

  # special case: routes called 'toassault' will be considered as owned fleet routes
  for r in g.routes.keys():
    route = g.routes[r]

    if route.name == "toassault":
      route.assaultplanetid = 0
      assaultroutesowned.append(route)

  # special case: routes called 'assaultme' will be considered as unowned fleet routes
  for r in g.routes.keys():
    route = g.routes[r]

    if route.name == "assaultme":
      route.assaultplanetid = 0
      assaultroutesnotowned.append(route)

#  print "routes around owned planets:"
#  print assaultroutesowned
#  print "routes around unowned planets:"
#  print assaultroutesnotowned

  for route in assaultroutesnotowned:
    route.fleetcount = 0

  # go through the list of fleets orbiting planets we own and find a new place to redir them to
  for route in assaultroutesowned:
    for f in g.fleets:
      f.load()
      if f.routeid == route.routeid:
        print "fleet %s on assault route %s assaulting planet %d" % (f, route, route.assaultplanetid)
        #print route.points

        # we have a fleet orbiting a planet that is owned, find a nearby one to assault
        targets = sorted(assaultroutesnotowned, key=lambda route: game.distance_between(route.points[0], f.coords))
        #print targets

        # try to find a nearby target route, but not pile entirely on one
        # algorithm: pick the closest, least used target route from the list
        # of target routes within 20 units of distance of the nearest one
        mindistance = game.distance_between(targets[0].points[0], f.coords)

        minfleets = -1
        for t in targets:
          distance = game.distance_between(t.points[0], f.coords)
          if distance - mindistance < 20:
            if minfleets < 0 or t.fleetcount < minfleets:
              minfleets = t.fleetcount

        foundtarget = targets[0]
        for t in targets:
          distance = game.distance_between(t.points[0], f.coords)
          #print "%s %d %d %d" % (t, t.fleetcount, distance, mindistance)
          if distance - mindistance < 20:
            if t.fleetcount == minfleets:
              foundtarget = t
              break

        targetroute = foundtarget
        print "nearest route around planet under assault at %s" % targetroute

        targetroute.fleetcount += 1

        if doupgrade:
          print "moving fleet to new route"
          f.move_to_route(targetroute)

  g.write_planet_cache()
  g.write_fleet_cache()
示例#3
0
def BuildArcs(g, doupgrade, maxarcs, perplanet, leave, source, sink):

  # find a list of potential arc builders
  print "looking for arc builders..."
  total_arcs = 0
  arc_builders = []
  for p in g.planets:
    if source.inside(p.location):
      p.load()
      count = p.how_many_can_build({'arcs': 1})
      if count and p.society > 30 and p.population > 20000:
        print "planet " + str(p) + " can build " + str(count) + " arcs"
        p.distance_to_target = sink.distance(p.location)
        arc_builders.append(p)
        total_arcs += count

  # sort arc builders by distance to target
  arc_builders = sorted(arc_builders, key=lambda planet: planet.distance_to_target)

  print "found " + str(len(arc_builders)) + " arc building planets capable of building " + str(total_arcs) + " arcs"

  # load the sectors around the target point
  print "looking for unowned planets at target location..."
  sect = g.load_sectors(sink.bounding_box())
  #print sect
  unowned_targets = sect["planets"]["unowned"]

  # trim planets to ones strictly within the radius specified
  foo = []
  for p in unowned_targets:
    if sink.inside(p.location):
      foo.append(p)
  unowned_targets = foo

  print "found " + str(len(unowned_targets)) + " unowned planets"
  
  # trim the list of targets to ones that dont have an arc already incoming
  # 
  print "trimming list of unowned planets..."
  for f in g.fleets:
    f.load()
    try:
      if f.disposition == "Colonize":
      # look for destinations in the NAME-NUMBER form
        pnum = int(f.destination.split('-')[1])
        for p in unowned_targets:
          if p.planetid == pnum:
            print "fleet " + str(f) + " already heading for dest"
            unowned_targets.remove(p)
            break
    except:
      pass

  print "now have " + str(len(unowned_targets)) + " unowned planets"

  # build arcs
  built = 0
  if len(unowned_targets) > 0:
    print "building arcs..."
    arc = { 'arcs': 1 }
    done = False
    for p in arc_builders:
      if done:
        break

      count = p.how_many_can_build(arc);

      # trim the number we can build by the min left limit
      count -= leave
      if count <= 0:
        continue

      # trim the number we can build by per-planet limit
      if perplanet > 0 and count > perplanet:
        count = perplanet

      # for this builder, find the closest unowned planets
      for t in unowned_targets:
        t.distance_to_target = game.distance_between(p.location, t.location)
      unowned_targets = sorted(unowned_targets, key=lambda planet: planet.distance_to_target)

      print "planet " + str(p) + " can build " + str(count) + " arcs"
      while not done and count > 0 and p.can_build(arc):
          t = unowned_targets[0]
          print "looking to build to " + str(t) + " distance: " + str(t.distance_to_target)
          if doupgrade:
            fleet = p.build_fleet(arc)
            if fleet:
              fleet.move_to_planet(t)
            else:
              print " failed to build fleet"
              count = 0
              break
          
          # cull this target from the list
          unowned_targets.remove(t)
          built += 1 
          count -= 1
          maxarcs -= 1
          if (maxarcs == 0):
            done = True
          if len(unowned_targets) == 0:
            done = True

  if built > 0:
    if doupgrade:
      print "built %d arcs" % built
    else:
      print "would have built %d arcs" % built

  g.write_planet_cache()
  g.write_fleet_cache()
示例#4
0
def Assault(g, doupgrade, minfleet, source, sink, owner):
  built = 0

  # find a list of potential fleet builders
  print "looking for fleet builders..."
  total_fleets = 0
  fleet_builders = []
  for p in g.planets:
    if source.inside(p.location):
      p.load()
      count = p.how_many_can_build({'frigates': minfleet})
      if count > 0:
        print "planet " + str(p) + " can build " + str(count) + " fleets"
        p.distance_to_target = sink.distance(p.location)
        fleet_builders.append(p)
        total_fleets += count

  # sort fleet builders by distance to target
  fleet_builders = sorted(fleet_builders, key=lambda planet: planet.distance_to_target)

  print "found " + str(len(fleet_builders)) + " fleet building planets capable of building " + str(total_fleets) + " fleets"

  # load the sectors around the target point
  print "looking for owned planets at target location..."
  sect = g.load_sectors(sink.bounding_box())
  #print sect
  targets = []
  for p in sect["planets"]["owned"]:
    #print p
    if p.owner == owner:
      targets.append(p)

  print "found " + str(len(targets)) + " target planets"

  g.load_fleet_cache()
  build = 0
  if len(targets) > 0:
    print "building assault fleets"
    f = { 'frigates': minfleet }
    done = False
    for p in fleet_builders:
      if done:
        break

      # for this builder, find the closest unowned planets
      for t in targets:
        t.distance_to_target = game.distance_between(p.location, t.location)
      targets = sorted(targets, key=lambda planet: planet.distance_to_target)

      count = p.how_many_can_build(f);

      print "planet " + str(p) + " can build " + str(count) + " fleets"
      while not done and count > 0 and p.can_build(f):
        t = targets[0]
        print "looking to build to " + str(t) + " distance: " + str(t.distance_to_target)
        if doupgrade:
          fleet = p.build_fleet(f)
          if fleet:
            fleet.move_to_planet(t)
          else:
            print " failed to build fleet"
            count = 0
            break

        # cull this target from the list
        targets.remove(t)
        built += 1
        count -= 1
        if len(targets) == 0:
          done = True

  if built > 0:
    if doupgrade:
      print "built %d fleets" % built
    else:
      print "would have built %d fleets" % built

  g.write_planet_cache()
  g.write_fleet_cache()
示例#5
0
文件: arc.py 项目: ysei/davesgalaxy
def BuildArcs(g, doupgrade, maxarcs, perplanet, leave, source, sink, escort):

  escortfleet = game.ParseFleet(escort)
  print escortfleet

  # construct fleet we want to build
  arc = { 'arcs': 1 }
  arc.update(escortfleet)

  # find a list of potential arc builders
  print "looking for arc builders..."
  total_arcs = 0
  arc_builders = []
  for p in g.planets:
    if source.inside(p.location):
      p.load()
      count = p.how_many_can_build(arc)
      if count > 0 and ((p.society > 40 and p.population > 1000000) or p.population > 5000000):
        print "planet " + str(p) + " can build " + str(count) + " arcs"
        p.distance_to_target = sink.distance(p.location)
        arc_builders.append(p)
        total_arcs += count

  print "found " + str(len(arc_builders)) + " arc building planets capable of building " + str(total_arcs) + " arcs"

  # load the sectors around the target point
  print "looking for unowned planets at target location..."
  unowned_targets = game.FindUnownedPlanetsInShape(g, sink)
  
  print "found " + str(len(unowned_targets)) + " unowned planets"

  # trim the list of targets to ones that dont have an arc already incoming
  print "trimming list of unowned planets..."
  unowned_targets = game.TrimColonyTargettedPlanets(g, unowned_targets)
  print "now have " + str(len(unowned_targets)) + " unowned planets"

  # build arcs
  built = 0
  if len(unowned_targets) > 0:
    print "building arcs..."

    # sort the shortest distances for each arc builder to all the targets
    for p in arc_builders:
      p.targets = sorted(unowned_targets, key=lambda planet: game.distance_between(planet.location, p.location))
      p.arcs_can_build = p.how_many_can_build(arc) # delete later
      p.arcs_built = 0
  
    # iterate through the list, building the shortest builder -> target path until out of arcs or one of the passed in terminating conditions
    while len(arc_builders) > 0 and len(unowned_targets) > 0:
      # sort all of the arc builders by the shortest closest target
      arc_builders = sorted(arc_builders, key=lambda planet: game.distance_between(planet.location, planet.targets[0].location))

      p = arc_builders[0]
      t = p.targets[0]

      count = p.arcs_can_build

      # trim the number we can build by the min left limit
      count -= leave
      if count <= 0 or (perplanet > 0 and p.arcs_built >= perplanet):
        arc_builders.remove(p)
        continue

      #print "planet " + str(p) + " can build " + str(count) + " arcs"
      print "looking to build from " + str(p) + " to " + str(t) + " distance: " + str(game.distance_between(p.location, t.location))
      if doupgrade:
        fleet = p.build_fleet(arc)
        if fleet:
          fleet.move_to_planet(t)
        else:
          print " failed to build fleet"
          p.arcs_can_build = 0
          continue

      unowned_targets.remove(t)
      built += 1
      p.arcs_can_build -= 1
      p.arcs_built += 1

      # cull this target from the list
      for p in arc_builders:
        p.targets.remove(t)

      if maxarcs > 0 and built >= maxarcs:
        break

    # make sure we dont leave any extra metadata on the planets
    for p in arc_builders:
      del p.arcs_built
      del p.arcs_can_build
      del p.targets

  if built > 0:
    if doupgrade:
      print "built %d arc fleets" % built
    else:
      print "would have built %d arc fleets" % built

  g.write_planet_cache()
  g.write_fleet_cache()
示例#6
0
def SeedArcs(g, doupgrade, source, fleetstr, targetx, targety, targetradius, mindistance, maxdistance, size):

  fleet = game.ParseFleet(fleetstr)
  print fleet

  # find a list of potential builders
  print "looking for builders..."
  total = 0
  builders = []
  for p in g.planets:
    if source.inside(p.location):
      p.load()
      count = p.how_many_can_build(fleet)
      if count > 0:
        print "planet " + str(p) + " can build " + str(count) + " fleets"
        builders.append(p)
        total += count

  print "found " + str(len(builders)) + " building planets capable of building " + str(total) + " fleets"

  for p in builders:
    print p

    done = False
    while not done:
      if targetx >= 0 and targety >= 0:
        # if target x,y was passed in, select a spot around it
        startx = targetx
        starty = targety
      else:
        # pick a random spot approximately near the starting location
        startx = p.location[0]
        starty = p.location[1]

      x = random.uniform(startx - targetradius, startx + targetradius)
      y = random.uniform(starty - targetradius, starty + targetradius)

      distance = game.distance_between((startx,starty),(x,y))
      if distance > targetradius:
        continue

      distance = game.distance_between((x,y),p.location)
      if maxdistance > 0 and distance > maxdistance:
        continue
      if mindistance > 0 and distance < mindistance:
        continue

      #print x
      #print y

      #print "distance %f" % distance

      sect = g.load_sectors(((x-size,y-size),(x+size,y+size)))
      #print sect

      # make sure the sector is uninhabited
      if len(sect["planets"]["owned"]) > 0 or len(sect["planets"]["unowned"]) == 0:
        continue

      # trim sparse sectors
      numplanets = len(sect["planets"]["unowned"])
      if numplanets < 5:
        continue

      print "found a potential sect with %d planets at around %f %f" % (numplanets, x, y)
      done = True

      # pick a target
      target = sect["planets"]["unowned"][random.randrange(numplanets - 1)]
      print "target planet %s, distance %f" % (str(target), game.distance_between(p.location, target.location))

      if doupgrade:
        f = p.build_fleet(fleet)
        if f:
          f.move_to_planet(target)
        else:
          print " failed to build fleet"
          continue