From 058cde14e1c4724e4e07e52171d01582e938e2dd Mon Sep 17 00:00:00 2001 From: alek13 Date: Mon, 25 Sep 2023 15:06:24 +0300 Subject: [PATCH] Mobs: Traders: Refactoring: rename `TraderDef` -> `TraderConfig` & use local. Relates to #1170 --- mods/lord/Entities/lottmobs/src/trader.lua | 2 +- .../lottmobs/src/trader_Inventory.lua | 30 +++++++++---------- .../Entities/lottmobs/src/trader_goods.lua | 25 +++++++++------- mods/lord/Entities/lottmobs/src/traders.lua | 4 +-- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/mods/lord/Entities/lottmobs/src/trader.lua b/mods/lord/Entities/lottmobs/src/trader.lua index e5104c3dc..0c8323ba3 100644 --- a/mods/lord/Entities/lottmobs/src/trader.lua +++ b/mods/lord/Entities/lottmobs/src/trader.lua @@ -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()) diff --git a/mods/lord/Entities/lottmobs/src/trader_Inventory.lua b/mods/lord/Entities/lottmobs/src/trader_Inventory.lua index c4833b694..0f174d026 100644 --- a/mods/lord/Entities/lottmobs/src/trader_Inventory.lua +++ b/mods/lord/Entities/lottmobs/src/trader_Inventory.lua @@ -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 @@ -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] @@ -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 @@ -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 @@ -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 diff --git a/mods/lord/Entities/lottmobs/src/trader_goods.lua b/mods/lord/Entities/lottmobs/src/trader_goods.lua index 92489d0cd..e058e735e 100644 --- a/mods/lord/Entities/lottmobs/src/trader_goods.lua +++ b/mods/lord/Entities/lottmobs/src/trader_goods.lua @@ -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 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 }, @@ -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 }, @@ -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 }, @@ -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 }, @@ -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", @@ -162,3 +165,5 @@ lottmobs.orc = { SL("Arrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr!, KILL! KILL! KILL!") }, } + +return trader_config diff --git a/mods/lord/Entities/lottmobs/src/traders.lua b/mods/lord/Entities/lottmobs/src/traders.lua index ea2f65c52..7e1ef8e70 100644 --- a/mods/lord/Entities/lottmobs/src/traders.lua +++ b/mods/lord/Entities/lottmobs/src/traders.lua @@ -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 = { @@ -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)