| 1 |
#include "SpecifierType.h" |
| 2 |
#include "util/ObjectivesException.h" |
| 3 |
|
| 4 |
#include "string/convert.h" |
| 5 |
#include "i18n.h" |
| 6 |
|
| 7 |
namespace objectives |
| 8 |
{ |
| 9 |
|
| 10 |
// Static enum count |
| 11 |
int SpecifierType::enumCount = 0; |
| 12 |
|
| 13 |
// SpecifierType types |
| 14 |
|
| 15 |
const SpecifierType& SpecifierType::SPEC_NONE() { |
| 16 |
static SpecifierType _instance("none", _("No specifier")); |
| 17 |
return _instance; |
| 18 |
} |
| 19 |
const SpecifierType& SpecifierType::SPEC_NAME() { |
| 20 |
static SpecifierType _instance("name", _("Name of single entity")); |
| 21 |
return _instance; |
| 22 |
} |
| 23 |
const SpecifierType& SpecifierType::SPEC_OVERALL() { |
| 24 |
static SpecifierType _instance("overall", _("Overall (component-specific)")); |
| 25 |
return _instance; |
| 26 |
} |
| 27 |
const SpecifierType& SpecifierType::SPEC_GROUP() { |
| 28 |
static SpecifierType _instance( |
| 29 |
"group", _("Group identifier (component-specific)") |
| 30 |
); |
| 31 |
return _instance; |
| 32 |
} |
| 33 |
const SpecifierType& SpecifierType::SPEC_CLASSNAME() { |
| 34 |
static SpecifierType _instance("classname", _("Any entity of specified class")); |
| 35 |
return _instance; |
| 36 |
} |
| 37 |
const SpecifierType& SpecifierType::SPEC_SPAWNCLASS() { |
| 38 |
static SpecifierType _instance( |
| 39 |
"spawnclass", _("Any entity with SDK-level spawnclass") |
| 40 |
); |
| 41 |
return _instance; |
| 42 |
} |
| 43 |
const SpecifierType& SpecifierType::SPEC_AI_TYPE() { |
| 44 |
static SpecifierType _instance("ai_type", _("Any AI of specified type")); |
| 45 |
return _instance; |
| 46 |
} |
| 47 |
const SpecifierType& SpecifierType::SPEC_AI_TEAM() { |
| 48 |
static SpecifierType _instance("ai_team", _("Any AI on specified team")); |
| 49 |
return _instance; |
| 50 |
} |
| 51 |
const SpecifierType& SpecifierType::SPEC_AI_INNOCENCE() { |
| 52 |
static SpecifierType _instance( |
| 53 |
"ai_innocence", _("Any AI with specified combat status") |
| 54 |
); |
| 55 |
return _instance; |
| 56 |
} |
| 57 |
|
| 58 |
// SpecifierType sets |
| 59 |
|
| 60 |
const SpecifierTypeSet& SpecifierType::SET_ALL() { |
| 61 |
static SpecifierTypeSet _instance; |
| 62 |
if (_instance.empty()) { |
| 63 |
_instance.insert(SPEC_NONE()); |
| 64 |
_instance.insert(SPEC_NAME()); |
| 65 |
_instance.insert(SPEC_OVERALL()); |
| 66 |
_instance.insert(SPEC_GROUP()); |
| 67 |
_instance.insert(SPEC_CLASSNAME()); |
| 68 |
_instance.insert(SPEC_SPAWNCLASS()); |
| 69 |
_instance.insert(SPEC_AI_TYPE()); |
| 70 |
_instance.insert(SPEC_AI_TEAM()); |
| 71 |
_instance.insert(SPEC_AI_INNOCENCE()); |
| 72 |
} |
| 73 |
return _instance; |
| 74 |
} |
| 75 |
|
| 76 |
const SpecifierTypeSet& SpecifierType::SET_ITEM() { |
| 77 |
static SpecifierTypeSet _instance; |
| 78 |
if (_instance.empty()) { |
| 79 |
_instance.insert(SPEC_NONE()); |
| 80 |
_instance.insert(SPEC_NAME()); |
| 81 |
_instance.insert(SPEC_OVERALL()); |
| 82 |
_instance.insert(SPEC_GROUP()); |
| 83 |
_instance.insert(SPEC_CLASSNAME()); |
| 84 |
_instance.insert(SPEC_SPAWNCLASS()); |
| 85 |
} |
| 86 |
return _instance; |
| 87 |
} |
| 88 |
|
| 89 |
const SpecifierTypeSet& SpecifierType::SET_READABLE() |
| 90 |
{ |
| 91 |
static SpecifierTypeSet _instance; |
| 92 |
|
| 93 |
if (_instance.empty()) |
| 94 |
{ |
| 95 |
_instance.insert(SPEC_NONE()); |
| 96 |
_instance.insert(SPEC_NAME()); |
| 97 |
} |
| 98 |
|
| 99 |
return _instance; |
| 100 |
} |
| 101 |
|
| 102 |
const SpecifierTypeSet& SpecifierType::SET_LOCATION() { |
| 103 |
static SpecifierTypeSet _instance; |
| 104 |
|
| 105 |
if (_instance.empty()) { |
| 106 |
_instance.insert(SPEC_NONE()); |
| 107 |
_instance.insert(SPEC_NAME()); |
| 108 |
_instance.insert(SPEC_GROUP()); |
| 109 |
} |
| 110 |
|
| 111 |
return _instance; |
| 112 |
} |
| 113 |
|
| 114 |
const SpecifierTypeSet& SpecifierType::SET_STANDARD_AI() { |
| 115 |
static SpecifierTypeSet _instance; |
| 116 |
if (_instance.empty()) { |
| 117 |
_instance.insert(SPEC_NONE()); |
| 118 |
_instance.insert(SPEC_NAME()); |
| 119 |
_instance.insert(SPEC_OVERALL()); |
| 120 |
_instance.insert(SPEC_CLASSNAME()); |
| 121 |
_instance.insert(SPEC_SPAWNCLASS()); |
| 122 |
_instance.insert(SPEC_AI_TYPE()); |
| 123 |
_instance.insert(SPEC_AI_TEAM()); |
| 124 |
_instance.insert(SPEC_AI_INNOCENCE()); |
| 125 |
} |
| 126 |
return _instance; |
| 127 |
} |
| 128 |
|
| 129 |
// Map instance owner |
| 130 |
SpecifierType::SpecifierTypeMap& SpecifierType::getMap() |
| 131 |
{ |
| 132 |
static SpecifierTypeMap _instance; |
| 133 |
|
| 134 |
if (_instance.empty()) |
| 135 |
{ |
| 136 |
// greebo: Make sure all default specifiers are in the map |
| 137 |
_instance.insert(SpecifierTypeMap::value_type(SPEC_NONE().getName(), SPEC_NONE())); |
| 138 |
_instance.insert(SpecifierTypeMap::value_type(SPEC_NAME().getName(), SPEC_NAME())); |
| 139 |
_instance.insert(SpecifierTypeMap::value_type(SPEC_OVERALL().getName(), SPEC_OVERALL())); |
| 140 |
_instance.insert(SpecifierTypeMap::value_type(SPEC_GROUP().getName(), SPEC_GROUP())); |
| 141 |
_instance.insert(SpecifierTypeMap::value_type(SPEC_CLASSNAME().getName(), SPEC_CLASSNAME())); |
| 142 |
_instance.insert(SpecifierTypeMap::value_type(SPEC_SPAWNCLASS().getName(), SPEC_SPAWNCLASS())); |
| 143 |
_instance.insert(SpecifierTypeMap::value_type(SPEC_AI_TYPE().getName(), SPEC_AI_TYPE())); |
| 144 |
_instance.insert(SpecifierTypeMap::value_type(SPEC_AI_TEAM().getName(), SPEC_AI_TEAM())); |
| 145 |
_instance.insert(SpecifierTypeMap::value_type(SPEC_AI_INNOCENCE().getName(), SPEC_AI_INNOCENCE())); |
| 146 |
} |
| 147 |
|
| 148 |
return _instance; |
| 149 |
} |
| 150 |
|
| 151 |
// Map lookup function |
| 152 |
const SpecifierType& SpecifierType::getSpecifierType(const std::string& name) |
| 153 |
{ |
| 154 |
std::string specName = name.empty() ? "none" : name; |
| 155 |
|
| 156 |
SpecifierTypeMap::const_iterator i = getMap().find(specName); |
| 157 |
|
| 158 |
if (i != getMap().end()) |
| 159 |
{ |
| 160 |
return i->second; |
| 161 |
} |
| 162 |
else |
| 163 |
{ |
| 164 |
throw ObjectivesException("SpecifierType " + name + " not found."); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
const SpecifierType& SpecifierType::getSpecifierType(int id) |
| 169 |
{ |
| 170 |
SpecifierTypeMap::const_iterator i = std::find_if(getMap().begin(), getMap().end(), |
| 171 |
[=] (const SpecifierTypeMap::value_type& pair)->bool |
| 172 |
{ |
| 173 |
return pair.second.getId() == id; |
| 174 |
}); |
| 175 |
|
| 176 |
if (i != getMap().end()) |
| 177 |
{ |
| 178 |
return i->second; |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
throw ObjectivesException("SpecifierType ID " + string::to_string(id) + " not found."); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
// Construct a named SpecifierType object, incrementing the count |
| 187 |
SpecifierType::SpecifierType(const std::string& name, const std::string& displayName) |
| 188 |
: _id(enumCount++), |
| 189 |
_name(name), |
| 190 |
_displayName(displayName) |
| 191 |
{ } |
| 192 |
|
| 193 |
} |