def setUp(self): self.w = SecurityWorld() self.wt = worldtalker.WorldTalker(self.w) self.own_ai = SecurityAI(self.wt) self.other_ai = SecurityAI(self.wt) top_left = (0, 0) bottom_right = (self.w.mapSize - 1, self.w.mapSize - 1) self.top_left = top_left self.bottom_right = bottom_right s = self.w.Stats(ai_id=self.own_ai.ai_id, team=self.own_ai.team) s.ai = self.own_ai self.own_unit = self.w.createUnit(s, top_left) s = self.w.Stats(ai_id=self.other_ai.ai_id, team=self.other_ai.team) s.ai = self.other_ai self.other_unit = self.w.createUnit(s, bottom_right) b = mapobject.Building(self.wt) self.w.buildings[b] = self.own_ai self.w.map.placeObject(b, top_left) self.own_b = b b = mapobject.Building(self.wt) self.w.buildings[b] = self.other_ai self.w.map.placeObject(b, bottom_right) self.other_b = b
def setUp(self): self.w = SecurityWorld() self.w.teams = defaultdict(lambda: "<<UNDEFINED>>") self.w.team_map = defaultdict(lambda: "<<UNDEFINED>>") self.wt = worldtalker.WorldTalker(self.w) self.ai = SecurityAI(self.wt) top_left = (0,0) bottom_right = (self.w.mapSize-1, self.w.mapSize-1) self.top_left = top_left self.bottom_right = bottom_right s = self.w.Stats(ai_id=self.ai.ai_id, team=self.ai.team) s.ai = self.ai self.unit = self.w.createUnit(s, top_left) b = mapobject.Building(self.wt) self.w.buildings[b] = self.ai self.b = b b = mapobject.Building(self.wt)
def main(ai_classes=[]): w = world.World() wt = worldtalker.WorldTalker(w) global World World = w for ai in ai_classes: AI.append(ai(wt)) for ai in AI: ai._init() ai_cycler = itertools.cycle(AI) if settings.SAVE_IMAGES: surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 200, 200) cairo_context = cairo.Context(surface) for ai in AI: b = mapobject.Building(wt) w.buildings[b] = next(ai_cycler) w.map.placeObject(b, w.map.getRandomSquare()) for turn in xrange(LIFESPAN): for ai in AI: ai._spin() # try: # ai.spin() # except Exception, e: # log.info("AI raised exception %s, skipping this turn for it" % (e)) w.Turn() if settings.SAVE_IMAGES: worldmap.draw_map(cairo_context, 200, 200, AI, w) log.info("Finished simulating the world")
def placeRandomBuilding(self): b = mapobject.Building(self.wt) # Make sure this building is not within a distance # from any other buildings. attempts = 0 best_min_guess = 0 best_base = None while True: min_guess = map_settings.MAP_SIZE**2 attempts += 1 rand_square = self.map.getRandomSquare() within_range_of_other_building = False best_square = None dist_from_edge = math.sqrt(map_settings.MAP_SIZE) / 2 if rand_square[0] < dist_from_edge: continue if rand_square[1] < dist_from_edge: continue if map_settings.MAP_SIZE - rand_square[0] < dist_from_edge: continue if map_settings.MAP_SIZE - rand_square[1] < dist_from_edge: continue for building in self.buildings: pos = self.map.getPosition(building) if building == b or not pos: continue spawn_distance = map_settings.BUILDING_SPAWN_DISTANCE * math.log(map_settings.MAP_SIZE) dist = calcDistance(pos, rand_square) if dist < spawn_distance: within_range_of_other_building = True if dist < min_guess: min_guess = dist if min_guess > best_min_guess: best_square = rand_square best_min_guess = min_guess if not within_range_of_other_building: # Redistribute all buildings? break if attempts >= 5 and best_square: log.info("Couldn't place building far enough away after five tries, taking best guess") rand_square = best_square break self.map.placeObject(b, rand_square) return b
def add_building(self, ai=None): b = mapobject.Building(self.wt) self.world.buildings[b] = next(self.ai_cycler) self.world.map.placeObject(b, self.world.map.getRandomSquare())
def __init__(self, mapsize=None): if not mapsize: mapsize = map_settings.MAP_SIZE self.mapSize = mapsize self.wt = worldtalker.WorldTalker(self) self.AI = [] # Map a unit or AI to a team self.teams = {} # Maps a team to its AI self.team_map = {} self.ai_cycler = itertools.cycle(self.AI) self.ai_profiles = {} self.units = {} # instead of a list, it will point to the unit's attributes. self.all_units = {} # instead of a list, it will point to the unit's attributes. self.end_game_units = None # number of units alive at end of game self.under_attack = set() map_settings.MAP_SIZE = mapsize self.map = worldmap.Map(map_settings.MAP_SIZE) self.currentTurn = 0 self.events = set() self.unitevents = defaultdict(set) self.unitstatus = defaultdict(object) self.ai_units = defaultdict(set) self.ai_new_units = defaultdict(set) self.ai_dead_units = defaultdict(set) self.ai_lost_buildings = defaultdict(set) self.ai_new_buildings = defaultdict(set) self.ai_highlighted_regions = defaultdict(set) self.ai_highlighted_lines = defaultdict(set) state = random.getstate() random.seed(map_settings.SEED) self.map_state = random.getstate() random.setstate(state) self.execution_times = defaultdict(lambda: defaultdict(int)) self.buildings = {} self.units_per_base = {} # These contain the amount of time left for a building to spawn a unit self.spawn_counters = defaultdict(int) self.spawn_points = defaultdict(int) # contains how many units are left per building self.spawn_resources = defaultdict(int) if map_settings.SPAWN_POINTS: for coord in map_settings.SPAWN_POINTS: if not isValidSquare(coord, self.mapSize): continue b = mapobject.Building(self.wt) self.buildings[b] = None self.spawn_points[b] = None self.map.placeObject(b, coord) if map_settings.BUILDINGS: for coord in map_settings.BUILDINGS: if not isValidSquare(coord, self.mapSize): continue b = mapobject.Building(self.wt) self.buildings[b] = None self.map.placeObject(b, coord) log.info('Adding %s buildings to map', map_settings.ADDITIONAL_BUILDINGS) for i in xrange(map_settings.ADDITIONAL_BUILDINGS): self.buildings[self.placeRandomBuilding()] = None self.unitfullpaths = defaultdict(bool) self.endpaths = defaultdict(object) self.unitpaths = {} self.bulletpaths = {} self.bullets = {} self.died = {} self.corpses = {} self.collisions = defaultdict(int) self.survivors = {} self.dead_units = {} self.oldbullets = [] self.bullet_endings = defaultdict(bool) self.bulletRange = map_settings.MAP_SIZE/map_settings.BULLET_RANGE_MODIFIER self.bulletSpeed = map_settings.MAP_SIZE/map_settings.BULLET_SPEED_MODIFIER self.__initStats() self.visibleunits = defaultdict(set) self.visiblebuildings = defaultdict(set) self.__calcVisibility()