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/>.
17 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
23 #include "game_share/ai_wrapper.h"
25 #include "../ai_share/ai_share.h"
27 #include "game_share/persistent_data.h"
29 #include "ai_primitive_parser.h"
32 using namespace NLLIGO
;
33 using namespace NLNET
;
34 using namespace NLMISC
;
38 NLLIGO::CLigoConfig
* LigoConfigPtr
;
41 void CAiWrapperServer::init(NLLIGO::CLigoConfig
* ligoConfig
)
43 const char* CLASS_FILE_NAME
="world_editor_classes.xml";
45 if ( ! CPrimitiveContext::instance().CurrentLigoConfig
)
48 CPrimitiveContext::instance().CurrentLigoConfig
= R2::LigoConfigPtr
;
49 // Read the ligo primitive class file
50 // NLLIGO::Register();
54 AI_SHARE::init(R2::LigoConfigPtr
);
57 void CAiWrapperServer::streamToPdr(NLMISC::IStream
& stream
, const std::string
& primName
, CPersistentDataRecord
& pdr
)
59 H_AUTO_INST( streamToPdr
);
60 CAIPrimitiveParser::init(&pdr
);
61 AI_SHARE::parsePrimStream(stream
, primName
.c_str());
62 CAIPrimitiveParser::release();
65 void CAiWrapperServer::primsToPdr(NLLIGO::CPrimitives
*prims
, const std::string
& primName
, CPersistentDataRecord
& pdr
)
67 H_AUTO_INST( primsToPdr
);
68 CAIPrimitiveParser::init(&pdr
);
69 AI_SHARE::parsePrimNoStream(prims
, primName
.c_str());
70 CAIPrimitiveParser::release();
73 void CAiWrapperServer::pdrToFile(CPersistentDataRecord
& pdr
, const std::string
& pdrName
)
75 CAIPrimitiveParser::init(&pdr
);
76 CAIPrimitiveParser::getInstance().writeFile(pdrName
.c_str());
77 CAIPrimitiveParser::release();
80 void CAiWrapperServer::fileToPdr(const std::string
& pdrName
, CPersistentDataRecord
& pdr
)
82 CAIPrimitiveParser::init(&pdr
);
83 CAIPrimitiveParser::getInstance().readFile(pdrName
.c_str());
84 CAIPrimitiveParser::release();
87 void CAiWrapperServer::displayPdr( CPersistentDataRecord
& pdr
)
89 CAIPrimitiveParser::init(&pdr
);
90 CAIPrimitiveParser::getInstance().display();
91 CAIPrimitiveParser::release();
94 void CAiWrapperServer::clearPdr( CPersistentDataRecord
& pdr
)
96 CAIPrimitiveParser::init(&pdr
);
97 CAIPrimitiveParser::getInstance().clear();
98 CAIPrimitiveParser::release();
101 void CAiWrapperServer::primitiveFileToPdr(const std::string
& primitiveName
, CPersistentDataRecord
& pdr
)
104 CAIPrimitiveParser::init(&pdr
);
105 AI_SHARE::parsePrimFile(primitiveName
.c_str());
106 CAIPrimitiveParser::release();
109 void CAiWrapperServer::stopTest(TSessionId sessionId
, uint32 aiInstanceId
)
111 nldebug("Stop Test in session %u (aiInstance %u)", sessionId
.asInt(), aiInstanceId
);
112 CMessage
msgout("R2_STOPLIVE");
115 msgout
.serial(aiInstanceId
);
116 msgout
.serial(isBase
);
117 CUnifiedNetwork::getInstance()->send("AIS",msgout
);
120 void CAiWrapperServer::startTest(TSessionId sessionId
, uint32 aiInstanceId
, CPersistentDataRecord
& pdr
)
123 nldebug("Start Test in session %u (aiInstance %u)", sessionId
.asInt(), aiInstanceId
);
124 uint32 totalDataSize
= pdr
.totalDataSize();
125 char* dest
= new char[totalDataSize
];
126 if (!pdr
.toBuffer(dest
, totalDataSize
))
128 nlwarning("can't serialise data");
133 CMessage
msgout("R2_GOLIVE");
135 msgout
.serial(sessionId
);
136 msgout
.serial(aiInstanceId
);
137 msgout
.serial(isBase
);
138 msgout
.serial(totalDataSize
);
139 msgout
.serialBuffer (reinterpret_cast<uint8
*>(dest
), totalDataSize
);
140 CUnifiedNetwork::getInstance()->send("AIS",msgout
);
144 void CAiWrapperServer::startInstance(TSessionId sessionId
, uint32 aiInstanceId
)
146 nldebug("Start Instance in session %u (aiInstance %u)", sessionId
.asInt(), aiInstanceId
);
147 nlinfo("R2An: startInstance %u", aiInstanceId
);
148 CMessage
msgout("R2_STARTINSTANCE");
149 msgout
.serial(aiInstanceId
);
150 CUnifiedNetwork::getInstance()->send("AIS",msgout
);
154 void CAiWrapperServer::stopAct(TSessionId sessionId
, uint32 aiInstanceId
)
156 nldebug("Stop Act in session %u (aiInstance %u)", sessionId
.asInt(), aiInstanceId
);
157 CMessage
msgout("R2_STOPLIVE");
158 msgout
.serial(aiInstanceId
);
160 msgout
.serial(isBase
);
161 CUnifiedNetwork::getInstance()->send("AIS",msgout
);
164 void CAiWrapperServer::startAct(TSessionId sessionId
, uint32 aiInstanceId
, CPersistentDataRecord
& pdr
)
166 nldebug("Start Act in session %u (aiInstance %u)", sessionId
.asInt(), aiInstanceId
);
167 uint32 totalDataSize
= pdr
.totalDataSize();
168 char* dest
= new char[totalDataSize
];
169 if (!pdr
.toBuffer(dest
, totalDataSize
))
171 nlwarning("can't serialise data");
176 CMessage
msgout("R2_GOLIVE");
178 msgout
.serial(sessionId
);
179 msgout
.serial(aiInstanceId
);
180 msgout
.serial(isBase
);
181 msgout
.serial(totalDataSize
);
182 msgout
.serialBuffer (reinterpret_cast<uint8
*>(dest
), totalDataSize
);
183 CUnifiedNetwork::getInstance()->send("AIS",msgout
);
187 void CAiWrapperServer::setAggroRange(NLMISC::CEntityId entityId
, float range
)
189 CMessage
msgout("EXEC_COMMAND");
190 std::string command
= NLMISC::toString("eventSetNpcGroupAggroRange %s %f", entityId
.toString().c_str(), range
);
191 msgout
.serial(command
);
192 CUnifiedNetwork::getInstance()->send("EGS",msgout
);
197 static std::string
aliasToString(uint32 alias
)
199 uint32 staticPart
= alias
>> 20;
200 uint32 dynPart
= alias
& ((1 << 21)-1);
201 return NLMISC::toString("(A:%u:%u)", staticPart
, dynPart
);
205 void CAiWrapperServer::despawnEntity(NLMISC::CEntityId entityId
, uint32 alias
)
207 uint32 messageVersion
= 1;
209 std::string eid
= entityId
.toString();
211 std::string str
= NLMISC::toString("()despawnBotByAlias(\"%s\");", aliasToString(alias
).c_str());
213 CMessage
msgout("R2_NPC_BOT_SCRIPT_BY_ID");
214 msgout
.serial(messageVersion
);
215 msgout
.serial(nbString
);
219 CUnifiedNetwork::getInstance()->send("AIS",msgout
);
222 void CAiWrapperServer::setGrpHPLevel(NLMISC::CEntityId entityId
, uint32 alias
, float hp
)
224 uint32 messageVersion
= 1;
226 std::string eid
= entityId
.toString();
228 std::string hpstr
= NLMISC::toString("()setHPScale(%f);", hp
);
230 //CMessage msgout("EVENT_NPC_GROUP_SCRIPT");
231 CMessage
msgout("R2_NPC_BOT_SCRIPT_BY_ID");
232 msgout
.serial(messageVersion
);
233 msgout
.serial(nbString
);
235 msgout
.serial(hpstr
);
237 CUnifiedNetwork::getInstance()->send("AIS",msgout
);
240 void CAiWrapperServer::setHPLevel(NLMISC::CEntityId entityId
, uint32 alias
, float hp
)
243 uint32 messageVersion
= 1;
245 std::string eid
= entityId
.toString();
247 std::string hpstr
= NLMISC::toString("()setBotHPScaleByAlias(%f, \"%s\");", hp
, aliasToString(alias
).c_str());
249 //CMessage msgout("EVENT_NPC_GROUP_SCRIPT");
250 CMessage
msgout("R2_NPC_BOT_SCRIPT_BY_ID");
251 msgout
.serial(messageVersion
);
252 msgout
.serial(nbString
);
254 msgout
.serial(hpstr
);
256 CUnifiedNetwork::getInstance()->send("AIS",msgout
);
260 void CAiWrapperServer::triggerGrpEvent(NLMISC::CEntityId entityId
, float eventId
)
263 uint32 messageVersion
= 1;
265 std::string eid
= entityId
.toString();
266 std::string str
= NLMISC::toString("()setEvent(%f);", eventId
);
267 eventId
= (float)(int) eventId
;
268 if (eventId
< 0 || eventId
> 10) return;
270 CMessage
msgout("R2_NPC_BOT_SCRIPT_BY_ID");
271 msgout
.serial(messageVersion
);
272 msgout
.serial(nbString
);
276 CUnifiedNetwork::getInstance()->send("AIS",msgout
);
280 void CAiWrapperServer::controlNpc(NLMISC::CEntityId clientId
, NLMISC::CEntityId npc
)
283 uint32 messageVersion
= 1;
285 std::string eid
= npc
.toString();
286 std::string str
= NLMISC::toString("()setPlayerController(\"%s\",\"%s\");", npc
.toString().c_str(), clientId
.toString().c_str());
288 CMessage
msgout("R2_NPC_BOT_SCRIPT_BY_ID");
289 msgout
.serial(messageVersion
);
290 msgout
.serial(nbString
);
294 CUnifiedNetwork::getInstance()->send("AIS",msgout
);
297 void CAiWrapperServer::stopControlNpc(NLMISC::CEntityId clientId
, NLMISC::CEntityId npc
)
300 uint32 messageVersion
= 1;
302 std::string eid
= npc
.toString();
303 std::string str
= NLMISC::toString("()clearPlayerController(\"%s\");", npc
.toString().c_str());
305 CMessage
msgout("R2_NPC_BOT_SCRIPT_BY_ID");
306 msgout
.serial(messageVersion
);
307 msgout
.serial(nbString
);
311 CUnifiedNetwork::getInstance()->send("AIS",msgout
);
314 void CAiWrapperServer::triggerUserTrigger(const std::string
& groupName
, uint32 triggerId
)
317 uint32 messageVersion = 1;
320 std::string script = NLMISC::toString("()setEvent(%d);", triggerId);
323 CMessage msgout("R2_NPC_GROUP_SCRIPT_BY_NAME");
324 msgout.serial(messageVersion);
325 msgout.serial(nbString);
326 msgout.serial(const_cast<std::string&>(groupName));
327 msgout.serial(script);
329 CUnifiedNetwork::getInstance()->send("AIS",msgout);
336 std::string script
= NLMISC::toString("\"()setEvent(%d);\"", triggerId
);
337 CMessage
msgout("EXEC_COMMAND");
338 std::string command
= NLMISC::toString("script %s %s", groupName
.c_str(), script
.c_str());
339 msgout
.serial(command
);
340 CUnifiedNetwork::getInstance()->send("AIS",msgout
);
345 void CAiWrapperServer::setPioneerRight(NLMISC::CEntityId entityId
, const R2::TPioneerRight
& right
)
348 if (right
== R2::TPioneerRight::DM
)
351 CMessage
msgout("EXEC_COMMAND");
352 std::string command
= NLMISC::toString("God %s %d", entityId
.toString().c_str(), 1);
353 msgout
.serial(command
);
354 CUnifiedNetwork::getInstance()->send("EGS",msgout
);
357 CMessage
msgout("EXEC_COMMAND");
358 std::string command
= NLMISC::toString("Invisible %s %d", entityId
.toString().c_str(), 1);
359 msgout
.serial(command
);
360 CUnifiedNetwork::getInstance()->send("EGS",msgout
);
363 CMessage
msgout("EXEC_COMMAND");
364 std::string command
= NLMISC::toString("Aggro %s %d", entityId
.toString().c_str(), 0);
365 msgout
.serial(command
);
366 CUnifiedNetwork::getInstance()->send("EGS",msgout
);
369 else if (right
== R2::TPioneerRight::Tester
)
372 CMessage
msgout("EXEC_COMMAND");
373 std::string command
= NLMISC::toString("God %s %d", entityId
.toString().c_str(), 0);
374 msgout
.serial(command
);
375 CUnifiedNetwork::getInstance()->send("EGS",msgout
);
378 CMessage
msgout("EXEC_COMMAND");
379 std::string command
= NLMISC::toString("Invisible %s %d", entityId
.toString().c_str(), 0);
380 msgout
.serial(command
);
381 CUnifiedNetwork::getInstance()->send("EGS",msgout
);
384 CMessage
msgout("EXEC_COMMAND");
385 std::string command
= NLMISC::toString("Aggro %s %d", entityId
.toString().c_str(), 1);
386 msgout
.serial(command
);
387 CUnifiedNetwork::getInstance()->send("EGS",msgout
);
395 void CAiWrapperServer::askBotDespawnNotification(NLMISC::CEntityId creatureId
, TAIAlias alias
)
397 NLNET::CMessage
msgout("ASK_BOT_DESPAWN_NOTIFICATION");
398 uint32 messageVersion
= 1;
399 msgout
.serial(messageVersion
, alias
, creatureId
);
400 CUnifiedNetwork::getInstance()->send("AIS",msgout
);