Skip to content

Commit

Permalink
Fix a9a1a3e: Don't use station properties 1C/1D for IDs 00-FF (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
glx22 authored Dec 18, 2023
1 parent a35551c commit 79afbc0
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 75 deletions.
18 changes: 12 additions & 6 deletions nml/actions/action0.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,14 @@ def parse_property(prop_info, value_list, feature, id):
if "custom_function" in prop_info:
props = prop_info["custom_function"](*value_list)
else:

def apply_threshold(value):
if not isinstance(value, tuple):
return value
assert len(value) == 3
threshold, below, above = value
return below if id.value < threshold else above

# First process each element in the value_list
final_values = []
for i, value in enumerate(value_list):
Expand Down Expand Up @@ -629,10 +637,7 @@ def parse_property(prop_info, value_list, feature, id):
raise generic.ScriptError(
"String used as value for non-string property: " + str(prop_info["num"]), value.pos
)
string_range = prop_info["string"]
if isinstance(string_range, tuple):
threshold, below, above = string_range
string_range = below if id.value < threshold else above
string_range = apply_threshold(prop_info["string"])
stringid, string_actions = action4.get_string_action4s(feature, string_range, value, id)
value = expression.ConstantNumeric(stringid)
action_list_append.extend(string_actions)
Expand All @@ -646,8 +651,9 @@ def parse_property(prop_info, value_list, feature, id):
final_values.append(value)

# Now, write a single Action0 Property with all of these values
if prop_info["num"] != -1:
props = [Action0Property(prop_info["num"], final_values, prop_info["size"])]
prop_num = apply_threshold(prop_info["num"])
if prop_num != -1:
props = [Action0Property(prop_num, final_values, prop_info["size"])]
else:
props = []

Expand Down
6 changes: 4 additions & 2 deletions nml/actions/action0properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def get_size(self):
# 'string', if set, means that the value of the property should be a string.
# The value of characteristic indicates the string range to use (usually 0xD0 or 0xDC)
# If set to None, the string will use the ID of the item (used for vehicle names)
# It can also be a (threshold, below, above) tuple to select range depending on ID
#
# 'string_literal', if set, indicates that the value of the property should
# be a literal (quoted) string. The value of the characteristic is equal to
Expand Down Expand Up @@ -155,6 +156,7 @@ def get_size(self):
# 'num' is the Action0 property number of the action 0 property, as given by the
# nfo specs. If set to -1, no action0 property will be generated. If
# 'custom_function' is set, this value is not needed and can be left out.
# It can also be a (threshold, below, above) tuple to set 'num' depending on ID.
#
# 'size' is the size (in bytes) of the resulting action 0 property. Valid
# values are 1 (byte), 2 (word) or 4 (dword). For other (or variable) sizes,
Expand Down Expand Up @@ -706,8 +708,8 @@ def cargo_bitmask(value):
# 19 (road routing) reserved for future use
# 1A (advanced sprite layout) is implemented elsewhere
# 1B (minimum bridge height) JGR only
"name": {"size": 2, "num": 0x1C, "string": (256, 0xC5, 0xDC), "required": True},
"classname": {"size": 2, "num": 0x1D, "string": 0xDC},
"name": {"size": 2, "num": (256, -1, 0x1C), "string": (256, 0xC5, 0xDC), "required": True},
"classname": {"size": 2, "num": (256, -1, 0x1D), "string": (256, 0xC4, 0xDC)},
}
# fmt: on

Expand Down
4 changes: 2 additions & 2 deletions regression/040_station.nml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ spritelayout station_sprite_layout_1(a) {
}
}

