Jump to content

merm brain GetClosestToolShed and PickupTool functions return the farthest shed/tool instead


hoxi
  • Fixed

This applies to both the mermbrain and mermguardbrain files (this stuff could also be consolidated into a single common brain file).

Here's an example with GetClosestToolShed:

local function GetClosestToolShed(inst, dist)
    dist = dist or FIND_SHED_RANGE

    local x, y, z = inst.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x, 0, z, dist, nil, nil, TOOLSHED_ONEOF_TAGS)

    if #ents <= 0 then
        return nil
    end

    local shed = nil

    for _, ent in ipairs(ents) do
        if ent:CanSupply() then
            if ent:HasTag("merm_toolshed_upgraded") then
                return ent -- High priority.
            end

            shed = ent
        end
    end

    return shed
end

These work fine for the upgraded versions, as entity searches return results in order based on distance, and the return will stop on finding a valid one. But the ones that aren't upgraded will return the furthest one away instead, as iteration will keep going.

This probably needs a bit of reformatting, or to simply use a return statement for unupgraded ones as well, if no other arguments to determine the ideal shed/tool are added.


Steps to Reproduce
  1. Place multiple sheds or drop multiple tools around for merms to go to or pick up, that aren't upgraded.
  2. Notice how the furthest one should be chosen out of all the possible candidates.



User Feedback


A developer has marked this issue as fixed. This means that the issue has been addressed in the current development build and will likely be in the next update.

Hey! I'm happy that this got fixed, but I have to ask if not addressing it for PickupTool is intended or not:

local function PickupTool(inst)
    if inst.sg:HasStateTag("busy") then
        return nil
    end
    if NeedsTool(inst) then
        local x, y, z = inst.Transform:GetWorldPosition()
        local ents = TheSim:FindEntities(x, 0, z, FIND_SHED_RANGE, nil, MERM_TOOL_CANT_TAGS, MERM_TOOL_ONEOF_TAGS)
        if #ents <= 0 then
            return nil
        end
        local tool = nil
        for _, ent in ipairs(ents) do
            if ent:HasTag("merm_tool_upgraded") then
                return BufferedAction(inst, ent, ACTIONS.PICKUP) -- High priority.
            end
            tool = ent -- right here
        end
        return tool ~= nil and BufferedAction(inst, tool, ACTIONS.PICKUP) or nil
    end
end

I mentioned it in the report above but I probably should've shown a code snippet about it as well, that's my bad.

Share this comment


Link to comment
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
  • Create New...