2020-04-16 17:27:04 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../../Blocks/BlockObserver.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-19 16:14:40 -04:00
|
|
|
namespace ObserverHandler
|
2020-04-16 17:27:04 -04:00
|
|
|
{
|
2021-01-21 17:51:55 -05:00
|
|
|
static bool IsOn(NIBBLETYPE a_Meta)
|
2020-04-16 17:27:04 -04:00
|
|
|
{
|
|
|
|
return (a_Meta & 0x8) == 0x8;
|
|
|
|
}
|
|
|
|
|
2021-01-21 17:51:55 -05:00
|
|
|
static bool ShouldPowerOn(cChunk & Chunk, const Vector3i a_Position, NIBBLETYPE a_Meta, cIncrementalRedstoneSimulatorChunkData & a_Data)
|
2020-04-16 17:27:04 -04:00
|
|
|
{
|
|
|
|
BLOCKTYPE BlockType;
|
|
|
|
NIBBLETYPE BlockMeta;
|
2020-07-26 09:15:00 -04:00
|
|
|
if (!Chunk.UnboundedRelGetBlock(a_Position + cBlockObserverHandler::GetObservingFaceOffset(a_Meta), BlockType, BlockMeta))
|
2020-04-16 17:27:04 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-20 15:04:28 -04:00
|
|
|
auto & ObserverCache = a_Data.ObserverCache;
|
|
|
|
const auto FindResult = ObserverCache.find(a_Position);
|
|
|
|
const auto Observed = std::make_pair(BlockType, BlockMeta);
|
|
|
|
|
|
|
|
if (FindResult == ObserverCache.end())
|
|
|
|
{
|
|
|
|
// Cache the last seen block for this position:
|
|
|
|
ObserverCache.emplace(a_Position, Observed);
|
|
|
|
|
|
|
|
// Definitely should signal update:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The block this observer previously saw.
|
|
|
|
const auto Previous = FindResult->second;
|
|
|
|
|
|
|
|
// Update the last seen block:
|
|
|
|
FindResult->second = Observed;
|
2020-04-16 17:27:04 -04:00
|
|
|
|
|
|
|
// Determine if to signal an update based on the block previously observed changed
|
2020-08-20 15:04:28 -04:00
|
|
|
return Previous != Observed;
|
2020-04-16 17:27:04 -04:00
|
|
|
}
|
|
|
|
|
2021-01-21 17:51:55 -05:00
|
|
|
static PowerLevel GetPowerDeliveredToPosition(const cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType, bool IsLinked)
|
2020-04-16 17:27:04 -04:00
|
|
|
{
|
2020-08-08 13:22:16 -04:00
|
|
|
const auto Meta = a_Chunk.GetMeta(a_Position);
|
|
|
|
return (IsOn(Meta) && (a_QueryPosition == (a_Position + cBlockObserverHandler::GetSignalOutputOffset(Meta)))) ? 15 : 0;
|
2020-04-16 17:27:04 -04:00
|
|
|
}
|
|
|
|
|
2021-01-21 17:51:55 -05:00
|
|
|
static void Update(cChunk & a_Chunk, cChunk & CurrentlyTicking, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, const PowerLevel Power)
|
2020-04-16 17:27:04 -04:00
|
|
|
{
|
|
|
|
// LOGD("Evaluating Lenny the observer (%i %i %i)", a_Position.x, a_Position.y, a_Position.z);
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
auto & Data = DataForChunk(a_Chunk);
|
|
|
|
auto DelayInfo = Data.GetMechanismDelayInfo(a_Position);
|
2020-04-16 17:27:04 -04:00
|
|
|
|
|
|
|
if (DelayInfo == nullptr)
|
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
if (!ShouldPowerOn(a_Chunk, a_Position, a_Meta, Data))
|
2020-04-16 17:27:04 -04:00
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
return;
|
2020-04-16 17:27:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// From rest, we've determined there was a block update
|
|
|
|
// Schedule power-on 1 tick in the future
|
2020-07-26 09:15:00 -04:00
|
|
|
Data.m_MechanismDelays[a_Position] = std::make_pair(1, true);
|
2020-04-16 17:27:04 -04:00
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
return;
|
2020-04-16 17:27:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int DelayTicks;
|
|
|
|
bool ShouldPowerOn;
|
|
|
|
std::tie(DelayTicks, ShouldPowerOn) = *DelayInfo;
|
|
|
|
|
|
|
|
if (DelayTicks != 0)
|
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
return;
|
2020-04-16 17:27:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ShouldPowerOn)
|
|
|
|
{
|
|
|
|
// Remain on for 1 tick before resetting
|
|
|
|
*DelayInfo = std::make_pair(1, false);
|
2020-07-26 09:15:00 -04:00
|
|
|
a_Chunk.SetMeta(a_Position, a_Meta | 0x8);
|
2020-04-16 17:27:04 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We've reset. Erase delay data in preparation for detecting further updates
|
2020-07-26 09:15:00 -04:00
|
|
|
Data.m_MechanismDelays.erase(a_Position);
|
|
|
|
a_Chunk.SetMeta(a_Position, a_Meta & ~0x8);
|
2020-04-16 17:27:04 -04:00
|
|
|
}
|
|
|
|
|
2020-08-08 13:22:16 -04:00
|
|
|
UpdateAdjustedRelative(a_Chunk, CurrentlyTicking, a_Position, cBlockObserverHandler::GetSignalOutputOffset(a_Meta));
|
2020-04-16 17:27:04 -04:00
|
|
|
}
|
|
|
|
|
2021-01-21 17:51:55 -05:00
|
|
|
static void ForValidSourcePositions(const cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, ForEachSourceCallback & Callback)
|
2020-04-16 17:27:04 -04:00
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
UNUSED(a_Chunk);
|
2020-04-16 17:27:04 -04:00
|
|
|
UNUSED(a_Position);
|
|
|
|
UNUSED(a_BlockType);
|
|
|
|
UNUSED(a_BlockType);
|
|
|
|
}
|
|
|
|
};
|