2018-10-06 15:53:28 +02:00
|
|
|
--[[-- info
|
|
|
|
Provides the ability to purchase items from the market.
|
|
|
|
]]
|
|
|
|
|
|
|
|
-- dependencies
|
|
|
|
local Event = require 'utils.event'
|
|
|
|
local Token = require 'utils.global_token'
|
|
|
|
local Task = require 'utils.Task'
|
|
|
|
local Debug = require 'map_gen.Diggy.Debug'
|
|
|
|
local Template = require 'map_gen.Diggy.Template'
|
|
|
|
|
|
|
|
-- this
|
|
|
|
local MarketExchange = {}
|
|
|
|
|
|
|
|
global.MarketExchange = {
|
|
|
|
stone_sent_to_surface = 0,
|
|
|
|
}
|
|
|
|
|
2018-10-07 17:37:29 +02:00
|
|
|
local on_init;
|
|
|
|
|
2018-10-06 15:53:28 +02:00
|
|
|
--[[--
|
|
|
|
Registers all event handlers.
|
|
|
|
]]
|
2018-10-07 17:05:59 +02:00
|
|
|
function MarketExchange.register(config)
|
2018-10-06 15:53:28 +02:00
|
|
|
local market_items = {
|
|
|
|
{price = {{config.currency_item, 50}}, offer = {type = 'nothing', effect_description = 'Send ' .. config.stone_to_surface_amount .. ' stone to the surface'}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item_prototype in pairs(config.market_inventory) do
|
|
|
|
local bulk = item_prototype.bulk or {1}
|
|
|
|
for _, bulk_amount in pairs(bulk) do
|
|
|
|
table.insert(market_items, {
|
|
|
|
price = {{config.currency_item, item_prototype.price * bulk_amount}},
|
|
|
|
offer = {type = 'give-item', item = item_prototype.item_name, count = bulk_amount}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local on_market_timeout_finished = Token.register(function(params)
|
2018-10-07 17:37:29 +02:00
|
|
|
Template.market(params.surface, params.position, params.player_force, params.currency_item, params.market_items)
|
2018-10-07 13:50:25 +02:00
|
|
|
end)
|
|
|
|
|
2018-10-07 17:37:29 +02:00
|
|
|
on_init = function()
|
2018-10-07 17:05:59 +02:00
|
|
|
Task.set_timeout_in_ticks(60, on_market_timeout_finished, {
|
2018-10-07 17:37:29 +02:00
|
|
|
surface = game.surfaces.nauvis,
|
|
|
|
position = {x = 0, y = -5},
|
|
|
|
player_force = game.forces.player,
|
2018-10-07 13:50:25 +02:00
|
|
|
currency_item = config.currency_item,
|
|
|
|
market_items = market_items,
|
|
|
|
})
|
2018-10-07 17:37:29 +02:00
|
|
|
end
|
2018-10-06 15:53:28 +02:00
|
|
|
|
2018-10-07 13:50:25 +02:00
|
|
|
|
|
|
|
Event.add(defines.events.on_market_item_purchased, function (event)
|
|
|
|
if (1 ~= event.offer_index) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
global.MarketExchange.stone_sent_to_surface = global.MarketExchange.stone_sent_to_surface + config.stone_to_surface_amount
|
|
|
|
end)
|
2018-10-06 15:53:28 +02:00
|
|
|
end
|
|
|
|
|
2018-10-07 17:05:59 +02:00
|
|
|
function MarketExchange.get_extra_map_info(config)
|
|
|
|
return 'Market Exchange, trade your stone or send it to the surface'
|
|
|
|
end
|
|
|
|
|
2018-10-07 17:37:29 +02:00
|
|
|
function MarketExchange.on_init()
|
|
|
|
if ('function' ~= type(on_init)) then
|
|
|
|
error('Expected local on_init in MarketExchange to have a function assigned.')
|
|
|
|
end
|
|
|
|
|
|
|
|
on_init()
|
|
|
|
end
|
|
|
|
|
2018-10-06 15:53:28 +02:00
|
|
|
return MarketExchange
|