game.send_gamestate(connection_id)
cmd_GET_GAMESTATE.argtypes = ''
-#def cmd_QUERY(game, target_nick, msg, connection_id):
-# if not connection_id in game.sessions:
-# raise GameError('can only query when logged in')
-# t = game.get_thing(game.sessions[connection_id], False)
-# source_nick = t.name
-# for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
-# for c_id in game.sessions:
-# if game.sessions[c_id] == t.id_:
-# game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
-# game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
-# return
-# raise GameError('target user offline')
-# raise GameError('can only query with registered nicknames')
-#cmd_QUERY.argtypes = 'string string'
+# def cmd_QUERY(game, target_nick, msg, connection_id):
+# if not connection_id in game.sessions:
+# raise GameError('can only query when logged in')
+# t = game.get_thing(game.sessions[connection_id], False)
+# source_nick = t.name
+# for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
+# for c_id in game.sessions:
+# if game.sessions[c_id] == t.id_:
+# game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
+# game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
+# return
+# raise GameError('target user offline')
+# raise GameError('can only query with registered nicknames')
+# cmd_QUERY.argtypes = 'string string'
def cmd_PING(game, connection_id):
game.io.send('PONG', connection_id)
if little_yx in game.annotations[big_yx]:
del game.annotations[big_yx][little_yx]
else:
- if not big_yx in game.annotations:
+ if big_yx not in game.annotations:
game.annotations[big_yx] = {}
game.annotations[big_yx][little_yx] = msg
game.changed = True
if little_yx in game.portals[big_yx]:
del game.portals[big_yx][little_yx]
else:
- if not big_yx in game.portals:
+ if big_yx not in game.portals:
game.portals[big_yx] = {}
game.portals[big_yx][little_yx] = msg
game.changed = True
cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
- if not big_yx in game.annotations:
+ if big_yx not in game.annotations:
game.annotations[big_yx] = {}
game.annotations[big_yx][little_yx] = msg
game.changed = True
cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
- if not big_yx in game.portals:
+ if big_yx not in game.portals:
game.portals[big_yx] = {}
game.portals[big_yx][little_yx] = msg
game.changed = True
def cmd_GET_ANNOTATION(game, yx, connection_id):
player = game.get_player(connection_id)
big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
- annotation = '(unknown)';
+ annotation = '(unknown)'
if player.fov_test(big_yx, little_yx):
- annotation = '(none)';
+ annotation = '(none)'
if big_yx in game.annotations:
if little_yx in game.annotations[big_yx]:
annotation = game.annotations[big_yx][little_yx]
cmd_MAP_CONTROL_PW.argtypes = 'char string'
def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
- if not thing_type in game.thing_types:
+ if thing_type not in game.thing_types:
raise GameError('illegal thing type %s' % thing_type)
_ = game.get_map(big_yx)
t_old = None
self.set_line(3, 'X' * self.geometry.size.x)
self.set_line(4, 'X' * self.geometry.size.x)
for y in range(self.geometry.size.y):
- self[YX(y,0)] = 'X'
- self[YX(y,1)] = 'X'
- self[YX(y,2)] = 'X'
- self[YX(y,3)] = 'X'
- self[YX(y,4)] = 'X'
+ self[YX(y, 0)] = 'X'
+ self[YX(y, 1)] = 'X'
+ self[YX(y, 2)] = 'X'
+ self[YX(y, 3)] = 'X'
+ self[YX(y, 4)] = 'X'
elif type(self.geometry) == MapGeometryHex:
# TODO: for this to work we need a map side length divisible by 6.
- def draw_grid(offset=YX(0,0)):
+ def draw_grid(offset=YX(0, 0)):
dirs = ('DOWNRIGHT', 'RIGHT', 'UPRIGHT', 'RIGHT')
def draw_snake(start):
if alternate_hex:
draw_snake(offset + YX(0, 0))
- draw_snake(offset + YX((0 + alternate_hex) * distance, -int(1.5*distance)))
- draw_snake(offset + YX((1 + alternate_hex) * distance, 0))
- draw_snake(offset + YX((2 + alternate_hex) * distance, -int(1.5*distance)))
+ draw_snake(offset + YX((0 + alternate_hex) * distance,
+ -int(1.5 * distance)))
+ draw_snake(offset + YX((1 + alternate_hex) * distance,
+ 0))
+ draw_snake(offset + YX((2 + alternate_hex) * distance,
+ -int(1.5 * distance)))
distance = self.geometry.size.y // 3
draw_grid()
- draw_grid(YX(2,0))
- draw_grid(YX(0,2))
- draw_grid(YX(1,0))
- draw_grid(YX(0,1))
- draw_grid(YX(-1,0))
- draw_grid(YX(0,-1))
- draw_grid(YX(-2,0))
- draw_grid(YX(0,-2))
+ draw_grid(YX(2, 0))
+ draw_grid(YX(0, 2))
+ draw_grid(YX(1, 0))
+ draw_grid(YX(0, 1))
+ draw_grid(YX(-1, 0))
+ draw_grid(YX(0, -1))
+ draw_grid(YX(-2, 0))
+ draw_grid(YX(0, -2))
self.modified = old_modified
self.map_controls = {}
self.map_control_passwords = {}
self.annotations = {}
- self.spawn_point = YX(0,0), YX(0,0)
+ self.spawn_point = YX(0, 0), YX(0, 0)
self.portals = {}
self.player_chars = string.digits + string.ascii_letters
self.player_char_i = -1
return self.map_geometry.__class__.__name__[len('MapGeometry'):]
def get_player(self, connection_id):
- if not connection_id in self.sessions:
+ if connection_id not in self.sessions:
return None
player = self.get_thing(self.sessions[connection_id]['thing_id'])
return player
def save(self):
- def write(f, msg):
- f.write(msg + '\n')
-
- with open(self.io.save_file, 'w') as f:
- 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 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' %
- (big_yx, little_yx, quote(self.annotations[big_yx][little_yx])))
- for big_yx in self.portals:
- 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 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]))
- for pw in self.admin_passwords:
- write(f, 'ADMIN_PASSWORD %s' % pw)
- for t in [t for t in self.things if not t.type_ == 'Player']:
- write(f, 'THING %s %s %s %s' % (t.position[0],
- t.position[1], t.type_, t.id_))
- if hasattr(t, 'name'):
- write(f, 'THING_NAME %s %s' % (t.id_, quote(t.name)))
- write(f, 'SPAWN_POINT %s %s' % (self.spawn_point[0],
- self.spawn_point[1]))
+ def write(f, msg):
+ f.write(msg + '\n')
+
+ with open(self.io.save_file, 'w') as f:
+ 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 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' %
+ (big_yx, little_yx, quote(self.annotations[big_yx][little_yx])))
+ for big_yx in self.portals:
+ 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 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]))
+ for pw in self.admin_passwords:
+ write(f, 'ADMIN_PASSWORD %s' % pw)
+ for t in [t for t in self.things if not t.type_ == 'Player']:
+ write(f, 'THING %s %s %s %s' % (t.position[0],
+ t.position[1], t.type_, t.id_))
+ if hasattr(t, 'name'):
+ write(f, 'THING_NAME %s %s' % (t.id_, quote(t.name)))
+ write(f, 'SPAWN_POINT %s %s' % (self.spawn_point[0],
+ self.spawn_point[1]))
def get_map(self, big_yx, type_='normal'):
if type_ == 'normal':
maps = self.maps
elif type_ == 'control':
maps = self.map_controls
- if not big_yx in maps:
+ if big_yx not in maps:
maps[big_yx] = SaveableMap(self.map_geometry)
if type_ == 'control':
maps[big_yx].draw_presets(big_yx.y % 2)
self.annotations = {}
self.portals = {}
self.admin_passwords = []
- self.spawn_point = YX(0,0), YX(0,0)
+ self.spawn_point = YX(0, 0), YX(0, 0)
self.map_geometry = map_geometry
self.map_control_passwords = {'X': 'secret'}
- self.get_map(YX(0,0))
- self.get_map(YX(0,0), 'control')
+ self.get_map(YX(0, 0))
+ self.get_map(YX(0, 0), 'control')
self.annotations = {}
command(*args, connection_id=connection_id)
elif god_mode:
command(*args)
- #if store and not hasattr(command, 'dont_save'):
- # with open(self.game_file_name, 'a') as f:
- # f.write(input_ + '\n')
+ # if store and not hasattr(command, 'dont_save'):
+ # with open(self.game_file_name, 'a') as f:
+ # f.write(input_ + '\n')
except ArgError as e:
answer(connection_id, 'ARGUMENT_ERROR ' + quote(str(e)))
except PlayError as e:
"""
if connection_id:
for server in self.servers:
- if connection_id in server.clients:
+ if connection_id in server.clients:
client = server.clients[connection_id]
client.put(msg)
else:
def send(self, message, silent_connection_break=False):
"""Send via self.socket, encoded/delimited as way recv() expects.
- In detail, all \ and $ in message are escaped with prefixed \,
+ In detail, all \\ and $ in message are escaped with prefixed \\,
and an unescaped $ is appended as a message delimiter. Then,
socket.send() is called as often as necessary to ensure
message is sent fully, as socket.send() due to buffering may
In detail, socket.recv() is looped over for sequences of bytes
that can be decoded as a Unicode string delimited by an
- unescaped $, with \ and $ escapable by \. If a sequence of
+ unescaped $, with \\ and $ escapable by \\. If a sequence of
characters that ends in an unescaped $ cannot be decoded as
Unicode, None is returned as its representation. Stop once
socket.recv() returns nothing.
def server_close(self):
self.close()
-
if width_line != width_map:
raise ArgError('map line width %s unequal map width %s' % (width_line, width_map))
self.terrain = self.terrain[:y * width_map] + line +\
- self.terrain[(y + 1) * width_map:]
+ self.terrain[(y + 1) * width_map:]
def get_position_index(self, yx):
return yx.y * self.geometry.size.x + yx.x
def __init__(self, source_maps, source_center, radius, get_map):
self.source_maps = source_maps
self.radius = radius
- example_map = get_map(YX(0,0))
+ example_map = get_map(YX(0, 0))
self.source_geometry = example_map.geometry
size, self.offset, self.center = \
self.source_geometry.define_segment(source_center, radius)
if self.terrain[j] < self.terrain[i] - 1:
self.terrain[i] = self.terrain[j] + 1
shrunk = True
- #print('DEBUG Dijkstra')
- #line_to_print = []
- #x = 0
- #for n in self.terrain:
- # line_to_print += ['%3s' % n]
- # x += 1
- # if x >= self.size.x:
- # x = 0
- # print(' '.join(line_to_print))
- # line_to_print = []
+ # print('DEBUG Dijkstra')
+ # line_to_print = []
+ # x = 0
+ # for n in self.terrain:
+ # line_to_print += ['%3s' % n]
+ # x += 1
+ # if x >= self.size.x:
+ # x = 0
+ # print(' '.join(line_to_print))
+ # line_to_print = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
- self.terrain = '?' * self.size_i #self.size.y * self.size.x
+ self.terrain = '?' * self.size_i
self[self.center] = '.'
self.shadow_cones = []
self.circle_out(self.center, self.shadow_process)
def in_shadow_cone(new_cone):
for old_cone in self.shadow_cones:
if old_cone[0] <= new_cone[0] and \
- new_cone[1] <= old_cone[1]:
+ new_cone[1] <= old_cone[1]:
return True
# We might want to also shade tiles whose middle arm is inside a
# shadow cone for a darker FOV. Note that we then could not for
if unmerged:
self.shadow_cones += [cone]
- step_size = (CIRCLE/len(self.circle_out_directions)) / distance_to_center
+ step_size = (CIRCLE / len(self.circle_out_directions)) / distance_to_center
number_steps = dir_i * distance_to_center + dir_progress
- left_arm = correct_arm(step_size/2 + step_size*number_steps)
+ left_arm = correct_arm(step_size / 2 + step_size * number_steps)
right_arm = correct_arm(left_arm + step_size)
# Optimization potential: left cone could be derived from previous
tmpl_tokens = signature.split()
if len(tmpl_tokens) != len(args_tokens):
raise ArgError('Number of arguments (' + str(len(args_tokens)) +
- ') not expected number (' + str(len(tmpl_tokens))
- + ').')
+ ') not expected number (' + str(len(tmpl_tokens)) +
+ ').')
args = []
string_string = 'string'
for i in range(len(tmpl_tokens)):
pass
def do(self):
- for yxyx in[self.thing.position] + \
- list(self.thing.game.map_geometry.get_neighbors_yxyx(self.thing.position).values()):
+ for yxyx in [self.thing.position] + \
+ list(self.thing.game.map_geometry.get_neighbors_yxyx(
+ self.thing.position).values()):
if not self.thing.game.can_do_tile_with_pw(*yxyx, self.args[0]):
continue
self.thing.game.maps[yxyx[0]][yxyx[1]] = '.'
raise PlayError('already carrying something')
nothing_to_pick_up = True
for t in [t for t in self.thing.game.things
- if t != self.thing and t.position == self.thing.position
- and t.type_ != 'Player']:
+ if t != self.thing and t.position == self.thing.position and
+ t.type_ != 'Player']:
nothing_to_pick_up = False
break
if nothing_to_pick_up:
class ThingBase:
type_ = '?'
- def __init__(self, game, id_=0, position=(YX(0,0),YX(0,0))):
+ def __init__(self, game, id_=0, position=(YX(0, 0), YX(0, 0))):
self.game = game
if id_ == 0:
self.id_ = self.game.new_thing_id()
def reconnect(self):
self.log_msg('@ attempting reconnect')
self.send('QUIT')
- time.sleep(0.1) # FIXME necessitated by some some strange SSL race
- # conditions with ws4py, find out what exactly
+ # necessitated by some strange SSL race conditions with ws4py
+ time.sleep(0.1) # FIXME find out why exactly necessary
self.switch_mode('waiting_for_server')
self.connect()
if self.mode.name in {'control_tile_draw', 'control_tile_type',
'control_pw_type'}:
self.map_mode = 'protections'
- elif self.mode.name!= 'edit':
+ elif self.mode.name != 'edit':
self.map_mode = 'terrain + things'
if self.mode.shows_info or self.mode.name == 'control_tile_draw':
player = self.game.get_thing(self.game.player_id)
if type(self.game.map_geometry) == MapGeometryHex:
indent = 0
for line in map_lines_split:
- map_lines += [indent*' ' + ''.join(line)]
+ map_lines += [indent * ' ' + ''.join(line)]
indent = 0 if indent else 1
else:
for line in map_lines_split:
for i in range(self.size.y):
safe_addstr(i,
self.window_width * (not self.mode.has_input_prompt),
- ' '*self.window_width)
+ ' ' * self.window_width)
lines = []
for line in content.split('\n'):
lines += msg_into_lines_of_width(line, self.window_width)
draw_help()
curses.curs_set(False) # hide cursor
- curses.use_default_colors();
+ curses.use_default_colors()
stdscr.timeout(10)
reset_screen_size()
self.explorer = YX(0, 0)
if self.mode.mode_switch_on_key(self, key):
continue
elif key == self.keys['flatten'] and\
- 'FLATTEN_SURROUNDINGS' in self.game.tasks:
+ 'FLATTEN_SURROUNDINGS' in self.game.tasks:
self.send('TASK:FLATTEN_SURROUNDINGS ' + quote(self.password))
elif key == self.keys['toggle_map_mode']:
self.toggle_map_mode()