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