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