home · contact · privacy
Fix square grid scrolling bug.
[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.nickname, 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.nickname == 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     game.sessions[connection_id] = t.id_
86     game.io.send('LOGIN_OK', connection_id)
87     t.nickname = nick
88     game.io.send('CHAT ' + quote(t.nickname + ' entered the map.'))
89     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
90     game.changed = True
91 cmd_LOGIN.argtypes = 'string'
92
93 def cmd_NICK(game, nick, connection_id):
94     for t in [t for t in game.things if t.type_ == 'Player' and t.nickname == nick]:
95         raise GameError('name already in use')
96     if not connection_id in game.sessions:
97         raise GameError('can only rename when already logged in')
98     t_id = game.sessions[connection_id]
99     t = game.get_thing(t_id)
100     old_nick = t.nickname
101     t.nickname = nick
102     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
103     game.changed = True
104 cmd_NICK.argtypes = 'string'
105
106 def cmd_GET_GAMESTATE(game, connection_id):
107     game.send_gamestate(connection_id)
108 cmd_GET_GAMESTATE.argtypes = ''
109
110 #def cmd_QUERY(game, target_nick, msg, connection_id):
111 #    if not connection_id in game.sessions:
112 #        raise GameError('can only query when logged in')
113 #    t = game.get_thing(game.sessions[connection_id], False)
114 #    source_nick = t.nickname
115 #    for t in [t for t in game.things if t.type_ == 'Player' and t.nickname == target_nick]:
116 #        for c_id in game.sessions:
117 #            if game.sessions[c_id] == t.id_:
118 #                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
119 #                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
120 #                return
121 #        raise GameError('target user offline')
122 #    raise GameError('can only query with registered nicknames')
123 #cmd_QUERY.argtypes = 'string string'
124
125 def cmd_PING(game, connection_id):
126     game.io.send('PONG', connection_id)
127 cmd_PING.argtypes = ''
128
129 def cmd_TURN(game, n):
130     game.turn = n
131 cmd_TURN.argtypes = 'int:nonneg'
132
133 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
134     player = game.get_thing(game.sessions[connection_id])
135     if player.fov_stencil[yx] != '.':
136         raise GameError('cannot annotate tile outside field of view')
137     if not game.can_do_tile_with_pw(yx, pw):
138         raise GameError('wrong password for tile')
139     if msg == ' ':
140         if yx in game.annotations:
141             del game.annotations[yx]
142     else:
143         game.annotations[yx] = msg
144     game.changed = True
145 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
146
147 def cmd_PORTAL(game, yx, msg, pw, connection_id):
148     player = game.get_thing(game.sessions[connection_id])
149     if player.fov_stencil[yx] != '.':
150         raise GameError('cannot edit portal on tile outside field of view')
151     if not game.can_do_tile_with_pw(yx, pw):
152         raise GameError('wrong password for tile')
153     if msg == ' ':
154         if yx in game.portals:
155             del game.portals[yx]
156     else:
157         game.portals[yx] = msg
158     game.changed = True
159 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
160
161 def cmd_GOD_ANNOTATE(game, yx, msg):
162     game.annotations[yx] = msg
163     game.changed = True
164 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
165
166 def cmd_GOD_PORTAL(game, yx, msg):
167     game.portals[yx] = msg
168     game.changed = True
169 cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string'
170
171 def cmd_GET_ANNOTATION(game, yx, connection_id):
172     player = game.get_thing(game.sessions[connection_id])
173     annotation = '(unknown)';
174     if player.fov_stencil[yx] == '.':
175         annotation = '(none)';
176         if yx in game.annotations:
177             annotation = game.annotations[yx]
178     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
179 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
180
181 def cmd_MAP_LINE(game, y, line):
182     game.map.set_line(y, line)
183 cmd_MAP_LINE.argtypes = 'int:nonneg string'
184
185 def cmd_MAP(game, geometry, size):
186     map_geometry_class = globals()['MapGeometry' + geometry]
187     game.new_world(map_geometry_class(size))
188 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
189
190 def cmd_MAP_CONTROL_LINE(game, y, line):
191     game.map_control.set_line(y, line)
192 cmd_MAP_CONTROL_LINE.argtypes = 'int:nonneg string'
193
194 def cmd_MAP_CONTROL_PW(game, tile_class, password):
195     game.map_control_passwords[tile_class] = password
196 cmd_MAP_CONTROL_PW.argtypes = 'char string'
197
198 def cmd_THING(game, yx, thing_type, thing_id):
199     if not thing_type in game.thing_types:
200         raise GameError('illegal thing type %s' % thing_type)
201     if yx.y < 0 or yx.x < 0 or yx.y >= game.map.size.y or yx.x >= game.map.size.x:
202         raise GameError('illegal position %s' % yx)
203     t_old = None
204     if thing_id > 0:
205         t_old = game.get_thing(thing_id)
206     t_new = game.thing_types[thing_type](game, id_=thing_id, position=yx)
207     if t_old:
208         game.things[game.things.index(t_old)] = t_new
209     else:
210         game.things += [t_new]
211     game.changed = True
212 cmd_THING.argtypes = 'yx_tuple:nonneg string:thing_type int:nonneg'