the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "net.minecraft.world.item.h"
3#include "net.minecraft.world.level.h"
4#include "net.minecraft.world.level.redstone.h"
5#include "net.minecraft.world.level.tile.h"
6#include "net.minecraft.world.level.tile.entity.h"
7#include "net.minecraft.h"
8#include "ComparatorTile.h"
9
10ComparatorTile::ComparatorTile(int id, bool on) : DiodeTile(id, on)
11{
12 _isEntityTile = true;
13}
14
15int ComparatorTile::getResource(int data, Random *random, int playerBonusLevel)
16{
17 return Item::comparator_Id;
18}
19
20int ComparatorTile::cloneTileId(Level *level, int x, int y, int z)
21{
22 return Item::comparator_Id;
23}
24
25int ComparatorTile::getTurnOnDelay(int data)
26{
27 return 2;
28}
29
30DiodeTile *ComparatorTile::getOnTile()
31{
32 return Tile::comparator_on;
33}
34
35DiodeTile *ComparatorTile::getOffTile()
36{
37 return Tile::comparator_off;
38}
39
40int ComparatorTile::getRenderShape()
41{
42 return SHAPE_COMPARATOR;
43}
44
45Icon *ComparatorTile::getTexture(int face, int data)
46{
47 bool isOn = on || (data & BIT_IS_LIT) != 0;
48 // down is used by the torch tesselator
49 if (face == Facing::DOWN)
50 {
51 if (isOn)
52 {
53 return Tile::redstoneTorch_on->getTexture(face);
54 }
55 return Tile::redstoneTorch_off->getTexture(face);
56 }
57 if (face == Facing::UP)
58 {
59 if (isOn)
60 {
61 return Tile::comparator_on->icon;
62 }
63 return icon;
64 }
65 // edge of stone half-step
66 return Tile::stoneSlab->getTexture(Facing::UP);
67}
68
69bool ComparatorTile::isOn(int data)
70{
71 return on || (data & BIT_IS_LIT) != 0;
72}
73
74int ComparatorTile::getOutputSignal(LevelSource *levelSource, int x, int y, int z, int data)
75{
76 return getComparator(levelSource, x, y, z)->getOutputSignal();
77}
78
79int ComparatorTile::calculateOutputSignal(Level *level, int x, int y, int z, int data)
80{
81 if (!isReversedOutputSignal(data))
82 {
83 return getInputSignal(level, x, y, z, data);
84 }
85 else
86 {
87 return max(getInputSignal(level, x, y, z, data) - getAlternateSignal(level, x, y, z, data), Redstone::SIGNAL_NONE);
88 }
89}
90
91bool ComparatorTile::isReversedOutputSignal(int data)
92{
93 return (data & BIT_OUTPUT_SUBTRACT) == BIT_OUTPUT_SUBTRACT;
94}
95
96bool ComparatorTile::shouldTurnOn(Level *level, int x, int y, int z, int data)
97{
98 int input = getInputSignal(level, x, y, z, data);
99 if (input >= Redstone::SIGNAL_MAX) return true;
100 if (input == Redstone::SIGNAL_NONE) return false;
101
102 int alt = getAlternateSignal(level, x, y, z, data);
103 if (alt == Redstone::SIGNAL_NONE) return true;
104
105 return input >= alt;
106}
107
108int ComparatorTile::getInputSignal(Level *level, int x, int y, int z, int data)
109{
110 int result = DiodeTile::getInputSignal(level, x, y, z, data);
111
112 int dir = getDirection(data);
113 int xx = x + Direction::STEP_X[dir];
114 int zz = z + Direction::STEP_Z[dir];
115 int tile = level->getTile(xx, y, zz);
116
117 if (tile > 0)
118 {
119 if (Tile::tiles[tile]->hasAnalogOutputSignal())
120 {
121 result = Tile::tiles[tile]->getAnalogOutputSignal(level, xx, y, zz, Direction::DIRECTION_OPPOSITE[dir]);
122 }
123 else if (result < Redstone::SIGNAL_MAX && Tile::isSolidBlockingTile(tile))
124 {
125 xx += Direction::STEP_X[dir];
126 zz += Direction::STEP_Z[dir];
127 tile = level->getTile(xx, y, zz);
128
129 if (tile > 0 && Tile::tiles[tile]->hasAnalogOutputSignal())
130 {
131 result = Tile::tiles[tile]->getAnalogOutputSignal(level, xx, y, zz, Direction::DIRECTION_OPPOSITE[dir]);
132 }
133 }
134 }
135
136 return result;
137}
138
139shared_ptr<ComparatorTileEntity> ComparatorTile::getComparator(LevelSource *level, int x, int y, int z)
140{
141 return dynamic_pointer_cast<ComparatorTileEntity>( level->getTileEntity(x, y, z) );
142}
143
144bool ComparatorTile::use(Level *level, int x, int y, int z, shared_ptr<Player> player, int clickedFace, float clickX, float clickY, float clickZ, bool soundOnly)
145{
146 int data = level->getData(x, y, z);
147 bool isOn = on || ( (data & BIT_IS_LIT) != 0 );
148 bool subtract = !isReversedOutputSignal(data);
149 int outputBit = subtract ? BIT_OUTPUT_SUBTRACT : 0;
150 outputBit |= isOn ? BIT_IS_LIT : 0;
151
152 level->playSound(x + 0.5, y + 0.5, z + 0.5, eSoundType_RANDOM_CLICK, 0.3f, subtract ? 0.55f : 0.5f);
153
154 if (!soundOnly)
155 {
156 level->setData(x, y, z, outputBit | (data & DIRECTION_MASK), Tile::UPDATE_CLIENTS);
157 refreshOutputState(level, x, y, z, level->random);
158 }
159
160 return true;
161}
162
163void ComparatorTile::checkTickOnNeighbor(Level *level, int x, int y, int z, int type)
164{
165 if (!level->isTileToBeTickedAt(x, y, z, id))
166 {
167 int data = level->getData(x, y, z);
168 int outputValue = calculateOutputSignal(level, x, y, z, data);
169 int oldValue = getComparator(level, x, y, z)->getOutputSignal();
170
171 if (outputValue != oldValue || (isOn(data) != shouldTurnOn(level, x, y, z, data)))
172 {
173 // prioritize locking comparators
174 if (shouldPrioritize(level, x, y, z, data))
175 {
176 level->addToTickNextTick(x, y, z, id, getTurnOnDelay(0), -1);
177 }
178 else
179 {
180 level->addToTickNextTick(x, y, z, id, getTurnOnDelay(0), 0);
181 }
182 }
183 }
184}
185
186void ComparatorTile::refreshOutputState(Level *level, int x, int y, int z, Random *random)
187{
188 int data = level->getData(x, y, z);
189 int outputValue = calculateOutputSignal(level, x, y, z, data);
190 int oldValue = getComparator(level, x, y, z)->getOutputSignal();
191 getComparator(level, x, y, z)->setOutputSignal(outputValue);
192
193 if (oldValue != outputValue || !isReversedOutputSignal(data))
194 {
195 bool sourceOn = shouldTurnOn(level, x, y, z, data);
196 bool isOn = on || (data & BIT_IS_LIT) != 0;
197 if (isOn && !sourceOn)
198 {
199 level->setData(x, y, z, data & ~BIT_IS_LIT, Tile::UPDATE_CLIENTS);
200 }
201 else if (!isOn && sourceOn)
202 {
203 level->setData(x, y, z, data | BIT_IS_LIT, Tile::UPDATE_CLIENTS);
204 }
205 updateNeighborsInFront(level, x, y, z);
206 }
207}
208
209void ComparatorTile::tick(Level *level, int x, int y, int z, Random *random)
210{
211 if (on)
212 {
213 // clean-up old tiles with the 'on' id
214 int data = level->getData(x, y, z);
215 level->setTileAndData(x, y, z, getOffTile()->id, data | BIT_IS_LIT, Tile::UPDATE_NONE);
216 }
217 refreshOutputState(level, x, y, z, random);
218}
219
220void ComparatorTile::onPlace(Level *level, int x, int y, int z)
221{
222 DiodeTile::onPlace(level, x, y, z);
223 level->setTileEntity(x, y, z, newTileEntity(level));
224}
225
226void ComparatorTile::onRemove(Level *level, int x, int y, int z, int id, int data)
227{
228 DiodeTile::onRemove(level, x, y, z, id, data);
229 level->removeTileEntity(x, y, z);
230
231 updateNeighborsInFront(level, x, y, z);
232}
233
234bool ComparatorTile::triggerEvent(Level *level, int x, int y, int z, int b0, int b1)
235{
236 DiodeTile::triggerEvent(level, x, y, z, b0, b1);
237 shared_ptr<TileEntity> te = level->getTileEntity(x, y, z);
238 if (te != NULL)
239 {
240 return te->triggerEvent(b0, b1);
241 }
242 return false;
243}
244
245shared_ptr<TileEntity> ComparatorTile::newTileEntity(Level *level)
246{
247 return shared_ptr<ComparatorTileEntity>( new ComparatorTileEntity() );
248}
249
250bool ComparatorTile::TestUse()
251{
252 return true;
253}