Resource Preloader

Scripts

9 years
edited 8 years
Useful snippet for preloading your map resources and avoiding duplicates in preloading.

Code (jass) Select
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 improvements
18
            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 preloaded
27
28
          */function PreloadItem takes integer rawcode returns nothing/*
29
                - Assigns a certain type of item to be preloaded
30
31
          */function PreloadAbility takes integer rawcode returns nothing/*
32
                - Assigns a certain type of ability to be preloaded
33
34
          */function PreloadEffect takes string modelPath returns nothing/*
35
                - Assigns a certain type of effect to be preloaded
36
37
          */function PreloadSound takes string soundPath returns nothing/*
38
                - Assigns a certain type of sound to be preloaded
39
40
41
          */function PreloadUnitEx takes integer start, integer end returns nothing/*
42
                - Assigns a range of unit rawcodes to be preloaded
43
44
          */function PreloadItemEx takes integer start, integer end returns nothing/*
45
                - Assigns a range of item rawcodes to be preloaded
46
47
          */function PreloadAbilityEx takes integer start, integer end returns nothing/*
48
                - Assigns a range of ability rawcodes to be preloaded
49
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 to
55
       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
        endfunction
65
    endif
66
67
    //============================================== TextMacros ==============================================//
68
69
    //! textmacro ASSIGN takes NAME, ARG, TYPE, INDEX, I
70
    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
            endif
78
        else
79
            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
            endif
85
        endif
86
    endfunction
87
    //! endtextmacro
88
89
    //! textmacro ASSIGNWITHRANGE takes NAME
90
    function Preload$NAME$Ex takes integer start, integer end returns nothing
91
        local BJObjectId this = BJObjectId(start)
92
        local BJObjectId last = BJObjectId(end)
93
        loop
94
            call Preload$NAME$(this)
95
            exitwhen this == last
96
            if this > last then
97
                set this = this.minus_1()
98
            else
99
                set this = this.plus_1()
100
            endif
101
        endloop
102
    endfunction
103
    //! endtextmacro
104
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
        else
112
            call RemoveUnit(CreateUnit(Player(15), id, 0, 0, 0))
113
        endif
114
    endfunction
115
116
    private function DoItemPreload takes integer id returns nothing
117
        call RemoveItem(UnitAddItemById(S.dummy, id))
118
    endfunction
119
120
    private function DoAbilityPreload takes integer id returns nothing
121
        if UnitAddAbility(S.dummy, id) and UnitRemoveAbility(S.dummy, id) then
122
        endif
123
    endfunction
124
125
    private function DoEffectPreload takes string path returns nothing
126
        call DestroyEffect(AddSpecialEffectTarget(path, S.dummy, "origin"))
127
    endfunction
128
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
    endfunction
136
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
            endif
155
            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
        endmethod
163
    endmodule
164
165
    private struct S extends array
166
        static if LIBRARY_Table then
167
            static TableArray tb
168
        else
169
            static hashtable tb = InitHashtable()
170
        endif
171
        static unit dummy
172
        implement Init
173
    endstruct
174
175
176
endlibrary
9 years
UPDATED


- Made Table optional
- Added UnitRecycler as an optional requirement
- Upon calling PreloadUnit(), if the unit is not a hero and UnitRecycler is found, the unit will be added to the unit stock instead. Otherwise, it goes with normal preloading.
- dummy unit's movement is disabled to prevent possible game crash.
- You can now preload at any time during the game instead of only during the map initialization
- Significantly optimized the code
- Removed the unnecessary custom function for checking preload duplicates
- Added Table to the library requirements
- Preloading does not anymore happen in a single phase at the GUI Map Initialization
- Resources are now preloaded at the instant you call the preload function
- Other changes