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

[JASS] Problem with on attack script

Status
Not open for further replies.
Level 4
Joined
Nov 22, 2004
Messages
62
Ok im making a map with a total overhaul of the warcraft 3 attack and damage systems that enables hit/miss and criticals based on stats.

the thing is that i have four codes for attacking:
hero on hero
hero on unit
unit on hero
unit on unit

the reason i did this way is because doing everything in one could make the trigger giant and hard to mix... (i tried but all i got was more errors.)

so i started on GUI then passed to custom text and started messing with it,
using locals and reducing size ( specially the ''if'' stuff)

the thing is that, it was working before but now it isnt.

JASS:
function Trig_Hero_Damage_Hero_Func004C takes nothing returns boolean
    if ( not ( IsUnitType(GetAttacker(), UNIT_TYPE_HERO) == true ) ) then
        return false
    endif
    if ( not ( IsUnitType(GetAttackedUnitBJ(), UNIT_TYPE_HERO) == true ) ) then
        return false
    endif
    return true
endfunction

function Trig_Hero_Damage_Hero_Conditions takes nothing returns boolean
    if ( not Trig_Hero_Damage_Hero_Func004C() ) then
        return false
    endif
    return true
endfunction

// End Conditions (i dont know how to reduce this part...

function Trig_Hero_Damage_Hero_Actions takes nothing returns nothing
    local unit a = GetAttacker()          // Gets units and creates locals
    local unit b = GetAttackedUnitBJ()
    local location loc = GetUnitLoc(a)
    local real h
    local real c
    local real d
    local real def
    // Heroes Type   (detects heroes to deal damage based on int/str/agi
    if ( GetUnitTypeId(a) == 'U002' ) then
        set d = ( I2R(GetHeroStatBJ(bj_HEROSTAT_INT, a, true)) * 1.40 )
    else
    endif
    if ( GetUnitTypeId(a) == 'Ulic' ) then
        set d = ( I2R(GetHeroStatBJ(bj_HEROSTAT_INT, a, true)) * 1.40 )
    else
    endif
    if ( GetUnitTypeId(a) == 'Hamg' ) then
        set d = ( I2R(GetHeroStatBJ(bj_HEROSTAT_INT, a, true)) * 1.40 )
    else
    endif
    if ( GetUnitTypeId(a) == 'Obla' ) then
        set d = ( I2R(GetHeroStatBJ(bj_HEROSTAT_AGI, a, true)) * 1.40 )
    else
    endif
    if ( GetUnitTypeId(a) == 'O001' ) then
        set d = ( I2R(GetHeroStatBJ(bj_HEROSTAT_AGI, a, true)) * 1.40 )
    else
    endif
    if ( GetUnitTypeId(a) == 'H003' ) then
        set d = ( I2R(GetHeroStatBJ(bj_HEROSTAT_STR, a, true)) * 1.40 )
    else
    endif
    if ( GetUnitTypeId(a) == 'Hpal' ) then
        set d = ( I2R(GetHeroStatBJ(bj_HEROSTAT_STR, a, true)) * 1.40 )
    else
    endif
    if ( GetUnitTypeId(a) == 'H000' ) then
        set d = ( I2R(GetHeroStatBJ(bj_HEROSTAT_STR, a, true)) * 1.40 )
    else
    endif
    // End Heroes
    set h = I2R(( GetHeroStatBJ(bj_HEROSTAT_AGI, a, true) + GetHeroLevel(a) ))   //sets hit variable
    set def = I2R(( ( GetHeroStatBJ(bj_HEROSTAT_AGI, b, true) + GetHeroLevel(b) ) / 2 ))  //sets enemy hero deffense
    set c = ( I2R(GetHeroStatBJ(bj_HEROSTAT_AGI, a, true)) / 10.00 )  //sets critical hit chance
    if (  ( h / ( h + def ) ) >= GetRandomReal(0, 1)  ) then     //checks hit against def
        if (  ( c / 100.00 ) < GetRandomReal(0, 1)  ) then     //checks critical
            set d = ( GetRandomReal(0.85, 1.15) * d )         //randomizes damage
            call TriggerSleepAction( 0.35 )                //wait between start of animation and character hits enemy
            if ( loc == GetUnitLoc(a) ) then             // checks if attacker is at same location as it was when started animation to avoid an exploit
                call UnitDamageTargetBJ( a, b, d, ATTACK_TYPE_MELEE, DAMAGE_TYPE_NORMAL )  //deals the damage
                call CreateTextTagUnitBJ( I2S(R2I(d)), b, 0, 9.00, 100.00, 100, 100, 0 )  //creates floating text
                call SetTextTagVelocityBJ( GetLastCreatedTextTag(), 64, 90 )     //sets floating text velocity
            else
            endif
        else
            set d = ( 2.00 * d )      // doubles damage for critical
            set d = ( GetRandomReal(0.85, 1.15) * d )    // randomizes critical
            call TriggerSleepAction( 0.35 )         //from here till the end is almost the same as above
            if ( loc == GetUnitLoc(a) ) then        
                call UnitDamageTargetBJ( a, b, d, ATTACK_TYPE_MELEE, DAMAGE_TYPE_NORMAL )
                call CreateTextTagUnitBJ( I2S(R2I(d)), b, 0, 12.00, 100.00, 100, 100, 0 )
                call SetTextTagVelocityBJ( GetLastCreatedTextTag(), 32.00, 90 )
            else
            endif
        endif
    else
        call TriggerSleepAction( 0.35 )
        call CreateTextTagUnitBJ( "TRIGSTR_099", b, 0, 9.00, 100.00, 100, 100, 0 )
        call SetTextTagVelocityBJ( GetLastCreatedTextTag(), 64, 90 )
    endif
    call SetTextTagPermanentBJ( GetLastCreatedTextTag(), false )
    call SetTextTagLifespanBJ( GetLastCreatedTextTag(), 0.50 )
    call SetTextTagLifespanBJ( GetLastCreatedTextTag(), 0.30 )
