init.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. --[[
  2. Copyright (C) 2015 - Auke Kok <sofar@foo-projects.org>
  3. "flowerpot" is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of
  6. the license, or (at your option) any later version.
  7. --]]
  8. flowerpot = flowerpot or {}
  9. flowerpot.modpath = minetest.get_modpath("flowerpot")
  10. -- Localize for performance.
  11. local math_random = math.random
  12. -- handle plant removal from flowerpot
  13. -- Seems to be dead code, here.
  14. --[[
  15. local function flowerpot_on_punch(pos, node, puncher, pointed_thing)
  16. if puncher and not minetest.check_player_privs(puncher, "protection_bypass") then
  17. local name = puncher:get_player_name()
  18. if minetest.is_protected(pos, name) then
  19. minetest.record_protection_violation(pos, name)
  20. return false
  21. end
  22. end
  23. local nodedef = minetest.registered_nodes[node.name]
  24. local plant = nodedef.flowerpot_plantname
  25. assert(plant, "unknown plant in flowerpot: " .. node.name)
  26. minetest.sound_play(nodedef.sounds.dug, {pos = pos})
  27. minetest.handle_node_drops(pos, {plant}, puncher)
  28. minetest.swap_node(pos, {name = "flowerpot:empty"})
  29. end
  30. --]]
  31. -- handle plant insertion into flowerpot
  32. function flowerpot.on_rightclick(pos, node, clicker, itemstack, pointed_thing)
  33. if not clicker or not clicker:is_player() then
  34. return itemstack
  35. end
  36. local pname = clicker:get_player_name()
  37. if minetest.test_protection(pos, pname) then
  38. return itemstack
  39. end
  40. local nodename = itemstack:get_name()
  41. if not nodename then
  42. return itemstack
  43. end
  44. local ndef = itemstack:get_definition()
  45. if not ndef then
  46. return itemstack
  47. end
  48. -- If item defines a table of possible alternative nodes to insert,
  49. -- then pick a random node from the table of possibilities.
  50. if type(ndef.flowerpot_insert) == "table" then
  51. if #(ndef.flowerpot_insert) > 0 then
  52. nodename = ndef.flowerpot_insert[math_random(1, #(ndef.flowerpot_insert))]
  53. end
  54. end
  55. if type(nodename) ~= "string" then
  56. return itemstack
  57. end
  58. -- Check that a potted version of this item actually exists.
  59. local name = "flowerpot:" .. nodename:gsub(":", "_")
  60. local def = minetest.registered_nodes[name]
  61. if not def then
  62. return itemstack
  63. end
  64. minetest.sound_play(def.sounds.place, {pos = pos})
  65. minetest.swap_node(pos, {name = name})
  66. itemstack:take_item()
  67. return itemstack
  68. end
  69. local function get_tile(def)
  70. local tile = def.tiles[1]
  71. if type (tile) == "table" then
  72. return tile.name
  73. end
  74. return tile
  75. end
  76. function flowerpot.register_node(nodename, imagetransform)
  77. assert(nodename, "no nodename passed")
  78. local nodedef = minetest.registered_nodes[nodename]
  79. if not nodedef then
  80. minetest.log("error", nodename .. " is not a known node, unable to register flowerpot")
  81. return false
  82. end
  83. local fx = ""
  84. if imagetransform then
  85. fx = imagetransform
  86. end
  87. local desc = nodedef.description
  88. local name = nodedef.name:gsub(":", "_")
  89. local tiles = {}
  90. if nodedef.drawtype == "plantlike" or nodedef.drawtype == "firelike" then
  91. -- X-shaped plants, or similar.
  92. tiles = {
  93. {name = "flowerpot.png"},
  94. {name = get_tile(nodedef) .. fx},
  95. {name = "doors_blank.png"},
  96. }
  97. else
  98. -- Cubic plants (like cactus).
  99. tiles = {
  100. {name = "flowerpot.png"},
  101. {name = "doors_blank.png"},
  102. {name = get_tile(nodedef) .. fx},
  103. }
  104. end
  105. -- Drop rules. Drop results of `minetest.get_node_drops` by default,
  106. -- unless `flowerpot_drop` is present in the node definition.
  107. -- If `flowerpot_drop` is present, then drop that instead (must be a string).
  108. local drops = minetest.get_node_drops(nodename, "")
  109. local dropitems = {"flowerpot:empty"}
  110. if not nodedef.flowerpot_drop then
  111. for k, v in ipairs(drops) do
  112. table.insert(dropitems, v)
  113. end
  114. elseif type(nodedef.flowerpot_drop) == "string" then
  115. table.insert(dropitems, nodedef.flowerpot_drop)
  116. end
  117. minetest.register_node(":flowerpot:" .. name, {
  118. description = "Flowerpot With " .. utility.get_short_desc(desc),
  119. drawtype = "mesh",
  120. mesh = "flowerpot.obj",
  121. tiles = tiles,
  122. paramtype = "light",
  123. paramtype2 = "facedir",
  124. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  125. sunlight_propagates = true,
  126. collision_box = {
  127. type = "fixed",
  128. fixed = {-1/4, -1/2, -1/4, 1/4, -1/8, 1/4},
  129. },
  130. selection_box = {
  131. type = "fixed",
  132. fixed = {-1/4, -1/2, -1/4, 1/4, 7/16, 1/4},
  133. },
  134. sounds = default.node_sound_defaults(),
  135. groups = {attached_node = 1, oddly_breakable_by_hand = 1, snappy = 3, not_in_creative_inventory = 1},
  136. flowerpot_plantname = nodename,
  137. -- Some flowers emit light.
  138. light_source = nodedef.light_source,
  139. drop = {
  140. max_items = #dropitems,
  141. items = {
  142. {
  143. items = dropitems,
  144. rarity = 1,
  145. },
  146. }
  147. },
  148. })
  149. end
  150. if not flowerpot.loaded then
  151. -- empty flowerpot
  152. minetest.register_node("flowerpot:empty", {
  153. description = "Flowerpot",
  154. drawtype = "mesh",
  155. paramtype2 = "facedir",
  156. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  157. mesh = "flowerpot.obj",
  158. inventory_image = "flowerpot_item.png",
  159. wield_image = "flowerpot_item.png",
  160. tiles = {
  161. {name = "flowerpot.png"},
  162. {name = "doors_blank.png"},
  163. {name = "doors_blank.png"},
  164. },
  165. paramtype = "light",
  166. sunlight_propagates = true,
  167. collision_box = {
  168. type = "fixed",
  169. fixed = {-1/4, -1/2, -1/4, 1/4, -1/8, 1/4},
  170. },
  171. selection_box = {
  172. type = "fixed",
  173. fixed = {-1/4, -1/2, -1/4, 1/4, -1/16, 1/4},
  174. },
  175. sounds = default.node_sound_defaults(),
  176. groups = {attached_node = 1, oddly_breakable_by_hand = 3, cracky = 1, dig_immediate = 3},
  177. on_rightclick = function(...) return flowerpot.on_rightclick(...) end,
  178. })
  179. -- craft
  180. minetest.register_craft({
  181. output = "flowerpot:empty",
  182. recipe = {
  183. {"default:clay_brick", "", "default:clay_brick"},
  184. {"", "default:clay_brick", ""},
  185. }
  186. })
  187. -- default farming nodes
  188. for _, node in pairs({
  189. -- These items don't exist in our version of MTG.
  190. --"default:acacia_bush_sapling",
  191. --"default:acacia_bush_stem",
  192. --"default:bush_sapling",
  193. --"default:bush_stem",
  194. "basictrees:acacia_sapling",
  195. "basictrees:aspen_sapling",
  196. "basictrees:pine_sapling",
  197. "basictrees:tree_sapling",
  198. "basictrees:jungletree_sapling",
  199. "default:cactus",
  200. "default:dry_grass_1",
  201. "default:dry_grass_2",
  202. "default:dry_grass_3",
  203. "default:dry_grass_4",
  204. "default:dry_grass_5",
  205. "default:dry_grass2_1",
  206. "default:dry_grass2_2",
  207. "default:dry_grass2_3",
  208. "default:dry_grass2_4",
  209. "default:dry_grass2_5",
  210. "default:dry_shrub",
  211. "default:grass_1",
  212. "default:grass_2",
  213. "default:grass_3",
  214. "default:grass_4",
  215. "default:grass_5",
  216. -- Hanging grass variants are NOT used.
  217. "default:junglegrass",
  218. "default:coarsegrass",
  219. "default:papyrus",
  220. "farming:cotton_1",
  221. "farming:cotton_2",
  222. "farming:cotton_3",
  223. "farming:cotton_4",
  224. "farming:cotton_5",
  225. "farming:cotton_6",
  226. "farming:cotton_7",
  227. "farming:cotton_8",
  228. "farming:wheat_1",
  229. "farming:wheat_2",
  230. "farming:wheat_3",
  231. "farming:wheat_4",
  232. "farming:wheat_5",
  233. "farming:wheat_6",
  234. "farming:wheat_7",
  235. "farming:wheat_8",
  236. "flowers:dandelion_white",
  237. "flowers:dandelion_yellow",
  238. "flowers:geranium",
  239. "flowers:chrysanthemum_green",
  240. "flowers:rose",
  241. "flowers:rose_white",
  242. "flowers:zinnia_red",
  243. "flowers:tulip",
  244. "flowers:tulip_black",
  245. "flowers:viola",
  246. -- "flowers:desertrose_pink",
  247. -- "flowers:desertrose_red",
  248. "flowers:foxglove_pink",
  249. "flowers:bluebell",
  250. "flowers:snapdragon",
  251. "flowers:forgetmenot",
  252. "flowers:poppy_orange",
  253. "flowers:iris_black",
  254. "flowers:daylily",
  255. "flowers:lupine_purple",
  256. "flowers:jack",
  257. "flowers:mushroom_brown",
  258. "flowers:mushroom_red",
  259. "flowers:delphinium",
  260. "flowers:thistle",
  261. "flowers:lazarus",
  262. "flowers:mannagrass",
  263. "flowers:lockspur",
  264. "nether:glowflower",
  265. "nether:grass_dried",
  266. "nether:grass_1",
  267. "nether:grass_2",
  268. "nether:grass_3",
  269. "nethervine:vine",
  270. "firetree:sapling",
  271. "jungletree:jungletree_sapling",
  272. "moretrees:apple_tree_sapling",
  273. "moretrees:beech_sapling",
  274. "moretrees:birch_sapling",
  275. "moretrees:cedar_sapling",
  276. "moretrees:date_palm_sapling",
  277. "moretrees:fir_sapling",
  278. "moretrees:jungletree_sapling",
  279. "moretrees:oak_sapling",
  280. "moretrees:palm_sapling",
  281. "moretrees:poplar_sapling",
  282. "moretrees:rubber_tree_sapling",
  283. "moretrees:sequoia_sapling",
  284. "moretrees:spruce_sapling",
  285. "moretrees:willow_sapling",
  286. "cavestuff:mycena",
  287. "cavestuff:fungus",
  288. "carrot:plant_1",
  289. "carrot:plant_2",
  290. "carrot:plant_3",
  291. "carrot:plant_4",
  292. "carrot:plant_5",
  293. "carrot:plant_6",
  294. "carrot:plant_7",
  295. "carrot:plant_8",
  296. "cucumber:cucumber_1",
  297. "cucumber:cucumber_2",
  298. "cucumber:cucumber_3",
  299. "cucumber:cucumber_4",
  300. "tomato:plant_1",
  301. "tomato:plant_2",
  302. "tomato:plant_3",
  303. "tomato:plant_4",
  304. "tomato:plant_5",
  305. "tomato:plant_6",
  306. "tomato:plant_7",
  307. "tomato:plant_8",
  308. "potatoes:potato_1",
  309. "potatoes:potato_2",
  310. "potatoes:potato_3",
  311. "potatoes:potato_4",
  312. "aloevera:aloe_plant_01",
  313. "aloevera:aloe_plant_02",
  314. "aloevera:aloe_plant_03",
  315. "aloevera:aloe_plant_04",
  316. "onions:allium_sprouts_1",
  317. "onions:allium_sprouts_2",
  318. "onions:allium_sprouts_3",
  319. "onions:allium_sprouts_4",
  320. }) do
  321. flowerpot.register_node(node)
  322. end
  323. local c = "flowerpot:core"
  324. local f = flowerpot.modpath .. "/init.lua"
  325. reload.register_file(c, f, false)
  326. flowerpot.loaded = true
  327. end