Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- configuration, value 0 must be first
- local howMuchItems = { 0, 50, 100, 150, 200 }
- function widget:GetInfo()
- return {
- name = "zzz terraform helper",
- desc = "build terraformed defenses",
- author = "Svatopluk",
- date = "7/20/2015",
- license = "Hail Svatopluk 3x",
- layer = 100,
- enabled = true -- loaded by default?
- }
- end
- local windows = {}
- local Window
- local ScrollPanel
- local Chili
- local lab
- local scrollpanelContent
- local function tableToStr(t)
- local res = "{ "
- for key,value in pairs(t) do
- local valStr = value
- if type(valStr) == "boolean" then
- if valStr == true then
- valStr = "true"
- else
- valStr = "false"
- end
- end
- --res = res .. type(value) .. "; "
- res = res .. key .. "=" .. valStr .. "; "
- end
- res = res .. " }"
- return res
- end
- local myLogFile
- function openLogFile()
- -- myLogFile = io.open("gui_terraform_helper.log", "w")
- end
- function writeLog(msg)
- -- local currentTime = os.date("*t")
- -- myLogFile:write("" .. currentTime.hour .. ":" .. currentTime.min .. ":" .. currentTime.sec .. ": ")
- -- myLogFile:write(msg .. "\n")
- -- myLogFile:flush()
- end
- function closeLogFile()
- -- myLogFile:close()
- end
- local currentTerraformSelectedIndex = 1
- local currentTerraformSelectedValue = 0
- function widget:Shutdown()
- writeLog("widget:Shutdown()")
- closeLogFile()
- for i=1,#windows do
- (windows[i]):Dispose()
- end
- end
- local function buttonHighlight(btn)
- btn.font.color = {1.0, 0.0, 0.0, 1.0} -- RGBA
- btn:Invalidate()
- end
- local function buttonNormal(btn)
- btn.font.color = {1.0, 1.0, 1.0, 1.0} -- RGBA
- btn:Invalidate()
- end
- function widget:Initialize()
- openLogFile()
- writeLog("widget:Initialize()")
- if (not WG.Chili) then
- widgetHandler:RemoveWidget()
- return
- end
- Chili = WG.Chili
- Window = Chili.Window
- ScrollPanel = Chili.ScrollPanel
- local screen0 = Chili.Screen0
- local buttons = {}
- for i = 1, #howMuchItems do
- local txt = "" .. howMuchItems[i]
- buttons[i] = Chili.Button:New{
- x = 5,
- y = 5+(i-1)*20,
- width = 150,
- height = 15,
- caption = txt,
- fontsize = 13,
- autosize = false,
- --textColor = {1.0, 0.0, 0.0, 1.0},
- anchors = {left=true,bottom=true,right=true},
- OnClick = { function()
- --buttons[i]:SetCaption("selected")
- writeLog("clicked " .. txt .. ", prev value = " .. buttons[currentTerraformSelectedIndex].caption)
- buttonNormal(buttons[currentTerraformSelectedIndex])
- currentTerraformSelectedIndex = i
- buttonHighlight(buttons[currentTerraformSelectedIndex])
- currentTerraformSelectedValue = 0 + txt
- end },
- }
- buttonNormal(buttons[i])
- end
- buttonHighlight(buttons[currentTerraformSelectedIndex])
- windowMain = Chili.Window:New{
- dockable = true,
- parent = screen0,
- caption = "Terraform",
- draggable = true,
- resizable = true,
- dragUseGrip = true,
- clientWidth = 200,
- clientHeight = 50,
- backgroundColor = {0.8,0.8,0.8,0.9},
- children = buttons,
- }
- windows[#windows+1] = windowMain
- end
- local CMD_RAMP = 39734
- local CMD_LEVEL = 39736
- local CMD_RAISE = 39737
- local CMD_SMOOTH = 39738
- local CMD_RESTORE = 39739
- local CMD_BUMPY = 39740
- local CMD_TERRAFORM_INTERNAL = 39801
- local function GetTerraParameters(terraType ,points, constructors, teamID, volumeSelection)
- local insParams = {}
- --insParams[1] = terraform_type -- 1 = level, 2 = raise, 3 = smooth, 4 = ramp, 5 = restore
- insParams[1] = terraType
- --insParams[2] = team -- teamID of the team doing the terraform
- insParams[2] = teamID
- --insParams[3] = loop -- true or false
- insParams[3] = 1 --need to be closed
- insParams[4] = points[1].y --height
- insParams[5] = #points -- how many points there are in the lasso (2 for ramp)
- insParams[6] = #constructors -- how many constructors are working on it
- --insParams[7] = volumeSelection -- 0 = none, 1 = only raise, 2 = only lower
- insParams[7] = volumeSelection
- local i = 8
- for j = 1, #points do
- insParams[i] = points[j].x
- insParams[i + 1] = points[j].z
- i = i + 2
- end
- --i = i + 2
- for j = 1, #constructors do
- insParams[i] = constructors[j]
- i = i + 1
- end
- return insParams
- end
- -- [21:26] ivand bx1 is x, bz1 is z, bh1, bw1 are "xsize" and "zsize" normalized to unitheading (S/W/E/N)
- -- [21:27] ivand woff, hoff are mostly irrelevant and used only to cover buildings
- -- [21:27] ivand woff, hoff are mostly irrelevant and used only to cover buildings with terrawall
- -- [21:27] ivand so assume 0
- -- [21:27] ivand liftHeight is absolute (not relative) height if I recall it right
- -- [21:28] ivand notice "-1"'s in the code, this was the tricky part to figure out
- function math.round(a)
- return math.floor(a+0.5)
- end
- local function GetTerraRectByParams(bx1, bz1, bw1, bh1, liftHeight, woff, hoff)
- local rect={
- {x=bx1-bw1-woff, y=math.round(liftHeight), z=bz1-bh1-hoff},
- {x=bx1-bw1-woff, y=math.round(liftHeight), z=bz1+bh1+hoff-1},
- {x=bx1+bw1+woff-1, y=math.round(liftHeight), z=bz1+bh1+hoff-1},
- {x=bx1+bw1+woff-1, y=math.round(liftHeight), z=bz1-bh1-hoff}
- }
- rect[5]=rect[1]
- return rect
- end
- local buildOrdersLater = {}
- function widget:GameFrame(f)
- -- works, no need to log: writeLog("widget:GameFrame")
- local i = 1
- while i <= #buildOrdersLater do
- local item = buildOrdersLater[i]
- local pos = item[1]
- local targetTerraform = item[2]
- -- Spring.GetGroundHeight( number x, number z ) -> number y
- local currentHeight = Spring.GetGroundHeight(pos.x, pos.z)
- -- writeLog("current height = " .. currentHeight .. ", expecting " .. targetTerraform)
- if targetTerraform == currentHeight then
- writeLog("giving command and removing")
- local orderParams = item[3]
- Spring.GiveOrderToUnitArray(orderParams[1], orderParams[2], orderParams[3], orderParams[4])
- table.remove(buildOrdersLater, i)
- else
- i = i + 1
- end
- end
- if #buildOrdersLater == 0 then
- --widgetHandler:RemoveWidgetCallIn("GameFrame", self)
- --writeLog("widget:GameFrame - removing myself")
- end
- end
- -- return: bool removeCmd
- -- Called when a command is issued. Returning true deletes the command and does not send it through the network.
- function widget:CommandNotify(cmdId, cmdParams, cmdOptions)
- if currentTerraformSelectedIndex > 1 then
- -- check if command issued is to make building (hardcoded acceptable - stardust, ddm)
- if cmdId < 0 then
- local unitDefId = -cmdId
- writeLog("notified about build command for unitDefId id " .. unitDefId)
- -- 37 = stardust
- -- 275 = ddm
- if unitDefId == 37 or unitDefId == 275 then
- -- TODO: if yes, insert terraform command before it
- writeLog("inserting terraform command")
- --writeLog("cmdParams = " .. tableToStr(cmdParams))
- --writeLog("cmdOptions = " .. tableToStr(cmdOptions))
- -- zero-k XZ map and Y height
- -- cmdParams = { X / horizontal map pos, Y / height of position, Z / map vertical pos }
- local commandPos = {}
- commandPos.x = cmdParams[1]
- commandPos.y = cmdParams[2]
- commandPos.z = cmdParams[3]
- local terraformHowMuch = 0 + currentTerraformSelectedValue
- -- hardcoded values, seems to work, grid width x height on map
- local unitWidth = 10
- local unitHeight = 10
- local rect = GetTerraRectByParams(commandPos.x, commandPos.z, unitWidth, unitHeight, commandPos.y + terraformHowMuch, 0, 0)
- local selectedUnits = Spring.GetSelectedUnits()
- local myTeamId = Spring.GetMyTeamID()
- local volumeSelection = 0
- local terraformParams = GetTerraParameters(1, rect, selectedUnits, myTeamId, volumeSelection)
- -- this works - replaces build command with terraform command
- -- Spring.GiveOrderToUnit(number unitID, number cmdID, params = {number, etc...}, options = {"alt", "ctrl", "shift", "right"}))
- Spring.GiveOrderToUnit(selectedUnits[1], CMD_TERRAFORM_INTERNAL, terraformParams, {"shift"})
- --Spring.GiveOrderToUnit(selectedUnits[1], cmdId, cmdParams, cmdOptions)
- buildOrdersLater[#buildOrdersLater+1] = { commandPos, rect[1].y, { selectedUnits, cmdId, cmdParams, cmdOptions } }
- --widgetHandler:UpdateWidgetCallIn("GameFrame", self)
- return true
- -- trying to insert .... DOESN'T WORK
- --options.ALT -> treat param0 as a position instead of a tag
- --options.CONTROL -> use the build queue for factories
- --params[0] = command tag or position (negative numbers to reference
- --the back of the queue and 0 the first)
- --params[1] = insertCmd id
- --params[2] = insertCmd options (shift,alt,right click,etc.)
- --params[3 ... N+2] = insertCmd params[0 ... N-1]
- --local insertParams = {-1, CMD_TERRAFORM_INTERNAL, CMD.OPT_SHIFT }
- --for key,value in pairs(terraformParams) do
- -- insertParams[#insertParams+1] = value
- --end
- --Spring.GiveOrderToUnit(selectedUnits[1], CMD.INSERT, insertParams, {"alt"});
- end
- end
- end
- return false
- end
Advertisement
Add Comment
Please, Sign In to add comment