//ATM Spawner v3
//Coded By: Fish
//Included map spawns: rp_downtown_v2, rp_evocity_v2d, rp_bangclaw.

//If you would like to make custom ATM Banker spawns then please

require( "filex" )

if !file.Exists( "ATM/rp_downtown_v2.txt" ) then
	file.Write( "ATM/rp_downtown_v2.txt", "" )
	--
	if string.lower( game.GetMap() ) == "rp_downtown_v2" then
		filex.Append( "ATM/rp_downtown_v2.txt", "local ATMM = ents.Create( 'ATM_Machine' )\n" )
		filex.Append( "ATM/rp_downtown_v2.txt", "ATMM:SetPos( Vector( -1429.5125732422, 197.0241394043, -195.96875 ) )\n" )
		filex.Append( "ATM/rp_downtown_v2.txt", "ATMM:SetAngles( Angle( 0, -180 + 180, 0 ) )\n" )
		filex.Append( "ATM/rp_downtown_v2.txt", "ATMM:Spawn()\n" )
		filex.Append( "ATM/rp_downtown_v2.txt", "ATMM:Activate()\n" )
		--
		filex.Append( "ATM/rp_downtown_v2.txt", "local ATMM = ents.Create( 'ATM_Machine' )\n" )
		filex.Append( "ATM/rp_downtown_v2.txt", "ATMM:SetPos( Vector( -1433.4375, 35.1250, -195.7500 ) )\n" )
		filex.Append( "ATM/rp_downtown_v2.txt", "ATMM:SetAngles( Angle( 0, -180 + 180, 0 ) )\n" )
		filex.Append( "ATM/rp_downtown_v2.txt", "ATMM:Spawn()\n" )
		filex.Append( "ATM/rp_downtown_v2.txt", "ATMM:Activate()\n" )
	end
end

//Reset Bank Accounts... SuperAdmin Only!

concommand.Add( "atm_resetaccounts", function( ply )
	if !ply:IsSuperAdmin() then return end
	for k, v in pairs( player.GetAll() ) do
		v:SetPData( "pin", 9898989 )
		v:SetPData( "bankmoney", 0 )
	end
end )

hook.Add( "PlayerInitialSpawn", "PSNoteBank", function( ply )
	ply:ChatPrint( "This server uses DarkRP ATM Banker v3!" ) 
end )

concommand.Add( "atm_setspawn", function( ply )
	if ply:IsSuperAdmin() then
		filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "local ATMM = ents.Create( 'ATM_Machine' )\n" )
		filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetPos( Vector( " .. ply:GetEyeTrace().HitPos.x .. ", " .. ply:GetEyeTrace().HitPos.y .. ", " .. ply:GetEyeTrace().HitPos.z .. " ) )\n" )
		if ply:GetAngles().y > -15 and ply:GetAngles().y < 15 then
			filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetAngles( Angle(  0, 0 + 180, 0 ) )\n" )
		elseif ply:GetAngles().y > 80 and ply:GetAngles().y < 100 then
			filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetAngles( Angle( 0, 90 + 180, 0 ) )\n" )
		elseif ply:GetAngles().y > -190 and ply:GetAngles().y < -170 then
			filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetAngles( Angle( 0, -180 + 180, 0 ) )\n" )
		elseif ply:GetAngles().y > -100 and ply:GetAngles().y < -80 then
			filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetAngles( Angle( 0, -90 + 180, 0 ) )\n" )
		else
			filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetAngles( Angle( " .. ply:GetAngles().p .. ", " .. ply:GetAngles().y + 180 .. ", " .. ply:GetAngles().r .. " ) )\n" )
		end
		filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:Spawn()\n" )
		filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:Activate()\n" )
		filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetUseType( SIMPLE_USE )\n" )
		--
		ply:ChatPrint( "ATM Position Set!" )
		ply:ChatPrint( "To spawn the ATMs, use the command: atm_spawnatms" )
		ply:ChatPrint( "To remove the ATM spawns, use the command: atm_removespawns" )
	end
end )

hook.Add( "PlayerSay", "CPS1", function( ply, text )
	if string.lower( text ) == "/setatmspawn" then
			if ply:IsSuperAdmin() then
			filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "local ATMM = ents.Create( 'ATM_Machine' )\n" )
			filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetPos( Vector( " .. ply:GetEyeTrace().HitPos.x .. ", " .. ply:GetEyeTrace().HitPos.y .. ", " .. ply:GetEyeTrace().HitPos.z .. " ) )\n" )
			if ply:GetAngles().y > -15 and ply:GetAngles().y < 15 then
				filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetAngles( Angle(  0, 0 + 180, 0 ) )\n" )
			elseif ply:GetAngles().y > 80 and ply:GetAngles().y < 100 then
				filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetAngles( Angle( 0, 90 + 180, 0 ) )\n" )
			elseif ply:GetAngles().y > -190 and ply:GetAngles().y < -170 then
				filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetAngles( Angle( 0, -180 + 180, 0 ) )\n" )
			elseif ply:GetAngles().y > -100 and ply:GetAngles().y < -80 then
				filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetAngles( Angle( 0, -90 + 180, 0 ) )\n" )
			else
				filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetAngles( Angle( " .. ply:GetAngles().p .. ", " .. ply:GetAngles().y + 180 .. ", " .. ply:GetAngles().r .. " ) )\n" )
			end
			filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:Spawn()\n" )
			filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:Activate()\n" )
			filex.Append( "ATM/"..string.lower( game.GetMap() )..".txt", "ATMM:SetUseType( SIMPLE_USE )\n" )
			--
			ply:ChatPrint( "ATM Position Set!" )
			ply:ChatPrint( "To spawn the ATMs, use the command: atm_spawnatms" )
			ply:ChatPrint( "To remove the ATM spawns, use the command: atm_removespawns" )
		end
	end
end )

concommand.Add( "atm_spawnall", function( ply )
	if ply:IsSuperAdmin() then
		RunString( file.Read( "ATM/"..string.lower( game.GetMap() )..".txt" ) )
	end
end )

concommand.Add( "atm_removespawns", function( ply )
	if ply:IsSuperAdmin() then
		file.Delete( "ATM/"..string.lower( game.GetMap() )..".txt" )
	end
end )

concommand.Add( "atm_removeall", function( ply )
	if ply:IsSuperAdmin() then
		for k, v in pairs( ents.FindByClass( "ATM_Machine" ) ) do
			v:Remove()
		end
	end
end )

timer.Simple( 2, function()
	if file.Exists( "ATM/"..string.lower( game.GetMap() )..".txt" ) then
		RunString( file.Read( "ATM/"..string.lower( game.GetMap() )..".txt" ) )
	end
end )