//ATM Spawner
//Made by: Fish and Wattled Walnut

//The Original Spawn Is For rp_downtown_v2, near the PD...

require( "filex" )

if !file.Exists( "ATM/spawns.txt" ) then
file.Write( "ATM/spawns.txt", "" )
--
if string.lower( game.GetMap() ) == "rp_downtown_v2" then
filex.Append( "ATM/spawns.txt", "local ATMM = ents.Create( 'ATM_Machine' )\n" )
filex.Append( "ATM/spawns.txt", "ATMM:SetPos( Vector( -1429.5125732422, 197.0241394043, -195.96875 ) )\n" )
filex.Append( "ATM/spawns.txt", "ATMM:SetAngles( Angle( 0, -180 + 180, 0 ) )\n" )
filex.Append( "ATM/spawns.txt", "ATMM:Spawn()\n" )
filex.Append( "ATM/spawns.txt", "ATMM:Activate()\n" )
--
filex.Append( "ATM/spawns.txt", "local ATMM = ents.Create( 'ATM_Machine' )\n" )
filex.Append( "ATM/spawns.txt", "ATMM:SetPos( Vector( -1433.4375, 35.1250, -195.7500 ) )\n" )
filex.Append( "ATM/spawns.txt", "ATMM:SetAngles( Angle( 0, -180 + 180, 0 ) )\n" )
filex.Append( "ATM/spawns.txt", "ATMM:Spawn()\n" )
filex.Append( "ATM/spawns.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", "PISNote", function( ply )
ply:ChatPrint( "This server is using ATM Banker v2!" ) 
end )

concommand.Add( "atm_setspawn", function( ply )
if ply:IsSuperAdmin() then
filex.Append( "ATM/spawns.txt", "local ATMM = ents.Create( 'ATM_Machine' )\n" )
filex.Append( "ATM/spawns.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/spawns.txt", "ATMM:SetAngles( Angle(  0, 0 + 180, 0 ) )\n" )
elseif ply:GetAngles().y > 80 and ply:GetAngles().y < 100 then
filex.Append( "ATM/spawns.txt", "ATMM:SetAngles( Angle( 0, 90 + 180, 0 ) )\n" )
elseif ply:GetAngles().y > -190 and ply:GetAngles().y < -170 then
filex.Append( "ATM/spawns.txt", "ATMM:SetAngles( Angle( 0, -180 + 180, 0 ) )\n" )
elseif ply:GetAngles().y > -100 and ply:GetAngles().y < -80 then
filex.Append( "ATM/spawns.txt", "ATMM:SetAngles( Angle( 0, -90 + 180, 0 ) )\n" )
else
filex.Append( "ATM/spawns.txt", "ATMM:SetAngles( Angle( " .. ply:GetAngles().p .. ", " .. ply:GetAngles().y + 180 .. ", " .. ply:GetAngles().r .. " ) )\n" )
end
filex.Append( "ATM/spawns.txt", "ATMM:Spawn()\n" )
filex.Append( "ATM/spawns.txt", "ATMM:Activate()\n" )
filex.Append( "ATM/spawns.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/spawns.txt", "local ATMM = ents.Create( 'ATM_Machine' )\n" )
filex.Append( "ATM/spawns.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/spawns.txt", "ATMM:SetAngles( Angle(  0, 0 + 180, 0 ) )\n" )
elseif ply:GetAngles().y > 80 and ply:GetAngles().y < 100 then
filex.Append( "ATM/spawns.txt", "ATMM:SetAngles( Angle( 0, 90 + 180, 0 ) )\n" )
elseif ply:GetAngles().y > -190 and ply:GetAngles().y < -170 then
filex.Append( "ATM/spawns.txt", "ATMM:SetAngles( Angle( 0, -180 + 180, 0 ) )\n" )
elseif ply:GetAngles().y > -100 and ply:GetAngles().y < -80 then
filex.Append( "ATM/spawns.txt", "ATMM:SetAngles( Angle( 0, -90 + 180, 0 ) )\n" )
else
filex.Append( "ATM/spawns.txt", "ATMM:SetAngles( Angle( " .. ply:GetAngles().p .. ", " .. ply:GetAngles().y + 180 .. ", " .. ply:GetAngles().r .. " ) )\n" )
end
filex.Append( "ATM/spawns.txt", "ATMM:Spawn()\n" )
filex.Append( "ATM/spawns.txt", "ATMM:Activate()\n" )
filex.Append( "ATM/spawns.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/spawns.txt" ) )
	end
end )

concommand.Add( "atm_removespawns", function( ply )
if ply:IsSuperAdmin() then
file.Delete( "ATM/spawns.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/spawns.txt" ) then
RunString( file.Read( "ATM/spawns.txt" ) )
	end
end )