init.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. --
  2. -- Helper functions
  3. --
  4. boats = boats or {}
  5. local function is_water(pos)
  6. local nn = minetest.get_node(pos).name
  7. return minetest.get_item_group(nn, "water") ~= 0
  8. end
  9. local function get_sign(i)
  10. if i == 0 then
  11. return 0
  12. else
  13. return i / math.abs(i)
  14. end
  15. end
  16. local function get_velocity(v, yaw, y)
  17. local x = -math.sin(yaw) * v
  18. local z = math.cos(yaw) * v
  19. return {x = x, y = y, z = z}
  20. end
  21. local function get_v(v)
  22. return math.sqrt(v.x ^ 2 + v.z ^ 2)
  23. end
  24. --
  25. -- Boat entity
  26. --
  27. local boat = {
  28. physical = true,
  29. -- Warning: Do not change the position of the collisionbox top surface,
  30. -- lowering it causes the boat to fall through the world if underwater
  31. collisionbox = {-0.5, -0.35, -0.5, 0.5, 0.3, 0.5},
  32. visual = "mesh",
  33. mesh = "boats_boat.obj",
  34. textures = {"default_wood.png"},
  35. driver = nil,
  36. v = 0,
  37. last_v = 0,
  38. removed = false
  39. }
  40. function boat.on_rightclick(self, clicker)
  41. if not clicker or not clicker:is_player() then
  42. return
  43. end
  44. if clicker:get_hp() == 0 then
  45. return
  46. end
  47. local name = clicker:get_player_name()
  48. if self.driver and clicker == self.driver then
  49. self.driver = nil
  50. clicker:set_detach()
  51. default.player_attached[name] = false
  52. default.player_set_animation(clicker, "stand" , 30)
  53. local pos = clicker:getpos()
  54. pos = {x = pos.x, y = pos.y + 0.2, z = pos.z}
  55. minetest.after(0.1, function()
  56. clicker:set_pos(pos)
  57. end)
  58. elseif not self.driver then
  59. -- Not while attached to something else!
  60. if default.player_attached[name] then
  61. return
  62. end
  63. local attach = clicker:get_attach()
  64. if attach and attach:get_luaentity() then
  65. local luaentity = attach:get_luaentity()
  66. if luaentity.driver then
  67. luaentity.driver = nil
  68. end
  69. clicker:set_detach()
  70. end
  71. self.driver = clicker
  72. clicker:set_attach(self.object, "",
  73. {x = 0, y = 0.1, z = -3}, {x = 0, y = 0, z = 0})
  74. default.player_attached[name] = true
  75. minetest.after(0.2, function()
  76. default.player_set_animation(clicker, "sit" , 30)
  77. end)
  78. clicker:set_look_horizontal(self.object:getyaw())
  79. end
  80. end
  81. -- Make accessible externally.
  82. boats.on_rightclick = boat.on_rightclick
  83. function boat.on_activate(self, staticdata, dtime_s)
  84. self.object:set_armor_groups({immortal = 1})
  85. if staticdata then
  86. self.v = tonumber(staticdata)
  87. end
  88. self.last_v = self.v
  89. end
  90. function boat.get_staticdata(self)
  91. return tostring(self.v)
  92. end
  93. function boat.on_punch(self, puncher)
  94. if not puncher or not puncher:is_player() or self.removed then
  95. return
  96. end
  97. if self.driver and puncher == self.driver then
  98. self.driver = nil
  99. puncher:set_detach()
  100. default.player_attached[puncher:get_player_name()] = false
  101. end
  102. if not self.driver then
  103. self.removed = true
  104. local inv = puncher:get_inventory()
  105. if not minetest.setting_getbool("creative_mode")
  106. or not inv:contains_item("main", "boats:boat") then
  107. local leftover = inv:add_item("main", "boats:boat")
  108. -- if no room in inventory add a replacement boat to the world
  109. if not leftover:is_empty() then
  110. minetest.add_item(self.object:getpos(), leftover)
  111. end
  112. end
  113. -- delay remove to ensure player is detached
  114. minetest.after(0.1, function()
  115. self.object:remove()
  116. end)
  117. end
  118. end
  119. function boat.on_step(self, dtime)
  120. self.v = get_v(self.object:getvelocity()) * get_sign(self.v)
  121. if self.driver then
  122. local ctrl = self.driver:get_player_control()
  123. local yaw = self.object:getyaw()
  124. if ctrl.up then
  125. self.v = self.v + 0.1
  126. elseif ctrl.down then
  127. self.v = self.v - 0.1
  128. end
  129. if ctrl.left then
  130. if self.v < 0 then
  131. self.object:setyaw(yaw - (1 + dtime) * 0.03)
  132. else
  133. self.object:setyaw(yaw + (1 + dtime) * 0.03)
  134. end
  135. elseif ctrl.right then
  136. if self.v < 0 then
  137. self.object:setyaw(yaw + (1 + dtime) * 0.03)
  138. else
  139. self.object:setyaw(yaw - (1 + dtime) * 0.03)
  140. end
  141. end
  142. end
  143. local velo = self.object:getvelocity()
  144. if self.v == 0 and velo.x == 0 and velo.y == 0 and velo.z == 0 then
  145. self.object:set_pos(self.object:getpos())
  146. return
  147. end
  148. local s = get_sign(self.v)
  149. self.v = self.v - 0.02 * s
  150. if s ~= get_sign(self.v) then
  151. self.object:setvelocity({x = 0, y = 0, z = 0})
  152. self.v = 0
  153. return
  154. end
  155. if math.abs(self.v) > 5 then
  156. self.v = 5 * get_sign(self.v)
  157. end
  158. local p = self.object:getpos()
  159. p.y = p.y - 0.5
  160. local new_velo
  161. local new_acce = {x = 0, y = 0, z = 0}
  162. if not is_water(p) then
  163. local nodedef = minetest.reg_ns_nodes[
  164. minetest.get_node(p).name]
  165. if (not nodedef) or nodedef.walkable then
  166. self.v = 0
  167. new_acce = {x = 0, y = 1, z = 0}
  168. else
  169. new_acce = {x = 0, y = -9.8, z = 0}
  170. end
  171. new_velo = get_velocity(self.v, self.object:getyaw(),
  172. self.object:getvelocity().y)
  173. self.object:set_pos(self.object:getpos())
  174. else
  175. p.y = p.y + 1
  176. if is_water(p) then
  177. local y = self.object:getvelocity().y
  178. if y >= 5 then
  179. y = 5
  180. elseif y < 0 then
  181. new_acce = {x = 0, y = 20, z = 0}
  182. else
  183. new_acce = {x = 0, y = 5, z = 0}
  184. end
  185. new_velo = get_velocity(self.v, self.object:getyaw(), y)
  186. self.object:set_pos(self.object:getpos())
  187. else
  188. new_acce = {x = 0, y = 0, z = 0}
  189. if math.abs(self.object:getvelocity().y) < 1 then
  190. local pos = self.object:getpos()
  191. pos.y = math.floor(pos.y) + 0.5
  192. self.object:set_pos(pos)
  193. new_velo = get_velocity(self.v, self.object:getyaw(), 0)
  194. else
  195. new_velo = get_velocity(self.v, self.object:getyaw(),
  196. self.object:getvelocity().y)
  197. self.object:set_pos(self.object:getpos())
  198. end
  199. end
  200. end
  201. self.object:setvelocity(new_velo)
  202. self.object:setacceleration(new_acce)
  203. end
  204. minetest.register_entity("boats:boat", boat)
  205. minetest.register_craftitem("boats:boat", {
  206. description = "Boat",
  207. inventory_image = "boats_inventory.png",
  208. wield_image = "boats_wield.png",
  209. wield_scale = {x = 2, y = 2, z = 1},
  210. liquids_pointable = true,
  211. groups = {flammable = 2},
  212. on_place = function(itemstack, placer, pointed_thing)
  213. local under = pointed_thing.under
  214. local node = minetest.get_node(under)
  215. local udef = minetest.reg_ns_nodes[node.name]
  216. if udef and udef.on_rightclick and
  217. not (placer and placer:get_player_control().sneak) then
  218. return udef.on_rightclick(under, node, placer, itemstack,
  219. pointed_thing) or itemstack
  220. end
  221. if pointed_thing.type ~= "node" then
  222. return itemstack
  223. end
  224. if not is_water(pointed_thing.under) then
  225. return itemstack
  226. end
  227. pointed_thing.under.y = pointed_thing.under.y + 0.5
  228. local boat = minetest.add_entity(pointed_thing.under, "boats:boat")
  229. if boat then
  230. boat:setyaw(placer:get_look_horizontal())
  231. if not minetest.setting_getbool("creative_mode") then
  232. itemstack:take_item()
  233. end
  234. end
  235. return itemstack
  236. end,
  237. })
  238. minetest.register_craft({
  239. output = "boats:boat",
  240. recipe = {
  241. {"", "", "" },
  242. {"group:wood", "", "group:wood"},
  243. {"group:wood", "group:wood", "group:wood"},
  244. },
  245. })
  246. minetest.register_craft({
  247. type = "fuel",
  248. recipe = "boats:boat",
  249. burntime = 20,
  250. })