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 '
2018-10-10 20:49:49 +02:00
local Gui = require ' utils.gui '
2018-10-06 15:53:28 +02:00
local Debug = require ' map_gen.Diggy.Debug '
local Template = require ' map_gen.Diggy.Template '
2018-10-10 23:05:48 +02:00
local Global = require ' utils.global '
2018-11-06 21:36:11 +01:00
local Game = require ' utils.game '
2018-11-13 23:33:39 +01:00
local MarketUnlockables = require ' map_gen.Diggy.MarketUnlockables '
local calculate_level = MarketUnlockables.calculate_level
2018-10-17 20:50:07 +02:00
local insert = table.insert
2018-10-25 17:53:14 +02:00
local max = math.max
2018-10-06 15:53:28 +02:00
-- this
local MarketExchange = { }
2018-10-10 23:05:48 +02:00
local config = { }
local stone_tracker = {
2018-10-06 15:53:28 +02:00
stone_sent_to_surface = 0 ,
2018-10-10 20:49:49 +02:00
previous_stone_sent_to_surface = 0 ,
2018-11-12 17:51:55 +01:00
current_level = 0 ,
2018-10-06 15:53:28 +02:00
}
2018-10-17 20:50:07 +02:00
local stone_collecting = {
initial_value = 0 ,
active_modifier = 0 ,
research_modifier = 0 ,
market_modifier = 0 ,
}
2018-10-10 23:05:48 +02:00
local mining_efficiency = {
active_modifier = 0 ,
research_modifier = 0 ,
market_modifier = 0 ,
}
2018-10-17 20:50:07 +02:00
2018-10-10 23:05:48 +02:00
local inventory_slots = {
active_modifier = 0 ,
research_modifier = 0 ,
market_modifier = 0 ,
}
Global.register ( {
2018-10-17 20:50:07 +02:00
stone_collecting = stone_collecting ,
2018-10-10 23:05:48 +02:00
stone_tracker = stone_tracker ,
mining_efficiency = mining_efficiency ,
inventory_slots = inventory_slots ,
} , function ( tbl )
2018-10-17 20:50:07 +02:00
stone_collecting = tbl.stone_collecting
2018-10-10 23:05:48 +02:00
stone_tracker = tbl.stone_tracker
mining_efficiency = tbl.mining_efficiency
inventory_slots = tbl.inventory_slots
end )
2018-10-06 15:53:28 +02:00
2018-10-17 20:50:07 +02:00
local function send_stone_to_surface ( total )
stone_tracker.previous_stone_sent_to_surface = stone_tracker.stone_sent_to_surface
stone_tracker.stone_sent_to_surface = stone_tracker.stone_sent_to_surface + total
end
2018-10-10 20:49:49 +02:00
local on_market_timeout_finished = Token.register ( function ( params )
2018-11-12 20:23:25 +01:00
Template.market ( params.surface , params.position , params.player_force , { } )
2018-10-17 20:50:07 +02:00
local tiles = { }
for _ , position in pairs ( params.void_chest_tiles ) do
insert ( tiles , { name = ' tutorial-grid ' , position = position } )
end
params.surface . set_tiles ( tiles )
2018-10-10 20:49:49 +02:00
end )
2018-10-07 17:37:29 +02:00
2018-10-10 20:49:49 +02:00
local function update_mining_speed ( force )
-- remove the current buff
local old_modifier = force.manual_mining_speed_modifier - mining_efficiency.active_modifier
-- update the active modifier
mining_efficiency.active_modifier = mining_efficiency.research_modifier + mining_efficiency.market_modifier
-- add the new active modifier to the non-buffed modifier
force.manual_mining_speed_modifier = old_modifier + mining_efficiency.active_modifier
end
local function update_inventory_slots ( force )
-- remove the current buff
local old_modifier = force.character_inventory_slots_bonus - inventory_slots.active_modifier
-- update the active modifier
inventory_slots.active_modifier = inventory_slots.research_modifier + inventory_slots.market_modifier
-- add the new active modifier to the non-buffed modifier
force.character_inventory_slots_bonus = old_modifier + inventory_slots.active_modifier
end
2018-10-06 15:53:28 +02:00
2018-10-17 20:50:07 +02:00
local function update_stone_collecting ( )
-- remove the current buff
local old_modifier = stone_collecting.initial_value - stone_collecting.active_modifier
-- update the active modifier
stone_collecting.active_modifier = stone_collecting.research_modifier + stone_collecting.market_modifier
-- add the new active modifier to the non-buffed modifier
stone_collecting.initial_value = old_modifier + stone_collecting.active_modifier
end
2018-11-12 17:51:55 +01:00
--Handles the updating of market items when unlocked, also handles the buffs
2018-10-10 20:49:49 +02:00
local function update_market_contents ( market )
2018-10-17 20:50:07 +02:00
if ( stone_tracker.previous_stone_sent_to_surface == stone_tracker.stone_sent_to_surface ) then
return
end
2018-10-10 20:49:49 +02:00
local should_update_mining_speed = false
local should_update_inventory_slots = false
2018-10-17 20:50:07 +02:00
local should_update_stone_collecting = false
local add_market_item
2018-11-12 17:51:55 +01:00
local old_level = stone_tracker.current_level
2018-10-17 20:50:07 +02:00
local print = game.print
2018-10-10 20:49:49 +02:00
for _ , unlockable in pairs ( config.unlockables ) do
2018-11-13 23:33:39 +01:00
local stone_unlock = calculate_level ( unlockable.level )
local is_in_range = stone_unlock > stone_tracker.previous_stone_sent_to_surface and stone_unlock <= stone_tracker.stone_sent_to_surface
2018-11-12 17:51:55 +01:00
if ( is_in_range and stone_tracker.current_level == old_level ) then
2018-11-13 23:33:39 +01:00
while ( calculate_level ( stone_tracker.current_level ) < stone_tracker.stone_sent_to_surface ) do
2018-11-14 00:23:44 +01:00
if ( calculate_level ( stone_tracker.current_level + 1 ) <= stone_tracker.stone_sent_to_surface ) then
2018-11-13 23:33:39 +01:00
stone_tracker.current_level = stone_tracker.current_level + 1
else
break
end
end
2018-11-12 17:51:55 +01:00
end
2018-10-10 20:49:49 +02:00
-- only add the item to the market if it's between the old and new stone range
if ( is_in_range and unlockable.type == ' market ' ) then
2018-10-17 20:50:07 +02:00
add_market_item = add_market_item or market.add_market_item
2018-10-15 19:06:58 +02:00
local name = unlockable.prototype . name
local price = unlockable.prototype . price
2018-10-17 20:50:07 +02:00
print ( ' Mining Foreman: New wares at the market! Come get your ' .. name .. ' for only ' .. price .. ' ' .. config.currency_item .. ' ! ' )
add_market_item ( {
2018-10-15 19:06:58 +02:00
price = { { config.currency_item , price } } ,
offer = { type = ' give-item ' , item = name , count = 1 }
2018-10-06 15:53:28 +02:00
} )
end
end
2018-11-12 17:51:55 +01:00
if ( old_level < stone_tracker.current_level ) then
for _ , buffs in pairs ( config.buffs ) do
if ( buffs.prototype . name == ' mining_speed ' ) then
local value = buffs.prototype . value
print ( ' Mining Foreman: Increased mining speed by ' .. value .. ' %! ' )
should_update_mining_speed = true
mining_efficiency.market_modifier = mining_efficiency.market_modifier + ( value / 100 )
elseif ( buffs.prototype . name == ' inventory_slot ' ) then
local value = buffs.prototype . value
print ( ' Mining Foreman: Increased inventory slots by ' .. value .. ' ! ' )
should_update_inventory_slots = true
inventory_slots.market_modifier = inventory_slots.market_modifier + value
elseif ( buffs.prototype . name == ' stone_automation ' ) then
local value = buffs.prototype . value
if ( stone_tracker.current_level == 1 ) then
print ( ' Mining Foreman: We can now automatically send stone to the surface from a chest below the market! ' )
else
print ( ' Mining Foreman: We can now automatically send ' .. value .. ' more stones! ' )
end
should_update_stone_collecting = true
stone_collecting.market_modifier = stone_collecting.market_modifier + value
end
end
end
2018-10-10 20:49:49 +02:00
local force
if ( should_update_mining_speed ) then
force = force or game.forces . player
update_mining_speed ( force )
2018-10-07 17:37:29 +02:00
end
2018-10-06 15:53:28 +02:00
2018-10-10 20:49:49 +02:00
if ( should_update_inventory_slots ) then
force = force or game.forces . player
update_inventory_slots ( force )
end
2018-10-17 20:50:07 +02:00
if ( should_update_stone_collecting ) then
update_stone_collecting ( )
end
2018-10-10 20:49:49 +02:00
end
2018-10-07 13:50:25 +02:00
2018-10-10 20:49:49 +02:00
local function on_research_finished ( event )
local force = game.forces . player
local current_modifier = mining_efficiency.research_modifier
2018-11-06 18:48:38 +01:00
local new_modifier = force.mining_drill_productivity_bonus * config.mining_speed_productivity_multiplier * 0.5
2018-10-10 20:49:49 +02:00
if ( current_modifier == new_modifier ) then
-- something else was researched
return
end
2018-10-07 13:50:25 +02:00
2018-10-10 20:49:49 +02:00
mining_efficiency.research_modifier = new_modifier
2018-10-17 20:50:07 +02:00
inventory_slots.research_modifier = force.mining_drill_productivity_bonus * 50 -- 1 per level
stone_collecting.research_modifier = force.mining_drill_productivity_bonus * 1250 -- 25 per level
2018-10-10 20:49:49 +02:00
update_inventory_slots ( force )
update_mining_speed ( force )
2018-10-17 20:50:07 +02:00
update_stone_collecting ( )
2018-10-10 20:49:49 +02:00
end
2018-10-25 15:30:56 +02:00
local function comma_value ( n ) -- credit http://richard.warburton.it
2018-11-12 20:23:25 +01:00
local left , num , right = string.match ( n , ' ^([^%d]*%d)(%d*)(.-)$ ' )
return left .. ( num : reverse ( ) : gsub ( ' (%d%d%d) ' , ' %1, ' ) : reverse ( ) ) .. right
2018-10-25 15:30:56 +02:00
end
2018-10-11 22:17:25 +02:00
local function redraw_title ( data )
2018-10-25 15:30:56 +02:00
data.frame . caption = comma_value ( stone_tracker.stone_sent_to_surface ) .. ' ' .. config.currency_item .. ' sent to the surface '
2018-10-11 22:17:25 +02:00
end
2018-10-07 17:37:29 +02:00
2018-10-25 12:34:50 +02:00
local function get_data ( unlocks , stone , type )
local result = { }
for _ , data in pairs ( unlocks ) do
2018-11-13 23:33:39 +01:00
if calculate_level ( data.level ) == stone and data.type == type then
2018-10-25 17:53:14 +02:00
insert ( result , data )
2018-10-25 12:34:50 +02:00
end
end
return result
end
local tag_label_stone = Gui.uid_name ( )
local tag_label_buff = Gui.uid_name ( )
local tag_label_item = Gui.uid_name ( )
2018-11-05 19:03:27 +01:00
local function apply_heading_style ( style , width )
2018-10-25 12:34:50 +02:00
style.font = ' default-bold '
2018-11-05 19:03:27 +01:00
style.width = width
2018-10-25 12:34:50 +02:00
end
2018-11-12 17:51:55 +01:00
local function redraw_heading ( data , header )
2018-11-13 23:33:39 +01:00
local head_condition = ( header == 1 )
local frame = ( head_condition ) and data.market_list_heading or data.buff_list_heading
local header_caption = ( head_condition ) and ' Reward Item ' or ' Reward Buff '
2018-10-25 12:34:50 +02:00
Gui.clear ( frame )
2018-11-12 17:51:55 +01:00
local heading_table = frame.add ( { type = ' table ' , column_count = 2 } )
apply_heading_style ( heading_table.add ( { type = ' label ' , name = tag_label_stone , caption = ' Requirement ' } ) . style , 100 )
apply_heading_style ( heading_table.add ( { type = ' label ' , name = tag_label_buff , caption = header_caption } ) . style , 220 )
2018-10-25 12:34:50 +02:00
end
2018-10-25 15:30:56 +02:00
local function redraw_progressbar ( data )
2018-11-05 19:03:27 +01:00
local flow = data.market_progressbars
Gui.clear ( flow )
2018-10-25 15:30:56 +02:00
2018-11-05 19:03:27 +01:00
-- progress bar for next level
2018-11-13 23:33:39 +01:00
local act_stone = ( stone_tracker.current_level ~= 0 ) and calculate_level ( stone_tracker.current_level ) or 0
local next_stone = calculate_level ( stone_tracker.current_level + 1 )
2018-11-05 19:03:27 +01:00
local range = next_stone - act_stone
local sent = stone_tracker.stone_sent_to_surface - act_stone
2018-11-13 23:33:39 +01:00
local percentage = ( math.floor ( ( sent / range ) * 1000 ) ) * 0.001
percentage = ( percentage < 0 ) and ( percentage *- 1 ) or percentage
2018-11-05 19:03:27 +01:00
2018-11-13 23:33:39 +01:00
apply_heading_style ( flow.add ( { type = ' label ' , tooltip = ' Currently at level: ' .. stone_tracker.current_level .. ' \n Next level at: ' .. comma_value ( next_stone ) .. ' \n Remaining stone: ' .. comma_value ( range - sent ) , name = ' Diggy.MarketExchange.Frame.Progress.Level ' , caption = ' Progress to next level: ' } ) . style )
2018-11-05 19:03:27 +01:00
local level_progressbar = flow.add ( { type = ' progressbar ' , tooltip = percentage * 100 .. ' % stone to next level ' } )
2018-11-12 17:51:55 +01:00
level_progressbar.style . width = 350
2018-11-05 19:03:27 +01:00
level_progressbar.value = percentage
2018-10-25 15:30:56 +02:00
end
2018-10-25 12:34:50 +02:00
local function redraw_table ( data )
2018-10-11 22:17:25 +02:00
local market_scroll_pane = data.market_scroll_pane
Gui.clear ( market_scroll_pane )
2018-10-25 12:34:50 +02:00
local buffs = { }
local items = { }
2018-10-25 17:53:14 +02:00
local last_stone = 0
local number_of_rows = 0
2018-10-25 12:34:50 +02:00
local row = { }
2018-11-05 19:03:27 +01:00
-- create the progress bars in the window
2018-10-25 15:30:56 +02:00
redraw_progressbar ( data )
2018-10-25 12:34:50 +02:00
-- create table headings
2018-11-12 17:51:55 +01:00
redraw_heading ( data , 1 )
2018-10-25 12:34:50 +02:00
-- create table
for i = 1 , # config.unlockables do
2018-11-13 23:33:39 +01:00
if calculate_level ( config.unlockables [ i ] . level ) ~= last_stone then
2018-10-25 12:34:50 +02:00
-- get items and buffs for each stone value
2018-11-13 23:33:39 +01:00
items = get_data ( config.unlockables , calculate_level ( config.unlockables [ i ] . level ) , ' market ' )
2018-10-25 12:34:50 +02:00
-- get number of rows
2018-10-25 17:53:14 +02:00
number_of_rows = max ( # buffs , # items )
2018-10-25 12:34:50 +02:00
-- loop through buffs and items for number of rows
2018-10-25 17:53:14 +02:00
for j = 1 , number_of_rows do
2018-10-25 12:34:50 +02:00
local result = { }
2018-11-13 23:33:39 +01:00
local item = items [ j ]
local level = item.level
2018-10-25 15:30:56 +02:00
-- 1st column
2018-11-13 23:33:39 +01:00
result [ 6 ] = calculate_level ( level )
result [ 1 ] = ' Level ' .. level
2018-10-25 15:30:56 +02:00
-- 3rd column
2018-10-25 12:34:50 +02:00
if items [ j ] ~= nil then
2018-11-13 23:33:39 +01:00
result [ 3 ] = ' + ' .. item.prototype . name
2018-10-25 12:34:50 +02:00
else
result [ 3 ] = ' '
end
2018-10-25 15:30:56 +02:00
-- indicator to stop print stone number
if j > 1 then
2018-10-25 17:53:14 +02:00
result [ 4 ] = true
2018-10-25 15:30:56 +02:00
else
2018-10-25 17:53:14 +02:00
result [ 4 ] = false
2018-10-25 15:30:56 +02:00
end
-- indicator to draw horizontal line
2018-10-25 17:53:14 +02:00
if j == number_of_rows then
result [ 5 ] = true
2018-10-25 15:30:56 +02:00
else
2018-10-25 17:53:14 +02:00
result [ 5 ] = false
2018-10-25 15:30:56 +02:00
end
2018-10-25 12:34:50 +02:00
2018-10-25 17:53:14 +02:00
insert ( row , result )
2018-10-25 12:34:50 +02:00
end
2018-10-11 22:17:25 +02:00
end
2018-10-25 12:34:50 +02:00
-- save lastStone
2018-11-13 23:33:39 +01:00
last_stone = calculate_level ( config.unlockables [ i ] . level )
2018-10-25 12:34:50 +02:00
end
-- print table
for _ , unlockable in pairs ( row ) do
2018-11-12 17:51:55 +01:00
local is_unlocked = unlockable [ 6 ] <= stone_tracker.stone_sent_to_surface
local list = market_scroll_pane.add { type = ' table ' , column_count = 2 }
2018-10-25 12:34:50 +02:00
2018-10-25 15:30:56 +02:00
list.style . horizontal_spacing = 16
local caption = ' '
2018-10-25 17:53:14 +02:00
if unlockable [ 4 ] ~= true then
2018-11-12 17:51:55 +01:00
caption = unlockable [ 1 ]
2018-10-25 15:30:56 +02:00
else
caption = ' '
end
local tag_stone = list.add { type = ' label ' , name = tag_label_stone , caption = caption }
2018-11-12 17:51:55 +01:00
tag_stone.style . minimal_width = 100
2018-10-25 12:34:50 +02:00
local tag_items = list.add { type = ' label ' , name = tag_label_item , caption = unlockable [ 3 ] }
2018-11-12 17:51:55 +01:00
tag_items.style . minimal_width = 220
2018-10-11 22:17:25 +02:00
2018-10-25 15:30:56 +02:00
-- draw horizontal line
2018-10-25 17:53:14 +02:00
if unlockable [ 5 ] == true then
2018-10-25 15:30:56 +02:00
list.draw_horizontal_line_after_headers = true
end
2018-10-11 22:17:25 +02:00
if ( is_unlocked ) then
2018-10-25 12:34:50 +02:00
tag_stone.style . font_color = { r = 1 , g = 1 , b = 1 }
tag_items.style . font_color = { r = 1 , g = 1 , b = 1 }
2018-10-11 22:17:25 +02:00
else
2018-10-25 12:34:50 +02:00
tag_stone.style . font_color = { r = 0.5 , g = 0.5 , b = 0.5 }
tag_items.style . font_color = { r = 0.5 , g = 0.5 , b = 0.5 }
2018-10-11 22:17:25 +02:00
end
end
2018-10-10 20:49:49 +02:00
end
2018-11-12 17:51:55 +01:00
local function redraw_buff ( data ) --! Almost equals to the redraw_table() function !
local buff_scroll_pane = data.buff_scroll_pane
Gui.clear ( buff_scroll_pane )
local buffs = { }
local number_of_rows = 0
local row = { }
for i = 1 , # config.buffs do
-- get items and buffs for each stone value
buffs = config.buffs
local result = { }
-- 1st column
result [ 1 ] = ' All levels '
-- 2nd column
if buffs [ i ] . prototype.name == ' mining_speed ' then
result [ 2 ] = ' + ' .. buffs [ i ] . prototype.value .. ' % mining speed '
elseif buffs [ i ] . prototype.name == ' inventory_slot ' then
if buffs [ i ] . prototype.value > 1 then
result [ 2 ] = ' + ' .. buffs [ i ] . prototype.value .. ' inventory slots '
else
result [ 2 ] = ' + ' .. buffs [ i ] . prototype.value .. ' inventory slot '
end
elseif buffs [ i ] . prototype.name == ' stone_automation ' then
result [ 2 ] = ' + ' .. buffs [ i ] . prototype.value .. ' stones automatically sent '
else
result [ 2 ] = ' Description missing: unknown buff. Please contact admin '
end
-- 3rd column
result [ 3 ] = ' '
-- indicator to stop print level number
if i > 1 then
result [ 4 ] = true
else
result [ 4 ] = false
end
insert ( row , result )
end
for _ , unlockable in pairs ( row ) do
local list = buff_scroll_pane.add { type = ' table ' , column_count = 2 }
list.style . horizontal_spacing = 16
local caption = ' '
if unlockable [ 4 ] ~= true then
caption = unlockable [ 1 ]
else
caption = ' '
end
local tag_stone = list.add { type = ' label ' , name = buff_tag_label_stone , caption = caption }
tag_stone.style . minimal_width = 100
local tag_buffs = list.add { type = ' label ' , name = buff_tag_label_buff , caption = unlockable [ 2 ] }
tag_buffs.style . minimal_width = 220
tag_stone.style . font_color = { r = 1 , g = 1 , b = 1 }
tag_buffs.style . font_color = { r = 1 , g = 1 , b = 1 }
end
end
2018-10-12 19:37:16 +02:00
local function on_market_item_purchased ( event )
if ( 1 ~= event.offer_index ) then
return
end
2018-11-06 21:36:11 +01:00
local sum = config.stone_to_surface_amount * event.count
Game.print_player_floating_text ( event.player_index , ' - ' .. sum .. ' stone ' , { r = 0.6 , g = 0.55 , b = 0.42 } )
send_stone_to_surface ( sum )
2018-10-12 19:37:16 +02:00
update_market_contents ( event.market )
end
local function on_placed_entity ( event )
2018-10-17 20:50:07 +02:00
local market = event.entity
if ( ' market ' ~= market.name ) then
2018-10-12 19:37:16 +02:00
return
end
2018-10-17 20:50:07 +02:00
market.add_market_item ( {
price = { { config.currency_item , 50 } } ,
2018-11-12 20:23:25 +01:00
offer = { type = ' nothing ' , effect_description = ' Send ' .. config.stone_to_surface_amount .. ' ' .. config.currency_item .. ' to the surface. To see the overall progress and rewards, click the market button in the menu. ' }
2018-10-17 20:50:07 +02:00
} )
update_market_contents ( market )
2018-10-12 19:37:16 +02:00
end
function MarketExchange . get_extra_map_info ( config )
return ' Market Exchange, trade your stone or send it to the surface '
end
2018-10-10 20:49:49 +02:00
local function toggle ( event )
local player = event.player
local center = player.gui . center
2018-10-11 22:17:25 +02:00
local frame = center [ ' Diggy.MarketExchange.Frame ' ]
2018-10-10 20:49:49 +02:00
2018-10-11 22:17:25 +02:00
if ( frame ) then
Gui.destroy ( frame )
2018-10-10 20:49:49 +02:00
return
end
2018-10-11 22:17:25 +02:00
frame = center.add ( { name = ' Diggy.MarketExchange.Frame ' , type = ' frame ' , direction = ' vertical ' } )
2018-11-05 19:03:27 +01:00
local market_progressbars = frame.add ( { type = ' flow ' , direction = ' vertical ' } )
2018-10-25 15:30:56 +02:00
local market_list_heading = frame.add ( { type = ' flow ' , direction = ' horizontal ' } )
2018-10-11 22:17:25 +02:00
local market_scroll_pane = frame.add ( { type = ' scroll-pane ' } )
2018-11-12 17:51:55 +01:00
market_scroll_pane.style . maximal_height = 300
local buff_list_heading = frame.add ( { type = ' flow ' , direction = ' horizontal ' } )
local buff_scroll_pane = frame.add ( { type = ' scroll-pane ' } )
buff_scroll_pane.style . maximal_height = 100
2018-10-11 22:17:25 +02:00
frame.add ( { type = ' button ' , name = ' Diggy.MarketExchange.Button ' , caption = ' Close ' } )
local data = {
frame = frame ,
2018-11-05 19:03:27 +01:00
market_progressbars = market_progressbars ,
2018-10-25 15:30:56 +02:00
market_list_heading = market_list_heading ,
2018-10-11 22:17:25 +02:00
market_scroll_pane = market_scroll_pane ,
2018-11-12 17:51:55 +01:00
buff_list_heading = buff_list_heading ,
buff_scroll_pane = buff_scroll_pane ,
2018-10-11 22:17:25 +02:00
}
redraw_title ( data )
2018-10-25 12:34:50 +02:00
redraw_table ( data )
2018-11-12 17:51:55 +01:00
redraw_heading ( data , 2 )
redraw_buff ( data )
2018-10-11 22:17:25 +02:00
Gui.set_data ( frame , data )
player.opened = frame
2018-10-10 20:49:49 +02:00
end
2018-10-11 22:17:25 +02:00
local function on_player_created ( event )
2018-11-06 21:36:11 +01:00
Game.get_player_by_index ( event.player_index ) . gui.top . add ( {
2018-10-10 20:49:49 +02:00
name = ' Diggy.MarketExchange.Button ' ,
type = ' sprite-button ' ,
2018-11-12 20:23:25 +01:00
sprite = ' entity/market ' ,
2018-10-11 22:17:25 +02:00
} )
end
Gui.on_click ( ' Diggy.MarketExchange.Button ' , toggle )
Gui.on_custom_close ( ' Diggy.MarketExchange.Frame ' , function ( event )
event.element . destroy ( )
end )
function MarketExchange . on_init ( )
Task.set_timeout_in_ticks ( 50 , on_market_timeout_finished , {
surface = game.surfaces . nauvis ,
position = config.market_spawn_position ,
player_force = game.forces . player ,
2018-10-17 20:50:07 +02:00
void_chest_tiles = config.void_chest_tiles ,
2018-10-11 22:17:25 +02:00
} )
update_mining_speed ( game.forces . player )
end
--[[--
Registers all event handlers .
] ]
function MarketExchange . register ( cfg )
config = cfg
Event.add ( defines.events . on_research_finished , on_research_finished )
Event.add ( defines.events . on_market_item_purchased , on_market_item_purchased )
Event.add ( Template.events . on_placed_entity , on_placed_entity )
Event.add ( defines.events . on_player_created , on_player_created )
2018-10-17 20:50:07 +02:00
local x_min
local y_min
local x_max
local y_max
for _ , position in pairs ( config.void_chest_tiles ) do
local x = position.x
local y = position.y
if ( nil == x_min or x < x_min ) then
x_min = x
end
if ( nil == x_max or x > x_max ) then
x_max = x
end
if ( nil == y_min or y < y_min ) then
y_min = y
end
if ( nil == y_max or y > y_max ) then
y_max = y
end
end
local area = { { x_min , y_min } , { x_max + 1 , y_max + 1 } }
2018-11-06 21:36:11 +01:00
local message_x = ( x_max + x_min ) * 0.5
local message_y = ( y_max + y_min ) * 0.5
2018-10-17 20:50:07 +02:00
Event.on_nth_tick ( config.void_chest_frequency , function ( )
local send_to_surface = 0
2018-11-06 21:36:11 +01:00
local surface = game.surfaces . nauvis
local find_entities_filtered = surface.find_entities_filtered
2018-10-21 20:15:39 +02:00
local chests = find_entities_filtered ( { area = area , type = { ' container ' , ' logistic-container ' } } )
2018-10-17 20:50:07 +02:00
local to_fetch = stone_collecting.active_modifier
for _ , chest in pairs ( chests ) do
local chest_contents = chest.get_inventory ( defines.inventory . chest )
local stone_in_chest = chest_contents.get_item_count ( config.currency_item )
local delta = to_fetch
if ( stone_in_chest < delta ) then
delta = stone_in_chest
end
if ( delta > 0 ) then
chest_contents.remove ( { name = config.currency_item , count = delta } )
send_to_surface = send_to_surface + delta
end
end
if ( send_to_surface == 0 ) then
2018-11-06 21:36:11 +01:00
if ( 0 == to_fetch ) then
return
end
local message = ' Missing chests below market '
if ( # chests > 0 ) then
message = ' No stone in chests found '
end
Game.print_floating_text ( surface , { x = message_x , y = message_y } , message , { r = 220 , g = 100 , b = 50 } )
2018-10-17 20:50:07 +02:00
return
end
local markets = find_entities_filtered ( { name = ' market ' , position = config.market_spawn_position , limit = 1 } )
if ( # markets == 0 ) then
2018-11-12 20:23:25 +01:00
Debug.print_position ( config.market_spawn_position , ' Unable to find a market ' )
2018-10-17 20:50:07 +02:00
return
end
2018-11-06 21:36:11 +01:00
local message = send_to_surface .. ' stone sent to the surface '
Game.print_floating_text ( surface , { x = message_x , y = message_y } , message , { r = 0.6 , g = 0.55 , b = 0.42 } )
2018-10-17 20:50:07 +02:00
send_stone_to_surface ( send_to_surface )
update_market_contents ( markets [ 1 ] )
end )
2018-10-07 17:37:29 +02:00
end
2018-10-06 15:53:28 +02:00
return MarketExchange