LUA patches for other levels in forest

Maniac Munchkins


```lua -- Another chest in the field for the hero to break open! -- Attack the chest to break it open. -- Some munchkins wont stand idly by while you attack it! -- Defend yourself when a munchkin gets too close.

while true do
local enemy = hero:findNearestEnemy()
local distance = hero:distanceTo(enemy)
if hero:isReady(“cleave”) then
– First priority is to cleave if its ready:

elseif distance < 5 then
    -- Attack the nearest munchkin that gets too close:
    
else
    -- Otherwise, try to break open the chest:
    
end

end

<hr> 
Introduction:

Break open a chest while being attacked by groups of munchkins and certain, particularly angry, munchkins.

```lua
if condition1 then
    -- This only happens if condition1 is true.
elseif condition2 then
    -- This only happens if condition1 is false and condition2 is true.
else
    -- This only happens if both condition1 and condition2 are false.
end

Overview:

In this level, the munchkins will periodically attack without the support of their comrades!

Use cleave to defeat any groups of munchkins that get close, but only use it when it’s off cooldown! isReady will help with that.

if hero:isReady("cleave") then
    -- This will only happen when the heros "cleave" is ready to be used.
end

Check if munchkins get to close using the distanceTo method. Remember that distanceTo finds a number between the hero and the argument.

Note that the less-than sign, or < is only useful at comparing two numbers! hero.findNearestEnemy() returns an enemy, not a number! hero.isReady() returns a true or false value, not a number! Be sure to only use < when comparing 2 numbers, like 5, or hero.distanceTo(enemy).

local distance = hero:distanceTo(enemy)
if hero:isReady("cleave") then
-- elseif is a special term! It tells the hero to not do the next part if the first part was True, or tells them to do the second part if the first part wasnt True.
elseif distance < 5 then
    -- This will only happen when the enemy is closer than 5 meters and cleave isnt ready.
end

Finally, if cleave isn’t ready and the nearest munchkin is more than 5 meters away, you’re free to attack the chest!

-- Remember that else only happens when the other if-statements were False.
else 
    -- This will only happen when there isnt an enemy closer than 5 meters and cleave isnt ready.
end

Blind Distance


```lua -- Its an ambush and your only weapon is the old, blind wizard. -- Your task is to tell him the distance to the coming ogres.

– This function finds the nearest enemy and returns the distance to it.
– If there is no enemy, the function returns 0.
local nearestEnemyDistance = function()
local enemy = hero:findNearestEnemy()
local result = 0
if enemy then
local result = hero:distanceTo(enemy)
end
return result
end

while true do
– Call nearestEnemyDistance() and
– save the result in the variable "distance"
local distance = nearestEnemyDistance()
– If the variable “distance” is greater than 0:

    -- Say the value of "distance" variable.

end

<hr>
Introduction:

That village is too quiet. Looks like it's an ambush.
The blind wizard is your only friend, but he is a really powerful mage.
You will be the spotter for him. Watch for ogres and say the distance for any incoming.
Don't worry about direction, the distance is enough.
The wizard's powers are limited, use them **only when see an ogre**.

Use the predefined function that finds the nearest enemy and returns the distance to it (or 0 if no enemy).
You can use function result in your code if you store it in a variable.

```lua
local enemy = hero:findNearestEnemy()

Overview:

Functions can contain several (or many) instructions to make you can clearer and more readable.
Also, functions allow avoiding repetition of code.

The function can return values and you can use them to get some data from them.
You’ve met it before, when you used hero.findNearestEnemy().

To return a value from a function, use the keyword return in the function.
Place the value (or variable) which you want to return after that.

local someFunction = function()
    ...
    return 3 -- the function returns 3.
end

You can save function’s result in a variable and use it further in your code:

local x = someFunction()
-- Now x equals 3
hero:say(x)

Salted Earth


```lua -- Ogres are attacking a nearby settlement! -- Be careful, though, for the ogres have sown the ground with poison. -- Gather coins and defeat the ogres, but avoid the burls and poison!

while true do
local enemy = hero:findNearestEnemy()
if enemy.type == “munchkin” or enemy.type == “thrower” then
hero:attack(enemy)
end
local item = hero:findNearestItem()
– Check the item type to make sure the hero doesnt pick up poison!
– Look for types: ‘gem’ and ‘coin’

end

<hr>
Introduction:

This level introduces the concept of the `boolean or`. 

Placing an `or` between two boolean values will return a single boolean value, much like `+` takes 2 numbers and spits out a another number.

`or` returns `true` if either the value before or after is `true`, or `false` if both are `false`.

```lua
-- Write boolean or using 'or'
hero:say(False or False) -- Hero says 'False'
hero:say(False or True) -- Hero says 'True'
hero:say(True or False) -- Hero says 'True'
hero:say(True or True) -- Hero says 'True'

