diff --git a/mods/lord/Entities/lottmobs/src/trader.lua b/mods/lord/Entities/lottmobs/src/trader.lua index 0c8323ba3..95f7e3ac2 100644 --- a/mods/lord/Entities/lottmobs/src/trader.lua +++ b/mods/lord/Entities/lottmobs/src/trader.lua @@ -1,5 +1,7 @@ local SL = minetest.get_translator("lottmobs") +--- @type trader.config[] +local config = dofile(minetest.get_modpath("lottmobs").."/src/trader_config.lua") --- @type trader.Form local Form = dofile(minetest.get_modpath("lottmobs").."/src/trader_Form.lua") --- @type trader.Inventory @@ -23,17 +25,19 @@ end --- @param entity LuaEntity --- @param clicker Player ---- @param trader_def TraderConfig +--- @param race string --- @param race_privilege string -function lottmobs_trader(entity, clicker, trader_def, race_privilege) +function lottmobs_trader(entity, clicker, race, race_privilege) face_pos(entity, clicker:get_pos()) local player_name = clicker:get_player_name() --- self.messages = tostring(race.messages[math.random(1,#race.messages)]) + + local trader_config = config[race] + if entity.id == 0 then entity.id = (math.random(1, 1000) * math.random(1, 10000)) .. entity.name .. (math.random(1, 1000) ^ 2) end if entity.game_name == "mob" then - entity.game_name = tostring(trader_def.names[math.random(1,#trader_def.names)]) + entity.game_name = tostring(trader_config.names[math.random(1,#trader_config.names)]) --self.nametag = self.game_name end @@ -41,10 +45,10 @@ function lottmobs_trader(entity, clicker, trader_def, race_privilege) player_name, "[NPC] <" .. SL("Trader") .. " " .. SL(entity.game_name) .. "> " .. SL("Hello") .. ", " .. player_name .. ", \n" .. - tostring(trader_def.messages[math.random(1, #trader_def.messages)]) -- messages already translated + tostring(trader_config.messages[math.random(1, #trader_config.messages)]) -- messages already translated ) - local inventory_id = Inventory:new(clicker, entity, trader_def, race_privilege):get_id() + local inventory_id = Inventory:new(clicker, entity, trader_config.items, race_privilege):get_id() Form:new(clicker, inventory_id, entity.game_name):open() end diff --git a/mods/lord/Entities/lottmobs/src/trader_Inventory.lua b/mods/lord/Entities/lottmobs/src/trader_Inventory.lua index 0f174d026..36de48575 100644 --- a/mods/lord/Entities/lottmobs/src/trader_Inventory.lua +++ b/mods/lord/Entities/lottmobs/src/trader_Inventory.lua @@ -24,14 +24,12 @@ local function update_takeaway(inv) end --- @param trader_inventory InvRef ---- @param trader_config TraderConfig -local function add_goods(trader_inventory, trader_config) - local goods = trader_config.items - +--- @param goods_config trader.config.good[] +local function add_goods(trader_inventory, goods_config) local max_goods = trader_inventory:get_size("goods") local i = 1 - for name, good in pairs(goods) do + for name, good in pairs(goods_config) do -- FIXME: оказывается это какой-то обратный процент, т.к. ">", а не "<" if math.random(0, 100) > good.chance then trader_inventory:set_stack("goods", i, name) @@ -51,10 +49,11 @@ local function get_discount(price, same_race) end --- @param good_stack_string string ---- @param trader_def TraderConfig +--- @param goods_config trader.config.good[] --- @param same_race boolean -local function get_price_for(good_stack_string, trader_def, same_race) - local good = trader_def.items[good_stack_string] +--- @return string|nil stack string (for ex.: "lord_money:silver_coin 9") +local function get_price_for(good_stack_string, goods_config, same_race) + local good = goods_config[good_stack_string] return good and get_discount(good.price, same_race) or nil end @@ -166,8 +165,8 @@ local Inventory = { entity_id = nil, --- @type string detached_inv_id = nil, - --- @type TraderConfig - trader_config = nil, + --- @type trader.config.good[] + goods_config = nil, --- @type boolean same_race = false, } @@ -176,16 +175,16 @@ local Inventory = { --- @public --- @param player Player --- @param entity LuaEntity ---- @param trader_config TraderConfig +--- @param goods_config trader.config.good[] --- @param race_privilege string --- @return trader.Inventory -function Inventory:new(player, entity, trader_config, race_privilege) +function Inventory:new(player, entity, goods_config, race_privilege) local class = self self = {} - self.player_name = player:get_player_name() - self.entity_id = entity.id - self.trader_config = trader_config + self.player_name = player:get_player_name() + self.entity_id = entity.id + self.goods_config = goods_config if minetest.get_player_privs(self.player_name)[race_privilege] ~= nil then self.same_race = true end @@ -216,7 +215,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_config, self.same_race) + local price = get_price_for(sel_stack_string, self.goods_config, self.same_race) inventory:set_stack("price", 1, price) update_takeaway(inventory) end @@ -231,7 +230,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_config) + add_goods(trader_inventory, self.goods_config) return trader_inventory end diff --git a/mods/lord/Entities/lottmobs/src/trader_goods.lua b/mods/lord/Entities/lottmobs/src/trader_config.lua similarity index 96% rename from mods/lord/Entities/lottmobs/src/trader_goods.lua rename to mods/lord/Entities/lottmobs/src/trader_config.lua index e058e735e..81b4509ec 100644 --- a/mods/lord/Entities/lottmobs/src/trader_goods.lua +++ b/mods/lord/Entities/lottmobs/src/trader_config.lua @@ -2,15 +2,19 @@ --{thing selling, price (in gold ingots), chance that it won't appear in the trader's inventory} local SL = minetest.get_translator("lottmobs") ---- @class TraderConfig ---- @field items table key: stack_string, price: stack_string, chance: percent +--- @class trader.config +--- @field items table key: stack_string, value: {price: stack_string, chance: percent} --- @field names string[] random names for traders --- @field messages string[] random messages for traders ---- @type TraderConfig[] +--- @class trader.config.good +--- @field price string stack_string +--- @field chance number percent + +--- @type trader.config[] local trader_config = {} ---- @type TraderConfig +--- @type trader.config trader_config.dwarf = { items = { ["lord_money:gold_coin 1"] = { price = "lord_money:silver_coin 10", chance = 5 }, @@ -46,7 +50,7 @@ trader_config.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 TraderConfig +--- @type trader.config trader_config.elf = { items = { ["lord_money:gold_coin 1"] = { price = "lord_money:silver_coin 10", chance = 5 }, @@ -82,7 +86,7 @@ trader_config.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 TraderConfig +--- @type trader.config trader_config.hobbit = { items = { ["lord_money:gold_coin 1"] = { price = "lord_money:silver_coin 10", chance = 5 }, @@ -118,7 +122,7 @@ trader_config.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 TraderConfig +--- @type trader.config trader_config.human = { items = { ["lord_money:gold_coin 1"] = { price = "lord_money:silver_coin 10", chance = 5 }, diff --git a/mods/lord/Entities/lottmobs/src/traders.lua b/mods/lord/Entities/lottmobs/src/traders.lua index 7e1ef8e70..82194d71d 100644 --- a/mods/lord/Entities/lottmobs/src/traders.lua +++ b/mods/lord/Entities/lottmobs/src/traders.lua @@ -1,4 +1,3 @@ -local trader_config = dofile(minetest.get_modpath("lottmobs").."/src/trader_goods.lua") dofile(minetest.get_modpath("lottmobs").."/src/trader.lua") local common_trader_definition = { @@ -44,7 +43,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, trader_config[def.race], race_privilege) + lottmobs_trader(self, clicker, def.race, race_privilege) end mobs:register_mob(name, def)