From aa0d9623aa8fb2eeffc657aaf11c00ab1ebe90eb Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 1 May 2022 17:16:46 -0400 Subject: [PATCH] De-obfuscated most of the texture generation code --- src/blocks.h | 5 +++ src/terrain.h | 2 +- src/textures.c | 95 ++++++++++++++++++++++++++++++-------------------- src/textures.h | 10 ++++-- 4 files changed, 71 insertions(+), 41 deletions(-) diff --git a/src/blocks.h b/src/blocks.h index 5eaec81..52e69da 100644 --- a/src/blocks.h +++ b/src/blocks.h @@ -1,4 +1,9 @@ #pragma once +#include + +typedef u_int8_t Block; + +#define NUMBER_OF_BLOCKS 16 #define BLOCK_AIR 0 #define BLOCK_GRASS 1 diff --git a/src/terrain.h b/src/terrain.h index 30c1cfd..675ed95 100644 --- a/src/terrain.h +++ b/src/terrain.h @@ -3,6 +3,7 @@ #include #include #include +#include "blocks.h" #include "coords.h" #include "utility.h" @@ -12,7 +13,6 @@ #define CHUNK_SIZE 64 #define CHUNK_DATA_SIZE CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE -typedef u_int8_t Block; typedef struct _Chunk Chunk; typedef struct _World World; diff --git a/src/textures.c b/src/textures.c index bed5cad..6cc8b30 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1,8 +1,8 @@ #include "textures.h" -#include "blocks.h" +#include -int textures[12288] = {0}; -const u_int16_t cobbleCracks[16] = { +int textures[TEXTURES_SIZE] = { 0 }; +const u_int16_t cobbleCracks[BLOCK_TEXTURE_H] = { 0b0000001110000100, 0b0010110010000110, 0b1011100011001110, @@ -21,23 +21,28 @@ const u_int16_t cobbleCracks[16] = { 0b0000111100111110, }; +static void genTexture (Block); + /* genTextures * Takes in a seed and an array where the textures should go. * Generates game textures in that array. */ -void genTextures(unsigned int seed) { +void genTextures (unsigned int seed) { srand(seed); - for (int blockId = 1; blockId < 16; blockId++) { + for (Block blockId = 1; blockId < NUMBER_OF_BLOCKS; blockId++) { genTexture(blockId); } } -void genTexture (int blockId) { - int k = 255 - randm(96); - for (int y = 0; y < 48; y++) - for (int x = 0; x < 16; x++) { - int baseColor = 9858122; +/* genTexture + * Generates a texture for the given block ID. + */ +static void genTexture (Block blockId) { + int brightness = 255 - randm(96); + for (int y = 0; y < BLOCK_TEXTURE_H * 3; y++) + for (int x = 0; x < BLOCK_TEXTURE_W; x++) { + int baseColor = 0x966C4A; int noiseFloor = 255; int noiseScale = 96; @@ -47,7 +52,7 @@ void genTexture (int blockId) { } if (blockId == BLOCK_STONE) - baseColor = 8355711; + baseColor = 0x7F7F7F; if (blockId == BLOCK_GRAVEL) { baseColor = 0xAAAAAA; @@ -58,28 +63,28 @@ void genTexture (int blockId) { blockId == BLOCK_STONE || blockId == BLOCK_WATER; if (!needAltNoise || randm(3) == 0) - k = noiseFloor - randm(noiseScale); + brightness = noiseFloor - randm(noiseScale); if ( blockId == BLOCK_GRASS && y < (x * x * (3 + x) * 81 >> 2 & 0x3) + 18 ) { - baseColor = 6990400; + baseColor = 0x6AAA40; } else if ( blockId == BLOCK_GRASS && y < (x * x * (3 + x) * 81 >> 2 & 0x3) + 19 ) { - k = k * 2 / 3; + brightness = brightness * 2 / 3; } if (blockId == BLOCK_WOOD) { - baseColor = 6771249; + baseColor = 0x675231; if ( x > 0 && x < 15 && ((y > 0 && y < 15) || (y > 32 && y < 47)) ) { - baseColor = 12359778; + baseColor = 0xBC9862; int i6 = x - 7; int i7 = (y & 0xF) - 7; @@ -87,22 +92,25 @@ void genTexture (int blockId) { if (i7 < 0) i7 = 1 - i7; if (i7 > i6) i6 = i7; - k = 196 - randm(32) + i6 % 3 * 32; + brightness = 196 - randm(32) + i6 % 3 * 32; } else if (randm(2) == 0) { - k = k * (150 - (x & 0x1) * 100) / 100; + brightness = + brightness * + (150 - (x & 0x1) * 100) / 100; } } switch (blockId) { case BLOCK_BRICKS: - baseColor = 11876885; + baseColor = 0xB53A15; if ((x + y / 4 * 4) % 8 == 0 || y % 4 == 0) baseColor = 12365733; break; case BLOCK_COBBLESTONE: baseColor = 0x999999; - k -= ((cobbleCracks[y & 0xF] >> x) & 0b1) * 128; + brightness -= + ((cobbleCracks[y & 0xF] >> x) & 0b1) * 128; break; case BLOCK_WATER: @@ -110,49 +118,62 @@ void genTexture (int blockId) { break; case BLOCK_PLAYER_HEAD: - k = 255; + brightness = 255; if ( - dist2d(x, y % 16, 8, 8) > 6.2 || + dist2d(x, y % BLOCK_TEXTURE_H, 8, 8) > 6.2 || (y / 16) % 3 == 2 ) { baseColor = 0x000000; } else { baseColor = 0xFFFFFF; - k -= dist2d(x, y % 16, 8, 2) * 8; + brightness -= dist2d ( + x, y % BLOCK_TEXTURE_H, + 8, 2) * 8; } break; case BLOCK_PLAYER_BODY: - k = 255; + brightness = 255; if ( - (dist2d(x, y % 16, 8, 16) > 12.2 || + (dist2d(x, y % BLOCK_TEXTURE_H, 8, 16) > 12.2 || (y / 16) % 3 != 1) && (y / 16) % 3 != 2 ) { baseColor = 0x000000; } else { baseColor = 0xFFFFFF; - k -= dist2d(x, y % 16, 8, 2) * 8; + brightness -= dist2d ( + x, y % BLOCK_TEXTURE_H, + 8, 2) * 8; } break; } - int i2 = k; - if (y >= 32) - i2 /= 2; if (blockId == BLOCK_LEAVES) { - baseColor = 5298487; + baseColor = 0x50D937; + + // Randomly punch holes in the texture if (randm(2) == 0) { baseColor = 0; - i2 = 255; + brightness = 255; } } - // darken bottom of blocks - int i3 = (baseColor >> 16 & 0xFF) - * i2 / 255 << 16 | (baseColor >> 8 & 0xFF) - * i2 / 255 << 8 | (baseColor & 0xFF) - * i2 / 255; - textures[x + y * 16 + blockId * 256 * 3] = i3; + // Darken bottom of blocks + if (y >= BLOCK_TEXTURE_H * 2) { + brightness /= 2; + } + + // Apply brightness value + int finalColor = + (baseColor >> 16 & 0xFF) * brightness / 255 << 16 | + (baseColor >> 8 & 0xFF) * brightness / 255 << 8 | + (baseColor & 0xFF) * brightness / 255; + + textures [ + x + + y * BLOCK_TEXTURE_H + + blockId * BLOCK_TEXTURE_W * BLOCK_TEXTURE_H * 3 + ] = finalColor; } } diff --git a/src/textures.h b/src/textures.h index 3510129..eb51c5c 100644 --- a/src/textures.h +++ b/src/textures.h @@ -3,7 +3,11 @@ #include #include #include "utility.h" +#include "blocks.h" -extern int textures[12288]; -void genTextures(unsigned int); -void genTexture (int); +#define BLOCK_TEXTURE_W 16 +#define BLOCK_TEXTURE_H 16 +#define TEXTURES_SIZE NUMBER_OF_BLOCKS * BLOCK_TEXTURE_W * BLOCK_TEXTURE_H * 3 + +extern int textures[TEXTURES_SIZE]; +void genTextures (unsigned int);