-- ArmoredPacksm4hLMod by 2S v2.2.0

-- NOTE:
-- Please do not distribute this code without permission.
-- You can edit this file only for personal use. 
-- If this code is re-released in your mod, please add credits.
-- Thanks..

-------------------------------------------------------------------------------------------------------------------
local ArmoredPacksm4hLMod = {}
function ArmoredPacksm4hLMod.unload()	
	UI.REMOVE_BLIP(blip)
end

local Notification = {}

function Notification.unload() end
function Notification.init() end
function Notification.tick() end

function notify(msg) 
	UI._SET_NOTIFICATION_TEXT_ENTRY("STRING")
	UI._ADD_TEXT_COMPONENT_STRING(msg)
	UI._DRAW_NOTIFICATION(FALSE, FALSE)
end

--DISPLAY BLIP
local blip = UI.ADD_BLIP_FOR_COORD(159.324,-254.644,51.400)	
		UI.SET_BLIP_COLOUR(blip, 1)
		UI.SET_BLIP_SPRITE(blip, 84)
		UI.SET_BLIP_NAME_FROM_TEXT_FILE(blip, "Men4Hired")
		UI.SET_BLIP_SCALE(blip, 1)
		UI.SET_BLIP_AS_SHORT_RANGE(blip, true)

-------------MENU KEYS----------------
--NUMPAD
local openMenukey = Keys.E
local upKey = Keys.NumPad8
local downKey = Keys.NumPad2
local selectKey = Keys.NumPad5
local cancelKey = Keys.NumPad0

--ARROW KEYS
local arrowupKey = Keys.Up
local arrowdownKey = Keys.Down
local arrowselectKey = Keys.Return
local arrowcancelKey = Keys.Back

-------------------------------------------------------------------------------------------------------------------
--DO NOT EDIT BEYOND THIS POINT------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------

function mainM4hLInit()
	APM4hLGUI = APM4hLGUIinit()

	--MENU
	APM4hLGUI.mainMenu("main", "", "THE LOST MC - MEN 4 HIRED", 0.17, 0.1, 0.234, 0.0522, 0.003, 0.35, 2)
	APM4hLGUI.addButton("main", "Hexer & 1 Men $8000", spawnM4HLMC,"hexer", false, 0.35, 0)
	APM4hLGUI.addButton("main", "Minivan & 2 Men $15000", spawnM4HLMC,"minivan", false, 0.35, 0)	
	APM4hLGUI.addButton("main", "Burrito & 3 Men $23500", spawnM4HLMC,"gburrito", false, 0.35, 0)
end

local ArmoredPacksm4hLMod = {}

function ArmoredPacksm4hLMod.unload()
end
function ArmoredPacksm4hLMod.init()
mainM4hLInit()

	--NUMPAD
	local E = openMenukey
	local numpad8 = upKey
	local numpad2 = downKey
	local numpad5 = selectKey
	local numpad0 = cancelKey
	
	--ARROW KEYS
	local keyup = arrowupKey
	local keydown = arrowdownKey
	local keyenter = arrowselectKey
	local keybackspace = arrowcancelKey

	APM4hLGUI.updateSelection = {["menuOpen"] = {["keys"] = {E},["menu"] = "main"},["menuUp"] = {["keys"] = {numpad8,keyup}}, 
		["menuDown"] = {["keys"] = {numpad2,keydown}},["menuCancel"] = {["keys"] = {numpad0,keybackspace}},["select"] = {["keys"] = {numpad5,keyenter}}}
end
function ArmoredPacksm4hLMod.tick()
	

	
		APM4hLGUI.tick()
		
end

-------------------------------------------------------------------------------------------------------------------

