Paste #5448: Untitled Paste

Date: 2014/06/19 13:54:30 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


######################################################################################
# USAGE:
# First define the following:
# sortList - A list of all server flags to be compared
# cord - The coordinate you want to make sure the npc goes toward (so they don't walk backward to a closest point)
# operator - Defines if cord should be getting larger or small
# location - Location of the entity in question
# Them simply inject closestUtility
#
# HOW IT WORKS:
# 1) foreach sortList it does the following
# 2) if there is no current "winning" value (defined by currentClosestNumber/Name) it defines the current value is the "winner"
# 2) if there is a current "winning" value it makes sure the distance from the new value (to the $%location%) is less than %currentClosestNumber%
# 2) if it is smaller it also checks that that %location% has a %cord% value !%operator% than the location it is going to.
# 2) if those last 2 if's are true it define %vlaue% (the current testing location) as the current "winner" (%currentClosestNumber%)
######################################################################################
closestTrigger:
# this is just a world script for trigging closest utility manually, I used it to test the script
# by having it use my player's location just to make sure sorting was correct.
# editting the defines here will allow you to test more easily.
  type: world
  events:
    on closest command:
    - define sortList li@Trainee1.loc1|Trainee1.loc2|Trainee1.loc3|Trainee1.loc4|Trainee1.loc5|Trainee1.loc6|Trainee1.loc7|Trainee1.loc8|Trainee1.loc9|Trainee1.loc10|Trainee1.loc11|Trainee1.loc12|Trainee1.loc13|Trainee1.end
    - define cord z
    - define operator LESS
    - define location <player.location>
    - inject closestUtility
trainee1Closest:
  type: task
  script:
  - define sortList li@Trainee1.loc1|Trainee1.loc2|Trainee1.loc3|Trainee1.loc4|Trainee1.loc5|Trainee1.loc6|Trainee1.loc7|Trainee1.loc8|Trainee1.loc9|Trainee1.loc10|Trainee1.loc11|Trainee1.loc12|Trainee1.loc13|Trainee1.end
  - define cord z
  - define operator LESS
  - inject closestUtility
closestUtility:
# currentClosestNumber = distance to the current "best" point to be compared to in each foreach
# currentClosestName = the name of the server flag that has the current closest distance
# as well as being the closest the flag must also be the correct direction (x,y,z) as defined by %cord%
# and must be %operator% (MORE / LESS) than the npc/player's locaton.
# currently you can only use operator and cord to find distance on one axis so all points must be going forward
# on that axis.
  type: task
  script:
  - foreach <def[sortList]> {
    - if !<def[currentClosestNumber].exists> {
      - define currentClosestNumber <server.flag[%value%].as_location.distance[%location%]>
      - define currentClosestName %value%
      } else if <server.flag[%value%].as_location.distance[%location%].is[%operator%].than[<def[currentClosestNumber]>]> {
      - if !<def[location].%cord%.is[%operator%].than[<server.flag[%value%].as_location.%cord%>]> {
        - define currentClosestNumber <server.flag[%value%].as_location.distance[%location%]>
        - define currentClosestName %value%
        }
      }
    - announce "%currentClosestName%"
    - announce "%currentClosestNumber%"
    }
  - announce "%currentClosestName%"
  - announce "%currentClosestNumber%"