• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Lua] Floating text damage with critical hit chance

Status
Not open for further replies.
Level 1
Joined
Jun 10, 2019
Messages
4
This should be a simple way to show the damage as floating text numbers. Made it for an RPG-Map with only one hero each player.
Auto-attacks will displayed as white and all other attacks will displayed as yellow numbers. Every damage has a chance to deal critical damage. Crit damage will displayed bigger then normal hits.
You will need two more variables:

stats_critchance = XX (actual crit chance for all your units)
stats_critdamage = X.X (for 50% more damage use 1.5, double damage on crit 2.0, ...and so on)

what do you guys think?

Lua:
function show_textdamage()
    local trigger = CreateTrigger()
    TriggerRegisterAnyUnitEventBJ(trigger, EVENT_PLAYER_UNIT_DAMAGED)   
    TriggerAddAction(trigger, function()
        local unit = BlzGetEventDamageTarget() 
        local source = GetEventDamageSource()
        local damage = GetEventDamage()
        local textpaint_g = nil
        local textpaint_b = nil
        local textsize = nil
        local player = bj_FORCE_PLAYER[GetPlayerId(GetOwningPlayer(source))] 
  
        local random_int = GetRandomInt(1, 100)
        local random_facing = GetRandomInt(-15, 15)
        if BlzGetEventIsAttack() == true then 
            textpaint_g = 255
            textpaint_b = 255
        else
            textpaint_g = 215
            textpaint_b = 0
        end
                
        if random_int >= stats_critchance then 
            textsize = 12
            BlzSetEventDamage(damage)
        else
            textsize = 20                                
            BlzSetEventDamage(damage * stats_critdamage) 
            damage = damage * stats_critdamage  
        end
        local tag = CreateTextTagUnitBJ(math.floor( damage ), unit, 5.00, textsize, 255, textpaint_g, textpaint_b, 0)
        SetTextTagVelocityBJ(tag, 270, GetUnitFacing(source) + random_facing )
        SetTextTagPermanentBJ(tag, false)
        SetTextTagLifespanBJ(tag, 1)
        SetTextTagFadepointBJ(tag, 0)
        ShowTextTagForceBJ(false, tag, GetPlayersAll())
        ShowTextTagForceBJ(true, tag, player)        
    end)
end
 
Last edited:

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,543
Nothing happens if random_int is equal to stats_critchance.

Also, you aren't taking advantage of ELSE in your If Then Else statements. There's no need to ask questions like "Is random int less than crit chance?" when you've already determined that it was greater than it.

You can simplify it even further to this:
Lua:
        if BlzGetEventIsAttack() == true then --is damage from autoattack? Yes, textcolor white
            textpaint_g = 255
            textpaint_b = 255
        else --is damage from autoattack? No, textcolor yellow
            textpaint_g = 215
            textpaint_b = 0
        end
        if random_int > stats_critchance then --check for no critical hit. For example if you gain 60% critchance, random_int needs to be between 61-100 to NOT crit.
            textsize = 12
            BlzSetEventDamage(damage)
        else   --check for critical hit. If you gain 60% critchance, stats_critchance is 60 and must be higher then random_int
            textsize = 20              --critical hits bigger on screen then normal hits.
            BlzSetEventDamage(damage * stats_critdamage) --if true, incoming damage gets duplicated with stats_critdamage (1.5 on default), so damage is 50% higher den normal hit.
            damage = damage * stats_critdamage 
        end
You can define textpaint_r as 255 instead of Nil since that's it's value for both attacks/non-attacks. Saves you 2 lines of code.

Also, ideally you would avoid using BJ functions when a non-BJ version exists, at least that's the general rule of thumb. Although, it's probably not that big of a deal for floating text stuff.

Additionally, GetForceOfPlayer() leaks a Player Group. A simple fix to this is to overwrite the function by placing this code somewhere in your map:
Lua:
function GetForceOfPlayer(player)
    return bj_FORCE_PLAYER[GetPlayerId(player)]
end
Thanks to Tasyen for the idea.

That's all I can think of. Good luck with your map!
 
Last edited:
Level 1
Joined
Jun 10, 2019
Messages
4
Nothing happens if random_int is equal to stats_critchance.

Also, you aren't taking advantage of ELSE in your If Then Else statements. There's no need to ask questions like "Is random int less than crit chance?" when you've already determined that it was greater than it.

You can simplify it even further to this:
Lua:
        if BlzGetEventIsAttack() == true then --is damage from autoattack? Yes, textcolor white
            textpaint_g = 255
            textpaint_b = 255
        else --is damage from autoattack? No, textcolor yellow
            textpaint_g = 215
            textpaint_b = 0
        end
        if random_int >= stats_critchance then --check for no critical hit. For example if you gain 60% critchance, random_int needs to be between 61-100 to NOT crit.
            textsize = 12
            BlzSetEventDamage(damage)
        else   --check for critical hit. If you gain 60% critchance, stats_critchance is 60 and must be higher then random_int
            textsize = 20              --critical hits bigger on screen then normal hits.
            BlzSetEventDamage(damage * stats_critdamage) --if true, incoming damage gets duplicated with stats_critdamage (1.5 on default), so damage is 50% higher den normal hit.
            damage = damage * stats_critdamage 
        end
You can define textpaint_r as 255 instead of Nil since that's it's value for both attacks/non-attacks. Saves you 2 lines of code.

Also, ideally you would avoid using BJ functions when a non-BJ version exists, at least that's the general rule of thumb. Although, it's probably not that big of a deal for floating text stuff.

Additionally, GetForceOfPlayer() leaks a Player Group. A simple fix to this is to overwrite the function by placing this code somewhere in your map:
Lua:
function GetForceOfPlayer(player)
    return bj_FORCE_PLAYER[GetPlayerId(player)]
end
Thanks to Tasyen for the idea.

That's all I can think of. Good luck with your map!

Thanks for the advice, Uncle. I implemented it right away. Lua programming is still pretty new to me, so I appreciate any advice. :)
 
Status
Not open for further replies.
Top