init.lua 6.7 KB

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