-- *** Example spawning car ***
-- Since: v0.4.2
-- Make new car is area player.

-- Don't require from directory scripts it need load from special folder modules or other folder
local STAGEC = require("modules.STAGEC")
local Car = require("modules.car")

local gamestate = 0
local ped_pool = {} 
local new_car = 0

-- Wait update frame
while (GetGameFrame() == 0) do end 

-- Load STAGEC.SCR (Stage Constructor)
if (GetSCRName() ~= "data/STAGEC.SCR") then
	SetSCRName("data/STAGEC.SCR")
end

while (true) do	
	wait(0)
	if (GetGameFrame() > 0) then
		if (gamestate == 0) then -- If start new game
			wait(50)
			for i = 1, 6 do
				ped_pool[i] = GetPedStruct(i)
			end
			if (#ped_pool > 0) then
				gamestate = 1
			end
		else
			if (gamestate == 1) then
				for i,ped in ipairs(ped_pool) do		
					if (IsKeyDown(0x31)) then -- Down key "1" for add car
						local x, y, z = GetPedPos(ped)		
						new_car = STAGEC.CreateCar(x + 2, y, z, GetPedAngle(ped), 54, 65535, 65535, 1)
						WriteInLog(string.format("Spawning car id %d struct %X name %s\n", GetCarID(new_car), new_car, Car.GetCarName(new_car)))
					end
					if (IsKeyDown(0x32)) then -- Down key "2" for delete car
						STAGEC.DeleteCar(new_car) -- Delete car
					end	
				end
			end
		end
	else
		gamestate = 0 -- If not frame to game restart or game not loaded
	end
end