void JediManager::checkForceStatusCommand(CreatureObject* creature) { Lua* lua = DirectorManager::instance()->getLuaInstance(); Reference<LuaFunction*> luaCheckForceStatusCommand = lua->createFunction(getJediManagerName(), "checkForceStatusCommand", 0); *luaCheckForceStatusCommand << creature; luaCheckForceStatusCommand->callFunction(); }
int TicketCollectorImplementation::handleObjectMenuSelect(CreatureObject* player, byte selectedID) { if (selectedID == 20) { player->executeObjectControllerAction(0x5DCD41A2); //boardShuttle } else if (selectedID == 193 && JediManager::instance()->getJediProgressionType() == JediManager::VILLAGEJEDIPROGRESSION) { Zone* thisZone = getZone(); if (thisZone == NULL) return 0; ManagedReference<PlanetManager*> planetManager = thisZone->getPlanetManager(); if (planetManager == NULL) return 0; PlanetTravelPoint* ptp = planetManager->getNearestPlanetTravelPoint(_this.getReferenceUnsafeStaticCast(), 64.f); if (ptp != NULL && ptp->isInterplanetary()) { PlayerObject* ghost = player->getPlayerObject(); if (ghost != NULL && ghost->hasActiveQuestBitSet(PlayerQuestData::FS_CRAFTING4_QUEST_03) && !ghost->hasCompletedQuestsBitSet(PlayerQuestData::FS_CRAFTING4_QUEST_03)) { Lua* lua = DirectorManager::instance()->getLuaInstance(); Reference<LuaFunction*> luaObtainData = lua->createFunction("FsCrafting4", "obtainSatelliteData", 0); *luaObtainData << player; *luaObtainData << _this.getReferenceUnsafeStaticCast(); luaObtainData->callFunction(); } } } return 0; }
int LuaContainerComponent::canAddObject(SceneObject* sceneObject, SceneObject* object, int containmentType, String& errorDescription) { if (sceneObject == object) { errorDescription = "@container_error_message:container02"; //You cannot add something to itself. return TransferErrorCode::CANTADDTOITSELF; } Lua* lua = DirectorManager::instance()->getLuaInstance(); LuaFunction runMethod(lua->getLuaState(), luaClassName, "canAddObject", 1); runMethod << sceneObject; runMethod << object; runMethod << containmentType; runMethod.callFunction(); int result = lua_tointeger(lua->getLuaState(), -1); lua_pop(lua->getLuaState(), 1); if (result == -1) result = ContainerComponent::canAddObject(sceneObject, object, containmentType, errorDescription); return result; }
void PetManagerImplementation::loadLuaConfig() { info("Loading configuration file.", true); Lua* lua = new Lua(); lua->init(); lua->runFile("scripts/managers/pet_manager.lua"); LuaObject luaObject = lua->getGlobalObject("mountSpeedData"); if (luaObject.isValidTable()) { for (int i = 1; i <= luaObject.getTableSize(); ++i) { LuaObject speedData = luaObject.getObjectAt(i); if (speedData.isValidTable()) { String filename = speedData.getStringAt(1); float runSpeed = speedData.getFloatAt(2); float gallopSpeedMultiplier = speedData.getFloatAt(3); int gallopDuration = speedData.getIntAt(4); int gallopCooldown = speedData.getIntAt(5); Reference<MountSpeedData*> data = new MountSpeedData(filename, runSpeed, gallopSpeedMultiplier, gallopDuration, gallopCooldown); mountSpeedData.add(data); } speedData.pop(); } } luaObject.pop(); info("Loaded " + String::valueOf(mountSpeedData.size()) + " mount speeds.", true); }
bool LuaBehavior::checkConditions(AiAgent* agent) { #ifdef AI_DEBUG Time timer; #endif // Use DirectorManager in order to have access to AiAgent Lua* lua = DirectorManager::instance()->getLuaInstance(); // TODO (dannuic): should I check for valid table here? //agent->info(className, true); LuaFunction runMethod(lua->getLuaState(), className, "checkConditions", 1); runMethod << agent; runMethod.callFunction(); //agent->info(className + " check...", true); bool result = lua_toboolean(lua->getLuaState(), -1); lua_pop(lua->getLuaState(), 1); #ifdef AI_DEBUG String key = className + "::checkConditions"; agent->incrementLuaCall(key); agent->addToLuaTime(key, Time().getMikroTime() - timer.getMikroTime()); #endif return result; }
float LuaBehavior::end(AiAgent* agent) { #ifdef AI_DEBUG Time timer; #endif // Use DirectorManager in order to have access to AiAgent Lua* lua = DirectorManager::instance()->getLuaInstance(); // TODO (dannuic): should I check for valid table here? LuaFunction runMethod(lua->getLuaState(), className, "terminate", 1); runMethod << agent; //agent->info(className + " end...", true); // ZoneServer* zoneServer = agent->getZoneServer(); // ChatManager* chatManager = zoneServer->getChatManager(); // chatManager->broadcastMessage(agent, className + " end...", 0, 0, 0); runMethod.callFunction(); float result = lua_tonumber(lua->getLuaState(), -1); lua_pop(lua->getLuaState(), 1); #ifdef AI_DEBUG String key = className + "::terminate"; agent->incrementLuaCall(key); agent->addToLuaTime(key, Time().getMikroTime() - timer.getMikroTime()); #endif return result; }
void JediManager::onPlayerLoggedOut(CreatureObject* creature) { Lua* lua = DirectorManager::instance()->getLuaInstance(); Reference<LuaFunction*> luaOnPlayerLoggedOut = lua->createFunction(getJediManagerName(), "onPlayerLoggedOut", 0); *luaOnPlayerLoggedOut << creature; luaOnPlayerLoggedOut->callFunction(); }
int LuaBehavior::interrupt(AiAgent* agent, SceneObject* source, int64 msg) { #ifdef AI_DEBUG Time timer; #endif Lua* lua = DirectorManager::instance()->getLuaInstance(); LuaFunction messageFunc(lua->getLuaState(), className, "interrupt", 1); messageFunc << agent; messageFunc << source; //arg1 messageFunc << msg; //arg2 messageFunc.callFunction(); int result = lua_tointeger(lua->getLuaState(), -1); lua_pop(lua->getLuaState(), 1); #ifdef AI_DEBUG String key = className; if (msg == 17) key += "::startAwarenessInterrupt"; else if (msg == 31) key += "::startCombatInterrupt"; agent->incrementLuaCall(key); agent->addToLuaTime(key, Time().getMikroTime() - timer.getMikroTime()); #endif //1 remove observer, 0 keep observer return result; }
int main() { Lua luaInstance; std::stringstream ss; auto globalTable = luaInstance.GetGlobalEnvironment(); auto myOwnPrint = luaInstance.CreateYieldingFunction<void(std::string)> ( [&](std::string str) { ss << str; } ); globalTable.Set("myownprint", myOwnPrint); luaInstance.LoadStandardLibraries(); auto cr = luaInstance.CreateCoroutine(); auto err = cr.RunScript( " myownprint 'hello '\n" " myownprint 'hello2 '\n" " myownprint 'hello3 '\n" ); while (cr.CanResume()) { ss << "yield "; auto err = cr.Resume(); } auto resstr = ss.str(); return resstr.compare("hello yield hello2 yield hello3 yield "); }
int main() { Lua lua; lua.LoadStandardLibraries(); auto global = lua.GetGlobalEnvironment(); lua.RunScript(R"( function x() return "meow" end )"); try { global.Get< LuaCoroutine >("x"); return 1; } catch(LuaError e) { std::cout << e.GetMessage() << std::endl; return e.GetMessage() != "Error: thread expected, got function\nError: bad argument #-1 (thread expected, got function)\n"; } return 2; }
LuaTable LoadLuaConfiguration(std::string file) { std::string script = LoadAllText(file); Lua lua; auto global = lua.GetGlobalEnvironment(); std::set<std::string> includedFiles; auto includeonce = lua.CreateFunction<void(std::string)>([&](std::string includedFile) { if (includedFiles.find(includedFile) == includedFiles.end()) { includedFiles.insert(includedFile); lua.RunScript(LoadAllText(includedFile)); } }); auto include = lua.CreateFunction<void(std::string)>([&](std::string includedFile) { includedFiles.insert(includedFile); lua.RunScript(LoadAllText(includedFile)); }); global.Set("include", include); global.Set("include_once", includeonce); lua.RunScript(script); return global; }
int main() { Lua lua; auto global = lua.GetGlobalEnvironment(); auto newCar = lua.CreateFunction<LuaTable()>( [&]() -> LuaTable { LuaTable table = lua.CreateTable(); table.Set("type", "renault"); return table; } ); global.Set("newCar", newCar); // Run the script that chooses a function to return lua.RunScript( "x = newCar()['type']" ); auto type = global.Get<std::string>("x"); return type.compare("renault"); }
void JediManager::onFSTreeCompleted(CreatureObject* creature, String branch) { Lua* lua = DirectorManager::instance()->getLuaInstance(); Reference<LuaFunction*> luaStartTask = lua->createFunction(getJediManagerName(), "onFSTreeCompleted", 0); *luaStartTask << creature; *luaStartTask << branch; luaStartTask->callFunction(); }
int main() { Lua lua; auto table = lua.CreateTable(); return 0; }
void JediManager::useItem(SceneObject* item, const int itemType, CreatureObject* creature) { Lua* lua = DirectorManager::instance()->getLuaInstance(); Reference<LuaFunction*> luaUseItem = lua->createFunction(getJediManagerName(), "useItem", 0); *luaUseItem << item; *luaUseItem << itemType; *luaUseItem << creature; luaUseItem->callFunction(); }
void LuaSystem::update(const std::set<Entity*>* entities) { std::set<Entity*>::iterator it; for (it = entities->begin(); it != entities->end(); ++it) { Entity* e = *it; Lua* lua = (Lua*)e->getComponent(LUA); lua->runScript(); } }
void SkillManager::loadFromLua() { Lua* lua = new Lua(); lua->init(); lua_register(lua->getLuaState(), "includeFile", &includeFile); lua_register(lua->getLuaState(), "addSkill", &addSkill); lua->runFile("scripts/skills/serverobjects.lua"); delete lua; }
void lua_hello_world_example() { Lua lua; lua.globals() ( "print", &print ) ; const char* script = "print('Hello World!\\n')"; lua.call( script, script + strlen(script), "hello_world" ).end(); }
void SkillManager::loadLuaConfig() { Lua* lua = new Lua(); lua->init(); lua->runFile("scripts/managers/skill_manager.lua"); apprenticeshipEnabled = lua->getGlobalByte("apprenticeshipEnabled"); delete lua; lua = NULL; }
int main() { Lua lua; auto global = lua.GetGlobalEnvironment(); auto userdata = lua.CreateUserdata<Data>(new Data(555)); global.Set("data", userdata); auto lud = global.Get< LuaLightUserdata<Data> >("data"); return lud->GetValue() != 555; }
void FactionManager::loadLuaConfig() { info("Loading config file.", true); Lua* lua = new Lua(); lua->init(); lua_register(lua->getLuaState(), "addFaction", addFaction); //Load the faction manager lua file. lua->runFile("scripts/managers/faction_manager.lua"); delete lua; lua = NULL; }
int main(int argc, char** argv) { FreeImage_Initialise(); gLua.init(); gConfig.init(); gDatabase.init(); gLog.init(); Window win; win.loadZoneModel(argc > 1 ? argv[1] : "gfaydark"); for (;;) { gTimerPool.check(); if (!win.mainLoop()) break; gTemp.reset(); Clock::sleepMilliseconds(25); } gLog.signalClose(); gLog.waitUntilClosed(); FreeImage_DeInitialise(); return 0; }
int main() { Lua lua; auto global = lua.GetGlobalEnvironment(); // Write a function in Lua lua.RunScript( "function addTwo(a)\n" " return a+2\n" "end\n" ); auto addTwo = global.Get< LuaFunction<int(int)> >("addTwo"); return addTwo.Invoke(5) != 7; }
void VendorManager::loadLuaVendors() { info("Loading Vendor Options..."); Lua* lua = new Lua(); lua->init(); lua->runFile("scripts/managers/vendor_manager.lua"); LuaObject menu = lua->getGlobalObject("VendorMenu"); rootNode = new VendorSelectionNode(); rootNode->parseFromLua(menu); menu.pop(); }
int main() { Lua lua; auto global = lua.GetGlobalEnvironment(); auto add = lua.CreateFunction<int(int,int)>([&](int a, int b) -> int { return a + b; }); global.Set("add", add); // Run the script that chooses a function to return lua.RunScript( "x = add(3,5)" ); auto num = global.Get<int>("x"); return num != 8; }
int main() { Lua lua; SomeClass* sc = new SomeClass(1); auto userdata1 = lua.CreateUserdata<SomeClass>(sc); using namespace std::placeholders; userdata1.Set("GetNumber", lua.CreateFunction< int() >( std::bind(&SomeClass::GetNumber, sc) )); auto global = lua.GetGlobalEnvironment(); global.Set("someclass", userdata1); lua.RunScript("x = someclass.GetNumber()"); return 1 - global.Get<int>("x"); }
void SceneObjectImplementation::setContainerComponent(const String& name) { containerComponent = ComponentManager::instance()->getComponent<ContainerComponent*>(name); if (containerComponent == NULL) { Lua* lua = DirectorManager::instance()->getLuaInstance(); LuaObject test = lua->getGlobalObject(name); if (test.isValidTable()) { containerComponent = new LuaContainerComponent(name); info("New Lua ContainerComponent created: '" + name + "' for " + templateObject->getFullTemplateString()); ComponentManager::instance()->putComponent(name, containerComponent); } else { error("ContainerComponent not found: '" + name + "' for " + templateObject->getFullTemplateString()); } test.pop(); } }
int main(int argc, char* argv[]) { const char* ui = "test.lua"; if ( argc > 1 ) { ui = argv[1]; } /* Setup callbacks. */ mgtk_assert_handler( mgtk_assert_dialog ); /* 'Link' up mgtk library stubs to these implementations. */ mgtk_link_import("mgtk_print", (void*)printf); //mgtk_link_import("mgtk_callback_get_image_data_rgb24", (void*)freyja_callback_get_image_data_rgb24); //mgtk_link_import("mgtk_get_pixmap_filename", (void*)freyja_get_pixmap_filename); //mgtk_link_import("mgtk_rc_map", (void*)freyja_rc_map); //mgtk_link_import("mgtk_get_resource_path", (void*)freyja_get_resource_path_callback); /* Hookup resource to event system */ //mgtk_link_import("mgtk_handle_color", (void*)freyja_handle_color); mgtk_link_import("mgtk_handle_application_window_close", (void*)mgtk_test_handle_application_window_close); mgtk_link_import("mgtk_handle_command", (void*)mgtk_test_handle_command); mgtk_link_import("mgtk_handle_command2i", (void*)mgtk_test_handle_command2i); //mgtk_link_import("mgtk_handle_event1u", (void*)freyja_handle_event1u); //mgtk_link_import("mgtk_handle_event1f", (void*)freyja_handle_event1f); //mgtk_link_import("mgtk_handle_gldisplay", (void*)freyja_handle_gldisplay); //mgtk_link_import("mgtk_handle_glresize", (void*)freyja_handle_glresize); //mgtk_link_import("mgtk_handle_key_press", (void*)freyja_handle_key_press); //mgtk_link_import("mgtk_handle_motion", (void*)freyja_handle_motion); //mgtk_link_import("mgtk_handle_mouse", (void*)freyja_handle_mouse); //mgtk_link_import("mgtk_handle_resource_start", (void*)mgtk_test_handle_resource_start); //mgtk_link_import("mgtk_handle_text_array", (void*)freyja_handle_text_array); //mgtk_link_import("mgtk_handle_text", (void*)freyja_handle_text); ResourceEvent::setResource( &gResource ); ResourceEventCallback::add("eShutdown", mgtk_test_shutdown); /* Export Lua mgtk functions to libfreyja VM. */ mgtk_lua_register_functions( gLua ); /* Setup the user interface. */ mgtk_init(argc, argv); /* Load the UI script here instead of using the * mgtk_handle_resource_start() callback. */ if ( !gLua.ExecuteFile( ui ) ) { mgtk_print("Failed to load test.lua\n"); } /* Start the user interface. */ mgtk_start(); return 0; }
void poll(Lua& L) { for (unsigned i=0; i<files.size(); i++) { const std::string& file = files[i]; ResourceManager::FileInfo& info = rm[file]; if (info.loaded) { info.loaded = 0; printf("running %s\n", info.path.c_str()); L.dofile(info.path); } } }
void MissionNpcSpawnMap::loadSpawnPointsFromLua() { info("Loading NPC mission spawn points.", true); try { Lua* lua = new Lua(); lua->init(); lua->runFile("scripts/managers/mission/mission_manager.lua"); LuaObject cities = lua->getGlobalObject("cities"); spawnMap.addCities(&cities); LuaObject universeObject = lua->getGlobalObject("universe"); spawnMap.readObject(&universeObject); } catch (Exception& e) { info(e.getMessage(), true); } }