test / test (push) Successful in 1m45s
The cavern layer gets its own biome table, picked per cell from two 2D noises so biomes are patches in x and y. Each row names a floor cover, floor flora, a ceiling chain, giant mushrooms, a pool threshold and a spawn table (read by strata.spawn_candidates when given x). - Glimmer grove: mycel turf, glowing glimmercaps (drop glowspores) and umbrel mushrooms that fell like trees and give wood. - Prism gallery: glowing prism clusters on floors and roofs (drop prism shards, a material for later gear). - Drowned grotto: silt floors, glowing dripvines up to 4 long and far more water pools. Flora glows, can be walked through and drops when the block holding it is dug; a hanging chain goes all at once. Glowspore + wood makes torches. Names and art are original; df_caverns was only a reference for the idea. Tests: 6 new cases in tests/caverns.lua (table, coverage, node_at scan, a generated site per biome, flora support, felling, recipe and spawns); the layer tests accept cavern biome nodes. 103/103 pass, no ERROR lines. Closes #70 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
196 lines
7.0 KiB
Lua
196 lines
7.0 KiB
Lua
-- Strata crafting: Terraria-style recipe list.
|
|
--
|
|
-- There is no crafting grid. The inventory screen lists every recipe the
|
|
-- player can make right now, given what they carry and which crafting
|
|
-- stations are within range of their avatar. Click a recipe to craft it.
|
|
|
|
local S = strata.S
|
|
local STATION_RANGE = 4
|
|
local MAX_SHOWN = 30
|
|
|
|
strata.recipes = {}
|
|
|
|
dofile(core.get_modpath("strata_crafting") .. "/inventory.lua")
|
|
|
|
--- Register a recipe.
|
|
-- def.output: itemstring, e.g. "strata_core:torch 3"
|
|
-- def.items: {["item:name"] = count, ...}
|
|
-- def.station: node name required nearby (optional)
|
|
function strata.register_recipe(def)
|
|
assert(def.output and def.items, "recipe needs output and items")
|
|
table.insert(strata.recipes, def)
|
|
end
|
|
|
|
local function station_near(name, station)
|
|
if not station then
|
|
return true
|
|
end
|
|
local pos = strata.get_avatar_pos(name)
|
|
if not pos then
|
|
return false
|
|
end
|
|
return core.find_node_near(vector.round(pos), STATION_RANGE, station, true) ~= nil
|
|
end
|
|
|
|
local function can_craft(player, recipe)
|
|
local inv = player:get_inventory()
|
|
for item, count in pairs(recipe.items) do
|
|
if not inv:contains_item("main", ItemStack(item .. " " .. count)) then
|
|
return false
|
|
end
|
|
end
|
|
return station_near(player:get_player_name(), recipe.station)
|
|
end
|
|
|
|
local function describe(recipe)
|
|
local parts = {}
|
|
for item, count in pairs(recipe.items) do
|
|
local def = core.registered_items[item]
|
|
table.insert(parts, count .. " x " .. (def and def.description or item))
|
|
end
|
|
table.sort(parts)
|
|
local text = S("Needs: @1", table.concat(parts, ", "))
|
|
if recipe.station then
|
|
local sdef = core.registered_nodes[recipe.station]
|
|
text = text .. "\n" .. S("At: @1", sdef and sdef.description or recipe.station)
|
|
end
|
|
return text
|
|
end
|
|
|
|
local function build_formspec(player)
|
|
local fs = {
|
|
"formspec_version[6]",
|
|
"size[13.25,11]",
|
|
"label[0.5,0.5;" .. core.formspec_escape(S("Crafting")) .. "]",
|
|
}
|
|
local shown = 0
|
|
for i, recipe in ipairs(strata.recipes) do
|
|
if shown < MAX_SHOWN and can_craft(player, recipe) then
|
|
local x = 0.5 + (shown % 10) * 1.25
|
|
local y = 0.9 + math.floor(shown / 10) * 1.25
|
|
local out = ItemStack(recipe.output)
|
|
local count = out:get_count() > 1 and tostring(out:get_count()) or ""
|
|
fs[#fs + 1] = ("item_image_button[%.2f,%.2f;1,1;%s;craft_%d;%s]"):format(
|
|
x, y, out:get_name(), i, count)
|
|
local odef = core.registered_items[out:get_name()]
|
|
fs[#fs + 1] = ("tooltip[craft_%d;%s]"):format(i, core.formspec_escape(
|
|
(odef and odef.description or out:get_name()) .. "\n" .. describe(recipe)))
|
|
shown = shown + 1
|
|
end
|
|
end
|
|
if shown == 0 then
|
|
fs[#fs + 1] = "label[0.5,1.5;" .. core.formspec_escape(
|
|
S("Nothing to craft. Gather materials or stand near a crafting station.")) .. "]"
|
|
end
|
|
fs[#fs + 1] = "label[0.5,5.2;" .. core.formspec_escape(S("Inventory")) .. "]"
|
|
fs[#fs + 1] = strata.inventory_controls(player, 4.8)
|
|
fs[#fs + 1] = "list[current_player;main;0.5,5.5;10,4;]"
|
|
return table.concat(fs)
|
|
end
|
|
|
|
local cache = {}
|
|
|
|
local function refresh(player)
|
|
local name = player:get_player_name()
|
|
local fs = build_formspec(player)
|
|
if cache[name] ~= fs then
|
|
cache[name] = fs
|
|
player:set_inventory_formspec(fs)
|
|
end
|
|
end
|
|
strata.refresh_crafting = refresh
|
|
|
|
local function do_craft(player, index)
|
|
local recipe = strata.recipes[index]
|
|
if not recipe or not can_craft(player, recipe) then
|
|
return
|
|
end
|
|
local inv = player:get_inventory()
|
|
local out = ItemStack(recipe.output)
|
|
for item, count in pairs(recipe.items) do
|
|
inv:remove_item("main", ItemStack(item .. " " .. count))
|
|
end
|
|
local left = inv:add_item("main", out)
|
|
if not left:is_empty() then
|
|
local pos = strata.get_avatar_pos(player:get_player_name()) or player:get_pos()
|
|
core.add_item(pos, left)
|
|
end
|
|
end
|
|
|
|
core.register_on_player_receive_fields(function(player, formname, fields)
|
|
if formname ~= "" then
|
|
return
|
|
end
|
|
if strata.inventory_fields(player, fields) then
|
|
refresh(player)
|
|
return true
|
|
end
|
|
for field in pairs(fields) do
|
|
local index = field:match("^craft_(%d+)$")
|
|
if index then
|
|
do_craft(player, tonumber(index))
|
|
refresh(player)
|
|
return true
|
|
end
|
|
end
|
|
end)
|
|
|
|
core.register_on_joinplayer(function(player)
|
|
refresh(player)
|
|
end)
|
|
|
|
core.register_on_leaveplayer(function(player)
|
|
cache[player:get_player_name()] = nil
|
|
end)
|
|
|
|
local timer = 0
|
|
core.register_globalstep(function(dtime)
|
|
timer = timer + dtime
|
|
if timer < 0.5 then
|
|
return
|
|
end
|
|
timer = 0
|
|
for _, player in ipairs(core.get_connected_players()) do
|
|
refresh(player)
|
|
end
|
|
end)
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Recipes (quantities follow Terraria where it makes sense)
|
|
--------------------------------------------------------------------------
|
|
|
|
local R = strata.register_recipe
|
|
local WB, FURNACE, ANVIL = "strata_core:workbench", "strata_core:furnace", "strata_core:anvil"
|
|
|
|
R({output = "strata_core:workbench", items = {["strata_core:wood"] = 10}})
|
|
R({output = "strata_core:torch 3", items = {["strata_core:gel"] = 1, ["strata_core:wood"] = 1}})
|
|
-- cavern torches, so a base underground can be lit without gel (#70)
|
|
R({output = "strata_core:torch 3", items = {["strata_core:glowspore"] = 1, ["strata_core:wood"] = 1}})
|
|
R({output = "strata_core:platform_wood 2", items = {["strata_core:wood"] = 1}})
|
|
|
|
R({output = "strata_core:wood_sword", items = {["strata_core:wood"] = 7}, station = WB})
|
|
R({output = "strata_core:wood_hammer", items = {["strata_core:wood"] = 8}, station = WB})
|
|
R({output = "strata_core:wall_wood 4", items = {["strata_core:wood"] = 1}, station = WB})
|
|
R({output = "strata_core:door_wood", items = {["strata_core:wood"] = 6}, station = WB})
|
|
R({output = "strata_core:chest_wood", items = {["strata_core:wood"] = 8, ["strata_core:iron_bar"] = 2}, station = WB})
|
|
R({output = "strata_core:wall_dirt 4", items = {["strata_core:dirt"] = 1}, station = WB})
|
|
R({output = "strata_core:wall_stone 4", items = {["strata_core:stone"] = 1}, station = WB})
|
|
R({output = "strata_core:wall_slate 4", items = {["strata_core:slate"] = 1}, station = WB})
|
|
R({output = "strata_core:wall_basalt 4", items = {["strata_core:basalt"] = 1}, station = WB})
|
|
R({output = "strata_core:furnace", items = {["strata_core:stone"] = 20,
|
|
["strata_core:wood"] = 4, ["strata_core:torch"] = 3}, station = WB})
|
|
R({output = "strata_core:anvil", items = {["strata_core:iron_bar"] = 5}, station = WB})
|
|
R({output = "strata_core:bucket", items = {["strata_core:iron_bar"] = 3}, station = ANVIL})
|
|
|
|
R({output = "strata_core:copper_bar", items = {["strata_core:copper_ore"] = 3}, station = FURNACE})
|
|
R({output = "strata_core:iron_bar", items = {["strata_core:iron_ore"] = 3}, station = FURNACE})
|
|
|
|
for _, m in ipairs({"copper", "iron"}) do
|
|
local bar = "strata_core:" .. m .. "_bar"
|
|
R({output = "strata_core:" .. m .. "_pickaxe", items = {[bar] = 12, ["strata_core:wood"] = 4}, station = ANVIL})
|
|
R({output = "strata_core:" .. m .. "_axe", items = {[bar] = 9, ["strata_core:wood"] = 3}, station = ANVIL})
|
|
R({output = "strata_core:" .. m .. "_hammer", items = {[bar] = 10, ["strata_core:wood"] = 3}, station = ANVIL})
|
|
R({output = "strata_core:" .. m .. "_sword", items = {[bar] = 8}, station = ANVIL})
|
|
R({output = "strata_core:" .. m .. "_shortsword", items = {[bar] = 7}, station = ANVIL})
|
|
end
|