7 movement: <select id="movement_keys" name="movement_keys" >
8 <option value="alphabetic" selected>w/a/s/d (square grid) or e,d,c,x,s,w (hex grid)</option>
9 <option value="arrow_or_numpad">arrow keys (square grid) or numpad (hex grid)</option>
11 rows: <input id="n_rows" type="number" step=2 min=10 value=24 />
12 cols: <input id="n_cols" type="number" step=4 min=20 value=80 />
13 command character: <select id="command_char"" >
14 <option value=":" selected>:</option>
15 <option value="/">/</option>
18 <pre id="terminal" style="display: inline-block;"></pre>
19 <textarea id="input" style="opacity: 0; width: 0px;"></textarea>
22 let websocket_location = "ws://localhost:8000";
24 let movement_keys_selector = document.getElementById("movement_keys");
25 let rows_selector = document.getElementById("n_rows");
26 let cols_selector = document.getElementById("n_cols");
27 let command_char_selector = document.getElementById("command_char");
32 initialize: function() {
33 this.rows = rows_selector.value;
34 this.cols = cols_selector.value;
35 this.pre_el = document.getElementById("terminal");
36 this.pre_el.style.color = this.foreground;
37 this.pre_el.style.backgroundColor = this.background;
40 for (let y = 0, x = 0; y <= this.rows; x++) {
44 this.content.push(line);
53 blink_screen: function() {
54 this.pre_el.style.color = this.background;
55 this.pre_el.style.backgroundColor = this.foreground;
57 this.pre_el.style.color = this.foreground;
58 this.pre_el.style.backgroundColor = this.background;
63 for (let y = 0; y < this.rows; y++) {
64 let line = this.content[y].join('');
65 pre_string += line + '\n';
67 this.pre_el.textContent = pre_string;
69 write: function(start_y, start_x, msg) {
70 for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
71 this.content[start_y][x] = msg[i];
74 drawBox: function(start_y, start_x, height, width) {
75 let end_y = start_y + height;
76 let end_x = start_x + width;
77 for (let y = start_y, x = start_x; y < this.rows; x++) {
85 this.content[y][x] = ' ';
89 terminal.initialize();
92 tokenize: function(str) {
98 for (let i = 0; i < str.length; i++) {
104 } else if (c == '\\') {
106 } else if (c == '"') {
111 } else if (c == '"') {
113 } else if (c === ' ') {
114 if (token.length > 0) {
123 if (token.length > 0) {
126 let token_starts = [];
127 for (let i = 0; i < token_ends.length; i++) {
128 token_starts.push(token_ends[i] - tokens[i].length);
130 return [tokens, token_starts];
132 parse_yx: function(position_string) {
133 let coordinate_strings = position_string.split(',')
134 let position = [0, 0];
135 position[0] = parseInt(coordinate_strings[0].slice(2));
136 position[1] = parseInt(coordinate_strings[1].slice(2));
148 init: function(url) {
150 this.websocket = new WebSocket(this.url);
151 this.websocket.onopen = function(event) {
152 window.setInterval(function() { server.send(['PING']) }, 30000);
153 tui.log_msg("@ server connected! :)");
154 tui.switch_mode(mode_login);
156 this.websocket.onclose = function(event) {
157 tui.log_msg("@ server disconnected :(");
158 tui.log_msg("@ hint: try the '" + command_char_selector.value + "reconnect' command");
160 this.websocket.onmessage = this.handle_event;
162 reconnect: function() {
163 this.reconnect_to(this.url);
165 reconnect_to: function(url) {
166 this.websocket.close();
169 send: function(tokens) {
170 this.websocket.send(unparser.untokenize(tokens));
172 handle_event: function(event) {
173 let tokens = parser.tokenize(event.data)[0];
174 if (tokens[0] === 'TURN') {
175 game.turn_complete = false;
178 game.turn = parseInt(tokens[1]);
179 } else if (tokens[0] === 'THING_POS') {
180 game.get_thing(tokens[1], true).position = parser.parse_yx(tokens[2]);
181 } else if (tokens[0] === 'THING_NAME') {
182 game.get_thing(tokens[1], true).name_ = tokens[2];
183 } else if (tokens[0] === 'MAP') {
184 game.map_geometry = tokens[1];
186 game.map_size = parser.parse_yx(tokens[2]);
188 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
189 game.turn_complete = true;
190 explorer.empty_info_db();
191 if (tui.mode == mode_post_login_wait) {
192 tui.switch_mode(mode_play);
194 } else if (tui.mode == mode_study) {
195 explorer.query_info();
197 let t = game.get_thing(game.player_id);
198 if (t.position in game.portals) {
199 tui.teleport_target = game.portals[t.position];
200 tui.switch_mode(mode_teleport);
204 } else if (tokens[0] === 'CHAT') {
205 tui.log_msg('# ' + tokens[1], 1);
206 } else if (tokens[0] === 'PLAYER_ID') {
207 game.player_id = parseInt(tokens[1]);
208 } else if (tokens[0] === 'LOGIN_OK') {
209 this.send(['GET_GAMESTATE']);
210 tui.switch_mode(mode_post_login_wait);
211 } else if (tokens[0] === 'PORTAL') {
212 let position = parser.parse_yx(tokens[1]);
213 game.portals[position] = tokens[2];
214 } else if (tokens[0] === 'ANNOTATION') {
215 let position = parser.parse_yx(tokens[1]);
216 explorer.update_info_db(position, tokens[2]);
217 } else if (tokens[0] === 'UNHANDLED_INPUT') {
218 tui.log_msg('? unknown command');
219 } else if (tokens[0] === 'PLAY_ERROR') {
220 terminal.blink_screen();
221 } else if (tokens[0] === 'ARGUMENT_ERROR') {
222 tui.log_msg('? syntax error: ' + tokens[1]);
223 } else if (tokens[0] === 'GAME_ERROR') {
224 tui.log_msg('? game error: ' + tokens[1]);
225 } else if (tokens[0] === 'PONG') {
228 tui.log_msg('? unhandled input: ' + event.data);
234 quote: function(str) {
236 for (let i = 0; i < str.length; i++) {
238 if (['"', '\\'].includes(c)) {
244 return quoted.join('');
246 to_yx: function(yx_coordinate) {
247 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
249 untokenize: function(tokens) {
250 let quoted_tokens = [];
251 for (let token of tokens) {
252 quoted_tokens.push(this.quote(token));
254 return quoted_tokens.join(" ");
259 constructor(name, has_input_prompt=false, shows_info=false, is_intro=false) {
261 this.has_input_prompt = has_input_prompt;
262 this.shows_info= shows_info;
263 this.is_intro = is_intro;
266 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
267 let mode_login = new Mode('login', true, false, true);
268 let mode_post_login_wait = new Mode('waiting for game world', false, false, true);
269 let mode_chat = new Mode('chat / write messages to players', true, false);
270 let mode_annotate = new Mode('add message to map tile', true, true);
271 let mode_play = new Mode('play / move around', false, false);
272 let mode_study = new Mode('check map tiles for messages', false, true);
273 let mode_edit = new Mode('write ASCII char to map tile', false, false);
274 let mode_teleport = new Mode('teleport away?', true);
275 let mode_portal = new Mode('add portal to map tile', true, true);
278 mode: mode_waiting_for_server,
282 window_width: terminal.cols / 2,
287 this.inputEl = document.getElementById("input");
288 this.inputEl.focus();
289 this.recalc_input_lines();
290 this.height_header = this.height_turn_line + this.height_mode_line;
291 this.log_msg("@ waiting for server connection ...");
294 init_wasd: function() {
296 if (movement_keys_selector.value == 'alphabetic') {
297 if (game.map_geometry == 'Square') {
298 this.movement_keys = {
304 tui.movement_keys_desc = 'w, a, s, d';
305 } else if (game.map_geometry == 'Hex') {
306 this.movement_keys = {
314 tui.movement_keys_desc = 'e, d, c, x, s, w';
316 } else if (movement_keys_selector.value == 'arrow_or_numpad') {
317 if (game.map_geometry == 'Square') {
318 this.movement_keys = {
322 'ArrowRight': 'RIGHT'
324 tui.movement_keys_desc = 'arrow keys';
325 } else if (game.map_geometry == 'Hex') {
326 this.movement_keys = {
334 tui.movement_keys_desc = 'numpad keys';
338 switch_mode: function(mode, keep_pos=false) {
339 if (mode == mode_study && !keep_pos && game.player_id in game.things) {
340 explorer.position = game.things[game.player_id].position;
344 if (mode == mode_annotate && explorer.position in explorer.info_db) {
345 let info = explorer.info_db[explorer.position];
346 if (info != "(none)") {
347 this.inputEl.value = info;
348 this.recalc_input_lines();
351 if (mode == mode_login) {
352 if (this.login_name) {
353 server.send(['LOGIN', this.login_name]);
355 this.log_msg("? need login name");
357 } else if (mode == mode_portal && explorer.position in game.portals) {
358 let portal = game.portals[explorer.position]
359 this.inputEl.value = portal;
360 this.recalc_input_lines();
361 } else if (mode == mode_teleport) {
362 tui.log_msg("@ May teleport to: " + tui.teleport_target);
363 tui.log_msg("@ Enter 'YES!' to entusiastically affirm.");
367 empty_input: function(str) {
368 this.inputEl.value = "";
369 if (this.mode.has_input_prompt) {
370 this.recalc_input_lines();
372 this.height_input = 0;
375 recalc_input_lines: function() {
376 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
377 this.height_input = this.input_lines.length;
379 msg_into_lines_of_width: function(msg, width) {
382 for (let i = 0, x = 0; i < msg.length; i++, x++) {
383 if (x >= width || msg[i] == "\n") {
388 if (msg[i] != "\n") {
395 log_msg: function(msg) {
397 while (this.log.length > 100) {
402 log_help: function() {
403 this.log_msg("HELP:");
404 this.log_msg("chat mode commands:");
405 this.log_msg(" " + command_char_selector.value + "nick NAME - re-name yourself to NAME");
406 this.log_msg(" " + command_char_selector.value + "msg USER TEXT - send TEXT to USER");
407 this.log_msg(" " + command_char_selector.value + "help - show this help");
408 this.log_msg(" " + command_char_selector.value + "P or " + command_char_selector.value + "play - switch to play mode");
409 this.log_msg(" " + command_char_selector.value + "? or " + command_char_selector.value + "study - switch to study mode");
410 this.log_msg("commands common to study and play mode:");
411 this.log_msg(" " + this.movement_keys_desc + " - move");
412 this.log_msg(" C - switch to chat mode");
413 this.log_msg("commands specific to play mode:");
414 this.log_msg(" E - write following ASCII character");
415 this.log_msg(" f - flatten surroundings");
416 this.log_msg(" ? - switch to study mode");
417 this.log_msg("commands specific to study mode:");
418 this.log_msg(" E - annotate terrain");
419 this.log_msg(" P - switch to play mode");
421 draw_map: function() {
422 let map_lines_split = [];
424 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
425 if (j == game.map_size[1]) {
426 map_lines_split.push(line);
430 line.push(game.map[i]);
432 map_lines_split.push(line);
433 for (const thing_id in game.things) {
434 let t = game.things[thing_id];
435 map_lines_split[t.position[0]][t.position[1]] = '@';
437 if (tui.mode.shows_info) {
438 map_lines_split[explorer.position[0]][explorer.position[1]] = '?';
441 if (game.map_geometry == 'Square') {
442 for (let line_split of map_lines_split) {
443 map_lines.push(line_split.join(''));
445 } else if (game.map_geometry == 'Hex') {
447 for (let line_split of map_lines_split) {
448 map_lines.push(' '.repeat(indent) + line_split.join(' '));
456 let window_center = [terminal.rows / 2, this.window_width / 2];
457 let player = game.things[game.player_id];
458 let center_position = [player.position[0], player.position[1]];
459 if (tui.mode.shows_info) {
460 center_position = [explorer.position[0], explorer.position[1]];
462 if (game.map_geometry == 'Hex') {
463 center_position[1] = center_position[1] * 2;
465 let offset = [center_position[0] - window_center[0],
466 center_position[1] - window_center[1]]
467 if (game.map_geometry == 'Hex' && offset[0] % 2) {
470 let term_y = Math.max(0, -offset[0]);
471 let term_x = Math.max(0, -offset[1]);
472 let map_y = Math.max(0, offset[0]);
473 let map_x = Math.max(0, offset[1]);
474 for (; term_y < terminal.rows && map_y < game.map_size[0]; term_y++, map_y++) {
475 let to_draw = map_lines[map_y].slice(map_x, this.window_width + offset[1]);
476 terminal.write(term_y, term_x, to_draw);
479 draw_mode_line: function() {
480 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
482 draw_turn_line: function(n) {
483 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
485 draw_history: function() {
486 let log_display_lines = [];
487 for (let line of this.log) {
488 log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
490 for (let y = terminal.rows - 1 - this.height_input,
491 i = log_display_lines.length - 1;
492 y >= this.height_header && i >= 0;
494 terminal.write(y, this.window_width, log_display_lines[i]);
497 draw_info: function() {
498 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
499 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
500 terminal.write(y, this.window_width, lines[i]);
503 draw_input: function() {
504 if (this.mode.has_input_prompt) {
505 for (let y = terminal.rows - this.height_input, i = 0; i < this.input_lines.length; y++, i++) {
506 terminal.write(y, this.window_width, this.input_lines[i]);
510 full_refresh: function() {
511 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
512 if (this.mode.is_intro) {
516 if (game.turn_complete) {
518 this.draw_turn_line();
520 this.draw_mode_line();
521 if (this.mode.shows_info) {
537 this.map_size = [0,0];
541 get_thing: function(id_, create_if_not_found=false) {
542 if (id_ in game.things) {
543 return game.things[id_];
544 } else if (create_if_not_found) {
545 let t = new Thing([0,0]);
546 game.things[id_] = t;
550 move: function(start_position, direction) {
551 let target = [start_position[0], start_position[1]];
552 if (direction == 'LEFT') {
554 } else if (direction == 'RIGHT') {
556 } else if (game.map_geometry == 'Square') {
557 if (direction == 'UP') {
559 } else if (direction == 'DOWN') {
562 } else if (game.map_geometry == 'Hex') {
563 let start_indented = start_position[0] % 2;
564 if (direction == 'UPLEFT') {
566 if (!start_indented) {
569 } else if (direction == 'UPRIGHT') {
571 if (start_indented) {
574 } else if (direction == 'DOWNLEFT') {
576 if (!start_indented) {
579 } else if (direction == 'DOWNRIGHT') {
581 if (start_indented) {
586 if (target[0] < 0 || target[1] < 0 ||
587 target[0] >= this.map_size[0] || target[1] >= this.map_size[1]) {
597 server.init(websocket_location);
602 move: function(direction) {
603 let target = game.move(this.position, direction);
605 this.position = target
609 terminal.blink_screen();
612 update_info_db: function(yx, str) {
613 this.info_db[yx] = str;
614 if (tui.mode == mode_study) {
618 empty_info_db: function() {
620 if (tui.mode == mode_study) {
624 query_info: function() {
625 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
627 get_info: function() {
629 let position_i = this.position[0] * game.map_size[1] + this.position[1];
630 info += "TERRAIN: " + game.map[position_i] + "\n";
631 for (let t_id in game.things) {
632 let t = game.things[t_id];
633 if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
636 info += ": " + t.name_;
641 if (this.position in game.portals) {
642 info += "PORTAL: " + game.portals[this.position] + "\n";
644 if (this.position in this.info_db) {
645 info += "ANNOTATIONS: " + this.info_db[this.position];
651 annotate: function(msg) {
652 if (msg.length == 0) {
653 msg = " "; // triggers annotation deletion
655 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
657 set_portal: function(msg) {
658 if (msg.length == 0) {
659 msg = " "; // triggers portal deletion
661 server.send(["PORTAL", unparser.to_yx(explorer.position), msg]);
665 tui.inputEl.addEventListener('input', (event) => {
666 if (tui.mode.has_input_prompt) {
667 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
668 if (tui.inputEl.value.length > max_length) {
669 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
671 tui.recalc_input_lines();
673 } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
674 server.send(["TASK:WRITE", tui.inputEl.value[0]]);
675 tui.switch_mode(mode_play);
676 } else if (tui.mode == mode_teleport) {
677 if (['Y', 'y'].includes(tui.inputEl.value[0])) {
678 server.reconnect_to(tui.teleport_target);
680 tui.log_msg("@ teleportation aborted");
681 tui.switch_mode(mode_play);
685 tui.inputEl.addEventListener('keydown', (event) => {
686 if (event.key == 'Enter') {
687 event.preventDefault();
689 if (tui.mode == mode_login && event.key == 'Enter') {
690 tui.login_name = tui.inputEl.value;
691 server.send(['LOGIN', tui.inputEl.value]);
693 } else if (tui.mode == mode_portal && event.key == 'Enter') {
694 explorer.set_portal(tui.inputEl.value);
695 tui.switch_mode(mode_study, true);
696 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
697 explorer.annotate(tui.inputEl.value);
698 tui.switch_mode(mode_study, true);
699 } else if (tui.mode == mode_teleport && event.key == 'Enter') {
700 if (tui.inputEl.value == 'YES!') {
701 server.reconnect_to(tui.teleport_target);
703 tui.log_msg('@ teleport aborted');
704 tui.switch_mode(mode_play);
706 } else if (tui.mode == mode_chat && event.key == 'Enter') {
707 let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
708 if (tokens.length > 0 && tokens[0].length > 0) {
709 if (tui.inputEl.value[0][0] == command_char_selector.value) {
710 if (tokens[0].slice(1) == 'play' || tokens[0].slice(1) == 'P') {
711 tui.switch_mode(mode_play);
712 } else if (tokens[0].slice(1) == 'study' || tokens[0].slice(1) == '?') {
713 tui.switch_mode(mode_study);
714 } else if (tokens[0].slice(1) == 'help') {
716 } else if (tokens[0].slice(1) == 'nick') {
717 if (tokens.length > 1) {
718 server.send(['LOGIN', tokens[1]]);
720 tui.log_msg('? need login name');
722 } else if (tokens[0].slice(1) == 'msg') {
723 if (tokens.length > 2) {
724 let msg = tui.inputEl.value.slice(token_starts[2]);
725 server.send(['QUERY', tokens[1], msg]);
727 tui.log_msg('? need message target and message');
729 } else if (tokens[0].slice(1) == 'reconnect') {
730 if (tokens.length > 1) {
731 server.reconnect_to(tokens[1]);
736 tui.log_msg('? unknown command');
739 server.send(['ALL', tui.inputEl.value]);
741 } else if (tui.inputEl.valuelength > 0) {
742 server.send(['ALL', tui.inputEl.value]);
746 } else if (tui.mode == mode_play) {
747 if (event.key === 'C') {
748 event.preventDefault();
749 tui.switch_mode(mode_chat);
750 } else if (event.key === 'E') {
751 event.preventDefault();
752 tui.switch_mode(mode_edit);
753 } else if (event.key === '?') {
754 tui.switch_mode(mode_study);
755 } else if (event.key === 'F1') {
757 } else if (event.key === 'f') {
758 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
759 } else if (event.key in tui.movement_keys) {
760 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
762 } else if (tui.mode == mode_study) {
763 if (event.key === 'C') {
764 event.preventDefault();
765 tui.switch_mode(mode_chat);
766 } else if (event.key == 'P') {
767 tui.switch_mode(mode_play);
768 } else if (event.key === 'p') {
769 event.preventDefault();
770 tui.switch_mode(mode_portal);
771 } else if (event.key in tui.movement_keys) {
772 explorer.move(tui.movement_keys[event.key]);
773 } else if (event.key === 'E') {
774 event.preventDefault();
775 tui.switch_mode(mode_annotate);
780 movement_keys_selector.addEventListener('input', function() {
783 rows_selector.addEventListener('input', function() {
784 if (rows_selector.value % 2 != 0) {
787 terminal.initialize();
790 cols_selector.addEventListener('input', function() {
791 if (cols_selector.value % 4 != 0) {
794 terminal.initialize();
795 tui.window_width = terminal.cols / 2,
798 window.setInterval(function() {
799 if (!(['input', 'n_cols', 'n_rows', 'movement_keys', 'command_char'].includes(document.activeElement.id))) {