Skip to content

Commit

Permalink
Add BlockLight EnvColor variable and fancy lighting blockdef values
Browse files Browse the repository at this point in the history
  • Loading branch information
Goodlyay committed May 15, 2024
1 parent a40648a commit bf23984
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 21 deletions.
32 changes: 32 additions & 0 deletions MCGalaxy/Blocks/BlockDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ public sealed class BlockDefinition {

[ConfigInt(null, null, -1, -1)]
public int InventoryOrder = -1;


/// <summary>
/// 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
/// </summary>
[ConfigInt(null, null, -1, -1, 15)] public int Brightness;
/// <summary>
/// Does this block use the sun environment color for light casting? (for fancy lighting option)
/// </summary>
[ConfigBool] public bool UseSunBrightness;

public BlockID GetBlock() { return Block.FromRaw(RawID); }
public void SetBlock(BlockID b) { RawID = Block.ToRaw(b); }
Expand All @@ -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;
}

/// <summary>
/// Called on this instance after it has been parsed from its json file
/// </summary>
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];
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
/// <summary>
/// Does not validate that the range falls within 0-15
/// </summary>
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;
Expand Down
44 changes: 40 additions & 4 deletions MCGalaxy/Commands/CPE/CustomBlockCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}


Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -416,6 +425,9 @@ static void DefineBlockStep(Player p, string value, BlockDefinitionsArgs args) {
SendStepHelp(p, args);
}

/// <summary>
/// Returns true if an edit actually occured
/// </summary>
static bool DoEdit(Player p, string[] parts, BlockDefinitionsArgs args, BlockID block) {
BlockDefinition def = args.defs[block], globalDef = BlockDefinition.GlobalDefs[block];

Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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" }
},
};


Expand Down
22 changes: 14 additions & 8 deletions MCGalaxy/Levels/LevelConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ public abstract class EnvConfig
/// <summary> Color of the skybox (Hex RGB color). Set to "" to use client defaults. </summary>
[ConfigString("SkyboxColor", "Env", "", true)]
public string SkyboxColor = "";

/// <summary> Color emitted by bright blocks (Hex RGB color). Set to "" to use client defaults. </summary>
[ConfigString("BlockLightColor", "Env", "", true)]
public string BlockLightColor = "";

public void ResetEnv() {
// TODO: Rewrite using ConfigElement somehow
Weather = ENV_USE_DEFAULT;
Expand All @@ -149,21 +152,24 @@ 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;
if (i == 2) return FogColor;
if (i == 3) return ShadowColor;
if (i == 4) return LightColor;
if (i == 5) return SkyboxColor;
if (i == 6) return BlockLightColor;

return null;
}
Expand Down
18 changes: 11 additions & 7 deletions MCGalaxy/Levels/LevelEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 7 additions & 1 deletion MCGalaxy/Network/Packets/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Player/Player.Handlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit bf23984

Please sign in to comment.