﻿-- *** Example test gamemode ***
-- Since: v0.4.1

local STAGEC = require("modules.STAGEC")
local Game = require("modules.game")
local Ped = require("modules.ped")
local Car = require("modules.car")

os.setlocale("en", "numeric") -- Set float style 1,0 to 1.0

local gamestate = 0
local playersate = 0
local player = 0
local ped_pool = {}
local car = 0
local parkedcar = 0
local gun = 0
local phone_pool = {{struct = 0, arrow = 0, state = 0}}
local x, y, z = 0
local walk_speed = 0.8
local sprint_speed = 8

local STATIC_PLAYER_SPAWNS = {
	{x = 93.0, y = 90.0, z = 2.0, a = 0, model = 1, skin = 25},
	{x = 93.0, y = 91.0, z = 2.0, a = 0, model = 1, skin = 26},
	{x = 93.0, y = 92.0, z = 2.0, a = 0, model = 1, skin = 27},
	{x = 93.0, y = 93.0, z = 2.0, a = 0, model = 1, skin = 28},
	{x = 93.0, y = 94.0, z = 2.0, a = 0, model = 1, skin = 29},
	{x = 93.0, y = 95.0, z = 2.0, a = 0, model = 1, skin = 30}
}

local STATIC_MISSION_PHONES = {
	{x = 95.7, y = 92.4, z = 2.0, a = 0, model = 163}
}

function OnNewGame(ped)
	STAGEC.SetAmbientLevel(0.6, 0)	
	for i,phone in ipairs(STATIC_MISSION_PHONES) do
		phone_pool[i].struct = STAGEC.CreateObj(phone.x, phone.y, phone.z, phone.a, phone.model, 0, 1)
		phone_pool[i].arrow = STAGEC.CreateArrow(phone.x, phone.y, phone.z, 0, 4)
		wait(50)
	end		
	local playerid = GetPedID(ped)
	player = GetPedPlayer(ped)
	SetPlayerGameCam(player, STATIC_PLAYER_SPAWNS[playerid].x,STATIC_PLAYER_SPAWNS[playerid].y,STATIC_PLAYER_SPAWNS[playerid].z)
	SetPedPos(ped, STATIC_PLAYER_SPAWNS[playerid].x,STATIC_PLAYER_SPAWNS[playerid].y,STATIC_PLAYER_SPAWNS[playerid].z)
	SetPedAngle(ped, STATIC_PLAYER_SPAWNS[playerid].a)	
	SetPedHealth(ped, 100)	
	SetPedModel(ped, STATIC_PLAYER_SPAWNS[playerid].model)
	SetPedSkin(ped, STATIC_PLAYER_SPAWNS[playerid].skin)
	for i,phone in ipairs(phone_pool) do
		STAGEC.SetPedPhoneState(ped, phone.struct, 40, 1)
		phone_pool[i].state = 1
	end
	gamestate = 2
	playersate = 1
	WriteInLog(string.format("Player id %d struct %X new game is started.\n", playerid, ped))
end

function OnPlayerSpawn(ped)
	local playerid = GetPedID(ped)
	player = GetPedPlayer(ped)
	SetPlayerGameCam(player, STATIC_PLAYER_SPAWNS[playerid].x,STATIC_PLAYER_SPAWNS[playerid].y,STATIC_PLAYER_SPAWNS[playerid].z)
	SetPedPos(ped, STATIC_PLAYER_SPAWNS[playerid].x,STATIC_PLAYER_SPAWNS[playerid].y,STATIC_PLAYER_SPAWNS[playerid].z)
	SetPedAngle(ped, STATIC_PLAYER_SPAWNS[playerid].a)
	SetPedModel(ped, STATIC_PLAYER_SPAWNS[playerid].model)
	SetPedSkin(ped, STATIC_PLAYER_SPAWNS[playerid].skin)
	for i,phone in ipairs(phone_pool) do
		x, y, z = GetObjPos(phone.struct)
		STAGEC.SetPedPhoneState(ped, phone.struct, 40, 1)
		phone_pool[i].state = 1
		phone_pool[i].arrow = STAGEC.CreateArrow(x, y, z, 0, 4)
	end
	playersate = 1
	WriteInLog(string.format("Player id %d struct %X is spawned.\n", playerid, ped))
