1 from plomrogue.misc import quote
5 def cmd_GEN_WORLD(game, yx, seed):
6 game.make_new_world(yx, seed)
7 cmd_GEN_WORLD.argtypes = 'yx_tuple:pos int:nonneg'
9 def cmd_GET_GAMESTATE(game, connection_id):
10 """Send game state to caller."""
11 game.send_gamestate(connection_id)
13 def cmd_SEED(game, seed):
14 game.rand.prngod_seed = seed
15 cmd_SEED.argtypes = 'int:nonneg'
17 def cmd_MAP_SIZE(game, size):
19 cmd_MAP_SIZE.argtypes = 'yx_tuple:pos'
21 def cmd_MAP(game, map_pos, awakeness):
22 """Ensure (possibly empty/'?'-filled) map at position map_pos.
24 Awakeness > 0 puts the map into the player's reality bubble.
27 m = game.get_map(map_pos)
29 cmd_MAP.argtypes = 'yx_tuple int:nonneg'
31 def cmd_MAP_STATS(game, map_pos, type_, population, health):
32 m = game.get_map(map_pos)
33 m.stats[type_] = {'population': population, 'health': health}
34 cmd_MAP_STATS = 'yx_tuple string:thingtype int:nonneg int:nonneg'
36 def cmd_THING_TYPE(game, i, type_):
37 t_old = game.get_thing(i, create_unfound=True)
38 t_new = game.thing_types[type_](game, i)
39 #attr_names_of_old = [name for name in dir(t_old) where name[:2] != '__']
40 #attr_names_of_new = [name for name in dir(t_new) where name[:2] != '__']
41 #class_new = type(t_new)
42 #for attr_name in [v for v in attr_names_of_old if v in attr_names_of_new]:
43 # if hasattr(class_new, attr_name):
44 # attr_new = getattr(class_new, attr_name)
45 # if type(attr_new) == property and attr_new.fset is None:
46 # continue # ignore read-only properties on t_new
47 # attr_old = getattr(t_old, attr_name)
48 # attr_new = getattr(t_new, attr_name)
49 # if type(attr_old) != type(attr_new):
51 # setattr(t_new, attr_name, attr_old)
52 t_new.position = t_old.position
53 t_new.in_inventory = t_old.in_inventory
54 t_old_index = game.things.index(t_old)
55 game.things[t_old_index] = t_new
56 cmd_THING_TYPE.argtypes = 'int:nonneg string:thingtype'
58 def cmd_THING_POS(game, i, big_yx, small_yx):
59 t = game.get_thing(i, create_unfound=True)
60 t.position = (big_yx, small_yx)
61 cmd_THING_POS.argtypes = 'int:nonneg yx_tuple yx_tuple:nonneg'
63 def cmd_THING_INVENTORY(game, id_, ids):
64 carrier = game.get_thing(id_, create_unfound=True)
65 carrier.inventory = ids
67 t = game.get_thing(id_, create_unfound=True)
69 t.position = carrier.position
70 cmd_THING_INVENTORY.argtypes = 'int:nonneg seq:int:nonneg'
72 def cmd_THING_HEALTH(game, id_, health):
73 t = game.get_thing(id_, create_unfound=True)
75 cmd_THING_HEALTH.argtypes = 'int:nonneg int:nonneg'
77 def cmd_GET_PICKABLE_ITEMS(game, connection_id):
78 pickable_ids = game.player.get_pickable_items()
79 if len(pickable_ids) > 0:
80 game.io.send('PICKABLE_ITEMS %s' %
81 ','.join([str(id_) for id_ in pickable_ids]))
83 game.io.send('PICKABLE_ITEMS ,')
85 def cmd_TERRAIN_LINE(game, big_yx, y, terrain_line):
86 game.maps[big_yx].set_line(y, terrain_line)
87 cmd_TERRAIN_LINE.argtypes = 'yx_tuple int:nonneg string'
89 def cmd_PLAYER_ID(game, id_):
90 # TODO: test whether valid thing ID
92 cmd_PLAYER_ID.argtypes = 'int:nonneg'
94 def cmd_TURN(game, n):
96 cmd_TURN.argtypes = 'int:nonneg'
98 def cmd_SWITCH_PLAYER(game):
99 game.player.set_task('WAIT')
100 thing_ids = [t.id_ for t in game.things]
101 player_index = thing_ids.index(game.player.id_)
102 if player_index == len(thing_ids) - 1:
103 game.player_id = thing_ids[0]
105 game.player_id = thing_ids[player_index + 1]
113 save_file_name = game.io.game_file_name + '.save'
114 with open(save_file_name, 'w') as f:
115 write(f, 'TURN %s' % game.turn)
116 write(f, 'SEED %s' % game.rand.prngod_seed)
117 write(f, 'MAP_SIZE %s' % (game.map_size,))
118 for map_pos in game.maps:
119 m = game.maps[map_pos]
120 write(f, 'MAP %s %s' % (map_pos, m.awake))
121 for t_type in m.stats:
122 write(f, 'MAP_STATS %s %s %s %s' %
123 (map_pos, t_type, m.stats[t_type]['population'],
124 m.stats[t_type]['health']))
125 for map_pos in game.maps:
126 for y, line in game.maps[map_pos].lines():
127 write(f, 'TERRAIN_LINE %s %5s %s' % (map_pos, y, quote(line)))
128 for thing in game.things:
129 write(f, 'THING_TYPE %s %s' % (thing.id_, thing.type_))
130 write(f, 'THING_POS %s %s %s' % (thing.id_, thing.position[0],
132 if hasattr(thing, 'health'):
133 write(f, 'THING_HEALTH %s %s' % (thing.id_, thing.health))
134 if len(thing.inventory) > 0:
135 write(f, 'THING_INVENTORY %s %s' %
136 (thing.id_,','.join([str(i) for i in thing.inventory])))
138 write(f, 'THING_INVENTORY %s ,' % thing.id_)
139 if hasattr(thing, 'task'):
142 task_args = task.get_args_string()
143 task_name = [k for k in game.tasks.keys()
144 if game.tasks[k] == task.__class__][0]
145 write(f, 'SET_TASK:%s %s %s %s' % (task_name, thing.id_,
146 task.todo, task_args))
147 write(f, 'PLAYER_ID %s' % game.player_id)
148 cmd_SAVE.dont_save = True