13 years
edited 8 years
BJObjectId - makes working with the default WE object-id(s) easier
The "default WE object-id(s)" are those automatically assigned to objects after creation in the editor, i.e: e000, A001, I002, etc.
You've probably seen/written stuff like this:
Which is a bit cumbersome when you have more than a dozen or so items, but we can simplify it:
It seems to me that this scales better if you have to do it a lot in your map.
BJObjectId.j:
The "default WE object-id(s)" are those automatically assigned to objects after creation in the editor, i.e: e000, A001, I002, etc.
You've probably seen/written stuff like this:
1 2 globals3 integer array my_items
4 integer my_items_count
5 endglobals6 7 ...
8 // initialize the array9 set my_items[1] = 'I000'
10 set my_items[2] = 'I001'
11 // ...12 set my_items[10] = 'I009'
13 set my_items[11] = 'I00A'
14 set my_items_count = 11
15 16 ...
17 // walk the array18 local integer i = 1
19 local integer item_id
20 loop21 exitwhen i > my_items_count
22 set item_id = my_items[i]
23 // do stuff with item_id24 set i = i + 1
25 endloop26
Which is a bit cumbersome when you have more than a dozen or so items, but we can simplify it:
1 2 ...
3 local BJObjectId item_id = BJObjectId('I000')
4 local BJObjectId last_item_id = BJObjectId('I00A')
5 loop6 exitwhen item_id > last_item_oid
7 // do stuff with item_id8 set item_id = item_id.plus_1()
9 endloop10 ...
11
It seems to me that this scales better if you have to do it a lot in your map.
BJObjectId.j:
1 2 library BJObjectId // 1.0
3 4 //! novjass5 6 // creating:7 8 // from native object-id / integer9 set oid = BJObjectId('A000')
10 11 // from string12 set oid = BJObjectId.from_str("I001")
13 14 15 // printing:16 17 call BJDebugMsg(oid.to_str())
18 19 20 // looping through a set of object-id(s)21 22 // forward23 local BJObjectId oid = BJObjectId('A000')
24 local BJObjectId last_oid = BJObjectId('A010')
25 loop26 exitwhen oid > last_oid
27 // do stuff28 set oid = oid.plus_1()
29 endloop30 31 // backward32 local BJObjectId oid = BJObjectId('A010')
33 local BJObjectId last_oid = BJObjectId('A000')
34 loop35 exitwhen oid < last_oid
36 // do stuff37 set oid = oid.minus_1()
38 endloop39 40 41 // mapping BJObjectId(s) to array-indices / struct instances42 43 local integer index
44 local MyStruct my_struct45 46 set index = BJObjectId('H000').to_unit_index()
47 set my_struct = MyStruct( BJObjectId('h001').to_unit_index() )
48 49 set index = BJObjectId('I000').to_item_index()
50 set my_struct = BJObjectId('I001').to_item_index() // don't really need the MyStruct cast (because vJass =))
51 52 set index = BJObjectId('B000').to_destructable_index()
53 set my_struct = BJObjectId('B001').to_destructable_index()
54 55 set index = BJObjectId('D000').to_doodad_index()
56 set my_struct = BJObjectId('D001').to_doodad_index()
57 58 set index = BJObjectId('A000').to_ability_index()
59 set my_struct = BJObjectId('A001').to_ability_index()
60 61 set index = BJObjectId('B000').to_buff_index()
62 set my_struct = BJObjectId('B001').to_buff_index()
63 64 set index = BJObjectId('R000').to_upgrade_index()
65 set my_struct = BJObjectId('R001').to_upgrade_index()
66 67 // for campaign objects the methods have a "c" after the "to_"68 set index = BJObjectId('A000').to_cunit_index()
69 set index = BJObjectId('A000').to_cability_index()
70 ...
71 72 // NOTE: the to_*_index methods break for object-id(s) > 'XXOZ', i.e73 // 900 is the maximum number of objects you can have and still be able to use those methods;74 // the reason is that the object-id(s) after 'XXOZ' have indices > 819075 76 //! endnovjass77 78 struct BJObjectId extends array
79 80 static method from_str takes string oid returns thistype
81 // '0' = 48 .. '9' = 57,82 // 'A' = 65 .. 'Z' = 9083 // 'a' = 97 .. 'z' = 12284 //85 // index(<chr>):86 // '0' = 0; chr(0 + 48) = '0' = 4887 // 'A' = 17; chr(17 + 48) = 'A' = 6588 // 'a' = 49; chr(49 + 48) = 'a' = 9789 //90 local string chars = "0123456789.......ABCDEFGHIJKLMNOPQRSTUVWXYZ......abcdefghijklmnopqrstuvwxyz"
91 local integer this = 0
92 local integer i
93 local integer j
94 local integer ordinal
95 local string chr
96 local integer pow_256 = 1
97 98 set i = 3
99 loop100 exitwhen i < 0
101 set chr = SubString(oid, i, i + 1)
102 103 set j = 0
104 loop105 exitwhen j >= 75
106 107 if chr == SubString(chars, j, j + 1) then
108 set this = this + (j + 48) * pow_256
109 set pow_256 = pow_256 * 256
110 exitwhen true
111 endif112 113 set j = j + 1
114 endloop115 116 set i = i - 1
117 endloop118 119 return this
120 endmethod121 122 method to_str takes nothing returns string
123 local string chars = "0123456789.......ABCDEFGHIJKLMNOPQRSTUVWXYZ......abcdefghijklmnopqrstuvwxyz"
124 local integer t = this
125 local integer i
126 local integer b
127 local string result = ""
128 129 set i = t
130 set t = i / 0x100
131 set b = i - t * 0x100 - 48
132 set result = SubString(chars, b, b + 1) + result
133 134 set i = t
135 set t = i / 0x100
136 set b = i - t * 0x100 - 48
137 set result = SubString(chars, b, b + 1) + result
138 139 set i = t
140 set t = i / 0x100
141 set b = i - t * 0x100 - 48
142 set result = SubString(chars, b, b + 1) + result
143 144 set t = t - 48
145 set result = SubString(chars, t, t + 1) + result
146 147 return result148 endmethod149 150 method plus_1 takes nothing returns thistype
151 local integer t = this
152 local integer i
153 local integer b1
154 local integer b2
155 local integer b3
156 local integer b4
157 158 set i = t
159 set t = i / 0x100
160 set b4 = i - t * 0x100
161 162 if b4 < 'Z' then
163 if b4 != '9' then
164 set i = i + 1
165 else166 set i = i + 8
167 endif168 else169 170 set i = t
171 set t = i / 0x100
172 set b3 = i - t * 0x100
173 if b3 < 'Z' then
174 if b3 != '9' then
175 set i = i * 0x00000100 + 0x00000100 + '0'
176 else177 set i = i * 0x00000100 + 0x00000800 + '0'
178 endif179 else180 181 set i = t
182 set t = i / 0x100
183 set b2 = i - t * 0x100
184 if b2 < 'Z' then
185 if b2 != '9' then
186 set i = i * 0x00010000 + 0x00010000 + '0' * 0x00000100 + '0'
187 else188 set i = i * 0x00010000 + 0x00080000 + '0' * 0x00000100 + '0'
189 endif190 else191 192 set i = t
193 if i != '9' then
194 set i = i * 0x01000000 + 0x01000000 + '0' * 0x00010000 + '0' * 0x00000100 + '0'
195 else196 set i = i * 0x01000000 + 0x08000000 + '0' * 0x00010000 + '0' * 0x00000100 + '0'
197 endif198 endif199 endif200 endif201 202 return i203 endmethod204 205 method minus_1 takes nothing returns thistype
206 local integer t = this
207 local integer i
208 local integer b1
209 local integer b2
210 local integer b3
211 local integer b4
212 213 set i = t
214 set t = i / 0x100
215 set b4 = i - t * 0x100
216 if b4 > '0' then
217 if b4 != 'A' then
218 set i = i - 1
219 else220 set i = i - 8
221 endif222 else223 224 set i = t
225 set t = i / 0x100
226 set b3 = i - t * 0x100
227 if b3 > '0' then
228 if b3 != 'A' then
229 set i = i * 0x00000100 - 0x00000100 + 'Z'
230 else231 set i = i * 0x00000100 - 0x00000800 + 'Z'
232 endif233 else234 235 set i = t
236 set t = i / 0x100
237 set b2 = i - t * 0x100
238 if b2 > '0' then
239 if b2 != 'A' then
240 set i = i * 0x00010000 - 0x00010000 + 'Z' * 0x00000100 + 'Z'
241 else242 set i = i * 0x00010000 - 0x00080000 + 'Z' * 0x00000100 + 'Z'
243 endif244 else245 246 set i = t
247 if i != 'A' then
248 set i = i * 0x01000000 - 0x01000000 + 'Z' * 0x00010000 + 'Z' * 0x00000100 + 'Z'
249 else250 set i = i * 0x01000000 - 0x08000000 + 'Z' * 0x00010000 + 'Z' * 0x00000100 + 'Z'
251 endif252 endif253 endif254 endif255 256 return i257 endmethod258 259 method operator< takes thistype other returns boolean
260 return integer(this) < integer(other)
261 endmethod262 263 private static integer array first_unit_oid
264 private static integer array first_cunit_oid
265 private static method onInit takes nothing returns nothing
266 set first_unit_oid['H'] = 'H000'
267 set first_unit_oid['h'] = 'h000'
268 set first_unit_oid['O'] = 'O000'
269 set first_unit_oid['o'] = 'o000'
270 set first_unit_oid['E'] = 'E000'
271 set first_unit_oid['e'] = 'e000'
272 set first_unit_oid['U'] = 'U000'
273 set first_unit_oid['u'] = 'u000'
274 set first_unit_oid['N'] = 'N000'
275 set first_unit_oid['n'] = 'n000'
276 277 set first_cunit_oid['H'] = 'H600'
278 set first_cunit_oid['h'] = 'h600'
279 set first_cunit_oid['O'] = 'O600'
280 set first_cunit_oid['o'] = 'o600'
281 set first_cunit_oid['E'] = 'E600'
282 set first_cunit_oid['e'] = 'e600'
283 set first_cunit_oid['U'] = 'U600'
284 set first_cunit_oid['u'] = 'u600'
285 set first_cunit_oid['N'] = 'N600'
286 set first_cunit_oid['n'] = 'n600'
287 endmethod288 289 method to_unit_index takes nothing returns integer
290 return this - first_unit_oid[this / 0x01000000] + 1
291 endmethod292 method to_cunit_index takes nothing returns integer
293 return this - first_cunit_oid[this / 0x01000000] + 1
294 endmethod295 296 method to_item_index takes nothing returns integer
297 return this - 'I000' + 1
298 endmethod299 method to_citem_index takes nothing returns integer
300 return this - 'I600' + 1
301 endmethod302 303 method to_destructable_index takes nothing returns integer
304 return this - 'B000' + 1
305 endmethod306 method to_cdestructable_index takes nothing returns integer
307 return this - 'B600' + 1
308 endmethod309 310 method to_doodad_index takes nothing returns integer
311 return this - 'D000' + 1
312 endmethod313 method to_cdoodad_index takes nothing returns integer
314 return this - 'D600' + 1
315 endmethod316 317 method to_ability_index takes nothing returns integer
318 return this - 'A000' + 1
319 endmethod320 method to_cability_index takes nothing returns integer
321 return this - 'A600' + 1
322 endmethod323 324 method to_buff_index takes nothing returns integer
325 return this - 'B000' + 1
326 endmethod327 method to_cbuff_index takes nothing returns integer
328 return this - 'B600' + 1
329 endmethod330 331 method to_upgrade_index takes nothing returns integer
332 return this - 'R000' + 1
333 endmethod334 method to_cupgrade_index takes nothing returns integer
335 return this - 'R600' + 1
336 endmethod337 338 endstruct339 340 endlibrary341