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=4 min=8 value=24 />
12 cols: <input id="n_cols" type="number" step=4 min=20 value=80 />
13 <option value=":" selected>:</option>
14 <option value="/">/</option>
17 <pre id="terminal" style="display: inline-block;"></pre>
18 <textarea id="input" style="opacity: 0; width: 0px;"></textarea>
21 let websocket_location = "ws://localhost:8000";
23 let movement_keys_selector = document.getElementById("movement_keys");
24 let rows_selector = document.getElementById("n_rows");
25 let cols_selector = document.getElementById("n_cols");
30 initialize: function() {
31 this.rows = rows_selector.value;
32 this.cols = cols_selector.value;
33 this.pre_el = document.getElementById("terminal");
34 this.pre_el.style.color = this.foreground;
35 this.pre_el.style.backgroundColor = this.background;
38 for (let y = 0, x = 0; y <= this.rows; x++) {
42 this.content.push(line);
51 blink_screen: function() {
52 this.pre_el.style.color = this.background;
53 this.pre_el.style.backgroundColor = this.foreground;
55 this.pre_el.style.color = this.foreground;
56 this.pre_el.style.backgroundColor = this.background;
61 for (let y = 0; y < this.rows; y++) {
62 let line = this.content[y].join('');
63 pre_string += line + '\n';
65 this.pre_el.textContent = pre_string;
67 write: function(start_y, start_x, msg) {
68 for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
69 this.content[start_y][x] = msg[i];
72 drawBox: function(start_y, start_x, height, width) {
73 let end_y = start_y + height;
74 let end_x = start_x + width;
75 for (let y = start_y, x = start_x; y < this.rows; x++) {
83 this.content[y][x] = ' ';
87 terminal.initialize();
90 tokenize: function(str) {
96 for (let i = 0; i < str.length; i++) {
102 } else if (c == '\\') {
104 } else if (c == '"') {
109 } else if (c == '"') {
111 } else if (c === ' ') {
112 if (token.length > 0) {
121 if (token.length > 0) {
124 let token_starts = [];
125 for (let i = 0; i < token_ends.length; i++) {
126 token_starts.push(token_ends[i] - tokens[i].length);
128 return [tokens, token_starts];
130 parse_yx: function(position_string) {
131 let coordinate_strings = position_string.split(',')
132 let position = [0, 0];
133 position[0] = parseInt(coordinate_strings[0].slice(2));
134 position[1] = parseInt(coordinate_strings[1].slice(2));
146 init: function(url) {
148 this.websocket = new WebSocket(this.url);
149 this.websocket.onopen = function(event) {
150 window.setInterval(function() { server.send(['PING']) }, 30000);
151 tui.log_msg("@ server connected! :)");
152 tui.switch_mode(mode_login);
154 this.websocket.onclose = function(event) {
155 tui.log_msg("@ server disconnected :(");
156 tui.log_msg("@ hint: try the '/reconnect' command");
158 this.websocket.onmessage = this.handle_event;
160 reconnect: function() {
161 this.reconnect_to(this.url);
163 reconnect_to: function(url) {
164 this.websocket.close();
167 send: function(tokens) {
168 this.websocket.send(unparser.untokenize(tokens));
170 handle_event: function(event) {
171 let tokens = parser.tokenize(event.data)[0];
172 if (tokens[0] === 'TURN') {
173 game.turn_complete = false;
176 game.turn = parseInt(tokens[1]);
177 } else if (tokens[0] === 'THING_POS') {
178 game.get_thing(tokens[1], true).position = parser.parse_yx(tokens[2]);
179 } else if (tokens[0] === 'THING_NAME') {
180 game.get_thing(tokens[1], true).name_ = tokens[2];
181 } else if (tokens[0] === 'MAP') {
182 game.map_geometry = tokens[1];
184 game.map_size = parser.parse_yx(tokens[2]);
186 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
187 game.turn_complete = true;
188 explorer.empty_info_db();
189 if (tui.mode == mode_post_login_wait) {
190 tui.switch_mode(mode_play);
192 } else if (tui.mode == mode_study) {
193 explorer.query_info();
195 let t = game.get_thing(game.player_id);
196 if (t.position in game.portals) {
197 tui.teleport_target = game.portals[t.position];
198 tui.switch_mode(mode_teleport);
202 } else if (tokens[0] === 'CHAT') {
203 tui.log_msg('# ' + tokens[1], 1);
204 } else if (tokens[0] === 'PLAYER_ID') {
205 game.player_id = parseInt(tokens[1]);
206 } else if (tokens[0] === 'LOGIN_OK') {
207 this.send(['GET_GAMESTATE']);
208 tui.switch_mode(mode_post_login_wait);
209 } else if (tokens[0] === 'PORTAL') {
210 let position = parser.parse_yx(tokens[1]);
211 game.portals[position] = tokens[2];
212 } else if (tokens[0] === 'ANNOTATION') {
213 let position = parser.parse_yx(tokens[1]);
214 explorer.update_info_db(position, tokens[2]);
215 } else if (tokens[0] === 'UNHANDLED_INPUT') {
216 tui.log_msg('? unknown command');
217 } else if (tokens[0] === 'PLAY_ERROR') {
218 terminal.blink_screen();
219 } else if (tokens[0] === 'ARGUMENT_ERROR') {
220 tui.log_msg('? syntax error: ' + tokens[1]);
221 } else if (tokens[0] === 'GAME_ERROR') {
222 tui.log_msg('? game error: ' + tokens[1]);
223 } else if (tokens[0] === 'PONG') {
226 tui.log_msg('? unhandled input: ' + event.data);
232 quote: function(str) {
234 for (let i = 0; i < str.length; i++) {
236 if (['"', '\\'].includes(c)) {
242 return quoted.join('');
244 to_yx: function(yx_coordinate) {
245 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
247 untokenize: function(tokens) {
248 let quoted_tokens = [];
249 for (let token of tokens) {
250 quoted_tokens.push(this.quote(token));
252 return quoted_tokens.join(" ");
257 constructor(name, has_input_prompt=false, shows_info=false, is_intro=false) {
259 this.has_input_prompt = has_input_prompt;
260 this.shows_info= shows_info;
261 this.is_intro = is_intro;
264 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
265 let mode_login = new Mode('login', true, false, true);
266 let mode_post_login_wait = new Mode('waiting for game world', false, false, true);
267 let mode_chat = new Mode('chat / write messages to players', true, false);
268 let mode_annotate = new Mode('add message to map tile', true, true);
269 let mode_play = new Mode('play / move around', false, false);
270 let mode_study = new Mode('check map tiles for messages', false, true);
271 let mode_edit = new Mode('write ASCII char to map tile', false, false);
272 let mode_teleport = new Mode('teleport away?', true);
273 let mode_portal = new Mode('add portal to map tile', true, true);
276 mode: mode_waiting_for_server,
280 window_width: terminal.cols / 2,
285 this.inputEl = document.getElementById("input");
286 this.inputEl.focus();
287 this.recalc_input_lines();
288 this.height_header = this.height_turn_line + this.height_mode_line;
289 this.log_msg("@ waiting for server connection ...");
292 init_wasd: function() {
294 if (movement_keys_selector.value == 'alphabetic') {
295 if (game.map_geometry == 'Square') {
296 this.movement_keys = {
302 tui.movement_keys_desc = 'w, a, s, d';
303 } else if (game.map_geometry == 'Hex') {
304 this.movement_keys = {
312 tui.movement_keys_desc = 'e, d, c, x, s, w';
314 } else if (movement_keys_selector.value == 'arrow_or_numpad') {
315 if (game.map_geometry == 'Square') {
316 this.movement_keys = {
320 'ArrowRight': 'RIGHT'
322 tui.movement_keys_desc = 'arrow keys';
323 } else if (game.map_geometry == 'Hex') {
324 this.movement_keys = {
332 tui.movement_keys_desc = 'numpad keys';
336 switch_mode: function(mode, keep_pos=false) {
337 if (mode == mode_study && !keep_pos && game.player_id in game.things) {
338 explorer.position = game.things[game.player_id].position;
342 if (mode == mode_annotate && explorer.position in explorer.info_db) {
343 let info = explorer.info_db[explorer.position];
344 if (info != "(none)") {
345 this.inputEl.value = info;
346 this.recalc_input_lines();
349 if (mode == mode_login) {
350 if (this.login_name) {
351 server.send(['LOGIN', this.login_name]);
353 this.log_msg("? need login name");
355 } else if (mode == mode_portal && explorer.position in game.portals) {
356 let portal = game.portals[explorer.position]
357 this.inputEl.value = portal;
358 this.recalc_input_lines();
359 } else if (mode == mode_teleport) {
360 tui.log_msg("@ May teleport to: " + tui.teleport_target);
361 tui.log_msg("@ Enter 'YES!' to entusiastically affirm.");
365 empty_input: function(str) {
366 this.inputEl.value = "";
367 if (this.mode.has_input_prompt) {
368 this.recalc_input_lines();
370 this.height_input = 0;
373 recalc_input_lines: function() {
374 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
375 this.height_input = this.input_lines.length;
377 msg_into_lines_of_width: function(msg, width) {
380 for (let i = 0, x = 0; i < msg.length; i++, x++) {
381 if (x >= width || msg[i] == "\n") {
386 if (msg[i] != "\n") {
393 log_msg: function(msg) {
395 while (this.log.length > 100) {
400 log_help: function() {
401 this.log_msg("HELP:");
402 this.log_msg("chat mode commands:");
403 this.log_msg(" /nick NAME - re-name yourself to NAME");
404 this.log_msg(" /msg USER TEXT - send TEXT to USER");
405 this.log_msg(" /help - show this help");
406 this.log_msg(" /P or /play - switch to play mode");
407 this.log_msg(" /? or /study - switch to study mode");
408 this.log_msg("commands common to study and play mode:");
409 this.log_msg(" " + this.movement_keys_desc + " - move");
410 this.log_msg(" C - switch to chat mode");
411 this.log_msg("commands specific to play mode:");
412 this.log_msg(" E - write following ASCII character");
413 this.log_msg(" f - flatten surroundings");
414 this.log_msg(" ? - switch to study mode");
415 this.log_msg("commands specific to study mode:");
416 this.log_msg(" E - annotate terrain");
417 this.log_msg(" P - switch to play mode");
419 draw_map: function() {
420 let map_lines_split = [];
422 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
423 if (j == game.map_size[1]) {
424 map_lines_split.push(line);
428 line.push(game.map[i]);
430 map_lines_split.push(line);
431 for (const thing_id in game.things) {
432 let t = game.things[thing_id];
433 map_lines_split[t.position[0]][t.position[1]] = '@';
435 if (tui.mode.shows_info) {
436 map_lines_split[explorer.position[0]][explorer.position[1]] = '?';
439 if (game.map_geometry == 'Square') {
440 for (let line_split of map_lines_split) {
441 map_lines.push(line_split.join(' '));
443 } else if (game.map_geometry == 'Hex') {
445 for (let line_split of map_lines_split) {
446 map_lines.push(' '.repeat(indent) + line_split.join(' '));
454 let window_center = [terminal.rows / 2, this.window_width / 2];
455 let player = game.things[game.player_id];
456 let center_position = [player.position[0], player.position[1]];
457 if (tui.mode.shows_info) {
458 center_position = [explorer.position[0], explorer.position[1]];
460 center_position[1] = center_position[1] * 2;
461 let offset = [center_position[0] - window_center[0],
462 center_position[1] - window_center[1]]
463 if (game.map_geometry == 'Hex' && offset[0] % 2) {
466 let term_y = Math.max(0, -offset[0]);
467 let term_x = Math.max(0, -offset[1]);
468 let map_y = Math.max(0, offset[0]);
469 let map_x = Math.max(0, offset[1]);
470 for (; term_y < terminal.rows && map_y < game.map_size[0]; term_y++, map_y++) {
471 let to_draw = map_lines[map_y].slice(map_x, this.window_width + offset[1]);
472 terminal.write(term_y, term_x, to_draw);
475 draw_mode_line: function() {
476 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
478 draw_turn_line: function(n) {
479 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
481 draw_history: function() {
482 let log_display_lines = [];
483 for (let line of this.log) {
484 log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
486 for (let y = terminal.rows - 1 - this.height_input,
487 i = log_display_lines.length - 1;
488 y >= this.height_header && i >= 0;
490 terminal.write(y, this.window_width, log_display_lines[i]);
493 draw_info: function() {
494 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
495 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
496 terminal.write(y, this.window_width, lines[i]);
499 draw_input: function() {
500 if (this.mode.has_input_prompt) {
501 for (let y = terminal.rows - this.height_input, i = 0; i < this.input_lines.length; y++, i++) {
502 terminal.write(y, this.window_width, this.input_lines[i]);
506 full_refresh: function() {
507 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
508 if (this.mode.is_intro) {
512 if (game.turn_complete) {
514 this.draw_turn_line();
516 this.draw_mode_line();
517 if (this.mode.shows_info) {
533 this.map_size = [0,0];
537 get_thing: function(id_, create_if_not_found=false) {
538 if (id_ in game.things) {
539 return game.things[id_];
540 } else if (create_if_not_found) {
541 let t = new Thing([0,0]);
542 game.things[id_] = t;
546 move: function(start_position, direction) {
547 let target = [start_position[0], start_position[1]];
548 if (direction == 'LEFT') {
550 } else if (direction == 'RIGHT') {
552 } else if (game.map_geometry == 'Square') {
553 if (direction == 'UP') {
555 } else if (direction == 'DOWN') {
558 } else if (game.map_geometry == 'Hex') {
559 let start_indented = start_position[0] % 2;
560 if (direction == 'UPLEFT') {
562 if (!start_indented) {
565 } else if (direction == 'UPRIGHT') {
567 if (start_indented) {
570 } else if (direction == 'DOWNLEFT') {
572 if (!start_indented) {
575 } else if (direction == 'DOWNRIGHT') {
577 if (start_indented) {
582 if (target[0] < 0 || target[1] < 0 ||
583 target[0] >= this.map_size[0] || target[1] >= this.map_size[1]) {
593 server.init(websocket_location);
598 move: function(direction) {
599 let target = game.move(this.position, direction);
601 this.position = target
605 terminal.blink_screen();
608 update_info_db: function(yx, str) {
609 this.info_db[yx] = str;
610 if (tui.mode == mode_study) {
614 empty_info_db: function() {
616 if (tui.mode == mode_study) {
620 query_info: function() {
621 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
623 get_info: function() {
625 let position_i = this.position[0] * game.map_size[1] + this.position[1];
626 info += "TERRAIN: " + game.map[position_i] + "\n";
627 for (let t_id in game.things) {
628 let t = game.things[t_id];
629 if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
632 info += ": " + t.name_;
637 if (this.position in game.portals) {
638 info += "PORTAL: " + game.portals[this.position] + "\n";
640 if (this.position in this.info_db) {
641 info += "ANNOTATIONS: " + this.info_db[this.position];
647 annotate: function(msg) {
648 if (msg.length == 0) {
649 msg = " "; // triggers annotation deletion
651 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
653 set_portal: function(msg) {
654 if (msg.length == 0) {
655 msg = " "; // triggers portal deletion
657 server.send(["PORTAL", unparser.to_yx(explorer.position), msg]);
661 tui.inputEl.addEventListener('input', (event) => {
662 if (tui.mode.has_input_prompt) {
663 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
664 if (tui.inputEl.value.length > max_length) {
665 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
667 tui.recalc_input_lines();
669 } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
670 server.send(["TASK:WRITE", tui.inputEl.value[0]]);
671 tui.switch_mode(mode_play);
672 } else if (tui.mode == mode_teleport) {
673 if (['Y', 'y'].includes(tui.inputEl.value[0])) {
674 server.reconnect_to(tui.teleport_target);
676 tui.log_msg("@ teleportation aborted");
677 tui.switch_mode(mode_play);
681 tui.inputEl.addEventListener('keydown', (event) => {
682 if (event.key == 'Enter') {
683 event.preventDefault();
685 if (tui.mode == mode_login && event.key == 'Enter') {
686 tui.login_name = tui.inputEl.value;
687 server.send(['LOGIN', tui.inputEl.value]);
689 } else if (tui.mode == mode_portal && event.key == 'Enter') {
690 explorer.set_portal(tui.inputEl.value);
691 tui.switch_mode(mode_study, true);
692 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
693 explorer.annotate(tui.inputEl.value);
694 tui.switch_mode(mode_study, true);
695 } else if (tui.mode == mode_teleport && event.key == 'Enter') {
696 if (tui.inputEl.value == 'YES!') {
697 server.reconnect_to(tui.teleport_target);
699 tui.log_msg('@ teleport aborted');
700 tui.switch_mode(mode_play);
702 } else if (tui.mode == mode_chat && event.key == 'Enter') {
703 let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
704 if (tokens.length > 0 && tokens[0].length > 0) {
705 if (tui.inputEl.value[0][0] == '/') {
706 if (tokens[0].slice(1) == 'play' || tokens[0].slice(1) == 'P') {
707 tui.switch_mode(mode_play);
708 } else if (tokens[0].slice(1) == 'study' || tokens[0].slice(1) == '?') {
709 tui.switch_mode(mode_study);
710 } else if (tokens[0].slice(1) == 'help') {
712 } else if (tokens[0].slice(1) == 'nick') {
713 if (tokens.length > 1) {
714 server.send(['LOGIN', tokens[1]]);
716 tui.log_msg('? need login name');
718 } else if (tokens[0].slice(1) == 'msg') {
719 if (tokens.length > 2) {
720 let msg = tui.inputEl.value.slice(token_starts[2]);
721 server.send(['QUERY', tokens[1], msg]);
723 tui.log_msg('? need message target and message');
725 } else if (tokens[0].slice(1) == 'reconnect') {
726 if (tokens.length > 1) {
727 server.reconnect_to(tokens[1]);
732 tui.log_msg('? unknown command');
735 server.send(['ALL', tui.inputEl.value]);
737 } else if (tui.inputEl.valuelength > 0) {
738 server.send(['ALL', tui.inputEl.value]);
742 } else if (tui.mode == mode_play) {
743 if (event.key === 'C') {
744 event.preventDefault();
745 tui.switch_mode(mode_chat);
746 } else if (event.key === 'E') {
747 event.preventDefault();
748 tui.switch_mode(mode_edit);
749 } else if (event.key === '?') {
750 tui.switch_mode(mode_study);
751 } else if (event.key === 'F1') {
753 } else if (event.key === 'f') {
754 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
755 } else if (event.key in tui.movement_keys) {
756 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
758 } else if (tui.mode == mode_study) {
759 if (event.key === 'C') {
760 event.preventDefault();
761 tui.switch_mode(mode_chat);
762 } else if (event.key == 'P') {
763 tui.switch_mode(mode_play);
764 } else if (event.key === 'p') {
765 event.preventDefault();
766 tui.switch_mode(mode_portal);
767 } else if (event.key in tui.movement_keys) {
768 explorer.move(tui.movement_keys[event.key]);
769 } else if (event.key === 'E') {
770 event.preventDefault();
771 tui.switch_mode(mode_annotate);
776 movement_keys_selector.addEventListener('input', function() {
779 rows_selector.addEventListener('input', function() {
780 if (rows_selector.value % 4 != 0) {
783 terminal.initialize();
786 cols_selector.addEventListener('input', function() {
787 if (cols_selector.value % 4 != 0) {
790 terminal.initialize();
791 tui.window_width = terminal.cols / 2,
794 window.setInterval(function() {
795 if (!(['input', 'n_cols', 'n_rows', 'movement_keys'].includes(document.activeElement.id))) {