Page 1 of 11

New widget: Enemy Spotter

Posted: 02 Dec 2009, 21:27
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
              

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

Re: New widget: Enemy Spotter

Posted: 02 Dec 2009, 21:43
by smoth
interesting.

Re: New widget: Enemy Spotter

Posted: 02 Dec 2009, 22:05
by Satirik
enemy = red ! NOOB

Re: New widget: Enemy Spotter

Posted: 02 Dec 2009, 22:09
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

Re: New widget: Enemy Spotter

Posted: 02 Dec 2009, 22:11
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

Re: New widget: Enemy Spotter

Posted: 02 Dec 2009, 22:12
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.

Re: New widget: Enemy Spotter

Posted: 02 Dec 2009, 22:13
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

Re: New widget: Enemy Spotter

Posted: 02 Dec 2009, 22:27
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

Re: New widget: Enemy Spotter

Posted: 02 Dec 2009, 22:28
by hoijui
i'd bet that in russia and china, blue is more often used for the enemy then red.

Re: New widget: Enemy Spotter

Posted: 02 Dec 2009, 22:34
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:

Re: New widget: Enemy Spotter

Posted: 03 Dec 2009, 00:00
by Gota
In soviet Russia team colors you

Re: New widget: Enemy Spotter

Posted: 03 Dec 2009, 00:23
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 ...

Re: New widget: Enemy Spotter

Posted: 03 Dec 2009, 00:29
by smoth
I did it....


Very cute smoth, don't make me warn you for backseat moderation.

Re: New widget: Enemy Spotter

Posted: 03 Dec 2009, 00:31
by Satirik
isn't a word missing between "know" and "to" ... like "how" im not an english master but the sentence sounds weird

Re: New widget: Enemy Spotter

Posted: 03 Dec 2009, 00:54
by JohannesH
Red is enemy in this thread too?

Re: New widget: Enemy Spotter

Posted: 03 Dec 2009, 01:19
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.

Re: New widget: Enemy Spotter

Posted: 03 Dec 2009, 01:21
by TradeMark
oh no, soon my thread gets locked again ;_;

Re: New widget: Enemy Spotter

Posted: 03 Dec 2009, 01:24
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!!!

Re: New widget: Enemy Spotter

Posted: 03 Dec 2009, 04:39
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...)

Re: New widget: Enemy Spotter

Posted: 03 Dec 2009, 05:39
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.