Range Widget

Range Widget

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
manolo_
Posts: 1370
Joined: 01 Jul 2008, 00:08

Range Widget

Post by manolo_ »

hi,

i recognized that argh made a widget, that shows the range of the selected unit, but it didnt get it working with xta. so could somebody help me? i think it defines units for that the widgets works (e.g. arty)


Code: Select all

function widget:GetInfo()
  return {
    name      = "Range Widget",
    desc      = "Shows your units' weapon ranges, when selected.",
    author    = "Argh",
    date      = "January 21, 2009",
    license   = "Public Domain, or the least-restrictive rights of your country of residence",
    layer     = 0,
    enabled   = true -- loaded by default?
  }
end

local RangeList = {}
local unitIDList
local id
local unitID
local Team = Spring.GetLocalTeamID()
local myRange = 0

local GetUnitDefID = Spring.GetUnitDefID
local glColor = gl.Color
local glDrawGroundCircle = gl.DrawGroundCircle
local glLineWidth = gl.LineWidth
local GetUnitPosition = Spring.GetUnitPosition
local GetUnitTeam = Spring.GetUnitTeam
local GetSelectedUnits = Spring.GetSelectedUnits
local GetUnitWeaponState = Spring.GetUnitWeaponState

function widget:Initialize()
	for ud,_ in pairs(UnitDefs) do
		if UnitDefs[ud].customParams.show_range_one == 'yes' then
			table.insert(RangeList,ud,{range_one = 0, colorR_one = tonumber(UnitDefs[ud].customParams.color_r_one), colorG_one = tonumber(UnitDefs[ud].customParams.color_g_one), colorB_one = tonumber(UnitDefs[ud].customParams.color_b_one)})
		end
		if UnitDefs[ud].customParams.show_range_two == 'yes' then
			table.insert(RangeList,ud,{range_two = 0, colorR_two = tonumber(UnitDefs[ud].customParams.color_r_two), colorG_two = tonumber(UnitDefs[ud].customParams.color_g_two), colorB_two = tonumber(UnitDefs[ud].customParams.color_b_two)})
		end
		if UnitDefs[ud].customParams.show_range_three == 'yes' then
			table.insert(RangeList,ud,{range_three = 0, colorR_three = tonumber(UnitDefs[ud].customParams.color_r_three), colorG_three = tonumber(UnitDefs[ud].customParams.color_g_three), colorB_three = tonumber(UnitDefs[ud].customParams.color_b_three)})
		end
	end
end

local stipple = (65520)
local startTime = Spring.GetTimer()
local glLineStipple = gl.LineStipple
local floor = math.floor
local DiffTimers = Spring.DiffTimers
local GetTimer = Spring.GetTimer

function widget:DrawWorldPreUnit()
	unitIDList = GetSelectedUnits()
	if unitIDList[1] ~= nil then

		local myTime = DiffTimers(GetTimer(), startTime)
		local timeShift = floor(myTime * 16 % 32)
		glLineStipple(1,stipple,-timeShift)

		for _,unitID in ipairs(unitIDList) do
			if Team == GetUnitTeam(unitID) then
				id = GetUnitDefID(unitID)
				if RangeList[id] then					
					x,y,z = GetUnitPosition(unitID)
					glLineWidth(2.5)
					if RangeList[id].range_one ~= nil then
						myRange = GetUnitWeaponState(unitID, 0, "range") 
						glColor(RangeList[id].colorR_one,RangeList[id].colorG_one,RangeList[id].colorB_one,0.35)
						glDrawGroundCircle(x,y,z,myRange,128)
					end
					if RangeList[id].range_two ~= nil then
						myRange = GetUnitWeaponState(unitID, 1, "range") 
						glColor(RangeList[id].colorR_two,RangeList[id].colorG_two,RangeList[id].colorB_two,0.35)
						glDrawGroundCircle(x,y,z,myRange,128)
					end
					if RangeList[id].range_three ~= nil then
						myRange = GetUnitWeaponState(unitID, 2, "range") 
						glColor(RangeList[id].colorR_three,RangeList[id].colorG_three,RangeList[id].colorB_three,0.35)
						glDrawGroundCircle(x,y,z,myRange,128)
					end
				end
			end
		end
		
		glLineStipple(false)
	
	end
