void LuaUtils::PushCommandDesc(lua_State* L, const CommandDescription& cd) { const int numParams = cd.params.size(); const int numTblKeys = 12; lua_checkstack(L, 1 + 1 + 1 + 1); lua_createtable(L, 0, numTblKeys); HSTR_PUSH_NUMBER(L, "id", cd.id); HSTR_PUSH_NUMBER(L, "type", cd.type); HSTR_PUSH_STRING(L, "name", cd.name); HSTR_PUSH_STRING(L, "action", cd.action); HSTR_PUSH_STRING(L, "tooltip", cd.tooltip); HSTR_PUSH_STRING(L, "texture", cd.iconname); HSTR_PUSH_STRING(L, "cursor", cd.mouseicon); HSTR_PUSH_BOOL(L, "hidden", cd.hidden); HSTR_PUSH_BOOL(L, "disabled", cd.disabled); HSTR_PUSH_BOOL(L, "showUnique", cd.showUnique); HSTR_PUSH_BOOL(L, "onlyTexture", cd.onlyTexture); HSTR_PUSH(L, "params"); lua_createtable(L, 0, numParams); for (int p = 0; p < numParams; p++) { lua_pushsstring(L, cd.params[p]); lua_rawseti(L, -2, p + 1); } // CmdDesc["params"] = {[1] = "string1", [2] = "string2", ...} lua_settable(L, -3); }
/// pushes 7 items on the stack static void PushUnitAndCommand(lua_State* L, const CUnit* unit, const Command& cmd) { // push the unit info lua_pushnumber(L, unit->id); lua_pushnumber(L, unit->unitDef->id); lua_pushnumber(L, unit->team); // push the command id lua_pushnumber(L, cmd.id); // push the params list lua_newtable(L); for (int p = 0; p < (int)cmd.params.size(); p++) { lua_pushnumber(L, p + 1); lua_pushnumber(L, cmd.params[p]); lua_rawset(L, -3); } HSTR_PUSH_NUMBER(L, "n", cmd.params.size()); // push the options table lua_newtable(L); HSTR_PUSH_NUMBER(L, "coded", cmd.options); HSTR_PUSH_BOOL(L, "alt", !!(cmd.options & ALT_KEY)); HSTR_PUSH_BOOL(L, "ctrl", !!(cmd.options & CONTROL_KEY)); HSTR_PUSH_BOOL(L, "shift", !!(cmd.options & SHIFT_KEY)); HSTR_PUSH_BOOL(L, "right", !!(cmd.options & RIGHT_MOUSE_KEY)); HSTR_PUSH_BOOL(L, "meta", !!(cmd.options & META_KEY)); // push the command tag lua_pushnumber(L, cmd.tag); }
bool CLuaHandle::AddBasicCalls() { HSTR_PUSH(L, "Script"); lua_newtable(L); { HSTR_PUSH_CFUNC(L, "Kill", KillActiveHandle); HSTR_PUSH_CFUNC(L, "GetName", CallOutGetName); HSTR_PUSH_CFUNC(L, "GetSynced", CallOutGetSynced); HSTR_PUSH_CFUNC(L, "GetFullCtrl", CallOutGetFullCtrl); HSTR_PUSH_CFUNC(L, "GetFullRead", CallOutGetFullRead); HSTR_PUSH_CFUNC(L, "GetCtrlTeam", CallOutGetCtrlTeam); HSTR_PUSH_CFUNC(L, "GetReadTeam", CallOutGetReadTeam); HSTR_PUSH_CFUNC(L, "GetReadAllyTeam", CallOutGetReadAllyTeam); HSTR_PUSH_CFUNC(L, "GetSelectTeam", CallOutGetSelectTeam); HSTR_PUSH_CFUNC(L, "GetGlobal", CallOutGetGlobal); HSTR_PUSH_CFUNC(L, "GetRegistry", CallOutGetRegistry); HSTR_PUSH_CFUNC(L, "GetCallInList", CallOutGetCallInList); // special team constants HSTR_PUSH_NUMBER(L, "NO_ACCESS_TEAM", CEventClient::NoAccessTeam); HSTR_PUSH_NUMBER(L, "ALL_ACCESS_TEAM", CEventClient::AllAccessTeam); //FIXME LuaArrays::PushEntries(L); } lua_rawset(L, -3); // extra math utilities lua_getglobal(L, "math"); LuaBitOps::PushEntries(L); lua_pop(L, 1); return true; }
bool CLuaRules::CommandFallback(const CUnit* unit, const Command& cmd) { if (!haveCommandFallback) { return true; // the call is not defined } LUA_CALL_IN_CHECK(L); lua_checkstack(L, 9); static const LuaHashString cmdStr("CommandFallback"); if (!cmdStr.GetGlobalFunc(L)) { return true; // the call is not defined } // push the unit info lua_pushnumber(L, unit->id); lua_pushnumber(L, unit->unitDef->id); lua_pushnumber(L, unit->team); // push the command id lua_pushnumber(L, cmd.id); // push the params list lua_newtable(L); for (int p = 0; p < (int)cmd.params.size(); p++) { lua_pushnumber(L, p + 1); lua_pushnumber(L, cmd.params[p]); lua_rawset(L, -3); } HSTR_PUSH_NUMBER(L, "n", cmd.params.size()); // push the options table lua_newtable(L); HSTR_PUSH_NUMBER(L, "coded", cmd.options); HSTR_PUSH_BOOL(L, "alt", !!(cmd.options & ALT_KEY)); HSTR_PUSH_BOOL(L, "ctrl", !!(cmd.options & CONTROL_KEY)); HSTR_PUSH_BOOL(L, "shift", !!(cmd.options & SHIFT_KEY)); HSTR_PUSH_BOOL(L, "right", !!(cmd.options & RIGHT_MOUSE_KEY)); HSTR_PUSH_BOOL(L, "meta", !!(cmd.options & META_KEY)); // push the command tag lua_pushnumber(L, cmd.tag); // call the function if (!RunCallIn(cmdStr, 7, 1)) { return true; } // get the results if (!lua_isboolean(L, -1)) { logOutput.Print("%s() bad return value\n", cmdStr.GetString().c_str()); lua_pop(L, 1); return true; } const bool retval = !!lua_toboolean(L, -1); lua_pop(L, 1); // return 'true' to remove the command return retval; }
bool CLuaRules::AllowCommand(const CUnit* unit, const Command& cmd) { if (!haveAllowCommand) { return true; // the call is not defined } lua_settop(L, 0); static const LuaHashString cmdStr("AllowCommand"); if (!cmdStr.GetGlobalFunc(L)) { lua_settop(L, 0); return true; // the call is not defined } // push the unit info lua_pushnumber(L, unit->id); lua_pushnumber(L, unit->unitDef->id); lua_pushnumber(L, unit->team); // push the command id lua_pushnumber(L, cmd.id); // push the params list lua_newtable(L); for (int p = 0; p < (int)cmd.params.size(); p++) { lua_pushnumber(L, p + 1); lua_pushnumber(L, cmd.params[p]); lua_rawset(L, -3); } HSTR_PUSH_NUMBER(L, "n", cmd.params.size()); // push the options table lua_newtable(L); HSTR_PUSH_NUMBER(L, "coded", cmd.options); HSTR_PUSH_BOOL(L, "alt", !!(cmd.options & ALT_KEY)); HSTR_PUSH_BOOL(L, "ctrl", !!(cmd.options & CONTROL_KEY)); HSTR_PUSH_BOOL(L, "shift", !!(cmd.options & SHIFT_KEY)); HSTR_PUSH_BOOL(L, "right", !!(cmd.options & RIGHT_MOUSE_KEY)); // call the function if (!RunCallIn(cmdStr, 6, 1)) { return true; } // get the results const int args = lua_gettop(L); if ((args != 1) || !lua_isboolean(L, -1)) { logOutput.Print("%s() bad return value (%i)\n", cmdStr.GetString().c_str(), args); lua_settop(L, 0); return true; } return !!lua_toboolean(L, -1); }
static int WeaponsTable(lua_State* L, const void* data) { const vector<UnitDefWeapon>& weapons = *((const vector<UnitDefWeapon>*)data); lua_newtable(L); for (size_t i = 0; i < weapons.size(); i++) { const UnitDefWeapon& udw = weapons[i]; const WeaponDef* weapon = udw.def; lua_pushnumber(L, i + LUA_WEAPON_BASE_INDEX); lua_newtable(L); { HSTR_PUSH_NUMBER(L, "weaponDef", weapon->id); HSTR_PUSH_NUMBER(L, "slavedTo", udw.slavedTo-1+LUA_WEAPON_BASE_INDEX); HSTR_PUSH_NUMBER(L, "fuelUsage", udw.fuelUsage); HSTR_PUSH_NUMBER(L, "maxAngleDif", udw.maxMainDirAngleDif); HSTR_PUSH_NUMBER(L, "mainDirX", udw.mainDir.x); HSTR_PUSH_NUMBER(L, "mainDirY", udw.mainDir.y); HSTR_PUSH_NUMBER(L, "mainDirZ", udw.mainDir.z); HSTR_PUSH(L, "badTargets"); CategorySetFromBits(L, &udw.badTargetCat); lua_rawset(L, -3); HSTR_PUSH(L, "onlyTargets"); CategorySetFromBits(L, &udw.onlyTargetCat); lua_rawset(L, -3); } lua_rawset(L, -3); } return 1; }
static int GuiSoundSetTable(lua_State* L, const void* data) { const GuiSoundSet& soundSet = *((const GuiSoundSet*) data); const int soundCount = (int)soundSet.sounds.size(); lua_newtable(L); for (int i = 0; i < soundCount; i++) { lua_pushnumber(L, i + 1); lua_newtable(L); const GuiSoundSet::Data& sound = soundSet.sounds[i]; HSTR_PUSH_STRING(L, "name", sound.name); HSTR_PUSH_NUMBER(L, "volume", sound.volume); if (!CLuaHandle::GetSynced(L)) { HSTR_PUSH_NUMBER(L, "id", sound.id); } lua_rawset(L, -3); } return 1; }
static void PushGuiSoundSet(lua_State* L, const string& name, const GuiSoundSet& soundSet) { const int soundCount = (int)soundSet.sounds.size(); lua_pushsstring(L, name); lua_newtable(L); for (int i = 0; i < soundCount; i++) { lua_pushnumber(L, i + 1); lua_newtable(L); const GuiSoundSet::Data& sound = soundSet.sounds[i]; HSTR_PUSH_STRING(L, "name", sound.name); HSTR_PUSH_NUMBER(L, "volume", sound.volume); if (!CLuaHandle::GetHandleSynced(L)) { HSTR_PUSH_NUMBER(L, "id", sound.id); } lua_rawset(L, -3); } lua_rawset(L, -3); }
static int DamagesArray(lua_State* L, const void* data) { const DamageArray& d = *((const DamageArray*)data); lua_newtable(L); HSTR_PUSH_NUMBER(L, "impulseFactor", d.impulseFactor); HSTR_PUSH_NUMBER(L, "impulseBoost", d.impulseBoost); HSTR_PUSH_NUMBER(L, "craterMult", d.craterMult); HSTR_PUSH_NUMBER(L, "craterBoost", d.craterBoost); HSTR_PUSH_NUMBER(L, "paralyzeDamageTime", d.paralyzeDamageTime); // damage values const int typeCount = damageArrayHandler->GetNumTypes(); for (int i = 0; i < typeCount; i++) { lua_pushnumber(L, i); lua_pushnumber(L, d[i]); lua_rawset(L, -3); } return 1; }
static int MoveDefTable(lua_State* L, const void* data) { const unsigned int mdType = *static_cast<const unsigned int*>(data); const MoveDef* md = NULL; lua_newtable(L); if (mdType == -1U) { return 1; } if ((md = moveDefHandler->GetMoveDefByPathType(mdType)) == NULL) { return 1; } HSTR_PUSH_NUMBER(L, "id", md->pathType); switch (md->speedModClass) { case MoveDef::Tank: { HSTR_PUSH_STRING(L, "family", "tank"); HSTR_PUSH_STRING(L, "type", "ground"); break; } case MoveDef::KBot: { HSTR_PUSH_STRING(L, "family", "kbot"); HSTR_PUSH_STRING(L, "type", "ground"); break; } case MoveDef::Hover: { HSTR_PUSH_STRING(L, "family", "hover"); HSTR_PUSH_STRING(L, "type", "hover"); break; } case MoveDef::Ship: { HSTR_PUSH_STRING(L, "family", "ship"); HSTR_PUSH_STRING(L, "type", "ship"); break; } } HSTR_PUSH_NUMBER(L, "xsize", md->xsize); HSTR_PUSH_NUMBER(L, "zsize", md->zsize); HSTR_PUSH_NUMBER(L, "depth", md->depth); HSTR_PUSH_NUMBER(L, "maxSlope", md->maxSlope); HSTR_PUSH_NUMBER(L, "slopeMod", md->slopeMod); HSTR_PUSH_NUMBER(L, "depthMod", md->depthModParams[MoveDef::DEPTHMOD_LIN_COEFF]); HSTR_PUSH_NUMBER(L, "crushStrength", md->crushStrength); HSTR_PUSH_BOOL(L, "heatMapping", md->heatMapping); HSTR_PUSH_NUMBER(L, "heatMod", md->heatMod); HSTR_PUSH_NUMBER(L, "heatProduced", md->heatProduced); HSTR_PUSH_STRING(L, "name", md->name); return 1; }
bool CLuaHandle::CommandNotify(const Command& cmd) { if (!CheckModUICtrl()) { return false; } LUA_CALL_IN_CHECK(L); lua_checkstack(L, 5); static const LuaHashString cmdStr("CommandNotify"); if (!PushUnsyncedCallIn(cmdStr)) { return false; // the call is not defined } // push the command id lua_pushnumber(L, cmd.id); // push the params list lua_newtable(L); for (int p = 0; p < (int)cmd.params.size(); p++) { lua_pushnumber(L, p + 1); lua_pushnumber(L, cmd.params[p]); lua_rawset(L, -3); } // push the options table lua_newtable(L); HSTR_PUSH_NUMBER(L, "coded", cmd.options); HSTR_PUSH_BOOL(L, "alt", !!(cmd.options & ALT_KEY)); HSTR_PUSH_BOOL(L, "ctrl", !!(cmd.options & CONTROL_KEY)); HSTR_PUSH_BOOL(L, "shift", !!(cmd.options & SHIFT_KEY)); HSTR_PUSH_BOOL(L, "right", !!(cmd.options & RIGHT_MOUSE_KEY)); HSTR_PUSH_BOOL(L, "meta", !!(cmd.options & META_KEY)); // call the function if (!RunCallInUnsynced(cmdStr, 3, 1)) { return false; } // get the results if (!lua_isboolean(L, -1)) { logOutput.Print("CommandNotify() bad return value\n"); lua_pop(L, 1); return false; } const bool retval = !!lua_toboolean(L, -1); lua_pop(L, 1); return retval; }
static int VisualsTable(lua_State* L, const void* data) { const struct WeaponDef::Visuals& v = *((const struct WeaponDef::Visuals*)data); lua_newtable(L); HSTR_PUSH_STRING(L, "modelName", v.modelName); HSTR_PUSH_NUMBER(L, "colorR", v.color.x); HSTR_PUSH_NUMBER(L, "colorG", v.color.y); HSTR_PUSH_NUMBER(L, "colorB", v.color.z); HSTR_PUSH_NUMBER(L, "color2R", v.color2.x); HSTR_PUSH_NUMBER(L, "color2G", v.color2.y); HSTR_PUSH_NUMBER(L, "color2B", v.color2.z); HSTR_PUSH_BOOL (L, "smokeTrail", v.smokeTrail); HSTR_PUSH_BOOL (L, "beamWeapon", v.beamweapon); HSTR_PUSH_NUMBER(L, "tileLength", v.tilelength); HSTR_PUSH_NUMBER(L, "scrollSpeed", v.scrollspeed); HSTR_PUSH_NUMBER(L, "pulseSpeed", v.pulseSpeed); HSTR_PUSH_NUMBER(L, "laserFlareSize", v.laserflaresize); HSTR_PUSH_NUMBER(L, "thickness", v.thickness); HSTR_PUSH_NUMBER(L, "coreThickness", v.corethickness); HSTR_PUSH_NUMBER(L, "beamDecay", v.beamdecay); HSTR_PUSH_NUMBER(L, "stages", v.stages); HSTR_PUSH_NUMBER(L, "sizeDecay", v.sizeDecay); HSTR_PUSH_NUMBER(L, "alphaDecay", v.alphaDecay); HSTR_PUSH_NUMBER(L, "separation", v.separation); HSTR_PUSH_BOOL (L, "noGap", v.noGap); HSTR_PUSH_BOOL (L, "alwaysVisible", v.alwaysVisible); return 1; // CColorMap *colorMap; // AtlasedTexture *texture1; // AtlasedTexture *texture2; // AtlasedTexture *texture3; // AtlasedTexture *texture4; }