Warcraft 3 documentation

vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

BJObjectId

Aniki · 874

0 Members and 1 Guest are viewing this topic.

BJObjectId
on: September 03, 2012, 08:42:33 AM
Category: Items, Units
Language: vJASS

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:
Code: jass
  1. globals
  2.     integer array my_items
  3.     integer my_items_count
  4. endglobals
  5.  
  6. ...
  7. // initialize the array
  8. set my_items[1] = 'I000'
  9. set my_items[2] = 'I001'
  10. // ...
  11. set my_items[10] = 'I009'
  12. set my_items[11] = 'I00A'
  13. set my_items_count = 11
  14.  
  15. ...
  16. // walk the array
  17. local integer i = 1
  18. local integer item_id
  19. loop
  20.     exitwhen i > my_items_count
  21.     set item_id = my_items[i]
  22.     // do stuff with item_id
  23.     set i = i + 1
  24. endloop
  25.  

Which is a bit cumbersome when you have more than a dozen or so items, but we can simplify it:
Code: jass
  1. ...
  2. local BJObjectId item_id = BJObjectId('I000')
  3. local BJObjectId last_item_id = BJObjectId('I00A')
  4. loop
  5.     exitwhen item_id > last_item_oid
  6.     // do stuff with item_id
  7.     set item_id = item_id.plus_1()
  8. endloop
  9. ...
  10.  

It seems to me that this scales better if you have to do it a lot in your map.

