+ cmd_GEN_WORLD.argtypes = 'string:geometry yx_tuple:pos string'
+
+ def get_command_signature(self, command_name):
+ from functools import partial
+
+ def cmd_TASK_colon(task_name, *args):
+ self.world.get_player().set_task(task_name, args)
+ self.proceed()
+
+ def cmd_SET_TASK_colon(task_name, thing_id, todo, *args):
+ t = self.world.get_thing(thing_id, False)
+ if t is None:
+ raiseArgError('No such Thing.')
+ task_class = self.task_manager.get_task_class(task_name)
+ t.task = task_class(t, args)
+ t.task.todo = todo
+
+ def task_prefixed(command_name, task_prefix, task_command,
+ argtypes_prefix=''):
+ func = None
+ argtypes = ''
+ if command_name[:len(task_prefix)] == task_prefix:
+ task_name = command_name[len(task_prefix):]
+ task_manager_reply = self.task_manager.get_task_class(task_name)
+ if task_manager_reply is not None:
+ func = partial(task_command, task_name)
+ task_class = task_manager_reply
+ argtypes = task_class.argtypes
+ if func is not None:
+ return func, argtypes_prefix + argtypes
+ return None, argtypes
+
+ func, argtypes = task_prefixed(command_name, 'TASK:', cmd_TASK_colon)
+ if func:
+ return func, argtypes
+ func, argtypes = task_prefixed(command_name, 'SET_TASK:',
+ cmd_SET_TASK_colon,
+ 'int:nonneg int:nonneg ')
+ if func:
+ return func, argtypes
+ func_candidate = 'cmd_' + command_name
+ if hasattr(self, func_candidate):
+ func = getattr(self, func_candidate)
+ if hasattr(func, 'argtypes'):
+ argtypes = func.argtypes
+ return func, argtypes
+
+ def get_string_options(self, string_option_type):
+ if string_option_type == 'geometry':
+ return self.map_manager.get_map_geometries()
+ elif string_option_type == 'direction':
+ return self.world.map_.get_directions()
+ return None
+
+ def cmd_PLAYER_ID(self, id_):
+ # TODO: test whether valid thing ID
+ self.world.player_id = id_
+ cmd_PLAYER_ID.argtypes = 'int:nonneg'
+
+ def cmd_TURN(self, n):
+ self.world.turn = n
+ cmd_TURN.argtypes = 'int:nonneg'
+
+ def cmd_SAVE(self):
+
+ def write(f, msg):
+ f.write(msg + '\n')
+
+ save_file_name = self.io.game_file_name + '.save'
+ with open(save_file_name, 'w') as f:
+ write(f, 'TURN %s' % self.world.turn)
+ write(f, 'MAP ' + self.world.map_.geometry + ' ' + server_.io.stringify_yx(self.world.map_.size))
+ for y, line in self.world.map_.lines():
+ write(f, 'TERRAIN_LINE %5s %s' % (y, server_.io.quote(line)))
+ for thing in self.world.things:
+ write(f, 'THING_TYPE %s %s' % (thing.id_, thing.type_))
+ write(f, 'THING_POS %s %s' % (thing.id_,
+ server_.io.stringify_yx(thing.position)))
+ task = thing.task
+ if task is not None:
+ task_args = task.get_args_string()
+ write(f, 'SET_TASK:%s %s %s %s' % (task.name, thing.id_,
+ task.todo, task_args))
+ write(f, 'PLAYER_ID %s' % self.world.player_id)
+ cmd_SAVE.dont_save = True