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 "..\Minecraft.Client\Minecraft.h"
3#include "RedStoneDustTile.h"
4#include "net.minecraft.world.item.h"
5#include "net.minecraft.world.level.h"
6#include "net.minecraft.world.level.redstone.h"
7#include "net.minecraft.world.level.tile.h"
8#include "net.minecraft.world.h"
9#include "net.minecraft.h"
10#include "Direction.h"
11#include "DiodeTile.h"
12
13// AP - added for Vita to set Alpha Cut out
14#include "IntBuffer.h"
15#include "..\Minecraft.Client\Tesselator.h"
16
17const wstring RedStoneDustTile::TEXTURE_CROSS = L"_cross";
18const wstring RedStoneDustTile::TEXTURE_LINE = L"_line";
19const wstring RedStoneDustTile::TEXTURE_CROSS_OVERLAY = L"_cross_overlay";
20const wstring RedStoneDustTile::TEXTURE_LINE_OVERLAY = L"_line_overlay";
21
22RedStoneDustTile::RedStoneDustTile(int id) : Tile(id, Material::decoration,isSolidRender())
23{
24 shouldSignal = true;
25
26 updateDefaultShape();
27
28 iconCross = NULL;
29 iconLine = NULL;
30 iconCrossOver = NULL;
31 iconLineOver = NULL;
32}
33
34// 4J Added override
35void RedStoneDustTile::updateDefaultShape()
36{
37 setShape(0, 0, 0, 1, 1 / 16.0f, 1);
38}
39
40AABB *RedStoneDustTile::getAABB(Level *level, int x, int y, int z)
41{
42 return NULL;
43}
44
45bool RedStoneDustTile::isSolidRender(bool isServerLevel)
46{
47 return false;
48}
49
50bool RedStoneDustTile::isCubeShaped()
51{
52 return false;
53}
54
55int RedStoneDustTile::getRenderShape()
56{
57 return Tile::SHAPE_RED_DUST;
58}
59
60int RedStoneDustTile::getColor() const
61{
62 return Minecraft::GetInstance()->getColourTable()->getColor(eMinecraftColour_Tile_RedstoneDust); // 0x800000;
63}
64
65int RedStoneDustTile::getColor(LevelSource *level, int x, int y, int z)
66{
67 return Minecraft::GetInstance()->getColourTable()->getColor(eMinecraftColour_Tile_RedstoneDust); // 0x800000;
68}
69
70int RedStoneDustTile::getColor(LevelSource *level, int x, int y, int z, int data)
71{
72 return Minecraft::GetInstance()->getColourTable()->getColor(eMinecraftColour_Tile_RedstoneDust); // 0x800000;
73}
74
75bool RedStoneDustTile::mayPlace(Level *level, int x, int y, int z)
76{
77 return level->isTopSolidBlocking(x, y - 1, z) || level->getTile(x, y - 1, z) == Tile::glowstone_Id;
78}
79
80void RedStoneDustTile::updatePowerStrength(Level *level, int x, int y, int z)
81{
82 updatePowerStrength(level, x, y, z, x, y, z);
83
84 vector<TilePos> updates = vector<TilePos>(toUpdate.begin(), toUpdate.end());
85 toUpdate.clear();
86
87 AUTO_VAR(itEnd, updates.end());
88 for(AUTO_VAR(it, updates.begin()); it != itEnd; it++)
89 {
90 TilePos tp = *it;
91 level->updateNeighborsAt(tp.x, tp.y, tp.z, id);
92 }
93}
94
95void RedStoneDustTile::updatePowerStrength(Level *level, int x, int y, int z, int xFrom, int yFrom, int zFrom)
96{
97 int old = level->getData(x, y, z);
98 int target = 0;
99
100 target = checkTarget(level, xFrom, yFrom, zFrom, target);
101
102 shouldSignal = false;
103 int neighborSignal = level->getBestNeighborSignal(x, y, z);
104 shouldSignal = true;
105
106 if (neighborSignal > Redstone::SIGNAL_NONE && neighborSignal > target - 1)
107 {
108 target = neighborSignal;
109 }
110
111 {
112 int newTarget = 0;
113 for (int i = 0; i < 4; i++)
114 {
115 int xt = x;
116 int zt = z;
117 if (i == 0) xt--;
118 if (i == 1) xt++;
119 if (i == 2) zt--;
120 if (i == 3) zt++;
121
122 if (xt != xFrom || zt != zFrom) newTarget = checkTarget(level, xt, y, zt, newTarget);
123 if (level->isSolidBlockingTile(xt, y, zt) && !level->isSolidBlockingTile(x, y + 1, z))
124 {
125 if ((xt != xFrom || zt != zFrom) && y >= yFrom)
126 newTarget = checkTarget(level, xt, y + 1, zt, newTarget);
127 }
128 else if (!level->isSolidBlockingTile(xt, y, zt))
129 {
130 if ((xt != xFrom || zt != zFrom) && y <= yFrom)
131 newTarget = checkTarget(level, xt, y - 1, zt, newTarget);
132 }
133 }
134 if (newTarget > target) target = newTarget - 1;
135 else if (target > 0) target--;
136 else target = 0;
137
138 if (neighborSignal > target - 1)
139 {
140 target = neighborSignal;
141 }
142 }
143
144 if (old != target)
145 {
146 level->setData(x, y, z, target, Tile::UPDATE_CLIENTS);
147
148 {
149 toUpdate.insert(TilePos(x, y, z));
150 toUpdate.insert(TilePos(x - 1, y, z));
151 toUpdate.insert(TilePos(x + 1, y, z));
152 toUpdate.insert(TilePos(x, y - 1, z));
153 toUpdate.insert(TilePos(x, y + 1, z));
154 toUpdate.insert(TilePos(x, y, z - 1));
155 toUpdate.insert(TilePos(x, y, z + 1));
156 }
157 }
158}
159
160void RedStoneDustTile::checkCornerChangeAt(Level *level, int x, int y, int z)
161{
162 if (level->getTile(x, y, z) != id) return;
163
164 level->updateNeighborsAt(x, y, z, id);
165 level->updateNeighborsAt(x - 1, y, z, id);
166 level->updateNeighborsAt(x + 1, y, z, id);
167 level->updateNeighborsAt(x, y, z - 1, id);
168 level->updateNeighborsAt(x, y, z + 1, id);
169
170 level->updateNeighborsAt(x, y - 1, z, id);
171 level->updateNeighborsAt(x, y + 1, z, id);
172}
173
174void RedStoneDustTile::onPlace(Level *level, int x, int y, int z)
175{
176 Tile::onPlace(level, x, y, z);
177 if (level->isClientSide) return;
178
179 updatePowerStrength(level, x, y, z);
180 level->updateNeighborsAt(x, y + 1, z, id);
181 level->updateNeighborsAt(x, y - 1, z, id);
182
183 checkCornerChangeAt(level, x - 1, y, z);
184 checkCornerChangeAt(level, x + 1, y, z);
185 checkCornerChangeAt(level, x, y, z - 1);
186 checkCornerChangeAt(level, x, y, z + 1);
187
188 if (level->isSolidBlockingTile(x - 1, y, z)) checkCornerChangeAt(level, x - 1, y + 1, z);
189 else checkCornerChangeAt(level, x - 1, y - 1, z);
190 if (level->isSolidBlockingTile(x + 1, y, z)) checkCornerChangeAt(level, x + 1, y + 1, z);
191 else checkCornerChangeAt(level, x + 1, y - 1, z);
192 if (level->isSolidBlockingTile(x, y, z - 1)) checkCornerChangeAt(level, x, y + 1, z - 1);
193 else checkCornerChangeAt(level, x, y - 1, z - 1);
194 if (level->isSolidBlockingTile(x, y, z + 1)) checkCornerChangeAt(level, x, y + 1, z + 1);
195 else checkCornerChangeAt(level, x, y - 1, z + 1);
196
197}
198
199void RedStoneDustTile::onRemove(Level *level, int x, int y, int z, int id, int data)
200{
201 Tile::onRemove(level, x, y, z, id, data);
202 if (level->isClientSide) return;
203
204 level->updateNeighborsAt(x, y + 1, z, this->id);
205 level->updateNeighborsAt(x, y - 1, z, this->id);
206 level->updateNeighborsAt(x + 1, y, z, this->id);
207 level->updateNeighborsAt(x - 1, y, z, this->id);
208 level->updateNeighborsAt(x, y, z + 1, this->id);
209 level->updateNeighborsAt(x, y, z - 1, this->id);
210 updatePowerStrength(level, x, y, z);
211
212 checkCornerChangeAt(level, x - 1, y, z);
213 checkCornerChangeAt(level, x + 1, y, z);
214 checkCornerChangeAt(level, x, y, z - 1);
215 checkCornerChangeAt(level, x, y, z + 1);
216
217 if (level->isSolidBlockingTile(x - 1, y, z)) checkCornerChangeAt(level, x - 1, y + 1, z);
218 else checkCornerChangeAt(level, x - 1, y - 1, z);
219 if (level->isSolidBlockingTile(x + 1, y, z)) checkCornerChangeAt(level, x + 1, y + 1, z);
220 else checkCornerChangeAt(level, x + 1, y - 1, z);
221 if (level->isSolidBlockingTile(x, y, z - 1)) checkCornerChangeAt(level, x, y + 1, z - 1);
222 else checkCornerChangeAt(level, x, y - 1, z - 1);
223 if (level->isSolidBlockingTile(x, y, z + 1)) checkCornerChangeAt(level, x, y + 1, z + 1);
224 else checkCornerChangeAt(level, x, y - 1, z + 1);
225}
226
227int RedStoneDustTile::checkTarget(Level *level, int x, int y, int z, int target)
228{
229 if (level->getTile(x, y, z) != id) return target;
230 int d = level->getData(x, y, z);
231 if (d > target) return d;
232 return target;
233}
234
235void RedStoneDustTile::neighborChanged(Level *level, int x, int y, int z, int type)
236{
237 if (level->isClientSide) return;
238
239 bool ok = mayPlace(level, x, y, z);
240
241 if (ok)
242 {
243 updatePowerStrength(level, x, y, z);
244 }
245 else
246 {
247 spawnResources(level, x, y, z, 0, 0);
248 level->removeTile(x, y, z);
249 }
250
251 Tile::neighborChanged(level, x, y, z, type);
252}
253
254int RedStoneDustTile::getResource(int data, Random *random, int playerBonusLevel)
255{
256 return Item::redStone->id;
257}
258
259int RedStoneDustTile::getDirectSignal(LevelSource *level, int x, int y, int z, int dir)
260{
261 if (!shouldSignal) return Redstone::SIGNAL_NONE;
262 return getSignal(level, x, y, z, dir);
263}
264
265int RedStoneDustTile::getSignal(LevelSource *level, int x, int y, int z, int dir)
266{
267 if (!shouldSignal) return Redstone::SIGNAL_NONE;
268 int data = level->getData(x, y, z);
269 if (data == Facing::DOWN)
270 {
271 return Redstone::SIGNAL_NONE;
272 }
273
274 if (dir == Facing::UP) return data;
275
276 bool w = shouldReceivePowerFrom(level, x - 1, y, z, Direction::WEST)
277 || (!level->isSolidBlockingTile(x - 1, y, z) && shouldReceivePowerFrom(level, x - 1, y - 1, z, Direction::UNDEFINED));
278 bool e = shouldReceivePowerFrom(level, x + 1, y, z, Direction::EAST)
279 || (!level->isSolidBlockingTile(x + 1, y, z) && shouldReceivePowerFrom(level, x + 1, y - 1, z, Direction::UNDEFINED));
280 bool n = shouldReceivePowerFrom(level, x, y, z - 1, Direction::NORTH)
281 || (!level->isSolidBlockingTile(x, y, z - 1) && shouldReceivePowerFrom(level, x, y - 1, z - 1, Direction::UNDEFINED));
282 bool s = shouldReceivePowerFrom(level, x, y, z + 1, Direction::SOUTH)
283 || (!level->isSolidBlockingTile(x, y, z + 1) && shouldReceivePowerFrom(level, x, y - 1, z + 1, Direction::UNDEFINED));
284
285 if (!level->isSolidBlockingTile(x, y + 1, z))
286 {
287 if (level->isSolidBlockingTile(x - 1, y, z) && shouldReceivePowerFrom(level, x - 1, y + 1, z, Direction::UNDEFINED)) w = true;
288 if (level->isSolidBlockingTile(x + 1, y, z) && shouldReceivePowerFrom(level, x + 1, y + 1, z, Direction::UNDEFINED)) e = true;
289 if (level->isSolidBlockingTile(x, y, z - 1) && shouldReceivePowerFrom(level, x, y + 1, z - 1, Direction::UNDEFINED)) n = true;
290 if (level->isSolidBlockingTile(x, y, z + 1) && shouldReceivePowerFrom(level, x, y + 1, z + 1, Direction::UNDEFINED)) s = true;
291 }
292
293 if (!n && !e && !w && !s && (dir >= 2 && dir <= 5)) return data;
294
295 if (dir == 2 && n && (!w && !e)) return data;
296 if (dir == 3 && s && (!w && !e)) return data;
297 if (dir == 4 && w && (!n && !s)) return data;
298 if (dir == 5 && e && (!n && !s)) return data;
299
300 return Redstone::SIGNAL_NONE;
301
302}
303
304
305bool RedStoneDustTile::isSignalSource()
306{
307 return shouldSignal;
308}
309
310void RedStoneDustTile::animateTick(Level *level, int x, int y, int z, Random *random)
311{
312 int data = level->getData(x, y, z);
313 if (data > 0)
314 {
315 double xx = x + 0.5 + (random->nextFloat() - 0.5) * 0.2;
316 double yy = y + 1 / 16.0f;
317 double zz = z + 0.5 + (random->nextFloat() - 0.5) * 0.2;
318 // use the x movement variable to determine particle color
319
320 // 4J Stu - Unused
321 //float pow = (data / 15.0f);
322 //float red = pow * 0.6f + 0.4f;
323 //if (data == 0) red = 0;
324
325 //float green = pow * pow * 0.7f - 0.5f;
326 //float blue = pow * pow * 0.6f - 0.7f;
327 //if (green < 0) green = 0;
328 //if (blue < 0) blue = 0;
329
330 unsigned int colour = 0;
331 if(data == 0)
332 {
333 colour = Minecraft::GetInstance()->getColourTable()->getColor( eMinecraftColour_Tile_RedstoneDustUnlit );
334 }
335 else
336 {
337 unsigned int minColour = Minecraft::GetInstance()->getColourTable()->getColor( eMinecraftColour_Tile_RedstoneDustLitMin );
338 unsigned int maxColour = Minecraft::GetInstance()->getColourTable()->getColor( eMinecraftColour_Tile_RedstoneDustLitMax );
339
340 byte redComponent = ((minColour>>16)&0xFF) + (( (maxColour>>16)&0xFF - (minColour>>16)&0xFF)*( (data-1)/14.0f));
341 byte greenComponent = ((minColour>>8)&0xFF) + (( (maxColour>>8)&0xFF - (minColour>>8)&0xFF)*( (data-1)/14.0f));
342 byte blueComponent = ((minColour)&0xFF) + (( (maxColour)&0xFF - (minColour)&0xFF)*( (data-1)/14.0f));
343
344 colour = redComponent<<16 | greenComponent<<8 | blueComponent;
345 }
346
347 float red = ((colour>>16)&0xFF)/255.0f;
348 float green = ((colour>>8)&0xFF)/255.0f;
349 float blue = (colour&0xFF)/255.0f;
350
351 level->addParticle(eParticleType_reddust, xx, yy, zz, red, green, blue);
352 }
353}
354
355
356bool RedStoneDustTile::shouldConnectTo(LevelSource *level, int x, int y, int z, int direction)
357{
358 int t = level->getTile(x, y, z);
359 if (t == Tile::redStoneDust_Id) return true;
360 if (t == 0) return false;
361 if (Tile::diode_off->isSameDiode(t))
362 {
363 int data = level->getData(x, y, z);
364 return direction == (data & DiodeTile::DIRECTION_MASK) || direction == Direction::DIRECTION_OPPOSITE[data & DiodeTile::DIRECTION_MASK];
365 }
366 else if (Tile::tiles[t]->isSignalSource() && direction != Direction::UNDEFINED) return true;
367
368 return false;
369}
370
371bool RedStoneDustTile::shouldReceivePowerFrom(LevelSource *level, int x, int y, int z, int direction)
372{
373 if (shouldConnectTo(level, x, y, z, direction))
374 {
375 return true;
376 }
377
378 int t = level->getTile(x, y, z);
379 if (t == Tile::diode_on_Id)
380 {
381 int data = level->getData(x, y, z);
382 return direction == (data & DiodeTile::DIRECTION_MASK);
383 }
384 return false;
385}
386
387int RedStoneDustTile::cloneTileId(Level *level, int x, int y, int z)
388{
389 return Item::redStone_Id;
390}
391
392void RedStoneDustTile::registerIcons(IconRegister *iconRegister)
393{
394 iconCross = iconRegister->registerIcon(getIconName() + TEXTURE_CROSS);
395 iconLine = iconRegister->registerIcon(getIconName() + TEXTURE_LINE);
396 iconCrossOver = iconRegister->registerIcon(getIconName() + TEXTURE_CROSS_OVERLAY);
397 iconLineOver = iconRegister->registerIcon(getIconName() + TEXTURE_LINE_OVERLAY);
398
399 icon = iconCross;
400}
401
402Icon *RedStoneDustTile::getTexture(const wstring &name)
403{
404#ifdef __PSVITA__
405 // AP - alpha cut out is expensive on vita. Set the Alpha Cut out flag
406 Tesselator* t = Tesselator::getInstance();
407 t->setAlphaCutOut( true );
408#endif
409
410 if (name.compare(TEXTURE_CROSS) == 0) return Tile::redStoneDust->iconCross;
411 if (name.compare(TEXTURE_LINE) == 0) return Tile::redStoneDust->iconLine;
412 if (name.compare(TEXTURE_CROSS_OVERLAY) == 0) return Tile::redStoneDust->iconCrossOver;
413 if (name.compare(TEXTURE_LINE_OVERLAY) == 0) return Tile::redStoneDust->iconLineOver;
414 return NULL;
415}