-- Guaranteed Military Vehicle Spawns at fixed coordinates
-- These vehicles will ALWAYS spawn at these locations

local guaranteedSpawns = {
    -- Format: {vehicleType, x, y, z, direction}
    -- x, y = world coordinates
    -- z = 0 for ground level
    -- direction = IsoDirections name: "N", "NE", "E", "SE", "S", "SW", "W", "NW"  (or nil for random)

    
    {"Base.SemiTruckBox_mil", 10670, 10411, 0, "S"},-- Muldraugh Police impound
    {"Base.SemiTruckBox_mil", 11668, 9940, 0, "S"}, -- Railyard

    {"Base.SemiTrailerVan_mil", 12449, 4261, 0, "S"}, -- Louisville checkpoint
    {"Base.SemiTruck_mil", 12466, 4313, 0, "N"},       -- Louisville checkpoint

    {"Base.SemiTrailerVan_mil", 15468, 3004, 0, "S"}, -- Louisville Airport

}

-- Inject additional spawns when specific map mods are active


--Secretz42 W.I.P. - add more locations as needed

if getActivatedMods():contains("\\Secretz42") then 
    -- Secretz42 map mod locations
    table.insert(guaranteedSpawns, {"Base.SemiTruck_mil",       9775, 13152,  0, "E"})
    table.insert(guaranteedSpawns, {"Base.SemiTrailerVan_mil",  10329, 12521, 0, "S"}) -- March Ridge checkpoint
end


if getActivatedMods():contains("\\RavenCreekB42") then 
    -- RavenCreekB42 map mod locations
    table.insert(guaranteedSpawns, {"Base.SemiTruck_mil",       6499, 15337,  0, "W"})
    table.insert(guaranteedSpawns, {"Base.SemiTrailerVan_mil",  6514, 15338, 0, "W"}) -- RavenCreekB42
end


-- if activeMods:contains("SomeOtherMod") then
--     table.insert(guaranteedSpawns, {"Base.SemiTruckBox_mil", 12345, 6789, 0, "W"})
-- end

Events.OnInitGlobalModData.Add(function()
    Events.LoadGridsquare.Add(function(square)
        local sx = square:getX()
        local sy = square:getY()

        for i, spawnData in ipairs(guaranteedSpawns) do
            if spawnData[2] == sx and spawnData[3] == sy then
                local vehicleType = spawnData[1]
                local direction   = spawnData[5]
                local spawnKey    = vehicleType .. "_" .. sx .. "_" .. sy

                local modData = ModData.getOrCreate("rSemiTruck_GuaranteedSpawns")
                if not modData[spawnKey] then
                    if not square:isVehicleIntersecting() then
                        local isoDir = direction and IsoDirections.fromString(direction) or IsoDirections.getRandom()
                        local vehicle = addVehicleDebug(vehicleType, isoDir, nil, square)
                        if vehicle then
                            modData[spawnKey] = true
                            ModData.transmit("rSemiTruck_GuaranteedSpawns")
                            --print(("rSemiTruck: Spawned " .. vehicleType .. " at (" .. sx .. ", " .. sy .. ")")
                        else
                            --print(("rSemiTruck: ERROR - addVehicleDebug returned nil for " .. vehicleType)
                        end
                    end
                end
                break
            end
        end
    end)
end)
