Skip to content

Commit 7f3ea8e

Browse files
committed
Refactor map colliders and randomize spawns
Convert map data to a grid-based layout and simplify collider defs: replace the old MapCategory/cube-based entries with a MAP_GRID, map constants, and merged AABB entries (MAP_COLLIDERS now contains explicit min/max boxes only). Update MapColliderDef to only store explicit AABB + isGround and remove cube auto-AABB handling. Adjust GameServer to read the new colliders (always use the provided AABB) and add <cstdlib> for rand(). Change GetSpawnPosition to pick a random position within one of four corner spawn areas (randomized X/Z per spawn); playerId/teamId are no longer used for deterministic offsets. This reduces server-side complexity and centralizes map geometry, but note spawn behavior is now nondeterministic unless srand is set elsewhere.
1 parent 1555113 commit 7f3ea8e

File tree

2 files changed

+96
-55
lines changed

2 files changed

+96
-55
lines changed

Network/game_server.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "map_colliders.h"
1111
#include <cmath>
1212
#include <cstdio>
13+
#include <cstdlib>
1314

1415
GameServer::GameServer()
1516
: m_pNetwork(nullptr)
@@ -38,22 +39,10 @@ void GameServer::Initialize(ENetServerNetwork* pNetwork)
3839
{
3940
const MapColliderDef& def = MAP_COLLIDERS[i];
4041
ServerCollider sc;
41-
if (def.category == MAP_CUBE)
42-
{
43-
// Cube: AABB = position ± 0.5
44-
sc.aabb = {
45-
{ def.posX - 0.5f, def.posY - 0.5f, def.posZ - 0.5f },
46-
{ def.posX + 0.5f, def.posY + 0.5f, def.posZ + 0.5f }
47-
};
48-
}
49-
else
50-
{
51-
// Explicit AABB
52-
sc.aabb = {
53-
{ def.minX, def.minY, def.minZ },
54-
{ def.maxX, def.maxY, def.maxZ }
55-
};
56-
}
42+
sc.aabb = {
43+
{ def.minX, def.minY, def.minZ },
44+
{ def.maxX, def.maxY, def.maxZ }
45+
};
5746
sc.isGround = def.isGround;
5847
m_Colliders.push_back(sc);
5948
}
@@ -80,15 +69,27 @@ uint8_t GameServer::AssignTeam() const
8069
}
8170

8271
//-----------------------------------------------------------------------------
83-
// Spawn position per team + player ID
72+
// Spawn position — random within one of 4 corner areas (4x4 each)
73+
//
74+
// Corner A (top-left): X: -9 to -5, Z: -9 to -5
75+
// Corner B (top-right): X: 5 to 9, Z: -9 to -5
76+
// Corner C (bottom-left): X: -9 to -5, Z: 5 to 9
77+
// Corner D (bottom-right): X: 5 to 9, Z: 5 to 9
8478
//-----------------------------------------------------------------------------
85-
Float3 GameServer::GetSpawnPosition(uint8_t playerId, uint8_t teamId)
79+
Float3 GameServer::GetSpawnPosition(uint8_t /*playerId*/, uint8_t /*teamId*/)
8680
{
87-
float offsets[] = { 0.0f, 5.0f, -5.0f, 10.0f };
88-
float x = offsets[playerId % 4];
89-
if (teamId == PlayerTeam::BLUE)
90-
x = -x; // Mirror for blue team
91-
float z = (teamId == PlayerTeam::RED) ? -20.0f : 20.0f;
81+
struct SpawnArea { float minX, maxX, minZ, maxZ; };
82+
static const SpawnArea areas[4] = {
83+
{ -9.0f, -5.0f, -9.0f, -5.0f }, // top-left
84+
{ 5.0f, 9.0f, -9.0f, -5.0f }, // top-right
85+
{ -9.0f, -5.0f, 5.0f, 9.0f }, // bottom-left
86+
{ 5.0f, 9.0f, 5.0f, 9.0f }, // bottom-right
87+
};
88+
89+
int corner = rand() % 4;
90+
const SpawnArea& a = areas[corner];
91+
float x = a.minX + static_cast<float>(rand()) / RAND_MAX * (a.maxX - a.minX);
92+
float z = a.minZ + static_cast<float>(rand()) / RAND_MAX * (a.maxZ - a.minZ);
9293
return { x, 0.0f, z };
9394
}
9495

Network/map_colliders.h

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,93 @@
22
//=============================================================================
33
// map_colliders.h
44
//
5-
// Shared map collider definitions.
5+
// Shared map data definitions.
66
// This file is used by BOTH client and server — keep in sync!
77
// No DirectXMath dependency — pure C structs.
8+
//
9+
// MAP_GRID[][] defines the obstacle layout (1=block, 0=empty).
10+
// MAP_COLLIDERS[] defines merged AABBs for physics/hitscan.
11+
// The client generates individual cube draw calls from the grid;
12+
// the server only reads MAP_COLLIDERS[].
813
//=============================================================================
914

