Advertisement
darkside3d1

Navigation Schedule 4.1

Oct 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Arrays - Use values in spreadsheet!
  2. var schedule = [-1,250,-1,500,-1,1000,-1,1500,-1,-1,-1,-1,2000,6000,-1,6500,-1,7000,10000,-1,-1,-1,-1,-1,10500,-1,11000,11500,12000,12500,13500,14000,14500,23500,-1];
  3.  
  4. var toX = [17,37,44,44,44,44,44,41,33,32,30,39,29,29,39,36,34,29,29,34,31,33,38,41,44,44,44,41,41,32,32,38,37,37,27];
  5.  
  6. var toY = [80,82,80,80,80,80,80,81,76,70,71,71,71,71,71,72,72,71,71,71,71,70,72,79,80,80,80,80,81,82,82,80,82,82,81];
  7.  
  8. var toZ = [133,142,143,140,145,143,144,143,157,174,187,195,197,197,195,195,197,197,197,191,187,176,161,150,140,145,143,143,139,146,146,140,142,142,132];
  9.  
  10. var rotate = [0,360,0,180,270,270,270,360,0,0,0,270,90,90,270,270,360,90,90,0,0,0,0,0,180,270,270,360,180,270,270,180,360,360,0];
  11.  
  12. var anim = [0,2,0,5,8,8,8,1,0,0,0,8,10,10,8,1,1,10,10,0,0,0,0,0,5,8,8,1,1,1,1,8,2,2,0];
  13.  
  14. //Settings - Player can adjust, if you know what you're doing...
  15.  
  16. var timesync = true; //NPC will check if the /time set command has been used and adjust their schedule accordingly.
  17.  
  18. var syncInterval = 400; //How often to check for the /time set command. Default 400 (20 seconds). No sense setting too low.
  19.  
  20. var speed = 2; //NPC's speed.
  21.  
  22. var diagnostics = true; //NPC will talk about what they are doing as they navigate. Disable this once you're satisfied the schedult is working correctly.
  23.  
  24. //Dynamic variables - Don't tamper with these, although it probably won't matter, since they'll change as required.
  25. var worldTime = 0;
  26. var lastTime = 0;
  27. var expectTime = 0;
  28. var id = 0; //The start point and the active point
  29. var end = 0;
  30. var myX = 0;
  31. var myY = 0;
  32. var myZ = 0;
  33. var isStopped = false;
  34. var isDay = true;
  35. var lastX = 0;
  36. var lastY = 0;
  37. var lastZ = 0;
  38. var startArray = 0;
  39. var endArray = 0;
  40.  
  41.  
  42. function init (event){
  43.     worldTime = (event.npc.world.getTime() % 24000);
  44.     lastTime = worldTime;
  45.    
  46. //Set some parameters
  47.     event.npc.ai.setMovingType (0);
  48.     event.npc.ai.setReturnsHome (false);
  49.     event.npc.ai.setAnimation (0);
  50.     event.npc.ai.setDoorInteract (1);
  51.     event.npc.ai.setStandingType(1);
  52.  
  53. //Boot up the schedule checker.
  54.     event.npc.timers.stop(1);
  55.     event.npc.timers.start(1,1,false);
  56.  
  57. //Boot up the /time set checker.   
  58.     if(timesync == true){
  59.         event.npc.timers.stop(0);
  60.         event.npc.timers.start(0,syncInterval,false);
  61.     }
  62. } //end init event
  63.  
  64. //Check if NPC is held up due to fighting.
  65. function target (event){
  66.     isStopped = true;
  67. }
  68.  
  69. function targetLost (event){
  70.     isStopped = false;
  71. }
  72.  
  73. function timer (event){
  74. //Timesync, if enabled. Corrects schedule if /time set command used.
  75.  
  76.     if (event.id == 0){
  77.         worldTime = (event.npc.world.getTime() % 24000);
  78.        
  79.         if (lastTime >= 24000-syncInterval){
  80.             expectTime = (lastTime - 24000 + syncInterval);
  81.         }
  82.         else{
  83.             expectTime = lastTime + syncInterval;
  84.         }
  85.         if(diagnostics==true){event.npc.say("The correct time is " + worldTime + ", and the last checked time was " + lastTime + ", and the expected time is " + expectTime + ".");}
  86.        
  87.  
  88.         if(!(expectTime <= worldTime + 1 && expectTime >= worldTime - 1)){ //Has time moved?
  89.             if(diagnostics==true){event.npc.say("What the wormhole? The time just changed!");}
  90.             event.npc.timers.stop(1);
  91.             event.npc.timers.start(1,1,false); 
  92.         }
  93.  
  94.         lastTime = worldTime;
  95.         event.npc.timers.stop(0);
  96.         event.npc.timers.start(0,syncInterval,false);
  97.     } //end timer 0 events
  98.    
  99.     //Timer to check schedule at major points
  100.  
  101.     if (event.id == 1){
  102.         worldTime = (event.npc.world.getTime() % 24000);
  103.             for(var c = schedule.length-1; c > -1; c--){
  104.                 if (schedule[c] < worldTime && schedule[c]>0){
  105.                     id= c;
  106.                     break;
  107.                 }
  108.                 if (schedule[c]>0){
  109.                     end = c;
  110.                 }
  111.             }
  112.     //Check if the NPC is working around the clock
  113.         if (end == 0){
  114.             end = startArray;
  115.         }
  116.         if (id == 0){
  117.             id = endArray;
  118.         }    
  119.         if(diagnostics==true){event.npc.say("The time is " + worldTime + ", the current scheduled time is " + schedule[id] + ", and the next scheduled time is " + schedule[end] + ".");}
  120.    
  121. //Start timer 1 proportionate to time remaining.
  122.         event.npc.timers.stop(1);
  123.         if(end>id){
  124.             event.npc.timers.start(1,schedule[end]-worldTime,false);
  125.         }
  126.         else{
  127.             event.npc.timers.start(1,24000-worldTime+schedule[end],false);
  128.         }
  129.         event.npc.timers.stop(2);
  130.         event.npc.timers.start(2,1,false);
  131.     } //End timer 1 events
  132.    
  133. //Timer 2 - For managing navigation
  134.     if (event.id == 2){
  135.         myX = event.npc.getBlockX();
  136.         myY = event.npc.getBlockY();
  137.         myZ = event.npc.getBlockZ();
  138.         if(diagnostics==true){event.npc.say("My current location is " + myX + ", " + myY + ", " + myZ + ".");}
  139.        
  140. //Does the NPC have a reason for not moving?  
  141.         if(isStopped==true){
  142.             event.npc.timers.stop(2);
  143.             event.npc.timers.start(2,40,false);
  144.         }
  145. //The NPC has no reason for standing still.
  146.         else{
  147. //Is the NPC at a marker?
  148.             if(myX == toX[id] && myY <= toY[id] + 2 && myY >= toY[id] - 2 && myZ == toZ[id]){
  149. //Is this the end point?
  150.                 if (id == end){
  151.                     event.npc.ai.setStandingType(1);
  152.                     event.npc.ai.setAnimation(anim[id]);
  153.                     event.npc.setRotation(rotate[id]);
  154.                     lastX = 0; lastY= 0 ; lastZ = 0;
  155.                     event.npc.timers.stop(2);
  156.                     event.npc.say("This is the end point.");
  157.                 }
  158. //This is not an end point.
  159.                 else{
  160. //Is the next id around the clock? (Is the next id tomorrow?)
  161.                     if(id+1>=schedule.length){
  162.                         id = 0;
  163.                     }
  164. //The next id is next in sequence.
  165.                     else{
  166.                         id = id+1;
  167.                     }
  168.                         if(diagnostics==true){event.npc.say("I'm at a point, and on to the next.");}
  169.                         event.npc.navigateTo(toX[id],toY[id],toZ[id],speed);
  170.                         lastX = myX; lastY= myY ; lastZ = myZ;
  171.                         event.npc.ai.setAnimation(anim[id]);
  172.                         event.npc.timers.stop(2);
  173.                         event.npc.timers.start(2,40,false);
  174.                     }
  175.                 }
  176. //The NPC is not at a marker.
  177.             else{
  178. //Is the NPC navigating? (Note - this is hit and miss.)
  179.                 isNav = event.npc.isNavigating();
  180.                
  181.                 if(isNav==true){
  182.                     event.npc.timers.stop(2);
  183.                     event.npc.timers.start(2,40,false);
  184.                     if(diagnostics==true){event.npc.say("I'm travelling between points.");}
  185.                 }
  186. //The NPC is not navigating.
  187.                 else{
  188. //Is the NPC stuck?
  189.                     if (myX==lastX && myY <= lastY + 1 && myY >= lastY - 1  && myZ == lastZ){
  190.                         lastX = 0;
  191.                         lastY = 0;
  192.                         lastZ = 0;
  193.                         event.npc.setPosition(toX[id],toY[id],toZ[id]);
  194.                         if(diagnostics==true){event.npc.say("I'm stuck... teleport!");}
  195.                         event.npc.timers.stop(2);
  196.                         event.npc.timers.start(2,1,false);
  197.                     }  
  198. //The NPC is not stuck.
  199.                     else{
  200.                     lastX = myX; lastY= myY ; lastZ = myZ;
  201.                     event.npc.navigateTo(toX[id],toY[id],toZ[id],speed);
  202.                     event.npc.ai.setAnimation(anim[id]);
  203.                     event.npc.timers.stop(2);
  204.                     event.npc.timers.start(2,40,false);
  205.                     if(diagnostics==true){event.npc.say("I should be navigating, but I wasn't. Now I am.");}
  206.                     }    
  207.                 }
  208.             }
  209.         }
  210.     }
  211. } //end timer events
  212.  
  213. //Current problems - Can't have a path that returns to itself without a timed point within the loop. Setting rotation whilst in certain animations (namely sleeping) doesn't work.
  214. //Need to mount NPCs to furniture properly - to do. Will probably solve above issue. MCF showers may prove difficult to mount, need to test.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement