Skip to content

Commit

Permalink
Mobs: Traders: Refactoring: rename TraderDef -> TraderConfig & us…
Browse files Browse the repository at this point in the history
…e local. Relates to #1170
  • Loading branch information
alek13 committed Sep 25, 2023
1 parent 6b14bc5 commit 058cde1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion mods/lord/Entities/lottmobs/src/trader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ end

--- @param entity LuaEntity
--- @param clicker Player
--- @param trader_def TraderDef
--- @param trader_def TraderConfig
--- @param race_privilege string
function lottmobs_trader(entity, clicker, trader_def, race_privilege)
face_pos(entity, clicker:get_pos())
Expand Down
30 changes: 15 additions & 15 deletions mods/lord/Entities/lottmobs/src/trader_Inventory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ local function update_takeaway(inv)
end

--- @param trader_inventory InvRef
--- @param trader_def TraderDef
local function add_goods(trader_inventory, trader_def)
local goods = trader_def.items
--- @param trader_config TraderConfig
local function add_goods(trader_inventory, trader_config)
local goods = trader_config.items

local max_goods = trader_inventory:get_size("goods")
local i = 1
Expand All @@ -51,7 +51,7 @@ local function get_discount(price, same_race)
end

--- @param good_stack_string string
--- @param trader_def TraderDef
--- @param trader_def TraderConfig
--- @param same_race boolean
local function get_price_for(good_stack_string, trader_def, same_race)
local good = trader_def.items[good_stack_string]
Expand Down Expand Up @@ -166,26 +166,26 @@ local Inventory = {
entity_id = nil,
--- @type string
detached_inv_id = nil,
--- @type TraderDef
trader_def = nil,
--- @type TraderConfig
trader_config = nil,
--- @type boolean
same_race = false,
}

--- Constructor
--- @public
--- @param player Player
--- @param entity LuaEntity
--- @param trader_def TraderDef
--- @param player Player
--- @param entity LuaEntity
--- @param trader_config TraderConfig
--- @param race_privilege string
--- @return trader.Inventory
function Inventory:new(player, entity, trader_def, race_privilege)
function Inventory:new(player, entity, trader_config, race_privilege)
local class = self
self = {}

self.player_name = player:get_player_name()
self.entity_id = entity.id
self.trader_def = trader_def
self.player_name = player:get_player_name()
self.entity_id = entity.id
self.trader_config = trader_config
if minetest.get_player_privs(self.player_name)[race_privilege] ~= nil then
self.same_race = true
end
Expand Down Expand Up @@ -216,7 +216,7 @@ function Inventory:create_detached_inventory(inventory_id)
local sel_stack = inventory:get_stack("selection", 1)
local sel_stack_string = sel_stack:get_name() .. " " .. sel_stack:get_count()

local price = get_price_for(sel_stack_string, self.trader_def, self.same_race)
local price = get_price_for(sel_stack_string, self.trader_config, self.same_race)
inventory:set_stack("price", 1, price)
update_takeaway(inventory)
end
Expand All @@ -231,7 +231,7 @@ function Inventory:create_detached_inventory(inventory_id)
trader_inventory:set_size("selection", 1)
trader_inventory:set_size("price", 1)
trader_inventory:set_size("payment", 1)
add_goods(trader_inventory, self.trader_def)
add_goods(trader_inventory, self.trader_config)

return trader_inventory
end
Expand Down
25 changes: 15 additions & 10 deletions mods/lord/Entities/lottmobs/src/trader_goods.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
--{thing selling, price (in gold ingots), chance that it won't appear in the trader's inventory}
local SL = minetest.get_translator("lottmobs")

--- @class TraderDef
--- @class TraderConfig
--- @field items table<string,{price:string,chance:number}> key: stack_string, price: stack_string, chance: percent
--- @field names string[] random names for traders
--- @field messages string[] random messages for traders

--- @type TraderDef
lottmobs.dwarf = {
--- @type TraderConfig[]
local trader_config = {}

--- @type TraderConfig
trader_config.dwarf = {
items = {
["lord_money:gold_coin 1"] = { price = "lord_money:silver_coin 10", chance = 5 },
["lord_money:silver_coin 1"] = { price = "lord_money:copper_coin 10", chance = 5 },
Expand Down Expand Up @@ -43,8 +46,8 @@ lottmobs.dwarf = {
SL("If you venture deep underground, beware! The monsters there are very powerful, and kill the unprepared instantly."), -- luacheck: no_max_line_length
}
}
--- @type TraderDef
lottmobs.elf = {
--- @type TraderConfig
trader_config.elf = {
items = {
["lord_money:gold_coin 1"] = { price = "lord_money:silver_coin 10", chance = 5 },
["lord_money:silver_coin 1"] = { price = "lord_money:copper_coin 10", chance = 5 },
Expand Down Expand Up @@ -79,8 +82,8 @@ lottmobs.elf = {
SL("Beware! Our society, and all societies, are on the edge of a knife blade - one false move and all will end, and Sauron will rule supreme."), -- luacheck: no_max_line_length
}
}
--- @type TraderDef
lottmobs.hobbit = {
--- @type TraderConfig
trader_config.hobbit = {
items = {
["lord_money:gold_coin 1"] = { price = "lord_money:silver_coin 10", chance = 5 },
["lord_money:silver_coin 1"] = { price = "lord_money:copper_coin 10", chance = 5 },
Expand Down Expand Up @@ -115,8 +118,8 @@ lottmobs.hobbit = {
SL("Food is meant to be enjoyed, not rushed. Don't just eat a little here and a little there, sit down for a proper meal sometimes..."), -- luacheck: no_max_line_length
}
}
--- @type TraderDef
lottmobs.human = {
--- @type TraderConfig
trader_config.human = {
items = {
["lord_money:gold_coin 1"] = { price = "lord_money:silver_coin 10", chance = 5 },
["lord_money:silver_coin 1"] = { price = "lord_money:copper_coin 10", chance = 5 },
Expand Down Expand Up @@ -151,7 +154,7 @@ lottmobs.human = {
SL("Life here is far from normal. We wish for peace, yet the only way we can get peace is through war..."),
}
}
lottmobs.orc = {
trader_config.orc = {
names = {
"Azog", "Balcmeg", "Boldog", "Bolg", "Golfimbul", "Gorbag", "Gorgol",
"Grishnákh", "Lagduf", "Lug", "Lugdush", "Mauhúr", "Muzgash", "Orcobal",
Expand All @@ -162,3 +165,5 @@ lottmobs.orc = {
SL("Arrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr!, KILL! KILL! KILL!")
},
}

return trader_config
4 changes: 2 additions & 2 deletions mods/lord/Entities/lottmobs/src/traders.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dofile(minetest.get_modpath("lottmobs").."/src/trader_goods.lua")
local trader_config = dofile(minetest.get_modpath("lottmobs").."/src/trader_goods.lua")
dofile(minetest.get_modpath("lottmobs").."/src/trader.lua")

local common_trader_definition = {
Expand Down Expand Up @@ -44,7 +44,7 @@ local function register_trader(name, definition)
local def = table.merge(common_trader_definition, definition)
local race_privilege = "GAME" .. def.race -- GAMEelf, GAMEman, GAMEhobbit, GAMEdwarf
def.on_rightclick = function(self, clicker)
lottmobs_trader(self, clicker, lottmobs[def.race], race_privilege)
lottmobs_trader(self, clicker, trader_config[def.race], race_privilege)
end

mobs:register_mob(name, def)
Expand Down

0 comments on commit 058cde1

Please sign in to comment.