home · contact · privacy
Shrink FOV map to radius.
[plomrogue2] / plomrogue / commands.py
1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3 from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex, Map
4
5
6
7 # TODO: instead of sending tasks, thing types etc. on request, send them on connection
8
9 def cmd_TASKS(game, connection_id):
10     tasks = []
11     game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
12 cmd_TASKS.argtypes = ''
13
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)),
17                      connection_id)
18 cmd_THING_TYPES.argtypes = ''
19
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])),
23                      connection_id)
24 cmd_TERRAINS.argtypes = ''
25
26 def cmd_ALL(game, msg, connection_id):
27
28     def lower_msg_by_volume(msg, volume):
29         lowered_msg = ''
30         for c in msg:
31             c = c
32             while random.random() > volume * 8:
33                 if c.isupper():
34                     c = c.lower()
35                 elif c != '.' and c != ' ':
36                     c = '.'
37                 else:
38                     c = ' '
39             lowered_msg += c
40         return lowered_msg
41
42     def dijkstra(speaker):
43         n_max = 255
44
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
48
49         shrunk = True
50         while shrunk:
51             shrunk = False
52             for i in range(map_size):
53                 if game.map.terrain[i] == 'X':
54                     continue
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
60                         shrunk = True
61         #print('DEBUG')
62         #line_to_print = []
63         #x = 0
64         #for n in dijkstra_map:
65         #    line_to_print += ['%3s' % n]
66         #    x += 1
67         #    if x >= game.map.size.x:
68         #        x = 0
69         #        print(' '.join(line_to_print))
70         #        line_to_print = []
71         return dijkstra_map
72
73     import random
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,
86                                                       lowered_msg)),
87                      c_id)
88 cmd_ALL.argtypes = 'string'
89
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)
101     t.name = nick
102     game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
103     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
104     game.changed = True
105 cmd_LOGIN.argtypes = 'string'
106
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)
114     old_nick = t.name
115     t.name = nick
116     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
117     game.changed = True
118 cmd_NICK.argtypes = 'string'
119
120 def cmd_GET_GAMESTATE(game, connection_id):
121     game.send_gamestate(connection_id)
122 cmd_GET_GAMESTATE.argtypes = ''
123
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)
134 #                return
135 #        raise GameError('target user offline')
136 #    raise GameError('can only query with registered nicknames')
137 #cmd_QUERY.argtypes = 'string string'
138
139 def cmd_PING(game, connection_id):
140     game.io.send('PONG', connection_id)
141 cmd_PING.argtypes = ''
142
143 def cmd_TURN(game, n):
144     game.turn = n
145 cmd_TURN.argtypes = 'int:nonneg'
146
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')
154     if msg == ' ':
155         if corrected_yx in game.annotations:
156             del game.annotations[corrected_yx]
157     else:
158         game.annotations[corrected_yx] = msg
159     game.changed = True
160 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
161
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')
169     if msg == ' ':
170         if corrected_yx in game.portals:
171             del game.portals[corrected_yx]
172     else:
173         game.portals[corrected_yx] = msg
174     game.changed = True
175 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
176
177 def cmd_GOD_ANNOTATE(game, yx, msg):
178     game.annotations[yx] = msg
179     game.changed = True
180 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
181
182 def cmd_GOD_PORTAL(game, yx, msg):
183     game.portals[yx] = msg
184     game.changed = True
185 cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string'
186
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'
197
198 def cmd_MAP_LINE(game, y, line):
199     game.map.set_line(y, line)
200 cmd_MAP_LINE.argtypes = 'int:nonneg string'
201
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'
206
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'
210
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'
214
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)
220     t_old = None
221     if thing_id > 0:
222         t_old = game.get_thing(thing_id)
223     t_new = game.thing_types[thing_type](game, id_=thing_id, position=yx)
224     if t_old:
225         game.things[game.things.index(t_old)] = t_new
226     else:
227         game.things += [t_new]
228     game.changed = True
229 cmd_THING.argtypes = 'yx_tuple:nonneg string:thing_type int:nonneg'
230
231 def cmd_THING_NAME(game, thing_id, name):
232     t = game.get_thing(thing_id)
233     if not t:
234         raise GameError('thing of ID %s not found' % thing_id)
235     t.name = name
236 cmd_THING_NAME.argtypes = 'int:pos string'