home · contact · privacy
Fix visibility tests on annotation, volume algorithm.
[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 def cmd_TASKS(game, connection_id):
8     tasks = []
9     game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
10 cmd_TASKS.argtypes = ''
11
12 def cmd_ALL(game, msg, connection_id):
13     import math
14     if not connection_id in game.sessions:
15         raise GameError('need to be logged in for this')
16     speaker = game.get_thing(game.sessions[connection_id], False)
17     dijkstra_map = Map(game.map_geometry.size)
18     n_max = 256
19     dijkstra_map.terrain = [n_max for i in range(dijkstra_map.size_i)]
20     dijkstra_map[speaker.position] = 0
21     shrunk = True
22     while shrunk:
23         shrunk = False
24         for pos in dijkstra_map:
25             if game.map[pos] == 'X':
26                 continue
27             neighbors = game.map_geometry.get_neighbors(pos)
28             for direction in [d for d in neighbors if neighbors[d]]:
29                 yx = neighbors[direction]
30                 if dijkstra_map[yx] < dijkstra_map[pos] - 1:
31                     dijkstra_map[pos] = dijkstra_map[yx] + 1
32                     shrunk = True
33     for c_id in game.sessions:
34         listener = game.get_thing(game.sessions[c_id], create_unfound=False)
35         volume = 1 / max(1, dijkstra_map[listener.position])
36         game.io.send('CHAT ' +
37                      quote('(volume: %.3f) %s: %s' % (volume, speaker.nickname, msg)),
38                      c_id)
39 cmd_ALL.argtypes = 'string'
40
41 def cmd_LOGIN(game, nick, connection_id):
42     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
43         raise GameError('name already in use')
44     if connection_id in game.sessions:
45         raise GameError('cannot log in twice')
46     t = game.thing_types['player'](game)
47     t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
48     game.things += [t]  # TODO refactor into Thing.__init__?
49     game.sessions[connection_id] = t.id_
50     game.io.send('LOGIN_OK', connection_id)
51     t.nickname = nick
52     game.io.send('CHAT ' + quote(t.nickname + ' entered the map.'))
53     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
54     game.changed = True
55 cmd_LOGIN.argtypes = 'string'
56
57 def cmd_NICK(game, nick, connection_id):
58     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
59         raise GameError('name already in use')
60     if not connection_id in game.sessions:
61         raise GameError('can only rename when already logged in')
62     t_id = game.sessions[connection_id]
63     t = game.get_thing(t_id, False)
64     old_nick = t.nickname
65     t.nickname = nick
66     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
67     game.changed = True
68 cmd_NICK.argtypes = 'string'
69
70 def cmd_GET_GAMESTATE(game, connection_id):
71     game.send_gamestate(connection_id)
72 cmd_GET_GAMESTATE.argtypes = ''
73
74 def cmd_QUERY(game, target_nick, msg, connection_id):
75     if not connection_id in game.sessions:
76         raise GameError('can only query when logged in')
77     t = game.get_thing(game.sessions[connection_id], False)
78     source_nick = t.nickname
79     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == target_nick]:
80         for c_id in game.sessions:
81             if game.sessions[c_id] == t.id_:
82                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
83                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
84                 return
85         raise GameError('target user offline')
86     raise GameError('can only query with registered nicknames')
87 cmd_QUERY.argtypes = 'string string'
88
89 def cmd_PING(game, connection_id):
90     game.io.send('PONG', connection_id)
91 cmd_PING.argtypes = ''
92
93 def cmd_TURN(game, n):
94     game.turn = n
95 cmd_TURN.argtypes = 'int:nonneg'
96
97 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
98     player = game.get_thing(game.sessions[connection_id], False)
99     if player.fov_stencil[yx] != '.':
100         raise GameError('cannot annotate tile outside field of view')
101     if not game.can_do_tile_with_pw(yx, pw):
102         raise GameError('wrong password for tile')
103     if msg == ' ':
104         if yx in game.annotations:
105             del game.annotations[yx]
106     else:
107         game.annotations[yx] = msg
108     game.changed = True
109 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
110
111 def cmd_PORTAL(game, yx, msg, pw, connection_id):
112     player = game.get_thing(game.sessions[connection_id], False)
113     if player.fov_stencil[yx] != '.':
114         raise GameError('cannot edit portal on tile outside field of view')
115     if not game.can_do_tile_with_pw(yx, pw):
116         raise GameError('wrong password for tile')
117     if msg == ' ':
118         if yx in game.portals:
119             del game.portals[yx]
120     else:
121         game.portals[yx] = msg
122     game.changed = True
123 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
124
125 def cmd_GOD_ANNOTATE(game, yx, msg):
126     game.annotations[yx] = msg
127     game.changed = True
128 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
129
130 def cmd_GOD_PORTAL(game, yx, msg):
131     game.portals[yx] = msg
132     game.changed = True
133 cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string'
134
135 def cmd_GET_ANNOTATION(game, yx, connection_id):
136     player = game.get_thing(game.sessions[connection_id], False)
137     annotation = '(unknown)';
138     if player.fov_stencil[yx] == '.':
139         annotation = '(none)';
140         if yx in game.annotations:
141             annotation = game.annotations[yx]
142     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
143 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
144
145 def cmd_MAP_LINE(game, y, line):
146     game.map.set_line(y, line)
147 cmd_MAP_LINE.argtypes = 'int:nonneg string'
148
149 def cmd_MAP(game, geometry, size):
150     map_geometry_class = globals()['MapGeometry' + geometry]
151     game.new_world(map_geometry_class(size))
152 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
153
154 def cmd_MAP_CONTROL_LINE(game, y, line):
155     game.map_control.set_line(y, line)
156 cmd_MAP_CONTROL_LINE.argtypes = 'int:nonneg string'
157
158 def cmd_MAP_CONTROL_PW(game, tile_class, password):
159     game.map_control_passwords[tile_class] = password
160 cmd_MAP_CONTROL_PW.argtypes = 'char string'