From d4cf3dcecd55a94cae437a7d258f615976857a71 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 25 Nov 2020 01:39:29 +0100
Subject: [PATCH] Put map editing into dedicated map editing mode.

---
 config.json          |   3 +-
 rogue_chat.html      | 133 ++++++++++++++++++++++++-------------------
 rogue_chat_curses.py |  60 ++++++++++++-------
 3 files changed, 115 insertions(+), 81 deletions(-)

diff --git a/config.json b/config.json
index 3538488..7cbbbf5 100644
--- a/config.json
+++ b/config.json
@@ -5,7 +5,8 @@
     "switch_to_annotate": "M",
     "switch_to_portal": "T",
     "switch_to_study": "?",
-    "switch_to_edit": "m",
+    "switch_to_edit": "E",
+    "switch_to_write": "m",
     "switch_to_admin": "A",
     "switch_to_control_pw_pw": "C",
     "switch_to_control_tile_type": "Q",
diff --git a/rogue_chat.html b/rogue_chat.html
index cf82cf9..ce6cf4c 100644
--- a/rogue_chat.html
+++ b/rogue_chat.html
@@ -49,32 +49,26 @@ terminal columns: <input id="n_cols" type="number" step=4 min=80 value=80 />
   <tr>
     <td><button id="switch_to_play">play mode</button></td>
     <td>
-      <table>
-        <tr>
-          <td><button id="take_thing">take thing</button></td>
-          <td><button id="switch_to_annotate">annotate tile</button></td>
-          <td><button id="switch_to_edit">change tile</button></td>
-          <td><button id="teleport">teleport</button></td>
-        </tr>
-        <tr>
-          <td><button id="drop_thing">drop thing</button></td>
-          <td><button id="flatten">flatten surroundings</button></td>
-          <td><button id="switch_to_password">change tile editing password</button></td>
-          <td><button id="switch_to_portal">edit portal link</button></td>
-        </tr>
-      </table>
+      <button id="take_thing">take thing</button>
+      <button id="teleport">teleport</button>
+      <button id="drop_thing">drop thing</button>
     </td>
   </tr>
+  <tr>
+    <td><button id="switch_to_edit">map edit mode</button></td>
+    <td>
+      <button id="switch_to_write">change tile</button>
+      <button id="flatten">flatten surroundings</button>
+      <button id="switch_to_password">change tile editing password</button>
+      <button id="switch_to_annotate">annotate tile</button>
+      <button id="switch_to_portal">edit portal link</button>
+    </td>
+  </tr>
+  <tr>
     <td><button id="switch_to_admin_enter">admin mode</button></td>
     <td>
-      <table>
-        <tr>
-          <td><button id="switch_to_control_pw_type">change tile control password</button></td>
-        </tr>
-        <tr>
-          <td><button id="switch_to_control_tile_type">change tiles control</button></td>
-        </tr>
-      </table>
+      <button id="switch_to_control_pw_type">change tile control password</button>
+      <button id="switch_to_control_tile_type">change tiles control</button>
     </td>
   <tr>
   </tr>
@@ -99,7 +93,8 @@ terminal columns: <input id="n_cols" type="number" step=4 min=80 value=80 />
 <li><input id="key_switch_to_chat" type="text" value="t" />
 <li><input id="key_switch_to_play" type="text" value="p" />
 <li><input id="key_switch_to_study" type="text" value="?" />
-<li><input id="key_switch_to_edit" type="text" value="m" />
+<li><input id="key_switch_to_edit" type="text" value="E" />
+<li><input id="key_switch_to_write" type="text" value="m" />
 <li><input id="key_switch_to_password" type="text" value="P" />
 <li><input id="key_switch_to_admin_enter" type="text" value="A" />
 <li><input id="key_switch_to_control_pw_type" type="text" value="C" />
@@ -111,8 +106,8 @@ terminal columns: <input id="n_cols" type="number" step=4 min=80 value=80 />
 </div>
 <script>
 "use strict";
