1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3 from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex, Map
7 # TODO: instead of sending tasks, thing types etc. on request, send them on connection
9 def cmd_TASKS(game, connection_id):
11 game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
12 cmd_TASKS.argtypes = ''
14 def cmd_THING_TYPES(game, connection_id):
15 for t_t in game.thing_types.values():
16 game.io.send('THING_TYPE %s %s' % (t_t.get_type(), quote(t_t.symbol_hint)),
18 cmd_THING_TYPES.argtypes = ''
20 def cmd_TERRAINS(game, connection_id):
21 for t in game.terrains.keys():
22 game.io.send('TERRAIN %s %s' % (quote(t), quote(game.terrains[t])),
24 cmd_TERRAINS.argtypes = ''
26 def cmd_ALL(game, msg, connection_id):
28 def lower_msg_by_volume(msg, volume):
32 while random.random() > volume * 8:
35 elif c != '.' and c != ' ':
42 def dijkstra(speaker):
45 map_size = game.map.size_i
46 dijkstra_map = [n_max for i in range(game.map.size_i)]
47 dijkstra_map[game.map.get_position_index(speaker.position)] = 0
52 for i in range(map_size):
53 if game.map.terrain[i] == 'X':
55 neighbors = game.map_geometry.get_neighbors_i(i)
56 for direction in [d for d in neighbors if neighbors[d]]:
57 j = neighbors[direction]
58 if dijkstra_map[j] < dijkstra_map[i] - 1:
59 dijkstra_map[i] = dijkstra_map[j] + 1
64 #for n in dijkstra_map:
65 # line_to_print += ['%3s' % n]
67 # if x >= game.map.size.x:
69 # print(' '.join(line_to_print))
74 if not connection_id in game.sessions:
75 raise GameError('need to be logged in for this')
76 speaker = game.get_thing(game.sessions[connection_id])
77 dijkstra_map = dijkstra(speaker)
78 for c_id in game.sessions:
79 listener = game.get_thing(game.sessions[c_id])
80 listener_vol = dijkstra_map[game.map.get_position_index(listener.position)]
81 volume = 1 / max(1, listener_vol)
82 lowered_msg = lower_msg_by_volume(msg, volume)
83 lowered_nick = lower_msg_by_volume(speaker.name, volume)
84 game.io.send('CHAT ' +
85 quote('(volume: %.2f) %s: %s' % (volume, lowered_nick,
88 cmd_ALL.argtypes = 'string'
90 def cmd_LOGIN(game, nick, connection_id):
91 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
92 raise GameError('name already in use')
93 if connection_id in game.sessions:
94 raise GameError('cannot log in twice')
95 t = game.thing_types['Player'](game)
96 t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
97 game.things += [t] # TODO refactor into Thing.__init__?
98 t.player_char = game.get_next_player_char()
99 game.sessions[connection_id] = t.id_
100 game.io.send('LOGIN_OK', connection_id)
102 game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
103 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
105 cmd_LOGIN.argtypes = 'string'
107 def cmd_NICK(game, nick, connection_id):
108 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
109 raise GameError('name already in use')
110 if not connection_id in game.sessions:
111 raise GameError('can only rename when already logged in')
112 t_id = game.sessions[connection_id]
113 t = game.get_thing(t_id)
116 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
118 cmd_NICK.argtypes = 'string'
120 def cmd_GET_GAMESTATE(game, connection_id):
121 game.send_gamestate(connection_id)
122 cmd_GET_GAMESTATE.argtypes = ''
124 #def cmd_QUERY(game, target_nick, msg, connection_id):
125 # if not connection_id in game.sessions:
126 # raise GameError('can only query when logged in')
127 # t = game.get_thing(game.sessions[connection_id], False)
128 # source_nick = t.name
129 # for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
130 # for c_id in game.sessions:
131 # if game.sessions[c_id] == t.id_:
132 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
133 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
135 # raise GameError('target user offline')
136 # raise GameError('can only query with registered nicknames')
137 #cmd_QUERY.argtypes = 'string string'
139 def cmd_PING(game, connection_id):
140 game.io.send('PONG', connection_id)
141 cmd_PING.argtypes = ''
143 def cmd_TURN(game, n):
145 cmd_TURN.argtypes = 'int:nonneg'
147 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
148 player = game.get_thing(game.sessions[connection_id])
149 corrected_yx = yx + player.fov_stencil.offset
150 if not player.fov_test(corrected_yx):
151 raise GameError('cannot annotate tile outside field of view')
152 if not game.can_do_tile_with_pw(corrected_yx, pw):
153 raise GameError('wrong password for tile')
155 if corrected_yx in game.annotations:
156 del game.annotations[corrected_yx]
158 game.annotations[corrected_yx] = msg
160 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
162 def cmd_PORTAL(game, yx, msg, pw, connection_id):
163 player = game.get_thing(game.sessions[connection_id])
164 corrected_yx = yx + player.fov_stencil.offset
165 if not player.fov_test(corrected_yx):
166 raise GameError('cannot edit portal on tile outside field of view')
167 if not game.can_do_tile_with_pw(corrected_yx, pw):
168 raise GameError('wrong password for tile')
170 if corrected_yx in game.portals:
171 del game.portals[corrected_yx]
173 game.portals[corrected_yx] = msg
175 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
177 def cmd_GOD_ANNOTATE(game, yx, msg):
178 game.annotations[yx] = msg
180 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
182 def cmd_GOD_PORTAL(game, yx, msg):
183 game.portals[yx] = msg
185 cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string'
187 def cmd_GET_ANNOTATION(game, yx, connection_id):
188 player = game.get_thing(game.sessions[connection_id])
189 corrected_yx = yx + player.fov_stencil.offset
190 annotation = '(unknown)';
191 if player.fov_test(corrected_yx):
192 annotation = '(none)';
193 if corrected_yx in game.annotations:
194 annotation = game.annotations[corrected_yx]
195 game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
196 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
198 def cmd_MAP_LINE(game, y, line):
199 game.map.set_line(y, line)
200 cmd_MAP_LINE.argtypes = 'int:nonneg string'
202 def cmd_MAP(game, geometry, size):
203 map_geometry_class = globals()['MapGeometry' + geometry]
204 game.new_world(map_geometry_class(size))
205 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
207 def cmd_MAP_CONTROL_LINE(game, y, line):
208 game.map_control.set_line(y, line)
209 cmd_MAP_CONTROL_LINE.argtypes = 'int:nonneg string'
211 def cmd_MAP_CONTROL_PW(game, tile_class, password):
212 game.map_control_passwords[tile_class] = password
213 cmd_MAP_CONTROL_PW.argtypes = 'char string'
215 def cmd_THING(game, yx, thing_type, thing_id):
216 if not thing_type in game.thing_types:
217 raise GameError('illegal thing type %s' % thing_type)
218 if yx.y < 0 or yx.x < 0 or yx.y >= game.map.size.y or yx.x >= game.map.size.x:
219 raise GameError('illegal position %s' % yx)
222 t_old = game.get_thing(thing_id)
223 t_new = game.thing_types[thing_type](game, id_=thing_id, position=yx)
225 game.things[game.things.index(t_old)] = t_new
227 game.things += [t_new]
229 cmd_THING.argtypes = 'yx_tuple:nonneg string:thing_type int:nonneg'
231 def cmd_THING_NAME(game, thing_id, name):
232 t = game.get_thing(thing_id)
234 raise GameError('thing of ID %s not found' % thing_id)
236 cmd_THING_NAME.argtypes = 'int:pos string'