HeliSpawnLocation = {{7746, 11802, 0}, {5556, 12483, 1}}
ArmorSpawnLocation = {{5593, 12465}, {12850, 6427}, {12598, 947}}
local WarThunderSpawn = {}
function WarThunderSpawn.initGMD()
    ModData.request("WarThunderSpawn");
end

function WarThunderSpawn.OnReceiveGlobalModData(_module, _packet)
    if _module == "WarThunderSpawn" then
        if _packet then
            ModData.remove("WarThunderSpawn")
            ModData.add("WarThunderSpawn", _packet)
        end
    end
end

Events.OnInitGlobalModData.Add(WarThunderSpawn.initGMD)
Events.OnReceiveGlobalModData.Add(WarThunderSpawn.OnReceiveGlobalModData)

local function spawnVehicle(Type, x, y, z, burned, chance, DefaultSpawn)
    local Type = Type
    if DefaultSpawn then
        sendClientCommand(getPlayer(), "WarThunderSpawn", "SpawnNewDefault", {
            x = x,
            y = y,
            z = z,
            VehicleType = Type,
            chance = chance,
            burned = burned
        })
    else
        sendClientCommand(getPlayer(), "WarThunderSpawn", "SpawnNew", {
            x = x,
            y = y,
            z = z,
            VehicleType = Type,
            chance = chance,
            burned = burned
        })
    end

end

local function CheckDistance(player, x, y)
    local PlayerX, PlayerY = player:getX(), player:getY()
    local Distance = math.sqrt((PlayerX - x) ^ 2 + (PlayerY - y) ^ 2)
    return Distance
end

local function CheckSpawnLocation()

    local player = getPlayer()
    local ModData = ModData.get("WarThunderSpawn")
    if HeliList then
        for k, v in pairs(HeliList) do
            if v.SpawnLocation then
                for i = 1, #v.SpawnLocation do
                    local Type = v.SpawnLocation[i].x .. "_" .. v.SpawnLocation[i].y .. "_" .. v.SpawnLocation[i].z
                    if not ModData.Location[k] then
                        if CheckDistance(player, v.SpawnLocation[i].x, v.SpawnLocation[i].y) < 100 then
                            spawnVehicle(k, v.SpawnLocation[i].x, v.SpawnLocation[i].y, v.SpawnLocation[i].z,
                                v.SpawnLocation[i].burned, v.SpawnLocation[i].chance)
                        end
                    else
                        if not ModData.Location[k][Type] then
                            if CheckDistance(player, v.SpawnLocation[i].x, v.SpawnLocation[i].y) < 100 then
                                spawnVehicle(k, v.SpawnLocation[i].x, v.SpawnLocation[i].y, v.SpawnLocation[i].z,
                                    v.SpawnLocation[i].burned, v.SpawnLocation[i].chance)
                            end
                        end
                    end
                end
            end
        end
        for i = 1, #HeliSpawnLocation do
            local Type = HeliSpawnLocation[i][1] .. "_" .. HeliSpawnLocation[i][2] .. "_" .. HeliSpawnLocation[i][3]
            if ModData.DefaultSpawn[Type] then
            else
                if CheckDistance(player, HeliSpawnLocation[i][1], HeliSpawnLocation[i][2]) < 100 then
                    local SpawnHeliTable = {}
                    for k, v in pairs(HeliList) do
                        if not v.SpawnLocation then
                            table.insert(SpawnHeliTable, k)
                        end
                    end
                    if #SpawnHeliTable ~= 0 then
                        spawnVehicle(SpawnHeliTable[ZombRand(1, #SpawnHeliTable)], HeliSpawnLocation[i][1],
                            HeliSpawnLocation[i][2], HeliSpawnLocation[i][3], false, 100, true)
                    end
                end
            end
        end
    end
    if PanzerList then
        for k, v in pairs(PanzerList) do
            if v.SpawnLocation then
                for i = 1, #v.SpawnLocation do
                    local Type = v.SpawnLocation[i].x .. "_" .. v.SpawnLocation[i].y
                    if not ModData.Location[k] then
                        if CheckDistance(player, v.SpawnLocation[i].x, v.SpawnLocation[i].y) < 100 then
                            spawnVehicle(k, v.SpawnLocation[i].x, v.SpawnLocation[i].y, 0, v.SpawnLocation[i].burned,
                                v.SpawnLocation[i].chance)
                        end
                    else
                        if not ModData.Location[k][Type] then
                            if CheckDistance(player, v.SpawnLocation[i].x, v.SpawnLocation[i].y) < 100 then
                                spawnVehicle(k, v.SpawnLocation[i].x, v.SpawnLocation[i].y, 0,
                                    v.SpawnLocation[i].burned, v.SpawnLocation[i].chance)
                            end
                        end
                    end
                end
            end
        end
        for i = 1, #ArmorSpawnLocation do
            local Type = ArmorSpawnLocation[i][1] .. "_" .. ArmorSpawnLocation[i][2]
            if ModData.DefaultSpawn[Type] then

            else
                if CheckDistance(player, ArmorSpawnLocation[i][1], ArmorSpawnLocation[i][2]) < 100 then
                    local SpawnArmorTable = {}
                    for k, v in pairs(PanzerList) do
                        if not v.SpawnLocation then
                            table.insert(SpawnArmorTable, k)
                        end
                    end
                    if #SpawnArmorTable ~= 0 then
                        spawnVehicle(SpawnArmorTable[ZombRand(1, #SpawnArmorTable)], ArmorSpawnLocation[i][1],
                            ArmorSpawnLocation[i][2], 0, false, 100, true)
                    end

                end
            end
        end
    end

end

function ResetAllSpawnArea()
    sendClientCommand(getPlayer(), "WarThunderSpawn", "ClearTheSpawnDate", {})
end

if SandboxVars.WT.SpawnTigerInWorld then
    Events.EveryOneMinute.Add(CheckSpawnLocation)
end

