summaryrefslogtreecommitdiff
path: root/src/KingSystem/Physics/System/physParamSet.cpp
blob: 3af099491632d852b70c2529f61f305ef7314ada (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "KingSystem/Physics/System/physParamSet.h"
#include "KingSystem/Physics/Cloth/physClothParam.h"
#include "KingSystem/Physics/Ragdoll/physRagdollParam.h"
#include "KingSystem/Physics/RigidBody/physEdgeRigidBodyParam.h"
#include "KingSystem/Physics/RigidBody/physRigidBodyParam.h"
#include "KingSystem/Physics/RigidBody/physRigidBodySetParam.h"
#include "KingSystem/Physics/SupportBone/physSupportBoneParam.h"
#include "KingSystem/Physics/System/physCharacterControllerParam.h"
#include "KingSystem/Physics/System/physContactInfoParam.h"
#include "KingSystem/Resource/resResourcePhysics.h"

namespace ksys::phys {

ParamSet::ParamSet()
    : use_rigid_body_set_num(0, "use_rigid_body_set_num", &obj),
      use_character_controller(false, "use_character_controller", &obj),
      use_ragdoll(false, "use_ragdoll", &obj), use_support_bone(false, "use_support_bone", &obj),
      use_cloth(false, "use_cloth", &obj), use_contact_info(false, "use_contact_info", &obj),
      use_system_group_handler(true, "use_system_group_handler", &obj),
      use_edge_rigid_body_num(false, "use_edge_rigid_body_num", &obj) {}

ParamSet::~ParamSet() {
    finalize();
}

bool ParamSet::parse(res::Physics* bphysics, agl::utl::ResParameterArchive archive,
                     sead::Heap* heap) {
    if (!archive.isValid())
        return false;

    if (!doParse(bphysics, archive, heap)) {
        finalize();
        return false;
    }

    return true;
}

template <typename T>
static void safeDelete(T*& ptr) {
    if (!ptr)
        return;
    delete ptr;
    ptr = nullptr;
}

void ParamSet::finalize() {
    rigid_body_sets.freeBuffer();
    safeDelete(character_controller);
    safeDelete(contact_info);
    safeDelete(cloth_set);
    safeDelete(ragdoll);
    safeDelete(support_bone);
    safeDelete(edge_rigid_body_set);
    *use_rigid_body_set_num = 0;
    *use_ragdoll = false;
    *use_cloth = false;
    *use_support_bone = false;
    *use_edge_rigid_body_num = 0;
}

bool ParamSet::doParse(res::Physics* bphysics, agl::utl::ResParameterArchive archive,
                       sead::Heap* heap) {
    const auto ParamSet = archive.getRootList().getResParameterList(0);
    if (ParamSet.getResParameterObjNum() < 1)
        return false;

    const auto header_obj = ParamSet.getResParameterObj(0);
    obj.applyResParameterObj(header_obj);

    const int num_rigid_body_sets = *use_rigid_body_set_num;
    if (num_rigid_body_sets > 0) {
        rigid_body_sets.allocBufferAssert(num_rigid_body_sets, heap);

        const auto RigidBodySet = agl::utl::getResParameterList(ParamSet, "RigidBodySet");
        if (!RigidBodySet)
            return false;

        for (auto it = RigidBodySet.listBegin(), end = RigidBodySet.listEnd(); it != end; ++it) {
            if (!rigid_body_sets[it.getIndex()].parse(it.getList(), heap))
                return false;
        }
    }

    if (*use_character_controller) {
        character_controller = new (heap) CharacterControllerParam;
        if (!character_controller->parse(
                agl::utl::getResParameterList(ParamSet, "CharacterController"), heap)) {
            return false;
        }
    }

    if (*use_contact_info) {
        contact_info = new (heap) ContactInfoParam;
        if (!contact_info->parse(agl::utl::getResParameterList(ParamSet, "RigidContactInfo"), heap))
            return false;
    }

    if (*use_support_bone) {
        support_bone = new (heap) SupportBoneParam;
        addObj(support_bone, "SupportBone");
    }

    if (*use_ragdoll) {
        ragdoll = new (heap) RagdollParam;
        addObj(ragdoll, "Ragdoll");
    }

    if (*use_cloth) {
        cloth_set = new (heap) ClothSetParam;
        const auto list = agl::utl::getResParameterList(ParamSet, "Cloth");
        if (list) {
            addList(cloth_set, "Cloth");
            if (!cloth_set->parse(list, heap))
                return false;
        }
    }

    if (*use_edge_rigid_body_num > 0) {
        edge_rigid_body_set = new (heap) EdgeRigidBodySetParam;
        edge_rigid_body_set->parse(*use_edge_rigid_body_num, heap);
        addList(edge_rigid_body_set, "EdgeRigidBody");
    }

    applyResParameterList(ParamSet);

    num_rigid_bodies_with_link_matrix = 0;
    for (int set_idx = 0; set_idx < num_rigid_body_sets; ++set_idx) {
        auto& set = rigid_body_sets[set_idx];

        const int num_rigid_bodies = set.getNumRigidBodies();
        for (int body_idx = 0; body_idx < num_rigid_bodies; ++body_idx) {
            if (!set.rigid_bodies[body_idx].info.link_matrix->isEmpty())
                ++num_rigid_bodies_with_link_matrix;
        }
    }

    bphysics->addList(this, "ParamSet");
    return true;
}

RigidBodySetParam& ParamSet::getRigidBodySet(int idx) {
    return rigid_body_sets[idx];
}

}  // namespace ksys::phys