• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.
[TFS 1.4.2] [Globalevent] Stamina regeneration inside protection zone

[TFS 1.4.2] [Globalevent] Stamina regeneration inside protection zone

czouski

Banned User
Joined
Nov 2, 2024
Messages
166
Solutions
1
Reaction score
64
czouski submitted a new resource:

[TFS 1.4.2] [Globalevent] Stamina regeneration inside protection zone - stamina

The script will query players position every second and give stamina to those who are inside protection zone for
180 seconds (for less than 39 hours )
600 seconds ( more than 39 but less than 42 aka premium stamina)

LUA:
local globalevent = GlobalEvent("staminaRegeneration")

local STORAGE_KEY = 48474645
local REGENERATION_INTERVAL_LOW = 180 -- seconds (3 minutes) for stamina below 39 hours
local REGENERATION_INTERVAL_HIGH = 600 -- seconds (10 minutes) for stamina between 39 and 42...

Read more about this resource...
 
Nice work but a suggestion, every second is not good idea to loop every players it will impact if you have alot players online.
 
Nice work but a suggestion, every second is not good idea to loop every players it will impact if you have alot players online.
if everything is happening in Sync then why would it lag?
if monster targets player i think is way more cpu extensive task for server than a global server every tick through online players.
Checking every second also ensures that the player is granted the stamina exactly at right time and does not lose any ticks.
Post automatically merged:

LUA:
local globalevent = GlobalEvent("staminaRegeneration")

local STORAGE_KEY = 48474645
local REGENERATION_INTERVAL_LOW = 180 -- seconds (3 minutes) for stamina below 39 hours
local REGENERATION_INTERVAL_HIGH = 600 -- seconds (10 minutes) for stamina between 39 and 42 hours
local STAMINA_FULL = 42 * 60 -- full stamina in minutes (42 hours)
local STAMINA_THRESHOLD = 39 * 60 -- threshold in minutes (39 hours)

function globalevent.onThink()
    for _, player in ipairs(Game.getPlayers()) do
        -- Check if player is in a protection zone and stamina is below the maximum
        if Tile(player:getPosition()):hasFlag(TILESTATE_PROTECTIONZONE) and player:getStamina() < STAMINA_FULL then
            -- Increment storage by 60 (for 60-second interval)
            local currentCount = player:getStorageValue(STORAGE_KEY)
            if currentCount < 0 then
                currentCount = 0
            end
            currentCount = currentCount + 60
            player:setStorageValue(STORAGE_KEY, currentCount)

            -- Determine regeneration interval based on player's current stamina
            local playerStamina = player:getStamina()
            local regenerationInterval = REGENERATION_INTERVAL_LOW -- Default to 3 minutes

            if playerStamina >= STAMINA_THRESHOLD and playerStamina < STAMINA_FULL then
                regenerationInterval = REGENERATION_INTERVAL_HIGH -- Set to 10 minutes
            end

            -- Check if the storage value has reached or exceeded the interval for stamina regeneration
            if currentCount >= regenerationInterval then
                -- Regenerate 1 minute of stamina
                player:setStamina(playerStamina + 1)

                -- Subtract the interval from the storage to carry over any extra time
                player:setStorageValue(STORAGE_KEY, currentCount - regenerationInterval)
            end
        elseif player:getStorageValue(STORAGE_KEY) ~= 0 then
            -- Reset storage to 0 if the player is not in a protection zone or has full stamina
            player:setStorageValue(STORAGE_KEY, 0)
        end
    end
    return true
end

globalevent:interval(60000) -- Executes every 60 seconds (1 minute)
globalevent:register()

here is a 60 second script if it does bother anyone.
 
Last edited:
Idk why so many people started sharing GPT code on here. AI does not create code, it searches a solution over the web and combines it/them into an answer. Most of the time even if it works its not good.

This code is tragic&a bottleneck for a server. Learn on your own/gpt can also teach well, but never depend on it to create a good code.
 
Idk why so many people started sharing GPT code on here. AI does not create code, it searches a solution over the web and combines it/them into an answer. Most of the time even if it works its not good.

This code is tragic&a bottleneck for a server. Learn on your own/gpt can also teach well, but never depend on it to create a good code.
the code is mine I created desired logic to do it in globalevents (yes I used chat gpt) while knowing the potential bottlenecks however the bottleneck realistically only exists for you, this is used on server leess more than 100 players and without editing C++ because in production we sometimes might want to push a feature in live while we supply the solution over c++ without having to restart a live game. I understand your concern while it might be valid it is not for the given scenario.
The comments are my essentialy what it understands from my prompt if you put those comments into chat gpt it will give you exactly same code. :)

