From d3d29fb4979f720fc1eed793b74b18d5dd0d521f Mon Sep 17 00:00:00 2001 From: LNJ Date: Sun, 2 Apr 2017 21:29:32 +0200 Subject: [PATCH] Add basic 2x2 Drawers They're still missing some functionality as pipeworks and drops after dug. This will be done in the next commit(s). --- LICENSE.txt | 3 +- init.lua | 6 +- lua/api.lua | 56 ++++++++---- lua/helpers.lua | 82 ++++++++++++++++-- lua/visual.lua | 59 +++++++++---- ...ood_front.png => drawers_wood_front_1.png} | Bin textures/drawers_wood_front_4.png | Bin 0 -> 587 bytes 7 files changed, 158 insertions(+), 48 deletions(-) rename textures/{drawers_wood_front.png => drawers_wood_front_1.png} (100%) create mode 100644 textures/drawers_wood_front_4.png diff --git a/LICENSE.txt b/LICENSE.txt index 9d7d3f9..49e861f 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -32,7 +32,8 @@ License of media: ----------------- Copyright (C) 2014 Justin Aquadro (MIT): textures/drawers_wood.png - textures/drawers_wood_front.png + textures/drawers_wood_front_1.png + textures/drawers_wood_front_4.png Everything not listed in here: Copyright (C) 2017 LNJ (CC0 1.0) diff --git a/init.lua b/init.lua index c805099..b15f828 100755 --- a/init.lua +++ b/init.lua @@ -59,8 +59,10 @@ dofile(modpath .. "/lua/api.lua") drawers.register_drawer("drawers:wood", { description = "Wooden Drawer", - tiles = {"drawers_wood.png", "drawers_wood.png", "drawers_wood.png", - "drawers_wood.png", "drawers_wood.png", "drawers_wood_front.png"}, + tiles1 = {"drawers_wood.png", "drawers_wood.png", "drawers_wood.png", + "drawers_wood.png", "drawers_wood.png", "drawers_wood_front_1.png"}, + tiles4 = {"drawers_wood.png", "drawers_wood.png", "drawers_wood.png", + "drawers_wood.png", "drawers_wood.png", "drawers_wood_front_4.png"}, groups = {choppy = 3, oddly_breakable_by_hand = 2}, sounds = drawers.WOOD_SOUNDS, drawer_stack_max_factor = 3 * 8, -- normal chest size diff --git a/lua/api.lua b/lua/api.lua index 3789018..99b2813 100755 --- a/lua/api.lua +++ b/lua/api.lua @@ -37,33 +37,40 @@ drawers.node_box_simple = { function drawers.drawer_on_construct(pos) local node = core.get_node(pos) local ndef = core.registered_nodes[node.name] + local drawerType = ndef.groups.drawer local base_stack_max = core.nodedef_default.stack_max or 99 local stack_max_factor = ndef.drawer_stack_max_factor or 24 -- 3x8 + stack_max_factor = math.floor(stack_max_factor / drawerType) -- drawerType => number of drawers in node -- meta local meta = core.get_meta(pos) - meta:set_string("name", "") - meta:set_int("count", 0) - meta:set_int("max_count", base_stack_max * stack_max_factor) - meta:set_int("stack_max_factor", stack_max_factor) - meta:set_int("base_stack_max", base_stack_max) - meta:set_string("entity_infotext", drawers.gen_info_text("Empty", 0, - stack_max_factor, base_stack_max)) + + i = 1 + while i <= drawerType do + meta:set_string("name"..i, "") + meta:set_int("count"..i, 0) + meta:set_int("max_count"..i, base_stack_max * stack_max_factor) + meta:set_int("base_stack_max"..i, base_stack_max) + meta:set_string("entity_infotext"..i, drawers.gen_info_text("Empty", 0, + stack_max_factor, base_stack_max)) + meta:set_int("stack_max_factor"..i, stack_max_factor) + + i = i + 1 + end drawers.spawn_visual(pos) end -- destruct drawer function drawers.drawer_on_destruct(pos) - local objs = core.get_objects_inside_radius(pos, 0.5) - if objs then - for _, obj in pairs(objs) do - if obj and obj:get_luaentity() and - obj:get_luaentity().name == "drawers:visual" then - obj:remove() - return - end + local objs = core.get_objects_inside_radius(pos, 0.537) + if not objs then return end + + for _, obj in pairs(objs) do + if obj and obj:get_luaentity() and + obj:get_luaentity().name == "drawers:visual" then + obj:remove() end end end @@ -110,7 +117,6 @@ function drawers.register_drawer(name, def) def.paramtype2 = "facedir" def.legacy_facedir_simple = true def.groups = def.groups or {} - def.groups.drawer = def.groups.drawer or 1 def.drawer_stack_max_factor = def.drawer_stack_max_factor or 24 -- events @@ -134,7 +140,23 @@ function drawers.register_drawer(name, def) def.after_dig_node = pipeworks.after_dig end - core.register_node(name, def) + -- normal drawer 1x1 = 1 + def1 = table.copy(def) + def1.tiles = def.tiles or def.tiles1 + def1.tiles1 = nil + def1.tiles4 = nil + def1.groups.drawer = 1 + core.register_node(name .. "1", def1) + core.register_alias(name, name .. "1") -- 1x1 drawer is the default one + + -- 2x2 = 4 + def4 = table.copy(def) + def4.description = def4.description .. " (2x2)" + def4.tiles = def.tiles4 + def4.tiles1 = nil + def4.tiles4 = nil + def4.groups.drawer = 4 + core.register_node(name .. "4", def4) if (not def.no_craft) and def.material then core.register_craft({ diff --git a/lua/helpers.lua b/lua/helpers.lua index 3718da3..87adbae 100755 --- a/lua/helpers.lua +++ b/lua/helpers.lua @@ -68,20 +68,84 @@ end function drawers.spawn_visual(pos) local node = core.get_node(pos) + local ndef = core.registered_nodes[node.name] + local drawerType = ndef.groups.drawer -- data for the new visual drawers.last_drawer_pos = pos - drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name")) + drawers.last_drawer_type = drawerType - local bdir = core.facedir_to_dir(node.param2) - local fdir = vector.new(-bdir.x, 0, -bdir.z) - local pos2 = vector.add(pos, vector.multiply(fdir, 0.438)) + if drawerType == 1 then -- 1x1 drawer + drawers.last_visual_id = "" + drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name")) - obj = core.add_entity(pos2, "drawers:visual") + local bdir = core.facedir_to_dir(node.param2) + local fdir = vector.new(-bdir.x, 0, -bdir.z) + local pos2 = vector.add(pos, vector.multiply(fdir, 0.438)) - if bdir.x < 0 then obj:setyaw(0.5 * math.pi) end - if bdir.z < 0 then obj:setyaw(math.pi) end - if bdir.x > 0 then obj:setyaw(1.5 * math.pi) end + obj = core.add_entity(pos2, "drawers:visual") - drawers.last_texture = nil + if bdir.x < 0 then obj:setyaw(0.5 * math.pi) end + if bdir.z < 0 then obj:setyaw(math.pi) end + if bdir.x > 0 then obj:setyaw(1.5 * math.pi) end + + drawers.last_texture = nil + else -- 2x2 drawer + local bdir = core.facedir_to_dir(node.param2) + + local fdir1 + local fdir2 + local fdir3 + local fdir4 + if node.param2 == 2 then + fdir1 = vector.new(-bdir.x + 0.5, 0.5, -bdir.z) + fdir2 = vector.new(-bdir.x - 0.5, 0.5, -bdir.z) + fdir3 = vector.new(-bdir.x + 0.5, -0.5, -bdir.z) + fdir4 = vector.new(-bdir.x - 0.5, -0.5, -bdir.z) + elseif node.param2 == 0 then + fdir1 = vector.new(-bdir.x - 0.5, 0.5, -bdir.z) + fdir2 = vector.new(-bdir.x + 0.5, 0.5, -bdir.z) + fdir3 = vector.new(-bdir.x - 0.5, -0.5, -bdir.z) + fdir4 = vector.new(-bdir.x + 0.5, -0.5, -bdir.z) + elseif node.param2 == 1 then + fdir1 = vector.new(-bdir.x, 0.5, -bdir.z + 0.5) + fdir2 = vector.new(-bdir.x, 0.5, -bdir.z - 0.5) + fdir3 = vector.new(-bdir.x, -0.5, -bdir.z + 0.5) + fdir4 = vector.new(-bdir.x, -0.5, -bdir.z - 0.5) + else + fdir1 = vector.new(-bdir.x, 0.5, -bdir.z - 0.5) + fdir2 = vector.new(-bdir.x, 0.5, -bdir.z + 0.5) + fdir3 = vector.new(-bdir.x, -0.5, -bdir.z - 0.5) + fdir4 = vector.new(-bdir.x, -0.5, -bdir.z + 0.5) + end + + objs = {} + + drawers.last_visual_id = 1 + drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name1")) + local pos1 = vector.add(pos, vector.multiply(fdir1, 0.438)) + objs[1] = core.add_entity(pos1, "drawers:visual") + + drawers.last_visual_id = 2 + drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name2")) + local pos2 = vector.add(pos, vector.multiply(fdir2, 0.438)) + objs[2] = core.add_entity(pos2, "drawers:visual") + + drawers.last_visual_id = 3 + drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name3")) + local pos3 = vector.add(pos, vector.multiply(fdir3, 0.438)) + objs[3] = core.add_entity(pos3, "drawers:visual") + + drawers.last_visual_id = 4 + drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name4")) + local pos4 = vector.add(pos, vector.multiply(fdir4, 0.438)) + objs[4] = core.add_entity(pos4, "drawers:visual") + + + for i,obj in pairs(objs) do + if bdir.x < 0 then obj:setyaw(0.5 * math.pi) end + if bdir.z < 0 then obj:setyaw(math.pi) end + if bdir.x > 0 then obj:setyaw(1.5 * math.pi) end + end + end end diff --git a/lua/visual.lua b/lua/visual.lua index 11dcc12..d2a0c36 100755 --- a/lua/visual.lua +++ b/lua/visual.lua @@ -43,7 +43,9 @@ core.register_entity("drawers:visual", { drawer_posx = self.drawer_pos.x, drawer_posy = self.drawer_pos.y, drawer_posz = self.drawer_pos.z, - texture = self.texture + texture = self.texture, + drawerType = self.drawerType, + visualId = self.visualId }) end, @@ -57,9 +59,13 @@ core.register_entity("drawers:visual", { z = data.drawer_posz, } self.texture = data.texture + self.drawerType = data.drawerType or 1 + self.visualId = data.visualId or "" else self.drawer_pos = drawers.last_drawer_pos self.texture = drawers.last_texture or "drawers_empty.png" + self.visualId = drawers.last_visual_id + self.drawerType = drawers.last_drawer_type end -- add self to public drawer visuals @@ -68,7 +74,6 @@ core.register_entity("drawers:visual", { -- PLEASE contact me, if this is wrong drawers.drawer_visuals[core.serialize(self.drawer_pos)] = self - local node = core.get_node(self.drawer_pos) -- collisionbox @@ -76,23 +81,39 @@ core.register_entity("drawers:visual", { if node.param2 == 1 or node.param2 == 3 then colbox = {0, -0.4374, -0.4374, 0, 0.4374, 0.4374} end + -- only half the size if it's a small drawer + if self.drawerType >= 2 then + for i,j in pairs(colbox) do + colbox[i] = j * 0.5 + end + end + + -- visual size + local visual_size = {x = 0.6, y = 0.6} + if self.drawerType >= 2 then + core.chat_send_all("small") + visual_size = {x = 0.3, y = 0.3} + end + -- drawer values local meta = core.get_meta(self.drawer_pos) - self.count = meta:get_int("count") - self.itemName = meta:get_string("name") - self.maxCount = meta:get_int("max_count") - self.itemStackMax = meta:get_int("base_stack_max") - self.stackMaxFactor = meta:get_int("stack_max_factor") + local vid = self.visualId + self.count = meta:get_int("count"..vid) + self.itemName = meta:get_string("name"..vid) + self.maxCount = meta:get_int("max_count"..vid) + self.itemStackMax = meta:get_int("base_stack_max"..vid) + self.stackMaxFactor = meta:get_int("stack_max_factor"..vid) -- infotext - local infotext = meta:get_string("entity_infotext") .. "\n\n\n\n\n" + local infotext = meta:get_string("entity_infotext"..vid) .. "\n\n\n\n\n" self.object:set_properties({ collisionbox = colbox, infotext = infotext, - textures = {self.texture} + textures = {self.texture}, + visual_size = visual_size }) -- make entity undestroyable @@ -129,7 +150,7 @@ core.register_entity("drawers:visual", { inv:add_item("main", stack) self.count = self.count - removeCount - meta:set_int("count", self.count) + meta:set_int("count"..self.visualId, self.count) -- update infotext local itemDescription = "" @@ -139,14 +160,14 @@ core.register_entity("drawers:visual", { if self.count <= 0 then self.itemName = "" - meta:set_string("name", self.itemName) + meta:set_string("name"..self.visualId, self.itemName) self.texture = "drawers_empty.png" itemDescription = "Empty" end local infotext = drawers.gen_info_text(itemDescription, self.count, self.stackMaxFactor, self.itemStackMax) - meta:set_string("entity_infotext", infotext) + meta:set_string("entity_infotext"..self.visualId, infotext) self.object:set_properties({ infotext = infotext .. "\n\n\n\n\n", @@ -209,7 +230,7 @@ core.register_entity("drawers:visual", { end local infotext = drawers.gen_info_text(itemDescription, self.count, self.stackMaxFactor, self.itemStackMax) - meta:set_string("entity_infotext", infotext) + meta:set_string("entity_infotext"..self.visualId, infotext) -- texture self.texture = drawers.get_inv_image(self.itemName) @@ -226,11 +247,11 @@ core.register_entity("drawers:visual", { end, saveMetaData = function(self, meta) - meta:set_int("count", self.count) - meta:set_string("name", self.itemName) - meta:set_int("max_count", self.maxCount) - meta:set_int("base_stack_max", self.itemStackMax) - meta:set_int("stack_max_factor", self.stackMaxFactor) + meta:set_int("count"..self.visualId, self.count) + meta:set_string("name"..self.visualId, self.itemName) + meta:set_int("max_count"..self.visualId, self.maxCount) + meta:set_int("base_stack_max"..self.visualId, self.itemStackMax) + meta:set_int("stack_max_factor"..self.visualId, self.stackMaxFactor) end }) @@ -239,7 +260,7 @@ core.register_lbm({ nodenames = {"group:drawer"}, run_at_every_load = true, action = function(pos, node) - local objs = core.get_objects_inside_radius(pos, 0.5) + local objs = core.get_objects_inside_radius(pos, 0.537) if objs then for _, obj in pairs(objs) do if obj and obj:get_luaentity() and diff --git a/textures/drawers_wood_front.png b/textures/drawers_wood_front_1.png similarity index 100% rename from textures/drawers_wood_front.png rename to textures/drawers_wood_front_1.png diff --git a/textures/drawers_wood_front_4.png b/textures/drawers_wood_front_4.png new file mode 100644 index 0000000000000000000000000000000000000000..c479b11314faa63bde74d6a61179a79cd258a36f GIT binary patch literal 587 zcmV-R0<`^!P)WdKukZXhu?AVGC!ATls9G$1lEIx#XjGB+SCFf}kRBDVl_00007bV*G` z2i^-61tJ=#>^$-S000SaNLh0L01EH`01EH{Laa2H00004XF*Lt006O%3;baP0004^ zNkl%i_^p5aBNH-Ar|0mi}~LX#j_78O?-~`w~uz$?;iu$ z-d4+NRbSpi;0R#Xw{I}u2Rj>4tSs89%If^2s{=zdq$IFiZ`7CV&Y8a#-DmLPNc0(AAaFB%?x|?fMO1);z086UupcJio8RWUf!&6WKtx=Rx4^whElakHh z0Ch`x8mZF-)0rB2={mx2G#IONssr9xzDbkc@gH#4<*=)p9&oDBI$#DpoMT^3_`ZAg zgF!59eR-$t0;l6Y!#HorkD}F;+LZOIU33NE=m^5WlGcF~r8?3MOUz1wk~m--DJnC; zHzj8G&|ipFNgR-i0+exPnP7l)D3r$kLwnFCO6hnQ%92#&I%vI^g<186s*Rx85QNW3 Z@ei1T;>h7Nl>`6)002ovPDHLkV1i-=`5FKK literal 0 HcmV?d00001