Commits

rkruppe committed bd42378

Loading gameset only once; very hacky as the screens are not updated to reflect that

  • Participants
  • Parent commits 1b8184a
  • Branches nsgui

Comments (0)

Files changed (2)

 
 parser = argparse.ArgumentParser(description="Tutagx Demo")
 parser.add_argument('-g', help='Name of gameset to load', metavar='GAMESET',
-                    default='default')
+                    dest='gameset', default='default')
 parser.add_argument('-d', '--debug', help="Add DEBUG output",
                     action='store_true', dest='debug', default=False)
 parser.add_argument('--home', help="Override game home directory",
     core_assets_path = ttgx_path.core_assets_dir()
     log.debug('Core assets expected in %r', core_assets_path)
     assets = ttgx.assets.AssetCollection(core_assets_path)
-    gsc = gamestate.GameStateController(assets)
+    gsc = gamestate.GameStateController(args.gameset, assets)
     return gsc
 
 

ttgx/model/gamestate.py

 class GameStateController:
     EVENTS = ('StateReset', 'NewGame')
 
-    def __init__(self, asset_collection=None, state=None):
+    def __init__(self, gameset_name, assets, state=None):
         self._state = state
         self._ctx = GameContext()
-        self.assets = asset_collection
+        self.assets = assets
+        self.gameset, self.gameset_assets = _load_gameset(gameset_name)
+        self.gameset_name = gameset_name
 
     def savegame_path(self, name):
         """
 
     def load(self, name):
         path = self.savegame_path(name)
+        #TODO rework error handling, it's incomplete and a mess
         try:
-            #TODO rework error handling, it's incomplete and a mess
             [new_state] = ttgx.toolutil.load(GameState, path)
         except IOError as e:
             raise LoadingError(e)
         if new_state is None:
             raise ValidationFailure("Validation failed", errors)
-        try:
-            _, self.gameset_assets = self.load_gameset(new_state.gameset)
-        except (ValidationFailure, IOError) as e:
-            raise LoadingError(e)
+        if new_state.gameset != self.gameset_name:
+            raise ValidationFailure("Wrong gameset")
         self._state = new_state
         self.StateReset.trigger()
 
         path = self.savegame_path(name)
         GameState.save_to_path([self._state], path)
 
-    def new_game(self, name):
-        gs, self.gameset_assets = self.load_gameset(name)
-        difficulty = gs.difficulties[0]
+    def new_game(self, *_):  #XXX remove params
+        difficulty = self.gameset.difficulties[0]
         config = difficulty.start_config
-        new_state = gs.gcx.create(
+        new_state = self.gameset.gcx.create(
             GameState,
             date=config.date,
             agency=config.agency,
             ai_actors=config.ais,
-            gameset=name,
+            gameset=self.gameset_name,
         )
         self._state = new_state
-        self._gameset_script_startup(gs)
+        self._gameset_script_startup()
         self.NewGame.trigger()
         self.StateReset.trigger()
 
-    def _gameset_script_startup(self, gs):
+    def _gameset_script_startup(self):
         ec = ttgx.util.script.ExecutionContext(GSC=self, GCX=self.ctx)
         self.gameset_script_context = ec
-        for ident in gs.startup_scripts:
+        for ident in self.gameset.startup_scripts:
             res = self.gameset_assets.script(ident)
             script = ttgx.util.script.ScriptExecution(res, ec)
             _, err = script.run()
             if err:
                 log.warning('Error in startup script %s: %r', ident, err)
 
-    def load_gameset(self, name):
-        """
-        Load the gameset of the given name and return both the gameset object
-        and an AssetCollection for that gameset's assets.
-        Raise IOError if the gameset is invalid.
-        """
-        gsdir = ttgx_path.gameset_path(name)
-        #TODO better error handling
-        [gs], _, vctx = ttgx.toolutil.load_with_context(GameSet, gsdir)
-        if vctx.errors:
-            log.error('Gameset %r (in %r) is invalid:', name, gsdir)
-            for err_line in validate.format_errors(vctx.errors):
-                log.error('%s', err_line)
-            raise ValidationFailure
-        assets_dir = os.path.join(gsdir, gs.assets)
-        gameset_assets = ttgx.assets.AssetCollection(assets_dir)
-        return gs, gameset_assets
-
     @property
     def date(self):
         return self._state.date
     def ctx(self):
         return self._state.gcx
 
+
+def _load_gameset(name):
+    print("!", 'Loading gameset ' + repr(name))
+    gsdir = ttgx_path.gameset_path(name)
+    #TODO better error handling
+    [gs], _, vctx = ttgx.toolutil.load_with_context(GameSet, gsdir)
+    if vctx.errors:
+        log.error('Gameset %r (in %r) is invalid:', name, gsdir)
+        for err_line in validate.format_errors(vctx.errors):
+            log.error('%s', err_line)
+        raise ValidationFailure
+    assets_dir = os.path.join(gsdir, gs.assets)
+    gameset_assets = ttgx.assets.AssetCollection(assets_dir)
+    return gs, gameset_assets
+