init.lua 6.6 KB

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