I tried my luck at this but the transformations won't work right, all vertices end up in the top left corner of the map.
Code: Select all
function gadget:GetInfo()
return {
name = "trails",
desc = "hopes to implement trails",
author = "KDR_11k (David Becker)",
date = "2008-02-22",
license = "Public Domain",
layer = 1,
enabled = true
}
end
if (gadgetHandler:IsSyncedCode()) then
--SYNCED
local function AddTrail(u,ud,team,piece,width,ttl,rate)
SendToUnsynced("Add",u,ud,team,piece,width,ttl,rate)
end
function gadget:UnitDestroyed(u,ud,team)
Spring.SendToUnsynced("Destroy",u)
end
function gadget:Initialize()
gadgetHandler:RegisterGlobal("AddTrail", AddTrail)
-- gadgetHandler:RegisterGlobal("RemoveTrail", RemoveTrail)
end
else
--UNSYNCED
local trailList = {}
local lastupdate = 0
local function AddTrail(u, ud, team, piece, width, ttl, rate)
local p = Spring.GetUnitScriptPiece(u, piece)
local t = {
width = width,
ttl = ttl,
rate = rate,
--texture = UnitDefs[ud].customParams.trailtex,
R = UnitDefs[ud].customParams.trailr,
G = UnitDefs[ud].customParams.trailg,
B = UnitDefs[ud].customParams.trailb,
A = UnitDefs[ud].customParams.trailalpha,
phase = 1,
maxPhase = math.ceil(ttl/rate),
}
if not trailList[u] then
trailList[u]={}
end
trailList[u][p] = t
end
local function RemoveTrail(u, ud, team, piece)
trailList[Spring.GetUnitScriptPiece(u, piece)]=nil
end
local function DrawTrail(vertexList, phase, maxPhase, unit, piece, width)
local current = phase
while (phase > 1 and current ~= phase - 1) or (phase == 1 and current ~= maxPhase) do
gl.Vertex(vertexList[current * 2 - 1][1],vertexList[current * 2 - 1][2],vertexList[current * 2 - 1][3])
gl.Vertex(vertexList[current * 2 - 0][1],vertexList[current * 2 - 0][2],vertexList[current * 2 - 0][3])
current = current + 1
if current > maxPhase then
current = 1
end
end
end
function gadget:DrawWorld()
gl.ResetMatrices()
if lastupdate < Spring.GetGameFrame() then
local f = Spring.GetGameFrame()
lastupdate = f
for u,ts in pairs(trailList) do
for p,t in pairs(ts) do
if f % t.rate < .1 and t.vertices then
gl.PushMatrix()
local x,y,z = Spring.GetUnitPosition(u)
gl.Translate(x,y,z)
gl.UnitMultMatrix(u)
gl.UnitPieceMultMatrix(u,p)
local pieceM1,pieceM2,pieceM3,pieceM4,pieceM5,_,_,pieceM8,pieceM9,_,_,pieceM12,pieceM13, pieceM14, pieceM15 = gl.GetMatrixData(GL.MODELVIEW)
gl.PopMatrix()
local width = t.width
local phase = t.phase
vertex0 = { width * .5 * pieceM1 + pieceM4, width * .5 * pieceM5 + pieceM8, width * .5 * pieceM9 + pieceM12}
vertex1 = { -width * .5 * pieceM1 + pieceM4, -width * .5 * pieceM5 + pieceM8, -width * .5 * pieceM9 + pieceM12}
t.vertices[phase * 2 - 1] = vertex0
t.vertices[phase * 2 - 0] = vertex1
t.phase = phase + 1
if phase >= t.maxPhase then
t.phase = 1
end
end
end
end
end
update = false
local _,_,_,_,_,ateam = Spring.GetTeamInfo(Spring.GetLocalTeamID())
local _,specView = Spring.GetSpectatingState()
--gl.DepthTest(GL.LEQUAL)
for u,ts in pairs(trailList) do
x,y,z = Spring.GetUnitPosition(u)
for p,t in pairs(ts) do
if specView or Spring.GetPositionLosState(x,y,z,ateam) then
if t.vertices then
gl.Color(t.R,t.G,t.B,t.A)
gl.BeginEnd(GL.TRIANGLE_STRIP,DrawTrail,t.vertices,t.phase,t.maxPhase,u,p,t.width)
else
local width = t.width
gl.PushMatrix()
local x,y,z = Spring.GetUnitPosition(u)
gl.Translate(x,y,z)
--gl.UnitMultMatrix(u)
gl.UnitPieceMultMatrix(u,p)
local pieceM1,_,_,pieceM4,pieceM5,_,_,pieceM8,pieceM9,_,_,pieceM12 = gl.GetMatrixData(GL.MODELVIEW)
gl.PopMatrix()
vertex0 = { width * .5 * pieceM1 + pieceM4, width * .5 * pieceM5 + pieceM8, width * .5 * pieceM9 + pieceM12}
vertex1 = { -width * .5 * pieceM1 + pieceM4, -width * .5 * pieceM5 + pieceM8, -width * .5 * pieceM9 + pieceM12}
local v = {}
for i = 1 , t.maxPhase do
v[2*i-1] = vertex0
v[2*i-0] = vertex1
end
trailList[u][p].vertices = v
end
end
end
end
gl.DepthTest(false)
gl.Color(1,1,1,1)
end
function gadget:RecvFromSynced(name, u, ud, team, piece, width, ttl, rate)
if name == "Add" then
AddTrail(u,ud,tean,piece,width,ttl,rate)
elseif name == "Destroy" then
trailList[u] = nil
end
end
end