Advertisement
Anaristos

buildpath

Oct 5th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.83 KB | None | 0 0
  1. function buildpath(from, to, sepChar)
  2. --
  3. -- if no command separator character is specified, use default.
  4. local sep  = sepChar or zs.func.char( zs.func.pref( 'iSepChar' ) )
  5. --
  6. zs.var.fromroom = from
  7. zs.var.toroom   = to
  8. --
  9. local sw = zs.func.pathfrom( from, to )
  10. --
  11. zs.var.rawsw = sw
  12. --
  13. local C, Cc, Cf, P, V, match = lpeg.C, lpeg.Cc, lpeg.Cf, lpeg.P, lpeg.V, lpeg.match
  14. --
  15. local directions, portal, sw_e, sw_s, walk = V( 'directions' ), V( 'portal' ), V( 'sw_e' ), V( 'sw_s' ), V( 'walk' )
  16. --
  17. local run = function( sw ) if sw:len() > 1 then sw = "run "..sw end return sw end
  18. --
  19. local fold = function( walk, sw )
  20. --
  21.     if walk:len() == 0 then return sw end
  22. --
  23.     if sw:len() == 0 then return walk end
  24. --
  25.     return walk..sep..sw
  26. --
  27. end
  28. --
  29. local speedwalk = P {                                                                   -- grammar for path decoding.
  30.                       'speedwalk',                                                      -- starting symbol name.
  31.                       sw_e       = P(sep),                                              -- end-of-speedwalk   marker.
  32.                       sw_s       = P('.'),                                              -- start-of-speedwalk marker.        
  33.                       directions = C((1 - sw_e)^0),                                     -- speedwalk string capture.
  34.                       portal     = P('(' * C((1 - P(')'))^1) * ')')^0,                  -- custom exit command capture.
  35.                       walk       = sw_s * portal * (directions / run) * sw_e^-1,        -- speedwalk iteration element.
  36.                       speedwalk  = Cf(Cc("") * walk^0, fold)                            -- complete speedwalk search pattern.
  37.                     } * -1                                                              -- end-of-input marker.
  38. --
  39. return match( speedwalk, sw )
  40. --
  41. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement