Merge branch 'lua_versions' into main/rendor-staging
[ryzomcore.git] / ryzom / tools / reynolds / reynolds_manager.cpp
blobb005aac5feb2aa955b22d4ae81b38f8b44645b10
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/>.
19 #include "stdpch.h"
21 #include "reynolds_manager.h"
23 #include "nel/georges/u_form_loader.h"
24 #include "nel/georges/load_form.h"
26 using namespace std;
27 using namespace NLMISC;
28 using namespace NLPACS;
29 using namespace NLGEORGES;
33 * Manager variables
36 // Continents
37 CContinentContainer CReynoldsManager::_Continents;
39 // Track Map, all tracks referenced, held by a simple pointer so when no one references a track, it is deleted
40 CReynoldsManager::TTrackMap CReynoldsManager::_TrackMap;
42 // Controlled Track Map, only controlled tracks, held by a smart pointer
43 CReynoldsManager::TControlledTrackMap CReynoldsManager::_ControlledTrackMap;
45 // Command interface
46 CReynoldsManager::ICommandInterface *CReynoldsManager::_CommandInterface = NULL;
48 // User init callback
49 CReynoldsManager::TUserCallback CReynoldsManager::_UserInitCallback = NULL;
51 // User motion callback
52 CReynoldsManager::TUserMotionCallback CReynoldsManager::_UserMotionCallback = NULL;
54 // User release callback
55 CReynoldsManager::TUserCallback CReynoldsManager::_UserReleaseCallback = NULL;
57 // Georges sheets
58 map<CSheetId, CTrack::CSheet> CReynoldsManager::_Sheets;
60 // Sheets initialised ?
61 bool CReynoldsManager::_Initialised;
63 // Current internal cycle
64 uint32 CReynoldsManager::_Cycle;
67 * Manager methods
71 // ------------------------------------
72 // Constructor
73 CReynoldsManager::CReynoldsManager()
75 nlerror("CReynoldsManager: static library, must not be instanciated !");
80 // ------------------------------------
81 // Init
82 void CReynoldsManager::init(const std::string &packSheetFile)
84 _Continents.init(100, 100, 8.0, 1);
86 initSheets(packSheetFile);
88 _Cycle = 0;
91 // ------------------------------------
92 // Update
93 void CReynoldsManager::update(double dt)
95 TControlledTrackMap::iterator it;
96 for (it=_ControlledTrackMap.begin(); it!=_ControlledTrackMap.end(); ++it)
98 CTrack *track = (CTrack*)((*it).second);
99 track->update(dt);
102 ++_Cycle;
105 // ------------------------------------
106 // Release
107 void CReynoldsManager::release()
109 _Continents.clear();
111 _ControlledTrackMap.clear();
113 if (!_TrackMap.empty())
115 nlwarning("ReynoldsLib:CReynoldsManager:release(): TrackMap not empty at release !!");
117 TTrackMap::iterator it;
118 for (it=_TrackMap.begin(); it!=_TrackMap.end(); ++it)
119 delete (*it).second;
121 _TrackMap.clear();
127 // ------------------------------------
128 // Load continent
129 void CReynoldsManager::loadContinent(const std::string &name, const std::string &file, sint index)
131 _Continents.loadContinent(name, file, index);
138 // ------------------------------------
139 // Create Track
140 CTrack *CReynoldsManager::createTrack(const CEntityId &entity)
142 // look for the target, and create a new track if not found
143 TTrackMap::iterator itt = _TrackMap.find(entity);
144 if (itt == _TrackMap.end())
146 CTrack *track = new CTrack();
147 pair<TTrackMap::iterator, bool> res = _TrackMap.insert(TTrackMap::value_type(entity, track));
148 itt = res.first;
150 requestSheet(entity);
152 return (*itt).second;
155 // ------------------------------------
156 // Remove Track from map
157 void CReynoldsManager::removeTrackFromMap(const CEntityId &entity)
159 _TrackMap.erase(entity);
169 // ------------------------------------
170 // Follow
171 void CReynoldsManager::follow(const NLMISC::CEntityId &entity, const NLMISC::CEntityId &target)
173 // look for the track, and create a new one if not found -- in both maps
174 CTrack *etrack = createTrack(entity);
176 TControlledTrackMap::iterator ite = _ControlledTrackMap.find(entity);
177 if (ite == _ControlledTrackMap.end())
178 _ControlledTrackMap.insert(TControlledTrackMap::value_type(entity, etrack));
180 // look for the target, and create a new track if not found
181 CTrack *ttrack = createTrack(target);
183 nldebug("ReynoldsLib:CReynoldsManager:follow(): %s now follows %s", entity.toString().c_str(), target.toString().c_str());
185 // let entity follow the target
186 etrack->follow(ttrack);
189 // ------------------------------------
190 // Go to
191 void CReynoldsManager::goTo(const NLMISC::CEntityId &entity, const NLMISC::CVectorD &position)
193 // look for the track, and create a new one if not found -- in both maps
194 CTrack *etrack = createTrack(entity);
196 TControlledTrackMap::iterator ite = _ControlledTrackMap.find(entity);
197 if (ite == _ControlledTrackMap.end())
198 _ControlledTrackMap.insert(TControlledTrackMap::value_type(entity, etrack));
200 // create a fake track for point position
201 static uint i = 0;
202 CEntityId id(0, i++, 0, 0);
203 CTrack *pointTo = createTrack(id);
204 pointTo->setStatic();
205 pointTo->setId(id, CSheetId::Unknown);
206 pointTo->setPosition(position, 0.0f);
208 nldebug("ReynoldsLib:CReynoldsManager:goTo(): %s now goes to (%.1f,%.1f)", entity.toString().c_str(), position.x, position.y);
210 // let entity follow the target
211 etrack->follow(pointTo);
214 // ------------------------------------
215 // Stop
216 void CReynoldsManager::leaveMove(const NLMISC::CEntityId &entity)
218 TControlledTrackMap::iterator ite = _ControlledTrackMap.find(entity);
219 if (ite == _ControlledTrackMap.end())
221 nlwarning("ReynoldsLib:CReynoldsManager:leaveMove(): undefined entity %s", entity.toString().c_str());
222 return;
225 nldebug("ReynoldsLib:CReynoldsManager:leaveMove(): %s control left", entity.toString().c_str());
227 (*ite).second->leave();
228 _ControlledTrackMap.erase(ite);
231 // ------------------------------------
232 // Destroy
233 void CReynoldsManager::destroy(const NLMISC::CEntityId &entity)
235 CTrack *track = getTrack(entity);
236 if (track != NULL)
237 track->forceRelease();
239 if (track != NULL && track->hasControlOwned())
240 track->leave();
242 _ControlledTrackMap.erase(entity);
250 // ------------------------------------
251 // Request Sheet
252 void CReynoldsManager::requestSheet(const NLMISC::CEntityId &entity)
254 if (!checkInterface())
255 return;
257 _CommandInterface->requestSheet(entity);
260 // ------------------------------------
261 // Request Position
262 void CReynoldsManager::requestPosition(const NLMISC::CEntityId &entity)
264 if (!checkInterface())
265 return;
267 _CommandInterface->requestPosition(entity);
270 // ------------------------------------
271 // Request Position Updates
272 void CReynoldsManager::requestPositionUpdates(const NLMISC::CEntityId &entity)
274 if (!checkInterface())
275 return;
277 _CommandInterface->requestPositionUpdates(entity);
280 // ------------------------------------
281 // Unrequest Position Updates
282 void CReynoldsManager::unrequestPositionUpdates(const NLMISC::CEntityId &entity)
284 if (!checkInterface())
285 return;
287 _CommandInterface->unrequestPositionUpdates(entity);
290 // ------------------------------------
291 // Request Vision
292 void CReynoldsManager::requestVision(const NLMISC::CEntityId &entity)
294 if (!checkInterface())
295 return;
297 _CommandInterface->requestVision(entity);
300 // ------------------------------------
301 // Unrequest Vision
302 void CReynoldsManager::unrequestVision(const NLMISC::CEntityId &entity)
304 if (!checkInterface())
305 return;
307 _CommandInterface->unrequestVision(entity);
311 // ------------------------------------
312 // Updated position
313 void CReynoldsManager::updatedPosition(const CEntityId &entity, const CVectorD &position, float heading)
315 if (_CommandInterface != NULL)
316 _CommandInterface->updatePosition(entity, position, heading);
319 // ------------------------------------
320 // Updated state
321 void CReynoldsManager::stateChanged(const CEntityId &entity, CTrackBase::TTrackState state)
323 if (_CommandInterface != NULL)
324 _CommandInterface->stateChanged(entity, state);
328 // ------------------------------------
329 // TrackStop
330 void CReynoldsManager::trackStop(CTrack *track)
332 if (checkInterface())
333 _CommandInterface->stopTrack(track->getId());
335 // remove track from controlled tracks
336 _ControlledTrackMap.erase(track->getId());
340 // ------------------------------------
341 // Create Move primitive
342 void CReynoldsManager::createMovePrimitive(const NLMISC::CVectorD &pos, NLPACS::UMovePrimitive *&primitive, NLPACS::UMoveContainer *&container)
344 primitive = NULL;
345 container = NULL;
347 uint8 continent = _Continents.findContinent(pos);
348 if (continent == -1)
350 nlwarning("ReynoldsLib:CReynoldsManager:createMovePrimitive(): unable to create move primitive");
351 return;
354 container = _Continents.getMoveContainer(continent);
355 primitive = container->addNonCollisionablePrimitive();
362 // ------------------------------------
363 // Set Sheet
364 void CReynoldsManager::setSheet(const NLMISC::CEntityId &id, const NLMISC::CSheetId &sheet)
366 CTrack *track = getTrack(id);
367 if (track == NULL)
369 nlwarning("ReynoldsLib:CReynoldsManager:setSheet(): Track %s not found", id.toString().c_str());
370 return;
373 if (track->hasId())
375 nlwarning("ReynoldsLib:CReynoldsManager:setSheet(): Track %s already has an Id", id.toString().c_str());
376 return;
379 track->setId(id, sheet);
382 // ------------------------------------
383 // Set Position
384 void CReynoldsManager::setPosition(const NLMISC::CEntityId &id, const NLMISC::CVectorD &position, float heading)
386 CTrack *track = getTrack(id);
387 if (track == NULL)
389 nlwarning("ReynoldsLib:CReynoldsManager:setPosition(): Track %s not found", id.toString().c_str());
390 return;
393 track->setPosition(position, heading);
396 // ------------------------------------
397 // Set Vision
398 void CReynoldsManager::setVision(const NLMISC::CEntityId &id, const std::vector<NLMISC::CEntityId> &in, const std::vector<NLMISC::CEntityId> &out)
400 CTrack *track = getTrack(id);
401 if (track == NULL)
403 nlwarning("ReynoldsLib:CReynoldsManager:setVision(): Track %s not found", id.toString().c_str());
404 return;
407 track->updateVision(in, out);
410 // ------------------------------------
411 // Set Vision
412 void CReynoldsManager::setVision(const NLMISC::CEntityId &id, const std::vector<NLMISC::CEntityId> &vision)
414 CTrack *track = getTrack(id);
415 if (track == NULL)
417 nlwarning("ReynoldsLib:CReynoldsManager:setVision(): Track %s not found", id.toString().c_str());
418 return;
421 track->updateVision(vision);
427 //-------------------------------------------------------------------------
428 // Init Sheets
429 void CReynoldsManager::initSheets(const std::string &packSheetFile)
431 if (_Initialised)
432 return;
434 std::vector<std::string> filters;
435 filters.push_back("creature");
436 filters.push_back("player");
438 loadForm(filters, packSheetFile, _Sheets);
440 _Initialised=true;
444 //-------------------------------------------------------------------------
445 // Lookup Sheet
446 const CTrack::CSheet *CReynoldsManager::lookup(const CSheetId &id)
448 nlassert(_Initialised);
450 // setup an iterator and lookup the sheet id in the map
451 std::map<CSheetId, CTrack::CSheet>::iterator it;
452 it=_Sheets.find(id);
454 // if we found a valid entry return a pointer to the creature record otherwise 0
455 if (it != _Sheets.end())
456 return &((*it).second);
457 else
458 return NULL;