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