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

[System] Track No New Posts Codes & Snippets

Started by
Purgeandfire

0 Members and 1 Guest are viewing this topic.

Rating

Average Score - 5 / 5

« Created: February 28, 2020, 06:17:46 PM by moyack »

[System] Track
on: July 24, 2012, 04:51:41 PM
Category: Interface, System
Language: vJASS
Download Demo Map

This manages trackable objects in such a way that it actually improves functionality and has an interface to make even the toughest projects very readable.

It allows you to:
- Create trackables for specific players. (without desyncs)
- Easily register a trackable click and trackable hover.
- Retrieve which player clicked the trackable.
- Retrieve trackable information - the model, x, y, z,  and facing.

Trackable2 allows for similar functionality, but this one is improved in both speed and handle efficiency. There are some cases where trackable2 has unnecessary lag/freezes, but the Track library fixes it.

This is one of the only systems I haven't completely rewritten time and time again. :) 

This is the same thing as the hive version but I improved the documentation a bit for readability.

Code: jass
  1. library Track /* v3.0.1.0
  2. *************************************************************************************
  3. *
  4. *   Manages trackable objects, allowing for easy event registrations, data retrieval,
  5. *   and adds the capability of retrieving which player interacted with the trackable.   
  6. *
  7. *************************************************************************************
  8. *
  9. *   */uses/*
  10. *   
  11. *       */ Table /*       hiveworkshop.com/forums/jass-functions-413/snippet-new-table-188084/
  12. *
  13. ************************************************************************************
  14. *
  15. *   SETTINGS
  16. */
  17. globals
  18.     private constant integer PLATFORM = 'Otip'
  19. endglobals
  20. /*
  21. ************************************************************************************
  22. *
  23. *    Functions
  24. *
  25. *        function CreateTrack takes string modelPath, real x, real y, real z, real facing returns Track
  26. *            - Creates a trackable of "modelPath" at the coordinates ( x, y, z ) with
  27. *            - "facing" in degrees.
  28. *        function CreateTrackForPlayer takes string modelPath, real x, real y, real z, real facing returns Track
  29. *            - Same as the function above, except it will create it for only one player.
  30. *
  31. *        function RegisterAnyClickEvent takes code c returns nothing
  32. *            - The code will be executed every time a trackable is clicked.
  33. *        function RegisterAnyHoverEvent takes code c returns nothing
  34. *            - The code will be executed every time a trackable is hovered.
  35. *        function RegisterClickEvent takes Track obj, code c returns nothing
  36. *            - The code will be executed every time a trackable of the instance
  37. *            - "obj" is clicked.
  38. *        function RegisterHoverEvent takes Track obj, code returns nothing
  39. *            - The code will be executed every time a trackable of the instance
  40. *            - "obj" is hovered.
  41. *
  42. *        function EnableTrackInstance takes Track obj, boolean flag returns nothing
  43. *            - If an instance is enabled, it will enable its events to fire.
  44. *            - All instances are enabled by default.
  45. *        function IsTrackInstanceEnabled takes Track obj returns boolean
  46. *            - Returns whether or not an instance has its events enabled.
  47. *
  48. *    Event Responses
  49. *
  50. *        function GetTriggerTrackInstance takes nothing returns Track
  51. *            - Returns the Track instance that had a player interaction.
  52. *        function GetTriggerTrackable takes nothing returns trackable
  53. *            - Returns the trackable object that had a player interaction.
  54. *        function GetTriggerTrackablePlayer takes nothing returns player
  55. *            - Returns the player that had interacted with the trackable object.
  56. *
  57. *******************************************************************
  58. *
  59. *   struct Track
  60. *
  61. *        static Track instance
  62. *           - The triggering instance of the event.
  63. *        static trackable object
  64. *           - The triggering trackable object of the event.
  65. *        static player tracker
  66. *           - The player who interacted with the trackable object of the event.
  67. *
  68. *        readonly real x
  69. *        readonly real y
  70. *        readonly real z
  71. *           - The coordinates of the trackable object.
  72. *        readonly real facing
  73. *           - The facing angle of the trackable object.
  74. *        readonly string model
  75. *           - The string path of the trackable object.
  76. *
  77. *        method operator enabled takes nothing returns boolean
  78. *       
  79. *        static method create takes string modelPath, real x, real y, real z, real facing returns Track
  80. *        static method createForPlayer takes string modelPath, real x, real y, real z, real facing, player p returns Track
  81. *
  82. *        static method registerAnyClick takes code c returns nothing
  83. *        static method registerAnyHover takes code c returns nothing
  84. *
  85. *        method registerOnClick takes code c returns nothing
  86. *        method registerOnHover takes code c returns nothing
  87. *
  88. *        method enable takes nothing returns nothing
  89. *        method disable takes nothing returns nothing
  90. *
  91. *            - All of the above are the struct interface equivalents of the functions.
  92. *
  93. ********************************************************************
  94. *   
  95. *    Credits
  96. *       - Azlier for Trackable2
  97. *       - Arhowk for finding a bug for me
  98. *
  99. ********************************************************************/
  100.    
  101.     private module Init
  102.         private static method onInit takes nothing returns nothing
  103.             set thistype.TrackTable     = Table.create()
  104.         endmethod
  105.     endmodule
  106.  
  107.     struct Track extends array
  108.         private static trigger anyClick = CreateTrigger()
  109.         private static trigger anyHover = CreateTrigger()
  110.         private static Table TrackTable = 0
  111.        
  112.         static thistype  instance = 0
  113.         static trackable object   = null
  114.         static player    tracker  = null
  115.        
  116.         private static integer ic = 0
  117.         private static integer ir = 0
  118.         private thistype rn
  119.        
  120.         readonly real    x
  121.         readonly real    y
  122.         readonly real    z
  123.         readonly real    facing
  124.         readonly string  model
  125.        
  126.         private boolean  flag
  127.         private trigger  reg
  128.         private trigger  onClick
  129.         private trigger  onHover
  130.        
  131.         static method registerAnyClick takes code c returns nothing
  132.             call TriggerAddCondition(.anyClick, Filter(c))
  133.         endmethod
  134.         static method registerAnyHover takes code c returns nothing
  135.             call TriggerAddCondition(.anyHover, Filter(c))
  136.         endmethod
  137.        
  138.         method registerClick takes code c returns nothing
  139.             if .onClick == null then
  140.                 set .onClick = CreateTrigger()
  141.             endif
  142.             call TriggerAddCondition(.onClick, Filter(c))
  143.         endmethod
  144.         method registerHover takes code c returns nothing
  145.             if .onHover == null then
  146.                 set .onHover = CreateTrigger()
  147.             endif
  148.             call TriggerAddCondition(.onHover, Filter(c))
  149.         endmethod
  150.        
  151.         method destroy takes nothing returns nothing
  152.             call TrackTable.remove(GetHandleId(.reg))
  153.             call TrackTable.remove(GetHandleId(.object))
  154.             call DestroyTrigger(.reg)
  155.             call DestroyTrigger(.onClick)
  156.             call DestroyTrigger(.onHover)
  157.             set .rn = ir
  158.             set ir  = this
  159.         endmethod
  160.        
  161.         method enable takes nothing returns nothing
  162.             set this.flag = true
  163.         endmethod
  164.         method disable takes nothing returns nothing
  165.             set this.flag = false
  166.         endmethod
  167.         method operator enabled takes nothing returns boolean
  168.             return this.flag
  169.         endmethod
  170.        
  171.         private static method onInteract takes nothing returns boolean
  172.             local thistype  temp = instance
  173.             local trackable tr   = object
  174.             local player    p    = tracker
  175.            
  176.             set instance = TrackTable[GetHandleId(GetTriggeringTrigger())]
  177.             set object   = GetTriggeringTrackable()
  178.             set tracker  = Player(TrackTable[GetHandleId(object)])
  179.            
  180.             if instance.flag then
  181.                 if GetTriggerEventId() == EVENT_GAME_TRACKABLE_TRACK then
  182.                     call TriggerEvaluate(instance.onHover)
  183.                     call TriggerEvaluate(anyHover)
  184.                 else
  185.                     call TriggerEvaluate(instance.onClick)
  186.                     call TriggerEvaluate(anyClick)
  187.                 endif
  188.             endif
  189.            
  190.             set instance = temp
  191.             set tracker  = p
  192.             set object   = tr
  193.             set tr = null
  194.             set p  = null
  195.             return false
  196.         endmethod
  197.        
  198.         private static method createTrack takes string modelPath, real x, real y, real z, real facing, player j returns thistype
  199.             local destructable dest = null
  200.             local thistype     this = ir
  201.             local integer      i    = 11
  202.             local trackable tr
  203.             local player p
  204.             local string s
  205.             if this == 0 then
  206.                 set ic   = ic + 1
  207.                 set this = ic
  208.             else
  209.                 set ir   = .rn
  210.             endif
  211.             if z != 0 then
  212.                 set dest = CreateDestructableZ(PLATFORM, x, y, z, 0, 1, 0)
  213.             endif
  214.             if j != null then
  215.                 set i    = GetPlayerId(j)
  216.             endif
  217.             set .x = x
  218.             set .y = y
  219.             set .z = z
  220.             set .flag    = true
  221.             set .facing  = facing
  222.             set .model   = modelPath
  223.             set .reg     = CreateTrigger()
  224.             set .onClick = null
  225.             set .onHover = null
  226.             set TrackTable[GetHandleId(.reg)] = this
  227.             call TriggerAddCondition(.reg, Condition(function thistype.onInteract))
  228.             loop
  229.                 set p = Player(i)
  230.                     if GetLocalPlayer() == p then
  231.                         set s = modelPath
  232.                     else
  233.                         set s = ""
  234.                     endif
  235.                     set tr = CreateTrackable(s, .x, .y, .facing)
  236.                     call TriggerRegisterTrackableHitEvent(.reg, tr)
  237.                     call TriggerRegisterTrackableTrackEvent(.reg, tr)
  238.                     set TrackTable[GetHandleId(tr)] = i
  239.                     if j != null then
  240.                         exitwhen true
  241.                     endif
  242.                 endif
  243.                 exitwhen i == 0
  244.                 set i = i - 1
  245.             endloop
  246.             if dest != null then
  247.                 call RemoveDestructable(dest)
  248.                 set dest = null
  249.             endif
  250.             set p  = null
  251.             set tr = null
  252.             return this
  253.         endmethod
  254.        
  255.         static method create takes string modelPath, real x, real y, real z, real facing returns thistype
  256.             return thistype.createTrack(modelPath, x, y, z, facing, null)
  257.         endmethod
  258.        
  259.         static method createForPlayer takes string modelPath, real x, real y, real z, real facing, player p returns thistype
  260.                 return 0
  261.             endif
  262.             return thistype.createTrack(modelPath, x, y, z, facing, p)
  263.         endmethod
  264.        
  265.         implement Init
  266.     endstruct
  267.    
  268.     function CreateTrack takes string modelPath, real x, real y, real z, real facing returns Track
  269.         return Track.create(modelPath, x, y, z, facing)
  270.     endfunction
  271.    
  272.     function CreateTrackForPlayer takes string modelPath, real x, real y, real z, real facing, player who returns Track
  273.         return Track.createForPlayer(modelPath, x, y, z, facing, who)
  274.     endfunction
  275.    
  276.     function EnableTrackInstance takes Track instance, boolean flag returns nothing
  277.         if flag then
  278.             call instance.enable()
  279.         else
  280.             call instance.disable()
  281.         endif
  282.     endfunction
  283.    
  284.     function IsTrackInstanceEnabled takes Track instance returns boolean
  285.         return instance.enabled
  286.     endfunction
  287.    
  288.     function RegisterAnyClickEvent takes code c returns nothing
  289.         call Track.registerAnyClick(c)
  290.     endfunction
  291.    
  292.     function RegisterAnyHoverEvent takes code c returns nothing
  293.         call Track.registerAnyHover(c)
  294.     endfunction
  295.    
  296.     function RegisterClickEvent takes Track obj, code c returns nothing
  297.         call obj.registerClick(c)
  298.     endfunction
  299.    
  300.     function RegisterHoverEvent takes Track obj, code c returns nothing
  301.         call obj.registerHover(c)
  302.     endfunction
  303.    
  304.     function GetTriggerTrackInstance takes nothing returns Track
  305.         return Track.instance
  306.     endfunction
  307.    
  308.     function GetTriggerTrackable takes nothing returns trackable
  309.         return Track.object
  310.     endfunction
  311.    
  312.     function GetTriggerTrackablePlayer takes nothing returns player
  313.         return Track.tracker
  314.     endfunction
  315. endlibrary

