New lua versions
[ryzomcore.git] / ryzom / server / src / ai_service / named_entity_manager.cpp
blobe929a91ad73196954c8dab19135847ed55e97f93
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdpch.h"
19 #include <queue>
20 #include "named_entity_manager.h"
22 CNamedEntityManager* CNamedEntityManager::_instance = 0;
24 CNamedEntityManager* CNamedEntityManager::getInstance()
26 if (_instance==0)
27 _instance = new CNamedEntityManager();
28 return _instance;
31 void CNamedEntityManager::destroyInstance()
33 delete _instance;
34 _instance = 0;
37 CNamedEntity& CNamedEntityManager::create(std::string const& name)
39 return get(name);
42 CNamedEntity& CNamedEntityManager::get(std::string const& name)
44 entity_name_container_t::iterator it = _nameIndex.find(name);
45 if (it!=_nameIndex.end())
46 return *it->second;
47 else
49 CNamedEntity* ptr = new CNamedEntity(name);
50 _entities.insert(ptr);
51 _nameIndex.insert(std::make_pair(ptr->name(), ptr));
52 _idIndex.insert(std::make_pair(ptr->id(), ptr));
53 return *ptr;
57 CNamedEntity* CNamedEntityManager::get(NLMISC::CEntityId const& id)
59 entity_id_container_t::iterator it = _idIndex.find(id);
60 if (it!=_idIndex.end())
61 return it->second;
62 else
63 return 0;
66 void CNamedEntityManager::destroy(CNamedEntity const& entity)
68 destroy(entity.name());
71 void CNamedEntityManager::destroy(std::string const& name)
73 entity_name_container_t::iterator it = _nameIndex.find(name);
74 if (it!=_nameIndex.end())
76 CNamedEntity* ptr = it->second;
77 _nameIndex.erase(ptr->name());
78 _idIndex.erase(ptr->id());
79 _entities.erase(ptr);
80 delete ptr;
84 void CNamedEntityManager::destroy(NLMISC::CEntityId const& id)
86 entity_id_container_t::iterator it = _idIndex.find(id);
87 if (it!=_idIndex.end())
89 CNamedEntity* ptr = it->second;
90 _nameIndex.erase(ptr->name());
91 _idIndex.erase(ptr->id());
92 _entities.erase(ptr);
93 delete ptr;
97 bool CNamedEntityManager::exists(std::string const& name)
99 return _nameIndex.find(name) != _nameIndex.end();
102 bool CNamedEntityManager::exists(NLMISC::CEntityId const& id)
104 return _idIndex.find(id) != _idIndex.end();
107 /** Valid format for request string is:
108 - * Select all entities
109 - <entity id> Select the specified entity using his eid (format is "(id:type:crea:dyn)")
110 - <name> Select an entity with its name (any name allowed for a named entity)
112 void CNamedEntityManager::select(std::string const& request, std::vector<NLMISC::CEntityId>& entities)
114 if (request.empty())
115 return;
117 if (request == "*")
119 // we want all entities
120 FOREACHC(it, entity_container_t, _entities)
122 entities.push_back((*it)->id());
125 else if (request[0] == '(')
127 // we want a specific entity id
128 NLMISC::CEntityId id(request);
129 if (exists(id))
130 entities.push_back(id);
132 else
134 // try with the name
135 if (exists(request))
136 entities.push_back(get(request).id());
140 #define ENTITY_VARIABLE(__name,__help) \
141 struct __name##Class : public NLMISC::ICommand \
143 __name##Class () : NLMISC::ICommand("variables",#__name, __help, "<entity> [<value>]") { Type = Variable; } \
144 virtual bool execute(const std::string &rawCommandString, const std::vector<std::string> &args, NLMISC::CLog &log, bool quiet, bool human) \
146 if (args.size() != 1 && args.size() != 2) \
147 return false; \
149 std::vector<NLMISC::CEntityId> entities; \
150 CNamedEntityManager::getInstance()->select(args[0], entities); \
152 for (uint i = 0; i < entities.size(); i++) \
154 std::string value; \
155 if (args.size()==2) \
156 value = args[1]; \
157 else \
158 value = "???"; \
159 pointer(entities[i], (args.size()==1), value); \
160 if (quiet) \
161 log.displayNL("%s %s", entities[i].toString().c_str(), value.c_str()); \
162 else \
163 log.displayNL("Entity %s Variable %s = %s", entities[i].toString().c_str(), _CommandName.c_str(), value.c_str()); \
165 return true; \
167 void pointer(NLMISC::CEntityId entity, bool get, std::string &value); \
168 }; \
169 __name##Class __name##Instance; \
170 void __name##Class::pointer(NLMISC::CEntityId entity, bool get, std::string &value)
172 #define ENTITY_GET_NAMEDENTITY \
173 CNamedEntity* namedEntity = CNamedEntityManager::getInstance()->get(entity); \
174 if(namedEntity == 0) \
176 nlwarning("Unknown entity '%s'", entity.toString().c_str()); \
177 if (get) value = "UnknownEntity"; \
178 return; \
182 struct NamedEntityChange
184 NamedEntityChange(NLMISC::CEntityId _id, std::string _prop, std::string _value)
185 : id(_id), prop(_prop), value(_value) { }
186 NLMISC::CEntityId id;
187 std::string prop;
188 std::string value;
190 std::queue<NamedEntityChange> namedEntityChangeQueue;
192 void execNamedEntityChanges()
194 CNamedEntityManager* manager = CNamedEntityManager::getInstance();
195 while (!namedEntityChangeQueue.empty())
197 NamedEntityChange& namedEntityChange = namedEntityChangeQueue.front();
198 CNamedEntity* namedEntity = manager->get(namedEntityChange.id);
199 if(namedEntity != 0)
201 namedEntity->set(namedEntityChange.prop, namedEntityChange.value, true);
203 namedEntityChangeQueue.pop();
209 ENTITY_VARIABLE(NamedEntityName, "Name of a named entity")
211 ENTITY_GET_NAMEDENTITY
213 if (get)
214 value = namedEntity->name();
215 else
216 nlwarning("Trying to change a named entity's name: '%s' to '%s'", namedEntity->name().c_str(), value.c_str());
219 ENTITY_VARIABLE(NamedEntityState, "State of a named entity")
221 ENTITY_GET_NAMEDENTITY
223 if (get)
224 value = namedEntity->getState();
225 else
226 // namedEntity->set("state", value, true);
227 namedEntityChangeQueue.push(NamedEntityChange(entity, "state", value));
230 ENTITY_VARIABLE(NamedEntityParam1, "Param1 of a named entity")
232 ENTITY_GET_NAMEDENTITY
234 if (get)
235 value = namedEntity->getParam1();
236 else
237 // namedEntity->set("param1", value, true);
238 namedEntityChangeQueue.push(NamedEntityChange(entity, "param1", value));
241 ENTITY_VARIABLE(NamedEntityParam2, "Param1 of a named entity")
243 ENTITY_GET_NAMEDENTITY
245 if (get)
246 value = namedEntity->getParam2();
247 else
248 // namedEntity->set("param2", value, true);
249 namedEntityChangeQueue.push(NamedEntityChange(entity, "param2", value));