Prime pathing help


#1

hello,
i have a problem in prime pathing, i was able to get the hero out of the maze, but whenever i try to get the soldiers out, it just sends them to my position, here is my code

the code
function isprime(number) {
    for (var i = 2; i <= Math.ceil(Math.sqrt(number)); i++) {
        if (number % i === 0) {
            return false;
        }
    }
    return true;
}
var mines = hero.findHazards();
var safemines = [];
var steps = 0;
var inc = true;
for (var i = 0; i < mines.length; i++) {
    var mine = mines[i];
    if (isprime(mine.value)) {
        safemines.push(mine);
    }
}
hero.say(safemines);
var movements = [];
while (true) {
    var movement = null;
    var mine = hero.findNearest(safemines);
    if (mine) {
        hero.moveXY(mine.pos.x,mine.pos.y);
    }
    var greenflag = hero.findFlag("green");
    if (greenflag) {
        hero.pickUpFlag(greenflag);
    }
    var blackflag = hero.findFlag("black");
    if (blackflag) {
        movements.push(hero.pos);
        hero.removeFlag(blackflag);
        hero.say("saved");
    }
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        if (movements.length > (i + 1) * 2) {
            movement = movements[(steps - i * 2)];
            hero.say(steps - i * 2);
            hero.say(movements);
            hero.say(movement);
            hero.command(friend, "move", movement);
            if (inc) {
                steps++;
                inc = false;
            }
        }
    }
    inc = true;
}

as you can see, i am trying to store my hero position when i put a black flag, so that i can move my soldiers after me, each soldier will move after the other soldier has moved 5 movements, but it is not working, the array is always filled with my current hero pos, so it doesn’t store the previous movements as i am trying to do, anyone can help???


#2

Try making the hero wait for the soldiers so they can pass. Because what seems to happening, based on your description, is that you move forward, soldiers follow, but before they reach where you are, you moved forward again, and recommanded them. So the soldiers never reached where they were supposed to go before you started moving to the next mine, which is why they get bombed up (since they were taking shortcuts)

Basically, you need to wait for all of your allies to reach your position before you move on, or they will take a shortcut through the mines (because you commanded them again after you passed the next layer, which overrides your last command) and get killed.

Your code is pretty nice. Some comments describing what the variable movement and movements are would be great. It took me a while to figure out what those variables meant, so it would help the others who are looking at this code if you edit your post and put in some comments.

What to do: Simply make your hero wait until all your troops caught up to you.

Hope this helps! :slight_smile:


#3

Thanks for your help, i already figured it out, but i’ve forgotten to check the replies in her.

i solved it basically by waiting for my soldiers, and i found something super wired, when i command the soldiers to go somewhere, they don’t go exactly there, so there is like a 0.4 difference or something, so i just made a function that checks if the soldier is nearly at the point, and i made a while loop saying move until the soldier finally is there(i found out that even if the condition of the while loop will be false at some point of time, the compiler still thinks it’s an infinite loop, so i added the say move command) and it worked perfectly.