- Reaction score
- 456
It won't be any simplier to me .. I don't even know what that text macro does there and so on.. I just know how to use structures and libraries and scopes.. (somehow).
Untitled Trigger 001
Events
Player - Player 1 (Red) Selects a unit
Conditions
Actions
Custom script: call FadeUnit(GetTriggerUnit(), 1.00, 0.00, true)
function FadeUnit takes unit whichUnit, real duration, real minimumFade, boolean removeAfterFade returns nothing
local FadeSystemData dat = FadeSystemData.create()
set dat.whichUnit = whichUnit
set dat.duration = duration
set dat.minimumFade = minimumFade
set dat.removeAfterFade = removeAfterFade
if not CollectionF.contains(dat) then // << ----- see no complications
call CollectionF.add(dat)
endif
if (CollectionF.size() > 0) then
call TimerStart(FS_Timer, PERIOD, true, function FadeUnitHandler)
endif
endfunction
//==============================================================================
// Collections -- STRUCT STORAGE SYSTEM BY COHADAR -- v2.1c
//==============================================================================
//
// PURPOUSE:
// * Storing structs for spells that require variable number of data
// * Great for algorithms that need random access.
// * Able to create 100% MUI spells.
//
// REFERENCE:
// * This system was inspired by java Collection interface
// <a href="http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.html" target="_blank" class="link link--external" rel="nofollow ugc noopener">http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.html</a>
// <a href="http://java.sun.com/javase/6/docs/api/java/util/Iterator.html" target="_blank" class="link link--external" rel="nofollow ugc noopener">http://java.sun.com/javase/6/docs/api/java/util/Iterator.html</a>
//
// DETAILS:
// * Collections are now implemented as static-only classes
// this removes limitations in number of elements from version 1.3
//
// * Main purpouse of collections is fast storing and removing of structs,
// Structs in Collection are NOT ordered in any way.
// Structs can change place inside collection at any time.
// Iterators are the only safe way to access collection elements.
//
// * Do NOT create or destoy Collection or Iterator classes,
// there is no point to that because all operations are static
//
// * Each Collection has exactly one iterator (for performanse reasons)
//
// * Collections are implemented with global array that is created
// by calling a collection macro inside a trigger scope.
// you can have 8191 structs stored in one collection.
//
// * Since collections are just a wrapper around of global array $X$_buff
// some people will feel the need to inline collection calls.
// Please do NOT do this unless it is really necessary for performanse.
// This will almost never be the case, collections are extremely fast.
//
//
// HOW TO USE:
// * For a trigger to use collections it MUST be inside a scope.
// At the beginning of scope insert this macro call
// //! runtextmacro COLLECTION("A")
//
// * This will create global array and other fields and methods
// needed for collection manipulation
//
// * To prevent lag when collections are first used put this into spells InitTrigger
// call CollectionA.init() // This is optional
//
//---------------------------------------------------------------------------
// BASIC LOOP: (data is your custom struct)
//---------------------------------------------------------------------------
//
// call IteratorA.reset() // <-- positions iterator at the beginning of collection
// loop
// exitwhen IteratorA.noNext()
// set data = IteratorA.next()
//
// if SomeExitCondition(data)
// call data.destroy()
// call IteratorA.remove()
// else
// -- Process data --
// endif
// endloop
//
//---------------------------------------------------------------------------
// LIST OF FUNCTIONS: (all methods are static)
//---------------------------------------------------------------------------
// struct CollectionX
// .init()
// .isEmpty() -> boolean
// .size() -> integer
// .contains(integer) -> boolean
// .add(integer) -> boolean
// .remove(integer) -> boolean
// .clear()
// endstruct
//
// struct IteratorX
// .reset()
// .noNext() -> boolean
// .next() -> integer
// .remove()
// endstruct
//---------------------------------------------------------------------------
//
// HOW TO IMPORT:
// * Just create a trigger named Collections
// * convert it to text and replace the whole trigger text with this one
//
//==============================================================================
globals
integer MAX_COLLECTION_SIZE = 8191
endglobals
//! textmacro COLLECTION takes X, MODIFIER
// Do NOT use this fields directly
globals
// Collection fields
private integer array $X$_buff
private integer $X$_size = 0
// Iterator fields
private integer $X$_index = -1
private boolean $X$_removeOK = false
endglobals
//==============================================================================
$MODIFIER$ struct Collection$X$
// prevents the lag by forcing wc3 to allocate whole array at once
static method init takes integer size returns nothing
set $X$_buff[size-1] = 0
endmethod
// checks if collection is empty
static method isEmpty takes nothing returns boolean
return $X$_size <= 0
endmethod
// returns the current size of collection
static method size takes nothing returns integer
return $X$_size
endmethod
// checks is element e is inside collection
static method contains takes integer e returns boolean
local integer i = 0
loop
exitwhen i >= $X$_size
if e == $X$_buff<i> then
return true
endif
set i = i + 1
endloop
return false
endmethod
// adds element e into collection
static method add takes integer e returns boolean
if $X$_size < MAX_COLLECTION_SIZE then
set $X$_buff[$X$_size] = e
set $X$_size = $X$_size + 1
return true
else
call BJDebugMsg("|C00FF0000ERROR: Collection$X$ overflow in scope " + SCOPE_PREFIX)
return false
endif
endmethod
// removes first element that matches e from collection
static method remove takes integer e returns boolean
local integer i = 0
loop
exitwhen i >= $X$_size
if e == $X$_buff<i> then
set $X$_size = $X$_size - 1
set $X$_buff<i> = $X$_buff[$X$_size]
return true
endif
set i = i + 1
endloop
return false
endmethod
// removes ALL elements from collection
static method clear takes nothing returns nothing
set $X$_size = 0
endmethod
endstruct
//==============================================================================
$MODIFIER$ struct Iterator$X$
// Use before any looping to reset iterator to the beginning of collection
static method reset takes nothing returns nothing
set $X$_index = -1
set $X$_removeOK = false
endmethod
// returns true if at the end of collection
static method noNext takes nothing returns boolean
return $X$_index >= ($X$_size-1)
endmethod
// Not safe operation, use only if noNext returns false
static method next takes nothing returns integer
set $X$_index = $X$_index + 1
set $X$_removeOK = true
return $X$_buff[$X$_index]
endmethod
//This method can be called only once per call to next.
static method remove takes nothing returns nothing
if $X$_removeOK then
set $X$_size = $X$_size - 1
set $X$_buff[$X$_index] = $X$_buff[$X$_size]
set $X$_index = $X$_index - 1
set $X$_removeOK = false
else
call BJDebugMsg("|c00FF0000ERROR: Iterator$X$ attempted invalid remove in scope" + SCOPE_PREFIX)
endif
endmethod
endstruct
//! endtextmacro
//forced by WE
function InitTrig_Collections takes nothing returns nothing
endfunction
</i></i></i>
library FadeSystem
private struct FadeSystemData
unit whichUnit
real duration
real minimumFade
boolean removeAfterFade
real current = 0.00
boolean wantDestroy = false
endstruct
globals
private FadeSystemData array udg_FadeSystem_array
private timer udg_FadeSystem_timer = CreateTimer()
private integer udg_FadeSystem_total = 0
endglobals
private function FadeUnitHandler takes nothing returns nothing
local FadeSystemData dat
local integer index = 0
loop
exitwhen (index == udg_FadeSystem_total)
set dat = udg_FadeSystem_array[index]
if (dat.current >= 100.00 - dat.minimumFade) or (GetUnitState(dat.whichUnit, UNIT_STATE_LIFE) <= 0.00) then
if (dat.removeAfterFade == true) then
call RemoveUnit(dat.whichUnit)
endif
set udg_FadeSystem_array[index] = udg_FadeSystem_array[udg_FadeSystem_total - 1]
set udg_FadeSystem_total = udg_FadeSystem_total - 1
call dat.destroy()
set index = index - 1
else
set dat.current = dat.current + (100.00 - dat.minimumFade) / dat.duration * 0.035
call SetUnitVertexColor(dat.whichUnit, 255, 255, 255, R2I((100.00-dat.current)*I2R(255)*0.01))
endif
set index = index + 1
endloop
if (udg_FadeSystem_total == 0) then
call PauseTimer(udg_FadeSystem_timer)
endif
endfunction
function FadeUnit takes unit whichUnit, real duration, real minimumFade, boolean removeAfterFade returns nothing
local FadeSystemData dat = FadeSystemData.create()
local integer index = 0
loop
if (whichUnit == udg_FadeSystem_array[index].whichUnit) then
call dat.destroy()
return
endif
exitwhen (index == udg_FadeSystem_total)
set index = index + 1
endloop
set dat.whichUnit = whichUnit
set dat.duration = duration
set dat.minimumFade = minimumFade
set dat.removeAfterFade = removeAfterFade
set udg_FadeSystem_total = udg_FadeSystem_total + 1
set udg_FadeSystem_array[udg_FadeSystem_total] = dat
if (udg_FadeSystem_total == 1) then
call TimerStart(udg_FadeSystem_timer, 0.035, true, function FadeUnitHandler)
endif
endfunction
endlibrary
//This maybe removed if code is placed in map's header.
function InitTrig_Fade_System takes nothing returns nothing
endfunction
if (udg_FadeSystem_total == 1) then
call TimerStart(udg_FadeSystem_timer, 0.035, true, function FadeUnitHandler)
endif
if (udg_FadeSystem_total >= 1) then // <------<<<
call TimerStart(udg_FadeSystem_timer, 0.035, true, function FadeUnitHandler)
endif
if (dat.current >= 100.00 - dat.minimumFade) or (GetUnitState(dat.whichUnit, UNIT_STATE_LIFE) <= 0.00) then
if (dat.callback != null) then
call dat.callback.execute(dat.whichUnit)
endif
// this function is using IFadedUnitHandler interface
function MyHandler takes unit u returns nothing
call RemoveUnit(u) // or anything else you want
endfunction
function Trig_Fade_Test_Actions takes nothing returns nothing
call FadeUnit( GetTriggerUnit(), 5.0, 0.5, IFadedUnitHandler.MyHandler)
endfunction
//===========================================================================
function InitTrig_Fade_Test takes nothing returns nothing
set gg_trg_Fade_Test = CreateTrigger( )
call TriggerRegisterAnyUnitEventBJ( gg_trg_Fade_Test, EVENT_PLAYER_UNIT_ATTACKED )
call TriggerAddAction( gg_trg_Fade_Test, function Trig_Fade_Test_Actions )
endfunction