home · contact · privacy
3b2464b42baa6d7c429832ea34138c970d00570d
[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 and thing types 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(), t_t.symbol_hint),
17                      connection_id)
18 cmd_THING_TYPES.argtypes = ''
19
20 def cmd_ALL(game, msg, connection_id):
21
22     def lower_msg_by_volume(msg, volume):
23         lowered_msg = ''
24         for c in msg:
25             c = c
26             while random.random() > volume * 8:
27                 if c.isupper():
28                     c = c.lower()
29                 elif c != '.' and c != ' ':
30                     c = '.'
31                 else:
32                     c = ' '
33             lowered_msg += c
34         return lowered_msg
35
36     import random
37     if not connection_id in game.sessions:
38         raise GameError('need to be logged in for this')
39     speaker = game.get_thing(game.sessions[connection_id])
40     n_max = 255
41     map_size = game.map.size_i
42     dijkstra_map = [n_max for i in range(game.map.size_i)]
43     dijkstra_map[game.map.get_position_index(speaker.position)] = 0
44     shrunk = True
45     while shrunk:
46         shrunk = False
47         for i in range(map_size):
48             if game.map.terrain[i] == 'X':
49                 continue
50             neighbors = game.map_geometry.get_neighbors_i(i)
51             for direction in [d for d in neighbors if neighbors[d]]:
52                 j = neighbors[direction]
53                 if dijkstra_map[j] < dijkstra_map[i] - 1:
54                     dijkstra_map[i] = dijkstra_map[j] + 1
55                     shrunk = True
56     #print('DEBUG')
57     #line_to_print = []
58     #x = 0
59     #for n in dijkstra_map:
60     #    line_to_print += ['%3s' % n]
61     #    x += 1
62     #    if x >= game.map.size.x:
63     #        x = 0
64     #        print(' '.join(line_to_print))
65     for c_id in game.sessions:
66         listener = game.get_thing(game.sessions[c_id])
67         listener_vol = dijkstra_map[game.map.get_position_index(listener.position)]
68         volume = 1 / max(1, listener_vol)
69         lowered_msg = lower_msg_by_volume(msg, volume)
70         lowered_nick = lower_msg_by_volume(speaker.name, volume)
71         game.io.send('CHAT ' +
72                      quote('(volume: %.2f) %s: %s' % (volume, lowered_nick,
73                                                       lowered_msg)),
74                      c_id)
75 cmd_ALL.argtypes = 'string'
76
77 def cmd_LOGIN(game, nick, connection_id):
78     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
79         raise GameError('name already in use')
80     if connection_id in game.sessions:
81         raise GameError('cannot log in twice')
82     t = game.thing_types['Player'](game)
83     t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
84     game.things += [t]  # TODO refactor into Thing.__init__?
85     t.player_char = game.get_next_player_char()
86     game.sessions[connection_id] = t.id_
87     game.io.send('LOGIN_OK', connection_id)
88     t.name = nick
89     game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
90     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
91     game.changed = True
92 cmd_LOGIN.argtypes = 'string'
93
94 def cmd_NICK(game, nick, connection_id):
95     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
96         raise GameError('name already in use')
97     if not connection_id in game.sessions:
98         raise GameError('can only rename when already logged in')
99     t_id = game.sessions[connection_id]
100     t = game.get_thing(t_id)
101     old_nick = t.name
102     t.name = nick
103     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
104     game.changed = True
105 cmd_NICK.argtypes = 'string'
106
107 def cmd_GET_GAMESTATE(game, connection_id):
108     game.send_gamestate(connection_id)
109 cmd_GET_GAMESTATE.argtypes = ''
110
111 #def cmd_QUERY(game, target_nick, msg, connection_id):
112 #    if not connection_id in game.sessions:
113 #        raise GameError('can only query when logged in')
114 #    t = game.get_thing(game.sessions[connection_id], False)
115 #    source_nick = t.name
116 #    for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
117 #        for c_id in game.sessions:
118 #            if game.sessions[c_id] == t.id_:
119 #                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
120 #                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
121 #                return
122 #        raise GameError('target user offline')
123 #    raise GameError('can only query with registered nicknames')
124 #cmd_QUERY.argtypes = 'string string'
125
126 def cmd_PING(game, connection_id):
127     game.io.send('PONG', connection_id)
128 cmd_PING.argtypes = ''
129
130 def cmd_TURN(game, n):
131     game.turn = n
132 cmd_TURN.argtypes = 'int:nonneg'
133
134 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
135     player = game.get_thing(game.sessions[connection_id])
136     if player.fov_stencil[yx] != '.':
137         raise GameError('cannot annotate tile outside field of view')
138     if not game.can_do_tile_with_pw(yx, pw):
139         raise GameError('wrong password for tile')
140     if msg == ' ':
141         if yx in game.annotations:
142             del game.annotations[yx]
143     else:
144         game.annotations[yx] = msg
145     game.changed = True
146 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
147
148 def cmd_PORTAL(game, yx, msg, pw, connection_id):
149     player = game.get_thing(game.sessions[connection_id])
150     if player.fov_stencil[yx] != '.':
151         raise GameError('cannot edit portal on tile outside field of view')
152     if not game.can_do_tile_with_pw(yx, pw):
153         raise GameError('wrong password for tile')
154     if msg == ' ':
155         if yx in game.portals:
156             del game.portals[yx]
157     else:
158         game.portals[yx] = msg
159     game.changed = True
160 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
161
162 def cmd_GOD_ANNOTATE(game, yx, msg):
163     game.annotations[yx] = msg
164     game.changed = True
165 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
166
167 def cmd_GOD_PORTAL(game, yx, msg):
168     game.portals[yx] = msg
169     game.changed = True
170 cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string'
171
172 def cmd_GET_ANNOTATION(game, yx, connection_id):
173     player = game.get_thing(game.sessions[connection_id])
174     annotation = '(unknown)';
175     if player.fov_stencil[yx] == '.':
176         annotation = '(none)';
177         if yx in game.annotations:
178             annotation = game.annotations[yx]
179     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
180 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
181
182 def cmd_MAP_LINE(game, y, line):
183     game.map.set_line(y, line)
184 cmd_MAP_LINE.argtypes = 'int:nonneg string'
185
186 def cmd_MAP(game, geometry, size):
187     map_geometry_class = globals()['MapGeometry' + geometry]
188     game.new_world(map_geometry_class(size))
189 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
190
191 def cmd_MAP_CONTROL_LINE(game, y, line):
192     game.map_control.set_line(y, line)
193 cmd_MAP_CONTROL_LINE.argtypes = 'int:nonneg string'
194
195 def cmd_MAP_CONTROL_PW(game, tile_class, password):
196     game.map_control_passwords[tile_class] = password
197 cmd_MAP_CONTROL_PW.argtypes = 'char string'
198
199 def cmd_THING(game, yx, thing_type, thing_id):
200     if not thing_type in game.thing_types:
201         raise GameError('illegal thing type %s' % thing_type)
202     if yx.y < 0 or yx.x < 0 or yx.y >= game.map.size.y or yx.x >= game.map.size.x:
203         raise GameError('illegal position %s' % yx)
204     t_old = None
205     if thing_id > 0:
206         t_old = game.get_thing(thing_id)
207     t_new = game.thing_types[thing_type](game, id_=thing_id, position=yx)
208     if t_old:
209         game.things[game.things.index(t_old)] = t_new
210     else:
211         game.things += [t_new]
212     game.changed = True
213 cmd_THING.argtypes = 'yx_tuple:nonneg string:thing_type int:nonneg'