Esempio n. 1
0
def _read_node_data_window(reader: BinReader) -> Window:
    (
        origin_x,  # 000
        origin_y,  # 004
        resolution_x,  # 008
        resolution_y,  # 012
        zero016,  # 212 zero bytes
        buffer_index,  # 228
        buffer_ptr,  # 232
        zero236,
        zero240,
        zero244,
    ) = reader.read(WINDOW)

    assert_eq("origin x", 0, origin_x, reader.prev + 0)
    assert_eq("origin y", 0, origin_y, reader.prev + 4)

    assert_eq("resolution x", 320, resolution_x, reader.prev + 8)
    assert_eq("resolution y", 200, resolution_y, reader.prev + 12)

    assert_all_zero("field 016", zero016, reader.prev + 16)

    assert_eq("buffer index", -1, buffer_index, reader.prev + 228)
    assert_eq("buffer ptr", 0, buffer_ptr, reader.prev + 232)

    assert_eq("field 236", 0, zero236, reader.prev + 236)
    assert_eq("field 240", 0, zero240, reader.prev + 240)
    assert_eq("field 244", 0, zero244, reader.prev + 244)

    # window nodes always have an action priority of 14

    return Window(
        type="Window",
        resolution=(resolution_x, resolution_y),
    )
Esempio n. 2
0
def read_anim_def_zero(reader: BinReader) -> None:
    # the first entry is always zero
    data = bytearray(reader.read_bytes(ANIM_DEF.size))
    # except for this one byte?
    assert_eq("anim def header byte 153", 3, data[153], reader.prev + 153)
    data[153] = 0
    assert_all_zero("anim def header", data, reader.prev)
    bdata = reader.read_bytes(RESET_STATE.size)
    assert_all_zero("anim def reset", bdata, reader.prev)
Esempio n. 3
0
def _read_node_data_lod(reader: BinReader) -> LevelOfDetail:
    (
        level,  # 00
        range_near_sq,  # 04
        range_far,  # 08
        range_far_sq,  # 12
        zero16,  # 44 zero bytes
        unk60,  # 60
        unk64,  # 64
        one68,  # 68
        zero72,  # 72
        unk76,  # 76
    ) = reader.read(LEVEL_OF_DETAIL)

    assert_in("level", (0, 1), level, reader.prev + 0)

    assert_between(
        "range near sq",
        0.0,
        1000.0 * 1000.0,
        range_near_sq,
        reader.prev + 4,
    )
    range_near = sqrt(range_near_sq)

    assert_ge("range far", 0.0, range_far, reader.prev + 8)
    expected = force_single_prec(range_far * range_far)
    assert_eq("range far sq", expected, range_far_sq, reader.prev + 12)

    assert_all_zero("field 16", zero16, reader.prev + 16)

    # TODO:
    assert_ge("field 60", 0.0, unk60, reader.prev + 60)
    expected = force_single_prec(unk60 * unk60)
    assert_eq("field 64", expected, unk64, reader.prev + 64)

    assert_eq("field 68", 1, one68, reader.prev + 68)
    # TODO:
    assert_in("field 72", (0, 1), zero72, reader.prev + 72)
    if zero72 == 0:
        assert_eq("field 76", 0, unk76, reader.prev + 76)
    else:
        assert_ne("field 76", 0, unk76, reader.prev + 76)

    # object 3d nodes always have an action priority of 6

    return LevelOfDetail(
        type="LOD",
        level=level == 1,
        range=(range_near, range_far),
        unk60=unk60,
        unk76=unk76,
    )
Esempio n. 4
0
def _read_static_sounds(reader: BinReader, count: int) -> List[NameRaw]:
    # the first entry is always zero
    name_raw, ptr = reader.read(STATIC_SOUND)
    assert_all_zero("name", name_raw, reader.prev + 0)
    assert_eq("field 32", 0, ptr, reader.prev + 32)

    sounds = []
    for _ in range(1, count):
        name_raw, ptr = reader.read(STATIC_SOUND)
        with assert_ascii("name", name_raw, reader.prev + 0):
            name, pad = ascii_zterm_partition(name_raw)
        assert_eq("field 32", 0, ptr, reader.prev + 32)
        sounds.append(NameRaw(name=name, pad=Base64(pad)))

    return sounds
Esempio n. 5
0
def _read_nodes(reader: BinReader, count: int) -> List[NamePtrFlag]:
    # the first entry is always zero
    name_raw, zero, ptr = reader.read(NODE)
    assert_all_zero("name", name_raw, reader.prev + 0)
    assert_eq("field 32", 0, zero, reader.prev + 32)
    assert_eq("field 36", 0, ptr, reader.prev + 36)

    nodes = []
    for _ in range(1, count):
        name_raw, zero, ptr = reader.read(NODE)
        with assert_ascii("name", name_raw, reader.prev + 0):
            name = ascii_zterm_node_name(name_raw)
        assert_eq("field 32", 0, zero, reader.prev + 32)
        assert_ne("field 36", 0, ptr, reader.prev + 36)
        nodes.append(NamePtrFlag(name=name, ptr=ptr))

    return nodes
Esempio n. 6
0
def _read_objects(reader: BinReader, count: int) -> List[NameRaw]:
    # the first entry is always zero
    data = reader.read_bytes(OBJECT.size)
    assert_all_zero("object", data, reader.prev)

    objects = []
    for _ in range(1, count):
        (name_raw, zero32, bin_dump) = reader.read(OBJECT)

        with assert_ascii("name", name_raw, reader.prev + 0):
            name = ascii_zterm_node_name(name_raw)

        assert_eq("field 32", 0, zero32, reader.prev + 32)
        # TODO: this is cheating, but i have no idea how to interpret this data
        # sometimes it's sensible, e.g. floats. other times, it seems like random
        # garbage.
        objects.append(NameRaw(name=name, pad=Base64(bin_dump.rstrip(b"\0"))))

    return objects