function APM4hLGUIinit()
	APM4hLGUI = {}
	APM4hLGUI.menu = {}
	APM4hLGUI.buttons = {}
	APM4hLGUI.updateSelection = {["menuOpen"] = {["keys"] = {},["menu"] = ""},["menuUp"] = {["keys"] = {}},
		["menuDown"] = {["keys"] = {}},["menuCancel"] = {["keys"] = {}},["select"] = {["keys"] = {}}}
	APM4hLGUI.activeButton = 1
	APM4hLGUI.activeMenu = ""
	APM4hLGUI.IndexStatus = {["text"] = "", ["time"] = 0}

	function APM4hLGUI.mainMenu(menuName, prevMenu, title, xmin, ymin, xmax, ymax, Space, textScale, font)
		if(APM4hLGUI.menu[menuName] == nil) then
			APM4hLGUI.menu[menuName] = {}
			lastOffset = ymin + ymax + Space
		end

		APM4hLGUI.menu[menuName] = {["prevMenu"] = prevMenu,["title"] = title,["xmin"] = xmin,["ymin"] = ymin,
			["xmax"] = xmax,["ymax"] = ymax,["spacing"] = Space,["textScale"] = textScale,
			["font"] = font,["lastOffset"] = lastOffset,["numButtons"] = 0}
	end
	
	function APM4hLGUI.addButton(mainMenu, text, funct, args, selectable, textScale, font)
		if(APM4hLGUI.buttons[mainMenu] == nil or APM4hLGUI.buttons[mainMenu]["settings"] == nil) then
			APM4hLGUI.buttons[mainMenu] = {["settings"] = {}}
		end

		local currButtonNum = #APM4hLGUI.buttons[mainMenu].settings + 1
		APM4hLGUI.menu[mainMenu].numButtons = currButtonNum

		if(selectable) then
			selectableOn = false
		else
		    selectableOn = nil
		end

		APM4hLGUI.buttons[mainMenu].settings[currButtonNum] = {["mainMenu"] = mainMenu,["text"] = text,["funct"] = funct,
			["args"] = args,["font"] = font,["xmin"] = APM4hLGUI.menu[mainMenu].xmin,
			["ymin"] = APM4hLGUI.menu[mainMenu].lastOffset,["xmax"] = APM4hLGUI.menu[mainMenu].xmax,
			["ymax"] = APM4hLGUI.menu[mainMenu].ymax,["textScale"] = textScale,
			["selectable"] = selectable,["selectableOn"] = selectableOn}

		APM4hLGUI.menu[mainMenu]["lastOffset"] = (APM4hLGUI.menu[mainMenu].lastOffset + APM4hLGUI.menu[mainMenu].ymax + APM4hLGUI.menu[mainMenu].spacing)
	end

	function APM4hLGUI.setActiveMenu(menuName)
		APM4hLGUI.activeButton = 1
		APM4hLGUI.activeMenu = menuName
	end

	function APM4hLGUI.drawIndex()
		if(nextDrawTime == nil or prevText ~= APM4hLGUI.IndexStatus.text) then
			nextDrawTime = GAMEPLAY.GET_GAME_TIMER() + APM4hLGUI.IndexStatus.time
		end

		if(nextDrawTime > GAMEPLAY.GET_GAME_TIMER()) then
			prevText = APM4hLGUI.IndexStatus.text
			UI.SET_TEXT_FONT(0)
			UI.SET_TEXT_SCALE(0.0, 0.35)
			UI.SET_TEXT_COLOUR(255, 255, 255, 255)
			UI.SET_TEXT_WRAP(0, 1)
			UI.SET_TEXT_CENTRE(true)
			UI.SET_TEXT_DROPSHADOW(10, 10, 0, 0, 0)
			UI.SET_TEXT_EDGE(1, 0, 0, 0, 205)
			UI._SET_TEXT_ENTRY("STRING")
			UI._ADD_TEXT_COMPONENT_STRING(APM4hLGUI.IndexStatus.text)
			UI._DRAW_TEXT(xmin , 0.5)
		else
			APM4hLGUI.IndexStatus = {["text"] = "", ["time"] = 0}
			nextDrawTime = nil
		end
	end

	function addButtonText(text, xmin, ymin, textScale, font)
		if(text ~= nil and xmin ~= nil and ymin ~= nil and textScale ~= nil and font ~= nil) then
			UI.SET_TEXT_FONT(font)
			UI.SET_TEXT_SCALE(0.0, 0.30)
			UI.SET_TEXT_COLOUR(255, 255, 255, 255)
			UI.SET_TEXT_CENTRE(true)
			UI.SET_TEXT_DROPSHADOW(0, 0, 0, 0, 0)
			UI.SET_TEXT_EDGE(1, 0, 0, 0, 205)
			UI._SET_TEXT_ENTRY("STRING")
			UI._ADD_TEXT_COMPONENT_STRING(text)
			UI._DRAW_TEXT(xmin , ymin - 0.0125)
		else
			print("text: ", text, ", xmin: ", xmin, ", ymin: ", ymin, ", textScale: ", textScale, ", font: ", font)
		    APM4hLGUI.setActiveMenu("")
		end
	end

	function APM4hLGUI.drawAPM4hLGUI()
		if(APM4hLGUI.activeMenu ~= "" and APM4hLGUI.activeMenu ~= nil) then
			local name = APM4hLGUI.activeMenu

			if(APM4hLGUI.menu[name] ~= nil) then
				local xmin = APM4hLGUI.menu[name].xmin
				local ymin = APM4hLGUI.menu[name].ymin
				local xmax = APM4hLGUI.menu[name].xmax
				local ymax = APM4hLGUI.menu[name].ymax
				
				local text = APM4hLGUI.menu[name].title
				local textScale = APM4hLGUI.menu[name].textScale
				local font = APM4hLGUI.menu[name].font
				local colors = {["r"] = 0, ["g"] = 0, ["b"] = 0, ["o"] = 255}

				addButtonText(text, xmin, ymin, textScale, font)
				GRAPHICS.DRAW_RECT(xmin, ymin, xmax, ymax, colors.r, colors.g, colors.b, colors.o);

				if(APM4hLGUI.buttons[name] ~= nil) then
					for i, v in pairs(APM4hLGUI.buttons[name].settings) do
						local buttonSettings = APM4hLGUI.buttons[name].settings[i]
						local xmin = buttonSettings.xmin
						local ymin = buttonSettings.ymin
						local xmax = buttonSettings.xmax
						local ymax = buttonSettings.ymax
						
						local font = buttonSettings.font
						local text = buttonSettings.text
						local textScale = buttonSettings.textScale
						local colors = {["r"] = 100, ["g"] = 50, ["b"] = 50, ["o"] = 180}

						if(buttonSettings.selectableOn == true) then
							text = buttonSettings.text .. " [ON]"
						elseif(buttonSettings.selectableOn == false) then
							text = buttonSettings.text .. " [OFF]"
						end
						
						if(i == APM4hLGUI.activeButton) then
							colors = {["r"] = 150, ["g"] = 100, ["b"] = 100, ["o"] = 200}
						end

						addButtonText(text, xmin, ymin, textScale, font)
						GRAPHICS.DRAW_RECT(xmin, ymin, xmax, ymax, colors.r, colors.g, colors.b, colors.o);
					end
				else
					APM4hLGUI.setActiveMenu("")
				end
			else
				APM4hLGUI.setActiveMenu("")
			end
		end
	end

	function APM4hLGUI.updateIndexStatus(text, time)
		APM4hLGUI.IndexStatus = {["text"] = text, ["time"] = time}
	end

	function APM4hLGUI.isButtonselectableOn()
		local activeButton = APM4hLGUI.buttons[APM4hLGUI.activeMenu].settings[APM4hLGUI.activeButton]
		if(activeButton.selectable) then
			return activeButton.selectableOn
		elseif(activeButton.selectable == flse) then
			print("Button \"", APM4hLGUI.buttons[APM4hLGUI.activeMenu].settings[APM4hLGUI.activeButton].text, "\"")
		    return nil
		end
	end

	function APM4hLGUI.toggleButtonState()
		local activeButton = APM4hLGUI.buttons[APM4hLGUI.activeMenu].settings[APM4hLGUI.activeButton]
		
		if(APM4hLGUI.isButtonselectableOn() and activeButton.selectable) then
			activeButton.selectableOn = false
		elseif(APM4hLGUI.isButtonselectableOn() == false and activeButton.selectable) then
			activeButton.selectableOn = true
			print("toggled on")
		end
	end

	function APM4hLGUI.tick()
		APM4hLGUI.drawIndex()
		APM4hLGUI.drawAPM4hLGUI()

		for i, key in pairs(APM4hLGUI.updateSelection.menuOpen.keys) do
			if(get_key_pressed(key)) then
				if lastTime==nil or GAMEPLAY.GET_GAME_TIMER()-lastTime>120 then lastTime=GAMEPLAY.GET_GAME_TIMER()
					if(APM4hLGUI.activeMenu == APM4hLGUI.updateSelection.menuOpen.menu) then
						APM4hLGUI.setActiveMenu(APM4hLGUI.menu[APM4hLGUI.activeMenu].prevMenu)
					else
					    APM4hLGUI.setActiveMenu(APM4hLGUI.updateSelection.menuOpen.menu)
					    APM4hLGUI.currentSelection = 1
					end
				end
			
			end
		end

		for k, key in pairs(APM4hLGUI.updateSelection.menuUp.keys) do
			if(get_key_pressed(key) and APM4hLGUI.activeMenu ~= "") then
				if lastTime==nil or GAMEPLAY.GET_GAME_TIMER()-lastTime>120 then lastTime=GAMEPLAY.GET_GAME_TIMER()
					APM4hLGUI.activeButton = APM4hLGUI.activeButton - 1

					if(APM4hLGUI.activeButton <= 0) then
						APM4hLGUI.activeButton = APM4hLGUI.menu[APM4hLGUI.activeMenu].numButtons
					end
				end
			
			end
		end

		for k, key in pairs(APM4hLGUI.updateSelection.menuDown.keys) do
			if(get_key_pressed(key) and APM4hLGUI.activeMenu ~= "") then
				if lastTime==nil or GAMEPLAY.GET_GAME_TIMER()-lastTime>120 then lastTime=GAMEPLAY.GET_GAME_TIMER()
					APM4hLGUI.activeButton = APM4hLGUI.activeButton + 1
					if(APM4hLGUI.activeButton > APM4hLGUI.menu[APM4hLGUI.activeMenu].numButtons) then
						APM4hLGUI.activeButton = 1
					end
				end
			
			end
		end

		for k, key in pairs(APM4hLGUI.updateSelection.select.keys) do
			if(get_key_pressed(key) and APM4hLGUI.activeMenu ~= "") then
			   if lastTime==nil or GAMEPLAY.GET_GAME_TIMER()-lastTime>120 then lastTime=GAMEPLAY.GET_GAME_TIMER()
				local currButton = APM4hLGUI.buttons[APM4hLGUI.activeMenu].settings[APM4hLGUI.activeButton]
				
				if(type(currButton.funct) == "function" and type(currButton.args) == "table") then
					APM4hLGUI.toggleButtonState()
					currButton.funct(currButton.args[1], currButton.args[2], currButton.args[3], currButton.args[4], currButton.args[5], currButton.args[6], currButton.args[7], currButton.args[8], currButton.args[9], currButton.args[10], currButton.args[11], currButton.args[12], currButton.args[13], currButton.args[14], currButton.args[15], currButton.args[16], currButton.args[17], currButton.args[18], currButton.args[19], currButton.args[20])

				elseif(type(currButton.funct) == "function" and type(currButton.args) == "string" or type(currButton.args) == "number"or type(currButton.args) == "boolean") then
					APM4hLGUI.toggleButtonState()
					currButton.funct(currButton.args)
					
				elseif(currButton.args == nil) then
					APM4hLGUI.toggleButtonState()
					currButton.funct()

				else
					print("function: ", currButton.funct, ", args: ", currButton.args)
				end
			   end
			
			end
		end

		for k, key in pairs(APM4hLGUI.updateSelection.menuCancel.keys) do
			if(get_key_pressed(key) and APM4hLGUI.activeMenu ~= "") then
				if lastTime==nil or GAMEPLAY.GET_GAME_TIMER()-lastTime>120 then lastTime=GAMEPLAY.GET_GAME_TIMER()
					APM4hLGUI.setActiveMenu(APM4hLGUI.menu[APM4hLGUI.activeMenu].prevMenu)
				end
			
			end
		end
	end

	return APM4hLGUI