P.s next time you provide feedback instead provide a better solution it might work out better :)
I agree for a either onMovement script that checks when player enters protection zone that starts counter might work more efficiently however you can also add iteration to the players to only do 20 players at a time very simply

mind that there is no stamina regeneration system in the engine itself most appropriate would probably be to create a new function and add it in player onThink given he is out of combat. :)
 
Last edited:
czouski updated [TFS 1.4.2] [Globalevent] Stamina regeneration inside protection zone with a new update entry:

v3

LUA:
local globalevent = GlobalEvent("staminaRegeneration")

local STORAGE_KEY = 48474645
local REGENERATION_INTERVAL_LOW = 180 -- seconds (3 minutes) for stamina below 39 hours
local REGENERATION_INTERVAL_HIGH = 600 -- seconds (10 minutes) for stamina between 39 and 42 hours
local STAMINA_FULL = 42 * 60 -- full stamina in minutes (42 hours)
local STAMINA_THRESHOLD = 39 * 60 -- threshold in minutes (39 hours)
local PLAYER_BATCH_SIZE = 20 -- Number of players to process per interval...

Read the rest of this update entry...
Post automatically merged:


C++:
void regenerateStamina(uint32_t interval) {
    // Convert interval from milliseconds to seconds
    uint32_t secondsPassed = interval / 1000;

    // Only regenerate if the player is in a protection zone and stamina is not full
    if (getTile()->hasFlag(TILESTATE_PROTECTIONZONE) && getStaminaMinutes() < STAMINA_FULL) {
        // Accumulate the time passed (interval is now in seconds)
        accumulatedTime += secondsPassed;

        // Determine regeneration interval based on the player's current stamina
        uint32_t regenerationInterval = REGENERATION_INTERVAL_LOW;  // Default to 3 minutes (180 seconds)

        if (getStaminaMinutes() >= STAMINA_THRESHOLD && getStaminaMinutes() < STAMINA_FULL) {
            regenerationInterval = REGENERATION_INTERVAL_HIGH;  // 10 minutes (600 seconds) if stamina is between 39 and 42 hours
        }

        // If enough time has passed for regeneration
        while (accumulatedTime >= regenerationInterval) {
            // Regenerate 1 minute of stamina
            setStaminaMinutes(getStaminaMinutes() + 1);

            // Reset the accumulated time by subtracting the regeneration interval
            accumulatedTime -= regenerationInterval;

            // Ensure stamina doesn't exceed the maximum (STAMINA_FULL)
            if (getStaminaMinutes() == STAMINA_FULL) {
                break;  // Exit the loop if stamina is full
            }
        }

        // Send updated stats to the player
        sendStats();
    }
    else {
        // If the player is not in a protection zone or has full stamina, reset accumulated time
        accumulatedTime = 0;
    }
}
Post automatically merged:

C++:
    void setStaminaMinutes(uint16_t minutes);  // Setter for staminaMinutes
Post automatically merged:

C++:
// In your Player class implementation (usually in the .cpp file)
void Player::setStaminaMinutes(uint16_t minutes) {
    // Ensure stamina does not exceed the maximum allowed value (e.g., 42 hours = 2520 minutes)
    staminaMinutes = std::min(minutes, STAMINA_FULL);  // STAMINA_FULL would be 2520 (42 hours)
}
Post automatically merged:

C++:
    private:
        uint32_t accumulatedTime = 0;  // Time accumulated since the last stamina regeneration (in seconds)
        static constexpr uint32_t REGENERATION_INTERVAL_LOW = 180;  // 3 minutes in seconds
        static constexpr uint32_t REGENERATION_INTERVAL_HIGH = 600;  // 10 minutes in seconds
        static const uint16_t STAMINA_FULL = 2520;  // Max stamina (42 hours = 2520 minutes)
        static constexpr uint16_t STAMINA_THRESHOLD = 2400;  // Stamina threshold in minutes
        std::forward_list<Condition*> getMuteConditions() const;

all by chat gpt with my minimal fixes of single variables and points

as said sometimes our priority for features might be limited and we want to push it in production without stopping game or reloading and recompiling machine.
Post automatically merged:

 
Last edited:
kkkk script misses
LUA:
if playerStamina >= STAMINA_FULL then
    player:setStorageValue(STORAGE_KEY, 0)
LUA:
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "1 minute has been added to your(Stamina).")
Code:
 
kkkk script misses
LUA:
if playerStamina >= STAMINA_FULL then
    player:setStorageValue(STORAGE_KEY, 0)
LUA:
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "1 minute has been added to your(Stamina).")
Code:
no it does not miss anything why would you inform player about it even he can see it in his skills window. thats extra memory you are spending also if playerstamina full is already in logic preventing regeneration completely why u setting it to 0 it already does it itself in elseif statement while player is going outside of protection zone
 
Back
Top