def create_map(width, height, grounds, own_grounds, min_pts, max_pts): """ Function that creates Tile Map based on User's input :returns: Tile Map object """ if min_pts is None: if max_pts is None: tmap = TileMap(width, height, grounds, own_grounds, min_pts=min_pts, max_pts=max_pts) else: tmap = TileMap(width, height, grounds, own_grounds, min_pts=min_pts) elif (min_pts is None) and (max_pts is None): tmap = TileMap(width, height, grounds, own_grounds, max_pts=max_pts) else: tmap = TileMap(width, height, grounds, own_grounds) return tmap
def __init__(self, MAP_W=12, MAP_H=12): os.chdir(MICROPOLISCORE_DIR) self.SHOW_GUI = False engine, win1 = main.train() os.chdir(CURR_DIR) self.engine = engine # print(dir(self.engine)) self.MAP_X = MAP_W self.MAP_Y = MAP_H self.MAP_XS = 5 self.MAP_YS = 5 self.engineTools = [ 'Residential', 'Commercial', 'Industrial', 'FireDept', 'PoliceDept', # TODO: implement query (skipped for now by indexing) 'Query', 'Wire', 'Clear', 'Rail', 'Road', 'Stadium', 'Park', 'Seaport', 'CoalPowerPlant', 'NuclearPowerPlant', 'Airport', 'Net', 'Water', 'Land', 'Forest', ] self.tools = [ 'Residential', 'Commercial', 'Industrial', 'Road', 'Wire', 'NuclearPowerPlant', 'Park', 'Clear', None ] # since query is exluded for now: self.num_tools = len(self.tools) # map with extra channel charting position of walker self.map = TileMap(self, self.MAP_X, self.MAP_Y, walker=True) self.zones = self.map.zones self.num_zones = self.map.num_zones # allows building on rubble and forest self.engine.autoBulldoze = True # for bots win1.playCity() self.engine.setFunds(1000000) engine.setSpeed(3) engine.setPasses(500) #engine.simSpeed =99 engine.clearMap() self.win1 = win1
def __init__(self, MAP_W=6, MAP_H=6): self.SHOW_GUI = False engine, win1 = main.train() os.chdir(CURR_DIR) self.engine = engine # print(dir(self.engine)) self.MAP_X = MAP_W self.MAP_Y = MAP_H # shifts build area to centre of 120 by 100 tile map # self.MAP_XS = 59 - self.MAP_X // 2 # self.MAP_YS = 49 - self.MAP_Y //2 self.MAP_XS = 1 self.MAP_YS = 1 self.engineTools = [ 'Residential', 'Commercial', 'Industrial', 'FireDept', 'PoliceDept', # TODO: implement query (skipped for now by indexing) 'Query', 'Wire', 'Clear', 'Rail', 'Road', 'Stadium', 'Park', 'Seaport', 'CoalPowerPlant', 'NuclearPowerPlant', 'Airport', 'Net', 'Water', 'Land', 'Forest', ] self.tools = [ 'Residential', 'Commercial', 'Industrial', 'Road', 'Wire', 'NuclearPowerPlant', 'Park', 'Clear' ] # since query is exluded for now: self.num_tools = len(self.tools) self.map = TileMap(self) self.zones = self.map.zones self.num_zones = self.map.num_zones # allows building on rubble and forest self.engine.autoBulldoze = True # for bots win1.playCity() self.engine.setFunds(1000000) engine.setSpeed(3) engine.setPasses(500) #engine.simSpeed =99 engine.clearMap() self.win1 = win1
class Level: def __init__(self, file, screen): self.__tile_map = TileMap(file, screen) self.done = False def keydown(self, key): self.__tile_map.mario.keydown(key) def keyup(self, key): self.__tile_map.mario.keyup(key) def update(self): self.__tile_map.update() def render(self): self.__tile_map.render()
def test_central_pts_to_array(): ls_of_rows = [ j for i in tilemap1.central_pts_to_array( tilemap1.zeros_array.tolist(), TileMap.amount_central_pts(tilemap1.width, tilemap1.height, tilemap1.grounds, tilemap1.own_grounds, tilemap1.min_pts, tilemap1.max_pts)) for j in i ] assert ((51, 153, 255) in ls_of_rows) is True assert ((125, 200, 100) in ls_of_rows) is True
def create_tilemap(map, data): tilemap = TileMap() for location, tile in get_tiles(map, data): tilemap.set_cube(*location, tile) return tilemap
import game from Box2D import b2World game.init(world=b2World(gravity=(0, 0), doSleep=True)) #import rule_connect #rule_connect.add() from camera import Camera Camera().add() import camera_control camera_control.add() import b2draw b2draw.init() '''from firefly import Firefly import random for _ in range(100): Firefly(random.randint(-40, 40), random.randint(-30, 30)).add()''' from tilemap import TileMap, SolidBlock tilemap = TileMap((0, 0), (10, 10)) tilemap.add() game.start()
from tilemap import TileMap tilemap1 = TileMap(20, 30, [], {}, 10, 20) tilemap2 = TileMap(20, 30, ['water', 'land', 'sand'], {'black': (0, 0, 0)}) tilemap3 = TileMap(10, 10, [], {}, 4) def test_colors_to_use(): assert tilemap1.colors_to_use() == [(51, 153, 255), (125, 200, 100)] assert tilemap2.colors_to_use() == [(51, 153, 255), (125, 200, 100), (255, 255, 153), (0, 0, 0)] def test_central_pts_to_array(): ls_of_rows = [ j for i in tilemap1.central_pts_to_array( tilemap1.zeros_array.tolist(), TileMap.amount_central_pts(tilemap1.width, tilemap1.height, tilemap1.grounds, tilemap1.own_grounds, tilemap1.min_pts, tilemap1.max_pts)) for j in i ] assert ((51, 153, 255) in ls_of_rows) is True assert ((125, 200, 100) in ls_of_rows) is True def test_amount_central_pts(): assert tilemap1.amount_central_pts( tilemap1.width, tilemap1.height, tilemap1.grounds, tilemap1.own_grounds, tilemap1.min_pts, tilemap1.max_pts) >= 10 assert tilemap1.amount_central_pts(
class MicroWalkControl(): def __init__(self, MAP_W=12, MAP_H=12): os.chdir(MICROPOLISCORE_DIR) self.SHOW_GUI = False engine, win1 = main.train() os.chdir(CURR_DIR) self.engine = engine # print(dir(self.engine)) self.MAP_X = MAP_W self.MAP_Y = MAP_H self.MAP_XS = 5 self.MAP_YS = 5 self.engineTools = [ 'Residential', 'Commercial', 'Industrial', 'FireDept', 'PoliceDept', # TODO: implement query (skipped for now by indexing) 'Query', 'Wire', 'Clear', 'Rail', 'Road', 'Stadium', 'Park', 'Seaport', 'CoalPowerPlant', 'NuclearPowerPlant', 'Airport', 'Net', 'Water', 'Land', 'Forest', ] self.tools = [ 'Residential', 'Commercial', 'Industrial', 'Road', 'Wire', 'NuclearPowerPlant', 'Park', 'Clear', None ] # since query is exluded for now: self.num_tools = len(self.tools) # map with extra channel charting position of walker self.map = TileMap(self, self.MAP_X, self.MAP_Y, walker=True) self.zones = self.map.zones self.num_zones = self.map.num_zones # allows building on rubble and forest self.engine.autoBulldoze = True # for bots win1.playCity() self.engine.setFunds(1000000) engine.setSpeed(3) engine.setPasses(500) #engine.simSpeed =99 engine.clearMap() self.win1 = win1 def layGrid(self, w, h): for i in range(self.MAP_X): for j in range(self.MAP_Y): gtk.mainiteration() self.engine.simTick() # vertical road if ((i + 4) % w == 0): self.doTool(i, j, 'Road') if ((j + 1) % h in [1, h - 1]) and \ j not in [0, self.MAP_Y -1]: self.doTool(i, j, 'Wire') # horizontal roads elif ((j + 1) % h == 0): self.doTool(i, j, 'Road') if ((i + 4) % w in [1, w - 1]) and \ i not in [0, self.MAP_X - 1]: self.doTool(i, j, 'Wire') # random zones elif ((i + 2 - (i + 4) // w) % 3) ==0 and \ ((j + 2 - (j + 1) // h) % 3) ==0: tool_i = random.randint(0, 3 - 1) self.doTool(i, j, ['Residential', 'Commercial', 'Industrial'][tool_i]) def clearMap(self): self.engine.clearMap() self.map.setEmpty() def getFunds(self): return self.engine.totalFunds def setFunds(self, funds): return self.engine.setFunds(funds) # called by map module def doBulldoze(self, x, y): return self.doSimTool(x, y, 'Clear') def doBotTool(self, x, y, tool): '''Takes string for tool''' return self.map.addZone(x, y, tool) def doTool(self, x, y, tool): '''Takes string for tool''' return self.map.addZone(x, y, tool) def toolDown(self, x, y, tool): '''Takes int for tool, depending on engine's index''' self.map.addZone(x, y, self.engineTools[tool]) # called by map module def doSimTool(self, x, y, tool): x += self.MAP_XS y += self.MAP_YS x = np.int(x) y = np.int(y) # gtk.mainiteration() return self.engine.toolDown(self.engineTools.index(tool), x, y) def singleStep(self, a, i): ''' a is an int between 0 and 4 * i, where i > 0''' endpos = self.map.walker_pos if a // i == 0: endpos[0] += 1 elif a // i == 1: endpos[1] += 1 elif a // i == 2: endpos[0] -= 1 elif a // i == 3: endpos[1] -= 1 endpos[0] = np.clip(endpos[0], 0, self.MAP_X - 1) endpos[1] = np.clip(endpos[1], 0, self.MAP_Y - 1) self.map.walker_pos = endpos def getResPop(self): return self.engine.resPop def getComPop(self): return self.engine.comPop def getIndPop(self): return self.engine.indPop def getTotPop(self): return self.engine.totPop def takeSetupAction(self, a): tool = self.tools[a[0]] x = a[1] y = a[2] self.doTool(x, y, tool) def takeAction(self, xstep, ystep, tool): '''tool int depends on self.tools indexing''' # print(xstep, ystep, tool) tool = self.tools[tool] x = np.clip(self.map.walker_pos[0] + xstep, 0, self.MAP_X - 1) y = np.clip(self.map.walker_pos[1] + ystep, 0, self.MAP_Y - 1) if tool: self.doBotTool(x, y, tool) # gtk.mainiteration() self.engine.simTick() def close(self): # self.engine.doReallyQuit() del (self.engine)
def __init__(self, file, screen): self.__tile_map = TileMap(file, screen) self.done = False
class MicropolisControl(): def __init__(self, MAP_W=12, MAP_H=12, PADDING=13, parallel_gui=False): self.parallel_gui = parallel_gui self.pgui = None if parallel_gui: import pexpect self.pgui = pexpect.spawn('/bin/bash') self.pgui.expect('sme') self.pgui.sendline('cd gym-micropolis') self.pgui.expect('sme') self.pgui.sendline('pwd') self.pgui.expect('sme') self.pgui.sendline('python2') self.pgui.expect('>>>') self.pgui.sendline( 'from gym_micropolis.envs.gui import MicropolisGUI') self.pgui.expect('>>>') self.pgui.sendline('m = MicropolisGUI({}, {})'.format( MAP_W, MAP_H)) self.pgui.expect('>>>') self.pgui.sendline('m.render()') self.pgui.expect('>>>') self.SHOW_GUI = False engine, win1 = main.train(bot=self) os.chdir(CURR_DIR) self.engine = engine self.engine.setGameLevel(2) self.MAP_X = MAP_W self.MAP_Y = MAP_H self.PADDING = PADDING # shifts build area to centre of 120 by 100 tile map # self.MAP_XS = 59 - self.MAP_X // 2 # self.MAP_YS = 49 - self.MAP_Y //2 self.MAP_XS = 1 self.MAP_YS = 1 self.num_roads = 0 self.engineTools = [ 'Residential', 'Commercial', 'Industrial', 'FireDept', 'PoliceDept', # TODO: implement query (skipped for now by indexing) 'Query', 'Wire', 'Clear', 'Rail', 'Road', 'Stadium', 'Park', 'Seaport', 'CoalPowerPlant', 'NuclearPowerPlant', 'Airport', 'Net', 'Water', 'Land', 'Forest', ] # Names correspond to those of resultant zones self.tools = [ 'Residential', 'Commercial', 'Industrial', 'FireDept', 'PoliceDept', # 'Query', 'Wire', 'Clear', 'Rail', 'Road', 'Stadium', 'Park', 'Seaport', 'CoalPowerPlant', 'NuclearPowerPlant', 'Airport', 'Net', 'Water', 'Land', 'Forest', ] #['Residential','Commercial','Industrial','Road','Wire','NuclearPowerPlant', 'Park', 'Clear'] # since query is exluded for now: self.num_tools = len(self.tools) self.map = TileMap(self, self.MAP_X + 2 * PADDING, self.MAP_Y + 2 * PADDING) self.zones = self.map.zones self.num_zones = self.map.num_zones # allows building on rubble and forest self.engine.autoBulldoze = True # for bots win1.playCity() self.engine.setFunds(1000000) engine.setSpeed(3) engine.setPasses(500) #engine.simSpeed =99 self.total_traffic = 0 self.last_total_traffic = 0 # engine.clearMap() self.win1 = win1 def layGrid(self, w, h): for i in range(self.MAP_X): for j in range(self.MAP_Y): # gtk.mainiteration() self.engine.simTick() # vertical road if ((i + 4) % w == 0): self.doTool(i, j, 'Road') if ((j + 1) % h in [1, h - 1]) and \ j not in [0, self.MAP_Y -1]: self.doTool(i, j, 'Wire') # horizontal roads elif ((j + 1) % h == 0): self.doTool(i, j, 'Road') if ((i + 4) % w in [1, w - 1]) and \ i not in [0, self.MAP_X - 1]: self.doTool(i, j, 'Wire') # random zones elif ((i + 2 - (i + 4) // w) % 3) ==0 and \ ((j + 2 - (j + 1) // h) % 3) ==0: tool_i = random.randint(0, 3 - 1) self.doTool(i, j, ['Residential', 'Commercial', 'Industrial'][tool_i]) def clearMap(self): self.engine.clearMap() self.map.setEmpty() if self.parallel_gui: self.pgui.sendline('m.clearMap()') self.pgui.expect('>>>') def getPopDensityMap(self): pop_density_map = np.zeros((1, self.MAP_X, self.MAP_Y)) for i in range(self.MAP_X): for j in range(self.MAP_Y): im = i + self.MAP_XS jm = j + self.MAP_YS im -= 2 jm -= 2 pop_density_map[0][i][j] = self.engine.getPopulationDensity( im, jm) return pop_density_map def getTrafficDensityMap(self): self.last_total_traffic = self.total_traffic self.total_traffic = 0 traffic_density_map = np.zeros((1, self.MAP_X, self.MAP_Y)) for i in range(self.MAP_X): for j in range(self.MAP_Y): im = i + self.MAP_XS jm = j + self.MAP_YS im -= 2 jm -= 4 xy_density = self.engine.getTrafficDensity(im, jm) self.total_traffic += xy_density traffic_density_map[0][i][j] = self.engine.getTrafficDensity( im, jm) return traffic_density_map def getPowerMap(self): power_map = np.zeros((1, self.MAP_X, self.MAP_Y)) for i in range(self.MAP_X): for j in range(self.MAP_Y): im = i + self.MAP_XS jm = j + self.MAP_YS power_map[0][i][j] = self.engine.getPowerGrid(im, jm) return power_map def getFunds(self): return self.engine.totalFunds def render(self): while gtk.events_pending(): gtk.main_iteration() def setFunds(self, funds): return self.engine.setFunds(funds) # called by map module def doBulldoze(self, x, y): return self.doSimTool(x, y, 'Clear') def doBotTool(self, x, y, tool, static_build=False): '''Takes string for tool''' return self.map.addZone(x + self.PADDING, y + self.PADDING, tool, static_build) def doTool(self, x, y, tool): '''Takes string for tool''' return self.map.addZone(x, y, tool) def playerToolDown(self, tool_int, x, y): zone_int = self.map.zoneInts[self.engineTools[tool_int]] #x += self.MAP_XS #y += self.MAP_YS self.map.addZoneSquare(zone_int, x, y, static_build=True) def toolDown(self, x, y, tool): '''Takes int for tool, depending on engine's index''' self.map.addZone(x, y, self.engineTools[tool]) # called by map module def doSimTool(self, x, y, tool): x += self.MAP_XS y += self.MAP_YS tool = self.engineTools.index(tool) return self.doSimToolInt(x, y, tool) def doSimToolInt(self, x, y, tool): if self.parallel_gui: self.pgui.sendline('m.doSimToolInt({}, {}, {})'.format(x, y, tool)) self.pgui.expect('>>>') return self.engine.toolDown(tool, x, y) def getResPop(self): return self.engine.resPop def getComPop(self): return self.engine.comPop def getIndPop(self): return self.engine.indPop def getTotPop(self): return self.engine.totalPop def takeSetupAction(self, a): tool = self.tools[a[0]] x = a[1] y = a[2] self.doTool(x, y, tool) def takeAction(self, a, static_build=False): '''tool int depends on self.tools indexing''' tool = self.tools[a[0]] x = int(a[1]) y = int(a[2]) self.doBotTool(x, y, tool, static_build) self.engine.simTick() # gtk.mainiteration() def close(self): # self.engine.doReallyQuit() del (self.engine)
class MicropolisControl(): def __init__(self, MAP_W=6, MAP_H=6): self.SHOW_GUI = False engine, win1 = main.train() os.chdir(CURR_DIR) self.engine = engine # print(dir(self.engine)) self.MAP_X = MAP_W self.MAP_Y = MAP_H # shifts build area to centre of 120 by 100 tile map # self.MAP_XS = 59 - self.MAP_X // 2 # self.MAP_YS = 49 - self.MAP_Y //2 self.MAP_XS = 1 self.MAP_YS = 1 self.engineTools = [ 'Residential', 'Commercial', 'Industrial', 'FireDept', 'PoliceDept', # TODO: implement query (skipped for now by indexing) 'Query', 'Wire', 'Clear', 'Rail', 'Road', 'Stadium', 'Park', 'Seaport', 'CoalPowerPlant', 'NuclearPowerPlant', 'Airport', 'Net', 'Water', 'Land', 'Forest', ] self.tools = [ 'Residential', 'Commercial', 'Industrial', 'Road', 'Wire', 'NuclearPowerPlant', 'Park', 'Clear' ] # since query is exluded for now: self.num_tools = len(self.tools) self.map = TileMap(self) self.zones = self.map.zones self.num_zones = self.map.num_zones # allows building on rubble and forest self.engine.autoBulldoze = True # for bots win1.playCity() self.engine.setFunds(1000000) engine.setSpeed(3) engine.setPasses(500) #engine.simSpeed =99 engine.clearMap() self.win1 = win1 def layGrid(self, w, h): for i in range(self.MAP_X): for j in range(self.MAP_Y): gtk.mainiteration() self.engine.simTick() # vertical road if ((i + 4) % w == 0): self.doTool(i, j, 'Road') if ((j + 1) % h in [1, h - 1]) and \ j not in [0, self.MAP_Y -1]: self.doTool(i, j, 'Wire') # horizontal roads elif ((j + 1) % h == 0): self.doTool(i, j, 'Road') if ((i + 4) % w in [1, w - 1]) and \ i not in [0, self.MAP_X - 1]: self.doTool(i, j, 'Wire') # random zones elif ((i + 2 - (i + 4) // w) % 3) ==0 and \ ((j + 2 - (j + 1) // h) % 3) ==0: tool_i = random.randint(0, 3 - 1) self.doTool(i, j, ['Residential', 'Commercial', 'Industrial'][tool_i]) def clearMap(self): self.engine.clearMap() self.map.setEmpty() def getFunds(self): return self.engine.totalFunds def setFunds(self, funds): return self.engine.setFunds(funds) # called by map module def doBulldoze(self, x, y): return self.doSimTool(x, y, 'Clear') def doTool(self, x, y, tool): '''Takes string for tool''' return self.map.addZone(x, y, tool) def toolDown(self, x, y, tool): '''Takes int for tool, depending on engine's index''' self.map.addZone(x, y, self.engineTools[tool]) # called by map module def doSimTool(self, x, y, tool): x += self.MAP_XS y += self.MAP_YS return self.engine.toolDown(self.engineTools.index(tool), x, y) def getResPop(self): return self.engine.resPop def getComPop(self): return self.engine.comPop def getIndPop(self): return self.engine.indPop def getTotPop(self): return self.engine.totPop def takeAction(self, a): '''tool int depends on self.tools indexing''' # if self.SHOW_GUI: # print('show gui') # gtk.mainiteration() self.engine.simTick() tool = self.tools[a[0]] x = a[1] y = a[2] self.doTool(x, y, tool) def close(self): # self.engine.doReallyQuit() del (self.engine)
import datashader as ds from datashader.utils import export_image import datashader.transfer_functions as tf import pandas as pd from colorcet import fire from PIL import Image from tilemap import TileMap point_df = pd.read_parquet("point_data.gzip") bounds = [1.288423e+07, 1.291500e+07, -3.772000e+06, -3.750000e+06] filter_point_df = point_df[(point_df["x"] >= bounds[0]) & (point_df["x"] <= bounds[1]) & (point_df["y"] >= bounds[2]) & (point_df["y"] <= bounds[3])] filter_point_desc_df = filter_point_df.describe() aspect_ratio = (filter_point_desc_df.loc["max", "y"] - filter_point_desc_df.loc["min", "y"])/(filter_point_desc_df.loc["max", "x"] - filter_point_desc_df.loc["min", "x"]) dim = 2000 cvs = ds.Canvas(plot_width=dim, plot_height=int(dim * aspect_ratio)) agg = cvs.points(filter_point_df, 'x', 'y') img = tf.shade(agg, cmap=fire) figname = 'perth_wires' export_image(img, figname, background="black") render_map = TileMap(extents=(115.7410072222, -32.0661730556, 116.0174188889, -31.8985416667)) map_image = render_map.render(zoom=14) map_image_resized = map_image.resize((2000, 1430), resample=Image.LANCZOS) map_image_resized.putalpha(255) map_image_resized.save("perth_street_map.png")
import sys import pygame from tilemap import TileMap pygame.init() screen = pygame.display.set_mode((1280, 720)) pygame.display.set_caption("Test") tile_map = TileMap(screen) # tile_map.load_map("tile_map.json") tile_map.load_map("level1.json") tile_map.update(0, 0) x = 0 y = 0 direction = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: direction = 1 elif event.key == pygame.K_UP: direction = 2
class MicropolisPaintControl(): def __init__(self, MAP_W=12, MAP_H=12, PADDING=13, parallel_gui=False, rank=None): self.SHOW_GUI=False engine, win1 = main.train(bot=self, rank=rank) os.chdir(CURR_DIR) self.engine = engine self.engine.setGameLevel(2) self.MAP_X = MAP_W self.MAP_Y = MAP_H self.PADDING = PADDING # shifts build area to centre of 120 by 100 tile map # self.MAP_XS = 59 - self.MAP_X // 2 # self.MAP_YS = 49 - self.MAP_Y //2 self.MAP_XS = 5 self.MAP_YS = 5 self.num_roads = 0 self.engineTools = ['Residential', 'Commercial', 'Industrial', 'FireDept', 'PoliceDept', # TODO: implement query (skipped for now by indexing) 'Query', 'Wire', 'Clear', 'Rail', 'Road', 'Stadium', 'Park', 'Seaport', 'CoalPowerPlant', 'NuclearPowerPlant', 'Airport', 'Net', 'Water', 'Land', 'Forest', ] # Names correspond to those of resultant zones self.tools = ['Residential', 'Commercial', 'Industrial', 'FireDept', 'PoliceDept', # 'Query', 'Clear', 'Wire', #'Land', 'Rail', 'Road', 'Stadium', 'Park', 'Seaport', 'CoalPowerPlant', 'NuclearPowerPlant', 'Airport', 'Net', 'Water', 'Land', 'Forest', ] #['Residential','Commercial','Industrial','Road','Wire','NuclearPowerPlant', 'Park', 'Clear'] # since query is exluded for now: self.num_tools = len(self.tools) self.map = TileMap(self, self.MAP_X + 2 * PADDING, self.MAP_Y + 2 * PADDING) self.zones = self.map.zones self.num_zones = self.map.num_zones # allows building on rubble and forest self.engine.autoBulldoze = True # for bots self.land_value = 0 win1.playCity() self.engine.setFunds(1000000) engine.setSpeed(3) engine.setPasses(500) #engine.simSpeed =99 self.total_traffic = 0 self.last_total_traffic = 0 # engine.clearMap() self.win1=win1 self.player_builds = [] def layGrid(self, w, h): for i in range(self.MAP_X): for j in range(self.MAP_Y): # gtk.mainiteration() self.engine.simTick() # vertical road if ((i + 4) % w == 0): self.doTool(i, j,'Road') if ((j + 1) % h in [1, h - 1]) and \ j not in [0, self.MAP_Y -1]: self.doTool(i, j, 'Wire') # horizontal roads elif ((j + 1) % h == 0): self.doTool(i, j,'Road') if ((i + 4) % w in [1, w - 1]) and \ i not in [0, self.MAP_X - 1]: self.doTool(i, j, 'Wire') # random zones elif ((i + 2 - (i + 4) // w) % 3) ==0 and \ ((j + 2 - (j + 1) // h) % 3) ==0: tool_i = random.randint(0, 3-1) self.doTool(i, j, ['Residential', 'Commercial', 'Industrial'][tool_i]) def newMap(self): self.engine.generateMap() self.updateMap() def clearMap(self): self.map.setEmpty() self.engine.clearMap() self.updateMap() def clearBotBuilds(self): self.map.clearBotBuilds() def updateMap(self): for i in range(self.MAP_X): for j in range(self.MAP_Y): tile_int = self.getTile(i,j) zone = zoneFromInt(tile_int) # assuming there are no zones not built via us, # or else we must find center self.map.updateTile(i, j, zone, (i,j)) def getDensityMaps(self): #self.last_pollution = self.pollution self.total_traffic = 0 self.land_value = 0 density_maps = np.zeros((3, self.MAP_X, self.MAP_Y)) for i in range (self.MAP_X): for j in range(self.MAP_Y): im = p_im = t_im = i + self.MAP_XS jm = p_jm = t_jm = j + self.MAP_YS t_im -= 2 t_jm -= 4 t_xy_density = self.engine.getTrafficDensity(im, jm) self.total_traffic += t_xy_density density_maps[2][i][j] = self.engine.getTrafficDensity(t_im, t_jm) p_im -= 2 p_jm -= 2 density_maps[1][i][j] = self.engine.getPopulationDensity(p_im, p_jm) density_maps[0][i][j] = self.engine.getPowerGrid(im, jm) self.land_value += self.engine.getLandValue(im, jm) return density_maps def getPowerMap(self): power_map = np.zeros((1, self.MAP_X, self.MAP_Y)) for i in range (self.MAP_X): for j in range(self.MAP_Y): im = i + self.MAP_XS jm = j + self.MAP_YS return power_map def getFunds(self): return self.engine.totalFunds def render(self): while gtk.events_pending(): #for i in range(2): gtk.main_iteration() def setFunds(self, funds): return self.engine.setFunds(funds) # called by map module def doBulldoze(self, x, y): return self.doSimTool(x,y,'Clear') def doLandOver(self, x, y): ''' a glitchy replacement to doBulldoze (layered buildings) ''' def doBotTool(self, x, y, tool, static_build=False): '''Takes string for tool''' return self.map.addZoneBot(x + self.PADDING, y + self.PADDING, tool, static_build=static_build) def doTool(self, x, y, tool): '''Takes string for tool''' return self.map.addZoneBot(x, y, tool) def playerToolDown(self, tool_int, x, y): if not x < self.MAP_X and y < self.MAP_Y: return #x += self.MAP_XS #y += self.MAP_YS #tool = self.tools[tool_int] #self.map.addZonePlayer(x, y, tool, static_build=True) self.player_builds += [(tool_int, x, y)] def toolDown(self, x, y, tool): '''Takes int for tool, depending on engine's index''' self.map.addZoneBot(x, y, self.engineTools[tool]) # called by map module def doSimTool(self, x, y, tool): x += self.MAP_XS y += self.MAP_YS tool = self.engineTools.index(tool) return self.doSimToolInt(x, y, tool) def getTile(self, x, y): x += self.MAP_XS y += self.MAP_YS return self.engine.getTile(x, y) & 1023 def doSimToolInt(self, x, y, tool): return self.engine.toolDown(tool, x, y) def getResPop(self): return self.engine.resPop def getComPop(self): return self.engine.comPop def getIndPop(self): return self.engine.indPop def getTotPop(self): return self.engine.totalPop def takeSetupAction(self, a): tool = self.tools[a[0]] x = a[1] y = a[2] self.doTool(x, y, tool) def takeAction(self, a, static_build=False): '''tool int depends on self.tools indexing''' for i in range(self.MAP_X): for j in range(self.MAP_Y): for t in range(self.num_tools): if a[t][i][j] == 1: tool = self.tools[t] self.doBotTool(i, j, tool, static_build) self.engine.simTick() # gtk.mainiteration() def printTileMap(self): tileMap = np.zeros(shape=(self.MAP_X, self.MAP_Y)) for i in range(self.MAP_X): for j in range(self.MAP_Y): tileMap[i][j] = self.getTile(i, j) print(tileMap) def close(self): # self.engine.doReallyQuit() del(self.engine)
def __init__(self, MAP_W=12, MAP_H=12, PADDING=13, parallel_gui=False, rank=None): self.SHOW_GUI=False engine, win1 = main.train(bot=self, rank=rank) os.chdir(CURR_DIR) self.engine = engine self.engine.setGameLevel(2) self.MAP_X = MAP_W self.MAP_Y = MAP_H self.PADDING = PADDING # shifts build area to centre of 120 by 100 tile map # self.MAP_XS = 59 - self.MAP_X // 2 # self.MAP_YS = 49 - self.MAP_Y //2 self.MAP_XS = 5 self.MAP_YS = 5 self.num_roads = 0 self.engineTools = ['Residential', 'Commercial', 'Industrial', 'FireDept', 'PoliceDept', # TODO: implement query (skipped for now by indexing) 'Query', 'Wire', 'Clear', 'Rail', 'Road', 'Stadium', 'Park', 'Seaport', 'CoalPowerPlant', 'NuclearPowerPlant', 'Airport', 'Net', 'Water', 'Land', 'Forest', ] # Names correspond to those of resultant zones self.tools = ['Residential', 'Commercial', 'Industrial', 'FireDept', 'PoliceDept', # 'Query', 'Clear', 'Wire', #'Land', 'Rail', 'Road', 'Stadium', 'Park', 'Seaport', 'CoalPowerPlant', 'NuclearPowerPlant', 'Airport', 'Net', 'Water', 'Land', 'Forest', ] #['Residential','Commercial','Industrial','Road','Wire','NuclearPowerPlant', 'Park', 'Clear'] # since query is exluded for now: self.num_tools = len(self.tools) self.map = TileMap(self, self.MAP_X + 2 * PADDING, self.MAP_Y + 2 * PADDING) self.zones = self.map.zones self.num_zones = self.map.num_zones # allows building on rubble and forest self.engine.autoBulldoze = True # for bots self.land_value = 0 win1.playCity() self.engine.setFunds(1000000) engine.setSpeed(3) engine.setPasses(500) #engine.simSpeed =99 self.total_traffic = 0 self.last_total_traffic = 0 # engine.clearMap() self.win1=win1 self.player_builds = []
def __init__(self, MAP_W=12, MAP_H=12, PADDING=13, parallel_gui=False): self.parallel_gui = parallel_gui self.pgui = None if parallel_gui: import pexpect self.pgui = pexpect.spawn('/bin/bash') self.pgui.expect('sme') self.pgui.sendline('cd gym-micropolis') self.pgui.expect('sme') self.pgui.sendline('pwd') self.pgui.expect('sme') self.pgui.sendline('python2') self.pgui.expect('>>>') self.pgui.sendline( 'from gym_micropolis.envs.gui import MicropolisGUI') self.pgui.expect('>>>') self.pgui.sendline('m = MicropolisGUI({}, {})'.format( MAP_W, MAP_H)) self.pgui.expect('>>>') self.pgui.sendline('m.render()') self.pgui.expect('>>>') self.SHOW_GUI = False engine, win1 = main.train(bot=self) os.chdir(CURR_DIR) self.engine = engine self.engine.setGameLevel(2) self.MAP_X = MAP_W self.MAP_Y = MAP_H self.PADDING = PADDING # shifts build area to centre of 120 by 100 tile map # self.MAP_XS = 59 - self.MAP_X // 2 # self.MAP_YS = 49 - self.MAP_Y //2 self.MAP_XS = 1 self.MAP_YS = 1 self.num_roads = 0 self.engineTools = [ 'Residential', 'Commercial', 'Industrial', 'FireDept', 'PoliceDept', # TODO: implement query (skipped for now by indexing) 'Query', 'Wire', 'Clear', 'Rail', 'Road', 'Stadium', 'Park', 'Seaport', 'CoalPowerPlant', 'NuclearPowerPlant', 'Airport', 'Net', 'Water', 'Land', 'Forest', ] # Names correspond to those of resultant zones self.tools = [ 'Residential', 'Commercial', 'Industrial', 'FireDept', 'PoliceDept', # 'Query', 'Wire', 'Clear', 'Rail', 'Road', 'Stadium', 'Park', 'Seaport', 'CoalPowerPlant', 'NuclearPowerPlant', 'Airport', 'Net', 'Water', 'Land', 'Forest', ] #['Residential','Commercial','Industrial','Road','Wire','NuclearPowerPlant', 'Park', 'Clear'] # since query is exluded for now: self.num_tools = len(self.tools) self.map = TileMap(self, self.MAP_X + 2 * PADDING, self.MAP_Y + 2 * PADDING) self.zones = self.map.zones self.num_zones = self.map.num_zones # allows building on rubble and forest self.engine.autoBulldoze = True # for bots win1.playCity() self.engine.setFunds(1000000) engine.setSpeed(3) engine.setPasses(500) #engine.simSpeed =99 self.total_traffic = 0 self.last_total_traffic = 0 # engine.clearMap() self.win1 = win1