Skip to content

Commit

Permalink
daily checkpoint #3
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxionary committed Mar 6, 2022
1 parent e9f76a2 commit 268e926
Show file tree
Hide file tree
Showing 8 changed files with 517 additions and 22 deletions.
134 changes: 134 additions & 0 deletions api/entities.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@

local pos_to_string = minetest.pos_to_string

local debug = false
local debug_cache = {}

local entities_by_pos = {}

function smartshop.api.record_entity(pos, obj)
local spos = pos_to_string(pos)
local entities = entities_by_pos[spos] or {}
table.insert(entities, obj)
entities_by_pos[spos] = entities
end

function smartshop.api.clear_entities(pos)
local spos = pos_to_string(pos)
local entities = entities_by_pos[spos] or {}
for _, obj in ipairs(entities) do
obj:remove()
end
entities_by_pos[spos] = nil
end

function smartshop.api.generate_entities(shop)

end

local function get_image_from_tile(tile)
if type(tile) == "string" then
return tile
elseif type(tile) == "table" then
local image_name
if type(tile.image) == "string" then
image_name = tile.image
elseif type(tile.name) == "string" then
image_name = tile.name
end
if image_name then
if tile.animation and tile.animation.type == "vertical_frames" and tile.animation.aspect_w and tile.animation.aspect_h then
return ("smartshop_animation_mask.png^[resize:%ix%i^[mask:"):format(tile.animation.aspect_w, tile.animation.aspect_h) .. image_name
elseif tile.animation and tile.animation.type == "sheet_2d" and tile.animation.frames_w and tile.animation.frames_h then
return image_name .. ("^[sheet:%ix%i:0,0"):format(tile.animation.frames_w, tile.animation.frames_h)
else
return image_name
end
end
end
return "unknown_node.png"
end

local function get_image_cube(tiles)
if #tiles == 6 then
return minetest.inventorycube(
get_image_from_tile(tiles[1]),
get_image_from_tile(tiles[6]),
get_image_from_tile(tiles[3])
)
elseif #tiles == 4 then
return minetest.inventorycube(
get_image_from_tile(tiles[1]),
get_image_from_tile(tiles[4]),
get_image_from_tile(tiles[3])
)
elseif #tiles == 3 then
return minetest.inventorycube(
get_image_from_tile(tiles[1]),
get_image_from_tile(tiles[3]),
get_image_from_tile(tiles[3])
)
elseif #tiles >= 1 then
return minetest.inventorycube(
get_image_from_tile(tiles[1]),
get_image_from_tile(tiles[1]),
get_image_from_tile(tiles[1])
)
end
return "unknown_node.png"
end

function smartshop.api.get_image(item)
local def = minetest.registered_items[item]
if not def then
return "unknown_node.png"
end

local image
local tiles = def.tiles or def.tile_images
local inventory_image = def.inventory_image

if inventory_image and inventory_image ~= "" then
if type(inventory_image) == "string" then
image = inventory_image

elseif type(inventory_image) == "table" and #inventory_image == 1 and type(inventory_image[1]) == "string" then
image = inventory_image[1]

else
smartshop.log("warning", "could not decode inventory image for %s", item)
image = "unknown_node.png"
end

elseif tiles then
if type(tiles) == "string" then
image = tiles

elseif type(tiles) == "table" then
if ((not def.type or def.type == "node") and
(not def.drawtype or
def.drawtype == "normal" or
def.drawtype == "allfaces" or
def.drawtype == "allfaces_optional" or
def.drawtype == "glasslike" or
def.drawtype == "glasslike_framed" or
def.drawtype == "glasslike_framed_optional" or
def.drawtype == "liquid")
) then
image = get_image_cube(tiles)
else
image = get_image_from_tile(tiles[1])
end
end
end

if (debug or not image or image == "" or image == "unknown_node.png") and not debug_cache[item] then
smartshop.log("warning", "[smartshop] definition for %s", item)
for key, value in pairs(def) do
smartshop.log("warning", "[smartshop] %q = %q", key, minetest.serialize(value))
end
debug_cache[item] = true
end

return image or "unknown_node.png"
end
41 changes: 35 additions & 6 deletions api/node_class.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,46 @@ function node_class:is_owner(player)
end
end

function node_class:set_infotext(format, ...)
self:set_string("infotext", format:format(...))
function node_class:set_infotext(infotext)
self:set_string("infotext", infotext)
end

function node_class:get_infotext()
return self.meta:get_string("infotext")
end

--------------------

function node_class:get_count(stack, match_meta)
if stack:is_empty() then
return 0
end
local inv = self.inv
local total = 0
if match_meta then
local singleton = ItemStack(stack)
singleton:set_count(1)
local singleton_string = singleton:to_string()
for i = 1, inv:get_size("main") do
local si = inv:get_stack("main", i)
local count = si:get_count()
si:set_count(1)
if singleton_string == si:to_string() then
total = total + count
end
end
else
local stack_name = stack:get_name()
for i = 1, inv:get_size("main") do
local si = inv:get_stack("main", i)
if stack_name == si:get_name() then
total = total + si:get_count()
end
end
end
return math.floor(total / stack:get_count())
end

function node_class:room_for_item(stack)
return self.inv:room_for_item("main", stack)
end
Expand Down Expand Up @@ -159,10 +191,7 @@ end
--------------------

function node_class:allow_metadata_inventory_put(listname, index, stack, player)
if not self:can_access(player) then
return 0

elseif stack:get_wear() ~= 0 then
if not self:can_access(player) or stack:get_wear() ~= 0 or not stack:is_known() then
return 0

else
Expand Down
17 changes: 8 additions & 9 deletions api/purchase_mechanics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ function api.register_purchase_mechanic(def)
end

function api.try_purchase(player, shop, n)
local player_inv = api.get_player_inv(player)

for _, def in ipairs(api.registered_purchase_mechanics) do
if def.allow_purchase(player, shop, n) then
def.do_purchase(player, shop, n)
if def.allow_purchase(player_inv, shop, n) then
def.do_purchase(player_inv, shop, n)
return true
end
end

local reason = api.get_purchase_fail_reason(player, shop, n)
local reason = api.get_purchase_fail_reason(player_inv, shop, n)
smartshop.chat_send_player(player, reason)

return false
end

function api.get_purchase_fail_reason(player, shop, n)
local player_inv = api.get_player_inv_class(player)
function api.get_purchase_fail_reason(player_inv, shop, n)
local pay_stack = shop:get_pay_stack(n)
local give_stack = shop:get_give_stack(n)

Expand All @@ -51,8 +52,7 @@ end

api.register_purchase_mechanic({
name = "smartshop:basic_purchase",
allow_purchase = function(player, shop, n)
local player_inv = player:get_inventory()
allow_purchase = function(player_inv, shop, n)
local pay_stack = shop:get_pay_stack(n)
local give_stack = shop:get_give_stack(n)

Expand All @@ -63,8 +63,7 @@ api.register_purchase_mechanic({
player_inv:room_for_item(give_stack)
)
end,
do_purchase = function(player, shop, n)
local player_inv = player:get_inventory()
do_purchase = function(player_inv, shop, n)
local pay_stack = shop:get_pay_stack(n)
local give_stack = shop:get_give_stack(n)

Expand Down
Loading

0 comments on commit 268e926

Please sign in to comment.