Guest User

gui_terraform_helper.lua

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