1
0

Merge pull request #2221 from mc-server/AreaCountBlocks

cBlockArea: Added CountSpecificBlocks() API function.
This commit is contained in:
Julian Laubstein 2015-06-08 23:05:49 +02:00
commit 87a8df7cde
3 changed files with 73 additions and 0 deletions

View File

@ -100,6 +100,11 @@ g_APIDesc =
CopyFrom = { Params = "BlockAreaSrc", Return = "", Notes = "Copies contents from BlockAreaSrc into self" }, CopyFrom = { Params = "BlockAreaSrc", Return = "", Notes = "Copies contents from BlockAreaSrc into self" },
CopyTo = { Params = "BlockAreaDst", Return = "", Notes = "Copies contents from self into BlockAreaDst." }, CopyTo = { Params = "BlockAreaDst", Return = "", Notes = "Copies contents from self into BlockAreaDst." },
CountNonAirBlocks = { Params = "", Return = "number", Notes = "Returns the count of blocks that are not air. Returns 0 if blocktypes not available. Block metas are ignored (if present, air with any meta is still considered air)." }, CountNonAirBlocks = { Params = "", Return = "number", Notes = "Returns the count of blocks that are not air. Returns 0 if blocktypes not available. Block metas are ignored (if present, air with any meta is still considered air)." },
CountSpecificBlocks =
{
{ Params = "BlockType", Return = "number", Notes = "Counts the number of occurences of the specified blocktype contained in the area." },
{ Params = "BlockType, BlockMeta", Return = "number", Notes = "Counts the number of occurrences of the specified blocktype + blockmeta combination contained in the area." },
},
Create = { Params = "SizeX, SizeY, SizeZ, [DataTypes]", Return = "", Notes = "Initializes this BlockArea to an empty area of the specified size and origin of {0, 0, 0}. Any previous contents are lost." }, Create = { Params = "SizeX, SizeY, SizeZ, [DataTypes]", Return = "", Notes = "Initializes this BlockArea to an empty area of the specified size and origin of {0, 0, 0}. Any previous contents are lost." },
Crop = { Params = "AddMinX, SubMaxX, AddMinY, SubMaxY, AddMinZ, SubMaxZ", Return = "", Notes = "Crops the specified number of blocks from each border. Modifies the size of this blockarea object." }, Crop = { Params = "AddMinX, SubMaxX, AddMinY, SubMaxY, AddMinZ, SubMaxZ", Return = "", Notes = "Crops the specified number of blocks from each border. Modifies the size of this blockarea object." },
DumpToRawFile = { Params = "FileName", Return = "", Notes = "Dumps the raw data into a file. For debugging purposes only." }, DumpToRawFile = { Params = "FileName", Return = "", Notes = "Dumps the raw data into a file. For debugging purposes only." },

View File

@ -1665,6 +1665,65 @@ size_t cBlockArea::CountNonAirBlocks(void) const
size_t cBlockArea::CountSpecificBlocks(BLOCKTYPE a_BlockType) const
{
// If blocktypes are not valid, log a warning and return zero occurences:
if (m_BlockTypes == nullptr)
{
LOGWARNING("%s: BlockTypes not available!", __FUNCTION__);
return 0;
}
// Count the blocks:
size_t num = GetBlockCount();
size_t res = 0;
for (size_t i = 0; i < num; i++)
{
if (m_BlockTypes[i] == a_BlockType)
{
res++;
}
}
return res;
}
size_t cBlockArea::CountSpecificBlocks(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) const
{
// If blocktypes are not valid, log a warning and return zero occurences:
if (m_BlockTypes == nullptr)
{
LOGWARNING("%s: BlockTypes not available!", __FUNCTION__);
return 0;
}
// If blockmetas are not valid, log a warning and count only blocktypes:
if (m_BlockMetas == nullptr)
{
LOGWARNING("%s: BlockMetas not available, comparing blocktypes only!", __FUNCTION__);
return CountSpecificBlocks(a_BlockType);
}
// Count the blocks:
size_t num = GetBlockCount();
size_t res = 0;
for (size_t i = 0; i < num; i++)
{
if ((m_BlockTypes[i] == a_BlockType) && (m_BlockMetas[i] == a_BlockMeta))
{
res++;
}
}
return res;
}
void cBlockArea::GetNonAirCropRelCoords(int & a_MinRelX, int & a_MinRelY, int & a_MinRelZ, int & a_MaxRelX, int & a_MaxRelY, int & a_MaxRelZ, BLOCKTYPE a_IgnoreBlockType) void cBlockArea::GetNonAirCropRelCoords(int & a_MinRelX, int & a_MinRelY, int & a_MinRelZ, int & a_MaxRelX, int & a_MaxRelY, int & a_MaxRelZ, BLOCKTYPE a_IgnoreBlockType)
{ {
// Check if blocktypes are valid: // Check if blocktypes are valid:

View File

@ -308,6 +308,15 @@ public:
Returns 0 if blocktypes not available. Block metas are ignored (if present, air with any meta is still considered air). */ Returns 0 if blocktypes not available. Block metas are ignored (if present, air with any meta is still considered air). */
size_t CountNonAirBlocks(void) const; size_t CountNonAirBlocks(void) const;
/** Returns how many times the specified block is contained in the area.
The blocks' meta values are ignored, only the blocktype is compared. */
size_t CountSpecificBlocks(BLOCKTYPE a_BlockType) const;
/** Returns how many times the specified block is contained in the area.
Both the block's type and meta must match in order to be counted in.
If the block metas aren't present in the area, logs a warning and ignores the meta specification. */
size_t CountSpecificBlocks(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) const;
// tolua_end // tolua_end
/** Returns the minimum and maximum coords in each direction for the first non-ignored block in each direction. /** Returns the minimum and maximum coords in each direction for the first non-ignored block in each direction.