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

[Programs] Big SHiny Toys

Started by BigSHinyToys, 15 May 2012 - 03:42 AM
BigSHinyToys #1
Posted 15 May 2012 - 05:42 AM
This is a collection of programs apis and snippets I have created over the past few months. Some are more polished that others and some are quite difficult to set up. If you have any ideas for improved functionality and stability i would be very grateful for the advice.

snippet safe write
will only write if position is on screen
Spoiler

function safeWrite(this)
local termX,termY = term.getSize()
local curX,curY = term.getCursorPos()
if termX-curX-1 < 0 then
  return
else
  write (string.sub(this,1,termX-curX-1))
  return
end
end

Function Turtle Move (with position tracing)
Spoiler

turX,turY,turZ = 0,0,0
compas = {"n","e","s","w"}
moves = {"U","D","L","R","F","B"}
face = 1 -- 1 north 2 east 3 south 4 west
function move(ins,rep) -- instructions are in moves table rep is the amount of times to repete move ment
if not ins and not rep then
  return false,"error no move specified"
elseif not rep then
  rep = 1
end
for i=1,rep do
  if ins == "U" then -- up move
   if turtle.up() then
	turZ = turZ+1
   else
	return false
   end
  end
  if ins == "D" then -- down move
   if turtle.down() then
	turZ = turZ-1
   else
	return false
   end
  end
  if ins == "L" then -- left turn
   if turtle.turnLeft() then
	face = face - 1
	if face < 1 then
	 face = 4
	end
   end
  end
  if ins == "R" then -- right turn
   if turtle.turnRight() then
	face = face + 1
	if face > 4 then
	 face = 1
	end
   end
  end
  if ins == "F" then -- forward move
   if turtle.forward() then
	if face == 1 then
	 turY = turY+1
	end
	if face == 2 then
	 turX = turX+1
	end
	if face == 3 then
	 turY = turY-1
	end
	if face == 4 then
	 turX = turX-1
	end
   end
  end
  if ins == "B" then -- back move
   if turtle.back() then
	if face == 1 then
	 turY = turY-1
	end
	if face == 2 then
	 turX = turX-1
	end
	if face == 3 then
	 turY = turY+1
	end
	if face == 4 then
	 turX = turX+1
	end
   end
  end
end
end

function simple Menu
Spoiler

--[[
   Basic Menu
   by
   Big Shiny Toys
  
]]--
local function menu(...)
local sel = 1
local list = {...}
local offX,offY = 1,1 -- change this to what ever off set you want
local curX,curY = term.getCursorPos()

while true do
  if sel > #list then sel = #list end
  if sel < 1 then sel = 1 end
  for i = 1,#list do
   term.setCursorPos(offX,offY+i-1)
   if sel == i then
	print("["..list[i].."]")
   else
	print(" "..list[i].." ")
   end
  end
  while true do
   local e,e1,e2,e3,e4,e5 = os.pullEvent()
   if e == "key" then
	if e1 == 200 then -- up key
	 sel = sel-1
	 break
	end
	if e1 == 208 then -- down key
	 sel = sel+1
	 break
	end
	if e1 == 28 then
	 term.setCursorPos(curX,curY)
	 return sel
	end
   end
  end
end
end
term.clear() -- clears all test from screen
print(menu("Item one","Item two","item three","there there")) -- add as many items as you want

TurtleCommand.lua
program that tracks turtle movement and allows user to "drive" the turtle with the W A S D Q E keys
Spoiler

