Skip to content

Commit 105a2b5

Browse files
committed
refactore in progress, still nothing works
1 parent 8f4252c commit 105a2b5

8 files changed

Lines changed: 109 additions & 94 deletions

src/CarnageGame.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -330,22 +330,16 @@ bool CarnageGame::StartScenario(const std::string& mapName)
330330
{
331331
for (int xBlock = 10; xBlock < 20 && currFindPosIter < mNumPlayers; ++xBlock)
332332
{
333-
pos[currFindPosIter] = glm::ivec3(xBlock, MAP_LAYERS_COUNT - 1, yBlock);
334-
335-
float currHeight = gGameMap.GetHeightAtPosition(pos[currFindPosIter]);
336-
int zBlock = static_cast<int>(currHeight);
337-
if (zBlock > MAP_LAYERS_COUNT - 1)
338-
continue;
339-
340-
BlockStyle* currBlock = gGameMap.GetBlock(xBlock, yBlock, zBlock);
341-
if (currBlock->mGroundType == eGroundType_Field ||
342-
currBlock->mGroundType == eGroundType_Pawement ||
343-
currBlock->mGroundType == eGroundType_Road)
333+
for (int zBlock = MAP_LAYERS_COUNT - 1; zBlock > -1; --zBlock)
344334
{
345-
pos[currFindPosIter].x += 0.5f;
346-
pos[currFindPosIter].z += 0.5f;
347-
pos[currFindPosIter].y = currHeight * 4;
348-
++currFindPosIter;
335+
BlockStyle* currBlock = gGameMap.GetBlock(xBlock, yBlock, zBlock);
336+
if (currBlock->mGroundType == eGroundType_Field ||
337+
currBlock->mGroundType == eGroundType_Pawement ||
338+
currBlock->mGroundType == eGroundType_Road)
339+
{
340+
pos[currFindPosIter++] = Convert::MapUnitsToMeters(glm::ivec3(xBlock, zBlock, yBlock));
341+
break;
342+
}
349343
}
350344
}
351345
}

src/GameCheatsWindow.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,9 @@ void GameCheatsWindow::DoUI(ImGuiIO& imguiContext)
7979
ImGui::Separator();
8080

8181
// get block location
82-
int mapcoordx = (int) pedPosition.x;
83-
int mapcoordy = (int) pedPosition.z;
84-
int maplayer = (int) pedPosition.y;
82+
glm::ivec3 blockPosition = Convert::MetersToMapUnits(pedPosition);
8583

86-
BlockStyle* currBlock = gGameMap.GetBlockClamp(mapcoordx, mapcoordy, maplayer);
84+
BlockStyle* currBlock = gGameMap.GetBlockClamp(blockPosition.x, blockPosition.z, blockPosition.y);
8785

8886
ImGui::Text("b ground: %s", cxx::enum_to_string(currBlock->mGroundType));
8987
ImGui::Text("b slope: %d", currBlock->mSlopeType);

src/GameMapHelpers.cpp

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -252,104 +252,101 @@ void GameMapHelpers::PutBlockFace(GameMapManager& cityScape, MapMeshData& meshDa
252252
meshData.mBlocksIndices[baseIndex + 5] = baseVertexIndex + 1;
253253
}
254254

