Hard Light Productions Forums

FreeSpace Releases => Scripting Releases => Topic started by: m!m on February 14, 2011, 06:11:15 am

Title: Missile particle script
Post by: m!m on February 14, 2011, 06:11:15 am
(http://img714.imageshack.us/img714/5835/missilescript.jpg)
Yes, yet another script. This script tries to imitate that a missile with $FreeFlighTime set actually ignites when is launches as seen in the screenshot above.
To use create a file called missilePart-sct.tbm in your data/tables directory and paste the following text into it:
Code: (missilePart-sct.tbm) [Select]
#Conditional Hooks

$Application: FS2_Open
$On Game Init:
[
function getLivingTime(maxLife, lifeLeft)
if (maxLife == nil or lifeLeft == nil) then
return -1
end

local time = maxLife - lifeLeft

return time
end

function weaponLivingTime(weapon)
if (weapon == nil or not weapon:isValid()) then
return -1
end

local maxLife = weapon.Class.LifeMax
local lifeLeft = weapon.LifeLeft

if (weapon.Class:isMissile() and weapon.HomingObject:isValid()) then
maxLife = maxLife * 1.2 -- "fix" for LiveLeft altering in code/weapons.cpp:4664
end

return getLivingTime(maxLife, lifeLeft)
end

function createWeaponParticle(weapon, velocity, lifeTime, radiusScale, texture)
if (weapon == nil or not weapon:isValid()) then
return
end

local model = weapon.Class.Model
local thrusters = model.Thrusters
for i=1, #thrusters do
local bank = thrusters[i]
if (bank:isValid()) then
for j=1, #bank do
local glow = bank[j]
if (glow:isValid()) then
local realVec = weapon.Orientation:unrotateVector(glow.Position)
local globalVec = weapon.Position + realVec

ts.createParticle(globalVec, velocity, lifeTime, glow.Radius * radiusScale, PARTICLE_BITMAP, -1, false, texture)
end
end
end
end
end

nullVector = ba.createVector(0, 0, 0)
maxFrameTime = 1 / 10
]

$State: GS_STATE_GAME_PLAY
$On Frame:
[
if ba.getFrametime() > maxFrameTime then
return -- If FPS is already low don't lower it with even more load
end

if (trailTexture == nil or not trailTexture:isValid()) then
return
end

if (ignitionAnim == nil or not ignitionAnim:isValid()) then
return
end

for i=1, #mn.Weapons do
local weapon = mn.Weapons[i]

if (weapon:isValid() and weapon.Class:isMissile() and weapon.HomingObject:isValid() and weaponLivingTime(weapon) > weapon.Class.FreeFlightTime) then
local velVec = weapon.Physics.Velocity - weapon:getfvec(true) * 200
if ((weapon:getfvec(true) * 200):getMagnitude() > weapon.Physics.Velocity:getMagnitude()) then
velVec = velVec / 20
end

local radius = (1 - (weapon.Class.Speed / weapon.Physics.Velocity:getMagnitude())) * 1.5
if (radius > 3) then
radius = 3
end

createWeaponParticle(weapon, velVec, particleLifeTime, radius * 3, trailTexture)

if (weapon.Class.FreeFlightTime > 0 and weaponLivingTime(weapon) >= weapon.Class.FreeFlightTime and (weaponLivingTime(weapon) - ba.getFrametime()) <= weapon.Class.FreeFlightTime) then
createWeaponParticle(weapon, nullVector, ingitionLifeTime, radius * 5, ignitionAnim)
end
end
end
]

$Application: FS2_Open
$On Mission Start:
[
trailTexture = gr.loadTexture("capflash", true)
if (trailTexture ~= nil and trailTexture:isValid()) then
particleLifeTime = trailTexture:getFramesLeft() / trailTexture:getFPS()
end

ignitionAnim = gr.loadTexture("exp20", true)
if (ignitionAnim ~= nil and ignitionAnim:isValid()) then
ingitionLifeTime =  ignitionAnim:getFramesLeft() / ignitionAnim:getFPS()
end

if (trailTexture == nil or not trailTexture:isValid()) then
ba.warning("Invalid trail texture!")
elseif (ignitionAnim == nil or not ignitionAnim:isValid()) then
ba.warning("Invalid ignition texture!")
end
]

$Application: FS2_Open
$On Mission End:
[
if (trailTexture ~= nil) then
trailTexture:unload()
trailTexture = nil
end
if (ignitionAnim ~= nil) then
ignitionAnim:unload()
ignitionAnim = nil
end
]

#End

Feel free to criticize and I'm always open for new feature requests :nod:

Regards,
m!m

EDIT: I should say that this script requires a build of Revision 7011 or later
Title: Re: Missile particle script
Post by: Fury on February 14, 2011, 06:59:30 am
Can you extend this to do the same with trails?
Title: Re: Missile particle script
Post by: m!m on February 14, 2011, 07:05:07 am
I'm not entirely sure what you mean. Could you please explain what you mean in detail? :)
Title: Re: Missile particle script
Post by: Fury on February 14, 2011, 07:42:31 am
Nevermind. I thought what this script does is prevent particle spews until free flight time expires, but it's apparently exact opposite.
Title: Re: Missile particle script
Post by: Wanderer on February 14, 2011, 07:59:24 am
Yes.. Well traditionally the booster rocket makes the most of the smoke and it burns only in initial launch / flight sequence
Title: Re: Missile particle script
Post by: m!m on February 14, 2011, 08:25:35 am
That's the effects this script should create. It only needs a better effect :nervous: :D
Title: Re: Missile particle script
Post by: Nuke on February 14, 2011, 09:00:34 am
that looks pretty cool.
Title: Re: Missile particle script
Post by: Dragon on February 15, 2011, 12:33:53 pm
That looks interesting, but I'd like to see a script that would allow making missiles which spend first part of their flight time unpowered, then start tracking and ignite their engine after their free flight time is expired. Booster rocket for these could be done with a muzzleflash (as they're ussualy fighter missiles). That would be really helpfull for atmospheric mods.
Title: Re: Missile particle script
Post by: m!m on February 15, 2011, 12:38:12 pm
If I understand you correctly, this is exactly what this script does :p.
This script creates a specified particle when the free flight time of a weapon runs of. Then it creates another type of particle as long as the missile accelerates.
Title: Re: Missile particle script
Post by: Dragon on February 16, 2011, 04:03:24 pm
No, I meant something like switching on the trail and PSpew  (which are disabled at launch) when the FreeFlightTime runs out.
Title: Re: Missile particle script
Post by: m!m on February 17, 2011, 05:47:35 am
That would need to be done inside the engine. It's currently not possible with scripting.
Title: Re: Missile particle script
Post by: Wanderer on February 19, 2011, 04:06:48 am
Well.. spawn 1 type weapon with fixed minimum spawn angle might do the trick - though you would still likely need scripting to make sure ai would fire the weapon as it should and that the 'swap' would happen when it should.
Title: Re: Missile particle script
Post by: Nuke on February 19, 2011, 03:07:09 pm
you know this gets me thinking, i should add some features to my particle system for missiles. should add a tag to indicate what stage(s) of flight in which a spewer will be allowed to emit.
i can think of the following stages:

