1 from plomrogue.misc import quote, stringify_yx
5 def cmd_GEN_WORLD(game, yx, seed):
6 game.world.make_new(yx, seed)
7 cmd_GEN_WORLD.argtypes = 'yx_tuple:pos string'
9 def cmd_GET_GAMESTATE(game, connection_id):
10 """Send game state to caller."""
11 game.send_gamestate(connection_id)
13 def cmd_MAP(game, yx):
14 """Create new map of size yx and only '?' cells."""
15 game.world.new_map(yx)
16 cmd_MAP.argtypes = 'yx_tuple:pos'
18 def cmd_THING_TYPE(game, i, type_):
19 t = game.world.get_thing(i)
21 cmd_THING_TYPE.argtypes = 'int:nonneg string'
23 def cmd_THING_POS(game, i, yx):
24 t = game.world.get_thing(i)
26 cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg'
28 def cmd_TERRAIN_LINE(game, y, terrain_line):
29 game.world.map_.set_line(y, terrain_line)
30 cmd_TERRAIN_LINE.argtypes = 'int:nonneg string'
32 def cmd_PLAYER_ID(game, id_):
33 # TODO: test whether valid thing ID
34 game.world.player_id = id_
35 cmd_PLAYER_ID.argtypes = 'int:nonneg'
37 def cmd_TURN(game, n):
39 cmd_TURN.argtypes = 'int:nonneg'
41 def cmd_SWITCH_PLAYER(game):
42 player = game.world.get_player()
43 player.set_task('WAIT')
44 thing_ids = [t.id_ for t in game.world.things]
45 player_index = thing_ids.index(player.id_)
46 if player_index == len(thing_ids) - 1:
47 game.world.player_id = thing_ids[0]
49 game.world.player_id = thing_ids[player_index + 1]
57 save_file_name = game.io.game_file_name + '.save'
58 with open(save_file_name, 'w') as f:
59 write(f, 'TURN %s' % game.world.turn)
60 write(f, 'MAP ' + stringify_yx(game.world.map_.size))
61 for y, line in game.world.map_.lines():
62 write(f, 'TERRAIN_LINE %5s %s' % (y, quote(line)))
63 for thing in game.world.things:
64 write(f, 'THING_TYPE %s %s' % (thing.id_, thing.type_))
65 write(f, 'THING_POS %s %s' % (thing.id_,
66 stringify_yx(thing.position)))
69 task_args = task.get_args_string()
70 write(f, 'SET_TASK:%s %s %s %s' % (task.name, thing.id_,
71 task.todo, task_args))
72 write(f, 'PLAYER_ID %s' % game.world.player_id)
73 cmd_SAVE.dont_save = True