Paste #42710: Edit of P#42709 - Sleep

Date: 2017/06/17 11:09:27 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


World Director:
 type: world

 events:
    #Morning
   on 6:00 in world:
   - flag global night:!
   - runtask 'loop through' "context:sleep_loop|npcs|global"

   #Night
   on 19:00 in world:
   - flag global night:true
   - runtask 'loop through' "context:sleep_loop|npcs|server"

sleep_loop:
    type: task
    context: npc

    script:
    - ^run <entity:n@<npc.id>> sleep_handler delay 1s


sleep_handler:
    type: task

    script:
    - if <cons:SleepCycle> == "false" 
          queue clear

    #If it is night time AND NPC is NOT sleeping, pause their normal path 
    - if <server.flag[night]> == true 
         if !<npc.flag[sleeping]> 
             pause waypoints

    #If it is night time AND NPC is NOT sleeping, walk to their bed     
    - if <server.flag[night]> == true 
         if !<npc.flag[sleeping]> 
              walkto location:<anchor:bed>

    #If it is night time AND NPC is NOT sleeping, set npc to sleeping      
    - if <server.flag[night]> == true 
        - if !<npc.flag[sleeping]> 
              flag <npc> sleeping:true

    #If NPC is sleeping and it is NOT night then resume normal path          
    - if <npc.flag[sleeping]> == true 
        - if !<server.flag[night]> 
              runtask 'unpause_waypoints' queue:unpause_queue

    #If NPC is sleeping and it is NOT night then wakeup          
    - if <npc.flag[sleeping]> == true {
        - if !<server.flag[night]> {
            - flag <npc> sleeping:!
            - execute as_npc  "npc wakeup"
        }
    }
unpause_waypoints:
  type: task

  script:
  - pause duration:1 waypoints
  - wait duration:1    


 #Each NPC is assigned sleep to activate the sleeping mechanism
"Sleep":
  type: assignment

  default constants:
    SleepCycle: true

  actions:
    on assignment:
    - if !<flag.g:npcs.aslist> contains '<npc.id>' flag global "npcs:->:<npc.id>"

    on spawn:
    - if !<flag.g:npcs.aslist> contains '<npc.id>' flag global "npcs:->:<npc.id>"

    on despawn:
    - if <flag.g:npcs.aslist> contains '<npc.id>' flag global "npcs:<-:<npc.id>"

    on remove:
    - if <flag.g:npcs.aslist> contains '<npc.id>' flag global "npcs:<-:<npc.id>"

    on complete navigation:
    - if <flag.n:sleeping> == true execute as_npc "npc select <npc.id>"
    - if <flag.n:sleeping> == true execute as_npc "npc sleep --anchor:bed"