How to flee from an enemy



  • Hello everyone!

    A couple of days ago, someone was attacking me and i didn't have any single line written regarding PvP (he was even more noob than me and i won 😎 ).

    I created a role to fight him, and since he was using only ATTACK parts i created a creep with only RANGED_ATTACK parts that moved backwards when InRangeTo(target, 3) while attacking. My problem is that the only way i found for running off him was to move towards a flag (or spawn, or whatever).

    Is there anything opposite to moveTo (like moveOff?) or any way to move in order to get further and further from a given pos?

    As usual...thanks in davance dear fellows!



  • Well this is one of those things that's going to have different solutions, depending on what you are looking for.

    Personally, I just have them retreat back to the base that created them. I don't want them getting stuck in a room and don't want to waste too much CPU to figuring this out.



  • You should look on the PathFinder. This is easy way to create flee behavior. This is shor example how you can use it. The enemys - this is array of enemy creeps, creep is your creep.

    let path = PathFinder.search(creep.pos, enemys.map(c=>{return{pos:c.pos,range:3}},{flee:true}).path
    creep.moveByPath(path)
    

    You can found more options in the documentation: https://docs.screeps.com/api/#PathFinder