Esempio n. 7
0
def _read_dynamic_sounds(reader: BinReader, count: int) -> List[NamePtrFlag]:
    # the first entry is always zero
    name_raw, flag, ptr, zero = reader.read(READER_LOOKUP)
    assert_all_zero("name", name_raw, reader.prev + 0)
    assert_eq("field 32", 0, flag, reader.prev + 32)
    assert_eq("field 36", 0, ptr, reader.prev + 36)
    assert_eq("field 40", 0, zero, reader.prev + 40)

    sounds = []
    for _ in range(1, count):
        name_raw, flag, ptr, zero = reader.read(READER_LOOKUP)
        with assert_ascii("name", name_raw, reader.prev + 0):
            name = ascii_zterm_node_name(name_raw)
        assert_eq("field 32", 0, flag, reader.prev + 32)
        assert_ne("field 36", 0, ptr, reader.prev + 36)
        assert_eq("field 40", 0, zero, reader.prev + 40)
        sounds.append(NamePtrFlag(name=name, ptr=ptr))

    return sounds
Esempio n. 8
0
def _read_lights(reader: BinReader, count: int) -> List[NamePtrFlag]:
    # the first entry is always zero
    name_raw, flag, ptr, zero = reader.read(READER_LOOKUP)
    assert_all_zero("name", name_raw, reader.prev + 0)
    assert_eq("field 32", 0, flag, reader.prev + 32)
    assert_eq("field 36", 0, ptr, reader.prev + 36)
    assert_eq("field 40", 0, zero, reader.prev + 40)

    lights = []
    for _ in range(1, count):
        name_raw, flag, ptr, zero = reader.read(READER_LOOKUP)
        with assert_ascii("name", name_raw, reader.prev + 0):
            name = ascii_zterm_node_name(name_raw)
        assert_eq("field 32", 0, flag, reader.prev + 32)
        assert_ne("field 36", 0, ptr, reader.prev + 36)
        # if this were non-zero, it would cause the light to be removed instead
        # of added (???)
        assert_eq("field 40", 0, zero, reader.prev + 40)
        lights.append(NamePtrFlag(name=name, ptr=ptr))

    return lights
Esempio n. 9
0
def _read_sequence_definitions(reader: BinReader, anim_def: AnimDef,
                               count: int) -> List[SeqDef]:
    sequences = []
    for _ in range(count):
        name_raw, flag, zero, seqdef_ptr, seqdef_len = reader.read(SEQDEF_INFO)
        with assert_ascii("name", name_raw, reader.prev + 0):
            name = ascii_zterm_padded(name_raw)

        assert_in("activation", (0x0, 0x303), flag, reader.prev + 32)
        activation: SeqActivation = "ON_CALL" if flag == 0x303 else "NONE"
        assert_all_zero("field 36", zero, reader.prev + 36)
        assert_gt("seqdef length", 0, seqdef_len, reader.prev + 56)
        assert_ne("seqdef ptr", 0, seqdef_ptr, reader.prev + 60)

        script = _parse_script(reader, anim_def, seqdef_len)
        sequences.append(
            SeqDef(name=name,
                   ptr=seqdef_ptr,
                   activation=activation,
                   script=script))

    return sequences
Esempio n. 10
0
def _read_puffers(reader: BinReader, count: int) -> List[NamePtrFlag]:
    # the first entry is always zero
    name_raw, flag, ptr, zero = reader.read(READER_LOOKUP)
    assert_all_zero("name", name_raw, reader.prev + 0)
    assert_eq("field 32", 0, flag, reader.prev + 32)
    assert_eq("field 36", 0, ptr, reader.prev + 36)
    assert_eq("field 40", 0, zero, reader.prev + 40)

    puffers = []
    for _ in range(1, count):
        name_raw, flag, ptr, zero = reader.read(READER_LOOKUP)
        with assert_ascii("name", name_raw, reader.prev + 0):
            name = ascii_zterm_padded(name_raw)
        assert_eq("field 32", 0, (flag & 0x00FFFFFF), reader.prev + 32)
        # TODO: what does this flag mean?
        # this is something the code does, but i'm not sure why
        # some of these values make decent floating point numbers
        flag = flag >> 24
        assert_ne("field 36", 0, ptr, reader.prev + 36)
        assert_eq("field 40", 0, zero, reader.prev + 40)
        puffers.append(NamePtrFlag(name=name, ptr=ptr, flag=flag))

    return puffers
