Fix World_GetBlock not working in plugins, simplify world api slightly

This commit is contained in:
UnknownShadow200 2019-05-04 20:22:17 +10:00
parent ca62280043
commit f1b7bd2ea5
10 changed files with 22 additions and 34 deletions

View File

@ -10,7 +10,6 @@
#include "GameStructs.h"
struct _BlockLists Blocks;
int Block_UsedCount, Block_IDMask;
const char* Sound_Names[SOUND_COUNT] = {
"none", "wood", "gravel", "grass", "stone",
@ -144,13 +143,6 @@ const static uint8_t bottomTex[BLOCK_CPE_COUNT] = { 0, 1, 2, 2, 16, 4, 15,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 13, 12, 29, 28, 56, 55, 6, 6, 7, 10,
4, 36, 37, 16, 11, 57, 50, 38, 80, 81, 82, 83, 84, 51, 54, 86, 58, 53, 52 };
#ifdef EXTENDED_BLOCKS
void Block_SetUsedCount(int count) {
Block_UsedCount = count;
Block_IDMask = Math_NextPowOf2(count) - 1;
}
#endif
bool Block_IsCustomDefined(BlockID block) {
return (Block_DefinedCustomBlocks[block >> 5] & (1u << (block & 0x1F))) != 0;
}
@ -694,10 +686,6 @@ static void Blocks_Reset(void) {
Block_ResetProps((BlockID)block);
}
Block_UpdateAllCulling();
#ifdef EXTENDED_BLOCKS
Block_SetUsedCount(256);
#endif
Block_RecalculateAllSpriteBB();
}

View File

@ -111,7 +111,6 @@ if (Blocks.Tinted[block]) {\
}
#ifdef EXTENDED_BLOCKS
extern int Block_UsedCount, Block_IDMask;
extern void Block_SetUsedCount(int count);
#endif

View File

