Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / server / src / ai_service / ai_mgr.cpp
blob5b8527af5b621cdc59b865d2f03be8b59b1f4be0
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"
18 #include "ai_mgr.h"
19 #include "ai_mgr_fauna.h"
20 #include "ai_mgr_npc.h"
21 #include "ai_mgr_pet.h"
22 #include "owners.h"
24 using namespace MULTI_LINE_FORMATER;
26 CManager::CManager(IManagerParent* parent, uint32 alias, std::string const& name, std::string const& filename)
27 : CAliasChild<IManagerParent>(parent, alias, name)
28 , CAliasTreeRoot(filename)
30 _CurSpawnRing = 0;
33 CManager::~CManager()
35 release();
36 _Groups.setChildSize(0);
39 CManager* CManager::createManager(AITYPES::TMgrType type, IManagerParent* managerParent, uint32 alias, std::string const& name, std::string const& mapName, std::string const& filename)
41 CManager* manager = NULL;
42 switch (type)
44 case AITYPES::MgrTypeFauna: manager = new CMgrFauna(managerParent, alias,name, filename); break;
45 case AITYPES::MgrTypeKami: manager = new CMgrNpc(managerParent, alias, name, filename); break;
46 case AITYPES::MgrTypeNpc: manager = new CMgrNpc(managerParent, alias, name, filename); break;
47 case AITYPES::MgrTypeKaravan: manager = new CMgrNpc(managerParent, alias,name, filename); break;
48 case AITYPES::MgrTypePet: manager = new CMgrPet(managerParent, alias,name, filename); break;
49 default:
50 nlwarning("CManager::createManager(): FAILED to instantiate manager due unmanaged type: %s", AITYPES::getName(type));
51 break;
53 return manager;
56 void CManager::update()
58 if (_SpawnList.size()>0)
60 H_AUTO(MgrUpdateSpawnList)
61 // deal with the spawn list
62 // if there are bot groups waiting to be spawned then treat a few of them
63 for (uint count=0;!_SpawnList.empty() && count<_MaxSpawns;count++)
65 while (_SpawnList.size()>0)
67 _CurSpawnRing++;
68 if (_CurSpawnRing>=_SpawnList.size())
69 _CurSpawnRing=0;
71 if (!_SpawnList[_CurSpawnRing]->isSpawned())
72 break;
74 _SpawnList.erase(_SpawnList.begin()+_CurSpawnRing); // remove the group.
77 if (_SpawnList.size()>0)
79 if (_SpawnList[_CurSpawnRing]->spawn())
80 _SpawnList.erase(_SpawnList.begin()+_CurSpawnRing); // remove the group.
85 if (groups().size()==0)
86 return;
88 // we're going to run through the groups updating each one once every n ticks where n is
89 // determined by the 'getUpdatePriority()' mask
90 // note that 'getUpdatePriority()' needs to be renamed but it's late, I'm tired and I just need
91 // an unused variable to work with that already exists so that I don't have to go through a complete
92 // project recompile.
93 uint32 timeOffset = CTimeInterface::gameCycle();
96 H_AUTO(MgrUpdateSpawned);
97 int index = -1;
98 FOREACH(it, CCont<CGroup>, groups())
100 ++index;
101 NLMISC::CSmartPtr<CGroup> persistent = *it; // To avoid destruction while executing ..
102 if (!persistent->isSpawned())
103 continue;
105 CSpawnGroup* const spawned = persistent->getSpawnObj();
106 if (spawned->getUpdatePriority()==1)
107 ++AISStat::GrpFastUpdCtr;
108 if (spawned->getUpdatePriority()==31)
109 ++AISStat::GrpSlowUpdCtr;
110 if (((index+timeOffset)&spawned->getUpdatePriority())!=0)
111 continue;
113 spawned->update();
118 CGroup* CManager::getNextValidGroupChild(CGroup* group)
120 return _Groups.getNextValidChild(static_cast<CGroup*>(group));
123 void CManager::serviceEvent(CServiceEvent const& info)
125 FOREACH(it, CCont<CGroup>, groups())
126 it->serviceEvent(info);
129 void CManager::spawn()
131 FOREACH(it, CCont<CGroup>, groups())
132 _SpawnList.push_back(*it);
133 _MaxSpawns = (groups().size()+127)/(127+1); // the 127 and 128 are arbitrary ( D M ).
136 void CManager::despawnMgr()
138 FOREACH(it, CCont<CGroup>, groups())
139 it->despawnGrp();
142 CGroup* CManager::getGroup(uint32 index)
144 if (index>=groups().size())
145 return NULL;
146 return groups()[index];
149 bool CManager::isEmpty() const
151 for(uint k = 0; k < _Groups.size(); ++k)
153 if (_Groups[k] && _Groups[k]->getNextValidBotChild() != NULL) return false;
155 return true;
158 std::string CManager::getIndexString() const
160 return getOwner()->getManagerIndexString(this);
163 std::string CManager::getOneLineInfoString() const
165 return std::string("Manager '") + getName() + "'";
168 std::string CManager::getFullName() const
170 return getName();
173 std::vector<std::string> CManager::getMultiLineInfoString() const
175 std::vector<std::string> container;
178 pushTitle(container, "CManager");
179 pushEntry(container, "id=" + getIndexString());
180 container.back() += " type=" + std::string(AITYPES::getName(type()));
181 container.back() += " alias=" + getAliasString();
182 container.back() += " name=" + getName();
183 pushFooter(container);
186 return container;
189 void CManager::release()
193 void CManager::addToSpawn(CGroup* group)
195 for (size_t i=0; i<_SpawnList.size(); ++i)
197 if (_SpawnList[i].ptr()==group)
198 return;
200 _SpawnList.push_back(group);
203 void CManager::removeFromSpawnList(CGroup const* group)
205 for (size_t i=0; i<_SpawnList.size(); ++i)
207 if (_SpawnList[i]==group)
209 _SpawnList.erase(_SpawnList.begin()+i); // remove the group.
210 return;
215 CAIInstance* CManager::getAIInstance() const
217 return getOwner()->getAIInstance();