def __init__(self, x, y, z, chunk_size): self["tiles"] = list() self["weather"] = "WEATHER_NONE" # weather is per chunk. # the tile represented on the over map self["overmap_tile"] = "open_air" # set this to true to have the changes updated on the disk, default is True so worldgen writes it to disk self["is_dirty"] = True self["was_loaded"] = False # start = time.time() for i in range(chunk_size): # 0-13 for j in range(chunk_size): # 0-13 chunkdict = {} # this position is on the worldmap. no position is ever repeated. each chunk tile gets its own position. chunkdict["position"] = Position(i + int(x * chunk_size), j + int(y * chunk_size), z) if int(z) <= 0: # make the earth chunkdict["terrain"] = Terrain("t_dirt") else: # make the air chunkdict["terrain"] = Terrain("t_open_air") # one creature per tile chunkdict["creature"] = None # can be zero to many items in a tile. chunkdict["items"] = [] # single furniture per tile chunkdict["furniture"] = None # one per tile chunkdict["vehicle"] = None # used in lightmap calculations, use 1 for base so we never have total darkness. chunkdict["lumens"] = 1 self["tiles"].append(chunkdict)
def __init__( self, x, y, z, chunk_size): # x, y, z relate to it's position on the world map. self.tiles = [] self.weather = "WEATHER_NONE" # weather is per chunk. self.overmap_tile = "open_air" # the tile represented on the over map self.is_dirty = ( True ) # set this to true to have the changes updated on the disk, default is True so worldgen writes it to disk self.was_loaded = "no" # start = time.time() for i in range(chunk_size): # 0-13 for j in range(chunk_size): # 0-13 chunkdict = {} chunkdict["position"] = Position( i + int(x * chunk_size), j + int(y * chunk_size), z ) # this position is on the worldmap. no position is ever repeated. each chunk tile gets its own position. if int(z) <= 0: chunkdict["terrain"] = Terrain("t_dirt") # make the earth else: chunkdict["terrain"] = Terrain( "t_open_air") # make the air chunkdict[ "creature"] = None # Creature() # one creature per tile chunkdict["items"] = [] # can be more then one item in a tile. chunkdict["furniture"] = None # single furniture per tile chunkdict["vehicle"] = None # one per tile chunkdict["trap"] = None # one per tile chunkdict[ "bullet"] = None # one per tile (TODO: figure out a better way) chunkdict["lumens"] = ( 1 ) # used in lightmap calculations, use 1 for base so we never have total darkness. self.tiles.append(chunkdict)
def build_json_building_at_position( self, filename, position ): # applys the json file to world coordinates. can be done over multiple chunks. print('building: ' + str(filename) + ' at ' + str(position)) #start = time.time() #TODO: fill the chunk overmap tile with this om_terrain with open(filename) as json_file: data = json.load(json_file) #print(data) # group = data['group'] # overmap_terrain = data['overmap_terrain'] layout = data['layout'] #print(floors) terrain = data['terrain'] # list fill_terrain = data['fill_terrain'] # string i, j = 0, 0 for row in layout: i = 0 for char in row: #print(char) #print(terrain) t_position = Position(position.x + i, position.y + j) if char in terrain: self.put_object_at_position( Terrain(terrain[char], t_position), t_position) else: self.put_object_at_position( Terrain(fill_terrain, t_position), t_position) # use fill_terrain if unrecognized. pass i = i + 1 j = j + 1
def build_json_building_at_position( self, filename, position ): # applys the json file to world coordinates. can be done over multiple chunks. self._log.debug("building: {} at {}".format(filename, position)) start = time.time() # TODO: fill the chunk overmap tile with this om_terrain with open(filename) as json_file: data = json.load(json_file) # print(data) # group = data['group'] # overmap_terrain = data['overmap_terrain'] floors = data["floors"] # print(floors) terrain = data["terrain"] # list furniture = data["furniture"] # list fill_terrain = data["fill_terrain"] # string impassable_tiles = ["t_wall"] # TODO: make this global for k, floor in floors.items(): # print(k) i, j = 0, 0 for row in floor: i = 0 for char in row: # print(char) # print(terrain) impassable = False t_position = Position(position.x + i, position.y + j, k) self.put_object_at_position( Terrain(fill_terrain, impassable), t_position) # use fill_terrain if unrecognized. if char in terrain: # print('char in terrain') if terrain[char] in impassable_tiles: impassable = True # print('placing: ' + str(terrain[char])) self.put_object_at_position( Terrain(terrain[char], impassable), t_position) elif char in furniture: # print('placing: ' + str(furniture[char])) self.put_object_at_position(Furniture(furniture[char]), t_position) else: # print('placed : ' + str(fill_terrain)) pass i = i + 1 j = j + 1 end = time.time() duration = end - start self._log.debug("Building {} took: {} seconds.".format( filename, duration))
def build_json_building_at_position(self, filename, position): """Applies the json file to worlds coordinates. can be done over multiple chunks.""" # print("building: {} at {}".format(filename, position)) start = time.time() # TODO: fill the chunk overmap tile with this om_terrain with open(filename) as json_file: data = json.load(json_file) # group = data["group"] # overmap_terrain = data["overmap_terrain"] floors = data["floors"] terrain = data["terrain"] # list furniture = data["furniture"] # list fill_terrain = data["fill_terrain"] # string impassable_tiles = ["t_wall"] # TODO: make this global for k, floor in floors.items(): i, j = 0, 0 for row in floor: i = 0 for char in row: impassable = False t_position = Position(position["x"] + i, position["y"] + j, k) self.put_object_at_position( Terrain(fill_terrain, impassable), t_position ) # use fill_terrain if unrecognized. if char in terrain: if terrain[char] in impassable_tiles: impassable = True self.put_object_at_position( Terrain(terrain[char], impassable), t_position ) elif char in furniture: self.put_object_at_position( Furniture(furniture[char]), t_position ) else: pass i = i + 1 j = j + 1 end = time.time() duration = end - start print("Building {} took: {} seconds.".format(filename, duration))
def __init__(self, x, y, chunk_size): # x, y relate to it's position on the world map. #print(chunk_size) self.is_dirty = True # set this to true to have the changes updated on the disk, default is True so worldgen writes it to disk self.was_loaded = 'no' self.map = dict() for i in range(chunk_size[0]): self.map[i] = dict() for j in range(chunk_size[1]): self.map[i][j] = Terrain( 't_grass', Position((x * chunk_size[0]) + i, (y * chunk_size[1]) + j)) self.creatures = list() # individual x, y self.items = list() # grabable items individual x, y self.furnitures = list() # doors, altars, etc. individual x, y self.players = list( ) # current active players on this chunk individual x, y
def get_tile_by_position(self, position): _chunk = self.get_chunk_by_position(position) # print(position) for tile in _chunk["tiles"]: if tile["position"] == position: return tile else: # print("ERROR: couldn't find position! Creating new one.") chunkdict = {} # this position is on the worldmap. no position is ever repeated. each chunk tile gets its own position. chunkdict["position"] = position chunkdict["terrain"] = Terrain("t_dirt") # make the earth chunkdict["creature"] = None # one per tile. chunkdict["items"] = [] # can be zero to many items in a tile. chunkdict["furniture"] = None # one furniture per tile # chunkdict["vehicle"] = None # one per tile, but may be part of a bigger whole. chunkdict["lumens"] = 0 # lighting engine _chunk["tiles"].append(chunkdict) return chunkdict
def __init__(self, x, y): # x, y relate to it's position on the world map. self[ "chunk_size"] = 13 # 0-12 tiles in x and y, z-level 1 tile only (0). self["x"] = x self["y"] = y self["tiles"] = list() self["weather"] = "WEATHER_NONE" # weather is per chunk. # the tile represented on the over map self["overmap_tile"] = "open_air" # set this to true to have the changes updated on the disk, default is True so worldgen writes it to disk self["is_dirty"] = False self["was_loaded"] = False self[ "should_stasis"] = False # TODO: when we unload from memory we need a flag not to loop through it during compute turn self["stasis"] = False # when a chunk is put into stasis. self[ "time_to_stasis"] = 100 # reduce by 1 every turn a player is not near it. set should_stasis when gets to zero # when a chunk is created for the first time fill with dirt and nothing else. start = time.time() for i in range(self["chunk_size"]): # 0-12 for j in range(self["chunk_size"]): # 0-12 chunkdict = {} # this position is on the worldmap. no position is ever repeated. each chunk tile gets its own position. chunkdict["position"] = Position( i + int(x * self["chunk_size"]), j + int(y * self["chunk_size"]), 0) chunkdict["terrain"] = Terrain("t_dirt") # make the earth chunkdict["creature"] = None # one per tile. chunkdict["items"] = [] # can be zero to many items in a tile. chunkdict["furniture"] = None # one furniture per tile # chunkdict["vehicle"] = None # one per tile, but may be part of a bigger whole. chunkdict["lumens"] = 0 # lighting engine self["tiles"].append(chunkdict) end = time.time() duration = end - start print('chunk generation took: ' + str(duration) + ' seconds.')
import json import random import itertools from src.rangeDict import RangeDict from src.terrain import Terrain WORLD_WIDTH = 100 WORLD_HEIGHT = 100 world = [] TERRAIN_RANGES = RangeDict({ range(1, 3): Terrain("DEEP WATER", "ACCBE1", range(1, 3)), range(3, 5): Terrain("SHALLOW WATER", "CEE5F2", range(3, 5)), range(5, 7): Terrain("GRASSLANDS", "588157", range(5, 7)), range(7, 9): Terrain("HILLS", "B2987B", range(7, 9)), range(9, 11): Terrain("MOUNTAINS", "4C5459", range(9, 11)) }) def generate_world(func): def wrapper(): for x in range(WORLD_WIDTH): world.append([]) for y in range(WORLD_HEIGHT): world[x].append(func(x, y)) return wrapper @generate_world