New widget: Enemy Spotter

New widget: Enemy Spotter

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

Moderator: Moderators

Post Reply
User avatar
TradeMark
Posts: 4867
Joined: 17 Feb 2006, 15:58

New widget: Enemy Spotter

Post by TradeMark »

Image

shows enemy units with a blue smoothed transparent layer under them, nice for example spotting enemy air units easily.

Edit: more examples of usage:

strong edges settings:
Image

Code: Select all

local circleDivs = 16 -- how precise circle? octagon by default
local innersize = 1.5 -- circle scale compared to unit radius
local outersize = 0.9 -- outer fade size compared to circle scale (1 = no outer fade)

local fadefrom = { 0, 0, 1, 0 } -- inner color
local colorSet = { 0, 0, 1, 0.23 } -- middle color
local fadeto = { 0, 0, 1, 0.4 } -- outer color
short outer fade settings:
Image

Code: Select all

local circleDivs = 16 -- how precise circle? octagon by default
local innersize = 1.5 -- circle scale compared to unit radius
local outersize = 1.1 -- outer fade size compared to circle scale (1 = no outer fade)
long outer fade, settings (least polygons used):
Image

Code: Select all

local circleDivs = 8 -- how precise circle? octagon by default
local innersize = 1.5 -- circle scale compared to unit radius
local outersize = 1.75 -- outer fade size compared to circle scale (1 = no outer fade)




Widget code:

Code: Select all

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    gui_enemy_spotter.lua
--  brief:   Draws blue smoothed octagon under enemy units
--  author:  Dave Rodgers (orig. TeamPlatter edited by TradeMark)
--
--  Copyright (C) 2007.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:GetInfo()
	return {
		name      = "EnemySpotter",
		desc      = "Draws blue smoothed octagon under enemy units",
		author    = "TradeMark",
		date      = "03.12.2009",
		license   = "GNU GPL, v2 or later",
		layer     = 5,
		enabled   = true  --  loaded by default?
	}
end

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

-- Automatically generated local definitions

local GL_LINE_LOOP           = GL.LINE_LOOP
local GL_TRIANGLE_FAN        = GL.TRIANGLE_FAN
local glBeginEnd             = gl.BeginEnd
local glColor                = gl.Color
local glCreateList           = gl.CreateList
local glDeleteList           = gl.DeleteList
local glDepthTest            = gl.DepthTest
local glDrawListAtUnit       = gl.DrawListAtUnit
local glLineWidth            = gl.LineWidth
local glPolygonOffset        = gl.PolygonOffset
local glVertex               = gl.Vertex
local spDiffTimers           = Spring.DiffTimers
local spGetAllUnits          = Spring.GetAllUnits
local spGetGroundNormal      = Spring.GetGroundNormal
local spGetSelectedUnits     = Spring.GetSelectedUnits
local spGetTeamColor         = Spring.GetTeamColor
local spGetTimer             = Spring.GetTimer
local spGetUnitBasePosition  = Spring.GetUnitBasePosition
local spGetUnitDefDimensions = Spring.GetUnitDefDimensions
local spGetUnitDefID         = Spring.GetUnitDefID
local spGetUnitRadius        = Spring.GetUnitRadius
local spGetUnitTeam          = Spring.GetUnitTeam
local spGetUnitViewPosition  = Spring.GetUnitViewPosition
local spIsUnitSelected       = Spring.IsUnitSelected
local spIsUnitVisible        = Spring.IsUnitVisible
local spSendCommands         = Spring.SendCommands


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


local circlePolys = 0
local myTeamID = Spring.GetLocalTeamID()
local realRadii = {}

local circleDivs = 8 -- how precise circle? octagon by default
local innersize = 1.5 -- circle scale compared to unit radius
local outersize = 1.75 -- outer fade size compared to circle scale (1 = no outer fade)

