2016-12-12 02:52:57 +01:00
|
|
|
-- Price table for items bought/sold by NPC traders by Zorman2000
|
|
|
|
-- This table should be globally accessible so that other mods can set
|
|
|
|
-- prices as they see fit.
|
|
|
|
|
|
|
|
npc.trade.prices = {}
|
|
|
|
|
2016-12-17 18:01:35 +01:00
|
|
|
-- Define default currency (based on lumps from default)
|
|
|
|
npc.trade.prices.currency = {
|
2017-01-26 18:55:04 +01:00
|
|
|
tier1 = {string = "default:gold_lump", name = "Gold lump"},
|
|
|
|
tier2 = {string = "default:copper_lump", name = "Copper lump"},
|
|
|
|
tier3 = {string = "default:iron_lump", name = "Iron lump"}
|
2016-12-17 18:01:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
-- TODO: Set the currency depending on available mods
|
|
|
|
|
2016-12-12 02:52:57 +01:00
|
|
|
-- Table that contains the prices
|
|
|
|
npc.trade.prices.table = {}
|
|
|
|
|
|
|
|
-- Default definitions for in-game items
|
2016-12-17 18:01:35 +01:00
|
|
|
-- Tier 3 items: cheap items
|
2017-01-26 18:55:04 +01:00
|
|
|
npc.trade.prices.table["default:cobble"] = {tier = npc.trade.prices.currency.tier3.string, count = 0.1}
|
|
|
|
npc.trade.prices.table["flowers:geranium"] = {tier = npc.trade.prices.currency.tier3.string, count = 0.5}
|
|
|
|
npc.trade.prices.table["default:apple"] = {tier = npc.trade.prices.currency.tier3.string, count = 1}
|
|
|
|
npc.trade.prices.table["default:tree"] = {tier = npc.trade.prices.currency.tier3.string, count = 2}
|
|
|
|
npc.trade.prices.table["flowers:rose"] = {tier = npc.trade.prices.currency.tier3.string, count = 2}
|
|
|
|
npc.trade.prices.table["default:stone"] = {tier = npc.trade.prices.currency.tier3.string, count = 2}
|
|
|
|
npc.trade.prices.table["farming:seed_cotton"] = {tier = npc.trade.prices.currency.tier3.string, count = 3}
|
|
|
|
npc.trade.prices.table["farming:seed_wheat"] = {tier = npc.trade.prices.currency.tier3.string, count = 3}
|
|
|
|
npc.trade.prices.table["default:clay_lump"] = {tier = npc.trade.prices.currency.tier3.string, count = 3}
|
|
|
|
npc.trade.prices.table["default:wood"] = {tier = npc.trade.prices.currency.tier3.string, count = 3}
|
|
|
|
npc.trade.prices.table["mobs:meat_raw"] = {tier = npc.trade.prices.currency.tier3.string, count = 4}
|
|
|
|
npc.trade.prices.table["default:sapling"] = {tier = npc.trade.prices.currency.tier3.string, count = 5}
|
|
|
|
npc.trade.prices.table["mobs:meat"] = {tier = npc.trade.prices.currency.tier3.string, count = 5}
|
|
|
|
npc.trade.prices.table["mobs:leather"] = {tier = npc.trade.prices.currency.tier3.string, count = 6}
|
|
|
|
npc.trade.prices.table["default:sword_stone"] = {tier = npc.trade.prices.currency.tier3.string, count = 6}
|
|
|
|
npc.trade.prices.table["default:shovel_stone"] = {tier = npc.trade.prices.currency.tier3.string, count = 6}
|
|
|
|
npc.trade.prices.table["default:axe_stone"] = {tier = npc.trade.prices.currency.tier3.string, count = 6}
|
|
|
|
npc.trade.prices.table["default:hoe_stone"] = {tier = npc.trade.prices.currency.tier3.string, count = 6}
|
|
|
|
npc.trade.prices.table["default:pick_stone"] = {tier = npc.trade.prices.currency.tier3.string, count = 7}
|
2017-09-22 00:53:43 +02:00
|
|
|
npc.trade.prices.table["bucket:bucket_empty"] = {tier = npc.trade.prices.currency.tier3.string, count = 10}
|
2017-01-26 18:55:04 +01:00
|
|
|
npc.trade.prices.table["farming:cotton"] = {tier = npc.trade.prices.currency.tier3.string, count = 15}
|
|
|
|
npc.trade.prices.table["farming:bread"] = {tier = npc.trade.prices.currency.tier3.string, count = 20}
|
2016-12-17 18:01:35 +01:00
|
|
|
|
|
|
|
-- Tier 2 items: medium priced items
|
2016-12-12 02:52:57 +01:00
|
|
|
|
2016-12-17 18:01:35 +01:00
|
|
|
-- Tier 1 items: expensive items
|
2017-02-02 03:16:31 +01:00
|
|
|
npc.trade.prices.table["default:mese_crystal"] = {tier = npc.trade.prices.currency.tier1.string, count = 45}
|
2017-01-26 18:55:04 +01:00
|
|
|
npc.trade.prices.table["default:diamond"] = {tier = npc.trade.prices.currency.tier1.string, count = 90}
|
|
|
|
npc.trade.prices.table["advanced_npc:marriage_ring"] = {tier = npc.trade.prices.currency.tier1.string, count = 100}
|
2016-12-12 02:52:57 +01:00
|
|
|
|
|
|
|
-- Functions
|
|
|
|
function npc.trade.prices.update(item_name, price)
|
|
|
|
for key,value in pairs(npc.trade.prices.table) do
|
|
|
|
if key == item_name then
|
|
|
|
value = price
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function npc.trade.prices.get(item_name)
|
|
|
|
for key,value in pairs(npc.trade.prices.table) do
|
|
|
|
if key == item_name then
|
|
|
|
return {item_name = key, price = value}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function npc.trade.prices.add(item_name, price)
|
|
|
|
if npc.trade.prices.get(item_name) == nil then
|
|
|
|
npc.trade.prices.table[item_name] = price
|
|
|
|
else
|
|
|
|
npc.trade.prices.update(item_name, price)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function npc.trade.prices.remove(item_name)
|
|
|
|
npc.trade.prices.table[item_name] = nil
|
2016-12-17 18:01:35 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Gets all the item for a specified budget
|
2017-01-26 18:55:04 +01:00
|
|
|
function npc.trade.prices.get_items_for_currency_count(tier, count, price_factor)
|
2016-12-17 18:01:35 +01:00
|
|
|
local result = {}
|
2017-02-09 17:38:14 +01:00
|
|
|
--minetest.log("Currency quantity: "..dump(count))
|
2016-12-17 18:01:35 +01:00
|
|
|
for item_name, price in pairs(npc.trade.prices.table) do
|
|
|
|
-- Check price currency is of the same tier
|
|
|
|
if price.tier == tier and price.count <= count then
|
2017-01-26 18:55:04 +01:00
|
|
|
result[item_name] = {price = price}
|
|
|
|
|
2017-01-30 02:08:34 +01:00
|
|
|
--minetest.log("Item name: "..dump(item_name)..", Price: "..dump(price))
|
2017-01-26 18:55:04 +01:00
|
|
|
|
|
|
|
local min_buying_item_count = 1
|
|
|
|
-- Calculate price NPC is going to buy for
|
|
|
|
local buying_price_count = price.count * price_factor
|
|
|
|
-- Check if the buying price is not an integer
|
|
|
|
if buying_price_count % 1 ~= 0 then
|
|
|
|
-- If not, increase the buying item count until we get an integer
|
|
|
|
local adjust = 1 / price_factor
|
|
|
|
if price.count < 1 then
|
|
|
|
adjust = 1 / (price.count * price_factor)
|
|
|
|
end
|
|
|
|
min_buying_item_count = min_buying_item_count * adjust
|
|
|
|
end
|
|
|
|
--minetest.log("Minimum item buy quantity: "..dump(min_buying_item_count))
|
|
|
|
--minetest.log("Minimum item price quantity: "..dump(buying_price_count))
|
|
|
|
-- Calculate maximum buyable quantity
|
|
|
|
local max_buying_item_count = min_buying_item_count
|
|
|
|
while ((max_buying_item_count + min_buying_item_count) * buying_price_count <= count) do
|
|
|
|
max_buying_item_count = max_buying_item_count + min_buying_item_count
|
|
|
|
end
|
|
|
|
--minetest.log("Maximum item buy quantity: "..dump(max_buying_item_count))
|
|
|
|
|
|
|
|
result[item_name].min_buyable_item_count = min_buying_item_count
|
2017-01-30 02:08:34 +01:00
|
|
|
result[item_name].min_buyable_item_price = buying_price_count
|
2017-01-26 18:55:04 +01:00
|
|
|
result[item_name].max_buyable_item_count = max_buying_item_count
|
2016-12-17 18:01:35 +01:00
|
|
|
end
|
|
|
|
end
|
2017-02-09 17:38:14 +01:00
|
|
|
--minetest.log("Final result: "..dump(result))
|
2016-12-17 18:01:35 +01:00
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
-- This methods will compare the given item string to the
|
|
|
|
-- currencies set in the currencies table. Returns true if
|
|
|
|
-- itemstring is a currency.
|
|
|
|
function npc.trade.prices.is_item_currency(itemstring)
|
2017-01-26 18:55:04 +01:00
|
|
|
if npc.get_item_name(itemstring) == npc.trade.prices.currency.tier3.string
|
|
|
|
or npc.get_item_name(itemstring) == npc.trade.prices.currency.tier2.string
|
|
|
|
or npc.get_item_name(itemstring) == npc.trade.prices.currency.tier1.string then
|
2016-12-17 18:01:35 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
2017-01-26 18:55:04 +01:00
|
|
|
end
|