|
| 1 | +#include <cstdlib> |
| 2 | + |
| 3 | +#include "wheatrenderer.h" |
| 4 | + |
| 5 | +void WheatRenderer::renderSpecialBlock(const BLOCK_WDATA block, GLFix x, GLFix y, GLFix z, Chunk &c) |
| 6 | +{ |
| 7 | + BlockRenderer::renderBillboard((x - c.absX()) / BLOCK_SIZE, (y - c.absY()) / BLOCK_SIZE, (z - c.absZ()) / BLOCK_SIZE, terrain_atlas[8 + getBLOCKDATA(block)][5].current, c); |
| 8 | +} |
| 9 | + |
| 10 | +AABB WheatRenderer::getAABB(const BLOCK_WDATA block, GLFix x, GLFix y, GLFix z) |
| 11 | +{ |
| 12 | + const GLFix height = BLOCK_SIZE / max_growth * getBLOCKDATA(block); |
| 13 | + |
| 14 | + return {x, y, z, x + BLOCK_SIZE, y + height, z + BLOCK_SIZE}; |
| 15 | +} |
| 16 | + |
| 17 | +void WheatRenderer::drawPreview(const BLOCK_WDATA /*block*/, TEXTURE &dest, int x, int y) |
| 18 | +{ |
| 19 | + BlockRenderer::drawTextureAtlasEntry(*terrain_resized, terrain_atlas[12][5].resized, true, dest, x, y); |
| 20 | +} |
| 21 | + |
| 22 | +void WheatRenderer::tick(const BLOCK_WDATA block, int local_x, int local_y, int local_z, Chunk &c) |
| 23 | +{ |
| 24 | + //If not irrigated, it's instantly withered (growth of 0) |
| 25 | + if(!isIrrigated(local_x, local_y, local_z, c)) |
| 26 | + return c.setLocalBlock(local_x, local_y, local_z, getBLOCKWDATA(getBLOCK(block), 0)); |
| 27 | + |
| 28 | + //Grow only if lucky |
| 29 | + if(rand() % 30 != 0) |
| 30 | + return; |
| 31 | + |
| 32 | + const uint8_t growth = getBLOCKDATA(block); |
| 33 | + if(growth == max_growth) |
| 34 | + return; |
| 35 | + |
| 36 | + c.setLocalBlock(local_x, local_y, local_z, getBLOCKWDATA(getBLOCK(block), growth + 1)); |
| 37 | +} |
| 38 | + |
| 39 | +void WheatRenderer::addedBlock(const BLOCK_WDATA /*block*/, int local_x, int local_y, int local_z, Chunk &c) |
| 40 | +{ |
| 41 | + if(!isIrrigated(local_x, local_y, local_z, c)) |
| 42 | + return c.setLocalBlock(local_x, local_y, local_z, BLOCK_AIR); |
| 43 | +} |
| 44 | + |
| 45 | +const char *WheatRenderer::getName(const BLOCK_WDATA) |
| 46 | +{ |
| 47 | + return "Wheat"; |
| 48 | +} |
| 49 | + |
| 50 | +//To be irrigated, a block of water must be adjacent to the block underneath, which has to be dirt or grass |
| 51 | +bool WheatRenderer::isIrrigated(const int local_x, const int local_y, const int local_z, const Chunk &c) |
| 52 | +{ |
| 53 | + const BLOCK underneath = c.getGlobalBlockRelative(local_x, local_y - 1, local_z); |
| 54 | + if(underneath != BLOCK_DIRT && underneath != BLOCK_GRASS) |
| 55 | + return false; |
| 56 | + |
| 57 | + return getBLOCK(c.getGlobalBlockRelative(local_x - 1, local_y - 1, local_z)) == BLOCK_WATER |
| 58 | + || getBLOCK(c.getGlobalBlockRelative(local_x + 1, local_y - 1, local_z)) == BLOCK_WATER |
| 59 | + || getBLOCK(c.getGlobalBlockRelative(local_x, local_y - 1, local_z - 1)) == BLOCK_WATER |
| 60 | + || getBLOCK(c.getGlobalBlockRelative(local_x, local_y - 1, local_z + 1)) == BLOCK_WATER; |
| 61 | +} |
0 commit comments