I need help finding a change.
Posted: 07 Dec 2016, 22:10
So in 100 this script still worked, something changed in 101 that broke it, no errors.
What this script did before:
there are 2 subgroups:
As of version 101 and after, when a group spawn happens, the rest of the units NO LONGER receive their move orders from the factory which spawned them, they remain in the factory as they never receive their orders. Any thoughts?
Handler
Spawner
What this script did before:
there are 2 subgroups:
- group spawn, by adding the tag "loosesquad" it will spawn the units but not pair them to a commander
Code: Select all
loosesquad = "1", squadMember1 = "unit", squadMember2 = "unit",
- squadspawn, it will spawn the units but not pair them to a commander
Code: Select all
squadMember1 = "unit", squadMember2 = "unit",
As of version 101 and after, when a group spawn happens, the rest of the units NO LONGER receive their move orders from the factory which spawned them, they remain in the factory as they never receive their orders. Any thoughts?
Handler
Code: Select all
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Automatically generated local definitions
local CMD_ATTACK = CMD.ATTACK
local CMD_FIRE_STATE = CMD.FIRE_STATE
local CMD_MOVE_STATE = CMD.MOVE_STATE
local CMD_STOP = CMD.STOP
local spGetTeamColor = Spring.GetTeamColor
local spGetUnitBasePosition = Spring.GetUnitBasePosition
local spGetUnitCommands = Spring.GetUnitCommands
local spGetUnitDefID = Spring.GetUnitDefID
local spGetUnitDirection = Spring.GetUnitDirection
local spGetUnitEstimatedPath = Spring.GetUnitEstimatedPath
local spGetUnitHeight = Spring.GetUnitHeight
local spGetUnitPosition = Spring.GetUnitPosition
local spGetUnitStates = Spring.GetUnitStates
local spGetUnitTeam = Spring.GetUnitTeam
local spGetVisibleUnits = Spring.GetVisibleUnits
local spGiveOrderArrayToUnitMap = Spring.GiveOrderArrayToUnitMap
local spGiveOrderToUnit = Spring.GiveOrderToUnit
local spSetUnitCOBValue = Spring.SetUnitCOBValue
local spSetUnitMoveGoal = Spring.SetUnitMoveGoal
local spSetUnitNoSelect = Spring.SetUnitNoSelect
local spSetUnitTarget = Spring.SetUnitTarget
local spGetLocalTeamID = Spring.GetLocalTeamID
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function gadget:GetInfo()
return {
name = "Squad Handler",
desc = "was bored",
author = "KDR_11k (David Becker)",
date = "2008-10-21",
license = "Public Domain",
layer = 1,
enabled = true
}
end
if (gadgetHandler:IsSyncedCode()) then
--SYNCED
local moveDist=40 --Units will retake their position if they arefurther than this away from their position in the formation
local holdPosDist=250 --Units further away will be set to hold position so they stop moving away if they see enemies
local squads={}
local squadMember={}
local patterns={
circle={
{80,0},{-80,0},{60,-60},{-60,-60},{0,80}
},
line={
{80,0},{-80,0},{160,0},{-160,0},{240,0},{-240,0}
},
bla={
{100,0},{-100,0},{50,-50},{-50,-50},{180,0},{-180,0}
},
}
local stopList={}
local orderRelayList={}
local stateOrderList={}
local function RegisterSquad(type, leader, units)
local followers={}
--local i = 1
for i,u in pairs(units) do
local ud = spGetUnitDefID(u)
spSetUnitNoSelect(u,true)
squadMember[u]={leader,i}
followers[i]=u
--spSetUnitCOBValue(u,75,UnitDefs[ud].speed * 1.4 * 65536/32) --move 40% faster to allow catching up
--i=i+1
end
squads[leader] = {type=type, followers=followers, pattern = UnitDefs[type].customParams.squadformation or "circle"}
end
local function ConvertQueue(cmds)
local newQ={}
for i,d in ipairs(cmds) do
newQ[i]={d.id,d.params,{}}
end
return newQ
end
function gadget:UnitDestroyed(u, ud, team)
if squadMember[u] then
local s = squadMember[u]
squads[s[1]].followers[s[2]] = nil
squadMember[u]=nil
end
if squads[u] then --was the squadleader
local leader = nil
local cmds = ConvertQueue(spGetUnitCommands(u))
for i,tu in pairs(squads[u].followers) do
if not leader then
spSetUnitNoSelect(tu,false)
spSetUnitCOBValue(u,75,UnitDefs[ud].speed * 65536)
squads[u].followers[i]=nil
squadMember[tu]=nil
squads[tu]=squads[u]
leader=tu
orderRelayList[tu]=cmds
else
squadMember[tu]={leader,i}
end
end
squads[u]=nil
end
end
function gadget:AllowCommand(u, ud, team, cmd, param, opt, tag, synced)
if not synced and squadMember[u] then
return false
end
if squads[u] then
if cmd == CMD_ATTACK then
for i,fu in pairs(squads[u].followers) do
if param[2] and param[3] then
spSetUnitTarget(fu,param[1],param[2],param[3])
else
spSetUnitTarget(fu,param[1])
end
end
elseif cmd == CMD_STOP then
for i,fu in pairs(squads[u].followers) do
stopList[fu]=true
end
elseif cmd == CMD_MOVE_STATE or cmd == CMD_FIRE_STATE then
for i,fu in pairs(squads[u].followers) do
stateOrderList[fu]={cmd,param}
end
end
return true
end
return true
end
function gadget:Initialize()
GG.RegisterSquad=RegisterSquad
_G.squads=squads
end
local function Pyth(x,ux,z,uz)
return math.sqrt((x-ux)*(x-ux) + (z-uz)*(z-uz))
end
local function GetFormationPosition(pattern,posNr,x,z,dx,dz)
local rx,rz
rx = -dz
rz = dx
local p = patterns[pattern][posNr]
return x + p[1]*rx + p[2]*dx, z + p[1]*rz + p[2]*dz
end
function gadget:GameFrame(f)
for u,_ in pairs(stopList) do
spGiveOrderToUnit(u,CMD_STOP,{},{})
stopList[u]=nil
end
for u,c in pairs(orderRelayList) do
spGiveOrderArrayToUnitMap({[u]=true},c)
orderRelayList[u]=nil
end
for u,d in pairs(stateOrderList) do
spGiveOrderToUnit(u,d[1],d[2],{})
stateOrderList[u]=nil
end
if f%59 < .1 then
for leader,d in pairs(squads) do
local wps, supp = spGetUnitEstimatedPath(leader)
local ux,uy,uz = spGetUnitBasePosition(leader)
local x,y,z
local dx,dz
local _,moveState = spGetUnitStates(leader)
if wps and wps[supp[2]-1] then
local p = wps[supp[2]-1]
x=p[1]
y=p[2]
z=p[3]
dist=Pyth(x,ux,z,uz)
dx=(x-ux)/dist
dz=(z-uz)/dist
else
x=ux
y=uy
z=uz
dx,_,dz=spGetUnitDirection(leader)
end
for i,u in pairs(d.followers) do
local fx,_,fz = spGetUnitPosition(u)
local tx,tz=GetFormationPosition(d.pattern,i,x,z,dx,dz)
local dist = Pyth(tx,fx,tz,fz)
if dist > moveDist then
spSetUnitMoveGoal(u,tx,0,tz)
else
spGiveOrderToUnit(u, CMD_MOVE_STATE,{moveState},{})
end
if dist > holdPosDist then
spGiveOrderToUnit(u, CMD_MOVE_STATE,{0},{})
end
end
end
end
end
else
--UNSYNCED
local glBillboard = gl.Billboard
local glColor = gl.Color
local glPopMatrix = gl.PopMatrix
local glPushMatrix = gl.PushMatrix
local glTexRect = gl.TexRect
local glTexture = gl.Texture
local glTranslate = gl.Translate
local size=10
local offset=15
local squads
function gadget:DrawWorld()
glTexture("bitmaps/icons/LeaderIcon.png")
local localTeam = spGetLocalTeamID()
for u,_ in spairs(squads) do
local team=spGetUnitTeam(u)
if team==localTeam then
local x,y,z=spGetUnitBasePosition(u)
local h = spGetUnitHeight(u)
local r,g,b = spGetTeamColor(team)
glColor(r,g,b,1)
glPushMatrix()
glTranslate(x,y+h,z)
glBillboard()
glTexRect(-size, -size + offset, size, size + offset, false, false)
glPopMatrix()
end
end
glTexture(false)
end
function gadget:Initialize()
squads=SYNCED.squads
end
end
Code: Select all
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Automatically generated local definitions
local spCreateUnit = Spring.CreateUnit
local spGetUnitBuildFacing = Spring.GetUnitBuildFacing
local spGetUnitStates = Spring.GetUnitStates
local spGetUnitCommands = Spring.GetUnitCommands
local spGetUnitPosition = Spring.GetUnitPosition
local spGiveOrderArrayToUnitMap = Spring.GiveOrderArrayToUnitMap
local spValidUnitID = Spring.ValidUnitID
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function gadget:GetInfo()
return {
name = "Squad Spawner",
desc = "Spawns a squad for some units",
author = "KDR_11k (David Becker)",
date = "2008-10-21",
license = "Public Domain",
layer = 1,
enabled = true
}
end
if (gadgetHandler:IsSyncedCode()) then
--SYNCED
local newList={}
GG.squadType={}
local squadSpawn={}
function gadget:UnitFinished(u,ud,team)
if not newList[u] then
squadSpawn[u]={ud=ud,team=team}
end
end
function gadget:UnitCreated(u,ud,team)
newList[u]=true
end
local function ConvertQueue(cmds,firestate,movestate)
local newQ={{CMD.FIRE_STATE, {firestate},{} }, {CMD.MOVE_STATE, {movestate},{} } }
for i,d in ipairs(cmds) do
newQ[i+2]={d.id,d.params,{"shift"}} --ignore opts since I don't think any part of a factory queue gives a damn about that...
end
return newQ
end
function gadget:GameFrame(f)
newList={}
for u,d in pairs(squadSpawn) do
if spValidUnitID(u) then
if GG.squadType[d.ud] then
Spring.Echo(d.ud, "squad spawner gameframe ")
local x,y,z=spGetUnitPosition(u)
local state = spGetUnitStates(u)
local cmds = ConvertQueue(spGetUnitCommands(u),state.firestate,state.movestate)
local h = spGetUnitBuildFacing(u)
local units= {}
local su={}
for i,nud in pairs(GG.squadType[d.ud]) do
local nu = spCreateUnit(nud,x+i,y,z,h,d.team)
if nu then
units[nu]=true
su[i]=nu
end
end
if GG.RegisterSquad and UnitDefs[d.ud].customParams.loosesquad ~= "1" then
GG.RegisterSquad(d.ud, u,su)
end
spGiveOrderArrayToUnitMap(units,cmds)
end
end
squadSpawn[u]=nil
end
end
function gadget:Initialize()
for ud,d in pairs(UnitDefs) do
local c = d.customParams
local i=1
local s={}
while c["squadmember"..i] do
s[i]=UnitDefNames[c["squadmember"..i]].id
i=i+1
end
if i > 1 then
GG.squadType[ud]=s
end
end
-- for n,t in pairs(GG.squadType) do
-- Spring.Echo("squad entry", n)
-- for k,v in pairs(t) do
-- Spring.Echo(k,v)
-- end
-- end
end
else
--UNSYNCED
return false
end