API List:
Code: jass
  1. function CreateTrack takes string modelPath, real x, real y, real z, real facing returns Track
  2.  
  3. function CreateTrackForPlayer takes string modelPath, real x, real y, real z, real facing returns Track
  4.  
  5. function RegisterAnyClickEvent takes code c returns nothing
  6.  
  7. function RegisterAnyHoverEvent takes code c returns nothing
  8.  
  9. function RegisterClickEvent takes Track obj, code c returns nothing
  10.  
  11. function RegisterHoverEvent takes Track obj, code c returns nothing
  12.  
  13. function EnableTrackInstance takes Track obj, boolean flag returns nothing
  14.  
  15. function IsTrackInstanceEnabled takes Track obj returns boolean
  16.  
  17. function GetTriggerTrackInstance takes nothing returns Track
  18.  
  19. function GetTriggerTrackable takes nothing returns trackable
  20.  
  21. function GetTriggerTrackablePlayer takes nothing returns player

If you need a sample, then check out the demo map below.
« Last Edit: December 19, 2017, 11:32:17 AM by moyack »



Re: [System] Track
Reply #1 on: July 25, 2012, 10:59:25 PM

The system has a very nice presentation, I like the implementation and the test map is lovely. Approvezord'ed


Re: [System] Track
Reply #2 on: August 03, 2012, 05:48:26 AM

Haha.. Nice, why PnF inactive on Hive??
I think Hive are suck now ( BECAUSE TO ARROGANT!! ) :) :)
PS:
moyack, I added you to my credit list ( Every map i was made ) Without reason..



Re: Re: [System] Track
Reply #3 on: August 03, 2012, 04:32:33 PM

PS:
moyack, I added you to my credit list ( Every map i was made ) Without reason..
All in your lifeshould have a reason, I hope something mine is helping you inn your map :)


Re: [System] Track
Reply #4 on: August 04, 2012, 12:08:52 AM

Because you made this helping site  :P  :P



 

Started by cohadar

Replies: 0
Views: 2040
Codes & Snippets

Started by ashujon

Replies: 0
Views: 2971
Warcraft III Spells and Systems

Started by azlier

Replies: 0
Views: 1787
Codes & Snippets

Started by Magtheridon96

Replies: 0
Views: 2022
Codes & Snippets

Started by moyack

Replies: 6
Views: 18995
Codes & Snippets
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)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...