bool LuaTable::GetKeys(vector<int>& data) const { if (!PushTable()) { return false; } const int table = lua_gettop(L); for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) { if (lua_israwnumber(L, -2)) { const int value = lua_toint(L, -2); data.push_back(value); } } std::sort(data.begin(), data.end()); return true; }
bool LuaTable::GetMap(map<int, string>& data) const { if (!PushTable()) { return false; } const int table = lua_gettop(L); for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) { if (lua_israwnumber(L, -2) && lua_isstring(L, -1)) { const int key = lua_toint(L, -2); const string value = lua_tostring(L, -1); data[key] = value; } } return true; }
bool CLuaHandle::DefaultCommand(const CUnit* unit, const CFeature* feature, int& cmd) { LUA_CALL_IN_CHECK(L); lua_checkstack(L, 4); static const LuaHashString cmdStr("DefaultCommand"); if (!PushUnsyncedCallIn(cmdStr)) { return false; } int args = 0; if (unit) { HSTR_PUSH(L, "unit"); lua_pushnumber(L, unit->id); args = 2; } else if (feature) { HSTR_PUSH(L, "feature"); lua_pushnumber(L, feature->id); args = 2; } /* FIXME else if (groundPos) { HSTR_PUSH(L, "ground"); lua_pushnumber(L, groundPos->x); lua_pushnumber(L, groundPos->y); lua_pushnumber(L, groundPos->z); args = 4; } else { HSTR_PUSH(L, "selection"); args = 1; } */ // call the routine RunCallInUnsynced(cmdStr, args, 1); if (!lua_isnumber(L, 1)) { lua_pop(L, 1); return false; } cmd = lua_toint(L, -1); lua_pop(L, 1); return true; }
static int ligne(lua_State *L) { unsigned short n = lua_gettop(L); short int x,y,y1,x1,page,i,xx,dx,dy,err,sx,sy,e2; short int coul; x = (lua_toint(L, 1)-1); y = (lua_toint(L, 2)-1); x1 = (lua_toint(L, 3)-1); y1 = (lua_toint(L, 4)-1); if(n>=5) {page= lua_toint(L, 5);if(page<0 || page >5) page = 0;} //peut representer la couleur ou un numero de buffer else page = 0; dx = abs(x1-x), sx = x<x1 ? 1 : -1; dy = abs(y1-y), sy = y<y1 ? 1 : -1; if ( !NetB ) { if (n==6 || dx == 0 || dy == 0) { if (x>x1) {xx=x ; x=max(x1 ,0) ;x1= min(xx,127);} if (y>y1) {xx=y ; y=max (y1,0) ;y1= min (xx,63);} // donc y<=y1 et x<=x1 if (n == 6) { // rectangle coul = lua_toint(L,6); for( i=y ; i<=y1 ; i++){ Hline(x, x1 , i , coul); } if (page!=coul) { Hline(x,x1,y,page); Vline(x1,y,y1,page); Hline(x,x1,y1,page); Vline(x,y,y1,page); } } else if (dy==0) Hline(x,x1,y1,page); else Vline(x,y,y1,page); //dx == 0 return 0; } } err = (dx>dy ? dx : -dy)/2; for(;;){ if ((x == x&0x007F) && (y == y&0x003F)) // si on est dans les plages de l'écran (tester si pas de pb pour les valeurs négatives) { if(NetB) ML_pixel(x,y,1,VRAM[page]); else draw_write_pix(x,y,page); } if (x==x1 && y==y1) break; e2 = err; if (e2 >-dx) { err -= dy; x += sx; } if (e2 < dy) { err += dx; y += sy; } } if (NetB && page==0)ML_display_vram(VRAM[0]); return 0; }
static inline CUnit* ParseUnit(lua_State* L, const char* caller, int index) { if (!lua_isnumber(L, index)) { luaL_error(L, "%s(): Bad unitID", caller); } const int unitID = lua_toint(L, index); if ((unitID < 0) || (static_cast<size_t>(unitID) >= uh->MaxUnits())) { luaL_error(L, "%s(): Bad unitID: %i\n", caller, unitID); } CUnit* unit = uh->units[unitID]; if (unit == NULL) { return NULL; } if (!CanControlUnit(L, unit)) { return NULL; } return unit; }
int LuaUtils::ParseFacing(lua_State* L, const char* caller, int index) { if (lua_israwnumber(L, index)) { return std::max(0, std::min(3, lua_toint(L, index))); } else if (lua_israwstring(L, index)) { const string dir = StringToLower(lua_tostring(L, index)); if (dir == "s") { return 0; } if (dir == "e") { return 1; } if (dir == "n") { return 2; } if (dir == "w") { return 3; } if (dir == "south") { return 0; } if (dir == "east") { return 1; } if (dir == "north") { return 2; } if (dir == "west") { return 3; } luaL_error(L, "%s(): bad facing string", caller); } luaL_error(L, "%s(): bad facing parameter", caller); return 0; }
void CLuaRules::SetRulesParam(lua_State* L, const char* caller, int offset, vector<float>& params, map<string, int>& paramsMap) { const int index = offset + 1; const int valIndex = offset + 2; int pIndex = -1; if (lua_israwnumber(L, index)) { pIndex = lua_toint(L, index) - 1; } else if (lua_israwstring(L, index)) { const string pName = lua_tostring(L, index); map<string, int>::const_iterator it = paramsMap.find(pName); if (it != paramsMap.end()) { pIndex = it->second; } else { // create a new parameter pIndex = params.size(); paramsMap[pName] = params.size(); params.push_back(0.0f); // dummy value } } else { luaL_error(L, "Incorrect arguments to %s()", caller); } if ((pIndex < 0) || (pIndex >= (int)params.size())) { return; } if (!lua_isnumber(L, valIndex)) { luaL_error(L, "Incorrect arguments to %s()", caller); } params[pIndex] = lua_tofloat(L, valIndex); return; }
static CTeam* ParseTeam(lua_State* L, const char* caller, int index) { if (!lua_isnumber(L, index)) { luaL_error(L, "%s(): Bad teamID type\n", caller); } const int teamID = lua_toint(L, index); if ((teamID < 0) || (teamID >= MAX_TEAMS)) { luaL_error(L, "%s(): Bad teamID: %i\n", teamID); } CTeam* team = teamHandler->Team(teamID); if (team == NULL) { return NULL; } const CLuaHandle* lh = CLuaHandle::GetActiveHandle(); const int ctrlTeam = lh->GetCtrlTeam(); if (ctrlTeam < 0) { return lh->GetFullCtrl() ? team : NULL; } if (ctrlTeam != teamID) { luaL_error(L, "%s(): No permission to control team %i\n", caller, teamID); } return team; }
static CUnit* ParseUnit(lua_State* L, const char* caller, int luaIndex) { if (!lua_isnumber(L, luaIndex)) { luaL_error(L, "%s(): Bad unitID", caller); } const int unitID = lua_toint(L, luaIndex); if ((unitID < 0) || (unitID >= MAX_UNITS)) { luaL_error(L, "%s(): Bad unitID: %i\n", caller, unitID); } CUnit* unit = uh->units[unitID]; if (unit == NULL) { return NULL; } const CLuaHandle* lh = CLuaHandle::GetActiveHandle(); const int ctrlTeam = lh->GetCtrlTeam(); if (ctrlTeam < 0) { return lh->GetFullCtrl() ? unit : NULL; } if (ctrlTeam != unit->team) { luaL_error(L, "%s(): No permission to control unit %i\n", caller, unitID); } return unit; }
Command LuaUtils::ParseCommandTable(lua_State* L, const char* caller, int table) { // cmdID lua_rawgeti(L, table, 1); if (!lua_isnumber(L, -1)) { luaL_error(L, "%s(): bad command ID", caller); } const int id = lua_toint(L, -1); Command cmd(id); lua_pop(L, 1); // params lua_rawgeti(L, table, 2); if (!lua_istable(L, -1)) { luaL_error(L, "%s(): bad param table", caller); } const int paramTable = lua_gettop(L); for (lua_pushnil(L); lua_next(L, paramTable) != 0; lua_pop(L, 1)) { if (lua_israwnumber(L, -2)) { // avoid 'n' if (!lua_isnumber(L, -1)) { luaL_error(L, "%s(): bad param table entry", caller); } const float value = lua_tofloat(L, -1); cmd.PushParam(value); } } lua_pop(L, 1); // options lua_rawgeti(L, table, 3); ParseCommandOptions(L, caller, lua_gettop(L), cmd); lua_pop(L, 1); // XXX should do some sanity checking? return cmd; }
int LuaSyncedMoveCtrl::SetProgressState(lua_State* L) { CScriptMoveType* moveType = ParseScriptMoveType(L, __FUNCTION__, 1); if (moveType == NULL) return 0; const int args = lua_gettop(L); // number of arguments if (args < 2) { luaL_error(L, "Incorrect arguments to SetProgressState()"); } if (lua_isnumber(L, 2)) { const int state = lua_toint(L, 2); if ((state < AMoveType::Done) || (state > AMoveType::Failed)) { luaL_error(L, "SetProgressState(): bad state value (%d)", state); } moveType->progressState = (AMoveType::ProgressState) state; } else if (lua_isstring(L, 2)) { const string state = lua_tostring(L, 2); if (state == "done") { moveType->progressState = AMoveType::Done; } else if (state == "active") { moveType->progressState = AMoveType::Active; } else if (state == "failed") { moveType->progressState = AMoveType::Failed; } else { luaL_error(L, "SetProgressState(): bad state value (%s)", state.c_str()); } } else { luaL_error(L, "Incorrect arguments to SetProgressState()"); } return 0; }
int LuaUtils::ZlibDecompress(lua_State* L) { const int args = lua_gettop(L); // number of arguments if (args < 1) return luaL_error(L, "ZlibCompress: missign data argument"); size_t inLen; const char* inData = luaL_checklstring(L, 1, &inLen); long unsigned bufsize = 65000; if (args > 1 && lua_isnumber(L, 2)) bufsize = std::max(lua_toint(L, 2), 0); std::vector<boost::uint8_t> uncompressed(bufsize, 0); const int error = uncompress(&uncompressed[0], &bufsize, (const boost::uint8_t*)inData, inLen); if (error == Z_OK) { lua_pushlstring(L, (const char*)&uncompressed[0], bufsize); return 1; } else { return luaL_error(L, "Error while decompressing"); } }
void LuaMaterial::Parse( lua_State* L, const int tableIdx, std::function<void(lua_State*, int, LuaMatShader&)> ParseShader, std::function<void(lua_State*, int, LuaMatTexture&)> ParseTexture, std::function<GLuint(lua_State*, int)> ParseDisplayList ) { for (lua_pushnil(L); lua_next(L, tableIdx) != 0; lua_pop(L, 1)) { if (!lua_israwstring(L, -2)) continue; const std::string key = StringToLower(lua_tostring(L, -2)); // uniforms if (key.find("uniforms") != std::string::npos) { if (!lua_istable(L, -1)) continue; if (key.find("standard") != std::string::npos) { uniforms[LuaMatShader::LUASHADER_PASS_FWD].Parse(L, lua_gettop(L)); continue; } if (key.find("deferred") != std::string::npos) { uniforms[LuaMatShader::LUASHADER_PASS_DFR].Parse(L, lua_gettop(L)); continue; } // fallback uniforms[LuaMatShader::LUASHADER_PASS_FWD].Parse(L, lua_gettop(L)); continue; } // shaders if (key.find("shader") != std::string::npos) { if (key.find("standard") != std::string::npos) { ParseShader(L, -1, shaders[LuaMatShader::LUASHADER_PASS_FWD]); continue; } if (key.find("deferred") != std::string::npos) { ParseShader(L, -1, shaders[LuaMatShader::LUASHADER_PASS_DFR]); continue; } // fallback ParseShader(L, -1, shaders[LuaMatShader::LUASHADER_PASS_FWD]); continue; } // textures if (key.substr(0, 7) == "texunit") { if (key.size() < 8) continue; if (key[7] == 's') { // "texunits" = {[0] = string|table, ...} if (!lua_istable(L, -1)) continue; const int texTable = lua_gettop(L); for (lua_pushnil(L); lua_next(L, texTable) != 0; lua_pop(L, 1)) { if (!lua_israwnumber(L, -2)) continue; const unsigned int texUnit = lua_toint(L, -2); if (texUnit >= LuaMatTexture::maxTexUnits) continue; ParseTexture(L, -1, textures[texUnit]); } } else { // "texunitX" = string|table const unsigned int texUnit = atoi(key.c_str() + 7); if (texUnit >= LuaMatTexture::maxTexUnits) continue; ParseTexture(L, -1, textures[texUnit]); } continue; } // dlists if (key == "prelist") { preList = ParseDisplayList(L, -1); continue; } if (key == "postlist") { postList = ParseDisplayList(L, -1); continue; } // misc if (key == "order") { order = luaL_checkint(L, -1); continue; } if (key == "culling") { if (lua_isnumber(L, -1)) cullingMode = (GLenum)lua_tonumber(L, -1); continue; } if (key == "usecamera") { useCamera = lua_isboolean(L, -1) && lua_toboolean(L, -1); continue; } LOG_L(L_WARNING, "LuaMaterial: incorrect key \"%s\"", key.c_str()); } }
//---------------------------------------------------------------------------// // Funciones // //---------------------------------------------------------------------------// int DemoInit(lua_State *pLuaState) { g_LuaPlayer->DemoInit(lua_tofloat(pLuaState,1), lua_tofloat(pLuaState,2), lua_toint(pLuaState,3), lua_tobool(pLuaState,4)); return 0; }
static int waitlua(lua_State *L) { Sleep(20 * lua_toint(L, 1)); return 0; }
static int randomm(lua_State *L) { lua_pushnumber(L,(Randommm((lua_toint(L, 1))))); return 1; }
int CLuaHandleSynced::CallAsTeam(lua_State* L) { CLuaHandleSynced* lhs = GetActiveHandle(); if (lhs->teamsLocked) { luaL_error(L, "CallAsTeam() called when teams are locked"); } const int args = lua_gettop(L); if ((args < 2) || !lua_isfunction(L, 2)) { luaL_error(L, "Incorrect arguments to CallAsTeam()"); } // save the current access const bool prevFullCtrl = lhs->fullCtrl; const bool prevFullRead = lhs->fullRead; const int prevCtrlTeam = lhs->ctrlTeam; const int prevReadTeam = lhs->readTeam; const int prevReadAllyTeam = lhs->readAllyTeam; const int prevSelectTeam = lhs->selectTeam; // parse the new access if (lua_isnumber(L, 1)) { const int teamID = lua_toint(L, 1); if ((teamID < MinSpecialTeam) || (teamID >= teamHandler->ActiveTeams())) { luaL_error(L, "Bad teamID in SetCtrlTeam"); } // ctrl lhs->ctrlTeam = teamID; lhs->fullCtrl = (lhs->ctrlTeam == CEventClient::AllAccessTeam); // read lhs->readTeam = teamID; lhs->readAllyTeam = (teamID < 0) ? teamID : teamHandler->AllyTeam(teamID); lhs->fullRead = (lhs->readAllyTeam == CEventClient::AllAccessTeam); activeFullRead = lhs->fullRead; activeReadAllyTeam = lhs->readAllyTeam; // select lhs->selectTeam = teamID; } else if (lua_istable(L, 1)) { const int table = 1; for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) { if (!lua_israwstring(L, -2) || !lua_isnumber(L, -1)) { continue; } const string key = lua_tostring(L, -2); const int teamID = lua_toint(L, -1); if ((teamID < MinSpecialTeam) || (teamID >= teamHandler->ActiveTeams())) { luaL_error(L, "Bad teamID in SetCtrlTeam"); } if (key == "ctrl") { lhs->ctrlTeam = teamID; lhs->fullCtrl = (lhs->ctrlTeam == CEventClient::AllAccessTeam); } else if (key == "read") { lhs->readTeam = teamID; lhs->readAllyTeam = (teamID < 0) ? teamID : teamHandler->AllyTeam(teamID); lhs->fullRead = (lhs->readAllyTeam == CEventClient::AllAccessTeam); activeFullRead = lhs->fullRead; activeReadAllyTeam = lhs->readAllyTeam; } else if (key == "select") { lhs->selectTeam = teamID; } } } else { luaL_error(L, "Incorrect arguments to CallAsTeam()"); } // call the function const int funcArgs = lua_gettop(L) - 2; // protected call so that the permissions are always reverted const int error = lua_pcall(lhs->L, funcArgs, LUA_MULTRET, 0); // revert the permissions lhs->fullCtrl = prevFullCtrl; lhs->fullRead = prevFullRead; lhs->ctrlTeam = prevCtrlTeam; lhs->readTeam = prevReadTeam; lhs->readAllyTeam = prevReadAllyTeam; lhs->selectTeam = prevSelectTeam; activeFullRead = prevFullRead; activeReadAllyTeam = prevReadAllyTeam; if (error != 0) { logOutput.Print("error = %i, %s, %s\n", error, "CallAsTeam", lua_tostring(L, -1)); lua_error(L); } return lua_gettop(L) - 1; // the teamID/table is still on the stack }
static int UnitDefNewIndex(lua_State* L) { // not a default value, set it if (!lua_isstring(L, 2)) { lua_rawset(L, 1); return 0; } const char* name = lua_tostring(L, 2); ParamMap::const_iterator it = paramMap.find(name); // not a default value, set it if (it == paramMap.end()) { lua_rawset(L, 1); return 0; } const void* userData = lua_touserdata(L, lua_upvalueindex(1)); const UnitDef* ud = static_cast<const UnitDef*>(userData); // write-protected if (!gs->editDefsEnabled) { luaL_error(L, "Attempt to write UnitDefs[%d].%s", ud->id, name); return 0; } // Definition editing const DataElement& elem = it->second; const char* p = ((const char*)ud) + elem.offset; switch (elem.type) { case FUNCTION_TYPE: case READONLY_TYPE: { luaL_error(L, "Can not write to %s", name); return 0; } case INT_TYPE: { *((int*)p) = lua_toint(L, -1); return 0; } case BOOL_TYPE: { *((bool*)p) = lua_toboolean(L, -1); return 0; } case FLOAT_TYPE: { *((float*)p) = lua_tofloat(L, -1); return 0; } case STRING_TYPE: { *((string*)p) = lua_tostring(L, -1); return 0; } case ERROR_TYPE: { LOG_L(L_ERROR, "[%s] ERROR_TYPE for key \"%s\" in UnitDefs __newindex", __FUNCTION__, name); lua_pushnil(L); return 1; } } return 0; }
int CLuaHandleSynced::CallAsTeam(lua_State* L) { const int args = lua_gettop(L); if ((args < 2) || !lua_isfunction(L, 2)) { luaL_error(L, "Incorrect arguments to CallAsTeam()"); } // save the current access const bool prevFullCtrl = CLuaHandle::GetHandleFullCtrl(L); const bool prevFullRead = CLuaHandle::GetHandleFullRead(L); const int prevCtrlTeam = CLuaHandle::GetHandleCtrlTeam(L); const int prevReadTeam = CLuaHandle::GetHandleReadTeam(L); const int prevReadAllyTeam = CLuaHandle::GetHandleReadAllyTeam(L); const int prevSelectTeam = CLuaHandle::GetHandleSelectTeam(L); // parse the new access if (lua_isnumber(L, 1)) { const int teamID = lua_toint(L, 1); if ((teamID < CEventClient::MinSpecialTeam) || (teamID >= teamHandler->ActiveTeams())) { luaL_error(L, "Bad teamID in SetCtrlTeam"); } // ctrl CLuaHandle::SetHandleCtrlTeam(L, teamID); CLuaHandle::SetHandleFullCtrl(L, prevCtrlTeam == CEventClient::AllAccessTeam); // read CLuaHandle::SetHandleReadTeam(L, teamID); CLuaHandle::SetHandleReadAllyTeam(L, (teamID < 0) ? teamID : teamHandler->AllyTeam(teamID)); CLuaHandle::SetHandleFullRead(L, prevReadAllyTeam == CEventClient::AllAccessTeam); // select CLuaHandle::SetHandleSelectTeam(L, teamID); } else if (lua_istable(L, 1)) { const int table = 1; for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) { if (!lua_israwstring(L, -2) || !lua_isnumber(L, -1)) { continue; } const string key = lua_tostring(L, -2); const int teamID = lua_toint(L, -1); if ((teamID < CEventClient::MinSpecialTeam) || (teamID >= teamHandler->ActiveTeams())) { luaL_error(L, "Bad teamID in SetCtrlTeam"); } if (key == "ctrl") { CLuaHandle::SetHandleCtrlTeam(L, teamID); CLuaHandle::SetHandleFullCtrl(L, prevCtrlTeam == CEventClient::AllAccessTeam); } else if (key == "read") { CLuaHandle::SetHandleReadTeam(L, teamID); CLuaHandle::SetHandleReadAllyTeam(L, (teamID < 0) ? teamID : teamHandler->AllyTeam(teamID)); CLuaHandle::SetHandleFullRead(L, prevReadAllyTeam == CEventClient::AllAccessTeam); } else if (key == "select") { CLuaHandle::SetHandleSelectTeam(L, teamID); } } } else { luaL_error(L, "Incorrect arguments to CallAsTeam()"); } // call the function const int funcArgs = lua_gettop(L) - 2; // protected call so that the permissions are always reverted const int error = lua_pcall(L, funcArgs, LUA_MULTRET, 0); // revert the permissions CLuaHandle::SetHandleFullCtrl(L, prevFullCtrl); CLuaHandle::SetHandleFullRead(L, prevFullRead); CLuaHandle::SetHandleCtrlTeam(L, prevCtrlTeam); CLuaHandle::SetHandleReadTeam(L, prevReadTeam); CLuaHandle::SetHandleReadAllyTeam(L, prevReadAllyTeam); CLuaHandle::SetHandleSelectTeam(L, prevSelectTeam); if (error != 0) { LOG_L(L_ERROR, "error = %i, %s, %s", error, "CallAsTeam", lua_tostring(L, -1)); lua_error(L); } return lua_gettop(L) - 1; // the teamID/table is still on the stack }
int DemoAddEvent(lua_State *pLuaState) { TEvent *pEvent = NEW TEvent; pEvent->iEvent = lua_toint (pLuaState,1); pEvent->fTime = lua_tofloat(pLuaState,2); pEvent->fDuration = 0.f; switch (pEvent->iEvent) { // For FX case EV_FX_ATTACH: pEvent->iFX = lua_toint(pLuaState,3); pEvent->iSetVarInt = lua_toint(pLuaState,4); break; case EV_FX_UNATTACH: pEvent->iFX = lua_toint(pLuaState,3); break; case EV_FX_RESET: pEvent->iFX = lua_toint(pLuaState,3); break; case EV_FX_ADDFILTER: pEvent->iFX = lua_toint(pLuaState,3); strcpy_s(pEvent->pSetFilter, 32, lua_tostring(pLuaState,4)); break; case EV_FX_SETSPEED: case EV_FX_SETALPHA: case EV_FX_SETTIME: // (EV_FX_SETXXX, TIME, FX, VALUE, VALUETO, DURATION pEvent->iFX = lua_toint (pLuaState,3); pEvent->fDuration = lua_tofloat(pLuaState,6); pEvent->fSetVarFloat = lua_tofloat(pLuaState,4); if (pEvent->fDuration > 0.f) pEvent->fSetVarFloatTo = lua_tofloat(pLuaState,5); break; case EV_FX_SETBLEND: pEvent->iFX = lua_toint(pLuaState,3); pEvent->iSetVarInt = lua_toint(pLuaState,4); break; case EV_FX_SETVAR: // (EV_FX_SETVAR, TIME, FX, VARTYPE, VARSCOPE, VAROBJECT, VAR, VALUE, VALUETO, DURATION); pEvent->iFX = lua_toint(pLuaState,3); pEvent->iSetVarType = lua_toint(pLuaState,4); pEvent->iSetVarScope = g_LuaPlayer->GetVarScope (pEvent->iFX, lua_tostring(pLuaState,5)); pEvent->iSetVarObject= g_LuaPlayer->GetVarObject(pEvent->iFX, pEvent->iSetVarScope, lua_tostring(pLuaState,6)); pEvent->iSetVar = g_LuaPlayer->GetVarName (pEvent->iFX, pEvent->iSetVarScope, lua_tostring(pLuaState,7)); pEvent->fDuration = lua_tofloat (pLuaState,10); switch (pEvent->iSetVarType) { case SET_VAR_INT: pEvent->iSetVarInt = lua_toint (pLuaState,8); break; case SET_VAR_FLOAT: pEvent->fSetVarFloat = lua_tofloat(pLuaState,8); if (pEvent->fDuration > 0.f) pEvent->fSetVarFloatTo = lua_tofloat(pLuaState,9); case SET_VAR_BOOL: pEvent->bSetVarBool = lua_tobool (pLuaState,8); break; } break; // For FF case EV_FF_SETVAR: // (EV_FF_SETVAR, TIME, FX, VARTYPE, VAR, DURATION, VALUE, VALUETO); pEvent->iFX = lua_toint(pLuaState,3); pEvent->iSetVarType = lua_toint (pLuaState,4); strcpy_s(pEvent->pSetVar, 32, lua_tostring(pLuaState,5)); pEvent->fDuration = lua_tofloat (pLuaState,8); switch (pEvent->iSetVarType) { case SET_VAR_INT: pEvent->iSetVarInt = lua_toint (pLuaState,6); break; case SET_VAR_FLOAT: pEvent->fSetVarFloat = lua_tofloat(pLuaState,6); if (pEvent->fDuration > 0.f) pEvent->fSetVarFloatTo = lua_tofloat(pLuaState,7); break; case SET_VAR_BOOL: pEvent->bSetVarBool = lua_tobool (pLuaState,6); break; } break; } g_LuaPlayer->AddEvent(pEvent); return 0; }
void CLuaRules::Cob2Lua(const LuaHashString& name, const CUnit* unit, int& argsCount, int args[MAX_LUA_COB_ARGS]) { static int callDepth = 0; if (callDepth >= 16) { LOG_L(L_WARNING, "CLuaRules::Cob2Lua() call overflow: %s", name.GetString().c_str()); args[0] = 0; // failure return; } LUA_CALL_IN_CHECK(L); const int top = lua_gettop(L); if (!lua_checkstack(L, 1 + 3 + argsCount)) { LOG_L(L_WARNING, "CLuaRules::Cob2Lua() lua_checkstack() error: %s", name.GetString().c_str()); args[0] = 0; // failure lua_settop(L, top); return; } if (!name.GetGlobalFunc(L)) { LOG_L(L_WARNING, "CLuaRules::Cob2Lua() missing function: %s", name.GetString().c_str()); args[0] = 0; // failure lua_settop(L, top); return; } lua_pushnumber(L, unit->id); lua_pushnumber(L, unit->unitDef->id); lua_pushnumber(L, unit->team); for (int a = 0; a < argsCount; a++) { lua_pushnumber(L, args[a]); } // call the routine callDepth++; const int* oldArgs = currentCobArgs; currentCobArgs = args; const bool error = !RunCallIn(name, 3 + argsCount, LUA_MULTRET); currentCobArgs = oldArgs; callDepth--; // bail on error if (error) { args[0] = 0; // failure lua_settop(L, top); return; } // get the results const int retArgs = std::min(lua_gettop(L) - top, (MAX_LUA_COB_ARGS - 1)); for (int a = 1; a <= retArgs; a++) { const int index = (a + top); if (lua_isnumber(L, index)) { args[a] = lua_toint(L, index); } else if (lua_isboolean(L, index)) { args[a] = lua_toboolean(L, index) ? 1 : 0; } else if (lua_istable(L, index)) { lua_rawgeti(L, index, 1); lua_rawgeti(L, index, 2); if (lua_isnumber(L, -2) && lua_isnumber(L, -1)) { const int x = lua_toint(L, -2); const int z = lua_toint(L, -1); args[a] = PACKXZ(x, z); } else { args[a] = 0; } lua_pop(L, 2); } else { args[a] = 0; } } args[0] = 1; // success lua_settop(L, top); return; }