# Help on Mages Might!

**URL:** <https://discourse.codecombat.com/t/help-on-mages-might/29284>\
**Category:** Level Help\
**Created:** [June 10, 2021, 12:19pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284 "2021-06-10T12:19:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Myth1cal](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/myth1cal/32/22882_2.png) [@Myth1cal](https://discourse.codecombat.com/u/Myth1cal)\
**Post date:** [June 10, 2021, 12:19pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/1 "2021-06-10T12:19:17Z")

</div>

Is it possible to use the opposite mana on the shield your opponent is using.

I know its possible with `hero.getEnemyHero()` but the problem is I don’t understand how these kinds of code work. I already know that it returns a copy of my hero object which it could be used to get your enemy’s state, I am wondering how I can use this to actually use the state of the enemy.

Can anyone explain?

---

<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 10, 2021, 6:19pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/2 "2021-06-10T18:19:04Z")

</div>

Your own hero has all the methods listed down the center of the screen. The opponent, returned by `hero.getEnemyHero()` has all the same methods, but CodeCombat will only let you call some of them. `hero.getActiveShield()` is one of them.

Example:

```python
opponent = hero.getEnemyHero()
shield = opponent.getActiveShield()
# Do something with shield.mana, which is one of "fire", "earth", "water"

```

* * *

For countering, you want to send the element the enemy shield type is weak against.

```python
affinities = {
  "fire": {"weak": "water", "strong": "earth"},
  "water": {"weak": "earth", "strong": "fire" },
  "earth": {"weak": "fire", "strong": "water"},
}

```

---

<div class="post-metadata">

**Author:** ![goutham123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/goutham123/32/21341_2.png) [@goutham123](https://discourse.codecombat.com/u/goutham123)\
**Post date:** [June 14, 2021, 6:08pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/3 "2021-06-14T18:08:32Z")

</div>

I am getting a error saying that  
 ![Screenshot 2021-06-14 130747](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/7/d/7d56c5b7489838bbbe7aff5173ef4cd85670ce40.png)  
can you please help me

---

<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 15, 2021, 6:36pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/4 "2021-06-15T18:36:38Z")

</div>

> `Cannot read property 'mana' of null`

In the same way that `hero.attack(hero.findNearestEnemy())` does not always work in a normal level (what if there is no enemy at all?), `hero.getActiveShield().mana` will not work if the hero has no shield. You need to check for the existence of the shield first:

```python
shield = hero.getActiveShield()
if shield and shield.mana == "fire":
  pass # Your code here

```

---

<div class="post-metadata">

