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

[Trigger] Get exact mana cost of spell

Status
Not open for further replies.
Level 7
Joined
Mar 8, 2009
Messages
360
Is there a way to get exact mana cost of spell?

This is what i tried but it isn't exact due to mana regen:
  • Untitled Trigger 001
    • Events
      • Unit - A unit Begins casting an ability
    • Conditions
    • Actions
      • Set mana1 = (Mana of (Triggering unit))
      • Wait 0.50 seconds
      • Set mana2 = (Mana of (Triggering unit))
      • Game - Display to (All players) the text: (Manacost + (String((mana1 - mana2))))
(I was to lazy to make it mui in jass)
 
Element's right, you can also do this:
  • Trigger1
  • Events
    • Unit - A unit begins casting an ability
  • Conditions
  • Actions
    • Set Mana[1] = (Mana of (Triggering unit))
  • Trigger2
  • Events
    • Unit - A unit starts the effect of an ability
  • Conditions
  • Actions
    • Set Mana[2] = (Mana of (Triggering unit))
    • Set Mana[3] = (Mana[1]-Mana[2]
    • Game - Display to (All Players) the Text: (String(Mana[3]))
It should work.
 
Level 6
Joined
Mar 20, 2008
Messages
208
Make a dummy unit, in the object editor set the dummy's mana regeneration to 0, then set the dummy's mana to the casters mana as the spell starts?.
 
Wait a sec and I'll make a function to get it...

EDIT: Done. It's usually accurate, and it's ALWAYS accurate first time it's called. It sets the real variable ManaRegen to the unit's mana regen. The only problem is that it's not entirely MUI. If it's called twice within 0.01 seconds it will bug.
JASS:
globals
    real OldMana
    unit GetRegenUnit
endglobals

function GetUnitManaRegenCallback takes nothing returns nothing
    call DestroyTimer(GetExpiredTimer())
    set udg_ManaRegen = GetUnitState(GetRegenUnit, UNIT_STATE_MANA) * 100.00
    call SetUnitState(GetRegenUnit, UNIT_STATE_MANA, OldMana)
endfunction
    

function GetUnitManaRegen takes unit u returns nothing
    set OldMana = GetUnitState(u, UNIT_STATE_MANA)
    set GetRegenUnit = u
    call SetUnitState(u, UNIT_STATE_MANA, 0)
    call TimerStart(CreateTimer(), 0.01, false, function GetUnitManaRegenCallback)
endfunction
 
Level 7
Joined
Mar 8, 2009
Messages
360
I tried to make a trigger that shows message of manacost but nothing happens, i think loop never end. I tried with debug messages at start of Trig_Check_mana_Actions and setCast and they show up in game. And its is also my first vJass i ever tried(and i must admit that i never read any vjass tutorial):

JASS:
globals
    boolean hasCast = false
    real OldMana
    unit GetRegenUnit
    real ManaRegen
endglobals

function GetUnitManaRegenCallback takes nothing returns nothing
    call DestroyTimer(GetExpiredTimer())
    set ManaRegen = GetUnitState(GetRegenUnit, UNIT_STATE_MANA) * 100.00
    call SetUnitState(GetRegenUnit, UNIT_STATE_MANA, OldMana)
endfunction

function GetUnitManaRegen takes unit u returns nothing
    set OldMana = GetUnitState(u, UNIT_STATE_MANA)
    set GetRegenUnit = u
    call SetUnitState(u, UNIT_STATE_MANA, 0)
    call TimerStart(CreateTimer(), 0.01, false, function GetUnitManaRegenCallback)
endfunction

function Trig_Check_mana_Actions takes nothing returns nothing
    local real mana1 = GetUnitStateSwap(UNIT_STATE_MANA, GetTriggerUnit())
    local real mana2
    local unit caster = GetSpellAbilityUnit()
    local timer t = CreateTimer()
    local real regenTime
    call TimerStart(t, 5.00, false, null)
    loop
        exitwhen ( hasCast == true )
        call TriggerSleepAction(0.10)
    endloop
    set mana2 = GetUnitStateSwap(UNIT_STATE_MANA, GetTriggerUnit())
    call GetUnitManaRegen(caster)
    set regenTime = 5.00 - TimerGetRemaining(t)
    call DisplayTextToForce( GetPlayersAll(), ( "Manacost " + R2S((mana1 - mana2) + regenTime*ManaRegen) ) )
endfunction

function SetCast takes nothing returns nothing
    set hasCast = true
endfunction

//===========================================================================
function InitTrig_Check_mana takes nothing returns nothing
    local trigger gg_trg_Check_mana = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Check_mana, EVENT_PLAYER_UNIT_SPELL_CAST )
    call TriggerAddAction( gg_trg_Check_mana, function Trig_Check_mana_Actions )
    set gg_trg_Check_mana = null
endfunction

function InitTrig_Check_mana2 takes nothing returns nothing
    local trigger gg_trg_Check_mana2 = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Check_mana2, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddAction( gg_trg_Check_mana2, function SetCast )
    set gg_trg_Check_mana2 = null