--[[
  Turtle Command
  By
  Big SHiny Toys
]]--
-- start of programs
local tProgs = {
manCon = function()
  while true do
   local e,e1,e2,e3,e4,e5 = coroutine.yield()
   if e == "char" then
	if e1 == "w" then
	 move("F")
	end
	if e1 == "s" then
	 move("B")
	end
	if e1 == "a" then
	 move("L")
	end
	if e1 == "d" then
	 move("R")
	end
	if e1 == "q" then
	 move("U")
	end
	if e1 == "e" then
	 move("D")
	end
   end
  end
end,
location = function()
  while true do
   local x,x1 = coroutine.yield()
   if not menuStatus then
	-- term.clear()
	-- term.setCursorPos(1,1)
	print("LOC X:"..turX.." Y:"..turY.." Z:"..turZ.." Compas: "..compas[face])
	if gpsX then
	 print("GPS X:"..gpsX.." Y:"..gpsY.." Z:"..gpsZ)
	else
	 print("gps unavalible")
	end
   end
   if x == "redstone" then
	term.setCursorPos(1,4)
	print("Redstone signal")
   end
  end
end,
gps = function()
  local pastX,pastY,pastZ,pastV = turX,turY,turZ,face
  local w,w1,w2,w3,w4,w5
  while true do
   w,w1,w2,w3,w4,w5 = coroutine.yield()
   -- if w == "char" and w1 == "t" then break end
   if pastX == turX and pastY == turY and pastZ == turZ and pastV == face then
   else
	gpsX,gpsY,gpsZ = gps.locate(1)
	pastX,pastY,pastZ,pastV = turX,turY,turZ,face
   end
  end
end,
menu = function()
  while true do
   coroutine.yield()
   if menuStatus then
	local a = menu("return","GPS Off","Quit")
	if a == 1 then
	 menuStatus = false
	end
	if a == 2 then
	end
	if a == 3 then
	 shutdown = true
	 menuStatus = false
	end
   end
  end
end
}
-- end of programs
-- globals
gpsX,gpsY,gpsZ = nil,nil,nil
turX,turY,turZ = 0,0,0
compas = {"n","e","s","w"}
moves = {"U","D","L","R","F","B"}
face = 1 -- 1 north 2 east 3 south 4 west
routines = {}
routinNames = {}
menuStatus = false
shutdown = false
-- local vars
local i = 1
local e,e1,e2,e3,e4,e5
-- functions
function menu(...)
local sel = 1
local list = {...}
local offX,offY = 1,1 -- change this to what ever off set you want
local curX,curY = term.getCursorPos()
local tX,tY

while true do
  if sel > #list then sel = #list end
  if sel < 1 then sel = 1 end
  for i = 1,#list do
   term.setCursorPos(offX,offY+i-1)
   if sel == i then
	print("["..list[i].."]")
   else
	print(" "..list[i].." ")
   end
  end
  while true do
   tX,tY = term.getCursorPos()
   local e,e1,e2,e3,e4,e5 = os.pullEvent()
   term.setCursorPos(tX,tY)
   if e == "key" then
	if e1 == 200 then -- up key
	 sel = sel-1
	 break
	end
	if e1 == 208 then -- down key
	 sel = sel+1
	 break
	end
	if e1 == 28 then
	 term.setCursorPos(curX,curY)
	 return sel
	end
   end
  end
end
end
function move(ins,rep)
if not ins and not rep then
  return false,"error no move specified"
elseif not rep then
  rep = 1
end
for i=1,rep do
  if ins == "U" then -- up move
   if turtle.up() then
	turZ = turZ+1
   else
	return false
   end
  end
  if ins == "D" then -- down move
   if turtle.down() then
	turZ = turZ-1
   else
	return false
   end
  end
  if ins == "L" then -- left turn
   if turtle.turnLeft() then
	face = face - 1
	if face < 1 then
	 face = 4
	end
   end
  end
  if ins == "R" then -- right turn
   if turtle.turnRight() then
	face = face + 1
	if face > 4 then
	 face = 1
	end
   end
  end
  if ins == "F" then -- forward move
   if turtle.forward() then
	if face == 1 then
	 turY = turY+1
	end
	if face == 2 then
	 turX = turX+1
	end
	if face == 3 then
	 turY = turY-1
	end
	if face == 4 then
	 turX = turX-1
	end
   end
  end
  if ins == "B" then -- back move
   if turtle.back() then
	if face == 1 then
	 turY = turY-1
	end
	if face == 2 then
	 turX = turX-1
	end
	if face == 3 then
	 turY = turY+1
	end
	if face == 4 then
	 turX = turX+1
	end
   end
  end
end
end
function run(fFun)
if fFun == nil then
  return false
