Solved Hp/Mana % Bug

Niioxce

Otland lurker
Premium User
Joined
Jun 22, 2012
Messages
322
Reaction score
3
Location
Sweden
I currently have a problem with Crypton3's hp/mana % source code. The problem is that after a certain level then the % just stops working and shows the normal number again (65,535) untill i level up some more and then it resets at 1% and goes up to 11% and then back to 65,535 and the it just resets over and over again. Here's a gif that shows it. I'm on 8.6 tfs 0.3.7 rev 5969
Imgur GIF
 

Sarah Wesker

ค∂vαηcε รүηтαx ❤
Joined
Mar 16, 2017
Messages
356
Reaction score
165
Location
London
Code:
msg->put<uint16_t>(player->getHealth() / player->getPlayerInfo(PLAYERINFO_MAXHEALTH) * 100);
msg->put<uint16_t>(100);

msg->put<uint16_t>(player->getPlayerInfo(PLAYERINFO_MANA) / player->getPlayerInfo(PLAYERINFO_MAXMANA) * 100);
msg->put<uint16_t>(100);
 
OP
Niioxce

Niioxce

Otland lurker
Premium User
Joined
Jun 22, 2012
Messages
322
Reaction score
3
Location
Sweden
That kinda works! Sometimes it gets stuck at 0 and then goes back upto 100 when i level up. It also goes down to 0 when i get attacked. That's most likely why.
 
OP
Niioxce

Niioxce

Otland lurker
Premium User
Joined
Jun 22, 2012
Messages
322
Reaction score
3
Location
Sweden
C++:
    if (player->getPlayerInfo(PLAYERINFO_MAXHEALTH) > 0)
    {
        msg->put<uint16_t>(uint16_t(player->getHealth() * 100 / player->getPlayerInfo(PLAYERINFO_MAXHEALTH)));
        msg->put<uint16_t>(100);
    }
    else
    {
        msg->put<uint16_t>(0);
        msg->put<uint16_t>(0);
    }
C++:
    if (player->getPlayerInfo(PLAYERINFO_MAXMANA) > 0)
    {
        msg->put<uint16_t>(player->getPlayerInfo(PLAYERINFO_MANA) * 100 / player->getPlayerInfo(PLAYERINFO_MAXMANA));
        msg->put<uint16_t>(100);
    }
    else
    {
        msg->put<uint16_t>(0);
        msg->put<uint16_t>(0);
    }
What i use right now
 
Last edited:
OP
Niioxce

Niioxce

Otland lurker
Premium User
Joined
Jun 22, 2012
Messages
322
Reaction score
3
Location
Sweden
Nope, with that code i posted above it is exactly as i described it in the original post.
 

Sarah Wesker

ค∂vαηcε รүηтαx ❤
Joined
Mar 16, 2017
Messages
356
Reaction score
165
Location
London
Code:
if (player->getPlayerInfo(PLAYERINFO_MAXMANA) > 0)
    {
        msg->put<uint16_t>(player->getPlayerInfo(PLAYERINFO_MANA) * 100 / player->getPlayerInfo(PLAYERINFO_MAXMANA));
        msg->put<uint16_t>(100);
    }
    else
    {
        msg->put<uint16_t>(0);
        msg->put<uint16_t>(100); // <----------- is 100 not 0
}
the condition is not necessary.
if > 0
 
OP
Niioxce

Niioxce

Otland lurker
Premium User
Joined
Jun 22, 2012
Messages
322
Reaction score
3
Location
Sweden
Now it's even more weird Imgur GIF

C++:
    if (player->getPlayerInfo(PLAYERINFO_MAXMANA))
    {
        msg->put<uint16_t>(player->getPlayerInfo(PLAYERINFO_MANA) * 100 / player->getPlayerInfo(PLAYERINFO_MAXMANA));
        msg->put<uint16_t>(100);
    }
    else
    {
        msg->put<uint16_t>(0);
        msg->put<uint16_t>(100);
    }
 

Sarah Wesker

ค∂vαηcε รүηтαx ❤
Joined
Mar 16, 2017
Messages
356
Reaction score
165
Location
London
Lua:
uint16_t resultPercentHp(uint16_t now, uint16_t max) {
    if (now < 1) {
        return 0;
    }
    uint16_t total = ((now / max) * 100);
    return total;
}

