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 mouse players</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="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>
64 <td><button id="switch_to_edit"></button></td>
66 <button id="switch_to_write"></button>
67 <button id="flatten"></button>
68 <button id="switch_to_annotate"></button>
69 <button id="switch_to_portal"></button>
70 <button id="switch_to_name_thing"></button>
71 <button id="switch_to_password"></button>
72 <button id="switch_to_enter_face"></button>
76 <td><button id="switch_to_admin_enter"></button></td>
78 <button id="switch_to_control_pw_type"></button>
79 <button id="switch_to_control_tile_type"></button>
80 <button id="switch_to_admin_thing_protect"></button>
81 <button id="toggle_tile_draw"></button>
86 <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 />
88 <li>move up (square grid): <input id="key_square_move_up" type="text" value="w" /> (hint: ArrowUp)
89 <li>move left (square grid): <input id="key_square_move_left" type="text" value="a" /> (hint: ArrowLeft)
90 <li>move down (square grid): <input id="key_square_move_down" type="text" value="s" /> (hint: ArrowDown)
91 <li>move right (square grid): <input id="key_square_move_right" type="text" value="d" /> (hint: ArrowRight)
92 <li>move up-left (hex grid): <input id="key_hex_move_upleft" type="text" value="w" />
93 <li>move up-right (hex grid): <input id="key_hex_move_upright" type="text" value="e" />
94 <li>move right (hex grid): <input id="key_hex_move_right" type="text" value="d" />
95 <li>move down-right (hex grid): <input id="key_hex_move_downright" type="text" value="x" />
96 <li>move down-left (hex grid): <input id="key_hex_move_downleft" type="text" value="y" />
97 <li>move left (hex grid): <input id="key_hex_move_left" type="text" value="a" />
98 <li>help: <input id="key_help" type="text" value="h" />
99 <li>flatten surroundings: <input id="key_flatten" type="text" value="F" />
100 <li>teleport: <input id="key_teleport" type="text" value="p" />
101 <li>drop thing: <input id="key_drop_thing" type="text" value="u" />
102 <li>open/close: <input id="key_door" type="text" value="D" />
103 <li>consume: <input id="key_consume" type="text" value="C" />
104 <li>install: <input id="key_install" type="text" value="I" />
105 <li><input id="key_switch_to_enter_face" type="text" value="f" />
106 <li><input id="key_switch_to_take_thing" type="text" value="z" />
107 <li><input id="key_switch_to_chat" type="text" value="t" />
108 <li><input id="key_switch_to_play" type="text" value="p" />
109 <li><input id="key_switch_to_study" type="text" value="?" />
110 <li><input id="key_switch_to_edit" type="text" value="E" />
111 <li><input id="key_switch_to_write" type="text" value="m" />
112 <li><input id="key_switch_to_name_thing" type="text" value="N" />
113 <li><input id="key_switch_to_command_thing" type="text" value="O" />
114 <li><input id="key_switch_to_password" type="text" value="P" />
115 <li><input id="key_switch_to_admin_enter" type="text" value="A" />
116 <li><input id="key_switch_to_control_pw_type" type="text" value="C" />
117 <li><input id="key_switch_to_control_tile_type" type="text" value="Q" />
118 <li><input id="key_switch_to_admin_thing_protect" type="text" value="T" />
119 <li><input id="key_switch_to_annotate" type="text" value="M" />
120 <li><input id="key_switch_to_portal" type="text" value="T" />
121 <li>toggle map view: <input id="key_toggle_map_mode" type="text" value="L" />
122 <li>toggle protection character drawing: <input id="key_toggle_tile_draw" type="text" value="m" />
127 let websocket_location = "wss://plomlompom.com/rogue_chat/";
128 //let websocket_location = "ws://localhost:8001/";
134 'long': 'This mode allows you to interact with the map in various ways.'
139 '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.'},
141 'short': 'world edit',
143 '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.'
146 'short': 'name thing',
148 'long': 'Give name to/change name of thing here.'
151 'short': 'command thing',
153 'long': 'Enter a command to the thing you carry. Enter nothing to return to play mode.'
156 'short': 'take thing',
157 'intro': 'Pick up a thing in reach by entering its index number. Enter nothing to abort.',
158 'long': 'You see a list of things which you could pick up. Enter the target thing\'s index, or, to leave, nothing.'
160 'admin_thing_protect': {
161 'short': 'change thing protection',
162 'intro': '@ enter thing protection character:',
163 'long': 'Change protection character for thing here.'
166 'short': 'enter your face',
167 'intro': '@ enter face line (enter nothing to abort):',
168 'long': 'Draw your face as ASCII art. The string you enter must be 9 characters long, and will be divided on display into three lines of three characters each, from top to bottom..'
171 'short': 'change terrain',
173 '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.'
176 'short': 'change protection character password',
177 'intro': '@ enter protection character for which you want to change the password:',
178 '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.'
181 'short': 'change protection character password',
183 '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.'
185 'control_tile_type': {
186 'short': 'change tiles protection',
187 'intro': '@ enter protection character which you want to draw:',
188 '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.'
190 'control_tile_draw': {
191 'short': 'change tiles protection',
193 '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.'
196 'short': 'annotate tile',
198 '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.'
201 'short': 'edit portal',
203 '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.'
208 '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'
213 'long': 'Enter your player name.'
215 'waiting_for_server': {
216 'short': 'waiting for server response',
217 'intro': '@ waiting for server …',
218 'long': 'Waiting for a server response.'
221 'short': 'waiting for server response',
223 'long': 'Waiting for a server response.'
226 'short': 'set world edit password',
228 '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.'
231 'short': 'become admin',
232 'intro': '@ enter admin password:',
233 'long': 'This mode allows you to become admin if you know an admin password.'
238 'long': 'This mode allows you access to actions limited to administrators.'
241 let key_descriptions = {
243 'flatten': 'flatten surroundings',
244 'teleport': 'teleport',
245 'drop_thing': 'drop thing',
246 'door': 'open/close',
247 'consume': 'consume',
248 'install': '(un-)install',
249 'toggle_map_mode': 'toggle map view',
250 'toggle_tile_draw': 'toggle protection character drawing',
251 'hex_move_upleft': 'up-left',
252 'hex_move_upright': 'up-right',
253 'hex_move_right': 'right',
254 'hex_move_left': 'left',
255 'hex_move_downleft': 'down-left',
256 'hex_move_downright': 'down-right',
257 'square_move_up': 'up',
258 'square_move_left': 'left',
259 'square_move_down': 'down',
260 'square_move_right': 'right',
262 for (const mode_name of Object.keys(mode_helps)) {
263 key_descriptions['switch_to_' + mode_name] = mode_helps[mode_name].short;
266 let rows_selector = document.getElementById("n_rows");
267 let cols_selector = document.getElementById("n_cols");
268 let key_selectors = document.querySelectorAll('[id^="key_"]');
270 for (const key_switch_selector of document.querySelectorAll('[id^="key_switch_to_"]')) {
271 const action = key_switch_selector.id.slice("key_switch_to_".length);
272 key_switch_selector.parentNode.prepend(mode_helps[action].short + ': ');
275 function restore_selector_value(selector) {
276 let stored_selection = window.localStorage.getItem(selector.id);
277 if (stored_selection) {
278 selector.value = stored_selection;
281 restore_selector_value(rows_selector);
282 restore_selector_value(cols_selector);
283 for (let key_selector of key_selectors) {
284 restore_selector_value(key_selector);
287 function escapeHTML(str) {
289 replace(/&/g, '&').
290 replace(/</g, '<').
291 replace(/>/g, '>').
292 replace(/'/g, ''').
293 replace(/"/g, '"');
297 initialize: function() {
298 this.rows = rows_selector.value;
299 this.cols = cols_selector.value;
300 this.pre_el = document.getElementById("terminal");
301 this.set_default_colors();
305 for (let y = 0, x = 0; y <= this.rows; x++) {
306 if (x == this.cols) {
309 this.content.push(line);
311 if (y == this.rows) {
318 apply_colors: function() {
319 this.pre_el.style.color = this.foreground;
320 this.pre_el.style.backgroundColor = this.background;
322 set_default_colors: function() {
323 this.foreground = 'white';
324 this.background = 'black';
327 set_random_colors: function() {
328 function rand(offset) {
329 return Math.floor(offset + Math.random() * 96).toString(16).padStart(2, '0');
331 this.foreground = '#' + rand(159) + rand(159) + rand(159);
332 this.background = '#' + rand(0) + rand(0) + rand(0);
335 blink_screen: function() {
336 this.pre_el.style.color = this.background;
337 this.pre_el.style.backgroundColor = this.foreground;
339 this.pre_el.style.color = this.foreground;
340 this.pre_el.style.backgroundColor = this.background;
343 refresh: function() {
344 let pre_content = '';
345 for (let y = 0; y < this.rows; y++) {
346 let line = this.content[y].join('');
348 if (y in tui.links) {
350 for (let span of tui.links[y]) {
351 chunks.push(escapeHTML(line.slice(start_x, span[0])));
352 chunks.push('<a target="_blank" href="');
353 chunks.push(escapeHTML(span[2]));
355 chunks.push(escapeHTML(line.slice(span[0], span[1])));
359 chunks.push(escapeHTML(line.slice(start_x)));
361 chunks = [escapeHTML(line)];
363 for (const chunk of chunks) {
364 pre_content += chunk;
368 this.pre_el.innerHTML = pre_content;
370 write: function(start_y, start_x, msg) {
371 for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
372 this.content[start_y][x] = msg[i];
375 drawBox: function(start_y, start_x, height, width) {
376 let end_y = start_y + height;
377 let end_x = start_x + width;
378 for (let y = start_y, x = start_x; y < this.rows; x++) {
386 this.content[y][x] = ' ';
390 terminal.initialize();
393 tokenize: function(str) {
398 for (let i = 0; i < str.length; i++) {
404 } else if (c == '\\') {
406 } else if (c == '"') {
411 } else if (c == '"') {
413 } else if (c === ' ') {
414 if (token.length > 0) {
422 if (token.length > 0) {
427 parse_yx: function(position_string) {
428 let coordinate_strings = position_string.split(',')
429 let position = [0, 0];
430 position[0] = parseInt(coordinate_strings[0].slice(2));
431 position[1] = parseInt(coordinate_strings[1].slice(2));
443 init: function(url) {
445 this.websocket = new WebSocket(this.url);
446 this.websocket.onopen = function(event) {
447 server.connected = true;
448 game.thing_types = {};
450 server.send(['TASKS']);
451 server.send(['TERRAINS']);
452 server.send(['THING_TYPES']);
453 tui.log_msg("@ server connected! :)");
454 tui.switch_mode('login');
456 this.websocket.onclose = function(event) {
457 server.connected = false;
458 tui.switch_mode('waiting_for_server');
459 tui.log_msg("@ server disconnected :(");
461 this.websocket.onmessage = this.handle_event;
463 reconnect_to: function(url) {
464 this.websocket.close();
467 send: function(tokens) {
468 this.websocket.send(unparser.untokenize(tokens));
470 handle_event: function(event) {
471 let tokens = parser.tokenize(event.data);
472 if (tokens[0] === 'TURN') {
473 game.turn_complete = false;
474 explorer.empty_annotations();
478 game.turn = parseInt(tokens[1]);
479 } else if (tokens[0] === 'THING') {
480 let t = game.get_thing(tokens[4], true);
481 t.position = parser.parse_yx(tokens[1]);
483 t.protection = tokens[3];
484 t.portable = parseInt(tokens[5]);
485 } else if (tokens[0] === 'THING_NAME') {
486 let t = game.get_thing(tokens[1], false);
488 } else if (tokens[0] === 'THING_FACE') {
489 let t = game.get_thing(tokens[1], false);
491 } else if (tokens[0] === 'THING_CHAR') {
492 let t = game.get_thing(tokens[1], false);
493 t.thing_char = tokens[2];
494 } else if (tokens[0] === 'TASKS') {
495 game.tasks = tokens[1].split(',');
496 tui.mode_write.legal = game.tasks.includes('WRITE');
497 tui.mode_command_thing.legal = game.tasks.includes('WRITE');
498 tui.mode_take_thing.legal = game.tasks.includes('PICK_UP');
499 } else if (tokens[0] === 'THING_TYPE') {
500 game.thing_types[tokens[1]] = tokens[2]
501 } else if (tokens[0] === 'THING_CARRYING') {
502 let t = game.get_thing(tokens[1], false);
504 } else if (tokens[0] === 'THING_INSTALLED') {
505 let t = game.get_thing(tokens[1], false);
507 } else if (tokens[0] === 'TERRAIN') {
508 game.terrains[tokens[1]] = tokens[2]
509 } else if (tokens[0] === 'MAP') {
510 game.map_geometry = tokens[1];
512 game.map_size = parser.parse_yx(tokens[2]);
514 } else if (tokens[0] === 'FOV') {
516 } else if (tokens[0] === 'MAP_CONTROL') {
517 game.map_control = tokens[1]
518 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
519 game.turn_complete = true;
520 if (tui.mode.name == 'post_login_wait') {
521 tui.switch_mode('play');
523 explorer.info_cached = false;
525 } else if (tokens[0] === 'CHAT') {
526 tui.log_msg('# ' + tokens[1], 1);
527 } else if (tokens[0] === 'REPLY') {
528 tui.log_msg('#MUSICPLAYER: ' + tokens[1], 1);
529 } else if (tokens[0] === 'PLAYER_ID') {
530 game.player_id = parseInt(tokens[1]);
531 } else if (tokens[0] === 'LOGIN_OK') {
532 this.send(['GET_GAMESTATE']);
533 tui.switch_mode('post_login_wait');
534 } else if (tokens[0] === 'DEFAULT_COLORS') {
535 terminal.set_default_colors();
536 } else if (tokens[0] === 'RANDOM_COLORS') {
537 terminal.set_random_colors();
538 } else if (tokens[0] === 'ADMIN_OK') {
540 tui.log_msg('@ you now have admin rights');
541 tui.switch_mode('admin');
542 } else if (tokens[0] === 'PORTAL') {
543 let position = parser.parse_yx(tokens[1]);
544 game.portals[position] = tokens[2];
545 } else if (tokens[0] === 'ANNOTATION') {
546 let position = parser.parse_yx(tokens[1]);
547 explorer.update_annotations(position, tokens[2]);
549 } else if (tokens[0] === 'UNHANDLED_INPUT') {
550 tui.log_msg('? unknown command');
551 } else if (tokens[0] === 'PLAY_ERROR') {
552 tui.log_msg('? ' + tokens[1]);
553 terminal.blink_screen();
554 } else if (tokens[0] === 'ARGUMENT_ERROR') {
555 tui.log_msg('? syntax error: ' + tokens[1]);
556 } else if (tokens[0] === 'GAME_ERROR') {
557 tui.log_msg('? game error: ' + tokens[1]);
558 } else if (tokens[0] === 'PONG') {
561 tui.log_msg('? unhandled input: ' + event.data);
567 quote: function(str) {
569 for (let i = 0; i < str.length; i++) {
571 if (['"', '\\'].includes(c)) {
577 return quoted.join('');
579 to_yx: function(yx_coordinate) {
580 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
582 untokenize: function(tokens) {
583 let quoted_tokens = [];
584 for (let token of tokens) {
585 quoted_tokens.push(this.quote(token));
587 return quoted_tokens.join(" ");
592 constructor(name, has_input_prompt=false, shows_info=false,
593 is_intro=false, is_single_char_entry=false) {
595 this.short_desc = mode_helps[name].short;
596 this.available_modes = [];
597 this.available_actions = [];
598 this.has_input_prompt = has_input_prompt;
599 this.shows_info= shows_info;
600 this.is_intro = is_intro;
601 this.help_intro = mode_helps[name].long;
602 this.intro_msg = mode_helps[name].intro;
603 this.is_single_char_entry = is_single_char_entry;
606 *iter_available_modes() {
607 for (let mode_name of this.available_modes) {
608 let mode = tui['mode_' + mode_name];
612 let key = tui.keys['switch_to_' + mode.name];
616 list_available_modes() {
618 if (this.available_modes.length > 0) {
619 msg += 'Other modes available from here:\n';
620 for (let [mode, key] of this.iter_available_modes()) {
621 msg += '[' + key + '] – ' + mode.short_desc + '\n';
626 mode_switch_on_key(key_event) {
627 for (let [mode, key] of this.iter_available_modes()) {
628 if (key_event.key == key) {
629 event.preventDefault();
630 tui.switch_mode(mode.name);
642 window_width: terminal.cols / 2,
650 mode_waiting_for_server: new Mode('waiting_for_server',
652 mode_login: new Mode('login', true, false, true),
653 mode_post_login_wait: new Mode('post_login_wait'),
654 mode_chat: new Mode('chat', true),
655 mode_annotate: new Mode('annotate', true, true),
656 mode_play: new Mode('play'),
657 mode_study: new Mode('study', false, true),
658 mode_write: new Mode('write', false, false, false, true),
659 mode_edit: new Mode('edit'),
660 mode_control_pw_type: new Mode('control_pw_type', true),
661 mode_admin_thing_protect: new Mode('admin_thing_protect', true),
662 mode_portal: new Mode('portal', true, true),
663 mode_password: new Mode('password', true),
664 mode_name_thing: new Mode('name_thing', true, true),
665 mode_command_thing: new Mode('command_thing', true),
666 mode_take_thing: new Mode('take_thing', true),
667 mode_enter_face: new Mode('enter_face', true),
668 mode_admin_enter: new Mode('admin_enter', true),
669 mode_admin: new Mode('admin'),
670 mode_control_pw_pw: new Mode('control_pw_pw', true),
671 mode_control_tile_type: new Mode('control_tile_type', true),
672 mode_control_tile_draw: new Mode('control_tile_draw'),
674 'flatten': 'FLATTEN_SURROUNDINGS',
675 'take_thing': 'PICK_UP',
676 'drop_thing': 'DROP',
679 'install': 'INSTALL',
680 'command': 'COMMAND',
681 'consume': 'INTOXICATE',
687 this.mode_play.available_modes = ["chat", "study", "edit", "admin_enter",
688 "command_thing", "take_thing"]
689 this.mode_play.available_actions = ["move", "drop_thing",
690 "teleport", "door", "consume", "install"];
691 this.mode_study.available_modes = ["chat", "play", "admin_enter", "edit"]
692 this.mode_study.available_actions = ["toggle_map_mode", "move_explorer"];
693 this.mode_admin.available_modes = ["admin_thing_protect", "control_pw_type",
694 "control_tile_type", "chat",
695 "study", "play", "edit"]
696 this.mode_admin.available_actions = ["move"];
697 this.mode_control_tile_draw.available_modes = ["admin_enter"]
698 this.mode_control_tile_draw.available_actions = ["toggle_tile_draw"];
699 this.mode_edit.available_modes = ["write", "annotate", "portal", "name_thing",
700 "password", "chat", "study", "play",
701 "admin_enter", "enter_face"]
702 this.mode_edit.available_actions = ["move", "flatten", "toggle_map_mode"]
703 this.inputEl = document.getElementById("input");
704 this.inputEl.focus();
705 this.switch_mode('waiting_for_server');
706 this.recalc_input_lines();
707 this.height_header = this.height_turn_line + this.height_mode_line;
710 init_keys: function() {
711 document.getElementById("move_table").hidden = true;
713 for (let key_selector of key_selectors) {
714 this.keys[key_selector.id.slice(4)] = key_selector.value;
716 this.movement_keys = {};
717 let geometry_prefix = 'undefinedMapGeometry_';
718 if (game.map_geometry) {
719 geometry_prefix = game.map_geometry.toLowerCase() + '_';
721 for (const key_name of Object.keys(key_descriptions)) {
722 if (key_name.startsWith(geometry_prefix)) {
723 let direction = key_name.split('_')[2].toUpperCase();
724 let key = this.keys[key_name];
725 this.movement_keys[key] = direction;
728 for (const move_button of document.querySelectorAll('[id*="_move_"]')) {
729 if (move_button.id.startsWith('key_')) {
732 move_button.hidden = true;
734 for (const move_button of document.querySelectorAll('[id^="' + geometry_prefix + 'move_"]')) {
735 document.getElementById("move_table").hidden = false;
736 move_button.hidden = false;
738 for (let el of document.getElementsByTagName("button")) {
739 let action_desc = key_descriptions[el.id];
740 let action_key = '[' + this.keys[el.id] + ']';
741 el.innerHTML = escapeHTML(action_desc) + '<br /><span class="keyboard_controlled">' + escapeHTML(action_key) + '</span>';
744 task_action_on: function(action) {
745 return game.tasks.includes(this.action_tasks[action]);
747 switch_mode: function(mode_name) {
748 if (this.mode && this.mode.name == 'control_tile_draw') {
749 tui.log_msg('@ finished tile protection drawing.')
751 this.tile_draw = false;
752 if (mode_name == 'admin_enter' && this.is_admin) {
754 } else if (['name_thing', 'admin_thing_protect'].includes(mode_name)) {
755 let player_position = game.things[game.player_id].position;
757 for (let t_id in game.things) {
758 if (t_id == game.player_id) {
761 let t = game.things[t_id];
762 if (player_position[0] == t.position[0] && player_position[1] == t.position[1]) {
768 terminal.blink_screen();
769 this.log_msg('? not standing over thing');
772 this.selected_thing_id = thing_id;
775 this.mode = this['mode_' + mode_name];
776 if (["control_tile_draw", "control_tile_type", "control_pw_type"].includes(this.mode.name)) {
777 this.map_mode = 'protections';
778 } else if (this.mode.name != "edit") {
779 this.map_mode = 'terrain + things';
781 if (this.mode.has_input_prompt || this.mode.is_single_char_entry) {
782 this.inputEl.focus();
784 if (game.player_id in game.things && (this.mode.shows_info || this.mode.name == 'control_tile_draw')) {
785 explorer.position = game.things[game.player_id].position;
787 this.inputEl.value = "";
788 this.restore_input_values();
789 for (let el of document.getElementsByTagName("button")) {
792 document.getElementById("help").disabled = false;
793 for (const action of this.mode.available_actions) {
794 if (["move", "move_explorer"].includes(action)) {
795 for (const move_key of document.querySelectorAll('[id*="_move_"]')) {
796 move_key.disabled = false;
798 } else if (Object.keys(this.action_tasks).includes(action)) {
799 if (this.task_action_on(action)) {
800 document.getElementById(action).disabled = false;
803 document.getElementById(action).disabled = false;
806 for (const mode_name of this.mode.available_modes) {
807 document.getElementById('switch_to_' + mode_name).disabled = false;
809 if (this.mode.intro_msg.length > 0) {
810 this.log_msg(this.mode.intro_msg);
812 if (this.mode.name == 'login') {
813 if (this.login_name) {
814 server.send(['LOGIN', this.login_name]);
816 this.log_msg("? need login name");
818 } else if (this.mode.is_single_char_entry) {
819 this.show_help = true;
820 } else if (this.mode.name == 'take_thing') {
821 this.log_msg("Portable things in reach for pick-up:");
822 const player = game.things[game.player_id];
823 const y = player.position[0]
824 const x = player.position[1]
825 let select_range = [y.toString() + ':' + x.toString(),
826 (y + 0).toString() + ':' + (x - 1).toString(),
827 (y + 0).toString() + ':' + (x + 1).toString(),
828 (y - 1).toString() + ':' + (x).toString(),
829 (y + 1).toString() + ':' + (x).toString()];
830 if (game.map_geometry == 'Hex') {
832 select_range.push((y - 1).toString() + ':' + (x + 1).toString());
833 select_range.push((y + 1).toString() + ':' + (x + 1).toString());
835 select_range.push((y - 1).toString() + ':' + (x - 1).toString());
836 select_range.push((y + 1).toString() + ':' + (x - 1).toString());
839 this.selectables = [];
840 for (const t_id in game.things) {
841 const t = game.things[t_id];
842 if (select_range.includes(t.position[0].toString()
843 + ':' + t.position[1].toString())
845 this.selectables.push([t_id, t]);
848 if (this.selectables.length == 0) {
851 for (let [i, t] of this.selectables.entries()) {
852 this.log_msg(i + ': ' + explorer.get_thing_info(t[1]));
855 } else if (this.mode.name == 'command_thing') {
856 server.send(['TASK:COMMAND', 'HELP']);
857 } else if (this.mode.name == 'control_pw_pw') {
858 this.log_msg('@ enter protection password for "' + this.tile_control_char + '":');
859 } else if (this.mode.name == 'control_tile_draw') {
860 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 + '].')
864 offset_links: function(offset, links) {
865 for (let y in links) {
866 let real_y = offset[0] + parseInt(y);
867 if (!this.links[real_y]) {
868 this.links[real_y] = [];
870 for (let link of links[y]) {
871 const offset_link = [link[0] + offset[1], link[1] + offset[1], link[2]];
872 this.links[real_y].push(offset_link);
876 restore_input_values: function() {
877 if (this.mode.name == 'annotate' && explorer.position in explorer.annotations) {
878 let info = explorer.annotations[explorer.position];
879 if (info != "(none)") {
880 this.inputEl.value = info;
882 } else if (this.mode.name == 'portal' && explorer.position in game.portals) {
883 let portal = game.portals[explorer.position]
884 this.inputEl.value = portal;
885 } else if (this.mode.name == 'password') {
886 this.inputEl.value = this.password;
887 } else if (this.mode.name == 'name_thing') {
888 let t = game.get_thing(this.selected_thing_id);
890 this.inputEl.value = t.name_;
892 } else if (this.mode.name == 'admin_thing_protect') {
893 let t = game.get_thing(this.selected_thing_id);
894 if (t && t.protection) {
895 this.inputEl.value = t.protection;
899 recalc_input_lines: function() {
900 if (this.mode.has_input_prompt) {
902 [this.input_lines, _] = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
904 this.input_lines = [];
906 this.height_input = this.input_lines.length;
908 msg_into_lines_of_width: function(msg, width) {
909 function push_inner_link(y, end_x) {
910 if (!inner_links[y]) {
913 inner_links[y].push([url_start_x, end_x, url]);
915 const matches = msg.matchAll(/https?:\/\/[^\s]+/g)
918 for (const match of matches) {
919 const url = match[0];
920 const url_start = match.index;
921 const url_end = match.index + match[0].length;
922 link_data[url_start] = url;
923 url_ends.push(url_end);
927 let inner_links = {};
931 for (let i = 0, x = 0, y = 0; i < msg.length; i++, x++) {
932 if (x >= width || msg[i] == "\n") {
934 push_inner_link(y, chunk.length);
936 if (url_ends[0] == i) {
944 if (msg[i] == "\n") {
949 if (msg[i] != "\n") {
952 if (i in link_data) {
956 } else if (url_ends[0] == i) {
958 push_inner_link(y, x);
964 push_inner_link(lines.length - 1, chunk.length);
966 return [lines, inner_links];
968 log_msg: function(msg) {
970 while (this.log.length > 100) {
975 draw_map: function() {
976 if (!game.turn_complete && this.map_lines.length == 0) {
979 if (game.turn_complete) {
980 let map_lines_split = [];
982 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
983 if (j == game.map_size[1]) {
984 map_lines_split.push(line);
988 if (this.map_mode == 'protections') {
989 line.push(game.map_control[i] + ' ');
991 line.push(game.map[i] + ' ');
994 map_lines_split.push(line);
995 if (this.map_mode == 'terrain + annotations') {
996 for (const coordinate of explorer.info_hints) {
997 map_lines_split[coordinate[0]][coordinate[1]] = 'A ';
999 } else if (this.map_mode == 'terrain + things') {
1000 for (const p in game.portals) {
1001 let coordinate = p.split(',')
1002 let original = map_lines_split[coordinate[0]][coordinate[1]];
1003 map_lines_split[coordinate[0]][coordinate[1]] = original[0] + 'P';
1005 let used_positions = [];
1006 function draw_thing(t, used_positions) {
1007 let symbol = game.thing_types[t.type_];
1008 let meta_char = ' ';
1010 meta_char = t.thing_char;
1012 if (used_positions.includes(t.position.toString())) {
1018 map_lines_split[t.position[0]][t.position[1]] = symbol + meta_char;
1019 used_positions.push(t.position.toString());
1021 for (const thing_id in game.things) {
1022 let t = game.things[thing_id];
1023 if (t.type_ != 'Player') {
1024 draw_thing(t, used_positions);
1027 for (const thing_id in game.things) {
1028 let t = game.things[thing_id];
1029 if (t.type_ == 'Player') {
1030 draw_thing(t, used_positions);
1034 let player = game.things[game.player_id];
1035 if (tui.mode.shows_info || tui.mode.name == 'control_tile_draw') {
1036 map_lines_split[explorer.position[0]][explorer.position[1]] = '??';
1037 } else if (tui.map_mode != 'terrain + things') {
1038 map_lines_split[player.position[0]][player.position[1]] = '??';
1041 if (game.map_geometry == 'Square') {
1042 for (let line_split of map_lines_split) {
1043 this.map_lines.push(line_split.join(''));
1045 } else if (game.map_geometry == 'Hex') {
1047 for (let line_split of map_lines_split) {
1048 this.map_lines.push(' '.repeat(indent) + line_split.join(''));
1056 let window_center = [terminal.rows / 2, this.window_width / 2];
1057 let center_position = [player.position[0], player.position[1]];
1058 if (tui.mode.shows_info || tui.mode.name == 'control_tile_draw') {
1059 center_position = [explorer.position[0], explorer.position[1]];
1061 center_position[1] = center_position[1] * 2;
1062 this.offset = [center_position[0] - window_center[0],
1063 center_position[1] - window_center[1]]
1064 if (game.map_geometry == 'Hex' && this.offset[0] % 2) {
1065 this.offset[1] += 1;
1068 let term_y = Math.max(0, -this.offset[0]);
1069 let term_x = Math.max(0, -this.offset[1]);
1070 let map_y = Math.max(0, this.offset[0]);
1071 let map_x = Math.max(0, this.offset[1]);
1072 for (; term_y < terminal.rows && map_y < this.map_lines.length; term_y++, map_y++) {
1073 let to_draw = this.map_lines[map_y].slice(map_x, this.window_width + this.offset[1]);
1074 terminal.write(term_y, term_x, to_draw);
1077 draw_mode_line: function() {
1078 let help = 'hit [' + this.keys.help + '] for help';
1079 if (this.mode.has_input_prompt) {
1080 help = 'enter /help for help';
1082 terminal.write(0, this.window_width, 'MODE: ' + this.mode.short_desc + ' – ' + help);
1084 draw_turn_line: function(n) {
1085 if (game.turn_complete) {
1086 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
1089 draw_history: function() {
1090 let log_display_lines = [];
1092 let y_offset_in_log = 0;
1093 for (let line of this.log) {
1094 let [new_lines, link_data] = this.msg_into_lines_of_width(line,
1096 log_display_lines = log_display_lines.concat(new_lines);
1097 for (const y in link_data) {
1098 const rel_y = y_offset_in_log + parseInt(y);
1099 log_links[rel_y] = [];
1100 for (let link of link_data[y]) {
1101 log_links[rel_y].push(link);
1104 y_offset_in_log += new_lines.length;
1106 let i = log_display_lines.length - 1;
1107 for (let y = terminal.rows - 1 - this.height_input;
1108 y >= this.height_header && i >= 0;
1110 terminal.write(y, this.window_width, log_display_lines[i]);
1112 for (const key of Object.keys(log_links)) {
1113 if (parseInt(key) <= i) {
1114 delete log_links[key];
1117 let offset = [terminal.rows - this.height_input - log_display_lines.length,
1119 this.offset_links(offset, log_links);
1121 draw_info: function() {
1122 const info = "MAP VIEW: " + tui.map_mode + "\n" + explorer.get_info();
1123 let [lines, link_data] = this.msg_into_lines_of_width(info, this.window_width);
1124 let offset = [this.height_header, this.window_width];
1125 for (let y = offset[0], i = 0; y < terminal.rows && i < lines.length; y++, i++) {
1126 terminal.write(y, offset[1], lines[i]);
1128 this.offset_links(offset, link_data);
1130 draw_input: function() {
1131 if (this.mode.has_input_prompt) {
1132 for (let y = terminal.rows - this.height_input, i = 0; i < this.input_lines.length; y++, i++) {
1133 terminal.write(y, this.window_width, this.input_lines[i]);
1137 draw_help: function() {
1138 let movement_keys_desc = '';
1139 if (!this.mode.is_intro) {
1140 movement_keys_desc = Object.keys(this.movement_keys).join(',');
1142 let content = this.mode.short_desc + " help\n\n" + this.mode.help_intro + "\n\n";
1143 if (this.mode.available_actions.length > 0) {
1144 content += "Available actions:\n";
1145 for (let action of this.mode.available_actions) {
1146 if (Object.keys(this.action_tasks).includes(action)) {
1147 if (!this.task_action_on(action)) {
1151 if (action == 'move_explorer') {
1154 if (action == 'move') {
1155 content += "[" + movement_keys_desc + "] – move\n"
1157 content += "[" + this.keys[action] + "] – " + key_descriptions[action] + "\n";
1162 content += this.mode.list_available_modes();
1164 if (!this.mode.has_input_prompt) {
1165 start_x = this.window_width
1167 terminal.drawBox(0, start_x, terminal.rows, this.window_width);
1168 let [lines, _] = this.msg_into_lines_of_width(content, this.window_width);
1169 for (let y = 0, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
1170 terminal.write(y, start_x, lines[i]);
1173 toggle_tile_draw: function() {
1174 if (tui.tile_draw) {
1175 tui.tile_draw = false;
1177 tui.tile_draw = true;
1180 toggle_map_mode: function() {
1181 if (tui.map_mode == 'terrain only') {
1182 tui.map_mode = 'terrain + annotations';
1183 } else if (tui.map_mode == 'terrain + annotations') {
1184 tui.map_mode = 'terrain + things';
1185 } else if (tui.map_mode == 'terrain + things') {
1186 tui.map_mode = 'protections';
1187 } else if (tui.map_mode == 'protections') {
1188 tui.map_mode = 'terrain only';
1191 full_refresh: function() {
1193 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
1194 this.recalc_input_lines();
1195 if (this.mode.is_intro) {
1196 this.draw_history();
1200 this.draw_turn_line();
1201 this.draw_mode_line();
1202 if (this.mode.shows_info) {
1205 this.draw_history();
1209 if (this.show_help) {
1221 this.map_control = "";
1222 this.map_size = [0,0];
1223 this.player_id = -1;
1227 get_thing: function(id_, create_if_not_found=false) {
1228 if (id_ in game.things) {
1229 return game.things[id_];
1230 } else if (create_if_not_found) {
1231 let t = new Thing([0,0]);
1232 game.things[id_] = t;
1236 move: function(start_position, direction) {
1237 let target = [start_position[0], start_position[1]];
1238 if (direction == 'LEFT') {
1240 } else if (direction == 'RIGHT') {
1242 } else if (game.map_geometry == 'Square') {
1243 if (direction == 'UP') {
1245 } else if (direction == 'DOWN') {
1248 } else if (game.map_geometry == 'Hex') {
1249 let start_indented = start_position[0] % 2;
1250 if (direction == 'UPLEFT') {
1252 if (!start_indented) {
1255 } else if (direction == 'UPRIGHT') {
1257 if (start_indented) {
1260 } else if (direction == 'DOWNLEFT') {
1262 if (!start_indented) {
1265 } else if (direction == 'DOWNRIGHT') {
1267 if (start_indented) {
1272 if (target[0] < 0 || target[1] < 0 ||
1273 target[0] >= this.map_size[0] || target[1] >= this.map_size[1]) {
1278 teleport: function() {
1279 let player = this.get_thing(game.player_id);
1280 if (player.position in this.portals) {
1281 server.reconnect_to(this.portals[player.position]);
1283 terminal.blink_screen();
1284 tui.log_msg('? not standing on portal')
1292 server.init(websocket_location);
1298 move: function(direction) {
1299 let target = game.move(this.position, direction);
1301 this.position = target
1302 this.info_cached = false;
1303 if (tui.tile_draw) {
1304 this.send_tile_control_command();
1307 terminal.blink_screen();
1310 update_annotations: function(yx, str) {
1311 this.annotations[yx] = str;
1312 if (tui.mode.name == 'study') {
1316 empty_annotations: function() {
1317 this.annotations = {};
1318 if (tui.mode.name == 'study') {
1322 get_info: function() {
1323 if (this.info_cached) {
1324 return this.info_cached;
1326 let info_to_cache = '';
1327 let position_i = this.position[0] * game.map_size[1] + this.position[1];
1328 if (game.fov[position_i] != '.') {
1329 info_to_cache += 'outside field of view';
1331 for (let t_id in game.things) {
1332 let t = game.things[t_id];
1333 if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
1334 info_to_cache += "THING: " + this.get_thing_info(t);
1335 let protection = t.protection;
1336 if (protection == '.') {
1337 protection = 'none';
1339 info_to_cache += " / protection: " + protection + "\n";
1341 info_to_cache += t.face.slice(0, 3) + '\n';
1342 info_to_cache += t.face.slice(3, 6) + '\n';
1343 info_to_cache += t.face.slice(6, 9) + '\n';
1347 let terrain_char = game.map[position_i]
1348 let terrain_desc = '?'
1349 if (game.terrains[terrain_char]) {
1350 terrain_desc = game.terrains[terrain_char];
1352 info_to_cache += 'TERRAIN: "' + terrain_char + '" / ' + terrain_desc + "\n";
1353 let protection = game.map_control[position_i];
1354 if (protection == '.') {
1355 protection = 'unprotected';
1357 info_to_cache += 'PROTECTION: ' + protection + '\n';
1358 if (this.position in game.portals) {
1359 info_to_cache += "PORTAL: " + game.portals[this.position] + "\n";
1361 if (this.position in this.annotations) {
1362 info_to_cache += "ANNOTATION: " + this.annotations[this.position];
1365 this.info_cached = info_to_cache;
1366 return this.info_cached;
1368 get_thing_info: function(t) {
1369 const symbol = game.thing_types[t.type_];
1370 let info = t.type_ + " / " + symbol;
1372 info += t.thing_char;
1375 info += " (" + t.name_ + ")";
1378 info += " / installed";
1382 annotate: function(msg) {
1383 if (msg.length == 0) {
1384 msg = " "; // triggers annotation deletion
1386 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg, tui.password]);
1388 set_portal: function(msg) {
1389 if (msg.length == 0) {
1390 msg = " "; // triggers portal deletion
1392 server.send(["PORTAL", unparser.to_yx(explorer.position), msg, tui.password]);
1394 send_tile_control_command: function() {
1395 server.send(["SET_TILE_CONTROL", unparser.to_yx(this.position), tui.tile_control_char]);
1399 tui.inputEl.addEventListener('input', (event) => {
1400 if (tui.mode.has_input_prompt) {
1401 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
1402 if (tui.inputEl.value.length > max_length) {
1403 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
1405 } else if (tui.mode.name == 'write' && tui.inputEl.value.length > 0) {
1406 server.send(["TASK:WRITE", tui.inputEl.value[0], tui.password]);
1407 tui.switch_mode('edit');
1411 document.onclick = function() {
1412 tui.show_help = false;
1414 tui.inputEl.addEventListener('keydown', (event) => {
1415 tui.show_help = false;
1416 if (event.key == 'Enter') {
1417 event.preventDefault();
1419 if (tui.mode.has_input_prompt && event.key == 'Enter'
1420 && tui.inputEl.value.length == 0
1421 && ['chat', 'command_thing', 'take_thing',
1422 'admin_enter'].includes(tui.mode.name)) {
1423 if (tui.mode.name != 'chat') {
1424 tui.log_msg('@ aborted');
1426 tui.switch_mode('play');
1427 } else if (tui.mode.has_input_prompt && event.key == 'Enter' && tui.inputEl.value == '/help') {
1428 tui.show_help = true;
1429 tui.inputEl.value = "";
1430 tui.restore_input_values();
1431 } else if (!tui.mode.has_input_prompt && event.key == tui.keys.help
1432 && !tui.mode.is_single_char_entry) {
1433 tui.show_help = true;
1434 } else if (tui.mode.name == 'login' && event.key == 'Enter') {
1435 tui.login_name = tui.inputEl.value;
1436 server.send(['LOGIN', tui.inputEl.value]);
1437 tui.inputEl.value = "";
1438 } else if (tui.mode.name == 'enter_face' && event.key == 'Enter') {
1439 if (tui.inputEl.value.length != 9) {
1440 tui.log_msg('? wrong input length, aborting');
1442 server.send(['PLAYER_FACE', tui.inputEl.value]);
1444 tui.inputEl.value = "";
1445 tui.switch_mode('edit');
1446 } else if (tui.mode.name == 'command_thing' && event.key == 'Enter') {
1447 if (tui.task_action_on('command')) {
1448 server.send(['TASK:COMMAND', tui.inputEl.value]);
1449 tui.inputEl.value = "";
1451 } else if (tui.mode.name == 'take_thing' && event.key == 'Enter') {
1452 const i = parseInt(tui.inputEl.value);
1453 if (isNaN(i) || i < 0 || i >= tui.selectables.length) {
1454 tui.log_msg('? invalid index, aborted');
1456 server.send(['TASK:PICK_UP', tui.selectables[i][0]]);
1458 tui.inputEl.value = "";
1459 tui.switch_mode('play');
1460 } else if (tui.mode.name == 'control_pw_pw' && event.key == 'Enter') {
1461 if (tui.inputEl.value.length == 0) {
1462 tui.log_msg('@ aborted');
1464 server.send(['SET_MAP_CONTROL_PASSWORD',
1465 tui.tile_control_char, tui.inputEl.value]);
1466 tui.log_msg('@ sent new password for protection character "' + tui.tile_control_char + '".');
1468 tui.switch_mode('admin');
1469 } else if (tui.mode.name == 'portal' && event.key == 'Enter') {
1470 explorer.set_portal(tui.inputEl.value);
1471 tui.switch_mode('edit');
1472 } else if (tui.mode.name == 'name_thing' && event.key == 'Enter') {
1473 if (tui.inputEl.value.length == 0) {
1474 tui.inputEl.value = " ";
1476 server.send(["THING_NAME", tui.selected_thing_id, tui.inputEl.value,
1478 tui.switch_mode('edit');
1479 } else if (tui.mode.name == 'annotate' && event.key == 'Enter') {
1480 explorer.annotate(tui.inputEl.value);
1481 tui.switch_mode('edit');
1482 } else if (tui.mode.name == 'password' && event.key == 'Enter') {
1483 if (tui.inputEl.value.length == 0) {
1484 tui.inputEl.value = " ";
1486 tui.password = tui.inputEl.value
1487 tui.switch_mode('edit');
1488 } else if (tui.mode.name == 'admin_enter' && event.key == 'Enter') {
1489 server.send(['BECOME_ADMIN', tui.inputEl.value]);
1490 tui.switch_mode('play');
1491 } else if (tui.mode.name == 'control_pw_type' && event.key == 'Enter') {
1492 if (tui.inputEl.value.length != 1) {
1493 tui.log_msg('@ entered non-single-char, therefore aborted');
1494 tui.switch_mode('admin');
1496 tui.tile_control_char = tui.inputEl.value[0];
1497 tui.switch_mode('control_pw_pw');
1499 } else if (tui.mode.name == 'control_tile_type' && event.key == 'Enter') {
1500 if (tui.inputEl.value.length != 1) {
1501 tui.log_msg('@ entered non-single-char, therefore aborted');
1502 tui.switch_mode('admin');
1504 tui.tile_control_char = tui.inputEl.value[0];
1505 tui.switch_mode('control_tile_draw');
1507 } else if (tui.mode.name == 'admin_thing_protect' && event.key == 'Enter') {
1508 if (tui.inputEl.value.length != 1) {
1509 tui.log_msg('@ entered non-single-char, therefore aborted');
1511 server.send(['THING_PROTECTION', tui.selected_thing_id, tui.inputEl.value])
1512 tui.log_msg('@ sent new protection character for thing');
1514 tui.switch_mode('admin');
1515 } else if (tui.mode.name == 'chat' && event.key == 'Enter') {
1516 let tokens = parser.tokenize(tui.inputEl.value);
1517 if (tokens.length > 0 && tokens[0].length > 0) {
1518 if (tui.inputEl.value[0][0] == '/') {
1519 if (tokens[0].slice(1) == 'nick') {
1520 if (tokens.length > 1) {
1521 server.send(['NICK', tokens[1]]);
1523 tui.log_msg('? need new name');
1526 tui.log_msg('? unknown command');
1529 server.send(['ALL', tui.inputEl.value]);
1531 } else if (tui.inputEl.valuelength > 0) {
1532 server.send(['ALL', tui.inputEl.value]);
1534 tui.inputEl.value = "";
1535 } else if (tui.mode.name == 'play') {
1536 if (tui.mode.mode_switch_on_key(event)) {
1538 } else if (event.key === tui.keys.drop_thing && tui.task_action_on('drop_thing')) {
1539 server.send(["TASK:DROP"]);
1540 } else if (event.key === tui.keys.consume && tui.task_action_on('consume')) {
1541 server.send(["TASK:INTOXICATE"]);
1542 } else if (event.key === tui.keys.door && tui.task_action_on('door')) {
1543 server.send(["TASK:DOOR"]);
1544 } else if (event.key === tui.keys.install && tui.task_action_on('install')) {
1545 server.send(["TASK:INSTALL"]);
1546 } else if (event.key in tui.movement_keys && tui.task_action_on('move')) {
1547 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
1548 } else if (event.key === tui.keys.teleport) {
1551 } else if (tui.mode.name == 'study') {
1552 if (tui.mode.mode_switch_on_key(event)) {
1554 } else if (event.key in tui.movement_keys) {
1555 explorer.move(tui.movement_keys[event.key]);
1556 } else if (event.key == tui.keys.toggle_map_mode) {
1557 tui.toggle_map_mode();
1559 } else if (tui.mode.name == 'control_tile_draw') {
1560 if (tui.mode.mode_switch_on_key(event)) {
1562 } else if (event.key in tui.movement_keys) {
1563 explorer.move(tui.movement_keys[event.key]);
1564 } else if (event.key === tui.keys.toggle_tile_draw) {
1565 tui.toggle_tile_draw();
1567 } else if (tui.mode.name == 'admin') {
1568 if (tui.mode.mode_switch_on_key(event)) {
1570 } else if (event.key in tui.movement_keys && tui.task_action_on('move')) {
1571 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
1573 } else if (tui.mode.name == 'edit') {
1574 if (tui.mode.mode_switch_on_key(event)) {
1576 } else if (event.key in tui.movement_keys && tui.task_action_on('move')) {
1577 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
1578 } else if (event.key === tui.keys.flatten && tui.task_action_on('flatten')) {
1579 server.send(["TASK:FLATTEN_SURROUNDINGS", tui.password]);
1580 } else if (event.key == tui.keys.toggle_map_mode) {
1581 tui.toggle_map_mode();
1587 rows_selector.addEventListener('input', function() {
1588 if (rows_selector.value % 4 != 0 || rows_selector.value < 24) {
1591 window.localStorage.setItem(rows_selector.id, rows_selector.value);
1592 terminal.initialize();
1595 cols_selector.addEventListener('input', function() {
1596 if (cols_selector.value % 4 != 0 || cols_selector.value < 80) {
1599 window.localStorage.setItem(cols_selector.id, cols_selector.value);
1600 terminal.initialize();
1601 tui.window_width = terminal.cols / 2,
1604 for (let key_selector of key_selectors) {
1605 key_selector.addEventListener('input', function() {
1606 window.localStorage.setItem(key_selector.id, key_selector.value);
1610 window.setInterval(function() {
1611 if (server.connected) {
1612 server.send(['PING']);
1614 server.reconnect_to(server.url);
1615 tui.log_msg('@ attempting reconnect …')
1618 window.setInterval(function() {
1620 let span_decoration = "none";
1621 if (document.activeElement == tui.inputEl) {
1622 val = "on (click outside terminal to change)";
1624 val = "off (click into terminal to change)";
1625 span_decoration = "line-through";
1627 document.getElementById("keyboard_control").textContent = val;
1628 for (const span of document.querySelectorAll('.keyboard_controlled')) {
1629 span.style.textDecoration = span_decoration;
1632 document.getElementById("terminal").onclick = function() {
1633 tui.inputEl.focus();
1635 document.getElementById("help").onclick = function() {
1636 tui.show_help = true;
1639 for (const switchEl of document.querySelectorAll('[id^="switch_to_"]')) {
1640 const mode = switchEl.id.slice("switch_to_".length);
1641 switchEl.onclick = function() {
1642 tui.switch_mode(mode);
1646 document.getElementById("toggle_tile_draw").onclick = function() {
1647 tui.toggle_tile_draw();
1649 document.getElementById("toggle_map_mode").onclick = function() {
1650 tui.toggle_map_mode();
1653 document.getElementById("drop_thing").onclick = function() {
1654 server.send(['TASK:DROP']);
1656 document.getElementById("flatten").onclick = function() {
1657 server.send(['TASK:FLATTEN_SURROUNDINGS', tui.password]);
1659 document.getElementById("door").onclick = function() {
1660 server.send(['TASK:DOOR']);
1662 document.getElementById("consume").onclick = function() {
1663 server.send(['TASK:INTOXICATE']);
1665 document.getElementById("install").onclick = function() {
1666 server.send(['TASK:INSTALL']);
1668 document.getElementById("teleport").onclick = function() {
1671 for (const move_button of document.querySelectorAll('[id*="_move_"]')) {
1672 let direction = move_button.id.split('_')[2].toUpperCase();
1673 move_button.onclick = function() {
1674 if (tui.mode.available_actions.includes("move")
1675 || tui.mode.available_actions.includes("move_explorer")) {
1676 server.send(['TASK:MOVE', direction]);
1678 explorer.move(direction);