# Summits gate infinite loop

**URL:** <https://discourse.codecombat.com/t/summits-gate-infinite-loop/29432>\
**Category:** Level Help\
**Created:** [June 25, 2021, 2:21am UTC](https://discourse.codecombat.com/t/summits-gate-infinite-loop/29432 "2021-06-25T02:21:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skoll](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@Skoll](https://discourse.codecombat.com/u/Skoll)\
**Post date:** [June 25, 2021, 2:21am UTC](https://discourse.codecombat.com/t/summits-gate-infinite-loop/29432/1 "2021-06-25T02:21:11Z")

</div>

```python
function fullHeal() {
    hero.say("here");
    var paladins = hero.findByType("paladin", hero.findFriends());
    var p1 = paladins[0];
    var p2 = paladins[1];    
    while (hero.health < hero.maxHealth) {
        if (p1.canCast("heal") && p2.canCast("heal")) {            
            hero.command(p1, "cast", "heal", hero);
            hero.command(p2, "cast", "heal", hero);
        }
        //hero.say("1");        
    }    
}

```

I’ve made this function to fully heal my hero using my 2 paladins. But for some reason this function results in an infinite loop unless i uncomment the “hero.say(“1”)” part. The weird thing is the function is not even executed if i dont have the hero.say part, but still causes an infinite loop elsewhere in my code.

---

<div class="post-metadata">

**Author:** ![Skoll](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@Skoll](https://discourse.codecombat.com/u/Skoll)\
**Post date:** [June 25, 2021, 3:46am UTC](https://discourse.codecombat.com/t/summits-gate-infinite-loop/29432/2 "2021-06-25T03:46:52Z")

</div>

This is my full code. So far i only call stageOne() and fullHeal()

```python
// Fight your way into the Inner Sanctum of the ogre chieftain, and defeat her.
var stage = 1;
function lowestHealthPaladin() {
    var lowestHealth = 99999;
    var lowestFriend = null;
    if (hero.health < hero.maxHealth / 2)
        return hero;
    var friends = hero.findFriends();
    for (var f = 0; f < friends.length; f++) {
        var friend = friends[f];
        if (friend.health < lowestHealth && friend.health < friend.maxHealth) {
            lowestHealth = friend.health;
            lowestFriend = friend;
        }
    }
    return lowestFriend;
}
function commandPaladin(paladin) {
    // Heal the paladin with the lowest health using lowestHealthPaladin()
    // You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    // Paladins can also shield: command(paladin, "shield")
    // And don't forget, they can attack, too!
    var low = lowestHealthPaladin();
    if (paladin.heatlh < paladin.maxHealth / 3) {
        //hero.say("shield");
        hero.command(paladin, "shield");
    } else if (low && paladin.canCast("heal")) {
        hero.command(paladin, "cast", "heal", low);
    } else
        hero.command(paladin, "attack", paladin.findNearestEnemy());
}
function commandArcher(friend) {
}
function commandSoldier(friend) {
}
function commandFriends() {
    // Command your friends.
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        if (friend.type == "archer") {
            commandArcher(friend);
        } else if (friend.type == "soldier") {
            commandSoldier(friend);
        } else if (friend.type == "paladin") {
            commandPaladin(friend);
        }
    }
}
function heroAttack() {
    var enemy = hero.findNearestEnemy();
    if (hero.isReady("jump")) {
        hero.jumpTo(enemy);
    }
    if (enemy && hero.canCast("chain-lightning", enemy)) {
        hero.cast("chain-lightning", enemy);
    }
    if (enemy && hero.isReady("bash"))
        hero.bash(enemy);
    if (enemy)
        hero.attack(enemy);
}
function stageOne() {
    var catapults = hero.findByType("catapult", hero.findEnemies());
    var friends = hero.findFriends();    
    commandFriends(friends, "defend", {
            "x": 1,
            "y": 38
        }); 
    while (catapults[0] && catapults[0].health > 0) {
        hero.attack(catapults[0]);
    }
    while (catapults[1] && catapults[1].health > 0) {
        hero.attack(catapults[1]);
    }
    while (true) {
        var enemy = hero.findNearestEnemy();
        if (enemy && enemy.type != "door") {
            if (hero.canCast("chain-lightning", enemy)) {                
                hero.cast("chain-lightning", enemy);
            } 
            else
                hero.attack(enemy);
        } else {            
            break;
        }
    }
    
    hero.moveXY(92, 34);
    commandFriends(friends, "move", hero.pos);        
}

function fullHeal() {
    //hero.say("here");
    var paladins = hero.findByType("paladin", hero.findFriends());
    var p1 = paladins[0];
    var p2 = paladins[1];    
    while (hero.health < hero.maxHealth) {
        if (p1.canCast("heal") && p2.canCast("heal")) {            
            hero.command(p1, "cast", "heal", hero);
            hero.command(p2, "cast", "heal", hero);
        }
        hero.say("1");        
    }    
}

function commandFriends(friends, command, target) {
    for (var i=0; i < friends.length; i++) {
        hero.command(friends[i], command, target);
    }    
}
stageOne();
fullHeal();
hero.say("full");

```

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [June 25, 2021, 7:08am UTC](https://discourse.codecombat.com/t/summits-gate-infinite-loop/29432/3 "2021-06-25T07:08:52Z")

</div>

Hi Skoll,

Try changing the ‘while’ into an ‘if’ for the function. Also try splitting the paladins, so that if p1 can cast heal then she heals, and if p2 can heal then she heals. I suspect the problem is that if one of them can’t heal then the while loops flicks around lots of times which is detected as an infinite loop.

Jenny

---

<div class="post-metadata">

**Author:** ![wishtalon](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@wishtalon](https://discourse.codecombat.com/u/wishtalon)\
**Post date:** [June 25, 2021, 10:50pm UTC](https://discourse.codecombat.com/t/summits-gate-infinite-loop/29432/4 "2021-06-25T22:50:54Z")

</div>

Try submitting it, then skip

---

<div class="post-metadata">

**Author:** ![Skoll](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@Skoll](https://discourse.codecombat.com/u/Skoll)\
**Post date:** [June 25, 2021, 11:01pm UTC](https://discourse.codecombat.com/t/summits-gate-infinite-loop/29432/5 "2021-06-25T23:01:04Z")

</div>

But if i change the while into an if, then the paladins would only heal once each right? I tried splitting the paladins but that didnt change anything.

---

<div class="post-metadata">

**Author:** ![Hydrobolic](https://avatars.discourse-cdn.com/v4/letter/h/e47c2d/32.png) [@Hydrobolic](https://discourse.codecombat.com/u/Hydrobolic)\
**Post date:** [June 26, 2021, 4:56am UTC](https://discourse.codecombat.com/t/summits-gate-infinite-loop/29432/6 "2021-06-26T04:56:45Z")

</div>

In the case where your hero is not at full health and at least one paladin is unable to heal, your `while` loop has no action in it, and thus will spin forever in an infinite loop. When you include the `hero.say` statement, instead CodeCombat blocks for a few frames every iteration as your hero says something, preventing an infinite loop. Nick comments on the exact behavior [here](https://discourse.codecombat.com/t/help-on-mages-might/29284/16).

How you want to solve this issue is up to you. The `hero.wait` command may come in handy as a replacement for an awkward `hero.say`.

* * *

(A side note- I’d suggest generalizing `fullHeal()` with a `for` loop over paladins so it’s able to utilize more or less than exactly two paladins. [for-of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) loops are worth learning about)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [June 28, 2021, 2:29am UTC](https://discourse.codecombat.com/t/summits-gate-infinite-loop/29432/7 "2021-06-28T02:29:43Z")

</div>

Closed on June 28, 2021, 2:29am UTC

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