all
free flight
powered flight
has lock
lost lock
armed
disarmed
emp

-this would probibly be done as a flag list. you would list the modes you want the effect to be active.
 example: $active_during_stage: ("free flight" "emp") ;only used when just launched or dead in the water
-'all' would be defaulted to on unless the tag is used, once used it will be assumed to be off and you would need to set the flag manually if you wanted to use it.
-would also call for an +override for xmt compatibility
Title: Re: Missile particle script
Post by: Dragon on June 11, 2011, 11:07:34 am
Could you modify this script to use a config for defining spew textures on a per-weapon basis?
Title: Re: Missile particle script
Post by: m!m on June 20, 2011, 04:14:51 am
That could be possible and I could use my particle trail script which already has all functionality required for this :D
Title: Re: Missile particle script
Post by: Droid803 on June 20, 2011, 04:18:05 pm
This script crashes FSO upon exit for me.
Title: Re: Missile particle script
Post by: Luis Dias on June 20, 2011, 04:37:45 pm
Is it possible for it to spawn different sized and with different speeds?

It needn't a random quantifier or anything of the sort. But rather than just one single equal emission, it would consist of, say, 6 emmissions. Three different sizes and two different speeds.
Title: Re: Missile particle script
Post by: Dragon on June 20, 2011, 06:43:14 pm
That could be possible and I could use my particle trail script which already has all functionality required for this :D
Incorporating this into particle trail script seems like a good idea.
Title: Re: Missile particle script
Post by: mjn.mixael on June 21, 2011, 01:25:30 pm
does the particle trail still not work with corkscrew missiles?
Title: Re: Missile particle script
Post by: m!m on June 21, 2011, 01:27:48 pm
I didn't test that case specifically but I can't imagine why it shouldn't work :nervous:
Title: Re: Missile particle script
Post by: Dragon on June 21, 2011, 05:21:42 pm
Well, it's not really that it doesn't work, it just flies in straight line instead of behind the missile, due to the way corkscrew missiles work. It was only fixed for default spew type, then Nuke implemented his particle code and broke it again. I don't know how scripts will handle this though.
Title: Re: Missile particle script
Post by: m!m on June 22, 2011, 02:10:29 am
The script simply takes the position of the object, a missile in this case and creates the particles at the point of its thruster(s). I'll integrate this into the particle trail script, after that I'll check if it works for corkscrew missiles. :nod:
Title: Re: Missile particle script
Post by: Dragon on June 22, 2011, 05:34:42 am
Well, I'm still not sure if it'll work "position of the object", IIRC, goes in straight line, but thruster point is attached to missile model, which does the corkscrew. I'd like a coder to give a better answer, but that's how I understand how the system works, so I may be wrong.