void ProtocolGame::AddPlayerStats(NetworkMessage_ptr msg)
{
    msg->put<char>(0xA0);
    msg->put<uint16_t>(resultPercentHp(player->getHealth(), player->getPlayerInfo(PLAYERINFO_MAXHEALTH)));
    msg->put<uint16_t>(100);
    msg->put<uint32_t>(uint32_t(player->getFreeCapacity() * 100));
    uint64_t experience = player->getExperience();
    if(experience > 0x7FFFFFFF) // client debugs after 2,147,483,647 exp
        msg->put<uint32_t>(0x7FFFFFFF);
    else
        msg->put<uint32_t>(experience);

    msg->put<uint16_t>(player->getPlayerInfo(PLAYERINFO_LEVEL));
    msg->put<char>(player->getPlayerInfo(PLAYERINFO_LEVELPERCENT));
    msg->put<uint16_t>(resultPercentHp(player->getPlayerInfo(PLAYERINFO_MANA), player->getPlayerInfo(PLAYERINFO_MAXMANA)));
    msg->put<uint16_t>(100);
    msg->put<char>(player->getPlayerInfo(PLAYERINFO_MAGICLEVEL));
    msg->put<char>(player->getPlayerInfo(PLAYERINFO_MAGICLEVELPERCENT));
    msg->put<char>(player->getPlayerInfo(PLAYERINFO_SOUL));
    msg->put<uint16_t>(player->getStaminaMinutes());
}
 
OP
Niioxce

Niioxce

Otland lurker
Premium User
Joined
Jun 22, 2012
Messages
322
Reaction score
3
Location
Sweden
Okay so, it kinda works again. The ONLY problem there is that it goes to 0 when im attacked or use a spell (but thats the mana). I don't see 65,535 anymore atleast Imgur GIF
Seems like it can either only be 0 or 100
 
Last edited:

MaR0

Active Member
Joined
Apr 16, 2018
Messages
269
Reaction score
26
try this
C++:
void ProtocolGame::AddPlayerStats(NetworkMessage_ptr msg)
{
    msg->put<char>(0xA0);
    if (player->getPlayerInfo(PLAYERINFO_MAXHEALTH) > 0)
{
  msg->put<uint16_t>(uint16_t(player->getHealth() * 100ULL / player->getPlayerInfo(PLAYERINFO_MAXHEALTH)));
  msg->put<uint16_t>(100);
}
else
{
  msg->put<uint16_t>(0);
  msg->put<uint16_t>(0);
}
    msg->put<uint32_t>(uint32_t(player->getFreeCapacity() * 100));
    uint64_t experience = player->getPlayerInfo(PLAYERINFO_LEVEL);
    if(experience > 0x7FFFFFFF)
        msg->put<uint32_t>(0x7FFFFFFF);
    else
        msg->put<uint32_t>(experience);

    msg->put<uint16_t>(0);
    msg->put<char>(player->getPlayerInfo(PLAYERINFO_LEVELPERCENT));
    if (player->getPlayerInfo(PLAYERINFO_MAXMANA) > 0)
{
  msg->put<uint16_t>(player->getPlayerInfo(PLAYERINFO_MANA) * 100ULL / player->getPlayerInfo(PLAYERINFO_MAXMANA));
  msg->put<uint16_t>(100);
}
C++:
void ProtocolGame::AddCreatureHealth(NetworkMessage_ptr msg,const Creature* creature)
{
    msg->put<char>(0x8C);
    msg->put<uint32_t>(creature->getID());
    if(!creature->getHideHealth())
        msg->put<char>((uint8_t)std::ceil(creature->getHealth() * 100. / std::max(creature->getMaxHealth(), (int32_t)1)));
    else
        msg->put<char>(0x00);
}
C++:
msg->put<char>((uint8_t)std::ceil(creature->getHealth() * 100. / std::max(creature->getMaxHealth(), 1)));
 
OP
Niioxce

Niioxce

Otland lurker
Premium User
Joined
Jun 22, 2012
Messages
322
Reaction score
3
Location
Sweden
Yes, this worked. Thank you so much MaRo!
 
Top