If the player band box attacks, a ship could be in range of multiple targets but only actually firing at one of them. But maybe that’s not a problem for this application…

As long as it is in range, it should consume ammunition. But, once again, for the thing suggested by evillejedi, it’s not that much of an issue because it is supposed to average the ammunition consumption over time, like a prediction table for real world logistics, planning that a regiment will consume X tons of ammunition per week of combat. If you have an error of a few hours here and there, it’s not that critical as long as you are not doing some “just in time” logistics (good for the industry, very bad for the military).

In other news, I reduced the CPU load of the code by staggering its implementation with a simple trigger based on the shipID of every ship and making it so the code for that specific ship gets activated once every X iterations with all ships dispersed relatively homogeneously between all X iterations. Before this, if the code was activated every 5 seconds, I noticed that all units activated the code at the same time, every 5 seconds, thus the stutter experienced in-game. Here, the stutter is reduced even more.

2 Likes

Double post, but I have to acknowledge that you are right and the decoy code activated itself for missiles fired at other planes of the group. Of course, it led to really cool effects of missiles banking hard away from their initial target to rush at a decoy fired a couple km away.

The other issue is that if you targeted 50 fighters and a destroyer, the capship-targeting torpedo would likely be fired at a fighter, and vice-versa.

In any case, I wrote an additional piece of code to pseudo-randomly select individual crafts in the selected target group and check whether they are of the appropriate attack class and within the correct range. It was a bit inspired by the code written a long time ago by, I think, the Star Trek mod team to do auto-repair on ally ships by scanning each ship and seeing whether it needed repairs.

EDIT 1: I will test this improved code during the week and check if it solves the CTD issue I have. Outside this issue, BTW, the code survived a stress-test with two groups of 150 flights of 3 planes each using missiles and decoys. I had around a thousand missiles and decoys fired before the CTD ended the game the very same way it ends it when a couple of flights fight each other.

It was lagging “a bit” at the time, but I had debug print options on giving me a log a MB large with the list of missiles, decoys and script steps being done.

EDIT 2: this new feature seems to be perfectly compatible with the AI script released on the other thread.

EDIT 3: I got the single target selection to work, which, by the way, leads to really cool BSG 2003 salvo missile launches. However, I still have this CTD bug I cannot isolate. It does happen even if the decoys are deactivated and is definitely linked to the missiles, but the log doesn’t say anything and even if I add print instruction between each and every script instruction, I simply learn that the crash takes place after the end of the custom code update for some missile and before another custom code can even begin. Strange and very annoying as it is the only real obstacle left for a release. The only clue at the moment is that the CTD takes place mostly at the same time, when the missiles’ shipID reach around 2,500. But it’s not it as my huge stress test reached 17,000 before the CTD, with a lot more launchers, of course.

You can see below the effects of the pseudo-random target selection algorithm for missiles:

EDIT 4: I rewrote the missile code (the new version is, BTW, fully commented and much cleaner to help people reverse-engineer and improve it as needed) as well as increased the time between code updates. It seems to have done the trick. Honestly, I have no clue what happened. I’ll do the same things for decoys and keep my fingers crossed.

EDIT 5: scratch this, it crashed pretty fast during a stress test with 200 fighters fighting it out.

3 Likes

I’ll be blunt, people: this is bad.

The CTD issue happens specifically during missile fire, AKA the core asset of the massive update I planned. It flew under the radar initially because it seems to trigger after numerous shots, but it cannot be ignored, as even with four dozen fighters firing missiles (without decoys), it still crashes under three minutes of total gameplay (including the game setup). I have tried tracking the whole thing without success or playing a bit with the update times, but it doesn’t seem to affect the entire mess. It doesn’t look a code error either, as the tracking procedure did not report any script interruption, and given that the scripts work just as planned for some time before the crash, it excludes the whole issue.

As I said, I rewrote the code, this time with nice comments and a clearer structure, without the legacy bits of code commented out like I had before. It did not work either. It is not linked to a table overload or stuff like this as I can get almost arbitrarly large numbers of missiles shot if I increase the number of available fighters.

This is both frustrating and infuriating as I am this close to a fully working clean ammunition system.

Oh dear :frowning: Good luck tracing the source of the CTD…

1 Like

Yeah. I’ve been on it for hours at a day over the past three or four days. Since the AI was working, actually, so I could see how it integrated itself with the new gameplay mechanic. If you have any idea on how to find the source, I would be grateful. Hell, I am seriously considering the option of putting a good bounty, like 150 to 250 $, on a reliable fix to that CTD (if I do so, I’ll upload a set of files with the code and all that’s needed to run the game with its new functionality), if only because it’s blocking me from something I have been dreaming of for a dozen years now. Plus, it could be something really good for other works such as full-feature remasters of Cataclysm.

It’s a long shot, but maybe @bitvenom or @thisquietreverie can analyze the crash dumps for some extra clues.

1 Like

Are they still active?

My approach is generally to put print statements everywhere until you can track what lines cause the issue. Beyond that (which I am sure you have already tried) I cannot offer any insight…

I did it indeed. That’s why I’m tearing my hair out, because the crash happens outside the scripts. I had, at one time, a long series of print(“A”) to, like print (“EG”) or something, and a log in MB before the crash. But at the beginning of each script, I had specific “start script X” and “end script X”. Guess what? The last thing I saw was stuff like end script decoy or end script missile. I’m 90 % sure it’s not a syntax issue, as I rewrote the scripts afterwards with no change to the result, and frankly, it’s working pretty well outside this, even stable with very low number of units. I’m starting to suspect my computer, since my hard drives are all 95 to 99 % full and the laptop starts getting old.

if its working properly with less units, it could be an overload of fx . It could also be an issue with data left over from one missile being used by a new missile . You could try uploading enough source on here so others can give it a test . if it is your computer being overloaded to much, then it may become more noticeable with the more people who try it .

1 Like

I will clean the stuff and try to make a standalone package for testing purposes, I will post a link when it’s done. Thanks!

3 Likes

Just a shot in the dark. But I figured I’d rather pitch you a daft thought than hold up progress in the odd chance this could mean anything.

Since the missiles are now spawning as ships do your fighters have shiphold abilities? I know the game will crash if a ship spawns something without having the option to hold anything at all first. I mean it doesn’t even have to be that ship type.

Or at least it used to be like that. In Classic anyways.

I don’t know.

Sure hope you can figure this out.

Nah, no need for this. The issue is that the code does work as intended. You can see in the screenshots I posted that I had up to several hundreds of missiles and decoys fired properly. Well, not exactly for the decoys, because I am still struggling a bit for a code allowing me to reliably determine which missiles are attacking the specific craft running the code so I can lure them on the decoy. ATM, the decoys will lure missiles fired at other friendly planes or even already-lured missiles. Quite pretty effect, actually, it’s like in Battlestar Galactica when a Raptor’s decoy lures a Cylon missile.

Nah, the problem is that it crashes at some point, and no debugging procedure I have tried managed to isolate a code or a game error at any point. It runs for some time, but after enough (though a different number each time) missiles have been shot, it suddenly crashes.

EDIT: however… I could try to work from the drone frigate code, which does something similar to the trick used here. Might be worth looking into it even though I fear it would require recompiling the .hod with dockpathes. I’ll do it later, though, I got way too much LUA over the past week and I’ve got to start Witcher 3.

In the case @BitVenom or another dev’ has the time to check, here is an upload of the dump and minidump I got after a CTD today (I was checking if configuring the missiles to not be rendered beyond a very short distance would help - it didn’t) after a couple of minutes with fighters firing and dodging missiles.

1 Like

2 things with your command line arguments,

  1. Does fakefullscreen do anything? I haven’t heard of that argument though it does appear in the exe (Though as fakeFullscreen [Second F is a capital])
  2. You miss-spelt traceHODs, you have it as taceHODs

I’ll correct the trace, though the Fakefullscreen is for borderless full screen.

Thought of something else. I mean maybe you’ve thought about this already? But by changing those missiles into ships do you think you’re hitting the hard-coded limit of the number of ships a player can have? I thought that was somewhere around 250. I don’t know. Maybe that’s different for RM. No idea.

Nope, I tested this specific hypothesis already. Several hundreds fighters each firing missiles and dropping decoys get a crash in roughly the same time as it would for a dozen or two fighters doing their stuff. I had counters indicating close to a thousand objects deployed overall before the crash, which would happen just as well with two dozen objects.

Tinkered here and there with no success, so I’m going to post my code to see if anyone has an idea what could go wrong.
This is the code in lib.lua, it covers what the “missiles”, fuel, decoys and torpedoes will do:

function Fuel_Strikecraft(CustomGroup, playerID, shipID, fuel_max)
--print("Fuel")
--print(shipID)
dofilepath("data:scripts/lib/custom/shiptable.lua")

local b = shipID
--print("Test 1")
--print(b)
-- --print(ships[b].weapon.ammo)

local ca = ships[b].fuel.fuel

if ca == -10 then

if SobGroup_Selected(CustomGroup) == 1 then
UI_SetTextLabelText("NewTaskbar", "MaxFuel", "N/A")	
end

else

--print(ships[b].fuel.fuel)
--print(ca)
local c = ca/fuel_max
--print(c)
local cc = floor(c*100)
--print(c)
--print(ca)
--print(fuel_max)



if ca>0 and SobGroup_GetActualSpeed(CustomGroup)>5 then
ships[b].fuel.fuel = ca-1 -- let's burn a bit more fuel, baby!
--print(ships[b].fuel.fuel)
ships[b].fuel.RTB = 1 -- I still have fuel
--SobGroup_SetHardPointHealth(CustomGroup,'FUELTANK',c)

if SobGroup_Selected(CustomGroup) == 1 then
UI_SetTextLabelText("NewTaskbar", "MaxFuel", ""..cc)	
end

end

if ca>0 then
if SobGroup_Selected(CustomGroup) == 1 then
UI_SetTextLabelText("NewTaskbar", "MaxFuel", ""..cc)
end
--print(cc)
else
ships[b].fuel.RTB = 0 -- I am Bingo Fuel, out of fuel, let's Return To Base
if SobGroup_Selected(CustomGroup) == 1 then
UI_SetTextLabelText("NewTaskbar", "MaxFuel", "OUT")	
end
end
end

end

function Display_Stats(screen,stat1,check1,stat2,check2,stat3,check3,stat4,check4)	
	local check = check1 + check2 + check3 + check4
	if check > 1 then
		UI_SetElementPosition(screen, "shipstatsframe", 11, 0);
	elseif check > 0 then
		UI_SetElementPosition(screen, "shipstatsframe", 32, 0);
	else
		UI_SetElementPosition(screen, "shipstatsframe", 50, 0);
	end			
	if check1 > 0 then
		UI_SetElementSize(screen, stat1, 40, 10);
		UI_SetElementVisible(screen, stat1, check1)	
	else
		UI_SetElementSize(screen, stat1, 0, 0);
		UI_SetElementVisible(screen, stat1, check1)
	end	
	if check2 > 0 then
		UI_SetElementSize(screen, stat2, 40, 10);
		UI_SetElementVisible(screen, stat2, check2)	
	else
		UI_SetElementSize(screen, stat2, 0, 0);
		UI_SetElementVisible(screen, stat2, check2)
	end	
	if check3 > 0 then
		UI_SetElementSize(screen, stat3, 40, 10);
		UI_SetElementVisible(screen, stat3, check3)	
	else
		UI_SetElementSize(screen, stat3, 0, 0);
		UI_SetElementVisible(screen, stat3, check3)
	end	
	if check4 > 0 then
		UI_SetElementSize(screen, stat4, 40, 10);
		UI_SetElementVisible(screen, stat4, check4)	
	else
		UI_SetElementSize(screen, stat4, 0, 0);
		UI_SetElementVisible(screen, stat4, check4)
	end
end

function Missile_Surface_To_Air(CustomGroup, playerID, shipID, ammo_tubes, missile_recharge, missile_reload_max)
--print("Start Script Missile")

-- Algorithm:

-- I want to check if my missile is ready to launch.
-- If it is not, I will add a tick to the reload cycle.
-- I want to check now if I am attacking.
-- If I am attacking, am I attacking a valid target?
-- If I am attacking a valid target, then I will select pseudo-randomly a single target among the valid ones.
-- I will launch my missile.
-- My missile will attack my target.
-- I will update the readiness cycle.
-- Whatever happens, I will update the displayed values for the UI.

-- The variables allowing me to check the current status of the craft.
local b = shipID

local e = ships[b].weapon.missile_reload



-- Is my missile ready to launch?
if e<missile_reload_max then -- my missile is not ready to launch, I will go further in the rearming cycle.

ships[b].weapon.missile_reload = e+1

else -- My missile is ready to launch.

-- Am I attacking anyone?
if (SobGroup_GetCurrentOrder(CustomGroup)==COMMAND_Attack) then

SobGroup_CreateIfNotExist("AttackTargetTempGroup")
SobGroup_Clear("AttackTargetTempGroup")
SobGroup_GetCommandTargets("AttackTargetTempGroup", CustomGroup, COMMAND_Attack) -- This is the SobGroup with the targets of my ship.


SobGroup_CreateIfNotExist("SingleTargetTempGroup")
SobGroup_Clear("SingleTargetTempGroup")
-- I am now checking if my craft is attacking a valid target.
if SobGroup_AreAnyFromTheseAttackFamilies("AttackTargetTempGroup", "Capturer, Fighter, Corvette, Utility, ResourceLarge, Resource")==1 then

NumberInitialTargets=SobGroup_Count("AttackTargetTempGroup") -- Here is how many targets are being attacked.
SobGroup_CreateIfNotExist("ProximityCheckTempGroup")
SobGroup_Clear("ProximityCheckTempGroup")