255-
int GameMapHelpers::GetSlopeHeight(int slopeType, int pxcoord_x, int pxcoord_y)
255+
float GameMapHelpers::GetSlopeHeight(int slopeType, float coord_x, float coord_y)
256256
{
257-
return 0.0f; // todo: redo
257+
debug_assert(coord_x >= 0.0f && coord_x <= 1.0f);
258+
debug_assert(coord_y >= 0.0f && coord_y <= 1.0f);
258259

259-
debug_assert(pxcoord_x >= 0 && pxcoord_x < PIXELS_PER_MAP_UNIT);
260-
debug_assert(pxcoord_y >= 0 && pxcoord_y < PIXELS_PER_MAP_UNIT);
260+
// all values and calculations are in map units
261261

262-
int pixmin = 0;
263-
int pixmax = 0;
264-
int pixcoord = 0;
262+
float min_evelation = 0.0f;
263+
float max_evelation = 0.0f;
264+
float lerp_factor = 0.0f;
265265

266266
switch (slopeType)
267267
{
268-
case 0: return 0;
268+
case 0: return 0.0f;
269+
269270
// N, 26 low, high
270271
case 1: case 2:
271-
pixmin = (PIXELS_PER_MAP_UNIT / 2) * (slopeType - 1 + 1);
272-
pixmax = (PIXELS_PER_MAP_UNIT / 2) * (slopeType - 1 + 0);
273-
pixcoord = pxcoord_y;
272+
min_evelation = (1.0f / 2) * (slopeType - 1 + 1);
273+
max_evelation = (1.0f / 2) * (slopeType - 1 + 0);
274+
lerp_factor = coord_y;
274275
break;
275276
// S, 26 low, high
276277
case 3: case 4:
277-
pixmin = (PIXELS_PER_MAP_UNIT / 2) * (slopeType - 3 + 0);
278-
pixmax = (PIXELS_PER_MAP_UNIT / 2) * (slopeType - 3 + 1);
279-
pixcoord = pxcoord_y;
278+
min_evelation = (1.0f / 2) * (slopeType - 3 + 0);
279+
max_evelation = (1.0f / 2) * (slopeType - 3 + 1);
280+
lerp_factor = coord_y;
280281
break;
281282
// W, 26 low, high
282283
case 5: case 6:
283-
pixmin = (PIXELS_PER_MAP_UNIT / 2) * (slopeType - 5 + 1);
284-
pixmax = (PIXELS_PER_MAP_UNIT / 2) * (slopeType - 5 + 0);
285-
pixcoord = pxcoord_x;
284+
min_evelation = (1.0f / 2) * (slopeType - 5 + 1);
285+
max_evelation = (1.0f / 2) * (slopeType - 5 + 0);
286+
lerp_factor = coord_x;
286287
break;
287288
// E, 26 low, high
288289
case 7: case 8:
289-
pixmin = (PIXELS_PER_MAP_UNIT / 2) * (slopeType - 7 + 0);
290-
pixmax = (PIXELS_PER_MAP_UNIT / 2) * (slopeType - 7 + 1);
291-
pixcoord = pxcoord_x;
290+
min_evelation = (1.0f / 2) * (slopeType - 7 + 0);
291+
max_evelation = (1.0f / 2) * (slopeType - 7 + 1);
292+
lerp_factor = coord_x;
292293
break;
293294
// N, 7 low - high
294295
case 9: case 10: case 11: case 12:
295296
case 13: case 14: case 15: case 16:
296-
pixmin = (PIXELS_PER_MAP_UNIT / 8) * (slopeType - 9 + 1);
297-
pixmax = (PIXELS_PER_MAP_UNIT / 8) * (slopeType - 9 + 0);
298-
pixcoord = pxcoord_y;
297+
min_evelation = (1.0f / 8) * (slopeType - 9 + 1);
298+
max_evelation = (1.0f / 8) * (slopeType - 9 + 0);
299+
lerp_factor = coord_y;
299300
break;
300301
// S, 7 low - high
301302
case 17: case 18: case 19: case 20:
302303
case 21: case 22: case 23: case 24:
303-
pixmin = (PIXELS_PER_MAP_UNIT / 8) * (slopeType - 17 + 0);
304-
pixmax = (PIXELS_PER_MAP_UNIT / 8) * (slopeType - 17 + 1);
305-
pixcoord = pxcoord_y;
304+
min_evelation = (1.0f / 8) * (slopeType - 17 + 0);
305+
max_evelation = (1.0f / 8) * (slopeType - 17 + 1);
306+
lerp_factor = coord_y;
306307
break;
307308
// W, 7 low - high
308309
case 25: case 26: case 27: case 28:
309310
case 29: case 30: case 31: case 32:
310-
pixmin = (PIXELS_PER_MAP_UNIT / 8) * (slopeType - 25 + 1);
311-
pixmax = (PIXELS_PER_MAP_UNIT / 8) * (slopeType - 25 + 0);
312-
pixcoord = pxcoord_x;
311+
min_evelation = (1.0f / 8) * (slopeType - 25 + 1);
312+
max_evelation = (1.0f / 8) * (slopeType - 25 + 0);
313+
lerp_factor = coord_x;
313314
break;
314315
// E, 7 low - high
315316
case 33: case 34: case 35: case 36:
316317
case 37: case 38: case 39: case 40:
317-
pixmin = (PIXELS_PER_MAP_UNIT / 8) * (slopeType - 33 + 0);
318-
pixmax = (PIXELS_PER_MAP_UNIT / 8) * (slopeType - 33 + 1);
319-
pixcoord = pxcoord_x;
318+
min_evelation = (1.0f / 8) * (slopeType - 33 + 0);
319+
max_evelation = (1.0f / 8) * (slopeType - 33 + 1);
320+
lerp_factor = coord_x;
320321
break;
321322
// 41 - 44 = 45 N,S,W,E
322323
case 41:
323-
pixmin = PIXELS_PER_MAP_UNIT;
324-
pixmax = 0;
325-
pixcoord = pxcoord_y;
324+
min_evelation = 1.0f;
325+
max_evelation = 0;
326+
lerp_factor = coord_y;
326327
break;
327328
case 42:
328-
pixmin = 0;
329-
pixmax = PIXELS_PER_MAP_UNIT;
330-
pixcoord = pxcoord_y;
329+
min_evelation = 0;
330+
max_evelation = 1.0f;
331+
lerp_factor = coord_y;
331332
break;
332333
case 43:
333-
pixmin = PIXELS_PER_MAP_UNIT;
334-
pixmax = 0;
335-
pixcoord = pxcoord_x;
334+
min_evelation = 1.0f;
335+
max_evelation = 0;
336+
lerp_factor = coord_x;
336337
break;
337338
case 44:
338-
pixmin = 0;
339-
pixmax = PIXELS_PER_MAP_UNIT;
340-
pixcoord = pxcoord_x;
339+
min_evelation = 0;
340+
max_evelation = 1.0f;
341+
lerp_factor = coord_x;
341342
break;
342343

343344
default:
344345
{
345346
debug_assert(false);
346-
return 0;
347+
return 0.0f;
347348
}
348349
}
349-
350-
float t = (pixcoord * 1.0f) / (PIXELS_PER_MAP_UNIT - 1);
351-
352-
// linear interpolate point
353-
int pixheight = static_cast<int>(glm::lerp(pixmin * 1.0f, pixmax * 1.0f, t));
354-
return pixheight;
350+
// linear interpolate elevation
351+
return glm::lerp(min_evelation, max_evelation, lerp_factor);
355352
}