endfunction

I wish there was a native like GetManaCostOfCastSpell() :(
 
Bah, it's barely vJass. The only vJass feature in this is globals blocks. I think your problem is that InitTrig_Check_mana2 never fires. It doesn't just fire on its own you know. Only InitTrig_+<Name of trigger> fires automatically. Put the actions in InitTrig_Check_mana2 inside InitTrig_Check_mana. Then it should work. Oh, and what's the timer about in your actions? You start it, then never use it.
 
Level 7
Joined
Mar 8, 2009
Messages
360
Now it works for 75%, the time is not very accurate yet, it uses regen of spell cast before last spell and i gonna make it mui, this is what i have now (in case you are bored and have ideas on how to mui'fy it :grin:).

@element - Will give you rep when i can again, already gave you rep before

JASS:
globals
    boolean hasCast = false
    real OldMana
    unit GetRegenUnit
    real ManaRegen
endglobals

function isHero takes nothing returns boolean
    return IsUnitType(GetTriggerUnit(), UNIT_TYPE_HERO)
endfunction

function GetUnitManaRegenCallback takes nothing returns nothing
    call DestroyTimer(GetExpiredTimer())
    set ManaRegen = GetUnitState(GetRegenUnit, UNIT_STATE_MANA) * 100
    call SetUnitState(GetRegenUnit, UNIT_STATE_MANA, OldMana)
endfunction

function GetUnitManaRegen takes unit u returns nothing
    set OldMana = GetUnitState(u, UNIT_STATE_MANA)
    set GetRegenUnit = u
    call SetUnitState(u, UNIT_STATE_MANA, 0)
    call TimerStart(CreateTimer(), 0.01, false, function GetUnitManaRegenCallback)
endfunction

function Trig_Check_mana_Actions takes nothing returns nothing
    local real mana1 = GetUnitStateSwap(UNIT_STATE_MANA, GetTriggerUnit())
    local real mana2
    local unit caster = GetSpellAbilityUnit()
    local timer t = CreateTimer()
    local real regenTime
    call TimerStart(t, 1.00, false, null)
    loop
        exitwhen ( hasCast == true )
        call TriggerSleepAction(0.10)
    endloop    
    set hasCast = false
    set regenTime = 1.00 - TimerGetRemaining(t)
    call DestroyTimer(t)
    set mana2 = GetUnitStateSwap(UNIT_STATE_MANA, GetTriggerUnit())   
    call GetUnitManaRegen(caster)
    call DisplayTextToForce( GetPlayersAll(), ( "Mana1 " + R2S(mana1)) )
    call DisplayTextToForce( GetPlayersAll(), ( "Mana2 " + R2S(mana2)) )
    call DisplayTextToForce( GetPlayersAll(), ( "regen " + R2S(ManaRegen)) )
    call DisplayTextToForce( GetPlayersAll(), ( "Manacost " + R2S((mana1 - mana2) + regenTime*ManaRegen) ) )
endfunction

function SetCast takes nothing returns nothing
    set hasCast = true
endfunction

//==========================================================================
function InitTrig_Check_mana takes nothing returns nothing
    local trigger gg_trg_Check_mana = CreateTrigger(  )
    local trigger gg_trg_Check_mana2 = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Check_mana, EVENT_PLAYER_UNIT_SPELL_CAST )
    call TriggerAddCondition( gg_trg_Check_mana, Condition(function isHero ) )
    call TriggerAddAction( gg_trg_Check_mana, function Trig_Check_mana_Actions )
    set gg_trg_Check_mana = null    
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Check_mana2, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( gg_trg_Check_mana2, Condition(function isHero ) )
    call TriggerAddAction( gg_trg_Check_mana2, function SetCast )
    set gg_trg_Check_mana2 = null
endfunction
 
It;s quite long and complicated so far, but i think you should be able to understand it... gotta go to school now anyway, so I'll finish it tonight.

EDIT: Ok, I think I've finished it but... it doesn't really get the exact mana cost of the spell - Flame Strike costs 130 mana, the system says 131.254... But I think what I've got is the best I can get.

EDIT2: I found the problem... I can't get heroes' mana regeneration correctly with the way I'm using without knowing how much bonus mana regen you get per intelligence point. I'll look it up in game constants.

EDIT3: Yey! I made the mana regen thing consider the hero's intelligence and now the system is accurate to 0.001 mana (130 mana returns 130.001). I'll just clean up the code and stuff then I'll post it.

EDIT4: Done. It's attached. To use it you must first register the spell with the system, by calling call GetSpellMana_RegisterSpell(SPELL_RAWCODE). The code will then print out the mana cost of the spell after it has been cast. If the unit which you expect to cast the spell does not exist when that function is called, the system will not work properly when that unit casts the spell unless you call call InitUnitManaRegen(UNIT_ID).
 

Attachments

  • GetSpellManaCost.w3x
    29.1 KB · Views: 40
Last edited:
Status
Not open for further replies.
Top