From 40a8c548ab067c85338d54b43a44b14cac0ba89b Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Thu, 25 Nov 2021 19:17:38 +0100 Subject: [PATCH] Make correct inventory cubes --- init.lua | 1 - src/caches.lua | 39 +++++++++++++++++++++++++++++++-------- src/callbacks.lua | 4 ++-- src/common.lua | 7 ++++--- src/gui.lua | 13 ++----------- 5 files changed, 39 insertions(+), 25 deletions(-) diff --git a/init.lua b/init.lua index d290f27..9dc5055 100644 --- a/init.lua +++ b/init.lua @@ -38,7 +38,6 @@ i3 = { fuel_cache = {}, usages_cache = {}, recipes_cache = {}, - content_ids = {}, cubes = {}, tabs = {}, diff --git a/src/caches.lua b/src/caches.lua index e75c631..e1e703f 100644 --- a/src/caches.lua +++ b/src/caches.lua @@ -1,13 +1,14 @@ local replacements = {fuel = {}} local ItemStack = ItemStack +local draw_cube = core.inventorycube local fmt, reg_items, reg_aliases, reg_nodes = i3.get("fmt", "reg_items", "reg_aliases", "reg_nodes") local maxn, copy, insert, sort, match, sub = i3.get("maxn", "copy", "insert", "sort", "match", "sub") local is_group, extract_groups, item_has_groups, groups_to_items = i3.get("is_group", "extract_groups", "item_has_groups", "groups_to_items") -local true_str, is_table, valid_item, table_merge, table_replace, rcp_eq = - i3.get("true_str", "is_table", "valid_item", "table_merge", "table_replace", "rcp_eq") +local true_str, true_table, is_table, valid_item, table_merge, table_replace, rcp_eq = + i3.get("true_str", "true_table", "is_table", "valid_item", "table_merge", "table_replace", "rcp_eq") local function get_burntime(item) return core.get_craft_result{method = "fuel", items = {item}}.time @@ -286,18 +287,40 @@ local function init_recipes() end end +local function get_cube(tiles) + if not true_table(tiles) then + return "i3_blank.png" + end + + local t = copy(tiles) + local texture + + for k, v in pairs(t) do + if type(v) == "table" then + t[k] = v.name + end + end + + -- Tiles: up, down, right, left, back, front + -- Inventory cube: up, front, right + if #t <= 2 then + texture = draw_cube(t[1], t[1], t[1]) + elseif #t <= 5 then + texture = draw_cube(t[1], t[3], t[3]) + else -- Full tileset + texture = draw_cube(t[1], t[6], t[3]) + end + + return texture +end + local function init_cubes() for name, def in pairs(reg_nodes) do if def and def.drawtype == "normal" or def.drawtype == "liquid" or sub(def.drawtype, 1, 9) == "glasslike" or sub(def.drawtype, 1, 8) == "allfaces" then local id = core.get_content_id(name) - i3.content_ids[id] = name - - local tile = def.tiles[1].name or def.tiles[1] - local cube = core.inventorycube(tile, tile, tile) - - i3.cubes[name] = cube + i3.cubes[id] = get_cube(def.tiles) end end end diff --git a/src/callbacks.lua b/src/callbacks.lua index 71b845b..d4f2e15 100644 --- a/src/callbacks.lua +++ b/src/callbacks.lua @@ -5,7 +5,7 @@ local ItemStack = ItemStack local S, min, random = i3.get("S", "min", "random") local reg_items, reg_aliases = i3.get("reg_items", "reg_aliases") local fmt, find, match, sub, lower, split = i3.get("fmt", "find", "match", "sub", "lower", "split") -local vec_new, vec_eq, vec_round = i3.get("vec_new", "vec_eq", "vec_round") +local vec_eq, vec_round = i3.get("vec_eq", "vec_round") local sort, copy, insert, remove, indexof = i3.get("sort", "copy", "insert", "remove", "indexof") local msg, is_fav, pos_to_str, str_to_pos, add_hud_waypoint, play_sound, spawn_item = @@ -93,7 +93,7 @@ i3.new_tab("inventory", { remove(data.waypoints, id) elseif action == "teleport" then - local pos = vec_new(str_to_pos(waypoint.pos)) + local pos = str_to_pos(waypoint.pos) safe_teleport(player, pos) msg(name, S("Teleported to: @1", waypoint.name)) diff --git a/src/common.lua b/src/common.lua index 2a6993f..3345092 100644 --- a/src/common.lua +++ b/src/common.lua @@ -1,7 +1,7 @@ local ItemStack = ItemStack local loadstring = loadstring -local vec_add, vec_mul = vector.add, vector.multiply +local vec_new, vec_add, vec_mul = vector.new, vector.add, vector.multiply local sort, concat, insert = table.sort, table.concat, table.insert local min, floor, ceil = math.min, math.floor, math.ceil local fmt, find, match, gmatch, sub, split, lower = @@ -410,10 +410,11 @@ local function safe_teleport(player, pos) local name = player:get_player_name() play_sound(name, "i3_teleport", 0.8) - pos.y = pos.y + 0.5 + local p = vec_new(pos) + p.y = p.y + 0.25 local vel = player:get_velocity() player:add_velocity(vec_mul(vel, -1)) - player:set_pos(pos) + player:set_pos(p) end local function get_sorting_idx(name) diff --git a/src/gui.lua b/src/gui.lua index 2ca2356..356b54e 100644 --- a/src/gui.lua +++ b/src/gui.lua @@ -241,17 +241,8 @@ local function get_isometric_view(fs, pos, X, Y) local area = VoxelArea:new{MinEdge = emin, MaxEdge = emax} local data = vm:get_data() - local t = {} - data[0] = #data - - for i = 1, data[0] do - local id = data[i] - t[i] = i3.content_ids[id] - end - for idx in area:iterp(pos1, pos2) do - local name = t[idx] - local cube = i3.cubes[name] + local cube = i3.cubes[data[idx]] if cube then local p = area:position(idx) @@ -342,7 +333,7 @@ local function get_waypoint_fs(fs, data, player, yextra, ctn_len) fs("image_button", 4.65, y - 3.25, 0.25, 0.25, PNG.cancel_hover .. "^\\[brighten", "close_preview", "") - local pos = vec_new(str_to_pos(data.waypoints[i].pos)) + local pos = str_to_pos(data.waypoints[i].pos) get_isometric_view(fs, pos, 0.6, y - 2.5) fs("image", 2.7, y - 1.5, 0.3, 0.3, PNG.flag) end