Getting Units to Attack without Turret

Getting Units to Attack without Turret

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Getting Units to Attack without Turret

Post 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
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Getting Units to Attack without Turret

Post 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.
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Getting Units to Attack without Turret

Post by kalda341 »

Ultimately i want to remove the whole turret script, but if turret = false the unit won't attack.
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Getting Units to Attack without Turret

Post 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.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Getting Units to Attack without Turret

Post by FLOZi »

Maybe I'm missing the point altogether, but why not just give infantry a limited firearc with the usual engine tags?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Getting Units to Attack without Turret

Post 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
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Getting Units to Attack without Turret

Post 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.
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Getting Units to Attack without Turret

Post 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?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Getting Units to Attack without Turret

Post 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. :(
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Getting Units to Attack without Turret

Post 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?
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Getting Units to Attack without Turret

Post 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).
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Getting Units to Attack without Turret

Post 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.
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Getting Units to Attack without Turret

Post by kalda341 »

Any ideas?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Getting Units to Attack without Turret

Post 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?
Attachments
spring 2011-02-10 18-53-46-48.gif
spring 2011-02-10 18-53-46-48.gif (2.76 MiB) Viewed 2042 times
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Getting Units to Attack without Turret

Post 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.
User avatar
Pxtl
Posts: 6112
Joined: 23 Oct 2004, 01:43

Re: Getting Units to Attack without Turret

Post 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.
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Re: Getting Units to Attack without Turret

Post 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?
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Getting Units to Attack without Turret

Post 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?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Getting Units to Attack without Turret

Post 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.
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Getting Units to Attack without Turret

Post 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?
Post Reply

Return to “Lua Scripts”