Advertisement
Guest User

Untitled

a guest
Feb 18th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.64 KB | None | 0 0
  1. class 'Freeroam'
  2.  
  3. function Freeroam:__init()
  4.     self.vehicles               = {}
  5.     self.player_spawns          = {}
  6.     self.teleports              = {}
  7.     self.hotspots               = {}
  8.  
  9.     -- Weapons to use
  10.     self.one_handed             = { Weapon.Handgun, Weapon.Revolver, Weapon.SMG,
  11.                                     Weapon.SawnOffShotgun }
  12.  
  13.     self.two_handed             = { Weapon.Assault, Weapon.Shotgun,
  14.                                     Weapon.Sniper, Weapon.MachineGun }
  15.  
  16.     self.ammo_counts            = {
  17.         [2] = { 12, 60 }, [4] = { 7, 35 }, [5] = { 30, 90 },
  18.         [6] = { 3, 18 }, [11] = { 20, 100 }, [13] = { 6, 36 },
  19.         [14] = { 4, 32 }, [16] = { 3, 12 }, [17] = { 5, 5 },
  20.         [28] = { 26, 130 }
  21.     }
  22.  
  23.     -- Load spawns
  24.     self:LoadSpawns( "spawns.txt" )
  25.  
  26.     -- Subscribe to events
  27.     Events:Subscribe( "ClientModuleLoad",   self, self.ClientModuleLoad )
  28.     Events:Subscribe( "ModuleUnload",       self, self.ModuleUnload )
  29.     Events:Subscribe( "ModulesLoad",        self, self.ModulesLoad )
  30.     Events:Subscribe( "PlayerSpawn",        self, self.PlayerSpawn )
  31.     Events:Subscribe( "PlayerChat",         self, self.PlayerChat )
  32.     Events:Subscribe( "PlayerDeath",         self, self.PlayerDeath )
  33. end
  34.  
  35. -- Functions to parse the spawns
  36. function Freeroam:LoadSpawns( filename )
  37.     -- Open up the spawns
  38.     print("Opening " .. filename)
  39.     local file = io.open( filename, "r" )
  40.  
  41.     if file == nil then
  42.         print( "No spawns.txt, aborting loading of spawns" )
  43.         return
  44.     end
  45.  
  46.     -- Start a timer to measure load time
  47.     local timer = Timer()
  48.  
  49.     -- For each line, handle appropriately
  50.     for line in file:lines() do
  51.         if line:sub(1,1) == "V" then
  52.             self:ParseVehicleSpawn( line )
  53.         elseif line:sub(1,1) == "P" then
  54.             self:ParsePlayerSpawn( line )
  55.         elseif line:sub(1,1) == "T" then
  56.             self:ParseTeleport( line )
  57.         end
  58.     end
  59.    
  60.     for k, v in pairs(self.teleports) do
  61.         table.insert( self.hotspots, { k, v } )
  62.     end
  63.  
  64.     print( string.format( "Loaded spawns, %.02f seconds",
  65.                             timer:GetSeconds() ) )
  66.  
  67.     file:close()
  68. end
  69.  
  70. function Freeroam:ParseVehicleSpawn( line )
  71.     -- Remove start, end and spaces from line
  72.     line = line:gsub( "VehicleSpawn%(", "" )
  73.     line = line:gsub( "%)", "" )
  74.     line = line:gsub( " ", "" )
  75.  
  76.     -- Split line into tokens
  77.     local tokens = line:split( "," )  
  78.  
  79.     -- Model ID string
  80.     local model_id_str  = tokens[1]
  81.  
  82.     -- Create tables containing appropriate strings
  83.     local pos_str       = { tokens[2], tokens[3], tokens[4] }
  84.     local ang_str       = { tokens[5], tokens[6], tokens[7], tokens[8] }
  85.  
  86.     -- Create vehicle args table
  87.     local args = {}
  88.  
  89.     -- Fill in args table
  90.     args.model_id       = tonumber( model_id_str )
  91.     args.position       = Vector3(   tonumber( pos_str[1] ),
  92.                                     tonumber( pos_str[2] ),
  93.                                     tonumber( pos_str[3] ) )
  94.  
  95.     args.angle          = Angle(    tonumber( ang_str[1] ),
  96.                                     tonumber( ang_str[2] ),
  97.                                     tonumber( ang_str[3] ),
  98.                                     tonumber( ang_str[4] ) )
  99.  
  100.     if #tokens > 8 then
  101.         if tokens[9] ~= "NULL" then
  102.             -- If there's a template, set it
  103.             args.template = tokens[9]
  104.         end
  105.  
  106.         if #tokens > 9 then
  107.             if tokens[10] ~= "NULL" then
  108.                 -- If there's a decal, set it
  109.                 args.decal = tokens[10]
  110.             end
  111.         end
  112.     end
  113.  
  114.     -- Create the vehicle
  115.     args.enabled = true
  116.     local v = Vehicle.Create( args )
  117.  
  118.     -- Save to table
  119.     self.vehicles[ v:GetId() ] = v
  120. end
  121.  
  122. function Freeroam:ParsePlayerSpawn( line )
  123.     -- Remove start, spaces
  124.     line = line:gsub( "P", "" )
  125.     line = line:gsub( " ", "" )
  126.  
  127.     -- Split into tokens
  128.     local tokens        = line:split( "," )
  129.     -- Create table containing appropriate strings
  130.     local pos_str       = { tokens[1], tokens[2], tokens[3] }
  131.     -- Create vector
  132.     local vector        = Vector3(   tonumber( pos_str[1] ),
  133.                                     tonumber( pos_str[2] ),
  134.                                     tonumber( pos_str[3] ) )
  135.  
  136.     -- Save to table
  137.     table.insert( self.player_spawns, vector )
  138. end
  139.  
  140. function Freeroam:ParseTeleport( line )
  141.     -- Remove start, spaces
  142.     line = line:sub( 3 )
  143.     line = line:gsub( " ", "" )
  144.  
  145.     -- Split into tokens
  146.     local tokens        = line:split( "," )
  147.     -- Create table containing appropriate strings
  148.     local pos_str       = { tokens[2], tokens[3], tokens[4] }
  149.     -- Create vector
  150.     local vector        = Vector3(   tonumber( pos_str[1] ),
  151.                                     tonumber( pos_str[2] ),
  152.                                     tonumber( pos_str[3] ) )
  153.  
  154.     -- Save to teleports table
  155.     self.teleports[ tokens[1] ] = vector
  156. end
  157.  
  158. -- Functions for utility use
  159. function Freeroam:GiveNewWeapons( p )
  160.     -- Give random weapons from the predefined list
  161.     p:ClearInventory()
  162.  
  163.     local one_id = table.randomvalue( self.one_handed )
  164.     local two_id = table.randomvalue( self.two_handed )
  165.  
  166.     p:GiveWeapon( WeaponSlot.Right,
  167.         Weapon( one_id,
  168.             self.ammo_counts[one_id][1],
  169.             self.ammo_counts[one_id][2] * 6 ) )
  170.     p:GiveWeapon( WeaponSlot.Primary,
  171.         Weapon( two_id,
  172.             self.ammo_counts[two_id][1],
  173.             self.ammo_counts[two_id][2] * 6 ) )
  174. end
  175.  
  176. function Freeroam:RandomizePosition( pos, magnitude, offset )
  177.     if magnitude == nil then
  178.         magnitude = 10
  179.     end
  180.  
  181.     if offset == nil then
  182.         offset = 250
  183.     end
  184.  
  185.     return pos + Vector3(    math.random( -magnitude, magnitude ),
  186.                             math.random( -magnitude, 0 ) + offset,
  187.                             math.random( -magnitude, magnitude ) )
  188. end
  189.  
  190. -- Chat handlers
  191. -- Create table containing chat handlers
  192. ChatHandlers = {}
  193.  
  194. function ChatHandlers:teleport( args )
  195.     local dest = args[1]
  196.  
  197.     -- Handle user help
  198.     if dest == "" or dest == nil or dest == "help" then
  199.         args.player:SendChatMessage( "Teleport locations: ",
  200.                                         Color( 0, 255, 0 ) )
  201.  
  202.         local i = 0
  203.         local str = ""
  204.  
  205.         for k,v in pairs(self.teleports) do
  206.             -- Send message every 4 teleports
  207.             i = i + 1
  208.             str = str .. k
  209.  
  210.             if i % 4 ~= 0 then
  211.                 -- If it's not the last teleport of the line, add a comma
  212.                 str = str .. ", "
  213.             else
  214.                 args.player:SendChatMessage( "    " .. str, Color( 255, 255, 255 ) )
  215.                 str = ""
  216.             end
  217.         end
  218.     elseif self.teleports[dest] ~= nil then
  219.         -- If they're not in the main world, refuse them
  220.         if args.player:GetWorld() ~= DefaultWorld then
  221.             args.player:SendChatMessage(
  222.                 "You are not in the main world! Exit any gamemodes and try again.",
  223.                 Color( 255, 0, 0 ) )
  224.  
  225.             return
  226.         end
  227.  
  228.         -- If the teleport is valid, teleport them there
  229.         args.player:SetPosition(
  230.             self:RandomizePosition( self.teleports[dest] ) )
  231.     else
  232.         -- Notify of invalid teleport
  233.         args.player:SendChatMessage( "Invalid teleport destination!",
  234.                                         Color( 255, 0, 0 ) )
  235.     end
  236. end
  237.  
  238. -- Alias tp to teleport
  239. ChatHandlers.tp = ChatHandlers.teleport
  240.  
  241. -- Events
  242. function Freeroam:ClientModuleLoad( args )
  243.     Network:Send( args.player, "Hotspots", self.hotspots )
  244. end
  245.  
  246. function Freeroam:ModuleUnload( args )
  247.     -- On unload, remove all valid vehicles
  248.     for k,v in pairs(self.vehicles) do
  249.         if IsValid(v) then
  250.             v:Remove()
  251.         end
  252.     end
  253. end
  254.  
  255. function Freeroam:ModulesLoad()
  256.     for _, v in ipairs(self.player_spawns) do
  257.         Events:Fire( "SpawnPoint", v )
  258.     end
  259.  
  260.     for _, v in pairs(self.teleports) do
  261.         Events:Fire( "TeleportPoint", v )
  262.     end
  263. end
  264.  
  265. function Freeroam:PlayerSpawn( args )
  266.  
  267.     if args.player:GetWorld() == DefaultWorld then
  268.    
  269.         local death_pos = args.player:GetValue("death_pos")
  270.         if death_pos then
  271.             default_spawn = false
  272.             args.player:SetPosition(death_pos + Vector3(0, 2000, 0))
  273.         -- If there are any player spawns, then teleport them
  274.         elseif #self.player_spawns > 0 then
  275.             local position = table.randomvalue( self.player_spawns )            
  276.  
  277.             args.player:SetPosition( self:RandomizePosition( position ) )
  278.             default_spawn = false
  279.         end
  280.  
  281.         self:GiveNewWeapons( args.player )
  282.     end
  283.  
  284.     return default_spawn
  285. end
  286.  
  287. function Freeroam:PlayerChat( args )
  288.     local msg = args.text
  289.  
  290.     if msg:sub(1, 1) ~= "/" then
  291.         return true
  292.     end
  293.  
  294.     -- Truncate the starting character
  295.     msg = msg:sub(2)
  296.  
  297.     -- Split the message
  298.     local cmd_args = msg:split(" ")
  299.     local cmd_name = cmd_args[1]
  300.  
  301.     -- Remove the command name
  302.     table.remove( cmd_args, 1 )
  303.     cmd_args.player = args.player
  304.  
  305.     -- Grab the function
  306.     local func = ChatHandlers[string.lower(cmd_name)]
  307.     if func ~= nil then
  308.         -- If it's valid, call it
  309.         func( self, cmd_args )
  310.     end
  311.  
  312.     return false
  313. end
  314.  
  315. function Freeroam:PlayerDeath( args )
  316.     if args.killer and args.killer:GetSteamId() ~= args.player:GetSteamId() then
  317.         args.killer:SetMoney(args.killer:GetMoney() + 100)
  318.     end
  319.     args.player:SetValue("death_pos", args.player:GetPosition())
  320. end
  321.  
  322. -- Create our class, and start the script proper
  323. freeroam = Freeroam()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement