1
0

raycast.lua 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. local function raycast_with_pointabilities(start_pos, end_pos, pointabilities)
  2. local ray = core.raycast(start_pos, end_pos, nil, nil, pointabilities)
  3. for hit in ray do
  4. if hit.type == "node" then
  5. return hit.under
  6. end
  7. end
  8. return nil
  9. end
  10. local function test_raycast_pointabilities(player, pos1)
  11. local pos2 = pos1:offset(0, 0, 1)
  12. local pos3 = pos1:offset(0, 0, 2)
  13. local oldnode1 = core.get_node(pos1)
  14. local oldnode2 = core.get_node(pos2)
  15. local oldnode3 = core.get_node(pos3)
  16. core.swap_node(pos1, {name = "air"})
  17. core.swap_node(pos2, {name = "testnodes:not_pointable"})
  18. core.swap_node(pos3, {name = "testnodes:pointable"})
  19. local p = nil
  20. assert(raycast_with_pointabilities(pos1, pos3, p) == pos3)
  21. p = core.registered_items["testtools:blocked_pointing_staff"].pointabilities
  22. assert(raycast_with_pointabilities(pos1, pos3, p) == nil)
  23. p = core.registered_items["testtools:ultimate_pointing_staff"].pointabilities
  24. assert(raycast_with_pointabilities(pos1, pos3, p) == pos2)
  25. core.swap_node(pos1, oldnode1)
  26. core.swap_node(pos2, oldnode2)
  27. core.swap_node(pos3, oldnode3)
  28. end
  29. unittests.register("test_raycast_pointabilities", test_raycast_pointabilities, {map=true})