• 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++ Learning c++, what hits code means?

roriscrave

Advanced OT User
Joined
Dec 7, 2011
Messages
1,188
Solutions
34
Reaction score
200
Hi bro, i'm starting to study C/C++, what this code means?
C++:
int32_t maxValue = static_cast<int32_t>(Weapons::getMaxWeaponDamage(player->getLevel(), attackSkill, attackValue, attackFactor) * player->getVocation()->distDamageMultiplier);

I know player->getLevel() = player Level
attackSkill = player skill (in this case distance)
attackValue (attack of weapon)
and attackFactor (depends if players have full attack/full def. etc)

My doubt is, how this script calculate? Because i tested with a character:
Code:
Level:  410
attackSkill: 40
Attack weapon: 2200
Attackvalue:1
distDamageMultiplier:1
and i printed maxValue, it print "8391"
so i dont understand how this formula calculate
 
Solution
Lua:
getWeaponDamage = function(player, target, item, criticalHit)
    attackSkill = player:getWeaponSkill(item)
    attackValue = math.max(0, item:getAttack())
    attackFactor = player:getAttackFactor()
    maxValue = (getMaxWeaponDamage(player:getLevel(), attackSkill, attackValue, attackFactor) * player:getVocation():get('meleeDamageMultiplier'))

    if (criticalHit) then
        return maxValue * -1
    end

    return math.random(0, maxValue) * -1
end


getMaxWeaponDamage = function(level, attackSkill, attackValue, attackFactor)
    return math.ceil((level / 5) + (((((attackSkill / 4.0) + 1) * (attackValue / 3.0)) * 1.03) / attackFactor))
end

Here, stripped of it's typings and C-family syntax, rendered into mostly-valid Lua...
Your weapon attack is 2200 so what do you expect and you explained how it calculates it. Its simple math
 
If you want to know how its calculated you have to go the actual function: Weapons::getMaxWeaponDamage
the maxValue (getWeaponDamage) is not used in damage formula?

I'm wanting to change the minimum and maximum values, because I don't understand Tibia's formulas.
sometimes the character hit miss, sometimes hit 900, sometimes hit 8000. it's a very big variation. (yes, I'm testing it with 2000 atk weapon, because my server will have this weapon).
I'm trying to vary less, that the attack is between 7000 to 8000 (and misses some times), and not between 1 to 8000 + misses

getWeaponDamage:
C:
int32_t WeaponMelee::getWeaponDamage(const Player* player, const Creature*, const Item* item, bool maxDamage /*= false*/) const
{
    int32_t attackSkill = player->getWeaponSkill(item);
    int32_t attackValue = std::max<int32_t>(0, item->getAttack());
    float attackFactor = player->getAttackFactor();

    int32_t maxValue = static_cast<int32_t>(Weapons::getMaxWeaponDamage(player->getLevel(), attackSkill, attackValue, attackFactor) * player->getVocation()->meleeDamageMultiplier);
    if (maxDamage) {
        return -maxValue;
    }

    return -normal_random(0, maxValue);
}

getMaxWeaponDamage (players)
C++:
//monsters
int32_t Weapons::getMaxMeleeDamage(int32_t attackSkill, int32_t attackValue)
{
    return static_cast<int32_t>(std::ceil((attackSkill * (attackValue * 0.05)) + (attackValue * 0.5)));
}

//players
int32_t Weapons::getMaxWeaponDamage(uint32_t level, int32_t attackSkill, int32_t attackValue, float attackFactor)
{
    return static_cast<int32_t>(std::round((level / 5) + (((((attackSkill / 4.) + 1) * (attackValue / 3.)) * 1.03) / attackFactor)));
}
 
Last edited:
Lua:
getWeaponDamage = function(player, target, item, criticalHit)
    attackSkill = player:getWeaponSkill(item)
    attackValue = math.max(0, item:getAttack())
    attackFactor = player:getAttackFactor()
    maxValue = (getMaxWeaponDamage(player:getLevel(), attackSkill, attackValue, attackFactor) * player:getVocation():get('meleeDamageMultiplier'))

    if (criticalHit) then
        return maxValue * -1
    end

    return math.random(0, maxValue) * -1
end


getMaxWeaponDamage = function(level, attackSkill, attackValue, attackFactor)
    return math.ceil((level / 5) + (((((attackSkill / 4.0) + 1) * (attackValue / 3.0)) * 1.03) / attackFactor))
