Skip to content

Commit

Permalink
Effects Int: Potions: crafts in book paged by groups, ordered by power.
Browse files Browse the repository at this point in the history
Closes #1816
  • Loading branch information
alek13 committed Dec 11, 2024
1 parent 1192917 commit 10f9f5a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
42 changes: 32 additions & 10 deletions mods/lord/Tools/lord_books/potions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ end
--- @param item_name string
--- @param item_definition ItemDefinition
--- @param i number
function Form.recipe(item_name, item_definition, i)
local dy = i % Form.RECIPES_PER_PAGE
function Form.recipe(item_name, item_definition, i, is_ingredients_page)
local dy = is_ingredients_page
and i % Form.RECIPES_PER_PAGE
or tonumber(item_name:sub(-1)) - 1 -- name of potion has `_1`, `_2`, `_3` postfix at the end

local recipe = minetest.get_craft_recipe(item_name, minetest.CraftMethod.POTION, minetest.CraftType.COOKING)

Expand All @@ -97,30 +99,50 @@ function Form.recipe(item_name, item_definition, i)
.. spec.item_image_button(5, 2 + dy, 1, 1, recipe.input[1][2], recipe.input[1][2], '')
.. spec.image (6, 2 + dy, 1, 1, 'benches_laboratory.png')
.. spec.item_image_button(7, 2 + dy, 1, 1, item_name, item_name, '')
end

--- @param list table<string,NodeDefinition>
--- @param callback fun(item_name:string,item_definition:ItemDefinition):boolean return `true` for stop iteration
local function foreach_ingredient_in(list, callback)
for item_name, item_definition in pairs(list) do
if callback(item_name, item_definition) then break end
end
end

--- @param list table<string,table<string,NodeDefinition>>
--- @param callback fun(item_name:string,item_definition:ItemDefinition):boolean return `true` for stop iteration
local function foreach_potion_in(list, callback)
for group_name, group_list in pairs(list) do
for item_name, item_definition in pairs(group_list) do
if callback(item_name, item_definition) then break end
end
end
end

--- @private
--- @param page number
--- @return string
function Form.list_page(page)
local list = Form.is_ingredients_page(page)
local is_ingredients_page = Form.is_ingredients_page(page)
local list = is_ingredients_page
and potions.ingredient.get_all()
or potions.potion.get_all()
or potions.potion.get_all_grouped()

local page_list = Form.is_ingredients_page(page) and page or page - Form.pages_of.ingredients
local items_to_skip = (page_list - 1) * Form.RECIPES_PER_PAGE
local list_page = is_ingredients_page and page or page - Form.pages_of.ingredients
local items_to_skip = (list_page - 1) * Form.RECIPES_PER_PAGE
local i = 1
local form_spec = ''
for item_name, item_definition in pairs(list) do

local foreach_in = is_ingredients_page and foreach_ingredient_in or foreach_potion_in
foreach_in(list, function(item_name, item_definition)
if i > items_to_skip + Form.RECIPES_PER_PAGE then
break
return true
end
if i > items_to_skip then
form_spec = form_spec .. Form.recipe(item_name, item_definition, i)
form_spec = form_spec .. Form.recipe(item_name, item_definition, i, is_ingredients_page)
end
i = i + 1
end
end)

return form_spec
end
Expand Down
31 changes: 21 additions & 10 deletions mods/lord/Tools/lord_potions/src/lord_potions/api/potion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ local colorize = minetest.colorize

local potions = {
--- @type table<string,NodeDefinition>|NodeDefinition[]
all_items = {},
all_items = {},
--- @type table<string,table<string,NodeDefinition>>|NodeDefinition[][]
all_items_grouped = {},
--- @type table<string,NodeDefinition>|NodeDefinition[]
lord_items = {},
lord_items = {},
--- @type table<string,table<string,NodeDefinition>>|NodeDefinition[][]
lord_items_grouped = {},
}

--- @param node_name string technical node name ("<mod>:<node>").
Expand Down Expand Up @@ -43,7 +47,7 @@ local function register_potion_node(item_name, title, description, color, effect
'lord_potions_bottle_content_mask.png' ..
'^[colorize:' .. color .. ':170' ..
'^[colorize:#000:' .. content_opacity_by_level ..
')'
')'
local texture = bottle_contents_img .. '^(lord_potions_bottle.png)'

minetest.register_node(item_name, {
Expand Down Expand Up @@ -105,8 +109,13 @@ local function register_potion(name_prefix, title, description, color, effect, l

register_potion_node(item_name, title, description, color, effect, level, groups)

potions.all_items[item_name] = minetest.registered_nodes[item_name]
potions.lord_items[item_name] = minetest.registered_nodes[item_name]
potions.all_items_grouped [effect.group] = potions.all_items_grouped [effect.group] or {}
potions.lord_items_grouped[effect.group] = potions.lord_items_grouped[effect.group] or {}

potions.all_items [item_name] = minetest.registered_nodes[item_name]
potions.lord_items[item_name] = minetest.registered_nodes[item_name]
potions.all_items_grouped [effect.group][item_name] = minetest.registered_nodes[item_name]
potions.lord_items_grouped[effect.group][item_name] = minetest.registered_nodes[item_name]

register_potion_craft(item_name, recipe)
end
Expand Down Expand Up @@ -157,10 +166,12 @@ end


return {
add_existing = add_existing,
register = register_potion,
register_group = register_potion_group,
get_all = function() return potions.all_items end,
add_existing = add_existing,
register = register_potion,
register_group = register_potion_group,
get_all = function() return potions.all_items end,
get_all_grouped = function() return potions.all_items_grouped end,
--- returns only lord internal registered potions.
get_lord = function() return potions.lord_items end,
get_lord = function() return potions.lord_items end,
get_lord_grouped = function() return potions.lord_items_grouped end,
}

0 comments on commit 10f9f5a

Please sign in to comment.