10-
// Object categories
11-
enum MapCategory
15+
//-----------------------------------------------------------------------------
16+
// Grid-based map layout
17+
//-----------------------------------------------------------------------------
18+
static const int MAP_GRID_ROWS = 20;
19+
static const int MAP_GRID_COLS = 20;
20+
static const int MAP_BLOCK_HEIGHT = 4; // cubes stacked per obstacle cell
21+
static const float MAP_OFFSET_X = -10.0f; // world X offset (center the grid)
22+
static const float MAP_OFFSET_Z = -10.0f; // world Z offset (center the grid)
23+
24+
static const int MAP_GRID[MAP_GRID_ROWS][MAP_GRID_COLS] =
1225
{
13-
MAP_GROUND = 0, // Ground plane — rendered as MeshField, has AABB
14-
MAP_CUBE = 1, // Cube block — rendered with Cube_Draw, AABB from position
15-
MAP_WALL = 2, // Invisible wall — AABB only, no rendering
26+
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
27+
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
28+
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
29+
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
30+
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
31+
{1,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1},
32+
{1,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1},
33+
{1,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1},
34+
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
35+
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
36+
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
37+
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
38+
{1,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1},
39+
{1,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1},
40+
{1,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1},
41+
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
42+
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
43+
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
44+
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
45+
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
1646
};
1747

18-
// Collider definition
19-
// For MAP_CUBE: AABB is auto-computed from position (±0.5 unit cube)
20-
// For MAP_GROUND / MAP_WALL: AABB is specified explicitly via min/max
48+
//-----------------------------------------------------------------------------
49+
// Collider definition (physics / hitscan)
50+
//-----------------------------------------------------------------------------
2151
struct MapColliderDef
2252
{
23-
int category;
24-
float posX, posY, posZ; // position (used for cube rendering & AABB center)
25-
float minX, minY, minZ; // AABB min (explicit, ignored for MAP_CUBE)
26-
float maxX, maxY, maxZ; // AABB max (explicit, ignored for MAP_CUBE)
27-
bool isGround; // true = player can land on this surface
53+
float minX, minY, minZ;
54+
float maxX, maxY, maxZ;
55+
bool isGround;
2856
};
2957

3058
// ============================================================================
31-
// MAP DATA — Add new colliders here!
59+
// COLLIDERS — Manually merged AABBs for physics/hitscan.
60+
//
61+
// Grid cell (row, col) maps to world:
62+
// X = col + MAP_OFFSET_X, Z = row + MAP_OFFSET_Z
63+
// A group from (r1,c1)-(r2,c2) → AABB:
64+
// min = (c1-10, 0, r1-10) max = (c2+1-10, 4, r2+1-10)
3265
// ============================================================================
3366
static const MapColliderDef MAP_COLLIDERS[] =
3467
{
35-
// === Ground ===
36-
// cat pos(x,y,z) AABB min AABB max ground
37-
{ MAP_GROUND, 0,0,0, -128.0f,-1.0f,-128.0f, 128.0f, 0.0f, 128.0f, true },
38-
39-
// === Cube Blocks (AABB auto = pos ± 0.5) ===
40-
// y=0.5 → AABB [0,1] (sits on ground) | y=1.5 → AABB [1,2] (second layer)
41-
// cat pos(x,y,z) (min/max ignored for cubes) ground
42-
{ MAP_CUBE, 7.5f, 0.5f, 7.5f, 0,0,0, 0,0,0, true },
43-
{ MAP_CUBE, 7.5f, 0.5f, 8.5f, 0,0,0, 0,0,0, true },
44-
{ MAP_CUBE, 8.5f, 0.5f, 7.5f, 0,0,0, 0,0,0, true },
45-
{ MAP_CUBE, 8.5f, 0.5f, 8.5f, 0,0,0, 0,0,0, true },
46-
{ MAP_CUBE, 7.5f, 1.5f, 7.5f, 0,0,0, 0,0,0, true },
47-
{ MAP_CUBE, 8.5f, 1.5f, 7.5f, 0,0,0, 0,0,0, true },
48-
49-
// === Invisible Walls (map boundary) ===
50-
// cat pos(unused) AABB min AABB max ground
51-
// (Add map boundary walls here if needed)
68+
// AABB min AABB max ground
69+
70+
// --- Ground plane ---
71+
{ -128.0f,-1.0f,-128.0f, 128.0f, 0.0f, 128.0f, true },
72+
73+
// --- Border walls ---
74+
// Top wall: row 0, cols 0-19
75+
{ -10.0f, 0.0f,-10.0f, 10.0f, 4.0f, -9.0f, true },
76+
// Bottom wall: row 19, cols 0-19
77+
{ -10.0f, 0.0f, 9.0f, 10.0f, 4.0f, 10.0f, true },
78+
// Left wall: rows 1-18, col 0
79+
{ -10.0f, 0.0f, -9.0f, -9.0f, 4.0f, 9.0f, true },
80+
// Right wall: rows 1-18, col 19
81+
{ 9.0f, 0.0f, -9.0f, 10.0f, 4.0f, 9.0f, true },
82+
83+
// --- Interior blocks (3x3 each, height 4) ---
84+
// Block A: rows 5-7, cols 5-7
85+
{ -5.0f, 0.0f, -5.0f, -2.0f, 4.0f, -2.0f, true },
86+
// Block B: rows 5-7, cols 12-14
87+
{ 2.0f, 0.0f, -5.0f, 5.0f, 4.0f, -2.0f, true },
88+
// Block C: rows 12-14, cols 5-7
89+
{ -5.0f, 0.0f, 2.0f, -2.0f, 4.0f, 5.0f, true },
90+
// Block D: rows 12-14, cols 12-14
91+
{ 2.0f, 0.0f, 2.0f, 5.0f, 4.0f, 5.0f, true },
5292
};
5393

5494
static const int MAP_COLLIDER_COUNT = sizeof(MAP_COLLIDERS) / sizeof(MAP_COLLIDERS[0]);

0 commit comments

Comments
 (0)