new post | browse code | authors | help | about

LuaBin 2.0

Viewing file root / lua / includes / util.lua

  1.  
  2. /*---------------------------------------------------------
  3.     IsTableOfEntitiesValid
  4. ---------------------------------------------------------*/
  5. function IsTableOfEntitiesValid( tab )
  6.  
  7.         if (!tab) then return false end
  8.  
  9.         for k, v in pairs( tab ) do
  10.                 if ( !IsValid( v ) ) then return false end
  11.         end
  12.        
  13.         return true
  14.        
  15. end
  16.  
  17. /*---------------------------------------------------------
  18.     To easily create a colour table
  19. ---------------------------------------------------------*/
  20. function Color( r, g, b, a )
  21.  
  22.         a = a or 255
  23.         return { r = tonumber(r), g = tonumber(g), b = tonumber(b), a = tonumber(a) }
  24.        
  25. end
  26.  
  27. /*---------------------------------------------------------
  28.         Prints a table to the console
  29. ---------------------------------------------------------*/
  30. function PrintTable ( t, indent, done )
  31.  
  32.         done = done or {}
  33.         indent = indent or 0
  34.  
  35.         for key, value in pairs (t) do
  36.  
  37.                 Msg ( string.rep ("\t", indent) )
  38.  
  39.                 if type (value) == "table" and not done [value] then
  40.  
  41.                         done [value] = true
  42.                         Msg (tostring (key) .. ":" .. "\n");
  43.                         PrintTable (value, indent + 2, done)
  44.  
  45.                 else
  46.  
  47.                         Msg (tostring (key) .. "\t=\t")
  48.                         Msg (tostring(value) .. "\n")
  49.  
  50.                 end
  51.  
  52.         end
  53.  
  54. end
  55.  
  56.  
  57. /*---------------------------------------------------------
  58.    Returns a random vector
  59. ---------------------------------------------------------*/
  60. function VectorRand()
  61.  
  62.         return Vector( math.Rand(-1.0, 1.0), math.Rand(-1.0, 1.0), math.Rand(-1.0, 1.0) )
  63. end
  64.  
  65.  
  66.  
  67. /*---------------------------------------------------------
  68.    Convenience function to precache a sound
  69. ---------------------------------------------------------*/
  70. function Sound( name )
  71.         util.PrecacheSound( name )
  72.         return name
  73. end
  74.  
  75.  
  76. /*---------------------------------------------------------
  77.    Convenience function to precache a model
  78. ---------------------------------------------------------*/
  79. function Model( name )
  80.         util.PrecacheModel( name )
  81.         return name
  82. end
  83.  
  84.  
  85. // Some nice globals so we don't keep creating objects for no reason
  86.  
  87. vector_origin           = Vector( 0, 0, 0 )
  88. vector_up                       = Vector( 0, 0, 1 )
  89.  
  90. color_white             = Color( 255,   255,    255,    255 )
  91. color_black             = Color( 0,     0,              0,              255 )
  92. color_transparent       = Color( 255,   255,    255,    0 )
  93.  
  94.  
  95. /*---------------------------------------------------------
  96.    For shared files. Includes the file if clientside,
  97.    marks for send it if serverside.
  98. ---------------------------------------------------------*/
  99. function IncludeClientFile( filename )
  100.        
  101.         if ( CLIENT ) then
  102.                 include( filename )
  103.         else
  104.                 AddCSLuaFile( filename )
  105.         end
  106.        
  107. end
  108.  
  109. // Globals..
  110. FORCE_STRING    = 1
  111. FORCE_NUMBER    = 2
  112. FORCE_BOOL              = 3
  113.  
  114. /*---------------------------------------------------------
  115.    AccessorFunc
  116.    Quickly make Get/Set accessor fuctions on the specified table
  117. ---------------------------------------------------------*/
  118. function AccessorFunc( tab, varname, name, iForce )
  119.  
  120.         tab[ "Get"..name ] = function ( self ) return self[ varname ] end
  121.  
  122.         if ( iForce == FORCE_STRING ) then
  123.                 tab[ "Set"..name ] = function ( self, v ) self[ varname ] = tostring(v) end
  124.         return end
  125.        
  126.         if ( iForce == FORCE_NUMBER ) then
  127.                 tab[ "Set"..name ] = function ( self, v ) self[ varname ] = tonumber(v) end
  128.         return end
  129.        
  130.         if ( iForce == FORCE_BOOL ) then
  131.                 tab[ "Set"..name ] = function ( self, v ) self[ varname ] = tobool(v) end
  132.         return end
  133.        
  134.         tab[ "Set"..name ] = function ( self, v ) self[ varname ] = v end
  135.  
  136. end
  137.  
  138. /*---------------------------------------------------------
  139.    AccessorFuncNW - FOR ENTITIES ONLY
  140.    Quickly make Get/Set accessor fuctions on the specified entity
  141. ---------------------------------------------------------*/
  142. function AccessorFuncNW( tab, varname, name, varDefault, iForce )
  143.  
  144.         tab[ "Get"..name ] = function ( self ) return self:GetNetworkedVar( varname, varDefault ) end
  145.  
  146.         if ( iForce == FORCE_STRING ) then
  147.                 tab[ "Set"..name ] = function ( self, v ) self:SetNetworkedVar( varname, tostring(v) ) end
  148.         return end
  149.        
  150.         if ( iForce == FORCE_NUMBER ) then
  151.                 tab[ "Set"..name ] = function ( self, v ) self:SetNetworkedVar( varname, tonumber(v) ) end
  152.         return end
  153.        
  154.         if ( iForce == FORCE_BOOL ) then
  155.                 tab[ "Set"..name ] = function ( self, v ) self:SetNetworkedVar( varname, tobool(v) ) end
  156.         return end
  157.        
  158.         tab[ "Set"..name ] = function ( self, v ) self:SetNetworkedVar( varname, v ) end
  159.  
  160. end
  161.  
  162. /*---------------------------------------------------------
  163.     Returns true if object is valid (is not nil and IsValid)
  164. ---------------------------------------------------------*/
  165. function IsValid( object )
  166.  
  167.         if (!object) then return false end
  168.         return object:IsValid()
  169.        
  170. end
  171.  
  172. /*---------------------------------------------------------
  173.     Returns true if entity is valid
  174. ---------------------------------------------------------*/
  175.  
  176.  
  177. /*---------------------------------------------------------
  178.     Utility function, automatically prints error
  179. ---------------------------------------------------------*/
  180. function PCallError( ... )
  181.  
  182.         local errored, retval = pcall( unpack( arg ) )
  183.        
  184.         if ( !errored ) then
  185.                 ErrorNoHalt( retval )
  186.                 return false, retval
  187.         end
  188.        
  189.         return true, retval
  190.        
  191. end
  192.  
  193. /*---------------------------------------------------------
  194.     Safely remove an entity
  195. ---------------------------------------------------------*/
  196. function SafeRemoveEntity( ent )
  197.  
  198.         if ( !ent || !ent:IsValid() || ent:IsPlayer() ) then return end
  199.        
  200.         ent:Remove()
  201.        
  202. end
  203.  
  204. /*---------------------------------------------------------
  205.     Safely remove an entity (delayed)
  206. ---------------------------------------------------------*/
  207. function SafeRemoveEntityDelayed( ent, timedelay )
  208.  
  209.         if (!ent || !ent:IsValid()) then return end
  210.        
  211.         timer.Simple( timedelay, function() SafeRemoveEntity( ent ) end )
  212.        
  213. end
  214.  
  215. /*---------------------------------------------------------
  216.     Simple lerp
  217. ---------------------------------------------------------*/
  218. function Lerp( delta, from, to )
  219.  
  220.         if ( delta > 1 ) then return to end
  221.         if ( delta < 0 ) then return from end
  222.        
  223.         return from + (to - from) * delta;
  224.  
  225. end
  226.  
  227. /*---------------------------------------------------------
  228.     Convert Var to Bool
  229. ---------------------------------------------------------*/
  230. function tobool( val )
  231.         if ( val == nil || val == false || val == 0 || val == "0" || val == "false" ) then return false end
  232.         return true
  233. end
  234.  
  235. /*---------------------------------------------------------
  236.     Universal function to filter out crappy models by name
  237. ---------------------------------------------------------*/
  238. function UTIL_IsUselessModel( modelname )
  239.  
  240.         local modelname = modelname:lower()
  241.  
  242.         if ( modelname:find( "_gesture" ) ) then return true end
  243.         if ( modelname:find( "_anim" ) ) then return true end
  244.         if ( modelname:find( "_gst" ) ) then return true end
  245.         if ( modelname:find( "_pst" ) ) then return true end
  246.         if ( modelname:find( "_shd" ) ) then return true end
  247.         if ( modelname:find( "_ss" ) ) then return true end
  248.         if ( modelname:find( "_posture" ) ) then return true end
  249.         if ( modelname:find( "_anm" ) ) then return true end
  250.         if ( modelname:find( "ghostanim" ) ) then return true end
  251.         if ( modelname:find( "_paths" ) ) then return true end
  252.         if ( modelname:find( "_shared" ) ) then return true end
  253.        
  254.         return false
  255.  
  256. end
  257.  
  258.  
  259. /*---------------------------------------------------------
  260.     Remember/Restore cursor position..
  261.         Clientside only
  262.         If you have a system where you hold a key to show the cursor
  263.         Call RememberCursorPosition when the key is released and call
  264.         RestoreCursorPosition to restore the cursor to where it was
  265.         when the key was released.
  266.         If you don't the cursor will appear in the middle of the screen
  267. ---------------------------------------------------------*/
  268. local StoredCursorPos = {}
  269.  
  270.  
  271.         local x, y = gui.MousePos()
  272.        
  273.         // If the cursor isn't visible it will return 0,0 ignore it.
  274.         if ( x == 0 && y == 0 ) then return end
  275.        
  276.         StoredCursorPos.x, StoredCursorPos.y = x, y
  277.        
  278. end
  279.  
  280.  
  281.         if ( !StoredCursorPos.x || !StoredCursorPos.y ) then return end
  282.         input.SetCursorPos( StoredCursorPos.x, StoredCursorPos.y )
  283.  
  284. end
  285.  
  286.  
  287. /*---------------------------------------------------------
  288.     Given a number, returns the right 'th
  289. ---------------------------------------------------------*/
  290. function STNDRD( num )
  291.  
  292.         if ( num == 1 ) then return "st" end
  293.         if ( num == 2 ) then return "nd" end
  294.         if ( num == 3 ) then return "rd" end
  295.        
  296.         return "th"
  297.  
  298. end
  299.  
  300.  
  301. /*---------------------------------------------------------
  302.     From Simple Gamemode Base (Rambo_9)
  303. ---------------------------------------------------------*/
  304. function TimedSin(freq,min,max,offset)
  305.     return math.sin(freq * math.pi * 2 * CurTime() + offset) * (max-min) * 0.5 + min
  306. end
  307.  
  308. /*---------------------------------------------------------
  309.     From Simple Gamemode Base (Rambo_9)
  310. ---------------------------------------------------------*/
  311. function TimedCos(freq,min,max,offset)
  312.     return math.cos(freq * math.pi * 2 * CurTime() + offset) * (max-min) * 0.5 + min
  313. end
  314.  
  315. /*---------------------------------------------------------
  316.     IsEnemyEntityName
  317. ---------------------------------------------------------*/
  318. function IsEnemyEntityName( victimtype )
  319.  
  320.         if ( victimtype == "npc_combine_s" ) then return true; end
  321.         if ( victimtype == "npc_cscanner" ) then return true; end
  322.         if ( victimtype == "npc_manhack" ) then return true; end
  323.         if ( victimtype == "npc_hunter" ) then return true; end
  324.         if ( victimtype == "npc_antlion" ) then return true; end
  325.         if ( victimtype == "npc_antlionguard" ) then return true; end
  326.         if ( victimtype == "npc_antlion_worker" ) then return true; end
  327.         if ( victimtype == "npc_fastzombie_torso" ) then return true; end
  328.         if ( victimtype == "npc_fastzombie" ) then return true; end
  329.         if ( victimtype == "npc_headcrab" ) then return true; end
  330.         if ( victimtype == "npc_headcrab_fast" ) then return true; end
  331.         if ( victimtype == "npc_poisonzombie" ) then return true; end
  332.         if ( victimtype == "npc_headcrab_poison" ) then return true; end
  333.         if ( victimtype == "npc_zombie" ) then return true; end
  334.         if ( victimtype == "npc_zombie_torso" ) then return true; end
  335.         if ( victimtype == "npc_zombine" ) then return true; end
  336.         if ( victimtype == "npc_gman" ) then return true; end
  337.         if ( victimtype == "npc_breen" ) then return true; end
  338.  
  339.         return false
  340.  
  341. end
  342.  
  343. /*---------------------------------------------------------
  344.     IsFriendEntityName
  345. ---------------------------------------------------------*/
  346. function IsFriendEntityName( victimtype )
  347.  
  348.         if ( victimtype == "npc_monk" ) then return true; end
  349.         if ( victimtype == "npc_alyx" ) then return true; end
  350.         if ( victimtype == "npc_barney" ) then return true; end
  351.         if ( victimtype == "npc_citizen" ) then return true; end
  352.         if ( victimtype == "npc_kleiner" ) then return true; end
  353.         if ( victimtype == "npc_magnusson" ) then return true; end
  354.         if ( victimtype == "npc_eli" ) then return true; end
  355.         if ( victimtype == "npc_mossman" ) then return true; end
  356.         if ( victimtype == "npc_vortigaunt" ) then return true; end
  357.  
  358.         return false
  359.  
  360. end