CapsAdmin

Untitled

Oct 25th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.99 KB | None | 0 0
  1. local asdf =
  2. {
  3.     player.GetByUniqueID(--[[PotcFdk]] "2211883894"),
  4. }
  5.  
  6. if table.HasValue(asdf, LocalPlayer()) then return end
  7.  
  8. kingdom = kingdom or {} local kingdom = kingdom
  9.  
  10. if kingdom.Plants then
  11.     for key, data in pairs(kingdom.Plants) do  
  12.         for key, mdl in pairs(data.models) do
  13.             SafeRemoveEntity(data.models[key])
  14.         end
  15.     end
  16. end
  17.  
  18. kingdom.Seed = 1
  19.  
  20. local math_random = math.random
  21. local VectorRand = VectorRand
  22.  
  23. if false then
  24.     local function R(seed)
  25.         return util.CRC(seed) / (2^32-1)
  26.     end
  27.  
  28.     function math_random(min, max, extra)
  29.         extra = extra or 0
  30.        
  31.         if not min or not max then
  32.             return R(kingdom.Seed + extra)
  33.         end
  34.        
  35.         return min + (R(kingdom.Seed + extra) * (max-min))
  36.     end
  37.  
  38.     function VectorRand()
  39.         return Vector(math_random(-1, 1, 1), math_random(-1, 1, 2),math_random(-1, 1, 3))
  40.     end
  41. end
  42. kingdom.Plants =
  43. {
  44.     trees =
  45.     {
  46.         size = 0.2,
  47.         dontmove = true,
  48.         compare = function(ang) return ang < 0.75 and ang > 0.35 end,
  49.         models =
  50.         {
  51.             "models/props_foliage/rock_forest01d.mdl",
  52.             "models/props_foliage/rock_forest01a.mdl",
  53.             "models/props_foliage/rock_forest03.mdl",
  54.             "models/props_foliage/rock_forest04.mdl",
  55.         },
  56.     },
  57.    
  58.     spooky =
  59.     {
  60.         chance = 0.5,
  61.         size = 0.2,
  62.         dontmove = true,
  63.         compare = function(ang) return ang < 0.5 end,
  64.         models =
  65.         {
  66.             "models/props_hive/nest_extract.mdl",
  67.             "models/props_hive/nest_lrg_flat.mdl",
  68.             "models/props_hive/nest_med_flat.mdl",
  69.         }
  70.     },
  71.    
  72.     misc =
  73.     {
  74.         chance = 0.95,
  75.         size = 5,
  76.         height = 5,
  77.         minheight = 7,
  78.         compare = function(ang) return ang > 0.6 end,
  79.         models =
  80.         {
  81.             --"models/props_swamp/shroom_ref_01_cluster.mdl",
  82.             "models/props_swamp/shroom_ref_01.mdl",
  83.         },
  84.     },
  85.        
  86.     --[[bushes =
  87.     {
  88.         chance = 0.99,
  89.         size = 1,
  90.         height = 1,
  91.         compare = function(ang) return ang > 0.3 end,
  92.         models =
  93.         {
  94.             "models/props_swamp/shrub_03_cluster.mdl",
  95.             "models/props_swamp/shrub_03b.mdl",
  96.             "models/props_swamp/shrub_03c.mdl",
  97.         },
  98.     },]]
  99.        
  100.     ferns =
  101.     {
  102.         chance = 0.99,
  103.         size = 1,
  104.         height = 2,
  105.         compare = function(ang) return ang > 0.3 end,
  106.         models =
  107.         {
  108.             "models/props_swamp/fern_01.mdl",
  109.             "models/props_swamp/fern_02.mdl",
  110.             "models/props_swamp/fern_03.mdl",
  111.             "models/props_swamp/fern_04.mdl",
  112.             "models/props_swamp/fern_05.mdl",
  113.             "models/props_swamp/fern_06.mdl",
  114.         }
  115.     },
  116.    
  117.     grass =
  118.     {
  119.         size = 2,
  120.         height = 0.7,
  121.         compare = function(ang) return ang > 0.3 end,
  122.         models =
  123.         {
  124.             "models/props_swamp/tallgrass_03.mdl",
  125.             "models/props_swamp/tallgrass_04.mdl",
  126.             "models/props_swamp/tallgrass_05.mdl",
  127.             "models/props_swamp/tallgrass_06.mdl",
  128.             "models/props_swamp/tallgrass_07.mdl",
  129.             "models/props_swamp/tallgrass_08.mdl",
  130.         }
  131.     },
  132.  
  133.     --[[crystals =
  134.     {
  135.         chance = 0.99,
  136.         size = 2,
  137.         compare = function(ang) return ang < 0 end,
  138.         material = "debug/env_cubemap_model",
  139.         models =
  140.         {
  141.             "models/props_foliage/spikeplant01.mdl",
  142.         }
  143.     },
  144.    
  145.     grey_grass =
  146.     {
  147.         chance = 0.8,
  148.         size = 2,
  149.         height = 0.5,
  150.         compare = function(ang) return ang > 0.3 end,
  151.         models =
  152.         {
  153.             "models/props_swamp/reeds.mdl",
  154.             "models/props_swamp/cattails_ref01.mdl",
  155.             "models/props_swamp/cattails_ref02.mdl",
  156.         },
  157.     },]]
  158.  
  159. }
  160.  
  161. if false then
  162. kingdom.Plants =
  163. {
  164.     trees =
  165.     {
  166.         size = 1,
  167.         dontmove = true,
  168.         compare = function(ang) return ang < 0.75 and ang > 0.35 end,
  169.         models =
  170.         {
  171.             "models/props_halloween/pumpkin_03.mdl",
  172.             "models/props_halloween/pumpkin_02.mdl",
  173.             "models/props_halloween/pumpkin_01.mdl",
  174.         },
  175.     },
  176.    
  177.     spooky =
  178.     {
  179.         chance = 0.9,
  180.         size = 5,
  181.         dontmove = true,
  182.         compare = function(ang) return ang < 0.5 end,
  183.         models =
  184.         {
  185.             "models/props_halloween/smlprop_spider.mdl",
  186.         }
  187.     },
  188.    
  189.     misc =
  190.     {
  191.         chance = 0.95,
  192.         size = 2,
  193.         minheight = 7,
  194.         compare = function(ang) return ang > 0.6 end,
  195.         models =
  196.         {
  197.             --"models/props_swamp/shroom_ref_01_cluster.mdl",
  198.             "models/props_halloween/candle.mdl",
  199.         },
  200.     },
  201.    
  202.     grass =
  203.     {
  204.         size = 3,
  205.         compare = function(ang) return ang > 0.3 end,
  206.         models =
  207.         {
  208.             "models/skeleton/skeleton_arm.mdl",
  209.             "models/skeleton/skeleton_arm_L.mdl",
  210.             "models/skeleton/skeleton_arm_L_noskins.mdl",
  211.             "models/skeleton/skeleton_arm_noskins.mdl",
  212.             "models/skeleton/skeleton_leg.mdl",
  213.             "models/skeleton/skeleton_leg_L.mdl",
  214.             "models/skeleton/skeleton_leg_L_noskins.mdl",
  215.             "models/skeleton/skeleton_leg_noskins.mdl",
  216.             "models/skeleton/skeleton_torso.mdl",
  217.             "models/skeleton/skeleton_torso2.mdl",
  218.             "models/skeleton/skeleton_torso2_noskins.mdl",
  219.             "models/skeleton/skeleton_torso3.mdl",
  220.             "models/skeleton/skeleton_torso3_noskins.mdl",
  221.             "models/skeleton/skeleton_torso_noskins.mdl",
  222.             "models/props_mvm/mvm_skeleton_arm.mdl",
  223.             "models/props_mvm/mvm_skeleton_leg.mdl",
  224.         }
  225.     },
  226. }
  227. end
  228.  
  229. local BOX_FRONT = 0
  230. local BOX_BACK = 1
  231. local BOX_RIGHT = 2
  232. local BOX_LEFT = 3
  233. local BOX_TOP = 4
  234. local BOX_BOTTOM = 5
  235.  
  236. for key, data in pairs(kingdom.Plants) do
  237.     for key, mdl in pairs(data.models) do
  238.         local ent = ClientsideModel(mdl, RENDERGROUP_OPAQUE)
  239.         ent:Spawn()
  240.         if data.material then
  241.             ent:SetMaterial(data.material)
  242.         end
  243.         ent.mins = ent:OBBMins()
  244.         ent.maxs = ent:OBBMaxs()
  245.         ent.rads = ent:BoundingRadius()
  246.         ent.center = ent.mins - ent.maxs
  247.        
  248.         ent.ldirs =
  249.         {
  250.             [BOX_FRONT] = ent.center + Vector(0, ent.rads, 0),
  251.             [BOX_BACK] = ent.center + Vector(0, -ent.rads, 0),
  252.            
  253.             [BOX_TOP] = ent.center + Vector(0, 0, ent.rads),
  254.             [BOX_BOTTOM] = ent.center + Vector(0, 0, -ent.rads),
  255.            
  256.             [BOX_LEFT] = ent.center + Vector(ent.rads, 0, 0),
  257.             [BOX_RIGHT] = ent.center + Vector(-ent.rads, 0, 0),
  258.            
  259.         }
  260.        
  261.         data.models[key] = ent
  262.     end
  263. end
  264.  
  265. local eyepos, eyeang = Vector(0,0,0), Angle(0,0,0)
  266. hook.Add("RenderScene", "kingdom", function(pos, ang) eyepos = pos eyeang = ang end)
  267.  
  268. local min_dist = 180
  269. min_dist = min_dist * min_dist
  270.  
  271. local MAX_DRAW_CALLS = 2048
  272. local PAUSE_RAY = false
  273.  
  274. do -- raymond
  275.     local scale = 2
  276.     local density = 1.25
  277.    
  278.     local res = 70
  279.     local max = 64
  280.     local max_cache = 1024
  281.     local ray_length = 2048*2
  282.     local box_size = 64
  283.     local forward_amount = 1.25
  284.  
  285.     kingdom.RayCache = {}
  286.  
  287.     local _in = {}
  288.     local cache_count = 0
  289.     local ordered = {}
  290.    
  291.     max = math.floor(max)
  292.     res = math.floor(res)
  293.     max_cache = math.floor(max_cache)
  294.    
  295.     function kingdom.SerializePos(pos, _res)
  296.         _res = _res or res
  297.         return string.format("%i%i%i", (pos.x+32000) / res, (pos.y+32000)  / res, (pos.z+32000) / res)
  298.     end
  299.    
  300.     local function VectorRandEx() return Vector(1 + math_random(), 1 + math_random(), 1 + math_random()) end
  301.    
  302.     function kingdom.HandleTraceResults(data)
  303.         if data.HitTexture == "**displacement**" then
  304.             local ang = data.HitNormal:Angle()
  305.             ang:RotateAroundAxis(ang:Right(), 270)
  306.             ang:RotateAroundAxis(data.HitNormal, math_random() * 360)
  307.             data.HitAngles = ang
  308.            
  309.             data.RandomVector = VectorRandEx()
  310.             data.RandomNumber = math_random()
  311.            
  312.             data.Dot = data.HitNormal:GetNormalized():Dot(-physenv.GetGravity():GetNormalized())
  313.            
  314.             data.CenterPos = data.HitPos - (data.Normal * 4)
  315.             data.LightColor = render.GetLightColor(data.CenterPos) * 0.2
  316.            
  317.             data.PixVis = util.GetPixelVisibleHandle()
  318.             data.LastVisible = 0
  319.             data.Alpha = 1
  320.             data.FadeIn = 0
  321.            
  322.             data.ModelInfo = {}
  323.                        
  324.             for key, info in RandomPairs(kingdom.Plants) do            
  325.                 if (not info.chance or math.random() > info.chance) and info.compare(data.Dot) then
  326.                     data.ModelInfo.Entity = info.models[math.random(#info.models)]
  327.                    
  328.                     local scale = kingdom.BaseScale * (VectorRandEx() * info.size)
  329.                                            
  330.                     if info.height then
  331.                         if info.minheight then
  332.                             scale.z = info.minheight + (math.random() * info.height)
  333.                         else
  334.                             scale.z = (math.random() * info.height)
  335.                         end
  336.                     end
  337.                    
  338.                     data.ModelScale = scale
  339.                     data.ModelRadius = data.ModelInfo.Entity.rads * scale
  340.                    
  341.                     for k,v in pairs(info) do
  342.                         data.ModelInfo[k] = data.ModelInfo[k] or  v
  343.                     end
  344.                     break
  345.                 end
  346.             end
  347.            
  348.             return data
  349.         end
  350.     end
  351.    
  352.     local VectorRand = VectorRand
  353.     local util_TraceLine = util.TraceLine
  354.     local MASK_SOLID_BRUSHONLY = MASK_SOLID_BRUSHONLY
  355.    
  356.     local key
  357.     local data
  358.     local wait = 0
  359.     local dir = Vector()
  360.    
  361.     function kingdom.GetFields(pos)
  362.         if PAUSE_RAY then
  363.             return kingdom.RayCache
  364.         end
  365.    
  366.         pos = pos or eyepos
  367.         pos = pos + LocalPlayer():GetVelocity() / 20
  368.         key = kingdom.SerializePos(pos)
  369.        
  370.         --kingdom.Seed = (kingdom.Seed + tonumber(kingdom.SerializePos(eyepos, 4)))%10000000
  371.        
  372.         if not util.QuickTrace(pos, Vector(0,0,1000)).Hit then
  373.             pos = pos + Vector(0,0,1000)
  374.             dir = Vector(0,0,-2)
  375.         else
  376.             dir = Vector(0,0,0)
  377.         end
  378.        
  379.         if not kingdom.RayCache[key] and wait < RealTime() then
  380.             for i = 1, max do
  381.                 key = kingdom.SerializePos(pos)
  382.                
  383.                 if kingdom.RayCache[key] then
  384.                     pos = eyepos + VectorRand() * box_size
  385.                     continue
  386.                 end
  387.                
  388.                 _in.start = pos
  389.                 _in.endpos = _in.start + (eyepos-pos):GetNormalized()*0.5 + (VectorRand()+dir) * ray_length
  390.                 _in.mask = MASK_SOLID_BRUSHONLY
  391.                
  392.                 data = util_TraceLine(_in)
  393.                 if data.Hit then
  394.                     key = kingdom.SerializePos(data.HitPos)
  395.  
  396.                     if kingdom.HandleTraceResults(data) and not kingdom.RayCache[key] then
  397.                         pos = data.HitPos
  398.                         kingdom.RayCache[key] = data
  399.                     end
  400.                 else
  401.                     wait = RealTime() + 0.25
  402.                     break
  403.                 end
  404.             end
  405.         end
  406.                
  407.         return kingdom.RayCache
  408.     end
  409. end
  410.  
  411. kingdom.BaseScale = Vector(1, 1, 1)
  412.  
  413. local forward = 64
  414. local sway_scale = 2
  415. local brightness = 1
  416.  
  417. local sway = 0
  418.  
  419.  
  420. local util_PixelVisible = util.PixelVisible
  421.  
  422. local max_dist = 3000
  423. max_dist = max_dist * max_dist
  424.  
  425. local len
  426. local alpha
  427.  
  428. function kingdom.CalcVis()
  429.     local t = RealTime()
  430.     local ply = LocalPlayer()
  431.  
  432.     for key, params in pairs(kingdom.GetFields()) do
  433.         len = (eyepos - params.CenterPos):LengthSqr()
  434.         if len > max_dist then
  435.             kingdom.RayCache[key] = nil
  436.         elseif len < min_dist then
  437.             params.Alpha = 1
  438.             params.Visible = true
  439.         else
  440.             alpha = util_PixelVisible(params.CenterPos, params.ModelInfo and params.ModelInfo.ModelRadius or 64, params.PixVis)
  441.             params.Alpha = math.min((-(len / max_dist) + 1)*2 + alpha * 10, 1)
  442.             params.Visible = alpha > 0
  443.         end
  444.     end
  445. end
  446.  
  447. local render_SetBlend = render.SetBlend
  448. local render_SetLightingOrigin = render.SetLightingOrigin
  449. local render_SetColorModulation = render.SetColorModulation
  450. local render_ResetModelLighting = render.ResetModelLighting
  451. local render_SuppressEngineLighting = render.SuppressEngineLighting
  452. local render_SetModelLighting = render.SetModelLighting
  453. local render_GetSurfaceColor = render.GetSurfaceColor
  454.  
  455. local c
  456.  
  457. function kingdom.Draw()
  458.     local delta = FrameTime()
  459.     local t = RealTime()
  460.     local ply = LocalPlayer()
  461.     local i = 0
  462.    
  463.     for key, params in pairs(kingdom.GetFields()) do   
  464.         if params.Visible then
  465.             local data = params.ModelInfo          
  466.             local ent = data.Entity or NULL
  467.            
  468.             if not ent:IsValid() then continue end
  469.            
  470.             -- pos
  471.             ent:SetRenderOrigin(params.HitPos)
  472.            
  473.             -- ang
  474.             local ang = params.HitAngles
  475.            
  476.             if not data.dontmove then
  477.                 ang = ang + Angle(0,math.sin(t + params.HitPos.z) * sway_scale,0)
  478.             end
  479.            
  480.             ent:SetRenderAngles(ang)
  481.  
  482.             -- fade
  483.             if params.Alpha ~= 1 or params.FadeIn ~= 1 then
  484.                 params.FadeIn = math.min(params.FadeIn + delta * 4, 1)
  485.                 local fade = params.Alpha * params.FadeIn
  486.                 ent:SetModelScale(params.ModelScale * math.max(fade, 0.75))
  487.                 render_SetBlend(fade)
  488.             else
  489.             -- scale
  490.                 render.SetBlend(1)
  491.                 ent:SetModelScale(params.ModelScale)
  492.             end
  493.            
  494.             ent:SetupBones()
  495.            
  496.             -- lighting
  497.             render_SuppressEngineLighting(true)
  498.             render_SetLightingOrigin(params.CenterPos)
  499.             render_ResetModelLighting(
  500.                 params.LightColor.r,
  501.                 params.LightColor.g,
  502.                 params.LightColor.b
  503.             )
  504.                            
  505.             for k, v in pairs(ent.ldirs) do
  506.                 params.SurfaceColors = params.SurfaceColors or {}
  507.                 c = params.SurfaceColors[k]
  508.                
  509.                 if not c then
  510.                     c = render_GetSurfaceColor(params.CenterPos, ang:Up() *-5 + params.CenterPos + v * -20)
  511.                     c.r = c.r ^ 1.25
  512.                     c.g = c.g ^ 1.25
  513.                     c.b = c.b ^ 1.25
  514.                 end
  515.                        
  516.                 render_SetModelLighting(
  517.                     k,
  518.                     c.r,
  519.                     c.g,
  520.                     c.b
  521.                 )
  522.                 -- sometimes it fails, so let's just use a blank color instead of it going super bright
  523.                 if c.r < 2 and c.g < 2 and c.b < 2 then
  524.                     params.SurfaceColors[k] = c
  525.                 else
  526.                     params.SurfaceColors[k] = Vector(1,1,1)
  527.                 end
  528.             end
  529.                
  530.             ent:DrawModel()
  531.            
  532.             render_SuppressEngineLighting(false)
  533.             render.SetColorModulation(brightness,brightness,brightness)
  534.         end
  535.     end
  536. end
  537.  
  538. local draw_method = 2
  539.  
  540. if draw_method == 1 then
  541.     hook.Add("PostDrawOpaqueRenderables", "kingdom", function()
  542.         kingdom.CalcVis()
  543.         kingdom.Draw()
  544.     end)
  545. elseif draw_method == 2 then
  546.     SafeRemoveEntity(kingdom.render_node)
  547.     local node = ClientsideModel("error.mdl")
  548.     kingdom.render_node = node
  549.    
  550.    
  551.     hook.Add("PostDrawOpaqueRenderables", "kingdom", function()
  552.         node:SetPos(eyepos + eyeang:Forward() * 32)
  553.     end)
  554.    
  555.     function node:RenderOverride()
  556.         kingdom.CalcVis()
  557.         kingdom.Draw()
  558.     end
  559. end
Advertisement
Add Comment
Please, Sign In to add comment