• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Wood cutter lua

roriscrave

Advanced OT User
Joined
Dec 7, 2011
Messages
1,188
Solutions
34
Reaction score
200
I need to check the minimum amount of wood for cutting.
each trunk is 12 meters long, need to cut (10 pieces of 3.5m + 8 pieces of 4.5m + 15 pieces of 5m)

exemple, if i cut 12m in 4,5 + 4,5 there will be 3m left, which will be "lost", as it cannot be used in any other cut
if i cut 1x 5 + 2x3.5 there will be 0m left, and rest 0m "lost".

what is the minimum amount of trunk I can use?
I need the solution that always finds the least amount of "wood scraps"

Lua:
local trunk = 12 -- (each wood have 12 meters)

local cuts = {
        -- ex: he need to cut 10 pieces, with 3,5meters each cut
[1] = {count = 10, meter_each = 3.5},
        -- ex: he need to cut 8 pieces, with 4,5meters each cut
[2] = {count = 8, meter_each = 4.5},
        -- ex: he need to cut 15 pieces, with 5meters each cut
[3] = {count = 15, meter_each = 5},

}
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    player:sendTextMessage(22, "The minimum for cutting is X woods and Y meters.")  
    return true
end
 
Here's a Java version. Seems to work ok, but I didn't test much. It's not fully cleaned up either, but I can't spend more time on it now. FWIW I got the size down more than I'd expected.

It was coded and tested in Eclipse (IDE). I can't help much (if at all) with compiling or running it outside the IDE
Java package name is ot.woodsaw. You'll have to create the directories yourself (or use Eclipse).

Feel free to ask questions here, but I probably won't answer Java questions. And remember my warning above - it's probably not an easy port to Lua :)

Remember: it just provides a solution. It doesn't guarantee the best , and generally won't be the best. It also can't tell you how good the provided solution is - to do that would require calculating all possible solutions.
 

Attachments

Here's a Java version. Seems to work ok, but I didn't test much. It's not fully cleaned up either, but I can't spend more time on it now. FWIW I got the size down more than I'd expected.

It was coded and tested in Eclipse (IDE). I can't help much (if at all) with compiling or running it outside the IDE
Java package name is ot.woodsaw. You'll have to create the directories yourself (or use Eclipse).

Feel free to ask questions here, but I probably won't answer Java questions. And remember my warning above - it's probably not an easy port to Lua :)

Remember: it just provides a solution. It doesn't guarantee the best , and generally won't be the best. It also can't tell you how good the provided solution is - to do that would require calculating all possible solutions.
I appreciate your effort in trying to help me, I will take your code and read it calmly.
as soon as i get it to the lua i post it here to the community.
thank you so much
 
Back
Top