-//let websocket_location = "wss://plomlompom.com/rogue_chat/";
-let websocket_location = "ws://localhost:8000/";
+let websocket_location = "wss://plomlompom.com/rogue_chat/";
+//let websocket_location = "ws://localhost:8000/";
 
 let mode_helps = {
     'play': {
@@ -123,7 +118,11 @@ let mode_helps = {
         'short': 'study',
         'long': 'This mode allows you to study the map and its tiles in detail.  Move the question mark over a tile, and the right half of the screen will show detailed information on it.'},
     'edit': {
-        'short': 'terrain edit',
+        'short': 'map edit',
+        'long': 'This mode allows you to change the map in various ways.'
+    },
+    'write': {
+        'short': 'terrain write',
         'long': 'This mode allows you to change the map tile you currently stand on (if your map editing password authorizes you so).  Just enter any printable ASCII character to imprint it on the ground below you.'
     },
     'control_pw_type': {
@@ -392,7 +391,7 @@ let server = {
             };
         } else if (tokens[0] === 'TASKS') {
             game.tasks = tokens[1].split(',');
-            tui.mode_edit.legal = game.tasks.includes('WRITE');
+            tui.mode_write.legal = game.tasks.includes('WRITE');
         } else if (tokens[0] === 'THING_TYPE') {
             game.thing_types[tokens[1]] = tokens[2]
         } else if (tokens[0] === 'TERRAIN') {
@@ -542,7 +541,8 @@ let tui = {
   mode_annotate: new Mode('annotate', true, true),
   mode_play: new Mode('play'),
   mode_study: new Mode('study', false, true),
-  mode_edit: new Mode('edit', false, false, false, true),
+  mode_write: new Mode('write', false, false, false, true),
+  mode_edit: new Mode('edit'),
   mode_control_pw_type: new Mode('control_pw_type',
                                   false, false, false, true),
   mode_portal: new Mode('portal', true, true),
@@ -554,14 +554,15 @@ let tui = {
                                    false, false, false, true),
   mode_control_tile_draw: new Mode('control_tile_draw'),
   init: function() {
-      this.mode_play.available_modes = ["chat", "study", "edit",
-                                        "annotate", "portal",
-                                        "password", "admin_enter"];
-      this.mode_study.available_modes = ["chat", "play", "admin_enter"];
-      this.mode_admin.available_modes = ["chat", "play", "study",
-                                         "control_pw_type",
-                                         "control_tile_type"];
-      this.mode_control_tile_draw.available_modes = ["admin_enter"];
+      this.mode_play.available_modes = ["chat", "study", "edit", "admin_enter"]
+      this.mode_study.available_modes = ["chat", "play", "admin_enter", "edit"]
+      this.mode_admin.available_modes = ["control_pw_type",
+                                         "control_tile_type", "chat",
+                                         "study", "play", "edit"]
+      this.mode_control_tile_draw.available_modes = ["admin"]
+      this.mode_edit.available_modes = ["write", "annotate", "portal",
+                                        "password", "chat", "study", "play",
+                                        "admin_enter"]
       this.mode = this.mode_waiting_for_server;
       this.inputEl = document.getElementById("input");
       this.inputEl.focus();
@@ -627,7 +628,7 @@ let tui = {
         el.disabled = true;
     }
     document.getElementById("help").disabled = false;
-    if (this.mode.name == 'play' || this.mode.name == 'study' || this.mode.name == 'control_tile_draw') {
+    if (this.mode.name == 'play' || this.mode.name == 'study' || this.mode.name == 'control_tile_draw' || this.mode.name == 'edit') {
         for (const move_key of document.querySelectorAll('[id^="move_"]')) {
             move_key.disabled = false;
         }
@@ -641,6 +642,9 @@ let tui = {
     if (!this.mode.is_intro && this.mode.name != 'chat') {
         document.getElementById("switch_to_chat").disabled = false;
     }
+    if (!this.mode.is_intro && this.mode.name != 'edit') {
+        document.getElementById("switch_to_edit").disabled = false;
+    }
     if (!this.mode.is_intro && this.mode.name != 'admin' && this.mode.name != 'admin_enter') {
         document.getElementById("switch_to_admin_enter").disabled = false;
     }
@@ -657,17 +661,17 @@ let tui = {
         if (game.tasks.includes('DROP')) {
             document.getElementById("drop_thing").disabled = false;
         }
-        if (game.tasks.includes('FLATTEN_SURROUNDINGS')) {
-            document.getElementById("flatten").disabled = false;
-        }
         if (game.tasks.includes('MOVE')) {
         }
         document.getElementById("teleport").disabled = false;
+    } else if (this.mode.name == 'edit') {
+        if (game.tasks.includes('FLATTEN_SURROUNDINGS')) {
+            document.getElementById("flatten").disabled = false;
+        }
         document.getElementById("switch_to_annotate").disabled = false;
-        document.getElementById("switch_to_edit").disabled = false;
+        document.getElementById("switch_to_write").disabled = false;
         document.getElementById("switch_to_portal").disabled = false;
         document.getElementById("switch_to_password").disabled = false;
-        document.getElementById("switch_to_admin_enter").disabled = false;
     } else if (this.mode.name == 'admin') {
         document.getElementById("switch_to_control_pw_type").disabled = false;
         document.getElementById("switch_to_control_tile_type").disabled = false;
@@ -939,9 +943,6 @@ let tui = {
           if (game.tasks.includes('DROP')) {
               content += "[" + this.keys.drop_thing + "] – drop carried thing\n";
           }
-          if (game.tasks.includes('FLATTEN_SURROUNDINGS')) {
-              content += "[" + tui.keys.flatten + "] – flatten player's surroundings\n";
-          }
           content += "[" + tui.keys.teleport + "] – teleport to other space\n";
           content += '\n';
       } else if (this.mode.name == 'study') {
@@ -949,10 +950,17 @@ let tui = {
           content += '[' + movement_keys_desc + '] – move question mark\n';
           content += '[' + this.keys.toggle_map_mode + '] – toggle view between terrain, annotations, and password protection areas\n';
           content += '\n';
+      } else if (this.mode.name == 'edit') {
+          content += "Available actions:\n";
+          if (game.tasks.includes('FLATTEN_SURROUNDINGS')) {
+              content += "[" + tui.keys.flatten + "] – flatten player's surroundings\n";
+          }
+          content += '\n';
       } else if (this.mode.name == 'chat') {
           content += '/nick NAME – re-name yourself to NAME\n';
           content += '/' + this.keys.switch_to_play + ' or /play – switch to play mode\n';
           content += '/' + this.keys.switch_to_study + ' or /study – switch to study mode\n';
+          content += '/' + this.keys.switch_to_edit + ' or /edit – switch to map edit mode\n';
           content += '/' + this.keys.switch_to_admin_enter + ' or /admin – switch to admin mode\n';
       }
       content += this.mode.list_available_modes();
@@ -1168,9 +1176,9 @@ tui.inputEl.addEventListener('input', (event) => {
             tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
         };
         tui.recalc_input_lines();
-    } else if (tui.mode.name == 'edit' && tui.inputEl.value.length > 0) {
+    } else if (tui.mode.name == 'write' && tui.inputEl.value.length > 0) {
         server.send(["TASK:WRITE", tui.inputEl.value[0], tui.password]);
-        tui.switch_mode('play');
+        tui.switch_mode('edit');
     } else if (tui.mode.name == 'control_pw_type' && tui.inputEl.value.length > 0) {
         tui.tile_control_char = tui.inputEl.value[0];
         tui.switch_mode('control_pw_pw');
@@ -1206,16 +1214,16 @@ tui.inputEl.addEventListener('keydown', (event) => {
         tui.switch_mode('admin');
     } else if (tui.mode.name == 'portal' && event.key == 'Enter') {
         explorer.set_portal(tui.inputEl.value);
-        tui.switch_mode('play');
+        tui.switch_mode('edit');
     } else if (tui.mode.name == 'annotate' && event.key == 'Enter') {
         explorer.annotate(tui.inputEl.value);
-        tui.switch_mode('play');
+        tui.switch_mode('edit');
     } else if (tui.mode.name == 'password' && event.key == 'Enter') {
         if (tui.inputEl.value.length == 0) {
             tui.inputEl.value = " ";
         }
         tui.password = tui.inputEl.value
-        tui.switch_mode('play');
+        tui.switch_mode('edit');
     } else if (tui.mode.name == 'admin_enter' && event.key == 'Enter') {
         server.send(['BECOME_ADMIN', tui.inputEl.value]);
         tui.switch_mode('play');
@@ -1227,6 +1235,8 @@ tui.inputEl.addEventListener('keydown', (event) => {
                     tui.switch_mode('play');
                 } else if (tokens[0].slice(1) == 'study' || tokens[0][1] == tui.keys.switch_to_study) {
                     tui.switch_mode('study');
+                } else if (tokens[0].slice(1) == 'edit' || tokens[0][1] == tui.keys.switch_to_edit) {
+                    tui.switch_mode('edit');
                 } else if (tokens[0].slice(1) == 'admin' || tokens[0][1] == tui.keys.switch_to_admin_enter) {
                     tui.switch_mode('admin_enter');
                 } else if (tokens[0].slice(1) == 'nick') {
@@ -1248,9 +1258,6 @@ tui.inputEl.addEventListener('keydown', (event) => {
     } else if (tui.mode.name == 'play') {
           if (tui.mode.mode_switch_on_key(event)) {
               null;
-          } else if (event.key === tui.keys.flatten
-                     && game.tasks.includes('FLATTEN_SURROUNDINGS')) {
-              server.send(["TASK:FLATTEN_SURROUNDINGS", tui.password]);
           } else if (event.key === tui.keys.take_thing
                      && game.tasks.includes('PICK_UP')) {
               server.send(["TASK:PICK_UP"]);
@@ -1262,12 +1269,6 @@ tui.inputEl.addEventListener('keydown', (event) => {
               server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
           } else if (event.key === tui.keys.teleport) {
               game.teleport();
-          } else if (event.key === tui.keys.switch_to_portal) {
-              event.preventDefault();
-              tui.switch_mode('portal');
-          } else if (event.key === tui.keys.switch_to_annotate) {
-              event.preventDefault();
-              tui.switch_mode('annotate');
           };
     } else if (tui.mode.name == 'study') {
         if (tui.mode.mode_switch_on_key(event)) {
@@ -1293,6 +1294,16 @@ tui.inputEl.addEventListener('keydown', (event) => {
         if (tui.mode.mode_switch_on_key(event)) {
               null;
         };
+    } else if (tui.mode.name == 'edit') {
+        if (tui.mode.mode_switch_on_key(event)) {
+              null;
+        } else if (event.key in tui.movement_keys
+                   && game.tasks.includes('MOVE')) {
+            server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
+        } else if (event.key === tui.keys.flatten
+                   && game.tasks.includes('FLATTEN_SURROUNDINGS')) {
+            server.send(["TASK:FLATTEN_SURROUNDINGS", tui.password]);
+        }
     }
     tui.full_refresh();
 }, false);
@@ -1355,6 +1366,10 @@ document.getElementById("switch_to_edit").onclick = function() {
     tui.switch_mode('edit');
     tui.full_refresh();
 };
+document.getElementById("switch_to_write").onclick = function() {
+    tui.switch_mode('write');
+    tui.full_refresh();
+};
 document.getElementById("switch_to_annotate").onclick = function() {
     tui.switch_mode('annotate');
     tui.full_refresh();
diff --git a/rogue_chat_curses.py b/rogue_chat_curses.py
index 3cd5914..cdc2c01 100755
--- a/rogue_chat_curses.py
+++ b/rogue_chat_curses.py
@@ -20,7 +20,11 @@ mode_helps = {
         'short': 'study',
         'long': 'This mode allows you to study the map and its tiles in detail.  Move the question mark over a tile, and the right half of the screen will show detailed information on it.'},
     'edit': {
-        'short': 'terrain edit',
+        'short': 'map edit',
+        'long': 'This mode allows you to change the map in various ways.'
+    },
+    'write': {
+        'short': 'terrain write',
         'long': 'This mode allows you to change the map tile you currently stand on (if your map editing password authorizes you so).  Just enter any printable ASCII character to imprint it on the ground below you.'
     },
     'control_pw_type': {
@@ -241,7 +245,7 @@ cmd_ANNOTATION.argtypes = 'yx_tuple:nonneg string'
 
 def cmd_TASKS(game, tasks_comma_separated):
     game.tasks = tasks_comma_separated.split(',')
-    game.tui.mode_edit.legal = 'WRITE' in game.tasks
+    game.tui.mode_write.legal = 'WRITE' in game.tasks
 cmd_TASKS.argtypes = 'string'
 
 def cmd_THING_TYPE(game, thing_type, symbol_hint):
@@ -347,7 +351,8 @@ class TUI:
     mode_admin = Mode('admin')
     mode_play = Mode('play')
     mode_study = Mode('study', shows_info=True)
-    mode_edit = Mode('edit', is_single_char_entry=True)
+    mode_write = Mode('write', is_single_char_entry=True)
+    mode_edit = Mode('edit')
     mode_control_pw_type = Mode('control_pw_type', is_single_char_entry=True)
     mode_control_pw_pw = Mode('control_pw_pw', has_input_prompt=True)
     mode_control_tile_type = Mode('control_tile_type', is_single_char_entry=True)
@@ -364,14 +369,15 @@ class TUI:
     def __init__(self, host):
         import os
         import json
-        self.mode_play.available_modes = ["chat", "study", "edit",
-                                          "annotate", "portal",
-                                          "password", "admin_enter"]
-        self.mode_study.available_modes = ["chat", "play", "admin_enter"]
-        self.mode_admin.available_modes = ["chat", "play", "study",
-                                           "control_pw_type",
-                                           "control_tile_type"]
+        self.mode_play.available_modes = ["chat", "study", "edit", "admin_enter"]
+        self.mode_study.available_modes = ["chat", "play", "admin_enter", "edit"]
+        self.mode_admin.available_modes = ["control_pw_type",
+                                           "control_tile_type", "chat",
+                                           "study", "play", "edit"]
         self.mode_control_tile_draw.available_modes = ["admin"]
+        self.mode_edit.available_modes = ["write", "annotate", "portal",
+                                          "password", "chat", "study", "play",
+                                          "admin_enter"]
         self.host = host
         self.game = Game()
         self.game.tui = self
@@ -390,7 +396,8 @@ class TUI:
             'switch_to_annotate': 'M',
             'switch_to_portal': 'T',
             'switch_to_study': '?',
-            'switch_to_edit': 'm',
+            'switch_to_edit': 'E',
+            'switch_to_write': 'm',
             'switch_to_admin_enter': 'A',
             'switch_to_control_pw_type': 'C',
             'switch_to_control_tile_type': 'Q',
@@ -720,8 +727,6 @@ class TUI:
                     content += "[%s] – take thing under player\n" % self.keys['take_thing']
                 if 'DROP' in self.game.tasks:
                     content += "[%s] – drop carried thing\n" % self.keys['drop_thing']
-                if 'FLATTEN_SURROUNDINGS' in self.game.tasks:
-                    content += "[%s] – flatten player's surroundings\n" % self.keys['flatten']
                 content += '[%s] – teleport to other space\n' % self.keys['teleport']
                 content += '\n'
             elif self.mode.name == 'study':
@@ -729,10 +734,16 @@ class TUI:
                 content += '[%s] – move question mark\n' % ','.join(self.movement_keys)
                 content += '[%s] – toggle view between terrain, annotations, and password protection areas\n' % self.keys['toggle_map_mode']
                 content += '\n'
+            elif self.mode.name == 'edit':
+                content += "Available actions:\n"
+                if 'FLATTEN_SURROUNDINGS' in self.game.tasks:
+                    content += "[%s] – flatten player's surroundings\n" % self.keys['flatten']
+                content += '\n'
             elif self.mode.name == 'chat':
                 content += '/nick NAME – re-name yourself to NAME\n'
                 content += '/%s or /play – switch to play mode\n' % self.keys['switch_to_play']
                 content += '/%s or /study – switch to study mode\n' % self.keys['switch_to_study']
+                content += '/%s or /edit – switch to map edit mode\n' % self.keys['switch_to_edit']
                 content += '/%s or /admin – switch to admin mode\n' % self.keys['switch_to_admin_enter']
             content += self.mode.list_available_modes(self)
             for i in range(self.size.y):
@@ -834,7 +845,7 @@ class TUI:
                     self.input_ = ' '
                 self.password = self.input_
                 self.input_ = ""
-                self.switch_mode('play')
+                self.switch_mode('edit')
             elif self.mode.name == 'admin_enter' and key == '\n':
                 self.send('BECOME_ADMIN ' + quote(self.input_))
                 self.input_ = ""
@@ -847,6 +858,8 @@ class TUI:
                         self.switch_mode('play')
                     elif self.input_ in {'/' + self.keys['switch_to_study'], '/study'}:
                         self.switch_mode('study')
+                    elif self.input_ in {'/' + self.keys['switch_to_edit'], '/edit'}:
+                        self.switch_mode('edit')
                     elif self.input_ in {'/' + self.keys['switch_to_admin_enter'], '/admin'}:
                         self.switch_mode('admin_enter')
                     elif self.input_.startswith('/nick'):
@@ -866,14 +879,14 @@ class TUI:
                 self.send('ANNOTATE %s %s %s' % (self.explorer, quote(self.input_),
                                                  quote(self.password)))
                 self.input_ = ""
-                self.switch_mode('play')
+                self.switch_mode('edit')
             elif self.mode.name == 'portal' and key == '\n':
                 if self.input_ == '':
                     self.input_ = ' '
                 self.send('PORTAL %s %s %s' % (self.explorer, quote(self.input_),
                                                quote(self.password)))
                 self.input_ = ""
-                self.switch_mode('play')
+                self.switch_mode('edit')
             elif self.mode.name == 'study':
                 if self.mode.mode_switch_on_key(self, key):
                     continue
@@ -889,9 +902,6 @@ class TUI:
             elif self.mode.name == 'play':
                 if self.mode.mode_switch_on_key(self, key):
                     continue
-                if key == self.keys['flatten'] and\
-                     'FLATTEN_SURROUNDINGS' in self.game.tasks:
-                    self.send('TASK:FLATTEN_SURROUNDINGS ' + quote(self.password))
                 elif key == self.keys['take_thing'] and 'PICK_UP' in self.game.tasks:
                     self.send('TASK:PICK_UP')
                 elif key == self.keys['drop_thing'] and 'DROP' in self.game.tasks:
@@ -906,9 +916,9 @@ class TUI:
                         self.log_msg('? not standing on portal')
                 elif key in self.movement_keys and 'MOVE' in self.game.tasks:
                     self.send('TASK:MOVE ' + self.movement_keys[key])
-            elif self.mode.name == 'edit':
+            elif self.mode.name == 'write':
                 self.send('TASK:WRITE %s %s' % (key, quote(self.password)))
-                self.switch_mode('play')
+                self.switch_mode('edit')
             elif self.mode.name == 'control_pw_type':
                 self.tile_control_char = key
                 self.switch_mode('control_pw_pw')
@@ -923,6 +933,14 @@ class TUI:
             elif self.mode.name == 'admin':
                 if self.mode.mode_switch_on_key(self, key):
                     continue
+            elif self.mode.name == 'edit':
+                if self.mode.mode_switch_on_key(self, key):
+                    continue
+                if key == self.keys['flatten'] and\
+                     'FLATTEN_SURROUNDINGS' in self.game.tasks:
+                    self.send('TASK:FLATTEN_SURROUNDINGS ' + quote(self.password))
+                elif key in self.movement_keys and 'MOVE' in self.game.tasks:
+                    self.send('TASK:MOVE ' + self.movement_keys[key])
 
 if len(sys.argv) != 2:
     raise ArgError('wrong number of arguments, need game host')
-- 
2.30.2