Esempio n. 11
0
def _read_node_data_light(  # pylint: disable=too-many-locals
        reader: BinReader, ) -> Light:
    (
        direction_x,  # 000
        direction_y,  # 004
        direction_z,  # 008
        trans_x,  # 012
        trans_y,  # 016
        trans_z,  # 020
        zero024,  # 112 zero bytes
        one136,
        zero140,
        zero144,
        zero148,
        zero152,
        diffuse,  # 156
        ambient,  # 160
        color_r,  # 164
        color_g,  # 168
        color_b,  # 172
        flag_raw,  # 176
        range_min,  # 180
        range_max,  # 184
        range_min_sq,  # 188
        range_max_sq,  # 192
        range_inv,  # 196
        parent_count,  # 200
        parent_ptr,  # 204
        zero208,
    ) = reader.read(LIGHT)

    # translation is never set
    assert_eq("trans x", 0.0, trans_x, reader.prev + 12)
    assert_eq("trans y", 0.0, trans_y, reader.prev + 16)
    assert_eq("trans z", 0.0, trans_z, reader.prev + 20)

    assert_all_zero("field 024", zero024, reader.prev + 24)

    assert_eq("field 136", 1.0, one136, reader.prev + 136)
    assert_eq("field 140", 0.0, zero140, reader.prev + 140)
    assert_eq("field 144", 0.0, zero144, reader.prev + 144)
    assert_eq("field 148", 0.0, zero148, reader.prev + 148)
    assert_eq("field 152", 0.0, zero152, reader.prev + 152)

    assert_between("diffuse", 0.0, 1.0, diffuse, reader.prev + 156)
    assert_between("ambient", 0.0, 1.0, ambient, reader.prev + 160)

    assert_eq("color r", 1.0, color_r, reader.prev + 164)
    assert_eq("color g", 1.0, color_g, reader.prev + 168)
    assert_eq("color b", 1.0, color_b, reader.prev + 172)

    with assert_flag("flag", flag_raw, reader.prev + 176):
        flag = LightFlag.check(flag_raw)

    assert_eq("flag", LIGHT_FLAG, flag, reader.prev + 176)

    assert_gt("range min", 0.0, range_min, reader.prev + 180)
    assert_gt("range max", range_min, range_max, reader.prev + 184)
    expected = range_min * range_min
    assert_eq("range min sq", expected, range_min_sq, reader.prev + 188)
    expected = range_max * range_max
    assert_eq("range max sq", expected, range_max_sq, reader.prev + 192)
    expected = force_single_prec(1.0 / (range_max - range_min))
    assert_eq("range inv", expected, range_inv, reader.prev + 196)

    # if this was ever zero, field 208 wouldn't be read
    assert_eq("parent count", 1, parent_count, reader.prev + 200)
    assert_ne("parent ptr", 0, parent_ptr, reader.prev + 204)
    assert_eq("field 208", 0, zero208, reader.prev + 208)

    # light nodes always have an action priority of 9

    return Light(
        type="Light",
        direction=(direction_x, direction_y, direction_z),
        diffuse=diffuse,
        ambient=ambient,
        color=(color_r, color_g, color_b),
        range=(range_min, range_max),
        parent_ptr=parent_ptr,
    )
Esempio n. 12
0
def _read_node_data_camera(  # pylint: disable=too-many-locals
        reader: BinReader, ) -> Camera:
    (
        world_index,  # 000
        window_index,  # 004
        focus_node_xy,  # 008
        focus_node_xz,  # 012
        flag_raw,  # 016
        trans_x,  # 020
        trans_y,  # 024
        trans_z,  # 028
        rot_x,  # 032
        rot_y,  # 036
        rot_z,  # 040
        zero044,  # 132 zero bytes
        clip_near_z,  # 176
        clip_far_z,  # 180
        zero184,  # 24 zero bytes
        lod_multiplier,  # 208
        lod_inv_sq,  # 212
        fov_h_zoom_factor,  # 216
        fov_v_zoom_factor,  # 220
        fov_h_base,  # 224
        fov_v_base,  # 228
        fov_h,  # 232
        fov_v,  # 236
        fov_h_half,  # 240
        fov_v_half,  # 244
        one248,
        zero252,  # 60 zero bytes
        one312,
        zero316,  # 72 zero bytes
        one388,
        zero392,  # 72 zero bytes
        zero464,
        fov_h_tan_inv,  # 468
        fov_v_tan_inv,  # 472
        stride,
        zone_set,
        unk484,
    ) = reader.read(CAMERA)

    assert_eq("world index", 0, world_index, reader.prev + 0)
    assert_eq("window index", 1, window_index, reader.prev + 4)
    assert_eq("focus node xy", -1, focus_node_xy, reader.prev + 8)
    assert_eq("focus node xz", -1, focus_node_xz, reader.prev + 12)
    assert_eq("flag", 0, flag_raw, reader.prev + 16)

    assert_eq("trans x", 0.0, trans_x, reader.prev + 20)
    assert_eq("trans y", 0.0, trans_y, reader.prev + 24)
    assert_eq("trans z", 0.0, trans_z, reader.prev + 28)

    assert_eq("rot x", 0.0, rot_x, reader.prev + 32)
    assert_eq("rot y", 0.0, rot_y, reader.prev + 36)
    assert_eq("rot z", 0.0, rot_z, reader.prev + 40)

    # WorldTranslate: Vec3
    # WorldRotate: Vec3
    # MtwMatrix: Mat
    # Unk: Vec3
    # ViewVector: Vec3
    # Matrix: Mat
    # AltTranslate: Vec3
    assert_all_zero("field 044", zero044, reader.prev + 44)

    assert_gt("clip near z", 0.0, clip_near_z, reader.prev + 176)
    assert_gt("clip far z", clip_near_z, clip_far_z, reader.prev + 180)

    assert_all_zero("field 184", zero184, reader.prev + 184)

    assert_eq("LOD mul", 1.0, lod_multiplier, reader.prev + 208)
    assert_eq("LOD inv sq", 1.0, lod_inv_sq, reader.prev + 212)

    assert_eq("FOV H zoom factor", 1.0, fov_h_zoom_factor, reader.prev + 216)
    assert_eq("FOV V zoom factor", 1.0, fov_v_zoom_factor, reader.prev + 220)
    assert_gt("FOV H base", 0.0, fov_h_base, reader.prev + 224)
    assert_gt("FOV V base", 0.0, fov_v_base, reader.prev + 228)
    assert_eq("FOV H zoomed", fov_h_base, fov_h, reader.prev + 232)
    assert_eq("FOV V zoomed", fov_v_base, fov_v, reader.prev + 236)
    assert_eq("FOV H half", fov_h / 2.0, fov_h_half, reader.prev + 240)
    assert_eq("FOV V half", fov_v / 2.0, fov_v_half, reader.prev + 244)

    assert_eq("field 248", 1, one248, reader.prev + 248)
    assert_all_zero("field 252", zero252, reader.prev + 252)

    assert_eq("field 312", 1, one312, reader.prev + 312)
    assert_all_zero("field 316", zero316, reader.prev + 316)

    assert_eq("field 388", 1, one388, reader.prev + 388)
    assert_all_zero("field 392", zero392, reader.prev + 392)

    assert_eq("field 464", 0, zero464, reader.prev + 464)

    expected = force_single_prec(1.0 / tan(fov_h_half))
    assert_eq("FOV H tan inv", expected, fov_h_tan_inv, reader.prev + 468)
    expected = force_single_prec(1.0 / tan(fov_v_half))
    assert_eq("FOV V tan inv", expected, fov_v_tan_inv, reader.prev + 472)

    assert_eq("stride", 0, stride, reader.prev + 476)
    assert_eq("zone set", 0, zone_set, reader.prev + 480)
    assert_eq("field 484", -256, unk484, reader.prev + 484)

    return Camera(type="Camera",
                  clip=(clip_near_z, clip_far_z),
                  fov=(fov_h_base, fov_v_base))
Esempio n. 13
0
def read_node_data_object3d(  # pylint: disable=too-many-locals
        reader: BinReader, ) -> Object3d:
    (
        flag_raw,  # 000
        opacity,  # 004
        zero008,
        zero012,
        zero016,
        zero020,
        rot_x,  # 024
        rot_y,  # 028
        rot_z,  # 032
        scale_x,  # 036
        scale_y,  # 040
        scale_z,  # 044
        matrix00,  # 048
        matrix01,  # 052
        matrix02,  # 056
        matrix10,  # 060
        matrix11,  # 064
        matrix12,  # 068
        matrix20,  # 072
        matrix21,  # 076
        matrix22,  # 080
        trans_x,  # 084
        trans_y,  # 088
        trans_z,  # 092
        zero096,  # 42 zero bytes
    ) = reader.read(OBJECT3D)

    assert_in("flag", (32, 40), flag_raw, reader.prev + 0)
    assert_eq("opacity", 0.0, opacity, reader.prev + 4)

    assert_eq("field 008", 0.0, zero008, reader.prev + 8)
    assert_eq("field 012", 0.0, zero012, reader.prev + 12)
    assert_eq("field 016", 0.0, zero016, reader.prev + 16)
    assert_eq("field 020", 0.0, zero020, reader.prev + 20)

    assert_eq("scale x", 1.0, scale_x, reader.prev + 36)
    assert_eq("scale y", 1.0, scale_y, reader.prev + 40)
    assert_eq("scale z", 1.0, scale_z, reader.prev + 44)

    assert_all_zero("field 096", zero096, reader.prev + 96)

    def _assert_matrix(expected: Matrix) -> None:
        assert_eq("matrix 00", expected[0], matrix00, reader.prev + 48)
        assert_eq("matrix 01", expected[1], matrix01, reader.prev + 52)
        assert_eq("matrix 02", expected[2], matrix02, reader.prev + 56)
        assert_eq("matrix 10", expected[3], matrix10, reader.prev + 60)
        assert_eq("matrix 11", expected[4], matrix11, reader.prev + 64)
        assert_eq("matrix 12", expected[5], matrix12, reader.prev + 68)
        assert_eq("matrix 20", expected[6], matrix20, reader.prev + 72)
        assert_eq("matrix 21", expected[7], matrix21, reader.prev + 76)
        assert_eq("matrix 22", expected[8], matrix22, reader.prev + 80)

    matrix_sign = extract_zero_signs(
        matrix00,
        matrix01,
        matrix02,
        matrix10,
        matrix11,
        matrix12,
        matrix20,
        matrix21,
        matrix22,
    )

    if flag_raw == 40:
        assert_eq("rot x", 0.0, rot_x, reader.prev + 24)
        assert_eq("rot y", 0.0, rot_y, reader.prev + 28)
        assert_eq("rot z", 0.0, rot_z, reader.prev + 32)

        assert_eq("trans x", 0.0, trans_x, reader.prev + 84)
        assert_eq("trans y", 0.0, trans_y, reader.prev + 88)
        assert_eq("trans z", 0.0, trans_z, reader.prev + 92)

        _assert_matrix(IDENTITY_MATRIX)

        rotation = None
        translation = None
        matrix = None
    else:
        # all values between PI
        assert_between("rot x", -PI, PI, rot_x, reader.prev + 24)
        assert_between("rot y", -PI, PI, rot_y, reader.prev + 28)
        assert_between("rot z", -PI, PI, rot_z, reader.prev + 32)

        rotation = (rot_x, rot_y, rot_z)
        translation = (trans_x, trans_y, trans_z)

        expected = euler_to_matrix(rot_x, rot_y, rot_z)

        # in most cases, the calculated matrix is correct :/
        # for 58 out of 2729 Object3D nodes, this fails though
        try:
            _assert_matrix(expected)
        except Mech3ParseError:
            matrix = (
                matrix00,
                matrix01,
                matrix02,
                matrix10,
                matrix11,
                matrix12,
                matrix20,
                matrix21,
                matrix22,
            )
            matrix_sign = 0
        else:
            matrix = None

    # object 3d nodes always have an action priority of 6

    return Object3d(
        type="Object3D",
        rotation=rotation,
        translation=translation,
        matrix=matrix,
        matrix_sign=matrix_sign,
    )
