Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Arrays - Use values in spreadsheet!
- 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];
- 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];
- 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];
- 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];
- 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];
- 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];
- //Settings - Player can adjust, if you know what you're doing...
- var timesync = true; //NPC will check if the /time set command has been used and adjust their schedule accordingly.
- var syncInterval = 400; //How often to check for the /time set command. Default 400 (20 seconds). No sense setting too low.
- var speed = 2; //NPC's speed.
- 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.
- //Dynamic variables - Don't tamper with these, although it probably won't matter, since they'll change as required.
- var worldTime = 0;
- var lastTime = 0;
- var expectTime = 0;
- var id = 0; //The start point and the active point
- var end = 0;
- var myX = 0;
- var myY = 0;
- var myZ = 0;
- var isStopped = false;
- var isDay = true;
- var lastX = 0;
- var lastY = 0;
- var lastZ = 0;
- var startArray = 0;
- var endArray = 0;
- function init (event){
- worldTime = (event.npc.world.getTime() % 24000);
- lastTime = worldTime;
- //Set some parameters
- event.npc.ai.setMovingType (0);
- event.npc.ai.setReturnsHome (false);
- event.npc.ai.setAnimation (0);
- event.npc.ai.setDoorInteract (1);
- event.npc.ai.setStandingType(1);
- //Boot up the schedule checker.
- event.npc.timers.stop(1);
- event.npc.timers.start(1,1,false);
- //Boot up the /time set checker.
- if(timesync == true){
- event.npc.timers.stop(0);
- event.npc.timers.start(0,syncInterval,false);
- }
- } //end init event
- //Check if NPC is held up due to fighting.
- function target (event){
- isStopped = true;
- }
- function targetLost (event){
- isStopped = false;
- }
- function timer (event){
- //Timesync, if enabled. Corrects schedule if /time set command used.
- if (event.id == 0){
- worldTime = (event.npc.world.getTime() % 24000);
- if (lastTime >= 24000-syncInterval){
- expectTime = (lastTime - 24000 + syncInterval);
- }
- else{
- expectTime = lastTime + syncInterval;
- }
- if(diagnostics==true){event.npc.say("The correct time is " + worldTime + ", and the last checked time was " + lastTime + ", and the expected time is " + expectTime + ".");}
- if(!(expectTime <= worldTime + 1 && expectTime >= worldTime - 1)){ //Has time moved?
- if(diagnostics==true){event.npc.say("What the wormhole? The time just changed!");}
- event.npc.timers.stop(1);
- event.npc.timers.start(1,1,false);
- }
- lastTime = worldTime;
- event.npc.timers.stop(0);
- event.npc.timers.start(0,syncInterval,false);
- } //end timer 0 events
- //Timer to check schedule at major points
- if (event.id == 1){
- worldTime = (event.npc.world.getTime() % 24000);
- for(var c = schedule.length-1; c > -1; c--){
- if (schedule[c] < worldTime && schedule[c]>0){
- id= c;
- break;
- }
- if (schedule[c]>0){
- end = c;
- }
- }
- //Check if the NPC is working around the clock
- if (end == 0){
- end = startArray;
- }
- if (id == 0){
- id = endArray;
- }
- 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] + ".");}
- //Start timer 1 proportionate to time remaining.
- event.npc.timers.stop(1);
- if(end>id){
- event.npc.timers.start(1,schedule[end]-worldTime,false);
- }
- else{
- event.npc.timers.start(1,24000-worldTime+schedule[end],false);
- }
- event.npc.timers.stop(2);
- event.npc.timers.start(2,1,false);
- } //End timer 1 events
- //Timer 2 - For managing navigation
- if (event.id == 2){
- myX = event.npc.getBlockX();
- myY = event.npc.getBlockY();
- myZ = event.npc.getBlockZ();
- if(diagnostics==true){event.npc.say("My current location is " + myX + ", " + myY + ", " + myZ + ".");}
- //Does the NPC have a reason for not moving?
- if(isStopped==true){
- event.npc.timers.stop(2);
- event.npc.timers.start(2,40,false);
- }
- //The NPC has no reason for standing still.
- else{
- //Is the NPC at a marker?
- if(myX == toX[id] && myY <= toY[id] + 2 && myY >= toY[id] - 2 && myZ == toZ[id]){
- //Is this the end point?
- if (id == end){
- event.npc.ai.setStandingType(1);
- event.npc.ai.setAnimation(anim[id]);
- event.npc.setRotation(rotate[id]);
- lastX = 0; lastY= 0 ; lastZ = 0;
- event.npc.timers.stop(2);
- event.npc.say("This is the end point.");
- }
- //This is not an end point.
- else{
- //Is the next id around the clock? (Is the next id tomorrow?)
- if(id+1>=schedule.length){
- id = 0;
- }
- //The next id is next in sequence.
- else{
- id = id+1;
- }
- if(diagnostics==true){event.npc.say("I'm at a point, and on to the next.");}
- event.npc.navigateTo(toX[id],toY[id],toZ[id],speed);
- lastX = myX; lastY= myY ; lastZ = myZ;
- event.npc.ai.setAnimation(anim[id]);
- event.npc.timers.stop(2);
- event.npc.timers.start(2,40,false);
- }
- }
- //The NPC is not at a marker.
- else{
- //Is the NPC navigating? (Note - this is hit and miss.)
- isNav = event.npc.isNavigating();
- if(isNav==true){
- event.npc.timers.stop(2);
- event.npc.timers.start(2,40,false);
- if(diagnostics==true){event.npc.say("I'm travelling between points.");}
- }
- //The NPC is not navigating.
- else{
- //Is the NPC stuck?
- if (myX==lastX && myY <= lastY + 1 && myY >= lastY - 1 && myZ == lastZ){
- lastX = 0;
- lastY = 0;
- lastZ = 0;
- event.npc.setPosition(toX[id],toY[id],toZ[id]);
- if(diagnostics==true){event.npc.say("I'm stuck... teleport!");}
- event.npc.timers.stop(2);
- event.npc.timers.start(2,1,false);
- }
- //The NPC is not stuck.
- else{
- lastX = myX; lastY= myY ; lastZ = myZ;
- event.npc.navigateTo(toX[id],toY[id],toZ[id],speed);
- event.npc.ai.setAnimation(anim[id]);
- event.npc.timers.stop(2);
- event.npc.timers.start(2,40,false);
- if(diagnostics==true){event.npc.say("I should be navigating, but I wasn't. Now I am.");}
- }
- }
- }
- }
- }
- } //end timer events
- //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.
- //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