Overview:

This level introduces the concept of the boolean or. Placing an or between two boolean values will return a single boolean value, much like + takes 2 numbers and spits out a another number (in this case, the sum).

Remember that booleans are a single bit of data, true or false. or returns true if either the value before or after is true, or false if both are false.

-- Write boolean or using 'or'
hero:say(false or false) -- Hero says 'false'
hero:say(false or true) -- Hero says 'true'
hero:say(true or false) -- Hero says 'true'
hero:say(true or true) -- Hero says 'true'

Which is useful if you know the exact boolean, but, programming lets you do so much more!

Recall that <, >, <=, >=, == ~= return boolean values, so to make this more useful:

local enemy = hero:findNearestEnemy()
-- It helps to read it outloud:
if hero:distanceTo(enemy) < 10 or enemy.type == "thrower" then
    -- If distanceTo enemy is less than 10, OR, enemy type is thrower
    hero:attack(enemy)
end

Hints:

The or operator || is used for checking if either of two conditions are true.

Use an if-statement to check if the nearest item’s type is a "coin" or "gem".

if item.type == "coin" or item.type == "gem" then
    
end

Star Shower


```lua -- Pick up coins only if they are closer than 20m. -- Pick up all gems.

while true do
local item = hero:findNearestItem()
local distance = hero:distanceTo(item)
– If the item’s type is “gem”
– OR the distance to the item less than 20 meters:

    -- Move to item's position.

end

<hr>
Introduction:

A star shower is raining gems and coins down on you!
But star metal isn't long-lived and coins disappear quickly.
Gems don't disappear.

Use an **OR** statement to pick up close coins, OR gems:

```lua
if item.type == "gem" or distance < 20 then
    -- Get the item!
end

Overview:

The logical OR operator can make your code readable and help to avoid repetition.
For example instead several if statements:

if condition1 then
    -- Do something

end
if condition2 then
    -- Do the same again

end    

you can put them in one:

if condition1 or condition2 then
    -- Do something

end    

Avoiding to repeat the same code is a good practice because it makes your code readable.
Also if you want to change some code and logic you can do it one place.

Hit and Freeze


```lua -- You are trapped. Dont move, it ll be painful. -- Attack ogres only when they re within reach.

– This function checks if the enemy is in your attack range.
– The function returns a boolean value: true or false
local inAttackRange = function(enemy)
local distance = hero:distanceTo(enemy)
– Almost all swords have attack range of 3.
if distance <= 3 then
return true
else
return false
end
end

while true do
– Find the nearest enemy and store it in a variable.

-- Call inAttackRange(enemy), with the enemy as the argument
-- and save the result in the variable canAttack.

-- If the result stored in canAttack is true, then attack!

end

<hr>
Introduction:

You're caught in a trap! 
Wait until the ogres are close, then attack, or you'll injure yourself!

Functions can return a value, including a `boolean` value (true or false).

Use this to decide if an ogre is `inAttackRange()`!

```lua
local inAttackRange = function(enemy)
    local distance = hero:distanceTo(enemy)
    if distance <= 3 then
        -- return True because the enemy is in range
    else
        -- return False because the enemy is out of range
    end
end

Save the result to a variable to use it later in the code:

local canAttack = inAttackRange(target)

Overview:

You can have several return statements in a function, but only one will be used, because return causes the function to stop executing, and “returns” back to where the function was called.

local moreThanTen = function(n)
    -- if 'n' greater than 10, then the function will return true.
    if n > 10 then
        return true
    -- Otherwise 'return' inside 'else' will be called and the function will return false.
    else
        return false
    end
end

local isSmall = moreThanTen(5) -- isSmall == true

Useful Competitors


```lua -- The coin field has been seeded with vials of deadly poison. -- Ogres are attacking, while their peons are trying to steal your coins! -- Attack the enemy only if the type is NOT equal to "peon".

while true do
local enemy = hero.findNearestEnemy()
if enemy then
if not enemy.type == “peon” then
hero:attack(enemy)
end
end
local item = hero:findNearestItem()
if item then
– Gather the item only if the type is NOT equal to “poison”.

end

end

<hr>
Overview::

In this level you'll need to use the `not` operator to filter your enemies and items!

`not` takes the logical inverse of a value and returns it:

```lua
-- Not in LUA is written as: "not".
hero:say(not false) # The hero says 'true'
hero:say(not true) # The hero says 'false'

