Trade: A list of buyable items is now calculated with the maximum amount buyable (given the currency), the minimum buyable amount and the minimum price.
Port casual trading to use this system. Refactor the offer function to also support dedicated trading.
This commit is contained in:
parent
25f1fcbfa8
commit
d7a8de5b86
@ -87,7 +87,7 @@ function npc.trade.prices.get_items_for_currency_count(tier, count, price_factor
|
|||||||
if price.tier == tier and price.count <= count then
|
if price.tier == tier and price.count <= count then
|
||||||
result[item_name] = {price = price}
|
result[item_name] = {price = price}
|
||||||
|
|
||||||
minetest.log("Item name: "..dump(item_name)..", Price: "..dump(price))
|
--minetest.log("Item name: "..dump(item_name)..", Price: "..dump(price))
|
||||||
|
|
||||||
local min_buying_item_count = 1
|
local min_buying_item_count = 1
|
||||||
-- Calculate price NPC is going to buy for
|
-- Calculate price NPC is going to buy for
|
||||||
@ -111,11 +111,11 @@ function npc.trade.prices.get_items_for_currency_count(tier, count, price_factor
|
|||||||
--minetest.log("Maximum item buy quantity: "..dump(max_buying_item_count))
|
--minetest.log("Maximum item buy quantity: "..dump(max_buying_item_count))
|
||||||
|
|
||||||
result[item_name].min_buyable_item_count = min_buying_item_count
|
result[item_name].min_buyable_item_count = min_buying_item_count
|
||||||
result[item_name].min_buyable_item_price = min_buying_item_count * buying_price_count
|
result[item_name].min_buyable_item_price = buying_price_count
|
||||||
result[item_name].max_buyable_item_count = max_buying_item_count
|
result[item_name].max_buyable_item_count = max_buying_item_count
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
--minetest.log("Final result: "..dump(result))
|
minetest.log("Final result: "..dump(result))
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -147,7 +147,8 @@ function npc.trade.get_casual_trade_offer(self, offer_type)
|
|||||||
-- so that the price count is always an integer number
|
-- so that the price count is always an integer number
|
||||||
local amount_to_buy = math.random(buyable_items[item].min_buyable_item_count, buyable_items[item].max_buyable_item_count)
|
local amount_to_buy = math.random(buyable_items[item].min_buyable_item_count, buyable_items[item].max_buyable_item_count)
|
||||||
-- Create trade offer
|
-- Create trade offer
|
||||||
result = npc.trade.create_offer(npc.trade.OFFER_BUY, item, price, amount_to_buy)
|
minetest.log("Buyable item: "..dump(buyable_items[item]))
|
||||||
|
result = npc.trade.create_offer(npc.trade.OFFER_BUY, item, buyable_items[item].price, buyable_items[item].min_buyable_item_price, amount_to_buy)
|
||||||
else
|
else
|
||||||
-- Make sell offer, NPC will sell items to player at regular price
|
-- Make sell offer, NPC will sell items to player at regular price
|
||||||
-- NPC will also offer items from their inventory
|
-- NPC will also offer items from their inventory
|
||||||
@ -164,46 +165,23 @@ function npc.trade.get_casual_trade_offer(self, offer_type)
|
|||||||
-- Choose how many of this item will be sold to player
|
-- Choose how many of this item will be sold to player
|
||||||
local count = math.random(npc.get_item_count(item))
|
local count = math.random(npc.get_item_count(item))
|
||||||
-- Create trade offer
|
-- Create trade offer
|
||||||
result = npc.trade.create_offer(npc.trade.OFFER_SELL, npc.get_item_name(item), nil, count)
|
result = npc.trade.create_offer(npc.trade.OFFER_SELL, npc.get_item_name(item), nil, nil, count)
|
||||||
end
|
end
|
||||||
|
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Creates a trade offer based on the offer type, given item and count. If
|
-- Creates a trade offer based on the offer type, given item and count. If
|
||||||
-- the offer is a "buy" offer, it is required to provide currency items for
|
-- the offer is a "buy" offer, it is required to provide the price item and
|
||||||
-- payment. The currencies should come from either the NPC's inventory or a
|
-- the minimum price item count.
|
||||||
-- chest belonging to it.
|
function npc.trade.create_offer(offer_type, item, price, min_price_item_count, count)
|
||||||
function npc.trade.create_offer(offer_type, item, price_item_count, count)
|
|
||||||
|
|
||||||
minetest.log("Item:"..dump(item))
|
|
||||||
local result = {}
|
local result = {}
|
||||||
-- Check offer type
|
-- Check offer type
|
||||||
if offer_type == npc.trade.OFFER_BUY then
|
if offer_type == npc.trade.OFFER_BUY then
|
||||||
-- Get price for the given item
|
-- Get price for the given item
|
||||||
local item_price = npc.trade.prices.table[item]
|
|
||||||
minetest.log("Item price: "..dump(item_price))
|
|
||||||
-- Choose buying quantity. Since this is a buy offer, NPC will buy items
|
|
||||||
-- at half the price. Therefore, NPC will always ask for even quantities
|
|
||||||
-- so that the price count is always an integer number
|
|
||||||
minetest.log("Count: "..dump(count)..", price item count: "..dump(item_price.count))
|
|
||||||
local price_item_count = item_price.count * ((count) / 2)
|
|
||||||
minetest.log("Price item: "..dump(price_item_count))
|
|
||||||
-- Increase the amount to buy if the result of the price is a decimal number
|
|
||||||
while price_item_count % 1 ~= 0 do
|
|
||||||
minetest.log("Count: "..dump(count))
|
|
||||||
count = count + 1
|
|
||||||
price_item_count = item_price.count * ((count) / 2)
|
|
||||||
minetest.log("Price item: "..dump(price_item_count))
|
|
||||||
end
|
|
||||||
-- Check if the currency items can pay for the selected amount to buy
|
|
||||||
for i = 1, #currency_items do
|
|
||||||
minetest.log("Currency item: "..dump(currency_items[i]))
|
|
||||||
minetest.log("Name: "..dump(item_price.tier)..", Count: "..dump(price_item_count))
|
|
||||||
if currency_items[i].name == item_price.tier and currency_items[i].count >= item_price.count then
|
|
||||||
-- Create price itemstring
|
-- Create price itemstring
|
||||||
local price_string = item_price.tier.." "
|
local price_string = price.tier.." "
|
||||||
..tostring( item_price.count * (count / 2) )
|
..tostring( min_price_item_count * count )
|
||||||
|
|
||||||
-- Build the return object
|
-- Build the return object
|
||||||
result = {
|
result = {
|
||||||
@ -211,12 +189,9 @@ function npc.trade.create_offer(offer_type, item, price_item_count, count)
|
|||||||
item = item.." "..count,
|
item = item.." "..count,
|
||||||
price = price_string
|
price = price_string
|
||||||
}
|
}
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
-- Make sell offer, NPC will sell items to player at regular price
|
-- Make sell offer, NPC will sell items to player at regular price
|
||||||
-- Get and calculate price for this object
|
-- Get and calculate price for this object
|
||||||
minetest.log("Item: "..dump(item)..", name: "..dump(npc.get_item_name(item)))
|
|
||||||
local price_object = npc.trade.prices.table[item]
|
local price_object = npc.trade.prices.table[item]
|
||||||
-- Check price object, if price < 1 then offer to sell for 1
|
-- Check price object, if price < 1 then offer to sell for 1
|
||||||
if price_object.count < 1 then
|
if price_object.count < 1 then
|
||||||
|
Loading…
Reference in New Issue
Block a user