7 movement: <select id="WASD_selector" name="WASD_selector" >
8 <option value="w, a, s, d" selected>w, a, s, d</option>
9 <option value="arrow keys">arrow keys</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 wasd_selector = document.getElementById("WASD_selector");
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! :)");
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') {
177 game.turn = parseInt(tokens[1]);
178 } else if (tokens[0] === 'THING_POS') {
179 game.get_thing(tokens[1], true).position = parser.parse_yx(tokens[2]);
180 } else if (tokens[0] === 'THING_NAME') {
181 game.get_thing(tokens[1], true).name_ = tokens[2];
182 } else if (tokens[0] === 'MAP') {
183 game.map_size = parser.parse_yx(tokens[1]);
185 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
186 explorer.empty_info_db();
187 if (tui.mode == mode_study) {
188 explorer.query_info();
190 let t = game.get_thing(game.player_id);
191 if (t.position in game.portals) {
192 tui.teleport_target = game.portals[t.position];
193 tui.switch_mode(mode_teleport);
197 } else if (tokens[0] === 'CHAT') {
198 tui.log_msg('# ' + tokens[1], 1);
199 } else if (tokens[0] === 'PLAYER_ID') {
200 game.player_id = parseInt(tokens[1]);
201 } else if (tokens[0] === 'LOGIN_OK') {
202 this.send(['GET_GAMESTATE']);
204 tui.switch_mode(mode_play);
205 } else if (tokens[0] === 'PORTAL') {
206 let position = parser.parse_yx(tokens[1]);
207 game.portals[position] = tokens[2];
208 } else if (tokens[0] === 'ANNOTATION') {
209 let position = parser.parse_yx(tokens[1]);
210 explorer.update_info_db(position, tokens[2]);
211 } else if (tokens[0] === 'UNHANDLED_INPUT') {
212 tui.log_msg('? unknown command');
213 } else if (tokens[0] === 'PLAY_ERROR') {
214 terminal.blink_screen();
215 } else if (tokens[0] === 'ARGUMENT_ERROR') {
216 tui.log_msg('? syntax error: ' + tokens[1]);
217 } else if (tokens[0] === 'GAME_ERROR') {
218 tui.log_msg('? game error: ' + tokens[1]);
219 } else if (tokens[0] === 'PONG') {
222 tui.log_msg('? unhandled input: ' + event.data);
228 quote: function(str) {
230 for (let i = 0; i < str.length; i++) {
232 if (['"', '\\'].includes(c)) {
238 return quoted.join('');
240 to_yx: function(yx_coordinate) {
241 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
243 untokenize: function(tokens) {
244 let quoted_tokens = [];
245 for (let token of tokens) {
246 quoted_tokens.push(this.quote(token));
248 return quoted_tokens.join(" ");
253 constructor(name, has_input_prompt=false, shows_info=false, is_intro=false) {
255 this.has_input_prompt = has_input_prompt;
256 this.shows_info= shows_info;
257 this.is_intro = is_intro;
260 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
261 let mode_login = new Mode('login', true, false, true);
262 let mode_chat = new Mode('chat / write messages to players', true, false);
263 let mode_annotate = new Mode('add message to map tile', true, true);
264 let mode_play = new Mode('play / move around', false, false);
265 let mode_study = new Mode('check map tiles for messages', false, true);
266 let mode_edit = new Mode('write ASCII char to map tile', false, false);
267 let mode_teleport = new Mode('teleport away?');
268 let mode_portal = new Mode('add portal to map tile', true, true);
271 mode: mode_waiting_for_server,
275 window_width: terminal.cols / 2,
280 this.inputEl = document.getElementById("input");
281 this.inputEl.focus();
282 this.recalc_input_lines();
283 this.height_header = this.height_turn_line + this.height_mode_line;
284 this.log_msg("@ waiting for server connection ...");
287 init_wasd: function() {
288 if (wasd_selector.value == 'w, a, s, d') {
293 } else if (wasd_selector.value == 'arrow keys') {
294 tui.key_up = 'ArrowUp';
295 tui.key_down = 'ArrowDown';
296 tui.key_left = 'ArrowLeft';
297 tui.key_right = 'ArrowRight';
299 tui.movement_keys_desc = wasd_selector.value;
301 init_login: function() {
302 this.log_msg("@ please enter your username:");
303 this.switch_mode(mode_login);
305 switch_mode: function(mode, keep_pos=false) {
306 if (mode == mode_study && !keep_pos) {
307 explorer.position = game.things[game.player_id].position;
311 if (mode == mode_annotate && explorer.position in explorer.info_db) {
312 let info = explorer.info_db[explorer.position];
313 if (info != "(none)") {
314 this.inputEl.value = info;
315 this.recalc_input_lines();
318 if (mode == mode_portal && explorer.position in game.portals) {
319 let portal = game.portals[explorer.position]
320 this.inputEl.value = portal;
321 this.recalc_input_lines();
322 } else if (mode == mode_teleport) {
323 tui.log_msg("@ May teleport to: " + tui.teleport_target);
324 tui.log_msg("@ Type Y or y to affirm, other keys to abort.");
328 empty_input: function(str) {
329 this.inputEl.value = "";
330 if (this.mode.has_input_prompt) {
331 this.recalc_input_lines();
333 this.height_input = 0;
336 recalc_input_lines: function() {
337 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
338 this.height_input = this.input_lines.length;
340 msg_into_lines_of_width: function(msg, width) {
343 for (let i = 0, x = 0; i < msg.length; i++, x++) {
344 if (x >= width || msg[i] == "\n") {
349 if (msg[i] != "\n") {
356 log_msg: function(msg) {
358 while (this.log.length > 100) {
363 log_help: function() {
364 this.log_msg("HELP:");
365 this.log_msg("chat mode commands:");
366 this.log_msg(" " + command_char_selector.value + "nick NAME - re-name yourself to NAME");
367 this.log_msg(" " + command_char_selector.value + "msg USER TEXT - send TEXT to USER");
368 this.log_msg(" " + command_char_selector.value + "help - show this help");
369 this.log_msg(" " + command_char_selector.value + "p or " + command_char_selector.value + "play - switch to play mode");
370 this.log_msg(" " + command_char_selector.value + "? or " + command_char_selector.value + "study - switch to study mode");
371 this.log_msg("commands common to study and play mode:");
372 this.log_msg(" " + this.movement_keys_desc + " - move");
373 this.log_msg(" c - switch to chat mode");
374 this.log_msg("commands specific to play mode:");
375 this.log_msg(" e - write following ASCII character");
376 this.log_msg(" f - flatten surroundings");
377 this.log_msg(" ? - switch to study mode");
378 this.log_msg("commands specific to study mode:");
379 this.log_msg(" e - annotate terrain");
380 this.log_msg(" p - switch to play mode");
382 draw_map: function() {
385 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
386 if (j == game.map_size[1]) {
387 map_lines.push(line);
391 line.push(game.map[i]);
393 map_lines.push(line);
394 let center_pos = [Math.floor(game.map_size[0] / 2),
395 Math.floor(game.map_size[1] / 2)];
396 for (const thing_id in game.things) {
397 let t = game.things[thing_id];
398 map_lines[t.position[0]][t.position[1]] = '@';
399 if (game.player_id == thing_id) {
400 center_pos = t.position;
403 if (tui.mode.shows_info) {
404 map_lines[explorer.position[0]][explorer.position[1]] = '?';
405 center_pos = explorer.position;
407 let offset = [(terminal.rows / 2) - center_pos[0],
408 this.window_width / 2 - center_pos[1]];
409 for (let term_y = offset[0], map_y = 0;
410 term_y < terminal.rows && map_y < game.map_size[0];
413 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
414 terminal.write(term_y, offset[1], to_draw);
418 draw_mode_line: function() {
419 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
421 draw_turn_line: function(n) {
422 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
424 draw_history: function() {
425 let log_display_lines = [];
426 for (let line of this.log) {
427 log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
429 for (let y = terminal.rows - 1 - this.height_input,
430 i = log_display_lines.length - 1;
431 y >= this.height_header && i >= 0;
433 terminal.write(y, this.window_width, log_display_lines[i]);
436 draw_info: function() {
437 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
438 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
439 terminal.write(y, this.window_width, lines[i]);
442 draw_input: function() {
443 if (this.mode.has_input_prompt) {
444 for (let y = terminal.rows - this.height_input, i = 0; i < this.input_lines.length; y++, i++) {
445 terminal.write(y, this.window_width, this.input_lines[i]);
449 full_refresh: function() {
450 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
451 if (this.mode.is_intro) {
456 this.draw_turn_line();
457 this.draw_mode_line();
458 if (this.mode.shows_info) {
474 this.map_size = [0,0];
478 get_thing: function(id_, create_if_not_found=false) {
479 if (id_ in game.things) {
480 return game.things[id_];
481 } else if (create_if_not_found) {
482 let t = new Thing([0,0]);
483 game.things[id_] = t;
492 server.init(websocket_location);
497 move: function(direction) {
499 try_pos[0] = this.position[0];
500 try_pos[1] = this.position[1];
501 if (direction == 'left') {
503 } else if (direction == 'right') {
505 } else if (direction == 'up') {
507 } else if (direction == 'down') {
510 if (!(try_pos[0] < 0) &&
512 !(try_pos[0] >= game.map_size[0])
513 && !(try_pos[1] >= game.map_size[1])) {
514 this.position = try_pos;
519 update_info_db: function(yx, str) {
520 this.info_db[yx] = str;
521 if (tui.mode == mode_study) {
525 empty_info_db: function() {
527 if (tui.mode == mode_study) {
531 query_info: function() {
532 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
534 get_info: function() {
536 let position_i = this.position[0] * game.map_size[1] + this.position[1];
537 info += "TERRAIN: " + game.map[position_i] + "\n";
538 for (let t_id in game.things) {
539 let t = game.things[t_id];
540 if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
543 info += ": " + t.name_;
548 if (this.position in game.portals) {
549 info += "PORTAL: " + game.portals[this.position] + "\n";
551 if (this.position in this.info_db) {
552 info += "ANNOTATIONS: " + this.info_db[this.position];
558 annotate: function(msg) {
559 if (msg.length == 0) {
560 msg = " "; // triggers annotation deletion
562 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
564 set_portal: function(msg) {
565 if (msg.length == 0) {
566 msg = " "; // triggers portal deletion
568 server.send(["PORTAL", unparser.to_yx(explorer.position), msg]);
572 tui.inputEl.addEventListener('input', (event) => {
573 if (tui.mode.has_input_prompt) {
574 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
575 if (tui.inputEl.value.length > max_length) {
576 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
578 tui.recalc_input_lines();
580 } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
581 server.send(["TASK:WRITE", tui.inputEl.value[0]]);
582 tui.switch_mode(mode_play);
583 } else if (tui.mode == mode_teleport) {
584 if (['Y', 'y'].includes(tui.inputEl.value[0])) {
585 server.reconnect_to(tui.teleport_target);
587 tui.log_msg("@ teleportation aborted");
588 tui.switch_mode(mode_play);
592 tui.inputEl.addEventListener('keydown', (event) => {
593 if (event.key == 'Enter') {
594 event.preventDefault();
596 if (tui.mode == mode_login && event.key == 'Enter') {
597 server.send(['LOGIN', tui.inputEl.value]);
598 tui.switch_mode(mode_login);
599 } else if (tui.mode == mode_portal && event.key == 'Enter') {
600 explorer.set_portal(tui.inputEl.value);
601 tui.switch_mode(mode_study, true);
602 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
603 explorer.annotate(tui.inputEl.value);
604 tui.switch_mode(mode_study, true);
605 } else if (tui.mode == mode_chat && event.key == 'Enter') {
606 let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
607 if (tokens.length > 0 && tokens[0].length > 0) {
608 if (tokens[0][0] == command_char_selector.value) {
609 if (tokens[0].slice(1) == 'play' || tokens[0].slice(1) == 'p') {
610 tui.switch_mode(mode_play);
611 } else if (tokens[0].slice(1) == 'study' || tokens[0].slice(1) == '?') {
612 tui.switch_mode(mode_study);
613 } else if (tokens[0].slice(1) == 'help') {
615 } else if (tokens[0].slice(1) == 'nick') {
616 if (tokens.length > 1) {
617 server.send(['LOGIN', tokens[1]]);
619 tui.log_msg('? need login name');
621 } else if (tokens[0].slice(1) == 'msg') {
622 if (tokens.length > 2) {
623 let msg = tui.inputEl.value.slice(token_starts[2]);
624 server.send(['QUERY', tokens[1], msg]);
626 tui.log_msg('? need message target and message');
628 } else if (tokens[0].slice(1) == 'reconnect') {
629 if (tokens.length > 1) {
630 server.reconnect_to(tokens[1]);
635 tui.log_msg('? unknown command');
638 server.send(['ALL', tui.inputEl.value]);
640 } else if (tui.inputEl.valuelength > 0) {
641 server.send(['ALL', tui.inputEl.value]);
645 } else if (tui.mode == mode_play) {
646 if (event.key === 'c') {
647 event.preventDefault();
648 tui.switch_mode(mode_chat);
649 } else if (event.key === 'e') {
650 event.preventDefault();
651 tui.switch_mode(mode_edit);
652 } else if (event.key === '?') {
653 tui.switch_mode(mode_study);
654 } else if (event.key === 'F1') {
656 } else if (event.key === 'f') {
657 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
658 } else if (event.key === tui.key_left) {
659 server.send(['TASK:MOVE', 'LEFT']);
660 } else if (event.key === tui.key_right) {
661 server.send(['TASK:MOVE', 'RIGHT']);
662 } else if (event.key === tui.key_up) {
663 server.send(['TASK:MOVE', 'UP']);
664 } else if (event.key === tui.key_down) {
665 server.send(['TASK:MOVE', 'DOWN']);
667 } else if (tui.mode == mode_study) {
668 if (event.key === 'c') {
669 event.preventDefault();
670 tui.switch_mode(mode_chat);
671 } else if (event.key == 'p') {
672 tui.switch_mode(mode_play);
673 } else if (event.key === 'P') {
674 event.preventDefault();
675 tui.switch_mode(mode_portal);
676 } else if (event.key === tui.key_left) {
677 explorer.move('left');
678 } else if (event.key === tui.key_right) {
679 explorer.move('right');
680 } else if (event.key === tui.key_up) {
682 } else if (event.key === tui.key_down) {
683 explorer.move('down');
684 } else if (event.key === 'e') {
685 event.preventDefault();
686 tui.switch_mode(mode_annotate);
691 wasd_selector.addEventListener('input', function() {
694 rows_selector.addEventListener('input', function() {
695 if (rows_selector.value % 2 != 0) {
698 terminal.initialize();
701 cols_selector.addEventListener('input', function() {
702 if (cols_selector.value % 4 != 0) {
705 terminal.initialize();
706 tui.window_width = terminal.cols / 2,
709 window.setInterval(function() {
710 if (!(['input', 'n_cols', 'n_rows', 'WASD_selector', 'command_char'].includes(document.activeElement.id))) {