Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cruise missile custom weapon behavior #1796

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions luarules/gadgets/unit_custom_weapons_behaviours.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ end
if gadgetHandler:IsSyncedCode() then

local projectiles = {}
local active_projectiles = {}
local checkingFunctions = {}
local applyingFunctions = {}
local math_sqrt = math.sqrt
Expand All @@ -26,6 +27,63 @@ if gadgetHandler:IsSyncedCode() then
end
end

checkingFunctions.cruise = {}
checkingFunctions.cruise["distance>0"] = function (proID)
--Spring.Echo()

if Spring.GetProjectileTimeToLive(proID) <= 0 then
return true
end
local targetTypeInt,target = Spring.GetProjectileTarget(proID)
local xx,yy,zz
local xxv,yyv,zzv
if targetTypeInt == string.byte('g') then
xx = target[1]
yy = target[2]
zz = target[3]
end
if targetTypeInt == string.byte('u') then
_,_,_,_,_,_,xx,yy,zz = Spring.GetUnitPosition(target,true,true)
end
local xp,yp,zp = Spring.GetProjectilePosition(proID)
local vxp,vyp,vzp = Spring.GetProjectileVelocity(proID)
local mag = math_sqrt(vxp*vxp+vyp*vyp+vzp*vzp)
local infos = projectiles[proID]
if math_sqrt((xp-xx)^2 + (yp-yy)^2 + (zp-zz)^2) > tonumber(infos.lockon_dist) then
yg = Spring.GetGroundHeight(xp,zp)
nx,ny,nz,slope= Spring.GetGroundNormal(xp,zp)
--Spring.Echo(Spring.GetGroundNormal(xp,zp))
--Spring.Echo(tonumber(infos.cruise_height)*slope)
if yp < yg + tonumber(infos.cruise_min_height) then
active_projectiles[proID] = true
Spring.SetProjectilePosition(proID,xp,yg + tonumber(infos.cruise_min_height),zp)
local norm = (vxp*nx+vyp*ny+vzp*nz)
xxv = vxp - norm*nx*0
yyv = vyp - norm*ny
zzv = vzp - norm*nz*0
Spring.SetProjectileVelocity(proID,xxv,yyv,zzv)
end
if yp > yg + tonumber(infos.cruise_max_height) and active_projectiles[proID] and vyp > -mag*.25 then
-- do not clamp to max height if
-- vertical velocity downward is more than 1/4 of current speed
-- probably just went off lip of steep cliff
Spring.SetProjectilePosition(proID,xp,yg + tonumber(infos.cruise_max_height),zp)
local norm = (vxp*nx+vyp*ny+vzp*nz)
xxv = vxp - norm*nx*0
yyv = vyp - norm*ny
zzv = vzp - norm*nz*0
Spring.SetProjectileVelocity(proID,xxv,yyv,zzv)
end
return false
else
return true
end
end

applyingFunctions.cruise = function (proID)
return false
end

checkingFunctions.cannonwaterpen = {}
checkingFunctions.cannonwaterpen["ypos<0"] = function (proID)
local _,y,_ = Spring.GetProjectilePosition(proID)
Expand Down Expand Up @@ -107,18 +165,21 @@ if gadgetHandler:IsSyncedCode() then
local wDefID = Spring.GetProjectileDefID(proID)
if specialWeaponCustomDefs[wDefID] then
projectiles[proID] = specialWeaponCustomDefs[wDefID]
active_projectiles[proID] = nil
end
end

function gadget:ProjectileDestroyed(proID)
projectiles[proID] = nil
active_projectiles[proID] = nil
end

function gadget:GameFrame(f)
for proID, infos in pairs(projectiles) do
if checkingFunctions[infos.speceffect][infos.when](proID) == true then
applyingFunctions[infos.speceffect](proID)
projectiles[proID] = nil
active_projectiles[proID] = nil
end
end
end
Expand Down