diff --git a/map_gen/maps/quadrants/item_to_chest.lua b/map_gen/maps/quadrants/item_to_chest.lua new file mode 100644 index 00000000..5b23cb69 --- /dev/null +++ b/map_gen/maps/quadrants/item_to_chest.lua @@ -0,0 +1,62 @@ +local RS = require 'map_gen.shared.redmew_surface' +local Game = require 'utils.game' +local Event = require 'utils.event' + +local Public = {} + +local function create_chest(player) + local surface = RS.get_surface() + local pos = surface.find_non_colliding_position('steel-chest', player.position, 0, 1) + local chest = surface.create_entity { name = 'steel-chest', position = pos, force = player.force } + chest.minable = false + return chest +end + +function Public.transfer_inventory(player_index, inventories) + if inventories == nil or player_index == nil then + return 'You need to specify a player index and a table of define.inventory' + end + local player = Game.get_player_by_index(player_index) + local chest = create_chest(player) + for _, inventory in pairs(inventories) do + inventory = player.get_inventory(inventory) + for name, count in pairs(inventory.get_contents()) do + local ItemStack = { name = name, count = count } + inventory.remove(ItemStack) + while count > 0 do + if not chest.can_insert(ItemStack) then + chest = create_chest(player) + end + count = count - chest.insert(ItemStack) + ItemStack = { name = name, count = count } + end + end + end + return true +end + +local function on_gui_closed(event) + local entity = event.entity + if entity == nil or not entity.valid then + return + end + if entity.name == 'steel-chest' and entity.minable == false and not entity.has_items_inside() then + entity.destroy() + end +end + +local function ctrl_empty(event) + local entity = event.last_entity + if entity == nil or not entity.valid then + return + end + if entity.name == 'steel-chest' and not entity.minable then + event.entity = entity + on_gui_closed(event) + end +end + +Event.add(defines.events.on_gui_closed, on_gui_closed) +Event.add(defines.events.on_selected_entity_changed, ctrl_empty) + +return Public diff --git a/map_gen/maps/quadrants/scenario.lua b/map_gen/maps/quadrants/scenario.lua index b2144bf0..7b4ad86a 100644 --- a/map_gen/maps/quadrants/scenario.lua +++ b/map_gen/maps/quadrants/scenario.lua @@ -251,24 +251,24 @@ local start_tree_1 = b.entity(tree_rectangle_1, 'tree-01') local start_oil = b.resource(oil_rectangle, 'crude-oil', b.exponential_value(100000, 0, 1)) local start_tree_2 = b.entity(tree_rectangle_2, 'tree-01') -start_iron = b.combine({ b.translate(start_iron, base_x, base_y), b.translate(start_iron, -base_x, -base_y), b.translate(start_iron, base_x, -base_y), b.translate(start_iron, -base_x, base_y) }) +start_iron = b.any({ b.translate(start_iron, base_x, base_y), b.translate(start_iron, -base_x, -base_y), b.translate(start_iron, base_x, -base_y), b.translate(start_iron, -base_x, base_y) }) base_x = base_x + 32 -start_copper = b.combine({ b.translate(start_copper, base_x, base_y), b.translate(start_copper, -base_x, -base_y), b.translate(start_copper, base_x, -base_y), b.translate(start_copper, -base_x, base_y) }) +start_copper = b.any({ b.translate(start_copper, base_x, base_y), b.translate(start_copper, -base_x, -base_y), b.translate(start_copper, base_x, -base_y), b.translate(start_copper, -base_x, base_y) }) base_y = base_x -start_stone = b.combine({ b.translate(start_stone, base_x, base_y), b.translate(start_stone, -base_x, -base_y), b.translate(start_stone, base_x, -base_y), b.translate(start_stone, -base_x, base_y) }) +start_stone = b.any({ b.translate(start_stone, base_x, base_y), b.translate(start_stone, -base_x, -base_y), b.translate(start_stone, base_x, -base_y), b.translate(start_stone, -base_x, base_y) }) base_x = base_x - 32 -start_coal = b.combine({ b.translate(start_coal, base_x, base_y), b.translate(start_coal, -base_x, -base_y), b.translate(start_coal, base_x, -base_y), b.translate(start_coal, -base_x, base_y) }) +start_coal = b.any({ b.translate(start_coal, base_x, base_y), b.translate(start_coal, -base_x, -base_y), b.translate(start_coal, base_x, -base_y), b.translate(start_coal, -base_x, base_y) }) base_x = 64 base_y = 128 -start_tree_1 = b.combine({ b.translate(start_tree_1, base_x, base_y), b.translate(start_tree_1, -base_x, -base_y), b.translate(start_tree_1, base_x, -base_y), b.translate(start_oil, -base_x, base_y) }) +start_tree_1 = b.any({ b.translate(start_tree_1, base_x, base_y), b.translate(start_tree_1, -base_x, -base_y), b.translate(start_tree_1, base_x, -base_y), b.translate(start_oil, -base_x, base_y) }) base_x = 128 base_y = 64 -start_tree_2 = b.combine({ b.translate(start_tree_2, base_x, base_y), b.translate(start_tree_2, -base_x, -base_y), b.translate(start_tree_2, base_x, -base_y), b.translate(start_tree_2, -base_x, base_y) }) +start_tree_2 = b.any({ b.translate(start_tree_2, base_x, base_y), b.translate(start_tree_2, -base_x, -base_y), b.translate(start_tree_2, base_x, -base_y), b.translate(start_tree_2, -base_x, base_y) }) local map = b.apply_entities(quadrants, { start_iron, start_copper, start_stone, start_coal, start_tree_1, start_tree_2}) return map diff --git a/map_gen/maps/quadrants/switch_team.lua b/map_gen/maps/quadrants/switch_team.lua index 0506f681..8648865e 100644 --- a/map_gen/maps/quadrants/switch_team.lua +++ b/map_gen/maps/quadrants/switch_team.lua @@ -5,6 +5,8 @@ local abs = math.abs local Color = require 'resources.color_presets' local Popup = require 'features.gui.popup' local RS = require 'map_gen.shared.redmew_surface' +local Item_to_chest = require 'map_gen.maps.quadrants.item_to_chest' +local Global = require 'utils.global' local gui = {} @@ -15,6 +17,17 @@ local spawn_locations = { quadrant_4 = {64, 64} } +local toggle_chest_status = {} + +Global.register( + { + toggle_chest_status = toggle_chest_status + }, + function(tbl) + toggle_chest_status = tbl.toggle_chest_status + end +) + local quadrant_message = { { title = 'Research and command center', @@ -58,8 +71,8 @@ local quadrant_message = { local function teleport(event, quadrant) local player = event.player - - if (abs(player.position.x) <= 4 and abs(player.position.y) <= 4) or (player.get_inventory(1).is_empty() and player.get_inventory(2).is_empty() and (player.get_inventory(8) or player.get_inventory(3).is_empty())) then + local toggle_status = toggle_chest_status[player.index] + if (abs(player.position.x) <= 4 and abs(player.position.y) <= 4) or (player.get_inventory(1).is_empty() and player.get_inventory(2).is_empty() and (player.get_inventory(8) or player.get_inventory(3).is_empty())) or (toggle_status == 'ON' and Item_to_chest.transfer_inventory(player.index, {defines.inventory.player_quickbar, defines.inventory.player_main})) then local pos = RS.get_surface().find_non_colliding_position('player', spawn_locations['quadrant_'..quadrant], 5, 1) player.teleport(pos) player.force = game.forces['quadrant'..quadrant] @@ -70,6 +83,15 @@ local function teleport(event, quadrant) end end +local function redraw_chest_button(data, player) + local left_flow = data.chest_button_left_flow + local toggle_status = toggle_chest_status[player.index] + Gui.clear(left_flow) + + local button = left_flow.add({type = 'button', name = 'Quadrants.Button.Toggle', caption = 'Toggle inventory empty to chest. Currently: ' .. toggle_status}) + button.style.font = 'default' +end + local function toggle(event) local player = event.player local left = player.gui.left @@ -79,6 +101,8 @@ local function toggle(event) Gui.destroy(frame) return elseif (frame) then + local data = Gui.get_data(frame) + redraw_chest_button(data, player) return end @@ -128,24 +152,21 @@ local function toggle(event) left_flow.add({type = 'button', name = 'Quadrants.Button.3', caption = 'Oil and High Tech'}) right_flow.add({type = 'button', name = 'Quadrants.Button.4', caption = 'Logistics and Transport'}) + content_flow = frame.add {type = 'flow', direction = 'horizontal'} + local chest_button_left_flow = content_flow.add {type = 'flow', direction = 'horizontal'} + chest_button_left_flow.style.align = 'left' + chest_button_left_flow.style.horizontally_stretchable = true + local data = { frame = frame, + chest_button_left_flow = chest_button_left_flow } + redraw_chest_button(data, player) + Gui.set_data(frame, data) end -Gui.on_click('Quadrants.Button.1', function(event) teleport(event, 1) end) -Gui.on_click('Quadrants.Button.2', function(event) teleport(event, 2) end) -Gui.on_click('Quadrants.Button.3', function(event) teleport(event, 3) end) -Gui.on_click('Quadrants.Button.4', function(event) teleport(event, 4) end) - - -local function on_player_created(event) - event.player = Game.get_player_by_index(event.player_index) - toggle(event) -end - local function update_gui() local players = game.connected_players for i = #players, 1, -1 do @@ -161,6 +182,31 @@ local function update_gui() end end +local function toggle_chest(event) + local toggle_status = toggle_chest_status[event.player.index] + if toggle_status == 'OFF' then + toggle_chest_status[event.player.index] = 'ON' + else + toggle_chest_status[event.player.index] = 'OFF' + end + event.trigger = true + toggle(event) +end + + +Gui.on_click('Quadrants.Button.1', function(event) teleport(event, 1) end) +Gui.on_click('Quadrants.Button.2', function(event) teleport(event, 2) end) +Gui.on_click('Quadrants.Button.3', function(event) teleport(event, 3) end) +Gui.on_click('Quadrants.Button.4', function(event) teleport(event, 4) end) +Gui.on_click('Quadrants.Button.Toggle', function(event) toggle_chest(event) end) + + +local function on_player_created(event) + event.player = Game.get_player_by_index(event.player_index) + toggle_chest_status[event.player_index] = 'OFF' + toggle(event) +end + Event.add(defines.events.on_player_created, on_player_created) Event.on_nth_tick(61, update_gui)