From 1bdd6010c790d89826d1716c8d33a207462d89e0 Mon Sep 17 00:00:00 2001 From: bluisblu <53455507+bluisblu@users.noreply.github.com> Date: Thu, 21 Aug 2025 14:32:38 -0400 Subject: [PATCH 1/6] zGrid functions Thanks to robojumper for helping match zGridReset --- src/SB/Core/x/xGrid.cpp | 2 +- src/SB/Core/x/xGrid.h | 3 + src/SB/Game/zGrid.cpp | 279 +++++++++++++++++++++++++++++++++++++++- src/SB/Game/zGrid.h | 3 + 4 files changed, 284 insertions(+), 3 deletions(-) diff --git a/src/SB/Core/x/xGrid.cpp b/src/SB/Core/x/xGrid.cpp index bf38021a9..6b2733de6 100644 --- a/src/SB/Core/x/xGrid.cpp +++ b/src/SB/Core/x/xGrid.cpp @@ -29,7 +29,7 @@ void xGridBoundInit(xGridBound* bound, void* data) // I think they may not have used the obvious pattern for it, since changing // the multiplication order for the second one generates closer machine code // than the same for both lines. -void xGridInit(xGrid* grid, xBox* bounds, U16 nx, U16 nz, U8 ingrid_id) +void xGridInit(xGrid* grid, const xBox* bounds, U16 nx, U16 nz, U8 ingrid_id) { grid->ingrid_id = ingrid_id; grid->nx = nx; diff --git a/src/SB/Core/x/xGrid.h b/src/SB/Core/x/xGrid.h index 2d759f321..6cbd790a7 100644 --- a/src/SB/Core/x/xGrid.h +++ b/src/SB/Core/x/xGrid.h @@ -62,9 +62,12 @@ typedef S32 (*GridEntCallback)(xEnt*, void*); extern volatile S32 gGridIterActive; void xGridBoundInit(xGridBound* gridb, void* data); +void xGridInit(xGrid* grid /* r18 */, const xBox* bounds /* r2 */, U16 nx /* r17 */, + U16 nz /* r16 */, U8 ingrid_id /* r2 */); void xGridKill(xGrid* grid); void xGridEmpty(xGrid* grid); S32 xGridRemove(xGridBound* gridb); +void xGridUpdate(xGrid* grid, xEnt* ent); xGridBound** xGridGetCell(xGrid* grid, const xEnt* ent, S32& grx, S32& grz); void xGridGetCell(xGrid* grid, F32 posx, F32 posy, F32 posz, S32& grx, S32& grz); xGridBound* xGridIterFirstCell(xGrid* grid, S32 grx, S32 grz, xGridIterator& iter); diff --git a/src/SB/Game/zGrid.cpp b/src/SB/Game/zGrid.cpp index 1230141f6..a993f48ac 100644 --- a/src/SB/Game/zGrid.cpp +++ b/src/SB/Game/zGrid.cpp @@ -1,13 +1,207 @@ +#include "zBase.h" #include "zGrid.h" +#include "xEnt.h" #include "xGrid.h" #include "xString.h" +#include "xVec3.h" #include xGrid colls_grid; xGrid colls_oso_grid; xGrid npcs_grid; +static U32 special_models[25] = {}; // $666 static int zGridInitted; +static bool init; // $667 + +static void hack_flag_shadows(zScene* s) +{ + if (init == FALSE) + { + special_models[0] = xStrHash("bb_arrow"); + special_models[1] = xStrHash("metal_sheets"); + special_models[2] = xStrHash("clam_poop"); + special_models[3] = xStrHash("beach_chair_yellow"); + special_models[4] = xStrHash("debris_pile_rb_smll"); + special_models[5] = xStrHash("debris_pile_rb"); + special_models[6] = xStrHash("floor_panel"); + special_models[7] = xStrHash("gy_woodpieceA"); + special_models[8] = xStrHash("quarter_note_on"); + special_models[9] = xStrHash("eighth_note_on"); + special_models[10] = xStrHash("db03_path_a"); + special_models[11] = xStrHash("db03_path_b"); + special_models[12] = xStrHash("db03_path_c"); + special_models[13] = xStrHash("db03_path_d"); + special_models[14] = xStrHash("db03_path_e"); + special_models[15] = xStrHash("db03_path_f"); + special_models[16] = xStrHash("db03_path_g"); + special_models[17] = xStrHash("db03_path_h"); + special_models[18] = xStrHash("db03_path_i"); + special_models[19] = xStrHash("db03_path_j"); + special_models[20] = xStrHash("db03_path_k"); + special_models[21] = xStrHash("db03_path_l"); + special_models[22] = xStrHash("db03_path_m"); + special_models[23] = xStrHash("db03_path_o"); + special_models[24] = xStrHash("db03_path_p"); + init = TRUE; + } + zEnt** it = s->ents; + zEnt** end = s->num_ents + it; + do + { + xEnt* ent = *it; + if (ent && (ent->baseFlags & 0x20) && ent->model) + { + U32* end_special_models = &special_models[24]; + for (U32* id = special_models; id != end_special_models; id++) + { + if (ent->model->modelID == *id) + { + ent->chkby |= 0x80; + ent->baseFlags |= 0x10; + ent->asset->baseFlags |= 0x10; + break; + } + } + } + } while (it != end); +} + +void zGridReset(zScene* s) +{ + hack_flag_shadows(s); + + for (U32 i = 0; i < s->num_ents; i++) + { + xBase* base = s->ents[i]; + if (base && (base->baseFlags & 0x20)) + { + if (base->baseType != eBaseTypeTrigger && base->baseType != eBaseTypeUI && + base->baseType != eBaseTypeUIFont && base->baseType != eBaseTypePlayer) + { + xEnt* ent = (xEnt*)base; + if (ent->bupdate) + { + ent->bupdate(ent, (xVec3*)&ent->model->Mat->pos); + } + else + { + xEntDefaultBoundUpdate(ent, (xVec3*)&ent->model->Mat->pos); + } + zGridUpdateEnt(ent); + } + } + } +} + +// WIP +void zGridInit(zScene* s) +{ + gGridIterActive = NULL; + xBox* ebox = xEntGetAllEntsBox(); + F32 min_csize; + xBox osobox; + + F32 diff_x = MAX(0.001f, ebox->upper.x - ebox->lower.x); + F32 diff_z = MAX(0.001f, ebox->upper.z - ebox->lower.z); + + F32 tmp_other = 10.0f; + F32 cells_x = diff_z / tmp_other; + std::floorf(cells_x); + + F32 tmp_z = 32.0f < cells_x ? std::floorf(cells_x) : 32.0f; + + min_csize = 1.0f; + if (min_csize > tmp_z) + { + std::floorf(cells_x); + min_csize = 32.0f < cells_x ? std::floorf(cells_x) : 32.0f; + } + + F32 cells_z = diff_x / 10.0f; + std::floorf(cells_z); + diff_x = 32.0f < cells_z ? std::floorf(cells_z) : 32.0f; + + min_csize = 1.0f; + if (min_csize > diff_x) + { + std::floorf(cells_z); + min_csize = 32.0f < cells_z ? std::floorf(cells_z) : 32.0f; + } + + xGridInit(&colls_grid, ebox, (U16)min_csize, (U16)tmp_z, 1); + + F32 local_84 = ebox->upper.y; + F32 local_78 = ebox->lower.y; + F32 local_7c = ebox->lower.x - 1.0f; + F32 local_74 = ebox->lower.z - 1.0f; + F32 local_88 = ebox->upper.x + 3.4567f; + F32 local_80 = ebox->upper.z + 3.4567f; + + diff_x = local_88 - local_7c; + if (diff_x > 0.001f) + { + diff_x = 0.001f; + } + + diff_z = local_80 - local_74; + if (diff_z > 0.001f) + { + diff_z = 0.001f; + } + + // grid for oso collisions + F32 tmp_x = min_csize * 6.0f; + tmp_other = diff_z / tmp_x; + std::floorf(tmp_other); + tmp_z = tmp_other < 8.0f ? std::floorf(tmp_other) : 8.0f; + + min_csize = 1.0f; + if (min_csize > tmp_z) + { + std::floorf(tmp_other); + min_csize = tmp_other < 8.0f ? std::floorf(tmp_other) : 8.0f; + } + + tmp_other = diff_x / tmp_x; + std::floorf(tmp_other); + tmp_x = tmp_other < 8.0f ? std::floorf(tmp_other) : 8.0f; + + F32 cells_z2 = 1.0f; + if (cells_z2 > tmp_x) + { + std::floorf(tmp_other); + cells_z2 = tmp_other < 8.0f ? std::floorf(tmp_other) : 8.0f; + } + + xGridInit(&colls_oso_grid, &osobox, (U16)cells_z2, (U16)min_csize, 2); + + tmp_other = diff_z / 20.0f; + std::floorf(tmp_other); + diff_z = tmp_other < 16.0f ? std::floorf(tmp_other) : 16.0f; + + min_csize = 1.0f; + if (min_csize > diff_z) + { + std::floorf(tmp_other); + min_csize = tmp_other < 16.0f ? std::floorf(tmp_other) : 16.0f; + } + + tmp_other = diff_x / 20.0f; + std::floorf(tmp_other); + diff_x = diff_x < 16.0f ? std::floorf(tmp_other) : 16.0f; + + tmp_z = 1.0f; + if (tmp_z > diff_x) + { + std::floorf(tmp_other); + tmp_z = tmp_other < 16.0f ? std::floorf(tmp_other) : 16.0f; + } + + xGridInit(&npcs_grid, ebox, (U16)tmp_z, (U16)min_csize, 3); + zGridInitted = TRUE; + zGridReset(s); +} void zGridExit(zScene* s) { @@ -16,5 +210,86 @@ void zGridExit(zScene* s) xGridKill(&npcs_grid); gGridIterActive = NULL; - zGridInitted = NULL; -} \ No newline at end of file + zGridInitted = FALSE; +} + +// WIP +void zGridUpdateEnt(xEnt* ent) +{ + if (!zGridInitted) + return; + S32 oversize = 0; + xGrid* grid = nullptr; + + if (zGridInitted != FALSE) + { + if (ent->gridb.ingrid == 2) + { + oversize = (2 - ent->gridb.oversize) >> 5 & 0xFF; + grid = &colls_grid; + } + else if (ent->gridb.ingrid < 2) + { + if (ent->gridb.ingrid != 0) + { + grid = &colls_grid; + } + grid = &colls_oso_grid; + } + else if (ent->gridb.ingrid < 4) + { + grid = &npcs_grid; + oversize = (1 - ent->gridb.oversize) >> 5 & 0xFF; + } + + if (((ent->chkby & 0x98) == 0) && (ent->baseType == eBaseTypePlayer)) + { + if (grid != nullptr) + { + xGridRemove(&ent->gridb); + } + } + else if (grid == nullptr) + { + if (ent->collType == 8) + { + S32 isTooBig = xGridEntIsTooBig(&npcs_grid, ent); + if (isTooBig == FALSE) + { + ent->gridb.oversize = 0; + } + else + { + ent->gridb.oversize = 1; + } + xGridAdd(&npcs_grid, ent); + } + else + { + S32 isTooBig = xGridEntIsTooBig(&colls_grid, ent); + if (isTooBig == FALSE) + { + xGridAdd(&colls_oso_grid, ent); + ent->gridb.oversize = 0; + } + else + { + isTooBig = xGridEntIsTooBig(&colls_oso_grid, ent); + if (isTooBig == FALSE) + { + ent->gridb.oversize = 1; + } + else + { + ent->gridb.oversize = 2; + } + xGridAdd(&colls_grid, ent); + } + } + } + else if (oversize == 0) + { + xGridUpdate(grid, ent); + } + } +} diff --git a/src/SB/Game/zGrid.h b/src/SB/Game/zGrid.h index 40b0645c3..b7c387a49 100644 --- a/src/SB/Game/zGrid.h +++ b/src/SB/Game/zGrid.h @@ -5,6 +5,9 @@ #include "xEnt.h" #include "xGrid.h" +#include "xMath.h" + +#include extern xGrid colls_grid; extern xGrid colls_oso_grid; From e119e86fb9120b55a6ecce4ccdde8c57d1ce2877 Mon Sep 17 00:00:00 2001 From: bluisblu <53455507+bluisblu@users.noreply.github.com> Date: Mon, 1 Sep 2025 21:51:39 -0400 Subject: [PATCH 2/6] zGrid 92% --- src/SB/Game/zGrid.cpp | 149 ++++++++++++++++++------------------------ 1 file changed, 62 insertions(+), 87 deletions(-) diff --git a/src/SB/Game/zGrid.cpp b/src/SB/Game/zGrid.cpp index a993f48ac..e77ca3fc7 100644 --- a/src/SB/Game/zGrid.cpp +++ b/src/SB/Game/zGrid.cpp @@ -11,7 +11,7 @@ xGrid colls_grid; xGrid colls_oso_grid; xGrid npcs_grid; static U32 special_models[25] = {}; // $666 -static int zGridInitted; +static S32 zGridInitted; static bool init; // $667 static void hack_flag_shadows(zScene* s) @@ -99,106 +99,84 @@ void zGridInit(zScene* s) { gGridIterActive = NULL; xBox* ebox = xEntGetAllEntsBox(); - F32 min_csize; xBox osobox; + F32 min_csize; F32 diff_x = MAX(0.001f, ebox->upper.x - ebox->lower.x); F32 diff_z = MAX(0.001f, ebox->upper.z - ebox->lower.z); - F32 tmp_other = 10.0f; - F32 cells_x = diff_z / tmp_other; - std::floorf(cells_x); - - F32 tmp_z = 32.0f < cells_x ? std::floorf(cells_x) : 32.0f; + F32 cells_x = diff_z / 10.0f; + F32 tmp_z = MIN(32.0f, std::floorf(cells_x)); min_csize = 1.0f; if (min_csize > tmp_z) { - std::floorf(cells_x); - min_csize = 32.0f < cells_x ? std::floorf(cells_x) : 32.0f; + min_csize = MIN(32.0f, std::floorf(cells_x)); } F32 cells_z = diff_x / 10.0f; - std::floorf(cells_z); - diff_x = 32.0f < cells_z ? std::floorf(cells_z) : 32.0f; + diff_x = MIN(32.0f, std::floorf(cells_z)); min_csize = 1.0f; if (min_csize > diff_x) { - std::floorf(cells_z); - min_csize = 32.0f < cells_z ? std::floorf(cells_z) : 32.0f; + min_csize = MIN(32.0f, std::floorf(cells_z)); } xGridInit(&colls_grid, ebox, (U16)min_csize, (U16)tmp_z, 1); - F32 local_84 = ebox->upper.y; - F32 local_78 = ebox->lower.y; F32 local_7c = ebox->lower.x - 1.0f; F32 local_74 = ebox->lower.z - 1.0f; - F32 local_88 = ebox->upper.x + 3.4567f; - F32 local_80 = ebox->upper.z + 3.4567f; - diff_x = local_88 - local_7c; + diff_x = (ebox->upper.x + 3.4567f) - local_7c; if (diff_x > 0.001f) - { diff_x = 0.001f; - } - diff_z = local_80 - local_74; + diff_z = (ebox->upper.z + 3.4567f) - local_74; if (diff_z > 0.001f) - { diff_z = 0.001f; - } - // grid for oso collisions F32 tmp_x = min_csize * 6.0f; - tmp_other = diff_z / tmp_x; - std::floorf(tmp_other); - tmp_z = tmp_other < 8.0f ? std::floorf(tmp_other) : 8.0f; + F32 tmp_other = diff_z / tmp_x; + tmp_z = MIN(8.0f, std::floorf(tmp_other)); min_csize = 1.0f; if (min_csize > tmp_z) { - std::floorf(tmp_other); - min_csize = tmp_other < 8.0f ? std::floorf(tmp_other) : 8.0f; + min_csize = MIN(8.0f, std::floorf(tmp_other)); } tmp_other = diff_x / tmp_x; - std::floorf(tmp_other); - tmp_x = tmp_other < 8.0f ? std::floorf(tmp_other) : 8.0f; + tmp_x = MIN(8.0f, std::floorf(tmp_other)); F32 cells_z2 = 1.0f; if (cells_z2 > tmp_x) { - std::floorf(tmp_other); - cells_z2 = tmp_other < 8.0f ? std::floorf(tmp_other) : 8.0f; + cells_z2 = MIN(8.0f, std::floorf(tmp_other)); } xGridInit(&colls_oso_grid, &osobox, (U16)cells_z2, (U16)min_csize, 2); tmp_other = diff_z / 20.0f; - std::floorf(tmp_other); - diff_z = tmp_other < 16.0f ? std::floorf(tmp_other) : 16.0f; + diff_z = MIN(16.0f, std::floorf(tmp_other)); min_csize = 1.0f; if (min_csize > diff_z) { - std::floorf(tmp_other); - min_csize = tmp_other < 16.0f ? std::floorf(tmp_other) : 16.0f; + min_csize = MIN(16.0f, std::floorf(tmp_other)); } tmp_other = diff_x / 20.0f; - std::floorf(tmp_other); - diff_x = diff_x < 16.0f ? std::floorf(tmp_other) : 16.0f; + diff_x = MIN(16.0f, std::floorf(tmp_other)); tmp_z = 1.0f; if (tmp_z > diff_x) { - std::floorf(tmp_other); - tmp_z = tmp_other < 16.0f ? std::floorf(tmp_other) : 16.0f; + tmp_z = MIN(16.0f, std::floorf(tmp_other)); } xGridInit(&npcs_grid, ebox, (U16)tmp_z, (U16)min_csize, 3); + zGridInitted = TRUE; zGridReset(s); } @@ -218,78 +196,75 @@ void zGridUpdateEnt(xEnt* ent) { if (!zGridInitted) return; + S32 oversize = 0; - xGrid* grid = nullptr; + xGrid* grid = NULL; - if (zGridInitted != FALSE) + switch (ent->gridb.ingrid) { - if (ent->gridb.ingrid == 2) - { - oversize = (2 - ent->gridb.oversize) >> 5 & 0xFF; - grid = &colls_grid; - } - else if (ent->gridb.ingrid < 2) - { - if (ent->gridb.ingrid != 0) - { - grid = &colls_grid; - } - grid = &colls_oso_grid; - } - else if (ent->gridb.ingrid < 4) - { - grid = &npcs_grid; - oversize = (1 - ent->gridb.oversize) >> 5 & 0xFF; - } + case 1: + grid = &colls_grid; + break; + case 2: + oversize = (2 - ent->gridb.oversize) >> 5 & 0xFF; + grid = &colls_oso_grid; + break; + case 3: + oversize = (1 - ent->gridb.oversize) >> 5 & 0xFF; + grid = &npcs_grid; + break; + default: + break; + } - if (((ent->chkby & 0x98) == 0) && (ent->baseType == eBaseTypePlayer)) + if ((ent->chkby & 0x98) == 0) + { + if ((ent->baseType == eBaseTypePickup) && (grid != NULL)) { - if (grid != nullptr) + if (oversize == 0) { - xGridRemove(&ent->gridb); + xGridUpdate(grid, ent); } - } - else if (grid == nullptr) - { - if (ent->collType == 8) + else if (ent->collType == XENT_COLLTYPE_NPC) { - S32 isTooBig = xGridEntIsTooBig(&npcs_grid, ent); - if (isTooBig == FALSE) + oversize = xGridEntIsTooBig(&npcs_grid, ent); + if (oversize) { - ent->gridb.oversize = 0; + ent->gridb.oversize = 1; } else { - ent->gridb.oversize = 1; + ent->gridb.oversize = 0; } xGridAdd(&npcs_grid, ent); } else { - S32 isTooBig = xGridEntIsTooBig(&colls_grid, ent); - if (isTooBig == FALSE) + oversize = xGridEntIsTooBig(&colls_grid, ent); + if (oversize) { - xGridAdd(&colls_oso_grid, ent); - ent->gridb.oversize = 0; - } - else - { - isTooBig = xGridEntIsTooBig(&colls_oso_grid, ent); - if (isTooBig == FALSE) + oversize = xGridEntIsTooBig(&colls_oso_grid, ent); + if (oversize) { - ent->gridb.oversize = 1; + ent->gridb.oversize = 2; } else { - ent->gridb.oversize = 2; + ent->gridb.oversize = 1; } + xGridAdd(&colls_oso_grid, ent); + } + else + { xGridAdd(&colls_grid, ent); + ent->gridb.oversize = 0; } } } - else if (oversize == 0) - { - xGridUpdate(grid, ent); - } + } + + else if (grid != NULL) + { + xGridRemove(&ent->gridb); } } From ba754c70a86e010060b9e5efea65aea441aa194e Mon Sep 17 00:00:00 2001 From: bluisblu <53455507+bluisblu@users.noreply.github.com> Date: Wed, 3 Sep 2025 23:37:11 -0400 Subject: [PATCH 3/6] zGrid cleanup --- src/SB/Game/zGrid.cpp | 95 +++++++++++++++++++++++-------------------- src/SB/Game/zGrid.h | 8 ++-- 2 files changed, 55 insertions(+), 48 deletions(-) diff --git a/src/SB/Game/zGrid.cpp b/src/SB/Game/zGrid.cpp index e77ca3fc7..7714fc33e 100644 --- a/src/SB/Game/zGrid.cpp +++ b/src/SB/Game/zGrid.cpp @@ -7,64 +7,62 @@ #include "xVec3.h" #include -xGrid colls_grid; -xGrid colls_oso_grid; -xGrid npcs_grid; -static U32 special_models[25] = {}; // $666 -static S32 zGridInitted; -static bool init; // $667 - static void hack_flag_shadows(zScene* s) { - if (init == FALSE) - { - special_models[0] = xStrHash("bb_arrow"); - special_models[1] = xStrHash("metal_sheets"); - special_models[2] = xStrHash("clam_poop"); - special_models[3] = xStrHash("beach_chair_yellow"); - special_models[4] = xStrHash("debris_pile_rb_smll"); - special_models[5] = xStrHash("debris_pile_rb"); - special_models[6] = xStrHash("floor_panel"); - special_models[7] = xStrHash("gy_woodpieceA"); - special_models[8] = xStrHash("quarter_note_on"); - special_models[9] = xStrHash("eighth_note_on"); - special_models[10] = xStrHash("db03_path_a"); - special_models[11] = xStrHash("db03_path_b"); - special_models[12] = xStrHash("db03_path_c"); - special_models[13] = xStrHash("db03_path_d"); - special_models[14] = xStrHash("db03_path_e"); - special_models[15] = xStrHash("db03_path_f"); - special_models[16] = xStrHash("db03_path_g"); - special_models[17] = xStrHash("db03_path_h"); - special_models[18] = xStrHash("db03_path_i"); - special_models[19] = xStrHash("db03_path_j"); - special_models[20] = xStrHash("db03_path_k"); - special_models[21] = xStrHash("db03_path_l"); - special_models[22] = xStrHash("db03_path_m"); - special_models[23] = xStrHash("db03_path_o"); - special_models[24] = xStrHash("db03_path_p"); - init = TRUE; - } + static U32 special_models[25] = { + xStrHash("bb_arrow"), + xStrHash("metal_sheets"), + xStrHash("clam_poop"), + xStrHash("beach_chair_yellow"), + xStrHash("debris_pile_rb_smll"), + xStrHash("debris_pile_rb"), + xStrHash("floor_panel"), + xStrHash("gy_woodpieceA"), + xStrHash("quarter_note_on"), + xStrHash("eighth_note_on"), + xStrHash("db03_path_a"), + xStrHash("db03_path_b"), + xStrHash("db03_path_c"), + xStrHash("db03_path_d"), + xStrHash("db03_path_e"), + xStrHash("db03_path_f"), + xStrHash("db03_path_g"), + xStrHash("db03_path_h"), + xStrHash("db03_path_i"), + xStrHash("db03_path_j"), + xStrHash("db03_path_k"), + xStrHash("db03_path_l"), + xStrHash("db03_path_m"), + xStrHash("db03_path_o"), + xStrHash("db03_path_p"), + }; // non-matching: stb happens too early? + zEnt** it = s->ents; - zEnt** end = s->num_ents + it; - do + zEnt** end = s->ents + s->num_ents; + + while (it != end) { + // non-matching: extra `mr` instruction? xEnt* ent = *it; - if (ent && (ent->baseFlags & 0x20) && ent->model) + if (ent && (ent->baseFlags & 0x20) && ent->asset) { - U32* end_special_models = &special_models[24]; - for (U32* id = special_models; id != end_special_models; id++) + U32* id = special_models; + U32* end_special_models = id + 25; + + while (id != end_special_models) { - if (ent->model->modelID == *id) + if (ent->asset->modelInfoID == *id) { ent->chkby |= 0x80; ent->baseFlags |= 0x10; ent->asset->baseFlags |= 0x10; break; } + id++; } } - } while (it != end); + it++; + } } void zGridReset(zScene* s) @@ -94,13 +92,16 @@ void zGridReset(zScene* s) } } -// WIP +// WIP, the MIN/MAX stuff seem like they should be CLAMP +// but the codegen is different. Also, there are a bunch +// of local vars here currently that weren't in the dwarf. +// All the dwarf had was ebox, min_csize and osobox. void zGridInit(zScene* s) { gGridIterActive = NULL; xBox* ebox = xEntGetAllEntsBox(); - xBox osobox; F32 min_csize; + xBox osobox; F32 diff_x = MAX(0.001f, ebox->upper.x - ebox->lower.x); F32 diff_z = MAX(0.001f, ebox->upper.z - ebox->lower.z); @@ -125,6 +126,9 @@ void zGridInit(zScene* s) xGridInit(&colls_grid, ebox, (U16)min_csize, (U16)tmp_z, 1); + // non-matching: missing a bunch of lwz, lfs and stw instructions, + // and ghidra has a few local variables that *appear* unused + F32 local_7c = ebox->lower.x - 1.0f; F32 local_74 = ebox->lower.z - 1.0f; @@ -200,6 +204,7 @@ void zGridUpdateEnt(xEnt* ent) S32 oversize = 0; xGrid* grid = NULL; + // case 2 and 3 need to use `cntlzw` but are `mr` currently switch (ent->gridb.ingrid) { case 1: diff --git a/src/SB/Game/zGrid.h b/src/SB/Game/zGrid.h index b7c387a49..0a1b6a49c 100644 --- a/src/SB/Game/zGrid.h +++ b/src/SB/Game/zGrid.h @@ -9,10 +9,12 @@ #include -extern xGrid colls_grid; -extern xGrid colls_oso_grid; -extern xGrid npcs_grid; +xGrid colls_grid; +xGrid colls_oso_grid; +xGrid npcs_grid; +static S32 zGridInitted; +static void hack_flag_shadows(zScene* s); void zGridUpdateEnt(xEnt* ent); void zGridExit(zScene*); void zGridInit(zScene* s); From 44848b4a02b6e2d84808fb3eb5a7649c11603b08 Mon Sep 17 00:00:00 2001 From: bluisblu <53455507+bluisblu@users.noreply.github.com> Date: Thu, 4 Sep 2025 00:40:29 -0400 Subject: [PATCH 4/6] zGrid OK --- src/SB/Game/zGrid.cpp | 5 +++++ src/SB/Game/zGrid.h | 8 +++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/SB/Game/zGrid.cpp b/src/SB/Game/zGrid.cpp index 7714fc33e..bc6008a6f 100644 --- a/src/SB/Game/zGrid.cpp +++ b/src/SB/Game/zGrid.cpp @@ -7,6 +7,11 @@ #include "xVec3.h" #include +xGrid colls_grid; +xGrid colls_oso_grid; +xGrid npcs_grid; +static S32 zGridInitted; + static void hack_flag_shadows(zScene* s) { static U32 special_models[25] = { diff --git a/src/SB/Game/zGrid.h b/src/SB/Game/zGrid.h index 0a1b6a49c..b7c387a49 100644 --- a/src/SB/Game/zGrid.h +++ b/src/SB/Game/zGrid.h @@ -9,12 +9,10 @@ #include -xGrid colls_grid; -xGrid colls_oso_grid; -xGrid npcs_grid; -static S32 zGridInitted; +extern xGrid colls_grid; +extern xGrid colls_oso_grid; +extern xGrid npcs_grid; -static void hack_flag_shadows(zScene* s); void zGridUpdateEnt(xEnt* ent); void zGridExit(zScene*); void zGridInit(zScene* s); From a4f79ed7fc7b83d5e0c3294b9d7efd311f9f8067 Mon Sep 17 00:00:00 2001 From: bluisblu <53455507+bluisblu@users.noreply.github.com> Date: Thu, 4 Sep 2025 00:55:30 -0400 Subject: [PATCH 5/6] Make requested changes --- src/SB/Core/x/xGrid.h | 3 +-- src/SB/Game/zGrid.h | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/SB/Core/x/xGrid.h b/src/SB/Core/x/xGrid.h index 6cbd790a7..653df9e32 100644 --- a/src/SB/Core/x/xGrid.h +++ b/src/SB/Core/x/xGrid.h @@ -62,8 +62,7 @@ typedef S32 (*GridEntCallback)(xEnt*, void*); extern volatile S32 gGridIterActive; void xGridBoundInit(xGridBound* gridb, void* data); -void xGridInit(xGrid* grid /* r18 */, const xBox* bounds /* r2 */, U16 nx /* r17 */, - U16 nz /* r16 */, U8 ingrid_id /* r2 */); +void xGridInit(xGrid* grid, const xBox* bounds, U16 nx, U16 nz, U8 ingrid_id); void xGridKill(xGrid* grid); void xGridEmpty(xGrid* grid); S32 xGridRemove(xGridBound* gridb); diff --git a/src/SB/Game/zGrid.h b/src/SB/Game/zGrid.h index b7c387a49..5de1529b9 100644 --- a/src/SB/Game/zGrid.h +++ b/src/SB/Game/zGrid.h @@ -5,7 +5,6 @@ #include "xEnt.h" #include "xGrid.h" -#include "xMath.h" #include From 842e815956acb914b51a2214c9aed5e0badb5cd4 Mon Sep 17 00:00:00 2001 From: bluisblu <53455507+bluisblu@users.noreply.github.com> Date: Thu, 4 Sep 2025 00:59:04 -0400 Subject: [PATCH 6/6] Move cmath include to zGrid.cpp --- src/SB/Game/zGrid.cpp | 1 + src/SB/Game/zGrid.h | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/SB/Game/zGrid.cpp b/src/SB/Game/zGrid.cpp index bc6008a6f..c06efb26f 100644 --- a/src/SB/Game/zGrid.cpp +++ b/src/SB/Game/zGrid.cpp @@ -6,6 +6,7 @@ #include "xVec3.h" #include +#include xGrid colls_grid; xGrid colls_oso_grid; diff --git a/src/SB/Game/zGrid.h b/src/SB/Game/zGrid.h index 5de1529b9..40b0645c3 100644 --- a/src/SB/Game/zGrid.h +++ b/src/SB/Game/zGrid.h @@ -6,8 +6,6 @@ #include "xEnt.h" #include "xGrid.h" -#include - extern xGrid colls_grid; extern xGrid colls_oso_grid; extern xGrid npcs_grid;