endfunction

//===========================================================================
function InitTrig_Hero_Damage_Hero takes nothing returns nothing
    set gg_trg_Hero_Damage_Hero = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Hero_Damage_Hero, EVENT_PLAYER_UNIT_ATTACKED )
    call TriggerAddCondition( gg_trg_Hero_Damage_Hero, Condition( function Trig_Hero_Damage_Hero_Conditions ) )
    call TriggerAddAction( gg_trg_Hero_Damage_Hero, function Trig_Hero_Damage_Hero_Actions )
endfunction

Thanks in advance.
PS: in kinda new to jass, but now that im learning C jass just seems more... beautifull...(and clean and better to do stuff... overal EXCELENT)
 
Level 8
Joined
Jul 23, 2005
Messages
329
JASS:
function Trig_Hero_Damage_Hero_Conditions takes nothing returns boolean
    return IsUnitType(GetAttacker(), UNIT_TYPE_HERO) == true and IsUnitType(GetTriggerUnit(), UNIT_TYPE_HERO) == true
endfunction

// End Conditions (i dont know how to reduce this part...

function Trig_Hero_Damage_Hero_Actions takes nothing returns nothing
    local unit a = GetAttacker()          // Gets units and creates locals
    local unit b = GetTriggerUnit()
    local location loc = GetUnitLoc(a)
    local real h
    local real c
    local real d
    local real def
    // Heroes Type   (detects heroes to deal damage based on int/str/agi
    if ( GetUnitTypeId(a) == 'U002' ) then
        set d = ( I2R(GetHeroInt( a, true)) * 1.40 )
    elseif ( GetUnitTypeId(a) == 'Ulic' ) then
        set d = ( I2R(GetHeroInt( a, true)) * 1.40 )
    elseif ( GetUnitTypeId(a) == 'Hamg' ) then
        set d = ( I2R(GetHeroInt( a, true)) * 1.40 )
    elseif ( GetUnitTypeId(a) == 'Obla' ) then
        set d = ( I2R(GetHeroAgi( a, true)) * 1.40 )
    elseif ( GetUnitTypeId(a) == 'O001' ) then
        set d = ( I2R(GetHeroAgi( a, true)) * 1.40 )
    elseif ( GetUnitTypeId(a) == 'H003' ) then
        set d = ( I2R(GetHeroStr( a, true)) * 1.40 )
    elseif ( GetUnitTypeId(a) == 'Hpal' ) then
        set d = ( I2R(GetHeroStr( a, true)) * 1.40 )
    elseif ( GetUnitTypeId(a) == 'H000' ) then
        set d = ( I2R(GetHeroStr( a, true)) * 1.40 )
    endif
    // End Heroes
    set h = I2R(( GetHeroAgi( a, true) + GetHeroLevel(a) ))   //sets hit variable
    set def = I2R(( ( GetHeroAgi( b, true) + GetHeroLevel(b) ) / 2 ))  //sets enemy hero deffense
    set c = ( I2R(GetHeroAgi( a, true)) / 10.00 )  //sets critical hit chance
    if (  ( h / ( h + def ) ) >= GetRandomReal(0, 1)  ) then     //checks hit against def
        if (  ( c / 100.00 ) < GetRandomReal(0, 1)  ) then     //checks critical
            set d = ( GetRandomReal(0.85, 1.15) * d )         //randomizes damage
            call TriggerSleepAction( 0.35 )                //wait between start of animation and character hits enemy
            if ( loc == GetUnitLoc(a) ) then             // checks if attacker is at same location as it was when started animation to avoid an exploit
                call UnitDamageTargetBJ( a, b, d, ATTACK_TYPE_MELEE, DAMAGE_TYPE_NORMAL )  //deals the damage
                call CreateTextTagUnitBJ( I2S(R2I(d)), b, 0, 9.00, 100.00, 100, 100, 0 )  //creates floating text
                call SetTextTagVelocityBJ( GetLastCreatedTextTag(), 64, 90 )     //sets floating text velocity
            endif
        else
            set d = ( 2.00 * d )      // doubles damage for critical
            set d = ( GetRandomReal(0.85, 1.15) * d )    // randomizes critical
            call TriggerSleepAction( 0.35 )         //from here till the end is almost the same as above
            if ( loc == GetUnitLoc(a) ) then        
                call UnitDamageTargetBJ( a, b, d, ATTACK_TYPE_MELEE, DAMAGE_TYPE_NORMAL )
                call CreateTextTagUnitBJ( I2S(R2I(d)), b, 0, 12.00, 100.00, 100, 100, 0 )
                call SetTextTagVelocityBJ( GetLastCreatedTextTag(), 32.00, 90 )
            endif
        endif
    else
        call TriggerSleepAction( 0.35 )
        call CreateTextTagUnitBJ( "TRIGSTR_099", b, 0, 9.00, 100.00, 100, 100, 0 )
        call SetTextTagVelocityBJ( GetLastCreatedTextTag(), 64, 90 )
    endif
    call SetTextTagPermanentBJ( GetLastCreatedTextTag(), false )
    call SetTextTagLifespanBJ( GetLastCreatedTextTag(), 0.50 )
    call SetTextTagLifespanBJ( GetLastCreatedTextTag(), 0.30 )
    set a = null
    set b = null
    set loc = null
