home · contact · privacy
Server/py: Fix and extend command_makeworld().
[plomrogue] / plomrogue-server.py
index 7c67024f39d6e7e4b17aa00912feb839c7597e9f..5b30855eefc7d2d52eb4906b4192bf1f4c265762 100755 (executable)
@@ -117,20 +117,22 @@ def record(command):
 
 
 def save_world():
-    """Save all commands needed to reconstruct current world state.""" 
+    """Save all commands needed to reconstruct current world state."""
     # TODO: Misses same optimizations as record() from the original record().
-    # TODO: How to handle strings that contain ' or "?
+
+    def quote(string):
+        string = string.replace("\u005C", '\u005C\u005C')
+        return '"' + string.replace('"', '\u005C"') + '"'
 
     def mapsetter(key):
         def helper(id):
             string = ""
             if world_db["Things"][id][key]:
-                memmap = world_db["Things"][id][key]
+                rmap = world_db["Things"][id][key]
                 length = world_db["MAP_LENGTH"]
                 for i in range(world_db["MAP_LENGTH"]):
-                    string = string + key + " " + str(i) + " '" + \
-                             memmap[i * length:(i * length) + length].decode() \
-                             + "'\n"
+                    line = rmap[i * length:(i * length) + length].decode()
+                    string = string + key + " " + str(i) + quote(line) + "\n"
             return string
         return helper
 
@@ -148,7 +150,7 @@ def save_world():
             for key in world_db[category][id]:
                 if not key in special_keys:
                     x = world_db[category][id][key]
-                    argument = "'" + x + "'" if str == type(x) else str(x)
+                    argument = quote(x) if str == type(x) else str(x)
                     string = string + key + " " + argument + "\n"
                 elif special_keys[key]:
                     string = string + special_keys[key](id)
@@ -289,8 +291,8 @@ def play_game():
 
 
 def remake_map():
-    # DUMMY.
-    print("I'd (re-)make the map now, if only I knew how.")
+    # DUMMY map creator.
+    world_db["MAP"] = bytearray(b'.' * (world_db["MAP_LENGTH"] ** 2))
 
 
 def set_world_inactive():
@@ -359,15 +361,38 @@ def command_seedmap(seed_string):
 
 def command_makeworld(seed_string):
     # DUMMY.
-    setter(None, "SEED_MAP", 0, 4294967295)(seed_string)
     setter(None, "SEED_RANDOMNESS", 0, 4294967295)(seed_string)
-    # TODO: Test for existence of player thing and 'wait' thing action?
+    player_will_be_generated = False
+    for ThingType in world_db["ThingTypes"]:
+        if 0 == ThingType:
+            if 0 < world_db["ThingTypes"][ThingType]["TT_START_NUMBER"]:
+                player_will_be_generated = True
+            break
+    if not player_will_be_generated:
+        print("Ignoring beyond SEED_MAP: " +
+              "No player type with start number >0 defined.")
+        return
+    wait_action = False
+    for ThingAction in world_db["ThingActions"]:
+        if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
+            wait_action = True
+    if not wait_action:
+        print("Ignoring beyond SEED_MAP: " +
+              "No thing action with name 'wait' defined.")
+        return
+    setter(None, "SEED_MAP", 0, 4294967295)(seed_string)
+    world_db["Things"] = {}
+    remake_map()
+    world_db["WORLD_ACTIVE"] = 1
+    # TODO: Generate things (player first, with updated memory), set TURN 1,
+    atomic_write(io_db["path_out"], "NEW_WORLD\n", do_append=True)
 
 
 def command_maplength(maplength_string):
     # DUMMY.
     set_world_inactive()
-    # TODO: remove things, map
+    # TODO: remove map (is this necessary? no memory management trouble …)
+    world_db["Things"] = {}
     setter(None, "MAP_LENGTH", 1, 256)(maplength_string)
 
 
@@ -375,18 +400,25 @@ def command_worldactive(worldactive_string):
     # DUMMY.
     val = integer_test(worldactive_string, 0, 1)
     if val:
-        if 0 != world_db["WORLD_ACTIVE"] and 0 == val:
-            set_world_inactive()
+        if 0 != world_db["WORLD_ACTIVE"]:
+            if 0 == val:
+                set_world_inactive()
+            else:
+                print("World already active.")
         elif 0 == world_db["WORLD_ACTIVE"]:
             wait_exists = False
+            for ThingAction in world_db["ThingActions"]:
+                if "wait" == ThingAction["TA_NAME"]:
+                    wait_exists = True
+                    break
             player_exists = False
-            map_exists = False
-            # TODO: perform tests:
-            # Is there thing action of name 'wait'?
-            # Is there a player thing?
-            # Is there a map?
+            for Thing in world_db["Things"]:
+                if 0 == ThingAction["T_ID"]:
+                    player_exists = True
+                    break
+            map_exists = "MAP" in world_db
             if wait_exists and player_exists and map_exists:
-                # TODO: rebuild al things' FOVs, map memories
+                # TODO: rebuild all things' FOVs, map memories
                 world_db["WORLD_ACTIVE"] = 1