Skip to content

Commit

Permalink
caravan ups improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
notnotmelon committed Aug 3, 2023
1 parent 709e2c5 commit 1bea4f6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions prototypes/creatures/caravan.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -262,4 +262,4 @@ data:extend{
icon_size = 64,
localised_name = {'virtual-signal-name.no-fuel'}
},
}
}--]]
28 changes: 16 additions & 12 deletions scripts/caravan/caravan-prototypes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions scripts/caravan/caravan.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 1bea4f6

Please sign in to comment.