Skip to content

Instantly share code, notes, and snippets.

@frosch123
Last active April 9, 2019 19:01
Show Gist options
  • Save frosch123/fb62db8e6a65c0de1614ad1f663f1827 to your computer and use it in GitHub Desktop.
Save frosch123/fb62db8e6a65c0de1614ad1f663f1827 to your computer and use it in GitHub Desktop.
diff --git a/nml/actions/action2var.py b/nml/actions/action2var.py
index 17df6c2..fba1cae 100644
--- a/nml/actions/action2var.py
+++ b/nml/actions/action2var.py
@@ -404,7 +404,7 @@ class Varaction2Parser(object):
max = 0xF if expr.info['perm'] else 0x10F
if isinstance(expr.register, expression.ConstantNumeric) and expr.register.value > max:
raise generic.ScriptError("Register number must be in range 0..{:d}, encountered {:d}.".format(max, expr.register.value), expr.pos)
- if expr.info['perm'] and self.feature not in (0x08, 0x0A, 0x0D):
+ if expr.info['perm'] and self.feature not in (0x0A, 0x0D, "towns"):
raise generic.ScriptError("Persistent storage is not supported for feature '{}'".format(general.feature_name(self.feature)), expr.pos)
if expr.info['store']:
@@ -414,7 +414,7 @@ class Varaction2Parser(object):
var_num = 0x7C if expr.info['perm'] else 0x7D
ret = expression.Variable(expression.ConstantNumeric(var_num), param=expr.register, pos=expr.pos)
- if expr.info['perm'] and self.feature == 0x08:
+ if expr.info['perm'] and self.feature == "towns":
# store grfid in register 0x100 for town persistent storage
grfid = expression.ConstantNumeric(0xFFFFFFFF if expr.grfid is None else expression.parse_string_to_dword(expr.grfid))
store_op = expression.BinOp(nmlop.STO_TMP, grfid, expression.ConstantNumeric(0x100), expr.pos)
@@ -708,7 +708,7 @@ def create_return_action(expr, feature, name, var_range):
- Reference to the created varaction2
@rtype: C{tuple} of (C{list} of L{BaseAction}, L{SpriteGroupRef})
"""
- varact2parser = Varaction2Parser(feature if var_range == 0x89 else action2var_variables.varact2parent_scope[feature])
+ varact2parser = Varaction2Parser(feature if var_range == 0x89 else action2var_variables.varact2parent_scope.get(feature))
varact2parser.parse_expr(expr)
action_list = varact2parser.extra_actions
@@ -814,7 +814,7 @@ def parse_sg_ref_result(result, action_list, parent_action, var_range):
# Result is parametrized
# Insert an intermediate varaction2 to store expressions in registers
- var_feature = parent_action.feature if var_range == 0x89 else action2var_variables.varact2parent_scope[parent_action.feature]
+ var_feature = parent_action.feature if var_range == 0x89 else action2var_variables.varact2parent_scope.get(parent_action.feature)
varact2parser = Varaction2Parser(var_feature)
layout = action2.resolve_spritegroup(result.name)
for i, param in enumerate(result.param_list):
@@ -929,7 +929,7 @@ def parse_result(value, action_list, act6, offset, parent_action, none_result, v
def get_feature(switch_block):
feature = next(iter(switch_block.feature_set))
if switch_block.var_range == 0x8A:
- feature = action2var_variables.varact2parent_scope[feature]
+ feature = action2var_variables.varact2parent_scope.get(feature)
if feature is None:
raise generic.ScriptError("Parent scope for this feature not available, feature: " + str(feature), switch_block.pos)
@@ -937,8 +937,8 @@ def get_feature(switch_block):
def reduce_varaction2_expr(expr, feature, extra_dicts = []):
# 'normal' and 60+x variables to use
- vars_normal = action2var_variables.varact2vars[feature]
- vars_60x = action2var_variables.varact2vars60x[feature]
+ vars_normal = action2var_variables.varact2vars.get(feature, {})
+ vars_60x = action2var_variables.varact2vars60x.get(feature, {})
# lambda function to convert (value, pos) to a function pointer
# since we need the variable name later on, a reverse lookup is needed
diff --git a/nml/actions/action2var_variables.py b/nml/actions/action2var_variables.py
index 4fb1db3..c4692c2 100644
--- a/nml/actions/action2var_variables.py
+++ b/nml/actions/action2var_variables.py
@@ -15,11 +15,20 @@ with NML; if not, write to the Free Software Foundation, Inc.,
from nml import expression, nmlop, generic
-# Use feature 0x12 for towns (accessible via station/house/industry parent scope)
-varact2vars = 0x13 * [{}]
-varact2vars60x = 0x13 * [{}]
-# feature number: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12
-varact2parent_scope = [0x00, 0x01, 0x02, 0x03, 0x12, None, 0x12, 0x12, None, 0x0A, 0x12, None, None, None, None, 0x12, None, None, None]
+varact2vars = dict()
+varact2vars60x = dict()
+varact2parent_scope = {
+ 0x00: 0x00,
+ 0x01: 0x01,
+ 0x02: 0x02,
+ 0x03: 0x03,
+ 0x04: "towns",
+ 0x06: "towns",
+ 0x07: "towns",
+ 0x09: 0x0A,
+ 0x0A: "towns",
+ 0x0F: "towns",
+}
def default_60xvar(name, args, pos, info):
"""
@@ -726,4 +735,4 @@ varact2vars60x[0x0F] = varact2vars60x_objects
varact2vars[0x10] = varact2vars_railtype
varact2vars[0x11] = varact2vars_airporttiles
varact2vars60x[0x11] = varact2vars60x_airporttiles
-varact2vars[0x12] = varact2vars_towns
+varact2vars["towns"] = varact2vars_towns
diff --git a/nml/ast/general.py b/nml/ast/general.py
index bca892d..b8a9f21 100644
--- a/nml/ast/general.py
+++ b/nml/ast/general.py
@@ -48,6 +48,8 @@ def feature_name(feature):
@return: String with feature as used in nml files.
@rtype: C{str}
"""
+ if feature == "towns":
+ return "FEAT_TOWNS"
for name, num in list(feature_ids.items()):
if num == feature:
return name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment