Coding Errors for "Lua" in most levels after Blind Distance

2 out of 5 levels After Blind Distance are not in the correct scripting language and Causes multiple errors and these errors result in having to delete all of the original script and starting from scratch
After doing some research I found that Lua was added not too long ago i think but for the creators benefit I’ll post the code that allowed me to beat the level and I’ll try to correct the default help code.
Lastly One thing that is making quite a few of the levels hard is that the not equal to function != doesn’t work at all as far as I can tell. Every time I use it it brings a scripting error but I manged to find ways around it.
I got on the level and the text that was suppose to be there was in another Scripting language. ( I think )

Macbook Air 11-inch
OS X El Capitan
1.6 GHz Intel core i5
Firefox
Lua

Blind Distance:

original:



// Tell the wizard the distance to the coming ogres.

// This function finds the nearest enemy and returns the distance to it.
function nearestEnemyDistance() {
    var enemy = hero.findNearestEnemy();
    // If there is no enemy, the function returns 0.
    var result = 0;
    if (enemy) {
        result = hero.distanceTo(enemy);
    }
    return result;
}

while (true) {
    // Call nearestEnemyDistance() and
    // save the result in the variable enemyDistance.
    var enemyDistance = nearestEnemyDistance();
    // If the enemyDistance is greater than 0: 
    
        // Say the value of enemyDistance variable.
        
}

What I think it should be:

-- Tell the wizard the distance to the coming ogres.
-- This function finds the nearest enemy and returns the distance to it.
local function nearestEnemyDistance()
    local enemy = hero:findNearestEnemy()
    -- If there is no enemy, the function returns 0.
    return enemy and hero:distanceTo(Enemy) or 0
end


while true do
    -- Call nearestEnemyDistance() and
    -- save the result in the variable enemyDistance.
    local distance = nearestEnemyDistance()
    -- If the enemyDistance is greater than 0: 
    
        -- Say the value of enemyDistance variable.
 end 

Solution:

local function nearestEnemyDistance()
    local enemy = hero:findNearest(hero:findEnemies())
    return enemy and hero:distanceTo(enemy) or 0
end
while true do
    local distance = nearestEnemyDistance()
    if distance > 0 then
        hero:say(distance)
    end
end

Hit and Freeze:

This One is different the original code was mostly unintelligible to me, and the solution is not guaranteed to work every time.

Original:

-- <%= trapped %>
-- <%= reach %>

-- <%= func_range %>
-- <%= func_return %> True or False
local inAttackRange = function(enemy)
    local distance = hero:distanceTo(enemy)
    -- <%= swords %>
    if distance <= 3 then
        return true
    else
        return false
    end
end

while true do
    -- <%= find_store %>
    
    -- <%= call_func %>
    -- <%= save_f_res %>
    
    -- <%= attack_if_st %> True, <%= attack_if_end %>

end

Solution:

local see = function()
local enemy = hero:findNearestEnemy()
local distance = hero:distanceTo(enemy)
 if distance <3 then 
    return true
        else
    return false
    end
end

loop
    local enemy = hero:findNearestEnemy()
    if see = true then
       hero:attack(enemy)
        else
            hero:shield()
            
    end
end

Spring Thunder:

Original:

// Certain coins and gems attract lightning.
// The hero should only grab silver coins and blue gems.

while (true) {
    var 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" && item.value == 2) {
        hero.moveXY(item.pos.x, item.pos.y);
    }
    // A blue gem has a value of 10.
    // Collect if item.type is equal to "gem"
    // AND item.value is equal to 10.
    
}

What I think it should be:

-- Certain coins and gems attract lightning.
-- The hero should only grab silver coins 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 

Solution:

while true do
    local item = hero:findNearest(hero:findItems())
    if item.type == "coin" and item.value == 2 then
        hero:moveXY(item.pos.x, item.pos.y)
    end
    -- The blue gem has value equals 10.
    -- Collect the item if it has type 'gem' AND its value equals 10.
    if item.type == "gem" and item.value == 10 then
        hero:moveXY(item.pos.x, item.pos.y)
    end
end

Usual Day:

Original:

// Use AND to check existence and type in one statement.

while (true) {
    var enemy = hero.findNearestEnemy();
    // With AND, the type is only checked if enemy exists.
    if (enemy && enemy.type == "munchkin") {
        hero.attack(enemy);
    }
    // Find the nearest item.
    
    // Collect item if it exists and its type is "coin".
    
}


What I think it should be:

-- Defeat munchkins, collect coins. Everything as usual.
-- Use AND to check existence and type in one statement.

while true do
    local enemy = hero:findNearestEnemy()
    -- With AND, the type is only checked if enemy exists.
    if enemy and enemy.type == "munchkin" then
        hero.attack(enemy)
    
    -- Find the nearest item.
    
    -- Collect item if it exists and its type is "coin".
    
end
end

Solution:

while true do
   local enemy = hero:findNearestEnemy()
     local item = hero:findNearestItem()
        if enemy and enemy.type == "munchkin" then
        hero:attack(enemy)
        else
         if item and item.type == "coin" then
        hero:move(item.pos)
       end 
    end 
end

Passing Through

Original:

// Don't insult this tribe of peaceful ogres.

