1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
20 #include "named_entity_manager.h"
22 CNamedEntityManager
* CNamedEntityManager::_instance
= 0;
24 CNamedEntityManager
* CNamedEntityManager::getInstance()
27 _instance
= new CNamedEntityManager();
31 void CNamedEntityManager::destroyInstance()
37 CNamedEntity
& CNamedEntityManager::create(std::string
const& name
)
42 CNamedEntity
& CNamedEntityManager::get(std::string
const& name
)
44 entity_name_container_t::iterator it
= _nameIndex
.find(name
);
45 if (it
!=_nameIndex
.end())
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
));
57 CNamedEntity
* CNamedEntityManager::get(NLMISC::CEntityId
const& id
)
59 entity_id_container_t::iterator it
= _idIndex
.find(id
);
60 if (it
!=_idIndex
.end())
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());
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());
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
)
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
);
130 entities
.push_back(id
);
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) \
149 std::vector<NLMISC::CEntityId> entities; \
150 CNamedEntityManager::getInstance()->select(args[0], entities); \
152 for (uint i = 0; i < entities.size(); i++) \
155 if (args.size()==2) \
159 pointer(entities[i], (args.size()==1), value); \
161 log.displayNL("%s %s", entities[i].toString().c_str(), value.c_str()); \
163 log.displayNL("Entity %s Variable %s = %s", entities[i].toString().c_str(), _CommandName.c_str(), value.c_str()); \
167 void pointer(NLMISC::CEntityId entity, bool get, std::string &value); \
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"; \
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
;
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
);
201 namedEntity
->set(namedEntityChange
.prop
, namedEntityChange
.value
, true);
203 namedEntityChangeQueue
.pop();
209 ENTITY_VARIABLE(NamedEntityName
, "Name of a named entity")
211 ENTITY_GET_NAMEDENTITY
214 value
= namedEntity
->name();
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
224 value
= namedEntity
->getState();
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
235 value
= namedEntity
->getParam1();
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
246 value
= namedEntity
->getParam2();
248 // namedEntity->set("param2", value, true);
249 namedEntityChangeQueue
.push(NamedEntityChange(entity
, "param2", value
));