New pull request for daylight sensor (#5066)
* Fixes #4918 Co-authored-by: Tiger Wang <ziwei.tiger@outlook.com>
This commit is contained in:
parent
7fbe4a0126
commit
a7e9f88ff3
54
src/Blocks/BlockDaylightSensor.h
Normal file
54
src/Blocks/BlockDaylightSensor.h
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "BlockHandler.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class cBlockDaylightSensorHandler final :
|
||||||
|
public cBlockHandler
|
||||||
|
{
|
||||||
|
using Super = cBlockHandler;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
using Super::Super;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
virtual bool OnUse(
|
||||||
|
cChunkInterface & a_ChunkInterface,
|
||||||
|
cWorldInterface & a_WorldInterface,
|
||||||
|
cPlayer & a_Player,
|
||||||
|
const Vector3i a_BlockPos,
|
||||||
|
eBlockFace a_BlockFace,
|
||||||
|
const Vector3i a_CursorPos
|
||||||
|
) const override
|
||||||
|
{
|
||||||
|
if (a_ChunkInterface.GetBlock(a_BlockPos) == E_BLOCK_DAYLIGHT_SENSOR)
|
||||||
|
{
|
||||||
|
a_ChunkInterface.SetBlock(a_BlockPos, E_BLOCK_INVERTED_DAYLIGHT_SENSOR, 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
a_ChunkInterface.SetBlock(a_BlockPos, E_BLOCK_DAYLIGHT_SENSOR, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, const cEntity * a_Digger, const cItem * a_Tool) const override
|
||||||
|
{
|
||||||
|
// Always drop the regular daylight sensor:
|
||||||
|
return { E_BLOCK_DAYLIGHT_SENSOR };
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
virtual bool IsUseable(void) const override
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
@ -24,6 +24,7 @@
|
|||||||
#include "BlockComparator.h"
|
#include "BlockComparator.h"
|
||||||
#include "BlockConcretePowder.h"
|
#include "BlockConcretePowder.h"
|
||||||
#include "BlockCrops.h"
|
#include "BlockCrops.h"
|
||||||
|
#include "BlockDaylightSensor.h"
|
||||||
#include "BlockDeadBush.h"
|
#include "BlockDeadBush.h"
|
||||||
#include "BlockDefaultBlock.h"
|
#include "BlockDefaultBlock.h"
|
||||||
#include "BlockDirt.h"
|
#include "BlockDirt.h"
|
||||||
@ -250,7 +251,7 @@ namespace
|
|||||||
constexpr cBlockFenceGateHandler BlockDarkOakFenceGateHandler (E_BLOCK_DARK_OAK_FENCE_GATE);
|
constexpr cBlockFenceGateHandler BlockDarkOakFenceGateHandler (E_BLOCK_DARK_OAK_FENCE_GATE);
|
||||||
constexpr cBlockFenceHandler BlockDarkOakFenceHandler (E_BLOCK_DARK_OAK_FENCE);
|
constexpr cBlockFenceHandler BlockDarkOakFenceHandler (E_BLOCK_DARK_OAK_FENCE);
|
||||||
constexpr cBlockStairsHandler BlockDarkOakWoodStairsHandler (E_BLOCK_DARK_OAK_WOOD_STAIRS);
|
constexpr cBlockStairsHandler BlockDarkOakWoodStairsHandler (E_BLOCK_DARK_OAK_WOOD_STAIRS);
|
||||||
constexpr cDefaultBlockHandler BlockDaylightSensorHandler (E_BLOCK_DAYLIGHT_SENSOR);
|
constexpr cBlockDaylightSensorHandler BlockDaylightSensorHandler (E_BLOCK_DAYLIGHT_SENSOR);
|
||||||
constexpr cBlockDeadBushHandler BlockDeadBushHandler (E_BLOCK_DEAD_BUSH);
|
constexpr cBlockDeadBushHandler BlockDeadBushHandler (E_BLOCK_DEAD_BUSH);
|
||||||
constexpr cBlockRailHandler BlockDetectorRailHandler (E_BLOCK_DETECTOR_RAIL);
|
constexpr cBlockRailHandler BlockDetectorRailHandler (E_BLOCK_DETECTOR_RAIL);
|
||||||
constexpr cDefaultBlockHandler BlockDiamondBlockHandler (E_BLOCK_DIAMOND_BLOCK);
|
constexpr cDefaultBlockHandler BlockDiamondBlockHandler (E_BLOCK_DIAMOND_BLOCK);
|
||||||
|
@ -26,6 +26,7 @@ target_sources(
|
|||||||
BlockComparator.h
|
BlockComparator.h
|
||||||
BlockConcretePowder.h
|
BlockConcretePowder.h
|
||||||
BlockCrops.h
|
BlockCrops.h
|
||||||
|
BlockDaylightSensor.h
|
||||||
BlockDeadBush.h
|
BlockDeadBush.h
|
||||||
BlockDefaultBlock.h
|
BlockDefaultBlock.h
|
||||||
BlockDirt.h
|
BlockDirt.h
|
||||||
|
@ -6,6 +6,7 @@ target_sources(
|
|||||||
RedstoneHandler.cpp
|
RedstoneHandler.cpp
|
||||||
|
|
||||||
CommandBlockHandler.h
|
CommandBlockHandler.h
|
||||||
|
DaylightSensorHandler.h
|
||||||
DoorHandler.h
|
DoorHandler.h
|
||||||
DropSpenserHandler.h
|
DropSpenserHandler.h
|
||||||
ForEachSourceCallback.h
|
ForEachSourceCallback.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);
|
||||||
|
}
|
||||||
|
};
|
@ -91,9 +91,11 @@ bool ForEachSourceCallback::ShouldQueryLinkedPosition(const BLOCKTYPE Block)
|
|||||||
{
|
{
|
||||||
switch (Block)
|
switch (Block)
|
||||||
{
|
{
|
||||||
// Normally we don't ask solid blocks for power because they don't have any (store, dirt, etc.)
|
// Normally we don't ask solid blocks for power because they don't have any (stone, dirt, etc.)
|
||||||
// However, these are mechanisms that are IsSolid, but still give power. Don't ignore them:
|
// However, these are mechanisms that are IsSolid, but still give power. Don't ignore them:
|
||||||
case E_BLOCK_BLOCK_OF_REDSTONE:
|
case E_BLOCK_BLOCK_OF_REDSTONE:
|
||||||
|
case E_BLOCK_DAYLIGHT_SENSOR:
|
||||||
|
case E_BLOCK_INVERTED_DAYLIGHT_SENSOR:
|
||||||
case E_BLOCK_OBSERVER:
|
case E_BLOCK_OBSERVER:
|
||||||
case E_BLOCK_TRAPPED_CHEST: return false;
|
case E_BLOCK_TRAPPED_CHEST: return false;
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ private:
|
|||||||
switch (a_Block) // Call the appropriate simulator for the entry's block type
|
switch (a_Block) // Call the appropriate simulator for the entry's block type
|
||||||
{
|
{
|
||||||
case E_BLOCK_DAYLIGHT_SENSOR:
|
case E_BLOCK_DAYLIGHT_SENSOR:
|
||||||
|
case E_BLOCK_INVERTED_DAYLIGHT_SENSOR:
|
||||||
case E_BLOCK_TRIPWIRE_HOOK:
|
case E_BLOCK_TRIPWIRE_HOOK:
|
||||||
case E_BLOCK_WOODEN_PRESSURE_PLATE:
|
case E_BLOCK_WOODEN_PRESSURE_PLATE:
|
||||||
case E_BLOCK_STONE_PRESSURE_PLATE:
|
case E_BLOCK_STONE_PRESSURE_PLATE:
|
||||||
@ -58,6 +59,7 @@ private:
|
|||||||
case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE:
|
case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE:
|
||||||
case E_BLOCK_HOPPER:
|
case E_BLOCK_HOPPER:
|
||||||
case E_BLOCK_INACTIVE_COMPARATOR:
|
case E_BLOCK_INACTIVE_COMPARATOR:
|
||||||
|
case E_BLOCK_INVERTED_DAYLIGHT_SENSOR:
|
||||||
case E_BLOCK_IRON_DOOR:
|
case E_BLOCK_IRON_DOOR:
|
||||||
case E_BLOCK_IRON_TRAPDOOR:
|
case E_BLOCK_IRON_TRAPDOOR:
|
||||||
case E_BLOCK_JUNGLE_DOOR:
|
case E_BLOCK_JUNGLE_DOOR:
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include "ForEachSourceCallback.h"
|
#include "ForEachSourceCallback.h"
|
||||||
|
|
||||||
#include "CommandBlockHandler.h"
|
#include "CommandBlockHandler.h"
|
||||||
|
#include "DaylightSensorHandler.h"
|
||||||
#include "DoorHandler.h"
|
#include "DoorHandler.h"
|
||||||
#include "RedstoneTorchHandler.h"
|
#include "RedstoneTorchHandler.h"
|
||||||
#include "RedstoneWireHandler.h"
|
#include "RedstoneWireHandler.h"
|
||||||
@ -30,58 +31,60 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define INVOKE_FOR_HANDLERS(Callback) \
|
#define INVOKE_FOR_HANDLERS(Callback) \
|
||||||
do \
|
do \
|
||||||
{ \
|
{ \
|
||||||
switch (BlockType) \
|
switch (BlockType) \
|
||||||
{ \
|
{ \
|
||||||
case E_BLOCK_ACTIVATOR_RAIL: \
|
case E_BLOCK_ACTIVATOR_RAIL: \
|
||||||
case E_BLOCK_DETECTOR_RAIL: \
|
case E_BLOCK_DETECTOR_RAIL: \
|
||||||
case E_BLOCK_POWERED_RAIL: return PoweredRailHandler::Callback; \
|
case E_BLOCK_POWERED_RAIL: return PoweredRailHandler::Callback; \
|
||||||
case E_BLOCK_ACTIVE_COMPARATOR: \
|
case E_BLOCK_ACTIVE_COMPARATOR: \
|
||||||
case E_BLOCK_INACTIVE_COMPARATOR: return RedstoneComparatorHandler::Callback; \
|
case E_BLOCK_INACTIVE_COMPARATOR: return RedstoneComparatorHandler::Callback; \
|
||||||
case E_BLOCK_DISPENSER: \
|
case E_BLOCK_DISPENSER: \
|
||||||
case E_BLOCK_DROPPER: return DropSpenserHandler::Callback; \
|
case E_BLOCK_DROPPER: return DropSpenserHandler::Callback; \
|
||||||
case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE: \
|
case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE: \
|
||||||
case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE: \
|
case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE: \
|
||||||
case E_BLOCK_STONE_PRESSURE_PLATE: \
|
case E_BLOCK_STONE_PRESSURE_PLATE: \
|
||||||
case E_BLOCK_WOODEN_PRESSURE_PLATE: return PressurePlateHandler::Callback; \
|
case E_BLOCK_WOODEN_PRESSURE_PLATE: return PressurePlateHandler::Callback; \
|
||||||
case E_BLOCK_ACACIA_FENCE_GATE: \
|
case E_BLOCK_ACACIA_FENCE_GATE: \
|
||||||
case E_BLOCK_BIRCH_FENCE_GATE: \
|
case E_BLOCK_BIRCH_FENCE_GATE: \
|
||||||
case E_BLOCK_DARK_OAK_FENCE_GATE: \
|
case E_BLOCK_DARK_OAK_FENCE_GATE: \
|
||||||
case E_BLOCK_FENCE_GATE: \
|
case E_BLOCK_FENCE_GATE: \
|
||||||
case E_BLOCK_IRON_TRAPDOOR: \
|
case E_BLOCK_IRON_TRAPDOOR: \
|
||||||
case E_BLOCK_JUNGLE_FENCE_GATE: \
|
case E_BLOCK_JUNGLE_FENCE_GATE: \
|
||||||
case E_BLOCK_SPRUCE_FENCE_GATE: \
|
case E_BLOCK_SPRUCE_FENCE_GATE: \
|
||||||
case E_BLOCK_TRAPDOOR: return SmallGateHandler::Callback; \
|
case E_BLOCK_TRAPDOOR: return SmallGateHandler::Callback; \
|
||||||
case E_BLOCK_REDSTONE_LAMP_OFF: \
|
case E_BLOCK_REDSTONE_LAMP_OFF: \
|
||||||
case E_BLOCK_REDSTONE_LAMP_ON: return RedstoneLampHandler::Callback; \
|
case E_BLOCK_REDSTONE_LAMP_ON: return RedstoneLampHandler::Callback; \
|
||||||
case E_BLOCK_REDSTONE_REPEATER_OFF: \
|
case E_BLOCK_REDSTONE_REPEATER_OFF: \
|
||||||
case E_BLOCK_REDSTONE_REPEATER_ON: return RedstoneRepeaterHandler::Callback; \
|
case E_BLOCK_REDSTONE_REPEATER_ON: return RedstoneRepeaterHandler::Callback; \
|
||||||
case E_BLOCK_REDSTONE_TORCH_OFF: \
|
case E_BLOCK_REDSTONE_TORCH_OFF: \
|
||||||
case E_BLOCK_REDSTONE_TORCH_ON: return RedstoneTorchHandler::Callback; \
|
case E_BLOCK_REDSTONE_TORCH_ON: return RedstoneTorchHandler::Callback; \
|
||||||
case E_BLOCK_OBSERVER: return ObserverHandler::Callback; \
|
case E_BLOCK_OBSERVER: return ObserverHandler::Callback; \
|
||||||
case E_BLOCK_PISTON: \
|
case E_BLOCK_PISTON: \
|
||||||
case E_BLOCK_STICKY_PISTON: return PistonHandler::Callback; \
|
case E_BLOCK_STICKY_PISTON: return PistonHandler::Callback; \
|
||||||
case E_BLOCK_LEVER: \
|
case E_BLOCK_DAYLIGHT_SENSOR: \
|
||||||
case E_BLOCK_STONE_BUTTON: \
|
case E_BLOCK_INVERTED_DAYLIGHT_SENSOR: return DaylightSensorHandler::Callback; \
|
||||||
case E_BLOCK_WOODEN_BUTTON: return RedstoneToggleHandler::Callback; \
|
case E_BLOCK_LEVER: \
|
||||||
case E_BLOCK_BLOCK_OF_REDSTONE: return RedstoneBlockHandler::Callback; \
|
case E_BLOCK_STONE_BUTTON: \
|
||||||
case E_BLOCK_COMMAND_BLOCK: return CommandBlockHandler::Callback; \
|
case E_BLOCK_WOODEN_BUTTON: return RedstoneToggleHandler::Callback; \
|
||||||
case E_BLOCK_HOPPER: return HopperHandler::Callback; \
|
case E_BLOCK_BLOCK_OF_REDSTONE: return RedstoneBlockHandler::Callback; \
|
||||||
case E_BLOCK_NOTE_BLOCK: return NoteBlockHandler::Callback; \
|
case E_BLOCK_COMMAND_BLOCK: return CommandBlockHandler::Callback; \
|
||||||
case E_BLOCK_REDSTONE_WIRE: return RedstoneWireHandler::Callback; \
|
case E_BLOCK_HOPPER: return HopperHandler::Callback; \
|
||||||
case E_BLOCK_TNT: return TNTHandler::Callback; \
|
case E_BLOCK_NOTE_BLOCK: return NoteBlockHandler::Callback; \
|
||||||
case E_BLOCK_TRAPPED_CHEST: return TrappedChestHandler::Callback; \
|
case E_BLOCK_REDSTONE_WIRE: return RedstoneWireHandler::Callback; \
|
||||||
case E_BLOCK_TRIPWIRE_HOOK: return TripwireHookHandler::Callback; \
|
case E_BLOCK_TNT: return TNTHandler::Callback; \
|
||||||
default: \
|
case E_BLOCK_TRAPPED_CHEST: return TrappedChestHandler::Callback; \
|
||||||
{ \
|
case E_BLOCK_TRIPWIRE_HOOK: return TripwireHookHandler::Callback; \
|
||||||
if (cBlockDoorHandler::IsDoorBlockType(BlockType)) \
|
default: \
|
||||||
{ \
|
{ \
|
||||||
return DoorHandler::Callback; \
|
if (cBlockDoorHandler::IsDoorBlockType(BlockType)) \
|
||||||
} \
|
{ \
|
||||||
} \
|
return DoorHandler::Callback; \
|
||||||
} \
|
} \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
} while (false)
|
} while (false)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user