Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 645e18d

Browse files
authored
Merge pull request #219 from storm-devs/feature/msg-model-substitute-geometry-node
model, ship: add MSG_MODEL_SUBSTITUTE_GEOMETRY_NODE message
2 parents 410e4d0 + 70cec78 commit 645e18d

File tree

7 files changed

+73
-66
lines changed

7 files changed

+73
-66
lines changed

src/libs/Common/include/model.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "Matrix.h"
55
#include "geos.h"
66
#include "object.h"
7-
#include "vmodule_api.h"
87

98
class NODER;
109

@@ -53,6 +52,8 @@ class NODE
5352
const CMatrix &globm, NODER *par, const char *lmPath) = 0;
5453

5554
virtual float Trace(const CVECTOR &src, const CVECTOR &dst) = 0;
55+
56+
virtual void SubstituteGeometry(const std::string& new_model) = 0;
5657
};
5758

5859
class VDX9RENDER;

src/libs/model/src/model.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,19 @@ uint64_t MODELR::ProcessMessage(MESSAGE &message)
393393
if (root)
394394
root->SetMaxViewDist(message.Float());
395395
break;
396+
case MSG_MODEL_SUBSTITUTE_GEOMETRY_NODE: {
397+
auto &&geometry_node = message.String();
398+
auto &&new_model_name = message.String();
399+
400+
if (auto *node = FindNode(geometry_node.c_str()))
401+
{
402+
node->SubstituteGeometry(new_model_name);
403+
}
404+
else
405+
{
406+
spdlog::trace("MODELR: Cannot substitute geometry node {}", geometry_node);
407+
}
408+
}
396409
}
397410
return 1;
398411
}

src/libs/model/src/modelr.h

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
#pragma once
22

3+
#include <string>
4+
35
#include "dx9render.h"
46
#include "geometry.h"
57
#include "model.h"
68

79
class NODER : public NODE
810
{
9-
char *sys_modelName;
10-
char *sys_LightPath;
11-
char *sys_TexPath;
12-
char *sys_lmPath;
13-
bool isReleaed;
11+
std::string sys_modelName_base;
12+
std::string sys_modelName_full;
13+
std::string sys_LightPath;
14+
std::string sys_TexPath;
15+
std::string sys_lmPath;
16+
bool isReleased;
1417

1518
static long depth, node;
1619
uintptr_t idGeoGroup; // id of "geometry" string
@@ -54,11 +57,15 @@ class NODER : public NODE
5457

5558
void SetTechnique(const char *name) override;
5659
const char *GetTechnique() override;
60+
61+
// replace only this node model without touching anything else
62+
void SubstituteGeometry(const std::string &new_model) override;
5763

5864
void ReleaseGeometry();
5965
void RestoreGeometry();
6066

6167
void SetMaxViewDist(float fDist);
68+
6269
};
6370

6471
#define MODEL_ANI_MAXBUFFERS 16

src/libs/model/src/node.cpp

