/// 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.GetID()); // 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)); // push the command tag lua_pushnumber(L, cmd.tag); }
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); }
int LuaUICommand::GetUICommands(lua_State* L) { SyncedGameCommands::actionExecutorsMap_t syncedExecutors = syncedGameCommands->GetActionExecutors(); UnsyncedGameCommands::actionExecutorsMap_t unsyncedExecutors = unsyncedGameCommands->GetActionExecutors(); int count = 0; lua_createtable(L, syncedExecutors.size() + unsyncedExecutors.size(), 0); for (SyncedGameCommands::actionExecutorsMap_t::iterator iter = syncedExecutors.begin(); iter != syncedExecutors.end(); ++iter) { lua_createtable(L, 0, 4); ISyncedActionExecutor* exec = iter->second; HSTR_PUSH_STRING(L, "command", exec->GetCommand()); HSTR_PUSH_STRING(L, "description", exec->GetDescription()); HSTR_PUSH_BOOL(L, "synced", exec->IsSynced()); HSTR_PUSH_BOOL(L, "cheat", exec->IsCheatRequired()); lua_rawseti(L, -2, count++); } for (UnsyncedGameCommands::actionExecutorsMap_t::iterator iter = unsyncedExecutors.begin(); iter != unsyncedExecutors.end(); ++iter) { lua_createtable(L, 0, 4); IUnsyncedActionExecutor* exec = iter->second; HSTR_PUSH_STRING(L, "command", exec->GetCommand()); HSTR_PUSH_STRING(L, "description", exec->GetDescription()); HSTR_PUSH_BOOL(L, "synced", exec->IsSynced()); HSTR_PUSH_BOOL(L, "cheat", exec->IsCheatRequired()); lua_rawseti(L, -2, count++); } return 1; }
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); }
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; }
bool CLuaHandle::KeyPress(unsigned short key, bool isRepeat) { if (!CheckModUICtrl()) { return false; } LUA_CALL_IN_CHECK(L); lua_checkstack(L, 6); static const LuaHashString cmdStr("KeyPress"); if (!PushUnsyncedCallIn(cmdStr)) { return false; // the call is not defined, do not take the event } lua_pushnumber(L, key); lua_newtable(L); HSTR_PUSH_BOOL(L, "alt", !!keys[SDLK_LALT]); HSTR_PUSH_BOOL(L, "ctrl", !!keys[SDLK_LCTRL]); HSTR_PUSH_BOOL(L, "meta", !!keys[SDLK_LMETA]); HSTR_PUSH_BOOL(L, "shift", !!keys[SDLK_LSHIFT]); lua_pushboolean(L, isRepeat); CKeySet ks(key, false); lua_pushstring(L, ks.GetString(true).c_str()); lua_pushnumber(L, currentUnicode); // call the function if (!RunCallInUnsynced(cmdStr, 5, 1)) { return false; } // const int args = lua_gettop(L); unused if (!lua_isboolean(L, -1)) { 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; }
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; }