Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / server / src / sabrina / sabrina_ai_interface.cpp
blobf0bfca2c59a57e16f552a819664330607d945b26
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/>.
20 //-----------------------------------------------
21 // includes
22 //-----------------------------------------------
23 // nel
24 #include "nel/net/message.h"
25 // sabrina
26 #include "sabrina_ai_interface.h"
27 #include "sabrina_message_callbacks.h"
30 //-----------------------------------------------
31 // singleton data
32 //-----------------------------------------------
34 std::vector<uint8> CSabrinaAIInterface::_RegisteredServices;
35 CSabrinaAIInterface::TAIEventList CSabrinaAIInterface::_AIEvents;
36 std::vector<CAiEventReport> CSabrinaAIInterface::_AIEventReports;
39 //-----------------------------------------------
40 // init()
41 //-----------------------------------------------
42 void CSabrinaAIInterface::init()
44 //array of callback items
45 NLNET::TUnifiedCallbackItem _cbArray[] =
47 { "REGISTER_AI_EVENT_REPORTS", SABRINA::cbRegisterServiceAI },
48 { "UNREGISTER_AI_EVENT_REPORTS", SABRINA::cbUnregisterServiceAI },
51 NLNET::CUnifiedNetwork::getInstance()->addCallbackArray( _cbArray, sizeof(_cbArray) / sizeof(_cbArray[0]) );
54 //-----------------------------------------------
55 // flushEvents()
56 //-----------------------------------------------
57 void CSabrinaAIInterface::flushEvents()
59 // iterator for vector of registered services
60 std::vector<uint8>::iterator it;
62 if ( !_AIEvents.empty() )
64 // build the message to send
65 NLNET::CMessage msgai("AI_EVENTS");
66 uint16 size = _AIEvents.size();
67 msgai.serial( size );
69 TAIEventList::iterator eventIt;
70 for (eventIt = _AIEvents.begin() ; eventIt != _AIEvents.end(); ++eventIt)
72 msgai.serial( *(*eventIt) );
73 delete (*eventIt);
76 // dispatch the message to all registered services
77 for (it = _RegisteredServices.begin() ; it != _RegisteredServices.end() ; ++it)
79 sendMessageViaMirror (*it, msgai);
80 // INFOLOG("Send AI_EVENTS to AI service %d", (*it) );
83 // clear the event report buffer
84 _AIEvents.clear();
87 if ( !_AIEventReports.empty() )
89 // build the message to send
90 CBSAIEventReportMsg msgAI;
91 const uint nbAiReports = _AIEventReports.size();
92 for (uint i = 0 ; i < nbAiReports ; ++i )
94 msgAI.pushBack( _AIEventReports[i] );
97 // dispatch the message to all registered services
98 for (it = _RegisteredServices.begin() ; it != _RegisteredServices.end() ; ++it)
100 msgAI.send (*it );
101 // INFOLOG("Send EVENT_REPORTS to AI service %d", (*it) );
104 // clear the event report buffer
105 _AIEventReports.clear();
110 //-----------------------------------------------
111 // init()
112 //-----------------------------------------------
113 void CSabrinaAIInterface::release()
117 //--------------------------------------------------------------
118 // registerAIService()
119 //--------------------------------------------------------------
120 void CSabrinaAIInterface::registerAIService( uint8 serviceId )
122 // make sure the service isn't already in our vector
123 for (uint32 i=0;i<_RegisteredServices.size();++i)
124 if (_RegisteredServices[i]==serviceId)
126 nlwarning("BUG: CSabrinaPhraseManager::unregisterAIService(): service '%d' not found in registerd AI service vector",serviceId);
127 #ifdef NL_DEBUG
128 nlstop
129 #endif
130 return;
132 // add the service to the vector
133 _RegisteredServices.push_back( serviceId );
136 //--------------------------------------------------------------
137 // unregisterAIService()
138 //--------------------------------------------------------------
139 void CSabrinaAIInterface::unregisterAIService( uint8 serviceId )
141 uint32 foundCount=0;
142 uint32 i=0;
143 // remove all occurences of the service from the services vector
144 while (i<_RegisteredServices.size())
146 if (_RegisteredServices[i]==serviceId)
148 ++foundCount;
149 _RegisteredServices[i]=_RegisteredServices[_RegisteredServices.size()-1];
150 _RegisteredServices.pop_back();
152 else
154 ++i;
157 // make sure the service was found and only found once in the vector
158 if (foundCount==0)
160 nlwarning("BUG: CSabrinaPhraseManager::unregisterAIService(): service '%d' not found in registerd AI service vector",serviceId);
161 #ifdef NL_DEBUG
162 nlstop
163 #endif
165 else if (foundCount>0)
167 nlwarning("BUG: CSabrinaPhraseManager::unregisterAIService(): found more than one ref to service '%d' in registerd AI service vector",serviceId);
168 #ifdef NL_DEBUG
169 nlstop
170 #endif
174 //--------------------------------------------------------------
175 // addAiEventReport()
176 //--------------------------------------------------------------
177 void CSabrinaAIInterface::addAiEventReport( const CAiEventReport &report )
179 _AIEventReports.push_back(report);
182 //--------------------------------------------------------------
183 // addAIEvent()
184 //--------------------------------------------------------------
185 void CSabrinaAIInterface::addAIEvent( const IAIEvent *event )
187 if (event != NULL)
188 _AIEvents.push_back( const_cast<IAIEvent*>(event) );