--*****************************************--
--  		Rob Mod V1.0 : Made By Slade   --
--*****************************************--

-- Copyright (c) 2016, Sladernimo, Member Of Vega Game Projects @ www.vega-gp.de


--***************************************************************************************************************
--=============
-- INFORMATION:
--=============
--
--
--
-----------
-- VERSION:
-----------
-- This is version 1.0 initial release.
--
--
-----------------
-- COMPATIBILITY:
-----------------
-- All of my mods are 100% compatible to each other.
-- In general the mods should be compatible to almost every other mod.
-- Obviously, if you are using a mod that injects or alters the game in the same way like one of my mods does, weird stuff can happen.
--
--
----------------------
-- SETUP/REQUIREMENTS:
----------------------
-- 1. Install ScriptHookV if not already done.
-- 2. Install LUA Plugin for ScriptHookV if not already done.
-- 3. Copy mbsRobMod.lua into your scripts/addins folder inside of your Grand Theft Auto V folder.
-- 4. Play the game in singleplayer.
-- 5. Enjoy the mod & have fun!
--
--
---------------
-- DESCRIPTION:
---------------
-- This mod adds the ability to rob random people on the street as well as
-- robbing all the shops the player wasn't able to rob in the original game.
-- Including clothing shops, hair salons, and tattooists.
-- Gameplay for the shops you was able to rob in the vanilla game has not been changed.
-- With the Rob Mod installed you will be able to rob 24/7 stores and gas stations in the same way like before.
--
--
------------
-- GAMEPLAY:
------------
-- HOW TO ROB:
-- If you aim at a person with a weapon in your hands
-- while being within a distance of 5 meters
-- that person will stop walking and lift its hands up while facing the player.
-- You can also stop fleeing people that way.
-- It will take between 10 and 25 seconds for the person to drop the money.
-- When the money drops you can hear a signal sound which indicates that the rob is finished.
-- You can find the money on the ground next to the person you have robbed.
-- After you robbed a specific person, you can't rob that person again for a appropriate amount of time.
-- If you aim at a person you have already robbed
-- that person will stand still while taking hands up (like while in the robbing progress)
-- but that person won't drop any money anymore.
-- The player will get wanted stars depending on which person or shop has been robbed.
-- The amount of money you can get by robbing people is relative random. (Take a look at the "List Of Rob Mod Shops" and the "List Of Rob Mod Special People".)
-- Besides the shops you can rob almost every person you can find on the map.
-- People that you want to rob need to be on foot.
-- If you want to rob a person sitting inside of a vehicle,
-- you will first need to get that person out of that vehicle
-- before you can start robbing.
--
--
-------------------------
-- LIST OF ROB MOD SHOPS:
-------------------------
-- Tattooists				: 0 wanted stars, 250$  - 350$.
-- Normal Hair Salons		: 1 wanted stars, 300$  - 500$.
-- Bincos & Discount Stores	: 2 wanted stars, 450$  - 900$.
-- Suburban	s				: 3 wanted stars, 800$  - 2000$.
-- Ponsonbys				: 4 wanted stars, 1800$ - 5000$.
-- Bob Mulét Hair Salons	: 5 wanted stars, 4500$ - 10000$.
--
--
---------------
-- AUTHOR INFO:
---------------
-- MBS stands for "Made By Slade". I am Slade, the author of this mod.
-- My primary alias is "Sladernimo". I'm a part of the Vega Game Projects - community.
-- Vega-GP is a gaming and modding community. You can visit us at "www.vega-gp.de".
-- I have made my GTA5 mods while playing the game.
-- They are all made to improve the gameplay expirience by
-- changing, removing and adding game features.
--
--
-----------------------------
-- VEGA-GAME PROJECTS INFO:
-----------------------------
--Vega Game Projects is a community for individual gamers and modders.
--We customize our games and servers to refine the overall gaming expirience and play togehter.
--We usually hang out on our Teamspeak Server.
--
--If you are intreseted visit our website at www.vega-gp.de
--
--or contact us:
--- TS³: vega-gp.de
--- Mail: contact@vega-gp.de
-- 
--
-------------------------------
-- LICENSE INFO (BSD LICENSE) :
-------------------------------
-- Copyright (c) 2016, Sladernimo, Member Of Vega Game Projects @ www.vega-gp.de
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are met:
-- 1. Redistributions of source code must retain the above copyright
--    notice, this list of conditions and the following disclaimer.
-- 2. Redistributions in binary form must reproduce the above copyright
--    notice, this list of conditions and the following disclaimer in the
--    documentation and/or other materials provided with the distribution.
-- 3. All advertising materials mentioning features or use of this software
--    must display the following acknowledgement:
--    This product includes software developed by the Vega-GP Community.
-- 4. Neither the name of the Vega-GP Community nor the
--    names of its contributors may be used to endorse or promote products
--    derived from this software without specific prior written permission.
-- 
-- THIS SOFTWARE IS PROVIDED BY Sladernimo ''AS IS'' AND ANY
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-- DISCLAIMED. IN NO EVENT SHALL Sladernimo BE LIABLE FOR ANY
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-- 
--
-----------------------
-- LICENSE EXPLANATION:
-----------------------
-- The license means especially the following:
-- You are free to use, distribute, copy, modify or use the code or parts of the code for your own work under the following permissions:
-- 1. You will always have to link the copyright (Copyright (c) 2016, Sladernimo, Member Of Vega Game Projects @ www.vega-gp.de) in a release of your own work or if you share it over a website for example.
-- 2. You will always have to keep this license (BSD License).
--
--
----------------
-- LEGAL NOTICE:
----------------
-- Grand Theft Auto / GTA 5 are registered trademarks of Rockstar Games. This modification is not affiliated with or endorsed by Rockstar Games.
-- © 2016. Rockstar Games and its subsidiaries. All other marks and trademarks are the property of their respective owners. All rights reserved.
--
--
--***************************************************************************************************************


