Skip to content

Commit

Permalink
Correct parsing of combat objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Kjir committed Nov 6, 2024
1 parent 9e534f3 commit bb7826a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
8 changes: 7 additions & 1 deletion mgz/fast/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,13 @@ def parse_de(data, version, save, skip=False):
team_id = unpack('<b', data)
data.read(9)
civilization_id = unpack('<I', data)
custom_civ_selection = None
if save >= 61.5:
data.read(4)
custom_civ_count = unpack('<I', data)
if save >= 63.0 and custom_civ_count > 0:
custom_civ_selection = []
for _ in range(custom_civ_count):
custom_civ_selection.append(unpack('<I', data))
de_string(data)
data.read(1)
ai_name = de_string(data)
Expand All @@ -432,6 +437,7 @@ def parse_de(data, version, save, skip=False):
type=type,
profile_id=profile_id,
civilization_id=civilization_id,
custom_civ_selection=custom_civ_selection,
prefer_random=prefer_random == 1
))
data.read(12)
Expand Down
5 changes: 3 additions & 2 deletions mgz/header/de.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from construct import (
Struct, Int32ul, Float32l, Array, Padding, Flag, If,
Byte, Int16ul, Bytes, Int32sl, Peek, Const, RepeatUntil,
Int64ul, Computed
Int64ul, Computed, IfThenElse
)

