mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-28 16:05:54 -04:00
Implement World class in C
This commit is contained in:
parent
b3beb74d84
commit
7973d3b2b8
@ -216,6 +216,7 @@
|
|||||||
<ClCompile Include="Vector3I.c" />
|
<ClCompile Include="Vector3I.c" />
|
||||||
<ClCompile Include="Vectors.c" />
|
<ClCompile Include="Vectors.c" />
|
||||||
<ClCompile Include="WinPlatform.c" />
|
<ClCompile Include="WinPlatform.c" />
|
||||||
|
<ClCompile Include="World.c" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
@ -67,6 +67,9 @@
|
|||||||
<Filter Include="Header Files\Map">
|
<Filter Include="Header Files\Map">
|
||||||
<UniqueIdentifier>{0ff1e440-e67d-4e09-913d-56417fbfe6b1}</UniqueIdentifier>
|
<UniqueIdentifier>{0ff1e440-e67d-4e09-913d-56417fbfe6b1}</UniqueIdentifier>
|
||||||
</Filter>
|
</Filter>
|
||||||
|
<Filter Include="Source Files\Map">
|
||||||
|
<UniqueIdentifier>{cb26e83f-9153-4964-9dc2-13502fbb1d1b}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="NotchyGenerator.h">
|
<ClInclude Include="NotchyGenerator.h">
|
||||||
@ -206,5 +209,8 @@
|
|||||||
<ClCompile Include="ExtMath.c">
|
<ClCompile Include="ExtMath.c">
|
||||||
<Filter>Source Files\Math</Filter>
|
<Filter>Source Files\Math</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="World.c">
|
||||||
|
<Filter>Source Files\Map</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
79
src/Client/World.c
Normal file
79
src/Client/World.c
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#include "World.h"
|
||||||
|
#include "BlockID.h"
|
||||||
|
#include "ErrorHandler.h"
|
||||||
|
#include "String.h"
|
||||||
|
|
||||||
|
void World_Reset() {
|
||||||
|
World_Width = 0; World_Height = 0; World_Length = 0;
|
||||||
|
World_Blocks = NULL; World_BlocksSize = 0;
|
||||||
|
Uuid = Guid.NewGuid();
|
||||||
|
}
|
||||||
|
|
||||||
|
void World_SetNewMap(BlockID* blocks, Int32 blocksSize, Int32 width, Int32 height, Int32 length) {
|
||||||
|
World_Width = width; World_Height = height; World_Length = length;
|
||||||
|
World_Blocks = blocks; World_BlocksSize = blocksSize;
|
||||||
|
if (World_BlocksSize == 0) World_Blocks = NULL;
|
||||||
|
|
||||||
|
if (blocksSize != (width * height * length)) {
|
||||||
|
ErrorHandler_Fail(String_FromConstant("Blocks array size does not match volume of map."));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Env.EdgeHeight == -1) Env.EdgeHeight = height / 2;
|
||||||
|
if (Env.CloudHeight == -1) Env.CloudHeight = height + 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
BlockID World_GetPhysicsBlock(Int32 x, Int32 y, Int32 z) {
|
||||||
|
if (x < 0 || x >= World_Width || z < 0 || z >= World_Length || y < 0) return BlockID_Bedrock;
|
||||||
|
if (y >= World_Height) return BlockID_Air;
|
||||||
|
|
||||||
|
return World_Blocks[(y * World_Length + z) * World_Width + x];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void World_SetBlock(Int32 x, Int32 y, Int32 z, BlockID blockId) {
|
||||||
|
World_Blocks[(y * World_Length + z) * World_Width + x] = blockId;
|
||||||
|
}
|
||||||
|
|
||||||
|
void World_SetBlock_3I(Vector3I p, BlockID blockId) {
|
||||||
|
World_Blocks[(p.Y * World_Length + p.Z) * World_Width + p.X] = blockId;
|
||||||
|
}
|
||||||
|
|
||||||
|
BlockID World_GetBlock(Int32 x, Int32 y, Int32 z) {
|
||||||
|
return World_Blocks[(y * World_Length + z) * World_Width + x];
|
||||||
|
}
|
||||||
|
|
||||||
|
BlockID World_GetBlock_3I(Vector3I p) {
|
||||||
|
return World_Blocks[(p.Y * World_Length + p.Z) * World_Width + p.X];
|
||||||
|
}
|
||||||
|
|
||||||
|
BlockID World_SafeGetBlock(Int32 x, Int32 y, Int32 z) {
|
||||||
|
return World_IsValidPos(x, y, z) ?
|
||||||
|
World_Blocks[(y * World_Length + z) * World_Width + x] : BlockID_Air;
|
||||||
|
}
|
||||||
|
|
||||||
|
BlockID World_SafeGetBlock_3I(Vector3I p) {
|
||||||
|
return World_IsValidPos(p.X, p.Y, p.Z) ?
|
||||||
|
World_Blocks[(p.Y * World_Length + p.Z) * World_Width + p.X] : BlockID_Air;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool World_IsValidPos(Int32 x, Int32 y, Int32 z) {
|
||||||
|
return x >= 0 && y >= 0 && z >= 0 &&
|
||||||
|
x < World_Width && y < World_Height && z < World_Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool World_IsValidPos_3I(Vector3I p) {
|
||||||
|
return p.X >= 0 && p.Y >= 0 && p.Z <= 0 &&
|
||||||
|
p.X < World_Width && p.Y < World_Height && p.Z < World_Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector3I World_GetCoords(Int32 index) {
|
||||||
|
if (index < 0 || index >= World_BlocksSize)
|
||||||
|
return Vector3I_Create1(-1);
|
||||||
|
|
||||||
|
Vector3I v;
|
||||||
|
v.X = index % World_Width;
|
||||||
|
v.Y = index / (World_Width * World_Length);
|
||||||
|
v.Z = (index / World_Width) % World_Length;
|
||||||
|
return v;
|
||||||
|
}
|
@ -9,22 +9,26 @@ Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
|
|||||||
|
|
||||||
|
|
||||||
/* Raw blocks of this world. */
|
/* Raw blocks of this world. */
|
||||||
BlockID* World_Blocks;
|
BlockID* World_Blocks = NULL;
|
||||||
|
|
||||||
|
/* Size of blocks array. */
|
||||||
|
Int32 World_BlocksSize = 0;
|
||||||
|
|
||||||
/* Length of world on X axis.*/
|
/* Length of world on X axis.*/
|
||||||
Int32 World_Width;
|
Int32 World_Width = 0;
|
||||||
|
|
||||||
/* Length of world on Y axis (vertical).*/
|
/* Length of world on Y axis (vertical).*/
|
||||||
Int32 World_Height;
|
Int32 World_Height = 0;
|
||||||
|
|
||||||
/* Length of world on Z axis.*/
|
/* Length of world on Z axis.*/
|
||||||
Int32 World_Length;
|
Int32 World_Length = 0;
|
||||||
|
|
||||||
/* Unique uuid/guid of this particular world. */
|
/* Unique uuid/guid of this particular world. */
|
||||||
UInt8 World_Uuid[16];
|
UInt8 World_Uuid[16];
|
||||||
|
|
||||||
/* Current terrain.png or texture pack url of this map. */
|
/* Current terrain.png or texture pack url of this map. */
|
||||||
String World_TextureUrl;
|
String World_TextureUrl;
|
||||||
|
// TODO: how to initalise this string
|
||||||
|
|
||||||
|
|
||||||
/* Resets all of the properties to their defaults. */
|
/* Resets all of the properties to their defaults. */
|
||||||
@ -63,3 +67,4 @@ bool World_IsValidPos_3I(Vector3I p);
|
|||||||
|
|
||||||
/* Unpacks the given index into the map's block array into its original world coordinates. */
|
/* Unpacks the given index into the map's block array into its original world coordinates. */
|
||||||
Vector3I World_GetCoords(int index);
|
Vector3I World_GetCoords(int index);
|
||||||
|
#endif
|
Loading…
x
Reference in New Issue
Block a user