-------------------------
-- MAIN DECLARATION    --
-------------------------
local mbsRobMod = {}


-------------------------
-- CUSTOM DECLARATIONS --
-------------------------
local isTriggered = false
local robbedPeds = {}
local robbedPedsTimers = {}
local robbedPedsTimerDelays = {}
local forbiddenTargetModelEntities = {}


--------------------
-- MAIN FUNCTIONS --
--------------------

-- INIT (RUNS ON INITIALISATION)
function mbsRobMod.init()
	-- set forbidden target model entities
	mbsRobMod.setForbiddenTargetModelEntities()
	robbedPeds = {}
	robbedPedsTimers = {}
	robbedPedsTimerDelays = {}
	isTriggered = false
end


-- TICK (RUNS ON EVERY GAME TICK)
function mbsRobMod.tick()
	if not isTriggered then
		isTriggered = true
		local playerPedID = PLAYER.PLAYER_PED_ID()
		if ENTITY.DOES_ENTITY_EXIST(playerPedID) then
			local player = PLAYER.PLAYER_ID()
			if PLAYER.IS_PLAYER_FREE_AIMING(player) then
				local isEntityTrue
				local targetEntity
				isEntityTrue, targetEntity = PLAYER._GET_AIMED_ENTITY(PLAYER.PLAYER_ID(), -1)
				if isEntityTrue then
					local targetModelEntity = ENTITY.GET_ENTITY_MODEL(targetEntity)
					local isMissionActive = GAMEPLAY.GET_MISSION_FLAG()
					-- check if it is a ped that should be robable
					if (ENTITY.IS_ENTITY_A_PED(targetEntity) and PED.IS_PED_ON_FOOT(targetEntity) and not WEAPON.IS_PED_ARMED(targetEntity, 4) and not PED.IS_PED_IN_MELEE_COMBAT(targetEntity) and not PED.IS_PED_SWIMMING(targetEntity) and (forbiddenTargetModelEntities[targetModelEntity] == nil) and not isMissionActive) then
						--print(targetModelEntity)
						-- distanceToTarget in ca meters
						local distanceToTarget = mbsRobMod.getDistanceBetween2Peds(playerPedID, targetEntity)
						if (distanceToTarget < 5.0) then
							local targetEntitiyCoords = ENTITY.GET_ENTITY_COORDS(targetEntity, true)
							AI.TASK_HANDS_UP(targetEntity, 10000, playerPedID, -1, -1)
							-- lets player shout to target
							--AUDIO._PLAY_AMBIENT_SPEECH1(playerPedID, "Stay_Down", "Speech_Params_Force")
							AUDIO._PLAY_AMBIENT_SPEECH1(playerPedID, "Shout_Threaten_Ped", "Speech_Params_Force_Shouted_Critical")
							
							if (robbedPeds[targetEntity] == nil) then
								robbedPeds[targetEntity] = targetEntity
								robbedPedsTimers[targetEntity] = GAMEPLAY.GET_GAME_TIMER()
								--print("test 0")
							end
							if not (robbedPedsTimers[targetEntity] == nil) then
								if (robbedPedsTimerDelays[targetEntity] == nil) then
									robbedPedsTimerDelays[targetEntity] = ((math.random() + math.random(10, 26)) * 1000)
									--print(robbedPedsTimerDelays[targetEntity])
									--print("test 1")
								end
								if ((robbedPedsTimers[targetEntity] + robbedPedsTimerDelays[targetEntity]) < GAMEPLAY.GET_GAME_TIMER() and not (robbedPeds[targetEntity] == -1)) then
									-- mark target entity with -1 to see if entity was already robbed
									robbedPeds[targetEntity] = -1
									-- calculate & set money amount
									local moneyAmount = mbsRobMod.getMoneyAmountForModelEntity(targetModelEntity)
									-- calculate & set wanted star level
									local wantedStarLevel = mbsRobMod.getWantedStarLevelForModelEntity(targetModelEntity)
									-- lets target scream
									--AUDIO._PLAY_AMBIENT_SPEECH1(playerPedID, "Generic_Shocked_High", "Speech_Params_Force")
									
									--print(moneyAmount)
									--print(wantedStarLevel)
									
									-- drop money at ped position
									OBJECT.CREATE_MONEY_PICKUPS(targetEntitiyCoords.x, targetEntitiyCoords.y, targetEntitiyCoords.z, moneyAmount, 1, 0)
									-- play audio sound to send a signal that the money has been dropped
									AUDIO.PLAY_SOUND_FRONTEND(-1, "CHALLENGE_UNLOCKED", "HUD_AWARDS", false)
									-- higher wanted level
									local actWantedLevel = PLAYER.GET_PLAYER_WANTED_LEVEL(player)
									if actWantedLevel < wantedStarLevel then
										PLAYER.SET_PLAYER_WANTED_LEVEL(player, wantedStarLevel, false)
									end
									-- set timer for when ped is dropping the money
									robbedPedsTimers[targetEntity] = GAMEPLAY.GET_GAME_TIMER()
								end
							end
						end
					end
				end
			end
		end
		isTriggered = false
	else
		return
	end
