mobs_npc/trader.lua

283 lines
7.0 KiB
Lua
Raw Normal View History

2016-06-11 11:54:30 +02:00
local S = mobs.intllib
2016-04-15 16:01:56 +02:00
mobs.human = {
-- Set the item pool size to be the amount of trades listed below
item_pool_size = 18,
2016-04-15 16:01:56 +02:00
items = {
--{item for sale, price, chance of appearing in trader's inventory}
{"default:apple 10", "default:gold_ingot 2", 10},
{"farming:bread 10", "default:gold_ingot 4", 5},
{"default:clay 10", "default:gold_ingot 2", 12},
{"default:brick 10", "default:gold_ingot 4", 17},
{"default:glass 10", "default:gold_ingot 4", 17},
{"default:obsidian 10", "default:gold_ingot 15", 50},
{"default:diamond 1", "default:gold_ingot 5", 40},
{"farming:wheat 10", "default:gold_ingot 2", 17},
{"default:tree 5", "default:gold_ingot 4", 20},
{"default:stone 10", "default:gold_ingot 8", 17},
{"default:desert_stone 10", "default:gold_ingot 8", 27},
{"default:sapling 1", "default:gold_ingot 1", 7},
{"default:pick_steel 1", "default:gold_ingot 2", 7},
{"default:sword_steel 1", "default:gold_ingot 2", 17},
{"default:shovel_steel 1", "default:gold_ingot 1", 17},
{"default:cactus 2", "default:gold_ingot 2", 40},
{"default:papyrus 2", "default:gold_ingot 2", 40},
{"default:mese_crystal_fragment 1", "default:dirt_with_grass 10", 90},
{"default:mese_crystal_fragment 1", "default:gold_ingot 5", 90},
2016-04-15 16:01:56 +02:00
},
names = {
"Bob", "Duncan", "Bill", "Tom", "James", "Ian", "Lenny",
"Dylan", "Ethan"
2016-04-15 16:01:56 +02:00
}
}
-- Trader ( same as NPC but with right-click shop )
mobs:register_mob("mobs_npc:trader", {
type = "npc",
passive = false,
damage = 3,
attack_type = "dogfight",
attacks_monsters = true,
attack_npcs = false,
2016-04-15 16:01:56 +02:00
pathfinding = false,
hp_min = 10,
hp_max = 20,
armor = 100,
collisionbox = {-0.35,-1.0,-0.35, 0.35,0.8,0.35},
visual = "mesh",
2018-07-05 22:23:44 +02:00
mesh = "mobs_character.b3d",
2016-04-15 16:01:56 +02:00
textures = {
{"mobs_trader.png"},
2016-04-15 16:01:56 +02:00
},
makes_footstep_sound = true,
sounds = {},
walk_velocity = 2,
run_velocity = 3,
jump = false,
drops = {},
water_damage = 0,
lava_damage = 4,
light_damage = 0,
follow = {"default:diamond"},
view_range = 15,
owner = "",
order = "stand",
fear_height = 3,
animation = {
speed_normal = 30,
speed_run = 30,
stand_start = 0,
stand_end = 79,
walk_start = 168,
walk_end = 187,
run_start = 168,
run_end = 187,
punch_start = 200,
punch_end = 219,
},
on_rightclick = function(self, clicker)
mobs_trader(self, clicker, entity, mobs.human)
end,
})
--This code comes almost exclusively from the trader and inventory of mobf, by Sapier.
--The copyright notice below is from mobf:
-------------------------------------------------------------------------------
-- Mob Framework Mod by Sapier
--
-- You may copy, use, modify or do nearly anything except removing this
-- copyright notice.
-- And of course you are NOT allow to pretend you have written it.
--
--! @file inventory.lua
--! @brief component containing mob inventory related functions
--! @copyright Sapier
--! @author Sapier
--! @date 2013-01-02
--
--! @defgroup Inventory Inventory subcomponent
--! @brief Component handling mob inventory
--! @ingroup framework_int
--! @{
--
-- Contact sapier a t gmx net
-------------------------------------------------------------------------------
-- This code has been heavily modified by isaiah658.
-- Trades are saved in entity metadata so they always stay the same after
-- initially being chosen. Also the formspec uses item image buttons instead of
-- inventory slots.
2016-04-15 16:01:56 +02:00
function mobs.add_goods(self, entity, race)
2016-04-15 16:01:56 +02:00
local trade_index = 1
local trades_already_added = {}
local trader_pool_size = 10
2016-04-15 16:01:56 +02:00
self.trades = {}
2016-04-15 16:01:56 +02:00
if race.item_pool_size < trader_pool_size then
trader_pool_size = race.item_pool_size
2016-04-15 16:01:56 +02:00
end
for i = 1, trader_pool_size do
2016-04-15 16:01:56 +02:00
-- If there are more trades than the amount being added, they are
-- randomly selected. If they are equal, there is no reason to randomly
-- select them
local random_trade = nil
2016-04-15 16:01:56 +02:00
if race.item_pool_size == trader_pool_size then
random_trade = i
else
while random_trade == nil do
2016-04-15 16:01:56 +02:00
local num = math.random(race.item_pool_size)
2016-04-15 16:01:56 +02:00
if trades_already_added[num] == nil then
trades_already_added[num] = true
random_trade = num
end
end
end
2016-04-15 16:01:56 +02:00
if math.random(0, 100) > race.items[random_trade][3] then
2016-04-15 16:01:56 +02:00
self.trades[trade_index] = {
race.items[random_trade][1],
race.items[random_trade][2]}
2016-04-15 16:01:56 +02:00
trade_index = trade_index + 1
end
2016-04-15 16:01:56 +02:00
end
end
function mobs_trader(self, clicker, entity, race)
2016-04-15 16:01:56 +02:00
if not self.id then
self.id = (math.random(1, 1000) * math.random(1, 10000))
.. self.name .. (math.random(1, 1000) ^ 2)
2016-04-15 16:01:56 +02:00
end
if not self.game_name then
2016-04-15 16:01:56 +02:00
self.game_name = tostring(race.names[math.random(1, #race.names)])
self.nametag = S("Trader @1", self.game_name)
2016-04-15 16:01:56 +02:00
self.object:set_properties({
nametag = self.nametag,
nametag_color = "#00FF00"
})
2016-04-15 16:01:56 +02:00
end
if self.trades == nil then
mobs.add_goods(self, entity, race)
2016-04-15 16:01:56 +02:00
end
local player = clicker:get_player_name()
2016-04-15 16:01:56 +02:00
minetest.chat_send_player(player,
S("[NPC] <Trader @1 > Hello, @2, have a look at my wares.",
self.game_name, player))
2016-04-15 16:01:56 +02:00
-- Make formspec trade list
local formspec_trade_list = ""
local x, y
2016-04-15 16:01:56 +02:00
for i = 1, 10 do
2016-04-15 16:01:56 +02:00
if self.trades[i] and self.trades[i] ~= "" then
2016-04-15 16:01:56 +02:00
if i < 6 then
x = 0.5
y = i - 0.5
2016-04-15 16:01:56 +02:00
else
x = 4.5
y = i - 5.5
2016-04-15 16:01:56 +02:00
end
formspec_trade_list = formspec_trade_list
.. "item_image_button[".. x ..",".. y ..";1,1;"
.. self.trades[i][2] .. ";prices#".. i .."#".. self.id ..";]"
.. "item_image_button[".. x + 2 ..",".. y ..";1,1;"
.. self.trades[i][1] .. ";goods#".. i .."#".. self.id ..";]"
.. "image[".. x + 1 ..",".. y ..";1,1;gui_arrow_blank.png]"
2016-04-15 16:01:56 +02:00
end
end
minetest.show_formspec(player, "mobs_npc:trade", "size[8,10]"
.. default.gui_bg_img
.. default.gui_slots
.. "label[0.5,-0.1;" .. S("Trader @1's stock:", self.game_name) .. "]"
.. formspec_trade_list
.. "list[current_player;main;0,6;8,4;]"
)
2016-04-15 16:01:56 +02:00
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
2016-04-15 16:01:56 +02:00
if formname ~= "mobs_npc:trade" then return end
2016-04-15 16:01:56 +02:00
if fields then
2016-04-15 16:01:56 +02:00
local trade = ""
2016-04-15 16:01:56 +02:00
for k, v in pairs(fields) do
trade = tostring(k)
end
2016-04-15 16:01:56 +02:00
local id = trade:split("#")[3]
local self = nil
2016-04-15 16:01:56 +02:00
if id ~= nil then
2016-04-15 16:01:56 +02:00
for k, v in pairs(minetest.luaentities) do
2016-04-15 16:01:56 +02:00
if v.object and v.id and v.id == id then
self = v
break
end
end
end
2016-04-15 16:01:56 +02:00
if self ~= nil then
2016-04-15 16:01:56 +02:00
local trade_number = tonumber(trade:split("#")[2])
2016-04-15 16:01:56 +02:00
if trade_number ~= nil and self.trades[trade_number] ~= nil then
2016-04-15 16:01:56 +02:00
local price = self.trades[trade_number][2]
local goods = self.trades[trade_number][1]
local inv = player:get_inventory()
2016-04-15 16:01:56 +02:00
if inv:contains_item("main", price) then
2016-04-15 16:01:56 +02:00
inv:remove_item("main", price)
2016-04-15 16:01:56 +02:00
local leftover = inv:add_item("main", goods)
2016-04-15 16:01:56 +02:00
if leftover:get_count() > 0 then
2016-04-15 16:01:56 +02:00
-- drop item(s) in front of player
local droppos = player:get_pos()
local dir = player:get_look_dir()
2016-04-15 16:01:56 +02:00
droppos.x = droppos.x + dir.x
droppos.z = droppos.z + dir.z
2016-04-15 16:01:56 +02:00
minetest.add_item(droppos, leftover)
2016-04-15 16:01:56 +02:00
end
end
end
end
2016-04-15 16:01:56 +02:00
end
end)
2016-04-15 16:01:56 +02:00
2016-06-11 11:54:30 +02:00
mobs:register_egg("mobs_npc:trader", S("Trader"), "default_sandstone.png", 1)
2016-04-15 16:01:56 +02:00
-- compatibility
mobs:alias_mob("mobs:trader", "mobs_npc:trader")