HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Port of GameCache to Structs. Can someone check my 1st attempt?

05-21-2007, 11:16 AM#1
n13astra
Hi,

It was a modified version of ChuckleBrothers Attack Detection Engine in my map and i wanted to learn how to use structs so i thought it would be a good starting point to convert the Engine to use Structs, thus making it faster since it was causing lag in my map.

Can someone check over it please? Rep Given.

Edit: Looking over the code, ive realised that it leaks events which you cant help but i would like to know is should i be creating a trigger for each unit as is below or should i have the same trigger and just add the unit event to it each time a unit enters play? Is there an advantage doing it via 1 function? I know the system was used to allow multi different fuctions for each unit but i dont really need that functionality so is it worth removing it?

Here is the original. Ive removed a lot the code that i didnt need in mine.
http://www.wc3campaigns.net/showthread.php?t=78316

Collapse JASS:
function H2I takes handle h returns integer
    return h
    return 0
endfunction

// ===========================
function LocalVars takes nothing returns gamecache
    if udg_cscache == null then
        call FlushGameCache(InitGameCache("LocalVars.vx"))
        set udg_cscache = InitGameCache("LocalVars.vx")
    endif
 return udg_cscache
endfunction

function SetHandleInt takes handle subject, string name, integer value returns nothing
    if value==0 then
        call FlushStoredInteger(LocalVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(LocalVars(), I2S(H2I(subject)), name, value)
    endif
endfunction

function GetHandleInt takes handle subject, string name returns integer
    return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
endfunction


function I2T takes integer trigger_handle returns trigger
    return trigger_handle
    return null
endfunction

function I2TA takes integer trigger_handle returns triggeraction
    return trigger_handle
    return null
endfunction

function FlushHandleLocals takes handle subject returns nothing
    call FlushStoredMission(LocalVars(), I2S(H2I(subject)) )
endfunction

struct DMG_Func
    integer Action
    integer Dmg
endstruct

function DamageTypeTest takes nothing returns nothing
    local unit u=GetEventDamageSource()
    local string dmg=R2S(GetEventDamage())
    local string s=GetUnitName(u) + " dealt " + dmg + " damage."
    
    call ClearTextMessages()
    call DisplayTimedTextToPlayer(Player(0), 0.0, 0.0, 10.00, s)
endfunction

function IsAssimilateOrHeroUnit takes unit U returns boolean
    if((IsUnitType(U, UNIT_TYPE_HERO) == true) or (GetUnitTypeId(U) == 'h01B') or (GetUnitTypeId(U) == 'h01Q') or (GetUnitTypeId(U) == 'h01N') or (GetUnitTypeId(U) == 'h01P') or (GetUnitTypeId(U) == 'h01U') or (GetUnitTypeId(U) == 'h01V') or (GetUnitTypeId(U) == 'h01Z')) then
        return true 
    endif
    return false 
endfunction

function GenericDamage_Conditions takes nothing returns boolean
    if (IsAssimilateOrHeroUnit(GetEventDamageSource()) == true) then
        return true  
    endif
    return false
endfunction

function GenericDamage_DamageTaken takes nothing returns nothing
    call ExecuteFunc("DamageTypeTest")
endfunction

function GenericDamage_AddTriggers takes nothing returns nothing
    local trigger dmgtrigger = CreateTrigger()
    local DMG_Func dmgdata = DMG_Func.create()
    
    call SetHandleInt(GetTriggerUnit(), "dmgdata", dmgdata)  //Attach the Struct to the Unit
    call TriggerRegisterUnitEvent(dmgtrigger, GetTriggerUnit(), EVENT_UNIT_DAMAGED)
    call TriggerAddCondition(dmgtrigger, Condition(function GenericDamage_Conditions))
    set dmgdata.Action = H2I(TriggerAddAction(dmgtrigger, function GenericDamage_DamageTaken))
    set dmgdata.Dmg = H2I(dmgtrigger)    
    
    call BJDebugMsg("ADDED" + I2S(dmgdata.Action))
    
    set dmgtrigger = null
endfunction

function GenericDamage_RemoveTriggers takes nothing returns nothing
    local DMG_Func dmgdata = GetHandleInt(GetTriggerUnit(), "dmgdata")
    local trigger me = I2T(dmgdata.Dmg)
    local triggeraction ma = I2TA(dmgdata.Action)

    if (IsAssimilateOrHeroUnit(GetTriggerUnit())) then
        set me = null
        set ma = null
        return
    endif

    call BJDebugMsg("REMOVED" + I2S(dmgdata.Action))

    call TriggerClearConditions(me)
    call TriggerRemoveAction(me,ma)
    call DestroyTrigger(me)
    call FlushHandleLocals(GetTriggerUnit())
    call dmgdata.destroy()
    
    set me = null
    set ma = null
endfunction

function GenericDamage_InitialiseSystem takes nothing returns nothing
    local trigger entermap = CreateTrigger()
    local trigger upondeath = CreateTrigger()
    local trigger dmgtrigger
    local group g = CreateGroup()
    local unit u
    local DMG_Func dmgdata

    call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, null)
    loop
        set u = FirstOfGroup(g)
        exitwhen(u == null)
        set dmgdata = DMG_Func.create()   //Create the Struct to store the Unit DMG Data
        set dmgtrigger = CreateTrigger()
        call SetHandleInt(u, "dmgdata", dmgdata)  //Attach the Struct to the Unit
        call TriggerRegisterUnitEvent(dmgtrigger, u, EVENT_UNIT_DAMAGED)
        call TriggerAddCondition(dmgtrigger, Condition(function GenericDamage_Conditions))
        set dmgdata.Action = H2I(TriggerAddAction(dmgtrigger, function GenericDamage_DamageTaken))
        set dmgdata.Dmg = H2I(dmgtrigger)
        call GroupRemoveUnit(g,u)
    endloop

    call TriggerRegisterEnterRectSimple(entermap, bj_mapInitialPlayableArea)
    call TriggerAddAction(entermap, function GenericDamage_AddTriggers)

    call TriggerRegisterAnyUnitEventBJ(upondeath, EVENT_PLAYER_UNIT_DEATH)
    call TriggerAddAction(upondeath, function GenericDamage_RemoveTriggers)

    call DestroyGroup(g)
    set dmgtrigger = null
    set g = null
    set upondeath = null
endfunction
05-21-2007, 02:41 PM#2
Vexorian
by doing
Collapse JASS:
struct DMG_Func
    integer Action
    integer Dmg
endstruct

You are getting rid of 50% of the benefits of using structs.

you should:

Collapse JASS:
struct DMG_Func
    triggeraction Action
    integer Dmg
endstruct

And then stop using H2I and I2TA when getting the Action member.
05-22-2007, 02:18 PM#3
n13astra
Thanks,

and i might add. This new vJass standard is FAN-F**KING-TASTIC. The map lag has decreased by easy 20% and the code is 1000x easier to read.

Good work to all those involved with this addon.

EDIT: I just noticed something after reading up on structs again.

Attaching structs. I noticed that this line in my code is different to how vex. suggests in his tutorial but it hasnt caused any problems for me so whats the purpose of doing it the way vex said?

MY CURRENT CODE
Collapse JASS:
function GenericDamage_RemoveTriggers takes nothing returns nothing
    local DMG_Func dmgdata = GetHandleInt(GetTriggerUnit(), "dmgdata")
...
endfunction

VEX. SUGGESTED WAY
Collapse JASS:
function GenericDamage_RemoveTriggers takes nothing returns nothing
    local DMG_Func dmgdata = DMG_Func( GetHandleInt(GetTriggerUnit(), "dmgdata") )
...
endfunction