init.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. if not minetest.global_exists("flowerpot") then flowerpot = {} end
  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}, true)
  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:dry_shrub2",
  212. "default:grass_1",
  213. "default:grass_2",
  214. "default:grass_3",
  215. "default:grass_4",
  216. "default:grass_5",
  217. -- Hanging grass variants are NOT used.
  218. "default:junglegrass",
  219. "default:coarsegrass",
  220. "default:papyrus",
  221. "farming:cotton_1",
  222. "farming:cotton_2",
  223. "farming:cotton_3",
  224. "farming:cotton_4",
  225. "farming:cotton_5",
  226. "farming:cotton_6",
  227. "farming:cotton_7",
  228. "farming:cotton_8",
  229. "farming:wheat_1",
  230. "farming:wheat_2",
  231. "farming:wheat_3",
  232. "farming:wheat_4",
  233. "farming:wheat_5",
  234. "farming:wheat_6",
  235. "farming:wheat_7",
  236. "farming:wheat_8",
  237. "flowers:dandelion_white",
  238. "flowers:dandelion_yellow",
  239. "flowers:geranium",
  240. "flowers:chrysanthemum_green",
  241. "flowers:rose",
  242. "flowers:rose_white",
  243. "flowers:zinnia_red",
  244. "flowers:tulip",
  245. "flowers:tulip_black",
  246. "flowers:viola",
  247. "flowers:desertrose_pink",
  248. "flowers:thornstalk",
  249. "flowers:desertrose_red",
  250. "flowers:foxglove_pink",
  251. "flowers:bluebell",
  252. "flowers:snapdragon",
  253. "flowers:forgetmenot",
  254. "flowers:poppy_orange",
  255. "flowers:iris_black",
  256. "flowers:daylily",
  257. "flowers:lupine_purple",
  258. "flowers:jack",
  259. "flowers:mushroom_brown",
  260. "flowers:mushroom_red",
  261. "flowers:delphinium",
  262. "flowers:thistle",
  263. "flowers:lazarus",
  264. "flowers:mannagrass",
  265. "flowers:lockspur",
  266. "nether:glowflower",
  267. "nether:grass_dried",
  268. "nether:grass_1",
  269. "nether:grass_2",
  270. "nether:grass_3",
  271. "nethervine:vine",
  272. "firetree:sapling",
  273. "jungletree:jungletree_sapling",
  274. "moretrees:apple_tree_sapling",
  275. "moretrees:beech_sapling",
  276. "moretrees:birch_sapling",
  277. "moretrees:cedar_sapling",
  278. "moretrees:date_palm_sapling",
  279. "moretrees:fir_sapling",
  280. "moretrees:jungletree_sapling",
  281. "moretrees:oak_sapling",
  282. "moretrees:palm_sapling",
  283. "moretrees:poplar_sapling",
  284. "moretrees:rubber_tree_sapling",
  285. "moretrees:sequoia_sapling",
  286. "moretrees:spruce_sapling",
  287. "moretrees:willow_sapling",
  288. "cavestuff:mycena",
  289. "cavestuff:fungus",
  290. "carrot:plant_1",
  291. "carrot:plant_2",
  292. "carrot:plant_3",
  293. "carrot:plant_4",
  294. "carrot:plant_5",
  295. "carrot:plant_6",
  296. "carrot:plant_7",
  297. "carrot:plant_8",
  298. "cucumber:cucumber_1",
  299. "cucumber:cucumber_2",
  300. "cucumber:cucumber_3",
  301. "cucumber:cucumber_4",
  302. "tomato:plant_1",
  303. "tomato:plant_2",
  304. "tomato:plant_3",
  305. "tomato:plant_4",
  306. "tomato:plant_5",
  307. "tomato:plant_6",
  308. "tomato:plant_7",
  309. "tomato:plant_8",
  310. "potatoes:potato_1",
  311. "potatoes:potato_2",
  312. "potatoes:potato_3",
  313. "potatoes:potato_4",
  314. "aloevera:aloe_plant_01",
  315. "aloevera:aloe_plant_02",
  316. "aloevera:aloe_plant_03",
  317. "aloevera:aloe_plant_04",
  318. "onions:allium_sprouts_1",
  319. "onions:allium_sprouts_2",
  320. "onions:allium_sprouts_3",
  321. "onions:allium_sprouts_4",
  322. "default:tvine_display",
  323. }) do
  324. flowerpot.register_node(node)
  325. end
  326. local c = "flowerpot:core"
  327. local f = flowerpot.modpath .. "/init.lua"
  328. reload.register_file(c, f, false)
  329. flowerpot.loaded = true
  330. end