Re: [Learning LUA] Lesson 3: Values and Operators is up!
Posted: Thu Apr 04, 2013 4:53 pm
Then I definetely need to study the framework more! 

Official Legend of Grimrock Forums
http://www.grimrock.net/forum/
Code: Select all
function checkThreeAlcoves()
-- Make one boolean variable for each alcove
local gemInLeftAlcove = false
local gemInCenterAlcove = false
local gemInRightAlcove = false
-- Check left alcove, and set its variable to true if gem is found
for i in leftAlcove:containedItems() do
if i.name == "gem_red" then
gemInLeftAlcove = true
end
end
-- Check center alcove, and set its variable to true if gem is found
for i in centerAlcove:containedItems() do
if i.name == "gem_red" then
gemInCenterAlcove = true
end
end
-- Check left alcove, and set its variable to true if gem is found
for i in rightAlcove:containedItems() do
if i.name == "gem_red" then
gemInRightAlcove = true
end
end
-- Test if all variables are true and open door
if gemInLeftAlcove and gemInCenterAlcove and gemInRightAlcove then
door:open()
end
end
Code: Select all
function checkOneAlcove(alcove)
-- Check if gem is in alcove and return true
-- Note: When the script finds one it returns true and exits the
-- function right away, not checking further items
for i in alcove:containedItems() do
if i.name == "gem_red" then
return true
end
end
-- If the script managed to get to here, this means a gem was not found. So let's return false:
return false
end
Code: Select all
function checkThreeAlcoves()
local gemInLeftAlcove = false
local gemInCenterAlcove = false
local gemInRightAlcove = false
if checkOneAlcove(leftAlcove) then
gemInLeftAlcove = true
end
if checkOneAlcove(centerAlcove) then
gemInCenterAlcove = true
end
if checkOneAlcove(rightAlcove) then
gemInRightAlcove = true
end
if gemInLeftAlcove and gemInCenterAlcove and gemInRightAlcove then
door:open()
end
end
Code: Select all
function checkThreeAlcoves()
if checkOneAlcove(leftAlcove)
and checkOneAlcove(centerAlcove)
and checkOneAlcove(rightAlcove) then
door:open()
end
end
Code: Select all
-- DRY and Reusable version
function alcoveContainsItem(alcove, item)
for i in alcove:containedItems() do
if i.name == item then
return true
end
end
return false
end
Code: Select all
function checkLeftCenterAndRightAlcovesForRedGems()
if alcoveContainsItem(leftAlcove, "gem_red")
and alcoveContainsItem(centerAlcove, "gem_red")
and alcoveContainsItem(rightAlcove, "gem_red") then
door:open()
end
end
Code: Select all
function checkThreeAlcovesForAnItem(alcove1, alcove2, alcove3, item)
if alcoveContainsItem(alcove1, item)
and alcoveContainsItem(alcove2, item)
and alcoveContainsItem(alcove3, item) then
door:open()
end
end
Code: Select all
function checkTwoAlcovesForAnItem(alcove1, alcove2, item)
function checkThreeAlcovesForAnItem(alcove1, alcove2, alcove3, item)
function checkFourAlcovesForAnItem(alcove1, alcove2, alcove3, alcove4, item)
function checkFiveAlcovesForAnItem(alcove1, alcove2, alcove3, alcove4, alcove5, item)
Code: Select all
function checkAlcovesForAnItem(alcovesTable, item)
local allAlcovesContainTheItem = true
for _, alcove in ipairs(alcovesTable) do
if not(alcoveContainsItem(alcove, item))
allAlcovesContainTheItem = false
end
end
if allAlcovesContainTheItem then
door:open()
end
end
Code: Select all
-- Specific function to call for that puzzle
function openDoorIfAlcovesHaveRedGems()
if checkAlcovesForAnItem({leftAlcove, centerAlcove, rightAlcove}, "gem_red") then
door:open()
end
end
-- Generic function used by the above one
function checkAlcovesForAnItem(alcovesTable, item)
local allAlcovesContainTheItem = true
for _, alcove in ipairs(alcovesTable) do
if not(alcoveContainsItem(alcove, item))
allAlcovesContainTheItem = false
end
end
return allAlcovesContainTheItem
end
-- Generic sub-function used by the above one
function alcoveContainsItem(alcove, item)
for i in alcove:containedItems() do
if i.name == item then
return true
end
end
return false
end
I'm sorry sir, I would like to check whether in the left alcove is a red gem, in the middle there is a green one, and in the rightmost a blue one. How can I do that?Diarmuid wrote:
Enjoy!!!!
Either go with alcoveContainsItem() of Step 4 and simply change the items looked for in the "if" statement or develop Step 8 by moving from a single item to an item table. Naturally the "for" loop then needs to have two index variables instead of one.alois wrote:I'm sorry sir, I would like to check whether in the left alcove is a red gem, in the middle there is a green one, and in the rightmost a blue one. How can I do that?
*I see a forthcoming Step 8*
This is true, thanks for pointing out the code specialization issue. I would have stopped at step 4 probably too in a normal situation, but here I knew that drakkan had a whole puzzle of alcoves like that of different colors (36 alcoves he said), so I kept on doing a function for that.Dr.Disaster wrote:I prefer Step 4 for readability and less time spend for writing code that prolly never gets used again. Why? Beginning with Step 5 code specialization starts so it's reusability is not guaranteed. In example when an alcove needs to be checked for more then one item Step 5 and up are not up for the task without additional code.