Magic-number elimination.
[open-adventure.git] / make_dungeon.py
index f90ad3291e29ac6f27b132cfcb575fe5991a5b3f..bae97f210ad7a9759bb8b10c27a034a2f23840d0 100755 (executable)
@@ -1,4 +1,6 @@
 #!/usr/bin/env python3
 #!/usr/bin/env python3
+# SPDX-FileCopyrightText: Eric S. Raymond <esr@thyrsus.com>
+# SPDX-License-Identifier: BSD-2-Clause
 """
 This is the open-adventure dungeon generator. It consumes a YAML description of
 the dungeon and outputs a dungeon.h and dungeon.c pair of C code files.
 """
 This is the open-adventure dungeon generator. It consumes a YAML description of
 the dungeon and outputs a dungeon.h and dungeon.c pair of C code files.
@@ -6,10 +8,10 @@ the dungeon and outputs a dungeon.h and dungeon.c pair of C code files.
 The nontrivial part of this is the compilation of the YAML for
 movement rules to the travel array that's actually used by
 playermove().
 The nontrivial part of this is the compilation of the YAML for
 movement rules to the travel array that's actually used by
 playermove().
-
-Copyright (c) 2017 by Eric S. Raymond
-SPDX-License-Identifier: BSD-2-clause
 """
 """
+
+# pylint: disable=consider-using-f-string,line-too-long,invalid-name,missing-function-docstring,too-many-branches,global-statement,multiple-imports,too-many-locals,too-many-statements,too-many-nested-blocks,no-else-return,raise-missing-from,redefined-outer-name
+
 import sys, yaml
 
 YAML_NAME = "adventure.yaml"
 import sys, yaml
 
 YAML_NAME = "adventure.yaml"
@@ -134,6 +136,7 @@ def get_objects(obj):
         }},
     }},
 """
         }},
     }},
 """
+    max_state = 0
     obj_str = ""
     for (i, item) in enumerate(obj):
         attr = item[1]
     obj_str = ""
     for (i, item) in enumerate(obj):
         attr = item[1]
@@ -157,6 +160,7 @@ def get_objects(obj):
                 statedefines += "/* States for %s */\n" % item[0]
                 for (n, label) in enumerate(labels):
                     statedefines += "#define %s\t%d\n" % (label, n)
                 statedefines += "/* States for %s */\n" % item[0]
                 for (n, label) in enumerate(labels):
                     statedefines += "#define %s\t%d\n" % (label, n)
+                    max_state = max(max_state, n)
                 statedefines += "\n"
         sounds_str = ""
         if attr.get("sounds") is None:
                 statedefines += "\n"
         sounds_str = ""
         if attr.get("sounds") is None:
@@ -190,6 +194,7 @@ def get_objects(obj):
         treasure = "true" if attr.get("treasure") else "false"
         obj_str += template.format(i, item[0], words_str, i_msg, locs[0], locs[1], treasure, descriptions_str, sounds_str, texts_str, changes_str)
     obj_str = obj_str[:-1] # trim trailing newline
         treasure = "true" if attr.get("treasure") else "false"
         obj_str += template.format(i, item[0], words_str, i_msg, locs[0], locs[1], treasure, descriptions_str, sounds_str, texts_str, changes_str)
     obj_str = obj_str[:-1] # trim trailing newline
+    statedefines += "/* Maximum state value */\n#define MAX_STATE %d\n" % max_state
     return obj_str
 
 def get_obituaries(obit):
     return obj_str
 
 def get_obituaries(obit):
@@ -524,7 +529,7 @@ def get_travel(travel):
     return out
 
 if __name__ == "__main__":
     return out
 
 if __name__ == "__main__":
-    with open(YAML_NAME, "r") as f:
+    with open(YAML_NAME, "r", encoding='ascii', errors='surrogateescape') as f:
         db = yaml.safe_load(f)
 
     locnames = [x[0] for x in db["locations"]]
         db = yaml.safe_load(f)
 
     locnames = [x[0] for x in db["locations"]]
@@ -536,10 +541,10 @@ if __name__ == "__main__":
                                  db["objects"])
     ignore = ""
     try:
                                  db["objects"])
     ignore = ""
     try:
-        with open(H_TEMPLATE_PATH, "r") as htf:
+        with open(H_TEMPLATE_PATH, "r", encoding='ascii', errors='surrogateescape') as htf:
             # read in dungeon.h template
             h_template = DONOTEDIT_COMMENT + htf.read()
             # read in dungeon.h template
             h_template = DONOTEDIT_COMMENT + htf.read()
-        with open(C_TEMPLATE_PATH, "r") as ctf:
+        with open(C_TEMPLATE_PATH, "r", encoding='ascii', errors='surrogateescape') as ctf:
             # read in dungeon.c template
             c_template = DONOTEDIT_COMMENT + ctf.read()
     except IOError as e:
             # read in dungeon.c template
             c_template = DONOTEDIT_COMMENT + ctf.read()
     except IOError as e:
@@ -587,10 +592,10 @@ if __name__ == "__main__":
         state_definitions  = statedefines
     )
 
         state_definitions  = statedefines
     )
 
-    with open(H_NAME, "w") as hf:
+    with open(H_NAME, "w", encoding='ascii', errors='surrogateescape') as hf:
         hf.write(h)
 
         hf.write(h)
 
-    with open(C_NAME, "w") as cf:
+    with open(C_NAME, "w", encoding='ascii', errors='surrogateescape') as cf:
         cf.write(c)
 
 # end
         cf.write(c)
 
 # end