from mgz.enums import VictoryEnum, ResourceLevelEnum, AgeEnum, PlayerTypeEnum, DifficultyEnum
Expand All @@ -28,7 +28,8 @@
"dat_crc"/Bytes(8),
"mp_game_version"/Byte,
"civ_id"/Int32ul,
"unk"/If(lambda ctx: find_save_version(ctx) >= 61.5, Int32ul),
"custom_civ_count"/IfThenElse(lambda ctx: find_save_version(ctx) >= 61.5, Int32ul, Computed(lambda _: 0)),
"custom_civ_ids"/Array(lambda ctx: ctx.custom_civ_count, Int32ul),
"ai_type"/de_string,
"ai_civ_name_index"/Byte,
"ai_name"/de_string,
Expand Down
6 changes: 2 additions & 4 deletions mgz/header/initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,14 @@
Embedded(IfThenElse(lambda ctx: ctx._.restore_time == 0,
Struct(
"objects"/RepeatUpTo(b'\x00', existing_object),
Find(b'\x00\x0b', None), # Some additional data was added in 63.0, so the separator is not aligned anymore. Since we don't know what this data represents, we skip it.
# Const(b'\x00\x0b'),
Const(b'\x00\x0b'),
# Skip Gaia trees for performance reasons
Embedded(IfThenElse(this._.type != 2,
Struct(
"s_size"/Int32ul,
"s_grow"/Int32ul,
"sleeping_objects"/RepeatUpTo(b'\x00', existing_object),
Find(b'\x00\x0b', None), # Some additional data was added in 63.0, so the separator is not aligned anymore. Since we don't know what this data represents, we skip it.
# Const(b'\x00\x0b'),
Const(b'\x00\x0b'),
"d_size"/Int32ul,
"d_grow"/Int32ul,
"doppleganger_objects"/RepeatUpTo(b'\x00', existing_object),
Expand Down
28 changes: 21 additions & 7 deletions mgz/header/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)

animated_sprite = "animated_sprite"/Struct(
"animate_interval"/Int32ul,
"animate_interval"/Float32l,
"animate_last"/Int32ul,
"last_frame"/Int16ul,
"frame_changed"/Byte,
Expand Down Expand Up @@ -109,7 +109,7 @@
# not the right way to do this, needs improvement
If(lambda ctx: find_save_version(ctx) >= 20.16, Struct(
"peek"/Peek(Bytes(6)),
If(lambda ctx: find_save_version(ctx) >= 25.22 and find_type(ctx) == 10, Bytes(1)),
If(lambda ctx: find_save_version(ctx) >= 25.22 and (find_type(ctx) == 10 or find_save_version(ctx) >= 63.0), Bytes(1)),
If(lambda ctx: find_save_version(ctx) < 25.22 and find_type(ctx) == 10 and ctx.peek[0] == 0 and ctx.peek[0:2] != b"\x00\x0b", Bytes(1)),
If(lambda ctx: find_type(ctx) == 20 and ctx.peek[4] == 0 and ctx.peek[4:6] != b"\x00\x0b", Bytes(1)),
)),
Expand Down Expand Up @@ -193,15 +193,16 @@
"user_defined_waypoints"/Array(lambda ctx: ctx.num_user_defined_waypoints, vector),
"has_substitute_position"/Int32ul,
"substitute_position"/vector,
"consecutive_subsitute_count"/Int32ul
"consecutive_substitute_count"/Int32ul
)

moving = "moving"/Struct(
Embedded(base_moving),
"hd_moving"/If(lambda ctx: find_version(ctx) == Version.HD, Bytes(1)),
"de_moving"/If(lambda ctx: find_version(ctx) == Version.DE, Bytes(17)),
"ver2616"/If(lambda ctx: 37 > find_save_version(ctx) >= 26.16, Bytes(8)),
"ver37"/If(lambda ctx: find_save_version(ctx) >= 37, Bytes(5)),
"ver37"/If(lambda ctx: 63 > find_save_version(ctx) >= 37, Bytes(5)),
"ver63"/If(lambda ctx: find_save_version(ctx) >= 63, Bytes(4)),
)

move_to = "move_to"/Struct(
Expand Down Expand Up @@ -389,7 +390,8 @@
"best_unit_to_attack"/Int32sl,
"formation_type"/Byte,
"de_unk"/If(lambda ctx: find_version(ctx) == Version.DE, Bytes(4)),
"de_unk_byte"/If(lambda ctx: find_save_version(ctx) >= 25.22, Byte)
"de_unk_byte"/If(lambda ctx: find_save_version(ctx) >= 25.22, Byte),
"de_unknown_2"/If(lambda ctx: find_save_version(ctx) >= 63.0 and ctx._.has_ai in (15, 17), Bytes(4))
)


Expand All @@ -398,7 +400,7 @@
"de_pre"/If(lambda ctx: find_version(ctx) == Version.DE and find_save_version(ctx) < 37, Bytes(4)),
"de"/If(lambda ctx: find_version(ctx) == Version.DE, Bytes(14)),
"de_2"/If(lambda ctx: find_save_version(ctx) >= 26.16, Bytes(16)),
"de_3"/If(lambda ctx: find_save_version(ctx) >= 26.18, Bytes(1)),
"de_3"/If(lambda ctx: 63 > find_save_version(ctx) >= 26.18, Bytes(1)),
"de_4"/If(lambda ctx: find_save_version(ctx) >= 61.5, Bytes(4)),
"next_volley"/Byte,
"using_special_animation"/Byte,
Expand Down Expand Up @@ -432,7 +434,19 @@
"de_unknown3"/If(lambda ctx: 26.18 > find_save_version(ctx) >= 26.16, Bytes(5)),
"de_unknown4"/If(lambda ctx: find_save_version(ctx) >= 26.18, Bytes(4)),
"de_unknown5"/If(lambda ctx: find_save_version(ctx) >= 50, Bytes(48)),
"de_unknown6"/If(lambda ctx: find_save_version(ctx) >= 61.5, Bytes(44))
"de_unknown6"/If(lambda ctx: find_save_version(ctx) >= 61.5, Bytes(40)),
"de_unknown7"/If(lambda ctx: find_save_version(ctx) >= 61.5, Int16ul),
Embedded(
IfThenElse(lambda ctx: find_save_version(ctx) >= 63.0 and ctx.has_ai == 15,
Struct(
"de_unknown8"/Float32l,
"de_unknown9"/Bytes(4)
),
Struct(
"de_unknown8"/Bytes(2)
)
)
),
)

production_queue = "production_queue"/Struct(
Expand Down
3 changes: 3 additions & 0 deletions mgz/header/playerstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@
Embedded(If(lambda ctx: find_version(ctx) in (Version.DE, Version.HD), Struct(
Array(this._._.num_header_data - 198, Float32l)
))),
Embedded(If(lambda ctx: find_save_version(ctx) >= 63, Struct(
Array(this._._.num_header_data, Float32l)
))),
Embedded(If(lambda ctx: find_version(ctx) in [Version.USERPATCH15, Version.MCP], Struct(
ModVersionAdapter("mod"/Float32l),
Array(6, Float32l),
Expand Down
Binary file modified tests/recs/de-63.0.aoe2record
Binary file not shown.

0 comments on commit bb7826a

Please sign in to comment.