Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions nml/actions/action1.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,22 +240,3 @@ def get_action1_index(spriteset, feature):
if spriteset in spriteset_collection.spritesets:
return spriteset_collection.get_index(spriteset)
assert False


def make_cb_failure_action1(feature):
"""
Create an action1 that may be used for a callback failure
If the last action1 is of the correct feature, no new action1 is needed
Else, add a new action1 with 1 spriteset containing 0 sprites

@param feature: Feature of the requested action 1
@type feature: C{int}

@return: List of actions to append (if any) and action1 index to use
@rtype: C{tuple} of (C{list} of L{BaseAction}, C{int})
"""
if feature in spriteset_collections:
actions = []
else:
actions = [Action1(feature, 0, 1, 0)]
return (actions, 0) # Index is currently always 0, but will change with ext. A1
20 changes: 0 additions & 20 deletions nml/actions/action2layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,26 +610,6 @@ def get_layout_action2s(spritelayout, feature):
return actions


def make_empty_layout_action2(feature, pos):
"""
Make an empty layout action2
For use with failed callbacks

@param feature: Feature of the sprite layout to create
@type feature: C{int}

@param pos: Positional context.
@type pos: L{Position}

@return: The created sprite layout action2
@rtype: L{Action2Layout}
"""
layout = ParsedSpriteLayout()
layout.ground_sprite = Action2LayoutSprite(feature, Action2LayoutSpriteType.GROUND)
layout.ground_sprite.set_param(expression.Identifier("sprite"), expression.ConstantNumeric(0))
return Action2Layout(feature, "@CB_FAILED_LAYOUT{:02X}".format(feature), pos, layout, [])


class StationSpriteset(expression.Expression):
def __init__(self, spriteset, args, var10, pos=None):
expression.Expression.__init__(self, pos)
Expand Down
14 changes: 0 additions & 14 deletions nml/actions/action2production.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,3 @@ def resolve_cargoitem(item):
prod_action = Action2Production(produce.name.value, produce.pos, 2, sub_in, add_out, again)

return finish_production_actions(produce, prod_action, action_list, varact2parser)


def make_empty_production_action2(pos):
"""
Make an empty production action2
For use with failed callbacks

@param pos: Positional context.
@type pos: L{Position}

@return: The created production action2
@rtype: L{Action2Production}
"""
return Action2Production("@CB_FAILED_PROD", pos, 0, [0, 0, 0], [0, 0], 0)
92 changes: 3 additions & 89 deletions nml/actions/action2var.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,80 +920,6 @@ def create_return_action(expr, feature, name, var_range):
return (action_list, ref)


failed_cb_results = {}


def get_failed_cb_result(feature, action_list, parent_action, pos):
"""
Get a sprite group reference to use for a failed callback
The actions needed are created on first use, then cached in L{failed_cb_results}

@param feature: Feature to use
@type feature: C{int}

@param action_list: Action list to append any extra actions to
@type action_list: C{list} of L{BaseAction}

@param parent_action: Reference to the action of which this is a result
@type parent_action: L{BaseAction}

@param pos: Positional context.
@type pos: L{Position}

@return: Sprite group reference to use
@rtype: L{SpriteGroupRef}
"""
if feature in failed_cb_results:
varaction2 = failed_cb_results[feature]
else:
# Create action2 (+ action1, if needed)
# Import here to avoid circular imports
from nml.actions import action1, action2layout, action2production, action2real

if feature == 0x0A:
# Industries -> production action2
act2 = action2production.make_empty_production_action2(pos)
elif feature in (0x07, 0x09, 0x0F, 0x11, 0x14):
# Tile layout action2
act2 = action2layout.make_empty_layout_action2(feature, pos)
else:
# Normal action2
act1_actions, act1_index = action1.make_cb_failure_action1(feature)
action_list.extend(act1_actions)
act2 = action2real.make_simple_real_action2(
feature, "@CB_FAILED_REAL{:02X}".format(feature), pos, act1_index
)
action_list.append(act2)

# Create varaction2, to choose between returning graphics and 0, depending on CB
varact2parser = Varaction2Parser(feature)
varact2parser.parse_expr(
expression.Variable(expression.ConstantNumeric(0x0C), mask=expression.ConstantNumeric(0xFFFF))
)

varaction2 = Action2Var(feature, "@CB_FAILED{:02X}".format(feature), pos, 0x89)
varaction2.var_list = varact2parser.var_list

varaction2.ranges.append(
VarAction2Range(
expression.ConstantNumeric(0),
expression.ConstantNumeric(0),
expression.ConstantNumeric(0),
"graphics callback -> return 0",
)
)
varaction2.default_result = expression.SpriteGroupRef(expression.Identifier(act2.name), [], None, act2)
varaction2.default_comment = "Non-graphics callback, return graphics result"
action2.add_ref(varaction2.default_result, varaction2)