end

-------------------------------------------------------------------------------------------------------------------

--PRICES
local m4h1Cost = 8000
local m4h2Cost = 15000
local m4h3Cost = 23000

--CARNAME
local m4h1CarName = "Hexer"
local m4h2CarName = "Minivan"
local m4h3CarName = "Burrito"

local showMenu = false

local buy = false
local timebuy = 99999
local CarName = "not set"

local spawned = false
local LL5Ped

local guards = {}
local bodyguardsInvincibleEnabled = false
local bodyguardVehicle = "gburrito"

local RndModels = true -- true for different set of models.
local RndWeapons = true -- true for different set of weapons.

--M4H NUMBERS
local bodyguardCount = 0
local amountAllowed = 3 -- MAX 7 Guards.
local amountAllowed2 = 2 -- MAX 7 Guards.
local amountAllowed3 = 1 -- MAX 7 Guards.
local bodyguardInVehicle = 0

--M4H MODELS 1
local pedModel = "G_M_Y_Lost_01"

--M4H WEAPONS 1
local meleeWeapon = "WEAPON_KNIFE"
local secondaryWeapon = "WEAPON_COMBATPISTOL"
local primaryWeapon = "WEAPON_PUMPSHOTGUN" 

--M4H MODELS 2
local RndModels_table = {"G_M_Y_Lost_01", "G_M_Y_Lost_02", "G_M_Y_Lost_03"}