else
  if tProgs[fFun] ~= nil then
   routines[#routines+1] = coroutine.create(tProgs[fFun])
   routinNames[#routinNames+1] = fFun
   return true
  end
end
return false
end
local function fExit()
while face ~= 1 do
  move("R")
end
error()
end
-- end of functions

-- start up sequence
rednet.open("right")
gpsX,gpsY,gpsZ = gps.locate(1)
if gpsX then
turX,turY,turZ = gpsX,gpsY,gpsZ
else
turX,turY,turZ = 0,0,0
end
gpsX,gpsY,gpsZ = nil,nil,nil
run("manCon")
run("location")
run("gps")
run("menu")
-- main loop
while true do
e,e1,e2,e3,e4,e5 = os.pullEvent()
term.clear()
term.setCursorPos(1,1)
if shutdown then
  fExit()
end
if e == "key" and e1 == 199 then
  if menuStatus then
   menuStatus = false
  else
   menuStatus = true
  end
end
i = 1
while true do
  if i == #routines+1 then
   break
  end
  if coroutine.status(routines[i]) == "deadened" then
   table.remove(routines,i)
   table.remove(routinNames,i)
  else
   if menuStatus then
	if routinNames[i] == "menu" then
	 coroutine.resume(routines[i],e,e1,e2,e3,e4,e5)
	elseif e == "char" or e == "key" then
	 coroutine.resume(routines[i])
	else
	 coroutine.resume(routines[i],e,e1,e2,e3,e4,e5)
	end
   else
	coroutine.resume(routines[i],e,e1,e2,e3,e4,e5)
   end
   i = i+1
  end
end
end
--[[
print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
move("U")
print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
move("D")
print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
move("F")
print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
move("L")
print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
move("F")
print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])

turX,turY,turZ = gps.locate(2)
local c1,c2,c3
local tab = {}
for i = 1,500 do
local special,speical2 =  moves[math.random(1,6)],math.random(1,6)
write("Ins:"..special.." For:"..speical2.." Res:")
move(special,speical2)
print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
c1,c2,c3 = gps.locate(2)
print("X"..c1.." Y:"..c2.." Z:"..c3)
table.insert(tab,"Ins: "..special.." For: "..speical2.." Res: tur: X:"..turX.." Y:"..turY.." Z:"..turZ.." Face: "..compas[face].." GPS: X:"..c1.." Y:"..c2.." Z:"..c3)
end
while face ~= 1 do
  move("R")
end
print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
c1,c2,c3 = gps.locate(2)
print("X"..c1.." Y:"..c2.." Z:"..c3)
table.insert(tab,"LAST".." Res: tur: X:"..turX.." Y:"..turY.." Z:"..turZ.." Face: "..compas[face].." GPS: X:"..c1.." Y:"..c2.." Z:"..c3)
print("adding to file")
if fs.exists("log") then
file = io.open("log","a")
else
file = io.open("log","w")
end
file:write("n".."== start of log ==".."n")
for i = 1,#tab do
file:write(tab[i].."n")
end
file:close()
print("file add compleet")
]]--

File Manager 50% complete
Spoiler

local function dialogBox(mes,yes,no,ok,cancle,mes2)
local indexer ={}
local sel2 = 1
local sel3 = 1
local with,higth = 26,6
local x13,y13 = term.getSize()
local off1 = (x13/2)-(with/2)
local off2 = (y13/2)-(higth/2)
local barier = string.rep("-",with-2)
local blank = string.rep(" ",with-2)
term.setCursorPos(off1,off2)
write("+"..barier.."+")
term.setCursorPos(off1,off2+1)
write("|"..blank.."|")
term.setCursorPos(off1,off2+2)
write("+"..barier.."+")
term.setCursorPos(off1,off2+3)
write("|"..blank.."|")
term.setCursorPos(off1,off2+4)
write("|"..blank.."|")
term.setCursorPos(off1,off2+5)
write("+"..barier.."+")
if mes ~= nil then
  term.setCursorPos(off1+1,off2+1)
  write(mes)
end
if mes2 ~= nil then
  term.setCursorPos(off1+1,off2+3)
  write(mes2)
