Skip to content

Commit

Permalink
Fix: random_switch with triggers has side-effects, and thus must not …
Browse files Browse the repository at this point in the history
…be optimised away.
  • Loading branch information
frosch123 committed Dec 23, 2020
1 parent cf57433 commit 9f5ab6d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nml/ast/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ def pre_process(self):
def optimise(self):
if self.optimised:
return True
if self.triggers.value != 0:
# Triggers have side-effects, and can't be skipped.
return False
if len(self.choices) == 1:
generic.print_warning("Block '{}' returns a constant, optimising.".format(self.name.value), self.pos)
self.optimised = self.choices[0].result.value
Expand Down
22 changes: 22 additions & 0 deletions regression/037_optimised_trigger.nml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
grf {
grfid: "NML\37";
name: string(STR_REGRESSION_NAME);
desc: string(STR_REGRESSION_DESC);
version: 0;
min_compatible_version: 0;
}

random_switch(FEAT_TRAINS, SELF, unconditional_switch) {
8: return 5;
}

random_switch(FEAT_TRAINS, SELF, trigger_only, bitmask(TRIGGER_VEHICLE_NEW_LOAD)) {
8: return 7;
}

item(FEAT_TRAINS, colored_train, 100) {
graphics {
colour_mapping: unconditional_switch;
default: trigger_only;
}
}
Binary file added regression/expected/037_optimised_trigger.grf
Binary file not shown.
35 changes: 35 additions & 0 deletions regression/expected/037_optimised_trigger.nfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Automatically generated by GRFCODEC. Do not modify!
// (Info version 32)
// Escapes: 2+ 2- 2< 2> 2u< 2u> 2/ 2% 2u/ 2u% 2* 2& 2| 2^ 2sto = 2s 2rst = 2r 2psto 2ror = 2rot 2cmp 2ucmp 2<< 2u>> 2>>
// Escapes: 71 70 7= 7! 7< 7> 7G 7g 7gG 7GG 7gg 7c 7C
// 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 \d6

1 * 54 14 "C" "INFO"
"B" "VRSN" \w4 \dx00000000
"B" "MINV" \w4 \dx00000000
"B" "NPAR" \w1 00
"B" "PALS" \w1 "A"
"B" "BLTR" \w1 "8"
00
00
2 * 52 08 08 "NML\37" "NML regression test" 00 "A test newgrf testing NML" 00
// Name: trigger_only
3 * 23 02 00 FF 80 01 \b0 08
\wx8007 \wx8007 \wx8007 \wx8007 \wx8007 \wx8007 \wx8007 \wx8007 // (8/8) -> (8/8): return 7;

4 * 9 00 00 \b1 01 FF \wx0064
1E 40

// Name: @action3_0
5 * 23 02 00 FF 89
0C 00 \dx0000FFFF
\b1
\wx8005 \dx0000002D \dx0000002D // return 5;
\wx00FF // trigger_only;

6 * 9 03 00 01 FF \wx0064 \b0
\wx00FF // @action3_0;

0 comments on commit 9f5ab6d

Please sign in to comment.