-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNodeCollisionUtils.cpp
More file actions
173 lines (148 loc) · 6 KB
/
NodeCollisionUtils.cpp
File metadata and controls
173 lines (148 loc) · 6 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "NodeCollisionUtils.h"
#include "File.h"
#include "KEnvironment.h"
#include "CKUtils.h"
#include "CoreClasses/CKService.h"
#include "CoreClasses/CKHook.h"
#include "CoreClasses/CKGroup.h"
#include "CoreClasses/CKComponent.h"
#include "CoreClasses/CKNode.h"
#include "CoreClasses/CKLogic.h"
#include <fmt/format.h>
#include <nlohmann/json.hpp>
namespace {
nlohmann::ordered_json g_jsCollisionInfo;
struct ShapeMemberListener : NamedMemberListener {
CKObject* owner;
std::string_view memberName;
std::vector<std::pair<CKObject*, CKBoundingShape*>> shapes;
void reflect(uint8_t& ref, const char* name) override {}
void reflect(uint16_t& ref, const char* name) override {}
void reflect(uint32_t& ref, const char* name) override {}
void reflect(float& ref, const char* name) override {}
void reflectAnyRef(kanyobjref& ref, int clfid, const char* name) override {
if (!ref)
return;
CKBoundingShape* shape = ref.get()->dyncast<CKBoundingShape>();
if (!shape)
return;
if (getFullName(name) == memberName)
shapes.push_back({ owner, shape });
}
void reflect(Vector3& ref, const char* name) override {}
void reflect(EventNode& ref, const char* name, CKObject* user) override {}
void reflect(MarkerIndex& ref, const char* name) override {}
void reflect(std::string& ref, const char* name) override {}
};
std::vector<std::pair<CKObject*, CKBoundingShape*>> getAllShapesFromMember(KEnvironment& kenv, int ownerFullId, std::string_view memberName)
{
if (ownerFullId == CKCrateCpnt::FULL_ID && memberName == "!crateCloneOBB") {
std::vector<std::pair<CKObject*, CKBoundingShape*>> result;
for (CKObject* obj : kenv.levelObjects.getClassType<CKCrateCpnt>().objects) {
CKCrateCpnt* cpnt = (CKCrateCpnt*)obj;
for (CKSceneNode* crateNode = cpnt->crateNode.get(); crateNode; crateNode = crateNode->next.get()) {
CSGBranch* crateBranch = crateNode->cast<CSGBranch>();
CKBoundingShape* crateVolume = crateBranch->child->cast<CKBoundingShape>();
result.push_back({ cpnt, crateVolume });
}
}
return result;
}
ShapeMemberListener sml;
sml.memberName = memberName;
auto reflectAs = [&]<typename T>() {
auto& cltype = kenv.levelObjects.getClassType(ownerFullId);
if (cltype.objects.empty() || !cltype.objects[0]->isSubclassOf<T>())
return;
for (CKObject* obj : cltype.objects) {
sml.owner = obj;
T* tobj = obj->cast<T>();
tobj->virtualReflectMembers(sml, &kenv);
}
};
reflectAs.template operator() < CKReflectableService > ();
reflectAs.template operator() < CKHook > ();
reflectAs.template operator() < CKGroup > ();
reflectAs.template operator() < CKComponent > ();
reflectAs.template operator() < CKReflectableLogic > ();
return std::move(sml.shapes);
}
CKBoundingShape* getShapeFromMember(KEnvironment& kenv, CKObject* owner, std::string_view memberName, CKBoundingShape* parentShape = nullptr)
{
if (owner->getClassFullID() == CKCrateCpnt::FULL_ID && memberName == "!crateCloneOBB") {
return parentShape;
}
ShapeMemberListener sml;
sml.owner = owner;
sml.memberName = memberName;
auto reflectAs = [&]<typename T>() {
if (T* tobj = owner->dyncast<T>()) {
tobj->virtualReflectMembers(sml, &kenv);
}
};
reflectAs.template operator() < CKReflectableService > ();
reflectAs.template operator() < CKHook > ();
reflectAs.template operator() < CKGroup > ();
reflectAs.template operator() < CKComponent > ();
reflectAs.template operator() < CKReflectableLogic > ();
assert(sml.shapes.size() < 2);
return sml.shapes.empty() ? nullptr : sml.shapes[0].second;
}
}
void NodeCollisionUtils::LoadNodeCollisionInfo(int gameVersion)
{
if (!g_jsCollisionInfo.empty())
return;
const auto fileName = fmt::format("NodeCollisionTestInfo_{}.json", gameVersion);
auto [fileData, fileSize] = GetResourceContent(fileName.c_str());
g_jsCollisionInfo = nlohmann::json::parse(std::string_view((const char*)fileData, fileSize));
for (auto& jsTest : g_jsCollisionInfo.at("collisionTests")) {
if (jsTest.at("obj1").at(0).get<int>() > jsTest.at("obj2").at(0).get<int>()) {
std::swap(jsTest.at("obj1"), jsTest.at("obj2"));
}
if (jsTest.contains("children")) {
for (auto& jsChild : jsTest.at("children")) {
if (jsChild.at("obj1").at(0).get<int>() > jsChild.at("obj2").at(0).get<int>()) {
std::swap(jsChild.at("obj1"), jsChild.at("obj2"));
}
}
}
}
}
void NodeCollisionUtils::CreateCollisionsForObject(KEnvironment& kenv, CKObject* owner)
{
LoadNodeCollisionInfo(kenv.version);
CKSrvCollision* srvCollision = kenv.levelObjects.getFirst<CKSrvCollision>();
auto jsTestList = g_jsCollisionInfo.at("collisionTests");
const int classFullID = owner->getClassFullID();
for (const auto& jsTest : jsTestList) {
for (const std::string_view objkey : { "obj1", "obj2" }) {
if (jsTest.at(objkey).at(0) == classFullID) {
const std::string_view otherObjkey = objkey == "obj1" ? "obj2" : "obj1";
const int otherClassFullID = jsTest.at(otherObjkey).at(0);
auto* objShape = getShapeFromMember(kenv, owner, jsTest.at(objkey).at(1));
if (!objShape) {
break;
}
auto otherShapes = getAllShapesFromMember(kenv, otherClassFullID, jsTest.at(otherObjkey).at(1));
for (auto [otherOwner, otherShape] : otherShapes) {
if (owner == otherOwner)
continue;
auto testIndex = srvCollision->addCollision(owner, objShape, otherOwner, otherShape);
if (jsTest.contains("children")) {
for (const auto& jsChild : jsTest.at("children")) {
assert(jsChild.at(objkey).at(0) == classFullID);
assert(jsChild.at(otherObjkey).at(0) == otherClassFullID);
auto* childShape = getShapeFromMember(kenv, owner, jsChild.at(objkey).at(1), objShape);
auto* otherChildShape = getShapeFromMember(kenv, otherOwner, jsChild.at(otherObjkey).at(1), otherShape);
assert(childShape && otherChildShape);
auto childIndex = srvCollision->addCollision(owner, childShape, otherOwner, otherChildShape);
srvCollision->setParent(childIndex, testIndex);
}
}
}
break;
}
}
}
}