• 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!

Need % hp/mn command

Stanos

Veteran OT User
Joined
Jun 12, 2018
Messages
587
Solutions
4
Reaction score
315
Location
Europe
Hi.
Does anyone have command that when you type !percentage its makes your health/mana to % and if you type it again it makes your health/mana to normal so its like turning on/off with the same command.
TFS 1.2
Client 8.60
 
Solution
Change lines 2885 and 2886 (protocolgame.cpp): otland/forgottenserver

from:
C++:
msg.add<uint16_t>(std::min<int32_t>(player->getHealth(), std::numeric_limits<uint16_t>::max()));
msg.add<uint16_t>(std::min<int32_t>(player->getMaxHealth(), std::numeric_limits<uint16_t>::max()));

to:
C++:
if (player->showHealthAsPercentage) {
        if (player->getMaxHealth() > 0) {
            msg.add<uint16_t>(player->getHealth() * 100 / player->getMaxHealth());
            msg.add<uint16_t>(100);
        } else {
            msg.add<uint16_t>(0);
            msg.add<uint16_t>(0);
        }
    } else {
        msg.add<uint16_t>(std::min<int32_t>(player->getHealth(), std::numeric_limits<uint16_t>::max()))...
Making stats display in % requires client edits afaik.
Thus it is not a command - you do not tell the server to change anything, this is purely client side and quite a bit more tricky - though you could make it more player friendly and just change it via a button.
 
it's not client sided, it's source edits inside of ProtocolGame::AddPlayerStats
you'd have to open up ProtocolGame::sendStats to a lua function such as Player.sendStats and inside of AddPlayerStats check for storage value of the player and add the health/mana values accordingly
 
Change lines 2885 and 2886 (protocolgame.cpp): otland/forgottenserver

from:
C++:
msg.add<uint16_t>(std::min<int32_t>(player->getHealth(), std::numeric_limits<uint16_t>::max()));
msg.add<uint16_t>(std::min<int32_t>(player->getMaxHealth(), std::numeric_limits<uint16_t>::max()));

to:
C++:
if (player->showHealthAsPercentage) {
        if (player->getMaxHealth() > 0) {
            msg.add<uint16_t>(player->getHealth() * 100 / player->getMaxHealth());
            msg.add<uint16_t>(100);
        } else {
            msg.add<uint16_t>(0);
            msg.add<uint16_t>(0);
        }
    } else {
        msg.add<uint16_t>(std::min<int32_t>(player->getHealth(), std::numeric_limits<uint16_t>::max()));
        msg.add<uint16_t>(std::min<int32_t>(player->getMaxHealth(), std::numeric_limits<uint16_t>::max()));
    }

Change lines 2902 and 2903 (protocolgame.cpp): otland/forgottenserver

from:
C++:
msg.add<uint16_t>(std::min<int32_t>(player->getMana(), std::numeric_limits<uint16_t>::max()));
msg.add<uint16_t>(std::min<int32_t>(player->getMaxMana(), std::numeric_limits<uint16_t>::max()));

to:
C++:
if (player->showManaAsPercentage) {
        if (player->getMaxMana() > 0) {
            msg.add<uint16_t>(player->getMana() * 100 / player->getMaxMana());
            msg.add<uint16_t>(100);
        }
        else {
            msg.add<uint16_t>(0);
            msg.add<uint16_t>(0);
        }
    }
    else {
        msg.add<uint16_t>(std::min<int32_t>(player->getMana(), std::numeric_limits<uint16_t>::max()));
        msg.add<uint16_t>(std::min<int32_t>(player->getMaxMana(), std::numeric_limits<uint16_t>::max()));
    }


player.h (otland/forgottenserver)

after:
C++:
bool hasLearnedInstantSpell(const std::string& spellName) const;

add:
C++:
bool showManaAsPercentage = false;
bool showHealthAsPercentage = false;

luascript.cpp (otland/forgottenserver)

after:
C++:
registerMethod("Player", "getFightMode", LuaScriptInterface::luaPlayerGetFightMode);

add:
Code:
registerMethod("Player", "toggleStatsAsPercentage", LuaScriptInterface::luaPlayertoggleStatsAsPercentage);

luascript.cpp (otland/forgottenserver)

add:

C++:
int LuaScriptInterface::luaPlayertoggleStatsAsPercentage(lua_State* L)
{
    // player:toggleStatsAsPercentage()
    Player* player = getUserdata<Player>(L, 1);
    if (player) {
        player->showManaAsPercentage = !player->showManaAsPercentage;
        player->showHealthAsPercentage = !player->showHealthAsPercentage;
        player->sendStats();
        lua_pushboolean(L, player->showHealthAsPercentage);
    } else {
        lua_pushnil(L);
    }
    return 1;
}

luascript.h (otland/forgottenserver)

after:
C++:
static int luaPlayerGetFightMode(lua_State* L);

add:
C++:
static int luaPlayertoggleStatsAsPercentage(lua_State* L);

We are done with the sources, lets create the talkaction:

data/talkactions/talkactions.xml:

Code:
<talkaction words="/togglestats" separator=" " script="toggle_stats.lua" />

data/talkactions/scripts/toggle_stats.lua:
Lua:
function onSay(player, words, param)
    if player:toggleStatsAsPercentage() then
        -- player is now using percentage notation, you can send him a message.
    else
        -- player is now using normal notation, you can send him a message.
    end
    return false
end

If I haven't missed anything, this should work.
I made it 2 separated booleans so you could easily change it a little bit and let players choose health and mana separately.
 
Solution
Change lines 2885 and 2886 (protocolgame.cpp): otland/forgottenserver

from:
C++:
msg.add<uint16_t>(std::min<int32_t>(player->getHealth(), std::numeric_limits<uint16_t>::max()));
msg.add<uint16_t>(std::min<int32_t>(player->getMaxHealth(), std::numeric_limits<uint16_t>::max()));

to:
C++:
if (player->showHealthAsPercentage) {
        if (player->getMaxHealth() > 0) {
            msg.add<uint16_t>(player->getHealth() * 100 / player->getMaxHealth());
            msg.add<uint16_t>(100);
        } else {
            msg.add<uint16_t>(0);
            msg.add<uint16_t>(0);
        }
    } else {
        msg.add<uint16_t>(std::min<int32_t>(player->getHealth(), std::numeric_limits<uint16_t>::max()));
        msg.add<uint16_t>(std::min<int32_t>(player->getMaxHealth(), std::numeric_limits<uint16_t>::max()));
    }

Change lines 2902 and 2903 (protocolgame.cpp): otland/forgottenserver

from:
C++:
msg.add<uint16_t>(std::min<int32_t>(player->getMana(), std::numeric_limits<uint16_t>::max()));
msg.add<uint16_t>(std::min<int32_t>(player->getMaxMana(), std::numeric_limits<uint16_t>::max()));

to:
C++:
if (player->showManaAsPercentage) {
        if (player->getMaxMana() > 0) {
            msg.add<uint16_t>(player->getMana() * 100 / player->getMaxMana());
            msg.add<uint16_t>(100);
        }
        else {
            msg.add<uint16_t>(0);
            msg.add<uint16_t>(0);
        }
    }
    else {
        msg.add<uint16_t>(std::min<int32_t>(player->getMana(), std::numeric_limits<uint16_t>::max()));
        msg.add<uint16_t>(std::min<int32_t>(player->getMaxMana(), std::numeric_limits<uint16_t>::max()));
    }


player.h (otland/forgottenserver)

after:
C++:
bool hasLearnedInstantSpell(const std::string& spellName) const;

add:
C++:
bool showManaAsPercentage = false;
bool showHealthAsPercentage = false;

luascript.cpp (otland/forgottenserver)

after:
C++:
registerMethod("Player", "getFightMode", LuaScriptInterface::luaPlayerGetFightMode);

add:
Code:
registerMethod("Player", "toggleStatsAsPercentage", LuaScriptInterface::luaPlayertoggleStatsAsPercentage);

luascript.cpp (otland/forgottenserver)

add:

C++:
int LuaScriptInterface::luaPlayertoggleStatsAsPercentage(lua_State* L)
{
    // player:toggleStatsAsPercentage()
    Player* player = getUserdata<Player>(L, 1);
    if (player) {
        player->showManaAsPercentage = !player->showManaAsPercentage;
        player->showHealthAsPercentage = !player->showHealthAsPercentage;
        player->sendStats();
        lua_pushboolean(L, player->showHealthAsPercentage);
    } else {
        lua_pushnil(L);
    }
    return 1;
}

luascript.h (otland/forgottenserver)

after:
C++:
static int luaPlayerGetFightMode(lua_State* L);

add:
C++:
static int luaPlayertoggleStatsAsPercentage(lua_State* L);

We are done with the sources, lets create the talkaction:

data/talkactions/talkactions.xml:

Code:
<talkaction words="/togglestats" separator=" " script="toggle_stats.lua" />

data/talkactions/scripts/toggle_stats.lua:
Lua:
function onSay(player, words, param)
    if player:toggleStatsAsPercentage() then
        -- player is now using percentage notation, you can send him a message.
    else
        -- player is now using normal notation, you can send him a message.
    end
    return false
end

If I haven't missed anything, this should work.
I made it 2 separated booleans so you could easily change it a little bit and let players choose health and mana separately.
Perfection
 
Back
Top