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>
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 = "ws://localhost:8000";
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);
176 tui.log_msg("@ server connected! :)");
177 tui.switch_mode(mode_login);
179 this.websocket.onclose = function(event) {
180 tui.log_msg("@ server disconnected :(");
181 tui.log_msg("@ hint: try the '/reconnect' command");
183 this.websocket.onmessage = this.handle_event;
185 reconnect: function() {
186 this.reconnect_to(this.url);
188 reconnect_to: function(url) {
189 this.websocket.close();
192 send: function(tokens) {
193 this.websocket.send(unparser.untokenize(tokens));
195 handle_event: function(event) {
196 let tokens = parser.tokenize(event.data)[0];
197 if (tokens[0] === 'TURN') {
198 game.turn_complete = false;
201 game.turn = parseInt(tokens[1]);
202 } else if (tokens[0] === 'THING_POS') {
203 game.get_thing(tokens[1], true).position = parser.parse_yx(tokens[2]);
204 } else if (tokens[0] === 'THING_NAME') {
205 game.get_thing(tokens[1], true).name_ = tokens[2];
206 } else if (tokens[0] === 'MAP') {
207 game.map_geometry = tokens[1];
209 game.map_size = parser.parse_yx(tokens[2]);
211 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
212 game.turn_complete = true;
213 explorer.empty_info_db();
214 if (tui.mode == mode_post_login_wait) {
215 tui.switch_mode(mode_play);
217 } else if (tui.mode == mode_study) {
218 explorer.query_info();
220 let t = game.get_thing(game.player_id);
221 if (t.position in game.portals) {
222 tui.teleport_target = game.portals[t.position];
223 tui.switch_mode(mode_teleport);
227 } else if (tokens[0] === 'CHAT') {
228 tui.log_msg('# ' + tokens[1], 1);
229 } else if (tokens[0] === 'PLAYER_ID') {
230 game.player_id = parseInt(tokens[1]);
231 } else if (tokens[0] === 'LOGIN_OK') {
232 this.send(['GET_GAMESTATE']);
233 tui.switch_mode(mode_post_login_wait);
234 } else if (tokens[0] === 'PORTAL') {
235 let position = parser.parse_yx(tokens[1]);
236 game.portals[position] = tokens[2];
237 } else if (tokens[0] === 'ANNOTATION') {
238 let position = parser.parse_yx(tokens[1]);
239 explorer.update_info_db(position, tokens[2]);
240 } else if (tokens[0] === 'UNHANDLED_INPUT') {
241 tui.log_msg('? unknown command');
242 } else if (tokens[0] === 'PLAY_ERROR') {
243 terminal.blink_screen();
244 } else if (tokens[0] === 'ARGUMENT_ERROR') {
245 tui.log_msg('? syntax error: ' + tokens[1]);
246 } else if (tokens[0] === 'GAME_ERROR') {
247 tui.log_msg('? game error: ' + tokens[1]);
248 } else if (tokens[0] === 'PONG') {
251 tui.log_msg('? unhandled input: ' + event.data);
257 quote: function(str) {
259 for (let i = 0; i < str.length; i++) {
261 if (['"', '\\'].includes(c)) {
267 return quoted.join('');
269 to_yx: function(yx_coordinate) {
270 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
272 untokenize: function(tokens) {
273 let quoted_tokens = [];
274 for (let token of tokens) {
275 quoted_tokens.push(this.quote(token));
277 return quoted_tokens.join(" ");
282 constructor(name, has_input_prompt=false, shows_info=false, is_intro=false) {
284 this.has_input_prompt = has_input_prompt;
285 this.shows_info= shows_info;
286 this.is_intro = is_intro;
289 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
290 let mode_login = new Mode('login', true, false, true);
291 let mode_post_login_wait = new Mode('waiting for game world', false, false, true);
292 let mode_chat = new Mode('chat / write messages to players', true, false);
293 let mode_annotate = new Mode('add message to map tile', true, true);
294 let mode_play = new Mode('play / move around', false, false);
295 let mode_study = new Mode('check map tiles for messages', false, true);
296 let mode_edit = new Mode('write ASCII char to map tile', false, false);
297 let mode_teleport = new Mode('teleport away?', true);
298 let mode_portal = new Mode('add portal to map tile', true, true);
301 mode: mode_waiting_for_server,
305 window_width: terminal.cols / 2,
310 this.inputEl = document.getElementById("input");
311 this.inputEl.focus();
312 this.recalc_input_lines();
313 this.height_header = this.height_turn_line + this.height_mode_line;
314 this.log_msg("@ waiting for server connection ...");
317 init_keys: function() {
319 for (let key_selector of key_selectors) {
320 this.keys[key_selector.id.slice(4)] = key_selector.value;
322 if (game.map_geometry == 'Square') {
323 this.movement_keys = {
324 [this.keys.square_move_up]: 'UP',
325 [this.keys.square_move_left]: 'LEFT',
326 [this.keys.square_move_down]: 'DOWN',
327 [this.keys.square_move_right]: 'RIGHT'
329 } else if (game.map_geometry == 'Hex') {
330 this.movement_keys = {
331 [this.keys.hex_move_upleft]: 'UPLEFT',
332 [this.keys.hex_move_upright]: 'UPRIGHT',
333 [this.keys.hex_move_right]: 'RIGHT',
334 [this.keys.hex_move_downright]: 'DOWNRIGHT',
335 [this.keys.hex_move_downleft]: 'DOWNLEFT',
336 [this.keys.hex_move_left]: 'LEFT'
340 switch_mode: function(mode, keep_pos=false) {
341 if (mode == mode_study && !keep_pos && game.player_id in game.things) {
342 explorer.position = game.things[game.player_id].position;
346 if (mode == mode_annotate && explorer.position in explorer.info_db) {
347 let info = explorer.info_db[explorer.position];
348 if (info != "(none)") {
349 this.inputEl.value = info;
350 this.recalc_input_lines();
353 if (mode == mode_login) {
354 if (this.login_name) {
355 server.send(['LOGIN', this.login_name]);
357 this.log_msg("? need login name");
359 } else if (mode == mode_portal && explorer.position in game.portals) {
360 let portal = game.portals[explorer.position]
361 this.inputEl.value = portal;
362 this.recalc_input_lines();
363 } else if (mode == mode_teleport) {
364 tui.log_msg("@ May teleport to: " + tui.teleport_target);
365 tui.log_msg("@ Enter 'YES!' to entusiastically affirm.");
369 empty_input: function(str) {
370 this.inputEl.value = "";
371 if (this.mode.has_input_prompt) {
372 this.recalc_input_lines();
374 this.height_input = 0;
377 recalc_input_lines: function() {
378 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
379 this.height_input = this.input_lines.length;
381 msg_into_lines_of_width: function(msg, width) {
384 for (let i = 0, x = 0; i < msg.length; i++, x++) {
385 if (x >= width || msg[i] == "\n") {
390 if (msg[i] != "\n") {
397 log_msg: function(msg) {
399 while (this.log.length > 100) {
404 log_help: function() {
405 let movement_keys_desc = Object.keys(this.movement_keys).join(',');
406 this.log_msg("HELP:");
407 this.log_msg("chat mode commands:");
408 this.log_msg(" /nick NAME - re-name yourself to NAME");
409 this.log_msg(" /msg USER TEXT - send TEXT to USER");
410 this.log_msg(" /help - show this help");
411 this.log_msg(" /" + this.keys.switch_to_play + " or /play - switch to play mode");
412 this.log_msg(" /" + this.keys.switch_to_study + " or /study - switch to study mode");
413 this.log_msg("commands common to study and play mode:");
414 this.log_msg(" " + movement_keys_desc + " - move");
415 this.log_msg(" " + this.keys.switch_to_chat + " - switch to chat mode");
416 this.log_msg("commands specific to play mode:");
417 this.log_msg(" " + this.keys.switch_to_edit + " - write following ASCII character");
418 this.log_msg(" " + this.keys.flatten + " - flatten surroundings");
419 this.log_msg(" " + this.keys.switch_to_study + " - switch to study mode");
420 this.log_msg("commands specific to study mode:");
421 this.log_msg(" " + this.keys.switch_to_annotate + " - annotate terrain");
422 this.log_msg(" " + this.keys.switch_to_play + " - switch to play mode");
424 draw_map: function() {
425 let map_lines_split = [];
427 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
428 if (j == game.map_size[1]) {
429 map_lines_split.push(line);
433 line.push(game.map[i]);
435 map_lines_split.push(line);
436 for (const thing_id in game.things) {
437 let t = game.things[thing_id];
438 map_lines_split[t.position[0]][t.position[1]] = '@';
440 if (tui.mode.shows_info) {
441 map_lines_split[explorer.position[0]][explorer.position[1]] = '?';
444 if (game.map_geometry == 'Square') {
445 for (let line_split of map_lines_split) {
446 map_lines.push(line_split.join(' '));
448 } else if (game.map_geometry == 'Hex') {
450 for (let line_split of map_lines_split) {
451 map_lines.push(' '.repeat(indent) + line_split.join(' '));
459 let window_center = [terminal.rows / 2, this.window_width / 2];
460 let player = game.things[game.player_id];
461 let center_position = [player.position[0], player.position[1]];
462 if (tui.mode.shows_info) {
463 center_position = [explorer.position[0], explorer.position[1]];
465 center_position[1] = center_position[1] * 2;
466 let offset = [center_position[0] - window_center[0],
467 center_position[1] - window_center[1]]
468 if (game.map_geometry == 'Hex' && offset[0] % 2) {
471 let term_y = Math.max(0, -offset[0]);
472 let term_x = Math.max(0, -offset[1]);
473 let map_y = Math.max(0, offset[0]);
474 let map_x = Math.max(0, offset[1]);
475 for (; term_y < terminal.rows && map_y < game.map_size[0]; term_y++, map_y++) {
476 let to_draw = map_lines[map_y].slice(map_x, this.window_width + offset[1]);
477 terminal.write(term_y, term_x, to_draw);
480 draw_mode_line: function() {
481 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
483 draw_turn_line: function(n) {
484 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
486 draw_history: function() {
487 let log_display_lines = [];
488 for (let line of this.log) {
489 log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
491 for (let y = terminal.rows - 1 - this.height_input,
492 i = log_display_lines.length - 1;
493 y >= this.height_header && i >= 0;
495 terminal.write(y, this.window_width, log_display_lines[i]);
498 draw_info: function() {
499 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
500 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
501 terminal.write(y, this.window_width, lines[i]);
504 draw_input: function() {
505 if (this.mode.has_input_prompt) {
506 for (let y = terminal.rows - this.height_input, i = 0; i < this.input_lines.length; y++, i++) {
507 terminal.write(y, this.window_width, this.input_lines[i]);
511 full_refresh: function() {
512 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
513 if (this.mode.is_intro) {
517 if (game.turn_complete) {
519 this.draw_turn_line();
521 this.draw_mode_line();
522 if (this.mode.shows_info) {
538 this.map_size = [0,0];
542 get_thing: function(id_, create_if_not_found=false) {
543 if (id_ in game.things) {
544 return game.things[id_];
545 } else if (create_if_not_found) {
546 let t = new Thing([0,0]);
547 game.things[id_] = t;
551 move: function(start_position, direction) {
552 let target = [start_position[0], start_position[1]];
553 if (direction == 'LEFT') {
555 } else if (direction == 'RIGHT') {
557 } else if (game.map_geometry == 'Square') {
558 if (direction == 'UP') {
560 } else if (direction == 'DOWN') {
563 } else if (game.map_geometry == 'Hex') {
564 let start_indented = start_position[0] % 2;
565 if (direction == 'UPLEFT') {
567 if (!start_indented) {
570 } else if (direction == 'UPRIGHT') {
572 if (start_indented) {
575 } else if (direction == 'DOWNLEFT') {
577 if (!start_indented) {
580 } else if (direction == 'DOWNRIGHT') {
582 if (start_indented) {
587 if (target[0] < 0 || target[1] < 0 ||
588 target[0] >= this.map_size[0] || target[1] >= this.map_size[1]) {
598 server.init(websocket_location);
603 move: function(direction) {
604 let target = game.move(this.position, direction);
606 this.position = target
610 terminal.blink_screen();
613 update_info_db: function(yx, str) {
614 this.info_db[yx] = str;
615 if (tui.mode == mode_study) {
619 empty_info_db: function() {
621 if (tui.mode == mode_study) {
625 query_info: function() {
626 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
628 get_info: function() {
630 let position_i = this.position[0] * game.map_size[1] + this.position[1];
631 info += "TERRAIN: " + game.map[position_i] + "\n";
632 for (let t_id in game.things) {
633 let t = game.things[t_id];
634 if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
637 info += ": " + t.name_;
642 if (this.position in game.portals) {
643 info += "PORTAL: " + game.portals[this.position] + "\n";
645 if (this.position in this.info_db) {
646 info += "ANNOTATIONS: " + this.info_db[this.position];
652 annotate: function(msg) {
653 if (msg.length == 0) {
654 msg = " "; // triggers annotation deletion
656 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
658 set_portal: function(msg) {
659 if (msg.length == 0) {
660 msg = " "; // triggers portal deletion
662 server.send(["PORTAL", unparser.to_yx(explorer.position), msg]);
666 tui.inputEl.addEventListener('input', (event) => {
667 if (tui.mode.has_input_prompt) {
668 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
669 if (tui.inputEl.value.length > max_length) {
670 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
672 tui.recalc_input_lines();
674 } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
675 server.send(["TASK:WRITE", tui.inputEl.value[0]]);
676 tui.switch_mode(mode_play);
677 } else if (tui.mode == mode_teleport) {
678 if (['Y', 'y'].includes(tui.inputEl.value[0])) {
679 server.reconnect_to(tui.teleport_target);
681 tui.log_msg("@ teleportation aborted");
682 tui.switch_mode(mode_play);
686 tui.inputEl.addEventListener('keydown', (event) => {
687 if (event.key == 'Enter') {
688 event.preventDefault();
690 if (tui.mode == mode_login && event.key == 'Enter') {
691 tui.login_name = tui.inputEl.value;
692 server.send(['LOGIN', tui.inputEl.value]);
694 } else if (tui.mode == mode_portal && event.key == 'Enter') {
695 explorer.set_portal(tui.inputEl.value);
696 tui.switch_mode(mode_study, true);
697 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
698 explorer.annotate(tui.inputEl.value);
699 tui.switch_mode(mode_study, true);
700 } else if (tui.mode == mode_teleport && event.key == 'Enter') {
701 if (tui.inputEl.value == 'YES!') {
702 server.reconnect_to(tui.teleport_target);
704 tui.log_msg('@ teleport aborted');
705 tui.switch_mode(mode_play);
707 } else if (tui.mode == mode_chat && event.key == 'Enter') {
708 let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
709 if (tokens.length > 0 && tokens[0].length > 0) {
710 if (tui.inputEl.value[0][0] == '/') {
711 if (tokens[0].slice(1) == 'play' || tokens[0].slice(1) == 'P') {
712 tui.switch_mode(mode_play);
713 } else if (tokens[0].slice(1) == 'study' || tokens[0].slice(1) == '?') {
714 tui.switch_mode(mode_study);
715 } else if (tokens[0].slice(1) == 'help') {
717 } else if (tokens[0].slice(1) == 'nick') {
718 if (tokens.length > 1) {
719 server.send(['LOGIN', tokens[1]]);
721 tui.log_msg('? need login name');
723 } else if (tokens[0].slice(1) == 'msg') {
724 if (tokens.length > 2) {
725 let msg = tui.inputEl.value.slice(token_starts[2]);
726 server.send(['QUERY', tokens[1], msg]);
728 tui.log_msg('? need message target and message');
730 } else if (tokens[0].slice(1) == 'reconnect') {
731 if (tokens.length > 1) {
732 server.reconnect_to(tokens[1]);
737 tui.log_msg('? unknown command');
740 server.send(['ALL', tui.inputEl.value]);
742 } else if (tui.inputEl.valuelength > 0) {
743 server.send(['ALL', tui.inputEl.value]);
747 } else if (tui.mode == mode_play) {
748 if (event.key === tui.keys.switch_to_chat) {
749 event.preventDefault();
750 tui.switch_mode(mode_chat);
751 } else if (event.key === tui.keys.switch_to_edit) {
752 event.preventDefault();
753 tui.switch_mode(mode_edit);
754 } else if (event.key === tui.keys.switch_to_study) {
755 tui.switch_mode(mode_study);
756 } else if (event.key === tui.keys.flatten) {
757 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
758 } else if (event.key in tui.movement_keys) {
759 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
761 } else if (tui.mode == mode_study) {
762 if (event.key === tui.keys.switch_to_chat) {
763 event.preventDefault();
764 tui.switch_mode(mode_chat);
765 } else if (event.key == tui.keys.switch_to_play) {
766 tui.switch_mode(mode_play);
767 } else if (event.key === tui.keys.switch_to_portal) {
768 event.preventDefault();
769 tui.switch_mode(mode_portal);
770 } else if (event.key in tui.movement_keys) {
771 explorer.move(tui.movement_keys[event.key]);
772 } else if (event.key === tui.keys.switch_to_annotate) {
773 event.preventDefault();
774 tui.switch_mode(mode_annotate);
779 rows_selector.addEventListener('input', function() {
780 if (rows_selector.value % 4 != 0) {
783 window.localStorage.setItem(rows_selector.id, rows_selector.value);
784 terminal.initialize();
787 cols_selector.addEventListener('input', function() {
788 if (cols_selector.value % 4 != 0) {
791 window.localStorage.setItem(cols_selector.id, cols_selector.value);
792 terminal.initialize();
793 tui.window_width = terminal.cols / 2,
796 for (let key_selector of key_selectors) {
797 key_selector.addEventListener('input', function() {
798 window.localStorage.setItem(key_selector.id, key_selector.value);
802 window.setInterval(function() {
803 if (!(['input', 'n_cols', 'n_rows'].includes(document.activeElement.id)
804 || document.activeElement.id.startsWith('key_'))) {