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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
#include "serialization-component.hpp"
#include "ecs.hpp"
#include "entities.hpp"
#include "serialization.hpp"
#include "math-helpers.hpp"
#include "BulletCollision/CollisionShapes/btCollisionShape.h"
#include "pk.h"
#define SRLZTN_FLOAT_WIDTH 10
bool pke_serialize_inst_pos(srlztn_serialize_helper *h, const glm::vec3 pos, const glm::quat quat_rot, const glm::vec3 scale) {
if (pos != glm::vec3(0)) {
h->o << SRLZTN_POSROT_POS << "["
<< std::setw(SRLZTN_FLOAT_WIDTH) << pos[0] << ","
<< std::setw(SRLZTN_FLOAT_WIDTH) << pos[1] << ","
<< std::setw(SRLZTN_FLOAT_WIDTH) << pos[2] << "]" << std::endl;
}
if (quat_rot != glm::quat{}) {
h->o << SRLZTN_POSROT_ROT << "["
<< std::setw(SRLZTN_FLOAT_WIDTH) << quat_rot[0] << ","
<< std::setw(SRLZTN_FLOAT_WIDTH) << quat_rot[1] << ","
<< std::setw(SRLZTN_FLOAT_WIDTH) << quat_rot[2] << ","
<< std::setw(SRLZTN_FLOAT_WIDTH) << quat_rot[3] << "]" << std::endl;
}
if (scale != glm::vec3(1)) {
h->o << SRLZTN_POSROT_SCALE << "["
<< std::setw(SRLZTN_FLOAT_WIDTH) << scale[0] << ","
<< std::setw(SRLZTN_FLOAT_WIDTH) << scale[1] << ","
<< std::setw(SRLZTN_FLOAT_WIDTH) << scale[2] << "]" << std::endl;
}
return true;
}
bool pke_deserialize_inst_pos(srlztn_deserialize_helper *h, glm::vec3 &pos, glm::quat &quat_rot, glm::vec3 &scale) {
const char *starting_char;
uint64_t prefix_len;
PK_STN_RES stn_res;
if (strstr(h->read_line, SRLZTN_POSROT_POS)) {
prefix_len = strlen(SRLZTN_POSROT_POS);
starting_char = strchr(h->read_line + prefix_len, '[') + 1;
assert(starting_char != nullptr);
char *pEnd = nullptr;
long index = 0;
do {
assert(index < 3);
stn_res = pk_stn(&pos[index], starting_char, &pEnd);
if (stn_res != PK_STN_RES_SUCCESS) return false;
starting_char = pEnd + 1;
++index;
} while (*pEnd != ']');
return true;
}
if (strstr(h->read_line, SRLZTN_POSROT_ROT)) {
prefix_len = strlen(SRLZTN_POSROT_ROT);
starting_char = strchr(h->read_line + prefix_len, '[') + 1;
assert(starting_char != nullptr);
char *pEnd = nullptr;
long index = 0;
do {
assert(index < 4);
stn_res = pk_stn(&quat_rot[index], starting_char, &pEnd);
if (stn_res != PK_STN_RES_SUCCESS) return false;
starting_char = pEnd + 1;
++index;
} while (*pEnd != ']');
return true;
}
if (strstr(h->read_line, SRLZTN_POSROT_SCALE)) {
prefix_len = strlen(SRLZTN_POSROT_SCALE);
starting_char = strchr(h->read_line + prefix_len, '[') + 1;
assert(starting_char != nullptr);
char *pEnd = nullptr;
long index = 0;
do {
assert(index < 3);
stn_res = pk_stn(&scale[index], starting_char, &pEnd);
if (stn_res != PK_STN_RES_SUCCESS) return false;
starting_char = pEnd + 1;
++index;
} while (*pEnd != ']');
return true;
}
return false;
}
bool pke_serialize_instance(srlztn_serialize_helper *h, const CompInstance *comp) {
EntityType *et = nullptr;
if (comp->grBindsHandle != GrBindsHandle_MAX) {
et = EntityType_FindByEntityHandle(ECS_GetGrBinds(comp->grBindsHandle)->entHandle);
}
CompInstance c{};
glm::vec3 pos;
glm::quat quat_rot;
glm::vec3 scale;
float mass;
PhysicsCollision collisionLayer;
PhysicsCollision collisionMask;
{
mass = comp->bt.rigidBody->getMass();
collisionLayer = PhysicsCollision{static_cast<PhysicsCollision_T>(comp->bt.rigidBody->getBroadphaseProxy()->m_collisionFilterGroup)};
collisionMask = PhysicsCollision{static_cast<PhysicsCollision_T>(comp->bt.rigidBody->getBroadphaseProxy()->m_collisionFilterMask)};
btTransform trans;
comp->bt.motionState->getWorldTransform(trans);
BulletToGlm(trans.getOrigin(), pos);
BulletToGlm(trans.getRotation(), quat_rot);
BulletToGlm(comp->bt.rigidBody->getCollisionShape()->getLocalScaling(), scale);
}
if (comp->uuid != pk_uuid_zed && comp->uuid != pk_uuid_max) {
h->o << SRLZTN_INSTANCE_COMPONENT_UUID << comp->uuid << std::endl;
}
if (et != nullptr) {
h->o << SRLZTN_INSTANCE_COMPONENT_ENTITY_TYPE_CODE << et->entityTypeCode.val << std::endl;
}
pke_serialize_inst_pos(h, pos, quat_rot, scale);
if (mass != 1) {
h->o << SRLZTN_INSTANCE_COMPONENT_MASS << mass << std::endl;
}
if (collisionLayer != c.physicsLayer) {
h->o << SRLZTN_INSTANCE_COMPONENT_COLLISION_LAYER << static_cast<PhysicsCollision_T>(collisionLayer) << std::endl;
}
if (collisionMask != c.physicsMask) {
h->o << SRLZTN_INSTANCE_COMPONENT_COLLISION_MASK << static_cast<PhysicsCollision_T>(collisionMask) << std::endl;
}
if (comp->collisionCallback.name[0] != '\0') {
h->o << SRLZTN_INSTANCE_COMPONENT_COLLISION_CB_SIGNATURE << comp->collisionCallback.name << std::endl;
}
return true;
}
bool pke_deserialize_instance(srlztn_deserialize_helper *h) {
uint64_t prefix_len;
PK_STN_RES stn_res;
EntityType *et_ptr = nullptr;
float mass;
InstPos inst_pos;
CompInstance comp{};
glm::vec3 pos = glm::vec3(0);
glm::quat quat_rot = glm::quat(0, 0, 0, 1);
glm::vec3 scale = glm::vec3(1);
comp.collisionCallback.name[0] = '\0';
while (h->i->getline(h->read_line, h->read_line_len)) {
if (strstr(SRLZTN_OBJ_END, h->read_line)) {
if (et_ptr == nullptr) {
fprintf(stdout, "[Game::DeserializeInstance] Unknown EntityTypeCode, skipping instance.\n");
break;
}
btVector3 bt_pos;
btQuaternion bt_quat;
GlmToBullet(pos, bt_pos);
GlmToBullet(quat_rot, bt_quat);
GlmToBullet(scale, inst_pos.scale);
inst_pos.mass = mass;
inst_pos.posRot.setIdentity();
inst_pos.posRot.setOrigin(bt_pos);
inst_pos.posRot.setRotation(bt_quat);
if (et_ptr->createInstanceCallback.func != nullptr) {
/* TODO 2025-03-27 JCB
* We have not yet defined what the appropriate callback signature
* for creating an entity instance is.
* What should be passed as arguments? What would need to be passed
* that couldn't be accessed globally?
* Consider changing this callback to trigger after creating a
* generic instance, rather than *creating* it.
* Also consider just requiring a generic instance for any given
* EntityType.
*/
// typedef Entity_Base *CreateInst();
// entity = reinterpret_cast<CreateInst*>(et_ptr->createInstanceCallback.func)();
fprintf(stderr, "[%s] Attempted to call EntityType::createInstanceCallback and we have not yet defined a valid function signature", __FILE__);
} else {
srlztn_instance_mapping map{};
map.serialized_uuid = comp.uuid;
map.created_entity = EntityType_CreateGenericInstance(et_ptr, h->scene, &comp, &inst_pos);
// TODO gross
pk_arr_t<CompInstance *> instances;
ECS_GetInstances(map.created_entity, instances);
for (uint32_t i = 0; i < instances.next; ++i) {
if (comp.uuid == instances[i]->uuid) {
map.created_instance = instances[i];
}
}
if (map.created_instance == nullptr) {
fprintf(stderr, "[pke_deserialize_instance] Failed to find created instance for creating mapping");
} else {
pk_arr_append(&h->mapping, &map);
}
}
return true;
}
if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_UUID)) {
prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_UUID);
(h->read_line + prefix_len) >> comp.uuid ;
continue;
}
if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_ENTITY_TYPE_CODE)) {
prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_ENTITY_TYPE_CODE);
if (strlen(h->read_line + prefix_len) > 1) {
et_ptr = EntityType_FindByTypeCode(h->read_line + prefix_len);
}
continue;
}
if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_MASS)) {
prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_MASS);
stn_res = pk_stn(&mass, h->read_line + prefix_len, nullptr);
if (stn_res != PK_STN_RES_SUCCESS) return false;
continue;
}
if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_COLLISION_LAYER)) {
prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_COLLISION_LAYER);
stn_res = pk_stn(&comp.physicsLayer, h->read_line + prefix_len, nullptr, 10);
if (stn_res != PK_STN_RES_SUCCESS) return false;
continue;
}
if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_COLLISION_MASK)) {
prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_COLLISION_MASK);
stn_res = pk_stn(&comp.physicsMask, h->read_line + prefix_len, nullptr, 10);
if (stn_res != PK_STN_RES_SUCCESS) return false;
continue;
}
if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_COLLISION_CB_SIGNATURE)) {
prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_COLLISION_CB_SIGNATURE);
strncpy(comp.collisionCallback.name, h->read_line + prefix_len, 16);
continue;
}
if (pke_deserialize_inst_pos(h, pos, quat_rot, scale)) {
continue;
}
}
return false;
}
|