BJObjectId.j:
Code: jass
  1. library BJObjectId // 1.0
  2.  
  3. //! novjass
  4.  
  5. // creating:
  6.  
  7.     // from native object-id / integer
  8.     set oid = BJObjectId('A000')
  9.  
  10.     // from string
  11.     set oid = BJObjectId.from_str("I001")
  12.  
  13.  
  14. // printing:
  15.  
  16.     call BJDebugMsg(oid.to_str())
  17.  
  18.  
  19. // looping through a set of object-id(s)
  20.  
  21.     // forward
  22.     local BJObjectId oid = BJObjectId('A000')
  23.     local BJObjectId last_oid = BJObjectId('A010')
  24.     loop
  25.         exitwhen oid > last_oid
  26.         // do stuff
  27.         set oid = oid.plus_1()
  28.     endloop
  29.  
  30.     // backward
  31.     local BJObjectId oid = BJObjectId('A010')
  32.     local BJObjectId last_oid = BJObjectId('A000')
  33.     loop
  34.         exitwhen oid < last_oid
  35.         // do stuff
  36.         set oid = oid.minus_1()
  37.     endloop
  38.  
  39.  
  40. // mapping BJObjectId(s) to array-indices / struct instances
  41.  
  42.     local integer index
  43.     local MyStruct my_struct
  44.  
  45.     set index = BJObjectId('H000').to_unit_index()
  46.     set my_struct = MyStruct( BJObjectId('h001').to_unit_index() )
  47.  
  48.     set index = BJObjectId('I000').to_item_index()
  49.     set my_struct = BJObjectId('I001').to_item_index() // don't really need the MyStruct cast (because vJass =))
  50.  
  51.     set index = BJObjectId('B000').to_destructable_index()
  52.     set my_struct = BJObjectId('B001').to_destructable_index()
  53.  
  54.     set index = BJObjectId('D000').to_doodad_index()
  55.     set my_struct = BJObjectId('D001').to_doodad_index()
  56.  
  57.     set index = BJObjectId('A000').to_ability_index()
  58.     set my_struct = BJObjectId('A001').to_ability_index()
  59.  
  60.     set index = BJObjectId('B000').to_buff_index()
  61.     set my_struct = BJObjectId('B001').to_buff_index()
  62.  
  63.     set index = BJObjectId('R000').to_upgrade_index()
  64.     set my_struct = BJObjectId('R001').to_upgrade_index()
  65.  
  66.     // for campaign objects the methods have a "c" after the "to_"
  67.     set index = BJObjectId('A000').to_cunit_index()
  68.     set index = BJObjectId('A000').to_cability_index()
  69.     ...
  70.  
  71. // NOTE: the to_*_index methods break for object-id(s) > 'XXOZ', i.e
  72. // 900 is the maximum number of objects you can have and still be able to use those methods;
  73. // the reason is that the object-id(s) after 'XXOZ' have indices > 8190
  74.  
  75. //! endnovjass
  76.  
  77. struct BJObjectId extends array
  78.  
  79. static method from_str takes string oid returns thistype
  80.     // '0' = 48 .. '9' = 57,
  81.     // 'A' = 65 .. 'Z' = 90
  82.     // 'a' = 97 .. 'z' = 122
  83.     //
  84.     // index(<chr>):
  85.     // '0' = 0; chr(0 + 48) = '0' = 48
  86.     // 'A' = 17; chr(17 + 48) = 'A' = 65
  87.     // 'a' = 49; chr(49 + 48) = 'a' = 97
  88.     //
  89.     local string chars = "0123456789.......ABCDEFGHIJKLMNOPQRSTUVWXYZ......abcdefghijklmnopqrstuvwxyz"
  90.     local integer this = 0
  91.     local integer i
  92.     local integer j
  93.     local integer ordinal
  94.     local string chr
  95.     local integer pow_256 = 1
  96.  
  97.     set i = 3
  98.     loop
  99.         exitwhen i < 0
  100.         set chr = SubString(oid, i, i + 1)
  101.  
  102.         set j = 0
  103.         loop
  104.             exitwhen j >= 75
  105.  
  106.             if chr == SubString(chars, j, j + 1) then
  107.                 set this = this + (j + 48) * pow_256
  108.                 set pow_256 = pow_256 * 256
  109.                 exitwhen true
  110.             endif
  111.  
  112.             set j = j + 1
  113.         endloop
  114.  
  115.         set i = i - 1
  116.     endloop
  117.  
  118.     return this
  119. endmethod
  120.  
  121. method to_str takes nothing returns string
  122.     local string chars = "0123456789.......ABCDEFGHIJKLMNOPQRSTUVWXYZ......abcdefghijklmnopqrstuvwxyz"
  123.     local integer t = this
  124.     local integer i
  125.     local integer b
  126.     local string result = ""
  127.  
  128.     set i = t
  129.     set t = i / 0x100
  130.     set b = i - t * 0x100 - 48
  131.     set result = SubString(chars, b, b + 1) + result
  132.  
  133.     set i = t
  134.     set t = i / 0x100
  135.     set b = i - t * 0x100 - 48
  136.     set result = SubString(chars, b, b + 1) + result
  137.  
  138.     set i = t
  139.     set t = i / 0x100
  140.     set b = i - t * 0x100 - 48
  141.     set result = SubString(chars, b, b + 1) + result
  142.  
  143.     set t = t - 48
  144.     set result = SubString(chars, t, t + 1) + result
  145.  
  146.     return result
  147. endmethod
  148.  
  149. method plus_1 takes nothing returns thistype
  150.     local integer t = this
  151.     local integer i
  152.     local integer b1
  153.     local integer b2
  154.     local integer b3
  155.     local integer b4
  156.  
  157.     set i = t
  158.     set t = i / 0x100
  159.     set b4 = i - t * 0x100
  160.  
  161.     if b4 < 'Z' then
  162.         if b4 != '9' then
  163.             set i = i + 1
  164.         else
  165.             set i = i + 8
  166.         endif
  167.     else
  168.  
  169.         set i = t
  170.         set t = i / 0x100
  171.         set b3 = i - t * 0x100
  172.         if b3 < 'Z' then
  173.             if b3 != '9' then
  174.                 set i = i * 0x00000100 + 0x00000100 + '0'
  175.             else
  176.                 set i = i * 0x00000100 + 0x00000800 + '0'
  177.             endif
  178.         else
  179.  
  180.             set i = t
  181.             set t = i / 0x100
  182.             set b2 = i - t * 0x100
  183.             if b2 < 'Z' then
  184.                 if b2 != '9' then
  185.                     set i = i * 0x00010000 + 0x00010000 + '0' * 0x00000100 + '0'
  186.                 else
  187.                     set i = i * 0x00010000 + 0x00080000 + '0' * 0x00000100 + '0'
  188.                 endif
  189.             else
  190.  
  191.                 set i = t
  192.                 if i != '9' then
  193.                     set i = i * 0x01000000 + 0x01000000 + '0' * 0x00010000 + '0' * 0x00000100 + '0'
  194.                 else
  195.                     set i = i * 0x01000000 + 0x08000000 + '0' * 0x00010000 + '0' * 0x00000100 + '0'
  196.                 endif
  197.             endif
  198.         endif
  199.     endif
  200.  
  201.     return i
  202. endmethod
  203.  
  204. method minus_1 takes nothing returns thistype
  205.     local integer t = this
  206.     local integer i
  207.     local integer b1
  208.     local integer b2
  209.     local integer b3
  210.     local integer b4
  211.  
  212.     set i = t
  213.     set t = i / 0x100
  214.     set b4 = i - t * 0x100
  215.     if b4 > '0' then
  216.         if b4 != 'A' then
  217.             set i = i - 1
  218.         else
  219.             set i = i - 8
  220.         endif
  221.     else
  222.  
  223.         set i = t
  224.         set t = i / 0x100
  225.         set b3 = i - t * 0x100
  226.         if b3 > '0' then
  227.             if b3 != 'A' then
  228.                 set i = i * 0x00000100 - 0x00000100 + 'Z'
  229.             else
  230.                 set i = i * 0x00000100 - 0x00000800 + 'Z'
  231.             endif
  232.         else
  233.  
  234.             set i = t
  235.             set t = i / 0x100
  236.             set b2 = i - t * 0x100
  237.             if b2 > '0' then
  238.                 if b2 != 'A' then
  239.                     set i = i * 0x00010000 - 0x00010000 + 'Z' * 0x00000100 + 'Z'
  240.                 else
  241.                     set i = i * 0x00010000 - 0x00080000 + 'Z' * 0x00000100 + 'Z'
  242.                 endif
  243.             else
  244.  
  245.                 set i = t
  246.                 if i != 'A' then
  247.                     set i = i * 0x01000000 - 0x01000000 + 'Z' * 0x00010000 + 'Z' * 0x00000100 + 'Z'
  248.                 else
  249.                     set i = i * 0x01000000 - 0x08000000 + 'Z' * 0x00010000 + 'Z' * 0x00000100 + 'Z'
  250.                 endif
  251.             endif
  252.         endif
  253.     endif
  254.  
  255.     return i
  256. endmethod
  257.  
  258. method operator< takes thistype other returns boolean
  259.     return integer(this) < integer(other)
  260. endmethod
  261.  
  262. private static integer array first_unit_oid
  263. private static integer array first_cunit_oid
  264. private static method onInit takes nothing returns nothing
  265.     set first_unit_oid['H'] = 'H000'
  266.     set first_unit_oid['h'] = 'h000'
  267.     set first_unit_oid['O'] = 'O000'
  268.     set first_unit_oid['o'] = 'o000'
  269.     set first_unit_oid['E'] = 'E000'
  270.     set first_unit_oid['e'] = 'e000'
  271.     set first_unit_oid['U'] = 'U000'
  272.     set first_unit_oid['u'] = 'u000'
  273.     set first_unit_oid['N'] = 'N000'
  274.     set first_unit_oid['n'] = 'n000'
  275.  
  276.     set first_cunit_oid['H'] = 'H600'
  277.     set first_cunit_oid['h'] = 'h600'
  278.     set first_cunit_oid['O'] = 'O600'
  279.     set first_cunit_oid['o'] = 'o600'
  280.     set first_cunit_oid['E'] = 'E600'
  281.     set first_cunit_oid['e'] = 'e600'
  282.     set first_cunit_oid['U'] = 'U600'
  283.     set first_cunit_oid['u'] = 'u600'
  284.     set first_cunit_oid['N'] = 'N600'
  285.     set first_cunit_oid['n'] = 'n600'
  286. endmethod
  287.  
  288. method to_unit_index takes nothing returns integer
  289.     return this - first_unit_oid[this / 0x01000000] + 1
  290. endmethod
  291. method to_cunit_index takes nothing returns integer
  292.     return this - first_cunit_oid[this / 0x01000000] + 1
  293. endmethod
  294.  
  295. method to_item_index takes nothing returns integer
  296.     return this - 'I000' + 1
  297. endmethod
  298. method to_citem_index takes nothing returns integer
  299.     return this - 'I600' + 1
  300. endmethod
  301.  
  302. method to_destructable_index takes nothing returns integer
  303.     return this - 'B000' + 1
  304. endmethod
  305. method to_cdestructable_index takes nothing returns integer
  306.     return this - 'B600' + 1
  307. endmethod
  308.  
  309. method to_doodad_index takes nothing returns integer
  310.     return this - 'D000' + 1
  311. endmethod
  312. method to_cdoodad_index takes nothing returns integer
  313.     return this - 'D600' + 1
  314. endmethod
  315.  
  316. method to_ability_index takes nothing returns integer
  317.     return this - 'A000' + 1
  318. endmethod
  319. method to_cability_index takes nothing returns integer
  320.     return this - 'A600' + 1
  321. endmethod
  322.  
  323. method to_buff_index takes nothing returns integer
  324.     return this - 'B000' + 1
  325. endmethod
  326. method to_cbuff_index takes nothing returns integer
  327.     return this - 'B600' + 1
  328. endmethod
  329.  
  330. method to_upgrade_index takes nothing returns integer
  331.     return this - 'R000' + 1
  332. endmethod
  333. method to_cupgrade_index takes nothing returns integer
  334.     return this - 'R600' + 1
  335. endmethod
  336.  
  337. endstruct
  338.  
  339. endlibrary
  340.  
« Last Edit: December 19, 2017, 11:39:49 PM by moyack »



 

Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)