Page 1 of 2

Getting Units to Attack without Turret

Posted: 05 Feb 2011, 07:32
by kalda341
The following script works for infantry when turret = true in the weapon defs

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
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

Re: Getting Units to Attack without Turret

Posted: 05 Feb 2011, 12:08
by knorke
not sure if i understood 100% correctly, do you want infantry that aims/returns to normal instantly like ie space marines in starcraft broodwar?
after instructed to move, move backwards or to the side, etc before returning to the normal orientation.
could you just make them return to normal faster?
your script already has
local RESTORE_DELAY =
at the top, maybe just put a lower number?
there is also
Spring.SetUnitRotation in http://springrts.com/wiki/Lua_MoveCtrl
if thats helpfull maybe.

Re: Getting Units to Attack without Turret

Posted: 05 Feb 2011, 19:16
by kalda341
Ultimately i want to remove the whole turret script, but if turret = false the unit won't attack.

Re: Getting Units to Attack without Turret

Posted: 05 Feb 2011, 21:30
by kalda341
The spring.setunitrotation would probably be helpful if I could work out how to set the unitID to be the unit the script is for. But yes, it would probably be better if I could just remove the whole turret script.

Re: Getting Units to Attack without Turret

Posted: 05 Feb 2011, 21:58
by FLOZi
Maybe I'm missing the point altogether, but why not just give infantry a limited firearc with the usual engine tags?

Re: Getting Units to Attack without Turret

Posted: 05 Feb 2011, 22:26
by knorke
work out how to set the unitID to be the unit the script is for.
its just unitID ;)
http://answers.springlobby.info/questio ... own-unitid

Re: Getting Units to Attack without Turret

Posted: 05 Feb 2011, 22:31
by kalda341
knorke wrote:
work out how to set the unitID to be the unit the script is for.
its just unitID ;)
http://answers.springlobby.info/questio ... own-unitid
I tried that, but I get a bad unitID error.

Re: Getting Units to Attack without Turret

Posted: 05 Feb 2011, 22:37
by kalda341
FLOZi wrote:Maybe I'm missing the point altogether, but why not just give infantry a limited firearc with the usual engine tags?
I don't really understand what you mean. I assume you also don't understand what I mean (fair enough, I've done a pretty bad job of explaining it).
The easiest solution to what I want is to completely abandon the turret script and set turret = false in the weapondefs. However, what would I replace the script with so the unit can still aim and attack? Perhaps it would be easier if someone just posted a standard infantry script and I worked it out myself?

Re: Getting Units to Attack without Turret

Posted: 05 Feb 2011, 23:29
by FLOZi
I figured you want a script that allows an human type unit to swivel its torso to aim but not so much that it can do a scene from the exorcist and break its back. :(

Re: Getting Units to Attack without Turret

Posted: 05 Feb 2011, 23:47
by kalda341
FLOZi wrote:I figured you want a script that allows an human type unit to swivel its torso to aim but not so much that it can do a scene from the exorcist and break its back. :(
No, the whole body was going to rotate. I was thinking about that at first, but the torso is near the top of the model hierarchy. The walking backwards / side to side without turning would work but it is too difficult to get a convincing animation. I want to just abandon the turret script, but i cannot get the unit to attack with turret = false. What would be the replacement unitscript?

Re: Getting Units to Attack without Turret

Posted: 07 Feb 2011, 07:40
by Forboding Angel
kalda341 wrote:but i cannot get the unit to attack with turret = false.
Needs moar tolerance (1000 - 5000 should do the trick nicely).

Re: Getting Units to Attack without Turret

Posted: 07 Feb 2011, 07:43
by kalda341
Forboding Angel wrote:
kalda341 wrote:but i cannot get the unit to attack with turret = false.
Needs moar tolerance (1000 - 5000 should do the trick nicely).
The unit still cannot attack.

Re: Getting Units to Attack without Turret

Posted: 09 Feb 2011, 06:45
by kalda341
Any ideas?

Re: Getting Units to Attack without Turret

Posted: 10 Feb 2011, 18:43
by knorke
kalda341 wrote:
knorke wrote:
work out how to set the unitID to be the unit the script is for.
its just unitID ;)
http://answers.springlobby.info/questio ... own-unitid
I tried that, but I get a bad unitID error.
When you do

Code: Select all

function script.Create()
	Spring.Echo ("unit " .. unitID .. " was created!")
end
Does that give an error too?

Isnt the idea of turret=false that Springs pathfinder aims the unit at the target?
ie if you look at fighter planes in the *a Mods, they also turn towards the target and all that. Shouldnt it work the same for ground units?

Re: Getting Units to Attack without Turret

Posted: 10 Feb 2011, 19:11
by kalda341
When you do

Code: Select all

function script.Create()
	Spring.Echo ("unit " .. unitID .. " was created!")
end
Does that give an error too?

Isnt the idea of turret=false that Springs pathfinder aims the unit at the target?
ie if you look at fighter planes in the *a Mods, they also turn towards the target and all that. Shouldnt it work the same for ground units?
I worked out that the syntax was wrong with the unitID. It also wasn't really what I was looking for, though close. I hadn't thought of fighter planes and are looking through the zk scripts right now.

Re: Getting Units to Attack without Turret

Posted: 10 Feb 2011, 19:26
by Pxtl
The extreme case is to do what Kernel Panic did with the Byte unit - the *entire unit* is a turret and the actual body of the unit is invisible/nonexistent. Combine that with a script to force units to cease moving when firing and you might get some of your desired effects.

Re: Getting Units to Attack without Turret

Posted: 11 Feb 2011, 22:43
by bobthedinosaur
Why doesn't he just do a check in the aim script for the angle to the target, and if it isn't in the angle wanted tell it to change the units heading to point at the target?

Re: Getting Units to Attack without Turret

Posted: 12 Feb 2011, 10:38
by kalda341
bobthedinosaur wrote:Why doesn't he just do a check in the aim script for the angle to the target, and if it isn't in the angle wanted tell it to change the units heading to point at the target?
That's kinda what I want. What is the callout for changing heading?

Re: Getting Units to Attack without Turret

Posted: 12 Feb 2011, 17:48
by knorke
There is
Spring.SetUnitRotation (unitID, rotx, roty, rotz)
http://springrts.com/wiki/Lua_SyncedCtrl
but it changes rotation instantly.
Also not sure how good it plays together with ie pathfinding.

Re: Getting Units to Attack without Turret

Posted: 12 Feb 2011, 21:02
by kalda341
knorke wrote:There is
Spring.SetUnitRotation (unitID, rotx, roty, rotz)
http://springrts.com/wiki/Lua_SyncedCtrl
but it changes rotation instantly.
Also not sure how good it plays together with ie pathfinding.
To animate is (so it's not instant) would it work if I: Spring.UnitScript.GetPieceRotation and then something likeSpring.SetUnitRotation (unitID, 0, localisedpeicerotation+1, 0)
or something like that in the aim weapon script?