wand.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. local MT = minetest
  2. local vMod = vm_lighting_wand
  3. local locate = vMod.locate
  4. -- Actually places the light node and logs it as a action
  5. local place_light = function(lighting_node, lighting_pos, player)
  6. local player_name = player:get_player_name()
  7. -- do cost and subtract sun_power or error
  8. if not vMod.do_power_cost(player, lighting_pos) then
  9. return
  10. end
  11. MT.set_node(lighting_pos, {name = lighting_node})
  12. vMod.play_effects(player:get_pos(), lighting_pos)
  13. vMod.logger(player_name .. " places node " .. lighting_node .. " at " .. MT.pos_to_string(lighting_pos))
  14. end
  15. -- undoes what place_lighting_node did
  16. local remove_light = function(lighting_node, lighting_pos, player)
  17. local player_name = player:get_player_name()
  18. -- do cost and subtract sun_power or error
  19. if not vMod.do_power_cost(player, lighting_pos, true) then
  20. return
  21. end
  22. MT.remove_node(lighting_pos)
  23. vMod.logger(player_name .. " removes node " .. lighting_node .. " at " .. MT.pos_to_string(lighting_pos))
  24. end
  25. -- main function called by the wand on_use()
  26. vMod.cast_wand = function(player)
  27. if not player then
  28. return
  29. end
  30. local player_name = player:get_player_name()
  31. local range = vMod.get_level(player).max_range
  32. local pointed_node = vMod.get_pointed_node(player, false, true, range)
  33. if not pointed_node then
  34. --todo: Add failed particle effect
  35. return
  36. end
  37. -- ensure pos are set (should always be set)
  38. if not pointed_node.pos_above then
  39. vMod.logger(locate("could not place light"))
  40. return
  41. end
  42. -- protection, not sure if this is needed?
  43. if MT.is_protected(pointed_node.pos_above, player_name) then
  44. MT.record_protection_violation(pointed_node.pos_above, player_name)
  45. return
  46. end
  47. -- ensure a node was found
  48. if pointed_node.node_above and not vMod.is_ignored_onlighting(pointed_node.node_above.name) then
  49. local node_light
  50. if pointed_node.node_above.drawtype == "airlike" then
  51. node_light = vMod.air_light
  52. end
  53. if pointed_node.node_above.drawtype == "liquid" then
  54. node_light = vMod.water_light
  55. end
  56. if node_light then
  57. local fn
  58. if pointed_node.node_above.name == node_light then
  59. fn = remove_light
  60. else
  61. fn = place_light
  62. end
  63. fn(node_light, pointed_node.pos_above, player)
  64. return
  65. end
  66. end
  67. end