Debian Codegraph @

plugins/dm.objectives/ComponentType.cpp

raw
1 #include "ComponentType.h"
2 #include "util/ObjectivesException.h"
3 #include "i18n.h"
4 #include <algorithm>
5 #include "string/convert.h"
6
7 namespace objectives
8 {
9
10 // Enum count
11 int ComponentType::enumCount = 0;
12
13 // Static map
14 ComponentType::ComponentTypeMap& ComponentType::getMap() {
15 static ComponentTypeMap _instance;
16 return _instance;
17 }
18
19 // Static instances
20
21 const ComponentType& ComponentType::COMP_KILL() {
22 static ComponentType _instance("kill", _("AI is killed"));
23 return _instance;
24 }
25
26 const ComponentType& ComponentType::COMP_KO() {
27 static ComponentType _instance("ko", _("AI is knocked out"));
28 return _instance;
29 }
30
31 const ComponentType& ComponentType::COMP_AI_FIND_ITEM() {
32 static ComponentType _instance("ai_find_item", _("AI finds an item"));
33 return _instance;
34 }
35
36 const ComponentType& ComponentType::COMP_AI_FIND_BODY() {
37 static ComponentType _instance("ai_find_body", _("AI finds a body"));
38 return _instance;
39 }
40
41 const ComponentType& ComponentType::COMP_ALERT() {
42 static ComponentType _instance("alert", _("AI is alerted")); // sic
43 return _instance;
44 }
45
46 const ComponentType& ComponentType::COMP_DESTROY() {
47 static ComponentType _instance("destroy", _("Object is destroyed"));
48 return _instance;
49 }
50
51 const ComponentType& ComponentType::COMP_ITEM() {
52 static ComponentType _instance("item", _("Player possesses item"));
53 return _instance;
54 }
55
56 const ComponentType& ComponentType::COMP_PICKPOCKET() {
57 static ComponentType _instance("pickpocket", _("Player pickpockets AI"));
58 return _instance;
59 }
60
61 const ComponentType& ComponentType::COMP_LOCATION() {
62 static ComponentType _instance("location", _("Item is in location"));
63 return _instance;
64 }
65
66 const ComponentType& ComponentType::COMP_INFO_LOCATION() {
67 static ComponentType _instance("info_location", _("Item is in info_location"));
68 return _instance;
69 }
70
71 const ComponentType& ComponentType::COMP_CUSTOM_ASYNC() {
72 static ComponentType _instance("custom", _("Custom script")); // sic
73 return _instance;
74 }
75
76 const ComponentType& ComponentType::COMP_CUSTOM_CLOCKED() {
77 static ComponentType _instance(
78 "custom_clocked", _("Custom script queried periodically")
79 );
80 return _instance;
81 }
82
83 const ComponentType& ComponentType::COMP_DISTANCE() {
84 static ComponentType _instance(
85 "distance", _("Two entities are within a radius of each other")
86 );
87 return _instance;
88 }
89
90 const ComponentType& ComponentType::COMP_READABLE_OPENED()
91 {
92 static ComponentType _instance(
93 "readable_opened", _("Readable is opened.")
94 );
95 return _instance;
96 }
97
98 const ComponentType& ComponentType::COMP_READABLE_CLOSED()
99 {
100 static ComponentType _instance(
101 "readable_closed", _("Readable is closed.")
102 );
103 return _instance;
104 }
105
106 const ComponentType& ComponentType::COMP_READABLE_PAGE_REACHED()
107 {
108 static ComponentType _instance(
109 "readable_page_reached", _("A certain page of a readable is reached.")
110 );
111 return _instance;
112 }
113
114 // Static sets
115 const ComponentTypeSet& ComponentType::SET_ALL() {
116 static ComponentTypeSet _instance;
117 if (_instance.empty()) {
118 _instance.insert(COMP_KILL());
119 _instance.insert(COMP_KO());
120 _instance.insert(COMP_AI_FIND_ITEM());
121 _instance.insert(COMP_AI_FIND_BODY());
122 _instance.insert(COMP_ALERT());
123 _instance.insert(COMP_DESTROY());
124 _instance.insert(COMP_ITEM());
125 _instance.insert(COMP_PICKPOCKET());
126 _instance.insert(COMP_LOCATION());
127 _instance.insert(COMP_INFO_LOCATION());
128 _instance.insert(COMP_CUSTOM_ASYNC());
129 _instance.insert(COMP_CUSTOM_CLOCKED());
130 _instance.insert(COMP_DISTANCE());
131 _instance.insert(COMP_READABLE_OPENED());
132 _instance.insert(COMP_READABLE_CLOSED());
133 _instance.insert(COMP_READABLE_PAGE_REACHED());
134 }
135 return _instance;
136 }
137
138 // Construct a named ComponentType
139 ComponentType::ComponentType(const std::string& name,
140 const std::string& displayName)
141 : _id(enumCount++),
142 _name(name),
143 _displayName(displayName)
144 {
145 // Register self in map
146 getMap().insert(ComponentTypeMap::value_type(name, *this));
147 }
148
149 // Lookup a named type in map
150 ComponentType ComponentType::getComponentType(const std::string& name)
151 {
152 ComponentTypeMap::const_iterator i = getMap().find(name);
153 if (i != getMap().end())
154 return i->second;
155 else
156 throw ObjectivesException("Invalid ComponentType: " + name);
157 }
158
159 ComponentType ComponentType::getComponentType(int id)
160 {
161 ComponentTypeMap::const_iterator i = std::find_if(getMap().begin(), getMap().end(),
162 [=] (const ComponentTypeMap::value_type& pair)->bool
163 {
164 return pair.second.getId() == id;
165 });
166
167 if (i != getMap().end())
168 return i->second;
169 else
170 throw ObjectivesException("Invalid ComponentType ID: " + string::to_string(id));
171 }
172
173 }