Skip to content

Commit a2b1b2d

Browse files
xPartition: 100% match (#672)
1 parent b60a121 commit a2b1b2d

File tree

5 files changed

+171
-14
lines changed

5 files changed

+171
-14
lines changed

configure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def MatchingFor(*versions):
448448
Object(NonMatching, "SB/Core/x/xParCmd.cpp"),
449449
Object(Matching, "SB/Core/x/xParGroup.cpp"),
450450
Object(Matching, "SB/Core/x/xParMgr.cpp"),
451-
Object(NonMatching, "SB/Core/x/xPartition.cpp"),
451+
Object(Matching, "SB/Core/x/xPartition.cpp", extra_cflags=["-sym on"]),
452452
Object(NonMatching, "SB/Core/x/xpkrsvc.cpp"),
453453
Object(NonMatching, "SB/Core/x/xQuickCull.cpp"),
454454
Object(Matching, "SB/Core/x/xsavegame.cpp"),

src/SB/Core/gc/iEnv.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ void iEnvLightingBasics(iEnv*, xEnvAsset*);
2727
void iEnvRender(iEnv* env);
2828
void iEnvEndRenderFX(iEnv*);
2929

30+
inline RwBBox* iEnvGetBBox(iEnv* r3)
31+
{
32+
return &r3->world->boundingBox;
33+
}
34+
3035
#endif

src/SB/Core/x/xEnv.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ void xEnvFree(xEnv* env);
3636
void xEnvSetup(xEnv* env);
3737
void xEnvRender(xEnv* env);
3838

39+
inline RwBBox* xEnvGetBBox(xEnv* r3)
40+
{
41+
return iEnvGetBBox(r3->geom);
42+
}
43+
3944
#endif

src/SB/Core/x/xPartition.cpp

Lines changed: 154 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ void xPartitionReset()
66
{
77
}
88

9-
_tagPartLink* PartitionGetFreeLink()
9+
static _tagPartLink* PartitionGetFreeLink()
1010
{
1111
return (_tagPartLink*)xMemAllocSize(sizeof(_tagPartLink));
1212
}
1313

14-
void PartitionSpaceReset(_tagPartSpace* space)
14+
static void PartitionSpaceReset(_tagPartSpace* space)
1515
{
1616
memset(space, 0, sizeof(_tagPartSpace));
1717
}
1818

19-
void PartitionSpaceInsert(_tagPartSpace* space, void* data)
19+
static void PartitionSpaceInsert(_tagPartSpace* space, void* data)
2020
{
2121
space->total++;
2222
_tagPartLink* head = &space->head;
@@ -31,10 +31,159 @@ void PartitionSpaceInsert(_tagPartSpace* space, void* data)
3131
tmp->next->next = NULL;
3232
}
3333

34-
// Need to figure out the correct order.
3534
S32 xPartitionGetTrueIdx(_tagPartition* part, S32 x_spaces, S32 y_spaces, S32 z_spaces)
3635
{
37-
return part->total_x * z_spaces + part->total_x * y_spaces * part->total_z + x_spaces;
36+
S32 idx = (y_spaces * (part->total_x * part->total_z));
37+
idx += part->total_x * z_spaces;
38+
idx += x_spaces;
39+
return idx;
40+
}
41+
42+
void xPartitionVolume(_tagPartition* part, xVolume* volume, S32 x_spaces, S32 y_spaces,
43+
S32 z_spaces)
44+
{
45+
memset(part, 0, sizeof(_tagPartition));
46+
xBound* xb = volume->GetBound();
47+
xBox* bb = &xb->box.box;
48+
part->min = bb->lower;
49+
part->max = bb->upper;
50+
51+
F32 dx = part->max.x - part->min.x;
52+
F32 dy = part->max.y - part->min.y;
53+
F32 dz = part->max.z - part->min.z;
54+
55+
part->total_x = x_spaces;
56+
part->total_y = y_spaces;
57+
part->total_z = z_spaces;
58+
59+
part->space_dim.x = dx / part->total_x;
60+
part->space_dim.y = dy / part->total_y;
61+
part->space_dim.z = dz / part->total_z;
62+
63+
part->space = (_tagPartSpace*)xMemAlloc(
64+
gActiveHeap, sizeof(_tagPartSpace) * part->total_x * part->total_y * part->total_z, 0);
65+
66+
for (S32 z = 0; z < z_spaces; z++)
67+
{
68+
for (S32 y = 0; y < y_spaces; y++)
69+
{
70+
for (S32 x = 0; x < x_spaces; x++)
71+
{
72+
S32 idx = xPartitionGetTrueIdx(part, x, y, z);
73+
PartitionSpaceReset(&part->space[idx]);
74+
}
75+
}
76+
}
77+
78+
PartitionSpaceReset(&part->global);
79+
}
80+
81+
void xPartitionWorld(_tagPartition* part, xEnv* env, S32 x_spaces, S32 y_spaces, S32 z_spaces)
82+
{
83+
memset(part, 0, sizeof(_tagPartition));
84+
xBox* bb = (xBox*)xEnvGetBBox(env);
85+
part->min = bb->lower;
86+
part->max = bb->upper;
87+
88+
F32 dx = part->max.x - part->min.x;
89+
F32 dy = part->max.y - part->min.y;
90+
F32 dz = part->max.z - part->min.z;
91+
92+
part->total_x = x_spaces;
93+
part->total_y = y_spaces;
94+
part->total_z = z_spaces;
95+
96+
part->space_dim.x = dx / part->total_x;
97+
part->space_dim.y = dy / part->total_y;
98+
part->space_dim.z = dz / part->total_z;
99+
100+
part->space = (_tagPartSpace*)xMemAlloc(
101+
gActiveHeap, sizeof(_tagPartSpace) * part->total_x * part->total_y * part->total_z, 0);
102+
103+
for (S32 z = 0; z < z_spaces; z++)
104+
{
105+
for (S32 y = 0; y < y_spaces; y++)
106+
{
107+
for (S32 x = 0; x < x_spaces; x++)
108+
{
109+
S32 idx = xPartitionGetTrueIdx(part, x, y, z);
110+
PartitionSpaceReset(&part->space[idx]);
111+
}
112+
}
113+
}
114+
115+
PartitionSpaceReset(&part->global);
116+
}
117+
118+
S32 xPartitionInsert(_tagPartition* part, void* insert_data, xVec3* insert_pos)
119+
{
120+
if (insert_pos->x < part->min.x || insert_pos->x > part->max.x || insert_pos->y < part->min.y ||
121+
insert_pos->y > part->max.y || insert_pos->z < part->min.z || insert_pos->z > part->max.z)
122+
{
123+
PartitionSpaceInsert(&part->global, insert_data);
124+
return -1;
125+
}
126+
127+
S32 idx = xPartitionGetTrueIdx(part, (insert_pos->x - part->min.x) / part->space_dim.x,
128+
(insert_pos->y - part->min.y) / part->space_dim.y,
129+
(insert_pos->z - part->min.z) / part->space_dim.z);
130+
PartitionSpaceInsert(&part->space[idx], insert_data);
131+
132+
return idx;
133+
}
134+
135+
S32 xPartitionUpdate(_tagPartition* part, void* data, S32 old_idx, xVec3* current_pos)
136+
{
137+
S32 cur_idx;
138+
139+
if (current_pos->x < part->min.x || current_pos->x > part->max.x ||
140+
current_pos->y < part->min.y || current_pos->y > part->max.y ||
141+
current_pos->z < part->min.z || current_pos->z > part->max.z)
142+
{
143+
cur_idx = -1;
144+
}
145+
else
146+
{
147+
cur_idx = xPartitionGetTrueIdx(part, (current_pos->x - part->min.x) / part->space_dim.x,
148+
(current_pos->y - part->min.y) / part->space_dim.y,
149+
(current_pos->z - part->min.z) / part->space_dim.z);
150+
}
151+
152+
if (old_idx == cur_idx)
153+
return cur_idx;
154+
155+
_tagPartSpace* src = old_idx == -1 ? &part->global : &part->space[old_idx];
156+
xPartitionSpaceMove(src, cur_idx == -1 ? &part->global : &part->space[cur_idx], (U32)data);
157+
return cur_idx;
158+
}
159+
160+
void xPartitionSpaceMove(_tagPartSpace* src, _tagPartSpace* dest, U32 data)
161+
{
162+
_tagPartLink* dest_lnk = &dest->head;
163+
_tagPartLink* src_lnk;
164+
_tagPartLink* src_pre;
165+
166+
while (dest_lnk->next != NULL)
167+
{
168+
dest_lnk = dest_lnk->next;
169+
}
170+
171+
src_lnk = &src->head;
172+
src_pre = &src->head;
173+
174+
while (src_lnk->data != (void*)data)
175+
{
176+
src_pre = src_lnk;
177+
src_lnk = src_lnk->next;
178+
}
179+
180+
src_pre->next = src_lnk->next;
181+
182+
dest_lnk->next = src_lnk;
183+
dest_lnk->next->next = NULL;
184+
185+
src->total--;
186+
dest->total++;
38187
}
39188

40189
void xPartitionDump(_tagPartition*, char*)

src/SB/Core/x/xPartition.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define XPARTITION_H
33

44
#include <types.h>
5-
#include "xMath3.h"
65
#include "xEnv.h"
76
#include "xVolume.h"
87

@@ -31,16 +30,15 @@ struct _tagPartition
3130
};
3231

