shadowm

Untitled

Nov 29th, 2013
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.57 KB | None | 0 0
  1. ---
  2. -- Gets the relative direction of a source hex to a target hex.
  3. -- Useful to determine in which direction a unit should be facing
  4. -- (from the source) to look at another unit (at the target).
  5. --
  6. -- NOTE: This initial implementation only handles southwest and
  7. -- southeast. The C++ code handling these calculations isn't exposed
  8. -- to Lua yet, but direction.lua provides an attempt at translating
  9. -- it.
  10. --
  11. -- [store_direction]
  12. --     from_x,from_y= ...
  13. --     to_x,to_y= ...
  14. --     variable="direction"
  15. -- [/store_direction]
  16. --
  17. -- Or:
  18. --
  19. -- [store_direction]
  20. --     [from]
  21. --         ... SLF ...
  22. --     [/from]
  23. --     [to]
  24. --         ... SLF ...
  25. --     [/to]
  26. --     variable="direction"
  27. -- [/store_direction]
  28. ---
  29. function wesnoth.wml_actions.store_direction(cfg)
  30.     local from_slf = helper.get_child(cfg, "from")
  31.     local to_slf = helper.get_child(cfg, "to")
  32.  
  33.     local a = { x = cfg.from_x, y = cfg.from_y }
  34.     local b = { x = cfg.to_x  , y = cfg.to_y   }
  35.  
  36.     if from_slf then
  37.         a.x = wesnoth.get_locations(from_slf)[1][1]
  38.         a.y = wesnoth.get_locations(from_slf)[1][2]
  39.     end
  40.     if to_slf then
  41.         b.x = wesnoth.get_locations(to_slf)[1][1]
  42.         b.y = wesnoth.get_locations(to_slf)[1][2]
  43.     end
  44.  
  45.     if not a.x or not a.y or not b.x or not b.y then
  46.         helper.wml_error "[store_direction] missing coordinate!"
  47.     end
  48.  
  49.     local variable = cfg.variable or "direction"
  50.  
  51.     -- local facing = loc_relative_direction(b, a) or "sw"
  52.     -- wesnoth.set_variable(variable, facing)
  53.  
  54.     if a.x < b.x then
  55.         wesnoth.set_variable(variable, "se")
  56.     else
  57.         wesnoth.set_variable(variable, "sw")
  58.     end
  59. end
Advertisement
Add Comment
Please, Sign In to add comment