end
if yes == 1 then table.insert(indexer,1) end
if no == 1 then table.insert(indexer,2) end
if ok == 1 then table.insert(indexer,3) end
if cancle == 1 then table.insert(indexer,4) end

sel2 = sel3
if sel3 > table.maxn(indexer) then
  sel3 = table.maxn(indexer)
  sel2 = indexer[sel3]
end
if sel3 < 1 then
  sel3 = 1
  sel2 = indexer[1]
end
sel2 = indexer[1]
while true do
  if yes == 1 then
  
   if sel2 == 1 then
	term.setCursorPos(off1+1,off2+4)
	write("[YES]")
   else
	term.setCursorPos(off1+1,off2+4)
	write(" YES ")
   end
  end
  if no == 1 then
   if sel2 == 2 then
	term.setCursorPos(off1+7,off2+4)
	write("[NO]")
   else
	term.setCursorPos(off1+7,off2+4)
	write(" NO ")
   end
  end
  if ok == 1 then
   if sel2 == 3 then
	term.setCursorPos(off1+12,off2+4)
	write("[OK]")
   else
	term.setCursorPos(off1+12,off2+4)
	write(" OK ")
   end
  end
  if cancle == 1 then
   if sel2 == 4 then
	term.setCursorPos(off1+17,off2+4)
	write("[CANCLE]")
   else
	term.setCursorPos(off1+17,off2+4)
	write(" CANCLE ")
   end
  end
   e,e1,e2,e3,e4,e5 = os.pullEvent()
  if e == "key" then
   if e1 == 203 then -- left
	sel3 = sel3-1
   end
   if e1 == 205 then -- right
	sel3 = sel3+1
   end
   if e1 == 28 then -- enter
	if indexer[sel3] == 1 then return true end
	if indexer[sel3] == 2 then return false end
	return indexer[sel3]
   end
  end
  sel2 = sel3
  if sel3 > table.maxn(indexer) then
   sel3 = table.maxn(indexer)
   sel2 = indexer[sel3]
  end
  if sel3 < 1 then
   sel3 = 1
   sel2 = indexer[1]
  end
end
end
local listSize = 0
local parth = {}
local function fileList(mode)
local sParth
local list = {}
local x12,y12 = term.getSize()
local sel = 3
local curent
local offset = 0
local flot = 0
local fileName

local function pointer() -- yes it points lol XD
  if sel > y12-2 then
   sel = y12-2
   offset = offset+1
  end

  if sel < 3 then
   sel = 3
   offset = offset-1
  end
  if offset < 0 then offset = 0 end
  term.setCursorPos(1,sel)
  write(">")
end

local function top()
  sParth = [[]]
  for i = 1,table.maxn(parth) do
   sParth = sParth..parth[i]..[[]]
  end
  list = fs.list(sParth)
  term.setCursorPos(1,1)
  write("Dir: "..sParth)
  local barier = string.rep("-",x12-2)
  term.setCursorPos(1,2)
  write(barier)
end

local function middle()
  local counter
  local specal
  local folder = {}
  local file = {}
  local this = nil
  for i = 1, table.maxn(list) do
   if fs.isDir(sParth..list[i]) then
	table.insert(folder,list[i])
   else
	table.insert(file,list[i])
   end
  end
  table.sort(folder)
  table.sort(file)
  table.insert(folder,1,[[]])
  if offset > table.maxn(folder)+table.maxn(file)- (y12-5) then
  offset = table.maxn(folder)+table.maxn(file)- (y12-5)
  end
  if offset < 0 then
  offset = 0
  end
  for i = 1 , y12-4 do
   if i+offset < table.maxn(folder)+1 then
	term.setCursorPos(3,i+2)
	this = folder[i+offset]
	write("[] "..folder[i+offset])
	counter = i
   else
	if counter == nil then
	 counter = 0
	 flot = offset - table.maxn(folder)
	end
	if i-counter+flot < table.maxn(file)+1 then
	term.setCursorPos(3,i+2)
	this = file[i-counter+flot]
	write("   "..file[i-counter+flot])
	end
   end
   if i+2 == sel then curent = this
   end
   this = nil
  end
  listSize = table.maxn(folder)+table.maxn(file)
