Dialogue: full support for flag-based dialogues.

Added support for nested dialogue trees.
Trade: (WIP) Refactor code to support dedicated trading.
This commit is contained in:
zorman2000
2017-01-26 12:55:04 -05:00
parent 7c3801a5ce
commit 75af34beb5
7 changed files with 367 additions and 101 deletions

View File

@ -6,9 +6,9 @@ npc.trade.prices = {}
-- Define default currency (based on lumps from default)
npc.trade.prices.currency = {
tier1 = "default:gold_lump",
tier2 = "default:copper_lump",
tier3 = "default:iron_lump"
tier1 = {string = "default:gold_lump", name = "Gold lump"},
tier2 = {string = "default:copper_lump", name = "Copper lump"},
tier3 = {string = "default:iron_lump", name = "Iron lump"}
}
-- TODO: Set the currency depending on available mods
@ -18,33 +18,33 @@ npc.trade.prices.table = {}
-- Default definitions for in-game items
-- Tier 3 items: cheap items
npc.trade.prices.table["default:cobble"] = {tier = npc.trade.prices.currency.tier3, count = 0.1}
npc.trade.prices.table["flowers:geranium"] = {tier = npc.trade.prices.currency.tier3, count = 0.5}
npc.trade.prices.table["default:apple"] = {tier = npc.trade.prices.currency.tier3, count = 1}
npc.trade.prices.table["default:tree"] = {tier = npc.trade.prices.currency.tier3, count = 2}
npc.trade.prices.table["flowers:rose"] = {tier = npc.trade.prices.currency.tier3, count = 2}
npc.trade.prices.table["default:stone"] = {tier = npc.trade.prices.currency.tier3, count = 2}
npc.trade.prices.table["farming:seed_cotton"] = {tier = npc.trade.prices.currency.tier3, count = 3}
npc.trade.prices.table["farming:seed_wheat"] = {tier = npc.trade.prices.currency.tier3, count = 3}
npc.trade.prices.table["default:clay_lump"] = {tier = npc.trade.prices.currency.tier3, count = 3}
npc.trade.prices.table["default:wood"] = {tier = npc.trade.prices.currency.tier3, count = 3}
npc.trade.prices.table["mobs:meat_raw"] = {tier = npc.trade.prices.currency.tier3, count = 4}
npc.trade.prices.table["default:sapling"] = {tier = npc.trade.prices.currency.tier3, count = 5}
npc.trade.prices.table["mobs:meat"] = {tier = npc.trade.prices.currency.tier3, count = 5}
npc.trade.prices.table["mobs:leather"] = {tier = npc.trade.prices.currency.tier3, count = 6}
npc.trade.prices.table["default:sword_stone"] = {tier = npc.trade.prices.currency.tier3, count = 6}
npc.trade.prices.table["default:shovel_stone"] = {tier = npc.trade.prices.currency.tier3, count = 6}
npc.trade.prices.table["default:axe_stone"] = {tier = npc.trade.prices.currency.tier3, count = 6}
npc.trade.prices.table["default:hoe_stone"] = {tier = npc.trade.prices.currency.tier3, count = 6}
npc.trade.prices.table["default:pick_stone"] = {tier = npc.trade.prices.currency.tier3, count = 7}
npc.trade.prices.table["farming:cotton"] = {tier = npc.trade.prices.currency.tier3, count = 15}
npc.trade.prices.table["farming:bread"] = {tier = npc.trade.prices.currency.tier3, count = 20}
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}
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}
-- Tier 2 items: medium priced items
-- Tier 1 items: expensive items
npc.trade.prices.table["default:diamond"] = {tier = npc.trade.prices.currency.tier1, count = 90}
npc.trade.prices.table["advanced_npc:marriage_ring"] = {tier = npc.trade.prices.currency.tier1, count = 100}
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}
-- Functions
function npc.trade.prices.update(item_name, price)
@ -79,14 +79,43 @@ function npc.trade.prices.remove(item_name)
end
-- Gets all the item for a specified budget
function npc.trade.prices.get_items_for_currency_count(tier, count)
function npc.trade.prices.get_items_for_currency_count(tier, count, price_factor)
local result = {}
minetest.log("Currency quantity: "..dump(count))
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
result[item_name] = price
result[item_name] = {price = price}
minetest.log("Item name: "..dump(item_name)..", Price: "..dump(price))
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
result[item_name].min_buyable_item_price = min_buying_item_count * buying_price_count
result[item_name].max_buyable_item_count = max_buying_item_count
end
end
--minetest.log("Final result: "..dump(result))
return result
end
@ -94,10 +123,10 @@ end
-- currencies set in the currencies table. Returns true if
-- itemstring is a currency.
function npc.trade.prices.is_item_currency(itemstring)
if npc.get_item_name(itemstring) == npc.trade.prices.currency.tier3
or npc.get_item_name(itemstring) == npc.trade.prices.currency.tier2
or npc.get_item_name(itemstring) == npc.trade.prices.currency.tier1 then
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
return true
end
return false
end
end

