1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
#include "stdafx.h"
#include "net.minecraft.h"
#include "net.minecraft.world.level.redstone.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.h"
#include "DiodeTile.h"
DiodeTile::DiodeTile(int id, bool on) : DirectionalTile(id, Material::decoration,isSolidRender())
{
this->on = on;
updateDefaultShape();
}
// 4J Added override
void DiodeTile::updateDefaultShape()
{
setShape(0, 0, 0, 1, 2.0f / 16.0f, 1);
}
bool DiodeTile::isCubeShaped()
{
return false;
}
bool DiodeTile::mayPlace(Level *level, int x, int y, int z)
{
if (!level->isTopSolidBlocking(x, y - 1, z))
{
return false;
}
return Tile::mayPlace(level, x, y, z);
}
bool DiodeTile::canSurvive(Level *level, int x, int y, int z)
{
if (!level->isTopSolidBlocking(x, y - 1, z))
{
return false;
}
return Tile::canSurvive(level, x, y, z);
}
void DiodeTile::tick(Level *level, int x, int y, int z, Random *random)
{
int data = level->getData(x, y, z);
if (!isLocked(level, x, y, z, data))
{
bool sourceOn = shouldTurnOn(level, x, y, z, data);
if (on && !sourceOn)
{
level->setTileAndData(x, y, z, getOffTile()->id, data, Tile::UPDATE_CLIENTS);
}
else if (!on)
{
// when off-diodes are ticked, they always turn on for one tick and
// then off again if necessary
level->setTileAndData(x, y, z, getOnTile()->id, data, Tile::UPDATE_CLIENTS);
if (!sourceOn)
{
level->addToTickNextTick(x, y, z, getOnTile()->id, getTurnOffDelay(data), -1);
}
}
}
}
Icon *DiodeTile::getTexture(int face, int data)
{
// down is used by the torch tesselator
if (face == Facing::DOWN)
{
if (on)
{
return Tile::redstoneTorch_on->getTexture(face);
}
return Tile::redstoneTorch_off->getTexture(face);
}
if (face == Facing::UP)
{
return icon;
}
// edge of stone half-step
return Tile::stoneSlab->getTexture(Facing::UP);
}
bool DiodeTile::shouldRenderFace(LevelSource *level, int x, int y, int z, int face)
{
if (face == Facing::DOWN || face == Facing::UP)
{
// up and down is a special case handled by the shape renderer
return false;
}
return true;
}
int DiodeTile::getRenderShape()
{
return SHAPE_DIODE;
}
bool DiodeTile::isOn(int data)
{
return on;
}
int DiodeTile::getDirectSignal(LevelSource *level, int x, int y, int z, int dir)
{
return getSignal(level, x, y, z, dir);
}
int DiodeTile::getSignal(LevelSource *level, int x, int y, int z, int facing)
{
int data = level->getData(x, y, z);
if (!isOn(data))
{
return Redstone::SIGNAL_NONE;
}
int dir = getDirection(data);
if (dir == Direction::SOUTH && facing == Facing::SOUTH) return getOutputSignal(level, x, y, z, data);
if (dir == Direction::WEST && facing == Facing::WEST) return getOutputSignal(level, x, y, z, data);
if (dir == Direction::NORTH && facing == Facing::NORTH) return getOutputSignal(level, x, y, z, data);
if (dir == Direction::EAST && facing == Facing::EAST) return getOutputSignal(level, x, y, z, data);
return Redstone::SIGNAL_NONE;
}
void DiodeTile::neighborChanged(Level *level, int x, int y, int z, int type)
{
if (!canSurvive(level, x, y, z))
{
this->spawnResources(level, x, y, z, level->getData(x, y, z), 0);
level->removeTile(x, y, z);
level->updateNeighborsAt(x + 1, y, z, id);
level->updateNeighborsAt(x - 1, y, z, id);
level->updateNeighborsAt(x, y, z + 1, id);
level->updateNeighborsAt(x, y, z - 1, id);
level->updateNeighborsAt(x, y - 1, z, id);
level->updateNeighborsAt(x, y + 1, z, id);
return;
}
checkTickOnNeighbor(level, x, y, z, type);
}
void DiodeTile::checkTickOnNeighbor(Level *level, int x, int y, int z, int type)
{
int data = level->getData(x, y, z);
if (!isLocked(level, x, y, z, data))
{
bool sourceOn = shouldTurnOn(level, x, y, z, data);
if ((on && !sourceOn || !on && sourceOn) && !level->isTileToBeTickedAt(x, y, z, id))
{
int prio = -1;
// if the tile in front is a repeater, we prioritize this update
if (shouldPrioritize(level, x, y, z, data))
{
prio = -3;
}
else if (on)
{
prio = -2;
}
level->addToTickNextTick(x, y, z, id, getTurnOnDelay(data), prio);
}
}
}
bool DiodeTile::isLocked(LevelSource *level, int x, int y, int z, int data)
{
return false;
}
bool DiodeTile::shouldTurnOn(Level *level, int x, int y, int z, int data)
{
return getInputSignal(level, x, y, z, data) > Redstone::SIGNAL_NONE;
}
int DiodeTile::getInputSignal(Level *level, int x, int y, int z, int data)
{
int dir = getDirection(data);
int xx = x + Direction::STEP_X[dir];
int zz = z + Direction::STEP_Z[dir];
int input = level->getSignal(xx, y, zz, Direction::DIRECTION_FACING[dir]);
if (input >= Redstone::SIGNAL_MAX) return input;
return max(input, level->getTile(xx, y, zz) == Tile::redStoneDust_Id ? level->getData(xx, y, zz) : Redstone::SIGNAL_NONE);
}
int DiodeTile::getAlternateSignal(LevelSource *level, int x, int y, int z, int data)
{
int dir = getDirection(data);
switch (dir)
{
case Direction::SOUTH:
case Direction::NORTH:
return max(getAlternateSignalAt(level, x - 1, y, z, Facing::WEST), getAlternateSignalAt(level, x + 1, y, z, Facing::EAST));
case Direction::EAST:
case Direction::WEST:
return max(getAlternateSignalAt(level, x, y, z + 1, Facing::SOUTH), getAlternateSignalAt(level, x, y, z - 1, Facing::NORTH));
}
return Redstone::SIGNAL_NONE;
}
int DiodeTile::getAlternateSignalAt(LevelSource *level, int x, int y, int z, int facing)
{
int tile = level->getTile(x, y, z);
if (isAlternateInput(tile))
{
if (tile == Tile::redStoneDust_Id)
{
return level->getData(x, y, z);
}
else
{
return level->getDirectSignal(x, y, z, facing);
}
}
return Redstone::SIGNAL_NONE;
}
bool DiodeTile::isSignalSource()
{
return true;
}
void DiodeTile::setPlacedBy(Level *level, int x, int y, int z, shared_ptr<LivingEntity> by, shared_ptr<ItemInstance> itemInstance)
{
int dir = (((Mth::floor(by->yRot * 4 / (360) + 0.5)) & 3) + 2) % 4;
level->setData(x, y, z, dir, Tile::UPDATE_ALL);
bool sourceOn = shouldTurnOn(level, x, y, z, dir);
if (sourceOn)
{
level->addToTickNextTick(x, y, z, id, 1);
}
}
void DiodeTile::onPlace(Level *level, int x, int y, int z)
{
updateNeighborsInFront(level, x, y, z);
}
void DiodeTile::updateNeighborsInFront(Level *level, int x, int y, int z)
{
int dir = getDirection(level->getData(x, y, z));
if (dir == Direction::WEST)
{
level->neighborChanged(x + 1, y, z, id);
level->updateNeighborsAtExceptFromFacing(x + 1, y, z, id, Facing::WEST);
}
if (dir == Direction::EAST)
{
level->neighborChanged(x - 1, y, z, id);
level->updateNeighborsAtExceptFromFacing(x - 1, y, z, id, Facing::EAST);
}
if (dir == Direction::NORTH)
{
level->neighborChanged(x, y, z + 1, id);
level->updateNeighborsAtExceptFromFacing(x, y, z + 1, id, Facing::NORTH);
}
if (dir == Direction::SOUTH)
{
level->neighborChanged(x, y, z - 1, id);
level->updateNeighborsAtExceptFromFacing(x, y, z - 1, id, Facing::SOUTH);
}
}
void DiodeTile::destroy(Level *level, int x, int y, int z, int data)
{
if (on)
{
level->updateNeighborsAt(x + 1, y, z, id);
level->updateNeighborsAt(x - 1, y, z, id);
level->updateNeighborsAt(x, y, z + 1, id);
level->updateNeighborsAt(x, y, z - 1, id);
level->updateNeighborsAt(x, y - 1, z, id);
level->updateNeighborsAt(x, y + 1, z, id);
}
Tile::destroy(level, x, y, z, data);
}
bool DiodeTile::isSolidRender(bool isServerLevel)
{
return false;
}
bool DiodeTile::isAlternateInput(int tile)
{
Tile *tt = Tile::tiles[tile];
return tt != nullptr && tt->isSignalSource();
}
int DiodeTile::getOutputSignal(LevelSource *level, int x, int y, int z, int data)
{
return Redstone::SIGNAL_MAX;
}
bool DiodeTile::isDiode(int id)
{
return Tile::diode_off->isSameDiode(id) || Tile::comparator_off->isSameDiode(id);
}
bool DiodeTile::isSameDiode(int id)
{
return id == getOnTile()->id || id == getOffTile()->id;
}
bool DiodeTile::shouldPrioritize(Level *level, int x, int y, int z, int data)
{
int dir = getDirection(data);
if (isDiode(level->getTile(x - Direction::STEP_X[dir], y, z - Direction::STEP_Z[dir])))
{
int odata = level->getData(x - Direction::STEP_X[dir], y, z - Direction::STEP_Z[dir]);
int odir = getDirection(odata);
return odir != dir;
}
return false;
}
int DiodeTile::getTurnOffDelay(int data)
{
return getTurnOnDelay(data);
}
bool DiodeTile::isMatching(int id)
{
return isSameDiode(id);
}
|