From bf23984370750cd9bc492dc574ce869690d2a9ec Mon Sep 17 00:00:00 2001 From: Goodlyay Date: Wed, 15 May 2024 03:41:31 -0700 Subject: [PATCH] Add BlockLight EnvColor variable and fancy lighting blockdef values --- MCGalaxy/Blocks/BlockDefinitions.cs | 32 +++++++++++++++ MCGalaxy/Commands/CPE/CustomBlockCommand.cs | 44 +++++++++++++++++++-- MCGalaxy/Levels/LevelConfig.cs | 22 +++++++---- MCGalaxy/Levels/LevelEnv.cs | 18 +++++---- MCGalaxy/Network/Packets/Packet.cs | 8 +++- MCGalaxy/Player/Player.Handlers.cs | 2 +- 6 files changed, 105 insertions(+), 21 deletions(-) diff --git a/MCGalaxy/Blocks/BlockDefinitions.cs b/MCGalaxy/Blocks/BlockDefinitions.cs index 90e2f5f6a..5ba029f21 100644 --- a/MCGalaxy/Blocks/BlockDefinitions.cs +++ b/MCGalaxy/Blocks/BlockDefinitions.cs @@ -61,6 +61,17 @@ public sealed class BlockDefinition { [ConfigInt(null, null, -1, -1)] public int InventoryOrder = -1; + + + /// + /// 0-15 value for how far this block casts light (for fancy lighting option). + /// -1 means this property has not been set by the user before + /// + [ConfigInt(null, null, -1, -1, 15)] public int Brightness; + /// + /// Does this block use the sun environment color for light casting? (for fancy lighting option) + /// + [ConfigBool] public bool UseSunBrightness; public BlockID GetBlock() { return Block.FromRaw(RawID); } public void SetBlock(BlockID b) { RawID = Block.ToRaw(b); } @@ -87,9 +98,18 @@ public BlockDefinition Copy() { def.LeftTex = LeftTex; def.RightTex = RightTex; def.FrontTex = FrontTex; def.BackTex = BackTex; def.InventoryOrder = InventoryOrder; + def.Brightness = Brightness; def.UseSunBrightness = UseSunBrightness; return def; } + /// + /// Called on this instance after it has been parsed from its json file + /// + void OnParsed() { + //Sync Brightness setting logically to max brightness if it has not been set before but this block is fullbright + if (Brightness == -1 && FullBright) Brightness = 15; + } + static ConfigElement[] elems; public static BlockDefinition[] Load(string path) { BlockDefinition[] defs = new BlockDefinition[Block.SUPPORTED_COUNT]; @@ -103,6 +123,7 @@ public static BlockDefinition[] Load(string path) { reader.OnMember = (obj, key, value) => { if (obj.Meta == null) obj.Meta = new BlockDefinition(); ConfigElement.Parse(elems, obj.Meta, key, (string)value); + ((BlockDefinition)obj.Meta).OnParsed(); }; JsonArray array = (JsonArray)reader.Parse(); @@ -265,6 +286,17 @@ public void SetSideTex(ushort id) { LeftTex = id; RightTex = id; FrontTex = id; BackTex = id; } + public void SetFullBright(bool fullBright) { + SetBrightness(fullBright ? 15 : 0, false); + } + /// + /// Does not validate that the range falls within 0-15 + /// + public void SetBrightness(int brightness, bool sun) { + Brightness = brightness; + UseSunBrightness = sun; + if (Brightness > 0) { FullBright = true; } else { FullBright = false; } + } internal static void SendLevelCustomBlocks(Player pl) { BlockDefinition[] defs = pl.level.CustomBlockDefs; diff --git a/MCGalaxy/Commands/CPE/CustomBlockCommand.cs b/MCGalaxy/Commands/CPE/CustomBlockCommand.cs index dc2207a01..8e06dbc14 100644 --- a/MCGalaxy/Commands/CPE/CustomBlockCommand.cs +++ b/MCGalaxy/Commands/CPE/CustomBlockCommand.cs @@ -259,8 +259,11 @@ static void DoInfo(Player p, BlockDefinitionsArgs args, BlockID block) { } else { p.Message(" Block is a cube from ({0}, {1}, {2}) to ({3}, {4}, {5})", def.MinX, def.MinZ, def.MinY, def.MaxX, def.MaxZ, def.MaxY); - p.Message(" Texture IDs (left: {0}, right: {1}, front: {2}, back: {3}, top: {4}, bottom: {5})", - def.LeftTex, def.RightTex, def.FrontTex, def.BackTex, def.TopTex, def.BottomTex); + p.Message(" Texture IDs:"); + p.Message(" left: {0}, right: {1}, front: {2}, back: {3}", + def.LeftTex, def.RightTex, def.FrontTex, def.BackTex); + p.Message(" top: {0}, bottom: {1}", + def.TopTex, def.BottomTex); } if (def.InventoryOrder < 0) { @@ -270,6 +273,10 @@ static void DoInfo(Player p, BlockDefinitionsArgs args, BlockID block) { } else { p.Message(" Order: " + def.InventoryOrder); } + if (def.Brightness > 0) { + string word = def.UseSunBrightness ? "SunBrightness" : "Brightness"; + p.Message(" {0}: {1}", word, def.Brightness); + } } @@ -384,8 +391,10 @@ static void DefineBlockStep(Player p, string value, BlockDefinitionsArgs args) { if (CommandParser.GetByte(p, value, "Walk sound", ref def.WalkSound, 0, 11)) step++; } else if (step == 13) { - if (CommandParser.GetBool(p, value, ref def.FullBright)) + if (CommandParser.GetBool(p, value, ref temp)) { + def.SetFullBright(temp); step++; + } } else if (step == 14) { if (CommandParser.GetByte(p, value, "Block draw", ref def.BlockDraw, 0, 4)) step++; @@ -416,6 +425,9 @@ static void DefineBlockStep(Player p, string value, BlockDefinitionsArgs args) { SendStepHelp(p, args); } + /// + /// Returns true if an edit actually occured + /// static bool DoEdit(Player p, string[] parts, BlockDefinitionsArgs args, BlockID block) { BlockDefinition def = args.defs[block], globalDef = BlockDefinition.GlobalDefs[block]; @@ -483,7 +495,7 @@ static bool DoEdit(Player p, string[] parts, BlockDefinitionsArgs args, BlockID if (!CommandParser.GetBool(p, value, ref temp)) { SendEditHelp(p, arg); return false; } - def.FullBright = temp; + def.SetFullBright(temp); break; case "shape": @@ -543,6 +555,22 @@ static bool DoEdit(Player p, string[] parts, BlockDefinitionsArgs args, BlockID p.Message("Set inventory order for {0} to {1}", blockName, order == def.RawID ? "default" : order.ToString()); return true; + + case "brightness": + int brightness = 0; + if (!CommandParser.GetInt(p, value, "brightness", ref brightness, 0, 15)) { + SendEditHelp(p, arg); return false; + } + def.SetBrightness(brightness, false); + break; + + case "sunbrightness": + int sunBrightness = 0; + if (!CommandParser.GetInt(p, value, "sunbrightness", ref sunBrightness, 0, 15)) { + SendEditHelp(p, arg); return false; + } + def.SetBrightness(sunBrightness, true); + break; default: p.Message("Unrecognised property: " + arg); return false; } @@ -855,6 +883,14 @@ static string MapPropertyName(string prop) { "The default position of a block is its ID.", "A position of 0 hides the block from the inventory." } }, + { "brightness", new string[] { "Type a number (0-15) for the brightness of the block.", + "You need Fancy Lighting to see differences between 1 and 15.", + "The block will glow using the \"blocklight\" env color" } + }, + { "sunbrightness", new string[] { "Type a number (0-15) for the sun-brightness of the block.", + "You need Fancy Lighting to see differences between 1 and 15.", + "The block will glow using the \"sun\" env color" } + }, }; diff --git a/MCGalaxy/Levels/LevelConfig.cs b/MCGalaxy/Levels/LevelConfig.cs index b8e3b4103..26e724e47 100644 --- a/MCGalaxy/Levels/LevelConfig.cs +++ b/MCGalaxy/Levels/LevelConfig.cs @@ -130,7 +130,10 @@ public abstract class EnvConfig /// Color of the skybox (Hex RGB color). Set to "" to use client defaults. [ConfigString("SkyboxColor", "Env", "", true)] public string SkyboxColor = ""; - + /// Color emitted by bright blocks (Hex RGB color). Set to "" to use client defaults. + [ConfigString("BlockLightColor", "Env", "", true)] + public string BlockLightColor = ""; + public void ResetEnv() { // TODO: Rewrite using ConfigElement somehow Weather = ENV_USE_DEFAULT; @@ -149,14 +152,16 @@ public void ResetEnv() { EdgeBlock = Block.Invalid; ExpFog = ENV_USE_DEFAULT; - CloudColor = ""; - FogColor = ""; - SkyColor = ""; - ShadowColor = ""; - LightColor = ""; - SkyboxColor = ""; + CloudColor = ""; + FogColor = ""; + SkyColor = ""; + ShadowColor = ""; + LightColor = ""; + SkyboxColor = ""; + BlockLightColor = ""; } - + + internal const int ENV_COLOR_COUNT = 6; public string GetColor(int i) { if (i == 0) return SkyColor; if (i == 1) return CloudColor; @@ -164,6 +169,7 @@ public string GetColor(int i) { if (i == 3) return ShadowColor; if (i == 4) return LightColor; if (i == 5) return SkyboxColor; + if (i == 6) return BlockLightColor; return null; } diff --git a/MCGalaxy/Levels/LevelEnv.cs b/MCGalaxy/Levels/LevelEnv.cs index 0f4d8c30c..9dd9de7eb 100644 --- a/MCGalaxy/Levels/LevelEnv.cs +++ b/MCGalaxy/Levels/LevelEnv.cs @@ -42,12 +42,13 @@ public static class EnvOptions { new EnvOption("EdgeLevel", SetEdgeLevel, "&HSets the water height of the map"), new EnvOption("SidesOffset", SetSidesOffset, "&HSets offset of bedrock from water (default -2)"), new EnvOption("MaxFog", SetMaxFog, "&HSets maximum fog distance in the map (e.g. 16 for a horror map)"), - new EnvOption("Sky", SetSky, "&HSets color of the sky (default 99CCFF)"), - new EnvOption("Clouds", SetClouds, "&HSets color of the clouds (default FFFFFF)"), - new EnvOption("Fog", SetFog, "&HSets color of the fog (default FFFFFF)"), - new EnvOption("Sun", SetSun, "&HSets color of blocks in sunlight (default FFFFFF)"), - new EnvOption("Shadow", SetShadow, "&HSets color of blocks in darkness (default 9B9B9B)"), - new EnvOption("Skybox", SetSkybox, "&HSets color of the skybox (default FFFFFF)"), + new EnvOption("Sky", SetSky, "&HSets color of the sky (default 99CCFF)"), + new EnvOption("Clouds", SetClouds, "&HSets color of the clouds (default FFFFFF)"), + new EnvOption("Fog", SetFog, "&HSets color of the fog (default FFFFFF)"), + new EnvOption("Sun", SetSun, "&HSets color of blocks in sunlight (default FFFFFF)"), + new EnvOption("Shadow", SetShadow, "&HSets color of blocks in darkness (default 9B9B9B)"), + new EnvOption("Skybox", SetSkybox, "&HSets color of the skybox (default FFFFFF)"), + new EnvOption("BlockLight", SetBlockLight, "&HSets color cast by bright blocks (default FFEBC6)"), new EnvOption("CloudsSpeed", SetCloudsSpeed, "&HSets how fast clouds move (negative moves in opposite direction)"), new EnvOption("WeatherSpeed", SetWeatherSpeed, "&HSets how fast rain/snow falls (negative falls upwards)"), new EnvOption("WeatherFade", SetWeatherFade, "&HSets how quickly rain/snow fades out over distance"), @@ -117,7 +118,10 @@ static void SetShadow(Player p, string area, EnvConfig cfg, string value) { static void SetSkybox(Player p, string area, EnvConfig cfg, string value) { SetColor(p, value, area, "skybox color", ref cfg.SkyboxColor); } - + static void SetBlockLight(Player p, string area, EnvConfig cfg, string value) { + SetColor(p, value, area, "block light color", ref cfg.BlockLightColor); + } + static void SetCloudsSpeed(Player p, string area, EnvConfig cfg, string value) { SetFloat(p, value, area, 256, "clouds speed", ref cfg.CloudsSpeed, -0xFFFFFF, 0xFFFFFF); } diff --git a/MCGalaxy/Network/Packets/Packet.cs b/MCGalaxy/Network/Packets/Packet.cs index 555b3990e..b8e52ee36 100644 --- a/MCGalaxy/Network/Packets/Packet.cs +++ b/MCGalaxy/Network/Packets/Packet.cs @@ -747,7 +747,13 @@ static void MakeDefineBlockStart(BlockDefinition def, byte[] buffer, ref int i, buffer[i++] = (byte)(def.BlocksLight ? 0 : 1); buffer[i++] = def.WalkSound; - buffer[i++] = (byte)(def.FullBright ? 1 : 0); + + // 0b_US--_LLLL where U = uses modern brightness, S = uses sun brightness, and L = brightness */ + byte brightness = (byte)Math.Max(0, Math.Min(def.Brightness, 15)); + brightness |= 1 << 7; // Insert "use modern brightness" flag (otherwise client will interpret it as either 0 or 15 brightness) + if (def.UseSunBrightness) brightness |= 1 << 6; // Insert "use sun color" flag + + buffer[i++] = brightness; } static void MakeDefineBlockEnd(BlockDefinition def, ref int i, byte[] buffer) { diff --git a/MCGalaxy/Player/Player.Handlers.cs b/MCGalaxy/Player/Player.Handlers.cs index 3fd40f639..15c647710 100644 --- a/MCGalaxy/Player/Player.Handlers.cs +++ b/MCGalaxy/Player/Player.Handlers.cs @@ -299,7 +299,7 @@ int CurrentEnvProp(EnvProp i, Zone zone) { public void SendCurrentEnv() { Zone zone = ZoneIn; - for (int i = 0; i <= 5; i++) { + for (int i = 0; i <= EnvConfig.ENV_COLOR_COUNT; i++) { string col = Server.Config.GetColor(i); if (level.Config.GetColor(i) != "") { col = level.Config.GetColor(i);