From 409784a52ce978d7b788826161b47f80f1301ec7 Mon Sep 17 00:00:00 2001 From: Evert Date: Wed, 18 Apr 2018 12:05:29 +0300 Subject: [PATCH] elemental ring --- craftitems.lua | 4 --- crystals.lua | 53 ++++++++++++++++++++++++++++++++---- init.lua | 3 ++ register.lua | 12 ++++++++ scanner.lua | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ table.lua | 2 +- wands.lua | 10 ++----- 7 files changed, 139 insertions(+), 19 deletions(-) create mode 100644 scanner.lua diff --git a/craftitems.lua b/craftitems.lua index d981420..aaefe91 100644 --- a/craftitems.lua +++ b/craftitems.lua @@ -14,7 +14,3 @@ minetest.register_craftitem("magicalities:cap_gold", { inventory_image = "magicalities_cap_gold.png" }) -minetest.register_craftitem("magicalities:element_ring", { - description = "Elemental Ring", - inventory_image = "magicalities_element_ring.png" -}) diff --git a/crystals.lua b/crystals.lua index fa4776c..2c33aa6 100644 --- a/crystals.lua +++ b/crystals.lua @@ -1,28 +1,69 @@ -- Magicalities crystals +magicalities.crystals = {} + local randbuff = PcgRandom(os.clock()) -local function generate_crystal_buffer(pos) +local function compare(a,b) + return a[2] > b[2] +end + +local function crystal_infotext(pos, data) + local meta = minetest.get_meta(pos) + local node = minetest.get_node(pos) + local nodedef = minetest.registered_nodes[node.name] + if not data then + data = minetest.deserialize(meta:get_string("contents")) + end + + -- Sort + local sortable = {} + for name, v in pairs(data) do + sortable[#sortable + 1] = {name, v[1]} + end + table.sort(sortable, compare) + + -- Create string + local str = nodedef.description.. "\n\n" + local datastrs = {} + for _, v in pairs(sortable) do + local elemdesc = magicalities.elements[v[1]].description + datastrs[#datastrs + 1] = v[2].."x "..elemdesc + end + str = str .. table.concat( datastrs, "\n") + + meta:set_string("infotext", str) +end + +function magicalities.crystals.generate_crystal_buffer(pos) local final = {} local node = minetest.get_node(pos) local nodedef = minetest.registered_nodes[node.name] local self_cnt = randbuff:next(10, 60) + local added = 0 for name, data in pairs(magicalities.elements) do - if #final > 5 then break end + if added > 5 then break end if not data.inheritance then if name == nodedef["_element"] then final[name] = {self_cnt, self_cnt} + added = added + 1 else if randbuff:next(0, 5) == 0 then local cnt = randbuff:next(0, math.floor(self_cnt / 4)) - final[name] = {cnt, cnt} + if cnt > 0 then + final[name] = {cnt, cnt} + added = added + 1 + end end end else if randbuff:next(0, 15) == 0 then local cnt = randbuff:next(0, math.floor(self_cnt / 8)) - final[name] = {cnt, cnt} + if cnt > 0 then + final[name] = {cnt, cnt} + added = added + 1 + end end end end @@ -31,13 +72,13 @@ local function generate_crystal_buffer(pos) end local function crystal_rightclick(pos, node, clicker, itemstack, pointed_thing) - local output = generate_crystal_buffer(pos) + local output = magicalities.crystals.generate_crystal_buffer(pos) local meta = minetest.get_meta(pos) -- Add contents to the crystal local contents = minetest.deserialize(meta:get_string("contents")) if not contents then - contents = generate_crystal_buffer(pos) + contents = magicalities.crystals.generate_crystal_buffer(pos) meta:set_string("contents", minetest.serialize(contents)) end diff --git a/init.lua b/init.lua index baf5e48..013dd6f 100644 --- a/init.lua +++ b/init.lua @@ -28,5 +28,8 @@ dofile(modpath.."/table.lua") -- Items dofile(modpath.."/craftitems.lua") +-- Scanner +dofile(modpath.."/scanner.lua") + -- Register dofile(modpath.."/register.lua") diff --git a/register.lua b/register.lua index b01b6f2..eebc8cc 100644 --- a/register.lua +++ b/register.lua @@ -44,6 +44,18 @@ local recipes = { ["dark"] = 25, ["air"] = 25, } + }, + { + input = { + {"default:gold_ingot", "default:gold_ingot", "default:gold_ingot"}, + {"default:gold_ingot", "", "default:gold_ingot"}, + }, + output = "magicalities:cap_gold", + requirements = { + ["earth"] = 5, + ["light"] = 5, + ["dark"] = 5, + } } } diff --git a/scanner.lua b/scanner.lua new file mode 100644 index 0000000..2c90b9f --- /dev/null +++ b/scanner.lua @@ -0,0 +1,74 @@ +-- Scans crystals for their contents + +local fmspecelems = { + ["earth"] = {2, 0.15}, + ["water"] = {0, 1}, + ["air"] = {4, 1}, + ["fire"] = {2, 4.85}, + ["light"] = {0, 4}, + ["dark"] = {4, 4} +} + +local function create_formspec(elements, desc) + local spec = "" + local labels = "" + + if not elements then + elements = {} + end + + for name, pos in pairs(fmspecelems) do + local cp = "" + local y = -0.4 + + if not elements[name] then + cp = "^[colorize:#2f2f2f:200" + elseif elements[name] and elements[name][1] == 0 then + cp = "^[colorize:#2f2f2f:128" + end + + if pos[2] > 2.5 then + y = 0.85 + end + + spec = spec .. "image["..pos[1]..","..pos[2]..";1,1;magicalities_symbol_"..name..".png"..cp.."]" + + if elements[name] then + labels = labels .. "label["..(pos[1] + 0.3)..","..(pos[2] + y)..";"..elements[name][1].."]" + end + end + + return "size[5,6]".. + default.gui_bg.. + default.gui_bg_img.. + "label[0,0;"..desc.."]".. + "image[0,0.3;6,6;magicalities_symbol_hexagram.png]".. + spec.. + labels +end + +local function show_spec(i, placer, pointed_thing) + local pos = pointed_thing.under + local node = minetest.get_node(pos) + + if not node or minetest.get_item_group(node.name, "crystal_cluster") == 0 then + return i + end + + local meta = minetest.get_meta(pos) + local nodedef = minetest.registered_nodes[node.name] + local contents = minetest.deserialize(meta:get_string("contents")) + if not contents then + contents = magicalities.crystals.generate_crystal_buffer(pos) + end + + minetest.show_formspec(placer:get_player_name(), "magicalities:crystal_scanner", create_formspec(contents, nodedef.description)) + return i +end + +minetest.register_craftitem("magicalities:element_ring", { + description = "Elemental Ring\nShows contents of crystals", + inventory_image = "magicalities_element_ring.png", + on_place = show_spec, +}) + diff --git a/table.lua b/table.lua index 9da1d9d..987bb12 100644 --- a/table.lua +++ b/table.lua @@ -24,7 +24,7 @@ local function arcane_table_formspec(data) local y = -0.4 if not data[name] then - cp = "^[colorize:#2f2f2f" + cp = "^[colorize:#2f2f2f:200" end if pos[2] > 2.5 then diff --git a/wands.lua b/wands.lua index d86d79c..880f63c 100644 --- a/wands.lua +++ b/wands.lua @@ -142,7 +142,7 @@ local function initialize_wand(stack) for name, data in pairs(magicalities.elements) do if not data.inheritance then - data_table[name] = 10 + data_table[name] = 0 end end @@ -206,12 +206,6 @@ local function use_wand(itemstack, user, pointed_thing) magicalities.wands.update_wand_desc(itemstack) end - if not magicalities.wands.wand_has_contents(itemstack, {water = 5}) then - return itemstack - end - - --itemstack = magicalities.wands.wand_take_contents(itemstack, {water = 5}) - -- Calculate velocity local dir = user:get_look_dir() local vel = {x=0,y=0,z=0} @@ -225,7 +219,7 @@ local function use_wand(itemstack, user, pointed_thing) pos.y = pos.y + (dir.y * 2) + 1.5 pos.z = pos.z + (dir.z * 2) - for i = 1, 8 do + for i = 1, 16 do -- Deviation local relvel = {x=0,y=0,z=0} relvel.x = vel.x + (randparticles:next((-i/2.5) * 1000, (i/2.5) * 1000) / 1000)