Clean a bit

This commit is contained in:
Miloslav Ciz 2024-09-29 21:41:06 +02:00
parent beda272f18
commit 3310008a0d
2 changed files with 57 additions and 118 deletions

View file

@ -16,8 +16,6 @@
#include <stdint.h> #include <stdint.h>
#include "map.h" #include "map.h"
// TODO: images should be 64x64 indexed, sky will be composed of 2x2 normal images
static const uint8_t map1[] = static const uint8_t map1[] =
{ {
LCR_MAP_MAGIC_NUMBER, LCR_MAP_MAGIC_NUMBER,
@ -48,10 +46,13 @@ LCR_MAP_BLOCK(LCR_BLOCK_START,32,1,32,0,LCR_BLOCK_TRANSFORM_ROT_180),
#define LCR_IMAGE_SIZE 64 ///< one-dimension resolution of bitmap image #define LCR_IMAGE_SIZE 64 ///< one-dimension resolution of bitmap image
#define LCR_IMAGE_STORE_SIZE (LCR_IMAGE_SIZE * LCR_IMAGE_SIZE + 256 * 2) #define LCR_IMAGE_STORE_SIZE (LCR_IMAGE_SIZE * LCR_IMAGE_SIZE + 256 * 2)
// TODO: put this into struct
uint16_t _LCR_currentImagePalette[256]; struct
const uint8_t *_LCR_currentImagePixel; {
const uint8_t *_LCR_currentImage; uint16_t palette[256];
const uint8_t *pixel;
const uint8_t *image;
} LCR_currentImage;
#define LCR_IMAGE_WALL_CONCRETE 0 #define LCR_IMAGE_WALL_CONCRETE 0
#define LCR_IMAGE_WALL_WOOD 1 #define LCR_IMAGE_WALL_WOOD 1
@ -6138,17 +6139,17 @@ static const uint8_t LCR_images[] =
void LCR_loadImage(unsigned int index) void LCR_loadImage(unsigned int index)
{ {
_LCR_currentImage = LCR_images + index * LCR_IMAGE_STORE_SIZE; LCR_currentImage.image = LCR_images + index * LCR_IMAGE_STORE_SIZE;
for (int i = 0; i < 256; ++i) for (int i = 0; i < 256; ++i)
{ {
_LCR_currentImagePalette[i] = *_LCR_currentImage; LCR_currentImage.palette[i] = *LCR_currentImage.image;
_LCR_currentImage++; LCR_currentImage.image++;
_LCR_currentImagePalette[i] |= ((uint16_t) (*_LCR_currentImage)) << 8; LCR_currentImage.palette[i] |= ((uint16_t) (*LCR_currentImage.image)) << 8;
_LCR_currentImage++; LCR_currentImage.image++;
} }
_LCR_currentImagePixel = _LCR_currentImage; LCR_currentImage.pixel = LCR_currentImage.image;
} }
/** /**
@ -6159,11 +6160,11 @@ void LCR_imageChangeBrightness(int up)
{ {
if (up) if (up)
for (int i = 0; i < 256; ++i) for (int i = 0; i < 256; ++i)
_LCR_currentImagePalette[i] |= 0x18e3; LCR_currentImage.palette[i] |= 0x18e3;
else else
for (int i = 0; i < 256; ++i) for (int i = 0; i < 256; ++i)
_LCR_currentImagePalette[i] = LCR_currentImage.palette[i] =
((_LCR_currentImagePalette[i] >> 1) & 0x7bef); ((LCR_currentImage.palette[i] >> 1) & 0x7bef);
} }
/** /**
@ -6175,7 +6176,7 @@ uint16_t LCR_sampleImage(int x, int y)
// TODO: bottleneck, later on optimize here // TODO: bottleneck, later on optimize here
x = (y % LCR_IMAGE_SIZE) * LCR_IMAGE_SIZE + (x % LCR_IMAGE_SIZE); x = (y % LCR_IMAGE_SIZE) * LCR_IMAGE_SIZE + (x % LCR_IMAGE_SIZE);
x += (x < 0) * (LCR_IMAGE_SIZE * LCR_IMAGE_SIZE); x += (x < 0) * (LCR_IMAGE_SIZE * LCR_IMAGE_SIZE);
return _LCR_currentImagePalette[_LCR_currentImage[x]]; return LCR_currentImage.palette[LCR_currentImage.image[x]];
} }
/** /**
@ -6184,8 +6185,8 @@ uint16_t LCR_sampleImage(int x, int y)
*/ */
uint16_t LCR_getNextImagePixel(void) uint16_t LCR_getNextImagePixel(void)
{ {
uint16_t r = _LCR_currentImagePalette[*_LCR_currentImagePixel]; uint16_t r = LCR_currentImage.palette[*LCR_currentImage.pixel];
_LCR_currentImagePixel++; LCR_currentImage.pixel++;
return r; return r;
} }

138
racing.h
View file

@ -16,27 +16,14 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_PHYSICS_UNIT 2048 ///< length of map square for physics engine #define LCR_PHYSICS_UNIT 2048 ///< length of map square for physics engine
#define TPE_RESHAPE_TENSION_LIMIT 10 #define TPE_RESHAPE_TENSION_LIMIT 10
#define TPE_RESHAPE_ITERATIONS 5 #define TPE_RESHAPE_ITERATIONS 5
#include "map.h" #include "map.h"
#include "tinyphysicsengine.h" #include "tinyphysicsengine.h"
// TODO: move some of this to constants? // TODO: move some of this to constants?
#if 0
#define LCR_GRAVITY (LCR_PHYSICS_UNIT / 100)
#define LCR_CAR_FORWARD_FRICTION (TPE_F / 11)
#define LCR_CAR_TURN_FRICTION (4 * TPE_F / 4)
#define LCR_CAR_ELASTICITY (TPE_F / 100)
#define LCR_CAR_ACCELERATION (LCR_PHYSICS_UNIT / 15)
#define LCR_CAR_TURN_SPEED (LCR_GAME_UNIT / 20)
#define LCR_CAR_TURN_MAX (LCR_GAME_UNIT / 4)
#else
#define LCR_GRAVITY (LCR_PHYSICS_UNIT / 140) #define LCR_GRAVITY (LCR_PHYSICS_UNIT / 140)
#define LCR_CAR_FORWARD_FRICTION (TPE_F / 9) #define LCR_CAR_FORWARD_FRICTION (TPE_F / 9)
#define LCR_CAR_TURN_FRICTION (TPE_F) #define LCR_CAR_TURN_FRICTION (TPE_F)
@ -45,8 +32,6 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_CAR_TURN_SPEED (LCR_GAME_UNIT / 18) #define LCR_CAR_TURN_SPEED (LCR_GAME_UNIT / 18)
#define LCR_CAR_TURN_MAX ((7 * LCR_GAME_UNIT) / 24) #define LCR_CAR_TURN_MAX ((7 * LCR_GAME_UNIT) / 24)
#endif
#define LCR_CAR_JOINTS 5 #define LCR_CAR_JOINTS 5
#define LCR_CAR_CONNECTIONS 10 #define LCR_CAR_CONNECTIONS 10
@ -339,52 +324,12 @@ if (LCR_currentMap.startPos[3])
(LCR_currentMap.startPos[3] == LCR_BLOCK_TRANSFORM_ROT_180 ? TPE_F / 2 : (LCR_currentMap.startPos[3] == LCR_BLOCK_TRANSFORM_ROT_180 ? TPE_F / 2 :
(TPE_F / 4)),0)); (TPE_F / 4)),0));
/*
const uint8_t *b = LCR_currentMap.blocks;
for (int i = 0; i < LCR_currentMap.blockCount; ++i)
{
if (*b == LCR_BLOCK_START)
{
uint8_t x, y, z;
LCR_LOG1("placing car to start position");
LCR_mapBlockGetCoords(b,&x,&y,&z);
TPE_bodyMoveTo(&(LCR_racing.carBody),
TPE_vec3(
(((TPE_Unit) x) - LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT +
LCR_PHYSICS_UNIT / 2,
(((TPE_Unit) y) - LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT / 2 +
LCR_PHYSICS_UNIT / 4,
(((TPE_Unit) z) - LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT +
LCR_PHYSICS_UNIT / 2
));
// resuse x:
x = LCR_mapBlockGetTransform(b) & 0x60;
if (x)
TPE_bodyRotateByAxis(&(LCR_racing.carBody),
TPE_vec3(0,x == LCR_BLOCK_TRANSFORM_ROT_90 ? TPE_F / 4 :
(x == LCR_BLOCK_TRANSFORM_ROT_180 ? TPE_F / 2 : (3 * TPE_F / 4)),0));
}
b += LCR_BLOCK_SIZE;
}
*/
for (int i = 0; i < LCR_CAR_JOINTS; ++i) for (int i = 0; i < LCR_CAR_JOINTS; ++i)
LCR_racing.carOKPositions[i] = TPE_vec3(0,0,0); LCR_racing.carOKPositions[i] = TPE_vec3(0,0,0);
LCR_racing.carNotOKCount = 0; LCR_racing.carNotOKCount = 0;
// TODO // TODO
} }
@ -676,13 +621,13 @@ LCR_racing.carPositions[0] =
if (angle < TPE_F / 4) // TODO: magic constant if (angle < TPE_F / 4) // TODO: magic constant
{ {
LCR_LOG2("car roof low, applying anti force") LCR_LOG2("roof squeezed, applying anti force")
tmpVec = TPE_vec3Times(carUp,LCR_PHYSICS_UNIT / 16); // TODO: 16 magic con. tmpVec = TPE_vec3Times(carUp,LCR_PHYSICS_UNIT / 16); // TODO: 16 magic con.
if (angle <= 0) if (angle <= 0)
{ {
LCR_LOG1("car roof flipped over, fixing") LCR_LOG1("roof flipped over, fixing")
LCR_racing.carBody.joints[4].position = wheelAverage; LCR_racing.carBody.joints[4].position = wheelAverage;
angle = 0; angle = 0;
} }
@ -700,75 +645,68 @@ LCR_racing.carPositions[0] =
} }
} }
// now try to decide if car shape is OK
TPE_Unit v1, v2;
angle = v1 = TPE_vec3Dot(
(TPE_vec3Dot( TPE_vec3Minus(
TPE_vec3Minus(
LCR_racing.carBody.joints[1].position, LCR_racing.carBody.joints[1].position,
LCR_racing.carBody.joints[0].position), LCR_racing.carBody.joints[0].position),
TPE_vec3Minus( TPE_vec3Minus(
LCR_racing.carBody.joints[2].position, LCR_racing.carBody.joints[3].position,
LCR_racing.carBody.joints[0].position)) + LCR_racing.carBody.joints[0].position));
TPE_vec3Dot(
v2 = TPE_vec3Dot(
TPE_vec3Minus(
LCR_racing.carBody.joints[1].position,
LCR_racing.carBody.joints[2].position),
TPE_vec3Minus( TPE_vec3Minus(
LCR_racing.carBody.joints[0].position, LCR_racing.carBody.joints[3].position,
LCR_racing.carBody.joints[3].position), LCR_racing.carBody.joints[2].position));
TPE_vec3Minus(
LCR_racing.carBody.joints[2].position,
LCR_racing.carBody.joints[3].position)));
int carOK = 1; // TODO: for checking if car shape is fine, if we're inside a wall etc. v1 = v1 > v2 ? ((v1 * LCR_GAME_UNIT) / TPE_nonZero(v2)) :
((v2 * LCR_GAME_UNIT) / TPE_nonZero(v1));
if (angle < 1900 || angle > 2350 || #define _TOLERANCE (LCR_GAME_UNIT / 8)
(LCR_racing.carBody.flags & TPE_BODY_FLAG_UNRESOLVED))
carOK = 0;
if ((LCR_racing.carBody.flags & TPE_BODY_FLAG_UNRESOLVED) ||
if (carOK) v1 < (LCR_GAME_UNIT - _TOLERANCE) ||
v1 > (LCR_GAME_UNIT + _TOLERANCE))
{ {
LCR_racing.carNotOKCount -= LCR_racing.carNotOKCount ? 1 : 0; // car not OK
for (int i = 0; i < LCR_CAR_JOINTS; ++i) if (LCR_racing.carNotOKCount > 8) // TODO: constant
LCR_racing.carOKPositions[i] = LCR_racing.carBody.joints[i].position;
}
else
{
if (LCR_racing.carNotOKCount > 5)
{ {
LCR_LOG1("car not OK, fixing"); LCR_LOG1("car not OK for some time, fixing");
for (int i = 0; i < LCR_CAR_JOINTS; ++i) for (int i = 0; i < LCR_CAR_JOINTS; ++i)
{ {
// iterate towards previous OK position
LCR_racing.carBody.joints[i].position = LCR_racing.carBody.joints[i].position =
TPE_vec3Plus( TPE_vec3Plus(LCR_racing.carBody.joints[i].position,
LCR_racing.carBody.joints[i].position,
LCR_racing.carOKPositions[i]); LCR_racing.carOKPositions[i]);
LCR_racing.carBody.joints[i].position.x /= 2; LCR_racing.carBody.joints[i].position.x /= 2;
LCR_racing.carBody.joints[i].position.y /= 2; LCR_racing.carBody.joints[i].position.y /= 2;
LCR_racing.carBody.joints[i].position.z /= 2; LCR_racing.carBody.joints[i].position.z /= 2;
for (int j = 0; j < 3; ++j) // lower speed a bit
LCR_racing.carBody.joints[i].velocity[j] =
(7 * ((int) LCR_racing.carBody.joints[i].velocity[j])) / 8;
//LCR_racing.carBody.joints[i].velocity[0] = 0;
//LCR_racing.carBody.joints[i].velocity[1] = 0;
//LCR_racing.carBody.joints[i].velocity[2] = 0;
} }
// TPE_bodyReshape(&(LCR_racing.carBody),
// LCR_racing.physicsWorld.environmentFunction);
// TODO
// LCR_racing.carNotOKCount = 0;
} }
else else
LCR_racing.carNotOKCount++; LCR_racing.carNotOKCount++;
} }
else
{
// car OK
LCR_racing.carNotOKCount -= LCR_racing.carNotOKCount ? 1 : 0;
for (int i = 0; i < LCR_CAR_JOINTS; ++i)
LCR_racing.carOKPositions[i] = LCR_racing.carBody.joints[i].position;
}
LCR_racing.tick++; LCR_racing.tick++;