Python1320

Untitled

Jan 24th, 2011
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.20 KB | None | 0 0
  1. local CLASSNAME = "gmod_tool"
  2.  
  3. local function check(variable, ...)
  4.     local name = debug.getinfo(2, "n").name
  5.     local func = debug.getinfo(2, "f").func
  6.     local types = {...}
  7.     local allowed = ""
  8.    
  9.     local matched = false
  10.    
  11.     for key, value in ipairs(types) do
  12.         if #types ~= key then
  13.             allowed = allowed .. value .. " or "
  14.         else
  15.             allowed = allowed .. value
  16.         end
  17.        
  18.         if type(variable) == value then
  19.             matched = true
  20.         end
  21.     end
  22.    
  23.     local arg = "???"
  24.    
  25.     for i=1, math.huge do
  26.         local key, value = debug.getlocal(2, i)
  27.         -- I'm not sure what to do about this part
  28.         if value == variable then
  29.             arg = i
  30.         break end
  31.     end
  32.    
  33.     if not matched then
  34.         error(("bad argument #%s to '%s' (%s expected, got %s)"):format(arg, name, allowed, type(typ)), 3)
  35.     end
  36. end
  37.  
  38. stool = stool or {}
  39.     local s = stool
  40.  
  41. stool.utils = {}
  42.     local u = {}
  43.  
  44. stool.stored_tools = {}
  45. stool.stool_meta = {}
  46. stool.weapon_meta = {}
  47.  
  48. stool.base_folder = "weapons/gmod_tool/stools/"
  49.  
  50. function u.GetFilnameFromPath(path, extension)
  51.     check(path, "string")
  52.     check(extension, "string", "nil")
  53.  
  54.     local tbl = ("/"):Explode(path)
  55.    
  56.     return tbl[#tbl]:Left(extension and -#extension or -5)
  57. end
  58.  
  59. function u.IsValidPhysics(v)
  60.     return type(v) == "PhysObj"
  61. end
  62.  
  63. function u.IsValidEntity(v)
  64.     return IsEntity(v) and v:IsValid()
  65. end
  66.  
  67. function u.IsWorld(v)
  68.     return IsEntity(v) and CLIENT and v:EntIndex() == 0 or SERVER and v:IsWorld() or false
  69. end
  70.  
  71. do -- register
  72.  
  73.     function stool.Register(TOOL, name)
  74.         check(TOOL, "table")
  75.         check(name, "string")
  76.             TOOL = s.GetSToolMeta():Create(TOOL)
  77.            
  78.             TOOL.Mode = name
  79.             TOOL:CreateConVars()
  80.            
  81.             s.stored_tools[name] = TOOL
  82.         _G.ToolObj = nil
  83.        
  84.         return TOOL
  85.     end
  86.  
  87.     function stool.RegisterFromFile(path, name)
  88.         check(path, "string")
  89.         check(name, "string", "nil")
  90.            
  91.         name = name or u.GetFilnameFromPath(path)
  92.        
  93.         _G.TOOL = {ClientConVar = {}}
  94.         _G.ToolObj = s.GetSToolMeta()
  95.         _G.SWEP = s.GetWeaponMeta()
  96.        
  97.             AddCSLuaFile(path)
  98.             include(path)
  99.             stool.Register(TOOL, name)
  100.            
  101.         _G.SWEP = nil
  102.         _G.ToolObj = nil
  103.         _G.TOOL = nil
  104.     end
  105.    
  106.     function stool.LoadTools(path, extension)
  107.         check(path, "string", "nil")
  108.         check(extension, "string", "nil")
  109.        
  110.         path = path or s.base_folder
  111.         extension = extension or "*.lua"
  112.        
  113.         for _, name in pairs(file.FindInLua(path .. extension)) do
  114.             stool.RegisterFromFile(path .. name)
  115.         end
  116.     end
  117.          
  118.     concommand.Add((SERVER and "sv_" or "cl_") .. "stool_reload", function(ply, _, args)
  119.         if ply:IsPlayer() and ply:IsAdmin() then   
  120.             local path = args[1]
  121.            
  122.             if path then
  123.                 s.RegisterFromFile(path)
  124.             else
  125.                 stool.LoadTools()
  126.             end
  127.         end
  128.     end)
  129. end
  130.  
  131. do -- get
  132.     function stool.Get(name)
  133.         if type(name) ~= "string" then return stool.GetSToolMeta() end
  134.        
  135.         return table.Copy(s.GetStored(name))
  136.     end
  137.  
  138.     function stool.GetStored(name)
  139.         check(name, "string")
  140.        
  141.         return s.stored_tools[name]
  142.     end
  143.  
  144.     function stool.GetAll()
  145.         return s.stored_tools
  146.     end
  147.    
  148.     function stool.GetWeaponMeta()
  149.         return s.weapon_meta
  150.     end
  151.    
  152.     function stool.GetSToolMeta()
  153.         return s.stool_meta
  154.     end
  155. end
  156.  
  157. do -- weapon meta
  158.     local SWEP = stool.weapon_meta
  159.    
  160.     SWEP.ClassName = "gmod_tool"
  161.     SWEP.Base = "weapon_base"
  162.        
  163.     SWEP.Author = ""
  164.     SWEP.Contact = ""
  165.     SWEP.Purpose = ""
  166.     SWEP.Instructions = ""
  167.  
  168.     SWEP.ViewModel = Model("models/weapons/v_toolgun.mdl")
  169.     SWEP.WorldModel = Model("models/weapons/w_toolgun.mdl")
  170.     SWEP.AnimPrefix = "python"
  171.    
  172.     SWEP.Weight = 5
  173.     SWEP.AutoSwitchTo = false
  174.     SWEP.AutoSwitchFrom = false
  175.  
  176.     SWEP.ShootSound = Sound( "Airboat.FireGunRevDown" )
  177.    
  178.     local mode_settings = {
  179.         ClipSize    = -1,
  180.         DefaultClip = -1,
  181.         Automatic = false,
  182.         Ammo = "none"
  183.     }
  184.    
  185.     SWEP.Primary = table.Copy(mode_settings)
  186.     SWEP.Secondary = table.Copy(mode_settings)
  187.  
  188.     SWEP.CanHolster = true
  189.     SWEP.CanDeploy = true
  190.    
  191.     SWEP.Tool = {}
  192.    
  193.     function SWEP:SetupDataTables()
  194.         self:DTVar("Int", 0, "Stage")
  195.     end
  196.    
  197.     function SWEP:CheckLimit( str )
  198.         return self.Owner:CheckLimit( str )
  199.     end
  200.  
  201.     function SWEP:ShouldDropOnDie()
  202.         return false
  203.     end
  204.  
  205.     function SWEP:InitializeTools()
  206.         local temp = {}
  207.        
  208.         for k,v in pairs( stool.GetAll() ) do
  209.             temp[k] = table.Copy(v)
  210.             temp[k].SWEP = self
  211.             temp[k].Owner = self.Owner
  212.             temp[k].Weapon = self.Weapon
  213.         end
  214.        
  215.         self.Tool = temp
  216.     end
  217.  
  218.     function SWEP:Initialize()
  219.         self:InitializeTools()
  220.        
  221.         // We create these here. The problem is that these are meant to be constant values.
  222.         // in the toolmode they're not because some tools can be automatic while some tools aren't.
  223.         // Since this is a global table it's shared between all instances of the gun.
  224.         // By creating new tables here we're making it so each tool has its own instance of the table
  225.         // So changing it won't affect the other tools.
  226.        
  227.         self.Primary = table.Copy(mode_settings)
  228.         self.Secondary = table.Copy(mode_settings)
  229.     end
  230.  
  231.     function SWEP:OnRestore()
  232.         self:InitializeTools()
  233.     end
  234.  
  235.     function SWEP:Reload()
  236.        
  237.         local tool = self:GetToolObject()
  238.        
  239.         if not self.Owner:KeyPressed(IN_RELOAD) or not tool then return end
  240.  
  241.         local mode = self:GetMode()
  242.         local trace = self.Owner:GetEyeTrace()
  243.         if trace.Hit then
  244.             tool:CheckObjects()
  245.             if tool:Allowed() and gamemode.Call( "CanTool", self.Owner, trace, mode ) and tool:Reload( trace ) then
  246.                 self:DoShootEffect(trace.HitPos, trace.HitNormal, trace.Entity, trace.PhysicsBone, IsFirstTimePredicted())
  247.             end
  248.         end
  249.     end
  250.    
  251.     function SWEP:GetMode()
  252.         return self.Mode
  253.     end
  254.  
  255.     local mode
  256.     local tool
  257.    
  258.     function SWEP:Think()
  259.         mode = self.Owner:GetInfo( "gmod_toolmode" )
  260.         self.Mode = mode
  261.        
  262.         tool = self:GetToolObject()
  263.        
  264.         if tool then
  265.             tool:CheckObjects()
  266.            
  267.             self.last_mode = self.current_mode
  268.             self.current_mode = mode
  269.            
  270.             if tool:Allowed() then
  271.                 if self.last_mode ~= self.current_mode and self:GetToolObject( self.last_mode ) then
  272.                     self:GetToolObject(self.last_mode):Holster()
  273.                 end
  274.                
  275.                 self.Primary.Automatic = tool.LeftClickAutomatic or false
  276.                 self.Secondary.Automatic = tool.RightClickAutomatic or false
  277.                 self.RequiresTraceHit = tool.RequiresTraceHit or true
  278.                
  279.                 tool:Think()
  280.             else
  281.                 self:GetToolObject( self.last_mode ):ReleaseGhostEntity()
  282.             end
  283.         end
  284.     end
  285.  
  286.     function SWEP:DoShootEffect( hitpos, hitnormal, entity, physbone, bFirstTimePredicted )
  287.  
  288.         self:EmitSound( self.ShootSound )
  289.         self:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
  290.         self.Owner:SetAnimation( PLAYER_ATTACK1 )
  291.        
  292.         if not bFirstTimePredicted then return end
  293.        
  294.         local effectdata = EffectData()
  295.             effectdata:SetOrigin( hitpos )
  296.             effectdata:SetNormal( hitnormal )
  297.             effectdata:SetEntity( entity )
  298.             effectdata:SetAttachment( physbone )
  299.         util.Effect( "selection_indicator", effectdata )   
  300.        
  301.         local effectdata = EffectData()
  302.             effectdata:SetOrigin( hitpos )
  303.             effectdata:SetStart( self.Owner:GetShootPos() )
  304.             effectdata:SetAttachment( 1 )
  305.             effectdata:SetEntity( self )
  306.         util.Effect( "ToolTracer", effectdata )
  307.        
  308.     end
  309.    
  310.     function SWEP:Attack(toolfunc)
  311.         local tool = self:GetToolObject()
  312.         if tool then           
  313.             local trace = self.Owner:GetEyeTrace()
  314.             if trace.Hit then
  315.                 tool:CheckObjects()            
  316.                 if
  317.                     tool:Allowed() and
  318.                     gamemode.Call( "CanTool", self.Owner, trace, self:GetMode() ) and
  319.                     tool[toolfunc](tool, trace)
  320.                 then
  321.                     self:DoShootEffect( trace.HitPos, trace.HitNormal, trace.Entity, trace.PhysicsBone, IsFirstTimePredicted() )
  322.                 end
  323.             end
  324.         end
  325.     end
  326.  
  327.     function SWEP:PrimaryAttack()
  328.         self:Attack("LeftClick")
  329.     end
  330.  
  331.     function SWEP:SecondaryAttack()
  332.         self:Attack("RightClick")
  333.     end
  334.  
  335.     function SWEP:ContextScreenClick( aimvec, mousecode, pressed, ply )
  336.         local tool = self:GetToolObject()
  337.         if tool and not (CLIENT and SinglePlayer()) and pressed then                   
  338.             local trace = util.TraceLine( utilx.GetPlayerTrace( CLIENT and GetViewEntity() or ply:GetViewEntity(), aimvec ) )
  339.            
  340.             if trace.Hit then      
  341.                 tool:CheckObjects()
  342.                 if
  343.                     tool:Allowed() and
  344.                     gamemode.Call( "CanTool", self.Owner, trace, self:GetMode()) and
  345.                     (
  346.                         mousecode == MOUSE_LEFT and tool:LeftClick( trace ) or
  347.                         mousecode == MOUSE_RIGHT and tool:RightClick( trace )
  348.                     )
  349.                 then           
  350.                     self:DoShootEffect( trace.HitPos, trace.HitNormal, trace.Entity, trace.PhysicsBone, true )
  351.                 end
  352.             end
  353.         end
  354.     end
  355.  
  356.     function SWEP:Holster()
  357.         local tool = self:GetToolObject()
  358.         return tool and tool:Holster() or self.CanHolster
  359.     end
  360.  
  361.     function SWEP:OnRemove()
  362.         local tool = self:GetToolObject()
  363.         if tool then
  364.             tool:ReleaseGhostEntity()
  365.         end    
  366.     end
  367.  
  368.     SWEP.OwnerChanged = SWEP.OnRemove
  369.  
  370.     function SWEP:Deploy()
  371.         local tool = self:GetToolObject()
  372.         if tool then
  373.             tool:UpdateData()
  374.             return tool:Deploy()
  375.         end
  376.         return self.CanDeploy
  377.     end
  378.  
  379.     function SWEP:GetToolObject( mode )
  380.         return self.Tool[ mode or self:GetMode() ] or false
  381.     end
  382.  
  383.     function SWEP:FireAnimationEvent( _,_,event )
  384.         return event == 21 or event == 5003 or nil
  385.     end
  386.    
  387.     if CLIENT then
  388.         local matScreen     = Material( "models/weapons/v_toolgun/screen" )
  389.         local txidScreen    = surface.GetTextureID( "models/weapons/v_toolgun/screen" )
  390.         local txRotating    = surface.GetTextureID( "pp/fb" )
  391.         local txBackground  = surface.GetTextureID( "models/weapons/v_toolgun/screen_bg" )
  392.         local RTTexture     = GetRenderTarget( "GModToolgunScreen", 256, 256 )
  393.  
  394.         surface.CreateFont( "Arial Black", 82, 1000, true, false, "GModToolScreen" )
  395.  
  396.         local function DrawScrollingText( text, y, texwide )
  397.  
  398.                 local w, h = surface.GetTextSize( text  )
  399.                 w = w + 64
  400.                
  401.                 local x = math.fmod( CurTime() * 400, w ) * -1;
  402.                
  403.                 while ( x < texwide ) do
  404.                
  405.                     surface.SetTextColor( 0, 0, 0, 255 )
  406.                     surface.SetTextPos( x + 3, y + 3 )
  407.                     surface.DrawText( text )
  408.                        
  409.                     surface.SetTextColor( 255, 255, 255, 255 )
  410.                     surface.SetTextPos( x, y )
  411.                     surface.DrawText( text )
  412.                    
  413.                     x = x + w
  414.                    
  415.                 end
  416.  
  417.         end
  418.        
  419.         local render = render
  420.         local gmod_toolmode = gmod_toolmode
  421.         local cam = cam
  422.         local ScrH = ScrH
  423.         local ScrW = ScrW
  424.        
  425.         local tool
  426.        
  427.         local TEX_SIZE = 256
  428.         local mode  = gmod_toolmode:GetString()
  429.         local NewRT = RTTexture
  430.         local oldW = ScrW()
  431.         local oldH = ScrH()
  432.        
  433.         function SWEP:RenderScreen()
  434.             tool = self:GetToolObject()
  435.            
  436.             if tool then   
  437.                 mode = gmod_toolmode:GetString()
  438.                 NewRT = RTTexture
  439.                 oldW = ScrW()
  440.                 oldH = ScrH()
  441.                
  442.                 matScreen:SetMaterialTexture( "$basetexture", NewRT )
  443.                
  444.                 local OldRT = render.GetRenderTarget()
  445.                     render.SetRenderTarget( NewRT )
  446.                     render.SetViewPort( 0, 0, TEX_SIZE, TEX_SIZE )
  447.                     cam.Start2D()
  448.                         surface.SetDrawColor( 255, 255, 255, 255 )
  449.                         surface.SetTexture( txBackground )
  450.                         surface.DrawTexturedRect( 0, 0, TEX_SIZE, TEX_SIZE )
  451.                         if tool.DrawToolScreen then
  452.                             self:GetToolObject():DrawToolScreen( TEX_SIZE, TEX_SIZE )
  453.                         else
  454.                             surface.SetFont( "GModToolScreen" )
  455.                             DrawScrollingText( "#Tool_"..mode.."_name", 64, TEX_SIZE )
  456.                         end
  457.  
  458.                     cam.End2D()
  459.                 render.SetRenderTarget( OldRT )
  460.                 render.SetViewPort( 0, 0, oldW, oldH )
  461.             end
  462.         end
  463.    
  464.  
  465.         local gmod_drawhelp = CreateClientConVar( "gmod_drawhelp", "1", true, false )
  466.         gmod_toolmode = CreateClientConVar( "gmod_toolmode", "rope", true, true )
  467.  
  468.         SWEP.PrintName = "Tool Gun NEW"        
  469.         SWEP.Slot = 5  
  470.         SWEP.SlotPos = 6   
  471.         SWEP.DrawAmmo = false
  472.         SWEP.DrawCrosshair = true
  473.  
  474.         SWEP.Spawnable = false
  475.         SWEP.AdminSpawnable = false
  476.  
  477.         SWEP.WepSelectIcon = surface.GetTextureID( "vgui/gmod_tool" )
  478.         SWEP.Gradient = surface.GetTextureID( "gui/gradient" )
  479.         SWEP.InfoIcon = surface.GetTextureID( "gui/info" )
  480.  
  481.         SWEP.ToolNameHeight = 0
  482.         SWEP.InfoBoxHeight = 0
  483.  
  484.         surface.CreateFont( "Arial Black", 48, 1000, true, false, "GModToolName" )
  485.         surface.CreateFont( "Arial", 24, 1000, true, false, "GModToolSubtitle" )
  486.         surface.CreateFont( "Arial", 17, 1000, true, false, "GModToolHelp" )
  487.  
  488.         local x, y = 50, 40
  489.         local w, h = 0, 0
  490.            
  491.         function SWEP:DrawHUD()
  492.  
  493.             if not gmod_drawhelp:GetBool() then return end
  494.            
  495.             local mode = gmod_toolmode:GetString()
  496.            
  497.             if not self:GetToolObject() then return end
  498.            
  499.             self:GetToolObject():DrawHUD()
  500.                
  501.             w, h = 0, 0
  502.            
  503.             local TextTable = {}
  504.             local QuadTable = {}
  505.            
  506.             QuadTable.texture   = self.Gradient
  507.             QuadTable.color     = Color( 10, 10, 10, 180 )
  508.            
  509.             QuadTable.x = 0
  510.             QuadTable.y = y-8
  511.             QuadTable.w = 600
  512.             QuadTable.h = self.ToolNameHeight - (y-8)
  513.             draw.TexturedQuad( QuadTable )
  514.            
  515.             TextTable.font = "GModToolName"
  516.             TextTable.color = Color( 240, 240, 240, 255 )
  517.             TextTable.pos = { x, y }
  518.             TextTable.text = "#Tool_"..mode.."_name"
  519.            
  520.             w, h = draw.TextShadow( TextTable, 2 )
  521.             y = y + h
  522.            
  523.  
  524.             TextTable.font = "GModToolSubtitle"
  525.             TextTable.pos = { x, y }
  526.             TextTable.text = "#Tool_"..mode.."_desc"
  527.             w, h = draw.TextShadow( TextTable, 1 )
  528.             y = y + h + 8
  529.            
  530.             self.ToolNameHeight = y
  531.            
  532.             //y = y + 4
  533.            
  534.             QuadTable.x = 0
  535.             QuadTable.y = y
  536.             QuadTable.w = 600
  537.             QuadTable.h = self.InfoBoxHeight
  538.             local alpha =  math.Clamp( 255 + (self:GetToolObject().LastMessage - CurTime())*800, 10, 255 )
  539.             QuadTable.color = Color( alpha, alpha, alpha, 230 )
  540.             draw.TexturedQuad( QuadTable )
  541.                
  542.             y = y + 4
  543.            
  544.             TextTable.font = "GModToolHelp"
  545.             TextTable.pos = { x + self.InfoBoxHeight, y  }
  546.             TextTable.text = "#Tool_"..mode.."_"..self:GetToolObject():GetStage()
  547.             w, h = draw.TextShadow( TextTable, 1 )
  548.            
  549.             surface.SetDrawColor( 255, 255, 255, 255 )
  550.             surface.SetTexture( self.InfoIcon )
  551.             surface.DrawTexturedRect( x+1, y+1, h-3, h-3 ) 
  552.            
  553.             self.InfoBoxHeight = h + 8
  554.            
  555.         end
  556.  
  557.         function SWEP:SetStage( ... )
  558.             return self:GetToolObject() and self:GetToolObject():SetStage( ... ) or false
  559.         end
  560.  
  561.         function SWEP:GetStage( ... )
  562.             return self:GetToolObject() and self:GetToolObject():GetStage( ... ) or false
  563.         end
  564.  
  565.         function SWEP:ClearObjects( ... )          
  566.             if self:GetToolObject() then self:GetToolObject():ClearObjects( ... ) end
  567.         end
  568.  
  569.         function SWEP:StartGhostEntities( ... )
  570.             if self:GetToolObject() then self:GetToolObject():StartGhostEntities( ... ) end
  571.         end
  572.  
  573.         function SWEP:PrintWeaponInfo( x, y, alpha )   
  574.         end
  575.  
  576.         function SWEP:FreezeMovement()
  577.             if self:GetToolObject() then return self:GetToolObject():FreezeMovement() end
  578.         end
  579.  
  580.  
  581.     end
  582.    
  583.     weapons.Register(SWEP, CLASSNAME, true)
  584. end
  585.  
  586. do -- stool meta
  587.     local META = s.stool_meta
  588.     META.__index = META
  589.    
  590.     function META:Create(o)
  591.         o = o or {}
  592.        
  593.         o = setmetatable(o, META)
  594.        
  595.         o.Mode = nil
  596.         o.SWEP = nil
  597.         o.Owner = nil
  598.         o.ClientConVar = {}
  599.         o.ServerConVar = {}
  600.         o.Objects = {}
  601.         o.Stage = 0
  602.         o.Message = "start"
  603.         o.LastMessage = 0
  604.         o.AllowedCVar = 0
  605.        
  606.         return o
  607.     end
  608.  
  609.     function META:UpdateData()
  610.         self:SetStage(self:NumObjects())
  611.     end
  612.  
  613.     function META:SetStage(i)
  614.         if SERVER then
  615.             self:GetWeapon().dt.Stage = i
  616.         end
  617.     end
  618.  
  619.     function META:GetStage()
  620.         return self:GetWeapon().dt.Stage
  621.     end
  622.  
  623.     function META:ClearObjects()
  624.         self:ReleaseGhostEntity()
  625.         self.Objects = {}
  626.         self:SetStage( 0 )
  627.     end
  628.  
  629.     function META:GetEnt(i)    
  630.         return self.Objects[i] and self.Objects[i].Ent or NULL
  631.     end
  632.  
  633.     function META:GetPos(i)
  634.         local object = self.Objects[i]
  635.        
  636.         -- a b or c overkill?
  637.         return
  638.             object and         
  639.                 u.IsWorld(object.Ent) and
  640.                     object.Pos
  641.                 or
  642.                
  643.                 u.IsValidPhysics(object.Phys) and
  644.                     object.Phys:LocalToWorld(object.Pos)
  645.                 or
  646.                
  647.                 object.Ent:IsValid() and
  648.                     object.Ent:LocalToWorld(object.Pos)
  649.                 or
  650.                
  651.                 nil
  652.     end
  653.  
  654.     function META:GetLocalPos(i)
  655.         return self.Objects[i].Pos
  656.     end
  657.  
  658.     function META:GetBone(i)
  659.         return self.Objects[i].Bone
  660.     end
  661.  
  662.     function META:GetNormal(i)
  663.         local object = self.Objects[i]
  664.        
  665.         if object then
  666.             if u.IsWorld(object.Ent) then
  667.                 return object.Normal
  668.             else
  669.                 local normal =
  670.                
  671.                     u.IsValidPhysics(object.Phys) and
  672.                         object.Phys:LocalToWorld(object.Normal)
  673.                     or
  674.                    
  675.                     u.IsValidEntity(object.Ent) and
  676.                         object.Ent:LocalToWorld(object.Normal)
  677.                
  678.                 return normal and (normal - self:GetPos(i)) or nil
  679.             end
  680.         end
  681.     end
  682.  
  683.     function META:GetPhys(i)
  684.         return self.Objects[i].Phys
  685.     end
  686.  
  687.     function META:SetObject(i, ent, pos, phys, bone, norm)
  688.  
  689.         local object = {}
  690.         object.Ent = ent
  691.         object.Phys = phys
  692.         object.Bone = bone
  693.         object.Normal = norm
  694.        
  695.         if u.IsWorld(ent) then
  696.             object.Phys = nil
  697.             object.Pos = pos
  698.         else
  699.             norm = norm + pos
  700.  
  701.             if u.IsValidPhysics(phys) then
  702.                 object.Normal = object.Phys:WorldToLocal(norm)
  703.                 object.Pos = object.Phys:WorldToLocal(pos)
  704.             elseif u.IsValidEntity(ent) then
  705.                 object.Normal = object.Ent:WorldToLocal(norm)
  706.                 object.Pos = object.Ent:WorldToLocal(pos)
  707.             end
  708.         end
  709.            
  710.         self.Objects[i] = object
  711.     end
  712.  
  713.     function META:NumObjects()
  714.         return CLIENT and self:GetStage() or #self.Objects
  715.     end
  716.  
  717.     function META:CreateConVars()
  718.         local mode = self:GetMode():lower()
  719.  
  720.         if CLIENT then
  721.             for cvar, default in pairs( self.ClientConVar ) do
  722.                 CreateClientConVar( mode.."_"..cvar, default, true, true )
  723.             end
  724.         else
  725.             self.AllowedCVar = CreateConVar( "toolmode_allow_"..mode, 1, FCVAR_NOTIFY )
  726.         end
  727.     end
  728.  
  729.     function META:GetServerInfo(property)      
  730.         return GetConVarString( self:GetMode():lower().."_"..property )
  731.     end
  732.  
  733.     function META:GetClientInfo(property)      
  734.         return self:GetOwner():GetInfo( self:GetMode():lower().."_"..property )
  735.     end
  736.  
  737.     function META:GetClientNumber( property, default )
  738.         return self:GetOwner():GetInfoNum( self:GetMode().."_"..property, default or 0 )
  739.     end
  740.  
  741.     function META:Allowed()
  742.         return SERVER and self.AllowedCVar:GetBool() or true
  743.     end
  744.  
  745.     function META:GetMode() return self.Mode end
  746.     function META:GetSWEP() return self.SWEP end
  747.     function META:GetOwner() return self:GetSWEP().Owner or self.Owner end
  748.     function META:GetWeapon() return self:GetSWEP().Weapon or self.Weapon end
  749.  
  750.     function META:LeftClick() return false end
  751.     function META:RightClick() return false end
  752.     function META:Reload() self:ClearObjects() end
  753.     function META:Deploy() self:ReleaseGhostEntity() return end
  754.     function META:Holster() self:ReleaseGhostEntity() return end
  755.     function META:Think() self:ReleaseGhostEntity() end
  756.    
  757.     function META:CheckObjects()
  758.         for k, v in pairs( self.Objects ) do
  759.             if not u.IsWorld(v.Ent) and not v.Ent:IsValid() then
  760.                 self:ClearObjects()
  761.             end
  762.         end
  763.     end
  764.    
  765.     if CLIENT then
  766.    
  767.         function META:FreezeMovement()
  768.             return false
  769.         end
  770.        
  771.         function META:DrawHUD() end
  772.        
  773.     end
  774.  
  775.     function META:MakeGhostEntity( model, pos, angle )
  776.         if SERVER and not SinglePlayer() or CLIENT and SinglePlayer() then return end
  777.        
  778.        
  779.         self:ReleaseGhostEntity()
  780.        
  781.         if util.IsValidProp( model ) then
  782.             local ghost = ents.Create( "prop_physics" )
  783.            
  784.             if ghost:IsValid() then            
  785.                 ghost:SetModel( model )
  786.                 ghost:SetPos( pos )
  787.                 ghost:SetAngles( angle )
  788.                 ghost:Spawn()
  789.                
  790.                 ghost:SetSolid( SOLID_VPHYSICS )
  791.                 ghost:SetMoveType( MOVETYPE_NONE )
  792.                 ghost:SetNotSolid( true )
  793.                 ghost:SetRenderMode( RENDERMODE_TRANSALPHA )
  794.                 ghost:SetColor( 255, 255, 255, 150 )
  795.                
  796.                 self.GhostEntity = ghost
  797.             end
  798.         end
  799.     end
  800.  
  801.     function META:StartGhostEntity( ent )
  802.         if SERVER and not SinglePlayer() or CLIENT and SinglePlayer() then return end
  803.         self:MakeGhostEntity( ent:GetModel(), ent:GetPos(), ent:GetAngles() )
  804.     end
  805.  
  806.     function META:ReleaseGhostEntity()
  807.         if self.GhostEntity then
  808.             SafeRemoveEntity(self.GhostEntity)
  809.             self.GhostEntity = nil
  810.         end
  811.        
  812.         if self.GhostEntities then
  813.             for k,v in pairs( self.GhostEntities ) do
  814.                 SafeRemoveEntity(self.GhostEntities[k])
  815.                 self.GhostEntities[k] = nil
  816.             end
  817.             self.GhostEntities = nil
  818.         end
  819.        
  820.         self.GhostOffset = nil     
  821.     end
  822.  
  823.     function META:UpdateGhostEntity()
  824.         local ghost = self.GhostEntity
  825.         if u.IsValidEntity(ghost) then
  826.             local trace = util.TraceLine( util.GetPlayerTrace( self:GetOwner(), self:GetOwner():GetCursorAimVector() ) )
  827.             local ent = self:GetEnt(1)
  828.             if trace.Hit and ent:IsValid() then            
  829.                 ghost:SetPos( ent:GetPos() )
  830.                 ghost:SetAngles( ent:AlignAngles( self:GetNormal(1):Angle(), (trace.HitNormal * -1):Angle() ) )
  831.                 ghost:SetPos(trace.HitPos + (ent:GetPos() - ghost:LocalToWorld(self:GetLocalPos(1))) + trace.HitNormal)
  832.             end
  833.         end
  834.     end
  835.  
  836. end
  837.  
  838. if SERVER then
  839.     for k, v in pairs(ents.FindByClass(CLASSNAME)) do
  840.         local ply = v.Owner
  841.         v:Remove()
  842.         timer.Simple(0.2, function()
  843.             if ply:IsPlayer() then
  844.                 ply:Give(CLASSNAME)
  845.             else
  846.                 Entity(1):Give(CLASSNAME)
  847.             end
  848.         end)
  849.     end
  850. end
  851.  
  852. do -- base test
  853.     local TOOL = {}
  854.  
  855.     TOOL.Category = "base tool"
  856.     TOOL.Name = "base test"
  857.    
  858.     TOOL.BaseValue_B = "HEY"
  859.  
  860.     function TOOL:LeftClick( trace )
  861.     end
  862.  
  863.     function TOOL:RightClick( trace )
  864.     end
  865.  
  866.     function TOOL:Reload( trace )
  867.     end
  868.  
  869.     function TOOL:Think()
  870.     end
  871.  
  872.     stool.Register(TOOL, "base_test")
  873. end
  874.  
  875. do -- example
  876.     local TOOL = {}
  877.    
  878.     TOOL.Base = "base_test"
  879.    
  880.     TOOL.Category = "test tool"
  881.     TOOL.Name = "test test"
  882.    
  883.     TOOL.BaseValue_A = "oh princess"
  884.  
  885.     function TOOL:LeftClick( trace )
  886.     end
  887.  
  888.     function TOOL:RightClick( trace )
  889.     end
  890.  
  891.     function TOOL:Reload( trace )
  892.     end
  893.  
  894.     function TOOL:Think()
  895.     end
  896.  
  897.     stool.Register(TOOL, "test_tool")
  898. end
  899.  
  900. --stool.LoadTools()
  901.  
  902. if CLIENT then epoe.Print("YIPPY\n") else print("YIPPY\n") end
Advertisement
Add Comment
Please, Sign In to add comment