Port more stuff to C

This commit is contained in:
UnknownShadow200 2017-04-29 14:30:47 +10:00
parent 3d8fc9eddb
commit 3e8de6a371
9 changed files with 465 additions and 153 deletions

147
src/Client/Block.c Normal file
View File

@ -0,0 +1,147 @@
#include "Block.h"
#include "DefaultSet.h"
void Block_Reset(Game* game) {
Init();
// TODO: Make this part of TerrainAtlas2D maybe?
using (FastBitmap fastBmp = new FastBitmap(game.TerrainAtlas.AtlasBitmap, true, true))
RecalculateSpriteBB(fastBmp);
}
void Block_Init() {
for (Int32 i = 0; i < DefinedCustomBlocks.Length; i++)
DefinedCustomBlocks[i] = 0;
for (Int32 block = 0; block < Block_Count; block++)
ResetBlockProps((BlockID)block);
UpdateCulling();
}
void Block_SetDefaultPerms(InventoryPermissions* place, InventoryPermissions* del) {
for (Int32 block = BlockID_Stone; block <= Block_MaxDefined; block++) {
place[block] = true;
del[block] = true;
}
place[BlockID_Lava] = false; del[BlockID_Lava] = false;
place[BlockID_Water] = false; del[BlockID_Water] = false;
place[BlockID_StillLava] = false; del[BlockID_StillLava] = false;
place[BlockID_StillWater] = false; del[BlockID_StillWater] = false;
place[BlockID_Bedrock] = false; del[BlockID_Bedrock] = false;
}
void Block_SetCollide(BlockID block, UInt8 collide) {
// necessary for cases where servers redefined core blocks before extended types were introduced
collide = DefaultSet_MapOldCollide(block, collide);
ExtendedCollide[block] = collide;
// Reduce extended collision types to their simpler forms
if (collide == CollideType_Ice) collide = CollideType_Solid;
if (collide == CollideType_SlipperyIce) collide = CollideType_Solid;
if (collide == CollideType_LiquidWater) collide = CollideType_Liquid;
if (collide == CollideType_LiquidLava) collide = CollideType_Liquid;
Collide[block] = collide;
}
void Block_SetDrawType(BlockID block, UInt8 draw) {
if (draw == DrawType_Opaque && Collide[block] != CollideType_Solid)
draw = DrawType_Transparent;
Draw[block] = draw;
FullOpaque[block] = draw == DrawType_Opaque
&& MinBB[block] == Vector3.Zero && MaxBB[block] == Vector3.One;
}
void Block_ResetProps(BlockID block) {
BlocksLight[block] = DefaultSet_BlocksLight(block);
FullBright[block] = DefaultSet_FullBright(block);
FogColour[block] = DefaultSet_FogColour(block);
FogDensity[block] = DefaultSet_FogDensity(block);
SetCollide(block, DefaultSet_Collide(block));
DigSounds[block] = DefaultSet_DigSound(block);
StepSounds[block] = DefaultSet_StepSound(block);
SpeedMultiplier[block] = 1;
Name[block] = Block_DefaultName(block);
Tinted[block] = false;
Draw[block] = DefaultSet_Draw(block);
if (Draw[block] == DrawType_Sprite) {
MinBB[block] = new Vector3(2.50f / 16f, 0, 2.50f / 16f);
MaxBB[block] = new Vector3(13.5f / 16f, 1, 13.5f / 16f);
}
else {
MinBB[block] = Vector3.Zero;
MaxBB[block] = Vector3.One;
MaxBB[block].Y = DefaultSet_Height(block);
}
SetBlockDraw(block, Draw[block]);
CalcRenderBounds(block);
LightOffset[block] = CalcLightOffset(block);
if (block >= Block_CpeCount) {
#if USE16_BIT
// give some random texture ids
SetTex((block * 10 + (block % 7) + 20) % 80, Side.Top, block);
SetTex((block * 8 + (block & 5) + 5) % 80, Side.Bottom, block);
SetSide((block * 4 + (block / 4) + 4) % 80, block);
#else
SetTex(0, Side.Top, block);
SetTex(0, Side.Bottom, block);
SetSide(0, block);
#endif
} else {
SetTex(topTex[block], Side.Top, block);
SetTex(bottomTex[block], Side.Bottom, block);
SetSide(sideTex[block], block);
}
}
Int32 Block_FindID(String* name) {
for (Int32 i = 0; i < Block_Count; i++) {
if (String_CaselessEquals(&Name[i], name)) return i;
}
return -1;
}
String Block_DefaultName(BlockID block) {
#if USE16_BIT
if (block >= 256) return "ID " + block;
#endif
if (block >= Block_CpeCount) {
String str;
String_Constant(&str, "Invalid");
return str;
}
// Find start and end of this particular block name
int start = 0;
for (int i = 0; i < block; i++)
start = Block.Names.IndexOf(' ', start) + 1;
int end = Block.Names.IndexOf(' ', start);
if (end == -1) end = Block.Names.Length;
buffer.Clear();
SplitUppercase(buffer, start, end);
return buffer.ToString();
}
static void SplitUppercase(StringBuffer buffer, int start, int end) {
int index = 0;
for (int i = start; i < end; i++) {
char c = Block.Names[i];
bool upper = Char.IsUpper(c) && i > start;
bool nextLower = i < end - 1 && !Char.IsUpper(Block.Names[i + 1]);
if (upper && nextLower) {
buffer.Append(ref index, ' ');
buffer.Append(ref index, Char.ToLower(c));
}
else {
buffer.Append(ref index, c);
}
}
}
}
}

