weight alcove

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
User avatar
Thorham
Posts: 80
Joined: Sat May 04, 2013 5:12 pm

Re: weight alcove

Post by Thorham »

Here's the new version of the script:

Code: Select all

isFinished = false

function checkAlcoves()
    if isFinished == true then
        return
    end

    if testWeight(alcw1, 0.1) and
       testWeight(alcw2, 0.2) and
       testWeight(alcw3, 1.2) and
       testWeight(alcw4, 1.2) then

        iron_weight.door:open()
        playSound("secret")
        isFinished = true
    else
        iron_weight.door:close()
    end
end

function testWeight(alcove, weight)
    local a
    local b = 0
    local c = weight * 10000

    for _, a in alcove.surface:contents() do
        b = b + (a:getWeight() * a:getStackSize() * 10000)
    end

    if round(b) == round(c) then
        return true
    end
    return false
end

function round(a)
    local b, c
    b, c = math.modf(a)

    if c >= 0.5 then
        return b + 1
    end
    return b
end
The above script multiplies all weights by 10000 and then simply rounds like this (included in above script):

Code: Select all

function round(a)
    local b, c
    b, c = math.modf(a)

    if c >= 0.5 then
        return b + 1
    end
    return b
end
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: weight alcove

Post by bongobeat »

works well, thanks! :D
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
Post Reply