enchanting.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. potions.enchantments = {
  2. simple = {
  3. {
  4. size = 2,
  5. layout = {"", "default:stick", "default:stick", "" },
  6. output = "potions:wand",
  7. }
  8. }
  9. }
  10. -- TODO: check size
  11. local function compare_recipe(a, b, bsize)
  12. for k,v in ipairs(a.layout) do
  13. if b[k] ~= v then
  14. return false
  15. end
  16. end
  17. return true
  18. end
  19. local function check_enchanting_recipe(type, layout, size)
  20. local tab = potions.enchantments[type]
  21. if not tab then
  22. return nil
  23. end
  24. for _,v in ipairs(tab) do
  25. if compare_recipe(v, layout, size) then
  26. return v.output
  27. end
  28. end
  29. return nil
  30. end
  31. local function ench_inv_op(pos, listname, index, stack, player)
  32. if listname == "output" then
  33. return
  34. end
  35. local meta = minetest.get_meta(pos)
  36. local inv = meta:get_inventory()
  37. local m = inv:get_list("main")--to_table()
  38. local l = {}
  39. for k,v in ipairs(m) do
  40. l[k] = v:get_name()
  41. end
  42. local res = check_enchanting_recipe("simple", l, 2)
  43. if res then
  44. inv:set_list("output", {res})
  45. end
  46. end
  47. minetest.register_node("potions:ench_table_wood", {
  48. description = "Wooden Enchanting Table",
  49. drawtype = "nodebox",
  50. node_box = {
  51. type = "fixed",
  52. fixed = {
  53. { -0.4, -0.5, -0.4, -0.25, 0.45, -0.25},
  54. { 0.25, -0.5, 0.25, 0.4, 0.45, 0.4},
  55. { -0.4, -0.5, 0.25, -0.25, 0.45, 0.4},
  56. { 0.25, -0.5, -0.4, 0.4, 0.45, -0.25},
  57. {-0.5, 0.35, -0.5, 0.5, 0.5, 0.5},
  58. },
  59. },
  60. tiles = {"default_junglewood.png"},
  61. paramtype = "light",
  62. paramtype2 = "facedir",
  63. groups = {choppy = 2, oddly_breakable_by_hand = 3},
  64. sounds = default.node_sound_wood_defaults(),
  65. stack_max = 1,
  66. on_construct = function(pos)
  67. local meta = minetest.get_meta(pos)
  68. local formspec = "size[8,8.5]"..
  69. "label[5,0;Wooden Enchanting Table]" ..
  70. "list[current_name;main;1,.5;2,2;]" ..
  71. "list[current_name;output;4.5,1.5;1,1;]" ..
  72. "list[current_player;main;0,4.25;8,1;]"..
  73. "list[current_player;main;0,5.5;8,3;8]"..
  74. default.gui_bg..default.gui_bg_img..default.gui_slots
  75. meta:set_string("formspec", formspec)
  76. meta:set_int("uses", 0)
  77. local inv = meta:get_inventory()
  78. inv:set_size("main", 2*2)
  79. inv:set_size("output", 1)
  80. end,
  81. after_place_node = function(pos, placer)
  82. local meta = minetest.get_meta(pos)
  83. local owner = placer and placer:get_player_name() or ""
  84. meta:set_string("owner", owner)
  85. end,
  86. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  87. if listname == "output" then
  88. return 0
  89. end
  90. return stack:get_count()
  91. end,
  92. on_metadata_inventory_put = ench_inv_op,
  93. on_metadata_inventory_move = ench_inv_op,
  94. on_metadata_inventory_take = function(pos, listname, index, stack, player)
  95. if listname == "output" then
  96. local meta = minetest.get_meta(pos)
  97. local inv = meta:get_inventory()
  98. inv:set_list("main", {})
  99. end
  100. end,
  101. -- on_receive_fields = function(pos, formname, fields, player)
  102. --
  103. -- end,
  104. })
  105. minetest.register_craft({
  106. output = 'potions:ench_table_wood',
  107. recipe = {
  108. {'dye:black', 'dye:red', 'dye:black'},
  109. {'default:junglewood', 'default:junglewood', 'default:junglewood'},
  110. {'default:junglewood', '', 'default:junglewood'},
  111. }
  112. })
  113. minetest.register_craftitem("potions:arcane_book", {
  114. description = "Arcane Book",
  115. inventory_image = "default_book.png",
  116. groups = {book = 1, flammable = 3},
  117. })
  118. minetest.register_node("potions:treasure_seed", {
  119. description = "Treasure Seed",
  120. -- tiles = {"default_mese.png"},
  121. drawtype = "airlike",
  122. groups = {crumbly = 3},
  123. drop = "default:sand",
  124. sounds = default.node_sound_dirt_defaults(),
  125. })
  126. local spawn_treasure = function(pos, node)
  127. -- print("treasure at " .. dump(pos))
  128. minetest.set_node(pos, {name="air"})
  129. pos.y = pos.y - 5
  130. for x = -1,1 do
  131. for z = -1,1 do
  132. minetest.set_node({x=pos.x+x, y=pos.y-1, z=pos.z+z}, {name="default:junglewood"})
  133. end
  134. end
  135. for y = 0,1 do
  136. for x = -1,1 do
  137. for z = -1,1 do
  138. minetest.set_node({x=pos.x+x, y=pos.y+y, z=pos.z+z}, {name="air"})
  139. end
  140. end
  141. end
  142. for x = -1,1 do
  143. for z = -1,1 do
  144. minetest.set_node({x=pos.x+x, y=pos.y+2, z=pos.z+z}, {name="default:junglewood"})
  145. end
  146. end
  147. minetest.set_node({x=pos.x+1, y=pos.y+0, z=pos.z+1}, {name="default:fence_junglewood"})
  148. minetest.set_node({x=pos.x-1, y=pos.y+0, z=pos.z+1}, {name="default:fence_junglewood"})
  149. minetest.set_node({x=pos.x+1, y=pos.y+0, z=pos.z-1}, {name="default:fence_junglewood"})
  150. minetest.set_node({x=pos.x-1, y=pos.y+0, z=pos.z-1}, {name="default:fence_junglewood"})
  151. minetest.set_node({x=pos.x+1, y=pos.y+1, z=pos.z+1}, {name="default:fence_junglewood"})
  152. minetest.set_node({x=pos.x-1, y=pos.y+1, z=pos.z+1}, {name="default:fence_junglewood"})
  153. minetest.set_node({x=pos.x+1, y=pos.y+1, z=pos.z-1}, {name="default:fence_junglewood"})
  154. minetest.set_node({x=pos.x-1, y=pos.y+1, z=pos.z-1}, {name="default:fence_junglewood"})
  155. pos.z = pos.z + 1
  156. minetest.set_node(pos, {name = "potions:small_chest"})
  157. local meta = minetest.get_meta(pos)
  158. local inv = meta:get_inventory()
  159. inv:set_list("main", {"potions:arcane_book"})
  160. end
  161. minetest.register_lbm({
  162. label = "spawn treasure",
  163. name = "potions:treasure_1",
  164. nodenames = {"potions:treasure_seed"},
  165. action = spawn_treasure,
  166. })
  167. minetest.register_abm({
  168. label = "spawn treasure",
  169. name = "potions:treasure_1",
  170. nodenames = {"potions:treasure_seed"},
  171. chance = 1,
  172. interval = 10,
  173. action = spawn_treasure,
  174. })
  175. minetest.register_decoration({
  176. name = "potions:treasure_seed",
  177. deco_type = "simple",
  178. place_on = {"default:sand"},
  179. sidelen = 16,
  180. noise_params = {
  181. offset = -0.016,
  182. scale = 0.024,
  183. spread = {x = 100, y = 100, z = 100},
  184. seed = 230,
  185. octaves = 4,
  186. persist = 0.6
  187. },
  188. flags = "force_placement",
  189. y_max = 5,
  190. y_min = -8,
  191. decoration = "potions:treasure_seed",
  192. })
  193. minetest.register_node("potions:small_chest", {
  194. description = "Small Chest",
  195. tiles = {
  196. "default_chest_top.png",
  197. "default_chest_top.png",
  198. "default_chest_side.png",
  199. "default_chest_side.png",
  200. "default_chest_lock.png",
  201. "default_chest_side.png"
  202. },
  203. paramtype = "light",
  204. paramtype2 = "facedir",
  205. is_ground_content = false,
  206. drawtype = "nodebox",
  207. node_box = {
  208. type = "fixed",
  209. fixed = {
  210. {-0.4, -0.5, -0.3, 0.4, 0.0, 0.3},
  211. },
  212. },
  213. collision_box = {
  214. type = "fixed",
  215. fixed = {
  216. {-0.4, -0.5, -0.3, 0.4, 0.0, 0.3},
  217. },
  218. },
  219. groups = {choppy = 3, oddly_breakable_by_hand = 2, divinable = 1 },
  220. sounds = default.node_sound_wood_defaults(),
  221. on_construct = function(pos)
  222. local meta = minetest.get_meta(pos)
  223. local inv = meta:get_inventory()
  224. inv:set_size("main", 3 * 2)
  225. meta:set_string("formspec",
  226. "size[8,7;]" ..
  227. "list[context;main;2,0.3;3,2;]" ..
  228. "list[current_player;main;0,2.85;8,1;]" ..
  229. "list[current_player;main;0,4.08;8,3;8]" ..
  230. "listring[context;main]" ..
  231. "listring[current_player;main]" ..
  232. default.get_hotbar_bg(0,2.85)
  233. )
  234. end,
  235. can_dig = function(pos,player)
  236. local inv = minetest.get_meta(pos):get_inventory()
  237. return inv:is_empty("main")
  238. end,
  239. })
  240. minetest.register_node("potions:divining_block", {
  241. description = "Divining Block",
  242. tiles = {"default_silver_sandstone_brick.png"},
  243. groups = {crumbly = 3},
  244. sounds = default.node_sound_dirt_defaults(),
  245. on_punch = function(pos, node, player)
  246. if potions.get_manna(player) > 50 then
  247. local p = minetest.find_node_near(pos, 50, {"group:divinable"})
  248. -- print(dump(p))
  249. if p then
  250. local dx = p.x - pos.x
  251. local dz = p.z - pos.z
  252. local l = math.sqrt(dx*dx + dz*dz)
  253. dx = dx / l
  254. dz = dz / l
  255. local vel = 4
  256. minetest.add_particlespawner({
  257. amount = 140,
  258. time = 15,
  259. minpos = pos,
  260. maxpos = pos,
  261. minvel = {x=dx*vel, y=5.5, z=dz*vel},
  262. maxvel = {x=dx*vel, y=5.5, z=dz*vel},
  263. minacc = {x=-0.1, y=-02.1, z=-0.1},
  264. maxacc = {x=0.1, y=-02.1, z=0.1},
  265. minexptime = 1.5,
  266. maxexptime = 5.5,
  267. -- collisiondetection = true,
  268. -- collision_removal = true,
  269. minsize = 0.5,
  270. maxsize = 2.5,
  271. texture = "potions_particle.png^[colorize:yellow:60",
  272. -- animation = tileanimation
  273. glow = 1
  274. })
  275. end
  276. potions.add_manna(player, -50)
  277. end
  278. end
  279. })
  280. minetest.register_node("potions:geode_divining_block", {
  281. description = "Divining Block",
  282. tiles = {"default_sandstone_brick.png"},
  283. groups = {crumbly = 3},
  284. sounds = default.node_sound_dirt_defaults(),
  285. on_punch = function(pos, node, player)
  286. if potions.get_manna(player) > 90 then
  287. local p = minetest.find_node_near(pos, 60, {"group:geode_wall"})
  288. -- print(dump(p))
  289. if p then
  290. local dx = p.x - pos.x
  291. local dz = p.z - pos.z
  292. local l = math.sqrt(dx*dx + dz*dz)
  293. dx = dx / l
  294. dz = dz / l
  295. local vel = 4
  296. minetest.add_particlespawner({
  297. amount = 140,
  298. time = 15,
  299. minpos = pos,
  300. maxpos = pos,
  301. minvel = {x=dx*vel, y=5.5, z=dz*vel},
  302. maxvel = {x=dx*vel, y=5.5, z=dz*vel},
  303. minacc = {x=-0.1, y=-02.1, z=-0.1},
  304. maxacc = {x=0.1, y=-02.1, z=0.1},
  305. minexptime = 1.5,
  306. maxexptime = 5.5,
  307. -- collisiondetection = true,
  308. -- collision_removal = true,
  309. minsize = 0.5,
  310. maxsize = 2.5,
  311. texture = "potions_particle.png^[colorize:yellow:60",
  312. -- animation = tileanimation
  313. glow = 1
  314. })
  315. end
  316. potions.add_manna(player, -90)
  317. end
  318. end
  319. })
  320. minetest.register_craft({
  321. output = 'potions:divining_block',
  322. recipe = {
  323. {'','',''},
  324. {'','group:leaves',''},
  325. {'default:silver_sandstone_block', 'default:silver_sandstone_block', 'default:silver_sandstone_block'},
  326. }
  327. })
  328. minetest.register_craft({
  329. output = 'potions:geode_divining_block',
  330. recipe = {
  331. {'','',''},
  332. {'','group:gem',''},
  333. {'default:silver_sandstone_block', 'default:silver_sandstone_block', 'default:silver_sandstone_block'},
  334. }
  335. })
  336. minetest.register_craft({
  337. output = 'potions:instant_hole',
  338. recipe = {
  339. {'default:dirt','potions:arcane_book','default:dirt'},
  340. {'', 'default:ladder', ''},
  341. {'', 'default:ladder', ''},
  342. }
  343. })
  344. minetest.register_craftitem("potions:firefly_stick", {
  345. description = "Firefly Stick",
  346. inventory_image = "default_stick.png^[colorize:yellow:200",
  347. stack_max = 5,
  348. on_use = function(itemstack, user, pointed_thing)
  349. if potions.get_manna(user) > 80 then
  350. local pos = user:getpos()
  351. pos.y = pos.y + 1
  352. local function try(p)
  353. local n = minetest.get_node(p)
  354. if n.name == "air" then
  355. minetest.set_node(p, {name="fireflies:firefly"})
  356. end
  357. end
  358. try({x=pos.x + 5, y=pos.y, z=pos.z + 0})
  359. try({x=pos.x + 5, y=pos.y, z=pos.z + 5})
  360. try({x=pos.x + 0, y=pos.y, z=pos.z + 0})
  361. try({x=pos.x + 0, y=pos.y, z=pos.z + 5})
  362. potions.add_manna(user, -80)
  363. end
  364. return itemstack
  365. end,
  366. })
  367. minetest.register_craft({
  368. output = 'potions:firefly_stick',
  369. recipe = {
  370. {'fireflies:firefly', '', ''},
  371. {'default:stick', '', ''},
  372. {'potions:arcane_book', '', ''},
  373. },
  374. replacements = {{"potions:arcane_book", "potions:arcane_book"}},
  375. })