end


-- UNLOAD (RUNS ON UNLOADING)
function mbsRobMod.unload()
	robbedPeds = {}
	robbedPedsTimers = {}
	robbedPedsTimerDelays = {}
	isTriggered = false
end


----------------------
-- CUSTOM FUNCTIONS --
----------------------

-- returns distance between 2 entities in ca meters
function mbsRobMod.getDistanceBetween2Peds(entityPed1, entityPed2)
	local result
	local entityCoordsPed1 = ENTITY.GET_ENTITY_COORDS(entityPed1, true)
	local entityCoordsPed2 = ENTITY.GET_ENTITY_COORDS(entityPed2, true)
	result = GAMEPLAY.GET_DISTANCE_BETWEEN_COORDS(entityCoordsPed1.x, entityCoordsPed1.y, entityCoordsPed1.z, entityCoordsPed2.x, entityCoordsPed2.y, entityCoordsPed2.z, true)
	return result
end


-- draws a text on screen
function mbsRobMod.drawText()
    
end


-- returns appropriate money amount for given model entity
function mbsRobMod.getMoneyAmountForModelEntity(modelEntity)
    local moneyAmount = ((math.random(20, 90)) * 1)
	-- if nil return normal money amount
	if (modelEntity == nil) then
		return moneyAmount
	-- Tattooists
	elseif (modelEntity == 2494442380) then
		moneyAmount = ((math.random(250, 350)) / 2)
		return moneyAmount
	-- Normal Hair Salons
	elseif (modelEntity == 373000027) then
		moneyAmount = ((math.random(300, 500)) / 2)
		return moneyAmount
	-- Binco & Discount Store
	elseif (modelEntity == 2842568196) then
		moneyAmount = ((math.random(450, 900)) / 2)
		return moneyAmount
	-- Suburban
	elseif (modelEntity == 1055701597) then
		moneyAmount = ((math.random(800, 2000)) / 2)
		return moneyAmount
	-- Ponsonby's
	elseif (modelEntity == 2923947184) then
		moneyAmount = ((math.random(1800, 5000)) / 2)
		return moneyAmount
	-- Bob Mulét Hair Salon
	elseif (modelEntity == 1099825042) then
		moneyAmount = ((math.random(4500, 10000)) / 2)
		return moneyAmount
	-- else return normal money amount
	else
		return moneyAmount
	end