3332
void xPartitionReset();
34-
_tagPartLink* PartitionGetFreeLink();
35-
void PartitionSpaceReset(_tagPartSpace* space);
36-
void PartitionSpaceInsert(_tagPartSpace* space, void* data);
3733
S32 xPartitionGetTrueIdx(_tagPartition* part, S32 x_spaces, S32 y_spaces, S32 z_spaces);
38-
void xPartitionWorld(_tagPartition* part, xEnv* env, S32 x_spaces, S32 y_spaces,
39-
S32 z_spaces);
4034
void xPartitionVolume(_tagPartition* part, xVolume* volume, S32 x_spaces, S32 y_spaces,
4135
S32 z_spaces);
42-
void xPartitionDump(_tagPartition* part, char* string);
43-
S32 xPartitionUpdate(_tagPartition* part, void* data, S32 old_idx, xVec3* current_pos);
36+
void xPartitionWorld(_tagPartition* part, xEnv* env, S32 x_spaces, S32 y_spaces,
37+
S32 z_spaces);
4438
S32 xPartitionInsert(_tagPartition* part, void* insert_data, xVec3* insert_pos);
39+
S32 xPartitionUpdate(_tagPartition* part, void* data, S32 old_idx, xVec3* current_pos);
40+
void xPartitionSpaceMove(_tagPartSpace* src, _tagPartSpace* dest, U32 data);
41+
void xPartitionDump(_tagPartition*, char*);
42+
4543

4644
#endif

0 commit comments

Comments
 (0)