#Conditional Hooks
$Application: FS2_Open

$On Game Init:
[

function ProxyKill(target, killer)

	if not target then return end

	if not killer then killer = hv.Player end
	
	if type(target) == "string" then
		targetObj = mn.Ships[target]
	else
		targetObj = target
	end
	
	if targetObj:isValid() then
		targetObj:kill(killer)
	end

end

mn.LuaSEXPs["lua-proxy-kill"].Action = ProxyKill


function PlayerTarget(ship, subsystem)

	if type(ship) == "string" then
		ship = mn.Ships[ship]
	end

	if ship:isValid() and hv.Player:isValid() and ship ~= hv.Player and hv.Player.HitpointsLeft > 0 then
		
		hv.Player.Target = ship
		
		if subsystem and ship[subsystem] and ship[subsystem]:isValid() then
		
			hv.Player.TargetSubsystem = ship[subsystem]
		
		end
		
	end

end

mn.LuaSEXPs["lua-set-player-target"].Action = PlayerTarget

mn.LuaSEXPs["lua-get-player-target"].Action = function(variable) variable.Value = hv.Player.Target.Name end

function Lockdown(name, noplaydead)

local isPlayer

if not name then
	name = hv.Player.Name
	isPlayer = true
elseif name == hv.Player then
	isPlayer = true
end

mn.evaluateSEXP("(lock-primary-weapon !" .. name .. "!)")
mn.evaluateSEXP("(lock-secondary-weapon !" .. name .. "!)")
mn.evaluateSEXP("(lock-afterburner !" .. name .. "!)") 
mn.evaluateSEXP("(disable-ets !" .. name .. "!)")

if not noplaydead then
	mn.evaluateSEXP("(player-use-ai)") 
	mn.evaluateSEXP("(add-goal !" .. name .. "! ( ai-play-dead 200 ) )")
end

if isPlayer then
	mn.evaluateSEXP("(lock-perspective (true) 0 " ..
;;FSO 24.2.0;;       "(true)" ..
	")")
	mn.evaluateSEXP("(ignore-key !-1! !T! !H! !F! !Shift-T! !Shift-H! !Alt-H! !Shift-F! !Alt-M! !Alt-J! )")
end

local ship = mn.Ships[name]

if ship:isValid() then
	if ship.Physics.ForwardDecelerationTime > 0.1 then
		ship.Physics.ForwardDecelerationTime = ship.Physics.ForwardDecelerationTime / 1000
	end
end

end

function LockdownSEXP(playdead, ...)

	for _, data in ipairs(arg) do
		local ship = data[1]
		if ship:isValid() then
			Lockdown(ship.Name, not playdead)
		end
	end

end

mn.LuaSEXPs["lua-lockdown"].Action = LockdownSEXP

function UnLockdown(name)

local isPlayer

if not name then
	name = hv.Player.Name
	isPlayer = true
elseif name == hv.Player then
	isPlayer = true
end

mn.evaluateSEXP("(unlock-primary-weapon !" .. name .. "!)")
mn.evaluateSEXP("(unlock-secondary-weapon !" .. name .. "!)")
mn.evaluateSEXP("(unlock-afterburner !" .. name .. "!)" ) 
mn.evaluateSEXP("(enable-ets !" .. name .. "!)")
mn.evaluateSEXP("(player-not-use-ai)") 
mn.evaluateSEXP("(clear-goals !" .. name .. "!)")

if isPlayer then
	mn.evaluateSEXP("(lock-perspective (false) 0 " ..
;;FSO 24.2.0;;       "(false)" ..
	")")
	mn.evaluateSEXP("(ignore-key !0! !T! !H! !F! !Shift-T! !Shift-H! !Alt-H! !Shift-F! !Alt-M! !Alt-J! )")
end

local ship = mn.Ships[name]

if ship:isValid() then
	if ship.Physics.ForwardDecelerationTime < 0.1 then
		ship.Physics.ForwardDecelerationTime = ship.Physics.ForwardDecelerationTime * 1000
	end
end

end

function UnLockdownSEXP(...)

	for _, data in ipairs(arg) do
		local ship = data[1]
		if ship:isValid() then
			UnLockdown(ship.Name)
		end
	end

end

mn.LuaSEXPs["lua-unlockdown"].Action = UnLockdownSEXP

function Targetable(name)

local isplayer = "false"

if not name and hv.Player:isValid() then
   local player = hv.Player
   name = player.Name
   isplayer = "true"
end

mn.evaluateSEXP("( ship-visible !" .. name .. "! )")
mn.evaluateSEXP("( ship-unstealthy !" .. name .. "! )")
mn.evaluateSEXP("( friendly-stealth-visible !" .. name .. "! )")

end

function UnTargetable(name)

local isplayer = "false"

if not name and hv.Player:isValid() then
   local player = hv.Player
   name = player.Name
   isplayer = "true"
end

mn.evaluateSEXP("( ship-invisible !" .. name .. "! )")
mn.evaluateSEXP("( ship-stealthy !" .. name .. "! )")
mn.evaluateSEXP("( friendly-stealth-invisible !" .. name .. "! )")

end

mn.LuaSEXPs["lua-set-targetable"].Action = function(...)

	for _, data in ipairs(arg) do
		local ship = data[1]
		if ship:isValid() then
			Targetable(ship.Name)
		end
	end

end

mn.LuaSEXPs["lua-set-untargetable"].Action = function(...)

	for _, data in ipairs(arg) do
		local ship = data[1]
		if ship:isValid() then
			UnTargetable(ship.Name)
		end
	end

end

function CutsceneKeyLock()

mn.evaluateSEXP("( ignore-key !-1! !T! !Shift-T! !H! !Shift-H! !Alt-H! !F! !Shift-F! !Y! !G! !Alt-Y! !Alt-T! !V! !S! !Shift-S! !Alt-S! !M! !Alt-M! !Left Ctrl! !.! !,! !/! !Shift-/! !X! !A! !Z! !Pad 7! )")
mn.evaluateSEXP("( ignore-key !-1! !Pad 9! !Pad 8! !Pad 2! !Pad 4! !Pad 6! !Backspace! !\\! ![! !]! !=! !-! !Shift-A! !Shift-Z! !Shift-D! !Shift-V! !Shift-X! !Shift-E! !Shift-W! !Shift-I! !Shift-P! !Shift-C! !Shift-J! !Shift-R! !R! !Pad *! !Pad .! !Pad Enter! !Pad 0! !Pad /! !Pad +! )")
mn.evaluateSEXP("( ignore-key !-1! !Pad -! !Pad 5! !'! !C! !Alt-J! !J! !Tab! !Insert! !Delete! !Home! !End! !Page Up! !Page Down! !Alt-D! !Q! !Up Arrow! !Down Arrow! !Left Arrow! !Right Arrow! !Scroll Lock! !Shift-Scroll Lock! !Alt-E! !Alt-Shift-E! !E! !Alt-R! !U! !Shift-U! !N! !K! !Shift-K! )")
mn.evaluateSEXP("( ignore-key !-1! !B! !Shift-B! !1! !2! !3! !4! !Alt-X! !Shift-.! !Shift-,! !L! !Shift-N! !Shift-End! !Shift-O! !Shift-3! !Shift-1! !Shift-Pad +! !Shift-Pad Enter! !Alt-Shift-Q! !Alt-A! !Alt-N! !Alt-G! !O! )")

end

function CutsceneKeyUnLock()

mn.evaluateSEXP("( ignore-key !0! !T! !Shift-T! !H! !Shift-H! !Alt-H! !F! !Shift-F! !Y! !G! !Alt-Y! !Alt-T! !V! !S! !Shift-S! !Alt-S! !M! !Alt-M! !Left Ctrl! !.! !,! !/! !Shift-/! !X! !A! !Z! !Pad 7! )")
mn.evaluateSEXP("( ignore-key !0! !Pad 9! !Pad 8! !Pad 2! !Pad 4! !Pad 6! !Backspace! !\\! ![! !]! !=! !-! !Shift-A! !Shift-Z! !Shift-D! !Shift-V! !Shift-X! !Shift-E! !Shift-W! !Shift-I! !Shift-P! !Shift-C! !Shift-J! !Shift-R! !R! !Pad *! !Pad .! !Pad Enter! !Pad 0! !Pad /! !Pad +! )")
mn.evaluateSEXP("( ignore-key !0! !Pad -! !Pad 5! !'! !C! !Alt-J! !J! !Tab! !Insert! !Delete! !Home! !End! !Page Up! !Page Down! !Alt-D! !Q! !Up Arrow! !Down Arrow! !Left Arrow! !Right Arrow! !Scroll Lock! !Shift-Scroll Lock! !Alt-E! !Alt-Shift-E! !E! !Alt-R! !U! !Shift-U! !N! !K! !Shift-K! )")
mn.evaluateSEXP("( ignore-key !0! !B! !Shift-B! !1! !2! !3! !4! !Alt-X! !Shift-.! !Shift-,! !L! !Shift-N! !Shift-End! !Shift-O! !Shift-3! !Shift-1! !Shift-Pad +! !Shift-Pad Enter! !Alt-Shift-Q! !Alt-A! !Alt-N! !Alt-G! !O! )")

end

mn.LuaSEXPs["lua-set-cutscene-keylock"].Action = function(enable)

	if enable then
		CutsceneKeyLock()
	else
		CutsceneKeyUnLock()
	end

end

function RemoveAllDebris()
 
        for i = 1, #mn.Debris do
                mn.Debris[i].HitpointsLeft = 0
        end
 
end

mn.LuaSEXPs["lua-remove-all-debris"].Action = RemoveAllDebris

function SwapClassWithData(class, primary, secondary, wepEnergy, ...)

	if type(class) == "string" then
		class = tb.ShipClasses[class]
	end
	
	for _, data in ipairs (arg) do
		local ship = data[1]
		if type(ship) == "string" then
			ship = mn.Ships[ship]
		end

		if ship:isValid() and class:isValid() then	--always check validity of game objects!
			
			local priWeapons = {}	--Declaring primary and secondary weapon tables as an empty object for later population
			local priAmmo = {}
			local secWeapons = {}
			local secAmmo = {}   
			local wepEnergypct = 1
			
			if wepEnergy then wepEnergypct = ship.WeaponEnergyLeft / ship.WeaponEnergyMax end
			
			if primary then
				--First let's figure out what weapons we have
				for i=1, #ship.PrimaryBanks do		-- ship.PrimaryBanks has a # operator that returns the number of banks
					local thisBank = ship.PrimaryBanks[i]
					priWeapons[i] = thisBank.WeaponClass
					priAmmo[i] = thisBank.AmmoLeft / thisBank.AmmoMax	--Storing ammo counts as a percent of max ammo so if ammo capacity changes we're all good
				end
			end
			
			if secondary then
				for i=1, #ship.SecondaryBanks do		-- SecondaryBanks too
					local thisBank = ship.SecondaryBanks[i]
					secWeapons[i] = thisBank.WeaponClass
					secAmmo[i] = thisBank.AmmoLeft / thisBank.AmmoMax
				end
			end
			
			--Now we switch the ship class
			
			ship.Class = class
			
			--Now we swap back the info
			
			if wepEnergy then ship.WeaponEnergyLeft = wepEnergypct * ship.WeaponEnergyMax end
			
			if primary then
				for i=1, #ship.PrimaryBanks do
					local thisBank = ship.PrimaryBanks[i]
					thisBank.WeaponClass = priWeapons[i]
					thisBank.AmmoLeft = math.floor(priAmmo[i] * thisBank.AmmoMax)
				end
			end
			
			if secondary then
				for i=1, #ship.SecondaryBanks do
					local thisBank = ship.SecondaryBanks[i]
					thisBank.WeaponClass = secWeapons[i]
					thisBank.AmmoLeft = math.floor(secAmmo[i] * thisBank.AmmoMax)
				end
			end
			
		end
	end

end

mn.LuaSEXPs["lua-change-ship-class-2"].Action = SwapClassWithData

function InCampaign()

	return mn.isInCampaign()

end

mn.LuaSEXPs["lua-is-in-campaign"].Action = InCampaign

function SwapPositions(a,b, orientation)

	local intermediate = a.Position
	
	a.Position = b.Position
	
	b.Position = intermediate
	
	if orientation then
		local o = a.Orientation
		
		a.Orientation = b.Orientation
		
		b.Orientation = o
		
	end
	
end

mn.LuaSEXPs["lua-swap-ship-position"].Action = SwapPositions

function ReverseWaypointPath(waypointpath)

	local t = {}
	local pathsize = #waypointpath
	
	for i=1, pathsize do
		t[pathsize-i+1] = waypointpath[i].Position
	end
	
	for i=1, pathsize do
		waypointpath[i].Position = t[i]
	end
	
end

mn.LuaSEXPs["lua-reverse-waypoint"].Action = ReverseWaypointPath

function GetAllPositions()

	local t = {}

	for thisShip in mn.getShipList() do
	
		local info = {}
		info.Ship = thisShip.Name
		info.Position = "X:" .. thisShip.Position.x .. ", Y:" .. thisShip.Position.y .. ", Z:" .. thisShip.Position.z
		info.Orientation = "P:" .. thisShip.Orientation.p .. ", B:" .. thisShip.Orientation.b .. ", H:" .. thisShip.Orientation.h
		t[#t + 1] = info
	
	end
	
	if DumpToFile then DumpToFile(t,"pos") else ba.warning("DumpToFile function not found! Make sure you have 'zlua-utils-sct.tbm' installed!\n") end

end

mn.LuaSEXPs["lua-dump-positions-to-file"].Action = GetAllPositions

function ReplaceTexture(ship, oldtex, newtex)

	if type(ship) == "string" then
		ship = mn.Ships[ship]
	end
		
	local newTexture = gr.loadTexture(newtex)
	
	ship.Textures[oldtex] = newTexture

end

mn.LuaSEXPs["lua-replace-texture"].Action = function(ship,oldtex,newtex) ReplaceTexture(ship, oldtex, newtex) end

mn.LuaSEXPs["lua-get-ammo-pct"].Action = function(ship, banktype, bank)

	local thisBank
	
	if banktype == 1 then
		thisBank = ship.PrimaryBanks[bank+1]
	elseif banktype == 2 then
		thisBank = ship.SecondaryBanks[bank+1]
	end
	
	return (thisBank.AmmoLeft / thisBank.AmmoMax) * 100

end

mn.LuaSEXPs["lua-set-ammo-pct"].Action = function(ship, banktype, bank, pct)

	local thisBank
	
	if banktype == 1 then
		thisBank = ship.PrimaryBanks[bank+1]
	elseif banktype == 2 then
		thisBank = ship.SecondaryBanks[bank+1]
	end
	
	thisBank.AmmoLeft = math.floor((pct/100) * thisBank.AmmoMax)

end

--These are some WoD specific functions for moving debris. This is just here because it's in WoD's misc-sc-functions.
function GetDebrisOffsets(parent)

	if type(parent) == "string" then parent = mn.Ships[parent] end

	FlagshipDebris = {}
	
	for i=1, #mn.Debris do
		if mn.Debris[i]:isValid() then
			local thisDebris = mn.Debris[i]
			local t = {}
			t.Sig = thisDebris:getSignature()
			t.Offset = thisDebris.Position - parent.Position
			
			FlagshipDebris[i] = t
		end
	end

end

function MoveDebris(parent)

	if type(parent) == "string" then parent = mn.Ships[parent] end

	for i=1, #FlagshipDebris do
		if FlagshipDebris[i] then
			local thisDebrisEntry = FlagshipDebris[i]
			local thisDebris = mn.getObjectFromSignature(thisDebrisEntry.Sig)
			thisDebris.Position = parent.Position + parent.Orientation:unrotateVector(thisDebrisEntry.Offset)
		end
	end
	
	FlagshipDebris = nil

end

]

#End