diff options
author | Aiden Neill <aidenneill@gmail.com> | 2020-12-19 02:42:34 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-19 02:42:34 +0100 |
commit | a7e9f88ff3dba3b99b58d2bf9ed2350e6154d876 (patch) | |
tree | 926f6dbc58ba7decf902d4f86bc72690e30337a3 /src/Simulator/IncrementalRedstoneSimulator/DaylightSensorHandler.h | |
parent | Added dimension check to nether portal (#5068) (diff) | |
download | cuberite-a7e9f88ff3dba3b99b58d2bf9ed2350e6154d876.tar cuberite-a7e9f88ff3dba3b99b58d2bf9ed2350e6154d876.tar.gz cuberite-a7e9f88ff3dba3b99b58d2bf9ed2350e6154d876.tar.bz2 cuberite-a7e9f88ff3dba3b99b58d2bf9ed2350e6154d876.tar.lz cuberite-a7e9f88ff3dba3b99b58d2bf9ed2350e6154d876.tar.xz cuberite-a7e9f88ff3dba3b99b58d2bf9ed2350e6154d876.tar.zst cuberite-a7e9f88ff3dba3b99b58d2bf9ed2350e6154d876.zip |
Diffstat (limited to 'src/Simulator/IncrementalRedstoneSimulator/DaylightSensorHandler.h')
-rw-r--r-- | src/Simulator/IncrementalRedstoneSimulator/DaylightSensorHandler.h | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/Simulator/IncrementalRedstoneSimulator/DaylightSensorHandler.h b/src/Simulator/IncrementalRedstoneSimulator/DaylightSensorHandler.h new file mode 100644 index 000000000..160486d29 --- /dev/null +++ b/src/Simulator/IncrementalRedstoneSimulator/DaylightSensorHandler.h @@ -0,0 +1,66 @@ + +#pragma once + +#include "World.h" + + + + + +namespace DaylightSensorHandler +{ + inline PowerLevel GetPowerLevel(const cChunk & a_Chunk, const Vector3i a_Position) + { + if (a_Chunk.GetBlock(a_Position) == E_BLOCK_INVERTED_DAYLIGHT_SENSOR) + { + // Inverted sensor directly returns darkened skylight, no fancy tricks: + return 15 - a_Chunk.GetSkyLightAltered(a_Position); + } + + // The [0, 1) proportion of the current day that has elapsed. + const auto ProportionOfDay = a_Chunk.GetWorld()->GetTimeOfDay() * (static_cast<float>(M_PI) / 12000.f); + + // The curved value of darkened skylight, with outputs somewhat similar to Vanilla. + const auto RawOutput = a_Chunk.GetSkyLightAltered(a_Position) * (0.6f * std::sin(ProportionOfDay) + 0.5f); + + // Saturate the amplified sine curve at 0 and 15: + return static_cast<PowerLevel>(std::clamp(RawOutput, 0.f, 15.f)); + } + + inline PowerLevel GetPowerDeliveredToPosition(const cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType, bool IsLinked) + { + UNUSED(a_Chunk); + UNUSED(a_BlockType); + UNUSED(a_QueryPosition); + + // Daylight sensors only output to immediately surrounding blocks: + return IsLinked ? 0 : a_Chunk.GetMeta(a_Position); + } + + inline void Update(cChunk & a_Chunk, cChunk & CurrentlyTicking, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, const PowerLevel Power) + { + // LOGD("Evaluating Darryl the daylight sensor (%d %d %d)", a_Position.x, a_Position.y, a_Position.z); + + // We don't need to update the daylight sensor often: + DataForChunk(a_Chunk).m_MechanismDelays[a_Position] = std::make_pair(10, bool()); + + // What the sensor should output according to the time-power function. + const auto PowerLevel = GetPowerLevel(a_Chunk, a_Position); + + // Only update the output if the power level has changed: + if (PowerLevel != a_Meta) + { + a_Chunk.SetMeta(a_Position, PowerLevel); + UpdateAdjustedRelatives(a_Chunk, CurrentlyTicking, a_Position, RelativeAdjacents); + } + } + + inline void ForValidSourcePositions(const cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, ForEachSourceCallback & Callback) + { + UNUSED(a_Chunk); + UNUSED(a_Position); + UNUSED(a_BlockType); + UNUSED(a_Meta); + UNUSED(Callback); + } +}; |