-- Thanks to AltF4_ToExit, Shurutsue, and Turbo. AND MASSIVE THANKS TO AZAKAELA!
--You may use this to spawn an item ANYWHERE in game, based on coordnitates you can get in World Ed -- Ask Azakaela if you have any questions!
--This is very useful for placing down story related items, such as notes, or items you want the player to have like... a sledgehammer for instance.


InsurgentStoryItems = InsurgentStoryItems or {};

local function customItemSpawnInit()
  local itemData = ModData.get("InsurgentCustomItemSpawns");
  if itemData == nil then
    itemData = {
	  --So you can escape
	  {x = 5585, y = 12447, z = 0, spawned = false, sX = 0.5, sY = 0.5, sZ = 0, item = "Insurgent.DeployedParachute"}, --Military base parachute 1
	  {x = 5596, y = 12447, z = 0, spawned = false, sX = 0.5, sY = 0.5, sZ = 0, item = "Insurgent.DeployedParachute"}, --Military base parachute 2
	  {x = 5527, y = 12591, z = 0, spawned = false, sX = 0.5, sY = 0.5, sZ = 0, item = "Insurgent.DeployedParachute"}, --Military base biomed building (KY facility)
	  {x = 5626, y = 12601, z = 0, spawned = false, sX = 0.5, sY = 0.5, sZ = 0, item = "Insurgent.DeployedParachute"}, --Military base biomed building (KY Facility)  
	  {x = 13770, y = 5817, z = 0, spawned = false, sX = 0.5, sY = 0.5, sZ = 0, item = "Insurgent.DeployedParachute"}, --Mall parachute
	  {x = 13741, y = 6695, z = 0, spawned = false, sX = 0.5, sY = 0.5, sZ = 0, item = "Insurgent.DeployedParachute"}, --Valley parachute
	  {x = 11273, y = 6625, z = 0, spawned = false, sX = 0.5, sY = 0.5, sZ = 0, item = "Insurgent.DeployedParachute"}, --Westpoint dock parachute
	  {x = 7643, y = 11938, z = 0, spawned = false, sX = 0.5, sY = 0.5, sZ = 0, item = "Insurgent.DeployedParachute"}, --Prison parachute	  
	  {x = 5538, y = 12470, z = 0, spawned = false, sX = 0.5, sY = 0.5, sZ = 0, item = "Insurgent.Sample1"}, 
	  {x = 5562, y = 12476, z = 0, spawned = false, sX = 0.5, sY = 0.5, sZ = 0, item = "Insurgent.ClassifiedDocument"}, 	  
	  {x = 5528, y = 12656, z = 0, spawned = false, sX = 0.5, sY = 0.5, sZ = 0, item = "Insurgent.Sample1"}, 			--KY version
	  {x = 5560, y = 12482, z = 0, spawned = false, sX = 0.5, sY = 0.5, sZ = 0, item = "Insurgent.ClassifiedDocument"}, --KY version 	  	  
    };
    ModData.add("InsurgentCustomItemSpawns", itemData);
  end
  InsurgentStoryItems.toSpawn = {};
  for i, v in pairs(itemData) do
    if not v.spawned then
      table.insert(InsurgentStoryItems.toSpawn, v);
    end
  end
  Events.LoadGridsquare.Add(checkItemSpawns);
end

function checkItemSpawns(sq)
  local x, y, z = sq:getX(), sq:getY(), sq:getZ();
  if #InsurgentStoryItems.toSpawn > 0 then
    for i = #InsurgentStoryItems.toSpawn, 1, - 1 do
      local itemSpawn = InsurgentStoryItems.toSpawn[i];
      if x == itemSpawn.x and y == itemSpawn.y and z == itemSpawn.z then
        sq:AddWorldInventoryItem(itemSpawn.item, itemSpawn.sX, itemSpawn.sY, itemSpawn.sZ);
        itemSpawn.spawned = true;
        table.remove(InsurgentStoryItems.toSpawn, i);
      end
    end
  else
    Events.LoadGridsquare.Remove(checkItemSpawns);
  end
end

Events.OnGameStart.Add(customItemSpawnInit);