item (FEAT_STATIONS, basic_station) {
item (FEAT_STATIONS, basic_station, 255) {
property {
class : "TEST";
classname: string(STR_STATION_TEST_CLASS);
Expand All @@ -128,7 +128,7 @@ item (FEAT_STATIONS, basic_station) {
}
}

item (FEAT_STATIONS, basic_station_copied_layout) {
item (FEAT_STATIONS, basic_station_copied_layout, 256) {
property {
class : "TEST";
classname: string(STR_STATION_TEST_CLASS);
Expand Down
Binary file modified regression/expected/040_station.grf
Binary file not shown.
86 changes: 42 additions & 44 deletions regression/expected/040_station.nfo
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,40 @@
00
00
2 * 52 08 08 "NML\40" "NML regression test" 00 "A test newgrf testing NML" 00
3 * 11 04 04 FF 01 \wxDC00 "Test" 00
3 * 27 04 04 FF 02 \wxDC00 "Test" 00 "Basic station 2" 00

4 * 16 00 08 \b1 02 FF \wx0000
09 "COAL" "LVST"

5 * 27 00 04 \b6 01 FF \wx0000
5 * 21 00 04 \b4 01 FF \wx00FF
08 "TEST"
1D \wxDC00
1C \wxC500
13 18
12 \dx00000002
0C F0

6 * 20 04 04 FF 01 \wxC500 "Basic station" 00
6 * 11 04 04 FF 01 \wxC4FF "Test" 00

7 * 20 04 04 FF 01 \wxC5FF "Basic station" 00

7 * 6 01 04 \b2 FF \wx0002
8 * 6 01 04 \b2 FF \wx0002

8 station.png 8bpp 1 1 5 5 -2 -2 normal
9 station.png 8bpp 7 1 5 5 -2 -2 normal
9 station.png 8bpp 1 1 5 5 -2 -2 normal
10 station.png 8bpp 7 1 5 5 -2 -2 normal

10 station.png 8bpp 2 2 3 3 -1 -1 normal
11 station.png 8bpp 8 2 3 3 -1 -1 normal
11 station.png 8bpp 2 2 3 3 -1 -1 normal
12 station.png 8bpp 8 2 3 3 -1 -1 normal

// Name: station_spriteset - feature 04
12 * 7 02 04 FF \b0 \b1
13 * 7 02 04 FF \b0 \b1

\w0

// Name: station_spriteset2 - feature 04
13 * 7 02 04 FE \b0 \b1
14 * 7 02 04 FE \b0 \b1

\w1

14 * 114 00 04 \b1 01 FF \wx0000
15 * 114 00 04 \b1 01 FF \wx00FF
1A \b2
\b68 \dx000003F4 \wx0000
\dx0000842E \wx0000 \b0 \b0 \b0 \b16 \b5 \b2
Expand All @@ -62,9 +62,9 @@
\dx0000842D \wx0000 \b11 \b0 \b0 \b5 \b16 \b2
\dx8000842D \wx0042 \b20 \b10 80 84 03

// Name: Station Layout@registers - Id 00
// Name: Station Layout@registers - Id FF
// a : register 80
15 * 77 02 04 FD 89
16 * 77 02 04 FD 89
1A 20 \dx00000000
\2sto 1A 20 \dx00000081
\2r 1A 20 \dx00000000
Expand All @@ -78,8 +78,8 @@
\b0
\wx8000 // Return computed value

// Name: Station Layout@prepare - Id 00
16 * 34 02 04 FC 89
// Name: Station Layout@prepare - Id FF
17 * 34 02 04 FC 89
1A 20 \dx00000000
\2sto 1A 20 \dx00000000
\2r 1A 20 \dx00000001
Expand All @@ -88,108 +88,106 @@
\wx8000 // Return computed value

// Name: @action3_0
17 * 49 02 04 FB 89
7E FC 20 \dxFFFFFFFF // Station Layout@prepare - Id 00
\2r 7E FD 20 \dxFFFFFFFF // Station Layout@registers - Id 00
18 * 49 02 04 FB 89
7E FC 20 \dxFFFFFFFF // Station Layout@prepare - Id FF
\2r 7E FD 20 \dxFFFFFFFF // Station Layout@registers - Id FF
\2r 10 00 \dx000000FF
\b2
\wx8000 \dx00000002 \dx00000002 // return 0;
\wx00FE \dx00000003 \dx00000003 // station_spriteset2;
\wx00FF // station_spriteset;

// Name: @action3_1
18 * 49 02 04 FA 89
7E FC 20 \dxFFFFFFFF // Station Layout@prepare - Id 00
\2r 7E FD 20 \dxFFFFFFFF // Station Layout@registers - Id 00
19 * 49 02 04 FA 89
7E FC 20 \dxFFFFFFFF // Station Layout@prepare - Id FF
\2r 7E FD 20 \dxFFFFFFFF // Station Layout@registers - Id FF
\2r 10 00 \dx000000FF
\b2
\wx00FF \dx00000001 \dx00000001 // station_spriteset;
\wx8000 \dx00000002 \dx00000002 // return 0;
\wx00FE // station_spriteset2;

// Name: @action3_2
19 * 49 02 04 FC 89
7E FC 20 \dxFFFFFFFF // Station Layout@prepare - Id 00
\2r 7E FD 20 \dxFFFFFFFF // Station Layout@registers - Id 00
20 * 49 02 04 FC 89
7E FC 20 \dxFFFFFFFF // Station Layout@prepare - Id FF
\2r 7E FD 20 \dxFFFFFFFF // Station Layout@registers - Id FF
\2r 10 00 \dx000000FF
\b2
\wx8000 \dx00000002 \dx00000002 // return 0;
\wx00FE \dx00000003 \dx00000003 // station_spriteset2;
\wx00FF // station_spriteset;

// Name: @action3_3
20 * 55 02 04 F9 89
21 * 55 02 04 F9 89
1A 20 \dx00000003
\2sto 1A 20 \dx00000003
\2r 7E FD 20 \dxFFFFFFFF // Station Layout@registers - Id 00
\2r 7E FD 20 \dxFFFFFFFF // Station Layout@registers - Id FF
\2r 10 00 \dx000000FF
\b2
\wx8000 \dx00000002 \dx00000002 // return 0;
\wx00FE \dx00000003 \dx00000003 // station_spriteset2;
\wx00FF // station_spriteset;

21 * 9 00 04 \b1 01 FF \wx0000
22 * 9 00 04 \b1 01 FF \wx00FF
0B 08

// Name: @return_action_0
22 * 20 02 04 F8 89
23 * 20 02 04 F8 89
43 38 \dx0000000F
\2+ 43 1C \dx0000000F
\b0
\wx8000 // Return computed value

// Name: @action3_4
23 * 23 02 04 F8 89
24 * 23 02 04 F8 89
0C 00 \dx0000FFFF
\b1
\wx00F8 \dx00000142 \dx00000142 // return (var[0x43, 24, 15] + var[0x43, 28, 15])
\wx00FB // @action3_0;

// Name: @return_action_1
24 * 20 02 04 F7 89
25 * 20 02 04 F7 89
43 38 \dx0000000F
\2+ 43 1C \dx0000000F
\b0
\wx8000 // Return computed value

// Name: @action3_5
25 * 33 02 04 F7 89
26 * 33 02 04 F7 89
0C 00 \dx0000FFFF
\b2
\wx00FA \dx00000000 \dx00000000 // @action3_1;
\wx00F7 \dx00000142 \dx00000142 // return (var[0x43, 24, 15] + var[0x43, 28, 15])
\wx00FB // @action3_0;

// Name: @return_action_2
26 * 20 02 04 FA 89
27 * 20 02 04 FA 89
43 38 \dx0000000F
\2+ 43 1C \dx0000000F
\b0
\wx8000 // Return computed value

// Name: @action3_6
27 * 33 02 04 FB 89
28 * 33 02 04 FB 89
0C 00 \dx0000FFFF
\b2
\wx00FC \dx00000000 \dx00000000 // @action3_2;
\wx00FA \dx00000142 \dx00000142 // return (var[0x43, 24, 15] + var[0x43, 28, 15])
\wx00FB // @action3_0;

28 * 16 03 04 01 00 \b3
29 * 18 03 04 01 FF \wx00FF \b3
00 \wx00F7 // @action3_5;
01 \wx00FB // @action3_6;
FF \wx00F9 // @action3_3;
\wx00F8 // @action3_4;

29 * 18 00 04 \b3 01 FF \wx0001
30 * 18 00 04 \b3 01 FF \wx0100
08 "TEST"
1D \wxDC00
1C \wxC501

30 * 22 04 04 FF 01 \wxC501 "Basic station 2" 00
1C \wxDC01

31 * 9 00 04 \b1 01 FF \wx0001
0A 00
31 * 9 00 04 \b1 01 FF \wx0100
0A FF

// Name: @CB_FAILED_REAL04
32 * 7 02 04 F8 \b0 \b1
Expand All @@ -205,13 +203,13 @@ FF \wx00F9 // @action3_3;

// Name: @action3_7
34 * 41 02 04 F8 89
7E FD 20 \dxFFFFFFFF // Station Layout@registers - Id 00
7E FD 20 \dxFFFFFFFF // Station Layout@registers - Id FF
\2r 10 00 \dx000000FF
\b2
\wx00FF \dx00000001 \dx00000001 // station_spriteset;
\wx00FE \dx00000003 \dx00000003 // station_spriteset2;
\wx00F8 // @CB_FAILED04;

35 * 7 03 04 01 01 \b0
35 * 9 03 04 01 FF \wx0100 \b0
\wx00F8 // @action3_7;

Binary file modified regression/expected/example_station.grf
Binary file not shown.
40 changes: 19 additions & 21 deletions regression/expected/example_station.nfo
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,57 @@
00
00
2 * 193 08 08 "NML\06" "NML Example NewGRF: Station" 00 "\8ENML Example NewGRF: Station\0D\98This NewGRF is intended to provide a coding example for the high-level NewGRF-coding language NML.\0DConversion of CHIPS Cow pens." 00
3 * 18 04 04 FF 01 \wxDC00 "NML Example" 00

// param[126] = param[161]
4 * 5 0D 7E \D= A1 00
3 * 5 0D 7E \D= A1 00

// param[127] = (param[126] - 302012611)
5 * 9 0D 7F \D- 7E FF \dx120058C3
4 * 9 0D 7F \D- 7E FF \dx120058C3

// param[127] = (param[127] << -31)
6 * 9 0D 7F \Du<< 7F FF \dxFFFFFFE1
5 * 9 0D 7F \Du<< 7F FF \dxFFFFFFE1

7 * 9 09 7F 04 \7= \dx00000000 01
6 * 9 09 7F 04 \7= \dx00000000 01

8 * 19 0B 03 7F 06 "1.2.0 (r22723)" 00
7 * 19 0B 03 7F 06 "1.2.0 (r22723)" 00

9 * 12 00 08 \b1 01 FF \wx0000
8 * 12 00 08 \b1 01 FF \wx0000
09 "LVST"

10 * 6 01 04 \b3 FF \wx0002
9 * 6 01 04 \b3 FF \wx0002

11 cows_cargo.png 8bpp 10 10 64 65 -31 -34 normal
12 cows_cargo.png 8bpp 220 10 64 65 -31 -34 normal
10 cows_cargo.png 8bpp 10 10 64 65 -31 -34 normal
11 cows_cargo.png 8bpp 220 10 64 65 -31 -34 normal

13 cows_cargo.png 8bpp 80 10 64 65 -31 -34 normal
14 cows_cargo.png 8bpp 290 10 64 65 -31 -34 normal
12 cows_cargo.png 8bpp 80 10 64 65 -31 -34 normal
13 cows_cargo.png 8bpp 290 10 64 65 -31 -34 normal

15 cows_cargo.png 8bpp 150 10 64 65 -31 -34 normal
16 cows_cargo.png 8bpp 360 10 64 65 -31 -34 normal
14 cows_cargo.png 8bpp 150 10 64 65 -31 -34 normal
15 cows_cargo.png 8bpp 360 10 64 65 -31 -34 normal

// Name: cow_pen_1 - feature 04
17 * 11 02 04 FF \b2 \b1
16 * 11 02 04 FF \b2 \b1
\w0 \w1
\w2

// Name: cow_pen_2 - feature 04
18 * 13 02 04 FE \b3 \b1
17 * 13 02 04 FE \b3 \b1
\w0 \w1 \w2
\w2

// Name: random_cow_pen
19 * 11 02 04 FE 80 00 \b16 02
18 * 11 02 04 FE 80 00 \b16 02
\wx00FF // (1/2) -> (1/2): cow_pen_1;
\wx00FE // (1/2) -> (1/2): cow_pen_2;

20 * 27 00 04 \b7 01 FF \wx0000
19 * 21 00 04 \b5 01 FF \wx0000
08 "NML_"
1D \wxDC00
1C \wxC500
10 \wx00A0
11 00
14 FF
15 FF

20 * 18 04 04 FF 01 \wxC400 "NML Example" 00

21 * 21 04 04 FF 01 \wxC500 "CHIPS Cow pens" 00

// Name: cow_pen_half - feature 04
Expand Down

0 comments on commit 79afbc0

Please sign in to comment.