Cuberite
A lightweight, fast and extensible game server for Minecraft
RedstoneHandler.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h"
3 
4 #include "RedstoneHandler.h"
5 #include "RedstoneDataHelper.h"
7 
8 #include "BlockType.h"
9 #include "CommandBlockHandler.h"
10 #include "DaylightSensorHandler.h"
11 #include "DoorHandler.h"
12 #include "RedstoneTorchHandler.h"
13 #include "RedstoneWireHandler.h"
15 #include "RedstoneToggleHandler.h"
16 #include "RedstoneLampHandler.h"
17 #include "RedstoneBlockHandler.h"
18 #include "PistonHandler.h"
19 #include "SmallGateHandler.h"
20 #include "NoteBlockHandler.h"
21 #include "ObserverHandler.h"
22 #include "TNTHandler.h"
23 #include "PoweredRailHandler.h"
24 #include "PressurePlateHandler.h"
25 #include "TripwireHookHandler.h"
26 #include "DropSpenserHandler.h"
28 #include "TrappedChestHandler.h"
29 #include "HopperHandler.h"
30 
31 
32 
33 
34 
35 #define INVOKE_FOR_HANDLERS(Callback) \
36  do \
37  { \
38  switch (BlockType) \
39  { \
40  case E_BLOCK_ACTIVATOR_RAIL: \
41  case E_BLOCK_DETECTOR_RAIL: \
42  case E_BLOCK_POWERED_RAIL: return PoweredRailHandler::Callback; \
43  case E_BLOCK_ACTIVE_COMPARATOR: \
44  case E_BLOCK_INACTIVE_COMPARATOR: return RedstoneComparatorHandler::Callback; \
45  case E_BLOCK_DISPENSER: \
46  case E_BLOCK_DROPPER: return DropSpenserHandler::Callback; \
47  case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE: \
48  case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE: \
49  case E_BLOCK_STONE_PRESSURE_PLATE: \
50  case E_BLOCK_WOODEN_PRESSURE_PLATE: return PressurePlateHandler::Callback; \
51  case E_BLOCK_ACACIA_FENCE_GATE: \
52  case E_BLOCK_BIRCH_FENCE_GATE: \
53  case E_BLOCK_DARK_OAK_FENCE_GATE: \
54  case E_BLOCK_FENCE_GATE: \
55  case E_BLOCK_IRON_TRAPDOOR: \
56  case E_BLOCK_JUNGLE_FENCE_GATE: \
57  case E_BLOCK_SPRUCE_FENCE_GATE: \
58  case E_BLOCK_TRAPDOOR: return SmallGateHandler::Callback; \
59  case E_BLOCK_REDSTONE_LAMP_OFF: \
60  case E_BLOCK_REDSTONE_LAMP_ON: return RedstoneLampHandler::Callback; \
61  case E_BLOCK_REDSTONE_REPEATER_OFF: \
62  case E_BLOCK_REDSTONE_REPEATER_ON: return RedstoneRepeaterHandler::Callback; \
63  case E_BLOCK_REDSTONE_TORCH_OFF: \
64  case E_BLOCK_REDSTONE_TORCH_ON: return RedstoneTorchHandler::Callback; \
65  case E_BLOCK_OBSERVER: return ObserverHandler::Callback; \
66  case E_BLOCK_PISTON: \
67  case E_BLOCK_STICKY_PISTON: return PistonHandler::Callback; \
68  case E_BLOCK_DAYLIGHT_SENSOR: \
69  case E_BLOCK_INVERTED_DAYLIGHT_SENSOR: return DaylightSensorHandler::Callback; \
70  case E_BLOCK_LEVER: \
71  case E_BLOCK_STONE_BUTTON: \
72  case E_BLOCK_WOODEN_BUTTON: return RedstoneToggleHandler::Callback; \
73  case E_BLOCK_BLOCK_OF_REDSTONE: return RedstoneBlockHandler::Callback; \
74  case E_BLOCK_COMMAND_BLOCK: return CommandBlockHandler::Callback; \
75  case E_BLOCK_HOPPER: return HopperHandler::Callback; \
76  case E_BLOCK_NOTE_BLOCK: return NoteBlockHandler::Callback; \
77  case E_BLOCK_REDSTONE_WIRE: return RedstoneWireHandler::Callback; \
78  case E_BLOCK_TNT: return TNTHandler::Callback; \
79  case E_BLOCK_TRAPPED_CHEST: return TrappedChestHandler::Callback; \
80  case E_BLOCK_TRIPWIRE_HOOK: return TripwireHookHandler::Callback; \
81  default: \
82  { \
83  if (cBlockDoorHandler::IsDoorBlockType(BlockType)) \
84  { \
85  return DoorHandler::Callback; \
86  } \
87  } \
88  } \
89  } while (false)
90 
91 
92 
93 
94 
95 namespace RedstoneHandler
96 {
97  PowerLevel GetPowerDeliveredToPosition(const cChunk & Chunk, const Vector3i Position, const BLOCKTYPE BlockType, const Vector3i QueryPosition, const BLOCKTYPE QueryBlockType, const bool IsLinked)
98  {
99  INVOKE_FOR_HANDLERS(GetPowerDeliveredToPosition(Chunk, Position, BlockType, QueryPosition, QueryBlockType, IsLinked));
100 
101  // Fell through the switch statement
102  // Block at Position doesn't have a corresponding redstone handler
103  // ErasePowerData will have been called in AddBlock
104 
105  // Default:
106  return 0;
107  }
108 
109  void Update(cChunk & Chunk, cChunk & CurrentlyTicking, const Vector3i Position, const BLOCKTYPE BlockType, const NIBBLETYPE Meta, const PowerLevel PowerLevel)
110  {
111  INVOKE_FOR_HANDLERS(Update(Chunk, CurrentlyTicking, Position, BlockType, Meta, PowerLevel));
112  }
113 
114  void ForValidSourcePositions(const cChunk & Chunk, const Vector3i Position, const BLOCKTYPE BlockType, const NIBBLETYPE Meta, ForEachSourceCallback & Callback)
115  {
116  INVOKE_FOR_HANDLERS(ForValidSourcePositions(Chunk, Position, BlockType, Meta, Callback));
117  }
118 
119  void SetWireState(const cChunk & Chunk, const Vector3i Position)
120  {
121  RedstoneWireHandler::SetWireState(Chunk, Position);
122  }
123 }
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
BlockType
Definition: BlockTypes.h:4
#define INVOKE_FOR_HANDLERS(Callback)
unsigned char PowerLevel
void SetWireState(const cChunk &Chunk, const Vector3i Position)
Temporary: compute and set the block state of a redstone wire.
void Update(cChunk &Chunk, cChunk &CurrentlyTicking, const Vector3i Position, const BLOCKTYPE BlockType, const NIBBLETYPE Meta, const PowerLevel PowerLevel)
Tells a redstone component at this position to update itself.
PowerLevel GetPowerDeliveredToPosition(const cChunk &Chunk, const Vector3i Position, const BLOCKTYPE BlockType, const Vector3i QueryPosition, const BLOCKTYPE QueryBlockType, const bool IsLinked)
Asks a redstone component at the source position how much power it will deliver to the querying posit...
void ForValidSourcePositions(const cChunk &Chunk, const Vector3i Position, const BLOCKTYPE BlockType, const NIBBLETYPE Meta, ForEachSourceCallback &Callback)
Invokes Callback for each position this component can accept power from.
static void SetWireState(const cChunk &a_Chunk, const Vector3i a_Position)
Temporary.
Definition: Chunk.h:36