2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-08-28 20:51:25 -04:00
|
|
|
#include "../../BoundingBox.h"
|
2020-04-13 18:23:43 -04:00
|
|
|
#include "../../Entities/Pickup.h"
|
2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-19 16:14:40 -04:00
|
|
|
namespace PressurePlateHandler
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2021-01-21 17:51:55 -05:00
|
|
|
static unsigned char GetPowerLevel(const cChunk & Chunk, const Vector3i Position, const BLOCKTYPE BlockType)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-10-05 08:09:42 -04:00
|
|
|
size_t NumberOfEntities = 0;
|
2017-09-25 16:41:11 -04:00
|
|
|
bool FoundPlayer = false;
|
2020-07-26 09:15:00 -04:00
|
|
|
|
|
|
|
Chunk.ForEachEntityInBox(cBoundingBox(Vector3d(0.5, 0, 0.5) + Position, 0.5, 0.5), [&](cEntity & Entity)
|
|
|
|
{
|
|
|
|
if (Entity.IsPlayer())
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
FoundPlayer = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Entity.IsPickup())
|
|
|
|
{
|
2020-10-05 08:09:42 -04:00
|
|
|
const auto & Pickup = static_cast<cPickup &>(Entity);
|
|
|
|
NumberOfEntities += static_cast<size_t>(Pickup.GetItem().m_ItemCount);
|
2015-06-26 18:24:51 -04:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-26 09:15:00 -04:00
|
|
|
NumberOfEntities++;
|
|
|
|
return false;
|
|
|
|
});
|
2015-06-26 18:24:51 -04:00
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
switch (BlockType)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
|
|
|
case E_BLOCK_STONE_PRESSURE_PLATE:
|
|
|
|
{
|
2020-10-05 08:09:42 -04:00
|
|
|
return FoundPlayer ? 15 : 0;
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
case E_BLOCK_WOODEN_PRESSURE_PLATE:
|
|
|
|
{
|
2020-10-05 08:09:42 -04:00
|
|
|
return (NumberOfEntities != 0) ? 15 : 0;
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE:
|
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
return std::min(static_cast<unsigned char>(CeilC(NumberOfEntities / 10.f)), static_cast<unsigned char>(15));
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE:
|
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
return std::min(static_cast<unsigned char>(NumberOfEntities), static_cast<unsigned char>(15));
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"Unhandled/unimplemented block in pressure plate handler!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 17:51:55 -05:00
|
|
|
static const char * GetClickOnSound(BLOCKTYPE a_BlockType)
|
2020-08-19 16:14:40 -04:00
|
|
|
{
|
|
|
|
// manage on-sound
|
|
|
|
switch (a_BlockType)
|
|
|
|
{
|
|
|
|
case E_BLOCK_STONE_PRESSURE_PLATE: return "block.stone_pressureplate.click_on";
|
|
|
|
case E_BLOCK_WOODEN_PRESSURE_PLATE: return "block.wood_pressureplate.click_on";
|
|
|
|
case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE:
|
|
|
|
case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE: return "block.metal_pressureplate.click_on";
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"No on sound for this one!");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 17:51:55 -05:00
|
|
|
static const char * GetClickOffSound(BLOCKTYPE a_BlockType)
|
2020-08-19 16:14:40 -04:00
|
|
|
{
|
|
|
|
// manage off-sound
|
|
|
|
switch (a_BlockType)
|
|
|
|
{
|
|
|
|
case E_BLOCK_STONE_PRESSURE_PLATE: return "block.stone_pressureplate.click_off";
|
|
|
|
case E_BLOCK_WOODEN_PRESSURE_PLATE: return "block.wood_pressureplate.click_off";
|
|
|
|
case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE:
|
|
|
|
case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE: return "block.metal_pressureplate.click_off";
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"No off sound for this one!");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-19 16:14:40 -04:00
|
|
|
{
|
|
|
|
UNUSED(a_BlockType);
|
|
|
|
UNUSED(a_QueryPosition);
|
|
|
|
UNUSED(a_QueryBlockType);
|
|
|
|
|
|
|
|
// Plates only link power blocks below
|
|
|
|
// Retrieve and return the cached power calculated by Update for performance:
|
2020-08-20 15:04:28 -04:00
|
|
|
return (IsLinked && (a_QueryPosition != (a_Position + OffsetYM))) ? 0 : DataForChunk(a_Chunk).GetCachedPowerData(a_Position);
|
2020-08-19 16:14:40 -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-07-26 09:15:00 -04:00
|
|
|
{
|
|
|
|
// LOGD("Evaluating clicky the pressure plate (%d %d %d)", a_Position.x, a_Position.y, a_Position.z);
|
2020-04-13 18:23:43 -04:00
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
auto & ChunkData = DataForChunk(a_Chunk);
|
2020-04-13 18:23:43 -04:00
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
const auto PreviousPower = ChunkData.GetCachedPowerData(a_Position);
|
|
|
|
const auto Absolute = cChunkDef::RelativeToAbsolute(a_Position, a_Chunk.GetPos());
|
2020-08-20 15:04:28 -04:00
|
|
|
const auto PowerLevel = GetPowerLevel(a_Chunk, Absolute, a_BlockType); // Get the current power of the platey
|
2020-07-26 09:15:00 -04:00
|
|
|
const auto DelayInfo = ChunkData.GetMechanismDelayInfo(a_Position);
|
2015-06-26 18:24:51 -04:00
|
|
|
|
2020-04-13 18:23:43 -04:00
|
|
|
// Resting state?
|
|
|
|
if (DelayInfo == nullptr)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-08-20 15:04:28 -04:00
|
|
|
if (PowerLevel == 0)
|
2020-04-13 18:23:43 -04:00
|
|
|
{
|
|
|
|
// Nothing happened, back to rest
|
2020-07-26 09:15:00 -04:00
|
|
|
return;
|
2020-04-13 18:23:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// From rest, a player stepped on us
|
|
|
|
// Schedule a minimum 0.5 second delay before even thinking about releasing
|
2020-07-26 09:15:00 -04:00
|
|
|
ChunkData.m_MechanismDelays[a_Position] = std::make_pair(5, true);
|
2020-04-13 18:23:43 -04:00
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
a_Chunk.GetWorld()->BroadcastSoundEffect(GetClickOnSound(a_BlockType), Absolute, 0.5f, 0.6f);
|
2020-04-13 18:23:43 -04:00
|
|
|
|
|
|
|
// Update power
|
2020-08-20 15:04:28 -04:00
|
|
|
ChunkData.SetCachedPowerData(a_Position, PowerLevel);
|
2020-04-13 18:23:43 -04:00
|
|
|
|
|
|
|
// Immediately depress plate
|
2020-07-26 09:15:00 -04:00
|
|
|
a_Chunk.SetMeta(a_Position, E_META_PRESSURE_PLATE_DEPRESSED);
|
2020-08-08 13:22:16 -04:00
|
|
|
|
|
|
|
UpdateAdjustedRelatives(a_Chunk, CurrentlyTicking, a_Position, RelativeAdjacents);
|
|
|
|
return;
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:23:43 -04:00
|
|
|
// Not a resting state
|
|
|
|
|
|
|
|
int DelayTicks;
|
|
|
|
bool HasExitedMinimumOnDelayPhase;
|
|
|
|
std::tie(DelayTicks, HasExitedMinimumOnDelayPhase) = *DelayInfo;
|
|
|
|
|
|
|
|
// Are we waiting for the initial delay or subsequent release delay?
|
|
|
|
if (DelayTicks > 0)
|
|
|
|
{
|
|
|
|
// Nothing changes, if there is nothing on it anymore, because the state is locked.
|
2020-08-20 15:04:28 -04:00
|
|
|
if (PowerLevel == 0)
|
2020-04-13 18:23:43 -04:00
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
return;
|
2020-04-13 18:23:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Yes. Are we waiting to release, and found that the player stepped on it again?
|
|
|
|
if (!HasExitedMinimumOnDelayPhase)
|
|
|
|
{
|
|
|
|
// Reset delay
|
|
|
|
*DelayInfo = std::make_pair(0, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Did the power level change and is still above zero?
|
2020-08-20 15:04:28 -04:00
|
|
|
if (PowerLevel != PreviousPower)
|
2020-04-13 18:23:43 -04:00
|
|
|
{
|
|
|
|
// Yes. Update power
|
2020-08-20 15:04:28 -04:00
|
|
|
ChunkData.SetCachedPowerData(a_Position, PowerLevel);
|
2020-08-08 13:22:16 -04:00
|
|
|
UpdateAdjustedRelatives(a_Chunk, CurrentlyTicking, a_Position, RelativeAdjacents);
|
2020-04-13 18:23:43 -04:00
|
|
|
}
|
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
return;
|
2020-04-13 18:23:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not waiting for anything. Has the initial delay elapsed?
|
|
|
|
if (HasExitedMinimumOnDelayPhase)
|
|
|
|
{
|
|
|
|
// Yep, initial delay elapsed. Has the player gotten off?
|
2020-08-20 15:04:28 -04:00
|
|
|
if (PowerLevel == 0)
|
2020-04-13 18:23:43 -04:00
|
|
|
{
|
|
|
|
// Yes. Go into subsequent release delay, for a further 0.5 seconds
|
|
|
|
*DelayInfo = std::make_pair(5, false);
|
2020-07-26 09:15:00 -04:00
|
|
|
return;
|
2020-04-13 18:23:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Did the power level change and is still above zero?
|
2020-08-20 15:04:28 -04:00
|
|
|
if (PowerLevel != PreviousPower)
|
2020-04-13 18:23:43 -04:00
|
|
|
{
|
|
|
|
// Yes. Update power
|
2020-08-20 15:04:28 -04:00
|
|
|
ChunkData.SetCachedPowerData(a_Position, PowerLevel);
|
2020-08-08 13:22:16 -04:00
|
|
|
UpdateAdjustedRelatives(a_Chunk, CurrentlyTicking, a_Position, RelativeAdjacents);
|
2020-04-13 18:23:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Yes, but player's still on the plate, do nothing
|
2020-07-26 09:15:00 -04:00
|
|
|
return;
|
2020-04-13 18:23:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Just got out of the subsequent release phase, reset everything and raise the plate
|
2020-07-26 09:15:00 -04:00
|
|
|
ChunkData.m_MechanismDelays.erase(a_Position);
|
2020-04-13 18:23:43 -04:00
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
a_Chunk.GetWorld()->BroadcastSoundEffect(GetClickOffSound(a_BlockType), Absolute, 0.5f, 0.5f);
|
2020-08-20 15:04:28 -04:00
|
|
|
ChunkData.SetCachedPowerData(a_Position, PowerLevel);
|
2020-04-13 18:23:43 -04:00
|
|
|
|
2020-07-26 09:15:00 -04:00
|
|
|
a_Chunk.SetMeta(a_Position, E_META_PRESSURE_PLATE_RAISED);
|
2020-08-08 13:22:16 -04:00
|
|
|
UpdateAdjustedRelatives(a_Chunk, CurrentlyTicking, a_Position, RelativeAdjacents);
|
2015-06-26 18:24:51 -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)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-07-26 09:15:00 -04:00
|
|
|
UNUSED(a_Chunk);
|
2015-06-26 18:24:51 -04:00
|
|
|
UNUSED(a_Position);
|
|
|
|
UNUSED(a_BlockType);
|
|
|
|
UNUSED(a_Meta);
|
2020-07-26 09:15:00 -04:00
|
|
|
UNUSED(Callback);
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
};
|