end
local function bottom()
local barier = string.rep("-",x12-2)
term.setCursorPos(1,y12-1)
write(barier)
term.setCursorPos(1,y12)
write("File Name: "..tostring(fileName))
term.setCursorPos(x12-string.len(tostring(curent))-1,y12)
write(tostring(curent))
end
local function scroll()
  scBar = math.floor(((sel-3+offset)/(listSize+2))*(y12-2))
  for i = 0,y12-3 do
   term.setCursorPos(x12-2,i+2)
   if scBar == i then
	write("[]")
   else
	write("||")
   end
  end
end

while true do
  term.clear()
  top()
  pointer()
  middle()
  bottom()
  scroll()
  e,e1,e2,e3,e4,e5 = os.pullEvent()
  if e == "key" then
   if e1 == 200 then -- up key
	sel = sel-1
   end
   if e1 == 208 then -- down key
	sel = sel+1
   end
   if e1 == 15 then -- tab
	term.setCursorPos(1,y12)
	term.clearLine()
	write("File Name: ")
	term.setCursorPos(12,y12)
	fileName = io.read()
	specal = true
   end
   if e1 == 29 then -- Ctrl
	if dialogBox("Message",1,1,0,0,"Exit file select") then
	 return false
	end
   end
   if e1 == 28 or specal then -- enter key
	if curent == [[]] then
	 table.remove(parth)
	else
	 if curent ~= nil then
	  if fs.isDir(sParth..curent) then
	   table.insert(parth,curent)
	  end
	 end
	 if mode == "save" then
	  if fs.isDir(sParth..tostring(curent)) or curent == [[]] then
	  else
	   if fileName == nil and curent == nil then
		 dialogBox("Enter a Name",0,0,1,0,"press Tab to add name")
	   else
		if fs.exists(sParth..tostring(curent)) then
		 if fs.isReadOnly(sParth..tostring(curent)) then
		   dialogBox("Error",0,0,1,0,"File is read only")
		  else
		  if dialogBox("Confirm OVER WRITE",1,1,0,0,"Save as:"..sParth..curent) then
		   return sParth..curent
		  end
		 end
		else
		 if dialogBox("Confirm",1,1,0,0,"Save as:"..sParth..fileName) then
		  return sParth..fileName
		 end
		end
	   end
	  end
	 end
	 if mode == "load" then
	  if curent ~= nil then
	   if fs.isDir(sParth..curent) then
		else
		if dialogBox("confirm",1,1,0,0,"load :"..sParth..curent) then
		 return sParth..curent
		else
		end
	   end
	  else
	  dialogBox("Error",0,0,1,0,"No file selected")
	  end
	 end
	 if mode == "file" then
	  if fs.isDir(sParth..tostring(curent)) or curent == [[]] then
	  else
	   if curent == nil then
	   else
		if fs.exists(sParth..tostring(curent)) then
		 return sParth..curent
		end
	   end
	  end
	 end
	end
	specal = false
   end
  end
end
end
local temp
local failed
while true do
temp = fileList("file")
failed = nil
if temp == false then
  term.clear()
  term.setCursorPos(1,1)
  return
else
  failed = shell.run(temp)
  if failed then
   term.clear()
   term.setCursorPos(1,1)
   dialogBox("Program Crashed",0,0,1,0,"unknown cause")
  end
end
end

ALL ABOVE SCRIPTS are released to the general public under no license please redistribute then modify them and do what ever you would like to them. YOU do NOT have to credit me for any part or whole script.

If you have a suggestion for a script please leave me a message and i will consider it.
ComputerCraftFan11 #2
Posted 15 May 2012 - 05:45 AM
Suggestion for the turtleCommand.lua:

You should make it draw a "#" in front of the turtle whenever it detects a block so it will look like a camera / a game with mining :P/>/>
BigSHinyToys #3
Posted 15 May 2012 - 05:49 AM
Suggestion for the turtleCommand.lua:

You should make it draw a "#" in front of the turtle whenever it detects a block so it will look like a camera / a game with mining :P/>/>

I will add this when I add remote wifi control to the system. Thanks for the suggestion