end

Here, stripped of it's typings and C-family syntax, rendered into mostly-valid Lua.

Can you follow it now?
 
Solution
the maxValue (getWeaponDamage) is not used in damage formula?

I'm wanting to change the minimum and maximum values, because I don't understand Tibia's formulas.
sometimes the character hit miss, sometimes hit 900, sometimes hit 8000. it's a very big variation. (yes, I'm testing it with 2000 atk weapon, because my server will have this weapon).
I'm trying to vary less, that the attack is between 7000 to 8000 (and misses some times), and not between 1 to 8000 + misses

getWeaponDamage:
C:
int32_t WeaponMelee::getWeaponDamage(const Player* player, const Creature*, const Item* item, bool maxDamage /*= false*/) const
{
    int32_t attackSkill = player->getWeaponSkill(item);
    int32_t attackValue = std::max<int32_t>(0, item->getAttack());
    float attackFactor = player->getAttackFactor();

    int32_t maxValue = static_cast<int32_t>(Weapons::getMaxWeaponDamage(player->getLevel(), attackSkill, attackValue, attackFactor) * player->getVocation()->meleeDamageMultiplier);
    if (maxDamage) {
        return -maxValue;
    }

    return -normal_random(0, maxValue);
}

getMaxWeaponDamage (players)
C++:
//monsters
int32_t Weapons::getMaxMeleeDamage(int32_t attackSkill, int32_t attackValue)
{
    return static_cast<int32_t>(std::ceil((attackSkill * (attackValue * 0.05)) + (attackValue * 0.5)));
}

//players
int32_t Weapons::getMaxWeaponDamage(uint32_t level, int32_t attackSkill, int32_t attackValue, float attackFactor)
{
    return static_cast<int32_t>(std::round((level / 5) + (((((attackSkill / 4.) + 1) * (attackValue / 3.)) * 1.03) / attackFactor)));
}


So your problem here is this line:
C++:
    return -normal_random(0, maxValue);

This returns a random number between 0 and the max value your weapon could do.

As a Workaround, i suggest you to think about what your min value would be, eg, 80% of maxvalue:
C++:
return -normal_random(maxValue*0.8, maxValue);

This way, if the max value is 1000, you could hit between 800-1000

Edit: Making this change could cause your attack won't miss, not sure, that is something to check.
 
So your problem here is this line:

This returns a random number between 0 and the max value your weapon could do.

As a Workaround

You're in C-land here. You don't need to "work-around". Here we call changes patches.

Between 0 and maxvalue is precisely what it's suppose to do. You'll notice the name? If you want a different distribution, use different code.

1590747342001.png

Clamping based on a multiplier of the calculated maxValue would be a very crude hack. Especially when maybe he could learn something.
 
You're in C-land here. You don't need to "work-around". Here we call changes patches.

Between 0 and maxvalue is precisely what it's suppose to do. You'll notice the name? If you want a different distribution, use different code.

View attachment 46103

Clamping based on a multiplier of the calculated maxValue would be a very crude hack. Especially when maybe he could learn something.
Well, if he wants a min value, then he should go with my solution or something similar. It's not about distributions or shits, we are talking about min and max values. Even with different distributions, you could get 0, 1, or 2 as a min value for the hit, and he stills wants it to be 80% of max.


And TBH, your first line, talking about c-land and patches... just lame.
 
Yes, but I suspect it wasn't seeing the low values that bothered this user, it was the frequency in which they appeared.

Regarding the latter, I just think using the term "work around" is totally inappropriate for a number range generator alteration, where every aspect of it is under your control and understanding the entire system can easily fit inside a single developers focus. It's a term meant for usage as "bypassing a big problem that would be more work than it's worth to refactor ourselves". This situation doesn't qualify. Lame would be standing idle while the precision of language is loosened into meaninglessness.
Fuck that. This is a simple patch.
and fuck the horse you rode in on.


In my opinion putting it forward as a suggestion without also inviting the user to recognize that clamping at 80% would have an extreme effect on melee player balance because of the overall DPS change is poor form.

At the very least be more imaginative in how to apply bottom end clamping. Lots of numbers to play with here. For example: Square root of player level times player weapon skill, plus square root of attack factor plus 1 times weapon attack. I mean, this is a perfect place to inject permanent bonuses as quest rewards. 🤷‍♂️
 
Back
Top