init.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. -- boats/init.lua
  2. -- Load support for MT game translation.
  3. local S = minetest.get_translator("boats")
  4. --
  5. -- Helper functions
  6. --
  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_velocity(v, yaw, y)
  12. local x = -math.sin(yaw) * v
  13. local z = math.cos(yaw) * v
  14. return {x = x, y = y, z = z}
  15. end
  16. local function get_v(v)
  17. return math.sqrt(v.x ^ 2 + v.z ^ 2)
  18. end
  19. --
  20. -- Boat entity
  21. --
  22. local boat = {
  23. initial_properties = {
  24. physical = true,
  25. -- Warning: Do not change the position of the collisionbox top surface,
  26. -- lowering it causes the boat to fall through the world if underwater
  27. collisionbox = {-0.5, -0.35, -0.5, 0.5, 0.3, 0.5},
  28. visual = "mesh",
  29. mesh = "boats_boat.obj",
  30. textures = {"default_wood.png"},
  31. },
  32. driver = nil,
  33. v = 0,
  34. last_v = 0,
  35. removed = false,
  36. auto = false
  37. }
  38. function boat.on_rightclick(self, clicker)
  39. if not clicker or not clicker:is_player() then
  40. return
  41. end
  42. local name = clicker:get_player_name()
  43. if self.driver and name == self.driver then
  44. -- Cleanup happens in boat.on_detach_child
  45. clicker:set_detach()
  46. player_api.set_animation(clicker, "stand", 30)
  47. local pos = clicker:get_pos()
  48. pos = {x = pos.x, y = pos.y + 0.2, z = pos.z}
  49. minetest.after(0.1, function()
  50. clicker:set_pos(pos)
  51. end)
  52. elseif not self.driver then
  53. clicker:set_attach(self.object, "",
  54. {x = 0.5, y = 1, z = -3}, {x = 0, y = 0, z = 0})
  55. self.driver = name
  56. player_api.player_attached[name] = true
  57. minetest.after(0.2, function()
  58. player_api.set_animation(clicker, "sit", 30)
  59. end)
  60. clicker:set_look_horizontal(self.object:get_yaw())
  61. end
  62. end
  63. -- If driver leaves server while driving boat
  64. function boat.on_detach_child(self, child)
  65. if child and child:get_player_name() == self.driver then
  66. player_api.player_attached[child:get_player_name()] = false
  67. self.driver = nil
  68. self.auto = false
  69. end
  70. end
  71. function boat.on_activate(self, staticdata, dtime_s)
  72. self.object:set_armor_groups({immortal = 1})
  73. if staticdata then
  74. self.v = tonumber(staticdata)
  75. end
  76. self.last_v = self.v
  77. end
  78. function boat.get_staticdata(self)
  79. return tostring(self.v)
  80. end
  81. function boat.on_punch(self, puncher)
  82. if not puncher or not puncher:is_player() or self.removed then
  83. return
  84. end
  85. local name = puncher:get_player_name()
  86. if self.driver and name == self.driver then
  87. self.driver = nil
  88. puncher:set_detach()
  89. player_api.player_attached[name] = false
  90. end
  91. if not self.driver then
  92. self.removed = true
  93. local inv = puncher:get_inventory()
  94. if not minetest.is_creative_enabled(name)
  95. or not inv:contains_item("main", "boats:boat") then
  96. local leftover = inv:add_item("main", "boats:boat")
  97. -- if no room in inventory add a replacement boat to the world
  98. if not leftover:is_empty() then
  99. minetest.add_item(self.object:get_pos(), leftover)
  100. end
  101. end
  102. -- delay remove to ensure player is detached
  103. minetest.after(0.1, function()
  104. self.object:remove()
  105. end)
  106. end
  107. end
  108. function boat.on_step(self, dtime)
  109. self.v = get_v(self.object:get_velocity()) * math.sign(self.v)
  110. if self.driver then
  111. local driver_objref = minetest.get_player_by_name(self.driver)
  112. if driver_objref then
  113. local ctrl = driver_objref:get_player_control()
  114. if ctrl.up and ctrl.down then
  115. if not self.auto then
  116. self.auto = true
  117. minetest.chat_send_player(self.driver, S("Boat cruise mode on"))
  118. end
  119. elseif ctrl.down then
  120. self.v = self.v - dtime * 2.0
  121. if self.auto then
  122. self.auto = false
  123. minetest.chat_send_player(self.driver, S("Boat cruise mode off"))
  124. end
  125. elseif ctrl.up or self.auto then
  126. self.v = self.v + dtime * 2.0
  127. end
  128. if ctrl.left then
  129. if self.v < -0.001 then
  130. self.object:set_yaw(self.object:get_yaw() - dtime * 0.9)
  131. else
  132. self.object:set_yaw(self.object:get_yaw() + dtime * 0.9)
  133. end
  134. elseif ctrl.right then
  135. if self.v < -0.001 then
  136. self.object:set_yaw(self.object:get_yaw() + dtime * 0.9)
  137. else
  138. self.object:set_yaw(self.object:get_yaw() - dtime * 0.9)
  139. end
  140. end
  141. end
  142. end
  143. local velo = self.object:get_velocity()
  144. if not self.driver and
  145. self.v == 0 and velo.x == 0 and velo.y == 0 and velo.z == 0 then
  146. self.object:set_pos(self.object:get_pos())
  147. return
  148. end
  149. -- We need to preserve velocity sign to properly apply drag force
  150. -- while moving backward
  151. local drag = dtime * math.sign(self.v) * (0.01 + 0.0796 * self.v * self.v)
  152. -- If drag is larger than velocity, then stop horizontal movement
  153. if math.abs(self.v) <= math.abs(drag) then
  154. self.v = 0
  155. else
  156. self.v = self.v - drag
  157. end
  158. local p = self.object:get_pos()
  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.registered_nodes[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:get_yaw(),
  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:get_yaw(), 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:get_yaw(), 0)
  193. else
  194. new_velo = get_velocity(self.v, self.object:get_yaw(),
  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 = S("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.registered_nodes[node.name]
  215. if udef and udef.on_rightclick and
  216. not (placer and placer:is_player() and
  217. 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. boat = minetest.add_entity(pointed_thing.under, "boats:boat")
  229. if boat then
  230. if placer then
  231. boat:set_yaw(placer:get_look_horizontal())
  232. end
  233. local player_name = placer and placer:get_player_name() or ""
  234. if not minetest.is_creative_enabled(player_name) then
  235. itemstack:take_item()
  236. end
  237. end
  238. return itemstack
  239. end,
  240. })
  241. minetest.register_craft({
  242. output = "boats:boat",
  243. recipe = {
  244. {"", "", "" },
  245. {"group:wood", "", "group:wood"},
  246. {"group:wood", "group:wood", "group:wood"},
  247. },
  248. })
  249. minetest.register_craft({
  250. type = "fuel",
  251. recipe = "boats:boat",
  252. burntime = 20,
  253. })