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 //-----------------------------------------------
22 //-----------------------------------------------
24 #include "nel/net/message.h"
26 #include "sabrina_ai_interface.h"
27 #include "sabrina_message_callbacks.h"
30 //-----------------------------------------------
32 //-----------------------------------------------
34 std::vector
<uint8
> CSabrinaAIInterface::_RegisteredServices
;
35 CSabrinaAIInterface::TAIEventList
CSabrinaAIInterface::_AIEvents
;
36 std::vector
<CAiEventReport
> CSabrinaAIInterface::_AIEventReports
;
39 //-----------------------------------------------
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 //-----------------------------------------------
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();
69 TAIEventList::iterator eventIt
;
70 for (eventIt
= _AIEvents
.begin() ; eventIt
!= _AIEvents
.end(); ++eventIt
)
72 msgai
.serial( *(*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
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
)
101 // INFOLOG("Send EVENT_REPORTS to AI service %d", (*it) );
104 // clear the event report buffer
105 _AIEventReports
.clear();
110 //-----------------------------------------------
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
);
132 // add the service to the vector
133 _RegisteredServices
.push_back( serviceId
);
136 //--------------------------------------------------------------
137 // unregisterAIService()
138 //--------------------------------------------------------------
139 void CSabrinaAIInterface::unregisterAIService( uint8 serviceId
)
143 // remove all occurences of the service from the services vector
144 while (i
<_RegisteredServices
.size())
146 if (_RegisteredServices
[i
]==serviceId
)
149 _RegisteredServices
[i
]=_RegisteredServices
[_RegisteredServices
.size()-1];
150 _RegisteredServices
.pop_back();
157 // make sure the service was found and only found once in the vector
160 nlwarning("BUG: CSabrinaPhraseManager::unregisterAIService(): service '%d' not found in registerd AI service vector",serviceId
);
165 else if (foundCount
>0)
167 nlwarning("BUG: CSabrinaPhraseManager::unregisterAIService(): found more than one ref to service '%d' in registerd AI service vector",serviceId
);
174 //--------------------------------------------------------------
175 // addAiEventReport()
176 //--------------------------------------------------------------
177 void CSabrinaAIInterface::addAiEventReport( const CAiEventReport
&report
)
179 _AIEventReports
.push_back(report
);
182 //--------------------------------------------------------------
184 //--------------------------------------------------------------
185 void CSabrinaAIInterface::addAIEvent( const IAIEvent
*event
)
188 _AIEvents
.push_back( const_cast<IAIEvent
*>(event
) );