endfunction

//===========================================================================
function InitTrig_Hero_Damage_Hero takes nothing returns nothing
    set gg_trg_Hero_Damage_Hero = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Hero_Damage_Hero, EVENT_PLAYER_UNIT_ATTACKED )
    call TriggerAddCondition( gg_trg_Hero_Damage_Hero, Condition( function Trig_Hero_Damage_Hero_Conditions ) )
    call TriggerAddAction( gg_trg_Hero_Damage_Hero, function Trig_Hero_Damage_Hero_Actions )
endfunction
First, let's simplify the conditions. Next, Let's remove some BJs (Not all of them though. Kinda busy atm so I can't help with much). Er, you know, not every if statement needs an else. If you don't need the else, don't use it. Finally, remove the leaks...




Efficiencies aside, I can't see why this isn't working.
 
Level 4
Joined
Nov 22, 2004
Messages
62
Thanks alot Preskooldude!

does leaks include local var leaks? (i read that there is local vars that can leak)
by the way dont need to do all stuff for me i just need some directions, i think i can work my way out byt your advices =)

removing BJ's == putting natives i guess

k i remoeved some BJs (because i dont see how removing texttag coud make better)

JASS:
function Trig_Hero_Damage_Hero_Conditions takes nothing returns boolean
    return IsUnitType(GetAttacker(), UNIT_TYPE_HERO) == true and IsUnitType(GetTriggerUnit(), UNIT_TYPE_HERO) == true
endfunction

// End Conditions

