Add a build restriction based on the count of types of units a player has (i.e. you can only build this building if you have 0 of this other building)

This commit is contained in:
Tim Felgentreff 2015-08-31 09:14:36 +02:00
parent 255195c499
commit b8a8a453d9
3 changed files with 69 additions and 0 deletions

View file

@ -425,6 +425,20 @@ public:
CUnitType *RestrictType;
};
class CBuildRestrictionHasUnit : public CBuildRestriction
{
public:
CBuildRestrictionHasUnit() : Count(0), RestrictType(NULL) {};
virtual ~CBuildRestrictionHasUnit() {};
virtual void Init() { this->RestrictType = UnitTypeByIdent(this->RestrictTypeName); };
virtual bool Check(const CUnit *builder, const CUnitType &type, const Vec2i &pos, CUnit *&ontoptarget) const;
int Count;
DistanceTypeType CountType;
std::string RestrictTypeName;
CUnitType *RestrictType;
};
/// Base structure of unit-type
/// @todo n0body: AutoBuildRate not implemented.
class CUnitType

View file

@ -175,6 +175,26 @@ bool CBuildRestrictionDistance::Check(const CUnit *builder, const CUnitType &typ
this->DistanceType == NotEqual);
}
/**
** Check HasUnit Restriction
*/
bool CBuildRestrictionHasUnit::Check(const CUnit *builder, const CUnitType &type, const Vec2i &pos, CUnit *&) const
{
Vec2i pos1(0, 0);
Vec2i pos2(0, 0);
int count = ThisPlayer->GetUnitTotalCount(*this->RestrictType);
switch (this->CountType)
{
case LessThan: return count < this->Count;
case LessThanEqual: return count <= this->Count;
case Equal: return count == this->Count;
case NotEqual: return count != this->Count;
case GreaterThanEqual: return count >= this->Count;
case GreaterThan: return count > this->Count;
default: return false;
}
}
inline bool CBuildRestrictionAddOn::functor::operator()(const CUnit *const unit) const
{
return (unit->Type == Parent && unit->tilePos == this->pos);

View file

@ -303,6 +303,41 @@ static void ParseBuildingRules(lua_State *l, std::vector<CBuildRestriction *> &b
}
}
andlist->push_back(b);
} else if (!strcmp(value, "has-unit")) {
CBuildRestrictionHasUnit *b = new CBuildRestrictionHasUnit;
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
value = LuaToString(l, -2);
if (!strcmp(value, "Type")) {
b->RestrictTypeName = LuaToString(l, -1);
} else if (!strcmp(value, "Count")) {
b->Count = LuaToNumber(l, -1);
} else if (!strcmp(value, "CountType")) {
value = LuaToString(l, -1);
if (value[0] == '=') {
if ((value[1] == '=' && value[2] == '\0') || (value[1] == '\0')) {
b->CountType = Equal;
}
} else if (value[0] == '>') {
if (value[1] == '=' && value[2] == '\0') {
b->CountType = GreaterThanEqual;
} else if (value[1] == '\0') {
b->CountType = GreaterThan;
}
} else if (value[0] == '<') {
if (value[1] == '=' && value[2] == '\0') {
b->CountType = LessThanEqual;
} else if (value[1] == '\0') {
b->CountType = LessThan;
}
} else if (value[0] == '!' && value[1] == '=' && value[2] == '\0') {
b->CountType = NotEqual;
}
} else {
LuaError(l, "Unsupported BuildingRules has-unit tag: %s" _C_ value);
}
}
andlist->push_back(b);
} else {
LuaError(l, "Unsupported BuildingRules tag: %s" _C_ value);
}