Advertisement
rezecib

update.lua

Jun 16th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.03 KB | None | 0 0
  1. local function SpawnEndMeteors(maxmeteors)
  2.     maxmeteors = maxmeteors or 7
  3.     local nummeteor = math.random(1,maxmeteors)
  4.     for i,v in ipairs(AllPlayers) do
  5.         for k = 1, nummeteor do
  6.             if v and v:IsValid() then
  7.                 v:DoTaskInTime(((1 * math.random()) + .33) * k * .5, function()
  8.                     local pt = v:GetPosition()
  9.                     local theta = math.random() * 2 * PI
  10.                     local radius = 0
  11.                     if v:HasTag("playerghost") then --spread the meteors more once the player is a ghost
  12.                         radius = math.random(k+1, 10+(k*2))
  13.                     else
  14.                         radius = math.random(k-1, 5+(k*2))
  15.                     end
  16.                     local offset = FindWalkableOffset(pt, theta, radius, 12, true)
  17.                     if offset then
  18.                         pt = pt + offset
  19.                     end
  20.                     local meteor = SpawnPrefab("shadowmeteor")
  21.                     meteor.Transform:SetPosition(pt:Get())
  22.                 end)
  23.             end
  24.         end
  25.     end
  26. end
  27.  
  28.  
  29. local function SpawnEndHounds()
  30.     local numhounds = math.random(1,3)
  31.     for i,v in ipairs(AllPlayers) do
  32.         for k = 1, numhounds do
  33.             TheWorld.components.hounded:ForceReleaseSpawn(v)
  34.         end
  35.     end
  36. end
  37.  
  38. --this is an update that always runs on wall time (not sim time)
  39. function WallUpdate(dt)
  40.     if AUTOSPAWN_MASTER_SLAVE then
  41.         SpawnSecondInstance()
  42.     end
  43.  
  44.     --TheSim:ProfilerPush("LuaWallUpdate")
  45.  
  46.     TheSim:ProfilerPush("RPC queue")
  47.     HandleRPCQueue()
  48.     TheSim:ProfilerPop()
  49.  
  50.     HandleUserCmdQueue()
  51.  
  52.     if TheFocalPoint ~= nil then
  53.         TheSim:SetActiveAreaCenterpoint(TheFocalPoint.Transform:GetWorldPosition())
  54.     else
  55.         TheSim:SetActiveAreaCenterpoint(0, 0, 0)
  56.     end
  57.  
  58.     TheSim:ProfilerPush("updating wall components")
  59.     for k,v in pairs(WallUpdatingEnts) do
  60.         if v.wallupdatecomponents then
  61.             for cmp in pairs(v.wallupdatecomponents) do
  62.                 if cmp.OnWallUpdate then
  63.                     cmp:OnWallUpdate( dt )
  64.                 end
  65.             end
  66.         end
  67.     end
  68.     for k,v in pairs(NewWallUpdatingEnts) do
  69.         WallUpdatingEnts[k] = v
  70.         NewWallUpdatingEnts[k] = nil
  71.     end
  72.     TheSim:ProfilerPop()
  73.  
  74.     TheSim:ProfilerPush("mixer")
  75.     TheMixer:Update(dt)
  76.     TheSim:ProfilerPop()
  77.  
  78.     if not IsSimPaused() then
  79.         TheSim:ProfilerPush("camera")
  80.         TheCamera:Update(dt)
  81.         TheSim:ProfilerPop()
  82.     end
  83.  
  84.     CheckForUpsellTimeout(dt)
  85.  
  86.     TheSim:ProfilerPush("input")
  87.     if not SimTearingDown then
  88.         TheInput:OnUpdate()
  89.     end
  90.     TheSim:ProfilerPop()
  91.  
  92.     TheSim:ProfilerPush("fe")
  93.     if global_error_widget == nil then
  94.         TheFrontEnd:Update(dt)
  95.     else
  96.         global_error_widget:OnUpdate(dt)
  97.     end
  98.     TheSim:ProfilerPop()
  99.  
  100.     --TheSim:ProfilerPop()
  101.  
  102.     -- Server termination script
  103.     -- Only runs if the SERVER_TERMINATION_TIMER constant has been overriden (which we do with the pax demo)
  104.     if SERVER_TERMINATION_TIMER > 0 and TheNet:GetIsServer() then
  105.         local original_time = SERVER_TERMINATION_TIMER
  106.         SERVER_TERMINATION_TIMER = SERVER_TERMINATION_TIMER - dt
  107.  
  108.         if SERVER_TERMINATION_TIMER <= 60 and original_time % 5 <= 0.02 and SERVER_TERMINATION_TIMER > 0 then
  109.             SpawnEndHounds()
  110.         end
  111.         if SERVER_TERMINATION_TIMER <= 30 and original_time % 2 <= 0.02 and SERVER_TERMINATION_TIMER > 0 then
  112.             SpawnEndMeteors()
  113.         end
  114.  
  115.         if SERVER_TERMINATION_TIMER <= 0 then
  116.             TheSim:Quit()
  117.         elseif SERVER_TERMINATION_TIMER <= 30 and original_time > 30 then
  118.             TheNet:Announce( "The sky is falling!", nil )
  119.         elseif SERVER_TERMINATION_TIMER <= 60 and original_time > 60 then
  120.             TheNet:Announce( "Let slip the dogs of war!", nil )
  121.         elseif SERVER_TERMINATION_TIMER <= 120 and original_time > 120 then
  122.             TheNet:Announce( "End times are almost here.", nil )
  123.         elseif SERVER_TERMINATION_TIMER <= 180 and original_time > 180 then
  124.             TheNet:Announce( "End times are coming.", nil )
  125.         end
  126.     end
  127. end
  128.  
  129. function PostUpdate(dt)
  130.     --TheSim:ProfilerPush("LuaPostUpdate")
  131.     EmitterManager:PostUpdate()
  132.     --TheSim:ProfilerPop()
  133. end
  134.  
  135.  
  136. local StaticComponentLongUpdates = {}
  137. function RegisterStaticComponentLongUpdate(classname, fn)
  138.     StaticComponentLongUpdates[classname] = fn
  139. end
  140.  
  141.  
  142. local StaticComponentUpdates = {}
  143. function RegisterStaticComponentUpdate(classname, fn)
  144.     StaticComponentUpdates[classname] = fn
  145. end
  146.  
  147.  
  148. local last_tick_seen = -1
  149. --This is where the magic happens
  150. start_profiler = false
  151. local updates_profiled = 0
  152. local updates_to_profile = 30*5
  153. local total_time = 0
  154. function Update(dt)
  155.     HandleClassInstanceTracking()
  156.     --TheSim:ProfilerPush("LuaUpdate")
  157.     CheckDemoTimeout()
  158.  
  159.     if PLATFORM == "NACL" then
  160.         AccumulatedStatsHeartbeat(dt)
  161.     end
  162.  
  163.     if not IsSimPaused() then
  164.         local tick = TheSim:GetTick()
  165.         if tick > last_tick_seen then
  166.             TickRPCQueue()
  167.  
  168.             TheSim:ProfilerPush("scheduler")
  169.             for i = last_tick_seen +1, tick do
  170.                 RunScheduler(i)
  171.             end
  172.             TheSim:ProfilerPop()
  173.  
  174.             if SimShuttingDown then
  175.                 return
  176.             end
  177.  
  178.             TheSim:ProfilerPush("static components")
  179.             for k,v in pairs(StaticComponentUpdates) do
  180.                 v(dt)
  181.             end
  182.             TheSim:ProfilerPop()
  183.  
  184.             TheSim:ProfilerPush("updating components")
  185.             for k,v in pairs(UpdatingEnts) do
  186.                 --TheSim:ProfilerPush(v.prefab)
  187.                 if v.updatecomponents then
  188.                     for cmp in pairs(v.updatecomponents) do
  189.                         --TheSim:ProfilerPush(v:GetComponentName(cmp))
  190.                         if cmp.OnUpdate and not StopUpdatingComponents[cmp] then
  191.                             cmp:OnUpdate(dt)
  192.                         end
  193.                         --TheSim:ProfilerPop()
  194.                     end
  195.                 end
  196.                 --TheSim:ProfilerPop()
  197.             end
  198.  
  199.             for k,v in pairs(NewUpdatingEnts) do
  200.                 UpdatingEnts[k] = v
  201.             end
  202.             NewUpdatingEnts = {}
  203.  
  204.             for k,v in pairs(StopUpdatingComponents) do
  205.                 v:StopUpdatingComponent_Deferred(k)
  206.             end
  207.             StopUpdatingComponents = {}
  208.  
  209.             TheSim:ProfilerPop()
  210.  
  211.             for i = last_tick_seen + 1, tick do
  212.                 TheSim:ProfilerPush("LuaSG")
  213.                 SGManager:Update(i)
  214.                 TheSim:ProfilerPop()
  215.  
  216.                 TheSim:ProfilerPush("LuaBrain")
  217.                 updates_profiled = updates_profiled + 1
  218.                 local start = os.clock()
  219.                 BrainManager:Update(i)
  220.                 total_time = total_time + os.clock() - start
  221.                 if updates_profiled == updates_to_profile then
  222.                     print("Average time for brain update:", total_time/updates_to_profile*1000, "ms")
  223.                 end
  224.                 TheSim:ProfilerPop()
  225.             end
  226.         else
  227.             print ("Saw this before")
  228.         end
  229.         last_tick_seen = tick
  230.     end
  231.  
  232.     --TheSim:ProfilerPop()
  233. end
  234.  
  235. --this is for advancing the sim long periods of time (to skip nights, come back from caves, etc)
  236. function LongUpdate(dt, ignore_player)
  237.     --print ("LONG UPDATE", dt, ignore_player)
  238.     local function doupdate(dt)
  239.         for k,v in pairs(StaticComponentLongUpdates) do
  240.             v(dt)
  241.         end
  242.  
  243.         for i,v in ipairs(AllPlayers) do
  244.             if ignore_player then
  245.                 if v.components.beard then
  246.                     v.components.beard.pause = true
  247.                 end
  248.  
  249.                 if v.components.beaverness then
  250.                     v.components.beaverness.ignoremoon = true
  251.                 end
  252.             end
  253.         end
  254.  
  255.  
  256.         for k,v in pairs(Ents) do
  257.  
  258.             local should_ignore = false
  259.             if ignore_player then
  260.  
  261.                 if v.components.inventoryitem then
  262.                     local grand_owner = v.components.inventoryitem:GetGrandOwner()
  263.                     if grand_owner ~= nil then
  264.                         if grand_owner:HasTag("player") then
  265.                             should_ignore = true
  266.                         elseif grand_owner.prefab == "chester"
  267.                             or grand_owner.prefab == "hutch" then
  268.                             local leader = grand_owner.components.follower.leader
  269.                             if leader ~= nil and leader:HasTag("player") then
  270.                                 should_ignore = true
  271.                             end
  272.                         end
  273.                     end
  274.                 end
  275.  
  276.                 if v.components.follower and v.components.follower.leader and v.components.follower.leader:HasTag("player") then
  277.                     should_ignore = true
  278.                 end
  279.  
  280.                 if v:HasTag("player") then
  281.                     should_ignore = true
  282.                 end
  283.             end
  284.                
  285.             if not should_ignore then
  286.                 v:LongUpdate(dt)
  287.             end
  288.            
  289.         end
  290.  
  291.         for i,v in ipairs(AllPlayers) do
  292.             if v.components.beard then
  293.                 v.components.beard.pause = nil
  294.             end
  295.  
  296.             if v.components.beaverness then
  297.                 v.components.beaverness.ignoremoon = nil
  298.             end
  299.         end
  300.  
  301.     end
  302.  
  303.     doupdate(dt)
  304.  
  305. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement