home · contact · privacy
Refactor and extend new library.
[plomrogue2-experiments] / new / plomrogue / commands.py
1 from plomrogue.misc import quote, stringify_yx
2
3
4
5 def cmd_GEN_WORLD(game, yx, seed):
6     game.world.make_new(yx, seed)
7 cmd_GEN_WORLD.argtypes = 'yx_tuple:pos string'
8
9 def cmd_GET_GAMESTATE(game, connection_id):
10     """Send game state to caller."""
11     game.send_gamestate(connection_id)
12
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'
17
18 def cmd_THING_TYPE(game, i, type_):
19     t = game.world.get_thing(i)
20     t.type_ = type_
21 cmd_THING_TYPE.argtypes = 'int:nonneg string'
22
23 def cmd_THING_POS(game, i, yx):
24     t = game.world.get_thing(i)
25     t.position = list(yx)
26 cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg'
27
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'
31
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'
36
37 def cmd_TURN(game, n):
38     game.world.turn = n
39 cmd_TURN.argtypes = 'int:nonneg'
40
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]
48     else:
49         game.world.player_id = thing_ids[player_index + 1]
50     game.proceed()
51
52 def cmd_SAVE(game):
53
54     def write(f, msg):
55         f.write(msg + '\n')
56
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)))
67             task = thing.task
68             if task is not None:
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