13 terminal rows: <input id="n_rows" type="number" step=4 min=24 value=24 />
14 / terminal columns: <input id="n_cols" type="number" step=4 min=80 value=80 />
15 / <a href="https://plomlompom.com/repos/?p=plomrogue2;a=summary">source code</a> (includes proper terminal/ncurses client)
17 <pre id="terminal"></pre>
18 <textarea id="input" style="opacity: 0; width: 0px;"></textarea>
20 keyboard input/control: <span id="keyboard_control"></span>
22 <h3>button controls for hard-to-remember keybindings</h3>
23 <table id="move_table" style="float: left">
25 <td style="text-align: right"><button id="hex_move_upleft"></button></td>
26 <td style="text-align: center"><button id="square_move_up"></button></td>
27 <td><button id="hex_move_upright"></button></td>
30 <td style="text-align: right;"><button id="square_move_left"></button><button id="hex_move_left">left</button></td>
31 <td stlye="text-align: center;">move</td>
32 <td><button id="square_move_right"></button><button id="hex_move_right"></button></td>
35 <td><button id="hex_move_downleft"></button></td>
36 <td style="text-align: center"><button id="square_move_down"></button></td>
37 <td><button id="hex_move_downright"></button></td>
42 <td><button id="help"></button></td>
45 <td><button id="switch_to_chat"></button><br /></td>
48 <td><button id="switch_to_study"></button></td>
49 <td><button id="toggle_map_mode"></button>
52 <td><button id="switch_to_play"></button></td>
54 <button id="switch_to_take_thing"></button>
55 <button id="switch_to_drop_thing"></button>
56 <button id="door"></button>
57 <button id="consume"></button>
58 <button id="switch_to_command_thing"></button>
59 <button id="teleport"></button>
60 <button id="install"></button>
61 <button id="wear"></button>
62 <button id="spin"></button>
66 <td><button id="switch_to_edit"></button></td>
68 <button id="switch_to_write"></button>
69 <button id="flatten"></button>
70 <button id="switch_to_annotate"></button>
71 <button id="switch_to_portal"></button>
72 <button id="switch_to_name_thing"></button>
73 <button id="switch_to_password"></button>
74 <button id="switch_to_enter_face"></button>
78 <td><button id="switch_to_admin_enter"></button></td>
80 <button id="switch_to_control_pw_type"></button>
81 <button id="switch_to_control_tile_type"></button>
82 <button id="switch_to_admin_thing_protect"></button>
83 <button id="toggle_tile_draw"></button>
88 <h3>edit keybindings</h3> (see <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values">here</a> for non-obvious available values):<br />
90 <li>move up (square grid): <input id="key_square_move_up" type="text" value="w" /> (hint: ArrowUp)
91 <li>move left (square grid): <input id="key_square_move_left" type="text" value="a" /> (hint: ArrowLeft)
92 <li>move down (square grid): <input id="key_square_move_down" type="text" value="s" /> (hint: ArrowDown)
93 <li>move right (square grid): <input id="key_square_move_right" type="text" value="d" /> (hint: ArrowRight)
94 <li>move up-left (hex grid): <input id="key_hex_move_upleft" type="text" value="w" />
95 <li>move up-right (hex grid): <input id="key_hex_move_upright" type="text" value="e" />
96 <li>move right (hex grid): <input id="key_hex_move_right" type="text" value="d" />
97 <li>move down-right (hex grid): <input id="key_hex_move_downright" type="text" value="x" />
98 <li>move down-left (hex grid): <input id="key_hex_move_downleft" type="text" value="y" />
99 <li>move left (hex grid): <input id="key_hex_move_left" type="text" value="a" />
100 <li>help: <input id="key_help" type="text" value="h" />
101 <li>flatten surroundings: <input id="key_flatten" type="text" value="F" />
102 <li>teleport: <input id="key_teleport" type="text" value="p" />
103 <li>spin: <input id="key_spin" type="text" value="S" />
104 <li>open/close: <input id="key_door" type="text" value="D" />
105 <li>consume: <input id="key_consume" type="text" value="C" />
106 <li>install: <input id="key_install" type="text" value="I" />
107 <li>(un-)wear: <input id="key_wear" type="text" value="W" />
108 <li><input id="key_switch_to_drop_thing" type="text" value="u" />
109 <li><input id="key_switch_to_enter_face" type="text" value="f" />
110 <li><input id="key_switch_to_take_thing" type="text" value="z" />
111 <li><input id="key_switch_to_chat" type="text" value="t" />
112 <li><input id="key_switch_to_play" type="text" value="p" />
113 <li><input id="key_switch_to_study" type="text" value="?" />
114 <li><input id="key_switch_to_edit" type="text" value="E" />
115 <li><input id="key_switch_to_write" type="text" value="m" />
116 <li><input id="key_switch_to_name_thing" type="text" value="N" />
117 <li><input id="key_switch_to_command_thing" type="text" value="O" />
118 <li><input id="key_switch_to_password" type="text" value="P" />
119 <li><input id="key_switch_to_admin_enter" type="text" value="A" />
120 <li><input id="key_switch_to_control_pw_type" type="text" value="C" />
121 <li><input id="key_switch_to_control_tile_type" type="text" value="Q" />
122 <li><input id="key_switch_to_admin_thing_protect" type="text" value="T" />
123 <li><input id="key_switch_to_annotate" type="text" value="M" />
124 <li><input id="key_switch_to_portal" type="text" value="T" />
125 <li>toggle map view: <input id="key_toggle_map_mode" type="text" value="L" />
126 <li>toggle protection character drawing: <input id="key_toggle_tile_draw" type="text" value="m" />
131 let websocket_location = "wss://plomlompom.com/rogue_chat/";
132 //let websocket_location = "ws://localhost:8000/";
138 'long': 'This mode allows you to interact with the map in various ways.'
143 'long': 'This mode allows you to study the map and its tiles in detail. Move the question mark over a tile, and the right half of the screen will show detailed information on it. Toggle the map view to show or hide different information layers.'},
145 'short': 'world edit',
147 'long': 'This mode allows you to change the game world in various ways. Individual map tiles can be protected by "protection characters", which you can see by toggling into the protections map view. You can edit a tile if you set the world edit password that matches its protection character. The character "." marks the absence of protection: Such tiles can always be edited.'
150 'short': 'name thing',
152 'long': 'Give name to/change name of thing here.'
155 'short': 'command thing',
157 'long': 'Enter a command to the thing you carry. Enter nothing to return to play mode.'
160 'short': 'take thing',
161 'intro': 'Pick up a thing in reach by entering its index number. Enter nothing to abort.',
162 'long': 'You see a list of things which you could pick up. Enter the target thing\'s index, or, to leave, nothing.'
165 'short': 'drop thing',
166 'intro': 'Enter number of direction to which you want to drop thing.',
167 'long': 'Drop currently carried thing by entering the target direction index. Enter nothing to return to play mode..'
169 'admin_thing_protect': {
170 'short': 'change thing protection',
171 'intro': '@ enter thing protection character:',
172 'long': 'Change protection character for thing here.'
175 'short': 'enter your face',
176 'intro': '@ enter face line (enter nothing to abort):',
177 'long': 'Draw your face as ASCII art. The string you enter must be 18 characters long, and will be divided on display into 3 lines of 6 characters each, from top to bottom..'
180 'short': 'change terrain',
182 'long': 'This mode allows you to change the map tile you currently stand on (if your world editing password authorizes you so). Just enter any printable ASCII character to imprint it on the ground below you.'
185 'short': 'change protection character password',
186 'intro': '@ enter protection character for which you want to change the password:',
187 'long': 'This mode is the first of two steps to change the password for a protection character. First enter the protection character for which you want to change the password.'
190 'short': 'change protection character password',
192 'long': 'This mode is the second of two steps to change the password for a protection character. Enter the new password for the protection character you chose.'
194 'control_tile_type': {
195 'short': 'change tiles protection',
196 'intro': '@ enter protection character which you want to draw:',
197 'long': 'This mode is the first of two steps to change tile protection areas on the map. First enter the tile protection character you want to write.'
199 'control_tile_draw': {
200 'short': 'change tiles protection',
202 'long': 'This mode is the second of two steps to change tile protection areas on the map. Toggle tile protection drawing on/off and move the ?? cursor around the map to draw the selected protection character.'
205 'short': 'annotate tile',
207 'long': 'This mode allows you to add/edit a comment on the tile you are currently standing on (provided your world editing password authorizes you so). Hit Return to leave.'
210 'short': 'edit portal',
212 'long': 'This mode allows you to imprint/edit/remove a teleportation target on the ground you are currently standing on (provided your world editing password authorizes you so). Enter or edit a URL to imprint a teleportation target; enter emptiness to remove a pre-existing teleportation target. Hit Return to leave.'
217 'long': 'This mode allows you to engage in chit-chat with other users. Any line you enter into the input prompt that does not start with a "/" will be sent out to nearby players – but barriers and distance will reduce what they can read, so stand close to them to ensure they get your message. Lines that start with a "/" are used for commands like:\n\n/nick NAME – re-name yourself to NAME'
222 'long': 'Enter your player name.'
224 'waiting_for_server': {
225 'short': 'waiting for server response',
226 'intro': '@ waiting for server …',
227 'long': 'Waiting for a server response.'
230 'short': 'waiting for server response',
232 'long': 'Waiting for a server response.'
235 'short': 'set world edit password',
237 'long': 'This mode allows you to change the password that you send to authorize yourself for editing password-protected elements of the world. Hit return to confirm and leave.'
240 'short': 'become admin',
241 'intro': '@ enter admin password:',
242 'long': 'This mode allows you to become admin if you know an admin password.'
247 'long': 'This mode allows you access to actions limited to administrators.'
250 let key_descriptions = {
252 'flatten': 'flatten surroundings',
253 'teleport': 'teleport',
254 'door': 'open/close',
255 'consume': 'consume',
256 'install': '(un-)install',
259 'toggle_map_mode': 'toggle map view',
260 'toggle_tile_draw': 'toggle protection character drawing',
261 'hex_move_upleft': 'up-left',
262 'hex_move_upright': 'up-right',
263 'hex_move_right': 'right',
264 'hex_move_left': 'left',
265 'hex_move_downleft': 'down-left',
266 'hex_move_downright': 'down-right',
267 'square_move_up': 'up',
268 'square_move_left': 'left',
269 'square_move_down': 'down',
270 'square_move_right': 'right',
272 for (const mode_name of Object.keys(mode_helps)) {
273 key_descriptions['switch_to_' + mode_name] = mode_helps[mode_name].short;
276 let rows_selector = document.getElementById("n_rows");
277 let cols_selector = document.getElementById("n_cols");
278 let key_selectors = document.querySelectorAll('[id^="key_"]');
280 for (const key_switch_selector of document.querySelectorAll('[id^="key_switch_to_"]')) {
281 const action = key_switch_selector.id.slice("key_switch_to_".length);
282 key_switch_selector.parentNode.prepend(mode_helps[action].short + ': ');
285 function restore_selector_value(selector) {
286 let stored_selection = window.localStorage.getItem(selector.id);
287 if (stored_selection) {
288 selector.value = stored_selection;
291 restore_selector_value(rows_selector);
292 restore_selector_value(cols_selector);
293 for (let key_selector of key_selectors) {
294 restore_selector_value(key_selector);
297 function escapeHTML(str) {
299 replace(/&/g, '&').
300 replace(/</g, '<').
301 replace(/>/g, '>').
302 replace(/'/g, ''').
303 replace(/"/g, '"');
307 initialize: function() {
308 this.rows = rows_selector.value;
309 this.cols = cols_selector.value;
310 this.pre_el = document.getElementById("terminal");
311 this.set_default_colors();
315 for (let y = 0, x = 0; y <= this.rows; x++) {
316 if (x == this.cols) {
319 this.content.push(line);
321 if (y == this.rows) {
328 apply_colors: function() {
329 this.pre_el.style.color = this.foreground;
330 this.pre_el.style.backgroundColor = this.background;
332 set_default_colors: function() {
333 this.foreground = 'white';
334 this.background = 'black';
337 set_random_colors: function() {
338 function rand(offset) {
339 return Math.floor(offset + Math.random() * 96).toString(16).padStart(2, '0');
341 this.foreground = '#' + rand(159) + rand(159) + rand(159);
342 this.background = '#' + rand(0) + rand(0) + rand(0);
345 blink_screen: function() {
346 this.pre_el.style.color = this.background;
347 this.pre_el.style.backgroundColor = this.foreground;
349 this.pre_el.style.color = this.foreground;
350 this.pre_el.style.backgroundColor = this.background;
353 refresh: function() {
354 let pre_content = '';
355 for (let y = 0; y < this.rows; y++) {
356 let line = this.content[y].join('');
358 if (y in tui.links) {
360 for (let span of tui.links[y]) {
361 chunks.push(escapeHTML(line.slice(start_x, span[0])));
362 chunks.push('<a target="_blank" href="');
363 chunks.push(escapeHTML(span[2]));
365 chunks.push(escapeHTML(line.slice(span[0], span[1])));
369 chunks.push(escapeHTML(line.slice(start_x)));
371 chunks = [escapeHTML(line)];
373 for (const chunk of chunks) {
374 pre_content += chunk;
378 this.pre_el.innerHTML = pre_content;
380 write: function(start_y, start_x, msg) {
381 for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
382 this.content[start_y][x] = msg[i];
385 drawBox: function(start_y, start_x, height, width) {
386 let end_y = start_y + height;
387 let end_x = start_x + width;
388 for (let y = start_y, x = start_x; y < this.rows; x++) {
396 this.content[y][x] = ' ';
400 terminal.initialize();
403 tokenize: function(str) {
408 for (let i = 0; i < str.length; i++) {
414 } else if (c == '\\') {
416 } else if (c == '"') {
421 } else if (c == '"') {
423 } else if (c === ' ') {
424 if (token.length > 0) {
432 if (token.length > 0) {
437 parse_yx: function(position_string) {
438 let coordinate_strings = position_string.split(',')
439 let position = [0, 0];
440 position[0] = parseInt(coordinate_strings[0].slice(2));
441 position[1] = parseInt(coordinate_strings[1].slice(2));
453 init: function(url) {
455 this.websocket = new WebSocket(this.url);
456 this.websocket.onopen = function(event) {
457 server.connected = true;
458 game.thing_types = {};
460 server.send(['TASKS']);
461 server.send(['TERRAINS']);
462 server.send(['THING_TYPES']);
463 tui.log_msg("@ server connected! :)");
464 tui.switch_mode('login');
466 this.websocket.onclose = function(event) {
467 server.connected = false;
468 tui.switch_mode('waiting_for_server');
469 tui.log_msg("@ server disconnected :(");
471 this.websocket.onmessage = this.handle_event;
473 reconnect_to: function(url) {
474 this.websocket.close();
477 send: function(tokens) {
478 this.websocket.send(unparser.untokenize(tokens));
480 handle_event: function(event) {
481 let tokens = parser.tokenize(event.data);
482 if (tokens[0] === 'TURN') {
483 game.turn_complete = false;
484 explorer.empty_annotations();
488 game.turn = parseInt(tokens[1]);
489 } else if (tokens[0] === 'THING') {
490 let t = game.get_thing(tokens[4], true);
491 t.position = parser.parse_yx(tokens[1]);
493 t.protection = tokens[3];
494 t.portable = parseInt(tokens[5]);
495 t.commandable = parseInt(tokens[6]);
496 } else if (tokens[0] === 'THING_NAME') {
497 let t = game.get_thing(tokens[1], false);
499 } else if (tokens[0] === 'THING_FACE') {
500 let t = game.get_thing(tokens[1], false);
502 } else if (tokens[0] === 'THING_HAT') {
503 let t = game.get_thing(tokens[1], false);
505 } else if (tokens[0] === 'THING_CHAR') {
506 let t = game.get_thing(tokens[1], false);
507 t.thing_char = tokens[2];
508 } else if (tokens[0] === 'TASKS') {
509 game.tasks = tokens[1].split(',');
510 tui.mode_write.legal = game.tasks.includes('WRITE');
511 tui.mode_command_thing.legal = game.tasks.includes('WRITE');
512 tui.mode_take_thing.legal = game.tasks.includes('PICK_UP');
513 tui.mode_drop_thing.legal = game.tasks.includes('DROP');
514 } else if (tokens[0] === 'THING_TYPE') {
515 game.thing_types[tokens[1]] = tokens[2]
516 } else if (tokens[0] === 'THING_CARRYING') {
517 let t = game.get_thing(tokens[1], false);
518 t.carrying = t = game.get_thing(tokens[2], false);
519 } else if (tokens[0] === 'THING_INSTALLED') {
520 let t = game.get_thing(tokens[1], false);
522 } else if (tokens[0] === 'TERRAIN') {
523 game.terrains[tokens[1]] = tokens[2]
524 } else if (tokens[0] === 'MAP') {
525 game.map_geometry = tokens[1];
527 game.map_size = parser.parse_yx(tokens[2]);
529 } else if (tokens[0] === 'FOV') {
531 } else if (tokens[0] === 'MAP_CONTROL') {
532 game.map_control = tokens[1]
533 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
534 game.turn_complete = true;
535 if (tui.mode.name == 'post_login_wait') {
536 tui.switch_mode('play');
538 explorer.info_cached = false;
540 } else if (tokens[0] === 'CHAT') {
541 tui.log_msg('# ' + tokens[1], 1);
542 } else if (tokens[0] === 'REPLY') {
543 tui.log_msg('#MUSICPLAYER: ' + tokens[1], 1);
544 } else if (tokens[0] === 'PLAYER_ID') {
545 game.player_id = parseInt(tokens[1]);
546 } else if (tokens[0] === 'LOGIN_OK') {
547 this.send(['GET_GAMESTATE']);
548 tui.switch_mode('post_login_wait');
549 } else if (tokens[0] === 'DEFAULT_COLORS') {
550 terminal.set_default_colors();
551 } else if (tokens[0] === 'RANDOM_COLORS') {
552 terminal.set_random_colors();
553 } else if (tokens[0] === 'ADMIN_OK') {
555 tui.log_msg('@ you now have admin rights');
556 tui.switch_mode('admin');
557 } else if (tokens[0] === 'PORTAL') {
558 let position = parser.parse_yx(tokens[1]);
559 game.portals[position] = tokens[2];
560 } else if (tokens[0] === 'ANNOTATION') {
561 let position = parser.parse_yx(tokens[1]);
562 explorer.update_annotations(position, tokens[2]);
564 } else if (tokens[0] === 'UNHANDLED_INPUT') {
565 tui.log_msg('? unknown command');
566 } else if (tokens[0] === 'PLAY_ERROR') {
567 tui.log_msg('? ' + tokens[1]);
568 terminal.blink_screen();
569 } else if (tokens[0] === 'ARGUMENT_ERROR') {
570 tui.log_msg('? syntax error: ' + tokens[1]);
571 } else if (tokens[0] === 'GAME_ERROR') {
572 tui.log_msg('? game error: ' + tokens[1]);
573 } else if (tokens[0] === 'PONG') {
576 tui.log_msg('? unhandled input: ' + event.data);
582 quote: function(str) {
584 for (let i = 0; i < str.length; i++) {
586 if (['"', '\\'].includes(c)) {
592 return quoted.join('');
594 to_yx: function(yx_coordinate) {
595 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
597 untokenize: function(tokens) {
598 let quoted_tokens = [];
599 for (let token of tokens) {
600 quoted_tokens.push(this.quote(token));
602 return quoted_tokens.join(" ");
607 constructor(name, has_input_prompt=false, shows_info=false,
608 is_intro=false, is_single_char_entry=false) {
610 this.short_desc = mode_helps[name].short;
611 this.available_modes = [];
612 this.available_actions = [];
613 this.has_input_prompt = has_input_prompt;
614 this.shows_info= shows_info;
615 this.is_intro = is_intro;
616 this.help_intro = mode_helps[name].long;
617 this.intro_msg = mode_helps[name].intro;
618 this.is_single_char_entry = is_single_char_entry;
621 *iter_available_modes() {
622 for (let mode_name of this.available_modes) {
623 let mode = tui['mode_' + mode_name];
627 let key = tui.keys['switch_to_' + mode.name];
631 list_available_modes() {
633 if (this.available_modes.length > 0) {
634 msg += 'Other modes available from here:\n';
635 for (let [mode, key] of this.iter_available_modes()) {
636 msg += '[' + key + '] – ' + mode.short_desc + '\n';
641 mode_switch_on_key(key_event) {
642 for (let [mode, key] of this.iter_available_modes()) {
643 if (key_event.key == key) {
644 event.preventDefault();
645 tui.switch_mode(mode.name);
657 window_width: terminal.cols / 2,
665 mode_waiting_for_server: new Mode('waiting_for_server',
667 mode_login: new Mode('login', true, false, true),
668 mode_post_login_wait: new Mode('post_login_wait'),
669 mode_chat: new Mode('chat', true),
670 mode_annotate: new Mode('annotate', true, true),
671 mode_play: new Mode('play'),
672 mode_study: new Mode('study', false, true),
673 mode_write: new Mode('write', false, false, false, true),
674 mode_edit: new Mode('edit'),
675 mode_control_pw_type: new Mode('control_pw_type', true),
676 mode_admin_thing_protect: new Mode('admin_thing_protect', true),
677 mode_portal: new Mode('portal', true, true),
678 mode_password: new Mode('password', true),
679 mode_name_thing: new Mode('name_thing', true, true),
680 mode_command_thing: new Mode('command_thing', true),
681 mode_take_thing: new Mode('take_thing', true),
682 mode_drop_thing: new Mode('drop_thing', true),
683 mode_enter_face: new Mode('enter_face', true),
684 mode_admin_enter: new Mode('admin_enter', true),
685 mode_admin: new Mode('admin'),
686 mode_control_pw_pw: new Mode('control_pw_pw', true),
687 mode_control_tile_type: new Mode('control_tile_type', true),
688 mode_control_tile_draw: new Mode('control_tile_draw'),
690 'flatten': 'FLATTEN_SURROUNDINGS',
691 'take_thing': 'PICK_UP',
692 'drop_thing': 'DROP',
695 'install': 'INSTALL',
697 'command': 'COMMAND',
698 'consume': 'INTOXICATE',
705 this.mode_play.available_modes = ["chat", "study", "edit", "admin_enter",
706 "command_thing", "take_thing", "drop_thing"]
707 this.mode_play.available_actions = ["move", "teleport", "door", "consume",
708 "install", "wear", "spin"];
709 this.mode_study.available_modes = ["chat", "play", "admin_enter", "edit"]
710 this.mode_study.available_actions = ["toggle_map_mode", "move_explorer"];
711 this.mode_admin.available_modes = ["admin_thing_protect", "control_pw_type",
712 "control_tile_type", "chat",
713 "study", "play", "edit"]
714 this.mode_admin.available_actions = ["move"];
715 this.mode_control_tile_draw.available_modes = ["admin_enter"]
716 this.mode_control_tile_draw.available_actions = ["toggle_tile_draw"];
717 this.mode_edit.available_modes = ["write", "annotate", "portal", "name_thing",
718 "password", "chat", "study", "play",
719 "admin_enter", "enter_face"]
720 this.mode_edit.available_actions = ["move", "flatten", "toggle_map_mode"]
721 this.inputEl = document.getElementById("input");
722 this.inputEl.focus();
723 this.switch_mode('waiting_for_server');
724 this.recalc_input_lines();
725 this.height_header = this.height_turn_line + this.height_mode_line;
728 init_keys: function() {
729 document.getElementById("move_table").hidden = true;
731 for (let key_selector of key_selectors) {
732 this.keys[key_selector.id.slice(4)] = key_selector.value;
734 this.movement_keys = {};
735 let geometry_prefix = 'undefinedMapGeometry_';
736 if (game.map_geometry) {
737 geometry_prefix = game.map_geometry.toLowerCase() + '_';
739 for (const key_name of Object.keys(key_descriptions)) {
740 if (key_name.startsWith(geometry_prefix)) {
741 let direction = key_name.split('_')[2].toUpperCase();
742 let key = this.keys[key_name];
743 this.movement_keys[key] = direction;
746 for (const move_button of document.querySelectorAll('[id*="_move_"]')) {
747 if (move_button.id.startsWith('key_')) {
750 move_button.hidden = true;
752 for (const move_button of document.querySelectorAll('[id^="' + geometry_prefix + 'move_"]')) {
753 document.getElementById("move_table").hidden = false;
754 move_button.hidden = false;
756 for (let el of document.getElementsByTagName("button")) {
757 let action_desc = key_descriptions[el.id];
758 let action_key = '[' + this.keys[el.id] + ']';
759 el.innerHTML = escapeHTML(action_desc) + '<br /><span class="keyboard_controlled">' + escapeHTML(action_key) + '</span>';
762 task_action_on: function(action) {
763 return game.tasks.includes(this.action_tasks[action]);
765 switch_mode: function(mode_name) {
766 if (this.mode && this.mode.name == 'control_tile_draw') {
767 tui.log_msg('@ finished tile protection drawing.')
769 this.tile_draw = false;
770 const player = game.things[game.player_id];
771 if (mode_name == 'command_thing' && (!player.carrying || !player.carrying.commandable)) {
772 this.log_msg('? not carrying anything commandable');
773 terminal.blink_screen();
774 this.switch_mode('play');
777 if (mode_name == 'drop_thing' && (!player.carrying)) {
778 this.log_msg('? not carrying anything droppable');
779 terminal.blink_screen();
780 this.switch_mode('play');
783 if (mode_name == 'admin_enter' && this.is_admin) {
785 } else if (['name_thing', 'admin_thing_protect'].includes(mode_name)) {
787 for (let t_id in game.things) {
788 if (t_id == game.player_id) {
791 let t = game.things[t_id];
792 if (player.position[0] == t.position[0]
793 && player.position[1] == t.position[1]) {
799 terminal.blink_screen();
800 this.log_msg('? not standing over thing');
803 this.selected_thing_id = thing_id;
806 this.mode = this['mode_' + mode_name];
807 if (["control_tile_draw", "control_tile_type", "control_pw_type"].includes(this.mode.name)) {
808 this.map_mode = 'protections';
809 } else if (this.mode.name != "edit") {
810 this.map_mode = 'terrain + things';
812 if (this.mode.has_input_prompt || this.mode.is_single_char_entry) {
813 this.inputEl.focus();
815 if (game.player_id in game.things && (this.mode.shows_info || this.mode.name == 'control_tile_draw')) {
816 explorer.position = game.things[game.player_id].position;
818 this.inputEl.value = "";
819 this.restore_input_values();
820 for (let el of document.getElementsByTagName("button")) {
823 document.getElementById("help").disabled = false;
824 for (const action of this.mode.available_actions) {
825 if (["move", "move_explorer"].includes(action)) {
826 for (const move_key of document.querySelectorAll('[id*="_move_"]')) {
827 move_key.disabled = false;
829 } else if (Object.keys(this.action_tasks).includes(action)) {
830 if (this.task_action_on(action)) {
831 document.getElementById(action).disabled = false;
834 document.getElementById(action).disabled = false;
837 for (const mode_name of this.mode.available_modes) {
838 document.getElementById('switch_to_' + mode_name).disabled = false;
840 if (this.mode.intro_msg.length > 0) {
841 this.log_msg(this.mode.intro_msg);
843 if (this.mode.name == 'login') {
844 if (this.login_name) {
845 server.send(['LOGIN', this.login_name]);
847 this.log_msg("? need login name");
849 } else if (this.mode.is_single_char_entry) {
850 this.show_help = true;
851 } else if (this.mode.name == 'take_thing') {
852 this.log_msg("Portable things in reach for pick-up:");
853 const player = game.things[game.player_id];
854 const y = player.position[0]
855 const x = player.position[1]
856 let select_range = [y.toString() + ':' + x.toString(),
857 (y + 0).toString() + ':' + (x - 1).toString(),
858 (y + 0).toString() + ':' + (x + 1).toString(),
859 (y - 1).toString() + ':' + (x).toString(),
860 (y + 1).toString() + ':' + (x).toString()];
861 if (game.map_geometry == 'Hex') {
863 select_range.push((y - 1).toString() + ':' + (x + 1).toString());
864 select_range.push((y + 1).toString() + ':' + (x + 1).toString());
866 select_range.push((y - 1).toString() + ':' + (x - 1).toString());
867 select_range.push((y + 1).toString() + ':' + (x - 1).toString());
870 this.selectables = [];
871 for (const t_id in game.things) {
872 const t = game.things[t_id];
873 if (select_range.includes(t.position[0].toString()
874 + ':' + t.position[1].toString())
876 this.selectables.push(t_id);
879 if (this.selectables.length == 0) {
880 this.log_msg('none');
881 terminal.blink_screen();
882 this.switch_mode('play');
885 for (let [i, t_id] of this.selectables.entries()) {
886 const t = game.things[t_id];
887 this.log_msg(i + ': ' + explorer.get_thing_info(t));
890 } else if (this.mode.name == 'drop_thing') {
891 this.log_msg('Direction to drop thing to:');
892 this.selectables = ['HERE'].concat(Object.values(this.movement_keys));
893 for (let [i, direction] of this.selectables.entries()) {
894 this.log_msg(i + ': ' + direction);
896 } else if (this.mode.name == 'command_thing') {
897 server.send(['TASK:COMMAND', 'HELP']);
898 } else if (this.mode.name == 'control_pw_pw') {
899 this.log_msg('@ enter protection password for "' + this.tile_control_char + '":');
900 } else if (this.mode.name == 'control_tile_draw') {
901 this.log_msg('@ can draw protection character "' + this.tile_control_char + '", turn drawing on/off with [' + this.keys.toggle_tile_draw + '], finish with [' + this.keys.switch_to_admin_enter + '].')
905 offset_links: function(offset, links) {
906 for (let y in links) {
907 let real_y = offset[0] + parseInt(y);
908 if (!this.links[real_y]) {
909 this.links[real_y] = [];
911 for (let link of links[y]) {
912 const offset_link = [link[0] + offset[1], link[1] + offset[1], link[2]];
913 this.links[real_y].push(offset_link);
917 restore_input_values: function() {
918 if (this.mode.name == 'annotate' && explorer.position in explorer.annotations) {
919 let info = explorer.annotations[explorer.position];
920 if (info != "(none)") {
921 this.inputEl.value = info;
923 } else if (this.mode.name == 'portal' && explorer.position in game.portals) {
924 let portal = game.portals[explorer.position]
925 this.inputEl.value = portal;
926 } else if (this.mode.name == 'password') {
927 this.inputEl.value = this.password;
928 } else if (this.mode.name == 'name_thing') {
929 let t = game.get_thing(this.selected_thing_id);
931 this.inputEl.value = t.name_;
933 } else if (this.mode.name == 'admin_thing_protect') {
934 let t = game.get_thing(this.selected_thing_id);
935 if (t && t.protection) {
936 this.inputEl.value = t.protection;
940 recalc_input_lines: function() {
941 if (this.mode.has_input_prompt) {
943 [this.input_lines, _] = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
945 this.input_lines = [];
947 this.height_input = this.input_lines.length;
949 msg_into_lines_of_width: function(msg, width) {
950 function push_inner_link(y, end_x) {
951 if (!inner_links[y]) {
954 inner_links[y].push([url_start_x, end_x, url]);
956 const matches = msg.matchAll(/https?:\/\/[^\s]+/g)
959 for (const match of matches) {
960 const url = match[0];
961 const url_start = match.index;
962 const url_end = match.index + match[0].length;
963 link_data[url_start] = url;
964 url_ends.push(url_end);
968 let inner_links = {};
972 for (let i = 0, x = 0, y = 0; i < msg.length; i++, x++) {
973 if (x >= width || msg[i] == "\n") {
975 push_inner_link(y, chunk.length);
977 if (url_ends[0] == i) {
985 if (msg[i] == "\n") {
990 if (msg[i] != "\n") {
993 if (i in link_data) {
997 } else if (url_ends[0] == i) {
999 push_inner_link(y, x);
1005 push_inner_link(lines.length - 1, chunk.length);
1007 return [lines, inner_links];
1009 log_msg: function(msg) {
1011 while (this.log.length > 100) {
1014 this.full_refresh();
1016 pick_selectable: function(task_name) {
1017 const i = parseInt(this.inputEl.value);
1018 if (isNaN(i) || i < 0 || i >= this.selectables.length) {
1019 tui.log_msg('? invalid index, aborted');
1021 server.send(['TASK:' + task_name, tui.selectables[i]]);
1023 this.inputEl.value = "";
1024 this.switch_mode('play');
1026 draw_map: function() {
1027 if (!game.turn_complete && this.map_lines.length == 0) {
1030 if (game.turn_complete) {
1031 let map_lines_split = [];
1033 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
1034 if (j == game.map_size[1]) {
1035 map_lines_split.push(line);
1039 if (this.map_mode == 'protections') {
1040 line.push(game.map_control[i] + ' ');
1042 line.push(game.map[i] + ' ');
1045 map_lines_split.push(line);
1046 if (this.map_mode == 'terrain + annotations') {
1047 for (const coordinate of explorer.info_hints) {
1048 map_lines_split[coordinate[0]][coordinate[1]] = 'A ';
1050 } else if (this.map_mode == 'terrain + things') {
1051 for (const p in game.portals) {
1052 let coordinate = p.split(',')
1053 let original = map_lines_split[coordinate[0]][coordinate[1]];
1054 map_lines_split[coordinate[0]][coordinate[1]] = original[0] + 'P';
1056 let used_positions = [];
1057 function draw_thing(t, used_positions) {
1058 let symbol = game.thing_types[t.type_];
1059 let meta_char = ' ';
1061 meta_char = t.thing_char;
1063 if (used_positions.includes(t.position.toString())) {
1069 map_lines_split[t.position[0]][t.position[1]] = symbol + meta_char;
1070 used_positions.push(t.position.toString());
1072 for (const thing_id in game.things) {
1073 let t = game.things[thing_id];
1074 if (t.type_ != 'Player') {
1075 draw_thing(t, used_positions);
1078 for (const thing_id in game.things) {
1079 let t = game.things[thing_id];
1080 if (t.type_ == 'Player') {
1081 draw_thing(t, used_positions);
1085 let player = game.things[game.player_id];
1086 if (tui.mode.shows_info || tui.mode.name == 'control_tile_draw') {
1087 map_lines_split[explorer.position[0]][explorer.position[1]] = '??';
1088 } else if (tui.map_mode != 'terrain + things') {
1089 map_lines_split[player.position[0]][player.position[1]] = '??';
1092 if (game.map_geometry == 'Square') {
1093 for (let line_split of map_lines_split) {
1094 this.map_lines.push(line_split.join(''));
1096 } else if (game.map_geometry == 'Hex') {
1098 for (let line_split of map_lines_split) {
1099 this.map_lines.push(' '.repeat(indent) + line_split.join(''));
1107 let window_center = [terminal.rows / 2, this.window_width / 2];
1108 let center_position = [player.position[0], player.position[1]];
1109 if (tui.mode.shows_info || tui.mode.name == 'control_tile_draw') {
1110 center_position = [explorer.position[0], explorer.position[1]];
1112 center_position[1] = center_position[1] * 2;
1113 this.offset = [center_position[0] - window_center[0],
1114 center_position[1] - window_center[1]]
1115 if (game.map_geometry == 'Hex' && this.offset[0] % 2) {
1116 this.offset[1] += 1;
1119 let term_y = Math.max(0, -this.offset[0]);
1120 let term_x = Math.max(0, -this.offset[1]);
1121 let map_y = Math.max(0, this.offset[0]);
1122 let map_x = Math.max(0, this.offset[1]);
1123 for (; term_y < terminal.rows && map_y < this.map_lines.length; term_y++, map_y++) {
1124 let to_draw = this.map_lines[map_y].slice(map_x, this.window_width + this.offset[1]);
1125 terminal.write(term_y, term_x, to_draw);
1128 draw_mode_line: function() {
1129 let help = 'hit [' + this.keys.help + '] for help';
1130 if (this.mode.has_input_prompt) {
1131 help = 'enter /help for help';
1133 terminal.write(0, this.window_width, 'MODE: ' + this.mode.short_desc + ' – ' + help);
1135 draw_turn_line: function(n) {
1136 if (game.turn_complete) {
1137 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
1140 draw_history: function() {
1141 let log_display_lines = [];
1143 let y_offset_in_log = 0;
1144 for (let line of this.log) {
1145 let [new_lines, link_data] = this.msg_into_lines_of_width(line,
1147 log_display_lines = log_display_lines.concat(new_lines);
1148 for (const y in link_data) {
1149 const rel_y = y_offset_in_log + parseInt(y);
1150 log_links[rel_y] = [];
1151 for (let link of link_data[y]) {
1152 log_links[rel_y].push(link);
1155 y_offset_in_log += new_lines.length;
1157 let i = log_display_lines.length - 1;
1158 for (let y = terminal.rows - 1 - this.height_input;
1159 y >= this.height_header && i >= 0;
1161 terminal.write(y, this.window_width, log_display_lines[i]);
1163 for (const key of Object.keys(log_links)) {
1164 if (parseInt(key) <= i) {
1165 delete log_links[key];
1168 let offset = [terminal.rows - this.height_input - log_display_lines.length,
1170 this.offset_links(offset, log_links);
1172 draw_info: function() {
1173 const info = "MAP VIEW: " + tui.map_mode + "\n" + explorer.get_info();
1174 let [lines, link_data] = this.msg_into_lines_of_width(info, this.window_width);
1175 let offset = [this.height_header, this.window_width];
1176 for (let y = offset[0], i = 0; y < terminal.rows && i < lines.length; y++, i++) {
1177 terminal.write(y, offset[1], lines[i]);
1179 this.offset_links(offset, link_data);
1181 draw_input: function() {
1182 if (this.mode.has_input_prompt) {
1183 for (let y = terminal.rows - this.height_input, i = 0; i < this.input_lines.length; y++, i++) {
1184 terminal.write(y, this.window_width, this.input_lines[i]);
1188 draw_help: function() {
1189 let movement_keys_desc = '';
1190 if (!this.mode.is_intro) {
1191 movement_keys_desc = Object.keys(this.movement_keys).join(',');
1193 let content = this.mode.short_desc + " help\n\n" + this.mode.help_intro + "\n\n";
1194 if (this.mode.available_actions.length > 0) {
1195 content += "Available actions:\n";
1196 for (let action of this.mode.available_actions) {
1197 if (Object.keys(this.action_tasks).includes(action)) {
1198 if (!this.task_action_on(action)) {
1202 if (action == 'move_explorer') {
1205 if (action == 'move') {
1206 content += "[" + movement_keys_desc + "] – move\n"
1208 content += "[" + this.keys[action] + "] – " + key_descriptions[action] + "\n";
1213 content += this.mode.list_available_modes();
1215 if (!this.mode.has_input_prompt) {
1216 start_x = this.window_width
1218 terminal.drawBox(0, start_x, terminal.rows, this.window_width);
1219 let [lines, _] = this.msg_into_lines_of_width(content, this.window_width);
1220 for (let y = 0, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
1221 terminal.write(y, start_x, lines[i]);
1224 toggle_tile_draw: function() {
1225 if (tui.tile_draw) {
1226 tui.tile_draw = false;
1228 tui.tile_draw = true;
1231 toggle_map_mode: function() {
1232 if (tui.map_mode == 'terrain only') {
1233 tui.map_mode = 'terrain + annotations';
1234 } else if (tui.map_mode == 'terrain + annotations') {
1235 tui.map_mode = 'terrain + things';
1236 } else if (tui.map_mode == 'terrain + things') {
1237 tui.map_mode = 'protections';
1238 } else if (tui.map_mode == 'protections') {
1239 tui.map_mode = 'terrain only';
1242 full_refresh: function() {
1244 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
1245 this.recalc_input_lines();
1246 if (this.mode.is_intro) {
1247 this.draw_history();
1251 this.draw_turn_line();
1252 this.draw_mode_line();
1253 if (this.mode.shows_info) {
1256 this.draw_history();
1260 if (this.show_help) {
1272 this.map_control = "";
1273 this.map_size = [0,0];
1274 this.player_id = -1;
1278 get_thing: function(id_, create_if_not_found=false) {
1279 if (id_ in game.things) {
1280 return game.things[id_];
1281 } else if (create_if_not_found) {
1282 let t = new Thing([0,0]);
1283 game.things[id_] = t;
1287 move: function(start_position, direction) {
1288 let target = [start_position[0], start_position[1]];
1289 if (direction == 'LEFT') {
1291 } else if (direction == 'RIGHT') {
1293 } else if (game.map_geometry == 'Square') {
1294 if (direction == 'UP') {
1296 } else if (direction == 'DOWN') {
1299 } else if (game.map_geometry == 'Hex') {
1300 let start_indented = start_position[0] % 2;
1301 if (direction == 'UPLEFT') {
1303 if (!start_indented) {
1306 } else if (direction == 'UPRIGHT') {
1308 if (start_indented) {
1311 } else if (direction == 'DOWNLEFT') {
1313 if (!start_indented) {
1316 } else if (direction == 'DOWNRIGHT') {
1318 if (start_indented) {
1323 if (target[0] < 0 || target[1] < 0 ||
1324 target[0] >= this.map_size[0] || target[1] >= this.map_size[1]) {
1329 teleport: function() {
1330 let player = this.get_thing(game.player_id);
1331 if (player.position in this.portals) {
1332 server.reconnect_to(this.portals[player.position]);
1334 terminal.blink_screen();
1335 tui.log_msg('? not standing on portal')
1343 server.init(websocket_location);
1349 move: function(direction) {
1350 let target = game.move(this.position, direction);
1352 this.position = target
1353 this.info_cached = false;
1354 if (tui.tile_draw) {
1355 this.send_tile_control_command();
1358 terminal.blink_screen();
1361 update_annotations: function(yx, str) {
1362 this.annotations[yx] = str;
1363 if (tui.mode.name == 'study') {
1367 empty_annotations: function() {
1368 this.annotations = {};
1369 if (tui.mode.name == 'study') {
1373 get_info: function() {
1374 if (this.info_cached) {
1375 return this.info_cached;
1377 let info_to_cache = '';
1378 let position_i = this.position[0] * game.map_size[1] + this.position[1];
1379 if (game.fov[position_i] != '.') {
1380 info_to_cache += 'outside field of view';
1382 for (let t_id in game.things) {
1383 let t = game.things[t_id];
1384 if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
1385 info_to_cache += "THING: " + this.get_thing_info(t);
1386 let protection = t.protection;
1387 if (protection == '.') {
1388 protection = 'none';
1390 info_to_cache += " / protection: " + protection + "\n";
1392 info_to_cache += t.hat.slice(0, 6) + '\n';
1393 info_to_cache += t.hat.slice(6, 12) + '\n';
1394 info_to_cache += t.hat.slice(12, 18) + '\n';
1397 info_to_cache += t.face.slice(0, 6) + '\n';
1398 info_to_cache += t.face.slice(6, 12) + '\n';
1399 info_to_cache += t.face.slice(12, 18) + '\n';
1403 let terrain_char = game.map[position_i]
1404 let terrain_desc = '?'
1405 if (game.terrains[terrain_char]) {
1406 terrain_desc = game.terrains[terrain_char];
1408 info_to_cache += 'TERRAIN: "' + terrain_char + '" / ' + terrain_desc + "\n";
1409 let protection = game.map_control[position_i];
1410 if (protection == '.') {
1411 protection = 'unprotected';
1413 info_to_cache += 'PROTECTION: ' + protection + '\n';
1414 if (this.position in game.portals) {
1415 info_to_cache += "PORTAL: " + game.portals[this.position] + "\n";
1417 if (this.position in this.annotations) {
1418 info_to_cache += "ANNOTATION: " + this.annotations[this.position];
1421 this.info_cached = info_to_cache;
1422 return this.info_cached;
1424 get_thing_info: function(t) {
1425 const symbol = game.thing_types[t.type_];
1426 let info = t.type_ + " / " + symbol;
1428 info += t.thing_char;
1431 info += " (" + t.name_ + ")";
1434 info += " / installed";
1438 annotate: function(msg) {
1439 if (msg.length == 0) {
1440 msg = " "; // triggers annotation deletion
1442 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg, tui.password]);
1444 set_portal: function(msg) {
1445 if (msg.length == 0) {
1446 msg = " "; // triggers portal deletion
1448 server.send(["PORTAL", unparser.to_yx(explorer.position), msg, tui.password]);
1450 send_tile_control_command: function() {
1451 server.send(["SET_TILE_CONTROL", unparser.to_yx(this.position), tui.tile_control_char]);
1455 tui.inputEl.addEventListener('input', (event) => {
1456 if (tui.mode.has_input_prompt) {
1457 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
1458 if (tui.inputEl.value.length > max_length) {
1459 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
1461 } else if (tui.mode.name == 'write' && tui.inputEl.value.length > 0) {
1462 server.send(["TASK:WRITE", tui.inputEl.value[0], tui.password]);
1463 tui.switch_mode('edit');
1467 document.onclick = function() {
1468 tui.show_help = false;
1470 tui.inputEl.addEventListener('keydown', (event) => {
1471 tui.show_help = false;
1472 if (event.key == 'Enter') {
1473 event.preventDefault();
1475 if ((!tui.mode.is_intro && event.key == 'Escape')
1476 || (tui.mode.has_input_prompt && event.key == 'Enter'
1477 && tui.inputEl.value.length == 0
1478 && ['chat', 'command_thing', 'take_thing', 'drop_thing',
1479 'admin_enter'].includes(tui.mode.name))) {
1480 if (!['chat', 'play', 'study', 'edit'].includes(tui.mode.name)) {
1481 tui.log_msg('@ aborted');
1483 tui.switch_mode('play');
1484 } else if (tui.mode.has_input_prompt && event.key == 'Enter' && tui.inputEl.value == '/help') {
1485 tui.show_help = true;
1486 tui.inputEl.value = "";
1487 tui.restore_input_values();
1488 } else if (!tui.mode.has_input_prompt && event.key == tui.keys.help
1489 && !tui.mode.is_single_char_entry) {
1490 tui.show_help = true;
1491 } else if (tui.mode.name == 'login' && event.key == 'Enter') {
1492 tui.login_name = tui.inputEl.value;
1493 server.send(['LOGIN', tui.inputEl.value]);
1494 tui.inputEl.value = "";
1495 } else if (tui.mode.name == 'enter_face' && event.key == 'Enter') {
1496 if (tui.inputEl.value.length != 18) {
1497 tui.log_msg('? wrong input length, aborting');
1499 server.send(['PLAYER_FACE', tui.inputEl.value]);
1501 tui.inputEl.value = "";
1502 tui.switch_mode('edit');
1503 } else if (tui.mode.name == 'command_thing' && event.key == 'Enter') {
1504 server.send(['TASK:COMMAND', tui.inputEl.value]);
1505 tui.inputEl.value = "";
1506 } else if (tui.mode.name == 'take_thing' && event.key == 'Enter') {
1507 tui.pick_selectable('PICK_UP');
1508 } else if (tui.mode.name == 'drop_thing' && event.key == 'Enter') {
1509 tui.pick_selectable('DROP');
1510 } else if (tui.mode.name == 'control_pw_pw' && event.key == 'Enter') {
1511 if (tui.inputEl.value.length == 0) {
1512 tui.log_msg('@ aborted');
1514 server.send(['SET_MAP_CONTROL_PASSWORD',
1515 tui.tile_control_char, tui.inputEl.value]);
1516 tui.log_msg('@ sent new password for protection character "' + tui.tile_control_char + '".');
1518 tui.switch_mode('admin');
1519 } else if (tui.mode.name == 'portal' && event.key == 'Enter') {
1520 explorer.set_portal(tui.inputEl.value);
1521 tui.switch_mode('edit');
1522 } else if (tui.mode.name == 'name_thing' && event.key == 'Enter') {
1523 if (tui.inputEl.value.length == 0) {
1524 tui.inputEl.value = " ";
1526 server.send(["THING_NAME", tui.selected_thing_id, tui.inputEl.value,
1528 tui.switch_mode('edit');
1529 } else if (tui.mode.name == 'annotate' && event.key == 'Enter') {
1530 explorer.annotate(tui.inputEl.value);
1531 tui.switch_mode('edit');
1532 } else if (tui.mode.name == 'password' && event.key == 'Enter') {
1533 if (tui.inputEl.value.length == 0) {
1534 tui.inputEl.value = " ";
1536 tui.password = tui.inputEl.value
1537 tui.switch_mode('edit');
1538 } else if (tui.mode.name == 'admin_enter' && event.key == 'Enter') {
1539 server.send(['BECOME_ADMIN', tui.inputEl.value]);
1540 tui.switch_mode('play');
1541 } else if (tui.mode.name == 'control_pw_type' && event.key == 'Enter') {
1542 if (tui.inputEl.value.length != 1) {
1543 tui.log_msg('@ entered non-single-char, therefore aborted');
1544 tui.switch_mode('admin');
1546 tui.tile_control_char = tui.inputEl.value[0];
1547 tui.switch_mode('control_pw_pw');
1549 } else if (tui.mode.name == 'control_tile_type' && event.key == 'Enter') {
1550 if (tui.inputEl.value.length != 1) {
1551 tui.log_msg('@ entered non-single-char, therefore aborted');
1552 tui.switch_mode('admin');
1554 tui.tile_control_char = tui.inputEl.value[0];
1555 tui.switch_mode('control_tile_draw');
1557 } else if (tui.mode.name == 'admin_thing_protect' && event.key == 'Enter') {
1558 if (tui.inputEl.value.length != 1) {
1559 tui.log_msg('@ entered non-single-char, therefore aborted');
1561 server.send(['THING_PROTECTION', tui.selected_thing_id, tui.inputEl.value])
1562 tui.log_msg('@ sent new protection character for thing');
1564 tui.switch_mode('admin');
1565 } else if (tui.mode.name == 'chat' && event.key == 'Enter') {
1566 let tokens = parser.tokenize(tui.inputEl.value);
1567 if (tokens.length > 0 && tokens[0].length > 0) {
1568 if (tui.inputEl.value[0][0] == '/') {
1569 if (tokens[0].slice(1) == 'nick') {
1570 if (tokens.length > 1) {
1571 server.send(['NICK', tokens[1]]);
1573 tui.log_msg('? need new name');
1576 tui.log_msg('? unknown command');
1579 server.send(['ALL', tui.inputEl.value]);
1581 } else if (tui.inputEl.valuelength > 0) {
1582 server.send(['ALL', tui.inputEl.value]);
1584 tui.inputEl.value = "";
1585 } else if (tui.mode.name == 'play') {
1586 if (tui.mode.mode_switch_on_key(event)) {
1588 } else if (event.key === tui.keys.consume && tui.task_action_on('consume')) {
1589 server.send(["TASK:INTOXICATE"]);
1590 } else if (event.key === tui.keys.door && tui.task_action_on('door')) {
1591 server.send(["TASK:DOOR"]);
1592 } else if (event.key === tui.keys.install && tui.task_action_on('install')) {
1593 server.send(["TASK:INSTALL"]);
1594 } else if (event.key === tui.keys.wear && tui.task_action_on('wear')) {
1595 server.send(["TASK:WEAR"]);
1596 } else if (event.key === tui.keys.spin && tui.task_action_on('spin')) {
1597 server.send(["TASK:SPIN"]);
1598 } else if (event.key in tui.movement_keys && tui.task_action_on('move')) {
1599 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
1600 } else if (event.key === tui.keys.teleport) {
1603 } else if (tui.mode.name == 'study') {
1604 if (tui.mode.mode_switch_on_key(event)) {
1606 } else if (event.key in tui.movement_keys) {
1607 explorer.move(tui.movement_keys[event.key]);
1608 } else if (event.key == tui.keys.toggle_map_mode) {
1609 tui.toggle_map_mode();
1611 } else if (tui.mode.name == 'control_tile_draw') {
1612 if (tui.mode.mode_switch_on_key(event)) {
1614 } else if (event.key in tui.movement_keys) {
1615 explorer.move(tui.movement_keys[event.key]);
1616 } else if (event.key === tui.keys.toggle_tile_draw) {
1617 tui.toggle_tile_draw();
1619 } else if (tui.mode.name == 'admin') {
1620 if (tui.mode.mode_switch_on_key(event)) {
1622 } else if (event.key in tui.movement_keys && tui.task_action_on('move')) {
1623 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
1625 } else if (tui.mode.name == 'edit') {
1626 if (tui.mode.mode_switch_on_key(event)) {
1628 } else if (event.key in tui.movement_keys && tui.task_action_on('move')) {
1629 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
1630 } else if (event.key === tui.keys.flatten && tui.task_action_on('flatten')) {
1631 server.send(["TASK:FLATTEN_SURROUNDINGS", tui.password]);
1632 } else if (event.key == tui.keys.toggle_map_mode) {
1633 tui.toggle_map_mode();
1639 rows_selector.addEventListener('input', function() {
1640 if (rows_selector.value % 4 != 0 || rows_selector.value < 24) {
1643 window.localStorage.setItem(rows_selector.id, rows_selector.value);
1644 terminal.initialize();
1647 cols_selector.addEventListener('input', function() {
1648 if (cols_selector.value % 4 != 0 || cols_selector.value < 80) {
1651 window.localStorage.setItem(cols_selector.id, cols_selector.value);
1652 terminal.initialize();
1653 tui.window_width = terminal.cols / 2,
1656 for (let key_selector of key_selectors) {
1657 key_selector.addEventListener('input', function() {
1658 window.localStorage.setItem(key_selector.id, key_selector.value);
1662 window.setInterval(function() {
1663 if (server.connected) {
1664 server.send(['PING']);
1666 server.reconnect_to(server.url);
1667 tui.log_msg('@ attempting reconnect …')
1670 window.setInterval(function() {
1672 let span_decoration = "none";
1673 if (document.activeElement == tui.inputEl) {
1674 val = "on (click outside terminal to change)";
1676 val = "off (click into terminal to change)";
1677 span_decoration = "line-through";
1679 document.getElementById("keyboard_control").textContent = val;
1680 for (const span of document.querySelectorAll('.keyboard_controlled')) {
1681 span.style.textDecoration = span_decoration;
1684 document.getElementById("terminal").onclick = function() {
1685 tui.inputEl.focus();
1687 document.getElementById("help").onclick = function() {
1688 tui.show_help = true;
1691 for (const switchEl of document.querySelectorAll('[id^="switch_to_"]')) {
1692 const mode = switchEl.id.slice("switch_to_".length);
1693 switchEl.onclick = function() {
1694 tui.switch_mode(mode);
1698 document.getElementById("toggle_tile_draw").onclick = function() {
1699 tui.toggle_tile_draw();
1701 document.getElementById("toggle_map_mode").onclick = function() {
1702 tui.toggle_map_mode();
1705 document.getElementById("flatten").onclick = function() {
1706 server.send(['TASK:FLATTEN_SURROUNDINGS', tui.password]);
1708 document.getElementById("door").onclick = function() {
1709 server.send(['TASK:DOOR']);
1711 document.getElementById("consume").onclick = function() {
1712 server.send(['TASK:INTOXICATE']);
1714 document.getElementById("install").onclick = function() {
1715 server.send(['TASK:INSTALL']);
1717 document.getElementById("wear").onclick = function() {
1718 server.send(['TASK:WEAR']);
1720 document.getElementById("spin").onclick = function() {
1721 server.send(['TASK:SPIN']);
1723 document.getElementById("teleport").onclick = function() {
1726 for (const move_button of document.querySelectorAll('[id*="_move_"]')) {
1727 if (move_button.id.startsWith('key_')) { // not a move button
1730 let direction = move_button.id.split('_')[2].toUpperCase();
1731 move_button.onclick = function() {
1732 if (tui.mode.available_actions.includes("move")) {
1733 server.send(['TASK:MOVE', direction]);
1734 } else if (tui.mode.available_actions.includes("move_explorer")) {
1735 explorer.move(direction);