
The idea is like this:
-give tank a "target marker" weapon
-detect impact of "target marker"
-create aircraft unit at tank's position: this is the real projectile
-projectile unit gets attack order to impact position of "target marker"
-projectile unit attacks that spot, then kills itself
The "target marker weapon" is needed because afaik there is no real other way to tell where a unit will/has shot.
(Make this fake weapon "super fast flying" and invisible if you want)
Only the relevant parts:
gagdget:
Code: Select all
local clusterWeapon = {}
clusterWeapon[WeaponDefNames["tplaunchbox_targetmarker"].id] = true
--if weapondef is defined in unitdef, its name is like unitname+"_"+weaponname
function gadget:Initialize()
for v,k in pairs (clusterWeapon) do
Script.SetWatchWeapon (v, true)
end
end
--this callin is only called for weapons that have it enabled with Script.SetWatchWeapon
--(which is done in the above lines)
function gadget:Explosion(weaponID, px, py, pz, ownerID)
if (clusterWeapon [weaponID]) then
env = Spring.UnitScript.GetScriptEnv(ownerID)
if (env) then Spring.UnitScript.CallAsUnit(ownerID, env.launchMissile, px, py, pz) end
end
return true
end
Code: Select all
function launchMissile (targetX,targetY,targetY)
local x,y,z = Spring.GetUnitPiecePosDir (unitID, flare)
local missile = Spring.CreateUnit ("tpcruisemissile", x,y,z, math.random(1,4), teamID)
Spring.SetUnitNoSelect (missile, true)
Spring.GiveOrderToUnit(missile, CMD.FIRE_STATE , { 0 }, {}) --hold fire
Spring.GiveOrderToUnit(missile, CMD.ATTACK , {targetX,targetY,targetY}, {CMD.OPT_INTERNAL})
end
Notice Spring.GetUnitPiecePosDir instead of GetUnitPosition so that the projectile appears at gun of unit, instead of at its feet.
unit script of projectile, to make it kill itself after shooting:
Code: Select all
local shots = 0
function script.FireWeapon()
shots=shots +1
if shots > 5 then Spring.DestroyUnit (unitID) end
--EmitSfx(flare, muzzleflash)
end
I found gunships with somewhat quick speed,turnrate etc work best.
Aircraft give nicer flight paths but sometimes fly in stupid circles.
With target categories, you can also make the projecitile attackable.
(think scud & tomahawk missiles in C&C Generals)