+class SaveableMap(Map):
+ modified = False
+
+ def __setitem__(self, *args, **kwargs):
+ super().__setitem__(*args, **kwargs)
+ self.modified = True
+
+ def set_line(self, *args, **kwargs):
+ super().set_line(*args, **kwargs)
+ self.modified = True
+
+
+
import os
class Game(GameBase):
def get_map_geometry_shape(self):
return self.map_geometry.__class__.__name__[len('MapGeometry'):]
+ def get_player(self, connection_id):
+ if not connection_id in self.sessions:
+ return None
+ player = self.get_thing(self.sessions[connection_id]['thing_id'])
+ return player
+
def send_gamestate(self, connection_id=None):
"""Send out game state data relevant to clients."""
self.io.send('TURN ' + str(self.turn))
for c_id in self.sessions:
- player = self.get_thing(self.sessions[c_id])
+ player = self.get_player(c_id)
visible_terrain = player.fov_stencil_map()
self.io.send('FOV %s' % quote(player.fov_stencil.terrain), c_id)
self.io.send('MAP %s %s %s' % (self.get_map_geometry_shape(),
target_yx = player.fov_stencil.target_yx(big_yx, little_yx)
portal = self.portals[big_yx][little_yx]
self.io.send('PORTAL %s %s' % (target_yx, quote(portal)), c_id)
+ for big_yx in self.annotations:
+ for little_yx in [little_yx for little_yx in self.annotations[big_yx]
+ if player.fov_test(big_yx, little_yx)]:
+ target_yx = player.fov_stencil.target_yx(big_yx, little_yx)
+ annotation = self.annotations[big_yx][little_yx]
+ self.io.send('ANNOTATION_HINT %s' % (target_yx,), c_id)
self.io.send('GAME_STATE_COMPLETE')
def run_tick(self):
connection_id_found = True
break
if not connection_id_found:
- t = self.get_thing(self.sessions[connection_id])
+ t = self.get_player(connection_id)
if hasattr(t, 'name'):
self.io.send('CHAT ' + quote(t.name + ' left the map.'))
self.things.remove(t)
t.proceed()
except GameError as e:
for connection_id in [c_id for c_id in self.sessions
- if self.sessions[c_id] == t.id_]:
+ if self.sessions[c_id]['thing_id'] == t.id_]:
self.io.send('GAME_ERROR ' + quote(str(e)), connection_id)
except PlayError as e:
for connection_id in [c_id for c_id in self.sessions
- if self.sessions[c_id] == t.id_]:
+ if self.sessions[c_id]['thing_id'] == t.id_]:
self.io.send('PLAY_ERROR ' + quote(str(e)), connection_id)
if self.changed:
self.turn += 1
return p
def cmd_TASK_colon(task_name, game, *args, connection_id):
- if connection_id not in game.sessions:
+ t = self.get_player(connection_id)
+ if not t:
raise GameError('Not registered as player.')
- t = game.get_thing(game.sessions[connection_id])
t.set_next_task(task_name, args)
def task_prefixed(command_name, task_prefix, task_command):
f.write(msg + '\n')
with open(self.io.save_file, 'w') as f:
- # TODO: save tasks
write(f, 'TURN %s' % self.turn)
map_geometry_shape = self.get_map_geometry_shape()
write(f, 'MAP %s %s' % (map_geometry_shape, self.map_geometry.size,))
- for yx in self.maps:
- for y, line in self.maps[yx].lines():
- write(f, 'MAP_LINE %s %5s %s' % (yx, y, quote(line)))
+ for big_yx in [yx for yx in self.maps if self.maps[yx].modified]:
+ for y, line in self.maps[big_yx].lines():
+ write(f, 'MAP_LINE %s %5s %s' % (big_yx, y, quote(line)))
for big_yx in self.annotations:
for little_yx in self.annotations[big_yx]:
write(f, 'GOD_ANNOTATE %s %s %s' %
for little_yx in self.portals[big_yx]:
write(f, 'GOD_PORTAL %s %s %s' % (big_yx, little_yx,
quote(self.portals[big_yx][little_yx])))
- for yx in self.map_controls:
- for y, line in self.map_controls[yx].lines():
- write(f, 'MAP_CONTROL_LINE %s %5s %s' % (yx, y, quote(line)))
+ for big_yx in [yx for yx in self.map_controls
+ if self.map_controls[yx].modified]:
+ for y, line in self.map_controls[big_yx].lines():
+ write(f, 'MAP_CONTROL_LINE %s %5s %s' % (big_yx, y, quote(line)))
for tile_class in self.map_control_passwords:
write(f, 'MAP_CONTROL_PW %s %s' % (tile_class,
self.map_control_passwords[tile_class]))
elif type_ == 'control':
maps = self.map_controls
if not big_yx in maps:
- maps[big_yx] = Map(self.map_geometry)
+ maps[big_yx] = SaveableMap(self.map_geometry)
return maps[big_yx]
def new_world(self, map_geometry):
self.map_geometry = map_geometry
- self.maps[YX(0,0)] = Map(self.map_geometry)
- self.map_controls[YX(0,0)] = Map(self.map_geometry)
+ self.maps[YX(0,0)] = SaveableMap(self.map_geometry)
+ self.map_controls[YX(0,0)] = SaveableMap(self.map_geometry)
self.annotations = {}