home · contact · privacy
ef18bbbedb9db5b5499e15bfa1c5a72e3234c045
[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 int:nonneg'
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_SEED(game, seed):
14     game.world.rand.prngod_seed = seed
15 cmd_SEED.argtypes = 'int:nonneg'
16
17 def cmd_MAP(game, map_pos, size):
18     """Create new map of size at position map_pos, and only '?' cells."""
19     game.world.new_map(map_pos, size)
20 cmd_MAP.argtypes = 'yx_tuple yx_tuple:pos'
21
22 def cmd_THING_TYPE(game, i, type_):
23     t_old = game.world.get_thing(i)
24     t_new = game.thing_types[type_](game.world, i)
25     #attr_names_of_old = [name for name in dir(t_old) where name[:2] != '__']
26     #attr_names_of_new = [name for name in dir(t_new) where name[:2] != '__']
27     #class_new = type(t_new)
28     #for attr_name in [v for v in attr_names_of_old if v in attr_names_of_new]:
29     #    if hasattr(class_new, attr_name):
30     #        attr_new = getattr(class_new, attr_name)
31     #        if type(attr_new) == property and attr_new.fset is None:
32     #            continue  # ignore read-only properties on t_new
33     #    attr_old = getattr(t_old, attr_name)
34     #    attr_new = getattr(t_new, attr_name)
35     #    if type(attr_old) != type(attr_new):
36     #        continue
37     #    setattr(t_new, attr_name, attr_old)
38     t_new.position = t_old.position
39     t_old_index = game.world.things.index(t_old)
40     game.world.things[t_old_index] = t_new
41 cmd_THING_TYPE.argtypes = 'int:nonneg string:thingtype'
42
43 def cmd_THING_POS(game, i, big_yx, small_yx):
44     t = game.world.get_thing(i)
45     t.position = (big_yx, small_yx)
46 cmd_THING_POS.argtypes = 'int:nonneg yx_tuple yx_tuple:nonneg'
47
48 def cmd_THING_INVENTORY(game, id_, ids):
49     t = game.world.get_thing(id_)
50     t.inventory = ids  # TODO: test whether valid IDs
51 cmd_THING_INVENTORY.argtypes = 'int:nonneg seq:int:nonneg'
52
53 def cmd_THING_HEALTH(game, id_, health):
54     t = game.world.get_thing(id_)
55     t.health = health
56 cmd_THING_HEALTH.argtypes = 'int:nonneg int:nonneg'
57
58 def cmd_GET_PICKABLE_ITEMS(game, connection_id):
59     pickable_ids = game.world.player.get_pickable_items()
60     if len(pickable_ids) > 0:
61         game.io.send('PICKABLE_ITEMS %s' %
62                      ','.join([str(id_) for id_ in pickable_ids]))
63     else:
64         game.io.send('PICKABLE_ITEMS ,')
65
66 def cmd_TERRAIN_LINE(game, big_yx, y, terrain_line):
67     game.world.maps[big_yx].set_line(y, terrain_line)
68 cmd_TERRAIN_LINE.argtypes = 'yx_tuple int:nonneg string'
69
70 def cmd_PLAYER_ID(game, id_):
71     # TODO: test whether valid thing ID
72     game.world.player_id = id_
73 cmd_PLAYER_ID.argtypes = 'int:nonneg'
74
75 def cmd_TURN(game, n):
76     game.world.turn = n
77 cmd_TURN.argtypes = 'int:nonneg'
78
79 def cmd_SWITCH_PLAYER(game):
80     game.world.player.set_task('WAIT')
81     thing_ids = [t.id_ for t in game.world.things]
82     player_index = thing_ids.index(game.world.player.id_)
83     if player_index == len(thing_ids) - 1:
84         game.world.player_id = thing_ids[0]
85     else:
86         game.world.player_id = thing_ids[player_index + 1]
87     game.proceed()
88
89 def cmd_SAVE(game):
90
91     def write(f, msg):
92         f.write(msg + '\n')
93
94     save_file_name = game.io.game_file_name + '.save'
95     with open(save_file_name, 'w') as f:
96         write(f, 'TURN %s' % game.world.turn)
97         write(f, 'SEED %s' % game.world.rand.prngod_seed)
98         for map_pos in game.world.maps:
99             write(f, 'MAP ' + stringify_yx(map_pos) + ' ' +
100                   stringify_yx(game.world.maps[(0,0)].size))
101         for map_pos in game.world.maps:
102             for y, line in game.world.maps[map_pos].lines():
103                  write(f, 'TERRAIN_LINE %s %5s %s' % (stringify_yx(map_pos),
104                                                       y, quote(line)))
105         for thing in game.world.things:
106             write(f, 'THING_TYPE %s %s' % (thing.id_, thing.type_))
107             write(f, 'THING_POS %s %s %s' % (thing.id_,
108                                              stringify_yx(thing.position[0]),
109                                              stringify_yx(thing.position[1])))
110             if hasattr(thing, 'health'):
111                 write(f, 'THING_HEALTH %s %s' % (thing.id_, thing.health))
112             if len(thing.inventory) > 0:
113                 write(f, 'THING_INVENTORY %s %s' %
114                       (thing.id_,','.join([str(i) for i in thing.inventory])))
115             else:
116                 write(f, 'THING_INVENTORY %s ,' % thing.id_)
117             if hasattr(thing, 'task'):
118                 task = thing.task
119                 if task is not None:
120                     task_args = task.get_args_string()
121                     task_name = [k for k in game.tasks.keys()
122                                  if game.tasks[k] == task.__class__][0]
123                     write(f, 'SET_TASK:%s %s %s %s' % (task_name, thing.id_,
124                                                        task.todo, task_args))
125         write(f, 'PLAYER_ID %s' % game.world.player_id)
126 cmd_SAVE.dont_save = True