while(true) {
    var item = hero.findNearestItem();
    if(item) {
        // If item.type IS NOT EQUAL TO "gem"
        if(item.type != "gem") {
            // Then follow your pet wolf.
            hero.moveXY(pet.pos.x, pet.pos.y);
        }
        // Else:
        
            // Move to the gem's position.
            
    }
}

What I think it should be:

-- Don't insult this tribe of peaceful ogres.

while true do
    local item = hero:findNearestItem()
    
    if item then
        -- If item.type IS NOT EQUAL TO "gem"
        if item.type != "gem" then
            -- Then follow your pet wolf.
            hero.moveXY(pet.pos.x, pet.pos.y)
        
        else
            
        
            -- Move to the gem's position.
           end 
    end
end

Solution:

while true do 
   local item = hero:findNearestItem()
    if item then 
        if item.type == "mushroom" then
            hero:moveXY(hero.pos.x, pet.pos.y)
            hero:moveXY(pet.pos.x, pet.pos.y)
        else
                 hero:moveXY(item.pos.x, item.pos.y)
end 
end 
end

Useful Competitors:

This one with out the != requires some thinking and got me a little frustrated.

Original:

-- TODO: Convert to 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:
--    enemy = hero.findNearestEnemy()
--    if enemy:
--        if not enemy.type is "peon":
--            hero.attack(enemy)
--    item = hero.findNearestItem()
--    if item:
--        # Gather the item only if the type is NOT equal to "poison".

What I think it should be:

-- 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 enemy.type != "peon" then
                hero.attack(enemy)
        item = hero:findNearestItem()        
        if item then
         -- Gather the item only if the type is NOT equal to "poison".
        end
                end
            end
    end

Solution:

hero:wait(7)
hero:moveXY(8, 34)
while true do
        local item = hero:findNearestItem()
        local enemy = hero:findNearestEnemy()
    if item then
        if item.type == "coin" and item.value == 1 then
            hero:moveXY(item.pos.x, item.pos.y)
            else
        if item.type == "coin" and item.value == 2 then
            hero:moveXY(item.pos.x, item.pos.y)
            else
        if item.type == "coin" and item.value == 3 then
            hero:moveXY(item.pos.x, item.pos.y)
            else
        if item.type == "gem" and item.value == 5 then
            hero:moveXY(item.pos.x, item.pos.y)
            else
        if enemy then
            if enemy.type == "MUNCHKIN" or "THROWER" then
            hero:attack(enemy)
end    
end      
end
end
end
end
end
end

Logical Path:
Its alright nothing wrong with it.

Return to thornbush farm:

Two things wrong with it for some reason the default function maybebuildtrap function upon saying “I should build” he can’t quite make it away in time to get away from the explosion and if you don’t have enough health you will die.
Second, the default script has, function… maybebuildtrap, it needs to be changed to, local function… maybebuildtrap.

original:

-- The function maybeBuildTrap defines TWO parameters!
function maybeBuildTrap (x, y) 
    -- Use x and y as the coordinates to move to.
    self:moveXY(x, y)
    local enemy = self:findNearestEnemy()
    if enemy then
        self:say("I should build!")
        -- Use buildXY to build a "fire-trap" at the given x and y.
        
    end
end

while true do
    -- This calls maybeBuildTrap, with the coordinates of the top entrance.
    maybeBuildTrap(43, 50)
    
    -- Now use maybeBuildTrap at the left entrance!
    
    -- Now use maybeBuildTrap at the bottom entrance!
    
end    

What it should be:

-- The function maybeBuildTrap defines TWO parameters!
local function maybeBuildTrap (x, y) 
    -- Use x and y as the coordinates to move to.
    self:moveXY(x, y)
    local enemy = self:findNearestEnemy()
    if enemy then
        -- Use buildXY to build a "fire-trap" at the given x and y.
        
    end
end

while true do
    -- This calls maybeBuildTrap, with the coordinates of the top entrance.
    maybeBuildTrap(43, 50)
    
    -- Now use maybeBuildTrap at the left entrance!
    
    -- Now use maybeBuildTrap at the bottom entrance!
    
end 

Solution:

 local function maybeBuildTrap (x, y) 
    self:moveXY(x, y)
    local enemy = self:findNearestEnemy()
    if enemy then
        self:buildXY("fire-trap", x, y)
        
    end
end

while true do

    maybeBuildTrap(43, 50)

    maybeBuildTrap(25, 34)

    maybeBuildTrap(43, 20)
end    

After Finding more levels with incorrect scripting I will quit putting what I think it will be solution and original due to there being quite a few.
Instead if asked too I will go in and patch as requested due to there being close to none reports about this and the non-existent use of Lua that I am seeing

@Harry_the_Wanderer
@nick
@Serg

1 Like

Most of the Artisians or level developers are JS mains, so they might accidentally type in Javascript as the sample code. We’re working on fixing these issues. @Harry_the_Wanderer

1 Like

So you have passed this level? In the future, remember to post your code according to the FAQ. https://discourse.codecombat.com/faq

1 Like

Okay good…but next time, you may wanna ask around here. we can’t give you the code but we will try to give you hints and pointers

1 Like

Hey Corey, can you submit the sample code as patches to the levels in the level editors for the Lua sample code? See this link: How to set the sample code for a level

4 Likes

I have found out where the level editor is and I am currently submitting my patches as you asked
It is now done
After going further and progressing more I realized something Approximately 2 out of 5 levels after blind distance have messed coding for Lua. Knowing this, would you like me to go in and patch them too

6 Likes