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>
61 <button id="wear"></button>
65 <td><button id="switch_to_edit"></button></td>
67 <button id="switch_to_write"></button>
68 <button id="flatten"></button>
69 <button id="switch_to_annotate"></button>
70 <button id="switch_to_portal"></button>
71 <button id="switch_to_name_thing"></button>
72 <button id="switch_to_password"></button>
73 <button id="switch_to_enter_face"></button>
77 <td><button id="switch_to_admin_enter"></button></td>
79 <button id="switch_to_control_pw_type"></button>
80 <button id="switch_to_control_tile_type"></button>
81 <button id="switch_to_admin_thing_protect"></button>
82 <button id="toggle_tile_draw"></button>
87 <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 />
89 <li>move up (square grid): <input id="key_square_move_up" type="text" value="w" /> (hint: ArrowUp)
90 <li>move left (square grid): <input id="key_square_move_left" type="text" value="a" /> (hint: ArrowLeft)
91 <li>move down (square grid): <input id="key_square_move_down" type="text" value="s" /> (hint: ArrowDown)
92 <li>move right (square grid): <input id="key_square_move_right" type="text" value="d" /> (hint: ArrowRight)
93 <li>move up-left (hex grid): <input id="key_hex_move_upleft" type="text" value="w" />
94 <li>move up-right (hex grid): <input id="key_hex_move_upright" type="text" value="e" />
95 <li>move right (hex grid): <input id="key_hex_move_right" type="text" value="d" />
96 <li>move down-right (hex grid): <input id="key_hex_move_downright" type="text" value="x" />
97 <li>move down-left (hex grid): <input id="key_hex_move_downleft" type="text" value="y" />
98 <li>move left (hex grid): <input id="key_hex_move_left" type="text" value="a" />
99 <li>help: <input id="key_help" type="text" value="h" />
100 <li>flatten surroundings: <input id="key_flatten" type="text" value="F" />
101 <li>teleport: <input id="key_teleport" type="text" value="p" />
102 <li>drop thing: <input id="key_drop_thing" type="text" value="u" />
103 <li>open/close: <input id="key_door" type="text" value="D" />
104 <li>consume: <input id="key_consume" type="text" value="C" />
105 <li>install: <input id="key_install" type="text" value="I" />
106 <li>(un-)wear: <input id="key_wear" type="text" value="W" />
107 <li><input id="key_switch_to_enter_face" type="text" value="f" />
108 <li><input id="key_switch_to_take_thing" type="text" value="z" />
109 <li><input id="key_switch_to_chat" type="text" value="t" />
110 <li><input id="key_switch_to_play" type="text" value="p" />
111 <li><input id="key_switch_to_study" type="text" value="?" />
112 <li><input id="key_switch_to_edit" type="text" value="E" />
113 <li><input id="key_switch_to_write" type="text" value="m" />
114 <li><input id="key_switch_to_name_thing" type="text" value="N" />
115 <li><input id="key_switch_to_command_thing" type="text" value="O" />
116 <li><input id="key_switch_to_password" type="text" value="P" />
117 <li><input id="key_switch_to_admin_enter" type="text" value="A" />
118 <li><input id="key_switch_to_control_pw_type" type="text" value="C" />
119 <li><input id="key_switch_to_control_tile_type" type="text" value="Q" />
120 <li><input id="key_switch_to_admin_thing_protect" type="text" value="T" />
121 <li><input id="key_switch_to_annotate" type="text" value="M" />
122 <li><input id="key_switch_to_portal" type="text" value="T" />
123 <li>toggle map view: <input id="key_toggle_map_mode" type="text" value="L" />
124 <li>toggle protection character drawing: <input id="key_toggle_tile_draw" type="text" value="m" />
129 let websocket_location = "wss://plomlompom.com/rogue_chat/";
130 //let websocket_location = "ws://localhost:8000/";
136 'long': 'This mode allows you to interact with the map in various ways.'
141 '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.'},
143 'short': 'world edit',
145 '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.'
148 'short': 'name thing',
150 'long': 'Give name to/change name of thing here.'
153 'short': 'command thing',
155 'long': 'Enter a command to the thing you carry. Enter nothing to return to play mode.'
158 'short': 'take thing',
159 'intro': 'Pick up a thing in reach by entering its index number. Enter nothing to abort.',
160 'long': 'You see a list of things which you could pick up. Enter the target thing\'s index, or, to leave, nothing.'
162 'admin_thing_protect': {
163 'short': 'change thing protection',
164 'intro': '@ enter thing protection character:',
165 'long': 'Change protection character for thing here.'
168 'short': 'enter your face',
169 'intro': '@ enter face line (enter nothing to abort):',
170 '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..'
173 'short': 'change terrain',
175 '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.'
178 'short': 'change protection character password',
179 'intro': '@ enter protection character for which you want to change the password:',
180 '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.'
183 'short': 'change protection character password',
185 '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.'
187 'control_tile_type': {
188 'short': 'change tiles protection',
189 'intro': '@ enter protection character which you want to draw:',
190 '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.'
192 'control_tile_draw': {
193 'short': 'change tiles protection',
195 '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.'
198 'short': 'annotate tile',
200 '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.'
203 'short': 'edit portal',
205 '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.'
210 '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'
215 'long': 'Enter your player name.'
217 'waiting_for_server': {
218 'short': 'waiting for server response',
219 'intro': '@ waiting for server …',
220 'long': 'Waiting for a server response.'
223 'short': 'waiting for server response',
225 'long': 'Waiting for a server response.'
228 'short': 'set world edit password',
230 '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.'
233 'short': 'become admin',
234 'intro': '@ enter admin password:',
235 'long': 'This mode allows you to become admin if you know an admin password.'
240 'long': 'This mode allows you access to actions limited to administrators.'
243 let key_descriptions = {
245 'flatten': 'flatten surroundings',
246 'teleport': 'teleport',
247 'drop_thing': 'drop thing',
248 'door': 'open/close',
249 'consume': 'consume',
250 'install': '(un-)install',
252 'toggle_map_mode': 'toggle map view',
253 'toggle_tile_draw': 'toggle protection character drawing',
254 'hex_move_upleft': 'up-left',
255 'hex_move_upright': 'up-right',
256 'hex_move_right': 'right',
257 'hex_move_left': 'left',
258 'hex_move_downleft': 'down-left',
259 'hex_move_downright': 'down-right',
260 'square_move_up': 'up',
261 'square_move_left': 'left',
262 'square_move_down': 'down',
263 'square_move_right': 'right',
265 for (const mode_name of Object.keys(mode_helps)) {
266 key_descriptions['switch_to_' + mode_name] = mode_helps[mode_name].short;
269 let rows_selector = document.getElementById("n_rows");
270 let cols_selector = document.getElementById("n_cols");
271 let key_selectors = document.querySelectorAll('[id^="key_"]');
273 for (const key_switch_selector of document.querySelectorAll('[id^="key_switch_to_"]')) {
274 const action = key_switch_selector.id.slice("key_switch_to_".length);
275 key_switch_selector.parentNode.prepend(mode_helps[action].short + ': ');
278 function restore_selector_value(selector) {
279 let stored_selection = window.localStorage.getItem(selector.id);
280 if (stored_selection) {
281 selector.value = stored_selection;
284 restore_selector_value(rows_selector);
285 restore_selector_value(cols_selector);
286 for (let key_selector of key_selectors) {
287 restore_selector_value(key_selector);
290 function escapeHTML(str) {
292 replace(/&/g, '&').
293 replace(/</g, '<').
294 replace(/>/g, '>').
295 replace(/'/g, ''').
296 replace(/"/g, '"');
300 initialize: function() {
301 this.rows = rows_selector.value;
302 this.cols = cols_selector.value;
303 this.pre_el = document.getElementById("terminal");
304 this.set_default_colors();
308 for (let y = 0, x = 0; y <= this.rows; x++) {
309 if (x == this.cols) {
312 this.content.push(line);
314 if (y == this.rows) {
321 apply_colors: function() {
322 this.pre_el.style.color = this.foreground;
323 this.pre_el.style.backgroundColor = this.background;
325 set_default_colors: function() {
326 this.foreground = 'white';
327 this.background = 'black';
330 set_random_colors: function() {
331 function rand(offset) {
332 return Math.floor(offset + Math.random() * 96).toString(16).padStart(2, '0');
334 this.foreground = '#' + rand(159) + rand(159) + rand(159);
335 this.background = '#' + rand(0) + rand(0) + rand(0);
338 blink_screen: function() {
339 this.pre_el.style.color = this.background;
340 this.pre_el.style.backgroundColor = this.foreground;
342 this.pre_el.style.color = this.foreground;
343 this.pre_el.style.backgroundColor = this.background;
346 refresh: function() {
347 let pre_content = '';
348 for (let y = 0; y < this.rows; y++) {
349 let line = this.content[y].join('');
351 if (y in tui.links) {
353 for (let span of tui.links[y]) {
354 chunks.push(escapeHTML(line.slice(start_x, span[0])));
355 chunks.push('<a target="_blank" href="');
356 chunks.push(escapeHTML(span[2]));
358 chunks.push(escapeHTML(line.slice(span[0], span[1])));
362 chunks.push(escapeHTML(line.slice(start_x)));
364 chunks = [escapeHTML(line)];
366 for (const chunk of chunks) {
367 pre_content += chunk;
371 this.pre_el.innerHTML = pre_content;
373 write: function(start_y, start_x, msg) {
374 for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
375 this.content[start_y][x] = msg[i];
378 drawBox: function(start_y, start_x, height, width) {
379 let end_y = start_y + height;
380 let end_x = start_x + width;
381 for (let y = start_y, x = start_x; y < this.rows; x++) {
389 this.content[y][x] = ' ';
393 terminal.initialize();
396 tokenize: function(str) {
401 for (let i = 0; i < str.length; i++) {
407 } else if (c == '\\') {
409 } else if (c == '"') {
414 } else if (c == '"') {
416 } else if (c === ' ') {
417 if (token.length > 0) {
425 if (token.length > 0) {
430 parse_yx: function(position_string) {
431 let coordinate_strings = position_string.split(',')
432 let position = [0, 0];
433 position[0] = parseInt(coordinate_strings[0].slice(2));
434 position[1] = parseInt(coordinate_strings[1].slice(2));
446 init: function(url) {
448 this.websocket = new WebSocket(this.url);
449 this.websocket.onopen = function(event) {
450 server.connected = true;
451 game.thing_types = {};
453 server.send(['TASKS']);
454 server.send(['TERRAINS']);
455 server.send(['THING_TYPES']);
456 tui.log_msg("@ server connected! :)");
457 tui.switch_mode('login');
459 this.websocket.onclose = function(event) {
460 server.connected = false;
461 tui.switch_mode('waiting_for_server');
462 tui.log_msg("@ server disconnected :(");
464 this.websocket.onmessage = this.handle_event;
466 reconnect_to: function(url) {
467 this.websocket.close();
470 send: function(tokens) {
471 this.websocket.send(unparser.untokenize(tokens));
473 handle_event: function(event) {
474 let tokens = parser.tokenize(event.data);
475 if (tokens[0] === 'TURN') {
476 game.turn_complete = false;
477 explorer.empty_annotations();
481 game.turn = parseInt(tokens[1]);
482 } else if (tokens[0] === 'THING') {
483 let t = game.get_thing(tokens[4], true);
484 t.position = parser.parse_yx(tokens[1]);
486 t.protection = tokens[3];
487 t.portable = parseInt(tokens[5]);
488 } else if (tokens[0] === 'THING_NAME') {
489 let t = game.get_thing(tokens[1], false);
491 } else if (tokens[0] === 'THING_FACE') {
492 let t = game.get_thing(tokens[1], false);
494 } else if (tokens[0] === 'THING_HAT') {
495 let t = game.get_thing(tokens[1], false);
497 } else if (tokens[0] === 'THING_CHAR') {
498 let t = game.get_thing(tokens[1], false);
499 t.thing_char = tokens[2];
500 } else if (tokens[0] === 'TASKS') {
501 game.tasks = tokens[1].split(',');
502 tui.mode_write.legal = game.tasks.includes('WRITE');
503 tui.mode_command_thing.legal = game.tasks.includes('WRITE');
504 tui.mode_take_thing.legal = game.tasks.includes('PICK_UP');
505 } else if (tokens[0] === 'THING_TYPE') {
506 game.thing_types[tokens[1]] = tokens[2]
507 } else if (tokens[0] === 'THING_CARRYING') {
508 let t = game.get_thing(tokens[1], false);
510 } else if (tokens[0] === 'THING_INSTALLED') {
511 let t = game.get_thing(tokens[1], false);
513 } else if (tokens[0] === 'TERRAIN') {
514 game.terrains[tokens[1]] = tokens[2]
515 } else if (tokens[0] === 'MAP') {
516 game.map_geometry = tokens[1];
518 game.map_size = parser.parse_yx(tokens[2]);
520 } else if (tokens[0] === 'FOV') {
522 } else if (tokens[0] === 'MAP_CONTROL') {
523 game.map_control = tokens[1]
524 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
525 game.turn_complete = true;
526 if (tui.mode.name == 'post_login_wait') {
527 tui.switch_mode('play');
529 explorer.info_cached = false;
531 } else if (tokens[0] === 'CHAT') {
532 tui.log_msg('# ' + tokens[1], 1);
533 } else if (tokens[0] === 'REPLY') {
534 tui.log_msg('#MUSICPLAYER: ' + tokens[1], 1);
535 } else if (tokens[0] === 'PLAYER_ID') {
536 game.player_id = parseInt(tokens[1]);
537 } else if (tokens[0] === 'LOGIN_OK') {
538 this.send(['GET_GAMESTATE']);
539 tui.switch_mode('post_login_wait');
540 } else if (tokens[0] === 'DEFAULT_COLORS') {
541 terminal.set_default_colors();
542 } else if (tokens[0] === 'RANDOM_COLORS') {
543 terminal.set_random_colors();
544 } else if (tokens[0] === 'ADMIN_OK') {
546 tui.log_msg('@ you now have admin rights');
547 tui.switch_mode('admin');
548 } else if (tokens[0] === 'PORTAL') {
549 let position = parser.parse_yx(tokens[1]);
550 game.portals[position] = tokens[2];
551 } else if (tokens[0] === 'ANNOTATION') {
552 let position = parser.parse_yx(tokens[1]);
553 explorer.update_annotations(position, tokens[2]);
555 } else if (tokens[0] === 'UNHANDLED_INPUT') {
556 tui.log_msg('? unknown command');
557 } else if (tokens[0] === 'PLAY_ERROR') {
558 tui.log_msg('? ' + tokens[1]);
559 terminal.blink_screen();
560 } else if (tokens[0] === 'ARGUMENT_ERROR') {
561 tui.log_msg('? syntax error: ' + tokens[1]);
562 } else if (tokens[0] === 'GAME_ERROR') {
563 tui.log_msg('? game error: ' + tokens[1]);
564 } else if (tokens[0] === 'PONG') {
567 tui.log_msg('? unhandled input: ' + event.data);
573 quote: function(str) {
575 for (let i = 0; i < str.length; i++) {
577 if (['"', '\\'].includes(c)) {
583 return quoted.join('');
585 to_yx: function(yx_coordinate) {
586 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
588 untokenize: function(tokens) {
589 let quoted_tokens = [];
590 for (let token of tokens) {
591 quoted_tokens.push(this.quote(token));
593 return quoted_tokens.join(" ");
598 constructor(name, has_input_prompt=false, shows_info=false,
599 is_intro=false, is_single_char_entry=false) {
601 this.short_desc = mode_helps[name].short;
602 this.available_modes = [];
603 this.available_actions = [];
604 this.has_input_prompt = has_input_prompt;
605 this.shows_info= shows_info;
606 this.is_intro = is_intro;
607 this.help_intro = mode_helps[name].long;
608 this.intro_msg = mode_helps[name].intro;
609 this.is_single_char_entry = is_single_char_entry;
612 *iter_available_modes() {
613 for (let mode_name of this.available_modes) {
614 let mode = tui['mode_' + mode_name];
618 let key = tui.keys['switch_to_' + mode.name];
622 list_available_modes() {
624 if (this.available_modes.length > 0) {
625 msg += 'Other modes available from here:\n';
626 for (let [mode, key] of this.iter_available_modes()) {
627 msg += '[' + key + '] – ' + mode.short_desc + '\n';
632 mode_switch_on_key(key_event) {
633 for (let [mode, key] of this.iter_available_modes()) {
634 if (key_event.key == key) {
635 event.preventDefault();
636 tui.switch_mode(mode.name);
648 window_width: terminal.cols / 2,
656 mode_waiting_for_server: new Mode('waiting_for_server',
658 mode_login: new Mode('login', true, false, true),
659 mode_post_login_wait: new Mode('post_login_wait'),
660 mode_chat: new Mode('chat', true),
661 mode_annotate: new Mode('annotate', true, true),
662 mode_play: new Mode('play'),
663 mode_study: new Mode('study', false, true),
664 mode_write: new Mode('write', false, false, false, true),
665 mode_edit: new Mode('edit'),
666 mode_control_pw_type: new Mode('control_pw_type', true),
667 mode_admin_thing_protect: new Mode('admin_thing_protect', true),
668 mode_portal: new Mode('portal', true, true),
669 mode_password: new Mode('password', true),
670 mode_name_thing: new Mode('name_thing', true, true),
671 mode_command_thing: new Mode('command_thing', true),
672 mode_take_thing: new Mode('take_thing', true),
673 mode_enter_face: new Mode('enter_face', true),
674 mode_admin_enter: new Mode('admin_enter', true),
675 mode_admin: new Mode('admin'),
676 mode_control_pw_pw: new Mode('control_pw_pw', true),
677 mode_control_tile_type: new Mode('control_tile_type', true),
678 mode_control_tile_draw: new Mode('control_tile_draw'),
680 'flatten': 'FLATTEN_SURROUNDINGS',
681 'take_thing': 'PICK_UP',
682 'drop_thing': 'DROP',
685 'install': 'INSTALL',
687 'command': 'COMMAND',
688 'consume': 'INTOXICATE',
694 this.mode_play.available_modes = ["chat", "study", "edit", "admin_enter",
695 "command_thing", "take_thing"]
696 this.mode_play.available_actions = ["move", "drop_thing", "teleport",
697 "door", "consume", "install", "wear"];
698 this.mode_study.available_modes = ["chat", "play", "admin_enter", "edit"]
699 this.mode_study.available_actions = ["toggle_map_mode", "move_explorer"];
700 this.mode_admin.available_modes = ["admin_thing_protect", "control_pw_type",
701 "control_tile_type", "chat",
702 "study", "play", "edit"]
703 this.mode_admin.available_actions = ["move"];
704 this.mode_control_tile_draw.available_modes = ["admin_enter"]
705 this.mode_control_tile_draw.available_actions = ["toggle_tile_draw"];
706 this.mode_edit.available_modes = ["write", "annotate", "portal", "name_thing",
707 "password", "chat", "study", "play",
708 "admin_enter", "enter_face"]
709 this.mode_edit.available_actions = ["move", "flatten", "toggle_map_mode"]
710 this.inputEl = document.getElementById("input");
711 this.inputEl.focus();
712 this.switch_mode('waiting_for_server');
713 this.recalc_input_lines();
714 this.height_header = this.height_turn_line + this.height_mode_line;
717 init_keys: function() {
718 document.getElementById("move_table").hidden = true;
720 for (let key_selector of key_selectors) {
721 this.keys[key_selector.id.slice(4)] = key_selector.value;
723 this.movement_keys = {};
724 let geometry_prefix = 'undefinedMapGeometry_';
725 if (game.map_geometry) {
726 geometry_prefix = game.map_geometry.toLowerCase() + '_';
728 for (const key_name of Object.keys(key_descriptions)) {
729 if (key_name.startsWith(geometry_prefix)) {
730 let direction = key_name.split('_')[2].toUpperCase();
731 let key = this.keys[key_name];
732 this.movement_keys[key] = direction;
735 for (const move_button of document.querySelectorAll('[id*="_move_"]')) {
736 if (move_button.id.startsWith('key_')) {
739 move_button.hidden = true;
741 for (const move_button of document.querySelectorAll('[id^="' + geometry_prefix + 'move_"]')) {
742 document.getElementById("move_table").hidden = false;
743 move_button.hidden = false;
745 for (let el of document.getElementsByTagName("button")) {
746 let action_desc = key_descriptions[el.id];
747 let action_key = '[' + this.keys[el.id] + ']';
748 el.innerHTML = escapeHTML(action_desc) + '<br /><span class="keyboard_controlled">' + escapeHTML(action_key) + '</span>';
751 task_action_on: function(action) {
752 return game.tasks.includes(this.action_tasks[action]);
754 switch_mode: function(mode_name) {
755 if (this.mode && this.mode.name == 'control_tile_draw') {
756 tui.log_msg('@ finished tile protection drawing.')
758 this.tile_draw = false;
759 if (mode_name == 'admin_enter' && this.is_admin) {
761 } else if (['name_thing', 'admin_thing_protect'].includes(mode_name)) {
762 let player_position = game.things[game.player_id].position;
764 for (let t_id in game.things) {
765 if (t_id == game.player_id) {
768 let t = game.things[t_id];
769 if (player_position[0] == t.position[0] && player_position[1] == t.position[1]) {
775 terminal.blink_screen();
776 this.log_msg('? not standing over thing');
779 this.selected_thing_id = thing_id;
782 this.mode = this['mode_' + mode_name];
783 if (["control_tile_draw", "control_tile_type", "control_pw_type"].includes(this.mode.name)) {
784 this.map_mode = 'protections';
785 } else if (this.mode.name != "edit") {
786 this.map_mode = 'terrain + things';
788 if (this.mode.has_input_prompt || this.mode.is_single_char_entry) {
789 this.inputEl.focus();
791 if (game.player_id in game.things && (this.mode.shows_info || this.mode.name == 'control_tile_draw')) {
792 explorer.position = game.things[game.player_id].position;
794 this.inputEl.value = "";
795 this.restore_input_values();
796 for (let el of document.getElementsByTagName("button")) {
799 document.getElementById("help").disabled = false;
800 for (const action of this.mode.available_actions) {
801 if (["move", "move_explorer"].includes(action)) {
802 for (const move_key of document.querySelectorAll('[id*="_move_"]')) {
803 move_key.disabled = false;
805 } else if (Object.keys(this.action_tasks).includes(action)) {
806 if (this.task_action_on(action)) {
807 document.getElementById(action).disabled = false;
810 document.getElementById(action).disabled = false;
813 for (const mode_name of this.mode.available_modes) {
814 document.getElementById('switch_to_' + mode_name).disabled = false;
816 if (this.mode.intro_msg.length > 0) {
817 this.log_msg(this.mode.intro_msg);
819 if (this.mode.name == 'login') {
820 if (this.login_name) {
821 server.send(['LOGIN', this.login_name]);
823 this.log_msg("? need login name");
825 } else if (this.mode.is_single_char_entry) {
826 this.show_help = true;
827 } else if (this.mode.name == 'take_thing') {
828 this.log_msg("Portable things in reach for pick-up:");
829 const player = game.things[game.player_id];
830 const y = player.position[0]
831 const x = player.position[1]
832 let select_range = [y.toString() + ':' + x.toString(),
833 (y + 0).toString() + ':' + (x - 1).toString(),
834 (y + 0).toString() + ':' + (x + 1).toString(),
835 (y - 1).toString() + ':' + (x).toString(),
836 (y + 1).toString() + ':' + (x).toString()];
837 if (game.map_geometry == 'Hex') {
839 select_range.push((y - 1).toString() + ':' + (x + 1).toString());
840 select_range.push((y + 1).toString() + ':' + (x + 1).toString());
842 select_range.push((y - 1).toString() + ':' + (x - 1).toString());
843 select_range.push((y + 1).toString() + ':' + (x - 1).toString());
846 this.selectables = [];
847 for (const t_id in game.things) {
848 const t = game.things[t_id];
849 if (select_range.includes(t.position[0].toString()
850 + ':' + t.position[1].toString())
852 this.selectables.push([t_id, t]);
855 if (this.selectables.length == 0) {
858 for (let [i, t] of this.selectables.entries()) {
859 this.log_msg(i + ': ' + explorer.get_thing_info(t[1]));
862 } else if (this.mode.name == 'command_thing') {
863 server.send(['TASK:COMMAND', 'HELP']);
864 } else if (this.mode.name == 'control_pw_pw') {
865 this.log_msg('@ enter protection password for "' + this.tile_control_char + '":');
866 } else if (this.mode.name == 'control_tile_draw') {
867 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 + '].')
871 offset_links: function(offset, links) {
872 for (let y in links) {
873 let real_y = offset[0] + parseInt(y);
874 if (!this.links[real_y]) {
875 this.links[real_y] = [];
877 for (let link of links[y]) {
878 const offset_link = [link[0] + offset[1], link[1] + offset[1], link[2]];
879 this.links[real_y].push(offset_link);
883 restore_input_values: function() {
884 if (this.mode.name == 'annotate' && explorer.position in explorer.annotations) {
885 let info = explorer.annotations[explorer.position];
886 if (info != "(none)") {
887 this.inputEl.value = info;
889 } else if (this.mode.name == 'portal' && explorer.position in game.portals) {
890 let portal = game.portals[explorer.position]
891 this.inputEl.value = portal;
892 } else if (this.mode.name == 'password') {
893 this.inputEl.value = this.password;
894 } else if (this.mode.name == 'name_thing') {
895 let t = game.get_thing(this.selected_thing_id);
897 this.inputEl.value = t.name_;
899 } else if (this.mode.name == 'admin_thing_protect') {
900 let t = game.get_thing(this.selected_thing_id);
901 if (t && t.protection) {
902 this.inputEl.value = t.protection;
906 recalc_input_lines: function() {
907 if (this.mode.has_input_prompt) {
909 [this.input_lines, _] = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
911 this.input_lines = [];
913 this.height_input = this.input_lines.length;
915 msg_into_lines_of_width: function(msg, width) {
916 function push_inner_link(y, end_x) {
917 if (!inner_links[y]) {
920 inner_links[y].push([url_start_x, end_x, url]);
922 const matches = msg.matchAll(/https?:\/\/[^\s]+/g)
925 for (const match of matches) {
926 const url = match[0];
927 const url_start = match.index;
928 const url_end = match.index + match[0].length;
929 link_data[url_start] = url;
930 url_ends.push(url_end);
934 let inner_links = {};
938 for (let i = 0, x = 0, y = 0; i < msg.length; i++, x++) {
939 if (x >= width || msg[i] == "\n") {
941 push_inner_link(y, chunk.length);
943 if (url_ends[0] == i) {
951 if (msg[i] == "\n") {
956 if (msg[i] != "\n") {
959 if (i in link_data) {
963 } else if (url_ends[0] == i) {
965 push_inner_link(y, x);
971 push_inner_link(lines.length - 1, chunk.length);
973 return [lines, inner_links];
975 log_msg: function(msg) {
977 while (this.log.length > 100) {
982 draw_map: function() {
983 if (!game.turn_complete && this.map_lines.length == 0) {
986 if (game.turn_complete) {
987 let map_lines_split = [];
989 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
990 if (j == game.map_size[1]) {
991 map_lines_split.push(line);
995 if (this.map_mode == 'protections') {
996 line.push(game.map_control[i] + ' ');
998 line.push(game.map[i] + ' ');
1001 map_lines_split.push(line);
1002 if (this.map_mode == 'terrain + annotations') {
1003 for (const coordinate of explorer.info_hints) {
1004 map_lines_split[coordinate[0]][coordinate[1]] = 'A ';
1006 } else if (this.map_mode == 'terrain + things') {
1007 for (const p in game.portals) {
1008 let coordinate = p.split(',')
1009 let original = map_lines_split[coordinate[0]][coordinate[1]];
1010 map_lines_split[coordinate[0]][coordinate[1]] = original[0] + 'P';
1012 let used_positions = [];
1013 function draw_thing(t, used_positions) {
1014 let symbol = game.thing_types[t.type_];
1015 let meta_char = ' ';
1017 meta_char = t.thing_char;
1019 if (used_positions.includes(t.position.toString())) {
1025 map_lines_split[t.position[0]][t.position[1]] = symbol + meta_char;
1026 used_positions.push(t.position.toString());
1028 for (const thing_id in game.things) {
1029 let t = game.things[thing_id];
1030 if (t.type_ != 'Player') {
1031 draw_thing(t, used_positions);
1034 for (const thing_id in game.things) {
1035 let t = game.things[thing_id];
1036 if (t.type_ == 'Player') {
1037 draw_thing(t, used_positions);
1041 let player = game.things[game.player_id];
1042 if (tui.mode.shows_info || tui.mode.name == 'control_tile_draw') {
1043 map_lines_split[explorer.position[0]][explorer.position[1]] = '??';
1044 } else if (tui.map_mode != 'terrain + things') {
1045 map_lines_split[player.position[0]][player.position[1]] = '??';
1048 if (game.map_geometry == 'Square') {
1049 for (let line_split of map_lines_split) {
1050 this.map_lines.push(line_split.join(''));
1052 } else if (game.map_geometry == 'Hex') {
1054 for (let line_split of map_lines_split) {
1055 this.map_lines.push(' '.repeat(indent) + line_split.join(''));
1063 let window_center = [terminal.rows / 2, this.window_width / 2];
1064 let center_position = [player.position[0], player.position[1]];
1065 if (tui.mode.shows_info || tui.mode.name == 'control_tile_draw') {
1066 center_position = [explorer.position[0], explorer.position[1]];
1068 center_position[1] = center_position[1] * 2;
1069 this.offset = [center_position[0] - window_center[0],
1070 center_position[1] - window_center[1]]
1071 if (game.map_geometry == 'Hex' && this.offset[0] % 2) {
1072 this.offset[1] += 1;
1075 let term_y = Math.max(0, -this.offset[0]);
1076 let term_x = Math.max(0, -this.offset[1]);
1077 let map_y = Math.max(0, this.offset[0]);
1078 let map_x = Math.max(0, this.offset[1]);
1079 for (; term_y < terminal.rows && map_y < this.map_lines.length; term_y++, map_y++) {
1080 let to_draw = this.map_lines[map_y].slice(map_x, this.window_width + this.offset[1]);
1081 terminal.write(term_y, term_x, to_draw);
1084 draw_mode_line: function() {
1085 let help = 'hit [' + this.keys.help + '] for help';
1086 if (this.mode.has_input_prompt) {
1087 help = 'enter /help for help';
1089 terminal.write(0, this.window_width, 'MODE: ' + this.mode.short_desc + ' – ' + help);
1091 draw_turn_line: function(n) {
1092 if (game.turn_complete) {
1093 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
1096 draw_history: function() {
1097 let log_display_lines = [];
1099 let y_offset_in_log = 0;
1100 for (let line of this.log) {
1101 let [new_lines, link_data] = this.msg_into_lines_of_width(line,
1103 log_display_lines = log_display_lines.concat(new_lines);
1104 for (const y in link_data) {
1105 const rel_y = y_offset_in_log + parseInt(y);
1106 log_links[rel_y] = [];
1107 for (let link of link_data[y]) {
1108 log_links[rel_y].push(link);
1111 y_offset_in_log += new_lines.length;
1113 let i = log_display_lines.length - 1;
1114 for (let y = terminal.rows - 1 - this.height_input;
1115 y >= this.height_header && i >= 0;
1117 terminal.write(y, this.window_width, log_display_lines[i]);
1119 for (const key of Object.keys(log_links)) {
1120 if (parseInt(key) <= i) {
1121 delete log_links[key];
1124 let offset = [terminal.rows - this.height_input - log_display_lines.length,
1126 this.offset_links(offset, log_links);
1128 draw_info: function() {
1129 const info = "MAP VIEW: " + tui.map_mode + "\n" + explorer.get_info();
1130 let [lines, link_data] = this.msg_into_lines_of_width(info, this.window_width);
1131 let offset = [this.height_header, this.window_width];
1132 for (let y = offset[0], i = 0; y < terminal.rows && i < lines.length; y++, i++) {
1133 terminal.write(y, offset[1], lines[i]);
1135 this.offset_links(offset, link_data);
1137 draw_input: function() {
1138 if (this.mode.has_input_prompt) {
1139 for (let y = terminal.rows - this.height_input, i = 0; i < this.input_lines.length; y++, i++) {
1140 terminal.write(y, this.window_width, this.input_lines[i]);
1144 draw_help: function() {
1145 let movement_keys_desc = '';
1146 if (!this.mode.is_intro) {
1147 movement_keys_desc = Object.keys(this.movement_keys).join(',');
1149 let content = this.mode.short_desc + " help\n\n" + this.mode.help_intro + "\n\n";
1150 if (this.mode.available_actions.length > 0) {
1151 content += "Available actions:\n";
1152 for (let action of this.mode.available_actions) {
1153 if (Object.keys(this.action_tasks).includes(action)) {
1154 if (!this.task_action_on(action)) {
1158 if (action == 'move_explorer') {
1161 if (action == 'move') {
1162 content += "[" + movement_keys_desc + "] – move\n"
1164 content += "[" + this.keys[action] + "] – " + key_descriptions[action] + "\n";
1169 content += this.mode.list_available_modes();
1171 if (!this.mode.has_input_prompt) {
1172 start_x = this.window_width
1174 terminal.drawBox(0, start_x, terminal.rows, this.window_width);
1175 let [lines, _] = this.msg_into_lines_of_width(content, this.window_width);
1176 for (let y = 0, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
1177 terminal.write(y, start_x, lines[i]);
1180 toggle_tile_draw: function() {
1181 if (tui.tile_draw) {
1182 tui.tile_draw = false;
1184 tui.tile_draw = true;
1187 toggle_map_mode: function() {
1188 if (tui.map_mode == 'terrain only') {
1189 tui.map_mode = 'terrain + annotations';
1190 } else if (tui.map_mode == 'terrain + annotations') {
1191 tui.map_mode = 'terrain + things';
1192 } else if (tui.map_mode == 'terrain + things') {
1193 tui.map_mode = 'protections';
1194 } else if (tui.map_mode == 'protections') {
1195 tui.map_mode = 'terrain only';
1198 full_refresh: function() {
1200 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
1201 this.recalc_input_lines();
1202 if (this.mode.is_intro) {
1203 this.draw_history();
1207 this.draw_turn_line();
1208 this.draw_mode_line();
1209 if (this.mode.shows_info) {
1212 this.draw_history();
1216 if (this.show_help) {
1228 this.map_control = "";
1229 this.map_size = [0,0];
1230 this.player_id = -1;
1234 get_thing: function(id_, create_if_not_found=false) {
1235 if (id_ in game.things) {
1236 return game.things[id_];
1237 } else if (create_if_not_found) {
1238 let t = new Thing([0,0]);
1239 game.things[id_] = t;
1243 move: function(start_position, direction) {
1244 let target = [start_position[0], start_position[1]];
1245 if (direction == 'LEFT') {
1247 } else if (direction == 'RIGHT') {
1249 } else if (game.map_geometry == 'Square') {
1250 if (direction == 'UP') {
1252 } else if (direction == 'DOWN') {
1255 } else if (game.map_geometry == 'Hex') {
1256 let start_indented = start_position[0] % 2;
1257 if (direction == 'UPLEFT') {
1259 if (!start_indented) {
1262 } else if (direction == 'UPRIGHT') {
1264 if (start_indented) {
1267 } else if (direction == 'DOWNLEFT') {
1269 if (!start_indented) {
1272 } else if (direction == 'DOWNRIGHT') {
1274 if (start_indented) {
1279 if (target[0] < 0 || target[1] < 0 ||
1280 target[0] >= this.map_size[0] || target[1] >= this.map_size[1]) {
1285 teleport: function() {
1286 let player = this.get_thing(game.player_id);
1287 if (player.position in this.portals) {
1288 server.reconnect_to(this.portals[player.position]);
1290 terminal.blink_screen();
1291 tui.log_msg('? not standing on portal')
1299 server.init(websocket_location);
1305 move: function(direction) {
1306 let target = game.move(this.position, direction);
1308 this.position = target
1309 this.info_cached = false;
1310 if (tui.tile_draw) {
1311 this.send_tile_control_command();
1314 terminal.blink_screen();
1317 update_annotations: function(yx, str) {
1318 this.annotations[yx] = str;
1319 if (tui.mode.name == 'study') {
1323 empty_annotations: function() {
1324 this.annotations = {};
1325 if (tui.mode.name == 'study') {
1329 get_info: function() {
1330 if (this.info_cached) {
1331 return this.info_cached;
1333 let info_to_cache = '';
1334 let position_i = this.position[0] * game.map_size[1] + this.position[1];
1335 if (game.fov[position_i] != '.') {
1336 info_to_cache += 'outside field of view';
1338 for (let t_id in game.things) {
1339 let t = game.things[t_id];
1340 if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
1341 info_to_cache += "THING: " + this.get_thing_info(t);
1342 let protection = t.protection;
1343 if (protection == '.') {
1344 protection = 'none';
1346 info_to_cache += " / protection: " + protection + "\n";
1348 info_to_cache += t.hat.slice(0, 6) + '\n';
1349 info_to_cache += t.hat.slice(6, 12) + '\n';
1350 info_to_cache += t.hat.slice(12, 18) + '\n';
1353 info_to_cache += t.face.slice(0, 6) + '\n';
1354 info_to_cache += t.face.slice(6, 12) + '\n';
1355 info_to_cache += t.face.slice(12, 18) + '\n';
1359 let terrain_char = game.map[position_i]
1360 let terrain_desc = '?'
1361 if (game.terrains[terrain_char]) {
1362 terrain_desc = game.terrains[terrain_char];
1364 info_to_cache += 'TERRAIN: "' + terrain_char + '" / ' + terrain_desc + "\n";
1365 let protection = game.map_control[position_i];
1366 if (protection == '.') {
1367 protection = 'unprotected';
1369 info_to_cache += 'PROTECTION: ' + protection + '\n';
1370 if (this.position in game.portals) {
1371 info_to_cache += "PORTAL: " + game.portals[this.position] + "\n";
1373 if (this.position in this.annotations) {
1374 info_to_cache += "ANNOTATION: " + this.annotations[this.position];
1377 this.info_cached = info_to_cache;
1378 return this.info_cached;
1380 get_thing_info: function(t) {
1381 const symbol = game.thing_types[t.type_];
1382 let info = t.type_ + " / " + symbol;
1384 info += t.thing_char;
1387 info += " (" + t.name_ + ")";
1390 info += " / installed";
1394 annotate: function(msg) {
1395 if (msg.length == 0) {
1396 msg = " "; // triggers annotation deletion
1398 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg, tui.password]);
1400 set_portal: function(msg) {
1401 if (msg.length == 0) {
1402 msg = " "; // triggers portal deletion
1404 server.send(["PORTAL", unparser.to_yx(explorer.position), msg, tui.password]);
1406 send_tile_control_command: function() {
1407 server.send(["SET_TILE_CONTROL", unparser.to_yx(this.position), tui.tile_control_char]);
1411 tui.inputEl.addEventListener('input', (event) => {
1412 if (tui.mode.has_input_prompt) {
1413 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
1414 if (tui.inputEl.value.length > max_length) {
1415 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
1417 } else if (tui.mode.name == 'write' && tui.inputEl.value.length > 0) {
1418 server.send(["TASK:WRITE", tui.inputEl.value[0], tui.password]);
1419 tui.switch_mode('edit');
1423 document.onclick = function() {
1424 tui.show_help = false;
1426 tui.inputEl.addEventListener('keydown', (event) => {
1427 tui.show_help = false;
1428 if (event.key == 'Enter') {
1429 event.preventDefault();
1431 if (tui.mode.has_input_prompt && event.key == 'Enter'
1432 && tui.inputEl.value.length == 0
1433 && ['chat', 'command_thing', 'take_thing',
1434 'admin_enter'].includes(tui.mode.name)) {
1435 if (tui.mode.name != 'chat') {
1436 tui.log_msg('@ aborted');
1438 tui.switch_mode('play');
1439 } else if (tui.mode.has_input_prompt && event.key == 'Enter' && tui.inputEl.value == '/help') {
1440 tui.show_help = true;
1441 tui.inputEl.value = "";
1442 tui.restore_input_values();
1443 } else if (!tui.mode.has_input_prompt && event.key == tui.keys.help
1444 && !tui.mode.is_single_char_entry) {
1445 tui.show_help = true;
1446 } else if (tui.mode.name == 'login' && event.key == 'Enter') {
1447 tui.login_name = tui.inputEl.value;
1448 server.send(['LOGIN', tui.inputEl.value]);
1449 tui.inputEl.value = "";
1450 } else if (tui.mode.name == 'enter_face' && event.key == 'Enter') {
1451 if (tui.inputEl.value.length != 18) {
1452 tui.log_msg('? wrong input length, aborting');
1454 server.send(['PLAYER_FACE', tui.inputEl.value]);
1456 tui.inputEl.value = "";
1457 tui.switch_mode('edit');
1458 } else if (tui.mode.name == 'command_thing' && event.key == 'Enter') {
1459 if (tui.task_action_on('command')) {
1460 server.send(['TASK:COMMAND', tui.inputEl.value]);
1461 tui.inputEl.value = "";
1463 } else if (tui.mode.name == 'take_thing' && event.key == 'Enter') {
1464 const i = parseInt(tui.inputEl.value);
1465 if (isNaN(i) || i < 0 || i >= tui.selectables.length) {
1466 tui.log_msg('? invalid index, aborted');
1468 server.send(['TASK:PICK_UP', tui.selectables[i][0]]);
1470 tui.inputEl.value = "";
1471 tui.switch_mode('play');
1472 } else if (tui.mode.name == 'control_pw_pw' && event.key == 'Enter') {
1473 if (tui.inputEl.value.length == 0) {
1474 tui.log_msg('@ aborted');
1476 server.send(['SET_MAP_CONTROL_PASSWORD',
1477 tui.tile_control_char, tui.inputEl.value]);
1478 tui.log_msg('@ sent new password for protection character "' + tui.tile_control_char + '".');
1480 tui.switch_mode('admin');
1481 } else if (tui.mode.name == 'portal' && event.key == 'Enter') {
1482 explorer.set_portal(tui.inputEl.value);
1483 tui.switch_mode('edit');
1484 } else if (tui.mode.name == 'name_thing' && event.key == 'Enter') {
1485 if (tui.inputEl.value.length == 0) {
1486 tui.inputEl.value = " ";
1488 server.send(["THING_NAME", tui.selected_thing_id, tui.inputEl.value,
1490 tui.switch_mode('edit');
1491 } else if (tui.mode.name == 'annotate' && event.key == 'Enter') {
1492 explorer.annotate(tui.inputEl.value);
1493 tui.switch_mode('edit');
1494 } else if (tui.mode.name == 'password' && event.key == 'Enter') {
1495 if (tui.inputEl.value.length == 0) {
1496 tui.inputEl.value = " ";
1498 tui.password = tui.inputEl.value
1499 tui.switch_mode('edit');
1500 } else if (tui.mode.name == 'admin_enter' && event.key == 'Enter') {
1501 server.send(['BECOME_ADMIN', tui.inputEl.value]);
1502 tui.switch_mode('play');
1503 } else if (tui.mode.name == 'control_pw_type' && event.key == 'Enter') {
1504 if (tui.inputEl.value.length != 1) {
1505 tui.log_msg('@ entered non-single-char, therefore aborted');
1506 tui.switch_mode('admin');
1508 tui.tile_control_char = tui.inputEl.value[0];
1509 tui.switch_mode('control_pw_pw');
1511 } else if (tui.mode.name == 'control_tile_type' && event.key == 'Enter') {
1512 if (tui.inputEl.value.length != 1) {
1513 tui.log_msg('@ entered non-single-char, therefore aborted');
1514 tui.switch_mode('admin');
1516 tui.tile_control_char = tui.inputEl.value[0];
1517 tui.switch_mode('control_tile_draw');
1519 } else if (tui.mode.name == 'admin_thing_protect' && event.key == 'Enter') {
1520 if (tui.inputEl.value.length != 1) {
1521 tui.log_msg('@ entered non-single-char, therefore aborted');
1523 server.send(['THING_PROTECTION', tui.selected_thing_id, tui.inputEl.value])
1524 tui.log_msg('@ sent new protection character for thing');
1526 tui.switch_mode('admin');
1527 } else if (tui.mode.name == 'chat' && event.key == 'Enter') {
1528 let tokens = parser.tokenize(tui.inputEl.value);
1529 if (tokens.length > 0 && tokens[0].length > 0) {
1530 if (tui.inputEl.value[0][0] == '/') {
1531 if (tokens[0].slice(1) == 'nick') {
1532 if (tokens.length > 1) {
1533 server.send(['NICK', tokens[1]]);
1535 tui.log_msg('? need new name');
1538 tui.log_msg('? unknown command');
1541 server.send(['ALL', tui.inputEl.value]);
1543 } else if (tui.inputEl.valuelength > 0) {
1544 server.send(['ALL', tui.inputEl.value]);
1546 tui.inputEl.value = "";
1547 } else if (tui.mode.name == 'play') {
1548 if (tui.mode.mode_switch_on_key(event)) {
1550 } else if (event.key === tui.keys.drop_thing && tui.task_action_on('drop_thing')) {
1551 server.send(["TASK:DROP"]);
1552 } else if (event.key === tui.keys.consume && tui.task_action_on('consume')) {
1553 server.send(["TASK:INTOXICATE"]);
1554 } else if (event.key === tui.keys.door && tui.task_action_on('door')) {
1555 server.send(["TASK:DOOR"]);
1556 } else if (event.key === tui.keys.install && tui.task_action_on('install')) {
1557 server.send(["TASK:INSTALL"]);
1558 } else if (event.key === tui.keys.wear && tui.task_action_on('wear')) {
1559 server.send(["TASK:WEAR"]);
1560 } else if (event.key in tui.movement_keys && tui.task_action_on('move')) {
1561 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
1562 } else if (event.key === tui.keys.teleport) {
1565 } else if (tui.mode.name == 'study') {
1566 if (tui.mode.mode_switch_on_key(event)) {
1568 } else if (event.key in tui.movement_keys) {
1569 explorer.move(tui.movement_keys[event.key]);
1570 } else if (event.key == tui.keys.toggle_map_mode) {
1571 tui.toggle_map_mode();
1573 } else if (tui.mode.name == 'control_tile_draw') {
1574 if (tui.mode.mode_switch_on_key(event)) {
1576 } else if (event.key in tui.movement_keys) {
1577 explorer.move(tui.movement_keys[event.key]);
1578 } else if (event.key === tui.keys.toggle_tile_draw) {
1579 tui.toggle_tile_draw();
1581 } else if (tui.mode.name == 'admin') {
1582 if (tui.mode.mode_switch_on_key(event)) {
1584 } else if (event.key in tui.movement_keys && tui.task_action_on('move')) {
1585 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
1587 } else if (tui.mode.name == 'edit') {
1588 if (tui.mode.mode_switch_on_key(event)) {
1590 } else if (event.key in tui.movement_keys && tui.task_action_on('move')) {
1591 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
1592 } else if (event.key === tui.keys.flatten && tui.task_action_on('flatten')) {
1593 server.send(["TASK:FLATTEN_SURROUNDINGS", tui.password]);
1594 } else if (event.key == tui.keys.toggle_map_mode) {
1595 tui.toggle_map_mode();
1601 rows_selector.addEventListener('input', function() {
1602 if (rows_selector.value % 4 != 0 || rows_selector.value < 24) {
1605 window.localStorage.setItem(rows_selector.id, rows_selector.value);
1606 terminal.initialize();
1609 cols_selector.addEventListener('input', function() {
1610 if (cols_selector.value % 4 != 0 || cols_selector.value < 80) {
1613 window.localStorage.setItem(cols_selector.id, cols_selector.value);
1614 terminal.initialize();
1615 tui.window_width = terminal.cols / 2,
1618 for (let key_selector of key_selectors) {
1619 key_selector.addEventListener('input', function() {
1620 window.localStorage.setItem(key_selector.id, key_selector.value);
1624 window.setInterval(function() {
1625 if (server.connected) {
1626 server.send(['PING']);
1628 server.reconnect_to(server.url);
1629 tui.log_msg('@ attempting reconnect …')
1632 window.setInterval(function() {
1634 let span_decoration = "none";
1635 if (document.activeElement == tui.inputEl) {
1636 val = "on (click outside terminal to change)";
1638 val = "off (click into terminal to change)";
1639 span_decoration = "line-through";
1641 document.getElementById("keyboard_control").textContent = val;
1642 for (const span of document.querySelectorAll('.keyboard_controlled')) {
1643 span.style.textDecoration = span_decoration;
1646 document.getElementById("terminal").onclick = function() {
1647 tui.inputEl.focus();
1649 document.getElementById("help").onclick = function() {
1650 tui.show_help = true;
1653 for (const switchEl of document.querySelectorAll('[id^="switch_to_"]')) {
1654 const mode = switchEl.id.slice("switch_to_".length);
1655 switchEl.onclick = function() {
1656 tui.switch_mode(mode);
1660 document.getElementById("toggle_tile_draw").onclick = function() {
1661 tui.toggle_tile_draw();
1663 document.getElementById("toggle_map_mode").onclick = function() {
1664 tui.toggle_map_mode();
1667 document.getElementById("drop_thing").onclick = function() {
1668 server.send(['TASK:DROP']);
1670 document.getElementById("flatten").onclick = function() {
1671 server.send(['TASK:FLATTEN_SURROUNDINGS', tui.password]);
1673 document.getElementById("door").onclick = function() {
1674 server.send(['TASK:DOOR']);
1676 document.getElementById("consume").onclick = function() {
1677 server.send(['TASK:INTOXICATE']);
1679 document.getElementById("install").onclick = function() {
1680 server.send(['TASK:INSTALL']);
1682 document.getElementById("wear").onclick = function() {
1683 server.send(['TASK:WEAR']);
1685 document.getElementById("teleport").onclick = function() {
1688 for (const move_button of document.querySelectorAll('[id*="_move_"]')) {
1689 if (move_button.id.startsWith('key_')) { // not a move button
1692 let direction = move_button.id.split('_')[2].toUpperCase();
1693 move_button.onclick = function() {
1694 if (tui.mode.available_actions.includes("move")) {
1695 server.send(['TASK:MOVE', direction]);
1696 } else if (tui.mode.available_actions.includes("move_explorer")) {
1697 explorer.move(direction);