From a994bf367cf7baaaa4c1bb257bec03321f0272c1 Mon Sep 17 00:00:00 2001 From: Goodlyay Date: Wed, 15 May 2024 17:04:18 -0700 Subject: [PATCH] Update lighting appropriately when block definitions are changed --- src/Lighting.c | 2 +- src/Lighting.h | 1 + src/ModernLighting.c | 5 +++-- src/Protocol.c | 22 ++++++++++++++++------ 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Lighting.c b/src/Lighting.c index 32972a14e..adb800862 100644 --- a/src/Lighting.c +++ b/src/Lighting.c @@ -102,7 +102,7 @@ static PackedCol ClassicLighting_Color_ZSide_Fast(int x, int y, int z) { return y > classic_heightmap[Lighting_Pack(x, z)] ? Env.SunZSide : Env.ShadowZSide; } -static void ClassicLighting_Refresh(void) { +void ClassicLighting_Refresh(void) { int i; for (i = 0; i < World.Width * World.Length; i++) { classic_heightmap[i] = HEIGHT_UNCALCULATED; diff --git a/src/Lighting.h b/src/Lighting.h index 447aa4e2f..5cc55b517 100644 --- a/src/Lighting.h +++ b/src/Lighting.h @@ -65,6 +65,7 @@ void Lighting_ApplyActive(void); void ModernLighting_SetActive(void); void ModernLighting_OnEnvVariableChanged(void* obj, int envVar); +void ClassicLighting_Refresh(void); void ClassicLighting_FreeState(void); void ClassicLighting_AllocState(void); int ClassicLighting_GetLightHeight(int x, int z); diff --git a/src/ModernLighting.c b/src/ModernLighting.c index 03c636fa6..2c3ce77dd 100644 --- a/src/ModernLighting.c +++ b/src/ModernLighting.c @@ -486,8 +486,9 @@ static void ModernLighting_OnBlockChanged(int x, int y, int z, BlockID oldBlock, /* Invalidates/Resets lighting state for all of the blocks in the world */ /* (e.g. because a block changed whether it is full bright or not) */ static void ModernLighting_Refresh(void) { - ModernLighting_InitPalettes(); - /* Set all the chunk lighting data flags to CHUNK_UNCALCULATED? */ + ClassicLighting_Refresh(); + ModernLighting_FreeState(); + ModernLighting_AllocState(); } static cc_bool ModernLighting_IsLit(int x, int y, int z) { return true; } static cc_bool ModernLighting_IsLit_Fast(int x, int y, int z) { return true; } diff --git a/src/Protocol.c b/src/Protocol.c index bda7b8a1a..f8aad4e60 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -1718,10 +1718,17 @@ static void CustomModels_Reset(void) { } /*########################################################################################################################* *------------------------------------------------------Custom blocks------------------------------------------------------* *#########################################################################################################################*/ -static void BlockDefs_OnBlockUpdated(BlockID block, cc_bool didBlockLight) { +static void BlockDefs_OnBlocksLightPropertyUpdated(BlockID block, cc_bool oldProp) { if (!World.Loaded) return; /* Need to refresh lighting when a block's light blocking state changes */ - if (Blocks.BlocksLight[block] != didBlockLight) Lighting.Refresh(); + if (Blocks.BlocksLight[block] != oldProp) Lighting.Refresh(); +} + +static void BlockDefs_OnBrightnessPropertyUpdated(BlockID block, cc_uint8 oldProp) { + if (!World.Loaded) return; + if (!Lighting_Modern) return; + /* Need to refresh modern lighting when a block's brightness changes */ + if (Blocks.Brightness[block] != oldProp) Lighting.Refresh(); } static TextureLoc BlockDefs_Tex(cc_uint8** ptr) { @@ -1740,13 +1747,15 @@ static TextureLoc BlockDefs_Tex(cc_uint8** ptr) { static BlockID BlockDefs_DefineBlockCommonStart(cc_uint8** ptr, cc_bool uniqueSideTexs) { cc_string name; BlockID block; - cc_bool didBlockLight; + cc_bool oldBlocksLight; + cc_uint8 oldBrightness; float speedLog2; cc_uint8 sound; cc_uint8* data = *ptr; ReadBlock(data, block); - didBlockLight = Blocks.BlocksLight[block]; + oldBlocksLight = Blocks.BlocksLight[block]; + oldBrightness = Blocks.Brightness[block]; Block_ResetProps(block); name = UNSAFE_GetString(data); data += STRING_SIZE; @@ -1768,7 +1777,7 @@ static BlockID BlockDefs_DefineBlockCommonStart(cc_uint8** ptr, cc_bool uniqueSi Block_Tex(block, FACE_YMIN) = BlockDefs_Tex(&data); Blocks.BlocksLight[block] = *data++ == 0; - BlockDefs_OnBlockUpdated(block, didBlockLight); + BlockDefs_OnBlocksLightPropertyUpdated(block, oldBlocksLight); sound = *data++; Blocks.StepSounds[block] = sound; @@ -1776,6 +1785,7 @@ static BlockID BlockDefs_DefineBlockCommonStart(cc_uint8** ptr, cc_bool uniqueSi if (sound == SOUND_GLASS) Blocks.StepSounds[block] = SOUND_STONE; Blocks.Brightness[block] = Block_ReadBrightness(*data++); + BlockDefs_OnBrightnessPropertyUpdated(block, oldBrightness); *ptr = data; return block; } @@ -1812,7 +1822,7 @@ static void BlockDefs_UndefineBlock(cc_uint8* data) { didBlockLight = Blocks.BlocksLight[block]; Block_UndefineCustom(block); - BlockDefs_OnBlockUpdated(block, didBlockLight); + BlockDefs_OnBlocksLightPropertyUpdated(block, didBlockLight); } static void BlockDefs_DefineBlockExt(cc_uint8* data) {