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

C++ Life leech chance using decimal numbers

Addams

OTMadness.com OTSes Services
Staff member
Board Moderator
Joined
Sep 29, 2009
Messages
2,920
Solutions
342
Reaction score
1,689
Location
Egypt
TFS version: 1.4.2
How can I make chance accepts decimal numbers? I am trying to use from 0.1, 0.2, 0.02, 5, 7, 30, etc.. and up to 100 like it is.
I tried to divine chance / 1.0 and then multiply it below * 1.0 but that didn't work.
C++:
if (casterPlayer->getHealth() < casterPlayer->getMaxHealth()) {
                uint16_t chance = casterPlayer->getSpecialSkill(SPECIALSKILL_LIFELEECHCHANCE);
                uint16_t skill = casterPlayer->getSpecialSkill(SPECIALSKILL_LIFELEECHAMOUNT);
                if (chance > 0 && skill > 0 && normal_random(1, 100) <= chance) {
                    leechCombat.primary.value = std::round(totalDamage * (skill / 100.));
                    g_game.combatChangeHealth(nullptr, casterPlayer, leechCombat);
                    casterPlayer->sendMagicEffect(casterPlayer->getPosition(), CONST_ME_MAGIC_RED);
                }
}
Excuse my low knowledge of C++
 
Last edited:
getSpecialSkill` already returns unsigned integer as these are stored as integers:
C++:
int32_t varSpecialSkills[SPECIALSKILL_LAST + 1] = {};
Same for items Abilities:
C++:
std::array<int32_t, SPECIALSKILL_LAST + 1> specialSkills = {0};

void Items::parseItemNode(const pugi::xml_node& itemNode, uint16_t id)
C++:
case ITEM_PARSE_LIFELEECHCHANCE: {
    abilities.specialSkills[SPECIALSKILL_LIFELEECHCHANCE] = pugi::cast<int32_t>(valueAttribute.value());
    break;
}

You'd have to change it everywhere to use double and load these attributes like:
C++:
pugi::cast<double>(valueAttribute.value());
 
I thought I can just change condition one separately without having to change the rest, I am now only using condition ones on Lua, Using an item that gives me a special skill condition but that item is usable up to 200 or something so I don't want to reach 100% chance very soon.
I've tried double/float and rand() myself, Was editing the thread but got a power outage.
 
Last edited:
Try it in Visual Studio with "refactor rename" and "show references" so you can localize all occurances in code to make sure you do it properly
 
Found a few when looked for references, Looks like it's harder than I thought.
a few.PNG
 
Yea lots of places but changes are rather trivial, changing type from integer to double.
As I see these skills are also sent to the client and it expects these are Integers, so another challenge.
 
I thought I can just change condition one separately without having to change the rest, I am now only using condition ones on Lua, Using an item that gives me a special skill condition but that item is usable up to 200 or something so I don't want to reach 100% chance very soon.
I've tried double/float and rand() myself, Was editing the thread but got a power outage.
C++:
double random_number = (rand() % 10000) / 100.0;
1678081187123.png
 
Chance is floating point now but anyway all items still can only have integer Abilities assigned. One way to make a workaround solution is to assume they’re specified like x100 so having chance set to 100 is 1 percent. As I guess main goal was to be able to create items that can have floating point abilities specified (for precised balance) and not to reduce/nerf every items abilities.
 
Easier than "REPLACE"
I didn't mean easier than replacing; I meant like a workaround to avoid changing the int values because if I use critical chance and mana leech chance too, that will be at least 30+ changes.
No. You won't be able to store in memory data with float value if its integer. same with database
It looks like I will need to make the 30+ changes then.
Chance is floating point now but anyway all items still can only have integer Abilities assigned. One way to make a workaround solution is to assume they’re specified like x100 so having chance set to 100 is 1 percent. As I guess main goal was to be able to create items that can have floating point abilities specified (for precised balance) and not to reduce/nerf every items abilities.
Thank you. I might consider using this workaround at the end and only allow using 10 or 100 to get a 1% chance instead of adding the condition with each item used.
 
Back
Top