@ -266,7 +266,7 @@ static bool ReadChunkData(int x1, int y1, int z1, bool* outAllAir) {
#ifndef EXTENDED_BLOCKS
ReadChunkBody(World.Blocks[index]);
#else
if (Block_UsedCount <= 256) {
if (World.IDMask <= 0xFF) {
ReadChunkBody(World.Blocks[index]);
} else {
ReadChunkBody(World.Blocks[index] | (World.Blocks2[index] << 8));
@ -312,7 +312,7 @@ static bool ReadBorderChunkData(int x1, int y1, int z1, bool* outAllAir) {
#ifndef EXTENDED_BLOCKS
ReadBorderChunkBody(World.Blocks[index]);
#else
if (Block_UsedCount <= 256) {
if (World.IDMask <= 0xFF) {
ReadBorderChunkBody(World.Blocks[index]);
} else {
ReadBorderChunkBody(World.Blocks[index] | (World.Blocks2[index] << 8));

View File

@ -392,7 +392,7 @@ static int EnvRenderer_CalcRainHeightAt(int x, int maxY, int z, int hIndex) {
#ifndef EXTENDED_BLOCKS
EnvRenderer_RainCalcBody(World.Blocks[i]);
#else
if (Block_UsedCount <= 256) {
if (World.IDMask <= 0xFF) {
EnvRenderer_RainCalcBody(World.Blocks[i]);
} else {
EnvRenderer_RainCalcBody(World.Blocks[i] | (World.Blocks2[i] << 8));

View File

@ -57,9 +57,6 @@ void Map_LoadFrom(const String* path) {
IMapImporter importer;
struct Stream stream;
ReturnCode res;
World_Reset();
Event_RaiseVoid(&WorldEvents.NewMap);
Game_Reset();
res = Stream_OpenFile(&stream, path);

View File

@ -177,9 +177,6 @@ void Game_UpdateProjection(void) {
void Game_Disconnect(const String* title, const String* reason) {
Event_RaiseVoid(&NetEvents.Disconnected);
World_Reset();
Event_RaiseVoid(&WorldEvents.NewMap);
Gui_FreeActive();
Gui_SetActive(DisconnectScreen_MakeInstance(title, reason));
Game_Reset();
@ -187,6 +184,9 @@ void Game_Disconnect(const String* title, const String* reason) {
void Game_Reset(void) {
struct IGameComponent* comp;
World_Reset();
Event_RaiseVoid(&WorldEvents.NewMap);
if (World_TextureUrl.length) {
TexturePack_ExtractDefault();
World_TextureUrl.length = 0;
@ -471,7 +471,7 @@ static void Game_Load(void) {
Game_AddComponent(&Animations_Component);
Game_AddComponent(&Inventory_Component);
Env_Reset();
World_Reset();
Game_AddComponent(&MapRenderer_Component);
Game_AddComponent(&EnvRenderer_Component);

View File

@ -30,7 +30,7 @@ static int Lighting_CalcHeightAt(int x, int maxY, int z, int hIndex) {
#ifndef EXTENDED_BLOCKS
Lighting_CalcBody(World.Blocks[i]);
#else
if (Block_UsedCount <= 256) {
if (World.IDMask <= 0xFF) {
Lighting_CalcBody(World.Blocks[i]);
} else {
Lighting_CalcBody(World.Blocks[i] | (World.Blocks2[i] << 8));
@ -145,7 +145,7 @@ static bool Lighting_NeedsNeighour(BlockID block, int i, int minY, int y, int nY
#ifndef EXTENDED_BLOCKS
Lighting_NeedsNeighourBody(World.Blocks[i]);
#else
if (Block_UsedCount <= 256) {
if (World.IDMask <= 0xFF) {
Lighting_NeedsNeighourBody(World.Blocks[i]);
} else {
Lighting_NeedsNeighourBody(World.Blocks[i] | (World.Blocks2[i] << 8));
@ -312,7 +312,7 @@ static bool Lighting_CalculateHeightmapCoverage(int x1, int z1, int xCount, int
#ifndef EXTENDED_BLOCKS
Lighting_CalculateBody(World.Blocks[mapIndex]);
#else
if (Block_UsedCount <= 256) {
if (World.IDMask <= 0xFF) {
Lighting_CalculateBody(World.Blocks[mapIndex]);
} else {
Lighting_CalculateBody(World.Blocks[mapIndex] | (World.Blocks2[mapIndex] << 8));

View File

@ -536,7 +536,6 @@ static void MPConnection_Reset(void) {
}
net_writeFailed = false;
Block_SetUsedCount(256);
Protocol_Reset();
Server_Free();
}

View File

@ -38,13 +38,13 @@ void World_Reset(void) {
#ifdef EXTENDED_BLOCKS
if (World.Blocks != World.Blocks2) Mem_Free(World.Blocks2);
World.Blocks2 = NULL;
World.IDMask = 0xFF;
#endif
Mem_Free(World.Blocks);
World.Blocks = NULL;
World_SetDimensions(0, 0, 0);
Env_Reset();
World_NewUuid();
}
void World_SetNewMap(BlockRaw* blocks, int width, int height, int length) {
@ -56,12 +56,13 @@ void World_SetNewMap(BlockRaw* blocks, int width, int height, int length) {
/* .cw maps may have set this to a non-NULL when importing */
if (!World.Blocks2) {
World.Blocks2 = World.Blocks;
Block_SetUsedCount(256);
World.IDMask = 0xFF;
}
#endif
if (Env.EdgeHeight == -1) { Env.EdgeHeight = height / 2; }
if (Env.CloudsHeight == -1) { Env.CloudsHeight = height + 2; }
World_NewUuid();
}
CC_NOINLINE void World_SetDimensions(int width, int height, int length) {
@ -77,7 +78,7 @@ CC_NOINLINE void World_SetDimensions(int width, int height, int length) {
#ifdef EXTENDED_BLOCKS
void World_SetMapUpper(BlockRaw* blocks) {
World.Blocks2 = blocks;
Block_SetUsedCount(768);
World.IDMask = 0x3FF;
}
#endif
@ -90,8 +91,7 @@ void World_SetBlock(int x, int y, int z, BlockID block) {
/* defer allocation of second map array if possible */
if (World.Blocks == World.Blocks2) {
if (block < 256) return;
World.Blocks2 = Mem_AllocCleared(World.Volume, 1, "blocks array upper");
Block_SetUsedCount(768);
World_SetMapUpper(Mem_AllocCleared(World.Volume, 1, "blocks array upper"));
}
World.Blocks2[i] = (BlockRaw)(block >> 8);
}

View File

@ -33,6 +33,12 @@ CC_VAR extern struct _WorldData {
int OneY;
/* Unique identifier for this world. */
uint8_t Uuid[16];
#ifdef EXTENDED_BLOCKS
/* Masks access to World.Blocks/World.Blocks2 */
/* e.g. this will be 255 if only 8 bit blocks are used */
int IDMask;
#endif
} World;
extern String World_TextureUrl;
@ -46,7 +52,6 @@ CC_API void World_SetNewMap(BlockRaw* blocks, int width, int height, int length)
CC_NOINLINE void World_SetDimensions(int width, int height, int length);
#ifdef EXTENDED_BLOCKS
extern int Block_IDMask;
/* Sets World.Blocks2 and updates internal state for more than 256 blocks. */
void World_SetMapUpper(BlockRaw* blocks);
@ -54,7 +59,7 @@ void World_SetMapUpper(BlockRaw* blocks);
/* NOTE: Does NOT check that the coordinates are inside the map. */
static CC_INLINE BlockID World_GetBlock(int x, int y, int z) {
int i = World_Pack(x, y, z);
return (BlockID)((World.Blocks[i] | (World.Blocks2[i] << 8)) & Block_IDMask);
return (BlockID)((World.Blocks[i] | (World.Blocks2[i] << 8)) & World.IDMask);
}
#else
#define World_GetBlock(x, y, z) World_Blocks[World_Pack(x, y, z)]