Updated block system to use macros instead of hardcoding the block IDs everywhere
This commit is contained in:
15
src/blocks.h
Normal file
15
src/blocks.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#define BLOCK_AIR 0
|
||||
#define BLOCK_GRASS 1
|
||||
#define BLOCK_DIRT 2
|
||||
#define BLOCK_SAND 3
|
||||
#define BLOCK_STONE 4
|
||||
#define BLOCK_BRICKS 5
|
||||
#define BLOCK_GRAVEL 6
|
||||
#define BLOCK_WOOD 7
|
||||
#define BLOCK_LEAVES 8
|
||||
#define BLOCK_COBBLESTONE 9
|
||||
|
||||
#define BLOCK_PLAYER_HEAD 14
|
||||
#define BLOCK_PLAYER_BODY 15
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "gameloop.h"
|
||||
#include "textures.h"
|
||||
#include "utility.h"
|
||||
#include "blocks.h"
|
||||
#include "menus.h"
|
||||
#include "data.h"
|
||||
#include "gui.h"
|
||||
@@ -257,7 +258,7 @@ int gameLoop (
|
||||
);
|
||||
|
||||
// Can't break other players
|
||||
if (blockid != 14 && blockid != 15) {
|
||||
if (blockid != BLOCK_PLAYER_BODY && blockid != BLOCK_PLAYER_HEAD) {
|
||||
InvSlot pickedUp = {
|
||||
.blockid = blockid,
|
||||
.amount = 1,
|
||||
@@ -492,7 +493,7 @@ int gameLoop (
|
||||
goto chunkNull;
|
||||
}
|
||||
|
||||
if (intersectedBlock > 0) {
|
||||
if (intersectedBlock != BLOCK_AIR) {
|
||||
// I'm guessing this eldritch horror figures out what pixel of
|
||||
// the block we hit
|
||||
i6 = (int)((f34 + f36) * 16.0) & 0xF;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "gui.h"
|
||||
#include "blocks.h"
|
||||
|
||||
const int BUFFER_W = 214;
|
||||
const int BUFFER_H = 120;
|
||||
@@ -348,7 +349,7 @@ void dirtBg (SDL_Renderer *renderer) {
|
||||
color = textures [
|
||||
(x & 0xF) +
|
||||
(y & 0xF) * 16 +
|
||||
2 * 256 * 3
|
||||
BLOCK_DIRT * 256 * 3
|
||||
];
|
||||
|
||||
SDL_SetRenderDrawColor(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "terrain.h"
|
||||
#include "blocks.h"
|
||||
|
||||
/* World_sort
|
||||
* Sorts all chunks in a world by hash
|
||||
@@ -96,7 +97,7 @@ int World_setBlock (
|
||||
) {
|
||||
static int b;
|
||||
static Chunk *chunk;
|
||||
b = World_getBlock(world, x, y, z) < 1;
|
||||
b = World_getBlock(world, x, y, z) == BLOCK_AIR;
|
||||
|
||||
if (force || b) { // If the block was air or we don't care
|
||||
chunk = chunkLookup(world, x, y, z);
|
||||
@@ -150,7 +151,7 @@ int ch_setBlock (
|
||||
nmod(x, CHUNK_SIZE) +
|
||||
(nmod(y, CHUNK_SIZE) * CHUNK_SIZE) +
|
||||
(nmod(z, CHUNK_SIZE) * CHUNK_SIZE * CHUNK_SIZE)
|
||||
] > 0;
|
||||
] != BLOCK_AIR;
|
||||
blocks[x + (y * CHUNK_SIZE) + (z * CHUNK_SIZE * CHUNK_SIZE)] = block;
|
||||
return notAir;
|
||||
}
|
||||
@@ -212,10 +213,10 @@ void genStructure (
|
||||
switch (type) {
|
||||
case 0: // tree
|
||||
for (int trunk = randm(2) + 4; trunk > 0; trunk --) {
|
||||
World_setBlock(world, x, y --, z, 7, 1);
|
||||
World_setBlock(world, x, y --, z, BLOCK_WOOD, 1);
|
||||
}
|
||||
setCube(world, x - 2, y + 1, z - 2, 5, 2, 5, 8, 0);
|
||||
setCube(world, x - 1, y - 1, z - 1, 3, 2, 3, 8, 0);
|
||||
setCube(world, x - 2, y + 1, z - 2, 5, 2, 5, BLOCK_LEAVES, 0);
|
||||
setCube(world, x - 1, y - 1, z - 1, 3, 2, 3, BLOCK_LEAVES, 0);
|
||||
break;
|
||||
|
||||
case 1: // pyramid
|
||||
@@ -228,7 +229,7 @@ void genStructure (
|
||||
y ++,
|
||||
z - step / 2,
|
||||
step, 1, step,
|
||||
5, 1
|
||||
BLOCK_BRICKS, 1
|
||||
);
|
||||
}
|
||||
break;
|
||||
@@ -272,7 +273,7 @@ int genChunk (
|
||||
distMax = 0;
|
||||
distMaxI = 0;
|
||||
for (i = 0; i < CHUNKARR_SIZE; i ++) {
|
||||
int dist = sqrt(
|
||||
int dist = sqrt (
|
||||
pow(coords.x - world->chunk[i].center.x, 2) +
|
||||
pow(coords.y - world->chunk[i].center.y, 2) +
|
||||
pow(coords.z - world->chunk[i].center.z, 2)
|
||||
@@ -373,7 +374,10 @@ void ch_genClassic (Block *blocks, int yOffset) {
|
||||
for (int z = 0; z < CHUNK_SIZE; z ++)
|
||||
if (y + yOffset > 32) {
|
||||
Block block = randm(2) == 0 ? randm(9) : 0;
|
||||
if (block == 3 || block == 6) { block = 2; }
|
||||
if (
|
||||
block == BLOCK_SAND ||
|
||||
block == BLOCK_GRAVEL
|
||||
) { block = BLOCK_DIRT; }
|
||||
ch_setBlock(blocks, x, y, z, block);
|
||||
}
|
||||
}
|
||||
@@ -410,13 +414,13 @@ void ch_genNew (
|
||||
for (int y = 0; y < CHUNK_SIZE; y ++)
|
||||
for (int z = 0; z < CHUNK_SIZE; z ++) {
|
||||
if (y + yOffset > heightmap[x][z] + 4)
|
||||
ch_setBlock(blocks, x, y, z, 4);
|
||||
ch_setBlock(blocks, x, y, z, BLOCK_STONE);
|
||||
else if (y + yOffset > heightmap[x][z])
|
||||
ch_setBlock(blocks, x, y, z, 2);
|
||||
ch_setBlock(blocks, x, y, z, BLOCK_DIRT);
|
||||
else if (y + yOffset == heightmap[x][z])
|
||||
ch_setBlock(blocks, x, y, z, 1);
|
||||
ch_setBlock(blocks, x, y, z, BLOCK_GRASS);
|
||||
else
|
||||
ch_setBlock(blocks, x, y, z, 0);
|
||||
ch_setBlock(blocks, x, y, z, BLOCK_AIR);
|
||||
}
|
||||
|
||||
// Generate caves
|
||||
@@ -450,16 +454,21 @@ void ch_genNew (
|
||||
int highPoint = 64 - elevation - height;
|
||||
|
||||
for (int y = highPoint; y < lowPoint; y ++) {
|
||||
ch_setBlock(blocks, x, y, z, 0);
|
||||
ch_setBlock(blocks, x, y, z, BLOCK_AIR);
|
||||
}
|
||||
|
||||
// Don't have bare dirt on the bottom
|
||||
if (ch_getBlock(blocks, x, lowPoint, z) == 2) {
|
||||
// What block we place down depends on the block above
|
||||
if (ch_getBlock(blocks, x, highPoint - 1, z) == 0) {
|
||||
ch_setBlock(blocks, x, lowPoint, z, 1);
|
||||
if (
|
||||
ch_getBlock(blocks, x, highPoint - 1, z) ==
|
||||
BLOCK_AIR
|
||||
) {
|
||||
ch_setBlock (blocks, x, lowPoint, z,
|
||||
BLOCK_GRASS);
|
||||
} else {
|
||||
ch_setBlock(blocks, x, lowPoint, z, 6);
|
||||
ch_setBlock (blocks, x, lowPoint, z,
|
||||
BLOCK_GRAVEL);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -495,9 +504,9 @@ void ch_genStone (Block *blocks, int yOffset) {
|
||||
for (int y = 0; y < CHUNK_SIZE; y ++)
|
||||
for (int z = 0; z < CHUNK_SIZE; z ++)
|
||||
if (y + yOffset > 32) {
|
||||
ch_setBlock(blocks, x, y, z, 4);
|
||||
ch_setBlock(blocks, x, y, z, BLOCK_STONE);
|
||||
} else {
|
||||
ch_setBlock(blocks, x, y, z, 0);
|
||||
ch_setBlock(blocks, x, y, z, BLOCK_AIR);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -505,9 +514,15 @@ void ch_genFlat (Block *blocks, int yOffset) {
|
||||
for (int x = 0; x < CHUNK_SIZE; x ++)
|
||||
for (int z = 0; z < CHUNK_SIZE; z ++)
|
||||
for (int y = 0; y < CHUNK_SIZE; y ++) {
|
||||
if (y + yOffset < 32) { ch_setBlock(blocks, x, y, z, 0); }
|
||||
if (y + yOffset == 32) { ch_setBlock(blocks, x, y, z, 1); }
|
||||
if (y + yOffset > 32) { ch_setBlock(blocks, x, y, z, 2); }
|
||||
if (y + yOffset < 32) {
|
||||
ch_setBlock(blocks, x, y, z, BLOCK_AIR);
|
||||
}
|
||||
if (y + yOffset == 32) {
|
||||
ch_setBlock(blocks, x, y, z, BLOCK_GRASS);
|
||||
}
|
||||
if (y + yOffset > 32) {
|
||||
ch_setBlock(blocks, x, y, z, BLOCK_DIRT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -520,7 +535,7 @@ void ch_genDev (Block *blocks, int xOffset, int yOffset, int zOffset) {
|
||||
|
||||
for (int x = 0; x < CHUNK_SIZE; x ++)
|
||||
for (int z = 0; z < CHUNK_SIZE; z ++) {
|
||||
ch_setBlock(blocks, x, 3, z, 14);
|
||||
ch_setBlock(blocks, x, 4, z, 15);
|
||||
ch_setBlock(blocks, x, 3, z, BLOCK_PLAYER_BODY);
|
||||
ch_setBlock(blocks, x, 4, z, BLOCK_PLAYER_HEAD);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "textures.h"
|
||||
#include "blocks.h"
|
||||
|
||||
int textures[12288] = {0};
|
||||
const u_int16_t cobbleCracks[16] = {
|
||||
@@ -40,42 +41,36 @@ void genTexture (int blockId) {
|
||||
int noiseFloor = 255;
|
||||
int noiseScale = 96;
|
||||
|
||||
// sand
|
||||
if (blockId == 3) {
|
||||
if (blockId == BLOCK_SAND) {
|
||||
baseColor = 0xd8ce9b;
|
||||
noiseScale = 48;
|
||||
}
|
||||
|
||||
// stone
|
||||
if (blockId == 4)
|
||||
if (blockId == BLOCK_STONE)
|
||||
baseColor = 8355711;
|
||||
|
||||
// gravel
|
||||
if (blockId == 6) {
|
||||
if (blockId == BLOCK_GRAVEL) {
|
||||
baseColor = 0xAAAAAA;
|
||||
noiseScale = 140;
|
||||
}
|
||||
|
||||
// add noise
|
||||
if (blockId != 4 || randm(3) == 0)
|
||||
if (blockId != BLOCK_STONE || randm(3) == 0)
|
||||
k = noiseFloor - randm(noiseScale);
|
||||
|
||||
// grass
|
||||
if (
|
||||
blockId == 1 &&
|
||||
blockId == BLOCK_GRASS &&
|
||||
y < (x * x * (3 + x) * 81 >> 2 & 0x3) + 18
|
||||
) {
|
||||
baseColor = 6990400;
|
||||
} else if (
|
||||
blockId == 1 &&
|
||||
blockId == BLOCK_GRASS &&
|
||||
y < (x * x * (3 + x) * 81 >> 2 & 0x3) + 19
|
||||
) {
|
||||
k = k * 2 / 3;
|
||||
}
|
||||
|
||||
|
||||
// logs
|
||||
if (blockId == 7) {
|
||||
if (blockId == BLOCK_WOOD) {
|
||||
baseColor = 6771249;
|
||||
if (
|
||||
x > 0 && x < 15 &&
|
||||
@@ -95,21 +90,19 @@ void genTexture (int blockId) {
|
||||
}
|
||||
}
|
||||
|
||||
// bricks
|
||||
if (blockId == 5) {
|
||||
switch (blockId) {
|
||||
case BLOCK_BRICKS:
|
||||
baseColor = 11876885;
|
||||
if ((x + y / 4 * 4) % 8 == 0 || y % 4 == 0)
|
||||
baseColor = 12365733;
|
||||
}
|
||||
|
||||
// cobblestone
|
||||
if (blockId == 9) {
|
||||
break;
|
||||
|
||||
case BLOCK_COBBLESTONE:
|
||||
baseColor = 0x999999;
|
||||
k -= ((cobbleCracks[y & 0xF] >> x) & 0b1) * 128;
|
||||
}
|
||||
|
||||
// Character head
|
||||
if (blockId == 14) {
|
||||
break;
|
||||
|
||||
case BLOCK_PLAYER_HEAD:
|
||||
k = 255;
|
||||
if (
|
||||
dist2d(x, 8, y % 16, 8) > 6.2 ||
|
||||
@@ -120,10 +113,9 @@ void genTexture (int blockId) {
|
||||
baseColor = 0xFFFFFF;
|
||||
k -= dist2d(x, 8, y % 16, 2) * 8;
|
||||
}
|
||||
}
|
||||
|
||||
// Character body
|
||||
if (blockId == 15) {
|
||||
break;
|
||||
|
||||
case BLOCK_PLAYER_BODY:
|
||||
k = 255;
|
||||
if (
|
||||
(dist2d(x, 8, y % 16, 16) > 12.2 ||
|
||||
@@ -135,12 +127,13 @@ void genTexture (int blockId) {
|
||||
baseColor = 0xFFFFFF;
|
||||
k -= dist2d(x, 8, y % 16, 2) * 8;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
int i2 = k;
|
||||
if (y >= 32)
|
||||
i2 /= 2;
|
||||
if (blockId == 8) {
|
||||
if (blockId == BLOCK_LEAVES) {
|
||||
baseColor = 5298487;
|
||||
if (randm(2) == 0) {
|
||||
baseColor = 0;
|
||||
|
||||
Reference in New Issue
Block a user