the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 491 lines 14 kB view raw
1#include "stdafx.h" 2#include "..\Minecraft.World\net.minecraft.world.item.h" 3#include "FireworksParticles.h" 4#include "Tesselator.h" 5#include "../Minecraft.World/Level.h" 6 7FireworksParticles::FireworksStarter::FireworksStarter(Level *level, double x, double y, double z, double xd, double yd, double zd, ParticleEngine *engine, CompoundTag *infoTag) : Particle(level, x, y, z, 0, 0, 0) 8{ 9 life = 0; 10 twinkleDelay = false; 11 12 this->xd = xd; 13 this->yd = yd; 14 this->zd = zd; 15 this->engine = engine; 16 lifetime = 8; 17 18 if (infoTag != NULL) 19 { 20 explosions = (ListTag<CompoundTag> *)infoTag->getList(FireworksItem::TAG_EXPLOSIONS)->copy(); 21 if (explosions->size() == 0) 22 { 23 explosions = NULL; 24 } 25 else 26 { 27 lifetime = explosions->size() * 2 - 1; 28 29 // check if any of the explosions has flickering 30 for (int e = 0; e < explosions->size(); e++) 31 { 32 CompoundTag *compoundTag = explosions->get(e); 33 if (compoundTag->getBoolean(FireworksItem::TAG_E_FLICKER)) 34 { 35 twinkleDelay = true; 36 lifetime += 15; 37 break; 38 } 39 } 40 } 41 } 42 else 43 { 44 // 4J: 45 explosions = NULL; 46 } 47} 48 49void FireworksParticles::FireworksStarter::render(Tesselator *t, float a, float xa, float ya, float za, float xa2, float za2) 50{ 51 // Do nothing 52} 53 54void FireworksParticles::FireworksStarter::tick() 55{ 56 if (life == 0 && explosions != NULL) 57 { 58 bool farEffect = isFarAwayFromCamera(); 59 60 bool largeExplosion = false; 61 if (explosions->size() >= 3) 62 { 63 largeExplosion = true; 64 } 65 else 66 { 67 for (int e = 0; e < explosions->size(); e++) 68 { 69 CompoundTag *compoundTag = explosions->get(e); 70 if (compoundTag->getByte(FireworksItem::TAG_E_TYPE) == FireworksItem::TYPE_BIG) 71 { 72 largeExplosion = true; 73 break; 74 } 75 } 76 } 77 78 eSOUND_TYPE soundId; 79 80 if (largeExplosion && farEffect) 81 { 82 soundId = eSoundType_FIREWORKS_LARGE_BLAST_FAR; 83 } 84 else if (largeExplosion && !farEffect) 85 { 86 soundId = eSoundType_FIREWORKS_LARGE_BLAST; 87 } 88 else if (!largeExplosion && farEffect) 89 { 90 soundId = eSoundType_FIREWORKS_BLAST_FAR; 91 } 92 else 93 { 94 soundId = eSoundType_FIREWORKS_BLAST; 95 } 96 97 level->playLocalSound(x, y, z, soundId, 20, .95f + random->nextFloat() * .1f, true, 100.0f); 98 } 99 100 if ((life % 2) == 0 && explosions != NULL && (life / 2) < explosions->size()) 101 { 102 int eIndex = life / 2; 103 CompoundTag *compoundTag = explosions->get(eIndex); 104 105 int type = compoundTag->getByte(FireworksItem::TAG_E_TYPE); 106 bool trail = compoundTag->getBoolean(FireworksItem::TAG_E_TRAIL); 107 bool flicker = compoundTag->getBoolean(FireworksItem::TAG_E_FLICKER); 108 intArray colors = compoundTag->getIntArray(FireworksItem::TAG_E_COLORS); 109 intArray fadeColors = compoundTag->getIntArray(FireworksItem::TAG_E_FADECOLORS); 110 111 if (type == FireworksItem::TYPE_BIG) 112 { 113 // large ball 114 createParticleBall(.5, 4, colors, fadeColors, trail, flicker); 115 } 116 else if (type == FireworksItem::TYPE_STAR) 117 { 118 double coords[6][2] = { 119 0.0, 1.0, 120 0.3455, 0.3090, 121 0.9511, 0.3090, 122 93.0 / 245.0, -31.0 / 245.0, 123 150.0 / 245.0, -197.0 / 245.0, 124 0.0, -88.0 / 245.0, 125 }; 126 coords2DArray coordsArray(6, 2); 127 for(unsigned int i = 0; i < coordsArray.length; ++i) 128 { 129 for(unsigned int j = 0; j < coordsArray[i]->length; ++j) 130 { 131 coordsArray[i]->data[j] = coords[i][j]; 132 } 133 } 134 135 // star-shape 136 createParticleShape(.5, coordsArray, colors, fadeColors, trail, flicker, false); 137 138 for(unsigned int i = 0; i < coordsArray.length; ++i) 139 { 140 delete [] coordsArray[i]->data; 141 } 142 delete [] coordsArray.data; 143 } 144 else if (type == FireworksItem::TYPE_CREEPER) 145 { 146 double coords[12][2] = { 147 0.0, 0.2, 148 0.2, 0.2, 149 0.2, 0.6, 150 0.6, 0.6, 151 0.6, 0.2, 152 0.2, 0.2, 153 0.2, 0.0, 154 0.4, 0.0, 155 0.4, -0.6, 156 0.2, -0.6, 157 0.2, -0.4, 158 0.0, -0.4, 159 }; 160 coords2DArray coordsArray(12, 2); 161 for(unsigned int i = 0; i < coordsArray.length; ++i) 162 { 163 for(unsigned int j = 0; j < coordsArray[i]->length; ++j) 164 { 165 coordsArray[i]->data[j] = coords[i][j]; 166 } 167 } 168 169 // creeper-shape 170 createParticleShape(.5, coordsArray, colors, fadeColors, trail, flicker, true); 171 172 for(unsigned int i = 0; i < coordsArray.length; ++i) 173 { 174 delete [] coordsArray[i]->data; 175 } 176 delete [] coordsArray.data; 177 } 178 else if (type == FireworksItem::TYPE_BURST) 179 { 180 createParticleBurst(colors, fadeColors, trail, flicker); 181 } 182 else 183 { 184 // small ball 185 createParticleBall(.25, 2, colors, fadeColors, trail, flicker); 186 } 187 { 188 int rgb = colors[0]; 189 float r = (float) ((rgb & 0xff0000) >> 16) / 255.0f; 190 float g = (float) ((rgb & 0x00ff00) >> 8) / 255.0f; 191 float b = (float) ((rgb & 0x0000ff) >> 0) / 255.0f; 192 shared_ptr<FireworksOverlayParticle> fireworksOverlayParticle = shared_ptr<FireworksOverlayParticle>(new FireworksParticles::FireworksOverlayParticle(level, x, y, z)); 193 fireworksOverlayParticle->setColor(r, g, b); 194 fireworksOverlayParticle->setAlpha(0.99f); // 4J added 195 engine->add(fireworksOverlayParticle); 196 } 197 } 198 life++; 199 if (life > lifetime) 200 { 201 if (twinkleDelay) 202 { 203 bool farEffect = isFarAwayFromCamera(); 204 eSOUND_TYPE soundId = farEffect ? eSoundType_FIREWORKS_TWINKLE_FAR : eSoundType_FIREWORKS_TWINKLE; 205 level->playLocalSound(x, y, z, soundId, 20, .90f + random->nextFloat() * .15f, true, 100.0f); 206 207 } 208 remove(); 209 } 210} 211 212bool FireworksParticles::FireworksStarter::isFarAwayFromCamera() 213{ 214 Minecraft *instance = Minecraft::GetInstance(); 215 if (instance != NULL && instance->cameraTargetPlayer != NULL) 216 { 217 if (instance->cameraTargetPlayer->distanceToSqr(x, y, z) < 16 * 16) 218 { 219 return false; 220 } 221 } 222 return true; 223} 224 225void FireworksParticles::FireworksStarter::createParticle(double x, double y, double z, double xa, double ya, double za, intArray rgbColors, intArray fadeColors, bool trail, bool flicker) 226{ 227 shared_ptr<FireworksSparkParticle> fireworksSparkParticle = shared_ptr<FireworksSparkParticle>(new FireworksSparkParticle(level, x, y, z, xa, ya, za, engine)); 228 fireworksSparkParticle->setAlpha(0.99f); 229 fireworksSparkParticle->setTrail(trail); 230 fireworksSparkParticle->setFlicker(flicker); 231 232 int color = random->nextInt(rgbColors.length); 233 fireworksSparkParticle->setColor(rgbColors[color]); 234 if (/*fadeColors != NULL &&*/ fadeColors.length > 0) 235 { 236 fireworksSparkParticle->setFadeColor(fadeColors[random->nextInt(fadeColors.length)]); 237 } 238 engine->add(fireworksSparkParticle); 239} 240 241void FireworksParticles::FireworksStarter::createParticleBall(double baseSpeed, int steps, intArray rgbColors, intArray fadeColors, bool trail, bool flicker) { 242 243 double xx = x; 244 double yy = y; 245 double zz = z; 246 247 for (int yStep = -steps; yStep <= steps; yStep++) { 248 for (int xStep = -steps; xStep <= steps; xStep++) { 249 for (int zStep = -steps; zStep <= steps; zStep++) { 250 double xa = xStep + (random->nextDouble() - random->nextDouble()) * .5; 251 double ya = yStep + (random->nextDouble() - random->nextDouble()) * .5; 252 double za = zStep + (random->nextDouble() - random->nextDouble()) * .5; 253 double len = sqrt(xa * xa + ya * ya + za * za) / baseSpeed + random->nextGaussian() * .05; 254 255 createParticle(xx, yy, zz, xa / len, ya / len, za / len, rgbColors, fadeColors, trail, flicker); 256 257 if (yStep != -steps && yStep != steps && xStep != -steps && xStep != steps) { 258 zStep += steps * 2 - 1; 259 } 260 } 261 } 262 } 263} 264 265void FireworksParticles::FireworksStarter::createParticleShape(double baseSpeed, coords2DArray coords, intArray rgbColors, intArray fadeColors, bool trail, bool flicker, bool flat) 266{ 267 double sx = coords[0]->data[0]; 268 double sy = coords[0]->data[1]; 269 270 { 271 createParticle(x, y, z, sx * baseSpeed, sy * baseSpeed, 0, rgbColors, fadeColors, trail, flicker); 272 } 273 274 float baseAngle = random->nextFloat() * PI; 275 double angleMod = (flat ? .034 : .34); 276 for (int angleStep = 0; angleStep < 3; angleStep++) 277 { 278 double angle = baseAngle + angleStep * PI * angleMod; 279 280 double ox = sx; 281 double oy = sy; 282 283 for (int c = 1; c < coords.length; c++) 284 { 285 double tx = coords[c]->data[0]; 286 double ty = coords[c]->data[1]; 287 288 for (double subStep = .25; subStep <= 1.0; subStep += .25) 289 { 290 double xa = (ox + (tx - ox) * subStep) * baseSpeed; 291 double ya = (oy + (ty - oy) * subStep) * baseSpeed; 292 293 double za = xa * sin(angle); 294 xa = xa * cos(angle); 295 296 for (double flip = -1; flip <= 1; flip += 2) 297 { 298 createParticle(x, y, z, xa * flip, ya, za * flip, rgbColors, fadeColors, trail, flicker); 299 } 300 } 301 ox = tx; 302 oy = ty; 303 } 304 305 } 306} 307 308void FireworksParticles::FireworksStarter::createParticleBurst(intArray rgbColors, intArray fadeColors, bool trail, bool flicker) 309{ 310 double baseOffX = random->nextGaussian() * .05; 311 double baseOffZ = random->nextGaussian() * .05; 312 313 for (int i = 0; i < 70; i++) { 314 315 double xa = xd * .5 + random->nextGaussian() * .15 + baseOffX; 316 double za = zd * .5 + random->nextGaussian() * .15 + baseOffZ; 317 double ya = yd * .5 + random->nextDouble() * .5; 318 319 createParticle(x, y, z, xa, ya, za, rgbColors, fadeColors, trail, flicker); 320 } 321} 322 323int FireworksParticles::FireworksStarter::getParticleTexture() 324{ 325 return ParticleEngine::MISC_TEXTURE; 326} 327 328FireworksParticles::FireworksSparkParticle::FireworksSparkParticle(Level *level, double x, double y, double z, double xa, double ya, double za, ParticleEngine *engine) : Particle(level, x, y, z) 329{ 330 baseTex = 10 * 16; 331 332 xd = xa; 333 yd = ya; 334 zd = za; 335 this->engine = engine; 336 337 size *= 0.75f; 338 339 lifetime = 48 + random->nextInt(12); 340#ifdef __PSVITA__ 341 noPhysics = true; // 4J - optimisation, these are just too slow on Vita to be running with physics on 342#else 343 noPhysics = false; 344#endif 345 346 347 trail = false; 348 flicker = false; 349 350 fadeR = 0.0f; 351 fadeG = 0.0f; 352 fadeB = 0.0f; 353 hasFade = false; 354} 355 356void FireworksParticles::FireworksSparkParticle::setTrail(bool trail) 357{ 358 this->trail = trail; 359} 360 361void FireworksParticles::FireworksSparkParticle::setFlicker(bool flicker) 362{ 363 this->flicker = flicker; 364} 365 366void FireworksParticles::FireworksSparkParticle::setColor(int rgb) 367{ 368 float r = (float) ((rgb & 0xff0000) >> 16) / 255.0f; 369 float g = (float) ((rgb & 0x00ff00) >> 8) / 255.0f; 370 float b = (float) ((rgb & 0x0000ff) >> 0) / 255.0f; 371 float scale = 1.0f; 372 Particle::setColor(r * scale, g * scale, b * scale); 373} 374 375void FireworksParticles::FireworksSparkParticle::setFadeColor(int rgb) 376{ 377 fadeR = (float) ((rgb & 0xff0000) >> 16) / 255.0f; 378 fadeG = (float) ((rgb & 0x00ff00) >> 8) / 255.0f; 379 fadeB = (float) ((rgb & 0x0000ff) >> 0) / 255.0f; 380 hasFade = true; 381} 382 383AABB *FireworksParticles::FireworksSparkParticle::getCollideBox() 384{ 385 return NULL; 386} 387 388bool FireworksParticles::FireworksSparkParticle::isPushable() 389{ 390 return false; 391} 392 393void FireworksParticles::FireworksSparkParticle::render(Tesselator *t, float a, float xa, float ya, float za, float xa2, float za2) 394{ 395 if (!flicker || age < (lifetime / 3) || (((age + lifetime) / 3) % 2) == 0) 396 { 397 Particle::render(t, a, xa, ya, za, xa2, za2); 398 } 399} 400 401void FireworksParticles::FireworksSparkParticle::tick() 402{ 403 xo = x; 404 yo = y; 405 zo = z; 406 407 if (age++ >= lifetime) remove(); 408 if (age > lifetime / 2) 409 { 410 setAlpha(1.0f - (((float) age - lifetime / 2) / (float) lifetime)); 411 412 if (hasFade) 413 { 414 rCol = rCol + (fadeR - rCol) * .2f; 415 gCol = gCol + (fadeG - gCol) * .2f; 416 bCol = bCol + (fadeB - bCol) * .2f; 417 } 418 } 419 420 setMiscTex(baseTex + (7 - age * 8 / lifetime)); 421 422 yd -= 0.004; 423 move(xd, yd, zd, true); // 4J - changed so these don't attempt to collide with entities 424 xd *= 0.91f; 425 yd *= 0.91f; 426 zd *= 0.91f; 427 428 if (onGround) 429 { 430 xd *= 0.7f; 431 zd *= 0.7f; 432 } 433 434 if (trail && (age < lifetime / 2) && ((age + lifetime) % 2) == 0) 435 { 436 shared_ptr<FireworksSparkParticle> fireworksSparkParticle = shared_ptr<FireworksSparkParticle>(new FireworksParticles::FireworksSparkParticle(level, x, y, z, 0, 0, 0, engine)); 437 fireworksSparkParticle->setAlpha(0.99f); 438 fireworksSparkParticle->setColor(rCol, gCol, bCol); 439 fireworksSparkParticle->age = fireworksSparkParticle->lifetime / 2; 440 if (hasFade) 441 { 442 fireworksSparkParticle->hasFade = true; 443 fireworksSparkParticle->fadeR = fadeR; 444 fireworksSparkParticle->fadeG = fadeG; 445 fireworksSparkParticle->fadeB = fadeB; 446 } 447 fireworksSparkParticle->flicker = flicker; 448 engine->add(fireworksSparkParticle); 449 } 450} 451 452void FireworksParticles::FireworksSparkParticle::setBaseTex(int baseTex) 453{ 454 this->baseTex = baseTex; 455} 456 457int FireworksParticles::FireworksSparkParticle::getLightColor(float a) 458{ 459 return SharedConstants::FULLBRIGHT_LIGHTVALUE; 460} 461 462float FireworksParticles::FireworksSparkParticle::getBrightness(float a) 463{ 464 return 1; 465} 466 467FireworksParticles::FireworksOverlayParticle::FireworksOverlayParticle(Level *level, double x, double y, double z) : Particle(level, x, y, z) 468{ 469 lifetime = 4; 470} 471 472void FireworksParticles::FireworksOverlayParticle::render(Tesselator *t, float a, float xa, float ya, float za, float xa2, float za2) 473{ 474 float u0 = 32.0f / 128.0f; 475 float u1 = u0 + 32.0f / 128.0f; 476 float v0 = 16.0f / 128.0f; 477 float v1 = v0 + 32.0f / 128.0f; 478 float r = 7.1f * sin(((float) age + a - 1.0f) * .25f * PI); 479 alpha = 0.6f - ((float) age + a - 1.0f) * .25f * .5f; 480 481 float x = (float) (xo + (this->x - xo) * a - xOff); 482 float y = (float) (yo + (this->y - yo) * a - yOff); 483 float z = (float) (zo + (this->z - zo) * a - zOff); 484 485 t->color(rCol, gCol, bCol, alpha); 486 487 t->vertexUV(x - xa * r - xa2 * r, y - ya * r, z - za * r - za2 * r, u1, v1); 488 t->vertexUV(x - xa * r + xa2 * r, y + ya * r, z - za * r + za2 * r, u1, v0); 489 t->vertexUV(x + xa * r + xa2 * r, y + ya * r, z + za * r + za2 * r, u0, v0); 490 t->vertexUV(x + xa * r - xa2 * r, y - ya * r, z + za * r - za2 * r, u0, v1); 491}