canoe.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. local modpath = minetest.get_modpath("sailing_canoe")
  2. local float_or_fall, turn, handle_wind, move =
  3. dofile(modpath .. "/movement.lua")
  4. local toggle_sail, turn_sail =
  5. dofile(modpath .. "/sail.lua")
  6. local set_colors, dye, spawn_particles, show_wind =
  7. dofile(modpath .. "/visuals.lua")
  8. local canoe = {
  9. hp_max = 300,
  10. initial_properties =
  11. {
  12. physical = true,
  13. -- Warning: Do not change the position of the collisionbox top surface,
  14. -- lowering it causes the boat to fall through the world if underwater
  15. collisionbox = {-1.3, -0.35, -1.3, 1.3, 0.3, 1.3},
  16. visual = "mesh",
  17. mesh = "sailing_canoe_canoe.obj",
  18. textures = {"sailing_canoe_entity.png"},
  19. },
  20. driver = nil,
  21. cargo = nil,
  22. equipment = nil,
  23. removed = false,
  24. sail = nil,
  25. sail_color = nil,
  26. sail_yaw = 0,
  27. control_cooldown = 0,
  28. speed = 0,
  29. flag = nil,
  30. flag_color = nil,
  31. }
  32. local driver_attach = {x = -10.5, y = 2, z = 0}
  33. local cargo_attach = {x = -10.5, y = 4, z = -10}
  34. local function remove(self)
  35. --TODO: drop item
  36. if self.sail
  37. then
  38. self.sail:remove()
  39. end
  40. if self.flag
  41. then
  42. self.flag:remove()
  43. end
  44. self.object:remove()
  45. end
  46. local function load_cargo(self)
  47. if self.control_cooldown > 0
  48. then
  49. return
  50. end
  51. self.control_cooldown = 0.25
  52. if self.cargo
  53. then
  54. --unload
  55. self.cargo:set_detach()
  56. self.cargo = nil
  57. else
  58. local pos = self.object:get_pos()
  59. local objs = minetest.get_objects_inside_radius(pos, 6)
  60. for _, obj in ipairs(objs)
  61. do
  62. if obj ~= self.object and
  63. obj ~= self.driver and
  64. obj ~= self.flag and
  65. obj ~= self.sail and
  66. not obj:is_player()
  67. then
  68. obj:set_attach(
  69. self.object,
  70. "",
  71. cargo_attach,
  72. {x = 0, y = 0, z = 0})
  73. self.cargo = obj
  74. break
  75. end
  76. end
  77. end
  78. end
  79. local function handle_control(self)
  80. if not self.driver
  81. then
  82. return
  83. end
  84. local ctrl = self.driver:get_player_control()
  85. if ctrl.sneak
  86. then
  87. turn_sail(self, ctrl.left, ctrl.right)
  88. else
  89. turn(self, ctrl.left, ctrl.right)
  90. end
  91. if ctrl.jump
  92. then
  93. toggle_sail(self)
  94. set_colors(self)
  95. end
  96. if ctrl.aux1
  97. then
  98. load_cargo(self)
  99. end
  100. end
  101. ------------------------------------------------------------------------
  102. function canoe.on_step(self, dtime)
  103. self.control_cooldown = self.control_cooldown - dtime
  104. handle_control(self)
  105. handle_wind(self)
  106. float_or_fall(self)
  107. move(self)
  108. spawn_particles(self)
  109. show_wind(self)
  110. end
  111. function canoe.on_detach_child(self, child)
  112. if child:is_player()
  113. then
  114. self.driver = nil
  115. end
  116. end
  117. function canoe.get_staticdata(self)
  118. local data =
  119. {
  120. flag_color = self.flag_color,
  121. sail_color = self.sail_color,
  122. hp = self.object:get_hp(),
  123. }
  124. return minetest.serialize(data)
  125. end
  126. function canoe.on_activate(self, staticdata)
  127. local data = minetest.deserialize(staticdata) or {}
  128. if not data.flag_color
  129. then
  130. data.flag_color = "red"
  131. end
  132. self.flag_color = data.flag_color
  133. if not data.sail_color
  134. then
  135. data.sail_color = "white"
  136. end
  137. self.sail_color = data.sail_color
  138. if data.hp
  139. then
  140. self.object:set_hp(data.hp)
  141. end
  142. local pos = self.object:get_pos()
  143. self.flag = minetest.add_entity(pos, "sailing_canoe:flag")
  144. if self.flag
  145. then
  146. self.flag:set_attach(
  147. self.object,
  148. "",
  149. nil,
  150. {x = 0, y = self.sail_yaw, z = 0})
  151. end
  152. set_colors(self)
  153. end
  154. function canoe.on_rightclick(self, clicker)
  155. if not clicker
  156. then
  157. return
  158. end
  159. if dye(self, clicker)
  160. then
  161. return
  162. end
  163. local name = clicker:get_player_name()
  164. if self.driver == clicker
  165. then
  166. --dismount
  167. clicker:set_detach()
  168. self.driver = nil
  169. if player_api
  170. then
  171. player_api.player_attached[name] = nil
  172. end
  173. elseif not self.driver
  174. then
  175. --mount
  176. self.driver = clicker
  177. clicker:set_attach(
  178. self.object,
  179. "",
  180. driver_attach,
  181. {x = 0, y = 0, z = 0})
  182. if player_api
  183. then
  184. player_api.player_attached[name] = true
  185. minetest.after(0.1, player_api.set_animation, clicker, "sit", 30)
  186. end
  187. end
  188. end
  189. function canoe.on_punch(self, puncher, time_from_last_punch, tool_capabilities, dir, damage)
  190. local hp = self.object:get_hp() - damage
  191. if puncher then
  192. --put item into inventory or drop it
  193. local name = puncher:get_player_name()
  194. if name ~= '' then
  195. local inv = puncher:get_inventory()
  196. local stack = ItemStack("sailing_canoe:canoe")
  197. local meta = stack:get_meta()
  198. meta:set_string("flag_color", self.flag_color)
  199. meta:set_string("sail_color", self.sail_color)
  200. --check if player is in creative and already has
  201. --the same item
  202. --takes sail and flag colors into account
  203. if not (creative and creative.is_enabled_for
  204. and creative.is_enabled_for(name)
  205. and inv:contains_item("main", stack, true))
  206. then
  207. stack = inv:add_item("main", stack)
  208. if not stack:is_empty()
  209. then
  210. minetest.add_item(self.object:get_pos(), stack)
  211. end
  212. end
  213. remove(self)
  214. elseif name == '' then
  215. if hp <= 0 then
  216. local pos = self.object:get_pos()
  217. local objs = minetest.get_objects_inside_radius(pos, 3)
  218. for _, obj in pairs(objs) do
  219. if obj:is_player() then
  220. local name = obj:get_player_name()
  221. obj:set_detach()
  222. self.driver = nil
  223. if player_api
  224. then
  225. player_api.player_attached[name] = nil
  226. end
  227. end
  228. end
  229. end
  230. end
  231. end
  232. end
  233. local sail =
  234. {
  235. initial_properties =
  236. {
  237. physical = false,
  238. visual = "mesh",
  239. mesh = "sailing_canoe_sail.obj",
  240. static_save = false,
  241. }
  242. }
  243. local flag =
  244. {
  245. initial_properties =
  246. {
  247. physical = false,
  248. visual = "mesh",
  249. mesh = "sailing_canoe_flag.obj",
  250. static_save = false,
  251. visual_size = {x = 0.1, y = 0.1, z = 0.1},
  252. }
  253. }
  254. local function attachment_on_step(self)
  255. if not self.object:get_attach()
  256. then
  257. self.object:remove()
  258. end
  259. end
  260. sail.on_step = attachment_on_step
  261. flag.on_step = attachment_on_step
  262. minetest.register_entity("sailing_canoe:canoe", canoe)
  263. minetest.register_entity("sailing_canoe:sail", sail)
  264. minetest.register_entity("sailing_canoe:flag", flag)