Skip to content

Commit

Permalink
save and load custom block info and textures over 256
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Feb 3, 2019
1 parent 66e8815 commit 2ae8fd9
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 76 deletions.
16 changes: 6 additions & 10 deletions src/Block.c
Expand Up @@ -266,13 +266,13 @@ void Block_ResetProps(BlockID block) {
Block_CalcLightOffset(block);

if (block >= BLOCK_CPE_COUNT) {
Block_SetTex(0, FACE_YMAX, block);
Block_SetTex(0, FACE_YMIN, block);
Block_Tex(block, FACE_YMAX) = 0;
Block_Tex(block, FACE_YMIN) = 0;
Block_SetSide(0, block);
} else {
Block_SetTex(topTex[block], FACE_YMAX, block);
Block_SetTex(bottomTex[block], FACE_YMIN, block);
Block_SetSide(sideTex[block], block);
Block_Tex(block, FACE_YMAX) = topTex[block];
Block_Tex(block, FACE_YMIN) = bottomTex[block];
Block_SetSide(sideTex[block], block);
}
}

Expand Down Expand Up @@ -310,10 +310,6 @@ void Block_SetSide(TextureLoc texLoc, BlockID blockId) {
Blocks.Textures[index + FACE_ZMAX] = texLoc;
}

void Block_SetTex(TextureLoc texLoc, Face face, BlockID blockId) {
Blocks.Textures[blockId * FACE_COUNT + face] = texLoc;
}


/*########################################################################################################################*
*--------------------------------------------------Block bounds/culling---------------------------------------------------*
Expand Down Expand Up @@ -414,7 +410,7 @@ static float Block_GetSpriteBB_MaxY(int size, int tileX, int tileY, Bitmap* bmp)
void Block_RecalculateBB(BlockID block) {
Bitmap* bmp = &Atlas_Bitmap;
int tileSize = Atlas_TileSize;
TextureLoc texLoc = Block_GetTex(block, FACE_XMAX);
TextureLoc texLoc = Block_Tex(block, FACE_XMAX);
int x = Atlas2D_TileX(texLoc), y = Atlas2D_TileY(texLoc);

Vector3 centre = { 0.5f, 0.0f, 0.5f };
Expand Down
5 changes: 2 additions & 3 deletions src/Block.h
Expand Up @@ -150,9 +150,8 @@ void Block_RecalculateBB(BlockID block);

/* Sets the textures of the side faces of the given block. */
void Block_SetSide(TextureLoc texLoc, BlockID blockId);
/* Sets the texture of the given face of the given block. */
void Block_SetTex(TextureLoc texLoc, Face face, BlockID blockId);
#define Block_GetTex(block, face) Blocks.Textures[(block) * FACE_COUNT + (face)]
/* The texture for the given face of the given block. */
#define Block_Tex(block, face) Blocks.Textures[(block) * FACE_COUNT + (face)]