end
User avatar
Niobium
Posts: 456
Joined: 07 Dec 2008, 02:35

Re: Range Widget

Post by Niobium »

I run something like what you are talking about. It shows the ranges of all weapons for all selected units. Works for every mod.

Code: Select all

--------------------------------------------------------------------------------

function widget:GetInfo()
	return {
		name      = "Unit Ranges",
		desc      = "Shows ranges of selected units",
		author    = "Niobium",
		date      = "Jan 14th, 2009",
		license   = "GNU GPL v2",
		layer     = 0,
		enabled   = true
	}
end

--------------------------------------------------------------------------------

local circleAlpha		= 0.35
local circleDivs		= 40 -- This is what spring uses

--------------------------------------------------------------------------------

local spGetSelUnitsSorted	= Spring.GetSelectedUnitsSorted
local spGetUnitViewPosition     = Spring.GetUnitViewPosition

local glColor				= gl.Color
local glLineWidth			= gl.LineWidth
local glDepthTest			= gl.DepthTest
local glDrawGroundCircle    = gl.DrawGroundCircle

--------------------------------------------------------------------------------

local uDefs			= UnitDefs
local wDefs			= WeaponDefs

local wepRanges		= {}
local buildRange	= {}

--------------------------------------------------------------------------------

function widget:Initialize()

	for uDefID, uDef in pairs(uDefs) do
		
		wepRanges[uDefID] = {}
		local weapons = uDef.weapons
		local entryIndex = 0
		for weaponIndex=1, #weapons do
			
			local weaponRange = wDefs[weapons[weaponIndex].weaponDef].range
			
			if (weaponRange > 16) then -- many 'fake' weapons have <= 16 range.
				entryIndex = entryIndex + 1
				wepRanges[uDefID][entryIndex] = weaponRange
			end
		end
	end
end

function widget:DrawWorld()

	-- OpenGL stuff
	glLineWidth(1.49)
	glDepthTest(true)
	
	-- Get selected units, sorted for efficiency
	local selUnits = spGetSelUnitsSorted()
	selUnits.n = nil -- So our loop works

	-- Set the color
	glColor(1.0, 0.3, 0.3, circleAlpha)
	
	-- Do the loop
	for uDefID, uIDs in pairs(selUnits) do
		
		local uWepRanges = wepRanges[uDefID]
		if uWepRanges then
			
			for i = 1, #uIDs do
				
				local ux, uy, uz = spGetUnitViewPosition(uIDs[i])
				
				for r = 1, #uWepRanges do
					glDrawGroundCircle(ux, uy, uz, uWepRanges[r], circleDivs)
				end
			end
		end
	end
end

--------------------------------------------------------------------------------
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Range Widget

Post by knorke »

i think it defines units for that the widgets works (e.g. arty)
I think the mod has to define what units will display range in the unit def files.
f UnitDefs[ud].customParams.show_range_one == 'yes'
[/quote]
needs a parameter show_range = yes and then some color variables too.
User avatar
manolo_
Posts: 1370
Joined: 01 Jul 2008, 00:08

Re: Range Widget

Post by manolo_ »

thx Niobium,

it works good, btw i asked several times that somebody will make it :D

edit: request: show the circle when i press meta-key?
User avatar
Niobium
Posts: 456
Joined: 07 Dec 2008, 02:35

Re: Range Widget

Post by Niobium »

manolo_ wrote:thx Niobium,

it works good, btw i asked several times that somebody will make it :D

edit: request: show the circle when i press meta-key?
Under 'function widget:DrawWorld()' add this line:

Code: Select all

if not select(3, Spring.GetModKeyState()) then return end
Post Reply

Return to “Lua Scripts”