7 terminal rows: <input id="n_rows" type="number" step=4 min=8 value=24 />
8 terminal columns: <input id="n_cols" type="number" step=4 min=20 value=80 />
10 <pre id="terminal" style="display: inline-block;"></pre>
11 <textarea id="input" style="opacity: 0; width: 0px;"></textarea>
13 keys (see <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values">here</a> for non-obvious available values):<br />
14 move up (square grid): <input id="key_square_move_up" type="text" value="w" /> (hint: ArrowUp)<br />
15 move left (square grid): <input id="key_square_move_left" type="text" value="a" /> (hint: ArrowLeft)<br />
16 move down (square grid): <input id="key_square_move_down" type="text" value="s" /> (hint: ArrowDown)<br />
17 move right (square grid): <input id="key_square_move_right" type="text" value="d" /> (hint: ArrowRight)<br />
18 move up-left (hex grid): <input id="key_hex_move_upleft" type="text" value="w" /><br />
19 move up-right (hex grid): <input id="key_hex_move_upright" type="text" value="e" /><br />
20 move right (hex grid): <input id="key_hex_move_right" type="text" value="d" /><br />
21 move down-right (hex grid): <input id="key_hex_move_downright" type="text" value="x" /><br />
22 move down-left (hex grid): <input id="key_hex_move_downleft" type="text" value="y" /><br />
23 move left (hex grid): <input id="key_hex_move_left" type="text" value="a" /><br />
24 flatten surroundings: <input id="key_flatten" type="text" value="F" /><br />
25 switch to chat mode: <input id="key_switch_to_chat" type="text" value="t" /><br />
26 switch to play mode: <input id="key_switch_to_play" type="text" value="p" /><br />
27 switch to study mode: <input id="key_switch_to_study" type="text" value="?" /><br />
28 edit terrain (from play mode): <input id="key_switch_to_edit" type="text" value="m" /><br />
29 annotate terrain (from study mode): <input id="key_switch_to_annotate" type="text" value="m" /><br />
30 annotate portal (from study mode): <input id="key_switch_to_portal" type="text" value="P" /><br />
34 let websocket_location = "wss://plomlompom.com/rogue_chat/";
36 let rows_selector = document.getElementById("n_rows");
37 let cols_selector = document.getElementById("n_cols");
38 let key_selectors = document.querySelectorAll('[id^="key_"]');
40 function restore_selector_value(selector) {
41 let stored_selection = window.localStorage.getItem(selector.id);
42 if (stored_selection) {
43 selector.value = stored_selection;
46 restore_selector_value(rows_selector);
47 restore_selector_value(cols_selector);
48 for (let key_selector of key_selectors) {
49 restore_selector_value(key_selector);
55 initialize: function() {
56 this.rows = rows_selector.value;
57 this.cols = cols_selector.value;
58 this.pre_el = document.getElementById("terminal");
59 this.pre_el.style.color = this.foreground;
60 this.pre_el.style.backgroundColor = this.background;
63 for (let y = 0, x = 0; y <= this.rows; x++) {
67 this.content.push(line);
76 blink_screen: function() {
77 this.pre_el.style.color = this.background;
78 this.pre_el.style.backgroundColor = this.foreground;
80 this.pre_el.style.color = this.foreground;
81 this.pre_el.style.backgroundColor = this.background;
86 for (let y = 0; y < this.rows; y++) {
87 let line = this.content[y].join('');
88 pre_string += line + '\n';
90 this.pre_el.textContent = pre_string;
92 write: function(start_y, start_x, msg) {
93 for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
94 this.content[start_y][x] = msg[i];
97 drawBox: function(start_y, start_x, height, width) {
98 let end_y = start_y + height;
99 let end_x = start_x + width;
100 for (let y = start_y, x = start_x; y < this.rows; x++) {
108 this.content[y][x] = ' ';
112 terminal.initialize();
115 tokenize: function(str) {
121 for (let i = 0; i < str.length; i++) {
127 } else if (c == '\\') {
129 } else if (c == '"') {
134 } else if (c == '"') {
136 } else if (c === ' ') {
137 if (token.length > 0) {
146 if (token.length > 0) {
149 let token_starts = [];
150 for (let i = 0; i < token_ends.length; i++) {
151 token_starts.push(token_ends[i] - tokens[i].length);
153 return [tokens, token_starts];
155 parse_yx: function(position_string) {
156 let coordinate_strings = position_string.split(',')
157 let position = [0, 0];
158 position[0] = parseInt(coordinate_strings[0].slice(2));
159 position[1] = parseInt(coordinate_strings[1].slice(2));
171 init: function(url) {
173 this.websocket = new WebSocket(this.url);
174 this.websocket.onopen = function(event) {
175 window.setInterval(function() { server.send(['PING']) }, 30000);
177 tui.log_msg("@ server connected! :)");
178 tui.switch_mode(mode_login);
180 this.websocket.onclose = function(event) {
181 tui.log_msg("@ server disconnected :(");
182 tui.log_msg("@ hint: try the '/reconnect' command");
184 this.websocket.onmessage = this.handle_event;
186 reconnect: function() {
187 this.reconnect_to(this.url);
189 reconnect_to: function(url) {
190 this.websocket.close();
193 send: function(tokens) {
194 this.websocket.send(unparser.untokenize(tokens));
196 handle_event: function(event) {
197 let tokens = parser.tokenize(event.data)[0];
198 if (tokens[0] === 'TURN') {
199 game.turn_complete = false;
202 game.turn = parseInt(tokens[1]);
203 } else if (tokens[0] === 'THING_POS') {
204 game.get_thing(tokens[1], true).position = parser.parse_yx(tokens[2]);
205 } else if (tokens[0] === 'THING_NAME') {
206 game.get_thing(tokens[1], true).name_ = tokens[2];
207 } else if (tokens[0] === 'TASKS') {
208 game.tasks = tokens[1].split(',')
209 } else if (tokens[0] === 'MAP') {
210 game.map_geometry = tokens[1];
212 game.map_size = parser.parse_yx(tokens[2]);
214 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
215 game.turn_complete = true;
216 explorer.empty_info_db();
217 if (tui.mode == mode_post_login_wait) {
218 tui.switch_mode(mode_play);
220 } else if (tui.mode == mode_study) {
221 explorer.query_info();
223 let t = game.get_thing(game.player_id);
224 if (t.position in game.portals) {
225 tui.teleport_target = game.portals[t.position];
226 tui.switch_mode(mode_teleport);
230 } else if (tokens[0] === 'CHAT') {
231 tui.log_msg('# ' + tokens[1], 1);
232 } else if (tokens[0] === 'PLAYER_ID') {
233 game.player_id = parseInt(tokens[1]);
234 } else if (tokens[0] === 'LOGIN_OK') {
235 this.send(['GET_GAMESTATE']);
236 tui.switch_mode(mode_post_login_wait);
237 } else if (tokens[0] === 'PORTAL') {
238 let position = parser.parse_yx(tokens[1]);
239 game.portals[position] = tokens[2];
240 } else if (tokens[0] === 'ANNOTATION') {
241 let position = parser.parse_yx(tokens[1]);
242 explorer.update_info_db(position, tokens[2]);
243 } else if (tokens[0] === 'UNHANDLED_INPUT') {
244 tui.log_msg('? unknown command');
245 } else if (tokens[0] === 'PLAY_ERROR') {
246 terminal.blink_screen();
247 } else if (tokens[0] === 'ARGUMENT_ERROR') {
248 tui.log_msg('? syntax error: ' + tokens[1]);
249 } else if (tokens[0] === 'GAME_ERROR') {
250 tui.log_msg('? game error: ' + tokens[1]);
251 } else if (tokens[0] === 'PONG') {
254 tui.log_msg('? unhandled input: ' + event.data);
260 quote: function(str) {
262 for (let i = 0; i < str.length; i++) {
264 if (['"', '\\'].includes(c)) {
270 return quoted.join('');
272 to_yx: function(yx_coordinate) {
273 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
275 untokenize: function(tokens) {
276 let quoted_tokens = [];
277 for (let token of tokens) {
278 quoted_tokens.push(this.quote(token));
280 return quoted_tokens.join(" ");
285 constructor(name, has_input_prompt=false, shows_info=false, is_intro=false) {
287 this.has_input_prompt = has_input_prompt;
288 this.shows_info= shows_info;
289 this.is_intro = is_intro;
292 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
293 let mode_login = new Mode('login', true, false, true);
294 let mode_post_login_wait = new Mode('waiting for game world', false, false, true);
295 let mode_chat = new Mode('chat / write messages to players', true, false);
296 let mode_annotate = new Mode('add message to map tile', true, true);
297 let mode_play = new Mode('play / move around', false, false);
298 let mode_study = new Mode('check map tiles for messages', false, true);
299 let mode_edit = new Mode('write ASCII char to map tile', false, false);
300 let mode_teleport = new Mode('teleport away?', true);
301 let mode_portal = new Mode('add portal to map tile', true, true);
304 mode: mode_waiting_for_server,
308 window_width: terminal.cols / 2,
313 this.inputEl = document.getElementById("input");
314 this.inputEl.focus();
315 this.recalc_input_lines();
316 this.height_header = this.height_turn_line + this.height_mode_line;
317 this.log_msg("@ waiting for server connection ...");
320 init_keys: function() {
322 for (let key_selector of key_selectors) {
323 this.keys[key_selector.id.slice(4)] = key_selector.value;
325 if (game.map_geometry == 'Square') {
326 this.movement_keys = {
327 [this.keys.square_move_up]: 'UP',
328 [this.keys.square_move_left]: 'LEFT',
329 [this.keys.square_move_down]: 'DOWN',
330 [this.keys.square_move_right]: 'RIGHT'
332 } else if (game.map_geometry == 'Hex') {
333 this.movement_keys = {
334 [this.keys.hex_move_upleft]: 'UPLEFT',
335 [this.keys.hex_move_upright]: 'UPRIGHT',
336 [this.keys.hex_move_right]: 'RIGHT',
337 [this.keys.hex_move_downright]: 'DOWNRIGHT',
338 [this.keys.hex_move_downleft]: 'DOWNLEFT',
339 [this.keys.hex_move_left]: 'LEFT'
343 switch_mode: function(mode, keep_pos=false) {
344 if (mode == mode_study && !keep_pos && game.player_id in game.things) {
345 explorer.position = game.things[game.player_id].position;
349 if (mode == mode_annotate && explorer.position in explorer.info_db) {
350 let info = explorer.info_db[explorer.position];
351 if (info != "(none)") {
352 this.inputEl.value = info;
353 this.recalc_input_lines();
356 if (mode == mode_login) {
357 if (this.login_name) {
358 server.send(['LOGIN', this.login_name]);
360 this.log_msg("? need login name");
362 } else if (mode == mode_portal && explorer.position in game.portals) {
363 let portal = game.portals[explorer.position]
364 this.inputEl.value = portal;
365 this.recalc_input_lines();
366 } else if (mode == mode_teleport) {
367 tui.log_msg("@ May teleport to: " + tui.teleport_target);
368 tui.log_msg("@ Enter 'YES!' to entusiastically affirm.");
372 empty_input: function(str) {
373 this.inputEl.value = "";
374 if (this.mode.has_input_prompt) {
375 this.recalc_input_lines();
377 this.height_input = 0;
380 recalc_input_lines: function() {
381 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
382 this.height_input = this.input_lines.length;
384 msg_into_lines_of_width: function(msg, width) {
387 for (let i = 0, x = 0; i < msg.length; i++, x++) {
388 if (x >= width || msg[i] == "\n") {
393 if (msg[i] != "\n") {
400 log_msg: function(msg) {
402 while (this.log.length > 100) {
407 log_help: function() {
408 let movement_keys_desc = Object.keys(this.movement_keys).join(',');
409 this.log_msg("HELP:");
410 this.log_msg("chat mode commands:");
411 this.log_msg(" /nick NAME - re-name yourself to NAME");
412 this.log_msg(" /msg USER TEXT - send TEXT to USER");
413 this.log_msg(" /help - show this help");
414 this.log_msg(" /" + this.keys.switch_to_play + " or /play - switch to play mode");
415 this.log_msg(" /" + this.keys.switch_to_study + " or /study - switch to study mode");
416 this.log_msg("commands common to study and play mode:");
417 if (game.tasks.includes('MOVE')) {
418 this.log_msg(" " + movement_keys_desc + " - move");
420 this.log_msg(" " + this.keys.switch_to_chat + " - switch to chat mode");
421 this.log_msg("commands specific to play mode:");
422 if (game.tasks.includes('WRITE')) {
423 this.log_msg(" " + this.keys.switch_to_edit + " - write following ASCII character");
425 if (game.tasks.includes('FLATTEN_SURROUNDINGS')) {
426 this.log_msg(" " + this.keys.flatten + " - flatten surroundings");
428 this.log_msg(" " + this.keys.switch_to_study + " - switch to study mode");
429 this.log_msg("commands specific to study mode:");
430 if (!game.tasks.includes('MOVE')) {
431 this.log_msg(" " + movement_keys_desc + " - move");
433 this.log_msg(" " + this.keys.switch_to_annotate + " - annotate terrain");
434 this.log_msg(" " + this.keys.switch_to_play + " - switch to play mode");
436 draw_map: function() {
437 let map_lines_split = [];
439 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
440 if (j == game.map_size[1]) {
441 map_lines_split.push(line);
445 line.push(game.map[i]);
447 map_lines_split.push(line);
448 for (const thing_id in game.things) {
449 let t = game.things[thing_id];
450 map_lines_split[t.position[0]][t.position[1]] = '@';
452 if (tui.mode.shows_info) {
453 map_lines_split[explorer.position[0]][explorer.position[1]] = '?';
456 if (game.map_geometry == 'Square') {
457 for (let line_split of map_lines_split) {
458 map_lines.push(line_split.join(' '));
460 } else if (game.map_geometry == 'Hex') {
462 for (let line_split of map_lines_split) {
463 map_lines.push(' '.repeat(indent) + line_split.join(' '));
471 let window_center = [terminal.rows / 2, this.window_width / 2];
472 let player = game.things[game.player_id];
473 let center_position = [player.position[0], player.position[1]];
474 if (tui.mode.shows_info) {
475 center_position = [explorer.position[0], explorer.position[1]];
477 center_position[1] = center_position[1] * 2;
478 let offset = [center_position[0] - window_center[0],
479 center_position[1] - window_center[1]]
480 if (game.map_geometry == 'Hex' && offset[0] % 2) {
483 let term_y = Math.max(0, -offset[0]);
484 let term_x = Math.max(0, -offset[1]);
485 let map_y = Math.max(0, offset[0]);
486 let map_x = Math.max(0, offset[1]);
487 for (; term_y < terminal.rows && map_y < game.map_size[0]; term_y++, map_y++) {
488 let to_draw = map_lines[map_y].slice(map_x, this.window_width + offset[1]);
489 terminal.write(term_y, term_x, to_draw);
492 draw_mode_line: function() {
493 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
495 draw_turn_line: function(n) {
496 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
498 draw_history: function() {
499 let log_display_lines = [];
500 for (let line of this.log) {
501 log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
503 for (let y = terminal.rows - 1 - this.height_input,
504 i = log_display_lines.length - 1;
505 y >= this.height_header && i >= 0;
507 terminal.write(y, this.window_width, log_display_lines[i]);
510 draw_info: function() {
511 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
512 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
513 terminal.write(y, this.window_width, lines[i]);
516 draw_input: function() {
517 if (this.mode.has_input_prompt) {
518 for (let y = terminal.rows - this.height_input, i = 0; i < this.input_lines.length; y++, i++) {
519 terminal.write(y, this.window_width, this.input_lines[i]);
523 full_refresh: function() {
524 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
525 if (this.mode.is_intro) {
529 if (game.turn_complete) {
531 this.draw_turn_line();
533 this.draw_mode_line();
534 if (this.mode.shows_info) {
550 this.map_size = [0,0];
555 get_thing: function(id_, create_if_not_found=false) {
556 if (id_ in game.things) {
557 return game.things[id_];
558 } else if (create_if_not_found) {
559 let t = new Thing([0,0]);
560 game.things[id_] = t;
564 move: function(start_position, direction) {
565 let target = [start_position[0], start_position[1]];
566 if (direction == 'LEFT') {
568 } else if (direction == 'RIGHT') {
570 } else if (game.map_geometry == 'Square') {
571 if (direction == 'UP') {
573 } else if (direction == 'DOWN') {
576 } else if (game.map_geometry == 'Hex') {
577 let start_indented = start_position[0] % 2;
578 if (direction == 'UPLEFT') {
580 if (!start_indented) {
583 } else if (direction == 'UPRIGHT') {
585 if (start_indented) {
588 } else if (direction == 'DOWNLEFT') {
590 if (!start_indented) {
593 } else if (direction == 'DOWNRIGHT') {
595 if (start_indented) {
600 if (target[0] < 0 || target[1] < 0 ||
601 target[0] >= this.map_size[0] || target[1] >= this.map_size[1]) {
611 server.init(websocket_location);
616 move: function(direction) {
617 let target = game.move(this.position, direction);
619 this.position = target
623 terminal.blink_screen();
626 update_info_db: function(yx, str) {
627 this.info_db[yx] = str;
628 if (tui.mode == mode_study) {
632 empty_info_db: function() {
634 if (tui.mode == mode_study) {
638 query_info: function() {
639 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
641 get_info: function() {
643 let position_i = this.position[0] * game.map_size[1] + this.position[1];
644 info += "TERRAIN: " + game.map[position_i] + "\n";
645 for (let t_id in game.things) {
646 let t = game.things[t_id];
647 if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
650 info += ": " + t.name_;
655 if (this.position in game.portals) {
656 info += "PORTAL: " + game.portals[this.position] + "\n";
658 if (this.position in this.info_db) {
659 info += "ANNOTATIONS: " + this.info_db[this.position];
665 annotate: function(msg) {
666 if (msg.length == 0) {
667 msg = " "; // triggers annotation deletion
669 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
671 set_portal: function(msg) {
672 if (msg.length == 0) {
673 msg = " "; // triggers portal deletion
675 server.send(["PORTAL", unparser.to_yx(explorer.position), msg]);
679 tui.inputEl.addEventListener('input', (event) => {
680 if (tui.mode.has_input_prompt) {
681 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
682 if (tui.inputEl.value.length > max_length) {
683 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
685 tui.recalc_input_lines();
687 } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
688 server.send(["TASK:WRITE", tui.inputEl.value[0]]);
689 tui.switch_mode(mode_play);
690 } else if (tui.mode == mode_teleport) {
691 if (['Y', 'y'].includes(tui.inputEl.value[0])) {
692 server.reconnect_to(tui.teleport_target);
694 tui.log_msg("@ teleportation aborted");
695 tui.switch_mode(mode_play);
699 tui.inputEl.addEventListener('keydown', (event) => {
700 if (event.key == 'Enter') {
701 event.preventDefault();
703 if (tui.mode == mode_login && event.key == 'Enter') {
704 tui.login_name = tui.inputEl.value;
705 server.send(['LOGIN', tui.inputEl.value]);
707 } else if (tui.mode == mode_portal && event.key == 'Enter') {
708 explorer.set_portal(tui.inputEl.value);
709 tui.switch_mode(mode_study, true);
710 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
711 explorer.annotate(tui.inputEl.value);
712 tui.switch_mode(mode_study, true);
713 } else if (tui.mode == mode_teleport && event.key == 'Enter') {
714 if (tui.inputEl.value == 'YES!') {
715 server.reconnect_to(tui.teleport_target);
717 tui.log_msg('@ teleport aborted');
718 tui.switch_mode(mode_play);
720 } else if (tui.mode == mode_chat && event.key == 'Enter') {
721 let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
722 if (tokens.length > 0 && tokens[0].length > 0) {
723 if (tui.inputEl.value[0][0] == '/') {
724 if (tokens[0].slice(1) == 'play' || tokens[0][1] == tui.keys.switch_to_play) {
725 tui.switch_mode(mode_play);
726 } else if (tokens[0].slice(1) == 'study' || tokens[0][1] == tui.keys.switch_to_study) {
727 tui.switch_mode(mode_study);
728 } else if (tokens[0].slice(1) == 'help') {
730 } else if (tokens[0].slice(1) == 'nick') {
731 if (tokens.length > 1) {
732 server.send(['NICK', tokens[1]]);
734 tui.log_msg('? need new name');
736 } else if (tokens[0].slice(1) == 'msg') {
737 if (tokens.length > 2) {
738 let msg = tui.inputEl.value.slice(token_starts[2]);
739 server.send(['QUERY', tokens[1], msg]);
741 tui.log_msg('? need message target and message');
743 } else if (tokens[0].slice(1) == 'reconnect') {
744 if (tokens.length > 1) {
745 server.reconnect_to(tokens[1]);
750 tui.log_msg('? unknown command');
753 server.send(['ALL', tui.inputEl.value]);
755 } else if (tui.inputEl.valuelength > 0) {
756 server.send(['ALL', tui.inputEl.value]);
760 } else if (tui.mode == mode_play) {
761 if (event.key === tui.keys.switch_to_chat) {
762 event.preventDefault();
763 tui.switch_mode(mode_chat);
764 } else if (event.key === tui.keys.switch_to_edit
765 && game.tasks.includes('WRITE')) {
766 event.preventDefault();
767 tui.switch_mode(mode_edit);
768 } else if (event.key === tui.keys.switch_to_study) {
769 tui.switch_mode(mode_study);
770 } else if (event.key === tui.keys.flatten
771 && game.tasks.includes('FLATTEN_SURROUNDINGS')) {
772 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
773 } else if (event.key in tui.movement_keys
774 && game.tasks.includes('MOVE')) {
775 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
777 } else if (tui.mode == mode_study) {
778 if (event.key === tui.keys.switch_to_chat) {
779 event.preventDefault();
780 tui.switch_mode(mode_chat);
781 } else if (event.key == tui.keys.switch_to_play) {
782 tui.switch_mode(mode_play);
783 } else if (event.key === tui.keys.switch_to_portal) {
784 event.preventDefault();
785 tui.switch_mode(mode_portal);
786 } else if (event.key in tui.movement_keys) {
787 explorer.move(tui.movement_keys[event.key]);
788 } else if (event.key === tui.keys.switch_to_annotate) {
789 event.preventDefault();
790 tui.switch_mode(mode_annotate);
795 rows_selector.addEventListener('input', function() {
796 if (rows_selector.value % 4 != 0) {
799 window.localStorage.setItem(rows_selector.id, rows_selector.value);
800 terminal.initialize();
803 cols_selector.addEventListener('input', function() {
804 if (cols_selector.value % 4 != 0) {
807 window.localStorage.setItem(cols_selector.id, cols_selector.value);
808 terminal.initialize();
809 tui.window_width = terminal.cols / 2,
812 for (let key_selector of key_selectors) {
813 key_selector.addEventListener('input', function() {
814 window.localStorage.setItem(key_selector.id, key_selector.value);
818 window.setInterval(function() {
819 if (!(['input', 'n_cols', 'n_rows'].includes(document.activeElement.id)
820 || document.activeElement.id.startsWith('key_'))) {