View File

@ -1,97 +1,135 @@
#ifndef CS_BLOCK_H
#define CS_BLOCK_H
/* List of all core/standard block IDs
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
/* Classic blocks */
#define Block_Air 0
#define Block_Stone 1
#define Block_Grass 2
#define Block_Dirt 3
#define Block_Cobblestone 4
#define Block_Wood 5
#define Block_Sapling 6
#define Block_Bedrock 7
#define Block_Water 8
#define Block_StillWater 9
#define Block_Lava 10
#define Block_StillLava 11
#define Block_Sand 12
#define Block_Gravel 13
#define Block_GoldOre 14
#define Block_IronOre 15
#define Block_CoalOre 16
#define Block_Log 17
#define Block_Leaves 18
#define Block_Sponge 19
#define Block_Glass 20
#define Block_Red 21
#define Block_Orange 22
#define Block_Yellow 23
#define Block_Lime 24
#define Block_Green 25
#define Block_Teal 26
#define Block_Aqua 27
#define Block_Cyan 28
#define Block_Blue 29
#define Block_Indigo 30
#define Block_Violet 31
#define Block_Magenta 32
#define Block_Pink 33
#define Block_Black 34
#define Block_Gray 35
#define Block_White 36
#define Block_Dandelion 37
#define Block_Rose 38
#define Block_BrownMushroom 39
#define Block_RedMushroom 40
#define Block_Gold 41
#define Block_Iron 42
#define Block_DoubleSlab 43
#define Block_Slab 44
#define Block_Brick 45
#define Block_TNT 46
#define Block_Bookshelf 47
#define Block_MossyRocks 48
#define Block_Obsidian 49
/* CPE blocks */
#define Block_CobblestoneSlab 50
#define Block_Rope 51
#define Block_Sandstone 52
#define Block_Snow 53
#define Block_Fire 54
#define Block_LightPink 55
#define Block_ForestGreen 56
#define Block_Brown 57
#define Block_DeepBlue 58
#define Block_Turquoise 59
#define Block_Ice 60
#define Block_CeramicTile 61
#define Block_Magma 62
#define Block_Pillar 63
#define Block_Crate 64
#define Block_StoneBrick 65
#ifndef CS_BLOCK_H
#define CS_BLOCK_H
#include "Typedefs.h"
#include "BlockID.h"
#include "String.h"
#include "FastColour.h"
/* Stores properties for blocks
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
/* Max block ID used in original classic */
#define Block_MaxOriginal Block_Obsidian
/* Number of blocks in original classic. */
#define Block_OriginalCount (Block_MaxOriginal + 1)
/* Sound types for blocks. */
/* Max block ID used in original classic plus CPE blocks. */
#define Block_MaxCpe Block_StoneBrick
#define SoundType_None 0
#define SoundType_Wood 1
#define SoundType_Gravel 2
#define SoundType_Grass 3
#define SoundType_Stone 4
#define SoundType_Metal 5
#define SoundType_Glass 6
#define SoundType_Cloth 7
#define SoundType_Sand 8
#define SoundType_Snow 9
/* Number of blocks in original classic plus CPE blocks. */
#define Block_CpeCount = (Block_MaxCpeBlock + 1)
#if USE16_BIT
#define Block_MaxDefined 0xFFF
#else
#define Block_MaxDefined 0xFF
#endif
/* Describes how a block is rendered in the world. */
/* Completely covers blocks behind (e.g. dirt). */
#define DrawType_Opaque 0
/* Blocks behind show (e.g. glass). Pixels are either fully visible or invisible. */
#define DrawType_Transparent 1
/* Same as Transparent, but all neighbour faces show. (e.g. leaves) */
#define DrawType_TransparentThick 2
/* Blocks behind show (e.g. water). Pixels blend with other blocks behind. */
#define DrawType_Translucent 3
/* Does not show (e.g. air). Can still be collided with. */
#define DrawType_Gas 4
/* Block renders as an X sprite (e.g. sapling). Pixels are either fully visible or invisible. */
#define DrawType_Sprite 5
/* Describes the interaction a block has with a player when they collide with it. */
/* No interaction when player collides. */
#define CollideType_Gas 0
/* 'swimming'/'bobbing' interaction when player collides. */
#define CollideType_Liquid 1
/* Block completely stops the player when they are moving. */
#define CollideType_Solid 2
/* Block is solid and partially slidable on. */
#define CollideType_Ice 3
/* Block is solid and fully slidable on. */
#define CollideType_SlipperyIce 4
/* Water style 'swimming'/'bobbing' interaction when player collides. */
#define CollideType_LiquidWater 5
/* Lava style 'swimming'/'bobbing' interaction when player collides. */
#define CollideType_LiquidLava 6
/* Gets whether the given block is a liquid. (water and lava) */
bool Block_IsLiquid(BlockID b) { return b >= BlockID_Water && b <= BlockID_StillLava; }
/* Gets whether the given block stops sunlight. */
bool BlocksLight[Block_Count];
/* Gets whether the given block should draw all its faces in a full white colour. */
bool FullBright[Block_Count];
/* Gets the name of the given block, or 'Invalid' if the block is not defined. */
String Name[Block_Count];
/* Gets the custom fog colour that should be used when the player is standing within this block.
Note that this is only used for exponential fog mode. */
FastColour FogColour[Block_Count];
/* Gets the fog density for the given block.
A value of 0 means this block does not apply fog.*/
Real32 FogDensity[Block_Count];
/* Gets the basic collision type for the given block. */
UInt8 Collide[Block_Count];
/* Gets the action performed when colliding with the given block. */
UInt8 ExtendedCollide[Block_Count];
/* Speed modifier when colliding (or standing on for solid collide type) with the given block. */
Real32 SpeedMultiplier[Block_Count];
/* Light offset of each block, as bitflags of 1 per face. */
UInt8 LightOffset[Block_Count];
/* Gets the DrawType for the given block. */
UInt8 Draw[Block_Count];
/* Gets whether the given block has an opaque draw type and is also a full tile block.
Full tile block means Min of (0, 0, 0) and max of (1, 1, 1).*/
bool FullOpaque[Block_Count];
UInt32 DefinedCustomBlocks[Block_Count >> 5];
/* Gets the dig sound ID for the given block. */
UInt8 DigSounds[Block_Count];
/* Gets the step sound ID for the given block. */
UInt8 StepSounds[Block_Count];
/* Gets whether the given block has a tinting colour applied to it when rendered.
The tinting colour used is the block's fog colour. */
bool Tinted[Block_Count];
/* Recalculates the initial properties and culling states for all blocks. */
void Block_Reset(Game* game);
/* Calculates the initial properties and culling states for all blocks. */
void Block_Init();
/* Initialises the default blocks the player is allowed to place and delete. */
void Block_SetDefaultPerms(InventoryPermissions place, InventoryPermissions del);
/* Sets collision type of a block. */
void Block_SetCollide(BlockID block, UInt8 collide);
void Block_SetDrawType(BlockID block, UInt8 draw);
/* Resets the properties for the given block to their defaults. */
void Block_ResetProps(BlockID block);
/* Finds the ID of the block whose name caselessly matches the input, -1 otherwise. */
Int32 Block_FindID(String name);
/* Gets the default name of a block. */
static String Block_DefaultName(BlockID block);
static void Block_SplitUppercase(String buffer, Int32 start, Int32 end);
#define Block_Count (MaxDefinedBlock + 1)
#define Block_Invalid Block_MaxDefined
#endif

