def __init__(self): # Initialize Widgets self.window = gtk.Window() box = gtk.VBox() screen = gtk.gdk.screen_get_default() self.drawSize = min(screen.get_width(), screen.get_height()) self.map_area = gtk.DrawingArea() box.pack_start(self.map_area) self.window.add(box) self.window.show_all() self.map_area.connect("expose-event", self.map_expose_event_cb) self.window.resize(250, 250) self.window.connect("destroy", end_threads) # Initialize the world self.world = world.World() self.wt = worldtalker.WorldTalker(self.world) self.AI = [] self.ai_cycler = itertools.cycle(self.AI) self.colors = {} self.guiTurn = 0 # Initialize our pixbuf queue self.stopped = False self.frame_queue = Queue.Queue(BUFFER_SIZE) self.lock = RLock()
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 __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()