From 7474e79099c8767f27d9a620c209a94cdbd92d82 Mon Sep 17 00:00:00 2001 From: Evert Date: Sat, 7 Apr 2018 21:32:41 +0300 Subject: [PATCH] do not use yet --- init.lua | 3 + items.lua | 4 + items/storage_disk.lua | 133 +++++++++++++++++++++++++++++++++ mod.conf | 2 +- network.lua | 7 -- nodes/disk_drive.lua | 93 +++++++++++++++++++++++ nodes/grid.lua | 23 ++++++ textures/storagetest_disk1.png | Bin 0 -> 513 bytes textures/storagetest_disk2.png | Bin 0 -> 546 bytes textures/storagetest_disk3.png | Bin 0 -> 535 bytes textures/storagetest_disk4.png | Bin 0 -> 543 bytes textures/storagetest_disk5.png | Bin 0 -> 518 bytes 12 files changed, 257 insertions(+), 8 deletions(-) create mode 100644 items.lua create mode 100644 items/storage_disk.lua create mode 100644 textures/storagetest_disk1.png create mode 100644 textures/storagetest_disk2.png create mode 100644 textures/storagetest_disk3.png create mode 100644 textures/storagetest_disk4.png create mode 100644 textures/storagetest_disk5.png diff --git a/init.lua b/init.lua index c6e6541..03a4872 100644 --- a/init.lua +++ b/init.lua @@ -10,5 +10,8 @@ storagetest.devices = {} -- Network dofile(modpath.."/network.lua") +-- Items +dofile(modpath.."/items.lua") + -- Nodes dofile(modpath.."/nodes.lua") diff --git a/items.lua b/items.lua new file mode 100644 index 0000000..03579eb --- /dev/null +++ b/items.lua @@ -0,0 +1,4 @@ +-- Storagetest items + +-- Drives +dofile(storagetest.modpath.."/items/storage_disk.lua") diff --git a/items/storage_disk.lua b/items/storage_disk.lua new file mode 100644 index 0000000..bd133b1 --- /dev/null +++ b/items/storage_disk.lua @@ -0,0 +1,133 @@ +-- Storage disks + +storagetest.disks = {} + +local function inv_to_table(inv) + local t = {} + for listname, list in pairs(inv:get_lists()) do + local size = inv:get_size(listname) + if size then + t[listname] = {} + for i = 1, size, 1 do + t[listname][i] = inv:get_stack(listname, i):to_table() + end + end + end + return t +end + +local function table_to_inv(inv, t) + for listname, list in pairs(t) do + for i, stack in pairs(list) do + inv:set_stack(listname, i, stack) + end + end +end + +local function save_inv_itemstack(inv, stack) + local meta = stack:get_meta() + meta:set_string("storagetest_inventory", minetest.serialize(inv_to_table(inv))) + return stack +end + +function storagetest.disks.register_disk(index, desc, capacity) + local mod = minetest.get_current_modname() + minetest.register_craftitem(mod..":storage_disk"..index, { + description = desc.."\nStores "..capacity.." Stacks", + inventory_image = "storagetest_disk"..index..".png", + groups = {storagetest_disk = 1}, + storagetest_capacity = capacity, + storagetest_name = "disk"..index, + stack_max = 1, + on_secondary_use = function (itemstack, user, pointed_thing) + local inv, stack = storagetest.disks.add_stack(itemstack, ItemStack("default:cobble 99")) + if not inv then print("full!"); return itemstack end + return stack + end + }) +end + +-- Make sure stack is disk +function storagetest.disks.is_valid_disk(stack) + local stack_name = stack:get_name() + return minetest.get_item_group(stack_name, "storagetest_disk") > 0 +end + +function storagetest.disks.get_stack_inventory(stack) + if not storagetest.disks.is_valid_disk(stack) then return nil end + local stack_name = stack:get_name() + local meta = stack:get_meta() + local name = minetest.registered_items[stack_name].storagetest_name + local capacity = minetest.registered_items[stack_name].storagetest_capacity + + local inv = minetest.create_detached_inventory(name, { + + }) + inv:set_size("main", capacity) + local invmetastring = meta:get_string("storagetest_inventory") + + if invmetastring ~= "" then + table_to_inv(inv, minetest.deserialize(invmetastring)) + save_inv_itemstack(inv, stack) + end + + return inv, stack +end + +function storagetest.disks.save_stack_inventory(inv, stack) + if not storagetest.disks.is_valid_disk(stack) then return nil end + stack = save_inv_itemstack(inv, stack) + + local meta = stack:get_meta() + local capacity = minetest.registered_items[stack:get_name()].storagetest_capacity + local desc = minetest.registered_items[stack:get_name()].description + meta:set_string("description", desc.."\nContains "..storagetest.disks.get_stack_count(nil, inv).."/"..capacity) + + return inv, stack +end + +function storagetest.disks.get_stack_count(stack, invn) + local inv = invn or storagetest.disks.get_stack_inventory(stack) + if not inv then return 0 end + + local count = 0 + for _,v in pairs(inv:get_list("main")) do + if not v:is_empty() then + count = count + 1 + end + end + + return count +end + +function storagetest.disks.add_stack(stack, item) + local inv = storagetest.disks.get_stack_inventory(stack) + if not inv then return nil end + if not inv:room_for_item("main", item) then return nil end + + inv:add_item("main", item) + + return storagetest.disks.save_stack_inventory(inv, stack) +end + +function storagetest.disks.has_stack(stack, item) + local inv = storagetest.disks.get_stack_inventory(stack) + if not inv then return nil end + return inv:contains_item("main", item, true) +end + +function storagetest.disks.take_stack(stack, item) + local inv = storagetest.disks.get_stack_inventory(stack) + if not inv then return nil end + local item = inv:remove_item("main", item) + + inv, stack = storagetest.disks.save_stack_inventory(inv, stack) + + return item, stack +end + +local capacities = {1000, 8000, 16000, 32000, 64000} +local descriptions = {"1K Disk", "8K Disk", "16K Disk", "32K Disk", "64K Disk"} +for i = 1, 5 do + storagetest.disks.register_disk(i, descriptions[i], capacities[i]) +end diff --git a/mod.conf b/mod.conf index 61883c7..1621d15 100644 --- a/mod.conf +++ b/mod.conf @@ -1,3 +1,3 @@ name = storagetest description = A solution to your hoarding addiction. -optional_depends = default +depends = default diff --git a/network.lua b/network.lua index eeadfbe..046d2df 100644 --- a/network.lua +++ b/network.lua @@ -171,13 +171,6 @@ minetest.register_chatcommand("storagectl", { end }) -local function concatinate(t1,t2) - for i=1,#t2 do - t1[#t1+1] = t2[i] - end - return t1 -end - function storagetest.network.register_abm_controller(name) minetest.register_abm({ nodenames = {name}, diff --git a/nodes/disk_drive.lua b/nodes/disk_drive.lua index 2747c93..4ff067b 100644 --- a/nodes/disk_drive.lua +++ b/nodes/disk_drive.lua @@ -1,13 +1,87 @@ -- Disk Drive +local function get_formspec() + return "size[8,8.5]".. + default.gui_bg.. + default.gui_bg_img.. + default.gui_slots.. + "label[0,0;Disk Drive]".. + "list[context;main;1,1;6,1;]".. + "list[current_player;main;0,4.25;8,1;]".. + "list[current_player;main;0,5.5;8,3;8]".. + "listring[context;main]".. + "listring[current_player;main]".. + default.get_hotbar_bg(0, 4.25) +end + +local function count_inv(inv) + local count = 0 + for _,stack in pairs(inv:get_list("main")) do + if not stack:is_empty() then + count = count + 1 + end + end + return count +end + local function timer(pos, elapsed) local refresh = false local meta = minetest.get_meta(pos) local node = minetest.get_node(pos) + local inv = meta:get_inventory() + + local count = count_inv(inv) + local cnname = minetest.registered_nodes[node.name]["_basename"] + node.name = cnname..count + storagetest.helpers.swap_node(pos, node) return refresh end +local function allow_metadata_inventory_put (pos, listname, index, stack, player) + if minetest.is_protected(pos, player:get_player_name()) then + return 0 + end + + if minetest.get_item_group(stack:get_name(), "storagetest_disk") == 0 then + return 0 + end + + return stack:get_count() +end + +local function allow_metadata_inventory_move (pos, from_list, from_index, to_list, to_index, count, player) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + local stack = inv:get_stack(from_list, from_index) + return allow_metadata_inventory_put(pos, to_list, to_index, stack, player) +end + +local function allow_metadata_inventory_take (pos, listname, index, stack, player) + if minetest.is_protected(pos, player:get_player_name()) then + return 0 + end + return stack:get_count() +end + +function storagetest.get_all_inventories(pos) + local node = minetest.get_node(pos) + if minetest.get_item_group(node.name, "storagetest_storage") == 0 then return nil end + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + + local drives = inv:get_list("meta") + local inventories = {} + for i, v in pairs(drives) do + if not v:is_empty() then + local inv1, stack = storagetest.disks.get_stack_inventory(v) + inventories[i] = {inventory = inv1, stack = stack} + end + end + + return inventories +end + local function register_disk_drive(index) local groups = { cracky = 1, @@ -29,13 +103,32 @@ local function register_disk_drive(index) "storagetest_drive_side.png", "storagetest_drive_side.png", "storagetest_drive.png"..driveoverlay, }, drop = "storagetest:disk_drive0", + _basename = "storagetest:disk_drive", paramtype2 = "facedir", on_timer = timer, groups = groups, on_construct = function (pos) storagetest.network.clear_networks(pos) + local meta = minetest.get_meta(pos) + meta:set_string("formspec", get_formspec()) + local inv = meta:get_inventory() + inv:set_size("main", 6) end, on_destruct = storagetest.network.clear_networks, + + allow_metadata_inventory_put = allow_metadata_inventory_put, + allow_metadata_inventory_take = allow_metadata_inventory_take, + allow_metadata_inventory_move = allow_metadata_inventory_move, + + on_metadata_inventory_move = function(pos) + minetest.get_node_timer(pos):start(0.02) + end, + on_metadata_inventory_put = function(pos) + minetest.get_node_timer(pos):start(0.02) + end, + on_metadata_inventory_take = function(pos) + minetest.get_node_timer(pos):start(0.02) + end, }) storagetest.devices["storagetest:disk_drive"..index] = true diff --git a/nodes/grid.lua b/nodes/grid.lua index cf61008..d3c5ba7 100644 --- a/nodes/grid.lua +++ b/nodes/grid.lua @@ -1,5 +1,22 @@ -- Storage Grid +storagetest.grid = {} + +function storagetest.grid.get_formspec(inventories, scroll_lvl, craft_inv) + return "size[8,12]".. + default.gui_bg.. + default.gui_bg_img.. + default.gui_slots.. + "label[0,0;Grid]".. + "list[context;main;0,0;1,1;]".. + "list[context;grid;0,1;7,6;]".. + "list[current_player;main;0,8;8,1;]".. + "list[current_player;main;0,9.2;8,3;8]".. + "listring[context;main]".. + "listring[current_player;main]".. + default.get_hotbar_bg(0, 8) +end + local function timer(pos, elapsed) local refresh = false local meta = minetest.get_meta(pos) @@ -23,6 +40,12 @@ minetest.register_node("storagetest:grid", { }, on_construct = function (pos) storagetest.network.clear_networks(pos) + local meta = minetest.get_meta(pos) + meta:set_string("formspec", storagetest.grid.get_formspec(nil, 1)) + + local inv = meta:get_inventory() + inv:set_size("main", 1) + inv:set_size("grid", 7*6) end, on_destruct = storagetest.network.clear_networks, storagetest_run = storagetest.helpers.grid_refresh, diff --git a/textures/storagetest_disk1.png b/textures/storagetest_disk1.png new file mode 100644 index 0000000000000000000000000000000000000000..0444d81f0da67345c711aa582f35fb4947064a69 GIT binary patch literal 513 zcmV+c0{;DpP)MCF}JXAyO}{*K5j?K-YC_w_9S2 zgb=t~E;H2KZikdI)lMO2e!e=N&u7MQ#5sp^j&U4m+m;yPjASM-3?>)}BzY~NIh%o}t_dR{z1Eh2B{r3bRgrCRtg&snnD2mTV1dvju<-KRI zSj+^35J)Mx-|wFUQc7ZsR8@si3Z+yM(^{jHqONOz89@@T*8WD)n!2vhT4RjC7=uy@ zYb{;ZA%vKXQGe&t7(-px2qAbp9*JbC`Ed$O)6g^x#u&0JqpGTOd8(Pd^ap`biv4~+ zE0$7HmSr04pOFNdbDT~m@;px-opTgLk@k6>^WWnab8D{wm9gLp00000NkvXXu0mjf D8zRv4 literal 0 HcmV?d00001 diff --git a/textures/storagetest_disk2.png b/textures/storagetest_disk2.png new file mode 100644 index 0000000000000000000000000000000000000000..8933fbde16b56a118370f438542d5ed06b91885f GIT binary patch literal 546 zcmV+-0^R+IP)`tI15FX0iaW>~ruY zYfFhR|O#iVvGRPbxmE@0BPR^cfW!V z!ozX>mL5W&oE3+U2q2;n5kv&QfrzD1BujHc^TfIx0?R`Uv_6jwLbNl9zv z!4%Fm=bUZM$;}Z*S&yo$(&2Wd{n7^tjA%}uoZg`;lnBe|34wTTItgH{<@MV)Oh)76 k(OOG6E7E#08uQ=lCsS{&@)OsuoB#j-07*qoM6N<$g4xRBcK`qY literal 0 HcmV?d00001 diff --git a/textures/storagetest_disk3.png b/textures/storagetest_disk3.png new file mode 100644 index 0000000000000000000000000000000000000000..5f6649bdfedb2a29e92c9f9604a0850281847340 GIT binary patch literal 535 zcmV+y0_gpTP)1T;@6Y zCV7~=N}nfphJtZO2u+PiLUz3f9dgqZU7Y%+9T1Gf=+`&%3;r>rOTORS#B#BqZCl!= z;e0rxZ5qBzr@ZXbIO1;BIyguwIteGm2P^&>)Xj-T5t&N-~LSZi_4@wC|xW9*Ui z1P+G-Aq0LsKH{9idryp!7$e?$y!Sk9HhAx`)}9~;ffyqIbzM`}H9$HK&V5c0LO5;L zZ}boX)3Q7sB7jmVE$=;}(WoaNA}FQU?{>!oN-1KDl&ck5YqZu$%ou~#nt4?L^azr` zU@!ncXBqRV!WcuAWn@`~)|&I-koxr%l`Ou`M@f0lSN?+ePot z9Vi*8T)6g^x0BX9or`^j0s`{U} z_ULBHyjmPyB7hTza}HBSvP4l7Bf9-?+|`>y0_PltF|U?5@A1y>1;r73zYAvq_U%$C5*DGdTKc3fTlq?}9#k1=-4tHrP` hCsQza{s-S){{RNZs|{HO$87)r002ovPDHLkV1oNO-md@v literal 0 HcmV?d00001 diff --git a/textures/storagetest_disk5.png b/textures/storagetest_disk5.png new file mode 100644 index 0000000000000000000000000000000000000000..993fb898c596185d393f13458ec5ffa2a32145a6 GIT binary patch literal 518 zcmV+h0{Q)kP)R5;76l09HTLbb%XZ<-uF0H z;(gD0ea0~C@zYNM+B9zwbj}^ybgv6NGIu_-*Tm=vVI+i+7(MOX6^4Px0yqJ9-(wc( zx*x>ovG_-V85RT40&RPRSs;e-VFt7IIClc3sbiWt0L)?ndys)ye8etEdF%50bcpi| zG%d40p4ayuK?D(H=y#_KL=-UU>H?Jv(JX!}Gax}{k z&2X-q?Gstp=uVUhNS0IATLhsWZW5DY&VOAHgxmckxBE*}b2KZctBZTZ^ItiB>C+*q za`yV&a`Gxv