From 4bc9842e4eb3af8f10d2593f0d1567c4f7110dd1 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 27 Aug 2022 17:07:48 +1000 Subject: [PATCH] Simply block define api again by avoiding need to call Block_SetCollide/Block_RecalculateBB --- src/Block.c | 46 +++++++++++++++++++++++++++------------------- src/Block.h | 34 ++++++++++++++-------------------- src/Entity.c | 2 +- src/Formats.c | 4 ++-- src/Protocol.c | 7 +++---- 5 files changed, 47 insertions(+), 46 deletions(-) diff --git a/src/Block.c b/src/Block.c index 61b5f56bb..d29805acb 100644 --- a/src/Block.c +++ b/src/Block.c @@ -93,7 +93,7 @@ static const struct SimpleBlockDef core_blockDefs[] = { { "Mossy rocks", 36, 36, 36, 16, FOG_NONE , 0, false, true, 100, DRAW_OPAQUE, COLLIDE_SOLID, SOUND_STONE, SOUND_STONE }, { "Obsidian", 37, 37, 37, 16, FOG_NONE , 0, false, true, 100, DRAW_OPAQUE, COLLIDE_SOLID, SOUND_STONE, SOUND_STONE }, { "Cobblestone slab", 16, 16, 16, 8, FOG_NONE , 0, false, true, 100, DRAW_OPAQUE, COLLIDE_SOLID, SOUND_STONE, SOUND_STONE }, -{ "Rope", 11, 11, 11, 16, FOG_NONE , 0, false, false, 100, DRAW_SPRITE, COLLIDE_NONE, SOUND_CLOTH, SOUND_CLOTH }, +{ "Rope", 11, 11, 11, 16, FOG_NONE , 0, false, false, 100, DRAW_SPRITE, COLLIDE_CLIMB, SOUND_CLOTH, SOUND_CLOTH }, { "Sandstone", 25, 41, 57, 16, FOG_NONE , 0, false, true, 100, DRAW_OPAQUE, COLLIDE_SOLID, SOUND_STONE, SOUND_STONE }, { "Snow", 50, 50, 50, 4, FOG_NONE , 0, false, true, 100, DRAW_OPAQUE, COLLIDE_NONE, SOUND_SNOW, SOUND_SNOW }, { "Fire", 38, 38, 38, 16, FOG_NONE , 0, true, false, 100, DRAW_SPRITE, COLLIDE_NONE, SOUND_WOOD, SOUND_NONE }, @@ -113,9 +113,9 @@ static const struct SimpleBlockDef core_blockDefs[] = { /*NAME TOP SID BOT HEI FOG_COLOR DENS FULL BLOCKS GRAV DRAW_MODE COLLIDE_MODE DIG_SOUND STEP_SOUND */ }; -/* Returns a backwards compatible collide type of a block. */ +/* Returns a backwards compatible collide type of a block */ static cc_uint8 DefaultSet_MapOldCollide(BlockID b, cc_uint8 collide) { - if (b == BLOCK_ROPE && collide == COLLIDE_NONE) return COLLIDE_CLIMB_ROPE; + if (b == BLOCK_ROPE && collide == COLLIDE_NONE) return COLLIDE_CLIMB; if (b == BLOCK_ICE && collide == COLLIDE_SOLID) return COLLIDE_ICE; if ((b == BLOCK_WATER || b == BLOCK_STILL_WATER) && collide == COLLIDE_LIQUID) @@ -136,9 +136,8 @@ static void Block_RecalcIsLiquid(BlockID b) { (collide == COLLIDE_LAVA && Blocks.Draw[b] == DRAW_TRANSPARENT); } -void Block_SetCollide(BlockID block, cc_uint8 collide) { - /* necessary if servers redefined core blocks, before extended collide types were added */ - collide = DefaultSet_MapOldCollide(block, collide); +/* Sets the basic and extended collide types of the given block */ +static void Block_SetCollide(BlockID block, cc_uint8 collide) { Blocks.ExtendedCollide[block] = collide; Block_RecalcIsLiquid(block); @@ -212,15 +211,6 @@ static void Block_CalcLightOffset(BlockID block) { Blocks.LightOffset[block] = flags; } -/* Recalculates bounding boxes of all sprite blocks */ -static void Block_RecalculateAllSpriteBB(void) { - int block; - for (block = BLOCK_AIR; block < BLOCK_COUNT; block++) { - if (Blocks.Draw[block] != DRAW_SPRITE) continue; - - Block_RecalculateBB((BlockID)block); - } -} static float GetSpriteBB_MinX(int size, int tileX, int tileY, const struct Bitmap* bmp) { BitmapCol* row; @@ -274,7 +264,8 @@ static float GetSpriteBB_MaxY(int size, int tileX, int tileY, const struct Bitma return 0.0f; } -void Block_RecalculateBB(BlockID block) { +/* Recalculates bounding box of the given sprite block */ +static void Block_RecalculateBB(BlockID block) { struct Bitmap* bmp = &Atlas2D.Bmp; int tileSize = Atlas2D.TileSize; TextureLoc texLoc = Block_Tex(block, FACE_XMAX); @@ -298,6 +289,16 @@ void Block_RecalculateBB(BlockID block) { Block_CalcRenderBounds(block); } +/* Recalculates bounding boxes of all sprite blocks */ +static void Block_RecalculateAllSpriteBB(void) { + int block; + for (block = BLOCK_AIR; block < BLOCK_COUNT; block++) { + if (Blocks.Draw[block] != DRAW_SPRITE) continue; + + Block_RecalculateBB((BlockID)block); + } +} + static void Block_CalcStretch(BlockID block) { /* faces which can be stretched on X axis */ @@ -420,11 +421,14 @@ static void Block_SetCustomDefined(BlockID block, cc_bool defined) { } } -void Block_DefineCustom(BlockID block) { - PackedCol black = PackedCol_Make(0, 0, 0, 255); - cc_string name = Block_UNSAFE_GetName(block); +void Block_DefineCustom(BlockID block, cc_bool checkSprite) { + PackedCol black = PackedCol_Make(0, 0, 0, 255); + cc_string name = Block_UNSAFE_GetName(block); + /* necessary if servers redefined core blocks, before extended collide types were added */ + cc_uint8 collide = DefaultSet_MapOldCollide(block, Blocks.Collide[block]); Blocks.Tinted[block] = Blocks.FogCol[block] != black && String_IndexOf(&name, '#') >= 0; + Block_SetCollide(block, collide); Block_SetDrawType(block, Blocks.Draw[block]); Block_CalcRenderBounds(block); Block_UpdateCulling(block); @@ -433,6 +437,10 @@ void Block_DefineCustom(BlockID block) { Inventory_AddDefault(block); Block_SetCustomDefined(block, true); Event_RaiseVoid(&BlockEvents.BlockDefChanged); + + if (!checkSprite) return; /* TODO eliminate this */ + /* Update sprite BoundingBox if necessary */ + if (Blocks.Draw[block] == DRAW_SPRITE) Block_RecalculateBB(block); } void Block_UndefineCustom(BlockID block) { diff --git a/src/Block.h b/src/Block.h index f0f733894..d30316d70 100644 --- a/src/Block.h +++ b/src/Block.h @@ -37,7 +37,7 @@ enum CollideType { COLLIDE_SLIPPERY_ICE, /* Block is solid and fully slidable on. */ COLLIDE_WATER, /* Water style 'swimming'/'bobbing' interaction when player collides. */ COLLIDE_LAVA, /* Lava style 'swimming'/'bobbing' interaction when player collides. */ - COLLIDE_CLIMB_ROPE /* Rope/Ladder style climbing interaction when player collides. */ + COLLIDE_CLIMB /* Rope/Ladder style climbing interaction when player collides. */ }; CC_VAR extern struct _BlockLists { @@ -113,44 +113,38 @@ if (Blocks.Tinted[block]) col = PackedCol_Tint(col, Blocks.FogCol[block]); /* and comparing whether the block directly behind them is in shadow or not */ #define LIGHT_FLAG_SHADES_FROM_BELOW 6 -/* Returns whether the given block has been changed from default. */ +/* Returns whether the given block has been changed from default */ cc_bool Block_IsCustomDefined(BlockID block); /* Updates state and raises event after the given block has been defined */ -void Block_DefineCustom(BlockID block); +void Block_DefineCustom(BlockID block, cc_bool checkSprite); /* Resets the given block to default */ void Block_UndefineCustom(BlockID block); - -/* Sets the basic and extended collide types of the given block. */ -void Block_SetCollide(BlockID block, cc_uint8 collide); -/* Resets all the properties of the given block to default. */ +/* Resets all the properties of the given block to default */ void Block_ResetProps(BlockID block); -/* Gets the name of the given block. */ -/* NOTE: Name points directly within underlying buffer, you MUST NOT persist this string. */ +/* Gets the name of the given block */ +/* NOTE: Name points directly within underlying buffer, you MUST NOT persist this string */ CC_API STRING_REF cc_string Block_UNSAFE_GetName(BlockID block); /* Sets the name of the given block. */ void Block_SetName(BlockID block, const cc_string* name); -/* Finds the ID of the block whose name caselessly matches given name. */ +/* Finds the ID of the block whose name caselessly matches given name */ CC_API int Block_FindID(const cc_string* name); -/* Attempts to parse given name as a numerical block ID. */ -/* Falls back to Block_FindID if this fails. */ +/* Attempts to parse given name as a numerical block ID */ +/* Falls back to Block_FindID if this fails */ CC_API int Block_Parse(const cc_string* name); -/* Recalculates bounding box of the given sprite block. */ -void Block_RecalculateBB(BlockID block); - -/* Sets the textures of the side faces of the given block. */ +/* Sets the textures of the side faces of the given block */ void Block_SetSide(TextureLoc texLoc, BlockID blockId); -/* The texture for the given face of the given block. */ +/* The texture for the given face of the given block */ #define Block_Tex(block, face) Blocks.Textures[(block) * FACE_COUNT + (face)] /* Whether the given face of this block is occluded/hidden */ #define Block_IsFaceHidden(block, other, face) (Blocks.Hidden[((block) * BLOCK_COUNT) + (other)] & (1 << (face))) -/* Whether blocks can be automatically rotated. */ +/* Whether blocks can be automatically rotated */ extern cc_bool AutoRotate_Enabled; -/* Attempts to find the rotated block based on the user's orientation and offset on selected block. */ -/* If no rotated block is found, returns given block. */ +/* Attempts to find the rotated block based on the user's orientation and offset on selected block */ +/* If no rotated block is found, returns given block */ BlockID AutoRotate_RotateBlock(BlockID block); /* Returns non 0 if both blocks belong to the same autorotate group */ cc_bool AutoRotate_BlocksShareGroup(BlockID block, BlockID blockOther); diff --git a/src/Entity.c b/src/Entity.c index 00e0ce514..54d19feb3 100644 --- a/src/Entity.c +++ b/src/Entity.c @@ -194,7 +194,7 @@ cc_bool Entity_TouchesAny(struct AABB* bounds, Entity_TouchesCondition condition return false; } -static cc_bool IsRopeCollide(BlockID b) { return Blocks.ExtendedCollide[b] == COLLIDE_CLIMB_ROPE; } +static cc_bool IsRopeCollide(BlockID b) { return Blocks.ExtendedCollide[b] == COLLIDE_CLIMB; } cc_bool Entity_TouchesAnyRope(struct Entity* e) { struct AABB bounds; Entity_GetBounds(e, &bounds); bounds.Max.Y += 0.5f / 16.0f; diff --git a/src/Formats.c b/src/Formats.c index 3b98efde0..05a9fb8fe 100644 --- a/src/Formats.c +++ b/src/Formats.c @@ -714,7 +714,7 @@ static void Cw_Callback_4(struct NbtTag* tag) { Blocks.SpriteOffset[id] = 0; } - Block_DefineCustom(id); + Block_DefineCustom(id, false); Blocks.CanPlace[id] = true; Blocks.CanDelete[id] = true; Event_RaiseVoid(&BlockEvents.PermissionsChanged); @@ -740,7 +740,7 @@ static void Cw_Callback_5(struct NbtTag* tag) { if (IsTag(tag->parent->parent, "BlockDefinitions") && Game_AllowCustomBlocks) { if (IsTag(tag, "ID")) { cw_curID = NbtTag_U8(tag); return; } if (IsTag(tag, "ID2")) { cw_curID = NbtTag_U16(tag); return; } - if (IsTag(tag, "CollideType")) { Block_SetCollide(id, NbtTag_U8(tag)); return; } + if (IsTag(tag, "CollideType")) { Blocks.Collide[id] = NbtTag_U8(tag); return; } if (IsTag(tag, "Speed")) { Blocks.SpeedMultiplier[id] = NbtTag_F32(tag); return; } if (IsTag(tag, "TransmitsLight")) { Blocks.BlocksLight[id] = NbtTag_U8(tag) == 0; return; } if (IsTag(tag, "FullBright")) { Blocks.FullBright[id] = NbtTag_U8(tag) != 0; return; } diff --git a/src/Protocol.c b/src/Protocol.c index c845f9737..138d75a49 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -1663,7 +1663,7 @@ static BlockID BlockDefs_DefineBlockCommonStart(cc_uint8** ptr, cc_bool uniqueSi name = UNSAFE_GetString(data); data += STRING_SIZE; Block_SetName(block, &name); - Block_SetCollide(block, *data++); + Blocks.Collide[block] = *data++; speedLog2 = (*data++ - 128) / 64.0f; #define LOG_2 0.693147180559945 @@ -1703,7 +1703,6 @@ static void BlockDefs_DefineBlockCommonEnd(cc_uint8* data, cc_uint8 shape, Block Blocks.FogDensity[block] = data[1] == 0 ? 0.0f : (data[1] + 1) / 128.0f; Blocks.FogCol[block] = PackedCol_Make(data[2], data[3], data[4], 255); - Block_DefineCustom(block); } static void BlockDefs_DefineBlock(cc_uint8* data) { @@ -1715,8 +1714,7 @@ static void BlockDefs_DefineBlock(cc_uint8* data) { } BlockDefs_DefineBlockCommonEnd(data, shape, block); - /* Update sprite BoundingBox if necessary */ - if (Blocks.Draw[block] == DRAW_SPRITE) Block_RecalculateBB(block); + Block_DefineCustom(block, true); } static void BlockDefs_UndefineBlock(cc_uint8* data) { @@ -1745,6 +1743,7 @@ static void BlockDefs_DefineBlockExt(cc_uint8* data) { Blocks.MinBB[block] = minBB; Blocks.MaxBB[block] = maxBB; BlockDefs_DefineBlockCommonEnd(data, 1, block); + Block_DefineCustom(block, false); } static void BlockDefs_Reset(void) {