Guest User

locatecmd.lua

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