From 1bea4f6015a0ec2bf46b8126441649869030c133 Mon Sep 17 00:00:00 2001 From: notnotmelon Date: Wed, 2 Aug 2023 21:15:31 -0500 Subject: [PATCH] caravan ups improvement --- changelog.txt | 2 +- prototypes/creatures/caravan.lua | 4 ++-- scripts/caravan/caravan-prototypes.lua | 28 +++++++++++++++----------- scripts/caravan/caravan.lua | 14 +++++++------ 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/changelog.txt b/changelog.txt index 6f12a8f7..52838487 100644 --- a/changelog.txt +++ b/changelog.txt @@ -4,8 +4,8 @@ Date: ? Changes: - fixed that crafting ingredients could sometimes be deleted when removing modules in a farm building - fixed that whenever your ulric transformation expired it would reset all connected caravan schedules - - added out of fuel map icons for caravans and aerial caravans - added fallback icon sprites for vessel to ground + - removed caravan map tags. this results in a 15x UPS improvement. for some reason that API call is extremely laggy --------------------------------------------------------------------------------------------------- Version: 2.1.7 Date: 2023-7-27 diff --git a/prototypes/creatures/caravan.lua b/prototypes/creatures/caravan.lua index b87d1d86..886b6308 100644 --- a/prototypes/creatures/caravan.lua +++ b/prototypes/creatures/caravan.lua @@ -229,7 +229,7 @@ ITEM { flags = {'hidden', 'not-stackable', 'only-in-cursor'}, } -data:extend{ +--[[data:extend{ { type = 'virtual-signal', icon = '__pyalienlifegraphics2__/graphics/icons/caravan-map-tag-mk01.png', @@ -262,4 +262,4 @@ data:extend{ icon_size = 64, localised_name = {'virtual-signal-name.no-fuel'} }, -} \ No newline at end of file +}--]] \ No newline at end of file diff --git a/scripts/caravan/caravan-prototypes.lua b/scripts/caravan/caravan-prototypes.lua index d1c51fb3..eb144f5a 100644 --- a/scripts/caravan/caravan-prototypes.lua +++ b/scripts/caravan/caravan-prototypes.lua @@ -249,15 +249,19 @@ local function get_outpost_inventory(outpost) end local function transfer_all_items(input_inventory, output_inventory) - for i = 1, #input_inventory do - local slot = input_inventory[i] - if slot.valid_for_read then - local inserted_count = output_inventory.insert(slot) - if inserted_count == slot.count then - slot.clear() - elseif inserted_count ~= 0 then input_inventory.remove{name = slot.name, count = inserted_count} end + if not output_inventory.find_empty_stack() then return false end + local changed = false + for item, count in pairs(input_inventory.get_contents()) do + local stack = {name = item, count = count} + if output_inventory.can_insert(stack) then + local inserted_count = output_inventory.insert{name = item, count = count} + if inserted_count ~= 0 then + input_inventory.remove{name = item, count = inserted_count} + changed = true + end end end + return changed end local function transfer_filtered_items(input_inventory, output_inventory, item, goal) -- TODO: make it work with complex items @@ -323,8 +327,8 @@ Caravan.actions = { if not outpost_inventory then return true end local inventory = caravan_data.inventory - transfer_all_items(outpost_inventory, inventory) - return inventory.is_full() + local changed = transfer_all_items(outpost_inventory, inventory) + return changed and inventory.is_full() end, ['empty-inventory'] = function(caravan_data, schedule, action) @@ -334,8 +338,8 @@ Caravan.actions = { if not outpost_inventory then return true end local inventory = caravan_data.inventory - transfer_all_items(inventory, outpost_inventory) - return inventory.is_empty() + local changed = transfer_all_items(inventory, outpost_inventory) + return changed and inventory.is_empty() end, ['empty-autotrash'] = function(caravan_data, schedule, action) @@ -346,7 +350,7 @@ Caravan.actions = { local inventory = caravan_data.inventory transfer_all_items(autotrash_inventory, inventory) - return autotrash_inventory.is_empty() or not inventory.find_empty_stack() + return true end, ['item-count'] = function(caravan_data, schedule, action) diff --git a/scripts/caravan/caravan.lua b/scripts/caravan/caravan.lua index 0f9753d3..ad3513cb 100644 --- a/scripts/caravan/caravan.lua +++ b/scripts/caravan/caravan.lua @@ -45,14 +45,15 @@ end local function exists_and_valid(v) return v and v.valid end function Caravan.validity_check(caravan_data) if not caravan_data or caravan_data.itemised then return false end + local inventory, fuel_inventory = caravan_data.inventory, caravan_data.fuel_inventory if not caravan_data.entity.valid or - (caravan_data.inventory and not exists_and_valid(caravan_data.inventory)) or - (caravan_data.fuel_inventory and not exists_and_valid(caravan_data.fuel_inventory)) + (inventory and not inventory.valid) or + (fuel_inventory and not fuel_inventory.valid) then if caravan_data.entity.valid then caravan_data.entity.destroy() end - if exists_and_valid(caravan_data.inventory) then caravan_data.inventory.destroy() end - if exists_and_valid(caravan_data.fuel_inventory) then caravan_data.fuel_inventory.destroy() end + if exists_and_valid(inventory) then inventory.destroy() end + if exists_and_valid(fuel_inventory) then fuel_inventory.destroy() end global.caravans[caravan_data.unit_number] = nil return false end @@ -464,7 +465,8 @@ Caravan.events[60] = function(event) local entity = caravan_data.entity local needs_fuel = caravan_data.fuel_inventory and caravan_data.fuel_bar == 0 and caravan_data.fuel_inventory.is_empty() - local icon + -- this was causing too much lag. waiting on a new API feature that will help + --[[local icon if needs_fuel then icon = no_fuel_map_tag else icon = prototypes[entity.name].map_tag end local map_tag = caravan_data.map_tag if map_tag and map_tag.valid then @@ -475,7 +477,7 @@ Caravan.events[60] = function(event) position = entity.position, icon = icon }) - ::didnt_move:: + ::didnt_move::--]] if needs_fuel then draw_error_sprite(entity, 'utility.fuel_icon', 30)