end

function OnPlayerDead(ped)
	STAGEC.DeleteCar(parkedcar)
	STAGEC.DeleteObj(gun)
	WriteInLog(string.format("Player id %d struct %X is died.\n", GetPedID(ped), ped))	
	playersate = 0
end

function CheakPhones(ped)
	for i,phone in ipairs(phone_pool) do
		if (phone.state == 1) then
			x, y, z = GetObjPos(phone.struct)
			if (GetPedRangeOfPoint(ped, x, y, z) <= 0.4) then
				if (i == 1) then
					STAGEC.SetPedPhoneState(ped, phone_pool[i].struct, 40, 0)
					STAGEC.DeleteArrow(phone_pool[i].arrow)			
					parkedcar = STAGEC.CreateCar(93.4, 92.8, 2.0, 270, 12, 65535, 65535, 1)
					gun = STAGEC.CreateObj(93.4, 90.8, 2.0, 90, 65, 60, 2)
					SetPedModel(ped, 2)
					SetPedSkin(ped, 0)
					phone.state = 0	
					return
				end
			end
		end
	end
end

while(GetGameFrame() == 0) do end

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

while (true) do	
	wait(0)
	if (GetGameFrame() > 0) then
		if (gamestate == 0) then -- If start new game
			wait(100)			
			for i = 1, 6, 1 do
				ped = GetPedStruct(i)
				--WriteInLog(string.format("ped index %d id %d struct %X\n",i,GetPedID(ped),ped))
				ped_pool[i] = ped
			end
			gamestate = 1
		else
			for i,ped in ipairs(ped_pool) do
				if (ped ~= 0) then				
					if (gamestate == 1 and GetPedHealth(ped) > 0) then
						OnNewGame(ped)
					end
					if (playersate == 1 and GetPedHealth(ped) <= 0) then
						SetPedTask(ped, 9)
						OnPlayerDead(ped)
						while (GetPedTask(ped) == 9) do end
					end
					if (playersate == 0 and GetPedHealth(ped) > 0) then
						OnPlayerSpawn(ped)
					end
					CheakPhones(ped)
					if (IsKeyDown(0x43)) then -- Down key "C" for save current position in MISI.log
						car = GetPedCar(ped)
						if (car ~= 0) then
							x, y, z = GetCarPos(car)
							WriteInLog(string.format("Saved car id %d, struct %X, {x = %.01f, y = %.01f, z = %.01f, a = %.01f, model = %d, color = %d}, name %s\n", GetCarID(car), car, x, y, z, GetCarAngle(car), GetCarModel(car), GetCarColor(car), Car.GetCarName(car)))
						else
							x, y, z = GetPedPos(ped)
							WriteInLog(string.format("Saved ped id %d, struct %X, {x = %.01f, y = %.01f, z = %.01f, a = %.01f, model = %d, skin = %d, weapon = %d}, name %s\n", GetPedID(ped), ped, x, y, z, GetPedAngle(ped), GetPedModel(ped), GetPedSkin(ped), GetPedCurrentWeaponID(ped), Ped.GetWeaponName(GetPedCurrentWeaponID(ped))))
						end
					elseif (IsKeyPress(0xA2)) then -- Press key CTRL for walking
						SetPedSpeed(ped, walk_speed)
					elseif (IsKeyPress(0xA0)) then -- Press key LSHIFT for sprinting
						SetPedSpeed(ped, sprint_speed)
					elseif (IsKeyDown(0x47)) then -- Down key G for enter as passenger
						x, y, z = GetPedPos(ped)
						car_pool = GetCarFromPoint(x, y, z, 2)
						for i,car in ipairs(car_pool) do
							if (car ~= 0) then
								--WriteInLog(string.format("car index %d id %d struct %X\n",i,GetCarID(car),car))
								SetPedCurrentCarDoorID(ped, 1)
								STAGEC.SetPedObjective(ped, car, 0, 0, 0, 100, 2)
								break
							end
						end
					end
				end
			end
		end
	else
		wait(100)
		gamestate = 0 -- If not frame to game restart or game not loaded
	end
end