notnotmelon / fluid-memory-storage

An alternative to flare stack/voiding fluids. The Fluid Memory Unit can store an infinite amount of any liquid or gas. The more fluid you store, the more power it will require!
MIT License
1 stars 0 forks source link

Not Infinite, Crashes at 32bit int max #4

Open necrogami opened 2 weeks ago

necrogami commented 2 weeks ago

The mod Fluid Memory Storage (1.7.5) caused a non-recoverable error. Please report this error to the mod author.

Error while running event fluid-memory-storage::on_nth_tick(15) Given min value (2.1475e+09) is too big, allowed values are from -2147483648 to 2147483647 stack traceback: [C]: in function 'set_slot' fluid-memory-storage/shared.lua:61: in function 'update_combinator' fluid-memory-storage/control.lua:57: in function 'update_unit_exterior' fluid-memory-storage/control.lua:109: in function 'update_unit' fluid-memory-storage/control.lua:117: in function <fluid-memory-storage/control.lua:112>

VerisZG commented 2 weeks ago

The mod Fluid Memory Storage (1.7.5) caused a non-recoverable error. Please report this error to the mod author.

Error while running event fluid-memory-storage::on_nth_tick(15) Given min value (2.1475e+09) is too big, allowed values are from -2147483648 to 2147483647 stack traceback: [C]: in function 'set_slot' fluid-memory-storage/shared.lua:61: in function 'update_combinator' fluid-memory-storage/control.lua:57: in function 'update_unit_exterior' fluid-memory-storage/control.lua:109: in function 'update_unit' fluid-memory-storage/control.lua:117: in function <fluid-memory-storage/control.lua:112>

VerisZG commented 2 weeks ago

I've checked this with AI, and probably this is the problem in control.lua:

Line 49: local total_count = unit_data.count + inventory_count might be changed to: local total_count = min(2147483647, unit_data.count + inventory_count)

Also, line 91: unit_data.count = min(2147483647, unit_data.count + amount_removed)

Then, in shared.lua line 60: count = min(2147483647, count), Shouldn't it be: count = min(2147483647, max(-2147483648, count)) And also, was there a syntax error due to the comma at the end of the line?

VerisZG commented 2 weeks ago

ok, changing above causes the error: The mod Fluid Memory Storage (1.7.5) caused a non-recoverable error. Please report this error to the mod author.

Error while running event fluid-memory-storage::on_nth_tick(15) fluid-memory-storage/shared.lua:60: attempt to call global 'max' (a nil value) stack traceback: fluid-memory-storage/shared.lua:60: in function 'update_combinator' fluid-memory-storage/control.lua:57: in function 'update_unit_exterior' fluid-memory-storage/control.lua:109: in function 'update_unit' fluid-memory-storage/control.lua:117: in function <fluid-memory-storage/control.lua:112>

I'll try to fix this...

VerisZG commented 2 weeks ago

by adding: line 4: local max = math.max

...

VerisZG commented 2 weeks ago

yeah, it seems that these changes resolve the problem

VerisZG commented 1 week ago

I've updated the function "update_unit" so it only takes from network when it can store the fluid inside.

local function update_unit(unit_data, unit_number, force)
    local entity = unit_data.entity

    if validity_check(unit_number, unit_data, force) then return end

    local changed = false

    if unit_data.item == nil then changed = detect_item(unit_data) end
    local item = unit_data.item
    if item == nil then return end
    local comfortable = unit_data.comfortable

    local inventory_count = entity.get_fluid_count(item)
    if inventory_count > comfortable then
        -- count how much we can store
        local max_storeable = 2147483647 - unit_data.count
        if max_storeable <= 0 then
            -- we cannot store more
            max_storeable = 0
        end

        -- count how much do we take from fluidbox
        local amount_to_remove = inventory_count - comfortable
        if amount_to_remove > max_storeable then
            amount_to_remove = max_storeable
        end

        if amount_to_remove > 0 then
            local amount_removed = entity.remove_fluid {name = item, amount = amount_to_remove}
            unit_data.temperature = combine_tempatures(unit_data.count, unit_data.temperature, amount_removed, entity.fluidbox[1].temperature)
            unit_data.count = unit_data.count + amount_removed
            inventory_count = inventory_count - amount_removed
            changed = true
        end
    elseif inventory_count < comfortable then
        if unit_data.previous_inventory_count ~= inventory_count then
            changed = true
        end
        local to_add = comfortable - inventory_count
        if unit_data.count < to_add then
            to_add = unit_data.count
        end
        if to_add ~= 0 then
            local amount_added = entity.insert_fluid {name = item, amount = to_add, temperature = unit_data.temperature}
            unit_data.count = unit_data.count - amount_added
            inventory_count = inventory_count + amount_added
        end
    end

    if force or changed then update_unit_exterior(unit_data, inventory_count) end
end