Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blocks attributes refactor #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/gameLayer/gameplay/ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ inline void AnimalBehaviour<E, SETTINGS>::updateAnimalBehaviour(float deltaTime,
auto blockPos = fromBlockPosToBlockPosInChunk(glm::ivec3(pos));
auto b = c->safeGet(blockPos);

if (b && b->isColidable())
if (b && b->isCollidable())
{
blockPos.y++;
auto b = c->safeGet(blockPos);
if (b && b->isColidable())
if (b && b->isCollidable())
{
//wall
direction = {};
Expand Down Expand Up @@ -139,7 +139,7 @@ inline void AnimalBehaviour<E, SETTINGS>::updateAnimalBehaviour(float deltaTime,
direction = {};
waitTime = 0;
}
else if (!b->isColidable())
else if (!b->isCollidable())
{

if (glm::dot(baseEntity->entity.lookDirectionAnimation, {0,-0.5,-1}) < 0.7)
Expand All @@ -154,7 +154,7 @@ inline void AnimalBehaviour<E, SETTINGS>::updateAnimalBehaviour(float deltaTime,
blockPos.y--;
auto b = c->safeGet(blockPos);

if (!b || !b->isColidable())
if (!b || !b->isCollidable())
{
direction = {};
waitTime = 0;
Expand Down
64 changes: 20 additions & 44 deletions shared/blocks.cpp
Original file line number Diff line number Diff line change
@@ -1,71 +1,47 @@
#include "blocks.h"
#include <algorithm>

bool isBlockMesh(BlockType type)
bool isOpaque(BlockType type)
{
return !isCrossMesh(type);
return (blockProperties[type] & Mask::IS_OPAQUE_MASK) != 0;
}

bool isCrossMesh(BlockType type)
bool isBlockMesh(BlockType type)
{
return isGrassMesh(type);
return !isCrossMesh(type); // dirty hack for now?
}

bool isControlBlock(BlockType type)
bool isTransparentGeometry(BlockType type)
{
return
type == BlockTypes::control1 ||
type == BlockTypes::control2 ||
type == BlockTypes::control3 ||
type == BlockTypes::control4;
return (blockProperties[type] & Mask::IS_TRANSPARENT_MASK) != 0;
}

bool isOpaque(BlockType type)
bool isLightEmitter(BlockType type)
{
//todo all leaves ?
return (blockProperties[type] & Mask::IS_LIGHT_EMITTER_MASK) != 0;
}

return
type != BlockTypes::air
&& type != BlockTypes::leaves
&& type != BlockTypes::jungle_leaves
&& type != BlockTypes::palm_leaves
&& type != BlockTypes::birch_leaves
&& type != BlockTypes::spruce_leaves
&& type != BlockTypes::spruce_leaves_red
&& type != BlockTypes::glowstone
&& type != BlockTypes::torch
&& !(isTransparentGeometry(type))
&& !(isGrassMesh(type));
bool isAnimatedBlock(BlockType type)
{
return (blockProperties[type] & Mask::IS_ANIMATED_MASK) != 0;
}

bool isLightEmitor(BlockType type)
bool isGrassMesh(BlockType type)
{
return type == BlockTypes::glowstone
|| type == BlockTypes::torch;
return (blockProperties[type] & Mask::IS_GRASS_MESH_MASK) != 0;
}

bool isTransparentGeometry(BlockType type)
bool isCrossMesh(BlockType type)
{
return type == BlockTypes::ice || type == BlockTypes::water || type == BlockTypes::glass;
return (blockProperties[type] & Mask::IS_CROSS_MESH_MASK) != 0;
}

bool isGrassMesh(BlockType type)
bool isCollidable(BlockType type)
{
return type == BlockTypes::grass
|| type == BlockTypes::rose
|| type == BlockTypes::cactus_bud
|| type == BlockTypes::dead_bush
;
return (blockProperties[type] & Mask::IS_COLLIDABLE_MASK) != 0;
}

bool isColidable(BlockType type)
bool isControlBlock(BlockType type)
{
return
type != BlockTypes::air &&
type != BlockTypes::grass &&
type != BlockTypes::rose &&
type != BlockTypes::cactus_bud &&
type != BlockTypes::dead_bush &&
type != BlockTypes::torch &&
type != BlockTypes::water;
return (blockProperties[type] & Mask::IS_CONTROL_BLOCK_MASK) != 0;
}
84 changes: 73 additions & 11 deletions shared/blocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
#include <glad/glad.h>
#include <glm/vec3.hpp>

const struct Mask {
static constexpr uint32_t IS_OPAQUE_MASK = 1 << 0;
static constexpr uint32_t IS_TRANSPARENT_MASK = 1 << 1;
static constexpr uint32_t IS_LIGHT_EMITTER_MASK = 1 << 2;
static constexpr uint32_t IS_ANIMATED_MASK = 1 << 3;
static constexpr uint32_t IS_GRASS_MESH_MASK = 1 << 4;
static constexpr uint32_t IS_CROSS_MESH_MASK = 1 << 5;
static constexpr uint32_t IS_COLLIDABLE_MASK = 1 << 6;
static constexpr uint32_t IS_CONTROL_BLOCK_MASK = 1 << 7;
};


enum BlockTypes
{
air = 0,
Expand Down Expand Up @@ -60,6 +72,59 @@ enum BlockTypes
BlocksCount
};

const uint32_t blockProperties[BlocksCount] = {
NULL, // air
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // grassBlock
Mask::IS_COLLIDABLE_MASK, // dirt
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // stone
Mask::IS_TRANSPARENT_MASK | Mask::IS_COLLIDABLE_MASK,// ice
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // woodLog
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // wooden_plank
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // cobblestone
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // gold_block
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // bricks
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // sand
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // sand_stone
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // snow_dirt
Mask::IS_ANIMATED_MASK | Mask::IS_COLLIDABLE_MASK,// leaves
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // gold_ore
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // coal_ore
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // stoneBrick
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // iron_ore
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // diamond_ore
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // bookShelf
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // birch_wood
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // gravel
Mask::IS_GRASS_MESH_MASK, // grass
Mask::IS_GRASS_MESH_MASK, // rose
Mask::IS_TRANSPARENT_MASK, // water
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // jungle_log
Mask::IS_ANIMATED_MASK | Mask::IS_COLLIDABLE_MASK,// jungle_leaves
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // palm_log
Mask::IS_ANIMATED_MASK | Mask::IS_COLLIDABLE_MASK,// palm_leaves
Mask::IS_GRASS_MESH_MASK, // cactus_bud
Mask::IS_GRASS_MESH_MASK, // dead_bush
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // jungle_planks
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // clay
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // hardened_clay
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // mud
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // packed_mud
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // mud_bricks
Mask::IS_CONTROL_BLOCK_MASK, // control1
Mask::IS_CONTROL_BLOCK_MASK, // control2
Mask::IS_CONTROL_BLOCK_MASK, // control3
Mask::IS_CONTROL_BLOCK_MASK, // control4
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // snow_block
Mask::IS_ANIMATED_MASK | Mask::IS_COLLIDABLE_MASK, // birch_leaves
Mask::IS_OPAQUE_MASK | Mask::IS_COLLIDABLE_MASK, // spruce_log
Mask::IS_ANIMATED_MASK | Mask::IS_COLLIDABLE_MASK, // spruce_leaves
Mask::IS_ANIMATED_MASK | Mask::IS_COLLIDABLE_MASK, // spruce_leaves_red
Mask::IS_LIGHT_EMITTER_MASK | Mask::IS_COLLIDABLE_MASK, // glowstone
Mask::IS_TRANSPARENT_MASK | Mask::IS_COLLIDABLE_MASK, // glass
NULL, // testBlock (this is not used anywhere?)
Mask::IS_LIGHT_EMITTER_MASK // torch
};

using BlockType = uint16_t;

bool isBlockMesh(BlockType type);
Expand All @@ -70,13 +135,13 @@ bool isControlBlock(BlockType type);

bool isOpaque(BlockType type);

bool isLightEmitor(BlockType type);
bool isLightEmitter(BlockType type);

bool isTransparentGeometry(BlockType type);

bool isGrassMesh(BlockType type);

bool isColidable(BlockType type);
bool isCollidable(BlockType type);

struct Block
{
Expand All @@ -90,13 +155,10 @@ struct Block
return ::isOpaque(type);
}

//rename is animated leaves
//rename is animated leaves (why?)
bool isAnimatedBlock()
{
return
type == BlockTypes::leaves || type == BlockTypes::jungle_leaves
|| type == BlockTypes::palm_leaves || type == BlockTypes::birch_leaves
|| type == BlockTypes::spruce_leaves || type == BlockTypes::spruce_leaves_red;
return (blockProperties[type] & Mask::IS_ANIMATED_MASK) != 0;
}

bool isTransparentGeometry()
Expand All @@ -109,9 +171,9 @@ struct Block
return ::isGrassMesh(type);
}

bool isLightEmitor()
bool isLightEmitter()
{
return ::isLightEmitor(type);
return ::isLightEmitter(type);
}

unsigned char getSkyLight()
Expand Down Expand Up @@ -149,9 +211,9 @@ struct Block
return ::isCrossMesh(type);
}

bool isColidable()
bool isCollidable()
{
return ::isColidable(type);
return ::isCollidable(type);
}

};
Expand Down
6 changes: 3 additions & 3 deletions src/gameLayer/chunkSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void ChunkSystem::update(glm::ivec3 playerBlockPosition, float deltaTime, UndoQu

auto &b = i->unsafeGet(x, y, z);

if (isLightEmitor(b.type))
if (isLightEmitter(b.type))
{
lightSystem.addLight(*this,
{i->data.x * CHUNK_SIZE + x, y, i->data.z * CHUNK_SIZE + z},
Expand Down Expand Up @@ -994,12 +994,12 @@ void ChunkSystem::changeBlockLightStuff(glm::ivec3 pos, int currentSkyLightLevel

}

if (isLightEmitor(oldType) && !isLightEmitor(newType))
if (isLightEmitter(oldType) && !isLightEmitter(newType))
{
//remove light
lightSystem.removeLight(*this, pos, currentNormalLightLevel);
}
else if (!isLightEmitor(oldType) && isLightEmitor(newType))
else if (!isLightEmitter(oldType) && isLightEmitter(newType))
{
lightSystem.addLight(*this, pos, 15);
}
Expand Down
2 changes: 1 addition & 1 deletion src/gameLayer/gameplay/physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ glm::dvec3 performCollision(glm::dvec3 pos, glm::dvec3 lastPos, glm::vec3 size,
}
//todo remove later and use unsafe get

if (b->isColidable())
if (b->isCollidable())
{


Expand Down
14 changes: 7 additions & 7 deletions src/gameLayer/gameplay/zombie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ bool ZombieServer::update(float deltaTime, decltype(chunkGetterSignature) *chunk
{
blockPos.y += 1;
auto b = serverChunkStorer.getBlockSafe(blockPos);
if (b && b->isColidable())
if (b && b->isCollidable())
{
problems = true;
return true;
Expand Down Expand Up @@ -222,7 +222,7 @@ bool ZombieServer::update(float deltaTime, decltype(chunkGetterSignature) *chunk

//blockPos.y -= 2;
//auto b2 = serverChunkStorer.getBlockSafe(blockPos);
//if (!b2 || !b2->isColidable())
//if (!b2 || !b2->isCollidable())
//{
// problems = true;
// break;
Expand Down Expand Up @@ -324,15 +324,15 @@ bool ZombieServer::update(float deltaTime, decltype(chunkGetterSignature) *chunk

auto b = serverChunkStorer.getBlockSafe(from3DPointToBlock(blockPos));

if (b && b->isColidable())
if (b && b->isCollidable())
{

//don't jump too tall walls lol
auto b = serverChunkStorer.getBlockSafe(from3DPointToBlock(blockPos) + glm::ivec3(0,1,0));
if (!b || !b->isColidable())
if (!b || !b->isCollidable())
{
auto b = serverChunkStorer.getBlockSafe(from3DPointToBlock(blockPos) + glm::ivec3(0, 2, 0));
if (!b || !b->isColidable())
if (!b || !b->isCollidable())
{
entity.forces.jump();
}
Expand All @@ -359,14 +359,14 @@ bool ZombieServer::update(float deltaTime, decltype(chunkGetterSignature) *chunk
direction = {};
waitTime = 0;
}
else if (!b->isColidable())
else if (!b->isCollidable())
{

//bigger fall under?
blockPos.y--;
auto b = serverChunkStorer.getBlockSafe(from3DPointToBlock(blockPos));

if (!b || !b->isColidable())
if (!b || !b->isCollidable())
{
direction = {};
waitTime = 0;
Expand Down
Loading