Friendly/neutral mobs running turtle off course
#1
Posted 02 August 2012 - 07:25 AM
Before I move I am using detect, then trying to dig it if there is something there, then detecting again to see if it clear which will catch if there is bedrock or something there but doesnt seem to help with mobs.
#2
Posted 02 August 2012 - 07:35 AM
example
if turtle.forward() then
print("moved")
else
print("failed")
end
we can make it keep retrying with something likelocal function goForward()
while not turtle.forward() do
sleep(1)
end
end
the above will loop until the turtle moves. call it instead of turtle.forward()just make one for each move function and you are all set
#3
Posted 02 August 2012 - 07:51 AM
#4
Posted 02 August 2012 - 12:25 PM
#5
Posted 02 August 2012 - 02:23 PM
#7
Posted 09 October 2012 - 01:32 PM
#8
Posted 09 October 2012 - 06:19 PM
local function mvdgK(mvcmd, drctn) -- i.e.(turtle.up, "Up")
local itr8 = 0
while not mvcmd() do
local fncnm = "detect"..drctn
if turtle[fncnm]() then
fncnm = "dig"..drctn
if not turtle[fncnm]() then trply("unbreakable block in path") return false end
elseif turtle.getFuelLevel() == 0 then trply( "Out of fuel" ) return false
else
fncnm = "attack"..drctn
turtle[fncnm]()
end
if itr8 > 64 then trply("persistent impediment") return false end
itr8 = itr8+1
end
return true
end
It uses another function, trply(), that is useful for remote control turtles. But it can be replaced with print() if you're not using remote control.
#9
Posted 12 October 2012 - 12:34 AM
ChunLing, on 09 October 2012 - 06:19 PM, said:
local function mvdgK(mvcmd, drctn) -- i.e.(turtle.up, "Up")
local itr8 = 0
while not mvcmd() do
local fncnm = "detect"..drctn
if turtle[fncnm]() then
fncnm = "dig"..drctn
if not turtle[fncnm]() then trply("unbreakable block in path") return false end
elseif turtle.getFuelLevel() == 0 then trply( "Out of fuel" ) return false
else
fncnm = "attack"..drctn
turtle[fncnm]()
end
if itr8 > 64 then trply("persistent impediment") return false end
itr8 = itr8+1
end
return true
end
It uses another function, trply(), that is useful for remote control turtles. But it can be replaced with print() if you're not using remote control.that is one way of doing it. also could you not just send in ("up") then use turtle[mvcmd] to make it less nit picky
#10
Posted 12 October 2012 - 04:14 AM
This function uses the commands turtle.forward, turtle.up, and turtle.down and one of the strings "" (an empty string), "Up", or "Down". It loops while the movement command fails, checking whether there is a block in the way and attempting to mine it if there is, checking fuel, and then just attacking towards the obstacle. It's not very "nit picky", as I understand the term.
#11
Posted 12 October 2012 - 09:15 AM
ChunLing, on 09 October 2012 - 06:19 PM, said:
local function mvdgK(mvcmd, drctn) -- i.e.(turtle.up, "Up")
local itr8 = 0
while not mvcmd() do
local fncnm = "detect"..drctn
if turtle[fncnm]() then
fncnm = "dig"..drctn
if not turtle[fncnm]() then trply("unbreakable block in path") return false end
elseif turtle.getFuelLevel() == 0 then trply( "Out of fuel" ) return false
else
fncnm = "attack"..drctn
turtle[fncnm]()
end
if itr8 > 64 then trply("persistent impediment") return false end
itr8 = itr8+1
end
return true
end
It uses another function, trply(), that is useful for remote control turtles. But it can be replaced with print() if you're not using remote control.Holy f..., can I hire you as a script obfuscator?
On another note, it's really just a matter of checking if you managed to proceed, for example
while not turtle.forward() do sleep(0.1); end
#13
Posted 12 October 2012 - 02:08 PM
Looking at my function, I realize that it is a little confusing to read. But because essentially the same function structure can be used for forward, up, and down, I decided to make a single function that would do any of the three. The code for it is a bit longer than just the code for moving forward, but much shorter than having all three movement functions separate.
And since nothing except the movement function gets executed if the movement succeeds, I don't feel it's too inefficient.
#14
Posted 12 October 2012 - 04:52 PM
ChunLing, on 12 October 2012 - 04:14 AM, said:
This function uses the commands turtle.forward, turtle.up, and turtle.down and one of the strings "" (an empty string), "Up", or "Down". It loops while the movement command fails, checking whether there is a block in the way and attempting to mine it if there is, checking fuel, and then just attacking towards the obstacle. It's not very "nit picky", as I understand the term.
I meant having to give it two variables is could be annoying over time
#15
Posted 12 October 2012 - 05:50 PM
#16
Posted 12 October 2012 - 10:01 PM
As for having two variables, I just go ahead and store the basic versions in three smaller functions, like so:
Quote
dfd = function() return mvdgK(turtle.forward,"") end,
dup = function() return mvdgK(turtle.up,"Up") end,
ddn = function() return mvdgK(turtle.down,"Down") end,
}
#17
Posted 13 October 2012 - 08:37 AM
ChunLing, on 12 October 2012 - 10:01 PM, said:
As for having two variables, I just go ahead and store the basic versions in three smaller functions, like so:
Quote
dfd = function() return mvdgK(turtle.forward,"") end,
dup = function() return mvdgK(turtle.up,"Up") end,
ddn = function() return mvdgK(turtle.down,"Down") end,
}
or you could do this
local function mvdgK(drctn) -- i.e.(turtle.up, "Up")
local mvcmd = turtle[drctn]
local itr8 = 0
while not mvcmd() do
local fncnm = "detect"..drctn
if turtle[fncnm]() then
fncnm = "dig"..drctn
if not turtle[fncnm]() then trply("unbreakable block in path") return false end
elseif turtle.getFuelLevel() == 0 then trply( "Out of fuel" ) return false
else
fncnm = "attack"..drctn
turtle[fncnm]()
end
if itr8 > 64 then trply("persistent impediment") return false end
itr8 = itr8+1
end
return true
end
#18
Posted 13 October 2012 - 08:58 AM
ChunLing, on 12 October 2012 - 10:01 PM, said:
The clue here is that "wellFormedClearVariableNameThatExplainsWhatItsFor" isn't (significantly, if at all) less efficient than "wllFrmdClrVrblNmThtXplnsWhttsFr", it's just a nightmare to try and decipher.
#19
Posted 13 October 2012 - 03:21 PM
BST, I'm sorry but the revised code doesn't work because "turtle.up" and ""turtle.dig/attackUp" are case mismatched, the same is true for down, and forward is an even worse mismatch. Yeah, I might wish that the functions provided by the API were named differently, but we work with what we've got, or we just wish.
But it's true that I could set the values automatically by using a simple table to associate the direction variable with the functions needed. The thing is, for my remote control program I wanted functions that could be called without needing arguments, which the structure I've used gives me. But now that I think about it, there is no reason I shouldn't use a table that points to the existing functions instead of messing with strings. That would be faster and more adaptable.
So, seeing if I can serve two masters...
function movedigKill(direction) -- i.e.(1 = forward, 2 = up, 3 = down)
local itr8 = 0
local action_list = {{turtle.forward,turtle.detect,turtle.dig,turtle.attack},
{turtle.up,turtle.detectUp,turtle.digUp,turtle.attackUp},
{turtle.down,turtle.detectDown,turtle.digDown,turtle.attackDown}}
while not action_list[direction][1]() do
if action_list[direction][2]() then
if not action_list[direction][3]() then trply("unbreakable block in path") return false end
elseif turtle.getFuelLevel() == 0 then trply("Out of fuel" ) return false
else action_list[direction][4]() end
if itr8 > 64 then trply("persistent impediment") return false end
itr8 = itr8+1
end
return true
end
Sorry, looks like I might have failed. But I think I am going to use this new version. It seems faster/better
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