**Author:** ![goutham123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/goutham123/32/21341_2.png) [@goutham123](https://discourse.codecombat.com/u/goutham123)\
**Post date:** [June 15, 2021, 6:48pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/5 "2021-06-15T18:48:12Z")

</div>

Thanks @Hydrobolic, going to try that out

---

<div class="post-metadata">

**Author:** ![goutham123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/goutham123/32/21341_2.png) [@goutham123](https://discourse.codecombat.com/u/goutham123)\
**Post date:** [June 15, 2021, 7:10pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/6 "2021-06-15T19:10:26Z")

</div>

Umm @Hydrobolic, I have one last question before I venture into fighting other people in mages’ might on my own. Is there anyway I can check the incoming projectiles from the enemy and act accordingly or is there anyway I know what spells the enemy casts??

With Great Thanks,  
goutham123

---

<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 15, 2021, 8:01pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/7 "2021-06-15T20:01:20Z")

</div>

You are not able to directly read what they enemy is casting or read what projectiles are currently traveling, but you are able to make educated guesses on what the opponent has cast by watching their current mana and seeing if it changes. Say, if the opponent’s fire jumps down by 15, you know they just cast a fire ball and can prepare a shield against it.

You are able to read if the opponent is building a collector or not by looking at `opponent.action`.

Keep in mind that you won’t be able to _perfectly_ predict what the opponent is casting as the only times you can run code are when you yourself aren’t casting or building and when a collector chooses its next item. If you are summoning a spell or building a collector, you are mostly blind to the opponent’s actions.

---

<div class="post-metadata">

**Author:** ![goutham123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/goutham123/32/21341_2.png) [@goutham123](https://discourse.codecombat.com/u/goutham123)\
**Post date:** [June 16, 2021, 11:38pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/8 "2021-06-16T23:38:22Z")

</div>

@Hydrobolic there is this spell called “fire-burn”, The animation of the fireball in making comes over the hero’s head, but nothing happens after that, No projectile comes from the hero after that and my mana gets wasted. Is this a bug or is it something else.

---

<div class="post-metadata">

**Author:** ![Code\_Master](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/code_master/32/16184_2.png) [@Code\_Master](https://discourse.codecombat.com/u/Code_Master)\
**Post date:** [June 17, 2021, 12:26am UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/9 "2021-06-17T00:26:51Z")

</div>

The “fire-burn” spell is a spell that damages the opponent’s collectors

---

<div class="post-metadata">

**Author:** ![goutham123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/goutham123/32/21341_2.png) [@goutham123](https://discourse.codecombat.com/u/goutham123)\
**Post date:** [June 17, 2021, 1:06am UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/10 "2021-06-17T01:06:38Z")

</div>

Ohhhhh, now if I look at the collectors I see the fire animation over their heads😅. Thanks @Code_Master

---

<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 17, 2021, 2:03am UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/11 "2021-06-17T02:03:49Z")

</div>

Yep, it’s a spell to directly attack the opponent’s collectors!

The documentation (“hints”) should be the first place you look for this kind of information. It’s briefly mentioned under “special spells.”

---

<div class="post-metadata">

**Author:** ![goutham123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/goutham123/32/21341_2.png) [@goutham123](https://discourse.codecombat.com/u/goutham123)\
**Post date:** [June 23, 2021, 4:39pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/12 "2021-06-23T16:39:07Z")

</div>

Hey everyone, I have a doubt, can you use time and wait in mages might. Or is there some other command to do that

goutham123

---

<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 23, 2021, 7:36pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/13 "2021-06-23T19:36:32Z")

</div>

> [@Mage's might wait](https://discourse.codecombat.com/t/mages-might-wait/29409/6):
>
> Codecombat does not provide the time module, and Mages’ Might does not provide the .wait() function. CodeCombat intercepts your while loops and artifically adds a one-tick delay between iterations. That’s why the code forever: do nothing runs fine. To wait time, just cycle through a while-true loop doing nothing, and the one-tick delays add up. You can either do this in your main loop as JWH suggests or with a utility function to wait inline: // JS function delay(ticks) { while (true) i…

---

<div class="post-metadata">

**Author:** ![Smloh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/smloh/32/21750_2.png) [@Smloh](https://discourse.codecombat.com/u/Smloh)\
**Post date:** [June 24, 2021, 5:47am UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/14 "2021-06-24T05:47:15Z")

</div>

That is interesting to learn. I had been running into issues previously, where a while loop which I thought would let time pass apparently didn’t. I found out it was something like only the outermost while loop that would cause time to pass. (In Python at least). Needed some ugly work-arounds.

---

<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 24, 2021, 6:57am UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/15 "2021-06-24T06:57:17Z")

</div>

I haven’t written competitive Python code in CodeCombat in a while, so I’m not clear on the intricacies of how it works. CodeCombat’s method of forcing a delay between iterations of a `while-true` is rather unnatural and undoubtedly leads to unexpected behaviour.

Reformatting a Mage’s Might program from casting spells in utility functions to focus on the event loop makes it trivial to run commands at arbitrary times, which works around these kinds of problems. In my program I maintain a schedule of spells I’d like to send in the future, so the main loop is as simple as “Forever: If I have a spell scheduled for this tick, cast it.”

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)\
**Post date:** [June 24, 2021, 4:31pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/16 "2021-06-24T16:31:46Z")

</div>

If I recall correctly, we add a one-tick delay to the `while true` loop if there has been no other blocking action called during that while loop frame, but we don’t do that when the `while` loop has some other condition other than a Boolean literal like `True`. There might be some other exceptions, like nesting other `while` loops inside. This should be the same in all languages, not just Python. Code for that is [here](https://github.com/codecombat/codecombat/blob/8811e25df6c22d10b90dc2ac5170af3d1abd3e97/app/lib/aether/interpreter.coffee#L158-L168):

```coffeescript
makeYieldFilter = (aether) -> (engine, evaluator, e) ->

  frame_stack = evaluator.frames
  #console.log x.type + " " + x.ast?.type for x in frame_stack
  #console.log "----"

  top = frame_stack[0]

  if e? and e.type is 'event' and e.event is 'loopBodyStart'

    # Legacy programming languages use 'Literal' whilst C++ and Java use 'BooleanLiteral'.
    if top.srcAst.type is 'WhileStatement' and (top.srcAst.test.type is 'Literal' or top.srcAst.test.type is 'BooleanLiteral')
      if aether.whileLoopMarker?
        currentMark = aether.whileLoopMarker(top)
        if currentMark is top.mark
          # console.log "[Aether] Frame #{this.world.age}: Forcing while-true loop to yield, repeat #{currentMark}"
          top.mark = currentMark + 1
          return true
        else
          # console.log "[Aether] Frame #{this.world.age}: Loop Avoided, mark #{top.mark} isnt #{currentMark}"
          top.mark = currentMark

  if aether._shouldYield
    yieldValue = aether._shouldYield
    aether._shouldYield = false
    frame_stack[1].didYield = true if frame_stack[1].type is 'loop'
    return true

  return false

```

---

<div class="post-metadata">

**Author:** ![goutham123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/goutham123/32/21341_2.png) [@goutham123](https://discourse.codecombat.com/u/goutham123)\
**Post date:** [June 24, 2021, 9:01pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/17 "2021-06-24T21:01:57Z")

</div>

Thanks Everyone, and I have one more question. How does @JWH changes the shield at the correct second wasting my mana and fooling my code. I tried using the opponent shield method I use for the fireball and arrow method. but it doesn’t work. Please help me.

P.S If you have an suggestions for me PLEASE post them I am literally desperate for help…  
and one more thing what is a good level to be in (in regular codecombat)

Thanks A lot,  
goutham123

---

<div class="post-metadata">

**Author:** ![PeterPalov](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/peterpalov/32/45280_2.png) [@PeterPalov](https://discourse.codecombat.com/u/PeterPalov)\
**Post date:** [June 25, 2021, 9:15am UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/18 "2021-06-25T09:15:23Z")

</div>

Hello! Thanks for this arena! It’s interesting and cool. Although I would like to receive some help on Mages Might. I can’t see opponent’s mana. Please, tell me where am I wrong.  
Here is a part of my code:

```python
enFire = 1
enWater = 1
enEarth = 1
def findBall():
    enHero = hero.getEnemyHero()
    fire = enHero.fire
    water = enHero.water
    earth = enHero.earth
    if enHero: 
        if enFire-fire > 1:
            return "water"
        elif enWater-water > 1:
            return "earth"
        elif enEarth-earth > 1:
            return "fire"
        else:
            return "no"
    global enFire
    enFire = fire
    global enWater
    enWater = water
    global enEarth
    enEarth = earth

while True:
    enHero = hero.getEnemyHero()
    hero.chooseItem = chooseMana    
    ball = findBall()
    if not ball == "no":
        if hero.water >= 3 and ball == "water" :
            hero.cast("water-shield")
        elif hero.earth >= 3 and ball == "earth":
            hero.cast("earth-shield")
        elif hero.fire >= 3 and ball == "fire":
            hero.cast("fire-shield")

```

👀  
Unfortunately I do not have much time (I say this quite often now) to play CodeCombat now, so do not wait me on the ladders.  
Also, I would be glad if someone would tell me how to debug here. `hero.say()` does not work so it’s difficult. Another question: can you command collectors to move to the place I want them to go?  
All help would be appreciated. 🙂  
I really like this arena and it reminds me Escort Duty. I like the idea of taking some chests/fruits or something like that and it would be great if there were more arenas as these.

---

<div class="post-metadata">

**Author:** ![goutham123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/goutham123/32/21341_2.png) [@goutham123](https://discourse.codecombat.com/u/goutham123)\
**Post date:** [June 25, 2021, 7:51pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/19 "2021-06-25T19:51:21Z")

</div>

@PeterPalov I am not an epic coder but I might be able to help you on the debug part. I was reading lots of mages might topic for help and I ran through someone with the same situation, and @nick said you could use `console.log` to do the debugging. I will try and find that topic, if I do I’ll send you the link.

glad to help,  
goutham123

---

<div class="post-metadata">

**Author:** ![goutham123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/goutham123/32/21341_2.png) [@goutham123](https://discourse.codecombat.com/u/goutham123)\
**Post date:** [June 25, 2021, 7:54pm UTC](https://discourse.codecombat.com/t/help-on-mages-might/29284/20 "2021-06-25T19:54:32Z")

</div>

I know how to also send the collectors to a specific place, you have to go into the function which had count in it (I think it was a collector function). Then in the return you would have to type `return {"x": x, "y": y}` and it would go to that specific place.

glad to help,  
goutham123

[Next page](https://discourse.codecombat.com/t/help-on-mages-might/29284.md?page=2)