Esempio n. 14
0
    def read(  # pylint: disable=too-many-locals,too-many-branches,too-many-statements
            cls, reader: BinReader, anim_def: AnimDef) -> ObjectMotion:
        (
            flag_raw,  # 000, 012
            node_index,  # 004, 016
            zero008,  # 008, 020
            gravity_value,  # 012, 024
            zero016,  # 016, 028
            trans_range_min_1,  # 020, 032
            trans_range_max_1,  # 024, 036
            trans_range_min_2,  # 028, 040
            trans_range_max_2,  # 032, 044
            trans_range_min_3,  # 036, 048
            trans_range_max_3,  # 040, 052
            trans_range_min_4,  # 044, 056
            trans_range_max_4,  # 048, 060
            translation_1,  # 052, 064
            translation_2,  # 056, 068
            translation_3,  # 060, 072
            translation_4,  # 064, 076
            translation_5,  # 068, 080
            translation_6,  # 072, 084
            # used for translation calculations
            zero076,  # 076, 088
            zero080,  # 080, 092
            zero084,  # 084, 096
            zero088,  # 088, 100
            zero092,  # 092, 104
            zero096,  # 096, 108
            # used for translation calculations
            unk100,  # 100, 112
            unk104,  # 104, 116
            unk108,  # 108, 120
            # FORWARD_ROTATION
            forward_rotation_1,  # 112, 124
            forward_rotation_2,  # 116, 128
            zero120,  # 120, 132
            # XYZ_ROTATION
            xyz_rotation_1,  # 124, 136
            xyz_rotation_2,  # 128, 140
            xyz_rotation_3,  # 132, 144
            xyz_rotation_4,  # 136, 148
            xyz_rotation_5,  # 140, 152
            xyz_rotation_6,  # 144, 156
            # used for xyz rotation calculations
            zero148,  # 148, 160
            zero152,  # 152, 164
            zero156,  # 156, 168
            scale_1,  # 160, 172
            scale_2,  # 164, 176
            scale_3,  # 168, 180
            scale_4,  # 172, 184
            scale_5,  # 176, 188
            scale_6,  # 180, 192
            # used for scale calculations
            zero184,  # 184, 196
            zero188,  # 188, 200
            zero192,  # 192, 204
            bounce_seq0_name_raw,  # 196, 208
            bounce_seq0_sentinel,  # 228, 240
            bounce_snd0_index,  # 230, 242
            bounce_snd0_volume,  # 232, 244
            bounce_seq1_name_raw,  # 236, 248
            bounce_seq1_sentinel,  # 268, 280
            bounce_snd1_index,  # 270, 282
            bounce_snd1_volume,  # 272, 284
            bounce_seq2_name_raw,  # 276, 288
            bounce_seq2_sentinel,  # 308, 320
            bounce_snd2_index,  # 310, 322
            bounce_snd2_volume,  # 312, 324
            run_time,  # 316, 328
        ) = reader.read(cls._STRUCT)

        assert_lt("flag", 0x7FFF, flag_raw, reader.prev + 0)
        with assert_flag("flag", flag_raw, reader.prev + 0):
            flag = MotionFlag.check(flag_raw)

        node = anim_def.get_node(node_index - 1, reader.prev + 4)
        assert_eq("field 008", 0.0, zero008, reader.prev + 8)
        assert_eq("field 016", 0.0, zero016, reader.prev + 16)

        gravity_no_alt = MotionFlag.GravityNoAltitude(flag)
        gravity_complex = MotionFlag.GravityComplex(flag)

        if not MotionFlag.Gravity(flag):
            assert_eq("gravity", 0.0, gravity_value, reader.prev + 12)
            assert_eq("gravity no alt", False, gravity_no_alt, reader.prev + 0)
            assert_eq("gravity complex", False, gravity_complex,
                      reader.prev + 0)
            gravity: Gravity = None
        elif gravity_no_alt:
            assert_eq("gravity complex", False, gravity_complex,
                      reader.prev + 0)
            gravity = ("NO_ALTITUDE", gravity_value)
        elif gravity_complex:
            gravity = ("COMPLEX", gravity_value)
        else:
            gravity = ("LOCAL", gravity_value)

        if MotionFlag.TranslationMin(flag):
            translation_range_min: Optional[Vec4] = (
                trans_range_min_1,
                trans_range_min_2,
                trans_range_min_3,
                trans_range_min_4,
            )
        else:
            assert_eq("trans range min 1", 0.0, trans_range_min_1,
                      reader.prev + 20)
            assert_eq("trans range min 2", 0.0, trans_range_min_2,
                      reader.prev + 28)
            assert_eq("trans range min 3", 0.0, trans_range_min_3,
                      reader.prev + 36)
            assert_eq("trans range min 4", 0.0, trans_range_min_4,
                      reader.prev + 44)
            translation_range_min = None

        if MotionFlag.TranslationMax(flag):
            translation_range_max: Optional[Vec4] = (
                trans_range_max_1,
                trans_range_max_2,
                trans_range_max_3,
                trans_range_max_4,
            )
        else:
            assert_eq("trans range max 1", 0.0, trans_range_max_1,
                      reader.prev + 24)
            assert_eq("trans range max 2", 0.0, trans_range_max_2,
                      reader.prev + 32)
            assert_eq("trans range max 3", 0.0, trans_range_max_3,
                      reader.prev + 40)
            assert_eq("trans range max 4", 0.0, trans_range_max_4,
                      reader.prev + 48)
            translation_range_max = None

        if MotionFlag.Translation(flag):
            translation: Optional[Vec9] = (
                translation_1,
                translation_2,
                translation_3,
                translation_4,
                translation_5,
                translation_6,
                unk100,
                unk104,
                unk108,
            )
        else:
            assert_eq("translation 1", 0.0, translation_1, reader.prev + 52)
            assert_eq("translation 2", 0.0, translation_2, reader.prev + 56)
            assert_eq("translation 3", 0.0, translation_3, reader.prev + 60)
            assert_eq("translation 4", 0.0, translation_4, reader.prev + 64)
            assert_eq("translation 5", 0.0, translation_5, reader.prev + 68)
            assert_eq("translation 6", 0.0, translation_6, reader.prev + 72)
            assert_eq("field 100", 0.0, unk100, reader.prev + 100)
            assert_eq("field 104", 0.0, unk104, reader.prev + 104)
            assert_eq("field 108", 0.0, unk108, reader.prev + 108)
            translation = None

        assert_eq("field 076", 0.0, zero076, reader.prev + 76)
        assert_eq("field 080", 0.0, zero080, reader.prev + 80)
        assert_eq("field 084", 0.0, zero084, reader.prev + 84)
        assert_eq("field 088", 0.0, zero088, reader.prev + 88)
        assert_eq("field 092", 0.0, zero092, reader.prev + 92)
        assert_eq("field 096", 0.0, zero096, reader.prev + 96)

        if MotionFlag.ForwardRotationTime(flag):
            forward_rotation: ForwardRotation = (
                "TIME",
                forward_rotation_1,
                forward_rotation_2,
            )
        elif MotionFlag.ForwardRotationDistance(flag):
            assert_eq("fwd rot 2", 0.0, forward_rotation_2, reader.prev + 116)
            forward_rotation = (
                "DISTANCE",
                forward_rotation_1,
                0.0,
            )
        else:
            assert_eq("fwd rot 1", 0.0, forward_rotation_1, reader.prev + 112)
            assert_eq("fwd rot 2", 0.0, forward_rotation_2, reader.prev + 116)
            forward_rotation = None

        assert_eq("field 120", 0.0, zero120, reader.prev + 120)

        if MotionFlag.XYZRotation(flag):
            xyz_rotation: Optional[Vec6] = (
                xyz_rotation_1,
                xyz_rotation_2,
                xyz_rotation_3,
                xyz_rotation_4,
                xyz_rotation_5,
                xyz_rotation_6,
            )
        else:
            assert_eq("xyz rot 1", 0.0, xyz_rotation_1, reader.prev + 124)
            assert_eq("xyz rot 2", 0.0, xyz_rotation_2, reader.prev + 128)
            assert_eq("xyz rot 3", 0.0, xyz_rotation_3, reader.prev + 132)
            assert_eq("xyz rot 4", 0.0, xyz_rotation_4, reader.prev + 136)
            assert_eq("xyz rot 5", 0.0, xyz_rotation_5, reader.prev + 140)
            assert_eq("xyz rot 6", 0.0, xyz_rotation_6, reader.prev + 144)
            xyz_rotation = None

        assert_eq("field 148", 0.0, zero148, reader.prev + 148)
        assert_eq("field 152", 0.0, zero152, reader.prev + 152)
        assert_eq("field 156", 0.0, zero156, reader.prev + 156)

        if MotionFlag.Scale(flag):
            scale: Optional[Vec6] = (
                scale_1,
                scale_2,
                scale_3,
                scale_4,
                scale_5,
                scale_6,
            )
        else:
            assert_eq("scale 1", 0.0, scale_1, reader.prev + 160)
            assert_eq("scale 2", 0.0, scale_2, reader.prev + 164)
            assert_eq("scale 3", 0.0, scale_3, reader.prev + 168)
            assert_eq("scale 4", 0.0, scale_4, reader.prev + 172)
            assert_eq("scale 5", 0.0, scale_5, reader.prev + 176)
            assert_eq("scale 6", 0.0, scale_6, reader.prev + 180)
            scale = None

        assert_eq("field 184", 0.0, zero184, reader.prev + 184)
        assert_eq("field 188", 0.0, zero188, reader.prev + 188)
        assert_eq("field 192", 0.0, zero192, reader.prev + 192)

        assert_eq("bounce seq 0 sentinel", -1, bounce_seq0_sentinel,
                  reader.prev + 228)
        assert_eq("bounce seq 1 sentinel", -1, bounce_seq1_sentinel,
                  reader.prev + 268)
        assert_eq("bounce seq 2 sentinel", -1, bounce_seq2_sentinel,
                  reader.prev + 308)

        bounce_sequence = []
        if MotionFlag.BounceSeq(flag):
            with assert_ascii("bounce seq 0 name", bounce_seq0_name_raw,
                              reader.prev + 196):
                bounce_seq0_name = ascii_zterm_padded(bounce_seq0_name_raw)

            # should have at least one value
            assert_ne("bounce seq 0 name", "", bounce_seq0_name,
                      reader.prev + 196)
            bounce_sequence.append(bounce_seq0_name)

            with assert_ascii("bounce seq 1 name", bounce_seq1_name_raw,
                              reader.prev + 236):
                bounce_seq1_name = ascii_zterm_padded(bounce_seq1_name_raw)
            if bounce_seq1_name:
                bounce_sequence.append(bounce_seq1_name)

            with assert_ascii("bounce seq 2 name", bounce_seq2_name_raw,
                              reader.prev + 276):
                bounce_seq2_name = ascii_zterm_padded(bounce_seq2_name_raw)
            if bounce_seq2_name:
                bounce_sequence.append(bounce_seq2_name)

        else:
            assert_all_zero("bounce seq 0 name", bounce_seq0_name_raw,
                            reader.prev + 196)
            assert_all_zero("bounce seq 1 name", bounce_seq1_name_raw,
                            reader.prev + 236)
            assert_all_zero("bounce seq 2 name", bounce_seq2_name_raw,
                            reader.prev + 276)

        bounce_sounds = []
        if MotionFlag.BounceSound(flag):

            # should have at least one value
            assert_gt("bounce snd 0 volume", 0.0, bounce_snd0_volume,
                      reader.prev + 232)
            sound = anim_def.get_sound(bounce_snd0_index - 1,
                                       reader.prev + 230)
            bounce_sounds.append((sound, bounce_snd0_volume))
        else:
            assert_eq("bounce snd 0 sound", 0, bounce_snd0_index,
                      reader.prev + 230)
            assert_eq("bounce snd 0 volume", 0.0, bounce_snd0_volume,
                      reader.prev + 232)

        # these are never set, regardless of the flag
        assert_eq("bounce snd 1 sound", 0, bounce_snd1_index,
                  reader.prev + 270)
        assert_eq("bounce snd 1 volume", 0.0, bounce_snd1_volume,
                  reader.prev + 272)

        assert_eq("bounce snd 2 sound", 0, bounce_snd2_index,
                  reader.prev + 310)
        assert_eq("bounce snd 2 volume", 0.0, bounce_snd2_volume,
                  reader.prev + 312)

        if MotionFlag.RunTime(flag):
            assert_gt("run time", 0.0, run_time, reader.prev + 316)
        else:
            assert_eq("run time", 0.0, run_time, reader.prev + 316)
            run_time = None

        return cls(
            node=node,
            gravity=gravity,
            impact_force=MotionFlag.ImpactForce(flag),
            translation_range_min=translation_range_min,
            translation_range_max=translation_range_max,
            translation=translation,
            forward_rotation=forward_rotation,
            xyz_rotation=xyz_rotation,
            scale=scale,
            bounce_sequence=bounce_sequence,
            bounce_sounds=bounce_sounds,
            run_time=run_time,
        )
