Added ai command setEquipment
[ryzomcore.git] / ryzom / server / src / simulation_service / com_lua_module.cpp
blob13a97fb024b7c84172727c7262fb507c3e389ba3
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 "com_lua_module.h"
19 #include "nel/misc/debug.h"
20 #include "nel/misc/path.h"
21 //#include "nel/misc/string.h"
23 #include "simulated_dmc.h"
24 //#include "palette.h"
25 //#include "property_accessor.h"
26 #include "simulated_client_edition_module.h"
27 #include "simulated_client_animation_module.h"
28 //#include "client_admin_module.h"
30 #include "r2_share/object.h"
31 #include "r2_share/r2_lua.h"
32 #include "r2_share/scenario.h"
34 #include <iostream>
35 #include <assert.h>
36 #include <vector>
37 #include <sstream>
38 #include <string>
40 // TMP TMP
41 //#include "lua_helper.h"
42 //#include "lua_ihm.h"
44 using namespace R2;
47 std::map<lua_State*, CComLuaModule*> CComLuaModule::_Instance;
49 #define CHECK_LUA_ARG_COUNT(count, funcName) if (args != count) { nlwarning("%d args required for lua function %s.", count, funcName); return 0; }
50 #define CHECK_LUA_ARG_COUNT_MAX(count, funcName) if (args > count) { nlwarning("Lua function %s accept at most %d arguments.", funcName, count); return 0; }
53 CComLuaModule::CComLuaModule(CDynamicMapClient* client, lua_State *luaState /*= NULL*/)
55 _Client = client;
57 if (!luaState)
59 #ifdef LUA_NEVRAX_VERSION
60 _LuaState = lua_open(NULL, NULL);
61 #else
62 _LuaState = lua_open();
63 #endif
64 _LuaOwnerShip = false;
65 luaopen_base(_LuaState);
66 luaopen_table(_LuaState);
67 luaopen_io(_LuaState);
68 luaopen_string(_LuaState);
69 luaopen_math(_LuaState);
70 luaopen_debug(_LuaState);
71 _LuaOwnerShip = true;
73 else
75 _LuaState = luaState;
76 _LuaOwnerShip = false;
79 _Instance[_LuaState] = this;
80 initLuaLib();
82 nlassert(_LuaState);
86 void CComLuaModule::initLuaLib()
88 const luaL_reg methods[] =
90 {"updateScenario", CComLuaModule::luaUpdateScenario},
91 {"requestUpdateRtScenario", CComLuaModule::luaRequestUpdateRtScenario},
92 {"requestCreateScenario", CComLuaModule::luaRequestCreateScenario},
93 {"requestMapConnection", CComLuaModule::luaRequestMapConnection},
94 {"requestReconnection", CComLuaModule::luaRequestReconnection},
95 {"requestListAdventure", CComLuaModule::luaRequestListAdventure},
97 {"requestInsertNode", CComLuaModule::luaRequestInsertNode},
98 {"requestInsertGhostNode", CComLuaModule::luaRequestInsertGhostNode},
99 {"requestSetNode", CComLuaModule::luaRequestSetNode},
100 {"requestEraseNode", CComLuaModule::luaRequestEraseNode},
101 {"requestMoveNode", CComLuaModule::luaRequestMoveNode},
102 {"requestGoLive", CComLuaModule::luaRequestGoLive},
103 {"requestStopLive", CComLuaModule::luaRequestStopLive},
104 {"requestStartAct", CComLuaModule::luaRequestStartAct},
105 {"requestStopAct", CComLuaModule::luaRequestStopAct},
106 {"requestStopAct", CComLuaModule::luaRequestStopAct},
107 {"requestCreatePrimitives", CComLuaModule::luaRequestCreatePrimitives},
108 {"requestSetWeather", CComLuaModule::luaRequestSetWeather},
110 {"requestTpPosition", CComLuaModule::luaRequestTpPosition},
111 {"requestDespawnEntity", CComLuaModule::luaRequestDespawnEntity},
112 {"requestDespawnGrp", CComLuaModule::luaRequestDespawnGrp},
114 {"requestStopAct", CComLuaModule::luaRequestStopAct},
115 {"newComponent", CComLuaModule::luaNewComponent},
116 {"requestTranslateFeatures", CComLuaModule::luaRequestTranslateFeatures},
117 {"registerGenerator", CComLuaModule::luaRegisterGenerator},
118 {"show", CComLuaModule::luaShow},
119 {"addPaletteElement", CComLuaModule::luaAddPaletteElement},
120 {"getPaletteElement", CComLuaModule::luaGetPaletteElement},
121 {"getPropertyValue", CComLuaModule::luaGetPropertyValue},
122 {"getScenarioObj",CComLuaModule::luaGetScenarioObj},
123 /*{"getPropertyList", CComLuaModule::luaGetPropertyList},*/
124 {"doFile2", CComLuaModule::luaDoFile2},
125 {"print", CComLuaModule::luaPrint},
126 {"save", CComLuaModule::luaSave},
127 {"load",CComLuaModule::luaLoad},
128 {"requestTalkAs",CComLuaModule::luaTalkAs},
129 {"requestStopTalk",CComLuaModule::luaStopTalkAs},
130 {"requestStringTable",CComLuaModule::luaRequestStringTable},
131 {"requestSetStringValue", CComLuaModule::luaRequestSetStringValue},
132 {"requestStringValue", CComLuaModule::luaRequestStringValue},
133 {"requestIdList", CComLuaModule::luaRequestIdList},
135 {"getNamespace", CComLuaModule::luaGetNamespace},
136 {"getIslandsLocation", CComLuaModule::luaGetIslandsLocation},
138 {0,0}
141 int initialStackSize = lua_gettop(_LuaState);
142 luaL_openlib(_LuaState, R2_LUA_PATH, methods, 0);
143 lua_settop(_LuaState, initialStackSize);
144 // load r2 features & components
145 doFile( "r2_core.lua" );
149 void CComLuaModule::doFile(const std::string& filename)
151 std::string filePath = NLMISC::CPath::lookup(filename, false, true);
152 if (filePath.empty())
154 nlwarning("Can't find %s", filename.c_str());
155 return;
157 std::string str = NLMISC::toString("dofile(\"%s\")", filePath.c_str() );
158 std::string errorMsg;
159 runLuaScript(str.c_str(), errorMsg);
162 bool CComLuaModule::runLuaScript(const std::string& script, std::string& erromsg)
164 const char *buff = script.c_str();
165 size_t size = script.size();
166 const char *name = script.c_str();
168 int status = luaL_loadbuffer(_LuaState, buff, size, name);
169 if (status == 0)
171 status = lua_pcall(_LuaState, 0, LUA_MULTRET, 0); /* call main */
174 if (status != 0)
176 erromsg = NLMISC::toString("%s\n", lua_tostring(_LuaState, -1));
177 lua_pop(_LuaState, 1);
178 return false;
181 return true;
183 sint CComLuaModule::luaDoFile2(lua_State* state)
185 sint args = lua_gettop(state);
186 nlassert(args == 1);
187 luaL_checktype(state, 1, LUA_TSTRING);
188 CComLuaModule* this2 = getInstance(state);
189 nlassert(this2);
190 std::string key( lua_tostring(state, 1) );
191 this2->doFile(key);
192 return 0;
195 //obsolete
196 void CComLuaModule::loadFeatures()
198 doFile("r2_core.lua");
201 CComLuaModule* CComLuaModule::getInstance(lua_State* state)
203 std::map<lua_State*, CComLuaModule*>::const_iterator found(_Instance.find(state));
204 if (found != _Instance.end())
206 return found->second;
208 return 0;
211 void CComLuaModule::callTranslateFeatures(CObject* scenario)
213 if (!scenario)
215 nlwarning("<CComLuaModule::callTranslateFeatures> called on NULL scenario");
216 return;
218 lua_getglobal(_LuaState, "r2");
219 lua_pushstring(_LuaState, "translateFeatures");
220 lua_gettable(_LuaState, -2);
221 setObjectToLua(_LuaState, scenario);
222 lua_call(_LuaState, 1, 0);
226 CObject* CComLuaModule::translateFeatures(CObject* hlScenario, std::string& errorMsg) const
228 if (!hlScenario)
230 errorMsg = "<CComLuaModule::translateFeatures> called on NULL scenario";
231 return 0;
233 lua_getglobal(_LuaState, "r2");
234 lua_pushstring(_LuaState, "doTranslateFeatures");
235 lua_gettable(_LuaState, -2);
236 setObjectToLua(_LuaState, hlScenario);
237 if ( lua_pcall(_LuaState, 1, 1, 0) !=0 )
239 errorMsg = NLMISC::toString( "error running function 'doTranslateFeatures': %s", lua_tostring(_LuaState, -1));
240 return 0;
242 CObject* ret = getObjectFromLua(_LuaState, -1);
243 return ret;
246 sint CComLuaModule::luaAddPaletteElement(lua_State* state)
249 luaL_checktype(state, 1, LUA_TSTRING);
250 luaL_checktype(state, 2, LUA_TTABLE);
251 CComLuaModule* this2 = getInstance(state);
252 nlassert(this2);
253 std::string key( lua_tostring(state, 1) );
254 CObject* object = this2->getObjectFromLua(state, 2);
255 this2->_Client->addPaletteElement(key, object);
256 return 0;
259 sint CComLuaModule::luaGetPropertyValue(lua_State* state)
261 sint args = lua_gettop(state);
262 nlassert(args == 2);
263 luaL_checktype(state, 1, LUA_TTABLE);
264 luaL_checktype(state, 2, LUA_TSTRING);
265 CComLuaModule* this2 = getInstance(state);
266 nlassert(this2);
267 CObject* object = this2->getObjectFromLua(state, 1);
268 std::string attrName( lua_tostring(state, 2) );
269 CObject* toRet = this2->_Client->getPropertyValue(object, attrName);
270 this2->setObjectToLua(state, toRet);
271 delete object;
272 return 1;
275 /*sint CComLuaModule::luaGetPropertyList(lua_State* state)
277 sint args = lua_gettop(state);
278 nlassert(args == 1);
279 luaL_checktype(state, 1, LUA_TTABLE);
280 CComLuaModule* this2 = getInstance(state);
281 nlassert(this2);
282 CObject* object = this2->getObjectFromLua(state, 1);
283 CObject* toRet = this2->_Client->getPropertyList(object);
284 this2->setObjectToLua(state, toRet);
285 delete toRet;
286 return 1;
288 sint CComLuaModule::luaGetPaletteElement(lua_State* state)
291 luaL_checktype(state, 1, LUA_TSTRING);
292 CComLuaModule* this2 = getInstance(state);
293 assert(this2);
294 std::string key( lua_tostring(state, 1) );
295 CObject* object = this2->_Client->getPaletteElement(key);
296 this2->setObjectToLua(state, object);
297 return 1;
300 sint CComLuaModule::luaRequestTranslateFeatures(lua_State* state)
303 CComLuaModule* this2 = getInstance(state);
304 nlassert(this2);
305 this2->_Client->requestTranslateFeatures();
306 return 0;
309 sint CComLuaModule::luaRequestGoLive(lua_State* state)
311 CComLuaModule* this2 = getInstance(state);
312 nlassert(this2);
313 this2->_Client->requestGoTest();
314 return 0;
318 sint CComLuaModule::luaRequestCreatePrimitives(lua_State* state)
320 CComLuaModule* this2 = getInstance(state);
321 nlassert(this2);
322 this2->_Client->getEditionModule().requestCreatePrimitives();
323 return 0;
327 sint CComLuaModule::luaRequestSetWeather(lua_State* state)
329 luaL_checktype(state, 1, LUA_TNUMBER);
330 uint16 weatherValue = (uint16) lua_tonumber(state, 1);
331 CComLuaModule* this2 = getInstance(state);
332 nlassert(this2);
333 this2->_Client->getAnimationModule().requestSetWeather(weatherValue);
334 return 0;
337 sint CComLuaModule::luaRequestStopLive(lua_State* state)
340 CComLuaModule* this2 = getInstance(state);
341 nlassert(this2);
342 this2->_Client->getEditionModule().requestStopTest();
343 return 0;
346 sint CComLuaModule::luaRequestStartAct(lua_State* state)
348 luaL_checktype(state, 1, LUA_TNUMBER);
349 uint32 actId(static_cast<uint32>(lua_tonumber(state, 1)));
350 CComLuaModule* this2 = getInstance(state);
351 nlassert(this2);
352 this2->_Client->getAnimationModule().requestStartAct(actId);
353 return 0;
356 sint CComLuaModule::luaRequestStopAct(lua_State* state)
358 CComLuaModule* this2 = getInstance(state);
359 nlassert(this2);
360 this2->_Client->getAnimationModule().requestStopAct();
361 return 0;
364 sint CComLuaModule::luaNewComponent(lua_State* state)
367 CComLuaModule* this2 = getInstance(state);
368 nlassert(this2);
370 luaL_checktype(state, 1, LUA_TSTRING);
371 std::string key(lua_tostring(state, 1));
372 CObject* object = this2->_Client->newComponent(key);
373 if (!object)
375 lua_pushnil(state);
376 return 1;
378 setObjectToLua(state, object);
379 delete object;
380 return 1;
383 sint CComLuaModule::luaRegisterGenerator(lua_State* state)
385 luaL_checktype(state, 1, LUA_TTABLE);
386 CComLuaModule* this2 = getInstance(state);
387 assert(this2);
388 CObject* object = this2->getObjectFromLua(state, 1);
389 this2->_Client->registerGenerator(object);
390 return 0;
393 sint CComLuaModule::luaShow(lua_State* state)
395 CComLuaModule* this2 = getInstance(state);
396 assert(this2);
397 this2->_Client->show();
398 return 0;
401 sint CComLuaModule::luaRequestUpdateRtScenario(lua_State* state)
403 CComLuaModule* this2 = getInstance(state);
404 assert(this2);
405 CObject* object = this2->getObjectFromLua(state, 1);
406 assert(object);
407 this2->_Client->requestUpdateRtScenario(object);
408 return 0;
411 sint CComLuaModule::luaRequestCreateScenario(lua_State* state)
413 luaL_checktype(state, 1, LUA_TTABLE);
414 CComLuaModule* this2 = getInstance(state);
415 nlassert(this2);
416 CObject* object = this2->getObjectFromLua(state, 1);
417 nlassert(object);
418 this2->_Client->requestCreateScenario(object);
419 delete object;
420 return 0;
423 sint CComLuaModule::luaPrint(lua_State* state)
426 sint args = lua_gettop(state);
427 CHECK_LUA_ARG_COUNT(1, "print");
429 // luaL_checktype(state, 1, LUA_TSTRING);
430 CComLuaModule* this2 = getInstance(state);
431 nlassert(this2);
432 CObject* object = this2->getObjectFromLua(state, 1);
433 if(!object)
435 nlinfo("nil");
436 return 0;
438 std::stringstream ss;
439 std::string s;
440 object->serialize(ss);
441 while (std::getline(ss, s))
443 nlinfo("%s", s.c_str());
447 //this2->_Client->requestCreateScenario(object);
448 delete object;
449 return 0;
454 sint CComLuaModule::luaRequestMapConnection(lua_State* state)
456 sint args = lua_gettop(state);
457 CHECK_LUA_ARG_COUNT(1, "requestMapConnection");
458 luaL_checktype(state, 1, LUA_TNUMBER);
460 uint32 adventureId = static_cast<uint32>( lua_tonumber(state, 1) );
462 CComLuaModule* this2 = getInstance(state);
463 nlassert(this2);
464 this2->_Client->getEditionModule().requestMapConnection(adventureId, true);
466 return 0;
471 sint CComLuaModule::luaRequestReconnection(lua_State* state)
473 sint args = lua_gettop(state);
474 CHECK_LUA_ARG_COUNT(0, "requestReconnection");
476 CComLuaModule* this2 = getInstance(state);
477 nlassert(this2);
478 this2->_Client->getEditionModule().requestReconnection();
480 return 0;
484 sint CComLuaModule::luaRequestListAdventure(lua_State* state)
486 sint args = lua_gettop(state);
487 CHECK_LUA_ARG_COUNT(0, "requestListAdventure");
489 CComLuaModule* this2 = getInstance(state);
490 nlassert(this2);
491 // AJM: this2->_Client->getAdminModule().requestListAdventure();
493 return 0;
497 sint CComLuaModule::requestInsertNode(lua_State* state, bool isGhost)
499 sint args = lua_gettop(state);
500 CHECK_LUA_ARG_COUNT(5, "requestInsertNode");
501 luaL_checktype(state, 1, LUA_TSTRING);
502 luaL_checktype(state, 2, LUA_TSTRING);
503 luaL_checktype(state, 3, LUA_TNUMBER);
504 luaL_checktype(state, 4, LUA_TSTRING);
505 luaL_checkany(state, 5); //TODO just string, number and table
507 std::string instanceId(lua_tostring(state, 1));
508 std::string attrName(lua_tostring(state, 2));
509 sint position(static_cast<sint>(lua_tonumber(state, 3)));
510 std::string key(lua_tostring(state, 4));
511 CObject* value = getObjectFromLua(state, 5);
512 // value->setGhost(isGhost);
513 CComLuaModule* this2 = getInstance(state);
514 nlassert(this2);
515 this2->_Client->requestInsertNode(
516 instanceId, attrName, position, key, value);
517 delete value;
518 return 0;
522 sint CComLuaModule::luaRequestInsertNode(lua_State* state)
524 return requestInsertNode(state, false);
527 sint CComLuaModule::luaRequestInsertGhostNode(lua_State* state)
529 return requestInsertNode(state, true);
532 sint CComLuaModule::luaRequestSetNode(lua_State* state)
534 sint args = lua_gettop(state);
535 CHECK_LUA_ARG_COUNT(3, "requestSetNode");
536 luaL_checktype(state, 1, LUA_TSTRING);
537 luaL_checktype(state, 2, LUA_TSTRING);
538 luaL_checkany(state, 3); //TODO just string, number and table
540 std::string instanceId(lua_tostring(state, 1));
541 std::string attrName(lua_tostring(state, 2));
542 CObject* value = getObjectFromLua(state, 3);
543 if (value == NULL)
545 nlwarning("requestSetNode : bad type for argument 3");
546 return 0;
548 CComLuaModule* this2 = getInstance(state);
549 nlassert(this2);
550 this2->_Client->requestSetNode( instanceId, attrName, value);
551 delete value;
552 return 0;
555 sint CComLuaModule::luaRequestEraseNode(lua_State* state)
557 sint args = lua_gettop(state);
558 CHECK_LUA_ARG_COUNT_MAX(3, "requestEraseNode")
559 luaL_checktype(state, 1, LUA_TSTRING);
560 if (args>1) { luaL_checktype(state, 2, LUA_TSTRING); }
561 if (args>2) { luaL_checknumber(state, 3); }
563 std::string instanceId(lua_tostring(state, 1));
564 std::string attrName = "";
565 sint position = -1;
566 if (args>1){ attrName = lua_tostring(state, 2);}
567 if (args>2){ position = static_cast<sint>(lua_tonumber(state, 3));}
568 CComLuaModule* this2 = getInstance(state);
569 nlassert(this2);
570 this2->_Client->requestEraseNode( instanceId, attrName, position);
571 return 0;
576 sint CComLuaModule::luaRequestMoveNode(lua_State* state)
578 sint args = lua_gettop(state);
579 CHECK_LUA_ARG_COUNT(6, "requestMoveNode");
580 luaL_checktype(state, 1, LUA_TSTRING);
581 luaL_checktype(state, 2, LUA_TSTRING);
582 luaL_checktype(state, 3, LUA_TNUMBER);
583 luaL_checktype(state, 4, LUA_TSTRING);
584 luaL_checktype(state, 5, LUA_TSTRING);
585 luaL_checktype(state, 6, LUA_TNUMBER);
588 luaL_checkany(state, 3); //TODO just string, number and table
590 std::string instanceId(lua_tostring(state, 1));
591 std::string attrName(lua_tostring(state, 2));
592 sint position = static_cast<sint>(lua_tonumber(state, 3));
594 std::string instanceId2(lua_tostring(state, 4));
595 std::string attrName2(lua_tostring(state, 5));
596 sint position2 = static_cast<sint>(lua_tonumber(state, 6));
598 CComLuaModule* this2 = getInstance(state);
599 nlassert(this2);
600 this2->_Client->requestMoveNode( instanceId, attrName, position, instanceId2, attrName2, position2);
601 return 0;
606 sint CComLuaModule::luaRequestTpPosition(lua_State* state)
608 sint args = lua_gettop(state);
609 CHECK_LUA_ARG_COUNT(2, "requestTpPosition");
610 luaL_checktype(state, 1, LUA_TNUMBER);
611 luaL_checktype(state, 2, LUA_TNUMBER);
613 float x = static_cast<float>(lua_tonumber(state, 1));
614 float y = static_cast<float>(lua_tonumber(state, 2));
615 CComLuaModule* this2 = getInstance(state);
616 nlassert(this2);
617 this2->_Client->getAnimationModule().requestTpPosition(x, y);
618 return 0;
622 sint CComLuaModule::luaRequestDespawnEntity(lua_State* state)
624 sint args = lua_gettop(state);
625 CHECK_LUA_ARG_COUNT(2, "requestDespawnEntity");
626 luaL_checktype(state, 1, LUA_TSTRING);
628 std::string npcId = lua_tostring(state, 1);
629 CComLuaModule* this2 = getInstance(state);
630 nlassert(this2);
631 this2->_Client->getAnimationModule().requestDespawnEntity(npcId);
632 return 0;
636 sint CComLuaModule::luaRequestDespawnGrp(lua_State* state)
638 sint args = lua_gettop(state);
639 CHECK_LUA_ARG_COUNT(2, "requestDespawnGrp");
640 luaL_checktype(state, 1, LUA_TSTRING);
642 std::string grpId = lua_tostring(state, 1);
643 CComLuaModule* this2 = getInstance(state);
644 nlassert(this2);
645 this2->_Client->getAnimationModule().requestDespawnEntity(grpId);
646 return 0;
649 void CComLuaModule::setObjectToLua(lua_State* state, CObject* object)
651 if (!object)
653 lua_pushnil(state);
654 return;
656 if ( object->isNumber() )
658 lua_pushnumber(state, object->toNumber());
659 return;
662 if (object->isRefId())
664 int initialStackSize = lua_gettop(state);
666 lua_pushvalue(state, LUA_GLOBALSINDEX); // _G
668 lua_pushstring(state, "r2"); // _G, "r2"
669 lua_gettable(state, -2); // G, r2
670 lua_pushstring(state, "RefIdMetatable"); // _G, r2, "RefIdMetatable"
671 lua_gettable(state, -2); // _G, r2, RefIdMetatable
672 lua_insert(state, -3); // RefIdMetatable, _G, r2
673 lua_pop(state, 2);
675 nlassert(lua_gettop(state) == initialStackSize + 1);
676 lua_newtable(state); // RefIdMetatable, {}
677 lua_pushstring(state, "Value"); // RefIdMetatable, {}, "Value"
678 lua_pushstring(state, object->toString().c_str()); // RefIdMetatable, {}, "Value", Value
679 lua_settable(state, -3); // RefIdMetatable, { "Value" = Value }
680 lua_insert(state, -2); // { "Value" = Value }, RefIdMetatable
681 lua_setmetatable(state, -2); // { "Value" = Value } + metatable
682 int finalStackSize = lua_gettop(state);
683 nlassert(finalStackSize == initialStackSize + 1);
684 return;
687 if ( object->isString() )
689 lua_pushstring(state, object->toString().c_str());
690 return;
693 if ( object->isTable() )
695 lua_newtable(state);
696 uint32 first = 0;
697 uint32 last = object->getSize();
698 uint32 arraySize = 0;
699 uint32 arrayIndex = 0;
701 for ( ; first != last; ++first)
703 if (object->getKey(first).empty()) { ++arraySize;}
706 std::vector<std::string> keys;
707 for (first=0 ; first != last; ++first)
709 std::string key = object->getKey(first);
710 CObject *value = object->getValue(first);
711 if (!key.empty())
713 lua_pushstring(state, key.c_str());
714 setObjectToLua(state, value);
715 lua_settable(state, -3);
716 keys.push_back(key);
718 else
720 ++arrayIndex;
721 setObjectToLua(state, value);
722 lua_rawseti(state, -2, arrayIndex);
726 if (0)
729 first = 0;
730 last = keys.size();
731 // if (!keys.empty())
733 lua_pushstring(state, "Keys");
734 lua_newtable(state);
735 for (; first != last; ++first)
737 lua_pushstring(state, keys[first].c_str());
738 lua_rawseti(state, -2, first+1);
740 luaL_setn(state, -1, last);
741 lua_settable(state, -3);
742 if (arraySize > 0)
744 luaL_setn(state, -1, arraySize);
749 else
751 nlwarning("error! object is not a string, not a number, not a table!");
752 lua_pushnil(state);
757 CObject* CComLuaModule::getObjectFromLua(lua_State* state, sint idx)
759 lua_pushvalue(state, idx);
761 // special case for RefID
762 if (lua_type(state, -1) == LUA_TTABLE)
764 if (lua_getmetatable(state, -1))
767 lua_pushvalue(state, LUA_GLOBALSINDEX); // obj, mt, _G
769 lua_pushstring(state, "r2"); // obj, mt, _G, "r2"
771 lua_gettable(state, -2); // obj, mt, _G, r2
773 lua_pushstring(state, "RefIdMetatable"); // obj, mt, _G, r2, "RefIdMetatable"
775 lua_gettable(state, -2); // obj, mt, _G, r2, RefIdMetatable
776 bool equal = lua_rawequal(state, -1, -4) != 0;
777 if (equal)
780 lua_pop(state, 4); // obj
782 lua_pushstring(state, "Value"); // obj, "Value"
784 lua_gettable(state, -2); // obj, value
785 CObject *result = 0;
786 if ( lua_isstring (state, -1) )
787 { const char* str = lua_tostring(state, -1);
788 if (str)
790 // result = new CObjectRefIdClient(str);
792 else
794 nlwarning("RefId error invalid string");
795 nlstop;
799 else
801 nlwarning("RefId not a string");
802 nlstop;
807 lua_pop(state, 2);
808 return result;
810 else
813 lua_pop(state, 4); // obj
818 switch (lua_type(state, -1))
820 case LUA_TNUMBER:
822 double value = lua_tonumber(state, -1);
823 lua_pop(state, 1);
824 return new CObjectNumber(value);
826 break;
828 case LUA_TSTRING:
830 std::string value = lua_tostring(state, -1);
831 lua_pop(state, 1);
832 return new CObjectString(value);
834 break;
836 case LUA_TTABLE:
838 CObjectTable* table = new CObjectTable(); //Client();
840 lua_pushnil(state);
841 while (lua_next(state, -2) != 0)
843 std::string key = "";
844 if ( lua_type(state, -2) == LUA_TSTRING)
846 key = lua_tostring(state, -2);
848 CObject* object = getObjectFromLua(state, -1);
849 if (object)
851 table->add(key, object);
853 lua_pop(state, 1);
855 lua_pop(state, 1);
856 table->sort();
857 return table;
859 break;
860 default:
861 lua_pop(state, 1);
862 // other types such as functions are ignored
863 return NULL;
864 break;
866 return 0;
870 CObject* CComLuaModule::loadLocal(const std::string& filename)
872 CObject* object = NULL;
873 if (lua_dofile(_LuaState, filename.c_str()) == 0)
875 lua_getglobal(_LuaState, "scenario");
877 if (lua_type(_LuaState, -1) == LUA_TTABLE)
879 object = getObjectFromLua(_LuaState);
882 if (!object)
884 nlwarning("Error while loading %s", filename.c_str());
886 return object;
890 bool CComLuaModule::load(const std::string& filename)
893 CObject* object = loadLocal(filename);
894 if (!object)
896 nlwarning("Error while loading %s", filename.c_str());
897 return false;
899 _Client->requestUploadScenario(object);
900 return true;
907 sint CComLuaModule::luaUpdateScenario(lua_State* state)
909 luaL_checktype(state, 1, LUA_TTABLE);
911 // lua_pushvalue(state, 1);
912 CObject* object = getObjectFromLua(state);
913 lua_pushliteral(state, "tmp1");
914 lua_pushliteral(state, "tmp1");
915 lua_pushliteral(state, "tmp1");
917 lua_getglobal(state, "write_table");
918 setObjectToLua(state, object);
919 lua_call(state, 1, 0);
921 // lua_pop(state, 1);
924 std::cout << "updateScenario" << std::endl;
925 CComLuaModule* this2 = getInstance(state);
926 nlassert(this2->_LuaState == state);
928 return 0;
931 CComLuaModule::~CComLuaModule()
933 if (_LuaOwnerShip)
935 lua_close(_LuaState);
941 sint CComLuaModule::luaLoad(lua_State* state)
943 luaL_checktype(state, 1, LUA_TSTRING);
944 CComLuaModule* this2 = getInstance(state);
945 nlassert(this2);
946 std::string filename( lua_tostring(state, 1) );
947 bool ok = this2->_Client->load(filename);
948 if (!ok)
950 lua_pushnil(state);
952 else
954 lua_pushnumber(state, 1);
956 return 1;
959 sint CComLuaModule::luaSave(lua_State* state)
961 luaL_checktype(state, 1, LUA_TSTRING);
962 CComLuaModule* this2 = getInstance(state);
963 nlassert(this2);
964 std::string filename( lua_tostring(state, 1) );
965 this2->_Client->save(filename);
966 return 0;
969 sint CComLuaModule::luaTalkAs(lua_State* state)
971 luaL_checktype(state, 1, LUA_TSTRING);
972 CComLuaModule* this2 = getInstance(state);
973 nlassert(this2);
974 std::string npcname( lua_tostring(state, 1) );
975 nlinfo(("luaTalkAs:: "+npcname).c_str());
976 this2->_Client->getAnimationModule().requestTalkAs(npcname);
977 return 0;
981 sint CComLuaModule::luaStopTalkAs(lua_State* state)
983 CComLuaModule* this2 = getInstance(state);
984 nlassert(this2);
985 this2->_Client->getAnimationModule().requestStopTalkAs();
986 return 0;
989 sint CComLuaModule::luaRequestStringTable(lua_State* state)
991 CComLuaModule* this2 = getInstance(state);
992 nlassert(this2);
993 this2->_Client->getAnimationModule().requestStringTable();
994 return 0;
997 sint CComLuaModule::luaRequestSetStringValue(lua_State* state)
999 luaL_checktype(state, 1, LUA_TSTRING);
1000 luaL_checktype(state, 2, LUA_TSTRING);
1001 CComLuaModule* this2 = getInstance(state);
1002 nlassert(this2);
1003 std::string localId( lua_tostring(state, 1) );
1004 std::string value(lua_tostring(state,2));
1005 this2->_Client->getAnimationModule().requestSetStringValue(localId,value);
1006 return 0;
1009 sint CComLuaModule::luaRequestStringValue(lua_State* state)
1011 luaL_checktype(state, 1, LUA_TSTRING);
1012 CComLuaModule* this2 = getInstance(state);
1013 nlassert(this2);
1014 std::string localId( lua_tostring(state, 1) );
1015 this2->_Client->getAnimationModule().requestStringValue(localId);
1016 return 0;
1019 sint CComLuaModule::luaRequestIdList(lua_State* state)
1021 CComLuaModule* this2 = getInstance(state);
1022 nlassert(this2);
1023 this2->_Client->getAnimationModule().requestIdList();
1024 return 0;
1029 sint CComLuaModule::luaGetScenarioObj(lua_State* state)
1031 CComLuaModule* this2 = getInstance(state);
1032 nlassert(this2);
1033 CObject* obj = this2->_Client->getCurrentScenario()->getHighLevel();
1034 setObjectToLua(state,obj);
1035 return 1;
1038 sint CComLuaModule::luaGetNamespace(lua_State* state)
1041 CComLuaModule* this2 = getInstance(state);
1042 nlassert(this2);
1043 TSessionId sessionId = this2->_Client->getEditionModule().getCurrentAdventureId();
1044 std::string value= NLMISC::toString("r2_%04d_",sessionId);
1045 lua_pushstring(state, value.c_str());
1046 return 1;
1050 sint CComLuaModule::luaGetIslandsLocation(lua_State* state)
1053 std::vector<std::string> locations;
1054 CComLuaModule* this2 = getInstance(state);
1055 nlassert(this2);
1056 // AJM: this2->_Client->getAdminModule().getIslandsLocation(locations);
1060 uint first(0), last( locations.size() ) ;
1063 // lua_pushstring(state, "Keys");
1064 lua_newtable(state);
1065 for (; first != last; ++first)
1067 lua_pushnumber(state, first+1);
1068 lua_pushstring(state, locations[first].c_str());
1069 lua_settable(state, -3);
1072 return 1;