kingkingyyk3
Visitor (Welcome to the Jungle, Baby!)
- Reaction score
- 216
JASS:
.
____ ___ ____
| \|_ _| \|----|
| || | _|_| || ||--|
|____/|___|____/|----|
Destructable Indexing and Death Event
v1.0.0 by kingking
What is it ?
This library indexes every destructable in map and reports their death.
==============
DIDE API :
==============
function TriggerRegisterDestDeathEvent (trigger whichTrigger)
function TriggerRegisterDestonIndexEvent (trigger whichTrigger)
function GetEventDestructable () -> destructable
function GetDestructableId (destructable dest) -> integer
function GetDestructableById (integer id) -> destructable
Notice : Please change all the CreateDestructable and CreateDestructableZ with :
function CreateDestructableEx (-same with CreateDestructable)
function CreateDestructableZEx (-same with CreateDestructableZ)
Tweak :
You can find "private function filter" in the library. It acts as default
filter. If you dont want the types of destructables to be indexed, use it
to eliminate.
Modules :
=======================
module DIDE_Property //Thanks to Cohadar for this interface.
=======================
This module is used to attach struct to destructable.
Usage :
set *StructName*[destructable] = whichInstance
*StructName*[destructable] -> Attached struct
=======================
module DIDE //Thanks to Jesus4Lyf for this interface.
=======================
This module is used when you want to attach struct to many destructable
automatically. Struct which used this module must extends array.
Usage :
*StructName*[destructable] -> Attached struct
Members :
*instance*.dest -> destructable
Optional methods :
private static method DIDE_filter takes destructable dest returns boolean
~This method is used to filter. If the destructable does not meet the condition,
the struct will not allocate an instance for it.
private method DIDE_onCreate takes nothing returns nothing
~When the struct is allocated, this method will be fired.
private method DIDE_onDestroy takes nothing returns nothing
~When the struct is deallocated, this method will be fired.
private static method DIDE_onInit takes nothing returns nothing
~Because DIDE uses structs init, so you need to replace onInit with this.
Requires :
Jasshelper v0.A.2.7 or newer
Event
Event
JASS:
library DIDE initializer Init requires Event
private function filter takes destructable dest returns boolean
return true
endfunction
globals
private hashtable hasht
private Event DeathEvent
private Event onIndexEvent
private destructable dest = null
private trigger onIndexStructAllocator = CreateTrigger()
private trigger onDeathStructDeallocator = CreateTrigger()
endglobals
function GetEventDestructable takes nothing returns destructable
return dest
endfunction
function TriggerRegisterDestDeathEvent takes trigger whichTrigger returns EventReg
return DeathEvent.register(whichTrigger)
endfunction
function TriggerRegisterDestOnIndexEvent takes trigger whichTrigger returns EventReg
return onIndexEvent.register(whichTrigger)
endfunction
private keyword DestructableIndex
function GetDestructableId takes destructable dest returns integer
return LoadInteger(hasht,GetHandleId(dest),0)
endfunction
function GetDestructableById takes integer whichId returns destructable
return DestructableIndex(whichId).d
endfunction
function CreateDestructableEx takes integer objectid, real x, real y, real face, real scale, integer variation returns destructable
set bj_lastCreatedDestructable = CreateDestructable(objectid,x,y,face,scale,variation)
call DestructableIndex.new(bj_lastCreatedDestructable)
return bj_lastCreatedDestructable
endfunction
function CreateDestructableZEx takes integer objectid, real x, real y, real z, real face, real scale, integer variation returns destructable
set bj_lastCreatedDestructable = CreateDestructableZ(objectid,x,y,z,face,scale,variation)
call DestructableIndex.new(bj_lastCreatedDestructable)
return bj_lastCreatedDestructable
endfunction
public function RegisterStruct takes code cf1, code cf2 returns nothing
call TriggerAddCondition(onIndexStructAllocator,Condition(cf1))
call TriggerAddCondition(onDeathStructDeallocator,Condition(cf2))
endfunction
private struct DestructableIndex
static conditionfunc cond
destructable d
trigger trig
triggercondition trigCond
integer id
static method removeIndex takes nothing returns boolean
local thistype this = LoadInteger(hasht,GetHandleId(GetTriggerDestructable()),0)
call TriggerEvaluate(onDeathStructDeallocator)
set dest = this.d
call DeathEvent.fire()
call TriggerRemoveCondition(this.trig,this.trigCond)
call DisableTrigger(this.trig)
call DestroyTrigger(this.trig)
set this.d = null
set this.trig = null
set this.trigCond = null
call RemoveSavedInteger(hasht,this.id,0)
call this.destroy()
return false
endmethod
static method new takes destructable dest returns nothing
local thistype this = thistype.allocate()
set this.d = dest
set this.trig = CreateTrigger()
set this.id = GetHandleId(dest)
call TriggerRegisterDeathEvent(this.trig,this.d)
set this.trigCond = TriggerAddCondition(this.trig,thistype.cond)
call SaveInteger(hasht,this.id,0,this)
endmethod
static method onInit takes nothing returns nothing
set thistype.cond = Condition(function thistype.removeIndex)
endmethod
endstruct
private function AllocateIndex takes nothing returns boolean
set dest = GetFilterDestructable()
if filter(dest) then
call DestructableIndex.new(dest)
call TriggerEvaluate(onIndexStructAllocator)
call onIndexEvent.fire()
endif
return false
endfunction
private function Init takes nothing returns nothing
set hasht = InitHashtable()
set DeathEvent = Event.create()
set onIndexEvent = Event.create()
call EnumDestructablesInRect(GetWorldBounds(),Condition(function AllocateIndex),null)
endfunction
public struct StructEx extends array
static method DIDE_filter takes destructable dest returns boolean
return filter(dest)
endmethod
method DIDE_onCreate takes nothing returns nothing
endmethod
method DIDE_onDestroy takes nothing returns nothing
endmethod
static method DIDE_onInit takes nothing returns nothing
endmethod
endstruct
module DIDE_Property
private static destructable array DIDE_dest
private static thistype array DIDE_data
static method [] takes destructable dest returns thistype
local integer DIDE_id = GetDestructableId(dest)
if DIDE_dest[DIDE_id] == dest then
return DIDE_data[DIDE_id]
endif
set DIDE_dest[DIDE_id] = null
set DIDE_data[DIDE_id] = 0
return 0
endmethod
static method []= takes destructable dest, thistype i returns nothing
local integer DIDE_id = GetDestructableId(dest)
set DIDE_dest[DIDE_id] = dest
set DIDE_data[DIDE_id] = i
endmethod
static method release takes destructable dest returns nothing
local integer DIDE_id = GetDestructableId(dest)
if DIDE_dest[DIDE_id] == dest then
call DIDE_data[DIDE_id].destroy()
set DIDE_dest[DIDE_id] = null
set DIDE_data[DIDE_id] = 0
endif
endmethod
endmodule
endlibrary
module DIDE
private static delegate DIDE_StructEx DELEGATED=0
private boolean DIDE_IsUsed = false
static method operator [] takes destructable dest returns thistype
local integer id = GetDestructableId(dest)
if thistype(id).DIDE_IsUsed == true then
return id
endif
return 0
endmethod
method operator dest takes nothing returns destructable
return GetDestructableById(this)
endmethod
static method DIDE_Create takes nothing returns boolean
local destructable dest = GetEventDestructable()
local thistype this = GetDestructableId(dest)
if thistype.DIDE_filter(dest) == true then
set this.DIDE_IsUsed = true
call this.DIDE_onCreate()
endif
set dest = null
return false
endmethod
static method DIDE_Destroy takes nothing returns boolean
local destructable dest = GetEventDestructable()
local thistype this = GetDestructableId(dest)
if this.DIDE_IsUsed == true then
set this.DIDE_IsUsed = false
call this.DIDE_onDestroy()
endif
set dest = null
return false
endmethod
static method onInit takes nothing returns nothing
call DIDE_RegisterStruct(function thistype.DIDE_Create,function thistype.DIDE_Destroy)
call DIDE_onInit()
endmethod
endmodule