end


-- returns appropriate wanted star level for given model entity
function mbsRobMod.getWantedStarLevelForModelEntity(modelEntity)
    local wantedStarLevel = 0
	-- if nil return normal money amount
	if (modelEntity == nil) then
		return wantedStarLevel
	-- Tattooists
	elseif (modelEntity == 2494442380) then
		wantedStarLevel = 1
		return wantedStarLevel
	-- Normal Hair Salons
	elseif (modelEntity == 373000027) then
		wantedStarLevel = 1
		return wantedStarLevel
	-- Binco & Discount Store
	elseif (modelEntity == 2842568196) then
		wantedStarLevel = 2
		return wantedStarLevel
	-- Suburban
	elseif (modelEntity == 1055701597) then
		wantedStarLevel = 3
		return wantedStarLevel
	-- Ponsonby's
	elseif (modelEntity == 2923947184) then
		wantedStarLevel = 4
		return wantedStarLevel
	-- Bob Mulét Hair Salon
	elseif (modelEntity == 1099825042) then
		wantedStarLevel = 5
		return wantedStarLevel
	-- else return normal money amount
	else
		return wantedStarLevel
	end
end


-- sets all disallowed model entity ids in an array
function mbsRobMod.setForbiddenTargetModelEntities(modelEntity)
    -- normal shop owners (24/7 and gas stations)
	forbiddenTargetModelEntities[416176080] = 416176080
	-- animals
	-- wild boars
	forbiddenTargetModelEntities[3462393972] = 3462393972
	-- chickens
	forbiddenTargetModelEntities[1794449327] = 1794449327
	-- cormorants
	forbiddenTargetModelEntities[1457690978] = 1457690978
	-- crows
	forbiddenTargetModelEntities[402729631] = 402729631
	-- seagulls
	forbiddenTargetModelEntities[3549666813] = 3549666813
	-- hawks
	forbiddenTargetModelEntities[2864127842] = 2864127842
	-- rabbits
	forbiddenTargetModelEntities[3753204865] = 3753204865
	-- coyotees
	forbiddenTargetModelEntities[1682622302] = 1682622302
	-- cats
	forbiddenTargetModelEntities[1462895032] = 1462895032
	-- lions
	forbiddenTargetModelEntities[307287994] = 307287994
	-- cows
	forbiddenTargetModelEntities[4244282910] = 4244282910
	-- rats
	forbiddenTargetModelEntities[3283429734] = 3283429734
	-- deers
	forbiddenTargetModelEntities[3630914197] = 3630914197
	-- pigs
	forbiddenTargetModelEntities[2971380566] = 2971380566
	-- dogs
	forbiddenTargetModelEntities[351016938] = 351016938 -- (Choppy)
	forbiddenTargetModelEntities[882848737] = 882848737
	forbiddenTargetModelEntities[1832265812] = 1832265812
	forbiddenTargetModelEntities[1318032802] = 1318032802
	forbiddenTargetModelEntities[2506301981] = 2506301981
	forbiddenTargetModelEntities[1125994524] = 1125994524 -- (Poodle)
end


-------------------------
-- RETURN MAIN MOD     --
-------------------------
return mbsRobMod







-- Copyright (c) 2016, Sladernimo, Member Of Vega Game Projects @ www.vega-gp.de