bool Block_IsFaceHidden(BlockID block, BlockID other, Face face);
/* Updates culling data of all blocks. */
Expand Down
30 changes: 15 additions & 15 deletions src/Builder.c
Expand Up @@ -85,14 +85,14 @@ static int Builder_TotalVerticesCount(void) {
*----------------------------------------------------Base mesh builder----------------------------------------------------*
*#########################################################################################################################*/
static void Builder_AddSpriteVertices(BlockID block) {
int i = Atlas1D_Index(Block_GetTex(block, FACE_XMIN));
int i = Atlas1D_Index(Block_Tex(block, FACE_XMIN));
struct Builder1DPart* part = &Builder_Parts[i];
part->sCount += 4 * 4;
}

static void Builder_AddVertices(BlockID block, Face face) {
int baseOffset = (Blocks.Draw[block] == DRAW_TRANSLUCENT) * ATLAS1D_MAX_ATLASES;
int i = Atlas1D_Index(Block_GetTex(block, face));
int i = Atlas1D_Index(Block_Tex(block, face));
struct Builder1DPart* part = &Builder_Parts[baseOffset + i];
part->fCount[face] += 4;
}
Expand Down Expand Up @@ -477,7 +477,7 @@ static void Builder_DrawSprite(int count) {

#define s_u1 0.0f
#define s_u2 UV2_Scale
loc = Block_GetTex(Builder_Block, FACE_XMAX);
loc = Block_Tex(Builder_Block, FACE_XMAX);
v1 = Atlas1D_RowId(loc) * Atlas1D_InvTileSize;
v2 = v1 + Atlas1D_InvTileSize * UV2_Scale;

Expand Down Expand Up @@ -670,7 +670,7 @@ static void NormalBuilder_RenderBlock(int index) {
Drawer.TintCol = Blocks.FogCol[Builder_Block];

if (count_XMin) {
loc = Block_GetTex(Builder_Block, FACE_XMIN);
loc = Block_Tex(Builder_Block, FACE_XMIN);
offset = (lightFlags >> FACE_XMIN) & 1;
part = &Builder_Parts[baseOffset + Atlas1D_Index(loc)];

Expand All @@ -680,7 +680,7 @@ static void NormalBuilder_RenderBlock(int index) {
}

if (count_XMax) {
loc = Block_GetTex(Builder_Block, FACE_XMAX);
loc = Block_Tex(Builder_Block, FACE_XMAX);
offset = (lightFlags >> FACE_XMAX) & 1;
part = &Builder_Parts[baseOffset + Atlas1D_Index(loc)];

Expand All @@ -690,7 +690,7 @@ static void NormalBuilder_RenderBlock(int index) {
}

if (count_ZMin) {
loc = Block_GetTex(Builder_Block, FACE_ZMIN);
loc = Block_Tex(Builder_Block, FACE_ZMIN);
offset = (lightFlags >> FACE_ZMIN) & 1;
part = &Builder_Parts[baseOffset + Atlas1D_Index(loc)];

Expand All @@ -700,7 +700,7 @@ static void NormalBuilder_RenderBlock(int index) {
}

if (count_ZMax) {
loc = Block_GetTex(Builder_Block, FACE_ZMAX);
loc = Block_Tex(Builder_Block, FACE_ZMAX);
offset = (lightFlags >> FACE_ZMAX) & 1;
part = &Builder_Parts[baseOffset + Atlas1D_Index(loc)];

Expand All @@ -710,7 +710,7 @@ static void NormalBuilder_RenderBlock(int index) {
}

if (count_YMin) {
loc = Block_GetTex(Builder_Block, FACE_YMIN);
loc = Block_Tex(Builder_Block, FACE_YMIN);
offset = (lightFlags >> FACE_YMIN) & 1;
part = &Builder_Parts[baseOffset + Atlas1D_Index(loc)];

Expand All @@ -719,7 +719,7 @@ static void NormalBuilder_RenderBlock(int index) {
}

if (count_YMax) {
loc = Block_GetTex(Builder_Block, FACE_YMAX);
loc = Block_Tex(Builder_Block, FACE_YMAX);
offset = (lightFlags >> FACE_YMAX) & 1;
part = &Builder_Parts[baseOffset + Atlas1D_Index(loc)];

Expand Down Expand Up @@ -925,7 +925,7 @@ static int Adv_StretchZ(int countIndex, int x, int y, int z, int chunkIndex, Blo
#define Adv_Tint(c) c.R = (uint8_t)(c.R * tint.R / 255); c.G = (uint8_t)(c.G * tint.G / 255); c.B = (uint8_t)(c.B * tint.B / 255);

static void Adv_DrawXMin(int count) {
TextureLoc texLoc = Block_GetTex(Builder_Block, FACE_XMIN);
TextureLoc texLoc = Block_Tex(Builder_Block, FACE_XMIN);
float vOrigin = Atlas1D_RowId(texLoc) * Atlas1D_InvTileSize;

float u1 = adv_minBB.Z, u2 = (count - 1) + adv_maxBB.Z * UV2_Scale;
Expand Down Expand Up @@ -966,7 +966,7 @@ static void Adv_DrawXMin(int count) {
}

static void Adv_DrawXMax(int count) {
TextureLoc texLoc = Block_GetTex(Builder_Block, FACE_XMAX);
TextureLoc texLoc = Block_Tex(Builder_Block, FACE_XMAX);
float vOrigin = Atlas1D_RowId(texLoc) * Atlas1D_InvTileSize;

float u1 = (count - adv_minBB.Z), u2 = (1 - adv_maxBB.Z) * UV2_Scale;
Expand Down Expand Up @@ -1007,7 +1007,7 @@ static void Adv_DrawXMax(int count) {
}

static void Adv_DrawZMin(int count) {
TextureLoc texLoc = Block_GetTex(Builder_Block, FACE_ZMIN);
TextureLoc texLoc = Block_Tex(Builder_Block, FACE_ZMIN);
float vOrigin = Atlas1D_RowId(texLoc) * Atlas1D_InvTileSize;

float u1 = (count - adv_minBB.X), u2 = (1 - adv_maxBB.X) * UV2_Scale;
Expand Down Expand Up @@ -1048,7 +1048,7 @@ static void Adv_DrawZMin(int count) {
}

static void Adv_DrawZMax(int count) {
TextureLoc texLoc = Block_GetTex(Builder_Block, FACE_ZMAX);
TextureLoc texLoc = Block_Tex(Builder_Block, FACE_ZMAX);
float vOrigin = Atlas1D_RowId(texLoc) * Atlas1D_InvTileSize;

float u1 = adv_minBB.X, u2 = (count - 1) + adv_maxBB.X * UV2_Scale;
Expand Down Expand Up @@ -1089,7 +1089,7 @@ static void Adv_DrawZMax(int count) {
}

static void Adv_DrawYMin(int count) {
TextureLoc texLoc = Block_GetTex(Builder_Block, FACE_YMIN);
TextureLoc texLoc = Block_Tex(Builder_Block, FACE_YMIN);
float vOrigin = Atlas1D_RowId(texLoc) * Atlas1D_InvTileSize;

float u1 = adv_minBB.X, u2 = (count - 1) + adv_maxBB.X * UV2_Scale;
Expand Down Expand Up @@ -1130,7 +1130,7 @@ static void Adv_DrawYMin(int count) {
}

static void Adv_DrawYMax(int count) {
TextureLoc texLoc = Block_GetTex(Builder_Block, FACE_YMAX);
TextureLoc texLoc = Block_Tex(Builder_Block, FACE_YMAX);
float vOrigin = Atlas1D_RowId(texLoc) * Atlas1D_InvTileSize;

float u1 = adv_minBB.X, u2 = (count - 1) + adv_maxBB.X * UV2_Scale;
Expand Down
2 changes: 1 addition & 1 deletion src/EnvRenderer.c
Expand Up @@ -575,7 +575,7 @@ void EnvRenderer_RenderMapEdges(double delta) {
}

static void EnvRenderer_MakeBorderTex(GfxResourceID* texId, BlockID block) {
TextureLoc loc = Block_GetTex(block, FACE_YMAX);
TextureLoc loc = Block_Tex(block, FACE_YMAX);
if (Gfx_LostContext) return;

Gfx_DeleteTexture(texId);
Expand Down
77 changes: 46 additions & 31 deletions src/Formats.c
Expand Up @@ -557,7 +557,8 @@ static void Cw_Callback_5(struct NbtTag* tag) {
}

if (IsTag(tag->Parent->Parent, "BlockDefinitions") && Game_AllowCustomBlocks) {
if (IsTag(tag, "ID")) { cw_curID = NbtTag_U8(tag); return; }
if (IsTag(tag, "ID")) { cw_curID = NbtTag_U8(tag); return; }
if (IsTag(tag, "ID2")) { cw_curID = NbtTag_U16(tag); return; }
if (IsTag(tag, "CollideType")) { Block_SetCollide(id, NbtTag_U8(tag)); return; }
if (IsTag(tag, "Speed")) { Blocks.SpeedMultiplier[id] = NbtTag_F32(tag); return; }
if (IsTag(tag, "TransmitsLight")) { Blocks.BlocksLight[id] = NbtTag_U8(tag) == 0; return; }
Expand All @@ -573,12 +574,16 @@ static void Cw_Callback_5(struct NbtTag* tag) {

if (IsTag(tag, "Textures")) {
arr = NbtTag_U8_Array(tag, 6);
Block_SetTex(arr[0], FACE_YMAX, id);
Block_SetTex(arr[1], FACE_YMIN, id);
Block_SetTex(arr[2], FACE_XMIN, id);
Block_SetTex(arr[3], FACE_XMAX, id);
Block_SetTex(arr[4], FACE_ZMIN, id);
Block_SetTex(arr[5], FACE_ZMAX, id);
Block_Tex(id, FACE_YMAX) = arr[0]; Block_Tex(id, FACE_YMIN) = arr[1];
Block_Tex(id, FACE_XMIN) = arr[2]; Block_Tex(id, FACE_XMAX) = arr[3];
Block_Tex(id, FACE_ZMIN) = arr[4]; Block_Tex(id, FACE_ZMAX) = arr[5];

/* hacky way of storing upper 8 bits */
if (tag->DataSize >= 12) {
Block_Tex(id, FACE_YMAX) |= arr[6] << 8; Block_Tex(id, FACE_YMIN) |= arr[7] << 8;
Block_Tex(id, FACE_XMIN) |= arr[8] << 8; Block_Tex(id, FACE_XMAX) |= arr[9] << 8;
Block_Tex(id, FACE_ZMIN) |= arr[10] << 8; Block_Tex(id, FACE_ZMAX) |= arr[11] << 8;
}
return;
}

Expand Down Expand Up @@ -934,12 +939,17 @@ static uint8_t cw_meta_cpe[303] = {
static uint8_t cw_meta_defs[19] = {
NBT_DICT, 0,16, 'B','l','o','c','k','D','e','f','i','n','i','t','i','o','n','s',
};
static uint8_t cw_meta_def[173] = {
NBT_DICT, 0,7, 'B','l','o','c','k','\0','\0',
static uint8_t cw_meta_def[189] = {
NBT_DICT, 0,9, 'B','l','o','c','k','\0','\0','\0','\0',
NBT_I8, 0,2, 'I','D', 0,
/* It would be have been better to just change ID to be a I16 */
/* Unfortunately this isn't backwards compatible with ClassicalSharp */
NBT_I16, 0,3, 'I','D','2', 0,0,
NBT_I8, 0,11, 'C','o','l','l','i','d','e','T','y','p','e', 0,
NBT_F32, 0,5, 'S','p','e','e','d', 0,0,0,0,
NBT_I8S, 0,8, 'T','e','x','t','u','r','e','s', 0,0,0,6, 0,0,0,0,0,0,
/* Ugly hack for supporting texture IDs over 255 */
/* First 6 elements are lower 8 bits, next 6 are upper 8 bits */
NBT_I8S, 0,8, 'T','e','x','t','u','r','e','s', 0,0,0,12, 0,0,0,0,0,0, 0,0,0,0,0,0,
NBT_I8, 0,14, 'T','r','a','n','s','m','i','t','s','L','i','g','h','t', 0,
NBT_I8, 0,9, 'W','a','l','k','S','o','u','n','d', 0,
NBT_I8, 0,10, 'F','u','l','l','B','r','i','g','h','t', 0,
Expand All @@ -956,53 +966,58 @@ static uint8_t cw_end[4] = {
NBT_END,
};


static ReturnCode Cw_WriteBockDef(struct Stream* stream, int b) {
uint8_t tmp[512];
String name;
int len;

bool sprite = Blocks.Draw[b] == DRAW_SPRITE;
union IntAndFloat speed;
TextureLoc tex;
uint8_t fog;
PackedCol col;
Vector3 minBB, maxBB;

Mem_Copy(tmp, cw_meta_def, sizeof(cw_meta_def));
{
/* Hacky unique tag name for each */
name = String_Init(&tmp[8], 0, 2);
name = String_Init(&tmp[8], 0, 4);
String_AppendHex(&name, b >> 8);
String_AppendHex(&name, b);
tmp[15] = b;

tmp[30] = Blocks.Collide[b];
tmp[17] = b;
Stream_SetU16_BE(&tmp[24], b);

tmp[40] = Blocks.Collide[b];
speed.f = Blocks.SpeedMultiplier[b];
Stream_SetU32_BE(&tmp[39], speed.u);
Stream_SetU32_BE(&tmp[49], speed.u);

tmp[58] = (uint8_t)Block_GetTex(b, FACE_YMAX);
tmp[59] = (uint8_t)Block_GetTex(b, FACE_YMIN);
tmp[60] = (uint8_t)Block_GetTex(b, FACE_XMIN);
tmp[61] = (uint8_t)Block_GetTex(b, FACE_XMAX);
tmp[62] = (uint8_t)Block_GetTex(b, FACE_ZMIN);
tmp[63] = (uint8_t)Block_GetTex(b, FACE_ZMAX);
tex = Block_Tex(b, FACE_YMAX); tmp[68] = tex; tmp[74] = tex >> 8;
tex = Block_Tex(b, FACE_YMIN); tmp[69] = tex; tmp[75] = tex >> 8;
tex = Block_Tex(b, FACE_XMIN); tmp[70] = tex; tmp[76] = tex >> 8;
tex = Block_Tex(b, FACE_XMAX); tmp[71] = tex; tmp[77] = tex >> 8;
tex = Block_Tex(b, FACE_ZMIN); tmp[72] = tex; tmp[78] = tex >> 8;
tex = Block_Tex(b, FACE_ZMAX); tmp[73] = tex; tmp[79] = tex >> 8;

tmp[81] = Blocks.BlocksLight[b] ? 0 : 1;
tmp[94] = Blocks.DigSounds[b];
tmp[108] = Blocks.FullBright[b] ? 1 : 0;
tmp[117] = sprite ? 0 : (uint8_t)(Blocks.MaxBB[b].Y * 16);
tmp[130] = sprite ? Blocks.SpriteOffset[b] : Blocks.Draw[b];
tmp[97] = Blocks.BlocksLight[b] ? 0 : 1;
tmp[110] = Blocks.DigSounds[b];
tmp[124] = Blocks.FullBright[b] ? 1 : 0;
tmp[133] = sprite ? 0 : (uint8_t)(Blocks.MaxBB[b].Y * 16);
tmp[146] = sprite ? Blocks.SpriteOffset[b] : Blocks.Draw[b];

fog = (uint8_t)(128 * Blocks.FogDensity[b] - 1);
col = Blocks.FogCol[b];
tmp[141] = Blocks.FogDensity[b] ? fog : 0;
tmp[142] = col.R; tmp[143] = col.G; tmp[144] = col.B;
tmp[157] = Blocks.FogDensity[b] ? fog : 0;
tmp[158] = col.R; tmp[159] = col.G; tmp[160] = col.B;

minBB = Blocks.MinBB[b]; maxBB = Blocks.MaxBB[b];
tmp[158] = (uint8_t)(minBB.X * 16); tmp[159] = (uint8_t)(minBB.Y * 16); tmp[160] = (uint8_t)(minBB.Z * 16);
tmp[161] = (uint8_t)(maxBB.X * 16); tmp[162] = (uint8_t)(maxBB.Y * 16); tmp[163] = (uint8_t)(maxBB.Z * 16);
tmp[174] = (uint8_t)(minBB.X * 16); tmp[175] = (uint8_t)(minBB.Y * 16); tmp[176] = (uint8_t)(minBB.Z * 16);
tmp[177] = (uint8_t)(maxBB.X * 16); tmp[178] = (uint8_t)(maxBB.Y * 16); tmp[179] = (uint8_t)(maxBB.Z * 16);
}

name = Block_UNSAFE_GetName(b);
len = Cw_WriteEndString(&tmp[171], &name);
len = Cw_WriteEndString(&tmp[187], &name);
return Stream_Write(stream, tmp, sizeof(cw_meta_def) + len);
}

Expand Down Expand Up @@ -1058,7 +1073,7 @@ ReturnCode Cw_Save(struct Stream* stream) {
if ((res = Stream_Write(stream, tmp, sizeof(cw_meta_cpe) + len))) return res;

if ((res = Stream_Write(stream, cw_meta_defs, sizeof(cw_meta_defs)))) return res;
for (b = 1; b < 256; b++) {
for (b = 1; b < BLOCK_COUNT; b++) {
if (!Block_IsCustomDefined(b)) continue;
if ((res = Cw_WriteBockDef(stream, b))) return res;
}
Expand Down
6 changes: 3 additions & 3 deletions src/IsometricDrawer.c
Expand Up @@ -62,15 +62,15 @@ static void IsometricDrawer_Flush(void) {
}

static TextureLoc IsometricDrawer_GetTexLoc(BlockID block, Face face) {
TextureLoc loc = Block_GetTex(block, face);
TextureLoc loc = Block_Tex(block, face);
iso_texIndex = Atlas1D_Index(loc);

if (iso_lastTexIndex != iso_texIndex) IsometricDrawer_Flush();
return loc;
}

static void IsometricDrawer_SpriteZQuad(BlockID block, bool firstPart) {
TextureLoc loc = Block_GetTex(block, FACE_ZMAX);
TextureLoc loc = Block_Tex(block, FACE_ZMAX);
TextureRec rec = Atlas1D_TexRec(loc, 1, &iso_texIndex);

VertexP3fT2fC4b v;
Expand Down Expand Up @@ -99,7 +99,7 @@ static void IsometricDrawer_SpriteZQuad(BlockID block, bool firstPart) {
}

static void IsometricDrawer_SpriteXQuad(BlockID block, bool firstPart) {
TextureLoc loc = Block_GetTex(block, FACE_XMAX);
TextureLoc loc = Block_Tex(block, FACE_XMAX);
TextureRec rec = Atlas1D_TexRec(loc, 1, &iso_texIndex);

VertexP3fT2fC4b v;
Expand Down
8 changes: 4 additions & 4 deletions src/Model.c
Expand Up @@ -1505,8 +1505,8 @@ static void BlockModel_Flush(void) {

#define BlockModel_FlushIfNotSame if (bModel_lastTexIndex != bModel_texIndex) { BlockModel_Flush(); }
static TextureLoc BlockModel_GetTex(Face face, VertexP3fT2fC4b** ptr) {
TextureLoc texLoc = Block_GetTex(bModel_block, face);
bModel_texIndex = Atlas1D_Index(texLoc);
TextureLoc texLoc = Block_Tex(bModel_block, face);
bModel_texIndex = Atlas1D_Index(texLoc);
BlockModel_FlushIfNotSame;

/* Need to reload ptr, in case was flushed */
Expand All @@ -1519,7 +1519,7 @@ static void BlockModel_SpriteZQuad(bool firstPart, bool mirror) {
VertexP3fT2fC4b* ptr, v;
PackedCol col;
float xz1, xz2;
TextureLoc loc = Block_GetTex(bModel_block, FACE_ZMAX);
TextureLoc loc = Block_Tex(bModel_block, FACE_ZMAX);
TextureRec rec = Atlas1D_TexRec(loc, 1, &bModel_texIndex);

BlockModel_FlushIfNotSame;
Expand Down Expand Up @@ -1549,7 +1549,7 @@ static void BlockModel_SpriteXQuad(bool firstPart, bool mirror) {
VertexP3fT2fC4b* ptr, v;
PackedCol col;
float x1, x2, z1, z2;
TextureLoc loc = Block_GetTex(bModel_block, FACE_XMAX);
TextureLoc loc = Block_Tex(bModel_block, FACE_XMAX);
TextureRec rec = Atlas1D_TexRec(loc, 1, &bModel_texIndex);

BlockModel_FlushIfNotSame;
Expand Down

0 comments on commit 2ae8fd9

Please sign in to comment.