This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
dadan3141's profile picture

dadan's drones

Started by dadan3141, 22 September 2012 - 12:36 AM
dadan3141 #1
Posted 22 September 2012 - 02:36 AM
This a collection of programs that I wrote. There are for turtles. My first program is a simple goto program. Next I made a simple goto and bomb program. If there are any bugs please tell me or if there are any other features/programs that you would like to see added.


EDIT: EDIT: Any ideas why the goto program only works once? I guess works now now idea why

Goto program

SpoilerThis my goto program for turtles inspired by the decent goto program for turtle by yourefunny. This one adds features such as checking fuel level and a refuel prompt. It requires a <x> <y> <z>. Basic path finding (try forward, if not try up, if not break up) can be used to go underground (not recommended).

EDIT: Bug fixed when value of x or z less then 0

pastebin



tArgs = {...}
face = 0
local up = 0
local fuel
local gx = tonumber(tArgs[1]) --target x pos
local gy = tonumber(tArgs[2]) --target y pos
local gz = tonumber(tArgs[3]) --target z pos
local x, y, z --defines the amount to move
function prepare() --setup
rednet.open("right") --activate right modem
cx,cy,cz = gps.locate(3) -- sets current x, y, z
if #tArgs ~= 3 then -- if incorrect amount of args
print("Please input x, y, z coordinates")
end
fuel = turtle.getFuelLevel() -- the fuel level of robot
print("Fuel Level: "..fuel)
if fuel < 1000 then -- random # choice should be enough for long distance
print("Fuel Level might be too low")
print("Would you like to refuel? yes/no")
input = read() --looking at user input
if  input == "yes" then
  turtle.select(1)
print("Place fuel source in selected slot")
turtle.refuel(20) --refuel with 20
print("Refuelling")
fuel = turtle.getFuelLevel() -- rechecking fuel level
print("Fuel Level is now: ".. fuel)
else
print("Refueling Canceled") -- if person type no
end
else
print("There should be enough fuel for the travel")
print("No refuelling necessary")
end
print("Current position: "..cx..", "..cy..", "..cz) --print cur pos
print("Going to: "..gx..", "..gy..", "..gz) --print target pos
end
function currentFace() --find what pos the turtle is facing
while not turtle.forward() do -- try go forward
if not turtle.up() then -- try to do up
turtle.digUp() -- dig up
end
end
local nx,ny,nz = gps.locate(3) --take new pos
print("New position: "..nx..", "..ny..", "..nz)
if cx < nx then -- this determines where the turtle is facing
face = 3
   elseif cx > nx then
   face = 1
   elseif cz < nz then
   face = 0
   elseif cz > nz then
   face = 2	
   end
   print("I am facing: "..face)
end
function turnToFace(f) --turn until facing desired direction
if face ~= f then
while face ~= f do
turtle.turnRight()
	os.sleep(.1)
face = face + 1
if face == 5 then
face = 0
end  
end
else
	print("Facing the right way")
end
end
function determineFaceX() -- determine the desired way to face on the x axis (1st step)
nx,ny,nz = gps.locate(3)

if gx < nx then
turnToFace(1)
elseif gx > nx then
turnToFace(3)
end
end
function determineFaceZ() -- determine the desired way to face on the z axis (2st step)
if gz < nz then
turnToFace(2)
elseif gz > nz then
turnToFace(4)
end
end
function determineMoveX() -- determine how much to move on the x axis
x = gx - nx
if x < 0 then
x = -x
end
end
function determineMoveY() -- determine how much to move on the y axis
y = gy - ny
end
function determineMoveZ() -- determine how much to move on the z axis
z = gz - nz
if z < 0 then
z = -z
end
end
function move (m) -- the actual movement on x and z axis
if x ==0 or z == 0 then
print("Don't need to move")
else
for i=1, m do
while not turtle.forward() do
up = up + 1
if not turtle.up() then
turtle.digUp()
end
end
end
end
end
function moveY (:)/>/> -- the actual movement on y axis
--print(y)
if y > 0 then
for i=1, y do
turtle.up()
end
elseif y < 0 then
for i = 1,-y do
while not turtle.down() do
turtle.digDown()
end
end
end
for p=1, up do
turtle.down()
end
end
-- calling all necessary function
prepare()
currentFace()
determineFaceX()
determineMoveX()
move(x)
determineFaceZ()
determineMoveZ()
move(z)
determineMoveY()
moveY(y)


Basic GPS bomber

SpoilerThis is a simple program that requires the x, y, z coords of the center of the area that you want to bomb and the radius that you want to bomb around. The radius must be a multiple of 2.

pastebin


tArgs = {...}
local bx = tonumber(tArgs[1])
local by = tonumber(tArgs[2])
local bz = tonumber(tArgs[3])
local size = tonumber(tArgs[4])
cx, cy, cz = gps.locate(3)
--print ("Bomb: "..x..", ",,y,,", "..z)
shell.run("goto",bx,by,bz)
function bomb()
local slot = 2
turtle.select(slot)
while not turtle.placeDown() do
  local wslot = slot
  turtle.select(wslot + 1)
end
rs.setOutput("bottom", true)
os.sleep(.5)
rs.setOutput("bottom", false)
end
function forward(x)
for i=1, x do
  turtle.forward()
end
end
function prepare(c)
turtle.turnRight()
turtle.turnRight()
forward(c+1)
turtle.turnRight()
forward(c)
turtle.turnRight()
forward(1)
end
function bomb1(c)
for i = 1, c do
  bomb()
  forward(1)
end
end
function bomb2(d)
for i = 1, d do
  forward(1)
  bomb()
end
end
function grid(g)
m = g/2
for i = 1, m do
  bomb1(g)
  nextrow1()
  bomb2(g)
  nextrow2()
end
nextrow2()
bomb(g)
end
function nextrow1()
turtle.turnRight()
forward(2)
turtle.turnRight()
end
function nextrow2()
turtle.turnLeft()
forward(2)
turtle.turnLeft()
end
function backtocenter(q)
turtle.turnLeft()
forward(q)
turtle.turnLeft()
forward(q)
end
function backhome(hx,hy,hz)
shell.run("goto",hx,hy,hz)
end
prepare(size)
grid(size)
backtocenter(size)
backhome(cx,cy,cz)
slango20 #2
Posted 12 October 2012 - 04:46 PM
Seems good, may use