function Trig_Hero_Damage_Hero_Actions takes nothing returns nothing
    local unit a = GetAttacker()          // Gets units and creates locals
    local unit b = GetTriggerUnit()
    local location loc = GetUnitLoc(a)
    local real h
    local real c
    local real d
    local real def
    // Heroes Type   (detects heroes to deal damage based on int/str/agi
    if ( GetUnitTypeId(a) == 'U002' ) then
        set d = ( I2R(GetHeroInt( a, true)) * 1.40 )
    endif
    if ( GetUnitTypeId(a) == 'Ulic' ) then
        set d = ( I2R(GetHeroInt( a, true)) * 1.40 )
    endif
    if ( GetUnitTypeId(a) == 'Hamg' ) then
        set d = ( I2R(GetHeroInt( a, true)) * 1.40 )
    endif
    if ( GetUnitTypeId(a) == 'Obla' ) then
        set d = ( I2R(GetHeroAgi( a, true)) * 1.40 )
    endif
    if ( GetUnitTypeId(a) == 'O001' ) then
        set d = ( I2R(GetHeroAgi( a, true)) * 1.40 )
    else
    endif
    if ( GetUnitTypeId(a) == 'H003' ) then
        set d = ( I2R(GetHeroStr( a, true)) * 1.40 )
    endif
    if ( GetUnitTypeId(a) == 'Hpal' ) then
        set d = ( I2R(GetHeroStr( a, true)) * 1.40 )
    endif
    if ( GetUnitTypeId(a) == 'H000' ) then
        set d = ( I2R(GetHeroStr( a, true)) * 1.40 )
    endif
    // End Heroes
    set h = I2R(( GetHeroAgi( a, true) + GetHeroLevel(a) ))   //sets hit variable
    set def = I2R(( ( GetHeroAgi( b, true) + GetHeroLevel(b) ) / 2 ))  //sets enemy hero deffense
    set c = ( I2R(GetHeroAgi( a, true)) / 10.00 )  //sets critical hit chance
    if (  ( h / ( h + def ) ) >= GetRandomReal(0, 1)  ) then     //checks hit against def
        if (  ( c / 100.00 ) < GetRandomReal(0, 1)  ) then     //checks critical
            set d = ( GetRandomReal(0.85, 1.15) * d )         //randomizes damage
            call TriggerSleepAction( 0.35 )                //wait between start of animation and character hits enemy
            if ( loc == GetUnitLoc(a) ) then             // checks if attacker is at same location as it was when started animation to avoid an exploit
                call UnitDamageTarget( a, b, d, true, true, ATTACK_TYPE_MELEE, DAMAGE_TYPE_NORMAL , WEAPON_TYPE_WHOKNOWS )  //deals the damage
                call CreateTextTagUnitBJ( I2S(R2I(d)), b, 0, 9.00, 100.00, 100, 100, 0 )  //creates floating text
                call SetTextTagVelocityBJ( GetLastCreatedTextTag(), 64, 90 )     //sets floating text velocity
            endif
        else
            set d = ( 2.00 * d )      // doubles damage for critical
            set d = ( GetRandomReal(0.85, 1.15) * d )    // randomizes critical
            call TriggerSleepAction( 0.35 )         //from here till the end is almost the same as above
            if ( loc == GetUnitLoc(a) ) then        
                call UnitDamageTarget( a, b, d, true, true, ATTACK_TYPE_MELEE, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_WHOKNOWS )
                call CreateTextTagUnitBJ( I2S(R2I(d)), b, 0, 12.00, 100.00, 100, 100, 0 )
                call SetTextTagVelocityBJ( GetLastCreatedTextTag(), 32.00, 90 )
            endif
        endif
    else
        call TriggerSleepAction( 0.35 )
        call CreateTextTagUnitBJ( "TRIGSTR_099", b, 0, 9.00, 100.00, 100, 100, 0 )
        call SetTextTagVelocityBJ( GetLastCreatedTextTag(), 64, 90 )
    endif
    call SetTextTagPermanent( GetLastCreatedTextTag(), false )
    call SetTextTagLifespan( GetLastCreatedTextTag(), 0.50 )
    call SetTextTagFadepoint( GetLastCreatedTextTag(), 0.30 )
endfunction

//===========================================================================
function InitTrig_Hero_Damage_Hero takes nothing returns nothing
    set gg_trg_Hero_Damage_Hero = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Hero_Damage_Hero, EVENT_PLAYER_UNIT_ATTACKED )
    call TriggerAddCondition( gg_trg_Hero_Damage_Hero, Condition( function Trig_Hero_Damage_Hero_Conditions ) )
    call TriggerAddAction( gg_trg_Hero_Damage_Hero, function Trig_Hero_Damage_Hero_Actions )
endfunction

now im gonna work on the leaks..
 
Last edited:
Level 4
Joined
Nov 22, 2004
Messages
62
cool teach me some C after you learn (strings especially)

Strings? what do you want to know? im not a genius so ill try my best =D

Im still looking at my code, its not working properly (started having problems when i introduced a exploit fix)

i just got home now ill take a look.. if someone can give me a heads up...
if you wanna test this code just set it so all damages does 0% to all armor (but normal) and change the '0001' and stuff to a heroes name code. it only works for heroes hitting heroes.

PS jasscraft is awesome.
 
Level 4
Joined
Nov 22, 2004
Messages
62
Just did... damn i am soo dumb why havent i thought on it before...
jass is soo easy to do now heh
 
Status
Not open for further replies.
Top