-- $Id: para.lua 27-Dec-2008 Frogs Friend $

para = {}
para.detail = para.detail or {};


--[[
Infantry_Full_Plane	0
Anti_Tank_Team_11_men	1
Commandos_11_men		2 
Specialists_9_men		3
Heavy_Weapons		4
Ammo/Fuel			5
Infantry_1_Squad_14_men	6
Infanty+Mech_23_men	7
Commandos_14_men		8
--]]

-- Maximum troops in transport: germany =18, ussr =28, usa =28, uk =28, japan =16, france =20

para.detail.airborne =
{
	usa = {
		[0] = {
			{desc = "Mods/Sainte Marie du Mont/american_airborne_officer.dsc", num=2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_medic.dsc", num=2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_bazooka.dsc", num=2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_lmg_30c.dsc", num=4},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_flamethrower.dsc", num=2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_sniper.dsc", num=1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_rifleman.dsc", num=10},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_smg.dsc", num=5},
		},
		[1] = {
			{desc = "Mods/Sainte Marie du Mont/american_airborne_officer.dsc", num=1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_rifleman.dsc", num=2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_bazooka.dsc", num=2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_sniper.dsc", num=2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_smg.dsc", num=4},
		},
		[2] = {
			{desc = "Mods/Sainte Marie du Mont/american_airborne_officer.dsc", num=1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_medic.dsc", num=1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_bazooka.dsc", num=1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_saboteur.dsc", num=4},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_rifleman.dsc", num=4},
		},
		[3] = {
			{desc = "Mods/Sainte Marie du Mont/american_airborne_officer.dsc", num = 1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_medic.dsc", num = 1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_mech.dsc", num = 2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_sniper.dsc", num = 1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_flamethrower.dsc", num = 2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_sapper.dsc", num = 1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_bazooka.dsc", num = 1},
		},
		[4] = {
			{desc = "Mods/Sainte Marie du Mont/american_airborne_willys_supply.dsc", num = 2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_37mm_m3a1.dsc", num = 1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_75mm_m1a1.dsc", num = 1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_30_cal_hmg.dsc", num = 1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_50_cal_hmg.dsc", num = 2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_willys_lmg_50c.dsc", num = 2},
		},
		[5] = {
			{desc = "Mods/Sainte Marie du Mont/american_airborne_willys_supply.dsc", num = 2},
			{desc = "Objects/ammo_boxes/ammo_4000.dsc", num = 3},
			{desc = "Objects/ammo_boxes/fuel_200.dsc", num = 3},
		},
		[6] = {
			{desc = "Mods/Sainte Marie du Mont/american_airborne_lmg_30c.dsc", num = 1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_rifleman.dsc", num = 6},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_bazooka.dsc", num = 2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_flamethrower.dsc", num = 1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_smg.dsc", num = 2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_medic.dsc", num = 1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_officer.dsc", num = 1},
		},
		[7] = {
			{desc = "Mods/Sainte Marie du Mont/american_airborne_mech.dsc", num = 4},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_flamethrower.dsc", num = 2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_medic.dsc", num = 1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_lmg_30c.dsc", num = 2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_officer.dsc", num = 2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_smg.dsc", num = 4},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_rifleman.dsc", num = 8},
		},
		[8] = {
			{desc = "Mods/Sainte Marie du Mont/american_airborne_officer.dsc", num=1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_rifleman.dsc", num=3},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_lmg_30c.dsc", num = 2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_medic.dsc", num=1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_sniper.dsc", num = 2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_bazooka.dsc", num=2},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_flamethrower.dsc", num = 1},
			{desc = "Mods/Sainte Marie du Mont/american_airborne_saboteur.dsc", num=2},
		},
	},
}

function para.airborne(player_nation, reinf_type)
	assert(player_nation and reinf_type, "one argument is nil");
	local nation = para.detail.airborne[player_nation];
	return nation[reinf_type];
end

function para.ParaPlane(Player, Position, Gate, GateOut, Radius, Cargo, CargoGroup)
	Radius = Radius or 5
	local s = AIparts:AddOneTime(AIparts:GetUniqueId("PLANE"), 1, function(self) mission_lua.PlaneBonus(self) end);
	local p = GetPlayer(Player);
	s.player = p.Id;
	local pos = utils.copy_value(mission.Waypoints[Position]);
	if s.position then s.position = GetRandomRadiusPoint(s.position, Radius) end
	s.desc = utils.getplane(p.CountryName, "transport")
	s.gate = Gate
	s.cargo = para.airborne(p.CountryName, Cargo)
	s.cargo.group = CargoGroup
	s.commands = {{unload_to = pos}, {dislocation = mission_lua.CalculateGate(GateOut)}, {disappear = true}}
end

function para.FighterPlane(Player, Position1, Position2, Position3, Gate, GateOut, Radius)
	Radius = Radius or 5
	local s = AIparts:AddOneTime(AIparts:GetUniqueId("PLANE"), 1, function(self) mission_lua.PlaneBonus(self) end);
	local p = GetPlayer(Player);
	s.player = p.Id;
	local pos1 = utils.copy_value(mission.Waypoints[Position1]);
	local pos2 = utils.copy_value(mission.Waypoints[Position2]);
	local pos3 = utils.copy_value(mission.Waypoints[Position3]);
	if s.position then s.position = GetRandomRadiusPoint(s.position, Radius) end
	s.desc = utils.getplane(p.CountryName, "fighter")
	s.gate = Gate
	s.commands = {{attack_place = pos1}, {dislocation = GetRandomRadiusPoint(pos1, 800)}, {attack_place = pos2}, {attack_place = pos3}, {dislocation = mission_lua.CalculateGate(GateOut)}, {disappear = true}}
end

function para.IsObjectDestroyed(ObjectId)
	local object = GetObject(ObjectId)
	if object then
		return object.Health
	else
		return 0
	end
end

function para.GetPlayer(ObjectId)
	local object = GetObject(ObjectId)
	if object then
		return object.PlayerId
	else
		return 99
	end
end

function para.IsUnitDead(ObjectID, PlayerName)

    local p = mission.Players.ByName[PlayerName]
    local units = p:GetObjectList()
    local result = false
 
    for _,u in pairs(units) do
        if u and u.Id == ObjectID then 
            --log("Unit " .. ObjectID .. " is alive")
            return true
        end
    end
    --log("Unit " .. ObjectID .. " is dead")
    return false

end