-- Does my craft attack several targets, needing a selection?
if NumberInitialTargets==1 then -- No, the craft is attacking a single target.

-- Is this single target within firing range?

SobGroup_FillProximitySobGroup("ProximityCheckTempGroup", "AttackTargetTempGroup" , CustomGroup , 4500)
if SobGroup_Count("ProximityCheckTempGroup")==1 then
SobGroup_FillUnion("SingleTargetTempGroup","SingleTargetTempGroup","AttackTargetTempGroup") -- I now have a SobGroup with the desired name and a single target.
end -- This is the end of the if loop checking if the single target is within firing range.

else -- Yes, the craft is attacking several target, I need to select a single one now.

local shipIndex = mod(b/16,NumberInitialTargets)+1 -- This is a pseudo-random starting point for the selection loop, based on the shipID of my craft and the size of the attacked group. Note that the shipID is always a multiple of 16 in game.
local Reference = shipIndex-1 -- This will be my reference to check when I have done a full loop.
local TargetLock = 0 -- This is the variable to check when I have achieved a lock on a valid unit.

while TargetLock<1 do

SobGroup_CreateIfNotExist("SingleTargetCheckGroup")
SobGroup_Clear("SingleTargetCheckGroup")
-- I will create a group with a single ship using the index.
SobGroup_FillShipsByIndexRange("SingleTargetCheckGroup", "AttackTargetTempGroup", shipIndex, 1)

-- Is the target of a correct type?
CheckTargetType = SobGroup_AreAnyFromTheseAttackFamilies("SingleTargetCheckGroup", "Capturer, Fighter, Corvette, Utility, ResourceLarge, Resource")
SobGroup_FillProximitySobGroup("ProximityCheckTempGroup", "SingleTargetCheckGroup" , CustomGroup , 4500)
-- Is the target within firing range?
CheckTargetRange = SobGroup_Count("ProximityCheckTempGroup")

-- Check if both conditions are fit.
if CheckTargetType*CheckTargetRange>0 then

SobGroup_FillUnion("SingleTargetTempGroup","SingleTargetTempGroup","SingleTargetCheckGroup") -- I now have a SobGroup with the desired name and a single target.
TargetLock=1

else

shipIndex=shipIndex+1
if shipIndex>NumberInitialTargets then

shipIndex=1

end

end -- This is the end of the if loop finding whether the currently scanned target is correct in type and range.

if shipIndex-1==Reference then -- I did a full loop.

TargetLock=1 -- Let's stop the loop, with or without target.

end

end -- This is the end of the while loop finding a valid target to attack.

end -- This is the end of the if loop checking if my craft is attacking a single target.


-- I am attacking a valid type of target, and I have gone through the loop checking whether or not I am attacking a valid target and, if so, I now have a dedicated group for a single target.

if SobGroup_Count("SingleTargetTempGroup")==1 then
-- I now have a single good target, I can launch my missile on it.
SobGroup_CreateIfNotExist("MissileTempGroup"..shipID)
SobGroup_Clear("MissileTempGroup"..shipID)

Volume_AddSphere("LaunchPositionVolume", SobGroup_GetPosition(CustomGroup), 0) -- The missile will appear in this location.

for i = 1,ammo_tubes,1 do -- The craft will launch as many missiles at it has tubes.
SobGroup_SpawnNewShipInSobGroup(playerID, "Msl_Spiculim_"..i, "Msl_Spiculim_"..i.."Tir"..shipID..d, "MissileTempGroup"..shipID, "LaunchPositionVolume") --replace [msl_Spiculim]..i by your torpedo unit's ship name
-- The huge amount of parameters for the Squadron Name is to ensure that no two missiles have the same squadron name. I don't know if it can cause issues but I do not want to test.
end -- This is the end of the for loop launching as many missiles as the craft has tubes.

Volume_Delete("LaunchPositionVolume")
SobGroup_ParadeSobGroup("MissileTempGroup"..shipID, CustomGroup, 2) -- We are now teleporting the created missiles to their "launch positions".

local EnemyPlayer = SobGroup_OwnedBy("SingleTargetTempGroup") -- This is the player being attacked.
SobGroup_AvoidanceIgnore("MissileTempGroup"..shipID, "Player_Ships"..playerID)

if EnemyPlayer > -1 then -- Let's avoid some crash.

SobGroup_AvoidanceIgnore("MissileTempGroup"..shipID, "Player_Ships"..EnemyPlayer)

end -- This is the end of the if loop made to avoid a crash during the avoidance ignoring script.

SobGroup_Attack(playerID,"MissileTempGroup"..shipID, "SingleTargetTempGroup") -- The missile will attack the single target.
SobGroup_MakeSelectable("MissileTempGroup"..shipID,0) -- The missile cannot be selected anymore by the player.
SobGroup_Clear("MissileTempGroup"..shipID) -- There is nothing we need to do anymore with the missile.
ships[b].weapon.missile_reload = 0 -- We reset the launch sequence.

end -- This is the end of the if loop checking if my craft is attacking a valid target and can launch the missile.

end -- This is the end of the if loop checking if my craft is attacking a valid type of target.

end -- This is the end of the if loop checking if my craft is attacking anyone.

end -- This is the end of the if loop checking if my missile is ready to launch.



-- Let's update the UI with the remaining missiles.
if SobGroup_Selected(CustomGroup) == 1 then
UI_SetTextLabelText("NewTaskbar", "MaxMissile", ""..d)	
end -- This is the end of the if loop updating the UI.


--print("End Script Missile")
end -- This is the end of the Missile_Air_To_Air function.

-- -- -- -- -- function Missile_Surface_To_Air(CustomGroup, playerID, shipID, ammo_tubes, missile_recharge, missile_reload_max)

-- -- -- -- -- dofilepath("data:scripts/lib/custom/shiptable.lua")

-- -- -- -- -- local b = shipID

-- -- -- -- -- --print("Munitions pour vaisseau :")
-- -- -- -- -- --print(d)


