import socket
import threading
from plomrogue.parser import ArgError, Parser
-from plomrogue.commands import (cmd_MAP, cmd_THING_POS, cmd_PLAYER_ID,
- cmd_THING_HEALTH)
+from plomrogue.commands import cmd_PLAYER_ID, cmd_THING_HEALTH
from plomrogue.game import Game, WorldBase
from plomrogue.mapping import MapHex
from plomrogue.io import PlomSocket
else:
for i in range(len(map_lines)):
map_lines[i] = '0' + map_lines[i]
- self.y_cut(map_lines, center[1][0], size[0])
+ self.y_cut(map_lines, center[0], size[0])
map_width = self.size[1] * 2 + 1
- self.x_cut(map_lines, center[1][1] * 2, size[1], map_width)
+ self.x_cut(map_lines, center[1] * 2, size[1], map_width)
return map_lines
on any update, even before we actually receive map data.
"""
super().__init__(*args, **kwargs)
- self.maps = {(0,0): ClientMap()}
+ self.map_ = ClientMap()
+ self.offset = (0,0)
self.player_inventory = []
self.player_id = 0
self.pickable_items = []
- def new_map(self, map_pos, size):
- self.maps[map_pos] = ClientMap(size)
+ def new_map(self, offset, size):
+ self.map_ = ClientMap(size)
+ self.offset = offset
@property
def player(self):
cmd_TURN.argtypes = 'int:nonneg'
+def cmd_VISIBLE_MAP(game, offset, size):
+ game.world.new_map(offset, size)
+cmd_VISIBLE_MAP.argtypes = 'yx_tuple yx_tuple:pos'
+
+
def cmd_VISIBLE_MAP_LINE(game, y, terrain_line):
- game.world.maps[(0,0)].set_line(y, terrain_line)
+ game.world.map_.set_line(y, terrain_line)
cmd_VISIBLE_MAP_LINE.argtypes = 'int:nonneg string'
cmd_THING_TYPE.argtypes = 'int:nonneg string'
+def cmd_THING_POS(game, i, yx):
+ t = game.world.get_thing(i)
+ t.position = yx
+cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg'
+
+
def cmd_PLAYER_INVENTORY(game, ids):
game.world.player_inventory[:] = ids # TODO: test whether valid IDs
game.tui.to_update['inventory'] = True
'PLAYER_ID': cmd_PLAYER_ID,
'PLAYER_INVENTORY': cmd_PLAYER_INVENTORY,
'GAME_STATE_COMPLETE': cmd_GAME_STATE_COMPLETE,
- 'MAP': cmd_MAP,
+ 'VISIBLE_MAP': cmd_VISIBLE_MAP,
'PICKABLE_ITEMS': cmd_PICKABLE_ITEMS,
'THING_TYPE': cmd_THING_TYPE,
'THING_HEALTH': cmd_THING_HEALTH,
def log(self, msg):
"""Prefix msg plus newline to self.log_text."""
self.log_text = msg + '\n' + self.log_text
+ with open('log', 'w') as f:
+ f.write(self.log_text)
self.tui.to_update['log'] = True
def symbol_for_type(self, type_):
def get_text_lines(self):
lines = []
- pos_i = self.tui.game.world.maps[(0,0)].\
- get_position_index(self.tui.examiner_position[1])
- terrain = self.tui.game.world.maps[(0,0)].terrain[pos_i]
+ pos_i = self.tui.game.world.map_.\
+ get_position_index(self.tui.examiner_position)
+ terrain = self.tui.game.world.map_.terrain[pos_i]
lines = [terrain]
for t in self.tui.game.world.things_at_pos(self.tui.examiner_position):
lines += [t.type_]
def draw(self):
def annotated_terrain():
- terrain_as_list = list(self.tui.game.world.maps[(0,0)].terrain[:])
+ terrain_as_list = list(self.tui.game.world.map_.terrain[:])
for t in self.tui.game.world.things:
if t.id_ in self.tui.game.world.player_inventory:
continue
- pos_i = self.tui.game.world.maps[(0,0)].\
- get_position_index(t.position[1])
+ pos_i = self.tui.game.world.map_.\
+ get_position_index(t.position)
symbol = self.tui.game.symbol_for_type(t.type_)
if terrain_as_list[pos_i][0] in {'f', '@', 'm'}:
old_symbol = terrain_as_list[pos_i][0]
else:
terrain_as_list[pos_i] = symbol
if self.tui.examiner_mode:
- pos_i = self.tui.game.world.maps[(0,0)].\
- get_position_index(self.tui.examiner_position[1])
+ pos_i = self.tui.game.world.map_.\
+ get_position_index(self.tui.examiner_position)
terrain_as_list[pos_i] = (terrain_as_list[pos_i][0], '?')
return terrain_as_list
chars_with_attrs += [c]
return chars_with_attrs
- if self.tui.game.world.maps[(0,0)].terrain == '':
+ if self.tui.game.world.map_.terrain == '':
lines = []
pad_y(lines)
self.safe_write(''.join(lines))
center = self.tui.game.world.player.position
if self.tui.examiner_mode:
center = self.tui.examiner_position
- lines = self.tui.game.world.maps[(0,0)].\
+ lines = self.tui.game.world.map_.\
format_to_view(annotated_terrain, center, self.size)
pad_or_cut_x(lines)
pad_y(lines)
def move_examiner(direction):
start_pos = self.examiner_position
- new_examine_pos = self.game.world.maps[(0,0)].\
- move(start_pos[0], direction)
+ new_examine_pos = self.game.world.map_.move(start_pos, direction)
if new_examine_pos:
- self.examiner_position[1] = new_examine_pos
+ self.examiner_position = new_examine_pos
self.to_update['map'] = True
def switch_to_pick_or_drop(target_widget):
"""Send game state to caller."""
game.send_gamestate(connection_id)
-def cmd_MAP(game, big_yx, small_yx):
- """Create new map of size small_yx at pos big_yx and only '?' cells."""
- game.world.new_map(big_yx, small_yx)
+def cmd_MAP(game, map_pos, size):
+ """Create new map of size at position map_pos, and only '?' cells."""
+ game.world.new_map(map_pos, size)
cmd_MAP.argtypes = 'yx_tuple yx_tuple:pos'
def cmd_THING_TYPE(game, i, type_):
def cmd_THING_POS(game, i, big_yx, small_yx):
t = game.world.get_thing(i)
t.position = (big_yx, small_yx)
-cmd_THING_POS.argtypes = 'int:nonneg yx_tuple yx_tuple'
+cmd_THING_POS.argtypes = 'int:nonneg yx_tuple yx_tuple:nonneg'
def cmd_THING_INVENTORY(game, id_, ids):
t = game.world.get_thing(id_)
def send_gamestate(self, connection_id=None):
"""Send out game state data relevant to clients."""
+ def send_thing(offset, thing):
+ offset_pos = (thing.position[1][0] - offset[0],
+ thing.position[1][1] - offset[1])
+ self.io.send('THING_TYPE %s %s' % (thing.id_, thing.type_))
+ self.io.send('THING_POS %s %s' % (thing.id_,
+ stringify_yx(offset_pos)))
+
self.io.send('TURN ' + str(self.world.turn))
visible_map = self.world.player.get_visible_map()
- self.io.send('MAP ' + stringify_yx([0,0]) + ' ' + stringify_yx(visible_map.size))
+ offset = self.world.player.get_surroundings_offset()
+ self.io.send('VISIBLE_MAP ' + stringify_yx(offset) + ' ' + stringify_yx(visible_map.size))
for y, line in visible_map.lines():
self.io.send('VISIBLE_MAP_LINE %5s %s' % (y, quote(line)))
- visible_things, offset = self.world.player.get_visible_things()
+ visible_things = self.world.player.get_visible_things()
for thing in visible_things:
- offset_pos = (thing.position[1][0] - offset[0],
- thing.position[1][1] - offset[1])
- self.io.send('THING_TYPE %s %s' % (thing.id_, thing.type_))
- self.io.send('THING_POS %s %s %s' % (thing.id_,
- stringify_yx(thing.position[0]),
- stringify_yx(offset_pos)))
+ send_thing(offset, thing)
if hasattr(thing, 'health'):
self.io.send('THING_HEALTH %s %s' % (thing.id_,
thing.health))
self.io.send('PLAYER_INVENTORY ,')
for id_ in self.world.player.inventory:
thing = self.world.get_thing(id_)
- self.io.send('THING_TYPE %s %s' % (thing.id_, thing.type_))
- self.io.send('THING_POS %s %s %s' % (thing.id_,
- stringify_yx(thing.position[0]),
- stringify_yx(thing.position[1])))
+ send_thing(offset, thing)
self.io.send('GAME_STATE_COMPLETE')
def proceed(self):