src/GameMapHelpers.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class GameMapHelpers final
1616

1717
// compute height for specific block slope type
1818
// @param slopeType: Slope type
19-
// @param pxcoord_x, pxcoord_y: Position within block specified in pixels [0, MAP_PIXELS_PER_TILE)
20-
// @return slope height specified in pixels [0, MAP_PIXELS_PER_TILE]
21-
static int GetSlopeHeight(int slopeType, int pxcoord_x, int pxcoord_y);
19+
// @param x, y: Position within block [0, 1]
20+
// @return slope height specified in map units [0, 1]
21+
static float GetSlopeHeight(int slopeType, float x, float y);
2222

2323
private:
2424
// internals

src/GameMapManager.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -224,39 +224,36 @@ void GameMapManager::FixShiftedBits()
224224

225225
float GameMapManager::GetHeightAtPosition(const glm::vec3& position, bool excludeWater) const
226226
{
227-
int mapcoordx = (int) position.x;
228-
int mapcoordy = (int) position.z;
229-
int maplayer = (int) (position.y + 0.5f);
227+
// get map block position in which we are located
228+
glm::ivec3 mapBlock = Convert::MetersToMapUnits(position);
230229

231-
float height = maplayer * 1.0f; // reset height to ground
232-
233-
for (;height > 0.0f;)
230+
float currentHeight = (float) mapBlock.y; // set current height to ground, map units
231+
for (; currentHeight > 0.0f;)
234232
{
235-
BlockStyle* blockData = GetBlockClamp(mapcoordx, mapcoordy, maplayer);
236-
237-
// slope
238-
int slope = blockData->mSlopeType;
233+
BlockStyle* blockData = GetBlockClamp(mapBlock.x, mapBlock.z, mapBlock.y); // y is map layer
239234

240-
if (slope) // compute slope height
235+
// compute slope height
236+
if (blockData->mSlopeType)
241237
{
242-
int cx = Convert::MapUnitsToPixels(position.x - mapcoordx);
243-
int cy = Convert::MapUnitsToPixels(position.z - mapcoordy);
238+
// subposition within block
239+
float cx = Convert::MetersToMapUnits(position.x) - mapBlock.x;
240+
float cy = Convert::MetersToMapUnits(position.z) - mapBlock.z;
241+
242+
currentHeight += GameMapHelpers::GetSlopeHeight(blockData->mSlopeType, cx, cy);
244243

245-
int pix_height = GameMapHelpers::GetSlopeHeight(slope, cx, cy);
246-
height += Convert::PixelsToMapUnits(pix_height);
247244
break;
248245
}
249246

250247
if (blockData->mGroundType == eGroundType_Air || (blockData->mGroundType == eGroundType_Water && excludeWater)) // fall through non solid block
251248
{
252-
height -= 1.0f;
253-
--maplayer;
249+
currentHeight -= 1.0f;
250+
mapBlock.y -= 1;
254251
continue;
255252
}
256253

257-
break;
254+
break; // bail out
258255
}
259-
return height;
256+
return Convert::MapUnitsToMeters(currentHeight);
260257
}
261258

