Infinite loop on Cloudtrip Treasure (JS)

I am getting an infinite loop in cloudtrip treasure. I know that while true loops can sometimes cause problems so I have while(hero.time <30). Is there anything in the code that can loop infinitly because I cannot tell.


var friends = hero.findFriends();
function compare(num){
        return num == Math.min(distances)
    }
for(var i = 0; i < friends.length; i++){
    var friend = friends[i]
    
    while(hero.time < 30){
        var items = hero.findItems();
        var distances = []
        while(distances.length < 10 || items.length == distances.length||hero.time < 30) {
            for(var j = 0; j < items.length || j < 10; i++){
                var item = items[j]
                distances.push(Math.pow((friend.pos.x-item.pos.x),2)+Math.pow((friend.pos.y-item.pos.y),2))
                }
        }
        var find = items[distances.findIndex(compare)];
        hero.command(friend, "move", find.pos);   
    }
}

1 Like

Are you always doing something in the while loop? If you aren’t it might be the cause of the infinity
loop.

The hero doesn’t do any actions in the 2nd while loop but calculations are still being made and the 2nd while loop is supposed to break out either after the length of “distances” (an array) reaches a certain size

distances over here is not defined, you have to either add it in the parameters or define it before the function.

I just tried declaring the variable before the function and it still infinitely loops. I thought that JS functions only needed you to define the variables when the function is actually run? I also tried substituting Math.min(distances) with just a plain number, both when the function declaration was at the top and the bottom of my code and that also did not work. Sorry for the late reply.

Would you please post your new code?

var friends = hero.findFriends();
var distances
function compare(num){
        return num == Math.min(distances)
    }
for(var i = 0; i < friends.length; i++){
    var friend = friends[i]
    
    while(hero.time < 30){
        var items = hero.findItems();
        distances = []
        while(distances.length < 10 || items.length == distances.length||hero.time < 30) {
            for(var j = 0; j < items.length || j < 10; i++){
                var item = items[j]
                distances.push(Math.pow((friend.pos.x-item.pos.x),2)+Math.pow((friend.pos.y-item.pos.y),2))
                }
        }
        var find = items[distances.findIndex(compare)];
        hero.command(friend, "move", find.pos);   
    }
}

I also tried

var friends = hero.findFriends();
var distances
for(var i = 0; i < friends.length; i++){
    var friend = friends[i]
    
    while(hero.time < 30){
        var items = hero.findItems();
        distances = []
        while(distances.length < 10 || items.length == distances.length||hero.time < 30) {
            for(var j = 0; j < items.length || j < 10; i++){
                var item = items[j]
                distances.push(Math.pow((friend.pos.x-item.pos.x),2)+Math.pow((friend.pos.y-item.pos.y),2))
                }
        }
        var find = items[distances.findIndex(compare)];
        hero.command(friend, "move", find.pos);   
    }
}
function compare(num){
        return num == Math.min(distances)
    }

But both of these still result in a loop

Edit: Just deleted the comments in the code so its easier to read

Try including distances as parameter for the compare function, and enter it when using the function

I tried it and it still infinitely loops
Note: the findIndex takes in arguments weirdly. Taken from :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

var friends = hero.findFriends();
var distances
function compare(num,dist){
        return num == Math.min(dist)
    }
for(var i = 0; i < friends.length; i++){
    var friend = friends[i]
    
    while(hero.time < 30){
        var items = hero.findItems();
        distances = []
        while(distances.length < 10 || items.length == distances.length||hero.time < 30) {
            for(var j = 0; j < items.length || j < 10; i++){
                var item = items[j]
                distances.push(Math.pow((friend.pos.x-item.pos.x),2)+Math.pow((friend.pos.y-item.pos.y),2))
                }
        }
        var find = items[distances.findIndex(compare, distance)];
        hero.command(friend, "move", find.pos);   
    }
}

Update: I have almost completely revamped my code but now the peasant is stuck trying to move to a place after he has already gotten there
Here’s my code now:

function goCoin(fNum){
var friends = hero.findFriends();
var friend = friends[fNum]
var distances = []
var items = hero.findByType("coin", hero.findItems());
 for(var j = 0; j < 10; j++){
     var item = items[j]
     distances.push(Math.sqrt((Math.pow((friend.pos.x-item.pos.x),2)+Math.pow((friend.pos.y-item.pos.y),2))).toPrecision(4))
    }
var cTarget
for(var k =0; k<distances.length; k++){
    if(distances[k]==Math.min(...distances)){
        cTarget =items[k]
        }
    }
    hero.command(friend, "move", {x:cTarget.pos.x, y:cTarget.pos.y});
    hero.command(friend, "move", {x:28, y:52});
}
goCoin(0)
goCoin(0)


Nevermind, I completely changed my code and it now works fine. Thanks anyways.

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.