﻿-- *** Example test gamemode ***
-- Since: v0.2b
-- Demonstration: change of player's position 1 (respawn), change skin, model, set max. running speed, save current coordinates in the log
-- Press key HOME (numpad), the player searches for the player at number 2 and changes his skin and teleports to the player at number 1

local gamestate = 0
local respawn = 0
local died = 0

function IsPedAlive(ped)
	if (ped ~= 0) then
		return true
	end
	return false
end

function IsPedInCar(ped)
	local car = GetPedCar(ped)
	if (car ~= 0) then
		return true
	end
	return false
end

function OnPedNewGame(ped)
	local player = GetPlayerStruct(ped)
	SetPlayerGameCam(player, 93,92,1)
	SetPlayerArmour(player, 200)
	SetPedHealth(ped, 200)
	SetPedPos(ped, 93,92,2)
	SetPedAngle(ped, 90)
	SetPedMaxSpeed(ped, 1024 + 128)
	SetPedSkin(ped, 0)
	SetPedModel(ped, 2)
	gamestate = 1
	respawn = 1
	WriteInLog(string.format("New game is started.\n" ))
end

function OnPedSpawn(ped)
	local player = GetPlayerStruct(ped)
	SetPlayerGameCam(player, 93,92,1)
	SetPedPos(ped, 93,92,2)
	SetPedAngle(ped, 90)
	SetPedMaxSpeed(ped, 1024 + 128)
	SetPedSkin(ped, 1)
	SetPedModel(ped, 2)
	respawn = 1
	died = 0
	WriteInLog(string.format("Player is spawned.\n" ))
end

function OnPedDied(ped)
	respawn = 0
	died = 1
	WriteInLog(string.format("Player is died.\n" ))	
end

while(true) do
	if (GetGameFrame() > 0) then
		local ped = GetPedStruct(1)
		if (IsPedAlive(ped)) then
			if (died == 0 and GetPedTask(ped) == 9) then
				OnPedDied(ped)
			end
			local player = GetPlayerStruct(ped)
			if (GetPedHealth(ped) <= 0 and gamestate > 0) then
				SetPedTask(ped, 9)
			end
			if (gamestate == 0 and GetPedHealth(ped) > 0 and GetPedTask(ped) == 7) then
				OnPedNewGame(ped)
			end
			if (respawn == 0 and GetPedHealth(ped) > 0 and GetPedTask(ped) == 7) then
				OnPedSpawn(ped)
			end
			if (IsPedInCar(ped)) then
				local car = GetPedCar(ped)
				local x, y, z = GetCarPos(car)
				--WriteInLog(string.format("In car id %d: model %d engine %d doorlock %d damage %d struct %X pos %.01f %.01f %.01f rot %.01f\n", GetCarID(car), GetCarModel(car), GetCarEngine(car), GetCarDoorLock(car), GetCarDamage(car), car, x,y,z, GetCarAngle(car)))
			else
				if (IsGameKeyPress(player, 0x74)) then
					local targetped = GetPedStruct(2)
					local targetpedplayer = GetPlayerStruct(targetped)
					local x,y,z = GetPedPos(ped)
					SetPlayerGameCam(targetpedplayer, x,y,z)
					SetPlayerWeaponSlot(targetpedplayer, 0)
					SetPedPos(targetped, x,y,z + 1)
					SetPedAngle(targetped, GetPedAngle(ped))
					SetPedMaxSpeed(targetped, 512)
					SetPedModel(targetped, 2)
					SetPedSkin(targetped, 25)
				end
				if (IsGameKeyPress(player, 0x86)) then
					SetPedHealth(ped, 0)
				end
				local x,y,z = GetPedPos(ped)
				--WriteInLog(string.format("In foot id %d: %X skin %d model %d money %d health %.01f spec %d pos %.01f %.01f %.01f rot %.01f\n", GetPedID(ped), ped, GetPedSkin(ped), GetPedModel(ped), GetPlayerMoney(player), GetPedHealth(ped), GetPedSpecMode(ped), x,y,z, GetPedAngle(ped)))
			end					
		else
			gamestate = 0
			respawn = 0	
		end
	else
		gamestate = 0
		respawn = 0	
		WriteInLog(string.format("No game frame...\n" ))
	end
	wait(200)
end