def _create_island_db(self, savepath): """Returns a dbreader instance for the creation of island dbfiles.""" horizons.main.PATHS.ISLAND_TEMPLATE = os.path.join(util.getUHPath(), horizons.main.PATHS.ISLAND_TEMPLATE) db = UhDbAccessor(savepath) read_island_template(db) return db
def loadResource(self, path): """ Loads the map from the given sqlite file """ # creates absolute path to mapfile path = os.path.join(util.getUHPath(), path) model = self._engine.getModel() map = model.createMap(path) map.setFilename(path) grid = model.getCellGrid(self.GRID_TYPE) # add layers ground_layer = map.createLayer(util.GROUND_LAYER_NAME, grid) building_layer = map.createLayer(util.BUILDING_LAYER_NAME, grid) # add camera self._createCamera(building_layer, map) map_db = DbReader(path) # TODO: check the map version number # load all islands islands = map_db("SELECT x, y, file FROM island") for island in islands: name = island[2] if name[0:6] == "random": raise RuntimeError("Map contains random generated islands. Cannot load that") for island in islands: self._loadIsland(ground_layer, model, *island) # load all buildings self._loadBuildings(building_layer, model, map_db) return map
def _create_map_db(self, savepath): """Returns a dbreader instance, that is connected to the main game data dbfiles.""" horizons.main.PATHS.SAVEGAME_TEMPLATE = os.path.join(util.getUHPath(), horizons.main.PATHS.SAVEGAME_TEMPLATE) db = UhDbAccessor(savepath) read_savegame_template(db) return db
def _fixupHorizons(self): """Fixes some UH quirks that have to do with globals""" class PatchedFife: imagemanager = self._engine.getImageManager() use_atlases = False pass horizons.main.fife = PatchedFife() uh_path = util.getUHPath() horizons.main.PATHS.TILE_SETS_DIRECTORY = os.path.join(uh_path, horizons.main.PATHS.TILE_SETS_DIRECTORY) horizons.main.PATHS.ACTION_SETS_DIRECTORY = os.path.join(uh_path, horizons.main.PATHS.ACTION_SETS_DIRECTORY) horizons.main.PATHS.DB_FILES = map(lambda file: os.path.join(uh_path, file), horizons.main.PATHS.DB_FILES)
def _loadBuildings(self): print("loading UH buildings...") for root, dirnames, filenames in os.walk(util.getUHPath() + '/content/objects/buildings'): for filename in fnmatch.filter(filenames, '*.yaml'): # This is needed for dict lookups! Do not convert to os.join! full_file = root + "/" + filename result = YamlCache.get_file(full_file) result['yaml_file'] = full_file self._loadBuilding(result) print("finished loading UH objects")
def _loadIsland(self, ground_layer, model, ix, iy, file): """ Loads an island from the given file """ island_db = DbReader(os.path.join(util.getUHPath(), file)) # load ground tiles ground = island_db("SELECT x, y, ground_id, action_id, rotation FROM ground") for (x, y, ground_id, action_id, rotation) in ground: groundTileName = util.getGroundTileName(ground_id) ground_tile = model.getObject(groundTileName, util.GROUND_NAMESPACE) x = ix + x y = iy + y position = fife.ModelCoordinate(x, y, 0) inst = ground_layer.createInstance(ground_tile, position) self.act(action_id+"_"+str(groundTileName), rotation, inst, ground_layer, x, y) fife.InstanceVisual.create(inst)
def _saveIsland(self, name, relative_island_filepath, tiles): """Writes an SQL file for an island""" island_filepath = os.path.join(util.getUHPath(), relative_island_filepath) print "Saving island " + island_filepath if os.path.exists(island_filepath): os.remove(island_filepath) island_db = self._create_island_db(island_filepath) island_db("BEGIN IMMEDIATE TRANSACTION") for instance in tiles: type = util.getGroundTileId(instance.getObject().getId()) # TODO (MMB) "default action id = 0" action_id = instance.getObject().getActionIds()[0] if instance.getCurrentAction() != None: action_id = instance.getCurrentAction().getId() # TODO (MMB) a bit of a hack to only get the name without a possible _ts_curved etc. suffix action_id = re.sub("_ts.*", "", action_id) (position, rotation) = self._extractPositionRotationFromInstance(instance) island_db("INSERT INTO ground VALUES (?, ?, ?, ?, ?)", position.x, position.y, type, action_id, rotation) island_db("COMMIT TRANSACTION"); pass
def _fixupFife(self): """Fixes some FIFE quirks that have to do with VFS""" vfs = self._engine.getVFS() vfs.addNewSource(util.getUHPath()) vfs.addNewSource("/")
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # ################################################### import horizons.main # necessary so the correct load order of all modules is guaranteed from horizons.util.uhdbaccessor import UhDbAccessor, read_savegame_template, \ read_island_template import fife.extensions.savers as mapSavers import os.path import re import scripts.editor import scripts.plugin import shutil import util TEMPLATE_DATAFORMAT_PATH = os.path.join(util.getUHPath(), 'content', 'savegame_template.sql') class MapSaver: def __init__(self, filepath, engine, map, importList): # copy template to save map self._filepath = filepath self._engine = engine self._model = engine.getModel() self._map = map self._mapDatabase = None def _fixRotation(self, rotation): """ Fixes FIFEs botched handling of rotations. Rotations are a) 0, 90, 180 or 270 and b) sometimes off by one.