local fadefrom = { 0, 0, 1, 0 } -- inner color
local colorSet = { 0, 0, 1, 0.23 } -- middle color
local fadeto = { 0, 0, 1, 0 } -- outer color


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


-- Creating polygons:
function widget:Initialize()
	circlePolys = glCreateList(function()
		-- inner:
		glBeginEnd(GL.TRIANGLES, function()
			local radstep = (2.0 * math.pi) / circleDivs
			for i = 1, circleDivs do
				local a1 = (i * radstep)
				local a2 = ((i+1) * radstep)
				glColor(fadefrom)
				glVertex(0, 0, 0)
				glColor(colorSet) 
				glVertex(math.sin(a1), 0, math.cos(a1))
				glVertex(math.sin(a2), 0, math.cos(a2))
			end
		end)
		if (outersize ~= 1) then 
			-- outer edge:
			glBeginEnd(GL.QUADS, function()
				local radstep = (2.0 * math.pi) / circleDivs
				for i = 1, circleDivs do
					local a1 = (i * radstep)
					local a2 = ((i+1) * radstep)
					glColor(colorSet)
					glVertex(math.sin(a1), 0, math.cos(a1))
					glVertex(math.sin(a2), 0, math.cos(a2))
					glColor(fadeto)
					glVertex(math.sin(a2)*outersize, 0, math.cos(a2)*outersize)
					glVertex(math.sin(a1)*outersize, 0, math.cos(a1)*outersize)
				end
			end)
		end
	end)
end

function widget:Shutdown()
	glDeleteList(circlePolys)
end


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


-- Retrieving radius:
local function GetUnitDefRealRadius(udid)
	local radius = realRadii[udid]
	if (radius) then return radius end
	local ud = UnitDefs[udid]
	if (ud == nil) then return nil end
	local dims = spGetUnitDefDimensions(udid)
	if (dims == nil) then return nil end
	local scale = ud.hitSphereScale -- missing in 0.76b1+
	scale = ((scale == nil) or (scale == 0.0)) and 1.0 or scale
	radius = dims.radius / scale
	realRadii[udid] = radius*innersize
	return radius
end


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


-- Drawing:
function widget:DrawWorldPreUnit()
	glDepthTest(true)
	glPolygonOffset(-100, -2)
	for _,unitID in ipairs(Spring.GetVisibleUnits()) do
		local teamID = spGetUnitTeam(unitID)
		if (teamID) then
			if ( not Spring.AreTeamsAllied(myTeamID, teamID) ) then
				local radius = GetUnitDefRealRadius(spGetUnitDefID(unitID))
				if (radius) then
					glDrawListAtUnit(unitID, circlePolys, false, radius, 1.0, radius)
				end
			end
		end
	end
end
              

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Last edited by TradeMark on 03 Dec 2009, 15:21, edited 4 times in total.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: New widget: Enemy Spotter

Post by smoth »

interesting.
Satirik
Lobby Developer
Posts: 1688
Joined: 16 Mar 2007, 18:27

Re: New widget: Enemy Spotter

Post by Satirik »

enemy = red ! NOOB
User avatar
TradeMark
Posts: 4867
Joined: 17 Feb 2006, 15:58

Re: New widget: Enemy Spotter

Post by TradeMark »

why does the color matter :D

as long as i can see blue better than red, i give a shit which color it is
Satirik
Lobby Developer
Posts: 1688
Joined: 16 Mar 2007, 18:27

Re: New widget: Enemy Spotter

Post by Satirik »

TradeMark wrote:why does the color matter :D

as long as i can see blue better than red, i give a shit which color it is
because blue is often use for ally or friendly things
User avatar
TradeMark
Posts: 4867
Joined: 17 Feb 2006, 15:58

Re: New widget: Enemy Spotter

Post by TradeMark »

well, you can change the colors easily if you want:
local fadefrom = { 0, 0, 1, 0 } -- inner color
local colorSet = { 0, 0, 1, 0.23 } -- middle color
local fadeto = { 0, 0, 1, 0 } -- outer color
i prefer blue tho.
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: New widget: Enemy Spotter