+34-52
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
VGEOMETRY *NODER::gs = nullptr;
88
VDX9RENDER *NODER::rs = nullptr;
9-
static long nlab = 0;
109
long NODER::depth = -1;
1110
long NODER::node;
1211
extern long clip_nps;
@@ -15,7 +14,6 @@ extern const CVECTOR *clip_c;
1514
extern float clip_r;
1615
extern GEOS::ADD_POLYGON_FUNC clip_geosap;
1716
extern ADD_POLYGON_FUNC clip_ap;
18-
char nm[256];
1917
GEOS::PLANE clip_gp[256];
2018
NODE *bestTraceNode = nullptr;
2119
GEOS *clipGeo;
@@ -43,7 +41,7 @@ bool AddPolygon(const GEOS::VERTEX *vr, long nv)
4341
//-------------------------------------------------------------------
4442
bool NODER::Clip()
4543
{
46-
if (isReleaed)
44+
if (isReleased)
4745
return false;
4846

4947
// check for bounding spheres intersection
@@ -122,7 +120,7 @@ float NODER::Update(CMatrix &mtx, CVECTOR &cnt)
122120
//----------------------------------------------------------
123121
float NODER::Trace(const CVECTOR &src, const CVECTOR &dst)
124122
{
125-
if (isReleaed)
123+
if (isReleased)
126124
return 2.0f;
127125
// check for bounding spheres intersection
128126
const auto lmn = dst - src;
@@ -174,17 +172,14 @@ NODER::NODER()
174172
nnext = 0;
175173
next = nullptr;
176174
parent = nullptr;
177-
sys_modelName = nullptr;
178-
sys_TexPath = nullptr;
179-
sys_LightPath = nullptr;
180-
sys_lmPath = nullptr;
181175

182176
max_view_dist = 0.f;
183177
}
184178

185179
bool NODER::Init(const char *lightPath, const char *pname, const char *oname, const CMatrix &m, const CMatrix &globm,
186180
NODER *par, const char *lmPath)
187181
{
182+
isReleased = false;
188183
name[0] = 0;
189184
technique[0] = 0;
190185
geoMaterialFunc = nullptr;
@@ -193,44 +188,28 @@ bool NODER::Init(const char *lightPath, const char *pname, const char *oname, co
193188
loc_mtx = m;
194189
glob_mtx.EqMultiply(loc_mtx, globm);
195190

196-
nlab++;
197-
if (oname[0] == 0)
198-
sprintf_s(nm, "%s", pname);
199-
else
200-
sprintf_s(nm, "%s_%s", pname, oname);
201-
202-
char lp[256];
203-
// sprintf_s(lp, "%s\\%s", lightPath, nm);
204-
sprintf_s(lp, "%s", lightPath);
205-
206-
auto len = strlen(nm) + 1;
207-
sys_modelName = new char[len];
208-
memcpy(sys_modelName, nm, len);
209-
210-
len = strlen(lp) + 1;
211-
sys_LightPath = new char[len];
212-
memcpy(sys_LightPath, lp, len);
191+
if (pname == nullptr)
192+
{
193+
throw std::runtime_error(fmt::format("NODER::Init: got nullptr model name"));
194+
}
195+
sys_modelName_base = pname;
213196

214-
len = strlen(lmPath) + 1;
215-
sys_lmPath = new char[len];
216-
memcpy(sys_lmPath, lmPath, len);
197+
if (oname && oname[0])
198+
sys_modelName_full = fmt::format("{}_{}", sys_modelName_base, oname);
199+
else
200+
sys_modelName_full = sys_modelName_base;
201+
202+
if (lightPath)
203+
sys_LightPath = lightPath;
217204

218-
const auto *const tPath = gs->GetTexturePath();
219-
len = strlen(tPath) + 1;
220-
sys_TexPath = new char[len];
221-
memcpy(sys_TexPath, tPath, len);
205+
if (lmPath)
206+
sys_lmPath = lmPath;
222207

223-
isReleaed = false;
208+
sys_TexPath = gs->GetTexturePath();
224209

225-
geo = gs->CreateGeometry(nm, lp, 0, lmPath);
210+
geo = gs->CreateGeometry(sys_modelName_full.c_str(), sys_LightPath.c_str(), 0, lmPath);
226211
if (!geo)
227212
{
228-
delete sys_modelName;
229-
sys_modelName = nullptr;
230-
delete sys_LightPath;
231-
sys_LightPath = nullptr;
232-
delete sys_lmPath;
233-
sys_lmPath = nullptr;
234213
return false;
235214
}
236215

@@ -305,19 +284,15 @@ NODER::~NODER()
305284
delete next[l];
306285
if (nnext > 0)
307286
free(next);
308-
delete sys_modelName;
309-
delete sys_LightPath;
310-
delete sys_lmPath;
311-
delete sys_TexPath;
312287
}
313288

314289
void NODER::ReleaseGeometry()
315290
{
316-
if (isReleaed)
291+
if (isReleased)
317292
return;
318293
delete geo;
319294
geo = nullptr;
320-
isReleaed = true;
295+
isReleased = true;
321296
for (long i = 0; i < nnext; i++)
322297
{
323298
if (!next[i])
@@ -328,21 +303,21 @@ void NODER::ReleaseGeometry()
328303

329304
void NODER::RestoreGeometry()
330305
{
331-
if (!isReleaed)
306+
if (!isReleased)
332307
return;
333308

334309
const auto *const tPath = gs->GetTexturePath();
335310
const auto len = strlen(tPath) + 1;
336311
auto *const ttPath = new char[len];
337312
memcpy(ttPath, tPath, len);
338-
gs->SetTexturePath(sys_TexPath);
339-
geo = gs->CreateGeometry(sys_modelName, sys_LightPath, 0, sys_lmPath);
313+
gs->SetTexturePath(sys_TexPath.c_str());
314+
geo = gs->CreateGeometry(sys_modelName_full.c_str(), sys_LightPath.c_str(), 0, sys_lmPath.c_str());
340315
gs->SetTexturePath(ttPath);
341316
delete[] ttPath;
342317
if (!geo)
343-
throw std::runtime_error("No geometry");
318+
throw std::runtime_error(fmt::format("Cannot restore geometry {}", sys_modelName_full));
344319

345-
isReleaed = false;
320+
isReleased = false;
346321
for (long i = 0; i < nnext; i++)
347322
{
348323
if (!next[i])
@@ -359,7 +334,7 @@ GEOS::PLANE TViewPlane[4];
359334

360335
void NODER::Draw()
361336
{
362-
if (isReleaed)
337+
if (isReleased)
363338
return;
364339

365340
const auto cnt = glob_mtx * center;
@@ -617,3 +592,10 @@ void NODER::SetMaxViewDist(float fDist)
617592
if (next[n])
618593
static_cast<NODER *>(next[n])->SetMaxViewDist(fDist);
619594
}
595+
596+
void NODER::SubstituteGeometry(const std::string &new_model)
597+
{
598+
sys_modelName_full = fmt::format("{}_{}", sys_modelName_base, new_model);
599+
ReleaseGeometry();
600+
RestoreGeometry();
601+
}

src/libs/rigging/src/Flag.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -151,27 +151,22 @@ uint64_t FLAG::ProcessMessage(MESSAGE &message)
151151
const auto eidModel = message.EntityID();
152152
const auto nNation = message.Long();
153153

154-
MODEL *host_mdl;
155-
host_mdl = static_cast<MODEL *>(EntityManager::GetEntityPointer(eidModel));
154+
MODEL *host_mdl = static_cast<MODEL *>(EntityManager::GetEntityPointer(eidModel));
156155
if (host_mdl == nullptr)
157156
{
158157
core.Trace("Missing INIT message to FLAG: bad MODEL");
158+
return 0;
159159
}
160160

161161
if (groupQuantity == 0)
162162
{
163163
gdata = new GROUPDATA[1];
164-
if (gdata == nullptr)
165-
throw std::runtime_error("Not memory allocation");
166-
167164
groupQuantity = 1;
168165
}
169166
else
170167
{
171168
auto *const oldgdata = gdata;
172169
gdata = new GROUPDATA[groupQuantity + 1];
173-
if (gdata == nullptr)
174-
throw std::runtime_error("Not memory allocation");
175170
memcpy(gdata, oldgdata, sizeof(GROUPDATA) * groupQuantity);
176171
delete oldgdata;
177172
groupQuantity++;

src/libs/shared_headers/include/shared/messages.h

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#define MSG_MODEL_SET_FOG 20511
2323
#define MSG_MODEL_SET_MAX_VIEW_DIST 20512
2424

25+
#define MSG_MODEL_SUBSTITUTE_GEOMETRY_NODE 20600 // "ss", geometry, new model name
26+
2527
//============================================================================================
2628
// blade messages
2729
//============================================================================================

src/libs/ship/src/ship.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -1301,8 +1301,15 @@ uint64_t SHIP::ProcessMessage(MESSAGE &message)
13011301
core.Send_Message(GetModelEID(), "ls", MSG_MODEL_SET_TECHNIQUE, sTech.c_str());
13021302
// MODEL * pModel = GetModel();
13031303
// NODE* pNode = pModel->GetNode(0);
1304+
break;
1305+
}
1306+
case MSG_MODEL_SUBSTITUTE_GEOMETRY_NODE: {
1307+
auto &&geometry_node = message.String();
1308+
auto &&new_model_name = message.String();
1309+
core.Send_Message(GetModelEID(), "lss", MSG_MODEL_SUBSTITUTE_GEOMETRY_NODE, geometry_node.c_str(),
1310+
new_model_name.c_str());
1311+
break;
13041312
}
1305-
break;
13061313
}
13071314
return 0;
13081315
}

0 commit comments

Comments
 (0)