Esempio n. 15
0
def read_anim_def(  # pylint: disable=too-many-locals,too-many-branches,too-many-statements
    reader: BinReader, ) -> Tuple[AnimDef, AnimDefPointers]:
    (
        anim_name_raw,
        name_raw,
        base_node_ptr,  # 064
        anim_root_raw,
        anim_root_ptr,
        zero104,  # 44 zero bytes
        flag_raw,  # 148
        zero152,
        activation_value,
        action_prio,
        byte155,
        exec_by_range_min,
        exec_by_range_max,
        reset_time,
        zero168,
        max_health,
        cur_health,
        zero180,
        zero184,
        zero188,
        zero192,
        seq_defs_ptr,  # 196
        reset_state_ptr,  # 200
        int204,
        int208,
        int212,
        zero216,  # 40 zero bytes
        reset_state_events_ptr,  # 256
        reset_state_events_len,  # 260
        seq_def_count,  # 264
        object_count,  # 265
        node_count,  # 266
        light_count,  # 267
        puffer_count,  # 268
        dynamic_sound_count,  # 269
        static_sound_count,  # 270
        unknown_count,  # 271
        activ_prereq_count,  # 272
        activ_prereq_min_to_satisfy,  # 273
        anim_ref_count,  # 274
        zero275,
        objects_ptr,  # 276
        nodes_ptr,  # 280
        lights_ptr,  # 284
        puffers_ptr,  # 288
        dynamic_sounds_ptr,  # 292
        static_sounds_ptr,  # 296
        unknown_ptr,  # 300
        activ_prereqs_ptr,  # 304
        anim_refs_ptr,  # 308
        zero312,
    ) = reader.read(ANIM_DEF)

    # save this so we can output accurate offsets after doing further reads
    data_offset = reader.prev

    with assert_ascii("anim name", anim_name_raw, reader.prev + 0):
        anim_name, _anim_name_pad = ascii_zterm_partition(anim_name_raw)

    with assert_ascii("name", name_raw, reader.prev + 32):
        name = ascii_zterm_padded(name_raw)

    assert_ne("base node ptr", 0, base_node_ptr, data_offset + 64)

    with assert_ascii("anim root", anim_root_raw, reader.prev + 68):
        anim_root, _anim_root_pad = ascii_zterm_partition(anim_root_raw)

    if name != anim_root:
        assert_ne("anim root ptr", base_node_ptr, anim_root_ptr,
                  data_offset + 100)
    else:
        assert_eq("anim root ptr", base_node_ptr, anim_root_ptr,
                  data_offset + 100)

    assert_all_zero("field 104", zero104, data_offset + 104)

    with assert_flag("flag", flag_raw, data_offset + 148):
        flag = AnimDefFlag.check(flag_raw)

    network_log: Optional[bool] = None
    if AnimDefFlag.NetworkLogSet(flag):
        network_log = AnimDefFlag.NetworkLogOn(flag)

    save_log: Optional[bool] = None
    if AnimDefFlag.SaveLogSet(flag):
        save_log = AnimDefFlag.SaveLogOn(flag)

    assert_eq("field 152", 0, zero152, data_offset + 152)
    assert_in("field 153", (0, 1, 2, 3, 4), activation_value,
              data_offset + 153)
    assert_eq("field 154", 4, action_prio, data_offset + 154)
    assert_eq("field 155", 2, byte155, data_offset + 155)

    exec_by_zone = AnimDefFlag.ExecutionByZone(flag)
    if not AnimDefFlag.ExecutionByRange(flag):
        assert_eq("exec by range min", 0.0, exec_by_range_min,
                  data_offset + 156)
        assert_eq("exec by range max", 0.0, exec_by_range_max,
                  data_offset + 156)
        exec_by_range = None
    else:
        assert_eq("exec by zone", False, exec_by_zone, data_offset + 148)
        assert_ge("exec by range min", 0.0, exec_by_range_min,
                  data_offset + 156)
        assert_ge("exec by range max", exec_by_range_max, exec_by_range_max,
                  data_offset + 156)
        exec_by_range = (exec_by_range_min, exec_by_range_max)

    if not AnimDefFlag.ResetUnk(flag):
        assert_eq("reset time", -1.0, reset_time, data_offset + 164)
        reset_time = None

    assert_eq("field 168", 0.0, zero168, data_offset + 168)
    assert_ge("health", 0.0, max_health, data_offset + 172)
    assert_eq("health", max_health, cur_health, data_offset + 176)
    assert_eq("field 180", 0, zero180, data_offset + 180)
    assert_eq("field 184", 0, zero184, data_offset + 184)
    assert_eq("field 188", 0, zero188, data_offset + 188)
    assert_eq("field 192", 0, zero192, data_offset + 192)

    # WTF???
    assert_eq("field 200", 0x45534552, int200, data_offset + 200)
    assert_eq("field 204", 0x45535F54, int204, data_offset + 204)
    assert_eq("field 208", 0x4E455551, int208, data_offset + 208)
    assert_eq("field 212", 0x00004543, int212, data_offset + 212)

    assert_all_zero("field 216", zero216, data_offset + 216)

    assert_eq("field 275", 0, zero275, data_offset + 275)
    assert_eq("field 312", 0, zero312, data_offset + 312)

    activation = ANIM_ACTIVATION[activation_value]

    if object_count:
        assert_ne("object ptr", 0, objects_ptr, data_offset + 276)
        objects = _read_objects(reader, object_count)
    else:
        assert_eq("object ptr", 0, objects_ptr, data_offset + 276)
        objects = []

    if node_count:
        assert_ne("node ptr", 0, nodes_ptr, data_offset + 280)
        nodes = _read_nodes(reader, node_count)
    else:
        assert_eq("node ptr", 0, nodes_ptr, data_offset + 280)
        nodes = []

    if light_count:
        assert_ne("light ptr", 0, lights_ptr, data_offset + 284)
        lights = _read_lights(reader, light_count)
    else:
        assert_eq("light ptr", 0, lights_ptr, data_offset + 284)
        lights = []

    if puffer_count:
        assert_ne("puffer ptr", 0, puffers_ptr, data_offset + 288)
        puffers = _read_puffers(reader, puffer_count)
    else:
        assert_eq("puffer ptr", 0, puffers_ptr, data_offset + 288)
        puffers = []

    if dynamic_sound_count:
        assert_ne("dynamic sound ptr", 0, dynamic_sounds_ptr,
                  data_offset + 292)
        dynamic_sounds = _read_dynamic_sounds(reader, dynamic_sound_count)
    else:
        assert_eq("dynamic sound ptr", 0, dynamic_sounds_ptr,
                  data_offset + 292)
        dynamic_sounds = []

    if static_sound_count:
        assert_ne("static sound ptr", 0, static_sounds_ptr, data_offset + 296)
        static_sounds = _read_static_sounds(reader, static_sound_count)
    else:
        assert_eq("static sound ptr", 0, static_sounds_ptr, data_offset + 296)
        static_sounds = []

    # this isn't set in any file i have (it is read like the static sound data)
    assert_eq("unknown count", 0, unknown_count, data_offset + 271)
    assert_eq("unknown ptr", 0, unknown_ptr, data_offset + 300)

    if activ_prereq_count:
        assert_ne("activ prereq ptr", 0, activ_prereqs_ptr, data_offset + 304)
        assert_in(
            "activ prereq min",
            (0, 1, 2),
            activ_prereq_min_to_satisfy,
            data_offset + 273,
        )
        activ_prereq = read_activation_prereq(
            reader,
            activ_prereq_count,
            activ_prereq_min_to_satisfy,
        )
    else:
        assert_eq("activ prereq ptr", 0, activ_prereqs_ptr, data_offset + 304)
        assert_eq("activ prereq min", 0, activ_prereq_min_to_satisfy,
                  data_offset + 273)
        activ_prereq = None

    if anim_ref_count:
        assert_ne("anim ref ptr", 0, anim_refs_ptr, data_offset + 308)
        anim_refs = _read_anim_refs(reader, anim_ref_count)
    else:
        assert_eq("anim ref ptr", 0, anim_refs_ptr, data_offset + 308)
        anim_refs = []

    base_name = name.replace(".flt", "")
    if name == anim_root:
        file_name = f"{base_name}-{anim_name}.json"
    else:
        file_name = f"{base_name}-{anim_name}-{anim_root}.json"

    anim_def = AnimDef(
        name=name,
        anim_name=anim_name,
        anim_root=anim_root,
        file_name=file_name,
        # ---
        auto_reset_node_states=AnimDefFlag.AutoResetNodeStates(flag),
        activation=activation,
        execution_by_range=exec_by_range,
        execution_by_zone=exec_by_zone,
        network_log=network_log,
        save_log=save_log,
        has_callback=AnimDefFlag.HasCallback(flag),
        callback_count=0,
        reset_time=reset_time,
        health=max_health,
        proximity_damage=AnimDefFlag.ProximityDamage(flag),
        # ---
        objects=objects,
        nodes=nodes,
        lights=lights,
        puffers=puffers,
        dynamic_sounds=dynamic_sounds,
        static_sounds=static_sounds,
        activation_prereq=activ_prereq,
        anim_refs=anim_refs,
        # skip reset_sequence and sequences, as they need to look up other items
    )

    # unconditional read
    anim_def.reset_sequence = _read_reset_state(
        reader,
        anim_def,
        reset_state_events_len,
        reset_state_events_ptr,
        data_offset + 256,
    )

    # this could be zero, in which case the pointer would also be NULL (but never is)
    assert_gt("seq count", 0, seq_def_count, data_offset + 264)
    assert_ne("seq ptr", 0, seq_defs_ptr, data_offset + 196)
    anim_def.sequences = _read_sequence_definitions(reader, anim_def,
                                                    seq_def_count)

    # the Callback script object checks if callbacks are allowed, but i also
    # want to catch the case where the flag might've been set, but no callbacks
    # were in the scripts
    if anim_def.has_callback:
        assert_gt("callbacks", 0, anim_def.callback_count, data_offset + 148)

    # don't need this value any more
    anim_def.callback_count = 0

    pointers = AnimDefPointers(
        file_name=file_name,
        objects_ptr=objects_ptr,
        nodes_ptr=nodes_ptr,
        lights_ptr=lights_ptr,
        puffers_ptr=puffers_ptr,
        dynamic_sounds_ptr=dynamic_sounds_ptr,
        static_sounds_ptr=static_sounds_ptr,
        activ_prereqs_ptr=activ_prereqs_ptr,
        anim_refs_ptr=anim_refs_ptr,
        reset_state_ptr=reset_state_ptr,
        reset_state_events_ptr=reset_state_events_ptr,
        seq_defs_ptr=seq_defs_ptr,
    )

    return anim_def, pointers