Guest User

Untitled

a guest
Sep 19th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 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["locator"].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.  
  65. function gadget:CommandFallback(unitID, unitDefID, unitTeam, cmdID, cmdParams, cmdOptions, cmdTag)
  66. metalx = 1000
  67. metaly = 2800
  68. metalz = 2800
  69.  
  70. if cmdID == CMD_STRT_LOC then --is beloved our custom command?
  71. if LOCATORS[unitDefID] then --is it a unit that should care about our custom command?
  72. activeLocators[unitID] = true
  73. local x = cmdParams[1]
  74. local y = cmdParams[2]
  75. local z = cmdParams[3]
  76. Spring.Echo("commandfallback is called.")
  77. Spring.GiveOrderToUnit(unitID, CMD.MOVE , {x,y,z }, {})
  78. --tells the locator to move to coords x,y,z; move to click
  79. else -- it is not our beloved custom command anymore!
  80. activeLocators[unitID] = nil
  81. end
  82.  
  83.  
  84.  
  85.  
  86.  
  87. --unitID ==
  88.  
  89.  
  90. -- if locatorList[ unitID ] then
  91. -- local midPos = true
  92. -- local aimPos = false
  93. -- local locatorPosX, _, locatorPosZ = Spring.GetUnitPosition(unitID, midPos, aimPos)
  94. -- Spring.Echo("The locator is told to distance check for mineral deposits.")
  95. -- if math.abs(metalx - locatorPosX)<=200 and math.abs(metalz - locatorPosZ)<=200 then
  96. -- Spring.Echo("The Locator is within range of a mineral deposit!")
  97. --local metalx = cmdParams[1]
  98. --local metaly = cmdParams[2]
  99. --local metalz = cmdParams[3]
  100. -- Spring.GiveOrderToUnit(unitID, CMD.MOVE , {metalx,metaly,metalz }, {})
  101.  
  102. -- end
  103. --else
  104. --Spring.Echo("That's not a locator.")
  105. --end
  106. end
  107.  
  108.  
  109.  
  110. end
  111.  
  112. function gadget:GameFrame(frame)
  113. if frame%10 == 0 then
  114. Spring.Echo("GameFrame initiated.")
  115. for unitID in pairs(activeLocators) do
  116. local unitDefID = Spring.GetUnitDefID(unitID)
  117. local locatorPosX, _, locatorPosZ = Spring.GetUnitPosition(unitID, midPos, aimPos)
  118. Spring.Echo("The locator is told to distance check for mineral deposits.")
  119. if math.abs(metalx - locatorPosX)<=200 and math.abs(metalz - locatorPosZ)<=200 then
  120. Spring.Echo("The Locator is within range of a mineral deposit!")
  121. depositLocated = true
  122. Spring.SetUnitMoveGoal(unitID, metalx, metaly, metalz)
  123. else
  124. depositLocated = nil
  125. --local metalx = cmdParams[1]
  126. --local metaly = cmdParams[2]
  127. --local metalz = cmdParams[3]
  128. end
  129. end
  130. end
  131. end
  132.  
  133. function gadget:UnitDestroyed(unitID, unitDefID, unitTeam)
  134. if locatorList[ unitID ] then
  135. locatorList[ unitID ] = nil
  136. end
  137. end
Advertisement
Add Comment
Please, Sign In to add comment