Guest User

Untitled

a guest
Sep 23rd, 2012
34
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function gadget:GetInfo()
  2. return {
  3. name = "Area Mex (gadget)",
  4. desc = "Adds a command to cap mexes in an area.",
  5. author = "Google Frog",
  6. date = "June 22, 2010",
  7. license = "GNU GPL, v2 or later",
  8. handler = true,
  9. layer = 0,
  10. enabled = true -- loaded by default?
  11. }
  12. end
  13.  
  14. local spInsertUnitCmdDesc = Spring.InsertUnitCmdDesc
  15. local spGiveOrderToUnit = Spring.GiveOrderToUnit
  16. local spGetUnitPosition = Spring.GetUnitPosition
  17.  
  18. local sqrt = math.sqrt
  19. local tasort = table.sort
  20. local taremove = table.remove
  21.  
  22. local CMD_AREA_MEX = 35792
  23.  
  24. include "LuaRules/Gadgets/mex_spot_finder.lua"
  25.  
  26. local mexCmdDesc = {
  27. id = CMD_AREA_MEX,
  28. type = CMDTYPE.ICON_AREA,
  29. name = 'Mex',
  30. cursor = 'areamex_cursor',
  31. action = 'areamex',
  32. tooltip = 'Builds Metal Extractors in the area',
  33. texture = 'bitmaps/ui/buttons/cmd_area_mex.png',
  34. }
  35.  
  36. Spring.AssignMouseCursor("areamex_cursor", "areamex_def", true)
  37.  
  38. local underWaterMexID = euwmetalextractor
  39.  
  40. local landMexID = emetalextractor
  41.  
  42. local mexSpot = {}
  43.  
  44. local recentTeamOrder = {}
  45.  
  46. local function disSQ(x1,z1,x2,z2)
  47. local dis = (x1-x2)*(x1-x2)+(z1-z2)*(z1-z2)
  48. return dis
  49. end
  50.  
  51. function TellUnitToBuildMexAt(uId, x, z)
  52. local y = Spring.GetGroundHeight(x, z)
  53. if y < 0 then
  54. spGiveOrderToUnit(uId, underWaterMexID, {x, y, z}, {"shift"})
  55. else
  56. spGiveOrderToUnit(uId, landMexID, {x, y, z} , {"shift"})
  57. end
  58. end
  59.  
  60. function gadget:AllowCommand(unitID, unitDefID, teamID,cmdID, cmdParams, cmdOptions)
  61.  
  62. if (cmdID == CMD_AREA_MEX) then
  63.  
  64. -- give previous order to con as speedup
  65. if recentTeamOrder[teamID] then
  66. local data = recentTeamOrder[teamID]
  67. local recentParams = data.params
  68. if recentParams[1] == cmdParams[1] and recentParams[2] == cmdParams[2] and recentParams[3] == cmdParams[3] and recentParams[4] == cmdParams[4] then
  69. if not cmdOptions.shift then
  70. spGiveOrderToUnit(unitID, CMD.STOP, {} , CMD.OPT_RIGHT )
  71. end
  72. for i = 1, data.mex.count do
  73. TellUnitToBuildMexAt(unitID, data.mex[i].x, data.mex[i].z)
  74. end
  75. return false
  76. end
  77. end
  78. local cx, cy, cz, cr = cmdParams[1], cmdParams[2], cmdParams[3], cmdParams[4]
  79.  
  80. local xmin = cx-cr
  81. local xmax = cx+cr
  82. local zmin = cz-cr
  83. local zmax = cz+cr
  84.  
  85. local commands = {count = 0}
  86. local orderedCommands = {count = 0}
  87.  
  88. local ux,_,uz = spGetUnitPosition(unitID)
  89.  
  90. for i = 1, mexSpot.count do
  91. if (disSQ(cx,cz,mexSpot[i].x,mexSpot[i].z) < cr^2) then -- circle area, slower
  92. commands[commands.count+1] = {x = mexSpot[i].x, z = mexSpot[i].z, d = disSQ(ux,uz,mexSpot[i].x,mexSpot[i].z)}
  93. commands.count = commands.count+1
  94. end
  95. end
  96.  
  97. while commands.count > 0 do
  98. tasort(commands, function(a,b) return a.d < b.d end)
  99. orderedCommands[orderedCommands.count+1] = commands[1]
  100. orderedCommands.count = orderedCommands.count+1
  101. comx = commands[1].x
  102. comz = commands[1].z
  103. taremove(commands, 1)
  104. commands.count = commands.count-1
  105. for i = 1, commands.count do
  106. commands[i].d = disSQ(comx,comz,commands[i].x,commands[i].z)
  107. end
  108. end
  109.  
  110. recentTeamOrder[teamID] = {
  111. params = {cmdParams[1],cmdParams[2],cmdParams[3],cmdParams[4]},
  112. mex = {count = 0},
  113. }
  114.  
  115. if not cmdOptions.shift then
  116. spGiveOrderToUnit(unitID, CMD.STOP, {} , CMD.OPT_RIGHT )
  117. end
  118. for i = 1, orderedCommands.count do
  119. TellUnitToBuildMexAt(unidID, orderedCommands[i].x, orderedCommands[i].z)
  120. recentTeamOrder[teamID].mex[recentTeamOrder[teamID].mex.count+1] = {x = orderedCommands[i].x, z = orderedCommands[i].z}
  121. recentTeamOrder[teamID].mex.count = recentTeamOrder[teamID].mex.count+1
  122. end
  123.  
  124. return false
  125. end
  126. return true -- allowed
  127. end
  128.  
  129. function gadget:UnitCreated(unitID, unitDefID)
  130. local ud = UnitDefs[unitDefID]
  131. if ud.isBuilder and not ud.isFactory then
  132. spInsertUnitCmdDesc(unitID, mexCmdDesc)
  133. end
  134. end
  135.  
  136. function gadget:Initialize()
  137. Spring.SendCommands({
  138. "unbindkeyset o",
  139. })
  140. Spring.SendCommands("bind o areamex")
  141. mexSpot = GetMetalMap()
  142. if not mexSpot then
  143. Spring.Echo("Mex spot detection failed")
  144. gadgetHandler:RemoveGadget()
  145. return
  146. end
  147. end
RAW Paste Data