--M4H WEAPONS 2
local rndMeleeWeapons = {"WEAPON_KNIFE", "WEAPON_CROWBAR"}
local rndSecondaryWeapons = {"WEAPON_MICROSMG", "WEAPON_HEAVYPISTOL"}
local rndPrimaryWeapons = {"WEAPON_SMG", "WEAPON_PUMPSHOTGUN"}

-------------------------------------------------------------------------------------------------------------------

--Pay System
function Pay(pay)

local playerPed = PLAYER.PLAYER_PED_ID()

		if (PED.IS_PED_MODEL(playerPed, GAMEPLAY.GET_HASH_KEY("player_zero"))) then
			model = 0
		elseif (PED.IS_PED_MODEL(playerPed, GAMEPLAY.GET_HASH_KEY("player_one"))) then                                
			model = 1                                         
		elseif (PED.IS_PED_MODEL(playerPed, GAMEPLAY.GET_HASH_KEY("player_two"))) then
			model = 2
		end
	
		local statname = "SP"..model.."_TOTAL_CASH"
		local hash = GAMEPLAY.GET_HASH_KEY(statname)
		local bool, val = STATS.STAT_GET_INT(hash, 0, -1)

		STATS.STAT_SET_INT(hash, val - pay, true)

end

------------------------------------------------------------------------------
--MODEL 1 LOAD
function ArmoredPacksm4hLMod.loadPedModel()
	model_hash = GAMEPLAY.GET_HASH_KEY(pedModel)

	if (not STREAMING.HAS_MODEL_LOADED(model_hash)) then
		STREAMING.REQUEST_MODEL(model_hash)
		print("[ArmoredPacksm4hLMod.loadPedModel]: Requesting Model: " .. pedModel .. "(" .. model_hash .. ")")

		while (true) do
			if (STREAMING.HAS_MODEL_LOADED(model_hash)) then
				print("[ArmoredPacksm4hLMod.loadPedModel]: Model: " .. pedModel .. "(" .. model_hash .. ")" .. " been loaded.")
				break
			end
			STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(Guard)
			wait(10)
		end
	end
end

--MODEL 1 UNLOAD
function ArmoredPacksm4hLMod.unloadPedModel()
	model_hash = GAMEPLAY.GET_HASH_KEY(pedModel)

	while (true) do
		if (STREAMING.HAS_MODEL_LOADED(model_hash)) then
			STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(model_hash)
			print("[ArmoredPacksm4hLMod.unloadPedModel]: Model: " .. pedModel .. "(" .. model_hash .. ")" .. 
			" has been set to 'NO LONGER NEEDED'." )
			break
		end
		wait(10)
	end
end

--MODELS 2 LOAD
function ArmoredPacksm4hLMod.loadRndPedModels()
	for k, v in pairs(RndModels_table) do
		model_hash = GAMEPLAY.GET_HASH_KEY(v)

		if (not STREAMING.HAS_MODEL_LOADED(model_hash)) then
			STREAMING.REQUEST_MODEL(model_hash)
			print("[ArmoredPacksm4hLMod.loadRndPedModels]: Requesting Model: " .. v .. "(" .. model_hash .. ")")

			while (true) do
				if (STREAMING.HAS_MODEL_LOADED(model_hash)) then
					print("[ArmoredPacksm4hLMod.loadRndPedModels]: Model: " .. v .. "(" .. model_hash .. ")" .. " been loaded.")
					break
				end
				wait(10)
			end
		end
	end
end

--MODELS 2 UNLOAD
function ArmoredPacksm4hLMod.unloadRndPedModels()
	for _, m in pairs(RndModels_table) do
		model_hash = GAMEPLAY.GET_HASH_KEY(m)

		while (true) do
			if (STREAMING.HAS_MODEL_LOADED(model_hash)) then
				STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(model_hash)
				print("[ArmoredPacksm4hLMod.unloadRndPedModels]: Model: " .. m .. "(" .. model_hash .. ")" .. 
				" has been set to 'NO LONGER NEEDED'." )
				break
			end
			wait(10)
		end
	end
end

--UNLOAD GUARD
function ArmoredPacksm4hLMod.unload()
	for k, guard in pairs(guards) do
		if (guard ~= nil) then 
			PED.DELETE_PED(guard)
			guards[k] = nil

			bodyguardCount = 0
		end
	end