98
src/Client/BlockID.h Normal file
View File

@ -0,0 +1,98 @@
#ifndef CS_BLOCKID_H
#define CS_BLOCKID_H
/* List of all core/standard block IDs
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
/* Classic blocks */
#define BlockID_Air 0
#define BlockID_Stone 1
#define BlockID_Grass 2
#define BlockID_Dirt 3
#define BlockID_Cobblestone 4
#define BlockID_Wood 5
#define BlockID_Sapling 6
#define BlockID_Bedrock 7
#define BlockID_Water 8
#define BlockID_StillWater 9
#define BlockID_Lava 10
#define BlockID_StillLava 11
#define BlockID_Sand 12
#define BlockID_Gravel 13
#define BlockID_GoldOre 14
#define BlockID_IronOre 15
#define BlockID_CoalOre 16
#define BlockID_Log 17
#define BlockID_Leaves 18
#define BlockID_Sponge 19
#define BlockID_Glass 20
#define BlockID_Red 21
#define BlockID_Orange 22
#define BlockID_Yellow 23
#define BlockID_Lime 24
#define BlockID_Green 25
#define BlockID_Teal 26
#define BlockID_Aqua 27
#define BlockID_Cyan 28
#define BlockID_Blue 29
#define BlockID_Indigo 30
#define BlockID_Violet 31
#define BlockID_Magenta 32
#define BlockID_Pink 33
#define BlockID_Black 34
#define BlockID_Gray 35
#define BlockID_White 36
#define BlockID_Dandelion 37
#define BlockID_Rose 38
#define BlockID_BrownMushroom 39
#define BlockID_RedMushroom 40
#define BlockID_Gold 41
#define BlockID_Iron 42
#define BlockID_DoubleSlab 43
#define BlockID_Slab 44
#define BlockID_Brick 45
#define BlockID_TNT 46
#define BlockID_Bookshelf 47
#define BlockID_MossyRocks 48
#define BlockID_Obsidian 49
/* CPE blocks */
#define BlockID_CobblestoneSlab 50
#define BlockID_Rope 51
#define BlockID_Sandstone 52
#define BlockID_Snow 53
#define BlockID_Fire 54
#define BlockID_LightPink 55
#define BlockID_ForestGreen 56
#define BlockID_Brown 57
#define BlockID_DeepBlue 58
#define BlockID_Turquoise 59
#define BlockID_Ice 60
#define BlockID_CeramicTile 61
#define BlockID_Magma 62
#define BlockID_Pillar 63
#define BlockID_Crate 64
#define BlockID_StoneBrick 65
/* Max block ID used in original classic */
#define Block_MaxOriginal BlockID_Obsidian
/* Number of blocks in original classic. */
#define Block_OriginalCount (Block_MaxOriginal + 1)
/* Max block ID used in original classic plus CPE blocks. */
#define Block_MaxCpe BlockID_StoneBrick
/* Number of blocks in original classic plus CPE blocks. */
#define Block_CpeCount (Block_MaxCpe + 1)
#if USE16_BIT
#define Block_MaxDefined 0xFFF
#else
#define Block_MaxDefined 0xFF
#endif
#define Block_Count (Block_MaxDefined + 1)
#define BlockID_Invalid Block_MaxDefined
#endif

