Cuberite
A lightweight, fast and extensible game server for Minecraft
DaylightSensorHandler.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "World.h"
5 
6 
7 
8 
9 
11 {
12  static PowerLevel GetPowerLevel(const cChunk & a_Chunk, const Vector3i a_Position)
13  {
14  if (a_Chunk.GetBlock(a_Position) == E_BLOCK_INVERTED_DAYLIGHT_SENSOR)
15  {
16  // Inverted sensor directly returns darkened skylight, no fancy tricks:
17  return 15 - a_Chunk.GetSkyLightAltered(a_Position);
18  }
19 
20  // The [0, 1) proportion of the current day that has elapsed.
21  const auto ProportionOfDay = a_Chunk.GetWorld()->GetTimeOfDay().count() * (static_cast<float>(M_PI) / 12000.f);
22 
23  // The curved value of darkened skylight, with outputs somewhat similar to Vanilla.
24  const auto RawOutput = a_Chunk.GetSkyLightAltered(a_Position) * (0.6f * std::sin(ProportionOfDay) + 0.5f);
25 
26  // Saturate the amplified sine curve at 0 and 15:
27  return static_cast<PowerLevel>(std::clamp(RawOutput, 0.f, 15.f));
28  }
29 
30  static PowerLevel GetPowerDeliveredToPosition(const cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType, bool IsLinked)
31  {
32  UNUSED(a_Chunk);
33  UNUSED(a_BlockType);
34  UNUSED(a_QueryPosition);
35 
36  // Daylight sensors only output to immediately surrounding blocks:
37  return IsLinked ? 0 : a_Chunk.GetMeta(a_Position);
38  }
39 
40  static void Update(cChunk & a_Chunk, cChunk & CurrentlyTicking, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, const PowerLevel Power)
41  {
42  // LOGD("Evaluating Darryl the daylight sensor (%d %d %d)", a_Position.x, a_Position.y, a_Position.z);
43 
44  // What the sensor should output according to the time-power function.
45  const auto PowerLevel = GetPowerLevel(a_Chunk, a_Position);
46 
47  // Only update the output if the power level has changed:
48  if (PowerLevel != a_Meta)
49  {
50  a_Chunk.SetMeta(a_Position, PowerLevel);
51  UpdateAdjustedRelatives(a_Chunk, CurrentlyTicking, a_Position, RelativeAdjacents);
52  }
53  }
54 
55  static void ForValidSourcePositions(const cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, ForEachSourceCallback & Callback)
56  {
57  UNUSED(a_Chunk);
58  UNUSED(a_Position);
59  UNUSED(a_BlockType);
60  UNUSED(a_Meta);
61  UNUSED(Callback);
62  }
63 };
@ E_BLOCK_INVERTED_DAYLIGHT_SENSOR
Definition: BlockType.h:197
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
#define UNUSED
Definition: Globals.h:72
constexpr std::array< Vector3i, 6 > RelativeAdjacents
void UpdateAdjustedRelatives(const cChunk &a_Chunk, const cChunk &a_TickingChunk, const Vector3i a_Position, const ArrayType &a_Relative)
unsigned char PowerLevel
unsigned char Power(const BlockState Block)
static PowerLevel GetPowerDeliveredToPosition(const cChunk &a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType, bool IsLinked)
static void Update(cChunk &a_Chunk, cChunk &CurrentlyTicking, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, const PowerLevel Power)
static void ForValidSourcePositions(const cChunk &a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, ForEachSourceCallback &Callback)
static PowerLevel GetPowerLevel(const cChunk &a_Chunk, const Vector3i a_Position)
Definition: Chunk.h:36
NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:279
BLOCKTYPE GetBlock(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:146
NIBBLETYPE GetSkyLightAltered(Vector3i a_RelPos) const
Get the level of sky light illuminating the block (0 - 15), taking daytime into a account.
Definition: Chunk.h:310
void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta)
Definition: Chunk.h:286
cWorld * GetWorld(void) const
Definition: Chunk.h:135
virtual cTickTime GetTimeOfDay(void) const override
Definition: World.cpp:480