Guest User

Untitled

a guest
Sep 19th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. function gadget:GetInfo()
  2. return {
  3. name = "locatecmd",
  4. desc = "Adds a command button for location initiation.",
  5. author = "Bilal Itani",
  6. date = "March, 2013",
  7. license = "GNU GPL, v3.0 or later",
  8. layer = 0,
  9. enabled = true -- loaded by default?
  10. }
  11. end
  12.  
  13. if (not gadgetHandler:IsSyncedCode()) then return end
  14. midPos = true
  15. aimPos = false
  16. --SYNCED
  17.  
  18.  
  19. --local EditUnitCmdDesc = Spring.EditUnitCmdDesc
  20. --local FindUnitCmdDesc = Spring.FindUnitCmdDesc
  21. --local GiveOrderToUnit = Spring.GiveOrderToUnit
  22. --local SetUnitNeutral = Spring.SetUnitNeutral
  23. --local GetUnitMoveTypeData = Spring.GetUnitMoveTypeData
  24. --local SetGroundMoveTypeData = Spring.MoveCtrl.SetGroundMoveTypeData
  25. --local SetGunshipMoveTypeData = Spring.MoveCtrl.SetGunshipMoveTypeData
  26.  
  27. local LOCATORS = {
  28. [UnitDefNames["tptank"].id] = true, --need to inherit the for real for some reason
  29. }
  30.  
  31. local activeLocators = {} --these too?
  32.  
  33. local locatorList = {}
  34.  
  35. local CMD_STRT_LOC = 34581
  36.  
  37. local locateCmd = {
  38. id = CMD_STRT_LOC,
  39. name = "Locate",
  40. action = "locate",
  41. type = CMDTYPE.ICON_MAP,
  42. tooltip = "Commands the locator to locate.",
  43. params = { 'Stop', 'Locate',}
  44. }
  45. --Info for cmd
  46.  
  47. function gadget:UnitCreated(unitID, unitDefID, unitTeam, builderID) --identifies unitID, unitDefID, unitTeam, and builderID of each unit created. Can be used for specified units.
  48. --callin for when locator is created
  49. if LOCATORS[unitDefID] then
  50. --makes callin locator specific
  51. locatorList[unitID] = true
  52.  
  53. Spring.InsertUnitCmdDesc(unitID, 500, locateCmd)
  54. --actually creates the cmd; makes it visible in the locator's cmd buttons
  55. else
  56.  
  57. end
  58.  
  59. end
  60.  
  61. -- <[2up]knorke> in CommandFallback() you must add the activated locator to a list
  62. -- <[2up]knorke> and in GameFrame you loop over that list and have each locator do its locator thing
  63.  
  64. function gadget:AllowCommand(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions)
  65. Spring.Echo (CMD[cmdID])
  66. if cmdID == CMD.STOP then --lolcator giving STOP command and is not active anymore
  67. Spring.Echo ("a stop command in AllowCommand")
  68. activeLocators[unitID] = nil
  69. end
  70. return true
  71. end
  72.  
  73.  
  74. function gadget:CommandFallback(unitID, unitDefID, unitTeam, cmdID, cmdParams, cmdOptions, cmdTag)
  75. metalx = 1000
  76. metaly = 2800
  77. metalz = 2800
  78.  
  79. if cmdID == CMD_STRT_LOC then --is beloved our custom command?
  80. if LOCATORS[unitDefID] then --is it a unit that should care about our custom command?
  81. activeLocators[unitID] = true
  82. local x = cmdParams[1]
  83. local y = cmdParams[2]
  84. local z = cmdParams[3]
  85. Spring.Echo("commandfallback is called.")
  86. Spring.GiveOrderToUnit(unitID, CMD.MOVE , {x,y,z }, {})
  87. --tells the locator to move to coords x,y,z; move to click
  88. end
  89. else -- it is not our beloved custom command anymore! * this is actually never called
  90. activeLocators[unitID] = nil
  91. Spring.Echo ("a different command in CommandFallback")
  92. end
  93.  
  94.  
  95.  
  96.  
  97.  
  98. --unitID ==
  99.  
  100.  
  101. -- if locatorList[ unitID ] then
  102. -- local midPos = true
  103. -- local aimPos = false
  104. -- local locatorPosX, _, locatorPosZ = Spring.GetUnitPosition(unitID, midPos, aimPos)
  105. -- Spring.Echo("The locator is told to distance check for mineral deposits.")
  106. -- if math.abs(metalx - locatorPosX)<=200 and math.abs(metalz - locatorPosZ)<=200 then
  107. -- Spring.Echo("The Locator is within range of a mineral deposit!")
  108. --local metalx = cmdParams[1]
  109. --local metaly = cmdParams[2]
  110. --local metalz = cmdParams[3]
  111. -- Spring.GiveOrderToUnit(unitID, CMD.MOVE , {metalx,metaly,metalz }, {})
  112.  
  113. -- end
  114. --else
  115. --Spring.Echo("That's not a locator.")
  116. --end
  117. end
  118.  
  119.  
  120.  
  121.  
  122.  
  123. function gadget:GameFrame(frame)
  124. if frame%10 == 0 then
  125. Spring.Echo("GameFrame initiated.")
  126. for unitID in pairs(activeLocators) do
  127. local unitDefID = Spring.GetUnitDefID(unitID)
  128. local locatorPosX, _, locatorPosZ = Spring.GetUnitPosition(unitID, midPos, aimPos)
  129. Spring.Echo("The locator is told to distance check for mineral deposits.")
  130. if math.abs(metalx - locatorPosX)<=200 and math.abs(metalz - locatorPosZ)<=200 then
  131. Spring.Echo("The Locator is within range of a mineral deposit!")
  132. depositLocated = true
  133. Spring.SetUnitMoveGoal(unitID, metalx, metaly, metalz)
  134. else
  135. depositLocated = nil
  136. --local metalx = cmdParams[1]
  137. --local metaly = cmdParams[2]
  138. --local metalz = cmdParams[3]
  139. end
  140. end
  141. end
  142. end
  143.  
  144. function gadget:UnitDestroyed(unitID, unitDefID, unitTeam)
  145. if locatorList[ unitID ] then
  146. locatorList[ unitID ] = nil
  147. end
  148. end
Advertisement
Add Comment
Please, Sign In to add comment