[QUESTION] Math equation for distance & energy on the ground



  • Hello everyone!
    First, let me just say this: I'm no math genius!

    I've been trying to make a mathematical equation for a while now but I'm kinda stuck..
    Basically, what I want is an equation that takes into account the distance and the amount of energy on the ground (DROPPED_ENERGY).

    Why?
    In my 'base' I have two essential creep-roles: Miners and Collectors.

    The miners just stand near the active sources and mine, dropping all the energy to the ground instantly.

    The second role; Collectors; are a bit more complicated.
    They grab the energy from the ground and transports them to either a spawn, an extension or the the controller (Links are not accounted for yet)
    Currently, I'm using the Creep.pos.findClosest function to find the nearest dropped energy, which has been working great so far but I ran into a problem.

    The findClosest function works great if you only have one source in your room. I currently have 2 miners on two different sources.
    One of the sources is very close to the spawn and the other is a little bit further away.

    The problem right now is that my Collectors only target the source near the spawn, since that is the closest (and will always be the closest).
    So, I was thinking: Is there a way to take the amount of energy on the ground into account when calculating the distance to the dropped energy?

    I currently have this code:

    var _roomData = {};
    Room.prototype.xFind = function(type, filter, sorter, opts)
    {
        if(_roomData[type] == undefined)
        {
            _roomData[type] = this.find(type, opts);
        }
    
        var back = _roomData[type];
    
        if(filter != undefined)
        {
            back = _.filter(back, filter);
        }
    
        if(sorter != undefined)
        {
            back.sort(sorter);
        }
    
        return back;
    };
    

    Which is really just a function to cache the results of all the find-function results. The interesting part here is the sorter argument.

    The sorter argument is a function that sorts the results if provided. The function takes two arguments: Object A and Object B.

    I've used a sorter function which looks like this to make my own "findClosest" function:

    function(a, b)
    {
        var ar = creep.pos.getRangeTo(a);
        var br = creep.pos.getRangeTo(b);
    
        if(ar == br)
            return 0;
    
        return (ar > br ? 1 : -1);
    }
    

    This however, only takes the range into account, not the dropped energy (a and b are in this case Energy objects).
    Does anyone have an idea on how to make an equation that takes into account the amount of energy in a & b?

    For example:
    Energy a is 11 tiles away (ar = 11) and has 500 energy (a.energy = 500)
    Energy b is 16 tiles away (br = 16) and has 2500 energy (b.energy = 2500).

    Now, b is not too far away for a, but my current function will always return a (since it's closer)..

    Anyone have any ideas? I really, really suck at math



  • Sorry about the code looking wierd.. for some reason Markdown isn't working properly when I try it 😣



  • Simplest solution: just multiply range and energy amount and see what happens. Or you could normalize the energy amounts to (0,1], or ... . Just be creative and experiment a bit. You could as well factor in the capacity of the carriers and the decay of the dropped energy (maybe it's gone once the creep arrived?).