Paste #42716: Edit of P#42715 - Sleep - 0.1.7

Date: 2017/06/17 13:07:49 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


World Director:
 type: world

 events:
    #Morning - 0 ticks
   on time 6 in creative:
   - flag global "night:false"
   - foreach <server.flag[npcs]> { - run sleep_handler npc:<def[value]> }


   #Night - 13000 ticks
   on time 19 in creative:
   - flag global "night:true"
   - foreach <server.flag[npcs]> { - run sleep_handler npc:<def[value]> }




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

  default constants:
    SleepCycle: true

  actions:
    on assignment:
    - if !<global.flag[npcs].aslist> contains '<npc.id>' { - flag global "npcs:->:<npc.id>" }

    on spawn:
    - if !<global.flag[npcs].aslist> contains '<npc.id>' { - flag global "npcs:->:<npc.id>" }

    on despawn:
    - if <global.flag[npcs].aslist> contains '<npc.id>' { - flag global "npcs:<-:<npc.id>" }

    on remove:
    - if <global.flag[npcs].aslist> contains '<npc.id>' { - flag global "npcs:<-:<npc.id>" }

    on complete navigation:
    - if <npc.has_flag[sleeping]> == true { - execute as_npc "npc select <npc.id>" }
    - if <npc.has_flag[sleeping]> == true { - execute as_npc "npc sleep --anchor:bed" }


sleep_handler:
    type: task

    script:
    - if <npc.constant[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]> {
            -  walk location:<npc.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]> {
             - run 'unpause_waypoints'  
          }
      }      
    #If NPC is sleeping and it is NOT night then wakeup          
    - if <npc.flag[sleeping]> == true {
        - if !<server.flag[night]> {
            - flag npc "sleeping:true"  
            - execute as_npc  "npc wakeup"  
        }
    }
unpause_waypoints:
  type: task

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