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 draw_map(self, world_data, turn_data): width = self.drawSize height = self.drawSize surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, width, height) cr = cairo.Context (surface) gdkcr = gtk.gdk.CairoContext (cr) worldmap.draw_map(gdkcr, width, height, world_data, turn_data) self.guiTurn += 1 # Draw the map cairo_context_final = self.map_area.window.cairo_create() pattern = cairo.SurfacePattern(surface) allocation = self.map_area.get_allocation() width = allocation.width height = allocation.height sx = width / float(self.drawSize) sy = height / float(self.drawSize) matrix = cairo.Matrix(xx=sx, yy=sy) cairo_context_final.transform(matrix) cairo_context_final.set_source(pattern) cairo_context_final.paint()
def save_map_to_queue(self): width = self.drawSize height = self.drawSize surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, width, height) cr = cairo.Context (surface) gdkcr = gtk.gdk.CairoContext (cr) worldmap.draw_map(gdkcr, width, height, self.AI, self.world) self.frame_queue.put(surface)