9 years
edited 8 years
Useful snippet for preloading your map resources and avoiding duplicates in preloading.
1 library ResourcePreloader /*v1.4
2
3
4 */uses /*
5
6 */BJObjectId /* https://wc3modding.info/5038/bjobjectid/
7 */optional Table /* https://wc3modding.info/4611/snippet-new-table/
8 */optional UnitRecycler /* https://wc3modding.info/5426/unitrecycler/
9
10 *///! novjass
11 12 |================|
13 | Written by AGD |
14 |================|
15 16 [CREDITS]
17 /* IcemanBo - for suggesting further improvements18 Silvenon - for the sound preloading method */19 20 21 |-----|
22 | API |
23 |-----|
24 25 function PreloadUnit takes integer rawcode returns nothing/*
26 - Assigns a certain type of unit to be preloaded27
28 */function PreloadItem takes integer rawcode returns nothing/*
29 - Assigns a certain type of item to be preloaded30
31 */function PreloadAbility takes integer rawcode returns nothing/*
32 - Assigns a certain type of ability to be preloaded33
34 */function PreloadEffect takes string modelPath returns nothing/*
35 - Assigns a certain type of effect to be preloaded36
37 */function PreloadSound takes string soundPath returns nothing/*
38 - Assigns a certain type of sound to be preloaded39
40
41 */function PreloadUnitEx takes integer start, integer end returns nothing/*
42 - Assigns a range of unit rawcodes to be preloaded43
44 */function PreloadItemEx takes integer start, integer end returns nothing/*
45 - Assigns a range of item rawcodes to be preloaded46
47 */function PreloadAbilityEx takes integer start, integer end returns nothing/*
48 - Assigns a range of ability rawcodes to be preloaded49
50
51 *///! endnovjass
52 53 //========================================================================================================//54 /* Do not try to change below this line if you're not so sure on what you're doing. Unless you want to55 change, check, or study the core of the system, it is not advised that you scroll any further. */56 //========================================================================================================//57 58 private keyword S
59 60 61 static if DEBUG_MODE then
62 private function Debug takes string msg returns nothing
63 call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "|CFFFFCC00[Resource Preloader]|R " + msg)
64 endfunction65 endif66 67 //============================================== TextMacros ==============================================//68 69 //! textmacro ASSIGN takes NAME, ARG, TYPE, INDEX, I70 function Preload$NAME$ takes $ARG$ what returns nothing
71 static if LIBRARY_Table then
72 if not S.tb[$I$].boolean[$INDEX$] then
73 set S.tb[$I$].boolean[$INDEX$] = true
74 call Do$NAME$Preload(what)
75 debug else
76 debug call Debug("|CFFFF0000Operation Cancelled :|R Entered $TYPE$ data was already preloaded")
77 endif78 else79 if not LoadBoolean(S.tb, $I$, $INDEX$) then
80 call SaveBoolean(S.tb, $I$, $INDEX$, true)
81 call Do$NAME$Preload(what)
82 debug else
83 debug call Debug("|CFFFF0000Operation Cancelled :|R Entered $TYPE$ data was already preloaded")
84 endif85 endif86 endfunction87 //! endtextmacro88 89 //! textmacro ASSIGNWITHRANGE takes NAME90 function Preload$NAME$Ex takes integer start, integer end returns nothing
91 local BJObjectId this = BJObjectId(start)
92 local BJObjectId last = BJObjectId(end)
93 loop94 call Preload$NAME$(this)
95 exitwhen this == last
96 if this > last then
97 set this = this.minus_1()
98 else99 set this = this.plus_1()
100 endif101 endloop102 endfunction103 //! endtextmacro104 105 //========================================================================================================//106 107 108 private function DoUnitPreload takes integer id returns nothing
109 static if LIBRARY_UnitRecycler then
110 call RecycleUnitEx(CreateUnit(Player(15), id, 0, 0, 0))
111 else112 call RemoveUnit(CreateUnit(Player(15), id, 0, 0, 0))
113 endif114 endfunction115 116 private function DoItemPreload takes integer id returns nothing
117 call RemoveItem(UnitAddItemById(S.dummy, id))
118 endfunction119 120 private function DoAbilityPreload takes integer id returns nothing
121 if UnitAddAbility(S.dummy, id) and UnitRemoveAbility(S.dummy, id) then
122 endif123 endfunction124 125 private function DoEffectPreload takes string path returns nothing
126 call DestroyEffect(AddSpecialEffectTarget(path, S.dummy, "origin"))
127 endfunction128 129 private function DoSoundPreload takes string path returns nothing
130 local sound s = CreateSound(path, false, false, false, 10, 10, "")
131 call SetSoundVolume(s, 0)
132 call StartSound(s)
133 call KillSoundWhenDone(s)
134 set s = null
135 endfunction136 137 //! runtextmacro ASSIGN("Unit", "integer", "unit", "what", "0")138 //! runtextmacro ASSIGN("Item", "integer", "item", "what", "1")139 //! runtextmacro ASSIGN("Ability", "integer", "ability", "what", "2")140 //! runtextmacro ASSIGN("Effect", "string", "effect", "StringHash(what)", "3")141 //! runtextmacro ASSIGN("Sound", "string", "sound", "StringHash(what)", "4")142 143 //! runtextmacro ASSIGNWITHRANGE("Unit")144 //! runtextmacro ASSIGNWITHRANGE("Item")145 //! runtextmacro ASSIGNWITHRANGE("Ability")146 147 //========================================================================================================//148 149 private module Init
150 private static method onInit takes nothing returns nothing
151 local rect bounds = GetWorldBounds()
152 static if LIBRARY_Table then
153 set tb = TableArray[5]
154 endif155 set dummy = CreateUnit(Player(15), 'hpea', 0, 0, 0)
156 call UnitAddAbility(dummy, 'AInv')
157 call UnitAddAbility(dummy, 'Avul')
158 call UnitRemoveAbility(dummy, 'Amov')
159 call SetUnitY(dummy, GetRectMaxY(bounds) + 1000)
160 call RemoveRect(bounds)
161 set bounds = null
162 endmethod163 endmodule164 165 private struct S extends array
166 static if LIBRARY_Table then
167 static TableArray tb168 else169 static hashtable tb = InitHashtable()
170 endif171 static unit dummy
172 implement Init173 endstruct174 175 176 endlibrary