7 rows: <input id="n_rows" type="number" step=4 min=8 value=24 />
8 cols: <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 square_move_up: <input id="key_square_move_up" type="text" value="w" /> (hint: ArrowUp)<br />
15 square_move_left: <input id="key_square_move_left" type="text" value="a" /> (hint: ArrowLeft)<br />
16 square_move_down: <input id="key_square_move_down" type="text" value="s" /> (hint: ArrowDown)<br />
17 square_move_right: <input id="key_square_move_right" type="text" value="d" /> (hint: ArrowRight)<br />
18 hex_move_upleft: <input id="key_hex_move_upleft" type="text" value="w" /><br />
19 hex_move_upright: <input id="key_hex_move_upright" type="text" value="e" /><br />
20 hex_move_right: <input id="key_hex_move_right" type="text" value="d" /><br />
21 hex_move_downright: <input id="key_hex_move_downright" type="text" value="c" /><br />
22 hex_move_downleft: <input id="key_hex_move_downleft" type="text" value="x" /><br />
23 hex_move_left: <input id="key_hex_move_left" type="text" value="s" /><br />
24 flatten_: <input id="key_flatten" type="text" value="f" /><br />
25 switch_to_chat: <input id="key_switch_to_chat" type="text" value="C" /><br />
26 switch_to_play: <input id="key_switch_to_play" type="text" value="P" /><br />
27 switch_to_annotate: <input id="key_switch_to_annotate" type="text" value="E" /><br />
28 switch_to_portal: <input id="key_switch_to_portal" type="text" value="p" /><br />
29 switch_to_study: <input id="key_switch_to_study" type="text" value="?" /><br />
30 switch_to_edit: <input id="key_switch_to_edit" type="text" value="E" /><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_"]');
43 initialize: function() {
44 this.rows = rows_selector.value;
45 this.cols = cols_selector.value;
46 this.pre_el = document.getElementById("terminal");
47 this.pre_el.style.color = this.foreground;
48 this.pre_el.style.backgroundColor = this.background;
51 for (let y = 0, x = 0; y <= this.rows; x++) {
55 this.content.push(line);
64 blink_screen: function() {
65 this.pre_el.style.color = this.background;
66 this.pre_el.style.backgroundColor = this.foreground;
68 this.pre_el.style.color = this.foreground;
69 this.pre_el.style.backgroundColor = this.background;
74 for (let y = 0; y < this.rows; y++) {
75 let line = this.content[y].join('');
76 pre_string += line + '\n';
78 this.pre_el.textContent = pre_string;
80 write: function(start_y, start_x, msg) {
81 for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
82 this.content[start_y][x] = msg[i];
85 drawBox: function(start_y, start_x, height, width) {
86 let end_y = start_y + height;
87 let end_x = start_x + width;
88 for (let y = start_y, x = start_x; y < this.rows; x++) {
96 this.content[y][x] = ' ';
100 terminal.initialize();
103 tokenize: function(str) {
109 for (let i = 0; i < str.length; i++) {
115 } else if (c == '\\') {
117 } else if (c == '"') {
122 } else if (c == '"') {
124 } else if (c === ' ') {
125 if (token.length > 0) {
134 if (token.length > 0) {
137 let token_starts = [];
138 for (let i = 0; i < token_ends.length; i++) {
139 token_starts.push(token_ends[i] - tokens[i].length);
141 return [tokens, token_starts];
143 parse_yx: function(position_string) {
144 let coordinate_strings = position_string.split(',')
145 let position = [0, 0];
146 position[0] = parseInt(coordinate_strings[0].slice(2));
147 position[1] = parseInt(coordinate_strings[1].slice(2));
159 init: function(url) {
161 this.websocket = new WebSocket(this.url);
162 this.websocket.onopen = function(event) {
163 window.setInterval(function() { server.send(['PING']) }, 30000);
164 tui.log_msg("@ server connected! :)");
165 tui.switch_mode(mode_login);
167 this.websocket.onclose = function(event) {
168 tui.log_msg("@ server disconnected :(");
169 tui.log_msg("@ hint: try the '/reconnect' command");
171 this.websocket.onmessage = this.handle_event;
173 reconnect: function() {
174 this.reconnect_to(this.url);
176 reconnect_to: function(url) {
177 this.websocket.close();
180 send: function(tokens) {
181 this.websocket.send(unparser.untokenize(tokens));
183 handle_event: function(event) {
184 let tokens = parser.tokenize(event.data)[0];
185 if (tokens[0] === 'TURN') {
186 game.turn_complete = false;
189 game.turn = parseInt(tokens[1]);
190 } else if (tokens[0] === 'THING_POS') {
191 game.get_thing(tokens[1], true).position = parser.parse_yx(tokens[2]);
192 } else if (tokens[0] === 'THING_NAME') {
193 game.get_thing(tokens[1], true).name_ = tokens[2];
194 } else if (tokens[0] === 'MAP') {
195 game.map_geometry = tokens[1];
197 game.map_size = parser.parse_yx(tokens[2]);
199 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
200 game.turn_complete = true;
201 explorer.empty_info_db();
202 if (tui.mode == mode_post_login_wait) {
203 tui.switch_mode(mode_play);
205 } else if (tui.mode == mode_study) {
206 explorer.query_info();
208 let t = game.get_thing(game.player_id);
209 if (t.position in game.portals) {
210 tui.teleport_target = game.portals[t.position];
211 tui.switch_mode(mode_teleport);
215 } else if (tokens[0] === 'CHAT') {
216 tui.log_msg('# ' + tokens[1], 1);
217 } else if (tokens[0] === 'PLAYER_ID') {
218 game.player_id = parseInt(tokens[1]);
219 } else if (tokens[0] === 'LOGIN_OK') {
220 this.send(['GET_GAMESTATE']);
221 tui.switch_mode(mode_post_login_wait);
222 } else if (tokens[0] === 'PORTAL') {
223 let position = parser.parse_yx(tokens[1]);
224 game.portals[position] = tokens[2];
225 } else if (tokens[0] === 'ANNOTATION') {
226 let position = parser.parse_yx(tokens[1]);
227 explorer.update_info_db(position, tokens[2]);
228 } else if (tokens[0] === 'UNHANDLED_INPUT') {
229 tui.log_msg('? unknown command');
230 } else if (tokens[0] === 'PLAY_ERROR') {
231 terminal.blink_screen();
232 } else if (tokens[0] === 'ARGUMENT_ERROR') {
233 tui.log_msg('? syntax error: ' + tokens[1]);
234 } else if (tokens[0] === 'GAME_ERROR') {
235 tui.log_msg('? game error: ' + tokens[1]);
236 } else if (tokens[0] === 'PONG') {
239 tui.log_msg('? unhandled input: ' + event.data);
245 quote: function(str) {
247 for (let i = 0; i < str.length; i++) {
249 if (['"', '\\'].includes(c)) {
255 return quoted.join('');
257 to_yx: function(yx_coordinate) {
258 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
260 untokenize: function(tokens) {
261 let quoted_tokens = [];
262 for (let token of tokens) {
263 quoted_tokens.push(this.quote(token));
265 return quoted_tokens.join(" ");
270 constructor(name, has_input_prompt=false, shows_info=false, is_intro=false) {
272 this.has_input_prompt = has_input_prompt;
273 this.shows_info= shows_info;
274 this.is_intro = is_intro;
277 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
278 let mode_login = new Mode('login', true, false, true);
279 let mode_post_login_wait = new Mode('waiting for game world', false, false, true);
280 let mode_chat = new Mode('chat / write messages to players', true, false);
281 let mode_annotate = new Mode('add message to map tile', true, true);
282 let mode_play = new Mode('play / move around', false, false);
283 let mode_study = new Mode('check map tiles for messages', false, true);
284 let mode_edit = new Mode('write ASCII char to map tile', false, false);
285 let mode_teleport = new Mode('teleport away?', true);
286 let mode_portal = new Mode('add portal to map tile', true, true);
289 mode: mode_waiting_for_server,
293 window_width: terminal.cols / 2,
298 this.inputEl = document.getElementById("input");
299 this.inputEl.focus();
300 this.recalc_input_lines();
301 this.height_header = this.height_turn_line + this.height_mode_line;
302 this.log_msg("@ waiting for server connection ...");
305 init_keys: function() {
307 for (let key_selector of key_selectors) {
308 this.keys[key_selector.id.slice(4)] = key_selector.value;
310 if (game.map_geometry == 'Square') {
311 this.movement_keys = {
312 [this.keys.square_move_up]: 'UP',
313 [this.keys.square_move_left]: 'LEFT',
314 [this.keys.square_move_down]: 'DOWN',
315 [this.keys.square_move_right]: 'RIGHT'
317 } else if (game.map_geometry == 'Hex') {
318 this.movement_keys = {
319 [this.keys.hex_move_upleft]: 'UPLEFT',
320 [this.keys.hex_move_upright]: 'UPRIGHT',
321 [this.keys.hex_move_right]: 'RIGHT',
322 [this.keys.hex_move_downright]: 'DOWNRIGHT',
323 [this.keys.hex_move_downleft]: 'DOWNLEFT',
324 [this.keys.hex_move_left]: 'LEFT'
328 switch_mode: function(mode, keep_pos=false) {
329 if (mode == mode_study && !keep_pos && game.player_id in game.things) {
330 explorer.position = game.things[game.player_id].position;
334 if (mode == mode_annotate && explorer.position in explorer.info_db) {
335 let info = explorer.info_db[explorer.position];
336 if (info != "(none)") {
337 this.inputEl.value = info;
338 this.recalc_input_lines();
341 if (mode == mode_login) {
342 if (this.login_name) {
343 server.send(['LOGIN', this.login_name]);
345 this.log_msg("? need login name");
347 } else if (mode == mode_portal && explorer.position in game.portals) {
348 let portal = game.portals[explorer.position]
349 this.inputEl.value = portal;
350 this.recalc_input_lines();
351 } else if (mode == mode_teleport) {
352 tui.log_msg("@ May teleport to: " + tui.teleport_target);
353 tui.log_msg("@ Enter 'YES!' to entusiastically affirm.");
357 empty_input: function(str) {
358 this.inputEl.value = "";
359 if (this.mode.has_input_prompt) {
360 this.recalc_input_lines();
362 this.height_input = 0;
365 recalc_input_lines: function() {
366 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
367 this.height_input = this.input_lines.length;
369 msg_into_lines_of_width: function(msg, width) {
372 for (let i = 0, x = 0; i < msg.length; i++, x++) {
373 if (x >= width || msg[i] == "\n") {
378 if (msg[i] != "\n") {
385 log_msg: function(msg) {
387 while (this.log.length > 100) {
392 log_help: function() {
393 let movement_keys_desc = Object.keys(this.movement_keys).join(',');
394 this.log_msg("HELP:");
395 this.log_msg("chat mode commands:");
396 this.log_msg(" /nick NAME - re-name yourself to NAME");
397 this.log_msg(" /msg USER TEXT - send TEXT to USER");
398 this.log_msg(" /help - show this help");
399 this.log_msg(" /P or /play - switch to play mode");
400 this.log_msg(" /? or /study - switch to study mode");
401 this.log_msg("commands common to study and play mode:");
402 this.log_msg(" " + movement_keys_desc + " - move");
403 this.log_msg(" " + this.keys.switch_to_chat + " - switch to chat mode");
404 this.log_msg("commands specific to play mode:");
405 this.log_msg(" " + this.keys.switch_to_edit + " - write following ASCII character");
406 this.log_msg(" " + this.keys.flatten + " - flatten surroundings");
407 this.log_msg(" " + this.keys.switch_to_study + " - switch to study mode");
408 this.log_msg("commands specific to study mode:");
409 this.log_msg(" " + this.keys.switch_to_annotate + " - annotate terrain");
410 this.log_msg(" " + this.keys.switch_to_play + " - switch to play mode");
412 draw_map: function() {
413 let map_lines_split = [];
415 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
416 if (j == game.map_size[1]) {
417 map_lines_split.push(line);
421 line.push(game.map[i]);
423 map_lines_split.push(line);
424 for (const thing_id in game.things) {
425 let t = game.things[thing_id];
426 map_lines_split[t.position[0]][t.position[1]] = '@';
428 if (tui.mode.shows_info) {
429 map_lines_split[explorer.position[0]][explorer.position[1]] = '?';
432 if (game.map_geometry == 'Square') {
433 for (let line_split of map_lines_split) {
434 map_lines.push(line_split.join(' '));
436 } else if (game.map_geometry == 'Hex') {
438 for (let line_split of map_lines_split) {
439 map_lines.push(' '.repeat(indent) + line_split.join(' '));
447 let window_center = [terminal.rows / 2, this.window_width / 2];
448 let player = game.things[game.player_id];
449 let center_position = [player.position[0], player.position[1]];
450 if (tui.mode.shows_info) {
451 center_position = [explorer.position[0], explorer.position[1]];
453 center_position[1] = center_position[1] * 2;
454 let offset = [center_position[0] - window_center[0],
455 center_position[1] - window_center[1]]
456 if (game.map_geometry == 'Hex' && offset[0] % 2) {
459 let term_y = Math.max(0, -offset[0]);
460 let term_x = Math.max(0, -offset[1]);
461 let map_y = Math.max(0, offset[0]);
462 let map_x = Math.max(0, offset[1]);
463 for (; term_y < terminal.rows && map_y < game.map_size[0]; term_y++, map_y++) {
464 let to_draw = map_lines[map_y].slice(map_x, this.window_width + offset[1]);
465 terminal.write(term_y, term_x, to_draw);
468 draw_mode_line: function() {
469 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
471 draw_turn_line: function(n) {
472 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
474 draw_history: function() {
475 let log_display_lines = [];
476 for (let line of this.log) {
477 log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
479 for (let y = terminal.rows - 1 - this.height_input,
480 i = log_display_lines.length - 1;
481 y >= this.height_header && i >= 0;
483 terminal.write(y, this.window_width, log_display_lines[i]);
486 draw_info: function() {
487 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
488 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
489 terminal.write(y, this.window_width, lines[i]);
492 draw_input: function() {
493 if (this.mode.has_input_prompt) {
494 for (let y = terminal.rows - this.height_input, i = 0; i < this.input_lines.length; y++, i++) {
495 terminal.write(y, this.window_width, this.input_lines[i]);
499 full_refresh: function() {
500 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
501 if (this.mode.is_intro) {
505 if (game.turn_complete) {
507 this.draw_turn_line();
509 this.draw_mode_line();
510 if (this.mode.shows_info) {
526 this.map_size = [0,0];
530 get_thing: function(id_, create_if_not_found=false) {
531 if (id_ in game.things) {
532 return game.things[id_];
533 } else if (create_if_not_found) {
534 let t = new Thing([0,0]);
535 game.things[id_] = t;
539 move: function(start_position, direction) {
540 let target = [start_position[0], start_position[1]];
541 if (direction == 'LEFT') {
543 } else if (direction == 'RIGHT') {
545 } else if (game.map_geometry == 'Square') {
546 if (direction == 'UP') {
548 } else if (direction == 'DOWN') {
551 } else if (game.map_geometry == 'Hex') {
552 let start_indented = start_position[0] % 2;
553 if (direction == 'UPLEFT') {
555 if (!start_indented) {
558 } else if (direction == 'UPRIGHT') {
560 if (start_indented) {
563 } else if (direction == 'DOWNLEFT') {
565 if (!start_indented) {
568 } else if (direction == 'DOWNRIGHT') {
570 if (start_indented) {
575 if (target[0] < 0 || target[1] < 0 ||
576 target[0] >= this.map_size[0] || target[1] >= this.map_size[1]) {
586 server.init(websocket_location);
591 move: function(direction) {
592 let target = game.move(this.position, direction);
594 this.position = target
598 terminal.blink_screen();
601 update_info_db: function(yx, str) {
602 this.info_db[yx] = str;
603 if (tui.mode == mode_study) {
607 empty_info_db: function() {
609 if (tui.mode == mode_study) {
613 query_info: function() {
614 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
616 get_info: function() {
618 let position_i = this.position[0] * game.map_size[1] + this.position[1];
619 info += "TERRAIN: " + game.map[position_i] + "\n";
620 for (let t_id in game.things) {
621 let t = game.things[t_id];
622 if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
625 info += ": " + t.name_;
630 if (this.position in game.portals) {
631 info += "PORTAL: " + game.portals[this.position] + "\n";
633 if (this.position in this.info_db) {
634 info += "ANNOTATIONS: " + this.info_db[this.position];
640 annotate: function(msg) {
641 if (msg.length == 0) {
642 msg = " "; // triggers annotation deletion
644 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
646 set_portal: function(msg) {
647 if (msg.length == 0) {
648 msg = " "; // triggers portal deletion
650 server.send(["PORTAL", unparser.to_yx(explorer.position), msg]);
654 tui.inputEl.addEventListener('input', (event) => {
655 if (tui.mode.has_input_prompt) {
656 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
657 if (tui.inputEl.value.length > max_length) {
658 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
660 tui.recalc_input_lines();
662 } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
663 server.send(["TASK:WRITE", tui.inputEl.value[0]]);
664 tui.switch_mode(mode_play);
665 } else if (tui.mode == mode_teleport) {
666 if (['Y', 'y'].includes(tui.inputEl.value[0])) {
667 server.reconnect_to(tui.teleport_target);
669 tui.log_msg("@ teleportation aborted");
670 tui.switch_mode(mode_play);
674 tui.inputEl.addEventListener('keydown', (event) => {
675 if (event.key == 'Enter') {
676 event.preventDefault();
678 if (tui.mode == mode_login && event.key == 'Enter') {
679 tui.login_name = tui.inputEl.value;
680 server.send(['LOGIN', tui.inputEl.value]);
682 } else if (tui.mode == mode_portal && event.key == 'Enter') {
683 explorer.set_portal(tui.inputEl.value);
684 tui.switch_mode(mode_study, true);
685 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
686 explorer.annotate(tui.inputEl.value);
687 tui.switch_mode(mode_study, true);
688 } else if (tui.mode == mode_teleport && event.key == 'Enter') {
689 if (tui.inputEl.value == 'YES!') {
690 server.reconnect_to(tui.teleport_target);
692 tui.log_msg('@ teleport aborted');
693 tui.switch_mode(mode_play);
695 } else if (tui.mode == mode_chat && event.key == 'Enter') {
696 let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
697 if (tokens.length > 0 && tokens[0].length > 0) {
698 if (tui.inputEl.value[0][0] == '/') {
699 if (tokens[0].slice(1) == 'play' || tokens[0].slice(1) == 'P') {
700 tui.switch_mode(mode_play);
701 } else if (tokens[0].slice(1) == 'study' || tokens[0].slice(1) == '?') {
702 tui.switch_mode(mode_study);
703 } else if (tokens[0].slice(1) == 'help') {
705 } else if (tokens[0].slice(1) == 'nick') {
706 if (tokens.length > 1) {
707 server.send(['LOGIN', tokens[1]]);
709 tui.log_msg('? need login name');
711 } else if (tokens[0].slice(1) == 'msg') {
712 if (tokens.length > 2) {
713 let msg = tui.inputEl.value.slice(token_starts[2]);
714 server.send(['QUERY', tokens[1], msg]);
716 tui.log_msg('? need message target and message');
718 } else if (tokens[0].slice(1) == 'reconnect') {
719 if (tokens.length > 1) {
720 server.reconnect_to(tokens[1]);
725 tui.log_msg('? unknown command');
728 server.send(['ALL', tui.inputEl.value]);
730 } else if (tui.inputEl.valuelength > 0) {
731 server.send(['ALL', tui.inputEl.value]);
735 } else if (tui.mode == mode_play) {
736 if (event.key === tui.keys.switch_to_chat) {
737 event.preventDefault();
738 tui.switch_mode(mode_chat);
739 } else if (event.key === tui.keys.switch_to_edit) {
740 event.preventDefault();
741 tui.switch_mode(mode_edit);
742 } else if (event.key === tui.keys.switch_to_study) {
743 tui.switch_mode(mode_study);
744 } else if (event.key === tui.keys.flatten) {
745 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
746 } else if (event.key in tui.movement_keys) {
747 server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
749 } else if (tui.mode == mode_study) {
750 if (event.key === tui.keys.switch_to_chat) {
751 event.preventDefault();
752 tui.switch_mode(mode_chat);
753 } else if (event.key == tui.keys.switch_to_play) {
754 tui.switch_mode(mode_play);
755 } else if (event.key === tui.keys.switch_to_portal) {
756 event.preventDefault();
757 tui.switch_mode(mode_portal);
758 } else if (event.key in tui.movement_keys) {
759 explorer.move(tui.movement_keys[event.key]);
760 } else if (event.key === tui.keys.switch_to_annotate) {
761 event.preventDefault();
762 tui.switch_mode(mode_annotate);
767 rows_selector.addEventListener('input', function() {
768 if (rows_selector.value % 4 != 0) {
771 terminal.initialize();
774 cols_selector.addEventListener('input', function() {
775 if (cols_selector.value % 4 != 0) {
778 terminal.initialize();
779 tui.window_width = terminal.cols / 2,
782 for (let key_selector of key_selectors) {
783 key_selector.addEventListener('input', function() {
787 window.setInterval(function() {
788 if (!(['input', 'n_cols', 'n_rows'].includes(document.activeElement.id)
789 || document.activeElement.id.startsWith('key_'))) {