Add block types

This commit is contained in:
Miloslav Ciz 2024-09-23 23:31:30 +02:00
parent e068b02b9f
commit f1bbb1e1b6
5 changed files with 93 additions and 104 deletions

View file

@ -27,10 +27,10 @@ static const uint8_t map1[] =
0,
LCR_MAP_BLOCK(LCR_BLOCK_FULL_ACCEL,36,0,32,LCR_BLOCK_MATERIAL_CONCRETE,0),
LCR_MAP_BLOCK(LCR_BLOCK_FULL_ACCEL,20,0,32,LCR_BLOCK_MATERIAL_CONCRETE,0),
LCR_MAP_BLOCK(LCR_BLOCK_FULL_ACCEL,32,0,37,LCR_BLOCK_MATERIAL_CONCRETE,0),
LCR_MAP_BLOCK(LCR_BLOCK_FULL_ACCEL,32,0,20,LCR_BLOCK_MATERIAL_CONCRETE,0),
LCR_MAP_BLOCK(LCR_BLOCK_RAMP,36,0,32,LCR_BLOCK_MATERIAL_CONCRETE,0),
LCR_MAP_BLOCK(LCR_BLOCK_RAMP_34,20,0,32,LCR_BLOCK_MATERIAL_CONCRETE,0),
LCR_MAP_BLOCK(LCR_BLOCK_RAMP_12,32,0,37,LCR_BLOCK_MATERIAL_CONCRETE,0),
LCR_MAP_BLOCK(LCR_BLOCK_RAMP_14,32,0,20,LCR_BLOCK_MATERIAL_CONCRETE,0),
/*
LCR_MAP_BLOCK(LCR_BLOCK_FULL_ACCEL,0,0,0,LCR_BLOCK_MATERIAL_CONCRETE,0),

55
map.h
View file

@ -76,11 +76,14 @@
#define LCR_BLOCK_LEFT 0x02 ///< filled left half
#define LCR_BLOCK_BOTTOM_LEFT 0x03 ///< filled bottom left quarter
#define LCR_BLOCK_BOTTOM_LEFT_FRONT 0x04 ///< filled bottom left front eigth
#define LCR_BLOCK_RAMP 0x05
#define LCR_BLOCK_RAMP_HALF 0x06
#define LCR_BLOCK_RAMP_CURVED 0x07
#define LCR_BLOCK_RAMP_CURVED_SHORT 0x08
#define LCR_BLOCK_RAMP_CURVED_WALL 0x09
#define LCR_BLOCK_RAMP 0x05 ///< plain ramp
#define LCR_BLOCK_RAMP_34 0x06 ///< plain ramp, 3/4 size
#define LCR_BLOCK_RAMP_12 0x07 ///< plain ramp, 1/2 size
#define LCR_BLOCK_RAMP_14 0x08 ///< plain ramp, 1/4 size
#define LCR_BLOCK_RAMP_CURVED 0x09
#define LCR_BLOCK_RAMP_CURVED_SHORT 0x0a
#define LCR_BLOCK_RAMP_CURVED_WALL 0x0b
#define LCR_BLOCK_FULL_ACCEL 0x20
#define LCR_BLOCK_FULL_FAN 0x30
@ -90,19 +93,18 @@
#define LCR_BLOCK_FINISH 0x42 ///< finish
// special blocks:
#define LCR_BLOCK_NONE 0x80 /**< no block, can be used e.g to make
holes */
#define LCR_BLOCK_CUBOID_FILL 0x81 /**< makes a cuboid from the previously
specified block, the size is given
by block coordinates */
#define LCR_BLOCK_CUBOID_HOLLOW 0x82 /**< same as the cuboid special block, but
makes a hollow cuboid */
#define LCR_BLOCK_START 0x83 /**< specifies start block position */
#define LCR_BLOCK_NONE 0x80 ///< no block, e.g to make holes
#define LCR_BLOCK_CUBOID_FILL 0x81 /**< makes a cuboid from the
previously specified block, the
size is given by block coords */
#define LCR_BLOCK_CUBOID_HOLLOW 0x82 /**< same as cuboid special block,
but makes a hollow one */
#define LCR_BLOCK_START 0x83 ///< specifies start block position
struct
{
uint16_t blockCount;
uint8_t blocks[LCR_SETTING_MAP_MAX_SIZE * LCR_BLOCK_SIZE];
uint8_t blocks[LCR_SETTING_MAP_MAX_BLOCKS * LCR_BLOCK_SIZE];
uint32_t startPos;
uint8_t environment;
@ -203,7 +205,7 @@ uint8_t *LCR_getMapBlockAtCoordNumber(uint32_t coord)
*/
uint8_t _LCR_mapAddBlock(const uint8_t block[LCR_BLOCK_SIZE])
{
if (LCR_currentMap.blockCount >= LCR_SETTING_MAP_MAX_SIZE)
if (LCR_currentMap.blockCount >= LCR_SETTING_MAP_MAX_BLOCKS)
return 0;
uint32_t coord = LCR_mapBlockGetCoordNumber(block);
@ -515,6 +517,29 @@ void LCR_mapGetBlockShape(uint8_t blockType, uint8_t transform,
ADD(0,0,0) ADD(0,0,6) ADD(6,0,6)
break;
case LCR_BLOCK_RAMP:
case LCR_BLOCK_RAMP_12:
case LCR_BLOCK_RAMP_14:
case LCR_BLOCK_RAMP_34:
{
uint8_t top =
(blockType == LCR_BLOCK_RAMP_14) +
(blockType == LCR_BLOCK_RAMP) * 4 +
(blockType == LCR_BLOCK_RAMP_12 || blockType == LCR_BLOCK_RAMP_34) * 2 +
(blockType == LCR_BLOCK_RAMP_34);
ADD(0,0,0) ADD(0,top,6) ADD(0,0,6) // side
ADD(6,0,0) ADD(6,0,6) ADD(6,top,6) // side
ADD(0,0,0) ADD(6,0,0) ADD(0,top,6) // top
ADD(6,0,0) ADD(6,top,6) ADD(0,top,6) // top
ADD(0,0,6) ADD(6,top,6) ADD(6,0,6) // back
ADD(0,0,6) ADD(0,top,6) ADD(6,top,6) // back
ADD(0,0,0) ADD(0,0,6) ADD(6,0,6) // bottom
ADD(0,0,0) ADD(6,0,6) ADD(6,0,0) // bottom
break;
}
default: break;
}

View file

@ -19,9 +19,7 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#include "map.h"
#include "tinyphysicsengine.h"
#define LCR_CAR_JOINTS 5
#define LCR_CAR_CONNECTIONS 10
// TODO: move some of this to constants?
#define LCR_GRAVITY (LCR_PHYSICS_UNIT / 100)
#define LCR_CAR_FORWARD_FRICTION (TPE_F / 11)
#define LCR_CAR_TURN_FRICTION (4 * TPE_F / 4)
@ -30,15 +28,8 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_CAR_TURN_SPEED (LCR_GAME_UNIT / 20)
#define LCR_CAR_TURN_MAX (LCR_GAME_UNIT / 4)
/*
#define LCR_GRAVITY (LCR_PHYSICS_UNIT / 100)
#define LCR_CAR_FORWARD_FRICTION (TPE_F / 14)
#define LCR_CAR_TURN_FRICTION (3 * TPE_F / 4)
#define LCR_CAR_ELASTICITY (TPE_F / 100)
#define LCR_CAR_ACCELERATION (LCR_PHYSICS_UNIT / 18)
#define LCR_CAR_TURN_SPEED (LCR_GAME_UNIT / 3)
#define LCR_CAR_TURN_MAX (LCR_GAME_UNIT / 4)
*/
#define LCR_CAR_JOINTS 5
#define LCR_CAR_CONNECTIONS 10
struct
{
@ -62,9 +53,7 @@ struct
TPE_Vec3 _LCR_TPE_vec3DividePlain(TPE_Vec3 v, TPE_Unit d)
{
v.x /= d;
v.y /= d;
v.z /= d;
v.x /= d; v.y /= d; v.z /= d;
return v;
}
@ -93,8 +82,6 @@ TPE_Vec3 _LCR_racingEnvironmentFunction(TPE_Vec3 point, TPE_Unit maxDist)
(LCR_PHYSICS_UNIT * LCR_MAP_SIZE_BLOCKS) / 2,
LCR_PHYSICS_UNIT * LCR_MAP_SIZE_BLOCKS)),point)
const uint8_t *block = LCR_currentMap.blocks;
if (maxDist <= LCR_PHYSICS_UNIT / 4) // considering half of square height
{
/* Here we only check the 8 closest blocks => relatively fast. */
@ -156,6 +143,8 @@ TPE_Vec3 _LCR_racingEnvironmentFunction(TPE_Vec3 point, TPE_Unit maxDist)
else
{
printf("oof\n");
const uint8_t *block = LCR_currentMap.blocks;
/* Full check of all map blocks, slow, shouldn't happen often! */
for (int j = 0; j < LCR_currentMap.blockCount; ++j)
{
@ -195,8 +184,7 @@ LCR_GameUnit _LCR_racingSmoothRot(LCR_GameUnit angleNew, LCR_GameUnit angleOld)
LCR_GameUnit diff = angleNew - angleOld;
if (diff > LCR_GAME_UNIT / 8 ||
diff < -1 * LCR_GAME_UNIT / 8)
if (diff > LCR_GAME_UNIT / 8 || diff < -1 * LCR_GAME_UNIT / 8)
return angleNew;
return angleOld + diff / 2;
@ -241,10 +229,8 @@ void LCR_racingInit(void)
LCR_racing.carJoints[4].sizeDivided *= 3;
LCR_racing.carJoints[4].sizeDivided /= 2;
TPE_bodyInit(&(LCR_racing.carBody),
LCR_racing.carJoints,LCR_CAR_JOINTS,
LCR_racing.carConnections,LCR_CAR_CONNECTIONS,
TPE_F);
TPE_bodyInit(&(LCR_racing.carBody),LCR_racing.carJoints,LCR_CAR_JOINTS,
LCR_racing.carConnections,LCR_CAR_CONNECTIONS,TPE_F);
TPE_worldInit(&(LCR_racing.physicsWorld),
&(LCR_racing.carBody),1,_LCR_racingEnvironmentFunction);
@ -274,11 +260,9 @@ void LCR_racingGetCarTransform(LCR_GameUnit position[3],
TPE_Vec3 v;
#if LCR_SETTING_SMOOTH_ANIMATIONS
v = TPE_vec3Plus(
LCR_racing.carPositions[1],
v = TPE_vec3Plus(LCR_racing.carPositions[1], // LERP previous and current pos
_LCR_TPE_vec3DividePlain(
TPE_vec3TimesPlain(
TPE_vec3Minus(
TPE_vec3TimesPlain(TPE_vec3Minus(
LCR_racing.carPositions[0],LCR_racing.carPositions[1]),
interpolationParam),LCR_GAME_UNIT));
@ -288,10 +272,8 @@ void LCR_racingGetCarTransform(LCR_GameUnit position[3],
rotation[0] = _LCR_racingSmoothRot(LCR_racing.carRotations[0].x,
LCR_racing.carRotations[1].x);
rotation[1] = _LCR_racingSmoothRot(LCR_racing.carRotations[0].y,
LCR_racing.carRotations[1].y);
rotation[2] = _LCR_racingSmoothRot(LCR_racing.carRotations[0].z,
LCR_racing.carRotations[1].z);
#else
@ -468,15 +450,13 @@ void LCR_racingStep(unsigned int input)
/* if the car falls on its roof the center joint may flip to the other
side, here we fix it */
// LCR_log("car flipped over, fixing");
LCR_racing.carBody.joints[4].position = TPE_vec3Plus(TPE_vec3Times(carUp,
LCR_GAME_UNIT / 4),LCR_racing.carBody.joints[4].position);
}
TPE_Vec3 tmpVec = LCR_racing.carPositions[0];
LCR_racing.carPositions[0] =
LCR_racing.carPositions[0] = // average position of 4 wheels to get car pos
_LCR_TPE_vec3DividePlain(
TPE_vec3TimesPlain(
TPE_vec3Plus(
@ -486,25 +466,19 @@ LCR_racing.carPositions[0] =
TPE_vec3Plus(
LCR_racing.carBody.joints[2].position,
LCR_racing.carBody.joints[3].position)
),LCR_GAME_UNIT),
4 * LCR_PHYSICS_UNIT);
),LCR_GAME_UNIT),4 * LCR_PHYSICS_UNIT);
LCR_racing.carPositions[0] =
TPE_vec3KeepWithinBox(
LCR_racing.carPositions[1],
LCR_racing.carPositions[0],
LCR_racing.carPositions[0] = // smooth the position
TPE_vec3KeepWithinBox(LCR_racing.carPositions[1],LCR_racing.carPositions[0],
TPE_vec3(
LCR_PHYSICS_UNIT / 64, // TODO: constant
LCR_PHYSICS_UNIT / 64,
LCR_PHYSICS_UNIT / 64
)
);
LCR_PHYSICS_UNIT / 64));
LCR_racing.carPositions[1] = tmpVec;
tmpVec = _LCR_TPE_vec3DividePlain(TPE_vec3TimesPlain(
TPE_bodyGetRotation(&(LCR_racing.carBody),0,2,1),
LCR_GAME_UNIT),TPE_F);
tmpVec = _LCR_TPE_vec3DividePlain(TPE_vec3TimesPlain(TPE_bodyGetRotation(
&(LCR_racing.carBody),0,2,1),LCR_GAME_UNIT),TPE_F);
LCR_racing.carRotations[1] = LCR_racing.carRotations[0];
LCR_racing.carRotations[0] = tmpVec;
@ -530,7 +504,7 @@ void LCR_physicsDebugDraw(LCR_GameUnit camPos[3], LCR_GameUnit camRot[2],
cView.z = (camFov * TPE_F) / LCR_GAME_UNIT;
TPE_worldDebugDraw(&(LCR_racing.physicsWorld),_LCR_drawPhysicsDebugPixel,
cPos,cRot,cView,16,2 * LCR_PHYSICS_UNIT);
cPos,cRot,cView,16,LCR_PHYSICS_UNIT / 4);
}
#endif // guard

View file

@ -8,10 +8,8 @@
#define S3L_RESOLUTION_X LCR_SETTING_RESOLUTION_X
#define S3L_RESOLUTION_Y LCR_SETTING_RESOLUTION_Y
#define S3L_PIXEL_FUNCTION _LCR_pixelFunc3D
#define S3L_PERSPECTIVE_CORRECTION 2
#define S3L_NEAR_CROSS_STRATEGY 1
#define S3L_Z_BUFFER 1
#include "small3dlib.h"
@ -30,7 +28,6 @@
LCR_RENDERER_CHUNK_RESOLUTION * LCR_RENDERER_CHUNK_RESOLUTION)
#define LCR_RENDERER_MODEL_COUNT 10
#define LCR_RENDERER_CAR_SCALE (LCR_RENDERER_UNIT / 4)
struct
@ -40,8 +37,6 @@ struct
S3L_Model3D *carModel;
S3L_Model3D *ghostModel;
// TODO: ghostModel
/**
The scene model array.
0, 1, 2, 3, 4, 5, 6, 7: nearest map chunk models
@ -426,16 +421,14 @@ uint8_t _LCR_rendererCheckMapTriCover(const S3L_Index *t1,
((t3[1] == t2[0] || t3[1] == t2[1] || t3[1] == t2[2]) << 1) |
((t3[2] == t2[0] || t3[2] == t2[1] || t3[2] == t2[2]) << 2);
if (
t3 != t1 && t3 != t2 &&
if (t3 != t1 && t3 != t2 &&
(sharedVerts == 3 || sharedVerts == 5 || sharedVerts == 6) &&
LCR_renderer.mapVerts[3 * t3[0] + plane] ==
LCR_renderer.mapVerts[3 * t3[1] + plane] &&
LCR_renderer.mapVerts[3 * t3[1] + plane] ==
LCR_renderer.mapVerts[3 * t3[2] + plane] &&
LCR_renderer.mapVerts[3 * t3[0] + plane] ==
LCR_renderer.mapVerts[3 * t1[0] + plane]
)
LCR_renderer.mapVerts[3 * t1[0] + plane])
{
// here shares exactly two vertices and is in the same plane
@ -787,11 +780,8 @@ uint8_t LCR_rendererInit(void)
S3L_vec4Set(&(LCR_renderer.carModel->transform.scale),
LCR_RENDERER_CAR_SCALE,LCR_RENDERER_CAR_SCALE,LCR_RENDERER_CAR_SCALE,0);
S3L_model3DInit(
LCR_carVertices
,LCR_CAR_VERTEX_COUNT,
LCR_carTriangles,LCR_CAR_TRIANGLE_COUNT,
LCR_renderer.ghostModel);
S3L_model3DInit(LCR_carVertices,LCR_CAR_VERTEX_COUNT,LCR_carTriangles,
LCR_CAR_TRIANGLE_COUNT,LCR_renderer.ghostModel);
LCR_renderer.ghostModel->transform.scale =
LCR_renderer.carModel->transform.scale;

View file

@ -51,10 +51,10 @@
#define LCR_SETTING_SKY_SIZE 4
#endif
#ifndef LCR_SETTING_MAP_MAX_SIZE
#ifndef LCR_SETTING_MAP_MAX_BLOCKS
/** Maximum number of blocks a map can consist of, decreasing will save RAM
but also rule out loading bigger maps. */
#define LCR_SETTING_MAP_MAX_SIZE 4096
#define LCR_SETTING_MAP_MAX_BLOCKS 4096
#endif
#ifndef LCR_SETTING_TRIANGLE_CULLING_PERIOD