View File

@ -103,9 +103,9 @@ end
-- items that a NPC has on his/her inventory
function npc.trade.get_currencies_in_inventory(self)
local result = {}
local tier3 = npc.inventory_contains(self, npc.trade.prices.currency.tier3)
local tier2 = npc.inventory_contains(self, npc.trade.prices.currency.tier2)
local tier1 = npc.inventory_contains(self, npc.trade.prices.currency.tier1)
local tier3 = npc.inventory_contains(self, npc.trade.prices.currency.tier3.string)
local tier2 = npc.inventory_contains(self, npc.trade.prices.currency.tier2.string)
local tier1 = npc.inventory_contains(self, npc.trade.prices.currency.tier1.string)
if tier3 ~= nil then
table.insert(result, {name = npc.get_item_name(tier3.item_string),
count = npc.get_item_count(tier3.item_string)} )
@ -134,34 +134,20 @@ function npc.trade.get_casual_trade_offer(self, offer_type)
-- Choose a random currency
local chosen_tier = currencies[math.random(#currencies)]
-- Get items for this currency
local buyable_items =
npc.trade.prices.get_items_for_currency_count(chosen_tier.name, chosen_tier.count)
local buyable_items =
npc.trade.prices.get_items_for_currency_count(chosen_tier.name, chosen_tier.count, 0.5)
-- Select a random item from the buyable items
local item_set = {}
for item,price in pairs(buyable_items) do
for item,price_info in pairs(buyable_items) do
table.insert(item_set, item)
end
local item = item_set[math.random(#item_set)]
-- 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
local amount_to_buy = math.random(1,5) * 2
local price_item_count = buyable_items[item].count * ((amount_to_buy) / 2)
-- Increase the amount to buy if the result of the price is a decimal number
while price_item_count % 1 ~= 0 do
amount_to_buy = amount_to_buy + 1
price_item_count = buyable_items[item].count * ((amount_to_buy) / 2)
end
-- Create price itemstring
local price_string = buyable_items[item].tier.." "
..tostring( buyable_items[item].count * (amount_to_buy / 2) )
-- Build the return object
result = {
offer_type = offer_type,
item = item.." "..amount_to_buy,
price = price_string
}
local amount_to_buy = math.random(buyable_items[item].min_buyable_item_count, buyable_items[item].max_buyable_item_count)
-- Create trade offer
result = npc.trade.create_offer(npc.trade.OFFER_BUY, item, price, amount_to_buy)
else
-- Make sell offer, NPC will sell items to player at regular price
-- NPC will also offer items from their inventory
@ -177,9 +163,61 @@ function npc.trade.get_casual_trade_offer(self, offer_type)
local item = sellable_items[math.random(#sellable_items)]
-- Choose how many of this item will be sold to player
local count = math.random(npc.get_item_count(item))
-- Create trade offer
result = npc.trade.create_offer(npc.trade.OFFER_SELL, npc.get_item_name(item), nil, count)
end
return result
end
-- 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
-- payment. The currencies should come from either the NPC's inventory or a
-- chest belonging to it.
function npc.trade.create_offer(offer_type, item, price_item_count, count)
minetest.log("Item:"..dump(item))
local result = {}
-- Check offer type
if offer_type == npc.trade.OFFER_BUY then
-- 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
local price_string = item_price.tier.." "
..tostring( item_price.count * (count / 2) )
-- Build the return object
result = {
offer_type = offer_type,
item = item.." "..count,
price = price_string
}
end
end
else
-- Make sell offer, NPC will sell items to player at regular price
-- 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[npc.get_item_name(item)]
local price_object = npc.trade.prices.table[item]
-- Check price object, if price < 1 then offer to sell for 1
if price_object.count < 1 then
price_object.count = 1
@ -194,8 +232,12 @@ function npc.trade.get_casual_trade_offer(self, offer_type)
end
return result
end
-- TODO: THis method needs to be refactored to be able to manage
-- both NPC inventories and chest inventories
function npc.trade.perform_trade(self, player_name, offer)
local item_stack = ItemStack(offer.item)
@ -219,7 +261,7 @@ function npc.trade.perform_trade(self, player_name, offer)
minetest.chat_send_player(player_name, "Thank you!")
else
minetest.chat_send_player(player_name,
"Looks like you can't what I'm giving you for payment!")
"Looks like you can't get what I'm giving you for payment!")
end
else
minetest.chat_send_player(player_name, "Looks like you don't have what I want to buy...")