Advertisement
Guest User

Overv

a guest
Aug 3rd, 2010
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.01 KB | None | 0 0
  1. --[[
  2.     Fancy wrapper that allows you to send user messages of arbitrary length, so no 255 byte limit! :O
  3.     With the added bonus of a Lua implementation of all non-char value transfering in user messages. :3
  4.         By Overv
  5. ]]--
  6.  
  7. AddCSLuaFile( "autorun/cl_umsg.lua" )
  8.  
  9. if ( SERVER ) then
  10.     --[[
  11.         Custom transfer system
  12.     ]]--
  13.    
  14.     umsg.PoolString( "UM_C" )
  15.     umsg.PoolString( "UM_D" )
  16.    
  17.     local _id, _filter
  18.     local _size = 0
  19.     local _buffer = {}
  20.    
  21.     local Char = umsg.Char
  22.     local String = umsg.String
  23.    
  24.     local Start = umsg.Start
  25.     local End = umsg.End
  26.    
  27.     function umsg.Start( id, filter )
  28.         _id = id
  29.         _filter  = filter
  30.         _buffer = {}
  31.     end
  32.    
  33.     local function flushBuffer()
  34.         if ( #_buffer == 0 ) then return end
  35.        
  36.         Start( "UM_C", _filter )
  37.             for i = 1, 200 do
  38.                 Char( _buffer[i] )
  39.             end
  40.         End()
  41.        
  42.         _buffer = {}
  43.     end
  44.    
  45.     function umsg.End()
  46.         flushBuffer()
  47.        
  48.         Start( "UM_D", _filter )
  49.             String( _id )
  50.         End()
  51.     end
  52.    
  53.     --[[
  54.         Custom type sending
  55.     ]]--
  56.    
  57.     function umsg.Char( c )
  58.         _size = _size + 1
  59.         table.insert( _buffer, c )
  60.        
  61.         if ( #_buffer == 200 ) then
  62.             flushBuffer()
  63.         end
  64.     end
  65.    
  66.     function umsg.Angle( a )
  67.         umsg.Float( a.p )
  68.         umsg.Float( a.y )
  69.         umsg.Float( a.r )
  70.     end
  71.    
  72.     function umsg.Bool( b )
  73.         if ( b ) then
  74.             umsg.Char( 1 )
  75.         else
  76.             umsg.Char( 0 )
  77.         end
  78.     end
  79.    
  80.     function umsg.Entity( e )
  81.         umsg.Short( e:EntIndex() )
  82.     end
  83.    
  84.     function umsg.Float( f )
  85.         f = f or 0
  86.        
  87.         local neg = f < 0
  88.         f = math.abs( f )
  89.        
  90.         -- Extract significant digits and exponent
  91.         local e = 0
  92.         if ( f >= 1 ) then
  93.             while ( f >= 1 ) do
  94.                 f = f / 10
  95.                 e = e + 1
  96.             end
  97.         else
  98.             while ( f < 0.1 ) do
  99.                 f = f * 10
  100.                 e = e - 1
  101.             end
  102.         end
  103.        
  104.         -- Discard digits
  105.         local s = tonumber( string.sub( f, 3, 9 ) )
  106.        
  107.         -- Negate if the original number was negative
  108.         if ( neg ) then s = -s end
  109.        
  110.         -- Convert to unsigned
  111.         s = s + 8388608
  112.        
  113.         -- Send significant digits as 3 byte number
  114.        
  115.         local a = math.modf( s / 65536 ) s = s - a * 65536
  116.         local b = math.modf( s / 256 ) s = s - b * 256
  117.         local c = s
  118.        
  119.         umsg.Char( a - 128 )
  120.         umsg.Char( b - 128 )
  121.         umsg.Char( c - 128 )
  122.        
  123.         -- Send exponent
  124.         umsg.Char( e )
  125.     end
  126.    
  127.     function umsg.Long( l )
  128.         -- Convert to unsigned
  129.         l = l + 2147483648
  130.        
  131.         local a = math.modf( l / 16777216 ) l = l - a * 16777216
  132.         local b = math.modf( l / 65536 ) l = l - b * 65536
  133.         local c = math.modf( l / 256 ) l = l - c * 256
  134.         local d = l
  135.        
  136.         umsg.Char( a - 128 )
  137.         umsg.Char( b - 128 )
  138.         umsg.Char( c - 128 )
  139.         umsg.Char( d - 128 )
  140.     end
  141.    
  142.     function umsg.Short( s )
  143.         -- Convert to unsigned
  144.         s = ( s or 0 ) + 32768
  145.        
  146.         local a = math.modf( s / 256 )
  147.        
  148.         umsg.Char( a - 128 )
  149.         umsg.Char( s - a * 256 - 128 )
  150.     end
  151.    
  152.     function umsg.String( s )
  153.         for i = 1, #s do
  154.             umsg.Char( s:sub( i, i ):byte() - 128 )
  155.         end
  156.         umsg.Char( 0 )
  157.     end
  158.    
  159.     function umsg.Vector( v )
  160.         umsg.Float( v.x )
  161.         umsg.Float( v.y )
  162.         umsg.Float( v.z )
  163.     end
  164.    
  165.     function umsg.VectorNormal( v )
  166.         umsg.Vector( v )
  167.     end
  168. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement