code help for sorting creeps by energy



  • Hi again,

    Sorry i realise I should be able to figure this out myself but I just can't. Can anyone help?

     

    //Find a mover to transfer it to
    var transferCreeps = creep.pos.findInRange(FIND_MY_CREEPS, 1);

    transferCreeps.sort(function(a, b) {return b.carry.energy - a.carry.energy});

    creep.transfer(transferCreeps[0], RESOURCE_ENERGY);

     

    I have a digger creep mining resources, and then he has to decide to transfer it to one of the two mover creeps standing next to it. I would want to transfer it always to the same creep, so I attempt to sort via the one carrying more.

    Unfortunately it seems to ignore the sorted list and always gives it to a certain creep over the other. This is most noticable when the priority creep has just walked into range from his walk.

     

    Could someone please let me know what I am doing wrong here - why is it not picking the creep that is currently carrying more.

     

    Thanks,

    Primoz



  • looks like the sorting code should be working fine (tried it just with some random numbers in simulation).

    Looking from the other side, for some reason transferCreeps[0] seems to be the wrong one, so how is this possible?

    I would suggest to add some console.log()s in between the lines to check some things, e.g.

    - the names and energy levels of both creeps,

    - the decision made using transferCreeps[0],

    - maybe just JSON.stringify the array itself before and after sorting.

     

    If i had to guess i'd say for some reason findInRange does not actually find both creeps, so sorting would be irrelevant in that case, maybe also output

    - transferCreeps.length, to check if the array DOES contain both creeps in range

     

     

     

     

     

     

     



  • hmm strange, it suddenly started working apart from it completely breaking since the digger finds himself and tries to pass to himself indefinately - but once he's been removed it just works correctly, i have no idea why it was not working before.

    similarly though a different sort function working on a similar principle is now failing how this one was 😞

    fyi, i put plenty of logs in between and they said that it was sorting correctly, but would still give to the wrong one. at least with the new issue i have its not even sorting, though i believe its because findClosestByPath returns a hash not a distance number.

    targets.sort(function(a, b) {return creep.pos.findClosestByPath(a) - creep.pos.findClosestByPath(b)});



  • yup, using getRangeTo fixed the issue



  • I'd say the problem with including the transferring creep in transferCreeps follows like this: Whenever your digger has the highest energy in the group (which is usually the case) then it will itself be transferCreeps[0] and do nothing. Otherwise, it'll completely ignore any creeps with less energy than it and only transfer to creeps with more energy than itself. Hopefully this explains the weird behavior.