forked from Mirrors/openclonk
New script function: GetPXSCount.
Counts number of loose pixels of a material in an area.issue1247
parent
3971d60614
commit
612a05fe40
|
@ -2491,6 +2491,25 @@ static bool FnGainScenarioAchievement(C4PropList * _this, C4String *achievement_
|
|||
return true;
|
||||
}
|
||||
|
||||
static long FnGetPXSCount(C4PropList * _this, Nillable<long> iMaterial, Nillable<long> iX0, Nillable<long> iY0, Nillable<long> iWdt, Nillable<long> iHgt)
|
||||
{
|
||||
if (iX0.IsNil())
|
||||
{
|
||||
// Search everywhere
|
||||
// All materials everywhere
|
||||
if (iMaterial.IsNil() || iMaterial == MNone) return ::PXS.GetCount();
|
||||
// Specific material everywhere
|
||||
return ::PXS.GetCount(iMaterial);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Material in area; offset by caller
|
||||
int32_t x = iX0, y = iY0;
|
||||
if (Object(_this)) { x += Object(_this)->GetX(); y += Object(_this)->GetY(); }
|
||||
return ::PXS.GetCount(iMaterial.IsNil() ? MNone : iMaterial, x, y, iWdt, iHgt);
|
||||
}
|
||||
}
|
||||
|
||||
extern C4ScriptConstDef C4ScriptGameConstMap[];
|
||||
extern C4ScriptFnDef C4ScriptGameFnMap[];
|
||||
|
||||
|
@ -2658,6 +2677,7 @@ void InitGameFunctionMap(C4AulScriptEngine *pEngine)
|
|||
AddFunc(pEngine, "PlayerObjectCommand", FnPlayerObjectCommand);
|
||||
AddFunc(pEngine, "EditCursor", FnEditCursor);
|
||||
AddFunc(pEngine, "GainScenarioAchievement", FnGainScenarioAchievement);
|
||||
AddFunc(pEngine, "GetPXSCount", FnGetPXSCount);
|
||||
|
||||
F(GetPlrKnowledge);
|
||||
F(GetComponent);
|
||||
|
|
|
@ -501,4 +501,31 @@ void C4PXSSystem::Delete(C4PXS *pPXS)
|
|||
iChunkPXS[cnt]--;
|
||||
}
|
||||
|
||||
int32_t C4PXSSystem::GetCount(int32_t mat) const
|
||||
{
|
||||
// count PXS of given material
|
||||
int32_t result = 0;
|
||||
for (size_t cnt = 0; cnt < PXSMaxChunk; cnt++) if (Chunk[cnt] && iChunkPXS[cnt])
|
||||
{
|
||||
C4PXS *pxp = Chunk[cnt];
|
||||
for (size_t cnt2 = 0; cnt2 < PXSChunkSize; cnt2++, pxp++) if (pxp->Mat == mat) ++result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int32_t C4PXSSystem::GetCount(int32_t mat, int32_t x, int32_t y, int32_t wdt, int32_t hgt) const
|
||||
{
|
||||
// count PXS of given material in given area
|
||||
int32_t result = 0;
|
||||
for (size_t cnt = 0; cnt < PXSMaxChunk; cnt++) if (Chunk[cnt] && iChunkPXS[cnt])
|
||||
{
|
||||
C4PXS *pxp = Chunk[cnt];
|
||||
for (size_t cnt2 = 0; cnt2 < PXSChunkSize; cnt2++, pxp++)
|
||||
if (pxp->Mat != MNone)
|
||||
if (pxp->Mat == mat || mat == MNone)
|
||||
if (Inside(pxp->x, x, x + wdt - 1) && Inside(pxp->y, y, y + hgt - 1)) ++result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
C4PXSSystem PXS;
|
||||
|
|
|
@ -58,6 +58,9 @@ public:
|
|||
bool Create(int32_t mat, C4Real ix, C4Real iy, C4Real ixdir=Fix0, C4Real iydir=Fix0);
|
||||
bool Load(C4Group &hGroup);
|
||||
bool Save(C4Group &hGroup);
|
||||
int32_t GetCount() const { return Count; } // count all PXS
|
||||
int32_t GetCount(int32_t mat) const; // count PXS of given material
|
||||
int32_t GetCount(int32_t mat, int32_t x, int32_t y, int32_t wdt, int32_t hgt) const; // count PXS of given material in given area. mat==-1 for all materials.
|
||||
protected:
|
||||
C4PXS *New();
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue