This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
MainframeIndex = 0 -- The only mainframe we have, could get more complicated with more mainframes and prioritizing
function Update(I)
last_positions = {}
last_times = {}
for trans = 0, I:GetLuaTransceiverCount() do
local numTar = I:GetNumberOfTargets(MainframeIndex)
local target_info = I:GetTargetInfo(0, trans % numTar)
local target_position = target_info.Position
local target_velocity = target_info.Velocity
for mis = 0, I:GetLuaControlledMissileCount(trans) - 1 do
if last_positions[trans] ~= nil then
delta_position = target_position - last_positions[trans]
delta_time = I:GetGameTime() - last_times[trans]
target_velocity = delta_position / delta_time
end
local missile_info = I:GetLuaControlledMissileInfo(trans, mis)
local missile_position = missile_info.Position
local missile_speed = Vector3.Magnitude(missile_info.Velocity)
local target_distance = Vector3.Distance(target_position, missile_position)
local target_vector = Vector3.Normalize(target_position - missile_position)
local relative_speed = missile_speed - Vector3.Dot(target_vector, target_velocity)
local prediction = target_velocity * target_distance / relative_speed
local point = target_position prediction
I:SetLuaControlledMissileAimPoint(trans, mis, point.x, point.y, point.z)
last_positions[trans] = target_position
last_times[trans] = I:GetGameTime()
end
end
end
The accumulation of like 7 scripts i found and a all nighter results in this fairly accurate self distributing among targets missile script. If anyone has improvements please let me know, i am in the search for perfection.
Edit:
After another 6hrs of fiddling fun i bring a better guidance that doesnt go backwards sometimes and tracks like a god, it also makes all missiles target the same enemy because i couldn't get the multi target to work again
MainframeIndex = 0 function Update(I) local num_targets = I:GetNumberOfTargets(MainframeIndex) local target_info = I:GetTargetInfo(MainframeIndex, 0) if target_info ~= nil then local target_position = target_info.Position local target_velocity = target_info.Velocity for transceiver_index = 0, I:GetLuaTransceiverCount() - 1 do for missile_index = 0, I:GetLuaControlledMissileCount(transceiver_index) - 1 do local missile_info = I:GetLuaControlledMissileInfo(transceiver_index, missile_index) local missile_position = missile_info.Position local missile_speed = Vector3.Magnitude(missile_info.Velocity) local target_direction = Vector3.Normalize(target_position - missile_position) local target_distance = Vector3.Distance(target_position, missile_position) if prev_target_position ~= nil then local delta_position = target_position - prev_target_position local delta_time = I:GetTime() - prev_update_time target_velocity = delta_position / delta_time end local relative_speed = missile_speed - Vector3.Dot(target_direction, target_velocity) local time_to_intercept local predicted_target_position = target_position local aim_point_position if relative_speed > 0 then time_to_intercept = target_distance / relative_speed predicted_target_position = target_position target_velocity * time_to_intercept if target_distance > missile_speed * time_to_intercept or time_to_intercept < 0 then aim_point_position = predicted_target_position else local a = (missile_speed^2 - relative_speed^2) local b = -2 * Vector3.Dot(target_direction, target_velocity) * target_distance local c = target_distance^2 local discriminant = b^2 - 4 * a * c if discriminant >= 0 then time_to_intercept = (-b - math.sqrt(discriminant)) / (2 * a) aim_point_position = target_position target_velocity * time_to_intercept else aim_point_position = predicted_target_position end end else aim_point_position = predicted_target_position end I:SetLuaControlledMissileAimPoint(transceiver_index, missile_index, aim_point_position.x, aim_point_position.y, aim_point_position.z) prev_missile_position = missile_position end end prev_target_position = target_position prev_update_time = I:GetTime() end end
Once more, let me know any improvements, any other scripts to consume into this, i will get perfection for the tracking at the cost of my mere 2 days of missed sleep.
Subreddit
Post Details
- Posted
- 1 year ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/FromTheDept...