· 6 years ago · Apr 11, 2020, 10:52 PM
1for key, value in pairs({ ["os/programs/advanceExcavate/excavate.lua"] = "ui.input.term=term\nlocal _x,_y,_w,_h=1,1,term.getSize()local savePath=\"os/programs/advanceExcavate/data/move.set\"local setPath=\"os/programs/advanceExcavate/data/data.set\"local startPath=\"os/startup/50-advanceExcavate.lua\"local set=dofile(setPath)local move= {}local inMove=false\nlocal offset=vector.new(0,0, -1)local trashList\nlocal stop=false\nlocal function getData()if fs.exists(savePath)then\nmove=dofile(savePath)vector.convert(move.pos)vector.convert(move.facing)if move.next then\nvector.convert(move.next)end\nelse\nmove.pos=vector.zero:copy()move.facing=vector.forward:copy()move.excavated=0\nend\nif set.pathTrashList then\ntrashList=dofile(set.pathTrashList)end\nend\ngetData()local function getFacingText()if move.facing.x>0 then\nreturn \"Forward\"elseif move.facing.x<0 then\nreturn \"Back\"elseif move.facing.y>0 then\nreturn \"Right\"else\nreturn \"Left\"end\nend\nlocal input=ui.input.new()local drawer=ui.drawer.new(input,_x,_y,_w,_h)ui.buffer.fill(drawer.buffer,\" \",colors.white,colors.white)ui.label.new(drawer,\"Advance Excavate\",theme.label1,_x,_y,_w,1)local label_pos=ui.label.new(drawer,\"Position: \",theme.label2,_x,_y+1,10,1)local label_posX=ui.label.new(drawer,\"X: \" ..move.pos.x,theme.label2,_x+10,_y+1,8,1)local label_posY=ui.label.new(drawer,\"Y: \" ..move.pos.y,theme.label2,_x+19,_y+1,8,1)local label_posZ=ui.label.new(drawer,\"Z: \" ..move.pos.z,theme.label2,_x+28,_y+1,8,1)local label_facing=ui.label.new(drawer,\"Facing: \" ..getFacingText(),theme.label2,_x,_y+2,_w,1)ui.label.new(drawer,\"Size:\",theme.label2,_x,_y+4,11,1)local label_size=ui.label.new(drawer,tostring(set.size),theme.label2,_x+12,_y+4,_w-12,1)ui.label.new(drawer,\"Excavated:\",theme.label2,_x,_y+5,11,1)local label_excavated=ui.label.new(drawer,tostring(move.excavated or 0),theme.label2,_x+12,_y+5,_w-12,1)ui.label.new(drawer,\"Fuel Level:\",theme.label2,_x,_y+6,11,1)local label_fuelLevel=ui.label.new(drawer,\"0 / 0\",theme.label2,_x+12,_y+6,_w-12,1)local text_mode=ui.text.new(drawer,\"Mode:\\n\\n\\n\",theme.label2,_x,_h-4,_w,3)text_mode.scaleH=false\nfunction text_mode:setText(text)self.text=text\nself:recalculate()self:repaint(\"this\")end\nui.label.new(drawer,\"\",theme.label1,_x,_h,_w,1)local button_pause=ui.button.new(drawer,IF(move.pause,\"Resume\",\"Pause\"),theme.button1,_x,_h,8,1)local button_stop=ui.button.new(drawer,\"Stop\",theme.button1,_x+_w-12,_h,6,1)local button_exit=ui.button.new(drawer,\"Exit\",theme.button1,_x+_w-6,_h,6,1)local group_main=drawer.selectionManager:addNewGroup()group_main:addElement(button_pause,nil,nil,button_stop,nil)group_main:addElement(button_stop,button_pause,nil,button_exit,nil)group_main:addElement(button_exit,button_stop,nil,nil,nil)drawer.selectionManager:select(button_pause,\"code\",not term.isColor())local function save()table.save(move,savePath)end\nlocal function deleteFiles()if fs.exists(savePath)then\nfs.delete(savePath)end\nif fs.exists(setPath)then\nfs.delete(setPath)end\nif fs.exists(startPath)then\nfs.delete(startPath)end\nend\nlocal function update(f,p,m)if m then\ninMove=true\nend\nif f then\nmove.facing:set(move.facing.y*f.x,move.facing.x*f.y)save()label_facing.text=\"Facing: \" ..getFacingText()label_facing:recalculate()label_facing:repaint(\"this\")end\nif p then\nmove.pos:set(move.pos.x+ (p.x*move.facing.x+p.y*move.facing.y),move.pos.y+ (p.x*move.facing.y+p.y*move.facing.x),move.pos.z+p.z)save()label_posX.text=\"X: \" ..move.pos.x\nlabel_posY.text=\"Y: \" ..move.pos.y\nlabel_posZ.text=\"Z: \" ..move.pos.z\nlabel_posX:recalculate()label_posY:recalculate()label_posZ:recalculate()label_posX:repaint(\"this\")label_posY:repaint(\"this\")label_posZ:repaint(\"this\")end\ninMove=m\nend\nlocal function fillFuel()local data=turtle.getItemDetail(1)for i=2,16 do\nif data then\nif turtle.getItemSpace(1) ==0 then\nreturn\nend\nlocal new=turtle.getItemDetail(i)if new and new.name==data.name and new.damage==data.damage then\nturtle.select(i)turtle.transferTo(1)end\nelse\nturtle.select(i)if turtle.refuel(1)then\nturtle.transferTo(1)data=turtle.getItemDetail(1)end\nend\nend\nend\nlocal function refuelTo(amount)turtle.select(1)while true do\nif turtle.getItemCount(1) <=1 then\nfillFuel()end\nif turtle.getItemCount(1) ==0 then\nreturn false\nelseif amount<turtle.getFuelLevel()then\nreturn true\nend\nturtle.refuel(1)end\nend\nlocal function getMoveDistance(from,to)local v=to-from\nreturn math.abs(v.x) +math.abs(v.y) +math.abs(v.z)end\nlocal function checkFuelLevel(distance)if turtle.getFuelLimit() ==0 then\nreturn true\nend\nlocal level=turtle.getFuelLevel\nif distance<level()then\nturtle.select(1)label_fuelLevel.text=level() ..\" / \" ..distance\nlabel_fuelLevel:recalculate()label_fuelLevel:repaint(\"this\")return true\nend\nif refuelTo(distance)then\nlabel_fuelLevel.text=level() ..\" / \" ..distance\nlabel_fuelLevel:recalculate()label_fuelLevel:repaint(\"this\")return true\nelse\nfillFuel()label_fuelLevel.text=level() ..\" / \" ..distance\nlabel_fuelLevel:recalculate()label_fuelLevel:repaint(\"this\")return refuelTo(distance)end\nend\nlocal function makeSpace()local free=false\nif trashList then\nlocal space=0\nfor i=IF(set.enderChest,3,2),16 do\nlocal ret=not set.trashWhiteList\nlocal data=turtle.getItemDetail(i)if data then\nfor k,v in pairs(trashList)do\nif v[1] ==data.name and IF(v[2],v[2] ==data.damage,true)then\nret=not ret\nbreak\nend\nend\nif ret then\nturtle.select(i)turtle.dropDown()space=space+1\nend\nif space>2 then\nfree=true\nend\nend\nend\nend\nturtle.select(1)return free\nend\nlocal function hasSpace()local free=false\nfor i=IF(set.enderChest,3,2),16 do\nif turtle.getItemCount(i) ==0 then\nreturn true\nend\nend\nfillFuel()for i=IF(set.enderChest,3,2),16 do\nif turtle.getItemCount(i) ==0 then\nfree=true\nbreak\nend\nend\nif not free and trashList then\nfree=makeSpace()end\nreturn free\nend\nlocal function getNextPos(offset,current,size)local v=current-offset\nlocal elevation\nif set.mineMode then\nelevation=math.min((v.z) %6,1) *2-1\nelse\nelevation=math.min((v.z) %2,1) *2-1\nend\nlocal direction= (v.y%2*2-1) * -elevation\nlocal x,y,z=0,0,0\nif(direction<0 and v.x<size)or(direction>0 and v.x>0)then\nx= -direction\nelseif(elevation<0 and v.y<size)or(elevation>0 and v.y>0)then\ny= -elevation\nelse\nif set.mineMode then\nz= -3\nelse\nz= -1\nend\nend\nreturn vector.new(current.x+x,current.y+y,current.z+z)end\nlocal function gotoDestination(v)return turtle.move.go(v-move.pos,update,move.facing)end\nlocal function gotoStart()turtle.move.go(vector.new(0,0, -move.pos.z+math.min(0,offset.z)),update)gotoDestination(vector.zero)end\nlocal function gotoNext()gotoDestination(offset)gotoDestination(vector.new(move.next.x,move.next.y,move.pos.z))gotoDestination(move.next)end\nlocal function setMode(mode)move.mode=mode\nif mode==\"empty_chest\"then\nif set.enderChest==true then\ntext_mode:setText(\"Empty Chest\\nPlace Ender Chest and emptying inventory.\")turtle.select(2)turtle.digUp()turtle.placeUp()for i=3,16 do\nif turtle.getItemCount(i) >0 then\nturtle.select(i)turtle.dropUp()end\nend\nturtle.select(2)turtle.digUp()turtle.select(1)else\ntext_mode:setText(\"Empty Chest\\nGo to Start position.\")gotoStart()turtle.move.look(\"back\",move.facing,update)text_mode:setText(\"Empty Chest\\nEmptying inventory.\")for i=2,16 do\nif turtle.getItemCount(i) >0 then\nturtle.select(i)turtle.drop()end\nend\nturtle.select(1)if not checkFuelLevel(getMoveDistance(move.pos,move.next) *2+2)then\nsetMode(\"request_fuel\")end\ntext_mode:setText(\"Resume mining\\nGo back to position.\")gotoNext()end\nelseif mode==\"request_fuel\"then\ntext_mode:setText(\"Request Fuel\\nGo to Start position.\")gotoStart()local amount=getMoveDistance(vector.zero,move.next) +20\ntext_mode:setText((\"Request Fuel\\nA fuel level of %s is needed.\\nCurrent fuel level is %s.\"):format(amount,turtle.getFuelLevel()))while true do\nif checkFuelLevel(amount)then\nbreak\nend\nos.pullEvent(\"turtle_inventory\")end\ntext_mode:setText(\"Resume mining\\nGo back to position.\")gotoNext()elseif mode==\"stop\"then\nbutton_pause:changeMode(2,true)button_stop:changeMode(2,true)button_exit:changeMode(2,true)text_mode:setText(\"Stop\\nGo back to start position.\")gotoStart()text_mode:setText(\"Stop\\nEmptying inventory.\")if set.enderChest==true then\nturtle.select(2)turtle.digUp()turtle.placeUp()for i=3,16 do\nif turtle.getItemCount(i) >0 then\nturtle.select(i)turtle.dropUp()end\nend\nturtle.select(2)turtle.digUp()turtle.select(1)else\nturtle.move.look(\"back\",move.facing,update)for i=2,16 do\nif turtle.getItemCount(i) >0 then\nturtle.select(i)turtle.drop()end\nend\nturtle.select(1)end\nturtle.move.look(\"forward\",move.facing,update)text_mode:setText(\"Stop\")deleteFiles()input:exit()end\nturtle.select(1)move.mode=nil\nend\nlocal function mine()if move.mode then\nsetMode(move.mode)elseif not hasSpace()then\nsetMode(\"empty_chest\")elseif move.next then\ntext_mode:setText(\"Resume mining\\nGo back to position.\")if not checkFuelLevel(getMoveDistance(move.pos,move.next) +1)then\nsetMode(\"request_fuel\")end\ngotoDestination(vector.new(move.next.x,move.next.y,move.pos.z))gotoDestination(move.next)else\ntext_mode:setText(\"Mining\\nGo to start position.\")if not checkFuelLevel(getMoveDistance(vector.zero,offset) +1)then\nsetMode(\"request_fuel\")end\ngotoDestination(offset)turtle.digUp()turtle.digDown()end\ntext_mode:setText(\"Mining\")repeat\nif not hasSpace()then\nsetMode(\"empty_chest\")text_mode:setText(\"Mining\")end\nif set.mineMode==true then\nif turtle.digUp()and not hasSpace()then\nsetMode(\"empty_chest\")text_mode:setText(\"Mining\")end\nif turtle.digDown()and not hasSpace()then\nsetMode(\"empty_chest\")text_mode:setText(\"Mining\")end\nend\nmove.excavated=move.excavated+3\nlabel_excavated.text=tostring(move.excavated)label_excavated:recalculate()label_excavated:repaint(\"this\")move.next=getNextPos(offset,move.pos,set.size-1)if not checkFuelLevel(getMoveDistance(vector.zero,move.next) +1)then\nsetMode(\"request_fuel\")text_mode:setText(\"Mining\")end\nuntil stop or not gotoDestination(move.next)setMode(\"stop\")end\nlocal checkPause=true\nlocal function waitForPause()while checkPause or inMove==true do\ncoroutine.yield()end\nmove.pause=not move.pause\ncheckPause=true\nsave()if move.pause==true then\nbutton_pause.text=\"Resume\"button_pause:recalculate()button_pause:repaint(\"this\")else\nbutton_pause.text=\"Pause\"button_pause:recalculate()button_pause:repaint(\"this\")end\nend\nfunction button_pause:onClick(event)checkPause=false\nend\nfunction button_stop:onClick(event)stop=true\nbutton_pause:changeMode(2,true)button_stop:changeMode(2,true)button_exit:changeMode(2,true)end\nfunction button_exit:onClick(event)deleteFiles()input:exit()end\nlocal function execute()input:eventLoop({[term] =drawer.event})end\ndrawer:draw()while true do\nif move.pause then\nif parallel.waitForAny(waitForPause,execute) ==2 then\nbreak\nend\nelse\nlocal exit=parallel.waitForAny(mine,waitForPause,execute)if exit==1 then\nbutton_pause:changeMode(2,true)button_stop:changeMode(2,true)button_exit:changeMode(1,true)execute()break\nelseif exit==3 then\nbreak\nend\nend\nend\n",["os/programs/advanceExcavate/main.lua"] = "\nui.input.term=term\nlocal _x,_y,_w,_h=1,1,term.getSize()local success,set=fs.doFile(\"os/programs/advanceExcavate/data/settings.set\")set=set or{}set.trashWhiteList=not(set.trashWhiteList==false)if set.pathTrashList and not fs.exists(set.pathTrashList)then\nset.pathTrashList=nil\nend\nset.enderChest=set.enderChest or false\nset.mineMode=not(set.mineMode==false)set.size=set.size or 0\nlocal input=ui.input.new()local drawer=ui.drawer.new(input,_x,_y,_w,_h)ui.buffer.fill(drawer.buffer,\" \",colors.white,colors.white)local label_title=ui.label.new(drawer,\"Advance Excavate\",theme.label1,_x,_y,_w-3,2)local button_exit=ui.button.new(drawer,\"<\",theme.button2,_x+_w-3,_y,3,1)local label_size=ui.label.new(drawer,\"Length: \",theme.label2,_x,_y+1,8,1)local iField_size=ui.inputField.new(drawer,nil,tostring(set.size),false,theme.iField2,_x+8,_y+1,_w-8,1)local label_trashList=ui.label.new(drawer,\"Trash List\",theme.label2,_x,_y+3,12,1)local toggle_trashList=ui.toggle.new(drawer, {\"White List\",\"Black List\"},set.trashWhiteList,theme.toggle1,13,_y+3,math.floor(_w/2),1)local button_pathTrashList=ui.button.new(drawer,\"Path\",theme.button1,_x,_y+4,6,1)local label_pathTrashList=ui.label.new(drawer,set.pathTrashList or \"No Selection\",theme.label2,_x+7,_y+4,_w-7,1)if not set.pathTrashList then\nlabel_pathTrashList:changeMode(2,true)end\nlocal label_chest=ui.label.new(drawer,\"Chest Mode\",theme.label2,_x,_y+6,12,1)local toggle_chest=ui.toggle.new(drawer, {\"Ender Chest\",\"Normal\"},set.enderChest,theme.toggle1,13,_y+6,math.floor(_w/2),1)local label_mine=ui.label.new(drawer,\"Mine Mode\",theme.label2,_x,_y+8,12,1)local toggle_mine=ui.toggle.new(drawer, {\"Efficient\",\"Radical\"},set.mineMode,theme.toggle1,13,_y+8,math.floor(_w/2),1)local text_info=ui.text.new(drawer,\" \\n \",theme.label2,1,_y+10,_w,_h-9)text_info.scaleH=false\nui.label.new(drawer,\"\",theme.label1,1,_h,_w,1)local button_start=ui.button.new(drawer,\"Start\",theme.button1,_x+_w-7,_h,7,1)local button_saveSettings=ui.button.new(drawer,\"Save Settings\",theme.button1,_x,_h,15,1)local group_menu=drawer.selectionManager:addNewGroup()group_menu.current=button_exit\nlocal group_main=drawer.selectionManager:addNewGroup()group_main.current=iField_size\nlocal group_save=drawer.selectionManager:addNewGroup()group_save.current=button_start\ngroup_menu.previous=group_save\ngroup_main.previous=group_menu\ngroup_save.previous=group_main\ngroup_menu.next=group_main\ngroup_main.next=group_save\ngroup_save.next=group_menu\ngroup_menu:addElement(button_exit,nil,nil,nil,iField_size)group_main:addElement(iField_size,nil,group_menu,nil,toggle_trashList)group_main:addElement(toggle_trashList,nil,iField_size,nil,button_pathTrashList)group_main:addElement(button_pathTrashList,nil,toggle_trashList,nil,toggle_chest)group_main:addElement(toggle_chest,nil,button_pathTrashList,nil,group_save)group_save:addElement(button_saveSettings,nil,toggle_chest,button_start,nil)group_save:addElement(button_start,button_saveSettings,toggle_chest,nil,nil)drawer.selectionManager:select(iField_size,\"code\",not term.isColor())local function updateTrashList()if set.pathTrashList then\ntoggle_trashList:changeMode(1,true)else\ntoggle_trashList:changeMode(2,true)end\nend\nupdateTrashList()local function updateInfoText()local text=\"Insert [tc=red]fuel[tc=clear] in [tc=red]slot 1[tc=clear]\"if toggle_chest._checked then\ntext=text..\" and an [tc=red]Ender Chest[tc=clear] in [tc=red]slot 2[tc=clear]\"end\ntext_info.text=text..\".\"text_info:recalculate()text_info:repaint(\"this\")end\nupdateInfoText()local function updateStart()local canStart=false\nif(tonumber(iField_size.text)or 0) >=1 then\nbutton_start:changeMode(1,true)group_save.current=button_start\nelse\nbutton_start:changeMode(2,true)group_save.current=button_saveSettings\nend\nend\nupdateStart()function button_exit:onClick()input:exit()end\nfunction button_pathTrashList:onClick(event)input:callFunction(function()local path=callfile(\"os/sys/explorer/main.lua\", {mode=\"select_one\",edit=false,select=event.name==\"key_up\",type=\"list\"})if path then\nset.pathTrashList=path\nlabel_pathTrashList.mode=1\nlabel_pathTrashList.text=path\nlabel_pathTrashList:recalculate()end\ndrawer:draw()updateTrashList()end)end\nfunction toggle_chest:onToggle(event,bool)updateInfoText()set.enderChest=bool\nend\nfunction toggle_trashList:onToggle(event,bool)set.trashWhiteList=bool\nend\nfunction iField_size:onTextEdit(event,text)if event==\"change\"then\nupdateStart()set.size=tonumber(text)or 0\nend\nend\nfunction button_saveSettings:onClick(event)table.save(set,\"os/programs/advanceExcavate/data/settings.set\")end\nfunction button_start:onClick(event)input:callFunction(function()term.clear()fs.delete(\"os/programs/advanceExcavate/data/move.set\")fs.delete(\"os/programs/advanceExcavate/data/data.set\")table.save(set,\"os/programs/advanceExcavate/data/data.set\")local file=fs.open(\"os/startup/50-advanceExcavate.lua\",\"w\")file.write('dofile(\"os/programs/advanceExcavate/excavate.lua\")')file.close()dofile(\"os/programs/advanceExcavate/excavate.lua\")input:exit()end)end\ndrawer:draw()input:eventLoop({[term] =drawer.event})\n" }) do
2local s, e = key:find(".*/")
3if s then
4local path = key:sub(s, e)
5if not fs.exists(path) then
6fs.makeDir(path)
7end
8end
9local file = io.open(key, "w+")
10file:write(value)
11file:close()
12end
13sys.require("[1.0.1]Turtle Move API")