diff --git a/maps/mountain_fortress_v3/icw/functions.lua b/maps/mountain_fortress_v3/icw/functions.lua index d7f05c63..5535bf50 100644 --- a/maps/mountain_fortress_v3/icw/functions.lua +++ b/maps/mountain_fortress_v3/icw/functions.lua @@ -10,6 +10,16 @@ function Public.request_reconstruction(icw) icw.rebuild_tick = game.tick + 30 end +local function validate_entity(entity) + if not entity then + return false + end + if not entity.valid then + return false + end + return true +end + local function delete_empty_surfaces(icw) for k, surface in pairs(icw.surfaces) do if not icw.trains[tonumber(surface.name)] then @@ -191,10 +201,16 @@ local transfer_functions = { } local function get_wagon_for_entity(icw, entity) + if not validate_entity(entity) then + return + end + local train = icw.trains[tonumber(entity.surface.name)] + if not train then return end + local position = entity.position for k, unit_number in pairs(train.wagons) do local wagon = icw.wagons[unit_number] @@ -265,7 +281,19 @@ function Public.kill_minimap(player) end end +function Public.is_minimap_valid(player, surface) + if validate_entity(player) then + if player.surface ~= surface then + Public.kill_minimap(player) + end + end +end + function Public.kill_wagon(icw, entity) + if not validate_entity(entity) then + return + end + if not Constants.wagon_types[entity.type] then return end @@ -534,11 +562,7 @@ function Public.create_wagon_room(icw, wagon) end function Public.create_wagon(icw, created_entity, delay_surface) - if not created_entity then - return - end - - if not created_entity.valid then + if not validate_entity(created_entity) then return end diff --git a/maps/mountain_fortress_v3/main.lua b/maps/mountain_fortress_v3/main.lua index 5d0a3540..d9f6b457 100644 --- a/maps/mountain_fortress_v3/main.lua +++ b/maps/mountain_fortress_v3/main.lua @@ -21,14 +21,14 @@ local Balance = require 'maps.mountain_fortress_v3.balance' local Entities = require 'maps.mountain_fortress_v3.entities' local Gui_mf = require 'maps.mountain_fortress_v3.gui' local ICW = require 'maps.mountain_fortress_v3.icw.main' +local ICW_Func = require 'maps.mountain_fortress_v3.icw.functions' local WD = require 'modules.wave_defense.table' local Map = require 'modules.map_info' local RPG = require 'maps.mountain_fortress_v3.rpg' local Terrain = require 'maps.mountain_fortress_v3.terrain' local Event = require 'utils.event' local WPT = require 'maps.mountain_fortress_v3.table' -local Locomotive = require 'maps.mountain_fortress_v3.locomotive'.locomotive_spawn -local render_train_hp = require 'maps.mountain_fortress_v3.locomotive'.render_train_hp +local Locomotive = require 'maps.mountain_fortress_v3.locomotive' local Score = require 'comfy_panel.score' local Poll = require 'comfy_panel.poll' local Collapse = require 'modules.collapse' @@ -251,8 +251,8 @@ function Public.reset_map() this.cargo_health = 10000 this.cargo_max_health = 10000 - Locomotive(surface, {x = -18, y = 25}) - render_train_hp() + Locomotive.locomotive_spawn(surface, {x = -18, y = 25}) + Locomotive.render_train_hp() render_direction(surface) -- LM.place_market() RPG.rpg_reset_all_players() @@ -322,6 +322,8 @@ local on_player_joined_game = function(event) set_difficulty() + ICW_Func.is_minimap_valid(player, surface) + if not this.players[player.index] then this.players[player.index] = { data = {} @@ -363,12 +365,10 @@ end local on_pre_player_left_game = function(event) local this = WPT.get() local player = game.players[event.player_index] - local tick + local tick = game.tick if player.character then if not this.offline_players_enabled then - tick = game.tick + 432000 - else - tick = game.tick + return end this.offline_players[#this.offline_players + 1] = { index = event.player_index, @@ -381,13 +381,7 @@ end local remove_offline_players = function() local this = WPT.get() if not this.offline_players_enabled then - if game.tick < 500 then - return - end - if game.tick % 432000 == 0 then - this.offline_players_enabled = true - return - end + return end local offline_players = WPT.get('offline_players') local active_surface_index = WPT.get('active_surface_index') diff --git a/maps/mountain_fortress_v3/table.lua b/maps/mountain_fortress_v3/table.lua index 2e627761..e7684349 100644 --- a/maps/mountain_fortress_v3/table.lua +++ b/maps/mountain_fortress_v3/table.lua @@ -81,7 +81,7 @@ function Public.reset_table() this.health_upgrades = 0 this.breached_wall = 1 this.entity_limits = {} - this.offline_players_enabled = false + this.offline_players_enabled = true this.left_top = { x = 0, y = 0 diff --git a/modules/autostash.lua b/modules/autostash.lua index 5fbbd8a2..db180b2e 100644 --- a/modules/autostash.lua +++ b/modules/autostash.lua @@ -24,11 +24,10 @@ local ore_names = { ['coal'] = true, ['stone'] = true, ['iron-ore'] = true, - ['copper-ore'] = true, - ['uranium-ore'] = true + ['copper-ore'] = true } -local function create_floaty_text(surface, position, name, count, height_offset) +local function create_floaty_text(surface, position, name, count) if autostash.floating_text_y_offsets[position.x .. '_' .. position.y] then autostash.floating_text_y_offsets[position.x .. '_' .. position.y] = autostash.floating_text_y_offsets[position.x .. '_' .. position.y] - 0.5 @@ -139,9 +138,9 @@ local function get_nearby_chests(player) local containers = {} local i = 0 if autostash.insert_into_furnace then - type = {'furnace', 'container'} + type = {'furnace', 'container', 'logistic-container'} else - type = {'container'} + type = {'container', 'logistic-container'} end for _, e in pairs(player.surface.find_entities_filtered({type = type, area = area})) do if ((player.position.x - e.position.x) ^ 2 + (player.position.y - e.position.y) ^ 2) <= r_square then @@ -155,30 +154,6 @@ local function get_nearby_chests(player) chests[size_of_chests] = entity end - for _, e in pairs(player.surface.find_entities_filtered({name = 'logistic-chest-storage', area = area})) do - if ((player.position.x - e.position.x) ^ 2 + (player.position.y - e.position.y) ^ 2) <= r_square then - i = i + 1 - containers[i] = e - end - end - sort_entities_by_distance(player.position, containers) - for _, entity in pairs(containers) do - size_of_chests = size_of_chests + 1 - chests[size_of_chests] = entity - end - - for _, e in pairs(player.surface.find_entities_filtered({name = 'logistic-chest-passive-provider', area = area})) do - if ((player.position.x - e.position.x) ^ 2 + (player.position.y - e.position.y) ^ 2) <= r_square then - i = i + 1 - containers[i] = e - end - end - sort_entities_by_distance(player.position, containers) - for _, entity in pairs(containers) do - size_of_chests = size_of_chests + 1 - chests[size_of_chests] = entity - end - return chests end @@ -192,6 +167,11 @@ local function does_inventory_contain_item_type(inventory, item_subgroup) end local function insert_item_into_chest(player_inventory, chests, filtered_chests, name, count, furnace) + local container = { + ['container'] = true, + ['logistic-container'] = true + } + --Attempt to store into furnaces. if furnace then for _, chest in pairs(chests) do @@ -210,8 +190,8 @@ local function insert_item_into_chest(player_inventory, chests, filtered_chests, end for _, chest in pairs(chests) do - local chest_inventory = chest.get_inventory(defines.inventory.chest) - if chest_inventory and chest.type == 'furnace' then + if chest.type == 'furnace' then + local chest_inventory = chest.get_inventory(defines.inventory.chest) if chest_inventory.can_insert({name = name, count = count}) then local inserted_count = chest_inventory.insert({name = name, count = count}) player_inventory.remove({name = name, count = inserted_count}) @@ -227,8 +207,8 @@ local function insert_item_into_chest(player_inventory, chests, filtered_chests, --Attempt to store in chests that already have the same item. for _, chest in pairs(chests) do - local chest_inventory = chest.get_inventory(defines.inventory.chest) - if chest_inventory and chest.type == 'container' then + if container[chest.type] then + local chest_inventory = chest.get_inventory(defines.inventory.chest) if chest_inventory.can_insert({name = name, count = count}) then if chest_inventory.find_item_stack(name) then local inserted_count = chest_inventory.insert({name = name, count = count}) @@ -246,8 +226,8 @@ local function insert_item_into_chest(player_inventory, chests, filtered_chests, --Attempt to store in empty chests. for _, chest in pairs(filtered_chests) do - local chest_inventory = chest.get_inventory(defines.inventory.chest) - if chest_inventory and chest.type == 'container' then + if container[chest.type] then + local chest_inventory = chest.get_inventory(defines.inventory.chest) if chest_inventory.can_insert({name = name, count = count}) then if chest_inventory.is_empty() then local inserted_count = chest_inventory.insert({name = name, count = count}) @@ -266,8 +246,8 @@ local function insert_item_into_chest(player_inventory, chests, filtered_chests, local item_subgroup = game.item_prototypes[name].subgroup.name if item_subgroup then for _, chest in pairs(filtered_chests) do - local chest_inventory = chest.get_inventory(defines.inventory.chest) - if chest_inventory and chest.type == 'container' then + if container[chest.type] then + local chest_inventory = chest.get_inventory(defines.inventory.chest) if chest_inventory.can_insert({name = name, count = count}) then if does_inventory_contain_item_type(chest_inventory, item_subgroup) then local inserted_count = chest_inventory.insert({name = name, count = count}) @@ -285,8 +265,8 @@ local function insert_item_into_chest(player_inventory, chests, filtered_chests, --Attempt to store in mixed chests. for _, chest in pairs(filtered_chests) do - local chest_inventory = chest.get_inventory(defines.inventory.chest) - if chest_inventory and chest.type == 'container' then + if container[chest.type] then + local chest_inventory = chest.get_inventory(defines.inventory.chest) if chest_inventory.can_insert({name = name, count = count}) then local inserted_count = chest_inventory.insert({name = name, count = count}) player_inventory.remove({name = name, count = inserted_count}) @@ -340,15 +320,16 @@ local function auto_stash(player, event) end for name, count in pairs(inventory.get_contents()) do - local is_ore = - game.entity_prototypes[name] and game.entity_prototypes[name].type == 'resource' or ore_names[name] + local is_ore = ore_names[name] + local is_resource = game.entity_prototypes[name] and game.entity_prototypes[name].type == 'resource' + if not inventory.find_item_stack(name).grid and not hotbar_items[name] then if ctrl then if is_ore and autostash.insert_into_furnace then insert_item_into_chest(inventory, chests, filtered_chests, name, count, true) end elseif button == defines.mouse_button_type.right then - if is_ore then + if is_resource or is_ore then insert_item_into_chest(inventory, chests, filtered_chests, name, count) end elseif button == defines.mouse_button_type.left then diff --git a/modules/show_inventory.lua b/modules/show_inventory.lua index 6cc9ec45..570a58f8 100644 --- a/modules/show_inventory.lua +++ b/modules/show_inventory.lua @@ -259,11 +259,9 @@ local function on_player_joined_game(event) end end -local function on_player_left_game(event) +local function on_pre_player_left_game(event) local player = game.players[event.player_index] - if validate_player(player) then - close_player_inventory(player) - end + close_player_inventory(player) end local function update_gui() @@ -331,6 +329,6 @@ end Event.add(defines.events.on_player_main_inventory_changed, update_gui) Event.add(defines.events.on_gui_closed, gui_closed) Event.add(defines.events.on_player_joined_game, on_player_joined_game) -Event.add(defines.events.on_player_left_game, on_player_left_game) +Event.add(defines.events.on_pre_player_left_game, on_pre_player_left_game) return Public