Recursion solutions
#1
Posted 24 July 2014 - 08:45 PM
#2
Posted 24 July 2014 - 09:34 PM
local function foo(stuff) --code return foo(more stuff) end
Not really the best way, you should try to "derecurse" your function to a loop structure.
EDIT:
Check out this link: http://www.lua.org/pil/6.3.html
Edited by flaghacker, 24 July 2014 - 09:39 PM.
#3
Posted 24 July 2014 - 09:35 PM
flaghacker, on 24 July 2014 - 09:34 PM, said:
local function foo(stuff) --code return foo(more stuff) end
Not really the best way, you should try to "derecurse" your function to a loop structure.
The problem with that is, I don't want to return, I have code to run after the function is called again.
#4
Posted 24 July 2014 - 09:38 PM
CCDCCCCDC DCCCCDCCC CCCDCCCCD
Where D is a block that is dug out and C is a block that is checked (works pretty well with multiple turtles too) although you do lose some checks on the very edge rows and columns
When I have used recursive methods, it's been more for mining tree leaves to ensure I get a sapling to replant with where I normally cap it at 3, for mining I would say you would probably want to go higher. A pseudocode implementation would be something like this (sorry not at home so I can't just dump you my leaf trimmer script which would be pretty easy to modify)
directions={up="down", down="up",south="north",west="east",north="south",east="west"}
local function recursiveMine(RecurCount, ReturnDirection)
if RecurCount<6 then
for side,antiside in pairs(direction) do
if Block(side)=ore then
dig(side)
go(side)
recursiveMine(RecurCount+1,antiside)
end
end
end
go(ReturnDirection)
end
Edited by hilburn, 24 July 2014 - 09:42 PM.
#5
Posted 24 July 2014 - 09:40 PM
function foo() return foo() end
#6
Posted 24 July 2014 - 09:46 PM
hilburn, on 24 July 2014 - 09:38 PM, said:
CCDCCCCDC DCCCCDCCC CCCDCCCCD
Where D is a block that is dug out and C is a block that is checked (works pretty well with multiple turtles too) although you do lose some checks on the very edge rows and columns
When I have used recursive methods, it's been more for mining tree leaves to ensure I get a sapling to replant with where I normally cap it at 3, for mining I would say you would probably want to go higher. A pseudocode implementation would be something like this (sorry not at home so I can't just dump you my leaf trimmer script which would be pretty easy to modify)
directions={up="down", down="up",south="north",west="east",north="south",east="west"}
local function recursiveMine(RecurCount, ReturnDirection)
if RecurCount<6 then
for side,antiside in pairs(direction) do
if Block(side)=ore then
dig(side)
go(side)
recursiveMine(RecurCount+1,antiside)
end
end
end
go(ReturnDirection)
end
function recursiveFunc( nLevel ) if nLevel > 250 then return end --#bla bla bla checking recursiveFunc( nLevel + 1 ) end
Cranium, on 24 July 2014 - 09:40 PM, said:
function foo() return foo() end
Yes, I know how to user "proper" recursion, but the way I need/want to use it does not allow for that unfortunately. Unless I returned the function, plus what I have left to do after and somehow ran that (lol variable stacking...)
#7
Posted 25 July 2014 - 04:36 AM
Lava lakes will kill that idea. They can get way too large for it. Your limit will be about a six-block-cubed area.
My general advice for recursion is this: There aren't that many applications that "require" it. No matter what the task, it probably isn't needed.
I guess if I were to try to take on this sort of task, I'd adapt this script I made for this "competition" (though unfortunately Konlab decided to butcher my code before running it, I can assure you it does work
Ironically, it uses recursion, but not for the same task. The basic idea is that the turtle can explore an area, and keeps going until it either reaches a certain point or decides it's boxed it. If it reaches a dead end, it refers to a look-up table and paths back to the last location it knew wasn't a dead end.
The recursion (which likely isn't needed at all, I just felt like using it!
So, yeah, I'd rig that to treat ores/lava/whatever as "open spaces" it's allowed to explore through, and everything else as impassable terrain. Once it decides it's boxed in I'd tell it to go back to its starting position.
I'm sure you'll want to use all of your own code, but looking at my pathfinder may at least give you some ideas.
#8
Posted 25 July 2014 - 06:01 AM
#9
Posted 25 July 2014 - 06:11 AM
hilburn, on 25 July 2014 - 06:01 AM, said:
That's not true, you can suck up 256 blocks of lava total, not radius.
#10
Posted 25 July 2014 - 06:45 AM
flaghacker, on 25 July 2014 - 06:11 AM, said:
hilburn, on 25 July 2014 - 06:01 AM, said:
That's not true, you can suck up 256 blocks of lava total, not radius.
Nope, let's pretend we had the (rather unreasonable) situation of a turtle having run into a point where 4 250 block long lines of lava source blocks have converged, going N,E,W and Up. Turtle checks North, finds lava, sucks lava, goes North, stack depth 1 repeat until finally stack depth = 250. Turtle then goes back to where it started as there is no more lava, stack depth 0, then finds lava East... sucks up another 250 lava blocks, returns, West, Up etc, You have sucked up 1000 lava without exceeding a stack depth of 250.
Admittedly it's not actually a radius, but it's close enough, with a fixed stack limit of 250 blocks, the turtle doing something like this could suck up 3.83x10^7 as opposed to 6.54x10^7 blocks, but at that point the number is essentially arbitrarily large
#11
Posted 25 July 2014 - 07:08 AM
The problem is that with the way the average lava lake forms, that figure is very likely to exceed the ~256 block limit. Yes, it's easy to envision set ups where the turtle would be forced to backtrack before hitting it and so could continue through massive areas, but those are very unlikely to actually be encountered in the field, I'm afraid.
#12
Posted 25 July 2014 - 08:29 AM
Anyway my point originally was that lava would not "kill that idea", you may not get all of the lava in a single recursion but you would get a more than sufficient quantity for the purposes of refuelling the turtle and/or preventing the branch being flooded by lava. You can then get the rest of the lava many branches later when you come across the remnants of the lake. Recursion in branch mining turtles is a practical way of maximising ores/fuel, though just mining out a 3x1 strip as it discounts the need for turns, or as chunkloading then becomes an issue, mining out 3 high layers in a quarrylike manner (ignoring junk) is a much better return in terms of ore/hour.
Another response to Yami's original query, if you wanted to achieve this without recursion (though tbh I don't see an issue with using it) you could probably do it in a loop by appending the location to a table, checking for ores, digging out an ore if it exists and going there, or removing the last location from the table, going to the previous last location, checking, digging etc, when that table is length 0 you are back where you started, so you could loop with repeat ... until #pos_table==0 or something
Edited by hilburn, 25 July 2014 - 08:42 AM.
#13
Posted 25 July 2014 - 02:52 PM
1) turtle mines until it has 200 recursion (that's what I set it as)
2) turtle recedes 1 level
3) turtle checks again, and will mine if it finds something
4) turtle has reached 200 again
5) turtle recedes 1 level
6) turtle recedes again, having completed it's check
7) turtle now has 2 recursion levels to mess with...
8) repeat
The table thing is a little more complicated than you think, I've been frustrated at every turn. I think I'm going to end up simply letting the turtle run out of recursion, because really with 200 block range, isn't it going to run out of inventory/fuel space?
Edit: Also the lava thing isn't working, it's identifying the block in front of it (air) as being lava because it picked up the lava below said air block. >
Edited by KingofGamesYami, 25 July 2014 - 03:00 PM.
#14
Posted 25 July 2014 - 04:38 PM
KingofGamesYami, on 25 July 2014 - 02:52 PM, said:
Meh, if it works under CC 1.5x, I'd say that's good enough. Dan'll fix the relevant bugs for 1.6x sooner or later.
#15
Posted 25 July 2014 - 04:39 PM
Bomb Bloke, on 25 July 2014 - 04:38 PM, said:
KingofGamesYami, on 25 July 2014 - 02:52 PM, said:
Meh, if it works under CC 1.5x, I'd say that's good enough. Dan'll fix the relevant bugs for 1.6x sooner or later.
#16
Posted 25 July 2014 - 07:37 PM
Can't test it right now, but I'm 95% sure it shouldn't have any ridiculous bugs
http://pastebin.com/hzDcF1LR
Hope this helps
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











