diff --git a/luaui/Widgets/gui_attackrange_gl4.lua b/luaui/Widgets/gui_attackrange_gl4.lua index c89ddf271f..c85a65a420 100644 --- a/luaui/Widgets/gui_attackrange_gl4.lua +++ b/luaui/Widgets/gui_attackrange_gl4.lua @@ -19,8 +19,8 @@ end -- Bindable action: cursor_range_toggle -- The widget's individual unit type's display setup is saved in LuaUI/config/AttackRangeConfig2.lua --------------------------------------------------------------------------------------------------------------------------- -local shift_only = false -- only show ranges when shift is held down -local cursor_unit_range = true -- displays the range of the unit at the mouse cursor (if there is one) +local shift_only = false -- only show ranges when shift is held down +local cursor_unit_range = true -- displays the range of the unit at the mouse cursor (if there is one) local selectionDisableThreshold = 90 -- turns off when selection is above this number local selectionDisableThresholdMult = 0.7 @@ -33,16 +33,14 @@ local buttonConfig = { } local colorConfig = { - drawStencil = true, -- wether to draw the outer, merged rings (quite expensive!) - cannon_separate_stencil = false, -- set to true to have cannon and ground be on different stencil mask - drawInnerRings = true, -- wether to draw inner, per attack rings (very cheap) + drawStencil = true, -- whether to draw the outer, merged rings (quite expensive!) + cannon_separate_stencil = false, -- set to true to have cannon and ground be on different stencil mask + drawInnerRings = true, -- whether to draw inner, per attack rings (very cheap) externalalpha = 0.80, -- alpha of outer rings internalalpha = 0.20, -- alpha of inner rings - fill_alpha = 0.10, -- this is the solid color in the middle of the stencil + fill_alpha = 0.10, -- this is the solid color in the middle of the stencil outer_fade_height_difference = 2500, -- this is the height difference at which the outer ring starts to fade out compared to inner rings - --distanceScaleStart = 1500, -- Linewidth is 100% up to this camera height - --distanceScaleEnd = 5000, -- Linewidth becomes 50% above this camera height ground = { color = { 1.0, 0.22, 0.05, 0.60 }, fadeparams = { 1500, 2200, 1.0, 0.0 }, -- FadeStart, FadeEnd, StartAlpha, EndAlpha @@ -75,19 +73,9 @@ local colorConfig = { ---------------------------------- local show_selected_weapon_ranges = true -local buttonconfigmap = { 'ground', 'nano', 'AA', 'cannon' } -local DEBUG = false --generates debug message local weaponTypeMap = { 'ground', 'nano', 'AA', 'cannon' } -local unitDefRings = {} --each entry should be a unitdefIDkey to very specific table: --- a list of tables, ideally ranged from 0 where - -local mobileAntiUnitDefs = { - [UnitDefNames.armscab.id] = true, - [UnitDefNames.armcarry.id] = true, - [UnitDefNames.cormabm.id] = true, - [UnitDefNames.corcarry.id] = true, -} +local unitDefRings = {} --each entry should be a unitdefIDkey to a table: local vtoldamagetag = Game.armorTypes['vtol'] local defaultdamagetag = Game.armorTypes['default'] @@ -172,28 +160,6 @@ local function tableToString(t) return "{" .. result:sub(1, -3) .. "}" end -local function dumpToFile(obj, prefix, filename) - local file = assert(io.open(filename, "w")) - if type(obj) == "table" then - for k, v in pairs(obj) do - local key = prefix and (prefix .. "." .. tostring(k)) or tostring(k) - if type(v) == "function" then - local info = debug.getinfo(v, "S") - file:write(key .. " (function) defined in " .. info.source .. " at line " .. info.linedefined .. "\n") - elseif type(v) == "table" then - file:write(key .. " (table):\n") - dumpToFile(v, key, filename) - else - file:write(key .. " = " .. tostring(v) .. "\n") - end - end - end - if type(obj) == "string" then - file:write(obj) - end - - file:close() -end local function convertToBitmap(statusTable) if type(statusTable) ~= "table" then @@ -242,7 +208,6 @@ local function initializeUnitDefRing(unitDefID) local range = weaponDef.range local dps = 0 local weaponType = unitDefRings[unitDefID]['weapons'][weaponNum] - --Spring.Echo(weaponType) if weaponType ~= nil and weaponType > 0 then local damage = 0 if weaponType == 3 then --AA @@ -256,15 +221,9 @@ local function initializeUnitDefRing(unitDefID) local isCylinder = weaponDef.cylinderTargeting and 1 or 0 local isDgun = (weaponDef.type == "DGun") and 1 or 0 - if isDgun == 1 then - --Spring.Echo("dgun found, range in weaponDef is: "..weaponDef.range) - end - - local customParams = weaponDef.customParams local wName = weaponDef.name if (weaponDef.type == "AircraftBomb") or (wName:find("bogus")) then - --Spring.Echo("bogus weapon found: "..tostring(wName)) range = 0 end --Spring.Echo("weaponNum: ".. weaponNum ..", name: " .. tableToString(weaponDef.name)) @@ -280,7 +239,6 @@ local function initializeUnitDefRing(unitDefID) isDgun } unitDefRings[unitDefID]['rings'][weaponNum] = ringParams - --Spring.Echo("Added ringParams: "..tableToString(ringParams)) end end @@ -302,7 +260,6 @@ local function initializeUnitDefRing(unitDefID) 0 } unitDefRings[unitDefID]['rings'][weaponCount + 1] = ringParams -- weaponCount + 1 is nano - --Spring.Echo("added builder! "..tableToString(unitDefRings[unitDef.id])) end end @@ -312,38 +269,6 @@ local function initUnitList() for unitDefID, _ in pairs(unitDefRings) do initializeUnitDefRing(unitDefID) end - -- Initialize Colors too - --[[ local scavlist = {} - for k,_ in pairs(unitDefRings) do - scavlist[k] = true - end - -- add scavs - for k,_ in pairs(scavlist) do - --Spring.Echo(k, UnitDefs[k].name) - if UnitDefNames[UnitDefs[k].name .. '_scav'] then - unitDefRings[UnitDefNames[UnitDefs[k].name .. '_scav'].id] = unitDefRings[k] - end - end - - local scavlist = {} - for k,_ in pairs(mobileAntiUnitDefs) do - scavlist[k] = true - end - for k,v in pairs(scavlist) do - mobileAntiUnitDefs[UnitDefNames[UnitDefs[k].name .. '_scav'].id] = mobileAntiUnitDefs[k] - end - - -- Initialize featureDefIDtoUnitDefID - local wreckheaps = {"_dead","_heap"} - for unitDefID,_ in pairs(unitDefRings) do - local unitDefName = UnitDefs[unitDefID].name - for i, suffix in pairs(wreckheaps) do - if FeatureDefNames[unitDefName..suffix] then - featureDefIDtoUnitDefID[FeatureDefNames[unitDefName..suffix].id] = unitDefID - --Spring.Echo(FeatureDefNames[unitDefName..suffix].id, unitDefID) - end - end - end ]] end --Button display configuration @@ -368,13 +293,12 @@ local GL_LEQUAL = GL.LEQUAL local GL_LINE_LOOP = GL.LINE_LOOP local GL_NOTEQUAL = GL.NOTEQUAL -local GL_KEEP = 0x1E00 --GL.KEEP +local GL_KEEP = 0x1E00 --GL.KEEP local GL_REPLACE = GL.REPLACE --GL.KEEP local spGetUnitDefID = Spring.GetUnitDefID local spGetUnitPosition = Spring.GetUnitPosition local spGetUnitAllyTeam = Spring.GetUnitAllyTeam -local spFindUnitCmdDesc = Spring.FindUnitCmdDesc local spGetMouseState = Spring.GetMouseState local spTraceScreenRay = Spring.TraceScreenRay local GetModKeyState = Spring.GetModKeyState @@ -456,10 +380,10 @@ local function makeCircleVBO(circleSegments) local VBOData = {} - for i = 0, circleSegments do -- this is +1 + for i = 0, circleSegments do -- this is +1 VBOData[#VBOData + 1] = math.sin(math.pi * 2 * i / circleSegments) -- X VBOData[#VBOData + 1] = math.cos(math.pi * 2 * i / circleSegments) -- Y - VBOData[#VBOData + 1] = i / circleSegments -- circumference [0-1] + VBOData[#VBOData + 1] = i / circleSegments -- circumference [0-1] VBOData[#VBOData + 1] = 0 end @@ -810,12 +734,10 @@ local function AddSelectedUnit(unitID, mouseover) if collections[unitID] ~= nil then return end --- if unittype is toggled off we don't proceed at all - local unitName = unitName[unitDefID] + local currentUnitName = unitName[unitDefID] local alliedUnit = (spGetUnitAllyTeam(unitID) == myAllyTeam) local allystring = alliedUnit and "ally" or "enemy" - --local alliedUnit = (spGetUnitAllyTeam(unitID) == myAllyTeam) - --local x, y, z, mpx, mpy, mpz, apx, apy, apz = spGetUnitPosition(unitID, true, true) local weapons = unitWeapons[unitDefID] if (not weapons or #weapons == 0) and not unitBuilder[unitDefID] then return end -- no weapons and not builder, nothing to add -- we want to add to unitDefRings here if it doesn't exist @@ -825,21 +747,11 @@ local function AddSelectedUnit(unitID, mouseover) for weaponNum = 1, #weapons do local weaponDefID = weapons[weaponNum].weaponDef local weaponDef = WeaponDefs[weaponDefID] - local range = weaponDef.range local weapon = weapons[weaponNum] - -- debug shit - --[[ local weaponTable = {} - for name,param in pairs(weapon.onlyTargets) do--:pairs() do - weaponTable[name]=param - end ]] - --Spring.Echo("----------weaponDef: "..tableToString(weaponTable)) if true then --range > 0 then -- trying something different - --if weaponDef.description:find("g2a") and not weaponDef.description:find("g2g") then if weapon.onlyTargets and weapon.onlyTargets.vtol then - --Spring.Echo("AA? " .. weaponDef.name..": "..tostring(weaponDef.description)) entry.weapons[weaponNum] = 3 -- weaponTypeMap[3] is "AA" - --Spring.Echo("added AA weapon: ".. weaponDef.name) elseif weaponDef.type == "Cannon" then entry.weapons[weaponNum] = 4 -- weaponTypeMap[4] is "cannon" else @@ -854,7 +766,6 @@ local function AddSelectedUnit(unitID, mouseover) end unitDefRings[unitDefID] = entry -- we insert the entry so we can reuse existing code - --Spring.Echo("unitDefRings entry added: "..tableToString(entry)) -- we need to initialize the other params initializeUnitDefRing(unitDefID) end @@ -864,39 +775,38 @@ local function AddSelectedUnit(unitID, mouseover) --for weaponNum = 1, #weapons do local addedRings = 0 - local weapons = unitDefRings[unitDefID]['weapons'] - for j, weaponType in pairs(weapons) do + local weaponTypes = unitDefRings[unitDefID]['weapons'] + for j, weaponType in pairs(weaponTypes) do local drawIt = true -- we need to check if the unit has on/off weapon states, and only add the one active local weaponOnOff - -- on off can be set on a building, we need to check that --spFindUnitCmdDesc(unitID, 85) ~= nil -- if this unit can toggle weapons + -- on off can be set on a building, we need to check that if unitOnOffable[unitDefID] and not unitOnOffName[unitDefID] then -- if it's a building with actual on/off, we display range if it's on weaponOnOff = unitsOnOff[unitID] or 1 drawIt = (weaponOnOff == 1) - elseif unitOnOffable[unitDefID] and unitOnOffName[unitDefID] then -- this is a unit or building with 2 weapons + elseif unitOnOffable[unitDefID] and unitOnOffName[unitDefID] then -- this is a unit or building with 2 weapons weaponOnOff = unitsOnOff[unitID] or 0 drawIt = ((weaponOnOff + 1) == j) or - #weapons == 1 -- remember weaponOnOff is 0 or 1, weapon number starts from 1 + #weaponTypes == 1 -- remember weaponOnOff is 0 or 1, weapon number starts from 1 end -- we add checks here for the display toggle status from config - if unitToggles[unitName] then -- only if there's a config, else default is to draw it - local wToggleStatuses = unitToggles[unitName][allystring] + if unitToggles[currentUnitName] then -- only if there's a config, else default is to draw it + local wToggleStatuses = unitToggles[currentUnitName][allystring] if type(wToggleStatuses) == 'table' then drawIt = wToggleStatuses[j] and drawIt else -- fixing the unitToggles table since something was corrupted local entry = {} - for i=1, #weapons do + for i=1, #weaponTypes do entry[i] = true end - unitToggles[unitName][allystring] = entry + unitToggles[currentUnitName][allystring] = entry end end local ringParams = unitDefRings[unitDefID]['rings'][j] if drawIt and ringParams[1] > 0 then - --local weaponType = unitDefRings[unitDefID]['weapons'][weaponNum] cacheTable[1] = mpx cacheTable[2] = mpy cacheTable[3] = mpz @@ -919,7 +829,6 @@ local function AddSelectedUnit(unitID, mouseover) local instanceID = 10000000 * (mouseover and 1 or 0) + 1000000 * weaponType + unitID + 100000 * j -- weapon index needs to be included here for uniqueness - --Spring.Echo("instanceID created: "..tostring(instanceID)) pushElementInstance(attackRangeVAOs[vaokey], cacheTable, instanceID, true, false, unitID) addedRings = addedRings + 1 if collections[unitID] == nil then @@ -936,7 +845,6 @@ local function AddSelectedUnit(unitID, mouseover) collections[unitID].vaokeys[instanceID] = vaokey end end - --Spring.Echo("Rings added: " ..tostring(addedRings)) -- we cheat here and update builder count if unitBuilder[unitDefID] and addedRings > 0 then selBuilderCount = selBuilderCount + 1 @@ -957,19 +865,16 @@ local function RemoveSelectedUnit(unitID, mouseover) popElementInstance(attackRangeVAOs[vaoKey], instanceKey) removedRings = removedRings + 1 end - --Spring.Echo("Rings removed: "..tostring(removedRings)) -- before we get rid of the definition we cheat again if unitBuilder[collections[unitID].unitDefID] then selBuilderCount = selBuilderCount - 1 end collections[unitID] = nil - --Spring.Echo("removed rings from unitID: "..tostring(unitID)) end end function widget:SelectionChanged(sel) updateSelection = true - --Spring.Echo("selection changed!") end local function InitializeBuilders() @@ -989,7 +894,6 @@ local function makeShaders() { vertex = vsSrc:gsub("//__DEFINES__", "#define MYGRAVITY " .. tostring(Game.gravity + 0.1)), fragment = fsSrc, - --geometry = gsSrc, no geom shader for now uniformInt = { heightmapTex = 0, losTex = 1, @@ -1001,7 +905,7 @@ local function makeShaders() }, "attackRangeShader GL4" ) - shaderCompiled = attackRangeShader:Initialize() + local shaderCompiled = attackRangeShader:Initialize() if not shaderCompiled then goodbye("Failed to compile attackRangeShader GL4 ") return false @@ -1035,7 +939,12 @@ local function initGL4() return makeShaders() end -function ToggleCursorRange(_, _, args) +local function toggleShowSelectedRanges(on) + if show_selected_weapon_ranges == on then return end + show_selected_weapon_ranges = on +end + +local function toggleCursorRange(_, _, args) cursor_unit_range = not cursor_unit_range Spring.Echo("Cursor unit range set to: " .. (cursor_unit_range and "ON" or "OFF")) end @@ -1056,17 +965,15 @@ function widget:Initialize() unitToggles[i] = v end - widgetHandler:AddAction("cursor_range_toggle", ToggleCursorRange, nil, "p") + widgetHandler:AddAction("cursor_range_toggle", toggleCursorRange, nil, "p") myAllyTeam = Spring.GetMyAllyTeamID() local allyteamlist = Spring.GetAllyTeamList() - --Spring.Echo("# of allyteams = ", #allyteamlist) - numallyteams = #allyteamlist updateSelection = true local _, _, _, shift = GetModKeyState() if shift_only and not shift then - Toggle(false) + toggleShowSelectedRanges(false) end InitializeBuilders() @@ -1103,16 +1010,6 @@ function widget:GameFrame(gf) gameFrame = gf end -local function GetModKeys() - local alt, ctrl, meta, shift = GetModKeyState() - - if GetInvertQueueKey() then -- Shift inversion - shift = not shift - end - - return alt, ctrl, meta, shift -end - local function RefreshSelectedUnits() local newSelUnits = {} for i, unitID in ipairs(selectedUnits) do @@ -1133,20 +1030,12 @@ local function UpdateSelectedUnits() selectedUnits = GetSelectedUnits() selUnitCount = #selectedUnits updateSelection = false - --[[ if innerRingDim ~= 0 then - local numUnitsSelected = #selectedUnits - if numUnitsSelected == 0 then numUnitsSelected = 1 end - if numUnitsSelected > 25 then numUnitsSelected = 25 end - innerRingDim = group_selection_fade_scale * 0.1 * numUnitsSelected - end ]] RefreshSelectedUnits() - --Spring.Echo("selUnitCount: ".. tostring(selUnitCount)..", selBuilderCount: ".. tostring(selBuilderCount)) end -- whether to draw the build range of all builders - only happens when isBuilding local function DrawBuilders() - --Spring.Echo("drawing all builders..."..tostring(isBuilding)) if isBuilding then for unitID, _ in pairs(builders) do if not selUnits[unitID] then @@ -1164,27 +1053,6 @@ end -- refresh all display according to toggle status local function RefreshEverything() - --[[ local units = Spring.GetAllUnits() - - for i = 1, #units do - local unitID = units[i] - local unitDef = UnitDefs[spGetUnitDefID(unitID)] - - if unitDef and unitDef.maxWeaponRange > 0 then -- there actually is a range to display - local alliedUnit = (spGetUnitAllyTeam(unitID) == myAllyTeam) - local allystring = alliedUnit and "ally" or "enemy" - local name = unitDef.name - -- if it's toggled off we remove it here - if unitToggles[unitName] and unitToggles[unitName][allystring] == 0 then - RemoveSelectedUnit(unitID) - else -- if it isn't toggled off and it's selected, we need to add it - if selections[unitID] then - AddSelectedUnit(unitID) - end - end - end - end ]] - -- what about just reinitialize? attackRangeVAOs = {} selections = {} @@ -1218,7 +1086,7 @@ local function CycleUnitDisplay(direction) unitToggles[name] = unitToggles[name] or {} if not unitToggles[name][allystring] then -- default toggle is on, we set it to off (0) for i = 1, #unitDefRings[unitDefID].weapons do - wToggleStatuses[i] = true -- every ring defined weapon is on by default + wToggleStatuses[i] = true -- every ring defined weapon is on by default end newToggleStatuses = getNextWeaponCombination(wToggleStatuses, direction) unitToggles[name][allystring] = newToggleStatuses @@ -1253,13 +1121,6 @@ local function CycleUnitDisplay(direction) RefreshEverything() end -function Toggle(on) - if show_selected_weapon_ranges == on then return end - --Spring.Echo("toggled ".. tostring(on)) - show_selected_weapon_ranges = on - --UpdateSelectedUnits() -end - function widget:KeyPress(key, mods, isRepeat) if key == 304 then shifted = true @@ -1309,10 +1170,10 @@ function widget:Update(dt) local cmdIndex, cmdID, cmdType, cmdName = GetActiveCommand() if shift_only then if shifted then - Toggle(true) + toggleShowSelectedRanges(true) else - if cmdID == 20 then Toggle(true) end - if not cmdID or cmdID ~= 20 then Toggle(false) end + if cmdID == 20 then toggleShowSelectedRanges(true) end + if not cmdID or cmdID ~= 20 then toggleShowSelectedRanges(false) end end end local isBuildingNow = (cmdID ~= nil) and (cmdID < 0) -- we're building, need to draw builder ranges @@ -1356,12 +1217,12 @@ local function DRAWRINGS(primitiveType, linethickness) local iT = attackRangeVAOs[atkRangeClass] stencilMask = 2 ^ (4 * (i - 1) + (j - 1)) -- from 1 to 128 drawcounts[stencilMask] = iT.usedElements - if iT.usedElements > 0 then --and buttonConfig[allyState][wt] then + if iT.usedElements > 0 then --and buttonConfig[allyState][wt] then if linethickness then glLineWidth(colorConfig[wt][linethickness] * cameraHeightFactor) end - glStencilMask(stencilMask) -- only allow these bits to get written - glStencilFunc(GL_NOTEQUAL, stencilMask, stencilMask) -- what to do with the stencil + glStencilMask(stencilMask) -- only allow these bits to get written + glStencilFunc(GL_NOTEQUAL, stencilMask, stencilMask) -- what to do with the stencil iT.VAO:DrawArrays(primitiveType, iT.numVertices, 0, iT.usedElements, 0) -- +1!!! end end @@ -1374,7 +1235,7 @@ local function DRAWRINGS(primitiveType, linethickness) local stencilOffset = colorConfig.cannon_separate_stencil and 3 or 0 stencilMask = 2 ^ (4 * (i - 1) + stencilOffset) -- if 0 then it's on the same as "ground" drawcounts[stencilMask] = iT.usedElements - if iT.usedElements > 0 then --and buttonConfig[allyState]["ground"] then + if iT.usedElements > 0 then if linethickness then glLineWidth(colorConfig['cannon'][linethickness] * cameraHeightFactor) end @@ -1391,14 +1252,12 @@ function widget:DrawWorldPreUnit() cameraHeightFactor = GetCameraHeightFactor() * 0.5 + 0.5 glTexture(0, "$heightmap") glTexture(1, "$info") - --glTexture(2, texture) -- Stencil Setup -- -- https://learnopengl.com/Advanced-OpenGL/Stencil-testing if colorConfig.drawStencil then glClear(GL_STENCIL_BUFFER_BIT) -- clear prev stencil glDepthTest(false) -- always draw - --glColorMask(false, false, false, false) -- disable color drawing glStencilTest(true) -- enable stencil test glStencilMask(255) -- all 8 bits glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE) -- Set The Stencil Buffer To 1 Where Draw Any Polygon @@ -1432,14 +1291,8 @@ function widget:DrawWorldPreUnit() end if colorConfig.drawInnerRings then - --[[ local drawDim = 1 - if innerRingDim ~= 0 then - drawDim = colorConfig.internalalpha / innerRingDim - if drawDim > 1.0 then drawDim = 1.0 end - end ]] attackRangeShader:SetUniform("lineAlphaUniform", colorConfig.internalalpha) attackRangeShader:SetUniform("drawMode", 2.0) - --attackRangeShader:SetUniform("drawAlpha", 1.0) attackRangeShader:SetUniform("fadeDistOffset", 0) DRAWRINGS(GL_LINE_LOOP, 'internallinethickness') -- DRAW THE INNER RINGS end @@ -1449,11 +1302,6 @@ function widget:DrawWorldPreUnit() glTexture(0, false) glTexture(1, false) glDepthTest(false) - if false and Spring.GetDrawFrame() % 60 == 0 then - local s = 'drawcounts: ' - for k, v in pairs(drawcounts) do s = s .. " " .. tostring(k) .. ":" .. tostring(v) end - Spring.Echo(s) - end end end