-- -- -- -- -- local e = ships[b].weapon.missile_reload
-- -- -- -- -- if e<missile_reload_max then
-- -- -- -- -- ships[b].weapon.missile_reload = e + missile_recharge -- if my torpedo is not ready, let's keep readying it by adding the ticks
-- -- -- -- -- end

	-- -- -- -- -- if e==missile_reload_max then -- I have torpedoes AND one of them is ready, so we can go with the torpedo attack script
		-- -- -- -- -- if(SobGroup_GetCurrentOrder(CustomGroup)==COMMAND_Attack)then -- Am I actually attacking?
			-- -- -- -- -- SobGroup_CreateIfNotExist("TorpedoAttackerTargetTempGroup")
			-- -- -- -- -- SobGroup_Clear("TorpedoAttackerTargetTempGroup")
			-- -- -- -- -- SobGroup_GetCommandTargets("TorpedoAttackerTargetTempGroup", CustomGroup, COMMAND_Attack) -- Here is my target!
			-- -- -- -- -- if(SobGroup_Count("TorpedoAttackerTargetTempGroup")>0)then -- I am actually attacking someone
				-- -- -- -- -- if(SobGroup_AreAnyFromTheseAttackFamilies("TorpedoAttackerTargetTempGroup", "Capturer, Fighter, Corvette, Utility, ResourceLarge, Resource")==1)then--edit the attack families the attacker can attack, or simply copy this from the attacker's .ship file
					-- -- -- -- -- SobGroup_CreateIfNotExist("TorpedoAttackerTargetTempGroup2") -- So, my target is a big boy, let's arm the torpedo, then!
					-- -- -- -- -- SobGroup_Clear("TorpedoAttackerTargetTempGroup2")
					-- -- -- -- -- SobGroup_FillProximitySobGroup("TorpedoAttackerTargetTempGroup2", "TorpedoAttackerTargetTempGroup" , CustomGroup , 4500)--edit the firerange of the torpedo, how far the attack will fire upon the target
					-- -- -- -- -- if(SobGroup_Empty("TorpedoAttackerTargetTempGroup2")==0)then -- I am in weapon range, my torpedo is ready, let's unleash hell!
						-- -- -- -- -- --print("I'm in range!")
						-- -- -- -- -- SobGroup_CreateIfNotExist("MissileTempGroup"..shipID) -- the torpedo will be there
						-- -- -- -- -- --print("A")
						-- -- -- -- -- SobGroup_Clear("MissileTempGroup"..shipID)
						-- -- -- -- -- --print("B")
						-- -- -- -- -- Volume_AddSphere("AttackerPositionVolume", SobGroup_GetPosition(CustomGroup), 0) -- it will appear in this location
						-- -- -- -- -- --print("C")
						-- -- -- -- -- for i = 1,ammo_tubes,1 do
						-- -- -- -- -- SobGroup_SpawnNewShipInSobGroup(playerID, "Msl_Spiculim_"..i, "Msl_Spiculim_"..i..shipID, "MissileTempGroup"..shipID, "AttackerPositionVolume")--replace [msl_torpedo] by your torpedo unit's ship name
						-- -- -- -- -- --SobGroup_SpawnNewShipInSobGroup(playerID, "msl_torpedo_2", "msl_torpedo_2"..shipID, "MissileTempGroup"..shipID, "AttackerPositionVolume")--replace [msl_torpedo] by your torpedo unit's ship name
						-- -- -- -- -- --SobGroup_AvoidanceIgnore("MissileTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- -- --print("D")
						-- -- -- -- -- end
						-- -- -- -- -- Volume_Delete("AttackerPositionVolume")
						-- -- -- -- -- --print("E")
						-- -- -- -- -- SobGroup_ParadeSobGroup("MissileTempGroup"..shipID, CustomGroup, 2)
						-- -- -- -- -- local ya = SobGroup_OwnedBy("TorpedoAttackerTargetTempGroup")
						-- -- -- -- -- if ya<0 then
						-- -- -- -- -- else
						-- -- -- -- -- SobGroup_AvoidanceIgnore("MissileTempGroup"..shipID, "Player_Ships"..ya)
						-- -- -- -- -- end
						-- -- -- -- -- SobGroup_AvoidanceIgnore("MissileTempGroup"..shipID, "Player_Ships"..playerID)
						-- -- -- -- -- --SobGroup_ParadeSobGroup("MissileTempGroup"..shipID, CustomGroup, 2)
						-- -- -- -- -- --print("F")
						-- -- -- -- -- --SobGroup_Attack(PlayerIndex, "MissileTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- -- --print("G")
						-- -- -- -- -- SobGroup_Attack(playerID,"MissileTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- -- --SobGroup_Kamikaze("MissileTempGroup"..shipID, "TorpedoAttackerTargetTempGroup") -- Tora! Tora! Tora!
						-- -- -- -- -- --print("H")
						-- -- -- -- -- SobGroup_MakeSelectable("MissileTempGroup"..shipID,0) -- Good bye, my little torpedo, you will be missed, but please do not miss.
						-- -- -- -- -- --print("I")
						-- -- -- -- -- SobGroup_Clear("MissileTempGroup"..shipID)
						-- -- -- -- -- ships[b].weapon.missile_reload = 0 -- Yay, let's go through another reload cycle!
						-- -- -- -- -- --print("J")
							-- -- -- -- -- -- aa = GetShipId("msl_torpedo")
						-- -- -- -- -- -- --print("ID du missile tiré")
						-- -- -- -- -- -- --print(aa)
						-- -- -- -- -- -- missile = aa
						-- -- -- -- -- -- target
						-- -- -- -- -- --SobGroup_SetHealth("MissileTempGroup"..shipID, 0)
						-- -- -- -- -- else
						-- -- -- -- -- --print("Almost in range...") -- Are we theeeeeere yet? No, kids.
					-- -- -- -- -- end
				-- -- -- -- -- end
			-- -- -- -- -- end
		-- -- -- -- -- end
	-- -- -- -- -- end
-- -- -- -- -- end


function Missile_Air_To_Air(CustomGroup, playerID, shipID, ammo_max, ammo_tubes, ammo_use, missile_recharge, missile_reload_max)
--print("Start Script Missile")

-- Algorithm:
-- I want to check if I still have any missile left.
-- If I do, then I will go to the following step.
-- I want to check if my missile is ready to launch.
-- If it is not, I will add a tick to the reload cycle.
-- I want to check now if I am attacking.
-- If I am attacking, am I attacking a valid target?
-- If I am attacking a valid target, then I will select pseudo-randomly a single target among the valid ones.
-- I will launch my missile.
-- My missile will attack my target.
-- I will update the readiness cycle and the number of missiles left in my craft.
-- Whatever happens, I will update the displayed values for the UI.

-- The variables allowing me to check the current status of the craft.
local b = shipID
local d = ships[b].weapon.ammo
local e = ships[b].weapon.missile_reload

-- Do I have any missile left?
if d > 0 then

-- Is my missile ready to launch?
if e<missile_reload_max then -- my missile is not ready to launch, I will go further in the rearming cycle.

ships[b].weapon.missile_reload = e+1

else -- My missile is ready to launch.

-- Am I attacking anyone?
if (SobGroup_GetCurrentOrder(CustomGroup)==COMMAND_Attack) then

SobGroup_CreateIfNotExist("AttackTargetTempGroup")
SobGroup_Clear("AttackTargetTempGroup")
SobGroup_GetCommandTargets("AttackTargetTempGroup", CustomGroup, COMMAND_Attack) -- This is the SobGroup with the targets of my ship.


SobGroup_CreateIfNotExist("SingleTargetTempGroup")
SobGroup_Clear("SingleTargetTempGroup")
-- I am now checking if my craft is attacking a valid target.
if SobGroup_AreAnyFromTheseAttackFamilies("AttackTargetTempGroup", "Capturer, Fighter, Corvette, Utility, ResourceLarge, Resource")==1 then

NumberInitialTargets=SobGroup_Count("AttackTargetTempGroup") -- Here is how many targets are being attacked.
SobGroup_CreateIfNotExist("ProximityCheckTempGroup")
SobGroup_Clear("ProximityCheckTempGroup")

-- Does my craft attack several targets, needing a selection?
if NumberInitialTargets==1 then -- No, the craft is attacking a single target.

-- Is this single target within firing range?

SobGroup_FillProximitySobGroup("ProximityCheckTempGroup", "AttackTargetTempGroup" , CustomGroup , 4500)
if SobGroup_Count("ProximityCheckTempGroup")==1 then
SobGroup_FillUnion("SingleTargetTempGroup","SingleTargetTempGroup","AttackTargetTempGroup") -- I now have a SobGroup with the desired name and a single target.
end -- This is the end of the if loop checking if the single target is within firing range.

else -- Yes, the craft is attacking several target, I need to select a single one now.

local shipIndex = mod(b/16,NumberInitialTargets)+1 -- This is a pseudo-random starting point for the selection loop, based on the shipID of my craft and the size of the attacked group. Note that the shipID is always a multiple of 16 in game.
local Reference = shipIndex-1 -- This will be my reference to check when I have done a full loop.
local TargetLock = 0 -- This is the variable to check when I have achieved a lock on a valid unit.

while TargetLock<1 do

SobGroup_CreateIfNotExist("SingleTargetCheckGroup")
SobGroup_Clear("SingleTargetCheckGroup")
-- I will create a group with a single ship using the index.
SobGroup_FillShipsByIndexRange("SingleTargetCheckGroup", "AttackTargetTempGroup", shipIndex, 1)

-- Is the target of a correct type?
CheckTargetType = SobGroup_AreAnyFromTheseAttackFamilies("SingleTargetCheckGroup", "Capturer, Fighter, Corvette, Utility, ResourceLarge, Resource")
SobGroup_FillProximitySobGroup("ProximityCheckTempGroup", "SingleTargetCheckGroup" , CustomGroup , 4500)
-- Is the target within firing range?
CheckTargetRange = SobGroup_Count("ProximityCheckTempGroup")

-- Check if both conditions are fit.
if CheckTargetType*CheckTargetRange>0 then

SobGroup_FillUnion("SingleTargetTempGroup","SingleTargetTempGroup","SingleTargetCheckGroup") -- I now have a SobGroup with the desired name and a single target.
TargetLock=1

else

shipIndex=shipIndex+1
if shipIndex>NumberInitialTargets then

shipIndex=1

end

end -- This is the end of the if loop finding whether the currently scanned target is correct in type and range.

if shipIndex-1==Reference then -- I did a full loop.

TargetLock=1 -- Let's stop the loop, with or without target.

end

end -- This is the end of the while loop finding a valid target to attack.

end -- This is the end of the if loop checking if my craft is attacking a single target.


-- I am attacking a valid type of target, and I have gone through the loop checking whether or not I am attacking a valid target and, if so, I now have a dedicated group for a single target.

if SobGroup_Count("SingleTargetTempGroup")==1 then
-- I now have a single good target, I can launch my missile on it.
SobGroup_CreateIfNotExist("MissileTempGroup"..shipID)
SobGroup_Clear("MissileTempGroup"..shipID)

Volume_AddSphere("LaunchPositionVolume", SobGroup_GetPosition(CustomGroup), 0) -- The missile will appear in this location.

for i = 1,ammo_tubes,1 do -- The craft will launch as many missiles at it has tubes.
SobGroup_SpawnNewShipInSobGroup(playerID, "Msl_Spiculim_"..i, "Msl_Spiculim_"..i.."Tir"..shipID..d, "MissileTempGroup"..shipID, "LaunchPositionVolume") --replace [msl_Spiculim]..i by your torpedo unit's ship name
-- The huge amount of parameters for the Squadron Name is to ensure that no two missiles have the same squadron name. I don't know if it can cause issues but I do not want to test.
end -- This is the end of the for loop launching as many missiles as the craft has tubes.

Volume_Delete("LaunchPositionVolume")
SobGroup_ParadeSobGroup("MissileTempGroup"..shipID, CustomGroup, 2) -- We are now teleporting the created missiles to their "launch positions".

local EnemyPlayer = SobGroup_OwnedBy("SingleTargetTempGroup") -- This is the player being attacked.
SobGroup_AvoidanceIgnore("MissileTempGroup"..shipID, "Player_Ships"..playerID)

if EnemyPlayer > -1 then -- Let's avoid some crash.

SobGroup_AvoidanceIgnore("MissileTempGroup"..shipID, "Player_Ships"..EnemyPlayer)

end -- This is the end of the if loop made to avoid a crash during the avoidance ignoring script.

SobGroup_Attack(playerID,"MissileTempGroup"..shipID, "SingleTargetTempGroup") -- The missile will attack the single target.
SobGroup_MakeSelectable("MissileTempGroup"..shipID,0) -- The missile cannot be selected anymore by the player.
SobGroup_Clear("MissileTempGroup"..shipID) -- There is nothing we need to do anymore with the missile.
ships[b].weapon.missile_reload = 0 -- We reset the launch sequence.
ships[b].weapon.ammo = d-ammo_use -- We remove one or several missiles.

end -- This is the end of the if loop checking if my craft is attacking a valid target and can launch the missile.

end -- This is the end of the if loop checking if my craft is attacking a valid type of target.

end -- This is the end of the if loop checking if my craft is attacking anyone.

end -- This is the end of the if loop checking if my missile is ready to launch.

end -- This is the end of the if loop checking if I have any missile left.

-- Let's update the UI with the remaining missiles.
if SobGroup_Selected(CustomGroup) == 1 then
UI_SetTextLabelText("NewTaskbar", "MaxMissile", ""..d)	
end -- This is the end of the if loop updating the UI.


--print("End Script Missile")
end -- This is the end of the Missile_Air_To_Air function.







-- -- -- -- -- function Missile_Air_To_Air(CustomGroup, playerID, shipID, ammo_max, ammo_tubes, ammo_use, missile_recharge, missile_reload_max)

-- -- -- -- -- dofilepath("data:scripts/lib/custom/shiptable.lua")

-- -- -- -- -- local b = shipID

-- -- -- -- -- local d = ships[b].weapon.ammo
-- -- -- -- -- if SobGroup_Selected(CustomGroup) == 1 then
-- -- -- -- -- UI_SetTextLabelText("NewTaskbar", "MaxMissile", ""..d)	
-- -- -- -- -- end
-- -- -- -- -- --print("Munitions pour vaisseau :")
-- -- -- -- -- --print(d)


-- -- -- -- -- local e = ships[b].weapon.missile_reload
-- -- -- -- -- if e<missile_reload_max then
-- -- -- -- -- ships[b].weapon.missile_reload = e + missile_recharge -- if my torpedo is not ready, let's keep readying it by adding the ticks
-- -- -- -- -- end

	-- -- -- -- -- if d>0 and e==missile_reload_max then -- I have torpedoes AND one of them is ready, so we can go with the torpedo attack script
		-- -- -- -- -- if(SobGroup_GetCurrentOrder(CustomGroup)==COMMAND_Attack)then -- Am I actually attacking?
			-- -- -- -- -- SobGroup_CreateIfNotExist("IncomingTempGroup")
			-- -- -- -- -- SobGroup_Clear("IncomingTempGroup")
			-- -- -- -- -- SobGroup_GetCommandTargets("IncomingTempGroup", CustomGroup, COMMAND_Attack) -- Here is my target!
			-- -- -- -- -- if(SobGroup_Count("IncomingTempGroup")>0)then -- I am actually attacking someone
			
			-- -- -- -- -- -- Script de sélection de cible unique.
			
			-- -- -- -- -- local shipCount = SobGroup_Count("IncomingTempGroup")
			-- -- -- -- -- local shipIndex = mod(b,shipCount)+1 -- sélection pseudo-aléatoire des cibles du groupe
			-- -- -- -- -- local DeLock = shipIndex -- Cette variable va me permettre de détecter si je n'ai aucune cible verrouillable.
			-- -- -- -- -- -- print("Variables :")
			-- -- -- -- -- -- print(shipCount)
			-- -- -- -- -- -- print(shipIndex)
			-- -- -- -- -- -- print(DeLock)
			-- -- -- -- -- local verrouillage = 0
			-- -- -- -- -- --while (shipIndex < shipCount) do
			-- -- -- -- -- while verrouillage < 1 do
			-- -- -- -- -- -- print("Test :")
			-- -- -- -- -- -- print(verrouillage)
			-- -- -- -- -- -- print(shipIndex)
			-- -- -- -- -- --print("Inbound missile #")
			-- -- -- -- -- --print(shipIndex)
				-- -- -- -- -- SobGroup_CreateIfNotExist("MissileCheck")
				-- -- -- -- -- SobGroup_Clear("MissileCheck")
				-- -- -- -- -- SobGroup_FillShipsByIndexRange("MissileCheck", "IncomingTempGroup", shipIndex, 1) -- Je crée un groupe individuel appelé MissileCheck et contenant le vaisseau numéro shipIndex de IncomingTempGroup.
				-- -- -- -- -- SobGroup_CreateIfNotExist("ProximityCheckTempGroup")
				-- -- -- -- -- SobGroup_Clear("ProximityCheckTempGroup")
				-- -- -- -- -- SobGroup_FillProximitySobGroup("ProximityCheckTempGroup", "MissileCheck" , CustomGroup , 4500)
				-- -- -- -- -- -- print(SobGroup_Count("IncomingTempGroup"))
				-- -- -- -- -- -- print(SobGroup_Count("ProximityCheckTempGroup"))
				-- -- -- -- -- -- print(SobGroup_AreAnyFromTheseAttackFamilies("MissileCheck", "Capturer, Fighter, Corvette, Utility, ResourceLarge, Resource"))
				-- -- -- -- -- shipIndex = shipIndex + 1 -- Je me prépare à tester une cible suivante
				
				-- -- -- -- -- if shipIndex>shipCount then
				-- -- -- -- -- shipIndex=shipIndex-shipCount			
				-- -- -- -- -- end
				
				-- -- -- -- -- if (SobGroup_Count("ProximityCheckTempGroup")*SobGroup_AreAnyFromTheseAttackFamilies("MissileCheck", "Capturer, Fighter, Corvette, Utility, ResourceLarge, Resource") > 0) then
				
					-- -- -- -- -- verrouillage = 1 -- J'ai une cible correcte et à portée de tir, on peut passer à la suite !
					
				-- -- -- -- -- end

				-- -- -- -- -- if shipIndex == DeLock and verrouillage < 1 then -- Je suis revenu à la case départ sans avoir trouvé de cible correcte.
					-- -- -- -- -- SobGroup_Clear("MissileCheck")
					-- -- -- -- -- verrouillage = 1 -- Je n'ai pas trouvé de cible correcte après avoir vérifié chaque cible potentielle et on passe à la suite avec un groupe de verrouillage vide.
				-- -- -- -- -- end
				
			-- -- -- -- -- end
			
			-- -- -- -- -- -- Script de sélection de cible unique.
			
			
			
				-- -- -- -- -- --if(SobGroup_AreAnyFromTheseAttackFamilies("TorpedoAttackerTargetTempGroup", "Capturer, Fighter, Corvette, Utility, ResourceLarge, Resource")==1)then--edit the attack families the attacker can attack, or simply copy this from the attacker's .ship file
					-- -- -- -- -- --SobGroup_CreateIfNotExist("TorpedoAttackerTargetTempGroup2") -- So, my target is a big boy, let's arm the torpedo, then!
					-- -- -- -- -- --SobGroup_Clear("TorpedoAttackerTargetTempGroup2")
					-- -- -- -- -- --SobGroup_FillProximitySobGroup("TorpedoAttackerTargetTempGroup2", "TorpedoAttackerTargetTempGroup" , CustomGroup , 4500)--edit the firerange of the torpedo, how far the attack will fire upon the target
					-- -- -- -- -- if(SobGroup_Empty("MissileCheck")==0)then -- I am in weapon range, my torpedo is ready, let's unleash hell!
						-- -- -- -- -- --print("I'm in range!")
						-- -- -- -- -- SobGroup_CreateIfNotExist("MissileTempGroup"..shipID) -- the torpedo will be there
						-- -- -- -- -- --print("A")
						-- -- -- -- -- SobGroup_Clear("MissileTempGroup"..shipID)
						-- -- -- -- -- --print("B")
						-- -- -- -- -- Volume_AddSphere("AttackerPositionVolume", SobGroup_GetPosition(CustomGroup), 0) -- it will appear in this location
						-- -- -- -- -- --print("C")
						-- -- -- -- -- for i = 1,ammo_tubes,1 do
						-- -- -- -- -- SobGroup_SpawnNewShipInSobGroup(playerID, "Msl_Spiculim_"..i, "Msl_Spiculim_"..i..shipID, "MissileTempGroup"..shipID, "AttackerPositionVolume")--replace [msl_torpedo] by your torpedo unit's ship name
						-- -- -- -- -- --SobGroup_SpawnNewShipInSobGroup(playerID, "msl_torpedo_2", "msl_torpedo_2"..shipID, "MissileTempGroup"..shipID, "AttackerPositionVolume")--replace [msl_torpedo] by your torpedo unit's ship name
						-- -- -- -- -- --SobGroup_AvoidanceIgnore("MissileTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- -- --print("D")
						-- -- -- -- -- end
						-- -- -- -- -- Volume_Delete("AttackerPositionVolume")
						-- -- -- -- -- --print("E")
						-- -- -- -- -- SobGroup_ParadeSobGroup("MissileTempGroup"..shipID, CustomGroup, 2)
						-- -- -- -- -- local ya = SobGroup_OwnedBy("MissileCheck")
						-- -- -- -- -- if ya<0 then
						-- -- -- -- -- else
						-- -- -- -- -- SobGroup_AvoidanceIgnore("MissileTempGroup"..shipID, "Player_Ships"..ya)
						-- -- -- -- -- end
						-- -- -- -- -- SobGroup_AvoidanceIgnore("MissileTempGroup"..shipID, "Player_Ships"..playerID)
						-- -- -- -- -- --SobGroup_ParadeSobGroup("MissileTempGroup"..shipID, CustomGroup, 2)
						-- -- -- -- -- --print("F")
						-- -- -- -- -- --SobGroup_Attack(PlayerIndex, "MissileTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- -- --print("G")
						-- -- -- -- -- SobGroup_Attack(playerID,"MissileTempGroup"..shipID, "MissileCheck")
						-- -- -- -- -- --SobGroup_Kamikaze("MissileTempGroup"..shipID, "TorpedoAttackerTargetTempGroup") -- Tora! Tora! Tora!
						-- -- -- -- -- --print("H")
						-- -- -- -- -- SobGroup_MakeSelectable("MissileTempGroup"..shipID,0) -- Good bye, my little torpedo, you will be missed, but please do not miss.
						-- -- -- -- -- --print("I")
						-- -- -- -- -- SobGroup_Clear("MissileTempGroup"..shipID)
						-- -- -- -- -- ships[b].weapon.missile_reload = 0 -- Yay, let's go through another reload cycle!
						-- -- -- -- -- --print("J")
							-- -- -- -- -- -- aa = GetShipId("msl_torpedo")
						-- -- -- -- -- -- --print("ID du missile tiré")
						-- -- -- -- -- -- --print(aa)
						-- -- -- -- -- -- missile = aa
						-- -- -- -- -- -- target
						-- -- -- -- -- local b = shipID
						-- -- -- -- -- ships[b].weapon.ammo = d-ammo_use -- let's consume a torpedo from the reserve
						-- -- -- -- -- --SobGroup_SetHealth("MissileTempGroup"..shipID, 0)
						-- -- -- -- -- else
						-- -- -- -- -- --print("Almost in range...") -- Are we theeeeeere yet? No, kids.
					-- -- -- -- -- end
				-- -- -- -- -- --end
			-- -- -- -- -- end
		-- -- -- -- -- end
	-- -- -- -- -- end
-- -- -- -- -- end

function Torpedo_Strikecraft(CustomGroup, playerID, shipID, torpedo_max, torpedo_tubes, torpedo_use, torpedo_recharge, torpedo_reload_max)
--print("Start Script Torpedo")

-- Algorithm:
-- I want to check if I still have any torpedo left.
-- If I do, then I will go to the following step.
-- I want to check if my torpedo is ready to launch.
-- If it is not, I will add a tick to the reload cycle.
-- I want to check now if I am attacking.
-- If I am attacking, am I attacking a valid target?
-- If I am attacking a valid target, then I will select pseudo-randomly a single target among the valid ones.
-- I will launch my torpedo.
-- My torpedo will attack my target.
-- I will update the readiness cycle and the number of torpedoes left in my craft.
-- Whatever happens, I will update the displayed values for the UI.

-- The variables allowing me to check the current status of the craft.
local b = shipID

if torpedo_max == 0 then
if SobGroup_Selected(CustomGroup) == 1 then
UI_SetTextLabelText("NewTaskbar", "MaxTorpedo", "NO")
end
else
local f = ships[b].weapon.torpedo
local e = ships[b].weapon.torpedo_reload

-- Do I have any torpedo left?
if f > 0 then
ships[b].weapon.RTB = 1
-- Is my torpedo ready to launch?
if e<torpedo_reload_max then -- my torpedo is not ready to launch, I will go further in the rearming cycle.

ships[b].weapon.torpedo_reload = e+1

else -- My torpedo is ready to launch.

-- Am I attacking anyone?
if (SobGroup_GetCurrentOrder(CustomGroup)==COMMAND_Attack) then

SobGroup_CreateIfNotExist("AttackTargetTempGroup")
SobGroup_Clear("AttackTargetTempGroup")
SobGroup_GetCommandTargets("AttackTargetTempGroup", CustomGroup, COMMAND_Attack) -- This is the SobGroup with the targets of my ship.


SobGroup_CreateIfNotExist("SingleTargetTempGroup")
SobGroup_Clear("SingleTargetTempGroup")
-- I am now checking if my craft is attacking a valid target.
if SobGroup_AreAnyFromTheseAttackFamilies("AttackTargetTempGroup", "Capturer, Frigate, SmallCapitalShip, BigCapitalShip, Mothership, Utility, ResourceLarge")==1 then

NumberInitialTargets=SobGroup_Count("AttackTargetTempGroup") -- Here is how many targets are being attacked.
SobGroup_CreateIfNotExist("ProximityCheckTempGroup")
SobGroup_Clear("ProximityCheckTempGroup")

-- Does my craft attack several targets, needing a selection?
if NumberInitialTargets==1 then -- No, the craft is attacking a single target.

-- Is this single target within firing range?

SobGroup_FillProximitySobGroup("ProximityCheckTempGroup", "AttackTargetTempGroup" , CustomGroup , 4500)
if SobGroup_Count("ProximityCheckTempGroup")==1 then
SobGroup_FillUnion("SingleTargetTempGroup","SingleTargetTempGroup","AttackTargetTempGroup") -- I now have a SobGroup with the desired name and a single target.
end -- This is the end of the if loop checking if the single target is within firing range.

else -- Yes, the craft is attacking several target, I need to select a single one now.

local shipIndex = mod(b/16,NumberInitialTargets)+1 -- This is a pseudo-random starting point for the selection loop, based on the shipID of my craft and the size of the attacked group. Note that the shipID is always a multiple of 16 in game.
local Reference = shipIndex-1 -- This will be my reference to check when I have done a full loop.
local TargetLock = 0 -- This is the variable to check when I have achieved a lock on a valid unit.

while TargetLock<1 do

SobGroup_CreateIfNotExist("SingleTargetCheckGroup")
SobGroup_Clear("SingleTargetCheckGroup")
-- I will create a group with a single ship using the index.
SobGroup_FillShipsByIndexRange("SingleTargetCheckGroup", "AttackTargetTempGroup", shipIndex, 1)

-- Is the target of a correct type?
CheckTargetType = SobGroup_AreAnyFromTheseAttackFamilies("SingleTargetCheckGroup", "Capturer, Frigate, SmallCapitalShip, BigCapitalShip, Mothership, Utility, ResourceLarge")
SobGroup_FillProximitySobGroup("ProximityCheckTempGroup", "SingleTargetCheckGroup" , CustomGroup , 4500)
-- Is the target within firing range?
CheckTargetRange = SobGroup_Count("ProximityCheckTempGroup")

-- Check if both conditions are fit.
if CheckTargetType*CheckTargetRange>0 then

SobGroup_FillUnion("SingleTargetTempGroup","SingleTargetTempGroup","SingleTargetCheckGroup") -- I now have a SobGroup with the desired name and a single target.
TargetLock=1

else

shipIndex=shipIndex+1
if shipIndex>NumberInitialTargets then

shipIndex=1

end

end -- This is the end of the if loop finding whether the currently scanned target is correct in type and range.

if shipIndex-1==Reference then -- I did a full loop.

TargetLock=1 -- Let's stop the loop, with or without target.

end

end -- This is the end of the while loop finding a valid target to attack.

end -- This is the end of the if loop checking if my craft is attacking a single target.


-- I am attacking a valid type of target, and I have gone through the loop checking whether or not I am attacking a valid target and, if so, I now have a dedicated group for a single target.

if SobGroup_Count("SingleTargetTempGroup")==1 then
-- I now have a single good target, I can launch my torpedo on it.
SobGroup_CreateIfNotExist("torpedoTempGroup"..shipID)
SobGroup_Clear("torpedoTempGroup"..shipID)

Volume_AddSphere("LaunchPositionVolume", SobGroup_GetPosition(CustomGroup), 0) -- The torpedo will appear in this location.

for i = 1,ammo_tubes,1 do -- The craft will launch as many torpedos at it has tubes.
SobGroup_SpawnNewShipInSobGroup(playerID, "msl_torpedo_"..i, "msl_torpedo_"..i.."Tir"..shipID..d, "torpedoTempGroup"..shipID, "LaunchPositionVolume") --replace [msl_Spiculim]..i by your torpedo unit's ship name
-- The huge amount of parameters for the Squadron Name is to ensure that no two torpedos have the same squadron name. I don't know if it can cause issues but I do not want to test.
end -- This is the end of the for loop launching as many torpedos as the craft has tubes.

Volume_Delete("LaunchPositionVolume")
SobGroup_ParadeSobGroup("torpedoTempGroup"..shipID, CustomGroup, 2) -- We are now teleporting the created torpedos to their "launch positions".

local EnemyPlayer = SobGroup_OwnedBy("SingleTargetTempGroup") -- This is the player being attacked.
SobGroup_AvoidanceIgnore("torpedoTempGroup"..shipID, "Player_Ships"..playerID)

if EnemyPlayer > -1 then -- Let's avoid some crash.

SobGroup_AvoidanceIgnore("torpedoTempGroup"..shipID, "Player_Ships"..EnemyPlayer)

end -- This is the end of the if loop made to avoid a crash during the avoidance ignoring script.

SobGroup_Attack(playerID,"torpedoTempGroup"..shipID, "SingleTargetTempGroup") -- The torpedo will attack the single target.
SobGroup_MakeSelectable("torpedoTempGroup"..shipID,0) -- The torpedo cannot be selected anymore by the player.
SobGroup_Clear("torpedoTempGroup"..shipID) -- There is nothing we need to do anymore with the torpedo.
ships[b].weapon.torpedo_reload = 0 -- We reset the launch sequence.
ships[b].weapon.torpedo = f-torpedo_use -- We remove one or several torpedos.

end -- This is the end of the if loop checking if my craft is attacking a valid target and can launch the torpedo.

end -- This is the end of the if loop checking if my craft is attacking a valid type of target.

end -- This is the end of the if loop checking if my craft is attacking anyone.

end -- This is the end of the if loop checking if my torpedo is ready to launch.

else

ships[b].weapon.RTB = 0 -- I am Winchester, out of ammunition, let's Return To Base

end -- This is the end of the if loop checking if I have any torpedo left.

-- Let's update the UI with the remaining torpedos.
if SobGroup_Selected(CustomGroup) == 1 then
UI_SetTextLabelText("NewTaskbar", "MaxTorpedo", ""..f)	
end -- This is the end of the if loop updating the UI.

end -- This is the end of the if loop checking if I actually am a torpedo bomber.
--print("End Script torpedo")
end -- This is the end of the Torpedo_Strikecraft function.

-- -- -- -- -- function Torpedo_Strikecraft(CustomGroup, playerID, shipID, torpedo_max, torpedo_tubes, torpedo_use, torpedo_recharge, torpedo_reload_max)

-- -- -- -- -- dofilepath("data:scripts/lib/custom/shiptable.lua")

-- -- -- -- -- local b = shipID

-- -- -- -- -- if torpedo_max == 0 then
-- -- -- -- -- if SobGroup_Selected(CustomGroup) == 1 then
-- -- -- -- -- UI_SetTextLabelText("NewTaskbar", "MaxTorpedo", "NO")
-- -- -- -- -- end
-- -- -- -- -- else
-- -- -- -- -- --print("Torpilles pour vaisseau :")
-- -- -- -- -- --print(f)
-- -- -- -- -- local f = ships[b].weapon.torpedo
-- -- -- -- -- if SobGroup_Selected(CustomGroup) == 1 and torpedo_max > -10 then
-- -- -- -- -- UI_SetTextLabelText("NewTaskbar", "MaxTorpedo", ""..f)
-- -- -- -- -- end

-- -- -- -- -- -- Checking if we are out of torpedoes (if we are out of missiles, it's less important).

-- -- -- -- -- if f>0 then
	-- -- -- -- -- ships[b].weapon.RTB = 1 -- I still have torpedoes
-- -- -- -- -- else
	-- -- -- -- -- ships[b].weapon.RTB = 0 -- I am Winchester, out of ammunition, let's Return To Base
-- -- -- -- -- end

-- -- -- -- -- local h = ships[b].weapon.torpedo_reload
-- -- -- -- -- if h<torpedo_reload_max then
-- -- -- -- -- ships[b].weapon.torpedo_reload = h + torpedo_recharge -- if my torpedo is not ready, let's keep readying it by adding the ticks
-- -- -- -- -- end
-- -- -- -- -- --print("Cycle de rechargement torpille pour vaisseau :")
-- -- -- -- -- --print(h)

	-- -- -- -- -- if f>0 and h==torpedo_reload_max then -- I have torpedoes AND one of them is ready, so we can go with the torpedo attack script
		-- -- -- -- -- if(SobGroup_GetCurrentOrder(CustomGroup)==COMMAND_Attack)then -- Am I actually attacking?
			-- -- -- -- -- SobGroup_CreateIfNotExist("TorpedoAttackerTargetTempGroup")
			-- -- -- -- -- SobGroup_Clear("TorpedoAttackerTargetTempGroup")
			-- -- -- -- -- SobGroup_GetCommandTargets("TorpedoAttackerTargetTempGroup", CustomGroup, COMMAND_Attack) -- Here is my target!
			-- -- -- -- -- if(SobGroup_Count("TorpedoAttackerTargetTempGroup")>0)then -- I am actually attacking someone
				-- -- -- -- -- if(SobGroup_AreAnyFromTheseAttackFamilies("TorpedoAttackerTargetTempGroup", "Capturer, Frigate, SmallCapitalShip, BigCapitalShip, Mothership, Utility, ResourceLarge")==1)then--edit the attack families the attacker can attack, or simply copy this from the attacker's .ship file
					-- -- -- -- -- SobGroup_CreateIfNotExist("TorpedoAttackerTargetTempGroup2") -- So, my target is a big boy, let's arm the torpedo, then!
					-- -- -- -- -- SobGroup_Clear("TorpedoAttackerTargetTempGroup2")
					-- -- -- -- -- SobGroup_FillProximitySobGroup("TorpedoAttackerTargetTempGroup2", "TorpedoAttackerTargetTempGroup" , CustomGroup , 2500)--edit the firerange of the torpedo, how far the attack will fire upon the target
					-- -- -- -- -- if(SobGroup_Empty("TorpedoAttackerTargetTempGroup2")==0)then -- I am in weapon range, my torpedo is ready, let's unleash hell!
						-- -- -- -- -- --print("I'm in range!")
						-- -- -- -- -- SobGroup_CreateIfNotExist("TorpedoTempGroup"..shipID) -- the torpedo will be there
						-- -- -- -- -- --print("A")
						-- -- -- -- -- SobGroup_Clear("TorpedoTempGroup"..shipID)
						-- -- -- -- -- --print("B")
						-- -- -- -- -- Volume_AddSphere("AttackerPositionVolume", SobGroup_GetPosition(CustomGroup), 0) -- it will appear in this location
						-- -- -- -- -- --print("C")
						-- -- -- -- -- local j = 1
						-- -- -- -- -- for j = 1,torpedo_tubes,1 do
						-- -- -- -- -- SobGroup_SpawnNewShipInSobGroup(playerID, "msl_torpedo_"..j, "msl_torpedo_"..j..shipID, "TorpedoTempGroup"..shipID, "AttackerPositionVolume")--replace [msl_torpedo] by your torpedo unit's ship name
						-- -- -- -- -- --SobGroup_AvoidanceIgnore("TorpedoTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- -- end
						-- -- -- -- -- --print("D")
						-- -- -- -- -- Volume_Delete("AttackerPositionVolume")
						-- -- -- -- -- --print("E")
						-- -- -- -- -- SobGroup_ParadeSobGroup("TorpedoTempGroup"..shipID, CustomGroup, 2)
						-- -- -- -- -- --SobGroup_ParadeSobGroup("TorpedoTempGroup"..shipID, CustomGroup, 2)
						-- -- -- -- -- --print("F")
						-- -- -- -- -- --SobGroup_Attack(PlayerIndex, "TorpedoTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- -- --print("G")
						-- -- -- -- -- SobGroup_Attack(playerID,"TorpedoTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- -- --SobGroup_Kamikaze("TorpedoTempGroup"..shipID, "TorpedoAttackerTargetTempGroup") -- Tora! Tora! Tora!
						-- -- -- -- -- --print("H")
						-- -- -- -- -- SobGroup_MakeSelectable("TorpedoTempGroup"..shipID,0) -- Good bye, my little torpedo, you will be missed, but please do not miss.
						-- -- -- -- -- --print("I")
						-- -- -- -- -- ships[b].weapon.torpedo_reload = 0 -- Yay, let's go through another reload cycle!
						-- -- -- -- -- --print("J")
							-- -- -- -- -- -- aa = GetShipId("msl_torpedo")
						-- -- -- -- -- -- --print("ID du missile tiré")
						-- -- -- -- -- -- --print(aa)
						-- -- -- -- -- -- missile = aa
						-- -- -- -- -- -- target
						-- -- -- -- -- if torpedo_max == -10 then
						-- -- -- -- -- else
						-- -- -- -- -- ships[b].weapon.torpedo = f-torpedo_use -- let's consume a torpedo from the reserve
						-- -- -- -- -- end
						-- -- -- -- -- --SobGroup_SetHealth("TorpedoTempGroup"..shipID, 0)
						-- -- -- -- -- else
						-- -- -- -- -- --print("Almost in range...") -- Are we theeeeeere yet? No, kids.
					-- -- -- -- -- end
				-- -- -- -- -- end
			-- -- -- -- -- end
		-- -- -- -- -- end
	-- -- -- -- -- end

-- -- -- -- -- end

-- -- -- -- -- end

function Torpedo_Epee_Strikecraft(CustomGroup, playerID, shipID, torpedo_max, torpedo_tubes, torpedo_use, torpedo_recharge, torpedo_reload_max)
--print("Start Script Torpedo")

-- Algorithm:
-- I want to check if I still have any torpedo left.
-- If I do, then I will go to the following step.
-- I want to check if my torpedo is ready to launch.
-- If it is not, I will add a tick to the reload cycle.
-- I want to check now if I am attacking.
-- If I am attacking, am I attacking a valid target?
-- If I am attacking a valid target, then I will select pseudo-randomly a single target among the valid ones.
-- I will launch my torpedo.
-- My torpedo will attack my target.
-- I will update the readiness cycle and the number of torpedoes left in my craft.
-- Whatever happens, I will update the displayed values for the UI.

-- The variables allowing me to check the current status of the craft.
local b = shipID

if torpedo_max == 0 then
if SobGroup_Selected(CustomGroup) == 1 then
UI_SetTextLabelText("NewTaskbar", "MaxTorpedo", "NO")
end
else
local f = ships[b].weapon.torpedo
local e = ships[b].weapon.torpedo_reload

-- Do I have any torpedo left?
if f > 0 then
ships[b].weapon.RTB = 1
-- Is my torpedo ready to launch?
if e<torpedo_reload_max then -- my torpedo is not ready to launch, I will go further in the rearming cycle.

ships[b].weapon.torpedo_reload = e+1

else -- My torpedo is ready to launch.

-- Am I attacking anyone?
if (SobGroup_GetCurrentOrder(CustomGroup)==COMMAND_Attack) then

SobGroup_CreateIfNotExist("AttackTargetTempGroup")
SobGroup_Clear("AttackTargetTempGroup")
SobGroup_GetCommandTargets("AttackTargetTempGroup", CustomGroup, COMMAND_Attack) -- This is the SobGroup with the targets of my ship.


SobGroup_CreateIfNotExist("SingleTargetTempGroup")
SobGroup_Clear("SingleTargetTempGroup")
-- I am now checking if my craft is attacking a valid target.
if SobGroup_AreAnyFromTheseAttackFamilies("AttackTargetTempGroup", "Capturer, Frigate, SmallCapitalShip, BigCapitalShip, Mothership, Utility, ResourceLarge")==1 then

NumberInitialTargets=SobGroup_Count("AttackTargetTempGroup") -- Here is how many targets are being attacked.
SobGroup_CreateIfNotExist("ProximityCheckTempGroup")
SobGroup_Clear("ProximityCheckTempGroup")

-- Does my craft attack several targets, needing a selection?
if NumberInitialTargets==1 then -- No, the craft is attacking a single target.

-- Is this single target within firing range?

SobGroup_FillProximitySobGroup("ProximityCheckTempGroup", "AttackTargetTempGroup" , CustomGroup , 4500)
if SobGroup_Count("ProximityCheckTempGroup")==1 then
SobGroup_FillUnion("SingleTargetTempGroup","SingleTargetTempGroup","AttackTargetTempGroup") -- I now have a SobGroup with the desired name and a single target.
end -- This is the end of the if loop checking if the single target is within firing range.

else -- Yes, the craft is attacking several target, I need to select a single one now.

local shipIndex = mod(b/16,NumberInitialTargets)+1 -- This is a pseudo-random starting point for the selection loop, based on the shipID of my craft and the size of the attacked group. Note that the shipID is always a multiple of 16 in game.
local Reference = shipIndex-1 -- This will be my reference to check when I have done a full loop.
local TargetLock = 0 -- This is the variable to check when I have achieved a lock on a valid unit.

while TargetLock<1 do

SobGroup_CreateIfNotExist("SingleTargetCheckGroup")
SobGroup_Clear("SingleTargetCheckGroup")
-- I will create a group with a single ship using the index.
SobGroup_FillShipsByIndexRange("SingleTargetCheckGroup", "AttackTargetTempGroup", shipIndex, 1)

-- Is the target of a correct type?
CheckTargetType = SobGroup_AreAnyFromTheseAttackFamilies("SingleTargetCheckGroup", "Capturer, Frigate, SmallCapitalShip, BigCapitalShip, Mothership, Utility, ResourceLarge")
SobGroup_FillProximitySobGroup("ProximityCheckTempGroup", "SingleTargetCheckGroup" , CustomGroup , 4500)
-- Is the target within firing range?
CheckTargetRange = SobGroup_Count("ProximityCheckTempGroup")

-- Check if both conditions are fit.
if CheckTargetType*CheckTargetRange>0 then

SobGroup_FillUnion("SingleTargetTempGroup","SingleTargetTempGroup","SingleTargetCheckGroup") -- I now have a SobGroup with the desired name and a single target.
TargetLock=1

else

shipIndex=shipIndex+1
if shipIndex>NumberInitialTargets then

shipIndex=1

end

end -- This is the end of the if loop finding whether the currently scanned target is correct in type and range.

if shipIndex-1==Reference then -- I did a full loop.

TargetLock=1 -- Let's stop the loop, with or without target.

end

end -- This is the end of the while loop finding a valid target to attack.

end -- This is the end of the if loop checking if my craft is attacking a single target.


-- I am attacking a valid type of target, and I have gone through the loop checking whether or not I am attacking a valid target and, if so, I now have a dedicated group for a single target.

if SobGroup_Count("SingleTargetTempGroup")==1 then
-- I now have a single good target, I can launch my torpedo on it.
SobGroup_CreateIfNotExist("torpedoTempGroup"..shipID)
SobGroup_Clear("torpedoTempGroup"..shipID)

Volume_AddSphere("LaunchPositionVolume", SobGroup_GetPosition(CustomGroup), 0) -- The torpedo will appear in this location.

for i = 1,ammo_tubes,1 do -- The craft will launch as many torpedos at it has tubes.
SobGroup_SpawnNewShipInSobGroup(playerID, "msl_light_torpedo_"..i, "msl_light_torpedo_"..i.."Tir"..shipID..d, "torpedoTempGroup"..shipID, "LaunchPositionVolume") --replace [msl_Spiculim]..i by your torpedo unit's ship name
-- The huge amount of parameters for the Squadron Name is to ensure that no two torpedos have the same squadron name. I don't know if it can cause issues but I do not want to test.
end -- This is the end of the for loop launching as many torpedos as the craft has tubes.

Volume_Delete("LaunchPositionVolume")
SobGroup_ParadeSobGroup("torpedoTempGroup"..shipID, CustomGroup, 2) -- We are now teleporting the created torpedos to their "launch positions".

local EnemyPlayer = SobGroup_OwnedBy("SingleTargetTempGroup") -- This is the player being attacked.
SobGroup_AvoidanceIgnore("torpedoTempGroup"..shipID, "Player_Ships"..playerID)

if EnemyPlayer > -1 then -- Let's avoid some crash.

SobGroup_AvoidanceIgnore("torpedoTempGroup"..shipID, "Player_Ships"..EnemyPlayer)

end -- This is the end of the if loop made to avoid a crash during the avoidance ignoring script.

SobGroup_Attack(playerID,"torpedoTempGroup"..shipID, "SingleTargetTempGroup") -- The torpedo will attack the single target.
SobGroup_MakeSelectable("torpedoTempGroup"..shipID,0) -- The torpedo cannot be selected anymore by the player.
SobGroup_Clear("torpedoTempGroup"..shipID) -- There is nothing we need to do anymore with the torpedo.
ships[b].weapon.torpedo_reload = 0 -- We reset the launch sequence.
ships[b].weapon.torpedo = f-torpedo_use -- We remove one or several torpedos.

end -- This is the end of the if loop checking if my craft is attacking a valid target and can launch the torpedo.

end -- This is the end of the if loop checking if my craft is attacking a valid type of target.

end -- This is the end of the if loop checking if my craft is attacking anyone.

end -- This is the end of the if loop checking if my torpedo is ready to launch.

else

ships[b].weapon.RTB = 0 -- I am Winchester, out of ammunition, let's Return To Base

end -- This is the end of the if loop checking if I have any torpedo left.

-- Let's update the UI with the remaining torpedos.
if SobGroup_Selected(CustomGroup) == 1 then
UI_SetTextLabelText("NewTaskbar", "MaxTorpedo", ""..f)	
end -- This is the end of the if loop updating the UI.

end -- This is the end of the if loop checking if I actually am a torpedo bomber.
--print("End Script torpedo")
end -- This is the end of the Torpedo_Strikecraft function.

-- -- -- -- function Torpedo_Epee_Strikecraft(CustomGroup, playerID, shipID, torpedo_max, torpedo_tubes, torpedo_use, torpedo_recharge, torpedo_reload_max)

-- -- -- -- dofilepath("data:scripts/lib/custom/shiptable.lua")

-- -- -- -- local b = shipID

-- -- -- -- if torpedo_max == 0 then
-- -- -- -- if SobGroup_Selected(CustomGroup) == 1 then
-- -- -- -- UI_SetTextLabelText("NewTaskbar", "MaxTorpedo", "NO")
-- -- -- -- end
-- -- -- -- else
-- -- -- -- --print("Torpilles pour vaisseau :")
-- -- -- -- --print(f)
-- -- -- -- local f = ships[b].weapon.torpedo
-- -- -- -- if SobGroup_Selected(CustomGroup) == 1 and torpedo_max > -10 then
-- -- -- -- UI_SetTextLabelText("NewTaskbar", "MaxTorpedo", ""..f)
-- -- -- -- end

-- -- -- -- -- Checking if we are out of torpedoes (if we are out of missiles, it's less important).

-- -- -- -- if f>0 then
	-- -- -- -- ships[b].weapon.RTB = 1 -- I still have torpedoes
-- -- -- -- else
	-- -- -- -- ships[b].weapon.RTB = 0 -- I am Winchester, out of ammunition, let's Return To Base
-- -- -- -- end

-- -- -- -- local h = ships[b].weapon.torpedo_reload
-- -- -- -- if h<torpedo_reload_max then
-- -- -- -- ships[b].weapon.torpedo_reload = h + torpedo_recharge -- if my torpedo is not ready, let's keep readying it by adding the ticks
-- -- -- -- end
-- -- -- -- --print("Cycle de rechargement torpille pour vaisseau :")
-- -- -- -- --print(h)

	-- -- -- -- if f>0 and h==torpedo_reload_max then -- I have torpedoes AND one of them is ready, so we can go with the torpedo attack script
		-- -- -- -- if(SobGroup_GetCurrentOrder(CustomGroup)==COMMAND_Attack)then -- Am I actually attacking?
			-- -- -- -- SobGroup_CreateIfNotExist("TorpedoAttackerTargetTempGroup")
			-- -- -- -- SobGroup_Clear("TorpedoAttackerTargetTempGroup")
			-- -- -- -- SobGroup_GetCommandTargets("TorpedoAttackerTargetTempGroup", CustomGroup, COMMAND_Attack) -- Here is my target!
			-- -- -- -- if(SobGroup_Count("TorpedoAttackerTargetTempGroup")>0)then -- I am actually attacking someone
				-- -- -- -- if(SobGroup_AreAnyFromTheseAttackFamilies("TorpedoAttackerTargetTempGroup", "Capturer, Frigate, SmallCapitalShip, BigCapitalShip, Mothership, Utility, ResourceLarge")==1)then--edit the attack families the attacker can attack, or simply copy this from the attacker's .ship file
					-- -- -- -- SobGroup_CreateIfNotExist("TorpedoAttackerTargetTempGroup2") -- So, my target is a big boy, let's arm the torpedo, then!
					-- -- -- -- SobGroup_Clear("TorpedoAttackerTargetTempGroup2")
					-- -- -- -- SobGroup_FillProximitySobGroup("TorpedoAttackerTargetTempGroup2", "TorpedoAttackerTargetTempGroup" , CustomGroup , 2500)--edit the firerange of the torpedo, how far the attack will fire upon the target
					-- -- -- -- if(SobGroup_Empty("TorpedoAttackerTargetTempGroup2")==0)then -- I am in weapon range, my torpedo is ready, let's unleash hell!
						-- -- -- -- --print("I'm in range!")
						-- -- -- -- SobGroup_CreateIfNotExist("TorpedoTempGroup"..shipID) -- the torpedo will be there
						-- -- -- -- --print("A")
						-- -- -- -- SobGroup_Clear("TorpedoTempGroup"..shipID)
						-- -- -- -- --print("B")
						-- -- -- -- Volume_AddSphere("AttackerPositionVolume", SobGroup_GetPosition(CustomGroup), 0) -- it will appear in this location
						-- -- -- -- --print("C")
						-- -- -- -- local j = 1
						-- -- -- -- for j = 1,torpedo_tubes,1 do
						-- -- -- -- SobGroup_SpawnNewShipInSobGroup(playerID, "msl_light_torpedo_"..j, "msl_light_torpedo_"..j..shipID, "TorpedoTempGroup"..shipID, "AttackerPositionVolume")--replace [msl_torpedo] by your torpedo unit's ship name
						-- -- -- -- --SobGroup_AvoidanceIgnore("TorpedoTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- end
						-- -- -- -- --print("D")
						-- -- -- -- Volume_Delete("AttackerPositionVolume")
						-- -- -- -- --print("E")
						-- -- -- -- SobGroup_ParadeSobGroup("TorpedoTempGroup"..shipID, CustomGroup, 2)
						-- -- -- -- --SobGroup_ParadeSobGroup("TorpedoTempGroup"..shipID, CustomGroup, 2)
						-- -- -- -- --print("F")
						-- -- -- -- --SobGroup_Attack(PlayerIndex, "TorpedoTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- --print("G")
						-- -- -- -- SobGroup_Attack(playerID,"TorpedoTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- --SobGroup_Kamikaze("TorpedoTempGroup"..shipID, "TorpedoAttackerTargetTempGroup") -- Tora! Tora! Tora!
						-- -- -- -- --print("H")
						-- -- -- -- SobGroup_MakeSelectable("TorpedoTempGroup"..shipID,0) -- Good bye, my little torpedo, you will be missed, but please do not miss.
						-- -- -- -- --print("I")
						-- -- -- -- ships[b].weapon.torpedo_reload = 0 -- Yay, let's go through another reload cycle!
						-- -- -- -- --print("J")
							-- -- -- -- -- aa = GetShipId("msl_torpedo")
						-- -- -- -- -- --print("ID du missile tiré")
						-- -- -- -- -- --print(aa)
						-- -- -- -- -- missile = aa
						-- -- -- -- -- target
						-- -- -- -- if torpedo_max == -10 then
						-- -- -- -- else
						-- -- -- -- ships[b].weapon.torpedo = f-torpedo_use -- let's consume a torpedo from the reserve
						-- -- -- -- end
						-- -- -- -- --SobGroup_SetHealth("TorpedoTempGroup"..shipID, 0)
						-- -- -- -- else
						-- -- -- -- --print("Almost in range...") -- Are we theeeeeere yet? No, kids.
					-- -- -- -- end
				-- -- -- -- end
			-- -- -- -- end
		-- -- -- -- end
	-- -- -- -- end

-- -- -- -- end

-- -- -- -- end

function Torpedo_Ship(CustomGroup, playerID, shipID, torpedo_max, torpedo_tubes, torpedo_use, torpedo_recharge, torpedo_reload_max,range)
--print("Start Script Torpedo")

-- Algorithm:
-- I want to check if I still have any torpedo left.
-- If I do, then I will go to the following step.
-- I want to check if my torpedo is ready to launch.
-- If it is not, I will add a tick to the reload cycle.
-- I want to check now if I am attacking.
-- If I am attacking, am I attacking a valid target?
-- If I am attacking a valid target, then I will select pseudo-randomly a single target among the valid ones.
-- I will launch my torpedo.
-- My torpedo will attack my target.
-- I will update the readiness cycle and the number of torpedoes left in my craft.
-- Whatever happens, I will update the displayed values for the UI.

-- The variables allowing me to check the current status of the craft.
local b = shipID

-- if torpedo_max == 0 then
-- if SobGroup_Selected(CustomGroup) == 1 then
-- UI_SetTextLabelText("NewTaskbar", "MaxTorpedo", "NO")
-- end
-- else
-- local f = ships[b].weapon.torpedo
local e = ships[b].weapon.torpedo_reload

-- Do I have any torpedo left?
-- if f > 0 then
ships[b].weapon.RTB = 1
-- Is my torpedo ready to launch?
if e<torpedo_reload_max then -- my torpedo is not ready to launch, I will go further in the rearming cycle.

ships[b].weapon.torpedo_reload = e+1

else -- My torpedo is ready to launch.

-- Am I attacking anyone?
if (SobGroup_GetCurrentOrder(CustomGroup)==COMMAND_Attack) then

SobGroup_CreateIfNotExist("AttackTargetTempGroup")
SobGroup_Clear("AttackTargetTempGroup")
SobGroup_GetCommandTargets("AttackTargetTempGroup", CustomGroup, COMMAND_Attack) -- This is the SobGroup with the targets of my ship.


SobGroup_CreateIfNotExist("SingleTargetTempGroup")
SobGroup_Clear("SingleTargetTempGroup")
-- I am now checking if my craft is attacking a valid target.
if SobGroup_AreAnyFromTheseAttackFamilies("AttackTargetTempGroup", "Capturer, Frigate, SmallCapitalShip, BigCapitalShip, Mothership, Utility, ResourceLarge")==1 then

NumberInitialTargets=SobGroup_Count("AttackTargetTempGroup") -- Here is how many targets are being attacked.
SobGroup_CreateIfNotExist("ProximityCheckTempGroup")
SobGroup_Clear("ProximityCheckTempGroup")

-- Does my craft attack several targets, needing a selection?
if NumberInitialTargets==1 then -- No, the craft is attacking a single target.

-- Is this single target within firing range?

SobGroup_FillProximitySobGroup("ProximityCheckTempGroup", "AttackTargetTempGroup" , CustomGroup , range)
if SobGroup_Count("ProximityCheckTempGroup")==1 then
SobGroup_FillUnion("SingleTargetTempGroup","SingleTargetTempGroup","AttackTargetTempGroup") -- I now have a SobGroup with the desired name and a single target.
end -- This is the end of the if loop checking if the single target is within firing range.

else -- Yes, the craft is attacking several target, I need to select a single one now.

local shipIndex = mod(b/16,NumberInitialTargets)+1 -- This is a pseudo-random starting point for the selection loop, based on the shipID of my craft and the size of the attacked group. Note that the shipID is always a multiple of 16 in game.
local Reference = shipIndex-1 -- This will be my reference to check when I have done a full loop.
local TargetLock = 0 -- This is the variable to check when I have achieved a lock on a valid unit.

while TargetLock<1 do

SobGroup_CreateIfNotExist("SingleTargetCheckGroup")
SobGroup_Clear("SingleTargetCheckGroup")
-- I will create a group with a single ship using the index.
SobGroup_FillShipsByIndexRange("SingleTargetCheckGroup", "AttackTargetTempGroup", shipIndex, 1)

-- Is the target of a correct type?
CheckTargetType = SobGroup_AreAnyFromTheseAttackFamilies("SingleTargetCheckGroup", "Capturer, Frigate, SmallCapitalShip, BigCapitalShip, Mothership, Utility, ResourceLarge")
SobGroup_FillProximitySobGroup("ProximityCheckTempGroup", "SingleTargetCheckGroup" , CustomGroup , range)
-- Is the target within firing range?
CheckTargetRange = SobGroup_Count("ProximityCheckTempGroup")

-- Check if both conditions are fit.
if CheckTargetType*CheckTargetRange>0 then

SobGroup_FillUnion("SingleTargetTempGroup","SingleTargetTempGroup","SingleTargetCheckGroup") -- I now have a SobGroup with the desired name and a single target.
TargetLock=1

else

shipIndex=shipIndex+1
if shipIndex>NumberInitialTargets then

shipIndex=1

end

end -- This is the end of the if loop finding whether the currently scanned target is correct in type and range.

if shipIndex-1==Reference then -- I did a full loop.

TargetLock=1 -- Let's stop the loop, with or without target.

end

end -- This is the end of the while loop finding a valid target to attack.

end -- This is the end of the if loop checking if my craft is attacking a single target.


-- I am attacking a valid type of target, and I have gone through the loop checking whether or not I am attacking a valid target and, if so, I now have a dedicated group for a single target.

if SobGroup_Count("SingleTargetTempGroup")==1 then
-- I now have a single good target, I can launch my torpedo on it.
SobGroup_CreateIfNotExist("torpedoTempGroup"..shipID)
SobGroup_Clear("torpedoTempGroup"..shipID)

Volume_AddSphere("LaunchPositionVolume", SobGroup_GetPosition(CustomGroup), 0) -- The torpedo will appear in this location.

for i = 1,ammo_tubes,1 do -- The craft will launch as many torpedos at it has tubes.
SobGroup_SpawnNewShipInSobGroup(playerID, "msl_torpedo_"..i, "msl_torpedo_"..i.."Tir"..shipID..d, "torpedoTempGroup"..shipID, "LaunchPositionVolume") --replace [msl_Spiculim]..i by your torpedo unit's ship name
-- The huge amount of parameters for the Squadron Name is to ensure that no two torpedos have the same squadron name. I don't know if it can cause issues but I do not want to test.
end -- This is the end of the for loop launching as many torpedos as the craft has tubes.

Volume_Delete("LaunchPositionVolume")
SobGroup_ParadeSobGroup("torpedoTempGroup"..shipID, CustomGroup, 2) -- We are now teleporting the created torpedos to their "launch positions".

local EnemyPlayer = SobGroup_OwnedBy("SingleTargetTempGroup") -- This is the player being attacked.
SobGroup_AvoidanceIgnore("torpedoTempGroup"..shipID, "Player_Ships"..playerID)

if EnemyPlayer > -1 then -- Let's avoid some crash.

SobGroup_AvoidanceIgnore("torpedoTempGroup"..shipID, "Player_Ships"..EnemyPlayer)

end -- This is the end of the if loop made to avoid a crash during the avoidance ignoring script.

SobGroup_Attack(playerID,"torpedoTempGroup"..shipID, "SingleTargetTempGroup") -- The torpedo will attack the single target.
SobGroup_MakeSelectable("torpedoTempGroup"..shipID,0) -- The torpedo cannot be selected anymore by the player.
SobGroup_Clear("torpedoTempGroup"..shipID) -- There is nothing we need to do anymore with the torpedo.
ships[b].weapon.torpedo_reload = 0 -- We reset the launch sequence.
ships[b].weapon.torpedo = f-torpedo_use -- We remove one or several torpedos.

end -- This is the end of the if loop checking if my craft is attacking a valid target and can launch the torpedo.

end -- This is the end of the if loop checking if my craft is attacking a valid type of target.

end -- This is the end of the if loop checking if my craft is attacking anyone.

end -- This is the end of the if loop checking if my torpedo is ready to launch.

-- else

-- ships[b].weapon.RTB = 0 -- I am Winchester, out of ammunition, let's Return To Base

-- end -- This is the end of the if loop checking if I have any torpedo left.

-- Let's update the UI with the remaining torpedos.
-- if SobGroup_Selected(CustomGroup) == 1 then
-- UI_SetTextLabelText("NewTaskbar", "MaxTorpedo", ""..f)	
-- end -- This is the end of the if loop updating the UI.

-- end -- This is the end of the if loop checking if I actually am a torpedo bomber.
--print("End Script torpedo")
end -- This is the end of the Torpedo_Ship function.

-- -- -- -- -- function Torpedo_Ship(CustomGroup, playerID, shipID, Torpedo_Research_1, torpedo_tubes, torpedo_use, torpedo_recharge, torpedo_reload_max, range)

-- -- -- -- -- dofilepath("data:scripts/lib/custom/shiptable.lua")

-- -- -- -- -- local b = shipID



-- -- -- -- -- -- Checking if we are out of torpedoes (if we are out of missiles, it's less important).

-- -- -- -- -- if Player_HasResearch(playerID, Torpedo_Research_1)==1 then

-- -- -- -- -- local f = ships[b].weapon.torpedo
-- -- -- -- -- local h = ships[b].weapon.torpedo_reload
-- -- -- -- -- if h<torpedo_reload_max then
-- -- -- -- -- ships[b].weapon.torpedo_reload = h + torpedo_recharge -- if my torpedo is not ready, let's keep readying it by adding the ticks
-- -- -- -- -- end
-- -- -- -- -- --print("Cycle de rechargement torpille pour vaisseau :")
-- -- -- -- -- --print(h)

	-- -- -- -- -- if f>0 and h==torpedo_reload_max then -- I have torpedoes AND one of them is ready, so we can go with the torpedo attack script
		-- -- -- -- -- if(SobGroup_GetCurrentOrder(CustomGroup)==COMMAND_Attack)then -- Am I actually attacking?
			-- -- -- -- -- SobGroup_CreateIfNotExist("TorpedoAttackerTargetTempGroup")
			-- -- -- -- -- SobGroup_Clear("TorpedoAttackerTargetTempGroup")
			-- -- -- -- -- SobGroup_GetCommandTargets("TorpedoAttackerTargetTempGroup", CustomGroup, COMMAND_Attack) -- Here is my target!
			-- -- -- -- -- if(SobGroup_Count("TorpedoAttackerTargetTempGroup")>0)then -- I am actually attacking someone
				-- -- -- -- -- if(SobGroup_AreAnyFromTheseAttackFamilies("TorpedoAttackerTargetTempGroup", "Capturer, Frigate, SmallCapitalShip, BigCapitalShip, Mothership, Utility, ResourceLarge")==1)then--edit the attack families the attacker can attack, or simply copy this from the attacker's .ship file
					-- -- -- -- -- SobGroup_CreateIfNotExist("TorpedoAttackerTargetTempGroup2") -- So, my target is a big boy, let's arm the torpedo, then!
					-- -- -- -- -- SobGroup_Clear("TorpedoAttackerTargetTempGroup2")
					-- -- -- -- -- SobGroup_FillProximitySobGroup("TorpedoAttackerTargetTempGroup2", "TorpedoAttackerTargetTempGroup" , CustomGroup , range)--edit the firerange of the torpedo, how far the attack will fire upon the target
					-- -- -- -- -- if(SobGroup_Empty("TorpedoAttackerTargetTempGroup2")==0)then -- I am in weapon range, my torpedo is ready, let's unleash hell!
						-- -- -- -- -- --print("I'm in range!")
						-- -- -- -- -- SobGroup_CreateIfNotExist("TorpedoTempGroup"..shipID) -- the torpedo will be there
						-- -- -- -- -- --print("A")
						-- -- -- -- -- SobGroup_Clear("TorpedoTempGroup"..shipID)
						-- -- -- -- -- --print("B")
						-- -- -- -- -- Volume_AddSphere("AttackerPositionVolume", SobGroup_GetPosition(CustomGroup), 0) -- it will appear in this location
						-- -- -- -- -- --print("C")
						-- -- -- -- -- local j = 1
						-- -- -- -- -- for j = 1,torpedo_tubes,1 do
						-- -- -- -- -- SobGroup_SpawnNewShipInSobGroup(playerID, "msl_torpedo_"..j, "msl_torpedo_"..j..shipID, "TorpedoTempGroup"..shipID, "AttackerPositionVolume")--replace [msl_torpedo] by your torpedo unit's ship name
						-- -- -- -- -- --SobGroup_AvoidanceIgnore("TorpedoTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- -- end
						-- -- -- -- -- --print("D")
						-- -- -- -- -- Volume_Delete("AttackerPositionVolume")
						-- -- -- -- -- --print("E")
						-- -- -- -- -- SobGroup_ParadeSobGroup("TorpedoTempGroup"..shipID, CustomGroup, 2)
						-- -- -- -- -- --SobGroup_ParadeSobGroup("TorpedoTempGroup"..shipID, CustomGroup, 2)
						-- -- -- -- -- --print("F")
						-- -- -- -- -- --SobGroup_Attack(PlayerIndex, "TorpedoTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- -- --print("G")
						-- -- -- -- -- SobGroup_Attack(playerID,"TorpedoTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						-- -- -- -- -- --SobGroup_Kamikaze("TorpedoTempGroup"..shipID, "TorpedoAttackerTargetTempGroup") -- Tora! Tora! Tora!
						-- -- -- -- -- --print("H")
						-- -- -- -- -- SobGroup_MakeSelectable("TorpedoTempGroup"..shipID,0) -- Good bye, my little torpedo, you will be missed, but please do not miss.
						-- -- -- -- -- --print("I")
						-- -- -- -- -- ships[b].weapon.torpedo_reload = 0 -- Yay, let's go through another reload cycle!
						-- -- -- -- -- --print("J")
							-- -- -- -- -- -- aa = GetShipId("msl_torpedo")
						-- -- -- -- -- -- --print("ID du missile tiré")
						-- -- -- -- -- -- --print(aa)
						-- -- -- -- -- -- missile = aa
						-- -- -- -- -- -- target
						-- -- -- -- -- --SobGroup_SetHealth("TorpedoTempGroup"..shipID, 0)
						-- -- -- -- -- else
						-- -- -- -- -- --print("Almost in range...") -- Are we theeeeeere yet? No, kids.
					-- -- -- -- -- end
				-- -- -- -- -- end
			-- -- -- -- -- end
		-- -- -- -- -- end
	-- -- -- -- -- end

-- -- -- -- -- end


-- -- -- -- -- end


-- Problème : si quelqu'un attaque un groupe de vaisseaux comprenant celui qui a le leurre, il se
-- considérera comme attaqué même si les missiles ne vont pas sur lui.
-- Solution 1 : il faut pouvoir distinguer la cible précise du missile en approche.
-- Solution 2 : il faut que le missile n'attaque qu'une seule cible à la fois.

-- Problème probable : si on attaque un groupe de vaisseaux avec au moins une cible valide pour les torpilles,
-- la torpille pourra être lancée sur n'importe qui, qu'importe la validité individuelle de la cible.
-- Solution : il faut que la torpille n'attaque qu'une cible à la fois.
-- Méthode : il faut que le groupe attaqué soit ramené à une seule cible.
function Decoy_Strikecraft(CustomGroup, playerID, shipID, decoy_max, decoy_use, decoy_recharge, decoy_reload_max)

dofilepath("data:scripts/lib/custom/shiptable.lua")

NumJoueurs = GlobalStats.NumPlayers

local b = shipID

local g = ships[b].weapon.decoy

local i = ships[b].weapon.decoy_reload

if i<decoy_reload_max then
ships[b].weapon.decoy_reload = i + decoy_recharge -- if my decoy is not ready, let's keep readying it by adding the ticks
end

SobGroup_CreateIfNotExist("Missiles_Total")

if SobGroup_Count("Missiles_Total")>0 then
if i==decoy_reload_max and g>0 then
SobGroup_CreateIfNotExist("AttackerTempGroup")
SobGroup_Clear("AttackerTempGroup")
SobGroup_CreateIfNotExist("Myself")
SobGroup_Clear("Myself")
SobGroup_SobGroupAdd("Myself", CustomGroup)
SobGroup_GetAttackers("Myself", "AttackerTempGroup")
k=SobGroup_Count("AttackerTempGroup")
print("Nombre d'attaquants :")
print(k)
if k>0 then

-- l=SobGroup_AreAnyOfTheseTypes("AttackerTempGroup", "Msl_Spiculim_1, Msl_Spiculim_2, Msl_Spiculim_3, Msl_Spiculim_4, Msl_Spiculim_5, Msl_Spiculim_6, Msl_Spiculim_7, Msl_Spiculim_8")
-- -- --print("Y a-t-il un missile ?")
-- -- --print(l)
-- m=SobGroup_AreAnyFromTheseAttackFamilies("AttackerTempGroup", "Fighter")
-- -- --print("Chasseur ?")
-- -- --print(m)
-- SobGroup_CreateIfNotExist("IncomingTempGroup1")
-- SobGroup_CreateIfNotExist("IncomingTempGroup2")
-- if SobGroup_Count("AttackerTempGroup")>0 then
-- y = SobGroup_OwnedBy("AttackerTempGroup")
-- --print("Possesseur :")
-- --print(y)
-- SobGroup_Clear("IncomingTempGroup1")
-- SobGroup_Clear("IncomingTempGroup2")
-- SobGroup_FillProximitySobGroup("IncomingTempGroup1", "Player_Ships"..y, CustomGroup, 5000)
-- --print("En approche ?")
-- --print(SobGroup_Count("IncomingTempGroup1"))
-- SobGroup_FillShipsByType("IncomingTempGroup2", "IncomingTempGroup1", "Msl_Spiculim_1")
-- --print(SobGroup_Count("IncomingTempGroup2"))
-- end
SobGroup_CreateIfNotExist("Missiles_"..playerID)

SobGroup_CreateIfNotExist("IncomingTempGroup1")
SobGroup_CreateIfNotExist("IncomingTempGroup2")
SobGroup_CreateIfNotExist("IncomingTempGroup")
SobGroup_CreateIfNotExist("IncomingTempGroupDanger")
SobGroup_CreateIfNotExist("MissileDanger")
SobGroup_CreateIfNotExist("MissileCheck")
SobGroup_Clear("IncomingTempGroup1")
SobGroup_Clear("IncomingTempGroup2")
SobGroup_Clear("IncomingTempGroup")
SobGroup_Clear("IncomingTempGroupDanger")
SobGroup_Clear("MissileDanger")
SobGroup_Clear("MissileCheck")
SobGroup_FillProximitySobGroup("IncomingTempGroup1", "Missiles_"..playerID, CustomGroup, 2500) -- Mes missiles.
--print("Missile Check :")
--print(SobGroup_Count("IncomingTempGroup1"))
SobGroup_FillProximitySobGroup("IncomingTempGroup2", "Missiles_Total", CustomGroup, 2500) -- Tous les missiles.
--print(SobGroup_Count("IncomingTempGroup2"))
--SobGroup_FillSubstract("IncomingTempGroup", "Missiles_Total", "Missiles_"..playerID)
SobGroup_FillSubstract("IncomingTempGroup", "IncomingTempGroup2", "IncomingTempGroup1") -- Tous les missiles sauf les miens.


--print(SobGroup_Count("IncomingTempGroup"))
local Danger = 0
			local missileCount = SobGroup_Count("IncomingTempGroup")
			local missileIndex = 0

			while (missileIndex < missileCount) do
			--print("Inbound missile #")
			--print(missileIndex)
				SobGroup_Clear("MissileCheck")
				SobGroup_FillShipsByIndexRange("MissileCheck", "IncomingTempGroup", missileIndex, 1)

				missileIndex = missileIndex + 1
			
				-- if (SobGroup_Count("MissileCheck") > 0) then
					-- missileIndex = missileIndex + (SobGroup_Count("MissileCheck")- 1)
				-- end

SobGroup_CreateIfNotExist("Target")
SobGroup_Clear("Target")
SobGroup_GetCommandTargets("Target", "MissileCheck", COMMAND_Attack)
--local Danger = SobGroup_GroupInGroup("Target", "Myself")
local Danger = SobGroup_GroupInGroup("MissileCheck", "AttackerTempGroup")
--print("Is the missile targeting me ?")
--print(Danger)

				if Danger == 1 then
				
					SobGroup_CreateIfNotExist("MissileDanger")
					SobGroup_SobGroupAdd("MissileDanger", "MissileCheck") -- if I want to decoy all missiles at once
					--print("I am targeted by this many missiles :")
					--print(SobGroup_Count("MissileDanger"))
					SobGroup_CreateIfNotExist("MissileDecoy")
					SobGroup_Clear("MissileDecoy")
					SobGroup_SobGroupAdd("MissileDecoy", "MissileCheck") -- if I want to decoy just the last missile found
					
				end
				
			end


--print("Cycle de rechargement pour leurres :")
--print(i)

if SobGroup_Count("MissileDecoy")>0 then

						-- SobGroup_CreateIfNotExist("Target")
						-- SobGroup_Clear("Target")
						-- SobGroup_GetCommandTargets("Target", "IncomingTempGroup2", COMMAND_Attack)
						--print("Cibles Missiles :")
						--print(SobGroup_Count("Target"))
						-- if SobGroup_GroupInGroup("Target", CustomGroup) then
						SobGroup_CreateIfNotExist("DecoyTempGroup"..shipID) -- the decoy will be there
						-- SobGroup_CreateIfNotExist("MissileTempGroup") -- the enemy missiles will be there
						--print("A1")
						--SobGroup_Clear("DecoyTempGroup")
						--SobGroup_Clear("MissileTempGroup")
						--print("B1")
						--SobGroup_FillShipsByType("MissileTempGroup", "AttackerTempGroup", "Msl_Spiculim_1, Msl_Spiculim_2, Msl_Spiculim_3, Msl_Spiculim_4, Msl_Spiculim_5, Msl_Spiculim_6, Msl_Spiculim_7, Msl_Spiculim_8")
						Volume_AddSphere("AttackerPositionVolume", SobGroup_GetPosition(CustomGroup), 0) -- it will appear in this location
						--print("C1")
						SobGroup_SpawnNewShipInSobGroup(playerID, "Msl_Decoy_1", "Msl_Decoy_1"..shipID, "DecoyTempGroup"..shipID, "AttackerPositionVolume")--replace [msl_torpedo] by your torpedo unit's ship name
						--SobGroup_SpawnNewShipInSobGroup(playerID, "msl_torpedo_2", "msl_torpedo_2"..shipID, "DecoyTempGroup"..shipID, "AttackerPositionVolume")--replace [msl_torpedo] by your torpedo unit's ship name
						--SobGroup_AvoidanceIgnore("DecoyTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						--print("D1")
						--print("Largage Leurre")
						Volume_Delete("AttackerPositionVolume")
						--print("E1")
						SobGroup_ParadeSobGroup("DecoyTempGroup"..shipID, CustomGroup, 2)
						local ya = SobGroup_OwnedBy("MissileDecoy")
						SobGroup_AvoidanceIgnore("DecoyTempGroup"..shipID, "Player_Ships"..playerID)
						if ya<0 then
						else
						SobGroup_AvoidanceIgnore("DecoyTempGroup"..shipID, "Player_Ships"..ya)
						end
						--SobGroup_ParadeSobGroup("DecoyTempGroup"..shipID, CustomGroup, 2)
						--print("F1")
						--SobGroup_Attack(PlayerIndex, "DecoyTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						--print("G1")
						SobGroup_Attack(playerID,"DecoyTempGroup"..shipID, "MissileDecoy")						--SobGroup_ParadeSobGroup("DecoyTempGroup"..shipID, CustomGroup, 2)
						--print("F")
						Position=SobGroup_GetPosition(CustomGroup)[1]
						if cos(Position)>0 then
						local z = SobGroup_OwnedBy("MissileDecoy")
						SobGroup_Attack(z, "MissileDecoy", "DecoyTempGroup"..shipID)
						--print("Leurre Succès")
						else
						--print("Leurre Raté")
						end
						--SobGroup_Attack(PlayerIndex, "DecoyTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						--print("G")
						--SobGroup_Attack(playerID,"DecoyTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						--SobGroup_Kamikaze("DecoyTempGroup"..shipID, "MissileDanger") -- Tora! Tora! Tora!
						--print("H1")
						SobGroup_MakeSelectable("DecoyTempGroup"..shipID,0) -- Good bye, my little torpedo, you will be missed, but please do not miss.
						SobGroup_Clear("DecoyTempGroup"..shipID)
						--print("I1")
						ships[b].weapon.decoy_reload = 0 -- Yay, let's go through another reload cycle!
						--print("J1")
							-- aa = GetShipId("msl_torpedo")
						-- --print("ID du missile tiré")
						-- --print(aa)
						-- missile = aa
						-- target

						ships[b].weapon.decoy = g-decoy_recharge -- let's consume a torpedo from the reserve
						-- end

end

end

end

end

if SobGroup_Selected(CustomGroup) == 1 then
UI_SetTextLabelText("NewTaskbar", "MaxDecoy", ""..g)
end

end

function Capship_Missiles(CustomGroup, playerID, shipID, capship_max, capship_tubes, capship_use, capship_recharge, capship_reload_max, range, Research_1, Research_2)

dofilepath("data:scripts/lib/custom/shiptable.lua")

b = shipID


if Player_HasResearch(playerID, Research_1)==1 then



e = ships[b].weapon.capship_reload

if e<capship_reload_max then
ships[b].weapon.capship_reload = e + capship_recharge -- if my torpedo is not ready, let's keep readying it by adding the ticks
end
--print("Cycle de rechargement pour vaisseau :")
--print(e)

	if e==capship_reload_max then -- I have torpedoes AND one of them is ready, so we can go with the torpedo attack script
		if(SobGroup_GetCurrentOrder(CustomGroup)==COMMAND_Attack)then -- Am I actually attacking?
			SobGroup_CreateIfNotExist("TorpedoAttackerTargetTempGroup")
			SobGroup_Clear("TorpedoAttackerTargetTempGroup")
			SobGroup_GetCommandTargets("TorpedoAttackerTargetTempGroup", CustomGroup, COMMAND_Attack) -- Here is my target!
			if(SobGroup_Count("TorpedoAttackerTargetTempGroup")>0)then -- I am actually attacking someone
				if(SobGroup_AreAnyFromTheseAttackFamilies("TorpedoAttackerTargetTempGroup", "Capturer, Frigate, SmallCapitalShip, BigCapitalShip, Mothership, Utility")==1)then--edit the attack families the attacker can attack, or simply copy this from the attacker's .ship file
					SobGroup_CreateIfNotExist("TorpedoAttackerTargetTempGroup2") -- So, my target is a big boy, let's arm the torpedo, then!
					SobGroup_Clear("TorpedoAttackerTargetTempGroup2")
					SobGroup_FillProximitySobGroup("TorpedoAttackerTargetTempGroup2", "TorpedoAttackerTargetTempGroup" , CustomGroup , range)--edit the firerange of the torpedo, how far the attack will fire upon the target
					if(SobGroup_Empty("TorpedoAttackerTargetTempGroup2")==0)then -- I am in weapon range, my torpedo is ready, let's unleash hell!
						--print("I'm in range!")
						SobGroup_CreateIfNotExist("CapShipMissileTempGroup"..shipID) -- the torpedo will be there
						--print("A")
						SobGroup_Clear("CapShipMissileTempGroup"..shipID)
						--print("B")
						Volume_AddSphere("AttackerPositionVolume", SobGroup_GetPosition(CustomGroup), 0) -- it will appear in this location
						--print("C")
						if Player_HasResearch(playerID, Research_2)==0 then
						for j = 1,capship_tubes,1 do
						SobGroup_SpawnNewShipInSobGroup(playerID, "msl_klr_cruise_missile", "msl_klr_cruise_missile"..shipID, "CapShipMissileTempGroup"..shipID, "AttackerPositionVolume")--replace [msl_torpedo] by your torpedo unit's ship name
						end
						else
--						SobGroup_SpawnNewShipInSobGroup(playerID, "msl_klr_cruise_missile", "msl_klr_cruise_missile"..shipID, "CapShipMissileTempGroup"..shipID, "AttackerPositionVolume")--replace [msl_torpedo] by your torpedo unit's ship name
						for j = 1,capship_tubes,1 do
						SobGroup_SpawnNewShipInSobGroup(playerID, "msl_skipper", "msl_skipper"..shipID, "CapShipMissileTempGroup"..shipID, "AttackerPositionVolume")--replace [msl_torpedo] by your torpedo unit's ship name
						end
						end
						--SobGroup_AvoidanceIgnore("CapShipMissileTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						--print("D")
						Volume_Delete("AttackerPositionVolume")
						--print("E")
						SobGroup_ParadeSobGroup("CapShipMissileTempGroup"..shipID, CustomGroup, 2)
						--SobGroup_ParadeSobGroup("CapShipMissileTempGroup"..shipID, CustomGroup, 2)
						--print("F")
						--SobGroup_Attack(PlayerIndex, "CapShipMissileTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						--print("G")
						SobGroup_Attack(playerID,"CapShipMissileTempGroup"..shipID, "TorpedoAttackerTargetTempGroup")
						--SobGroup_Kamikaze("CapShipMissileTempGroup"..shipID, "TorpedoAttackerTargetTempGroup") -- Tora! Tora! Tora!
						--print("H")
						SobGroup_MakeSelectable("CapShipMissileTempGroup"..shipID,0) -- Good bye, my little torpedo, you will be missed, but please do not miss.
						--print("I")
						ships[b].weapon.capship_reload = 0 -- Yay, let's go through another reload cycle!
						--print("J")
							-- aa = GetShipId("msl_torpedo")
						-- --print("ID du missile tiré")
						-- --print(aa)
						-- missile = aa
						-- target

						--ships[b].weapon.ammo = d-1 -- let's consume a torpedo from the reserve
						--SobGroup_SetHealth("CapShipMissileTempGroup"..shipID, 0)
						else
						--print("Almost in range...") -- Are we theeeeeere yet? No, kids.
					end
				end
			end
		end
	end
end

end



function Return_To_Base(CustomGroup, playerID, shipID, fuel_max, ammo_max, decoy_max, torpedo_max)

local b = shipID

local RTBTorp = ships[b].weapon.RTB
local RTBFuel = ships[b].fuel.RTB
local Corvette = ships[b].weapon.bomber
local RTB = RTBTorp*RTBFuel

--print("RTB :")
--print(RTBTorp)
--print(RTBFuel)
--print(RTB)

if RTB == 0 then -- I want to return to base

	SobGroup_FillProximitySobGroup("Test1", "Player_Ships"..playerID, CustomGroup, 1500) -- Who is around me, me included?
	SobGroup_FillProximitySobGroup("OwnShip", "Player_Ships"..playerID, CustomGroup, 5) -- Me!
	SobGroup_FillSubstract("Test2", "Test1", "OwnShip") -- Who is around me, me NOT included. Useful, you'll see why later.

	if Corvette == 1 then

		if SobGroup_AreAnyOfTheseTypes("Test2", "cfd_carrier, cfd_confederation, cfd_jutland, cfd_lexington, cfd_mothership, cfd_naval_base, cfd_savannah, cfd_victory, klr_bhantkara, klr_dubav, klr_hvarkann, klr_shipyard, klr_starbase, ubw_carrier, ubw_mothership, ubw_shipyard")==1 then -- there's a carrier in proximity, let's not go all over the map for nothing, shall we? Replace this by a list of your carrier-capable units.
			--print("CV found near me.")
			SobGroup_CreateIfNotExist("Carrier")
			SobGroup_Clear("Carrier")
			SobGroup_FillShipsByType("Carrier", "Test2", "cfd_carrier, cfd_confederation, cfd_jutland, cfd_lexington, cfd_mothership, cfd_naval_base, cfd_savannah, cfd_victory, klr_bhantkara, klr_dubav, klr_hvarkann, klr_shipyard, klr_starbase, ubw_carrier, ubw_mothership, ubw_shipyard")
			if SobGroup_IsDoingAbility("Test2", AB_Dock)==1 then
			--print("No Priority")
				SobGroup_GuardSobGroup(CustomGroup, "Carrier") -- there are other ships waiting to land on the carrier near me, I'll guard my carrier instead of sitting like a target			
		else
			--print("Priority") -- looks like noone is waiting to land, the sky is empty or everyone else is on guard, so it's my turn to land
-- what happened is that when you are RTB near a carrier, you will want to land. The first one to run the script in proximity to the carrier will land. Everyone after will see that landing plane and will get in guard position. When the plane landed, the next RTB plane to run the script will not see anyone landing and will start landing, etc., keeping only one plane on the landing pattern at all times!
			SobGroup_DockSobGroup(CustomGroup, "Carrier")													
		end
		else
			--print("No CV found around me.") -- no carrier around, I'll land on a random friendly carrier
			SobGroup_DockSobGroup(CustomGroup, "AllShips")
		end

	else

					if SobGroup_AreAnyOfTheseTypes("Test2", "cfd_ajax, cfd_battlecruiser, cfd_carrier, cfd_confederation, cfd_eagle, cfd_jutland, cfd_lexington, cfd_mothership, cfd_naval_base, cfd_savannah, cfd_tarawa, cfd_victory, klr_bhantkara, klr_dubav, klr_fralath, klr_fralthi_ii, klr_fralthra, klr_hvarkann, klr_ralarrad, klr_ralaxath, klr_shipyard, klr_starbase, ubw_battlecruiser, ubw_carrier, ubw_mothership, ubw_shipyard")==1 then
						--print("CV trouvé")
					SobGroup_CreateIfNotExist("Carrier")
					SobGroup_Clear("Carrier")
						SobGroup_FillShipsByType("Carrier","Test2","cfd_ajax, cfd_battlecruiser, cfd_carrier, cfd_confederation, cfd_eagle, cfd_jutland, cfd_lexington, cfd_mothership, cfd_naval_base, cfd_savannah, cfd_tarawa, cfd_victory, klr_bhantkara, klr_dubav, klr_fralath, klr_fralthi_ii, klr_fralthra, klr_hvarkann, klr_ralarrad, klr_ralaxath, klr_shipyard, klr_starbase, ubw_battlecruiser, ubw_carrier, ubw_mothership, ubw_shipyard")
						if SobGroup_IsDoingAbility("Test2", AB_Dock)==1 then
						--print("Pas en priorité")
							SobGroup_GuardSobGroup(CustomGroup, "Carrier")						
						else
						--print("A la priorité")
							SobGroup_DockSobGroup(CustomGroup, "Carrier")													
						end
					else
						--print("Pas de CV trouvé")
						SobGroup_DockSobGroup(CustomGroup, "AllShips")																			
					end

end

-- Reloading missiles, fuel, torpedoes and decoys.
end


if SobGroup_IsDocked(CustomGroup)==1 then

ships[b].weapon.ammo = ammo_max -- I got my ammunition reloaded by the ground crews
--ships[b].weapon.torpedo = weapon.torpedo_max -- I got my ammunition reloaded by the ground crews
ships[b].weapon.decoy = decoy_max -- I got my ammunition reloaded by the ground crews
ships[b].fuel.fuel = fuel_max -- I got my fuel reloaded by the ground crews
--SobGroup_SetHardPointHealth(CustomGroup,'FUELTANK',1)
ships[b].fuel.RTB = 1
ships[b].weapon.RTB = 1

if Corvette == 1 then

ships[b].weapon.torpedo = torpedo_max -- I got my ammunition reloaded by the ground crews

end

end

end

(continued in next post)