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

TFS 1.X+ Gained experience in percent?

Dran Ryszard

Active Member
Joined
Apr 25, 2023
Messages
136
Reaction score
30
Location
Poland
Hi, can someone help me how to show gained experience in percent? I mean change whole numbers like 6400000000 to how much percent player get?
100% = 1
99% = 0.99
1% = 0.01
0% = 0

Its possible?

TFS 1.5 8.6 with modals
 
find the experience for that level.

So level 50 is 1,847,300 experience.
Level 51 requires 1,965,000 experience.

1,965,000 - 1,847,300 = 117,700.

Now you find the current amount of experience the player has.

Let's say 1,850,000.

current experience - level 50 experience = 2,700
1,850,000 - 1,847,300 = 2,700

So now we just find out how much of a percentage 2,700 is of 117,700.

(2700 / 117700) * 100 = 2.293%~
 
@Xikini Okey i know how to calculate percents xD, but how to calculate how much percent add monster?
I Think maybe use part of code what show it in game, but how to check what % of level player have before killing and after?
I thinked save % in storage, but i feel is stupid idea

I forgott say in first post - i wants change a value what is showed after monster kill, (that white numbers upper of character)
 
@Xikini i do something wrong couse not work :( When i calc it in calculator then i get correct results, but in game it show 0.0000% :(
1728121566652.webp

C:
void Player::addExperience(Creature* source, uint64_t exp, bool sendText/* = false*/)
{
    uint64_t currLevelExp = Player::getExpForLevel(level);
    uint64_t nextLevelExp = Player::getExpForLevel(level + 1);
    uint64_t rawExp = exp;
    if (currLevelExp >= nextLevelExp) {
        //player has reached max level
        levelPercent = 0;
        sendStats();
        return;
    }

    g_events->eventPlayerOnGainExperience(this, source, exp, rawExp);
    if (exp == 0) {
        return;
    }

    experience += exp;

    if (sendText) {
        std::string expString = std::to_string(exp) + (exp != 1 ? " experience points." : " experience point.");

        TextMessage message(MESSAGE_STATUS_DEFAULT, "You gained " + expString);
        sendTextMessage(message);
        
        float proc_exp = (exp/nextLevelExp)*100;
        
        g_game.addAnimatedText(std::to_string(proc_exp) + "%", position, TEXTCOLOR_DARKPURPLE);

        SpectatorVec spectators;
        g_game.map.getSpectators(spectators, position, false, true);
        spectators.erase(this);
        if (!spectators.empty()) {
            message.type = MESSAGE_STATUS_DEFAULT;
            message.text = getName() + " gained " + expString;
            for (Creature* spectator : spectators) {
                spectator->getPlayer()->sendTextMessage(message);
            }
        }
    }
 
@Xikini i do something wrong couse not work :( When i calc it in calculator then i get correct results, but in game it show 0.0000% :(
View attachment 87499

C:
void Player::addExperience(Creature* source, uint64_t exp, bool sendText/* = false*/)
{
    uint64_t currLevelExp = Player::getExpForLevel(level);
    uint64_t nextLevelExp = Player::getExpForLevel(level + 1);
    uint64_t rawExp = exp;
    if (currLevelExp >= nextLevelExp) {
        //player has reached max level
        levelPercent = 0;
        sendStats();
        return;
    }

    g_events->eventPlayerOnGainExperience(this, source, exp, rawExp);
    if (exp == 0) {
        return;
    }

    experience += exp;

    if (sendText) {
        std::string expString = std::to_string(exp) + (exp != 1 ? " experience points." : " experience point.");

        TextMessage message(MESSAGE_STATUS_DEFAULT, "You gained " + expString);
        sendTextMessage(message);
       
        float proc_exp = (exp/nextLevelExp)*100;
       
        g_game.addAnimatedText(std::to_string(proc_exp) + "%", position, TEXTCOLOR_DARKPURPLE);

        SpectatorVec spectators;
        g_game.map.getSpectators(spectators, position, false, true);
        spectators.erase(this);
        if (!spectators.empty()) {
            message.type = MESSAGE_STATUS_DEFAULT;
            message.text = getName() + " gained " + expString;
            for (Creature* spectator : spectators) {
                spectator->getPlayer()->sendTextMessage(message);
            }
        }
    }

float proc_exp = (exp/nextLevelExp)*100;

exp and nextLevelExp are probably both integers.
In C++ when you divide a smaller integer against a larger integer the division will yield 0, because C++ truncates the fractional part in integer division.

Basically, the solution is to not do an integer division.
Explicitly cast them into a float.

float proc_exp = (static_cast<float>(exp) / static_cast<float>(nextLevelExp)) * 100;
 
@Xikini thanks, now work.
But...
I don't know where is problem, couse when monster add me 7% of level, then it show 3.57% :( (if i use variable currLevelExp, couse i think its correct, not a nextLevelExp ;D)

So i do this
But i think its not good but it work like i want xD
Code:
float proc_exp = ((static_cast<float>(exp) / static_cast<float>(currLevelExp)) * 100)*2;

Btw its possible to show only 2 numbers after comma?
 
Btw its possible to show only 2 numbers after comma?

C++:
float proc_exp = (static_cast<float>(exp) / static_cast<float>(nextLevelExp)) * 100;

// Format to two decimal places
std::stringstream stream;
stream << std::fixed << std::setprecision(2) << proc_exp;

g_game.addAnimatedText(stream.str() + "%", position, TEXTCOLOR_DARKPURPLE);

I don't know where is problem, couse when monster add me 7% of level, then it show 3.57% :( (if i use variable currLevelExp, couse i think its correct, not a nextLevelExp ;D)

I explained this up above, and you told me you know how to calculate percents already.
So good luck.
🤷‍♀️
 
@Xikini i think i calculate it good? Couse variable "currLevelExp" = how experience we need to level up, so when i use variable "exp" (exp = how we experience get after kill monsters with all rates/boots) so when i (exp/currLevelExp)*100 = how i percent get?
So if monster add me 100xp when i need 6200 then its ~1.6%

But i think that variables its not what i mean, couse when player get few level for one monster, then its give crazy numbers of percents ;D

But thanks @Xikini and sorry for waste ur time.
 
Back
Top