To use in a conditional statement:

if not hero.isReady('cleave') then
    -- Do something while cleave is on cooldown!
else
    -- Cleave is ready.
end

Hints:

Use an if-statement and not to find items that are not type "poison".

if item.type ~= "poison" then

end

Additional hint

When the items are all gone on the game screen item will equal nil meaning nothing.

Forest Shadow


```lua -- Big ogres cant see you in the forest. -- Attack only the small ogres in the forest. -- Collect coins and gems only. -- Dont leave the forest and dont eat/drink anything.

while true do
– Find the nearest enemy.
– Attack it only if its type is “thrower” or “munchkin”.

-- Find the nearest item.
-- Collect it only if its type is "gem" or "coin".

end

<hr>
Overview:

Remember you can use an **OR** statement to find different kinds of enemies or items:

```lua
if enemy.type == "munchkin" or enemy.type == "thrower" then
    hero:attack(enemy)
end

Coin Hunter


```lua -- To make the training more interesting Senick poisoned you. -- While you arent moving the poison is harmless.

– This function should check if a coin is closer than 20m.
local isCoinClose = function(coin)
– Find the distance to the coin.

-- If the distance is less than 20: 

    -- Return true
    
-- Else:

    -- Return false

end

while true do
local item = hero:findNearestItem()
if item then
– If isCoinClose(item) returns true:
if isCoinClose(item) then
– Move to items x, y position.

    end
end

end

<hr>
Introduction:

The famous hunter Senick agreed to train you!
Coins appear and disappear after a short time. 
Only move to coins that are closer than **20 meters**.

Write a function to decide if you should run for a coin:

```lua
-- coin is passed in as a parameter
local isCoinClose = function(coin)
    -- Return True if the coin is close
    -- Else return False
end

Overview:

You need to write a function which receives a parameter coin (an item),
finds the distance from the hero to that coin and decides if it close enough.

To get the distance to a coin use:

local distance = hero:distanceTo(coin)

To decide if it’s close enough (distance less than 20 metres) use:

if distance < 20 then
    ...
end

Metal Detector


```lua -- The artillery uses coins as a target. -- You ll be the rangefinder for the artillery.

– Write the function.
local coinDistance = function()
– Find the nearest coin,

-- If there is a coin, return the distance to it.

-- Else, return 0 (zero).

end

while true do
local distance = coinDistance()
if distance > 0 then
– Say the distance.

end

end

<hr>

Spring Thunder


```lua -- Certain coins and gems attract lightning. -- The hero should only grab silver and blue gems.

while true do
local item = hero:findNearestItem()
– A silver coin has a value of 2.
– Collect if item.type is equal to “coin”
– AND item.value is equal to 2.
if item.type == “coin” and item.value == 2 then
hero:moveXY(item.pos.x, item.pos.y)
end
– A blue gem has a value of 10.
– Collect if item.type is equal to “gem”
– AND item.value is equal to 10.

end

<hr>
Introduction:

Treasure collecting is a dangerous work when it's a thunderstorm outside.
Some gems and coins attract lightning - you'll want to avoid those!

Use an AND operator to determine if an item is safe to pick up. 

```lua
local item = hero:findNearestItem()
-- Silver coins have a value of 2
if item.type == "coin" and item.value == 2 then
    hero:moveXY(item.pos.x, item.pos.y)
end

A AND B is true only if both A and B are true.


Overview:

This level introduces the concept of the boolean and.

Placing an and between two boolean values will return a single boolean value,
much like * takes 2 numbers and spits out an another number (in this case, the multiplication).

Remember that booleans are a single bit of data, true or false.

and returns true if both the value before and after is true, or false if one (or both) of them is false.

-- Write boolean or using 'and'
hero:say(false && false) -- Hero says 'false'
hero:say(false && true) -- Hero says 'false'
hero:say(true && false) -- Hero says 'false'
hero:say(true && true) -- Hero says 'true'

Recall that <, >, <=, >=, == return boolean values, so to make this more useful:

local item = hero:findNearestItem()
-- It helps to read it outloud:
if hero.distanceTo(item) < 15 and item.type == "potion" then
    -- If distanceTo the item is less than 15, AND, items type is a potion
    hero:moveXY(item.pos.x, item.pos.y)
end
1 Like