Post by Forboding Angel »

Code: Select all

local circleDivs = 8 -- how precise circle? octagon by default
local sizemulti = 1.75; -- how big octagon compared to unit radius
local fadefrom = { 1, 0, 0, 0 } -- inner color
local colorSet = { 1, 0, 0, 0.23 } -- middle color
local fadeto = { 1, 0, 0, 0 } -- outer color
:roll:

Edit: Damn... Ninja'd
Last edited by Forboding Angel on 02 Dec 2009, 22:37, edited 1 time in total.
Satirik
Lobby Developer
Posts: 1688
Joined: 16 Mar 2007, 18:27

Re: New widget: Enemy Spotter

Post by Satirik »

default should be red that's what i meant ... you know forb unlike you i do know how to change code to do what i want ...

You apparently don't know to be civil on these forums. - Moderator
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: New widget: Enemy Spotter

Post by hoijui »

i'd bet that in russia and china, blue is more often used for the enemy then red.
User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

Re: New widget: Enemy Spotter

Post by CarRepairer »

hoijui wrote:i'd bet that in russia and china, blue is more often used for the enemy then red.
In Soviet Russia, ally team color is red! :roll:
User avatar
Gota
Posts: 7151
Joined: 11 Jan 2008, 16:55

Re: New widget: Enemy Spotter

Post by Gota »

In soviet Russia team colors you
Satirik
Lobby Developer
Posts: 1688
Joined: 16 Mar 2007, 18:27

Re: New widget: Enemy Spotter

Post by Satirik »

Satirik wrote:default should be red that's what i meant ... you know forb unlike you i do know how to change code to do what i want ...

You apparently don't know to be civil on these forums. - Moderator
... moderator's name is missing ...
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: New widget: Enemy Spotter

Post by smoth »

I did it....


Very cute smoth, don't make me warn you for backseat moderation.
Satirik
Lobby Developer
Posts: 1688
Joined: 16 Mar 2007, 18:27

Re: New widget: Enemy Spotter

Post by Satirik »

isn't a word missing between "know" and "to" ... like "how" im not an english master but the sentence sounds weird
User avatar
JohannesH
Posts: 1793
Joined: 07 Apr 2009, 12:43

Re: New widget: Enemy Spotter

Post by JohannesH »

Red is enemy in this thread too?
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: New widget: Enemy Spotter

Post by Forboding Angel »

Satirik wrote:isn't a word missing between "know" and "to" ... like "how" im not an english master but the sentence sounds weird
No, it is grammatically correct, you just fail. The sentence states that apparently you do not understand that in a public forum you should remain civil to other people.
User avatar
TradeMark
Posts: 4867
Joined: 17 Feb 2006, 15:58

Re: New widget: Enemy Spotter

Post by TradeMark »

oh no, soon my thread gets locked again ;_;
User avatar
Tribulex
A.N.T.S. Developer
Posts: 1894
Joined: 26 Sep 2009, 21:26

Re: New widget: Enemy Spotter

Post by Tribulex »

smoth wrote:I did it....


Very cute smoth, don't make me warn you for backseat moderation.
Smoth do it again so you get a warning and we find out who is behind this COMMUNISM!!!
luckywaldo7
Posts: 1398
Joined: 17 Sep 2008, 04:36

Re: New widget: Enemy Spotter

Post by luckywaldo7 »

JohannesH wrote:Red is enemy in this thread too?
I would lol but I don't want a spanking.

(...sorry mods couldn't help myself...)
User avatar
Tribulex
A.N.T.S. Developer
Posts: 1894
Joined: 26 Sep 2009, 21:26

Re: New widget: Enemy Spotter

Post by Tribulex »

They wouldnt apologize to you....


Also this widget seems to have an fps issue in certain situations. Ill investigate it further and post a fix.
Post Reply

Return to “Lua Scripts”