Getting Units to Attack without Turret
Posted: 05 Feb 2011, 07:32
The following script works for infantry when turret = true in the weapon defs
However this is far from elegant as the infantry will rotate to target with no animation (this could quite easily be solved though) but after instructed to move, move backwards or to the side, etc before returning to the normal orientation. What I need to do is abandon the whole turret script (but if turret = false the unit no longer attacks) or let the position after the infantry has rotated to shoot be the default orientation. Which is easier, i suspect the first, and how would I accomplish this?
Thanks in advance
Code: Select all
local flare, lf, ula, head, lrl, torso, gun, lra, lll, ura, lla, url, ull, pelvis, base, rf = piece('flare', 'lf', 'ula', 'head', 'lrl', 'torso', 'gun', 'lra', 'lll', 'ura', 'lla', 'url', 'ull', 'pelvis', 'base', 'rf')
local SIG_AIM = 2
local RESTORE_DELAY = Spring.UnitScript.GetLongestReloadTime(unitID) * 0.4
-- time for turret to wait before return
function script.Create()
return 0
end
local function RestoreAfterDelay(unitID)
-- wait, then turret return
Sleep(RESTORE_DELAY)
Turn(base, y_axis, 0, math.rad(150))
Turn(gun, x_axis, 0, math.rad(30))
end
local function Walk()
SetSignalMask( walk_go )
Sleep(30)
while ( true ) do
if x == 1 then
Turn( lll, x_axis, -5, 5 ) -- lift lower left leg
Turn( ull, x_axis, -1, 5 ) -- lift upper left leg
end
Turn( lrl, x_axis, 0, 5 ) -- lower lower right leg
Turn( url, x_axis, 1, 5 ) -- lower upper right leg
x = 1
WaitForTurn( lll, x_axis )
WaitForTurn( ull, x_axis )
WaitForTurn( lrl, x_axis )
WaitForTurn( url, x_axis )
Sleep(30)
Turn( lrl, x_axis, -5, 5 ) -- lift lower right leg
Turn( url, x_axis, -1, 5 ) -- lift upper right leg
Turn( lll, x_axis, 0, 5 ) -- lower lower left leg
Turn( ull, x_axis, 1, 5 ) -- lower upper left leg
WaitForTurn( lll, x_axis )
WaitForTurn( ull, x_axis )
WaitForTurn( lrl, x_axis )
WaitForTurn( url, x_axis )
Sleep(30)
end
end
local function StopWalk()
Signal( walk_go )
Turn( lll, x_axis, 0, 5 )
Turn( url, x_axis, 0, 5 )
Turn( lrl, x_axis, 0, 5 )
Turn( ull, x_axis, 0, 5 )
end
-- returns all legs to normal positions
function script.StartMoving()
StartThread( Walk )
end
function script.StopMoving()
StartThread( StopWalk )
x = 0
end
function script.AimWeapon(weaponID, heading, pitch)
Signal(SIG_AIM)
SetSignalMask(SIG_AIM)
-- stops firing and tuning at same time
Turn(base, y_axis, heading, math.rad(70))
Turn(gun, x_axis, -pitch, math.rad(60))
WaitForTurn(base, y_axis)
WaitForTurn(gun, x_axis)
StartThread(RestoreAfterDelay)
return true
end
function script.FireWeapon(weaponID)
EmitSfx(flare, 0)
end
function script.QueryWeapon() return flare end
-- check flare
function script.AimFromWeapon() return gun end
-- checks if flare is aiming at anything.
function script.Killed(recentDamage, maxHealth)
return 0
end
function script.HitByWeapon(x,z,weaponDef,damage)
-- stops damage before fully built
if GetUnitValue(COB.BUILD_PERCENT_LEFT)>2 then return 0
else return damage
end
end
Thanks in advance