2019-01-01 16:59:24 +02:00
|
|
|
--[[
|
|
|
|
The Retailer provides a market replacement GUI with additional
|
|
|
|
functionality. To register a market, you need the market entity and a name
|
|
|
|
for the market group:
|
|
|
|
|
|
|
|
Retailer.add_market(group_name, market_entity)
|
|
|
|
|
|
|
|
If you don't have a group name or don't want to re-use it, you can generate
|
|
|
|
a group name with:
|
|
|
|
|
|
|
|
local group_name = Retailer.generate_group_id()
|
|
|
|
|
|
|
|
To change the description displayed in the market GUI, you can call:
|
|
|
|
|
|
|
|
Retailer.set_market_group_label(group_name, 'Name of your market')
|
|
|
|
|
|
|
|
To add, remove, disable and enable items, you can call their respective
|
|
|
|
functions. Note that each item can be added only once and will replace the
|
|
|
|
previous if set again. Calling any of those functions will trigger a GUI
|
|
|
|
redraw for all markets in that group in the next tick.
|
|
|
|
|
|
|
|
When an item is bought from the market, it will raise an event you can
|
|
|
|
listen to: Retailer.events.on_market_purchase. Items can be registered
|
|
|
|
with different types. By default the type is 'item', which will insert the
|
|
|
|
item(s) bought into the player's inventory. Anything else will merely
|
|
|
|
remove the coins and trigger the event. You can listen to this event and do
|
|
|
|
whatever custom handling you want.
|
|
|
|
|
|
|
|
Items support the following structure:
|
|
|
|
{
|
|
|
|
name: the (raw) item inserted in inventory, does nothing when type is not item
|
|
|
|
name_label: the name shown in the GUI. If omitted and a prototype exists for 'name', it will use that LocalisedString
|
|
|
|
sprite: a custom sprite, will use 'item/<name>' if omitted
|
|
|
|
price: the price of an item, supports floats (0.95 for example)
|
|
|
|
description: an additional description displayed in the tooltip
|
|
|
|
disabled: whether or not the item should be disabled by default
|
|
|
|
disabled_reason: the reason the item is disabled
|
|
|
|
}
|
|
|
|
]]
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
require 'utils.table'
|
2018-11-25 21:09:23 +02:00
|
|
|
local Global = require 'utils.global'
|
2018-12-07 23:58:44 +02:00
|
|
|
local Gui = require 'utils.gui'
|
|
|
|
local Event = require 'utils.event'
|
2018-12-29 15:30:20 +02:00
|
|
|
local Token = require 'utils.token'
|
2019-01-04 22:02:55 +02:00
|
|
|
local Schedule = require 'utils.task'
|
2018-12-07 23:58:44 +02:00
|
|
|
local PlayerStats = require 'features.player_stats'
|
|
|
|
local Game = require 'utils.game'
|
|
|
|
local math = require 'utils.math'
|
2018-12-29 01:17:44 +02:00
|
|
|
local Color = require 'resources.color_presets'
|
2018-12-07 23:58:44 +02:00
|
|
|
local format = string.format
|
2018-12-22 01:39:18 +02:00
|
|
|
local size = table.size
|
|
|
|
local insert = table.insert
|
|
|
|
local pairs = pairs
|
|
|
|
local tonumber = tonumber
|
2018-12-29 15:30:20 +02:00
|
|
|
local set_timeout_in_ticks = Schedule.set_timeout_in_ticks
|
2018-12-07 23:58:44 +02:00
|
|
|
local clamp = math.clamp
|
|
|
|
local floor = math.floor
|
|
|
|
local ceil = math.ceil
|
2018-12-27 01:42:06 +02:00
|
|
|
local raise_event = script.raise_event
|
2018-12-07 23:58:44 +02:00
|
|
|
local market_frame_name = Gui.uid_name()
|
2018-12-22 01:39:18 +02:00
|
|
|
local market_frame_close_button_name = Gui.uid_name()
|
2018-12-07 23:58:44 +02:00
|
|
|
local item_button_name = Gui.uid_name()
|
|
|
|
local count_slider_name = Gui.uid_name()
|
|
|
|
local count_text_name = Gui.uid_name()
|
2018-11-25 21:09:23 +02:00
|
|
|
|
|
|
|
local Retailer = {}
|
|
|
|
|
2018-12-27 01:42:06 +02:00
|
|
|
Retailer.events = {
|
|
|
|
--- Triggered when a purchase is made
|
|
|
|
-- Event {
|
|
|
|
-- item = item,
|
|
|
|
-- count = count,
|
|
|
|
-- player = player,
|
|
|
|
-- group_name = group_name,
|
|
|
|
-- }
|
|
|
|
on_market_purchase = script.generate_event_name(),
|
|
|
|
}
|
|
|
|
|
2019-01-16 23:05:12 +02:00
|
|
|
Retailer.item_types = {
|
|
|
|
--- expects an array of item prototypes that can be inserted directly via
|
|
|
|
--- player.insert() called 'items' in the item prototype.
|
|
|
|
item_package = 'item_package',
|
|
|
|
}
|
|
|
|
|
2018-12-29 15:30:20 +02:00
|
|
|
local market_gui_close_distance_squared = 6 * 6 + 6 * 6
|
|
|
|
local do_update_market_gui -- token
|
|
|
|
|
2018-11-25 21:09:23 +02:00
|
|
|
---Global storage
|
2018-12-07 23:58:44 +02:00
|
|
|
---Markets are indexed by the position "x,y" and contains the group it belongs to
|
2018-11-25 21:09:23 +02:00
|
|
|
---Items are indexed by the group name and is a list indexed by the item name and contains the prices per item
|
2018-12-29 15:30:20 +02:00
|
|
|
---players_in_market_view is a list of {position, group_name} data
|
2018-11-25 21:09:23 +02:00
|
|
|
local memory = {
|
2018-12-23 14:33:54 +02:00
|
|
|
id = 0,
|
2018-11-25 21:09:23 +02:00
|
|
|
markets = {},
|
|
|
|
items = {},
|
2018-12-22 01:39:18 +02:00
|
|
|
group_label = {},
|
2018-12-29 14:16:41 +02:00
|
|
|
players_in_market_view = {},
|
2018-12-29 15:30:20 +02:00
|
|
|
market_gui_refresh_scheduled = {},
|
2019-01-18 21:32:48 +02:00
|
|
|
limited_items = {},
|
2018-11-25 21:09:23 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 21:43:50 +02:00
|
|
|
Global.register(memory, function (tbl)
|
|
|
|
memory = tbl
|
2018-11-25 21:09:23 +02:00
|
|
|
end)
|
|
|
|
|
2018-12-29 15:30:20 +02:00
|
|
|
local function schedule_market_gui_refresh(group_name)
|
|
|
|
if memory.market_gui_refresh_scheduled[group_name] then
|
|
|
|
-- already scheduled
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
set_timeout_in_ticks(1, do_update_market_gui, {group_name = group_name})
|
|
|
|
memory.market_gui_refresh_scheduled[group_name] = true
|
|
|
|
end
|
|
|
|
|
2018-12-23 14:33:54 +02:00
|
|
|
---Generates a unique identifier for a market group name, as alternative for a custom name.
|
|
|
|
function Retailer.generate_group_id()
|
|
|
|
local id = memory.id + 1
|
|
|
|
memory.id = id
|
|
|
|
return 'market-' .. id
|
|
|
|
end
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
---Sets the name of the market group, provides a user friendly label in the GUI.
|
|
|
|
---@param group_name string
|
|
|
|
---@param label string
|
|
|
|
function Retailer.set_market_group_label(group_name, label)
|
|
|
|
memory.group_label[group_name] = label
|
2018-12-29 15:30:20 +02:00
|
|
|
|
|
|
|
schedule_market_gui_refresh(group_name)
|
2018-12-22 01:39:18 +02:00
|
|
|
end
|
|
|
|
|
2018-12-27 01:42:06 +02:00
|
|
|
---Gets the name of the market group.
|
|
|
|
---@param group_name string
|
|
|
|
function Retailer.get_market_group_label(group_name)
|
|
|
|
return memory.group_label[group_name] or 'Market'
|
|
|
|
end
|
|
|
|
|
2018-12-07 23:58:44 +02:00
|
|
|
---Returns all item for the group_name retailer.
|
2019-01-18 21:32:48 +02:00
|
|
|
---@param market_group string
|
|
|
|
function Retailer.get_items(market_group)
|
|
|
|
return memory.items[market_group] or {}
|
2018-11-25 21:09:23 +02:00
|
|
|
end
|
|
|
|
|
2018-12-07 23:58:44 +02:00
|
|
|
---Removes an item from the markets for the group_name retailer.
|
2018-11-25 21:09:23 +02:00
|
|
|
---@param group_name string
|
|
|
|
---@param item_name string
|
2018-12-07 23:58:44 +02:00
|
|
|
function Retailer.remove_item(group_name, item_name)
|
2018-11-25 21:09:23 +02:00
|
|
|
if not memory.items[group_name] then
|
2018-12-07 23:58:44 +02:00
|
|
|
return
|
2018-11-25 21:09:23 +02:00
|
|
|
end
|
|
|
|
|
2018-12-07 23:58:44 +02:00
|
|
|
memory.items[group_name][item_name] = nil
|
2018-12-29 15:30:20 +02:00
|
|
|
|
|
|
|
schedule_market_gui_refresh(group_name)
|
2018-12-07 23:58:44 +02:00
|
|
|
end
|
|
|
|
|
2019-01-18 21:32:48 +02:00
|
|
|
---Returns the remaining market group item limit or -1 if there is none for a given player.
|
|
|
|
---@param market_group string
|
|
|
|
---@param item_name string
|
|
|
|
---@param player_index number
|
|
|
|
function Retailer.get_player_item_limit(market_group, item_name, player_index)
|
|
|
|
local item = Retailer.get_items(market_group)[item_name]
|
|
|
|
|
|
|
|
if not item then
|
|
|
|
Debug.print({message = 'Item not registered in the Retailer', data = {
|
|
|
|
market_group = market_group,
|
|
|
|
item_name = item_name,
|
|
|
|
}})
|
|
|
|
return -1
|
|
|
|
end
|
|
|
|
|
|
|
|
return memory.limited_items[market_group][item_name][player_index] or item.player_limit
|
|
|
|
end
|
|
|
|
|
|
|
|
---Returns the configured market group item limit or -1 if there is none.
|
|
|
|
---@param market_group string
|
|
|
|
---@param item_name string
|
|
|
|
function Retailer.get_item_limit(market_group, item_name)
|
|
|
|
local item = Retailer.get_items(market_group)[item_name]
|
|
|
|
|
|
|
|
if not item then
|
|
|
|
Debug.print({message = 'Item not registered in the Retailer', data = {
|
|
|
|
market_group = market_group,
|
|
|
|
item_name = item_name,
|
|
|
|
}})
|
|
|
|
return -1
|
|
|
|
end
|
|
|
|
|
|
|
|
return item.player_limit
|
|
|
|
end
|
|
|
|
|
|
|
|
---sets the configured market group item limit for a given player
|
|
|
|
---@param market_group string
|
|
|
|
---@param item_name string
|
|
|
|
---@param player_index number
|
|
|
|
---@param new_limit number
|
|
|
|
function Retailer.set_player_item_limit(market_group, item_name, player_index, new_limit)
|
|
|
|
if new_limit < 0 then
|
|
|
|
Debug.print({message = 'Cannot set a negative item limit', data = {
|
|
|
|
market_group = market_group,
|
|
|
|
item_name = item_name,
|
|
|
|
new_limit = new_limit,
|
|
|
|
}})
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local item = Retailer.get_items(market_group)[item_name]
|
|
|
|
|
|
|
|
if not item then
|
|
|
|
Debug.print({message = 'Item not registered in the Retailer', data = {
|
|
|
|
market_group = market_group,
|
|
|
|
item_name = item_name,
|
|
|
|
}})
|
|
|
|
return -1
|
|
|
|
end
|
|
|
|
|
|
|
|
if new_limit > item.player_limit then
|
|
|
|
Debug.print({message = 'Cannot set an item limit higher than the item prototype defined', data = {
|
|
|
|
market_group = market_group,
|
|
|
|
item_name = item_name,
|
|
|
|
new_limit = new_limit,
|
|
|
|
}})
|
|
|
|
new_limit = item.player_limit
|
|
|
|
end
|
|
|
|
memory.limited_items[market_group][item_name][player_index] = new_limit
|
|
|
|
end
|
|
|
|
|
2018-12-07 23:58:44 +02:00
|
|
|
local function redraw_market_items(data)
|
|
|
|
local grid = data.grid
|
|
|
|
|
|
|
|
Gui.clear(grid)
|
|
|
|
|
|
|
|
local count = data.count
|
|
|
|
local market_items = data.market_items
|
2019-01-16 23:05:12 +02:00
|
|
|
local player_index = data.player_index
|
2019-01-17 22:49:45 +02:00
|
|
|
local player_coins = Game.get_player_by_index(player_index).get_item_count('coin')
|
2018-12-22 01:39:18 +02:00
|
|
|
|
|
|
|
if size(market_items) == 0 then
|
|
|
|
grid.add({type = 'label', caption = 'No items available at this time'})
|
|
|
|
return
|
|
|
|
end
|
2018-12-07 23:58:44 +02:00
|
|
|
|
2019-01-18 21:32:48 +02:00
|
|
|
local limited_items = memory.limited_items[data.market_group]
|
|
|
|
|
2018-12-07 23:58:44 +02:00
|
|
|
for i, item in pairs(market_items) do
|
2019-01-16 23:05:12 +02:00
|
|
|
local has_stack_limit = item.stack_limit ~= -1
|
|
|
|
local stack_limit = has_stack_limit and item.stack_limit or count
|
|
|
|
local stack_count = has_stack_limit and stack_limit < count and item.stack_limit or count
|
|
|
|
local player_limit = item.player_limit
|
|
|
|
local has_player_limit = player_limit ~= -1
|
|
|
|
|
|
|
|
if has_player_limit then
|
|
|
|
local item_name = item.name
|
2019-01-17 22:49:45 +02:00
|
|
|
player_limit = limited_items[item_name][player_index]
|
2019-01-16 23:05:12 +02:00
|
|
|
|
2019-01-17 22:49:45 +02:00
|
|
|
if player_limit == nil then
|
2019-01-16 23:05:12 +02:00
|
|
|
-- no limit set yet
|
2019-01-17 22:49:45 +02:00
|
|
|
player_limit = item.player_limit
|
|
|
|
limited_items[item_name][player_index] = item.player_limit
|
2019-01-16 23:05:12 +02:00
|
|
|
end
|
|
|
|
|
2019-01-17 22:49:45 +02:00
|
|
|
if player_limit < stack_count then
|
2019-01-16 23:05:12 +02:00
|
|
|
-- ensure the stack count is never higher than the item limit for the player
|
2019-01-17 22:49:45 +02:00
|
|
|
stack_count = player_limit
|
2019-01-16 23:05:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local player_bought_max_total = has_player_limit and stack_count == 0
|
2018-12-07 23:58:44 +02:00
|
|
|
local price = item.price
|
2019-01-16 23:05:12 +02:00
|
|
|
local tooltip = {'', item.name_label}
|
2018-12-22 01:39:18 +02:00
|
|
|
local description = item.description
|
2018-12-31 21:43:50 +02:00
|
|
|
local total_price = ceil(price * stack_count)
|
2018-12-28 22:31:02 +02:00
|
|
|
local disabled = item.disabled == true
|
2018-12-22 01:39:18 +02:00
|
|
|
local message
|
2019-01-16 23:05:12 +02:00
|
|
|
|
|
|
|
if total_price == 0 then
|
|
|
|
message = 'FREE!'
|
|
|
|
elseif total_price == 1 then
|
2018-12-22 01:39:18 +02:00
|
|
|
message = '1 coin'
|
|
|
|
else
|
|
|
|
message = total_price .. ' coins'
|
|
|
|
end
|
|
|
|
|
|
|
|
local missing_coins = total_price - player_coins
|
|
|
|
local is_missing_coins = missing_coins > 0
|
2018-12-07 23:58:44 +02:00
|
|
|
|
2019-01-16 23:05:12 +02:00
|
|
|
if price ~= 0 then
|
|
|
|
insert(tooltip, format('\nprice: %.2f', price))
|
|
|
|
end
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
if description then
|
2019-01-25 02:13:25 +02:00
|
|
|
insert(tooltip, '\n')
|
|
|
|
insert(tooltip, item.description)
|
2018-12-22 01:39:18 +02:00
|
|
|
end
|
|
|
|
|
2018-12-28 22:31:02 +02:00
|
|
|
if disabled then
|
|
|
|
insert(tooltip, '\n\n' .. (item.disabled_reason or 'Not available'))
|
|
|
|
elseif is_missing_coins then
|
2018-12-31 21:43:50 +02:00
|
|
|
insert(tooltip, '\n\n' .. format('Missing %d coins to buy %d', missing_coins, stack_count))
|
2018-12-22 01:39:18 +02:00
|
|
|
end
|
2018-12-07 23:58:44 +02:00
|
|
|
|
2019-01-16 23:05:12 +02:00
|
|
|
if has_player_limit then
|
2019-01-17 22:49:45 +02:00
|
|
|
insert(tooltip, '\n\n' .. format('You have bought this item %d out of %d times', item.player_limit - player_limit, item.player_limit))
|
2019-01-16 23:05:12 +02:00
|
|
|
end
|
|
|
|
|
2018-12-07 23:58:44 +02:00
|
|
|
local button = grid.add({type = 'flow'}).add({
|
|
|
|
type = 'sprite-button',
|
|
|
|
name = item_button_name,
|
2018-12-22 01:39:18 +02:00
|
|
|
sprite = item.sprite,
|
2018-12-31 21:43:50 +02:00
|
|
|
number = stack_count,
|
2018-12-22 01:39:18 +02:00
|
|
|
tooltip = tooltip,
|
2018-12-07 23:58:44 +02:00
|
|
|
})
|
|
|
|
button.style = 'slot_button'
|
|
|
|
|
2018-12-31 21:43:50 +02:00
|
|
|
Gui.set_data(button, {index = i, data = data, stack_count = stack_count})
|
2018-12-07 23:58:44 +02:00
|
|
|
|
|
|
|
local label = grid.add({type = 'label', caption = message})
|
|
|
|
local label_style = label.style
|
2018-12-22 01:39:18 +02:00
|
|
|
label_style.width = 93
|
2018-12-27 01:42:06 +02:00
|
|
|
label_style.height = 32
|
2018-12-07 23:58:44 +02:00
|
|
|
label_style.font = 'default-bold'
|
2018-12-27 01:42:06 +02:00
|
|
|
label_style.vertical_align = 'center'
|
2018-12-22 01:39:18 +02:00
|
|
|
|
2019-01-16 23:05:12 +02:00
|
|
|
if disabled or player_bought_max_total then
|
2018-12-29 01:17:44 +02:00
|
|
|
label_style.font_color = Color.dark_grey
|
2018-12-28 22:31:02 +02:00
|
|
|
button.enabled = false
|
|
|
|
elseif is_missing_coins then
|
2018-12-29 01:17:44 +02:00
|
|
|
label_style.font_color = Color.red
|
2018-12-22 01:39:18 +02:00
|
|
|
button.enabled = false
|
|
|
|
end
|
2018-11-25 21:09:23 +02:00
|
|
|
end
|
2018-12-07 23:58:44 +02:00
|
|
|
end
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
local function do_coin_label(coin_count, label)
|
2018-12-07 23:58:44 +02:00
|
|
|
if coin_count == 1 then
|
2018-12-22 01:39:18 +02:00
|
|
|
label.caption = '1 coin available'
|
2018-12-07 23:58:44 +02:00
|
|
|
else
|
|
|
|
label.caption = coin_count .. ' coins available'
|
|
|
|
end
|
2018-12-22 01:39:18 +02:00
|
|
|
label.style.font = 'default-bold'
|
2018-11-25 21:09:23 +02:00
|
|
|
end
|
|
|
|
|
2018-12-07 23:58:44 +02:00
|
|
|
local function draw_market_frame(player, group_name)
|
|
|
|
local frame = player.gui.center.add({
|
|
|
|
type = 'frame',
|
|
|
|
name = market_frame_name,
|
2018-12-27 01:42:06 +02:00
|
|
|
caption = Retailer.get_market_group_label(group_name),
|
2018-12-07 23:58:44 +02:00
|
|
|
direction = 'vertical',
|
|
|
|
})
|
|
|
|
|
|
|
|
local scroll_pane = frame.add({type = 'scroll-pane'})
|
|
|
|
local scroll_style = scroll_pane.style
|
|
|
|
scroll_style.maximal_height = 600
|
|
|
|
|
|
|
|
local grid = scroll_pane.add({type = 'table', column_count = 10})
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
local market_items = Retailer.get_items(group_name)
|
|
|
|
local player_coins = player.get_item_count('coin')
|
2018-12-27 01:42:06 +02:00
|
|
|
local data = {
|
|
|
|
grid = grid,
|
|
|
|
count = 1,
|
|
|
|
market_items = market_items,
|
|
|
|
market_group = group_name,
|
2019-01-16 23:05:12 +02:00
|
|
|
player_index = player.index,
|
2018-12-27 01:42:06 +02:00
|
|
|
}
|
2018-12-07 23:58:44 +02:00
|
|
|
|
|
|
|
local coin_label = frame.add({type = 'label'})
|
2018-12-22 01:39:18 +02:00
|
|
|
do_coin_label(player_coins, coin_label)
|
2018-12-07 23:58:44 +02:00
|
|
|
data.coin_label = coin_label
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
redraw_market_items(data)
|
|
|
|
|
|
|
|
local bottom_grid = frame.add({type = 'table', column_count = 2})
|
|
|
|
|
|
|
|
bottom_grid.add({type = 'label', caption = 'Quantity: '}).style.font = 'default-bold'
|
|
|
|
|
|
|
|
local count_text = bottom_grid.add({
|
|
|
|
type = 'text-box',
|
|
|
|
name = count_text_name,
|
|
|
|
text = '1',
|
|
|
|
})
|
2018-12-07 23:58:44 +02:00
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
local count_slider = frame.add({
|
2018-12-07 23:58:44 +02:00
|
|
|
type = 'slider',
|
|
|
|
name = count_slider_name,
|
|
|
|
minimum_value = 1,
|
|
|
|
maximum_value = 7,
|
|
|
|
value = 1,
|
|
|
|
})
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
frame.add({name = market_frame_close_button_name, type = 'button', caption = 'Close'})
|
2018-12-07 23:58:44 +02:00
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
count_slider.style.width = 115
|
|
|
|
count_text.style.width = 45
|
2018-12-07 23:58:44 +02:00
|
|
|
|
|
|
|
data.slider = count_slider
|
|
|
|
data.text = count_text
|
|
|
|
|
|
|
|
Gui.set_data(count_slider, data)
|
|
|
|
Gui.set_data(count_text, data)
|
2018-12-29 15:30:20 +02:00
|
|
|
Gui.set_data(frame, data)
|
2018-12-07 23:58:44 +02:00
|
|
|
|
|
|
|
return frame
|
2018-11-25 21:09:23 +02:00
|
|
|
end
|
|
|
|
|
2018-12-29 15:30:20 +02:00
|
|
|
---Returns the group name of the market at the given position, nil if not registered.
|
|
|
|
---@param position Position
|
|
|
|
local function get_market_group_name(position)
|
|
|
|
return memory.markets[position.x .. ',' .. position.y]
|
|
|
|
end
|
|
|
|
|
|
|
|
---Sets the group name for a market at a given position.
|
|
|
|
---@param position Position
|
|
|
|
---@param group_name string
|
|
|
|
local function set_market_group_name(position, group_name)
|
|
|
|
memory.markets[position.x .. ',' .. position.y] = group_name
|
|
|
|
end
|
|
|
|
|
2018-12-07 23:58:44 +02:00
|
|
|
Event.add(defines.events.on_gui_opened, function (event)
|
|
|
|
if not event.gui_type == defines.gui_type.entity then
|
2018-11-25 21:09:23 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-12-07 23:58:44 +02:00
|
|
|
local entity = event.entity
|
|
|
|
if not entity or not entity.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local position = entity.position
|
2018-12-29 15:30:20 +02:00
|
|
|
local group_name = get_market_group_name(position)
|
2018-12-07 23:58:44 +02:00
|
|
|
if not group_name then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-12-29 14:16:41 +02:00
|
|
|
local player = Game.get_player_by_index(event.player_index)
|
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-12-29 15:30:20 +02:00
|
|
|
memory.players_in_market_view[player.index] = {
|
|
|
|
position = position,
|
|
|
|
group_name = group_name,
|
|
|
|
}
|
2018-12-07 23:58:44 +02:00
|
|
|
local frame = draw_market_frame(player, group_name)
|
|
|
|
|
|
|
|
player.opened = frame
|
|
|
|
end)
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
Gui.on_custom_close(market_frame_name, function (event)
|
2018-12-07 23:58:44 +02:00
|
|
|
local element = event.element
|
2018-12-29 14:16:41 +02:00
|
|
|
memory.players_in_market_view[event.player.index] = nil
|
2018-12-07 23:58:44 +02:00
|
|
|
Gui.destroy(element)
|
|
|
|
end)
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
local function close_market_gui(player)
|
2018-12-07 23:58:44 +02:00
|
|
|
local element = player.gui.center
|
2018-12-29 14:16:41 +02:00
|
|
|
memory.players_in_market_view[player.index] = nil
|
2018-12-07 23:58:44 +02:00
|
|
|
|
|
|
|
if element and element.valid then
|
|
|
|
element = element[market_frame_name]
|
|
|
|
if element and element.valid then
|
|
|
|
Gui.destroy(element)
|
|
|
|
end
|
|
|
|
end
|
2018-12-22 01:39:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
Gui.on_click(market_frame_close_button_name, function (event)
|
|
|
|
close_market_gui(event.player)
|
2018-12-07 23:58:44 +02:00
|
|
|
end)
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
Event.add(defines.events.on_player_died, function (event)
|
|
|
|
local player = Game.get_player_by_index(event.player_index or 0)
|
|
|
|
|
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
close_market_gui(player)
|
|
|
|
end)
|
|
|
|
|
|
|
|
Gui.on_value_changed(count_slider_name, function (event)
|
2018-12-07 23:58:44 +02:00
|
|
|
local element = event.element
|
|
|
|
local data = Gui.get_data(element)
|
|
|
|
|
|
|
|
local value = floor(element.slider_value)
|
|
|
|
local count
|
|
|
|
if value % 2 == 0 then
|
|
|
|
count = 10 ^ (value * 0.5) * 0.5
|
|
|
|
else
|
|
|
|
count = 10 ^ ((value - 1) * 0.5)
|
|
|
|
end
|
|
|
|
|
|
|
|
data.count = count
|
|
|
|
data.text.text = count
|
|
|
|
|
|
|
|
redraw_market_items(data)
|
|
|
|
end)
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
Gui.on_text_changed(count_text_name, function (event)
|
2018-12-07 23:58:44 +02:00
|
|
|
local element = event.element
|
|
|
|
local data = Gui.get_data(element)
|
|
|
|
|
|
|
|
local count = tonumber(element.text)
|
|
|
|
|
|
|
|
if count then
|
|
|
|
count = floor(count)
|
|
|
|
count = clamp(count, 1, 1000)
|
|
|
|
data.count = count
|
|
|
|
data.text.text = count
|
|
|
|
else
|
|
|
|
data.count = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
redraw_market_items(data)
|
|
|
|
end)
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
Gui.on_click(item_button_name, function (event)
|
2018-12-07 23:58:44 +02:00
|
|
|
local player = event.player
|
|
|
|
local element = event.element
|
|
|
|
local button_data = Gui.get_data(element)
|
|
|
|
local data = button_data.data
|
2018-12-31 21:43:50 +02:00
|
|
|
local stack_count = button_data.stack_count
|
2018-12-07 23:58:44 +02:00
|
|
|
|
|
|
|
local item = data.market_items[button_data.index]
|
|
|
|
|
2018-12-29 15:30:20 +02:00
|
|
|
if not item then
|
|
|
|
player.print('This item is no longer available in the market')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-12-28 22:31:02 +02:00
|
|
|
if item.disabled then
|
|
|
|
player.print({'', item.name_label, ' is disabled. ', item.disabled_reason or ''})
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-12-07 23:58:44 +02:00
|
|
|
local name = item.name
|
|
|
|
local price = item.price
|
|
|
|
|
2018-12-31 21:43:50 +02:00
|
|
|
local cost = ceil(price * stack_count)
|
2018-12-07 23:58:44 +02:00
|
|
|
local coin_count = player.get_item_count('coin')
|
|
|
|
|
|
|
|
if cost > coin_count then
|
|
|
|
player.print('Insufficient coins')
|
2018-11-25 21:09:23 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-01-17 22:49:45 +02:00
|
|
|
local market_group = data.market_group
|
2019-01-16 23:05:12 +02:00
|
|
|
if item.player_limit ~= -1 then
|
2019-01-17 22:49:45 +02:00
|
|
|
local limited_item = memory.limited_items[market_group][name]
|
2019-01-16 23:05:12 +02:00
|
|
|
limited_item[player.index] = limited_item[player.index] - stack_count
|
|
|
|
end
|
|
|
|
|
2018-12-27 01:42:06 +02:00
|
|
|
if item.type == 'item' then
|
2018-12-31 21:43:50 +02:00
|
|
|
local inserted = player.insert({name = name, count = stack_count})
|
|
|
|
if inserted < stack_count then
|
2018-12-27 01:42:06 +02:00
|
|
|
player.print('Insufficient inventory space')
|
|
|
|
if inserted > 0 then
|
|
|
|
player.remove_item({name = name, count = inserted})
|
|
|
|
end
|
|
|
|
return
|
2018-11-25 21:09:23 +02:00
|
|
|
end
|
2018-12-07 23:58:44 +02:00
|
|
|
end
|
|
|
|
|
2019-01-16 23:05:12 +02:00
|
|
|
if cost > 0 then
|
|
|
|
player.remove_item({name = 'coin', count = cost})
|
|
|
|
end
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
redraw_market_items(data)
|
2018-12-07 23:58:44 +02:00
|
|
|
PlayerStats.change_coin_spent(player.index, cost)
|
2019-02-02 14:10:30 +02:00
|
|
|
do_coin_label(coin_count - cost, data.coin_label)
|
2018-12-27 01:42:06 +02:00
|
|
|
|
|
|
|
raise_event(Retailer.events.on_market_purchase, {
|
|
|
|
item = item,
|
2018-12-31 21:43:50 +02:00
|
|
|
count = stack_count,
|
2018-12-27 01:42:06 +02:00
|
|
|
player = player,
|
2019-01-17 22:49:45 +02:00
|
|
|
group_name = market_group,
|
2018-12-27 01:42:06 +02:00
|
|
|
})
|
2018-12-07 23:58:44 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
---Add a market to the group_name retailer.
|
|
|
|
---@param group_name string
|
|
|
|
---@param market_entity LuaEntity
|
|
|
|
function Retailer.add_market(group_name, market_entity)
|
2018-12-29 15:30:20 +02:00
|
|
|
set_market_group_name(market_entity.position, group_name)
|
2018-12-07 23:58:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
---Sets an item for all the group_name markets.
|
|
|
|
---@param group_name string
|
|
|
|
---@param prototype table with item name and price
|
|
|
|
function Retailer.set_item(group_name, prototype)
|
|
|
|
if not memory.items[group_name] then
|
|
|
|
memory.items[group_name] = {}
|
2018-11-25 21:09:23 +02:00
|
|
|
end
|
2019-01-17 22:49:45 +02:00
|
|
|
if not memory.limited_items[group_name] then
|
|
|
|
memory.limited_items[group_name] = {}
|
|
|
|
end
|
2018-12-07 23:58:44 +02:00
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
local item_name = prototype.name
|
|
|
|
local name_label = prototype.name_label
|
|
|
|
|
|
|
|
if not name_label then
|
|
|
|
local item_prototype = game.item_prototypes[item_name]
|
|
|
|
name_label = item_prototype and item_prototype.localised_name
|
|
|
|
end
|
|
|
|
|
|
|
|
prototype.name_label = name_label or item_name
|
|
|
|
prototype.sprite = prototype.sprite or 'item/' .. item_name
|
2018-12-27 01:42:06 +02:00
|
|
|
prototype.type = prototype.type or 'item'
|
2018-12-22 01:39:18 +02:00
|
|
|
|
2018-12-31 21:43:50 +02:00
|
|
|
if not prototype.stack_limit then
|
|
|
|
prototype.stack_limit = -1
|
|
|
|
end
|
|
|
|
|
2019-01-16 23:05:12 +02:00
|
|
|
if not prototype.player_limit then
|
|
|
|
prototype.player_limit = -1
|
|
|
|
end
|
|
|
|
|
2018-12-22 01:39:18 +02:00
|
|
|
memory.items[group_name][item_name] = prototype
|
2019-01-18 21:32:48 +02:00
|
|
|
memory.limited_items[group_name][item_name] = {}
|
2018-11-25 21:09:23 +02:00
|
|
|
|
2018-12-29 15:30:20 +02:00
|
|
|
schedule_market_gui_refresh(group_name)
|
2018-12-29 14:16:41 +02:00
|
|
|
end
|
|
|
|
|
2018-12-28 22:31:02 +02:00
|
|
|
---Enables a market item by group name and item name if it's registered.
|
|
|
|
---@param group_name string
|
|
|
|
---@param item_name string
|
|
|
|
function Retailer.enable_item(group_name, item_name)
|
|
|
|
if not memory.items[group_name] then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local prototype = memory.items[group_name][item_name]
|
|
|
|
|
|
|
|
if not prototype then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
prototype.disabled = false
|
|
|
|
prototype.disabled_reason = false
|
2018-12-29 15:30:20 +02:00
|
|
|
|
|
|
|
schedule_market_gui_refresh(group_name)
|
2018-12-28 22:31:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
---Disables a market item by group name and item name if it's registered.
|
|
|
|
---@param group_name string
|
|
|
|
---@param item_name string
|
|
|
|
---@param disabled_reason string
|
|
|
|
function Retailer.disable_item(group_name, item_name, disabled_reason)
|
|
|
|
if not memory.items[group_name] then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local prototype = memory.items[group_name][item_name]
|
|
|
|
|
|
|
|
if not prototype then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
prototype.disabled = true
|
|
|
|
prototype.disabled_reason = disabled_reason
|
2018-12-29 15:30:20 +02:00
|
|
|
|
|
|
|
schedule_market_gui_refresh(group_name)
|
2018-12-28 22:31:02 +02:00
|
|
|
end
|
|
|
|
|
2018-12-29 15:30:20 +02:00
|
|
|
do_update_market_gui = Token.register(function(params)
|
|
|
|
local group_name = params.group_name
|
|
|
|
|
|
|
|
for player_index, view_data in pairs(memory.players_in_market_view) do
|
|
|
|
if group_name == view_data.group_name then
|
|
|
|
local player = Game.get_player_by_index(player_index)
|
|
|
|
if player and player.valid then
|
|
|
|
local frame = player.gui.center[market_frame_name]
|
|
|
|
if not frame or not frame.valid then
|
|
|
|
-- player already closed the market GUI and somehow this was not reported
|
|
|
|
memory.players_in_market_view[player_index] = nil
|
|
|
|
else
|
|
|
|
redraw_market_items(Gui.get_data(frame))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- player is no longer in the game, remove it from the market view
|
|
|
|
memory.players_in_market_view[player_index] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- mark it as updated
|
|
|
|
memory.market_gui_refresh_scheduled[group_name] = nil
|
|
|
|
end)
|
|
|
|
|
|
|
|
Event.on_nth_tick(37, function()
|
|
|
|
for player_index, view_data in pairs(memory.players_in_market_view) do
|
|
|
|
local player = Game.get_player_by_index(player_index)
|
|
|
|
if player and player.valid then
|
|
|
|
local player_position = player.position
|
|
|
|
local market_position = view_data.position
|
|
|
|
local delta_x = player_position.x - market_position.x
|
|
|
|
local delta_y = player_position.y - market_position.y
|
|
|
|
|
|
|
|
if delta_x * delta_x + delta_y * delta_y > market_gui_close_distance_squared then
|
|
|
|
close_market_gui(player)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- player is no longer in the game, remove it from the market view
|
|
|
|
memory.players_in_market_view[player_index] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2019-01-16 23:05:12 +02:00
|
|
|
Event.add(Retailer.events.on_market_purchase, function (event)
|
|
|
|
local package = event.item
|
|
|
|
if package.type ~= Retailer.item_types.item_package then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local player_insert = event.player.insert
|
|
|
|
for _, item in pairs(package.items) do
|
|
|
|
item.count = item.count * event.count
|
|
|
|
player_insert(item)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2018-11-25 21:09:23 +02:00
|
|
|
return Retailer
|