end

--DEAD GUARD
function ArmoredPacksm4hLMod.deleteOnDead()
	for k, guard in pairs(guards) do
		if (guard ~= nil) then
			if (ENTITY.IS_ENTITY_DEAD(guard)) then
				PED.DELETE_PED(guard)
				guards[k] = nil

				bodyguardCount = bodyguardCount - 1

				print("[ArmoredPacksm4hLMod.deleteOnDead]: Number of bodyguards: " .. bodyguardCount)
			end
		end
	end
end

function ArmoredPacksm4hLMod.applyRndWeaponsToBodyguards(i)
	local rndMWeapon = math.random(#rndMeleeWeapons)
	local rndSWeapon = math.random(#rndSecondaryWeapons)
	local rndPWeapon = math.random(#rndPrimaryWeapons)

	WEAPON.GIVE_DELAYED_WEAPON_TO_PED(guards[i], GAMEPLAY.GET_HASH_KEY(rndMeleeWeapons[rndMWeapon]), 500, true)
	WEAPON.GIVE_DELAYED_WEAPON_TO_PED(guards[i], GAMEPLAY.GET_HASH_KEY(rndSecondaryWeapons[rndSWeapon]), 500, true)
	WEAPON.GIVE_DELAYED_WEAPON_TO_PED(guards[i], GAMEPLAY.GET_HASH_KEY(rndPrimaryWeapons[rndPWeapon]), 500, true)
end

function ArmoredPacksm4hLMod.applyWeaponsToBodyguards(i)
	WEAPON.GIVE_DELAYED_WEAPON_TO_PED(guards[i], GAMEPLAY.GET_HASH_KEY(meleeWeapon), 500, true)
	WEAPON.GIVE_DELAYED_WEAPON_TO_PED(guards[i], GAMEPLAY.GET_HASH_KEY(secondaryWeapon), 500, true)
	WEAPON.GIVE_DELAYED_WEAPON_TO_PED(guards[i], GAMEPLAY.GET_HASH_KEY(primaryWeapon), 500, true)
end

function ArmoredPacksm4hLMod.applyNativesToBodyguards(i)
	local playerPed = PLAYER.PLAYER_PED_ID()
	local playerGroup = PED.GET_PED_GROUP_INDEX(playerPed)
	
	PED.SET_PED_CAN_SWITCH_WEAPON(guards[i], true)
	PED.SET_PED_AS_GROUP_MEMBER(guards[i], playerGroup)
	PED.SET_PED_ACCURACY(guards[i], 100)

	ENTITY.SET_ENTITY_INVINCIBLE(guards[i], bodyguardsInvincibleEnabled)

	WEAPON.SET_PED_DROPS_WEAPONS_WHEN_DEAD(guards[i], true)

	AI.SET_PED_PATH_CAN_USE_CLIMBOVERS(guards[i], true)
	AI.SET_PED_PATH_CAN_USE_LADDERS(guards[i], true)
	AI.SET_PED_PATH_CAN_DROP_FROM_HEIGHT(guards[i], true)
end

function ArmoredPacksm4hLMod.GetRndModel()
	local rndMod_1 = RndModels_table[math.random(#RndModels_table)]
	local rndMod_2 = RndModels_table[math.random(#RndModels_table)]

	if (RndMod_1 == RndMod_2 and RndMod_2 == RndMod_1) then
		return RndModels_table[math.random(#RndModels_table)]
	elseif (not RndMod_1 == RndMod_2 and not RndMod_2 == RndMod_1) then
		return RndMod_1 or RndMod_2
	end
end

function ArmoredPacksm4hLMod.teleportbodyguardsToPlayer()
	local playerPed = PLAYER.PLAYER_PED_ID()
	local player = PLAYER.GET_PLAYER_PED(playerPed)
	local playerPosition = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(playerPed, 0.0, 2.5, 0.0)

	for k, guard in pairs(guards) do
		if (guard ~= nil) then
			if (not PED.IS_PED_IN_ANY_VEHICLE(playerPed, false) and not PED.IS_PED_IN_ANY_VEHICLE(guard, false)) then
				ENTITY.SET_ENTITY_COORDS(guard, playerPosition.x, playerPosition.y,
				playerPosition.z, false, false, false, false)
			elseif(PED.IS_PED_IN_ANY_VEHICLE(playerPed, false) and not PED.IS_PED_IN_ANY_VEHICLE(guard, false)) then
				ENTITY.SET_ENTITY_COORDS(guard, playerPosition.x, playerPosition.y,
				playerPosition.z, false, false, false, false)
			end
		end
	end
end

--[[function ArmoredPacksm4hLMod.followPlayerInVehicle()
	local playerPed = PLAYER.PLAYER_PED_ID()
	local player = PLAYER.GET_PLAYER_PED(playerPed)
	local playerPosition = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(playerPed, 0.0, 5.0, 0.0)
	STREAMING.REQUEST_MODEL(bodyguardVehicle)

	while (not STREAMING.HAS_MODEL_LOADED(bodyguardVehicle)) do
		wait(10)
	end
	--local veh = VEHICLE.CREATE_VEHICLE(bodyguardVehicle, playerPosition.x, playerPosition.y, playerPosition.z, 0.0, 1, 1)
	if 
	for k, guard in pairs(guards) do
		if (guard ~= nil) then
			
		end
	end
end]]

------------------------------------------------------------------------------

function ArmoredPacksm4hLMod.tick()

	local playerPed = PLAYER.PLAYER_PED_ID()
	local playerPedinV = PED.IS_PED_IN_ANY_VEHICLE(playerPed, false)
	local playerCoords = ENTITY.GET_ENTITY_COORDS(playerPed, true)
	local buyPoint = GAMEPLAY.GET_DISTANCE_BETWEEN_COORDS(playerCoords.x, playerCoords.y, playerCoords.z, 159.324, -254.644, 51.400, true	)
	local player = PLAYER.GET_PLAYER_PED(playerPed)
	local playerExists = ENTITY.DOES_ENTITY_EXIST(playerPed)
	local playerPosition = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(playerPed, 0.0, 5.0, 0.0)
	local playerGroup = PED.GET_PED_GROUP_INDEX(playerPed)
	local location = ENTITY.GET_ENTITY_COORDS( playerPed, nil )
	
	--SHOW MENU FLAG
	if (not playerPedinV and buyPoint < 2 and not showMenu) then
		if PED._IS_PED_DEAD(LL5Ped, true) then
			notify("There is no one for hire, come back later.")
		else
			notify("Hi! PRESS E")
		
			if get_key_pressed(Keys.E) then
				showMenu = true
			end
		end
	end

	--SHOW MENU
	if(showMenu == true) then
		APM4hLGUI.tick()
	end

	--CLOSE MENU
	if(buyPoint > 2 and get_key_pressed(Keys.E)) then
		showMenu = false
	elseif(buyPoint > 10 and showMenu == true) then
		showMenu = false
	end

	--PLAYER & DEALER DISTANCE
	if(buyPoint > 5 and showMenu == true) then
		notify("PRESS E to close the Menu")
	end
	
	--LOST M4H DISMISS/DEAD
	if (get_key_pressed(46) and playerExists) then
		ArmoredPacksm4hLMod.unload()
	end
	
	if (get_key_pressed(45) and playerExists) then
		ArmoredPacksm4hLMod.teleportbodyguardsToPlayer()
	end

	ArmoredPacksm4hLMod.deleteOnDead()

	--SPAWN/DESPAWN DEALER
	if(spawned == false and GAMEPLAY.GET_DISTANCE_BETWEEN_COORDS( location.x, location.y, location.z,159.324, -254.644, 51.400, false ) < 150 ) then
		spawnLostM4H()
		spawned = true
	elseif(spawned == true and GAMEPLAY.GET_DISTANCE_BETWEEN_COORDS( location.x, location.y, location.z,159.324, -254.644, 51.400, false ) > 150 ) then
		PED.DELETE_PED(LL5Ped)
		PED.DELETE_PED(LG5Ped[0])
		PED.DELETE_PED(LG5Ped[1])
		PED.DELETE_PED(LG5Ped[2])
		LostLeaderMoney = 0
		spawned = false
	end
		
	
end

------------------------------------------------------------------------------

--LOST SPAWN
function spawnLostM4H()

	local weapon = GAMEPLAY.GET_HASH_KEY("WEAPON_SAWNOFFSHOTGUN")
	local weapon2 = GAMEPLAY.GET_HASH_KEY("WEAPON_MICROSMG")
	local weapon3 = GAMEPLAY.GET_HASH_KEY("WEAPON_SNIPERRIFLE")

	---LOSTLEADER---
	local LostLeader = GAMEPLAY.GET_HASH_KEY("G_M_Y_Lost_03")
	STREAMING.REQUEST_MODEL(LostLeader)
	while(not STREAMING.HAS_MODEL_LOADED(LostLeader)) do
		wait(50)
	end
	LL5Ped = PED.CREATE_PED(1, LostLeader, 159.324, -254.644, 51.400, 160, true, true)
	PED.SET_PED_CAN_SWITCH_WEAPON(LL5Ped, true)
	WEAPON.GIVE_DELAYED_WEAPON_TO_PED(LL5Ped, weapon, 1000, false)
	PED.SET_PED_RELATIONSHIP_GROUP_HASH(LL5Ped, GAMEPLAY.GET_HASH_KEY("PRIVATE_SECURITY"))
	ENTITY.SET_ENTITY_INVINCIBLE(LL5Ped, false)
	STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(LostLeader)
	LostLeaderMoney = (GAMEPLAY.GET_RANDOM_INT_IN_RANGE(1000,1000))
	PED.SET_PED_MONEY(LL5Ped, LostLeaderMoney)

	---LOSTGOON---
	LostGoon = GAMEPLAY.GET_HASH_KEY("G_M_Y_Lost_01")

	STREAMING.REQUEST_MODEL(LostGoon)
	
	while(not STREAMING.HAS_MODEL_LOADED(LostGoon)) do
		wait(50)
	end
	
	LG5Ped = {}
	
	LG5Ped[0] = PED.CREATE_PED(1, LostGoon, 154.722, -254.829, 51.400, 247, true,true)
	PED.SET_PED_CAN_SWITCH_WEAPON(LG5Ped[0], true)
	WEAPON.GIVE_DELAYED_WEAPON_TO_PED(LG5Ped[0], weapon2, 1000, false)
	PED.SET_PED_RELATIONSHIP_GROUP_HASH(LG5Ped[0], GAMEPLAY.GET_HASH_KEY("PRIVATE_SECURITY"))
	ENTITY.SET_ENTITY_INVINCIBLE(LG5Ped[0], false)
	
	LG5Ped[1] = PED.CREATE_PED(1, LostGoon, 143.709, -271.598, 50.450, 330, true,true)
	PED.SET_PED_CAN_SWITCH_WEAPON(LG5Ped[1], true)
	WEAPON.GIVE_DELAYED_WEAPON_TO_PED(LG5Ped[1], weapon3, 1000, false)
	PED.SET_PED_RELATIONSHIP_GROUP_HASH(LG5Ped[1], GAMEPLAY.GET_HASH_KEY("PRIVATE_SECURITY"))
	ENTITY.SET_ENTITY_INVINCIBLE(LG5Ped[1], false)
	
	LG5Ped[2] = PED.CREATE_PED(1, LostGoon, 170.538, -278.626, 50.276, 28, true,true)
	PED.SET_PED_CAN_SWITCH_WEAPON(LG5Ped[2], true)
	WEAPON.GIVE_DELAYED_WEAPON_TO_PED(LG5Ped[2], weapon3, 1000, false)
	PED.SET_PED_RELATIONSHIP_GROUP_HASH(LG5Ped[2], GAMEPLAY.GET_HASH_KEY("PRIVATE_SECURITY"))
	ENTITY.SET_ENTITY_INVINCIBLE(LG5Ped[2], false)
	
	STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(LostGoon)
	
	
end

------------------------------------------------------------------------------

--LOST M4H SPAWN
function spawnM4HLMC(modelString) 

	local playerPed = PLAYER.PLAYER_PED_ID()
	local player = PLAYER.GET_PLAYER_PED(playerPed)
	local playerExists = ENTITY.DOES_ENTITY_EXIST(playerPed)
	local playerPosition = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(playerPed, 0.0, 5.0, 0.0)
	local playerGroup = PED.GET_PED_GROUP_INDEX(playerPed)
	
	print (timebuy)
	print(TIME.GET_CLOCK_DAY_OF_MONTH())
	print(TIME.GET_CLOCK_DAY_OF_MONTH() ~= timebuy)

if(TIME.GET_CLOCK_DAY_OF_MONTH() ~= timebuy) then
	
	local cost
		

	timebuy = TIME.GET_CLOCK_DAY_OF_MONTH()
		
	STREAMING.REQUEST_MODEL(GAMEPLAY.GET_HASH_KEY(modelString))	
	
	while(not STREAMING.HAS_MODEL_LOADED(GAMEPLAY.GET_HASH_KEY(modelString))) do
		wait(50)
	end
		
	local vehLost = VEHICLE.CREATE_VEHICLE(GAMEPLAY.GET_HASH_KEY(modelString),  137.125, -242.648, 51.536, 161, true, true)
	VEHICLE.SET_VEHICLE_ON_GROUND_PROPERLY(vehLost)
		
			VEHICLE.SET_VEHICLE_NEEDS_TO_BE_HOTWIRED(vehLost, false) 		
			VEHICLE.SET_VEHICLE_HAS_BEEN_OWNED_BY_PLAYER(vehLost, true)
			VEHICLE.SET_VEHICLE_IS_STOLEN(vehLost, false)
			VEHICLE.SET_VEHICLE_IS_WANTED(vehLost, false)
			VEHICLE.SET_VEHICLE_MOD_KIT(vehLost, 0)
			VEHICLE.SET_VEHICLE_MOD(vehLost, 16, 3, true)
			VEHICLE.SET_VEHICLE_WHEEL_TYPE(vehLost, 120)
			VEHICLE.SET_VEHICLE_WINDOW_TINT(vehLost, 3) 
			VEHICLE.SET_CAN_RESPRAY_VEHICLE(vehLost, true)
			VEHICLE.SET_VEHICLE_CUSTOM_PRIMARY_COLOUR(vehLost, 0, 0, 0) 
			VEHICLE.SET_VEHICLE_CUSTOM_SECONDARY_COLOUR(vehLost, 0, 0, 0)
			VEHICLE.SET_VEHICLE_TYRES_CAN_BURST(vehLost, false) 
			VEHICLE.SET_VEHICLE_WHEELS_CAN_BREAK(vehLost, false) 			
			VEHICLE.SET_VEHICLE_NUMBER_PLATE_TEXT(vehLost, "LMC")
		
	STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(GAMEPLAY.GET_HASH_KEY(modelString))

	if(GAMEPLAY.GET_HASH_KEY(modelString) == GAMEPLAY.GET_HASH_KEY("hexer")) then cost = m4h1Cost
	elseif(GAMEPLAY.GET_HASH_KEY(modelString) == GAMEPLAY.GET_HASH_KEY("minivan")) then cost = m4h2Cost
	elseif(GAMEPLAY.GET_HASH_KEY(modelString) == GAMEPLAY.GET_HASH_KEY("gburrito")) then cost = m4h3Cost
	end
	
	if(GAMEPLAY.GET_HASH_KEY(modelString) == GAMEPLAY.GET_HASH_KEY("hexer")) then CarName = m4h1CarName
	elseif(GAMEPLAY.GET_HASH_KEY(modelString) == GAMEPLAY.GET_HASH_KEY("minivan")) then CarName = m4h2CarName
	elseif(GAMEPLAY.GET_HASH_KEY(modelString) == GAMEPLAY.GET_HASH_KEY("gburrito")) then CarName = m4h3CarName
	end

	--LOST M4H SPAWN
	if(GAMEPLAY.GET_HASH_KEY(modelString) == GAMEPLAY.GET_HASH_KEY("hexer")) then
		for i = 0, amountAllowed, 1 do
			if (bodyguardCount < amountAllowed3) then
				ArmoredPacksm4hLMod.loadRndPedModels()
				ArmoredPacksm4hLMod.loadPedModel()

				if (RndModels == true) then
					guards[i] = PED.CREATE_PED(26, GAMEPLAY.GET_HASH_KEY(ArmoredPacksm4hLMod.GetRndModel()),148.812, -253.582, 51.406, 176, false, true)

					if (RndWeapons == true) then
						ArmoredPacksm4hLMod.applyRndWeaponsToBodyguards(i)
					elseif (RndWeapons == false) then
						ArmoredPacksm4hLMod.applyWeaponsToBodyguards(i)
					end

					ArmoredPacksm4hLMod.applyNativesToBodyguards(i)
					ArmoredPacksm4hLMod.unloadRndPedModels()
				elseif (RndModels == false) then
					guards[i] = PED.CREATE_PED(26, GAMEPLAY.GET_HASH_KEY(pedModel),148.812, -253.582, 51.406, 176, false, true)

					if (RndWeapons == true) then
						ArmoredPacksm4hLMod.applyRndWeaponsToBodyguards(i)
					elseif (RndWeapons == false) then
						ArmoredPacksm4hLMod.applyWeaponsToBodyguards(i)
					end

					ArmoredPacksm4hLMod.applyNativesToBodyguards(i)
					ArmoredPacksm4hLMod.unloadPedModel()
				end

				bodyguardCount = bodyguardCount + 1
				print("[ArmoredPacksm4hLMod.tick]: Number of bodyguards: " .. bodyguardCount)
			end
		end
	end
		
	if(GAMEPLAY.GET_HASH_KEY(modelString) == GAMEPLAY.GET_HASH_KEY("minivan")) then
		for i = 0, amountAllowed, 1 do
			if (bodyguardCount < amountAllowed2) then
				ArmoredPacksm4hLMod.loadRndPedModels()
				ArmoredPacksm4hLMod.loadPedModel()

				if (RndModels == true) then
					guards[i] = PED.CREATE_PED(26, GAMEPLAY.GET_HASH_KEY(ArmoredPacksm4hLMod.GetRndModel()),148.812, -253.582, 51.406, 176, false, true)

					if (RndWeapons == true) then
						ArmoredPacksm4hLMod.applyRndWeaponsToBodyguards(i)
					elseif (RndWeapons == false) then
						ArmoredPacksm4hLMod.applyWeaponsToBodyguards(i)
					end

					ArmoredPacksm4hLMod.applyNativesToBodyguards(i)
					ArmoredPacksm4hLMod.unloadRndPedModels()
				elseif (RndModels == false) then
					guards[i] = PED.CREATE_PED(26, GAMEPLAY.GET_HASH_KEY(pedModel),148.812, -253.582, 51.406, 176, false, true)

					if (RndWeapons == true) then
						ArmoredPacksm4hLMod.applyRndWeaponsToBodyguards(i)
					elseif (RndWeapons == false) then
						ArmoredPacksm4hLMod.applyWeaponsToBodyguards(i)
					end

					ArmoredPacksm4hLMod.applyNativesToBodyguards(i)
					ArmoredPacksm4hLMod.unloadPedModel()
				end

				bodyguardCount = bodyguardCount + 1
				print("[ArmoredPacksm4hLMod.tick]: Number of bodyguards: " .. bodyguardCount)
			end
		end
	end
	
	if(GAMEPLAY.GET_HASH_KEY(modelString) == GAMEPLAY.GET_HASH_KEY("gburrito")) then
		for i = 0, amountAllowed, 1 do
			if (bodyguardCount < amountAllowed) then
				ArmoredPacksm4hLMod.loadRndPedModels()
				ArmoredPacksm4hLMod.loadPedModel()

				if (RndModels == true) then
					guards[i] = PED.CREATE_PED(26, GAMEPLAY.GET_HASH_KEY(ArmoredPacksm4hLMod.GetRndModel()),148.812, -253.582, 51.406, 176, false, true)

					if (RndWeapons == true) then
						ArmoredPacksm4hLMod.applyRndWeaponsToBodyguards(i)
					elseif (RndWeapons == false) then
						ArmoredPacksm4hLMod.applyWeaponsToBodyguards(i)
					end

					ArmoredPacksm4hLMod.applyNativesToBodyguards(i)
					ArmoredPacksm4hLMod.unloadRndPedModels()
				elseif (RndModels == false) then
					guards[i] = PED.CREATE_PED(26, GAMEPLAY.GET_HASH_KEY(pedModel),148.812, -253.582, 51.406, 176, false, true)

					if (RndWeapons == true) then
						ArmoredPacksm4hLMod.applyRndWeaponsToBodyguards(i)
					elseif (RndWeapons == false) then
						ArmoredPacksm4hLMod.applyWeaponsToBodyguards(i)
					end

					ArmoredPacksm4hLMod.applyNativesToBodyguards(i)
					ArmoredPacksm4hLMod.unloadPedModel()
				end

				bodyguardCount = bodyguardCount + 1
				print("[ArmoredPacksm4hLMod.tick]: Number of bodyguards: " .. bodyguardCount)
			end
		end
	end	

	ENTITY.SET_VEHICLE_AS_NO_LONGER_NEEDED(vehLost)

	Pay(cost)
	buy = true
	notify("Your " .. CarName .. " and your men is ready. Brother for live, Lost forever!")
		
elseif(TIME.GET_CLOCK_DAY_OF_MONTH() == timebuy) then
	notify("There is no one for hire today, come back tomorrow.")
end
		wait(5)
		
		
end

------------------------------------------------------------------------------
return ArmoredPacksm4hLMod

-- Credits to: Alexander Blade & headscript--