Guest User

gui_terraform_helper.lua

a guest
Jul 22nd, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.89 KB | None | 0 0
  1. -- configuration, value 0 must be first
  2. local howMuchItems = { 0, 50, 100, 150, 200 }
  3.  
  4.  
  5. function widget:GetInfo()
  6.   return {
  7.         name        = "zzz terraform helper",
  8.         desc        = "build terraformed defenses",
  9.         author      = "Svatopluk",
  10.         date        = "7/20/2015",
  11.         license     = "Hail Svatopluk 3x",
  12.         layer       = 100,
  13.         enabled   = true  --  loaded by default?
  14.   }
  15. end
  16.  
  17.  
  18. local windows = {}
  19. local Window
  20. local ScrollPanel
  21. local Chili
  22. local lab
  23. local scrollpanelContent
  24.  
  25. local function tableToStr(t)
  26.     local res = "{ "
  27.     for key,value in pairs(t) do
  28.         local valStr = value
  29.         if type(valStr) == "boolean" then
  30.             if valStr == true then
  31.                 valStr = "true"
  32.             else
  33.                 valStr = "false"
  34.             end
  35.         end
  36.         --res = res .. type(value) .. "; "
  37.         res = res .. key .. "=" .. valStr .. "; "
  38.     end
  39.     res = res .. " }"
  40.     return res
  41. end
  42.  
  43. local myLogFile
  44. function openLogFile()
  45.     -- myLogFile = io.open("gui_terraform_helper.log", "w")
  46. end
  47. function writeLog(msg)
  48.     -- local currentTime = os.date("*t")
  49.     -- myLogFile:write("" .. currentTime.hour  .. ":" .. currentTime.min  .. ":" .. currentTime.sec  .. ": ")
  50.     -- myLogFile:write(msg .. "\n")
  51.     -- myLogFile:flush()
  52. end
  53. function closeLogFile()
  54.     -- myLogFile:close()
  55. end
  56.  
  57. local currentTerraformSelectedIndex = 1
  58. local currentTerraformSelectedValue = 0
  59.  
  60. function widget:Shutdown()
  61.     writeLog("widget:Shutdown()")
  62.     closeLogFile()
  63.     for i=1,#windows do
  64.         (windows[i]):Dispose()
  65.     end
  66. end
  67.  
  68. local function buttonHighlight(btn)
  69.     btn.font.color = {1.0, 0.0, 0.0, 1.0} -- RGBA
  70.     btn:Invalidate()
  71. end
  72. local function buttonNormal(btn)
  73.     btn.font.color = {1.0, 1.0, 1.0, 1.0} -- RGBA
  74.     btn:Invalidate()
  75. end
  76.  
  77. function widget:Initialize()
  78.     openLogFile()
  79.     writeLog("widget:Initialize()")
  80.     if (not WG.Chili) then
  81.         widgetHandler:RemoveWidget()
  82.         return
  83.     end
  84.  
  85.     Chili = WG.Chili
  86.     Window = Chili.Window
  87.     ScrollPanel = Chili.ScrollPanel
  88.        
  89.     local screen0 = Chili.Screen0
  90.    
  91.     local buttons = {}
  92.     for i = 1, #howMuchItems do
  93.         local txt = "" .. howMuchItems[i]
  94.         buttons[i] = Chili.Button:New{
  95.             x = 5,
  96.             y = 5+(i-1)*20,
  97.             width = 150,
  98.             height = 15,
  99.             caption = txt,
  100.             fontsize = 13,
  101.             autosize = false,
  102.             --textColor = {1.0, 0.0, 0.0, 1.0},
  103.             anchors = {left=true,bottom=true,right=true},
  104.             OnClick = { function()
  105.                 --buttons[i]:SetCaption("selected")
  106.                 writeLog("clicked " .. txt .. ", prev value = " .. buttons[currentTerraformSelectedIndex].caption)
  107.                 buttonNormal(buttons[currentTerraformSelectedIndex])
  108.                 currentTerraformSelectedIndex = i
  109.                 buttonHighlight(buttons[currentTerraformSelectedIndex])
  110.                 currentTerraformSelectedValue = 0 + txt
  111.             end },
  112.         }
  113.         buttonNormal(buttons[i])
  114.     end
  115.     buttonHighlight(buttons[currentTerraformSelectedIndex])
  116.    
  117.    
  118.     windowMain = Chili.Window:New{
  119.         dockable = true,
  120.         parent = screen0,
  121.         caption = "Terraform",
  122.         draggable = true,
  123.         resizable = true,
  124.         dragUseGrip = true,
  125.         clientWidth = 200,
  126.         clientHeight = 50,
  127.         backgroundColor = {0.8,0.8,0.8,0.9},
  128.        
  129.         children =  buttons,
  130.     }
  131.     windows[#windows+1] = windowMain
  132.  
  133. end
  134.  
  135.  
  136. local CMD_RAMP = 39734
  137. local CMD_LEVEL = 39736
  138. local CMD_RAISE = 39737
  139. local CMD_SMOOTH = 39738
  140. local CMD_RESTORE = 39739
  141. local CMD_BUMPY = 39740
  142. local CMD_TERRAFORM_INTERNAL = 39801
  143.  
  144. local function GetTerraParameters(terraType ,points, constructors, teamID, volumeSelection)
  145.     local insParams = {}   
  146.     --insParams[1] = terraform_type -- 1 = level, 2 = raise, 3 = smooth, 4 = ramp, 5 = restore
  147.     insParams[1] = terraType   
  148.     --insParams[2] = team -- teamID of the team doing the terraform
  149.     insParams[2] = teamID  
  150.     --insParams[3] = loop -- true or false
  151.     insParams[3] = 1 --need to be closed   
  152.    
  153.     insParams[4] = points[1].y --height
  154.    
  155.     insParams[5] = #points -- how many points there are in the lasso (2 for ramp)
  156.     insParams[6] = #constructors -- how many constructors are working on it
  157.    
  158.     --insParams[7] = volumeSelection -- 0 = none, 1 = only raise, 2 = only lower
  159.     insParams[7] = volumeSelection
  160.        
  161.    
  162.     local i = 8
  163.     for j = 1, #points do
  164.         insParams[i] = points[j].x             
  165.         insParams[i + 1] = points[j].z             
  166.         i = i + 2
  167.     end
  168.            
  169.     --i = i + 2
  170.     for j = 1, #constructors do
  171.         insParams[i] = constructors[j]
  172.         i = i + 1
  173.     end
  174.        
  175.     return insParams   
  176. end
  177.  
  178. -- [21:26]  ivand bx1 is x, bz1 is z, bh1, bw1 are "xsize" and "zsize" normalized to unitheading (S/W/E/N)
  179. -- [21:27]  ivand woff, hoff are mostly irrelevant and used only to cover buildings
  180. -- [21:27]  ivand woff, hoff are mostly irrelevant and used only to cover buildings with terrawall
  181. -- [21:27]  ivand so assume 0
  182. -- [21:27]  ivand liftHeight is absolute (not relative) height if I recall it right
  183. -- [21:28]  ivand notice "-1"'s in the code, this was the tricky part to figure out
  184. function math.round(a)
  185.     return math.floor(a+0.5)
  186. end
  187. local function GetTerraRectByParams(bx1, bz1, bw1, bh1, liftHeight, woff, hoff)
  188.         local rect={
  189.             {x=bx1-bw1-woff, y=math.round(liftHeight), z=bz1-bh1-hoff},
  190.             {x=bx1-bw1-woff, y=math.round(liftHeight), z=bz1+bh1+hoff-1},
  191.             {x=bx1+bw1+woff-1, y=math.round(liftHeight), z=bz1+bh1+hoff-1},
  192.             {x=bx1+bw1+woff-1, y=math.round(liftHeight), z=bz1-bh1-hoff}
  193.         }      
  194.         rect[5]=rect[1]
  195.        
  196.         return rect    
  197. end
  198.  
  199.  
  200. local buildOrdersLater = {}
  201.  
  202. function widget:GameFrame(f)
  203.     -- works, no need to log: writeLog("widget:GameFrame")
  204.     local i = 1
  205.     while i <=  #buildOrdersLater do
  206.         local item = buildOrdersLater[i]
  207.         local pos = item[1]
  208.         local targetTerraform = item[2]
  209.         -- Spring.GetGroundHeight( number x, number z ) -> number y
  210.         local currentHeight = Spring.GetGroundHeight(pos.x, pos.z)
  211.         -- writeLog("current height = " .. currentHeight .. ", expecting " .. targetTerraform)
  212.         if targetTerraform == currentHeight then
  213.             writeLog("giving command and removing")
  214.             local orderParams = item[3]
  215.             Spring.GiveOrderToUnitArray(orderParams[1], orderParams[2], orderParams[3], orderParams[4])
  216.             table.remove(buildOrdersLater, i)
  217.         else
  218.             i = i + 1
  219.         end
  220.     end
  221.     if #buildOrdersLater == 0 then
  222.         --widgetHandler:RemoveWidgetCallIn("GameFrame", self)
  223.         --writeLog("widget:GameFrame - removing myself")
  224.     end
  225. end
  226.  
  227. -- return: bool removeCmd
  228. -- Called when a command is issued. Returning true deletes the command and does not send it through the network.
  229. function widget:CommandNotify(cmdId, cmdParams, cmdOptions)
  230.     if currentTerraformSelectedIndex > 1 then
  231.         -- check if command issued is to make building (hardcoded acceptable - stardust, ddm)
  232.         if cmdId < 0 then
  233.             local unitDefId = -cmdId
  234.             writeLog("notified about build command for unitDefId id " .. unitDefId)
  235.             -- 37 = stardust
  236.             -- 275 = ddm
  237.             if unitDefId == 37 or unitDefId == 275 then
  238.                 -- TODO: if yes, insert terraform command before it
  239.                 writeLog("inserting terraform command")
  240.                 --writeLog("cmdParams = " .. tableToStr(cmdParams))
  241.                 --writeLog("cmdOptions = " .. tableToStr(cmdOptions))
  242.                
  243.                
  244.                 -- zero-k XZ map and Y height
  245.                 -- cmdParams = { X / horizontal map pos, Y / height of position, Z / map vertical pos }
  246.                 local commandPos = {}
  247.                 commandPos.x = cmdParams[1]
  248.                 commandPos.y = cmdParams[2]
  249.                 commandPos.z = cmdParams[3]
  250.                
  251.                 local terraformHowMuch = 0 + currentTerraformSelectedValue
  252.                
  253.                 -- hardcoded values, seems to work, grid width x height on map
  254.                 local unitWidth = 10
  255.                 local unitHeight = 10
  256.                
  257.                
  258.                 local rect = GetTerraRectByParams(commandPos.x, commandPos.z, unitWidth, unitHeight, commandPos.y + terraformHowMuch, 0, 0)
  259.                
  260.                 local selectedUnits = Spring.GetSelectedUnits()
  261.                 local myTeamId = Spring.GetMyTeamID()
  262.                 local volumeSelection = 0
  263.                 local terraformParams = GetTerraParameters(1, rect, selectedUnits, myTeamId, volumeSelection)
  264.  
  265.                 -- this works - replaces build command with terraform command
  266.                 -- Spring.GiveOrderToUnit(number unitID,   number cmdID,  params = {number, etc...},  options = {"alt", "ctrl", "shift", "right"}))
  267.                 Spring.GiveOrderToUnit(selectedUnits[1], CMD_TERRAFORM_INTERNAL, terraformParams, {"shift"})
  268.                 --Spring.GiveOrderToUnit(selectedUnits[1], cmdId, cmdParams, cmdOptions)
  269.                 buildOrdersLater[#buildOrdersLater+1] = { commandPos, rect[1].y, { selectedUnits, cmdId, cmdParams, cmdOptions } }
  270.                 --widgetHandler:UpdateWidgetCallIn("GameFrame", self)
  271.                 return true
  272.                
  273.                 -- trying to insert .... DOESN'T WORK
  274.                 --options.ALT     -> treat param0 as a position instead of a tag
  275.                 --options.CONTROL -> use the build queue for factories
  276.                 --params[0] = command tag or position (negative numbers to reference
  277.                 --the back of the queue and 0 the first)
  278.                 --params[1] = insertCmd id
  279.                 --params[2] = insertCmd options (shift,alt,right click,etc.)
  280.                 --params[3 ... N+2] = insertCmd params[0 ... N-1]
  281.                 --local insertParams = {-1, CMD_TERRAFORM_INTERNAL, CMD.OPT_SHIFT }
  282.                 --for key,value in pairs(terraformParams) do
  283.                 --  insertParams[#insertParams+1] = value
  284.                 --end
  285.                 --Spring.GiveOrderToUnit(selectedUnits[1], CMD.INSERT, insertParams, {"alt"});
  286.             end
  287.         end
  288.     end
  289.     return false
  290. end
Advertisement
Add Comment
Please, Sign In to add comment