Sync_List XDBF::getSyncList(int et_type, unsigned long long identifier) { Sync_List list = { 0 }; // make sure the entry exists Entry *syncListTarget = getEntryById(identifier, et_type); if(syncListTarget == NULL) return list; int syncsInList = syncListTarget->length / 0x10; // initialize 'list' list.syncData = getSyncData(et_type, identifier * 2); list.listEntry = syncListTarget; list.entryCount = syncsInList; list.entries = new vector<Sync_Entry>(); list.entries = new vector<Sync_Entry>(syncsInList); // read all the entries in the sync list openedFile->setPosition(syncListTarget->address); for(int i = 0; i < syncsInList; i++) { list.entries->at(i).identifier = openedFile->readUInt64(); list.entries->at(i).syncId = openedFile->readUInt64(); } return list; }
/** * Query if the auton will initiate an event. * This will call up the LUA autons initEvent function which will * either return a 'null' string or data to build an * external event. * @return EventQueue::eEvent pointer to an external event or * a null pointer in which case nothing happens. */ std::unique_ptr<EventQueue::eEvent> AgentLuaInterface::takeStep() { if(removed) return NULL; if(nofile) return NULL; lua_settop(L,0); try { lua_getglobal(L, "_TakeStep"); if(lua_pcall(L,0,0,0) !=LUA_OK) { Output::Inst()->kprintf("<b><font color=\"brown\">Lua error on takeStep. %s, %s</font></b></>", filename.c_str() ,lua_tostring(L,-1)); Output::RunSimulation.store(false); return NULL; } if(moving) { movement(); } getSyncData(); return NULL; } catch(std::exception &e) { Output::Inst()->kprintf("<b><font color=\"red\">Exception on takeStep. %s, %s</font></b></>", filename.c_str() ,e.what()); Output::RunSimulation.store(false); } return NULL; }
void AgentLuaInterface::InitializeAgent() { if(removed) return; lua_settop(L,0); try { //init the LUA frog: lua_getglobal(L, "_InitializeAgent"); //Call the initAgent function (3 arguments, 0 results): if(lua_pcall(L,0,0,0)!=LUA_OK) { Output::Inst()->kprintf("<b><font color=\"brown\">Error on agent initialization, %s</font></b></>", lua_tostring(L,-1)); nofile = true; Output::Inst()->kprintf("Lua Agent disabled\n"); } } catch(std::exception& e) { Output::Inst()->kprintf("<b><font color=\"red\">Error on Agent initialization. %s, %s</font></b></>" , filename.c_str() ,e.what()); Output::RunSimulation = false; } if (gridmove == true) { GridMovement::addPos( int(posX*GridMovement::getScale()), int(posY*GridMovement::getScale()),ID); } getSyncData(); }
/** * Handler for internal events. * Will send all relevant event data to the LUA script which will then * process the event and either return a null string, or arguments * to initiate an external event. * @param event pointer to the internal event. * @return external event. */ std::unique_ptr<EventQueue::eEvent> AgentLuaInterface::handleEvent(std::unique_ptr<EventQueue::iEvent> eventPtr) { if(removed) return NULL; if(nofile) return NULL; //If the event isn't broadcast and the targetID is not mine lua_settop(L,0); //Output::Inst()->kprintf("handles event"); try { lua_getglobal(L,"_HandleEvent"); lua_pushnumber(L, eventPtr->event->posX); lua_pushnumber(L, eventPtr->event->posY); lua_pushnumber(L, eventPtr->event->posZ); lua_pushnumber(L, eventPtr->event->originID); lua_pushstring(L, eventPtr->event->desc.c_str()); lua_pushstring(L, eventPtr->event->luatable.c_str()); if(lua_pcall(L,6,0,0)!=LUA_OK) { Output::Inst()->kprintf("<b><font color=\"brown\">Error on event handling.%s, %s</font></b></>",filename.c_str(),lua_tostring(L,-1)); Output::RunSimulation.store(false); return NULL; } getSyncData(); return NULL; } catch(std::exception &e) { Output::Inst()->kprintf("<b><font color=\"red\">Exception on event handling.%s, %s</font></b></>",filename.c_str(), e.what()); Output::RunSimulation = false; } return NULL; }
AgentLuaInterface::AgentLuaInterface(int ID, double posX, double posY, double posZ, Sector *sector, std::string filename) : Agent(ID, posX, posY, posZ, sector), destinationX(posX), destinationY(posY),speed(1), moving(false),gridmove(false),filename(filename), nofile(false),removed(false),L(NULL) { desc = "LUA"; //Output::Inst()->kprintf("%f,%f", posX, posY); Output::Inst()->addGraphicAgent(ID, -1,-1); //Setup up the LUA stack: L = luaL_newstate(); if(L == NULL) { Output::Inst()->kprintf("<b><font color=\"brown\">A new Agent cannot be initialized. Lua(%s) is out of memory, Killing simulation</font></b></>", LUA_VERSION); Output::KillSimulation.store(true); removed = true; } else { luaL_openlibs(L); // Register the path to the Rana specific lua modules lua_getglobal(L, "package"); lua_getfield(L, -1, "path"); std::string cur_path = lua_tostring(L, -1); std::string module_path = Output::Inst()->RanaDir; //Output::Inst()->kdebug(module_path.c_str()); module_path.append("/modules/?.lua"); //Output::Inst()->kdebug(module_path.c_str()); cur_path.append(";"); cur_path.append(module_path); cur_path.append(";"); cur_path.append(Output::Inst()->AgentPath); cur_path.append("?.lua"); lua_pop(L,1); lua_pushstring(L, cur_path.c_str()); lua_setfield(L,-2,"path"); lua_pop(L,1); //set the global values for the agent: lua_pushnumber(L,ID); lua_setglobal(L,"ID"); lua_pushnumber(L,posX); lua_setglobal(L,"PositionX"); lua_pushnumber(L,posY); lua_setglobal(L, "PositionY"); lua_pushnumber(L, Phys::getMacroFactor()*Phys::getTimeRes()); lua_setglobal(L, "STEP_RESOLUTION"); lua_pushnumber(L, Phys::getTimeRes()); lua_setglobal(L, "EVENT_RESOLUTION"); lua_pushnumber(L, macroFactorMultiple); lua_setglobal(L, "StepMultiple"); lua_pushnumber(L, Phys::getEnvX()); lua_setglobal(L, "ENV_WIDTH"); lua_pushnumber(L, Phys::getEnvY()); lua_setglobal(L, "ENV_HEIGHT"); lua_pushnumber(L, Phys::getEnvZ()); lua_setglobal(L, "ENV_DEPTH"); lua_pushnumber(L, destinationX); lua_setglobal(L, "DestinationX"); lua_pushnumber(L,destinationY); lua_setglobal(L, "DestinationY"); lua_pushnumber(L, speed); lua_setglobal(L, "Speed"); lua_pushboolean(L, moving); lua_setglobal(L, "Moving"); lua_pushboolean(L, gridmove); lua_setglobal(L, "GridMove"); lua_pushnumber(L, mass); lua_setglobal(L, "Mass"); lua_pushnumber(L, charge); lua_setglobal(L, "Charge"); lua_pushnumber(L, radius); lua_setglobal(L, "Radius"); lua_pushnumber(L, color.red); lua_setglobal(L, "ColorRed"); lua_pushnumber(L, color.green); lua_setglobal(L, "ColorGreen"); lua_pushnumber(L, color.blue); lua_setglobal(L, "ColorBlue"); lua_pushnumber(L, color.alpha); lua_setglobal(L, "ColorAlpha"); //lua_newtable(L); //lua_setglobal(L, "EventTable"); //Register all the API functions: //Interface. lua_register(L, "l_debug", l_debug); lua_register(L, "l_print", l_print); lua_register(L, "say", l_debug); lua_register(L, "shout", l_print); //Physics and generator. lua_register(L, "l_speedOfSound", l_speedOfSound); lua_register(L, "l_distance", l_distance); lua_register(L, "l_currentTime",l_currentTime); lua_register(L, "l_currentTimeS", l_currentTimeS); lua_register(L, "l_generateEventID", l_generateEventID); lua_register(L, "l_getMacroFactor", l_getMacroFactor); lua_register(L, "l_getTimeResolution", l_getTimeResolution); lua_register(L, "l_getMersenneFloat", l_getMersenneFloat); lua_register(L, "l_getRandomFloat", l_getMersenneFloat); lua_register(L, "l_getRandomInteger", l_getMersenneInteger); lua_register(L, "l_getMersenneInteger", l_getMersenneInteger); //Map and movement. lua_register(L, "l_getEnvironmentSize", l_getEnvironmentSize); lua_register(L, "l_modifyMap", l_modifyMap); lua_register(L, "l_checkMap", l_checkMap); lua_register(L, "l_checkMapAndChange", l_checkMapAndChange); lua_register(L, "l_radialMapScan", l_radialMapScan); lua_register(L, "l_radialMapColorScan", l_radialMapColorScan); lua_register(L, "l_checkPosition", l_checkPosition); lua_register(L, "l_updatePosition", l_updatePosition); lua_register(L, "l_addPosition", l_addPosition); lua_register(L, "l_checkCollision", l_checkCollision); lua_register(L, "l_checkCollisionRadial", l_checkCollisionRadial); lua_register(L, "l_gridMove", l_gridMove); lua_register(L, "l_getMaskRadial", l_getMaskRadial); lua_register(L, "l_radialCollisionScan", l_radialCollisionScan); lua_register(L, "l_initializeGrid", l_initializeGrid); lua_register(L, "l_getGridScale", l_getGridScale); lua_register(L, "l_updatePositionIfFree", l_updatePositionIfFree); //Shared values. lua_register(L, "l_getSharedNumber", l_getSharedNumber); lua_register(L, "l_addSharedNumber",l_addSharedNumber); lua_register(L, "l_getSharedString", l_getSharedString); lua_register(L, "l_addSharedString", l_addSharedString); //Simulation core. lua_register(L, "l_getAgentPath", l_getAgentPath); lua_register(L, "l_getAgentPath", l_getAgentPath); lua_register(L, "l_stopSimulation", l_stopSimulation); lua_register(L, "l_removeAgent", l_removeAgent); lua_register(L, "l_addAgent", l_addAgent); //Agent. lua_register(L, "l_emitEvent", l_emitEvent); lua_register(L, "l_addGroup", l_addGroup); lua_register(L, "l_removeGroup", l_removeGroup); lua_register(L, "l_setStepMultiplier", l_setMacroFactorMultipler); lua_register(L, "l_changeAgentColor", l_changeAgentColor); std::string auxLib = Output::Inst()->RanaDir; auxLib.append("/modules/auxiliary.lua"); std::string settingsPath = filename; settingsPath.erase(settingsPath.end()-4,settingsPath.end()); settingsPath.append("_settings.lua"); if(luaL_loadfile(L, auxLib.c_str()) || lua_pcall(L,0,0,0)) { Output::Inst()->kprintf("<font color=\"red\">error : %s <\font>", lua_tostring(L, -1)); nofile = true; Output::Inst()->kprintf("Lua Agent disabled\n"); } if(luaL_loadfile(L, filename.c_str() ) || lua_pcall(L,0,0,0)) { Output::Inst()->kprintf("<font color=\"red\">error : %s <\font>", lua_tostring(L, -1)); nofile = true; Output::Inst()->kprintf("Lua Agent disabled\n"); } //if(sector != NULL) //Output::Inst()->kdebug("I belong to sector %i", sector->getID()); getSyncData(); //Call the Initialization function for the agent lua_settop(L,0); } moveFactor = Phys::getMacroFactor() * Phys::getTimeRes(); }