Cleanup: moved relationships into own file.
Moved trading and pricing code to another folder, separate files. NPC: Added further inventory functionality. Trading: Added price data structure, which allows for setting prices for items. Also includes some default definitions.
This commit is contained in:
53
trade/prices.lua
Normal file
53
trade/prices.lua
Normal file
@ -0,0 +1,53 @@
|
||||
-- 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 = {}
|
||||
|
||||
-- Table that contains the prices
|
||||
npc.trade.prices.table = {}
|
||||
|
||||
-- Default definitions for in-game items
|
||||
npc.trade.prices.table["default:apple"] = {item = "default:iron_ingot", count = 1}
|
||||
npc.trade.prices.table["default:stone"] = {item = "default:wood_planks", count = 1}
|
||||
npc.trade.prices.table["default:cobble"] = {item = "default:iron_ingot", count = 1}
|
||||
npc.trade.prices.table["farming:cotton"] = {item = "default:iron_ingot", count = 1}
|
||||
npc.trade.prices.table["farming:bread"] = {item = "default:gold_ingot", count = 1}
|
||||
npc.trade.prices.table["default:sword_stone"] = {item = "default:iron_ingot", count = 2}
|
||||
npc.trade.prices.table["default:pick_stone"] = {item = "default:iron_ingot", count = 1}
|
||||
npc.trade.prices.table["default:shovel_stone"] = {item = "default:iron_ingot", count = 2}
|
||||
npc.trade.prices.table["default:axe_stone"] = {item = "default:iron_ingot", count = 1}
|
||||
npc.trade.prices.table["default:hoe_stone"] = {item = "default:iron_ingot", count = 1}
|
||||
|
||||
|
||||
-- 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
|
||||
end
|
22
trade/trade.lua
Normal file
22
trade/trade.lua
Normal file
@ -0,0 +1,22 @@
|
||||
-- NPC trading abilities by Zorman2000
|
||||
|
||||
npc.trade = {}
|
||||
|
||||
npc.trade.CASUAL = "casual"
|
||||
npc.trade.TRADER = "trader"
|
||||
npc.trade.NONE = "none"
|
||||
|
||||
function npc.trade.get_random_trade_status()
|
||||
local chance = math.random(1,10)
|
||||
|
||||
if chance < 3 then
|
||||
-- Non-trader
|
||||
return npc.trade.NONE
|
||||
elseif 3 <= chance and chance <= 7 then
|
||||
-- Casual trader
|
||||
return npc.trade.CASUAL
|
||||
elseif chance > 7 then
|
||||
-- Trader by profession
|
||||
return npc.trade.TRADER
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user