bool CLuaHandleSynced::Initialize(const string& syncData) { LUA_CALL_IN_CHECK(L); lua_checkstack(L, 3); static const LuaHashString cmdStr("Initialize"); if (!cmdStr.GetGlobalFunc(L)) { return true; } int errfunc = SetupTraceback() ? -2 : 0; logOutput.Print("Initialize errfunc=%d\n", errfunc); lua_pushlstring(L, syncData.c_str(), syncData.size()); // call the routine if (!RunCallInTraceback(cmdStr, 1, 1, errfunc)) { return false; } // get the results if (!lua_isboolean(L, -1)) { lua_pop(L, 1); return true; } const bool retval = !!lua_toboolean(L, -1); lua_pop(L, 1); return retval; }
void CLuaHandleSynced::GameFrame(int frameNumber) { if (killMe) { string msg = GetName(); if (!killMsg.empty()) { msg += ": " + killMsg; } logOutput.Print("Disabled %s\n", msg.c_str()); delete this; return; } LUA_CALL_IN_CHECK(L); lua_checkstack(L, 4); int errfunc = SetupTraceback(); static const LuaHashString cmdStr("GameFrame"); if (!cmdStr.GetGlobalFunc(L)) { if (errfunc) lua_pop(L, 1); return; } lua_pushnumber(L, frameNumber); // 6 day roll-over // call the routine allowChanges = true; RunCallInTraceback(cmdStr, 1, 0, errfunc); allowChanges = allowUnsafeChanges; return; }
void CLuaHandle::UnitFromFactory(const CUnit* unit, const CUnit* factory, bool userOrders) { LUA_CALL_IN_CHECK(L); lua_checkstack(L, 9); int errfunc = SetupTraceback(); static const LuaHashString cmdStr("UnitFromFactory"); if (!cmdStr.GetGlobalFunc(L)) { // remove error handler if (errfunc) lua_pop(L, 1); return; // the call is not defined } lua_pushnumber(L, unit->id); lua_pushnumber(L, unit->unitDef->id); lua_pushnumber(L, unit->team); lua_pushnumber(L, factory->id); lua_pushnumber(L, factory->unitDef->id); lua_pushboolean(L, userOrders); // call the routine RunCallInTraceback(cmdStr, 6, 0, errfunc); return; }
void CLuaHandle::UnitCommand(const CUnit* unit, const Command& command) { LUA_CALL_IN_CHECK(L); lua_checkstack(L, 11); int errfunc = SetupTraceback(); static const LuaHashString cmdStr("UnitCommand"); if (!cmdStr.GetGlobalFunc(L)) { // remove error handler if (errfunc) lua_pop(L, 1); return; // the call is not defined } lua_pushnumber(L, unit->id); lua_pushnumber(L, unit->unitDef->id); lua_pushnumber(L, unit->team); lua_pushnumber(L, command.id); lua_pushnumber(L, command.options); const vector<float> params = command.params; lua_createtable(L, params.size(), 0); for (unsigned int i = 0; i < params.size(); i++) { lua_pushnumber(L, i + 1); lua_pushnumber(L, params[i]); lua_rawset(L, -3); } // call the routine RunCallInTraceback(cmdStr, 6, 0, errfunc); return; }
void CLuaHandle::UnitDestroyed(const CUnit* unit, const CUnit* attacker) { LUA_CALL_IN_CHECK(L); lua_checkstack(L, 9); int errfunc = SetupTraceback(); static const LuaHashString cmdStr("UnitDestroyed"); if (!cmdStr.GetGlobalFunc(L)) { // remove error handler if (errfunc) lua_pop(L, 1); return; // the call is not defined } int argCount = 3; lua_pushnumber(L, unit->id); lua_pushnumber(L, unit->unitDef->id); lua_pushnumber(L, unit->team); if (fullRead && (attacker != NULL)) { lua_pushnumber(L, attacker->id); lua_pushnumber(L, attacker->unitDef->id); lua_pushnumber(L, attacker->team); argCount += 3; } // call the routine RunCallInTraceback(cmdStr, argCount, 0, errfunc); return; }
void CLuaHandle::UnitCreated(const CUnit* unit, const CUnit* builder) { LUA_CALL_IN_CHECK(L); lua_checkstack(L, 7); int errfunc = SetupTraceback(); static const LuaHashString cmdStr("UnitCreated"); if (!cmdStr.GetGlobalFunc(L)) { // remove error handler if (errfunc) lua_pop(L, 1); return; // the call is not defined } lua_pushnumber(L, unit->id); lua_pushnumber(L, unit->unitDef->id); lua_pushnumber(L, unit->team); if (builder != NULL) { lua_pushnumber(L, builder->id); } int args = (builder != NULL) ? 4 : 3; // call the routine RunCallInTraceback(cmdStr, args, 0, errfunc); return; }
/** * called after every damage modification (even HitByWeaponId) * but before the damage is applied * * expects two numbers returned by lua code: * 1st is stored under *newDamage if newDamage != NULL * 2nd is stored under *impulseMult if impulseMult != NULL */ bool CLuaRules::UnitPreDamaged(const CUnit* unit, const CUnit* attacker, float damage, int weaponID, bool paralyzer, float* newDamage, float* impulseMult) { if (!haveUnitPreDamaged) { return false; } LUA_CALL_IN_CHECK(L); lua_checkstack(L, 11); const int errfunc = SetupTraceback(); static const LuaHashString cmdStr("UnitPreDamaged"); if (!cmdStr.GetGlobalFunc(L)) { // remove error handler if (errfunc) { lua_pop(L, 1); } return false; // the call is not defined } int argCount = 5; lua_pushnumber(L, unit->id); lua_pushnumber(L, unit->unitDef->id); lua_pushnumber(L, unit->team); lua_pushnumber(L, damage); lua_pushboolean(L, paralyzer); if (fullRead) { lua_pushnumber(L, weaponID); argCount += 1; if (attacker != NULL) { lua_pushnumber(L, attacker->id); lua_pushnumber(L, attacker->unitDef->id); lua_pushnumber(L, attacker->team); argCount += 3; } } // call the routine RunCallInTraceback(cmdStr, argCount, 2, errfunc); if (newDamage && lua_isnumber(L, -2)) { *newDamage = lua_tonumber(L, -2); } else if (!lua_isnumber(L, -2) || lua_isnil(L, -2)) { // first value is obligatory, so may not be nil logOutput.Print("%s(): 1st value returned should be a number (newDamage)\n", cmdStr.GetString().c_str()); } if (impulseMult && lua_isnumber(L, -1)) { *impulseMult = lua_tonumber(L, -1); } else if (!lua_isnumber(L, -1) && !lua_isnil(L, -1)) { // second value is optional, so nils are OK logOutput.Print("%s(): 2nd value returned should be a number (impulseMult)\n", cmdStr.GetString().c_str()); } lua_pop(L, 2); return true; }
void CLuaHandle::GameOver() { LUA_CALL_IN_CHECK(L); lua_checkstack(L, 2); int errfunc = SetupTraceback(); static const LuaHashString cmdStr("GameOver"); if (!cmdStr.GetGlobalFunc(L)) { // remove error handler if (errfunc) lua_pop(L, 1); return; // the call is not defined } // call the routine RunCallInTraceback(cmdStr, 0, 0, errfunc); return; }
void CLuaHandle::PlayerChanged(int playerID) { LUA_CALL_IN_CHECK(L); lua_checkstack(L, 4); int errfunc = SetupTraceback(); static const LuaHashString cmdStr("PlayerChanged"); if (!cmdStr.GetGlobalFunc(L)) { // remove error handler if (errfunc) lua_pop(L, 1); return; // the call is not defined } lua_pushnumber(L, playerID); // call the routine RunCallInTraceback(cmdStr, 1, 0, errfunc); return; }
void CLuaHandle::FeatureDestroyed(const CFeature* feature) { LUA_CALL_IN_CHECK(L); lua_checkstack(L, 5); int errfunc = SetupTraceback(); static const LuaHashString cmdStr("FeatureDestroyed"); if (!cmdStr.GetGlobalFunc(L)) { // remove error handler if (errfunc) lua_pop(L, 1); return; // the call is not defined } lua_pushnumber(L, feature->id); lua_pushnumber(L, feature->allyteam); // call the routine RunCallInTraceback(cmdStr, 2, 0, errfunc); return; }
inline void CLuaHandle::UnitCallIn(const LuaHashString& hs, const CUnit* unit) { LUA_CALL_IN_CHECK(L); lua_checkstack(L, 6); int errfunc = SetupTraceback(); if (!hs.GetGlobalFunc(L)) { // remove error handler if (errfunc) lua_pop(L, 1); return; // the call is not defined } lua_pushnumber(L, unit->id); lua_pushnumber(L, unit->unitDef->id); lua_pushnumber(L, unit->team); // call the routine RunCallInTraceback(hs, 3, 0, errfunc); return; }
void CLuaHandle::UnitGiven(const CUnit* unit, int oldTeam) { LUA_CALL_IN_CHECK(L); lua_checkstack(L, 7); int errfunc = SetupTraceback(); static const LuaHashString cmdStr("UnitGiven"); if (!cmdStr.GetGlobalFunc(L)) { // remove error handler if (errfunc) lua_pop(L, 1); return; // the call is not defined } lua_pushnumber(L, unit->id); lua_pushnumber(L, unit->unitDef->id); lua_pushnumber(L, unit->team); lua_pushnumber(L, oldTeam); // call the routine RunCallInTraceback(cmdStr, 4, 0, errfunc); return; }
void CLuaHandle::UnitDamaged(const CUnit* unit, const CUnit* attacker, float damage, int weaponID, bool paralyzer) { LUA_CALL_IN_CHECK(L); lua_checkstack(L, 11); int errfunc = SetupTraceback(); static const LuaHashString cmdStr("UnitDamaged"); if (!cmdStr.GetGlobalFunc(L)) { // remove error handler if (errfunc) lua_pop(L, 1); return; // the call is not defined } int argCount = 5; lua_pushnumber(L, unit->id); lua_pushnumber(L, unit->unitDef->id); lua_pushnumber(L, unit->team); lua_pushnumber(L, damage); lua_pushboolean(L, paralyzer); if (fullRead) { lua_pushnumber(L, weaponID); argCount += 1; if (attacker != NULL) { lua_pushnumber(L, attacker->id); lua_pushnumber(L, attacker->unitDef->id); lua_pushnumber(L, attacker->team); argCount += 3; } } // call the routine RunCallInTraceback(cmdStr, argCount, 0, errfunc); return; }