action_list.append(varaction2)
failed_cb_results[feature] = varaction2

ref = expression.SpriteGroupRef(expression.Identifier(varaction2.name), [], None, varaction2)
action2.add_ref(ref, parent_action)
return ref


def parse_sg_ref_result(result, action_list, parent_action, var_range):
"""
Parse a result that is a sprite group reference.
Expand All @@ -1013,8 +939,8 @@ def parse_sg_ref_result(result, action_list, parent_action, var_range):
@return: Result to use in the calling varaction2
@rtype: L{SpriteGroupRef}
"""
if result.name.value == "CB_FAILED":
return get_failed_cb_result(parent_action.feature, action_list, parent_action, result.pos)
if result.name.value == "" or result.name.value == "CB_FAILED":
return result

target = action2.resolve_spritegroup(result.name) if not result.act2 else None

Expand Down Expand Up @@ -1210,19 +1136,7 @@ def parse_varaction2(switch_block):
x is not None and x.value is None
for x in [r.result for r in switch_block.body.ranges] + [switch_block.body.default]
):
# Computed result is returned in at least one result
if len(switch_block.body.ranges) == 0:
# There is only a default, which is 'return computed result', so we're fine
none_result = expression.ConstantNumeric(0) # Return value does not matter
else:
# Add an extra action to return the computed value
extra_actions, none_result = create_return_action(
expression.Variable(expression.ConstantNumeric(0x1C)),
feature,
switch_block.name.value + "@return",
0x89,
)
action_list.extend(extra_actions)
none_result = expression.SpriteGroupRef(expression.StringLiteral("", 0), [], 0)

used_ranges = []
for r in switch_block.body.ranges:
Expand Down
4 changes: 3 additions & 1 deletion nml/expression/spritegroup_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ def get_action2_id(self, feature):
"""
if self.act2 is not None:
return self.act2.id
if self.name.value == "":
return 0x7FFE
if self.name.value == "CB_FAILED":
return 0 # 0 serves as a failed CB result because it is never used
return 0x7FFF
try:
spritegroup = action2.resolve_spritegroup(self.name)
except generic.ScriptError:
Expand Down
Binary file modified regression/expected/006_vehicle.grf
Binary file not shown.
32 changes: 10 additions & 22 deletions regression/expected/006_vehicle.nfo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Escapes: D= = DR D+ = DF D- = DC Du* = DM D* = DnF Du<< = DnC D<< = DO D& D| Du/ D/ Du% D%
// Format: spritenum imagefile depth xpos ypos xsize ysize xrel yrel zoom flags

0 * 4 \d28
0 * 4 \d26

1 * 54 14 "C" "INFO"
"B" "VRSN" \w4 \dx00000000
Expand Down Expand Up @@ -109,48 +109,36 @@ FF
\w0
\w0

// Name: @CB_FAILED_REAL01
22 * 9 02 01 FD \b1 \b1
\w0
\w0

// Name: @CB_FAILED01
23 * 23 02 01 FD 89
0C 00 \dx0000FFFF
\b1
\wx8000 \dx00000000 \dx00000000 // graphics callback -> return 0
\wx00FD // Non-graphics callback, return graphics result

// Name: @return_action_0
24 * 20 02 01 FC 89
22 * 20 02 01 FD 89
1A 20 \dx00000008
\2- 1C 00 \dxFFFFFFFF
\b0
\wx8000 // Return computed value

// Name: @action3_1
25 * 38 02 01 FC 89
23 * 38 02 01 FD 89
1A 20 \dx00000000
\2sto 1A 20 \dx00000080
\2r 7E FF 00 \dxFFFFFFFF // switch_length(0)
\b1
\wx00FD \dx0000FFFF \dx0000FFFF // @CB_FAILED01;
\wx00FC // return (8 - var[0x1C, 0, -1])
\wx7FFF \dx0000FFFF \dx0000FFFF // CB_FAILED;
\wx00FD // return (8 - var[0x1C, 0, -1])

// Name: @action3_0
26 * 23 02 01 FC 89
24 * 23 02 01 FD 89
10 00 \dx000000FF
\b1
\wx00FC \dx00000023 \dx00000023 // @action3_1;
\wx00FD \dx00000023 \dx00000023 // @action3_1;
\wx00FE // foster_express_set;

// Name: @action3_2
27 * 23 02 01 FE 89
25 * 23 02 01 FE 89
0C 00 \dx0000FFFF
\b1
\wx00FC \dx00000036 \dx00000036 // @action3_0;
\wx00FD \dx00000036 \dx00000036 // @action3_0;
\wx00FE // foster_express_set;

28 * 9 03 01 01 FF \wx0059 \b0
26 * 9 03 01 01 FF \wx0059 \b0
\wx00FE // @action3_2;

Binary file modified regression/expected/013_train_callback.grf
Binary file not shown.
Loading
Loading