4 |
local versionNum = '0.1' function widget:GetInfo() return { name = "Improved Newtons", desc = "v".. (versionNum) .."Autotarget for newtons. Select newton hold space then with mouse2 draw rectangle. Space + S to remove rectangle", author = "wolas", date = "2013", license = "GNU GPL, v2 or later", layer = 20, handler = true, enabled = true --loaded by default? } end include("keysym.h.lua") local checkRate = 2 -- spring has 30 frames per second, basically you -- control responsives and accuracy. -- On big setups checkRate = 1 is not recomended -- + count your ping in local newtonUnitDefID = UnitDefNames["corgrav"].id local GL_LINE_STRIP = GL.LINE_STRIP local glLineWidth = gl.LineWidth local glColor = gl.Color local glBeginEnd = gl.BeginEnd local glVertex = gl.Vertex local spTraceScreenRay = Spring.TraceScreenRay local spGetGroundHeight = Spring.GetGroundHeight local spGetMouseState = Spring.GetMouseState local spGetUnitsInRectangle = Spring.GetUnitsInRectangle local spGetUnitDefID = Spring.GetUnitDefID local spGetSelectedUnits = Spring.GetSelectedUnits local spGiveOrderToUnitArray = Spring.GiveOrderToUnitArray local spGetUnitCommands = Spring.GetUnitCommands local spGetUnitPosition = Spring.GetUnitPosition --local ech = Spring.Echo local floor = math.floor local springPoints = {} --[newtonGroupID] -> {points} springPoints[100] = {y = 100} springPoints[100] = nil local tempPoints = {} local newtonIDs = {} --[unitID] -> newtonGroupid local newtonTrianglePoints = {} --[unitID] -> { newtonTriangle Points} local newtonGroupIDs = {} --[newtonGroupID] -> {newtons} local newtonGroupColours = {} --[newtonGroupID] -> { red, green, blue } local selectedNewtons = nil --temprorary {newtons} local intensivity = 255 local colorIndex = 0 local color = {} local victim = nil local victimStillBeingAttacked = false --local damageTaken = 0 local selectedNewton = false local drawStarted = false local allowDraw = false --local cmdRate = 0 --local cmdRateS = 0 local softEnabled = false --if zero newtons has orders, uses less cpu local function FixRectangle(rect) rect.x = floor((rect.x)/16)*16 rect.y = floor((rect.y)/16)*16 rect.x2= floor((rect.x2)/16)*16 rect.y2= floor((rect.y2)/16)*16 rect.z_xy= spGetGroundHeight(rect.x, rect.y) rect.z_x2y2= spGetGroundHeight(rect.x2, rect.y2) rect.z_xy2= spGetGroundHeight(rect.x, rect.y2) rect.z_x2y= spGetGroundHeight(rect.x2, rect.y) if (rect.x2 < rect.x) then tmp = rect.x rect.x = rect.x2 rect.x2 = tmp --Spring.Echo("fixed X") end if (rect.y2 < rect.y) then tmp = rect.y rect.y = rect.y2 rect.y2 = tmp --Spring.Echo("fixed y") end return rect end local function legalPos(pos) return pos and pos[1] > 0 and pos[3] > 0 and pos[1] < Game.mapSizeX and pos[3] < Game.mapSizeZ end local function PickColor() color = {} ind = colorIndex % 6 if colorIndex > 5 and ind == 0 then intensivity = floor(intensivity / 2) end if (ind == 0 ) then color[1] = intensivity color[2] = 0 color[3] = 0 elseif (ind == 1) then color[1] = 0 color[2] = intensivity color[3] = 0 elseif (ind == 2) then color[1] = 0 color[2] = 0 color[3] = intensivity elseif (ind == 3) then color[1] = intensivity color[2] = intensivity color[3] = 0 elseif (ind == 4) then color[1] = intensivity color[2] = 0 color[3] = intensivity elseif (ind == 5) then color[1] = 0 color[2] = intensivity color[3] = intensivity end colorIndex = colorIndex + 1 if colorIndex > 30 then colorIndex = 0 end end local function RemoveDeadGroups(units) for nr, tmpNewtonID in pairs(units) do --remove IDs from groups tmpNewtonGroupID = newtonIDs[tmpNewtonID] newtonIDs[tmpNewtonID] = nil newtonTrianglePoints[tmpNewtonID] = nil if tmpNewtonGroupID ~= nil then group = {} group = newtonGroupIDs[tmpNewtonGroupID] if group ~= nil then ii = 0 for i, id in pairs ( group) do --bad argument to pairs got nil ii = ii +1 if id == tmpNewtonID then newtonGroupIDs[tmpNewtonGroupID][i] = nil --attempt to index nil value end end if ii ==1 then --maybe group is empty newtonGroupIDs[tmpNewtonGroupID] = nil springPoints[tmpNewtonGroupID] = nil newtonGroupColours[tmpNewtonGroupID] = nil end --else --ech("group was nil") --newtonGroupIDs[tmpNewtonGroupID] = nil --springPoints[tmpNewtonGroupID] = nil end end end end function NewGroup(selectedNewtons) newtonGroupIDs[selectedNewtons[1]] = selectedNewtons --lets try how stupid this idea is springPoints[selectedNewtons[1]] = tempPoints PickColor() newtonGroupColours[selectedNewtons[1]] = color color = nil for nr, unitID in pairs (selectedNewtons) do newtonIDs[unitID] = selectedNewtons[1] x, y, z = spGetUnitPosition (unitID) newtonTrianglePoints[unitID] = {y, x - 15, z - 15, x, z + 15, x +15, z -15} end end function CancelDraw() tempPoints = nil tempPoints = {} selectedNewtons = nil end function RemoveOrders(units) selectedNewtons = spGetSelectedUnits() RemoveDeadGroups(selectedNewtons) selectedNewtons = nil --ech("removing orders") end function widget:MousePress(g_mx, g_my, button) if allowDraw then units = spGetSelectedUnits() if selectedNewtons == nil then selectedNewtons = filterNewtons (units) elseif units[1] == nil then selectedNewtons = nil end if selectedNewtons ~= nil then --Spring.Echo (" newton selected") if button == 3 then _, pos = spTraceScreenRay(g_mx,g_my, true) if legalPos(pos) then if drawStarted == false then tempPoints.x = pos[1] tempPoints.y = pos[3] drawStarted = true --widgetHandler:UpdateWidgetCallIn("Update", self) UpdateRectangle() tempPoints = FixRectangle(tempPoints) else drawStarted = false tempPoints.x2 = pos[1] tempPoints.y2 = pos[3] tempPoints = FixRectangle(tempPoints) RemoveDeadGroups(selectedNewtons) NewGroup(selectedNewtons) CancelDraw() softEnabled = true end end elseif button == 1 then drawStarted = false end end end end function widget:KeyPress(key, mods) if key == KEYSYMS.S and mods["meta"] == true then RemoveOrders() elseif key == KEYSYMS.SPACE then allowDraw = true return true end end --function widget:MouseRelease(g_g_mx, g_my, button) -- if drawStarted then -- _, pos = spTraceScreenRay(g_mx,g_my, true) -- springPoints[100].x2 = pos[1] -- springPoints[100].y2 = pos[3] -- springPoints[100] = FixRectangle(springPoints[100]) -- end --end function widget:KeyRelease(key) if key == KEYSYMS.SPACE then allowDraw = false drawStarted = false CancelDraw() --ech("disallow draw") return true end end function filterNewtons ( unfilteredUnitIDs) filteredUnitIDs = {} i = 1 for nr, newton in pairs(unfilteredUnitIDs) do if (newtonUnitDefID == spGetUnitDefID(newton)) then filteredUnitIDs[i] = newton i=i+1 end end if filteredUnitIDs[1] == nil then return nil else return filteredUnitIDs end end function UpdateRectangle() g_mx, g_my = spGetMouseState() _, pos = spTraceScreenRay(g_mx,g_my, true) if pos then tempPoints.x2 = pos[1] tempPoints.y2 = pos[3] tempPoints = FixRectangle(tempPoints) end end function widget:Update(n) if drawStarted then UpdateRectangle() end end function widget:UnitDestroyed(unitID) if newtonIDs[unitID] ~= nil then RemoveDeadGroups({unitID}) end end function widget:UnitDamaged(unitID, unitDefID, unitTeam,damage, paralyzer , weaponDefID, attackerID, attackerDefID, attackerTeam ) if victim == unitID then victimStillBeingAttacked = true --ech("still being attacked") end end function widget:GameFrame(n) --if n % 30 == 0 then -- ech("cmdRate A=".. cmdRate .. " cmdRate S=" .. cmdRateS .. " SUM=" .. cmdRate + cmdRateS) -- cmdRate = 0 -- cmdRateS= 0 --end if softEnabled then if n % checkRate == 0 then if softEnabled ~= false then for tmpNewtonGroupID, tmpPoints in pairs (springPoints) do if tmpPoints ~= nil then units = spGetUnitsInRectangle(tmpPoints.x, tmpPoints.y, tmpPoints.x2, tmpPoints.y2) stop = true for i, unit in ipairs(units) do if UnitDefs[spGetUnitDefID(unit)].speed > 0 then stop = false if victimStillBeingAttacked then victimStillBeingAttacked = false break end --if (#cmdQueue>0) then --ech("attack " .. CMD.ATTACK) --ech("options " .. cmdQueue[1].options["coded"]) --ech("params ") --ech( cmdQueue[1].params) --if cmdQueue[1].id == 10 then -- --ech"breaking" -- break --end --end spGiveOrderToUnitArray(newtonGroupIDs[tmpNewtonGroupID], CMD.ATTACK, {unit}, {} ) victim = unit --cmdRate = cmdRate +1 break end end _, val = next(newtonGroupIDs[tmpNewtonGroupID]) if stop and spGetUnitCommands(val)[1] ~= nil then --elseif spGetUnitCommands(newtonGroupIDs[tmpNewtonGroupID][1])[1]~=nil then --badID[ example 2] spGiveOrderToUnitArray(newtonGroupIDs[tmpNewtonGroupID],CMD.STOP, {}, {}) victim = nil --cmdRateS = cmdRateS +1 --ech("stop") end end end end end end end local function DrawRectangleLine(rect) glVertex(rect.x,rect.z_xy, rect.y) glVertex(rect.x2,rect.z_x2y, rect.y) glVertex(rect.x2,rect.z_x2y2, rect.y2) glVertex(rect.x,rect.z_xy2, rect.y2) glVertex(rect.x,rect.z_xy, rect.y) end local function DrawTriangle(triangle) glVertex(triangle[2], triangle[1], triangle[3]) glVertex(triangle[4], triangle[1], triangle[5]) glVertex(triangle[6], triangle[1], triangle[7]) glVertex(triangle[2], triangle[1], triangle[3]) end function widget:DrawWorld() glLineWidth(2.0) glColor(1, 1, 0) if drawStarted then glBeginEnd(GL_LINE_STRIP, DrawRectangleLine, tempPoints) end for group, points in pairs (springPoints) do glColor(newtonGroupColours[group][1],newtonGroupColours[group][2],newtonGroupColours[group][3]) -- for some odd reason I see only 6 colors.. --ech(newtonGroupColours[group]) glBeginEnd(GL_LINE_STRIP, DrawRectangleLine, points) for _, unitID in pairs (newtonGroupIDs[group]) do glBeginEnd(GL_LINE_STRIP,DrawTriangle, newtonTrianglePoints[unitID]) end end --for unitID, triangle in pairs ( newtonTrianglePoints) do -- glBeginEnd(GL_LINE_STRIP, DrawTriangle, triangle) --end end
|
|
|