Skip to content

Commit

Permalink
Add: Support for station properties 0E and 0F (station layouts) (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
glx22 authored Sep 15, 2024
1 parent d3b5d93 commit f00eefa
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
66 changes: 63 additions & 3 deletions nml/actions/action0properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,66 @@ def cargo_bitmask(value):
return BitMask(value.values, value.pos).reduce()


class StationLayoutProp(BaseAction0Property):
def __init__(self, prop_num, data):
self.prop_num = prop_num
self.data = data

def write(self, file):
file.print_bytex(self.prop_num)
for (length, number), layout in self.data.items():
file.print_byte(length)
file.print_byte(number)
file.newline()
for platform in layout:
for type in platform:
file.print_bytex(type)
file.newline()
file.print_byte(0)
file.print_byte(0)
file.newline()

def get_size(self):
total_len = 3 # Prop number + ending
for length, number in self.data:
total_len += length * number + 2
return total_len


def station_layouts(value):
if isinstance(value, ConstantNumeric):
return [Action0Property(0x0F, value, 1 if value.value < 0xFF else 3)]
if not isinstance(value, Array) or len(value.values) == 0:
raise generic.ScriptError("station_layouts must be an array of layouts, or the ID of a station", value.pos)
layouts = {}
for layout in value.values:
if not isinstance(layout, Array) or len(layout.values) == 0:
raise generic.ScriptError("A station layout must be an array of platforms", layout.pos)
length = len(layout.values[0].values)
number = len(layout.values)
if (length, number) in layouts:
generic.print_warning(
generic.Warning.GENERIC, "Redefinition of layout {}x{}".format(length, number), layout.pos
)
layouts[(length, number)] = []
for platform in layout.values:
if not isinstance(platform, Array) or len(platform.values) == 0:
raise generic.ScriptError("A platform must be an array of tile types")
if len(platform.values) != length:
raise generic.ScriptError("All platforms in a station layout must have the same length", platform.pos)
for type in platform.values:
if type.reduce_constant().value % 2 != 0:
generic.print_warning(
generic.Warning.GENERIC,
"Invalid tile {} in layout {}x{}".format(type, length, number),
type.pos,
)
layouts[(length, number)].append(
[nmlop.AND(type, 0xFE).reduce_constant().value for type in platform.values]
)
return [StationLayoutProp(0x0E, layouts)]


def station_tile_flags(value):
if not isinstance(value, Array) or len(value.values) % 2 != 0:
raise generic.ScriptError("Flag list must be an array of even length", value.pos)
Expand Down Expand Up @@ -743,8 +803,8 @@ def station_tile_flags(value):
# 0B (callback flags) is not set by user
"disabled_platforms": {"size": 1, "num": 0x0C, "value_function": station_platforms_length},
"disabled_length": {"size": 1, "num": 0x0D, "value_function": station_platforms_length},
# 0E (station layout) callback 24 should be enough
# 0F (copy station layout)
"station_layouts": {"custom_function": station_layouts}, # = prop 0E
# 0F (copy station layout) is handled by station_layouts
"cargo_threshold": {"size": 2, "num": 0x10},
"draw_pylon_tiles": {"size": 1, "num": 0x11, "replaced_by": "tile_flags"},
"cargo_random_triggers": {"size": 4, "num": 0x12, "value_function": cargo_bitmask},
Expand All @@ -759,7 +819,7 @@ def station_tile_flags(value):
# 1B (minimum bridge height) JGR only
"name": {"size": 2, "num": (256, -1, 0x1C), "string": (256, 0xC5, 0xDC), "required": True},
"classname": {"size": 2, "num": (256, -1, 0x1D), "string": (256, 0xC4, 0xDC)},
"tile_flags": {"custom_function": station_tile_flags},
"tile_flags": {"custom_function": station_tile_flags}, # = prop 1E
}
# fmt: on

Expand Down
13 changes: 13 additions & 0 deletions regression/040_station.nml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ item (FEAT_STATIONS, basic_station, 255) {
bitmask(STAT_TILE_NOWIRE),
bitmask(STAT_TILE_PYLON, STAT_TILE_BLOCKED),
];
station_layouts: [
[
[0],
],
[
[2],
],
[
[4, 5],
[6, 7]
],
];
}
graphics {
foundations: 0;
Expand All @@ -144,6 +156,7 @@ item (FEAT_STATIONS, basic_station_copied_layout, 256) {
class : "TEST";
classname: string(STR_STATION_TEST_CLASS);
name : string(STR_STATION_BASIC2);
station_layouts: basic_station;
}
graphics {
sprite_layouts: basic_station;
Expand Down
Binary file modified regression/expected/040_station.grf
Binary file not shown.
11 changes: 9 additions & 2 deletions regression/expected/040_station.nfo
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@
4 * 16 00 08 \b1 02 FF \wx0000
09 "COAL" "LVST"

5 * 35 00 04 \b5 01 FF \wx00FF
5 * 47 00 04 \b6 01 FF \wx00FF
08 "TEST"
13 18
12 \dx00000002
0C F0
1E FF \w10
00 01 02 03 04 05 06 07
02 05
0E \b1 \b1
02
\b2 \b2
04 04
06 06
\b0 \b0

6 * 11 04 04 FF 01 \wxC4FF "Test" 00

Expand Down Expand Up @@ -182,10 +188,11 @@
FF \wx00F9 // @action3_3;
\wx00F8 // @action3_4;

30 * 18 00 04 \b3 01 FF \wx0100
30 * 22 00 04 \b4 01 FF \wx0100
08 "TEST"
1D \wxDC00
1C \wxDC01
0F FF \wx00FF

31 * 11 00 04 \b1 01 FF \wx0100
0A FF \wx00FF
Expand Down

0 comments on commit f00eefa

Please sign in to comment.