From: <geo...@us...> - 2009-09-12 22:10:45
|
Revision: 3162 http://freeorion.svn.sourceforge.net/freeorion/revision/?rev=3162&view=rev Author: geoffthemedio Date: 2009-09-12 22:10:32 +0000 (Sat, 12 Sep 2009) Log Message: ----------- Added C++ code for conditions to match ships with the indicated hull or number of indicated part. The parser doesn't know about these conditions, however, and there are no stringtable entries for them yet. Modified Paths: -------------- trunk/FreeOrion/universe/Condition.cpp trunk/FreeOrion/universe/Condition.h Modified: trunk/FreeOrion/universe/Condition.cpp =================================================================== --- trunk/FreeOrion/universe/Condition.cpp 2009-09-12 20:36:50 UTC (rev 3161) +++ trunk/FreeOrion/universe/Condition.cpp 2009-09-12 22:10:32 UTC (rev 3162) @@ -1084,6 +1084,84 @@ } /////////////////////////////////////////////////////////// +// DesignHasHull // +/////////////////////////////////////////////////////////// +Condition::DesignHasHull::DesignHasHull(const std::string& name) : + m_name(name) +{} + +std::string Condition::DesignHasHull::Description(bool negated/* = false*/) const +{ + std::string description_str = "DESC_DESIGN_HAS_HULL"; + if (negated) + description_str += "_NOT"; + return str(format(UserString(description_str)) % UserString(m_name)); +} + +std::string Condition::DesignHasHull::Dump() const +{ + return DumpIndent() + "DesignHasHull name = \"" + m_name + "\"\n"; +} + +bool Condition::DesignHasHull::Match(const UniverseObject* source, const UniverseObject* target) const +{ + if (const Ship* ship = universe_object_cast<const Ship*>(target)) + if (const ShipDesign* design = ship->Design()) + return (design->Hull() == m_name); + return false; +} + +/////////////////////////////////////////////////////////// +// DesignHasPart // +/////////////////////////////////////////////////////////// +Condition::DesignHasPart::DesignHasPart(const ValueRef::ValueRefBase<int>* low, const ValueRef::ValueRefBase<int>* high, const std::string& name) : + m_low(low), + m_high(high), + m_name(name) +{ +} + +Condition::DesignHasPart::~DesignHasPart() +{ + delete m_low; + delete m_high; +} + +std::string Condition::DesignHasPart::Description(bool negated/* = false*/) const +{ + std::string low_str = ValueRef::ConstantExpr(m_low) ? lexical_cast<std::string>(m_low->Eval(0, 0)) : m_low->Description(); + std::string high_str = ValueRef::ConstantExpr(m_high) ? lexical_cast<std::string>(m_high->Eval(0, 0)) : m_high->Description(); + std::string description_str = "DESC_DESIGN_HAS_PART"; + if (negated) + description_str += "_NOT"; + return str(format(UserString(description_str)) + % low_str + % high_str + % m_name); +} + +std::string Condition::DesignHasPart::Dump() const{ + return DumpIndent() + "DesignHasPart low = " + m_low->Dump() + "Number high = " + m_high->Dump() + " name = " + m_name; +} + +bool Condition::DesignHasPart::Match(const UniverseObject* source, const UniverseObject* target) const +{ + if (const Ship* ship = universe_object_cast<const Ship*>(target)) { + if (const ShipDesign* design = ship->Design()) { + const std::vector<std::string>& parts = design->Parts(); + int count = 0; + for (std::vector<std::string>::const_iterator it = parts.begin(); it != parts.end(); ++it) + if (*it == m_name) + ++count; + int low = m_low->Eval(source, target); // number matched can depend on some property of target object! + int high = m_high->Eval(source, target); + return (low <= count && count < high); + } + } + return false; +} + +/////////////////////////////////////////////////////////// // Chance // /////////////////////////////////////////////////////////// Condition::Chance::Chance(const ValueRef::ValueRefBase<double>* chance) : Modified: trunk/FreeOrion/universe/Condition.h =================================================================== --- trunk/FreeOrion/universe/Condition.h 2009-09-12 20:36:50 UTC (rev 3161) +++ trunk/FreeOrion/universe/Condition.h 2009-09-12 22:10:32 UTC (rev 3162) @@ -34,6 +34,8 @@ struct PlanetEnvironment; struct FocusType; struct StarType; + struct DesignHasHull; + struct DesignHasPart; struct Chance; struct MeterValue; struct EmpireStockpileValue; @@ -68,8 +70,9 @@ void serialize(Archive& ar, const unsigned int version); }; -/** Matches all objects if the number of objects that match Condition \a condition is is >= \a low and < \a high. - Matched objects may or may not themselves match the condition. */ +/** Matches all objects if the number of objects that match Condition + * \a condition is is >= \a low and < \a high. Matched objects may + * or may not themselves match the condition. */ struct Condition::Number : Condition::ConditionBase { Number(const ValueRef::ValueRefBase<int>* low, const ValueRef::ValueRefBase<int>* high, const ConditionBase* condition); @@ -138,8 +141,9 @@ void serialize(Archive& ar, const unsigned int version); }; -/** Matches all objects that are owned (if \a exclusive == false) or only owned (if \a exclusive == true) by an empire that has - affilitation type \a affilitation with Empire \a empire_id. */ +/** Matches all objects that are owned (if \a exclusive == false) or only owned + * (if \a exclusive == true) by an empire that has affilitation type + * \a affilitation with Empire \a empire_id. */ struct Condition::EmpireAffiliation : Condition::ConditionBase { EmpireAffiliation(const ValueRef::ValueRefBase<int>* empire_id, EmpireAffiliationType affiliation, bool exclusive); @@ -235,8 +239,9 @@ void serialize(Archive& ar, const unsigned int version); }; -/** Matches all objects that have an attached Special of the sort specified by \a name. Passing "All" for - \a name will match all objects with attached Specials. */ +/** Matches all objects that have an attached Special of the sort specified by + * \a name. Passing "All" for \a name will match all objects with attached + * Specials. */ struct Condition::HasSpecial : Condition::ConditionBase { HasSpecial(const std::string& name); @@ -252,8 +257,9 @@ void serialize(Archive& ar, const unsigned int version); }; -/** Matches all objects that contain an object that matches Condition \a condition. Container objects are Systems, - Planets (which contain Buildings), and Fleets (which contain Ships). */ +/** Matches all objects that contain an object that matches Condition + * \a condition. Container objects are Systems, Planets (which contain + * Buildings), and Fleets (which contain Ships). */ struct Condition::Contains : Condition::ConditionBase { Contains(const ConditionBase* condition); @@ -269,8 +275,9 @@ void serialize(Archive& ar, const unsigned int version); }; -/** Matches all objects that are contained by an object that matches Condition \a condition. Container objects - are Systems, Planets (which contain Buildings), and Fleets (which contain Ships). */ +/** Matches all objects that are contained by an object that matches Condition + * \a condition. Container objects are Systems, Planets (which contain + * Buildings), and Fleets (which contain Ships). */ struct Condition::ContainedBy : Condition::ConditionBase { ContainedBy(const ConditionBase* condition); @@ -286,8 +293,9 @@ void serialize(Archive& ar, const unsigned int version); }; -/** Matches all Planet objects that have one of the PlanetTypes in \a types. Note that all - Building objects which are on matching planets are also matched. */ +/** Matches all Planet objects that have one of the PlanetTypes in \a types. + * Note that all Building objects which are on matching planets are also + * matched. */ struct Condition::PlanetType : Condition::ConditionBase { PlanetType(const std::vector<const ValueRef::ValueRefBase< ::PlanetType>*>& types); @@ -304,8 +312,9 @@ void serialize(Archive& ar, const unsigned int version); }; -/** Matches all Planet objects that have one of the PlanetSizes in \a sizes. Note that all - Building objects which are on matching planets are also matched. */ +/** Matches all Planet objects that have one of the PlanetSizes in \a sizes. + * Note that all Building objects which are on matching planets are also + * matched. */ struct Condition::PlanetSize : Condition::ConditionBase { PlanetSize(const std::vector<const ValueRef::ValueRefBase< ::PlanetSize>*>& sizes); @@ -376,6 +385,42 @@ void serialize(Archive& ar, const unsigned int version); }; +/** Matches all ships whose ShipDesign has the hull specified by \a name. */ +struct Condition::DesignHasHull : Condition::ConditionBase +{ + DesignHasHull(const std::string& name); + virtual std::string Description(bool negated = false) const; + virtual std::string Dump() const; + +private: + virtual bool Match(const UniverseObject* source, const UniverseObject* target) const; + std::string m_name; + + friend class boost::serialization::access; + template <class Archive> + void serialize(Archive& ar, const unsigned int version); +}; + +/** Matches all ships whose ShipDesign has >= \a low and < \a high of the ship + * part specified by \a name. */ +struct Condition::DesignHasPart : Condition::ConditionBase +{ + DesignHasPart(const ValueRef::ValueRefBase<int>* low, const ValueRef::ValueRefBase<int>* high, const std::string& name); + virtual ~DesignHasPart(); + virtual std::string Description(bool negated = false) const; + virtual std::string Dump() const; + +private: + virtual bool Match(const UniverseObject* source, const UniverseObject* target) const; + std::string m_name; + const ValueRef::ValueRefBase<int>* m_low; + const ValueRef::ValueRefBase<int>* m_high; + + friend class boost::serialization::access; + template <class Archive> + void serialize(Archive& ar, const unsigned int version); +}; + /** Matches a given object with a linearly distributed probability of \a chance. */ struct Condition::Chance : Condition::ConditionBase { |