View File

@ -169,6 +169,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="BlockID.h" />
<ClInclude Include="Block.h" />
<ClInclude Include="Compiler.h" />
<ClInclude Include="DefaultSet.h" />
@ -182,6 +183,7 @@
<ClInclude Include="Typedefs.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Block.c" />
<ClCompile Include="Compiler.c" />
<ClCompile Include="DefaultSet.c" />
<ClCompile Include="Noise.c" />

View File

@ -72,15 +72,18 @@
<ClInclude Include="String.h">
<Filter>Header Files\Utils</Filter>
</ClInclude>
<ClInclude Include="Block.h">
<Filter>Header Files\Blocks</Filter>
</ClInclude>
<ClInclude Include="DefaultSet.h">
<Filter>Header Files\Blocks</Filter>
</ClInclude>
<ClInclude Include="FastColour.h">
<Filter>Header Files\2D\Utils</Filter>
</ClInclude>
<ClInclude Include="BlockID.h">
<Filter>Header Files\Blocks</Filter>
</ClInclude>
<ClInclude Include="Block.h">
<Filter>Header Files\Blocks</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="NotchyGenerator.c">
@ -104,5 +107,8 @@
<ClCompile Include="DefaultSet.c">
<Filter>Source Files\Blocks</Filter>
</ClCompile>
<ClCompile Include="Block.c">
<Filter>Source Files\Blocks</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -1,113 +1,114 @@
#include "DefaultSet.h"
#include "BlockID.h"
#include "Block.h"
Real32 DefaultSet_Height(BlockID b) {
if (b == Block_Slab) return 0.5f;
if (b == Block_CobblestoneSlab) return 0.5f;
if (b == Block_Snow) return 0.25f;
if (b == BlockID_Slab) return 0.5f;
if (b == BlockID_CobblestoneSlab) return 0.5f;
if (b == BlockID_Snow) return 0.25f;
return 1.0f;
}
bool DefaultSet_FullBright(BlockID b) {
return b == Block_Lava || b == Block_StillLava
|| b == Block_Magma || b == Block_Fire;
return b == BlockID_Lava || b == BlockID_StillLava
|| b == BlockID_Magma || b == BlockID_Fire;
}
Real32 DefaultSet_FogDensity(BlockID b) {
if (b == Block_Water || b == Block_StillWater)
if (b == BlockID_Water || b == BlockID_StillWater)
return 0.1f;
if (b == Block_Lava || b == Block_StillLava)
if (b == BlockID_Lava || b == BlockID_StillLava)
return 2.0f;
return 0.0f;
}
FastColour DefaultSet_FogColour(BlockID b) {
if (b == Block_Water || b == Block_StillWater)
if (b == BlockID_Water || b == BlockID_StillWater)
return FastColour_Create3(5, 5, 51);
if (b == Block_Lava || b == Block_StillLava)
if (b == BlockID_Lava || b == BlockID_StillLava)
return FastColour_Create3(153, 25, 0);
return FastColour_Create4(0, 0, 0, 0);
}
UInt8 DefaultSet_Collide(BlockID b) {
if (b == Block_Ice) return CollideType_Ice;
if (b == Block_Water || b == Block_StillWater)
if (b == BlockID_Ice) return CollideType_Ice;
if (b == BlockID_Water || b == BlockID_StillWater)
return CollideType_LiquidWater;
if (b == Block_Lava || b == Block_StillLava)
if (b == BlockID_Lava || b == BlockID_StillLava)
return CollideType_LiquidLava;
if (b == Block_Snow || b == Block_Air || DefaultSet_Draw(b) == DrawType_Sprite)
if (b == BlockID_Snow || b == BlockID_Air || DefaultSet_Draw(b) == DrawType_Sprite)
return CollideType_Gas;
return CollideType_Solid;
}
UInt8 DefaultSet_MapOldCollide(BlockID b, UInt8 collide) {
if (b == Block_Ice && collide == CollideType_Solid)
if (b == BlockID_Ice && collide == CollideType_Solid)
return CollideType_Ice;
if ((b == Block_Water || b == Block_StillWater) && collide == CollideType_Liquid)
if ((b == BlockID_Water || b == BlockID_StillWater) && collide == CollideType_Liquid)
return CollideType_LiquidWater;
if ((b == Block_Lava || b == Block_StillLava) && collide == CollideType_Liquid)
if ((b == BlockID_Lava || b == BlockID_StillLava) && collide == CollideType_Liquid)
return CollideType_LiquidLava;
return collide;
}
bool DefaultSet_BlocksLight(BlockID b) {
return !(b == Block_Glass || b == Block_Leaves
|| b == Block_Air || Draw(b) == DrawType_Sprite);
}
return !(b == BlockID_Glass || b == BlockID_Leaves
|| b == BlockID_Air || Draw(b) == DrawType_Sprite);
}
UInt8 DefaultSet_StepSound(BlockID b) {
if (b == Block_Glass) return SoundType_Stone;
if (b == Block_Rope) return SoundType_Cloth;
if (b == BlockID_Glass) return SoundType_Stone;
if (b == BlockID_Rope) return SoundType_Cloth;
if (Draw(b) == DrawType_Sprite) return SoundType_None;
return DigSound(b);
}
UInt8 DefaultSet_Draw(BlockID b) {
if (b == Block_Air || b == Block_Invalid) return DrawType_Gas;
if (b == Block_Leaves) return DrawType_TransparentThick;
if (b == BlockID_Air || b == BlockID_Invalid) return DrawType_Gas;
if (b == BlockID_Leaves) return DrawType_TransparentThick;
if (b == Block_Ice || b == Block_Water || b == Block_StillWater)
if (b == BlockID_Ice || b == BlockID_Water || b == BlockID_StillWater)
return DrawType_Translucent;
if (b == Block_Glass || b == Block_Leaves)
if (b == BlockID_Glass || b == BlockID_Leaves)
return DrawType_Transparent;
if (b >= Block_Dandelion && b <= Block_RedMushroom)
if (b >= BlockID_Dandelion && b <= BlockID_RedMushroom)
return DrawType_Sprite;
if (b == Block_Sapling || b == Block_Rope || b == Block_Fire)
if (b == BlockID_Sapling || b == BlockID_Rope || b == BlockID_Fire)
return DrawType_Sprite;
return DrawType_Opaque;
}
UInt8 DefaultSet_DigSound(BlockID b) {
if (b >= Block_Red && b <= Block_White)
if (b >= BlockID_Red && b <= BlockID_White)
return SoundType_Cloth;
if (b >= Block_LightPink && b <= Block_Turquoise)
if (b >= BlockID_LightPink && b <= BlockID_Turquoise)
return SoundType_Cloth;
if (b == Block_Iron || b == Block_Gold)
if (b == BlockID_Iron || b == BlockID_Gold)
return SoundType_Metal;
if (b == Block_Bookshelf || b == Block_Wood
|| b == Block_Log || b == Block_Crate || b == Block_Fire)
if (b == BlockID_Bookshelf || b == BlockID_Wood
|| b == BlockID_Log || b == BlockID_Crate || b == BlockID_Fire)
return SoundType_Wood;
if (b == Block_Rope) return SoundType_Cloth;
if (b == Block_Sand) return SoundType_Sand;
if (b == Block_Snow) return SoundType_Snow;
if (b == Block_Glass) return SoundType_Glass;
if (b == Block_Dirt || b == Block_Gravel)
if (b == BlockID_Rope) return SoundType_Cloth;
if (b == BlockID_Sand) return SoundType_Sand;
if (b == BlockID_Snow) return SoundType_Snow;
if (b == BlockID_Glass) return SoundType_Glass;
if (b == BlockID_Dirt || b == BlockID_Gravel)
return SoundType_Gravel;
if (b == Block_Grass || b == Block_Sapling || b == Block_TNT
|| b == Block_Leaves || b == Block_Sponge)
if (b == BlockID_Grass || b == BlockID_Sapling || b == BlockID_TNT
|| b == BlockID_Leaves || b == BlockID_Sponge)
return SoundType_Grass;
if (b >= Block_Dandelion && b <= Block_RedMushroom)
if (b >= BlockID_Dandelion && b <= BlockID_RedMushroom)
return SoundType_Grass;
if (b >= Block_Water && b <= Block_StillLava)
if (b >= BlockID_Water && b <= BlockID_StillLava)
return SoundType_None;
if (b >= Block_Stone && b <= Block_StoneBrick)
if (b >= BlockID_Stone && b <= BlockID_StoneBrick)
return SoundType_Stone;
return SoundType_None;
}

View File

@ -1,15 +1,15 @@
#ifndef CS_DEFAULT_BLOCKS_H
#define CS_DEFAULT_BLOCKS_H
#include "Typedefs.h"
#include "FastColour.h"
/* List of properties for core blocks.
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
/* Default height of a block. */
Real32 DefaultSet_Height(BlockID b);
/* Gets whether the given block is fully bright. */
#ifndef CS_DEFAULT_BLOCKS_H
#define CS_DEFAULT_BLOCKS_H
#include "Typedefs.h"
#include "FastColour.h"
/* List of properties for core blocks.
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
/* Default height of a block. */
Real32 DefaultSet_Height(BlockID b);
/* Gets whether the given block is fully bright. */
bool DefaultSet_FullBright(BlockID b);
/* Gets default fog density of a block. 0 means no fog density. */
@ -34,5 +34,5 @@ UInt8 DefaultSet_StepSound(BlockID b);
UInt8 DefaultSet_Draw(BlockID b);
/* Gets the ID of the sound played when deleting/placing this block. */
UInt8 DefaultSet_DigSound(BlockID b);
UInt8 DefaultSet_DigSound(BlockID b);
#endif

View File

@ -1,13 +1,13 @@
#include "String.h"
void String_Empty(String* str, UInt8* buffer, Int16 capacity) {
void String_Empty(String* str, UInt8* buffer, UInt16 capacity) {
str->buffer = buffer;
str->capacity = capacity;
str->length = 0;
}
void String_Constant(String* str, const UInt8* buffer) {
Int16 length = 0;
UInt16 length = 0;
UInt8 cur = 0;
while ((cur = *buffer) != 0) {
@ -28,6 +28,21 @@ bool String_Equals(String* a, String* b) {
return true;
}
bool String_CaselessEquals(String* a, String* b) {
if (a->length != b->length) return false;
for (Int32 i = 0; i < a->length; i++) {
UInt8 aCur = a->buffer[i];
UInt8 bCur = b->buffer[i];
if (aCur >= 'a' && aCur <= 'z') aCur -= 32;
if (bCur >= 'a' && bCur <= 'z') bCur -= 32;
if (aCur != bCur) return false;
}
return true;
}
int String_IndexOf(String* str, UInt8 c) {
for (Int32 i = 0; i < str->length; i++) {
if (str->buffer[i] == c) return i;

View File

@ -5,19 +5,21 @@
Copyright 2017 ClassicalSharp | Licensed under BSD-3
*/
#define String_BufferSize(n) (n + 1)
typedef struct String {
/* Pointer to raw characters. Size is capacity + 1, as buffer is null terminated. */
UInt8* buffer;
/* Number of characters used. */
Int16 length;
UInt16 length;
/* Max number of characters that can be in buffer. */
Int16 capacity;
UInt16 capacity;
} String;
/* Constructs a new string, filled with NULL characters. */
void String_Empty(String* str, UInt8* buffer, Int16 capacity);
void String_Empty(String* str, UInt8* buffer, UInt16 capacity);
/* Constructs a new string from a constant readonly string. */
void String_Constant(String* str, const UInt8* buffer);
@ -25,6 +27,9 @@ void String_Constant(String* str, const UInt8* buffer);
/* Returns whether two strings have same contents. */
bool String_Equals(String* a, String* b);
/* Returns whether two strings have same case-insensitive contents. */
bool String_CaselessEquals(String* a, String* b);
/* Finds the first index of c in given string, -1 if not found. */
int String_IndexOf(String* str, UInt8 c);