262259
bool GameMapManager::TraceSegment2D(const glm::vec2& origin, const glm::vec2& destination, float height, glm::vec2& outPoint)

src/GameMapManager.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@
33
#include "GameDefs.h"
44
#include "StyleData.h"
55

6+
/*
7+
1 map unit == 4.0 meters (see gamedefs)
8+
9+
^^^^^^^^
10+
| |
11+
| |
12+
|________|_ _ _ _ _ _ _ _ 12.0 meters (3 map units)
13+
________
14+
| |
15+
| |
16+
| |
17+
|________|_ _ _ _ _ _ _ _ 8.0 meters (2 map units)
18+
________
19+
| |
20+
| |
21+
| |
22+
|________|_ _ _ _ _ _ _ _ 4.0 meters (1 map unit)
23+
________
24+
| |
25+
| |
26+
| |
27+
|________|_ _ _ _ _ _ _ _ 0.0 meters (0 map units)
28+
*/
29+
630
// this class manages GTA map and style data which get loaded from CMP/G24-files
731
class GameMapManager final: public cxx::noncopyable
832
{
@@ -29,7 +53,7 @@ class GameMapManager final: public cxx::noncopyable
2953
BlockStyle* GetBlock(int coordx, int coordy, int layer) const;
3054
BlockStyle* GetBlockClamp(int coordx, int coordy, int layer) const;
3155

32-
// get real height at specified map point
56+
// Get real height at specified map point
3357
// @param position: Current position on map
3458
float GetHeightAtPosition(const glm::vec3& position, bool excludeWater = true) const;
3559

src/PhysicsManager.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ PhysicsManager gPhysics;
3333
PhysicsManager::PhysicsManager()
3434
: mMapCollisionShape()
3535
, mPhysicsWorld()
36+
, mGravityForce()
3637
{
3738
}
3839

@@ -48,6 +49,8 @@ bool PhysicsManager::InitPhysicsWorld()
4849
mSimulationStepTime = (float) (1.0 / physicsFramerate);
4950
debug_assert(mSimulationStepTime > 0.0);
5051

52+
mGravityForce = Convert::MapUnitsToMeters(0.5f);
53+
5154
CreateMapCollisionShape();
5255
return true;
5356
}
@@ -422,7 +425,7 @@ void PhysicsManager::FixedStepGravity()
422425

423426
if (!onTheGround && physicsComponent->mFalling)
424427
{
425-
physicsComponent->mHeight -= (mSimulationStepTime * 0.5f);
428+
physicsComponent->mHeight -= (mSimulationStepTime * mGravityForce);
426429
}
427430
else
428431
{

src/PhysicsManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class PhysicsManager final: private b2ContactListener
7575
float mSimulationTimeAccumulator;
7676
float mSimulationStepTime;
7777

78+
float mGravityForce; // meters per second
79+
7880
// physics components pools
7981
cxx::object_pool<PedPhysicsComponent> mPedsBodiesPool;
8082
cxx::object_pool<CarPhysicsComponent> mCarsBodiesPool;

0 commit comments

Comments
 (0)