cerastes

nocturnalzombies

Sep 24th, 2018
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.23 KB | None | 0 0
  1.  
  2. --[[
  3. Zombies become very active at night, scattering and moving around often
  4.  
  5. TwoHoursOfTerrorUse: from midnight to 2am zombies gain a speed boost
  6.  
  7. By Nolan Ritchie
  8.  
  9. Modified by cerastes
  10.  
  11. Changed the roaming algorithm:
  12. 1) A random number ("target"), a random increment, and a random location to roam to are picked for each zombie.
  13. 2) During every iteration of the roaming function, each zombie's target is reduced by the increment.
  14. 3) If the target reaches zero, steps 1 is repeated and the cycle restarts.
  15.  
  16. This approach makes zombie roaming look more random - zombies do not all change direction at the same time.
  17.  
  18. Finally, there's a "pullback" function. This roaming algorithm's downside is that the zombies might be directed
  19. to leave the player's cell. Once they leave, they can't be recalled easily. This means that this algorithm
  20. may actually push zombies away from the player, which is pretty much the exact opposite of the intended effect.
  21. So, a pullback function is needed to force the zombies to come towards the player if they get too close to the cell's edge.
  22.  
  23. ]]
  24.  
  25. local interval = 400;
  26. local PUCount = 0;
  27. local TwoHoursOfTerrorUse = false;
  28. local tempx;
  29. local tempy;
  30. local diffX;
  31. local diffY;
  32. local playerX;
  33. local playerY;
  34. local playerZ;
  35. local distanceFromPlayer;
  36. local target;
  37.  
  38. function updateZombie( currentZombie )
  39.     tempx = currentZombie:getX() + ZombRand(-100.0,100.0);
  40.     tempy = currentZombie:getY() + ZombRand(-100.0,100.0);
  41.     currentZombie:pathToLocation(tempx,tempy,currentZombie:getZ());
  42.     currentZombie:getModData().increment = ZombRand(100, 400);
  43.     currentZombie:getModData().target = ZombRand(interval, interval * 3 );
  44.     currentZombie:getModData().doNotDisturb = false;
  45. end
  46.  
  47.  
  48. function ScatterZombies()
  49.     local hour = getGameTime():getTimeOfDay();
  50.     if( hour < 5.0 ) or ( hour >= 22.0) then
  51.         if(TwoHoursOfTerrorUse) then sendClientCommand(getPlayer(), "NocturnalZombies", "ScatterZombies", {TwoHoursOfTerror= "true"});
  52.         else  sendClientCommand(getPlayer(), "NocturnalZombies", "ScatterZombies", {TwoHoursOfTerror= "false"}) end
  53.    
  54.         local zlist = getPlayer():getCell():getZombieList();
  55.        
  56.         if(zlist ~= nil) then      
  57.        
  58.             playerX = getPlayer():getX();
  59.             playerY = getPlayer():getY();
  60.             playerZ = getPlayer():getZ();
  61.            
  62.             --debug - see how many zombies are nearby
  63.             --print( zlist:size() );
  64.            
  65.             for i=0, zlist:size()-1 do
  66.                
  67.                 local currentZombie = zlist:get(i);            
  68.                
  69.                 if(currentZombie:getModData() ~= nil) then
  70.                     if( currentZombie:getModData().target ~= nil ) and ( currentZombie:getModData().doNotDisturb == false ) then
  71.                    
  72.                         target = currentZombie:getModData().target;
  73.                         target = target - currentZombie:getModData().increment;
  74.                        
  75.                         if( target <= 0 ) then 
  76.                             --- calculate new position and send the zombie there
  77.                             updateZombie(currentZombie);
  78.                         else
  79.                             currentZombie:getModData().target = target;
  80.                         end
  81.                
  82.                     else
  83.                         updateZombie(currentZombie);
  84.                     end
  85.                 else
  86.                     updateZombie(currentZombie);
  87.                 end
  88.                
  89.                 -- calculate the current zombie's distance from the player
  90.                 diffX = playerX - currentZombie:getX();
  91.                 diffY = playerY - currentZombie:getY();
  92.                 distanceFromPlayer = math.sqrt( ( diffX * diffX ) + ( diffY * diffY ) );
  93.                 --print( distanceFromPlayer );
  94.                
  95.                 -- pullback (see description above)
  96.                 if( distanceFromPlayer > 90 ) then
  97.                     currentZombie:pathToLocation( playerX, playerY, playerZ );
  98.                     currentZombie:getModData().doNotDisturb = true;
  99.                     currentZombie:getModData().increment = interval;
  100.                     currentZombie:getModData().target = interval * 2;
  101.                 else
  102.                 -- reset the do not disturb flag so that the zombie can path normally
  103.                     currentZombie:getModData().doNotDisturb = false;
  104.                 end
  105.                
  106.             end
  107.         end
  108.     end
  109. end
  110.  
  111.  
  112. function NZInit()
  113.     PUCount = 0;
  114.     if (TwoHoursOfTerror ~= nil) then
  115.         TwoHoursOfTerrorUse = TwoHoursOfTerror;
  116.     else
  117.         TwoHoursOfTerrorUse = false;
  118.     end
  119. end
  120.  
  121. function NZPlayerUpdateHandle(player)
  122. PUCount = PUCount + 1;
  123.     --player:Say(tostring(TwoHoursOfTerrorUse))
  124.     if(PUCount == interval) then
  125.         --player:Say("working");
  126.         ScatterZombies();
  127.         PUCount = 0;
  128.     end
  129.    
  130. end
  131.  
  132. Events.OnGameStart.Add(NZInit);
  133. Events.OnPlayerUpdate.Add(NZPlayerUpdateHandle);
Advertisement
Add Comment
Please, Sign In to add comment