"switch_to_study": "?",
"switch_to_edit": "E",
"switch_to_write": "m",
+ "switch_to_command_thing": "O",
"switch_to_name_thing": "N",
"switch_to_admin": "A",
"switch_to_control_pw_pw": "C",
cmd_TERRAINS.argtypes = ''
def cmd_ALL(game, msg, connection_id):
- from plomrogue.mapping import DijkstraMap
-
- def lower_msg_by_volume(msg, volume, largest_audible_distance):
- import random
- factor = largest_audible_distance / 4
- lowered_msg = ''
- for c in msg:
- c = c
- while random.random() > volume * factor:
- if c.isupper():
- c = c.lower()
- elif c != '.' and c != ' ':
- c = '.'
- else:
- c = ' '
- lowered_msg += c
- return lowered_msg
-
speaker = game.get_player(connection_id)
if not speaker:
raise GameError('need to be logged in for this')
- largest_audible_distance = 20
- things = [t for t in game.things if t.type_ != 'Player']
- dijkstra_map = DijkstraMap(things, game.maps, speaker.position,
- largest_audible_distance, game.get_map)
- for c_id in game.sessions:
- listener = game.get_player(c_id)
- target_yx = dijkstra_map.target_yx(*listener.position, True)
- if not target_yx:
- continue
- listener_distance = dijkstra_map[target_yx]
- if listener_distance > largest_audible_distance:
- continue
- volume = 1 / max(1, listener_distance)
- lowered_msg = lower_msg_by_volume(msg, volume, largest_audible_distance)
- lowered_nick = lower_msg_by_volume(speaker.name, volume,
- largest_audible_distance)
- game.io.send('CHAT ' +
- quote('(volume: %.2f) %s: %s' % (volume, lowered_nick,
- lowered_msg)),
- c_id)
+ speaker.sound(speaker.name, msg)
cmd_ALL.argtypes = 'string'
def cmd_SPAWN_POINT(game, big_yx, little_yx):
t.portable = False
t.thing_char = '#'
cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'
+
+def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
+ t = game.get_thing(thing_id)
+ if not t:
+ raise GameError('thing of ID %s not found' % thing_id)
+ if not t.type_ == 'MusicPlayer':
+ raise GameError('thing of ID %s not music player' % thing_id)
+ t.playing = playing
+ t.playlist_index = index
+ t.repeat = repeat
+cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int:nonneg bool'
+
+def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
+ t = game.get_thing(thing_id)
+ if not t:
+ raise GameError('thing of ID %s not found' % thing_id)
+ if not t.type_ == 'MusicPlayer':
+ raise GameError('thing of ID %s not music player' % thing_id)
+ t.playlist += [(title, length)]
+cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
write(f, 'GOD_THING_NAME %s %s' % (t.id_, quote(t.name)))
if t.type_ == 'Door' and t.blocking:
write(f, 'THING_DOOR_CLOSED %s' % t.id_)
+ if t.type_ == 'MusicPlayer':
+ write(f, 'THING_MUSICPLAYER_SETTINGS %s %s %s %s' %
+ (t.id_, int(t.playing), t.playlist_index, int(t.repeat)))
+ for item in t.playlist:
+ write(f, 'THING_MUSICPLAYER_PLAYLIST_ITEM %s %s %s' %
+ (t.id_, quote(item[0]), item[1]))
write(f, 'SPAWN_POINT %s %s' % (self.spawn_point[0],
self.spawn_point[1]))
if not arg.isdigit() or int(arg) < 1:
raise ArgError('Argument must be positive integer.')
args += [int(arg)]
+ elif tmpl == 'bool':
+ if not arg.isdigit() or int(arg) not in (0, 1):
+ raise ArgError('Argument must be 0 or 1.')
+ args += [bool(int(arg))]
elif tmpl == 'char':
try:
ord(arg)
if self.thing.game.sessions[c_id]['thing_id'] == self.thing.id_:
self.thing.game.io.send('RANDOM_COLORS', c_id)
self.thing.game.io.send('CHAT "You are drunk now."', c_id)
+ break
self.thing.drunk = 10000
+
+
+class Task_COMMAND(Task):
+ argtypes = 'string'
+
+ def check(self):
+ if self.thing.carrying is None:
+ raise PlayError('nothing to command')
+ if not self.thing.carrying.commandable:
+ raise PlayError('cannot command this item type')
+
+ def do(self):
+ from plomrogue.misc import quote
+ reply = self.thing.carrying.interpret(self.args[0])
+ for c_id in self.thing.game.sessions:
+ if self.thing.game.sessions[c_id]['thing_id'] == self.thing.id_:
+ self.thing.game.io.send('REPLY ' + quote(reply), c_id)
blocking = False
portable = False
protection = '.'
+ commandable = False
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def get_type(cls):
return cls.__name__[len('Thing_'):]
+ def sound(self, name, msg):
+ from plomrogue.mapping import DijkstraMap
+ from plomrogue.misc import quote
+
+ def lower_msg_by_volume(msg, volume, largest_audible_distance):
+ import random
+ factor = largest_audible_distance / 4
+ lowered_msg = ''
+ for c in msg:
+ c = c
+ while random.random() > volume * factor:
+ if c.isupper():
+ c = c.lower()
+ elif c != '.' and c != ' ':
+ c = '.'
+ else:
+ c = ' '
+ lowered_msg += c
+ return lowered_msg
+
+ largest_audible_distance = 20
+ # player's don't block sound (or should they?)
+ things = [t for t in self.game.things if t.type_ != 'Player']
+ dijkstra_map = DijkstraMap(things, self.game.maps, self.position,
+ largest_audible_distance, self.game.get_map)
+ for c_id in self.game.sessions:
+ listener = self.game.get_player(c_id)
+ target_yx = dijkstra_map.target_yx(*listener.position, True)
+ if not target_yx:
+ continue
+ listener_distance = dijkstra_map[target_yx]
+ if listener_distance > largest_audible_distance:
+ continue
+ volume = 1 / max(1, listener_distance)
+ lowered_msg = lower_msg_by_volume(msg, volume,
+ largest_audible_distance)
+ lowered_nick = lower_msg_by_volume(name, volume,
+ largest_audible_distance)
+ self.game.io.send('CHAT ' +
+ quote('(volume: %.2f) %s: %s' % (volume,
+ lowered_nick,
+ lowered_msg)),
+ c_id)
+
class Thing_Item(Thing):
+import datetime
+class Thing_MusicPlayer(Thing):
+ symbol_hint = 'R'
+ commandable = True
+ portable = True
+ playlist = []
+ repeat = True
+ next_song_start = datetime.datetime.now()
+ playlist_index = 0
+ playing = True
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.next_song_start = datetime.datetime.now()
+
+ def proceed(self):
+ if (not self.playing) or len(self.playlist) == 0:
+ return
+ if datetime.datetime.now() > self.next_song_start:
+ song_data = self.playlist[self.playlist_index]
+ self.playlist_index += 1
+ if self.playlist_index == len(self.playlist):
+ self.playlist_index = 0
+ if not self.repeat:
+ self.playing = False
+ self.next_song_start = datetime.datetime.now() +\
+ datetime.timedelta(seconds=song_data[1])
+ self.sound('MUSICPLAYER', song_data[0])
+ self.game.changed = True
+
+ def interpret(self, command):
+ if command == 'HELP':
+ msg = 'available commands:\n'
+ msg += 'HELP – show this help\n'
+ msg += 'PLAY – toggle playback on/off\n'
+ msg += 'REWIND – return to start of playlist\n'
+ msg += 'LIST – list programmed songs, durations\n'
+ msg += 'SKIP – to skip to next song\n'
+ msg += 'REPEAT – toggle playlist repeat on/off\n'
+ msg += 'ADD LENGTH SONG – add SONG to playlist, with LENGTH in format "minutes:seconds", i.e. something like "0:47" or "11:02"'
+ return msg
+ elif command == 'LIST':
+ msg = 'playlist:'
+ i = 0
+ for entry in self.playlist:
+ msg += '\n'
+ minutes = entry[1] // 60
+ seconds = entry[1] % 60
+ if seconds < 10:
+ seconds = '0%s' % seconds
+ selector = 'next:' if i == self.playlist_index else ' '
+ msg += '%s %s:%s – %s' % (selector, minutes, seconds, entry[0])
+ i += 1
+ return msg
+ elif command == 'PLAY':
+ self.playing = False if self.playing else True
+ self.game.changed = True
+ if self.playing:
+ return 'playing'
+ else:
+ return 'paused'
+ elif command == 'REWIND':
+ self.playlist_index = 0
+ self.next_song_start = datetime.datetime.now()
+ self.game.changed = True
+ return 'back at start of playlist'
+ elif command == 'SKIP':
+ self.next_song_start = datetime.datetime.now()
+ self.game.changed = True
+ return 'skipped'
+ elif command == 'REPEAT':
+ self.repeat = False if self.repeat else True
+ self.game.changed = True
+ if self.repeat:
+ return 'playlist repeat turned on'
+ else:
+ return 'playlist repeat turned off'
+ elif command.startswith('ADD '):
+ tokens = command.split(' ', 2)
+ if len(tokens) != 3:
+ return 'wrong syntax, see HELP'
+ length = tokens[1].split(':')
+ if len(length) != 2:
+ return 'wrong syntax, see HELP'
+ try:
+ minutes = int(length[0])
+ seconds = int(length[1])
+ except ValueError:
+ return 'wrong syntax, see HELP'
+ self.playlist += [(tokens[2], minutes * 60 + seconds)]
+ self.game.changed = True
+ return 'added'
+ else:
+ return 'cannot understand command'
+
+
+
class ThingAnimate(Thing):
blocking = True
drunk = 0
<button id="drop_thing"></button>
<button id="door"></button>
<button id="consume"></button>
+ <button id="switch_to_command_thing"></button>
<button id="teleport"></button>
</td>
</tr>
<li><input id="key_switch_to_edit" type="text" value="E" />
<li><input id="key_switch_to_write" type="text" value="m" />
<li><input id="key_switch_to_name_thing" type="text" value="N" />
+<li><input id="key_switch_to_command_thing" type="text" value="O" />
<li><input id="key_switch_to_password" type="text" value="P" />
<li><input id="key_switch_to_admin_enter" type="text" value="A" />
<li><input id="key_switch_to_control_pw_type" type="text" value="C" />
'short': 'name thing',
'long': 'Give name to/change name of thing here.'
},
+ 'command_thing': {
+ 'short': 'command thing',
+ 'long': 'Enter a command to the thing you carry. Enter nothing to return to play mode.'
+ },
'admin_thing_protect': {
'short': 'change thing protection',
'long': 'Change protection character for thing here.'
} else if (tokens[0] === 'TASKS') {
game.tasks = tokens[1].split(',');
tui.mode_write.legal = game.tasks.includes('WRITE');
+ tui.mode_command_thing.legal = game.tasks.includes('WRITE');
} else if (tokens[0] === 'THING_TYPE') {
game.thing_types[tokens[1]] = tokens[2]
} else if (tokens[0] === 'TERRAIN') {
tui.full_refresh();
} else if (tokens[0] === 'CHAT') {
tui.log_msg('# ' + tokens[1], 1);
+ } else if (tokens[0] === 'REPLY') {
+ tui.log_msg('#MUSICPLAYER: ' + tokens[1], 1);
} else if (tokens[0] === 'PLAYER_ID') {
game.player_id = parseInt(tokens[1]);
} else if (tokens[0] === 'LOGIN_OK') {
mode_portal: new Mode('portal', true, true),
mode_password: new Mode('password', true),
mode_name_thing: new Mode('name_thing', true, true),
+ mode_command_thing: new Mode('command_thing', true),
mode_admin_enter: new Mode('admin_enter', true),
mode_admin: new Mode('admin'),
mode_control_pw_pw: new Mode('control_pw_pw', true),
'drop_thing': 'DROP',
'move': 'MOVE',
'door': 'DOOR',
+ 'command': 'COMMAND',
'consume': 'INTOXICATE',
},
offset: [0,0],
map_lines: [],
init: function() {
this.mode_chat.available_modes = ["play", "study", "edit", "admin_enter"]
- this.mode_play.available_modes = ["chat", "study", "edit", "admin_enter"]
+ this.mode_play.available_modes = ["chat", "study", "edit", "admin_enter",
+ "command_thing"]
this.mode_play.available_actions = ["move", "take_thing", "drop_thing",
"teleport", "door", "consume"];
this.mode_study.available_modes = ["chat", "play", "admin_enter", "edit"]
}
} else if (this.mode.is_single_char_entry) {
this.show_help = true;
+ } else if (this.mode.name == 'command_thing') {
+ server.send(['TASK:COMMAND', 'HELP']);
} else if (this.mode.name == 'admin_enter') {
this.log_msg('@ enter admin password:')
} else if (this.mode.name == 'control_pw_type') {
tui.login_name = tui.inputEl.value;
server.send(['LOGIN', tui.inputEl.value]);
tui.inputEl.value = "";
+ } else if (tui.mode.name == 'command_thing' && event.key == 'Enter') {
+ if (tui.inputEl.value.length == 0) {
+ tui.log_msg('@ aborted');
+ tui.switch_mode('play');
+ } else if (tui.task_action_on('command')) {
+ server.send(['TASK:COMMAND', tui.inputEl.value]);
+ tui.inputEl.value = "";
+ }
} else if (tui.mode.name == 'control_pw_pw' && event.key == 'Enter') {
if (tui.inputEl.value.length == 0) {
tui.log_msg('@ aborted');
cmd_BECOME_ADMIN, cmd_SET_TILE_CONTROL,
cmd_GOD_THING_NAME, cmd_THING_DOOR_CLOSED,
cmd_GOD_THING_PROTECTION, cmd_THING_PROTECTION,
- cmd_SET_MAP_CONTROL_PASSWORD, cmd_SPAWN_POINT)
+ cmd_SET_MAP_CONTROL_PASSWORD, cmd_SPAWN_POINT,
+ cmd_THING_MUSICPLAYER_SETTINGS,
+ cmd_THING_MUSICPLAYER_PLAYLIST_ITEM)
from plomrogue.tasks import (Task_WAIT, Task_MOVE, Task_WRITE, Task_PICK_UP,
Task_DROP, Task_FLATTEN_SURROUNDINGS, Task_DOOR,
- Task_INTOXICATE)
+ Task_INTOXICATE, Task_COMMAND)
from plomrogue.things import (Thing_Player, Thing_Item, Thing_ItemSpawner,
Thing_SpawnPoint, Thing_SpawnPointSpawner,
Thing_Door, Thing_DoorSpawner, Thing_Consumable,
- Thing_ConsumableSpawner)
+ Thing_ConsumableSpawner, Thing_MusicPlayer)
from plomrogue.config import config
game = Game(config['savefile'])
game.register_command(cmd_SET_MAP_CONTROL_PASSWORD)
game.register_command(cmd_BECOME_ADMIN)
game.register_command(cmd_SPAWN_POINT)
+game.register_command(cmd_THING_MUSICPLAYER_SETTINGS)
+game.register_command(cmd_THING_MUSICPLAYER_PLAYLIST_ITEM)
game.register_task(Task_WAIT)
game.register_task(Task_MOVE)
game.register_task(Task_WRITE)
game.register_task(Task_DROP)
game.register_task(Task_DOOR)
game.register_task(Task_INTOXICATE)
+game.register_task(Task_COMMAND)
game.register_thing_type(Thing_Player)
game.register_thing_type(Thing_Item)
game.register_thing_type(Thing_ItemSpawner)
game.register_thing_type(Thing_DoorSpawner)
game.register_thing_type(Thing_Consumable)
game.register_thing_type(Thing_ConsumableSpawner)
+game.register_thing_type(Thing_MusicPlayer)
game.read_savefile()
game.io.start_loop()
for port in config['servers']:
'short': 'name thing',
'long': 'Give name to/change name of thing here.'
},
+ 'command_thing': {
+ 'short': 'command thing',
+ 'long': 'Enter a command to the thing you carry. Enter nothing to return to play mode.'
+ },
'admin_thing_protect': {
'short': 'change thing protection',
'long': 'Change protection character for thing here.'
game.tui.do_refresh = True
cmd_ADMIN_OK.argtypes = ''
+def cmd_REPLY(game, msg):
+ game.tui.log_msg('#MUSICPLAYER: ' + msg)
+ game.tui.do_refresh = True
+cmd_REPLY.argtypes = 'string'
+
def cmd_CHAT(game, msg):
game.tui.log_msg('# ' + msg)
game.tui.do_refresh = True
def cmd_TASKS(game, tasks_comma_separated):
game.tasks = tasks_comma_separated.split(',')
game.tui.mode_write.legal = 'WRITE' in game.tasks
+ game.tui.mode_command_thing.legal = 'COMMAND' in game.tasks
cmd_TASKS.argtypes = 'string'
def cmd_THING_TYPE(game, thing_type, symbol_hint):
self.register_command(cmd_ADMIN_OK)
self.register_command(cmd_PONG)
self.register_command(cmd_CHAT)
+ self.register_command(cmd_REPLY)
self.register_command(cmd_PLAYER_ID)
self.register_command(cmd_TURN)
self.register_command(cmd_THING)
mode_post_login_wait = Mode('post_login_wait', is_intro=True)
mode_password = Mode('password', has_input_prompt=True)
mode_name_thing = Mode('name_thing', has_input_prompt=True, shows_info=True)
+ mode_command_thing = Mode('command_thing', has_input_prompt=True)
is_admin = False
tile_draw = False
def __init__(self, host):
import os
import json
- self.mode_play.available_modes = ["chat", "study", "edit", "admin_enter"]
+ self.mode_play.available_modes = ["chat", "study", "edit", "admin_enter",
+ "command_thing"]
self.mode_play.available_actions = ["move", "take_thing", "drop_thing",
"teleport", "door", "consume"]
self.mode_study.available_modes = ["chat", "play", "admin_enter", "edit"]
'switch_to_edit': 'E',
'switch_to_write': 'm',
'switch_to_name_thing': 'N',
+ 'switch_to_command_thing': 'O',
'switch_to_admin_enter': 'A',
'switch_to_control_pw_type': 'C',
'switch_to_control_tile_type': 'Q',
self.send('LOGIN ' + quote(self.login_name))
else:
self.log_msg('@ enter username')
+ elif self.mode.name == 'command_thing':
+ self.send('TASK:COMMAND ' + quote('HELP'))
elif self.mode.name == 'admin_enter':
self.log_msg('@ enter admin password:')
elif self.mode.name == 'control_pw_type':
'drop_thing': 'DROP',
'door': 'DOOR',
'move': 'MOVE',
+ 'command': 'COMMAND',
'consume': 'INTOXICATE',
}
self.login_name = self.input_
self.send('LOGIN ' + quote(self.input_))
self.input_ = ""
+ elif self.mode.name == 'command_thing' and key == '\n':
+ if self.input_ == '':
+ self.log_msg('@ aborted')
+ self.switch_mode('play')
+ elif task_action_on('command'):
+ self.send('TASK:COMMAND ' + quote(self.input_))
+ self.input_ = ""
elif self.mode.name == 'control_pw_pw' and key == '\n':
if self.input_ == '':
self.log_msg('@ aborted')