home · contact · privacy
Simplify toilet need calcuation, add pee-into-pants emergency exit.
[plomrogue2] / plomrogue / commands.py
1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError, ArgError
3 from plomrogue.misc import Terrain
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     game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
11 cmd_TASKS.argtypes = ''
12
13 def cmd_THING_TYPES(game, connection_id):
14     for t_t in game.thing_types.values():
15         game.io.send('THING_TYPE %s %s' % (t_t.get_type(), quote(t_t.symbol_hint)),
16                      connection_id)
17 cmd_THING_TYPES.argtypes = ''
18
19 def cmd_TERRAINS(game, connection_id):
20     for t in game.terrains.values():
21         game.io.send('TERRAIN %s %s' % (quote(t.character),
22                                         quote(t.description)), connection_id)
23 cmd_TERRAINS.argtypes = ''
24
25 def cmd_TERRAIN(game, character, description,
26                 blocks_light, blocks_sound, blocks_movement):
27     game.terrains[character] = Terrain(character, description, blocks_light,
28                                        blocks_sound, blocks_movement)
29 cmd_TERRAIN.argtypes = 'char string bool bool bool'
30
31 def cmd_TERRAIN_TAG(game, character, tag):
32     if not character in game.terrains:
33         raise GameError('terrain does not exist: %s' % character)
34     game.terrains[character].tags.add(tag)
35 cmd_TERRAIN_TAG.argtypes = 'char string'
36
37 def cmd_ALL(game, msg, connection_id):
38     speaker = game.get_player(connection_id)
39     if not speaker:
40         raise GameError('need to be logged in for this')
41     if speaker.drunk > 0:
42         import random
43         drunken_msg = ''
44         for c in msg:
45             if random.random() > 0.85:
46                 c = random.choice(['l', 'll', 'n', 'nhg', 'w', 'wl', 'bw'])
47             drunken_msg += c
48         msg = drunken_msg
49     speaker.sound(speaker.name, msg)
50 cmd_ALL.argtypes = 'string'
51
52 def cmd_SPAWN_POINT(game, big_yx, little_yx):
53     if little_yx.y >= game.map_geometry.size.y or \
54        little_yx.x >= game.map_geometry.size.x:
55         raise GameError('illegal spawn point')
56     game.spawn_point = big_yx, little_yx
57 cmd_SPAWN_POINT.argtypes = 'yx_tuple yx_tuple:nonneg'
58
59 def cmd_LOGIN(game, nick, connection_id):
60     nick = nick.strip()
61     if len(nick) == 0:
62         raise GameError('empty name')
63     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
64         raise GameError('name already in use')
65     if game.get_player(connection_id):
66         raise GameError('cannot log in twice')
67     t = game.add_thing('Player', game.spawn_point)
68     t.name = nick
69     t.thing_char = game.get_next_player_char()
70     game.sessions[connection_id] = {
71         'thing_id': t.id_,
72         'status': 'player'
73     }
74     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
75     game.io.send('LOGIN_OK', connection_id)
76     game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
77     for s in [s for s in game.things
78               if s.type_ == 'SpawnPoint' and s.name == t.name]:
79         t.position = s.position
80         break
81     # game.changed = True  # handled by game.add_thing
82 cmd_LOGIN.argtypes = 'string'
83
84 def cmd_BECOME_ADMIN(game, password, connection_id):
85     player = game.get_player(connection_id)
86     if not player:
87         raise GameError('need to be logged in for this')
88     if password in game.admin_passwords:
89         game.sessions[connection_id]['status'] = 'admin'
90         game.io.send('ADMIN_OK', connection_id)
91     else:
92         raise GameError('wrong password')
93 cmd_BECOME_ADMIN.argtypes = 'string'
94
95 def cmd_ADMIN_PASSWORD(game, password):
96     game.admin_passwords += [password]
97 cmd_ADMIN_PASSWORD.argtypes = 'string'
98
99 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
100     player = game.get_player(connection_id)
101     if not player:
102         raise GameError('need to be logged in for this')
103     if not game.sessions[connection_id]['status'] == 'admin':
104         raise GameError('need to be admin for this')
105     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
106     map_control = game.get_map(big_yx, 'control')
107     map_control[little_yx] = control_char
108     game.changed = True
109     game.record_change((big_yx, little_yx), 'fov')
110 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
111
112 def cmd_THING_PROTECTION(game, protection_char, connection_id):
113     player = game.get_player(connection_id)
114     if not player:
115         raise GameError('need to be logged in for this')
116     if not game.sessions[connection_id]['status'] == 'admin':
117         raise GameError('need to be admin for this')
118     if not player.carrying:
119         raise GameError('need to carry a thing to protect it')
120     player.carrying.protection = protection_char
121     game.changed = True
122     game.record_change(player.carrying.position, 'other')
123 cmd_THING_PROTECTION.argtypes = 'char'
124
125 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
126     player = game.get_player(connection_id)
127     if not player:
128         raise GameError('need to be logged in for this')
129     if not game.sessions[connection_id]['status'] == 'admin':
130         raise GameError('need to be admin for this')
131     if tile_class == '.':
132         raise GameError('tile class "." must remain unprotected')
133     game.map_control_passwords[tile_class] = password
134 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
135
136 def cmd_NICK(game, nick, connection_id):
137     nick = nick.strip()
138     if len(nick) == 0:
139         raise GameError('empty name')
140     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
141         raise GameError('name already in use')
142     t = game.get_player(connection_id)
143     if not t:
144         raise GameError('can only rename when already logged in')
145     old_nick = t.name
146     t.name = nick
147     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
148     game.changed = True
149     game.record_change(t.position, 'other')
150 cmd_NICK.argtypes = 'string'
151
152 def cmd_GET_GAMESTATE(game, connection_id):
153     game.send_gamestate(connection_id)
154 cmd_GET_GAMESTATE.argtypes = ''
155
156 # def cmd_QUERY(game, target_nick, msg, connection_id):
157 #     if not connection_id in game.sessions:
158 #         raise GameError('can only query when logged in')
159 #     t = game.get_thing(game.sessions[connection_id], False)
160 #     source_nick = t.name
161 #     for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
162 #         for c_id in game.sessions:
163 #             if game.sessions[c_id] == t.id_:
164 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
165 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
166 #                 return
167 #         raise GameError('target user offline')
168 #     raise GameError('can only query with registered nicknames')
169 # cmd_QUERY.argtypes = 'string string'
170
171 def cmd_PING(game, connection_id):
172     game.io.send('PONG', connection_id)
173 cmd_PING.argtypes = ''
174
175 def cmd_TURN(game, n):
176     game.turn = n
177 cmd_TURN.argtypes = 'int:nonneg'
178
179 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
180     if len(msg) > 500:
181         raise ArgError('annotation text must be <= 500 characters')
182     player = game.get_player(connection_id)
183     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
184     if not player.fov_test(big_yx, little_yx):
185         raise GameError('cannot annotate tile outside field of view')
186     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
187         raise GameError('wrong password for tile')
188     if msg == ' ':
189         if big_yx in game.annotations:
190             if little_yx in game.annotations[big_yx]:
191                 del game.annotations[big_yx][little_yx]
192     else:
193         if big_yx not in game.annotations:
194             game.annotations[big_yx] = {}
195         game.annotations[big_yx][little_yx] = msg
196     game.record_change([big_yx, little_yx], 'other')
197     game.changed = True
198 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
199
200 def cmd_PORTAL(game, yx, msg, pw, connection_id):
201     player = game.get_player(connection_id)
202     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
203     if not player.fov_test(big_yx, little_yx):
204         raise GameError('cannot edit portal on tile outside field of view')
205     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
206         raise GameError('wrong password for tile')
207     if msg == ' ':
208         if big_yx in game.portals:
209             if little_yx in game.portals[big_yx]:
210                 del game.portals[big_yx][little_yx]
211     else:
212         if big_yx not in game.portals:
213             game.portals[big_yx] = {}
214         game.portals[big_yx][little_yx] = msg
215     game.record_change([big_yx, little_yx], 'other')
216     game.changed = True
217 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
218
219 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
220     if big_yx not in game.annotations:
221         game.annotations[big_yx] = {}
222     game.annotations[big_yx][little_yx] = msg
223     #game.changed = True
224 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
225
226 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
227     if big_yx not in game.portals:
228         game.portals[big_yx] = {}
229     game.portals[big_yx][little_yx] = msg
230     #game.changed = True
231 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
232
233 def cmd_MAP_LINE(game, big_yx, y, line):
234     map_ = game.get_map(big_yx)
235     map_.set_line(y, line)
236 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
237
238 def cmd_MAP(game, geometry, size):
239     from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
240     map_geometry_class = MapGeometrySquare
241     if geometry == 'Hex':
242         map_geometry_class = MapGeometryHex
243     game.new_world(map_geometry_class(size))
244     game.terrains = {}
245 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
246
247 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
248     map_control = game.get_map(big_yx, 'control')
249     map_control.set_line(y, line)
250 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
251
252 def cmd_MAP_CONTROL_PW(game, tile_class, password):
253     game.map_control_passwords[tile_class] = password
254 cmd_MAP_CONTROL_PW.argtypes = 'char string'
255
256 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
257     if thing_type not in game.thing_types:
258         raise GameError('illegal thing type %s' % thing_type)
259     _ = game.get_map(big_yx)
260     game.add_thing(thing_type, (big_yx, little_yx), id_=thing_id)
261 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
262
263 def cmd_THING_NAME(game, name, pw, connection_id):
264     player = game.get_player(connection_id)
265     if not player:
266         raise GameError('need to be logged in for this')
267     if not player.carrying:
268         raise GameError('need to carry a thing to rename it')
269     if not game.can_do_thing_with_pw(player.carrying, pw):
270         raise GameError('wrong password for thing')
271     if name == ' ':
272         if hasattr(player.carrying.__class__, 'name'):
273             raise GameError('cannot un-name things of this type')
274         if hasattr(player.carrying, 'name'):
275             del player.carrying.name
276     else:
277         player.carrying.name = name
278     game.changed = True
279     game.record_change(player.carrying.position, 'other')
280 cmd_THING_NAME.argtypes = 'string string'
281
282 def cmd_GOD_THING_NAME(game, thing_id, name):
283     t = game.get_thing(thing_id)
284     if not t:
285         raise GameError('thing of ID %s not found' % thing_id)
286     t.name = name
287 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
288
289 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
290     t = game.get_thing(thing_id)
291     if not t:
292         raise GameError('thing of ID %s not found' % thing_id)
293     t.protection = protection_char
294 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
295
296 def cmd_THING_DOOR_CLOSED(game, thing_id):
297     t = game.get_thing(thing_id)
298     if not t:
299         raise GameError('thing of ID %s not found' % thing_id)
300     if not t.type_ == 'Door':
301         raise GameError('thing of ID %s not door' % thing_id)
302     t.close()
303 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'
304
305 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
306     t = game.get_thing(thing_id)
307     if not t:
308         raise GameError('thing of ID %s not found' % thing_id)
309     if not t.type_ == 'MusicPlayer':
310         raise GameError('thing of ID %s not music player' % thing_id)
311     t.playing = playing
312     t.playlist_index = index
313     t.repeat = repeat
314 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int bool'
315
316 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
317     t = game.get_thing(thing_id)
318     if not t:
319         raise GameError('thing of ID %s not found' % thing_id)
320     if not t.type_ == 'MusicPlayer':
321         raise GameError('thing of ID %s not music player' % thing_id)
322     t.playlist += [(title, length)]
323 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
324
325 def cmd_THING_BOTTLE_EMPTY(game, thing_id):
326     t = game.get_thing(thing_id)
327     if not t:
328         raise GameError('thing of ID %s not found' % thing_id)
329     if not t.type_ == 'Bottle':
330         raise GameError('thing of ID %s not bottle' % thing_id)
331     t.empty()
332 cmd_THING_BOTTLE_EMPTY.argtypes = 'int:pos'
333
334 def cmd_THING_INSTALLED(game, thing_id):
335     t = game.get_thing(thing_id)
336     if not t:
337         raise GameError('thing of ID %s not found' % thing_id)
338     if not hasattr(t, 'installable'):
339         raise GameError('thing of ID %s not installable' % thing_id)
340     t.install()
341 cmd_THING_INSTALLED.argtypes = 'int:pos'
342
343 def cmd_PLAYER_FACE(game, face, connection_id):
344     t = game.get_player(connection_id)
345     if not t:
346         raise GameError('can only draw face when already logged in')
347     if len(face) != 18:
348         raise GameError('wrong face string length')
349     game.faces[t.name] = face
350     game.changed = True
351     game.record_change(t.position, 'other')
352 cmd_PLAYER_FACE.argtypes = 'string'
353
354 def cmd_PLAYER_HAT(game, hat, connection_id):
355     t = game.get_player(connection_id)
356     if not t:
357         raise GameError('can only edit hat when already logged in')
358     if not t.name in game.hats:
359         raise GameError('not currently wearing an editable hat')
360     if len(hat) != 18:
361         raise GameError('wrong hat string length')
362     legal_chars = t.get_cookie_chars()
363     for c in hat:
364         if c not in legal_chars:
365             raise GameError('used illegal character: "%s" – '
366                             'allowed characters: %s'
367                             % (c, legal_chars))
368     game.hats[t.name] = hat
369     game.changed = True
370     game.record_change(t.position, 'other')
371 cmd_PLAYER_HAT.argtypes = 'string'
372
373 def cmd_GOD_PLAYER_FACE(game, name, face):
374     if len(face) != 18:
375         raise GameError('wrong face string length')
376     game.faces[name] = face
377 cmd_GOD_PLAYER_FACE.argtypes = 'string string'
378
379 def cmd_GOD_PLAYER_HAT(game, name, hat):
380     if len(hat) != 18:
381         raise GameError('wrong hat string length')
382     game.hats[name] = hat
383 cmd_GOD_PLAYER_HAT.argtypes = 'string string'
384
385 def cmd_GOD_PLAYERS_HAT_CHARS(game, name, hat_chars):
386     game.players_hat_chars[name] = hat_chars
387 cmd_GOD_PLAYERS_HAT_CHARS.argtypes = 'string string'
388
389 def cmd_THING_HAT_DESIGN(game, thing_id, design):
390     if len(design) != 18:
391         raise GameError('hat design of wrong length')
392     t = game.get_thing(thing_id)
393     if not t:
394         raise GameError('thing of ID %s not found' % thing_id)
395     if t.type_ != 'Hat':
396         raise GameError('thing of ID %s not a hat' % thing_id)
397     t.design = design
398 cmd_THING_HAT_DESIGN.argtypes = 'int:pos string'