Guest User

CC-OptimineMgr

a guest
Aug 10th, 2016
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --
  2. -- OptimineMgr
  3. -- Bench test for mining Turtles
  4. -- ComputerCraft 1.78
  5. -- Require Command Computer
  6. --
  7. -- author: SukaiPoppuGo
  8. -- licence: CC BY-NC-SA 4.0
  9. -- https://creativecommons.org/licenses/by-nc-sa/4.0/
  10. --
  11.  
  12. if not commands then
  13.     error("OptimineMgr require a Command Computer",-1)
  14.     return
  15. end
  16.  
  17. local settingsFile = "/.settings"
  18. debugMode = true
  19. turtleID = false
  20.  
  21. tw,th = term.getSize()
  22. local sep = string.rep(string.char(131),tw-1)
  23. local function setColor(fg,bg,m)
  24.     m = m or term
  25.     m.setTextColor(fg) m.setBackgroundColor(bg)
  26. end
  27. local com_x,com_y,com_z = commands.getBlockPosition()
  28. local function sound_click()
  29.     if _HOST=="ComputerCraft 1.78 (Minecraft 1.8.9)" then
  30.         commands.playsound("gui.button.press","@p",com_x,com_y,com_z,0.1,0.6,0)
  31.     end
  32. end
  33.  
  34. if not os.getComputerLabel() then os.setComputerLabel("OptimineMgr") end
  35. outputPad = string.len(string.format("#%d %s",os.getComputerID(),os.getComputerLabel()))+2
  36. function output(str)
  37.     if string.len(str) < tw - outputPad then
  38.         str = str..string.rep(" ",tw-outputPad)
  39.     elseif string.len(str) > tw - outputPad then
  40.         str = string.sub(str,1,tw-outputPad)
  41.     end
  42.     term.setCursorPos(outputPad,1)
  43.     setColor(colors.gray, colors.black)
  44.     term.write(str)
  45. end
  46.  
  47. function header()
  48.     term.setCursorPos(1,1)
  49.     term.clear()
  50.     setColor(colors.lightGray,colors.black)
  51.     print(string.format("#%d %s",os.getComputerID(),os.getComputerLabel()))
  52.     print(sep)
  53. end
  54.  
  55. header()
  56.  
  57. --init Turtle
  58. local function giveTurtle(id)
  59.     output("Give Turtle with ID: #"..id) --#
  60.     commands.give("@p","computercraft:CC-TurtleAdvanced",1,0,"{"
  61. .."fuelLevel:"..(opt.turtleFuelLevel and opt.turtleFuelLevel or "100000")..","
  62. ..(opt.turtleEquipLeft and "leftUpgrade:\""..opt.turtleEquipLeft.."\"," or "leftUpgrade:\"minecraft:diamond_pickaxe\",")
  63. ..(opt.turtleEquipRight and "rightUpgrade:\""..opt.turtleEquipRight.."\"," or "")
  64. .."label:\"OptiMiner\","
  65. .."computerID:"..id..","
  66. .."id:\"turtleadv\"}")
  67. end
  68. local function initTurtleID()
  69.     output("Init new Turtle") --#
  70.     commands.setBlock(com_x,com_y+1,com_z,"computercraft:CC-TurtleAdvanced",0,"replace")
  71.     sleep(.3)
  72.     local t = peripheral.wrap("top")
  73.     sleep(.3)
  74.     t.turnOn()
  75.     sleep(.3)
  76.     turtleID = t.getID()
  77.     output("New Turtle ID: #"..turtleID) --#
  78.     commands.setBlock(com_x,com_y+1,com_z,"minecraft:air",0,"replace")
  79.     settings.set("OptiMiner.turtleID",turtleID)
  80.     settings.save(settingsFile)
  81.     return turtleID
  82. end
  83. if fs.exists(settingsFile) then
  84.     output("Load settings: "..settingsFile) --#
  85.     settings.load(settingsFile)
  86.     if not turtleID then
  87.         turtleID = settings.get("OptiMiner.turtleCustomID",settings.get("OptiMiner.turtleID"))
  88.     end
  89. elseif not turtleID then
  90.     giveTurtle(initTurtleID())
  91. end
  92.  
  93.  
  94. -- src: http://minecraft.gamepedia.com/Chunk_format#Mobs
  95. --types:
  96. -- Item, XPOrb
  97. -- Blaze,CaveSpider,Creeper,Enderman,Ghast,Guardian,LavaSlime,PigZombie,Silverfish,Skeleton,Slime,Spider,Witch,Zombie
  98. -- Bat,Chicken,Cow,EntityHorse,MushroomCow,Ozelot,Pig,Rabbit,Sheep,Squid,Wolf
  99. local function kill(chunk,...)
  100.     local entities= {...} or {"!Player"}
  101.     local pos = ""
  102.     if chunk then
  103.         pos = string.format("x=%d,y=%d,z=%d,dx=%d,dy=%d,dz=%d,",
  104.         chunk.x-1,chunk.y-1,chunk.z-1,16,16,16)
  105.     end
  106.     --summon LightningBolt
  107.     local _,_e
  108.     for _,_e in pairs(entities) do
  109.         output("Killing Entity: type=".._e) --#
  110.         commands.kill("@e["..pos.."type=".._e.."]")
  111.         sleep(.1)
  112.     end
  113. end
  114.  
  115. output("Locate target chunk") --#
  116. local targetChunk = {
  117.     x = com_x+16 - (com_x%16),
  118.     y = com_y,
  119.     z = com_z - (com_z%16),
  120. }
  121.  
  122. output("Locate ref chunk") --#
  123. local refChunk = {
  124.     x = targetChunk.x,
  125.     y = targetChunk.y - 17,
  126.     z = targetChunk.z,
  127. }
  128. function fillChunk(chunk,blockName)
  129.     blockName = blockName or "minecraft:stone"
  130.     commands.fill(chunk.x,chunk.y,chunk.z,chunk.x+15,chunk.y+15,chunk.z+15,blockName)
  131. end
  132.  
  133. function clearChunk(chunk)
  134.     output("Clear chunk") --#
  135.     kill(chunk,"!Player","Item")
  136.     fillChunk(chunk,"minecraft:air")
  137. end
  138.  
  139. function markChunk(chunk,blockName)
  140.     output("Mark chunk corners with: "..blockName) --#
  141.     commands.setblock(chunk.x,   chunk.y,   chunk.z,   blockName,0,"replace")
  142.     commands.setblock(chunk.x,   chunk.y+15,chunk.z,   blockName,0,"replace")
  143.     commands.setblock(chunk.x,   chunk.y,   chunk.z+15,blockName,0,"replace")
  144.     commands.setblock(chunk.x,   chunk.y+15,chunk.z+15,blockName,0,"replace")
  145.     commands.setblock(chunk.x+15,chunk.y,   chunk.z,   blockName,0,"replace")
  146.     commands.setblock(chunk.x+15,chunk.y+15,chunk.z,   blockName,0,"replace")
  147.     commands.setblock(chunk.x+15,chunk.y,   chunk.z+15,blockName,0,"replace")
  148.     commands.setblock(chunk.x+15,chunk.y+15,chunk.z+15,blockName,0,"replace")
  149. end
  150.  
  151. function ore(x,y,z,base)
  152.     local seed = math.random(16)
  153.     local oreType
  154.     if seed > 14 and y-base < 6 then oreType = "minecraft:diamond_ore"
  155.     elseif seed > 12 and y-base < 6 then oreType = "minecraft:lapis_ore"
  156.     elseif seed > 10 and y-base < 8 then oreType = "minecraft:redstone_ore"
  157.     elseif seed > 10 and y-base < 10 then oreType = "minecraft:gold_ore"
  158.     elseif seed > 8 and y-base < 12 then oreType = "minecraft:iron_ore"
  159.     else oreType = "minecraft:coal_ore" end
  160.     commands.fill(x,y,z,x+1,y+1,z+1,oreType,0,"replace")
  161.     local _x,_y,_z
  162.     for _x=0,1 do for _y=0,1 do for _z=0,1 do
  163.         local chance = (seed+_x+_y+_z)%3
  164.         if chance==0 then
  165.             commands.setblock(x+_x,y+_y,z+_z,"minecraft:stone",0,"replace")
  166.         end
  167.     end end end
  168. end
  169.  
  170. function ore_populate(chunk)
  171.     output("Populate ores") --#
  172.     local _x,_y,_z
  173.     for _x=1,13 do for _y=0,12 do for _z=1,13 do
  174.         output("Populate ores"..string.rep(".",_x%6)) --#
  175.         if math.random(100)==1 then
  176.             ore(chunk.x+_x,chunk.y+_y,chunk.z+_z,chunk.y)
  177.             sleep(.1)
  178.         end
  179.     end end end
  180.     commands.setblock(
  181.         chunk.x+1+math.random(13),
  182.         chunk.y+1+math.random(7),
  183.         chunk.z+1+math.random(13),
  184.         "minecraft:emerald_ore",0,"replace")
  185. end
  186.  
  187. stoneDataValues = {0,1,3,5}
  188. function bubble(x,y,z, blockType, dataValue)
  189.     local seed = math.random(16)
  190.     local v = seed%3
  191.     dataValue = dataValue or 0
  192.     if not blockType then
  193.         blockType = "minecraft:stone"
  194.         if seed < 4 then blockType = "minecraft:gravel"
  195.         elseif seed < 12 then blockType = "minecraft:dirt"
  196.         else dataValue = stoneDataValues[(seed%4)+1]
  197.         end
  198.     end
  199.     commands.fill(x+1,y,z+1,x+1+v,y+1+(v*2),z+1+v,blockType,dataValue,"replace")
  200.     commands.fill(x,y+1,z+1,x+1+(v*2),y+1+v,z+1+v,blockType,dataValue,"replace")
  201.     commands.fill(x+1,y+1,z,x+1+v,y+1+v,z+1+(v*2),blockType,dataValue,"replace")
  202. end
  203.  
  204. function dirt_populate(chunk)
  205.     output("Populate dirt") --#
  206.     local skip,_x,_y,_z=false
  207.     for _x=1,10 do for _y=0,9 do for _z=1,10 do
  208.         output("Populate dirt"..string.rep(".",_x%5)) --#
  209.         if not skip and math.random(60)==1 then
  210.             bubble(chunk.x+_x,chunk.y+_y,chunk.z+_z)
  211.             sleep(.1)
  212.             skip = true
  213.         end
  214.         skip = false
  215.     end end end
  216. end
  217.  
  218. function hole_populate(chunk)
  219.     output("Populate holes") --#
  220.     local nb,i = math.random(2,8)
  221.     for i=1,nb do
  222.         local _x,_y,_z= math.random(2,9), math.random(1,9), math.random(2,9)
  223.         bubble(chunk.x+_x,chunk.y+_y,chunk.z+_z,"minecraft:air")
  224.         sleep(.1)
  225.     end
  226. end
  227.  
  228. function lava_lake(x,y,z,chunk)
  229.     output("Generate lava lake") --#
  230.     local space_x,space_z = (chunk.x+15-x), (chunk.z+15-z)
  231.     local heigth = math.random(2,4)
  232.     local deep = math.random(math.max(1,heigth-1))
  233.     local h=1
  234.     local y1 = y+h-1
  235.     local x1,z1 = x+math.random(0,space_x-2), z+math.random(0,space_z-2)
  236.     local x2,z2 = x+math.random(space_x-1), z+math.random(space_z-1)
  237.     commands.fill(x1,y1,z1,x2,y1,z2,"minecraft:lava",0,"replace")
  238.     for h=2,heigth do
  239.         y1 = y+h-1
  240.         local filler = h>deep and "air" or "lava"
  241.         x1 = math.max(x,math.min(x+space_x-2, math.random(-2,2)+x1 ))
  242.         z1 = math.max(z,math.min(z+space_z-2, math.random(-2,2)+z1 ))
  243.         x2 = math.max(x,math.min(x+space_x-2, math.random(-2,2)+x2 ))
  244.         z2 = math.max(z,math.min(z+space_z-2, math.random(-2,2)+z2 ))
  245.         commands.fill(x1,y1,z1,x2,y1,z2,"minecraft:"..filler,0,"replace")
  246.     end
  247.     print("---")
  248. end
  249.  
  250. function lava_populate(chunk)
  251.     local nb,i = math.random(1,4)
  252.     for i=1,nb do
  253.         output("Populate lava") --#
  254.         local _x,_y,_z= math.random(3,13), math.random(1,9), math.random(3,13)
  255.         if _x<9 and _y<8 and _z<9 then lava_lake(chunk.x+_x,chunk.y+_y,chunk.z+_z,chunk)
  256.         else liquid_source("lava",chunk,chunk.x+_x,chunk.y+_y,chunk.z+_z)
  257.         end
  258.         sleep(.1)
  259.     end
  260. end
  261.  
  262. function water_lake(chunk,x,y,z)
  263.     output("Generate water lake") --#
  264.     local space_x,space_z = math.min(8,chunk.x+15-x), math.min(8,chunk.z+15-z)
  265.     local heigth = math.random(2,4)
  266.     local deep = math.random(math.max(1,heigth-2))
  267.     local h=1
  268.     local y1,y2 = y+h-1, y+deep-1
  269.     local x1,z1 = x+math.random(0,space_x-2), z+math.random(0,space_z-2)
  270.     local x2,z2 = x+math.random(space_x-1), z+math.random(space_z-1)
  271.     commands.fill(x1,y1,z1,x2,y2,z2,"minecraft:water",0,"replace")
  272.     for h=2,heigth do
  273.         y1 = y+h-1
  274.         y2 = h>deep and y1 or y+deep-1
  275.         local filler = h>deep and "air" or "water"
  276.         x1 = math.max(x,math.min(x+space_x-2, math.random(-1,1)+x1 ))
  277.         z1 = math.max(z,math.min(z+space_z-2, math.random(-1,1)+z1 ))
  278.         x2 = math.max(x,math.min(x+space_x-2, math.random(-1,1)+x2 ))
  279.         z2 = math.max(z,math.min(z+space_z-2, math.random(-1,1)+z2 ))
  280.         commands.fill(x1,y1,z1,x2,y2,z2,"minecraft:"..filler,0,"replace")
  281.     end
  282. end
  283.  
  284. function liquid_source(liquid,chunk,x,y,z)
  285.     output("Generate source of "..liquid) --#
  286.     local deep= y>chunk.y+2 and math.random(0,math.min(6,y-(chunk.y+1))) or 0
  287.     local side = math.random(-1,1)
  288.     local _x,_z = x,z
  289.     if deep>0 and side~=0 then
  290.         if math.random(2)==1 then _x=x+side
  291.         else _z=z+side end
  292.         commands.fill(_x,y,_z,_x,y-deep,_z,"minecraft:air",0,"replace")
  293.     end
  294.     commands.setblock(x,y,z,"minecraft:"..liquid,0,"replace")
  295.     if deep>0 and side~=0 then
  296.         commands.setblock(_x,y,_z,"minecraft:gravel",0,"replace")
  297.         sleep(.1)
  298.         kill(chunk,"FallingSand")
  299.     end
  300. end
  301.  
  302. function water_populate(chunk)
  303.     local nb,i = math.random(1,4)
  304.     for i=1,nb do
  305.         output("Populate water") --#
  306.         local _x,_y,_z= math.random(3,13), math.random(3,13), math.random(3,13)
  307.         if _x<9 and _y<9 and _z<9 and math.random(0,1)==1 then water_lake(chunk,chunk.x+_x,chunk.y+_y,chunk.z+_z)
  308.         else liquid_source("water",chunk,chunk.x+_x,chunk.y+_y,chunk.z+_z)
  309.         end
  310.         sleep(.1)
  311.     end
  312. end
  313.  
  314. --src: http://minecraft.gamepedia.com/Dungeon
  315. local function dungeon_loot()
  316.     output("Generate dungeon loot") --#
  317.     local loot = {}
  318.     --Stack #1
  319.     local slot,size=1,math.random(1,3)
  320.     for slot=1,size do
  321.         local chance = math.random(128)
  322.         if     chance > 107 then table.insert(loot,"id:\"minecraft:name_tag\",Count:1b")
  323.         elseif chance > 87  then table.insert(loot,"id:\"minecraft:saddle\",Count:1b")
  324.         elseif chance > 72  then table.insert(loot,"id:\"minecraft:record_13\",Count:1b")
  325.         elseif chance > 57  then table.insert(loot,"id:\"minecraft:record_cat\",Count:1b")
  326.         elseif chance > 42  then table.insert(loot,"id:\"minecraft:iron_horse_armor\",Count:1b")
  327.         elseif chance > 27  then table.insert(loot,"id:\"minecraft:golden_apple\",Count:1b")
  328.         elseif chance > 17  then table.insert(loot,"id:\"minecraft:enchanted_book\",Count:1b")
  329.         elseif chance > 7   then table.insert(loot,"id:\"minecraft:golden_horse_armor\",Count:1b")
  330.         elseif chance > 2   then table.insert(loot,"id:\"minecraft:diamond_horse_armor\",Count:1b")
  331.         else                     table.insert(loot,"id:\"minecraft:golden_apple\",Count:1b,Damage:1s")
  332.         end
  333.     end
  334.     --Stack #2
  335.     slot,size=1,math.random(1,4)
  336.     for slot=1,size do
  337.         local chance,qty = math.random(116), math.random(4)
  338.         if     chance > 95 then table.insert(loot,"id:\"minecraft:wheat\",Count:"..qty.."b")
  339.         elseif chance > 75 then table.insert(loot,"id:\"minecraft:bread\",Count:"..qty.."b")
  340.         elseif chance > 60 then table.insert(loot,"id:\"minecraft:coal\",Count:"..qty.."b")
  341.         elseif chance > 45 then table.insert(loot,"id:\"minecraft:redstone\",Count:"..qty.."b")
  342.         elseif chance > 35 then table.insert(loot,"id:\"minecraft:melon_seeds\",Count:"..math.max(2,qty).."b")
  343.         elseif chance > 25 then table.insert(loot,"id:\"minecraft:pumpkin_seeds\",Count:"..math.max(2,qty).."b")
  344.         elseif chance > 15 then table.insert(loot,"id:\"minecraft:iron_ingot\",Count:"..qty.."b")
  345.         elseif chance > 5  then table.insert(loot,"id:\"minecraft:bucket\",Count:1b")
  346.         else                    table.insert(loot,"id:\"minecraft:gold_ingot\",Count:"..qty.."b")
  347.         end
  348.     end
  349.     --Stack #3
  350.     for slot=1,3 do
  351.         local chance,qty = math.random(41), math.random(8) 
  352.         if     chance > 30 then table.insert(loot,"id:\"minecraft:bone\",Count:"..qty.."b")
  353.         elseif chance > 20 then table.insert(loot,"id:\"minecraft:gunpowder\",Count:"..qty.."b")
  354.         elseif chance > 10 then table.insert(loot,"id:\"minecraft:rotten_flesh\",Count:"..qty.."b")
  355.         else                    table.insert(loot,"id:\"minecraft:string\",Count:"..qty.."b")
  356.         end
  357.     end
  358.     return loot
  359. end
  360.  
  361. local function shuffle( t )
  362.     output("Shuffle dungeon chest") --#
  363.     local iterations = #t
  364.     local j
  365.     for i = iterations, 2, -1 do
  366.         j = math.random(i)
  367.         t[i], t[j] = t[j], t[i]
  368.     end
  369.     return t
  370. end
  371.  
  372. local function chest_data(loot)
  373.     local data,slotSet,s,slot = "",{},""
  374.     --Add empty slots
  375.     for slot=1,27 do loot[slot] = loot[slot] or false end
  376.     --Randomize
  377.     loot = shuffle(loot)
  378.     --Compile
  379.     for slot=1,27 do
  380.         if loot[slot] then
  381.             data = data..s..(slot-1)..":{Slot:"..(slot-1).."b,"..loot[slot].."}"
  382.             s = ","
  383.         end
  384.     end
  385.     return "Items:["..data.."]"
  386. end
  387.  
  388. --id: Zombie, Spider, Skeleton, CaveSpider, Blaze, Silverfish
  389. local function mobspawner_data(id)
  390.     output("Setup "..id.." mobspawner") --#
  391.     id = id or "Pig"
  392.     return "{EntityId:\""..id.."\"}"
  393. end
  394. function dungeon(x,y,z)
  395.     output("Generate dungeon") --#
  396.     local i
  397.     local Entiy = {"Zombie","Zombie","Spider","Skeleton"}
  398.     local wx,wz = 7+(math.random(0,1)*2),7+(math.random(0,1)*2)
  399.     --clear space and walls
  400.     commands.fill(x,y,z, x+wx-1,y+4,z+wz-1, "minecraft:air",0,"replace")
  401.     commands.fill(x,y,z, x+wx-1,y+4,z+wz-1, "minecraft:cobblestone",0,"outline")
  402.     commands.fill(x+1,y+4,z+1, x+wx-2,y+4,z+wz-2, "minecraft:air",0,"replace")
  403.     --floor
  404.     commands.fill(x,y,z, x+wx-1,y,z+wz-1, "minecraft:mossy_cobblestone",0,"replace")
  405.     for i=1,math.ceil(wx*wz/3) do
  406.         output("Generate dungeon"..string.rep(".",i%5)) --#
  407.         local _x,_z = math.random(0,wx-1),math.random(0,wz-1)
  408.         commands.setblock(x+_x,y,z+_z,"minecraft:cobblestone",0,"replace")
  409.         sleep(.1)
  410.     end
  411.     --Break wall
  412.     for i=1,math.random(3,7) do
  413.         bubble(math.random(x-1,x+wx-4),y+1,math.random(z-1,z+wz-4), "minecraft:air", 0)
  414.         sleep(.1)
  415.     end
  416.     --Place mob spawner
  417.     commands.setblock(x+math.ceil((wx-1)/2),y+1,z+math.ceil((wz-1)/2),
  418.         "minecraft:mob_spawner",0,"replace",
  419.         mobspawner_data(Entiy[math.random(1,4)]))
  420.     --Chests
  421.     if math.random(32)~=1 then
  422.         local place_seed = math.random(1,5)*2
  423.         local place = string.sub("121314232434",place_seed,place_seed+1)
  424.         local chance = math.random(1,2)
  425.         for i=1,chance do
  426.             local side = string.sub(place,i,i)
  427.             local dmg,_x,_z
  428.             --North -z
  429.             if side=="1" then dmg,_x,_z = 3,math.random(1,wx-2),1
  430.             --East +x
  431.             elseif side=="2" then dmg,_x,_z = 4,wx-2,math.random(1,wz-2)
  432.             --South +z
  433.             elseif side=="3" then dmg,_x,_z = 2,math.random(1,wx-1),wz-2
  434.             --West -x
  435.             elseif side=="4" then dmg,_x,_z = 5,1,math.random(1,wz-2)
  436.             end
  437.             commands.setblock(x+_x,y+1,z+_z,
  438.                 "minecraft:chest",dmg,"replace",
  439.                 "{"..chest_data(dungeon_loot()).."}")
  440.             sleep(.1)
  441.         end
  442.     end
  443. end
  444.  
  445. function clone()
  446.     output("Clone ref to target chunk ref chunk") --#
  447.     commands.clone(refChunk.x,refChunk.y,refChunk.z,
  448.         refChunk.x+15,refChunk.y+15,refChunk.z+15,
  449.         targetChunk.x,targetChunk.y,targetChunk.z, "replace")
  450. end
  451.  
  452. local function getSwitchStoneSate()
  453.     local data = commands.getBlockInfo(refChunk.x+8,refChunk.y-3,refChunk.z+8)
  454.     if data.name=="minecraft:glass" then return false
  455.     elseif data.name=="minecraft:stone" then return true
  456.     else
  457.         commands.setblock(refChunk.x+8,refChunk.y-3,refChunk.z+8,"minecraft:stone",0,"replace")
  458.         return true
  459.     end
  460. end
  461.  
  462. settings.load(settingsFile)
  463. opt = {
  464.     stone=getSwitchStoneSate(),
  465.     lava=settings.get("OptiMiner.lava",true),
  466.     water=settings.get("OptiMiner.water",true),
  467.     bedrock=settings.get("OptiMiner.bedrock",true),
  468.     dungeon=settings.get("OptiMiner.dungeon",false),
  469.     turtleCustomID=settings.get("OptiMiner.CustomID",turtleID),
  470.     turtleFuelLevel=settings.get("OptiMiner.turtleFuelLevel",100000),
  471.     turtleEquipLeft=settings.get("OptiMiner.turtleEquipLeft","minecraft:diamond_pickaxe"),
  472.     turtleEquipRight=settings.get("OptiMiner.turtleEquipRight",false),
  473. }
  474.  
  475. function save_params()
  476.     settings.set("OptiMiner.turtleID",turtleID)
  477.     local k,v
  478.     for k,v in pairs(opt) do
  479.         if k~="stone" then
  480.             settings.set("OptiMiner."..k,v)
  481.         end
  482.     end
  483.     settings.save(settingsFile)
  484. end
  485. save_params()
  486.  
  487. function bedrockLayer(state)
  488.     local blockType = state and "minecraft:bedrock" or "minecraft:air"
  489.     commands.fill(
  490.         refChunk.x,     refChunk.y+16,refChunk.z,
  491.         refChunk.x+15,  refChunk.y+16,refChunk.z+15,
  492.         blockType,0,"replace")
  493. end
  494.  
  495. function generate()
  496.     output("Generate ref chunk") --#
  497.     bedrockLayer(opt.bedrock)
  498.     fillChunk(refChunk, opt.stone and "minecraft:stone" or "minecraft:glass")
  499.     if opt.water then water_populate(refChunk) end
  500.     dirt_populate(refChunk)
  501.     ore_populate(refChunk)
  502.     hole_populate(refChunk)
  503.     if opt.lava then lava_populate(refChunk) end
  504.     if opt.dungeon then
  505.         dungeon(
  506.             refChunk.x+math.random(5),
  507.             refChunk.y+math.random(8),
  508.             refChunk.z+math.random(5))
  509.     end
  510. end
  511.  
  512. function clearRefChunk()
  513.     output("Clear ref chunk") --#
  514.     commands.fill(
  515.         refChunk.x,     refChunk.y+16,refChunk.z,
  516.         refChunk.x+15,  refChunk.y+16,refChunk.z+15,
  517.         "minecraft:air",0,"replace")
  518.     clearChunk(refChunk)
  519. end
  520. function clearTargetChunk()
  521.     output("Clear target chunk") --#
  522.     commands.fill(
  523.         targetChunk.x,      targetChunk.y+16,targetChunk.z,
  524.         targetChunk.x+15,   targetChunk.y+16,targetChunk.z+15,
  525.         "minecraft:air",0,"replace")
  526.     clearChunk(targetChunk)
  527. end
  528.  
  529. --fill <x1> <y1> <z1> <x2> <y2> <z2> <TileName> <dataValue> replace [replaceTileName] [replaceDataValue]
  530. function switchStone()
  531.     output(opt.stone and "Switch Glass to Stone" or "Switch Stone to Glass") --#
  532.     commands.fill(refChunk.x,refChunk.y,refChunk.z,
  533.         targetChunk.x+15,targetChunk.y+15,targetChunk.z+15,
  534.         opt.stone and "minecraft:stone" or "minecraft:glass",
  535.         0, "replace", opt.stone and "minecraft:glass" or "minecraft:stone")
  536.     commands.setblock(refChunk.x+8,refChunk.y-3,refChunk.z+8, opt.stone and "minecraft:stone" or "minecraft:glass",0,"replace")
  537. end
  538.  
  539. function highlightChunk(chunk)
  540.     output("Highlight target chunk") --#
  541.     commands.summon("LightningBolt",chunk.x+8, chunk.y+32,chunk.z+8)
  542.     local i
  543.     for i=-0,16 do
  544.         commands.summon("XPOrb",chunk.x+i, chunk.y+18,chunk.z-1, "{Age:5900,Value:0}")
  545.         commands.summon("XPOrb",chunk.x+i, chunk.y+18,chunk.z+16,"{Age:5900,Value:0}")
  546.         commands.summon("XPOrb",chunk.x-1, chunk.y+18,chunk.z+i, "{Age:5900,Value:0}")
  547.         commands.summon("XPOrb",chunk.x+16,chunk.y+18,chunk.z+i, "{Age:5900,Value:0}")
  548.     end
  549. end
  550.  
  551. local function btn(str,x,y,state,w)
  552.     state = state or false
  553.     local pad1,pad2 = " "," "
  554.     if not w then
  555.         w = string.len(str)+2
  556.     else
  557.         if string.len(str)>=w then
  558.             str = string.sub(str,1,w)
  559.             pad1,pad2 = "",""
  560.         else
  561.             pad1 = string.rep(" ",math.floor(w-string.len(str)/2))
  562.             pad2 = string.rep(" ",math.ceil(w-string.len(str)/2))
  563.         end
  564.     end
  565.     term.setCursorPos(x,y)
  566.     setColor(colors.black,state and colors.black or colors.lightGray)
  567.     term.write(string.rep(string.char(143),w))
  568.     term.setCursorPos(x,y+1)
  569.     setColor(colors.black,state and colors.white or colors.lightGray)
  570.     term.write(pad1..str..pad2)
  571.     setColor(state and colors.white or colors.gray,colors.black)
  572.     term.setCursorPos(x,y+2)
  573.     term.write(string.rep(string.char(131),w))
  574. end
  575. local function onclick_btn(_x,_y,str,bx,by)
  576.     local w = string.len(str)+1
  577.     return _x >= bx-1 and _x <= bx+w
  578.     and _y >= by and _y <= by+1
  579. end
  580.  
  581. local function btn_list(tItems,x,y,w)
  582.     local k,v
  583.     for k,v in pairs(tItems) do
  584.         local last = k >= #tItems
  585.         local str = v
  586.         local pad1,pad2 = " "," "
  587.         if not w then
  588.             w = string.len(str)+2
  589.         else
  590.             if string.len(str)>=w then
  591.                 str = string.sub(str,1,w)
  592.                 pad1,pad2 = "",""
  593.             else
  594.                 local len = (w-string.len(str))/2
  595.                 pad1 = string.rep(" ",math.floor(len))
  596.                 pad2 = string.rep(" ",math.ceil(len))
  597.             end
  598.         end
  599.         if k == 1 then
  600.             term.setCursorPos(x,y)
  601.             setColor(colors.black,state and colors.black or colors.lightGray)
  602.             term.write(string.rep(string.char(143),w))
  603.             y = y+1
  604.         end
  605.         term.setCursorPos(x,y)
  606.         setColor(colors.black,state and colors.white or colors.lightGray)
  607.         term.write(pad1..str..pad2)
  608.         term.setCursorPos(x,y+1)
  609.         if last then
  610.             setColor(colors.gray,colors.black)
  611.             term.write(string.rep(string.char(131),w))
  612.         else
  613.             setColor(colors.black,colors.lightGray)
  614.             term.write(string.rep(string.char(140),w))
  615.         end
  616.         y=y+2
  617.     end
  618. end
  619.  
  620. function display()
  621.     header()
  622.  
  623.     term.setCursorPos(3,4)
  624.     setColor(colors.yellow,colors.black)
  625.     term.write("Chunk:")
  626.     btn("Generate",9,3)
  627.     btn("Clone",20,3)
  628.     btn("Clear",28,3)
  629.     btn(opt.stone and "Stone" or "Glass",36,3)
  630.  
  631.     term.setCursorPos(2,7)
  632.     setColor(colors.yellow,colors.black)
  633.     term.write("Option:")
  634.     btn(opt.lava and string.char(26).."Lava" or "-Lava",9,6)
  635.     btn(opt.water and string.char(26).."Water" or "-Water",17,6)
  636.     btn(opt.bedrock and string.char(26).."Bedrock" or "-Bedrock",26,6)
  637.     btn(opt.dungeon and string.char(26).."Dungeon" or "-Dungeon",37,6)
  638.  
  639.     term.setCursorPos(3,10)
  640.     setColor(colors.yellow,colors.black)
  641.     term.write("Utils:")
  642.     btn("Turtle",11,9)
  643.     btn("Clear Mobs",20,9)
  644.     btn("Highlight chunk",33,9)
  645.    
  646.     --Credits
  647.     btn(string.char(1),2,16)
  648. end
  649.  
  650. function turtle_submenu()
  651.     local editTurtleParams = true
  652.     while editTurtleParams do
  653.         --Clear
  654.         term.setBackgroundColor(colors.black)
  655.         local i
  656.         for i=12,th do
  657.             term.setCursorPos(1,i)
  658.             term.clearLine()
  659.         end
  660.        
  661.         --Display submenu
  662.         term.setCursorPos(3,13)
  663.         setColor(colors.yellow,colors.black)
  664.         term.write("Turtle:{")
  665.         term.setCursorPos(10,18)
  666.         term.write("}")
  667.        
  668.         --GIVE
  669.         btn("Give ",2,14)
  670.        
  671.         --
  672.         btn("Close",2,17)
  673.        
  674.         btn_list({
  675.             "ID"..string.char(16)..string.rep(" ",8),
  676.             "Fuel"..string.char(16)..string.rep(" ",6),
  677.             "Equip",
  678.             },12,12,13)
  679.         --Edit Turtle ID
  680.         term.setCursorPos(16,13)
  681.         local strID = "#"..(opt.turtleCustomID and opt.turtleCustomID or turtleID)
  682.         setColor(colors.black,colors.white)
  683.         term.write(string.sub(strID,1,8)..string.rep(" ",math.max(0,8-string.len(strID))))
  684.  
  685.         --Edit fuel level
  686.         term.setCursorPos(18,15)
  687.         local fuelLevel = tostring(opt.turtleFuelLevel)
  688.         setColor(colors.black,colors.white)
  689.         term.write(string.sub(fuelLevel,1,6)..string.rep(" ",math.max(0,6-string.len(fuelLevel))))
  690.        
  691.         --Select Equip left/right
  692.         --btn("Equip",12,18)
  693.        
  694.         setColor(colors.yellow,colors.black)
  695.         local e,b,x,y = os.pullEvent("mouse_click")
  696.     --  output(string.format("Click %d x[%d],y[%d]",b,x,y)) --#
  697.         if b==1 then
  698.             if y<11 then
  699.                 editTurtleParams = false
  700.                 return false
  701.             elseif x>=2 and x<=8 then
  702.             --Close
  703.                 if y==18 or y==19 then
  704.                     sound_click()
  705.                     output("Close Turtle submenu") --#
  706.                     editTurtleParams = false
  707.                     return false
  708.             --Give
  709.                 elseif y==15 or y==16 then
  710.                     giveTurtle(opt.turtleCustomID and opt.turtleCustomID or turtleID)
  711.                     return true
  712.                 end
  713.             elseif x>=12 and x<=24 then
  714.             --Edit ID
  715.                 if y==13 then
  716.                     output("Input Turtle ID") --#
  717.                     sound_click()
  718.                     local confirm=false
  719.                     while not confirm do
  720.                         term.setCursorPos(15,13)
  721.                         term.setBackgroundColor(colors.white)
  722.                         term.write(string.rep(" ",9))
  723.                         term.setBackgroundColor(colors.lightGray)
  724.                         term.write(" ")
  725.                         term.setBackgroundColor(colors.black)
  726.                         term.write(string.rep(" ",tw))
  727.                         setColor(colors.black,colors.white)
  728.                         term.setCursorPos(15,13)
  729.                         local input = tonumber(read())
  730.                         setColor(colors.white,colors.black)
  731.                         if input and input>=0 then
  732.                             if input == os.getComputerID() then
  733.                                 output("ID #"..input.." used by "..os.getComputerLabel()) --#
  734.                             else
  735.                                 confirm = true
  736.                                 opt.turtleCustomID = input
  737.                                 save_params()
  738.                                 return true
  739.                             end
  740.                         else
  741.                             output("Turtle ID: number >= 0") --#
  742.                         end
  743.                     end
  744.             --Edit fuel
  745.                 elseif y==15 then
  746.                     output("Input Turtle Fuel level") --#
  747.                     sound_click()
  748.                     local confirm=false
  749.                     while not confirm do
  750.                         term.setCursorPos(17,15)
  751.                         term.setBackgroundColor(colors.white)
  752.                         term.write(string.rep(" ",7))
  753.                         term.setBackgroundColor(colors.lightGray)
  754.                         term.write(" ")
  755.                         term.setBackgroundColor(colors.black)
  756.                         term.write(string.rep(" ",tw))
  757.                         setColor(colors.black,colors.white)
  758.                         term.setCursorPos(17,15)
  759.                         local input = tonumber(read())
  760.                         setColor(colors.white,colors.black)
  761.                         if input and input>=0 and input<=100000 then
  762.                             output("Fuel level set: "..input) --#
  763.                             confirm = true
  764.                             opt.turtleFuelLevel = input
  765.                             save_params()
  766.                             return true
  767.                         elseif input and input<0 then
  768.                             output("Fuel level: number >= 0") --#
  769.                         elseif input and input>100000 then
  770.                             output("Fuel level limit: 100000") --#
  771.                         else
  772.                             output("Fuel level should be a number") --#
  773.                         end
  774.                     end
  775.             --Equip config
  776.                 elseif y==17 or y==18 then
  777.                     output("Config Turtle equipement") --#
  778.                     sound_click()
  779.                     --List (7 rows)
  780.                     local turtleEquipement = {
  781.                         { name ="pickaxe",        id="minecraft:diamond_pickaxe",  },
  782.                         { name ="shovel",         id="minecraft:diamond_shovel",   },
  783.                         { name ="sword",          id="minecraft:diamond_sword",    },
  784.                         { name ="crafting table", id="minecraft:crafting_table",   },
  785.                         { name ="modem",          id="computercraft:wireless_modem"},
  786.                         { name ="ender modem",    id="computercraft:advanced_modem"},
  787.                     }
  788.                     local sideEquipement = "L"
  789.                     local selectEquipment = true
  790.                     while selectEquipment do
  791.                         --Clear
  792.                         term.setBackgroundColor(colors.black)
  793.                         local i
  794.                         for i=12,th do
  795.                             term.setCursorPos(12,i)
  796.                             term.write(string.rep(" ",tw))
  797.                         end
  798.                         --Display
  799.                         btn_list({
  800.                             "Equip",
  801.                             (sideEquipement=="L" and "  Left  "..string.char(26) or"Left"),
  802.                             (sideEquipement=="R" and "  Right "..string.char(26) or"Right"),
  803.                             string.char(17),
  804.                             },12,12,13)
  805.                         local alt,k,item=false
  806.                         for k,item in pairs(turtleEquipement) do
  807.                             alt = not alt
  808.                             local cur = "  "
  809.                             if (sideEquipement=="L" and opt.turtleEquipLeft==item.id )
  810.                             or (sideEquipement=="R" and opt.turtleEquipRight==item.id) then
  811.                                 cur = " "..string.char(26)
  812.                             end
  813.                             term.setCursorPos(26,k+12)
  814.                             setColor(
  815.                                 cur=="  " and colors.black or colors.red,
  816.                                 alt and colors.white or colors.yellow)
  817.                             term.write(cur..item.name..string.rep(" ",math.max(0,tw-29-string.len(item.name))))
  818.                         end
  819.                         setColor(colors.white,colors.black)
  820.                        
  821.                         local e,b,x,y = os.pullEvent("mouse_click")
  822.                         output(string.format("Click %d x[%d],y[%d]",b,x,y)) --#
  823.                         if b==1 then
  824.                             --Give Turtle
  825.                             if x>=2 and x<=8 and (y==15 or y==16) then
  826.                                 giveTurtle(opt.turtleCustomID and opt.turtleCustomID or turtleID)
  827.                             --Close Turtle submenu
  828.                             elseif x>=2 and x<=8 and (y==18 or y==19) then
  829.                                 output("Close Turtle submenu") --#
  830.                                 sound_click()
  831.                                 selectEquipment = false
  832.                                 return false
  833.                             --Equipment submenu
  834.                             elseif x>=12 and x<=24 and y>=15 and y<=19 then
  835.                                 --Select Left equipment
  836.                                 if y==15 then
  837.                                     output("Equip left") --#
  838.                                     sound_click()
  839.                                     sideEquipement="L"
  840.                                 --Select Right equipment
  841.                                 elseif y==17 then
  842.                                     output("Equip right") --#
  843.                                     sound_click()
  844.                                     sideEquipement="R"
  845.                                 --Close Equipment selection
  846.                                 elseif y==19 then
  847.                                     output("Close equipment selection") --#
  848.                                     sound_click()
  849.                                     selectEquipment = false
  850.                                     break
  851.                                 end
  852.                             --Equipment list
  853.                             elseif x>=26 and x<=49 and y>=13 and y<=18 then
  854.                                 local selected = y-12
  855.                                 sound_click()
  856.                                 if sideEquipement=="L" then
  857.                                     if opt.turtleEquipLeft==turtleEquipement[selected].id then
  858.                                         output("Unequip left side") --#
  859.                                         opt.turtleEquipLeft=false
  860.                                     else
  861.                                         output("Equip left side: "..turtleEquipement[selected].name) --#
  862.                                         opt.turtleEquipLeft=turtleEquipement[selected].id
  863.                                     end
  864.                                 else
  865.                                     if opt.turtleEquipRight==turtleEquipement[selected].id then
  866.                                         output("Unequip right side") --#
  867.                                         opt.turtleEquipRight=false
  868.                                     else
  869.                                         output("Equip right side: "..turtleEquipement[selected].name) --#
  870.                                         opt.turtleEquipRight=turtleEquipement[selected].id
  871.                                     end
  872.                                 end
  873.                                 save_params()
  874.                             end
  875.                         end
  876.                         sleep(.1)
  877.                         if not selectEquipment then
  878.                             break
  879.                         end
  880.                     end
  881.                 end
  882.             end
  883.         end
  884.         sleep(.1)
  885.     end
  886. end
  887.  
  888. local function display_credits()
  889.     --Clear
  890.     term.setBackgroundColor(colors.black)
  891.     local i
  892.     for i=12,th do
  893.         term.setCursorPos(1,i)
  894.         term.clearLine()
  895.     end
  896.     setColor(colors.yellow,colors.black)
  897.     term.setCursorPos(3,17) term.write(string.char(2))
  898.     setColor(colors.white,colors.black)
  899.     term.write(string.char(17))
  900.    
  901.     setColor(colors.black,colors.white)
  902.     term.setCursorPos(5,16) term.write(string.rep(string.char(143),33))
  903.     term.setCursorPos(5,17) term.write(" OptimineMgr by SukaiPoppuGo     ")
  904.     term.setCursorPos(5,18) term.write(" Credits: 2016 - CC BY-NC-SA 4.0 ")
  905.     setColor(colors.white,colors.black)
  906.     term.setCursorPos(5,19) term.write(string.rep(string.char(131),33))
  907.     sleep(.5)
  908.     os.pullEvent("mouse_click")
  909. end
  910.    
  911. while true do
  912.     display()
  913.     local e,b,_x,_y = os.pullEvent("mouse_click")
  914.     term.setCursorPos(18,1)
  915.     setColor(colors.gray, colors.black)
  916.     --Credits
  917.     if     onclick_btn(_x,_y,"c",2,16)    then
  918.         output("Display credits "..string.char(3)) --#
  919.         sound_click()
  920.         display_credits()
  921.         output(string.char(3)) --#
  922.     --Chunk
  923.     elseif onclick_btn(_x,_y,"Generate",9,3)    then
  924.         output("Click: Generate chunk") --#
  925.         sound_click()
  926.         btn("Generate",9,3,true)
  927.         clearRefChunk()
  928.         generate(refChunk)
  929.         output("Done") --#
  930.     elseif onclick_btn(_x,_y,"Clone",20,3)      then
  931.         output("Click: Clone chunk") --#
  932.         sound_click()
  933.         btn("Clone",20,3,true)
  934.         clone()
  935.         output("Done") --#
  936.     elseif onclick_btn(_x,_y,"Clear",28,3)      then
  937.         output("Click: Clear chunk") --#
  938.         sound_click()
  939.         btn("Clear",28,3,true)
  940.         clearTargetChunk()
  941.         output("Done") --#
  942.     elseif onclick_btn(_x,_y,"Stone",36,3)       then
  943.         output("Toggle Stone/Glass") --#
  944.         sound_click()
  945.         opt.stone = not opt.stone
  946.         btn(opt.stone and "Stone" or "Glass",36,3,true)
  947.         switchStone()
  948.     --Options
  949.     elseif onclick_btn(_x,_y,"-Lava",9,6)        then
  950.         output("Click: Lava") --#
  951.         sound_click()
  952.         opt.lava = not opt.lava
  953.         btn(opt.lava and string.char(26).."Lava" or "-Lava",9,6,true)
  954.         save_params()
  955.     elseif onclick_btn(_x,_y,"-Water",16,6)      then
  956.         output("Click: Water") --#
  957.         sound_click()
  958.         opt.water = not opt.water
  959.         btn(opt.water and string.char(26).."Water" or "-Water",17,6,true)
  960.         save_params()
  961.     elseif onclick_btn(_x,_y,"-Bedrock",24,6)    then
  962.         output("Toggle Bedrock") --#
  963.         sound_click()
  964.         opt.bedrock = not opt.bedrock
  965.         btn(opt.bedrock and string.char(26).."Bedrock" or "-Bedrock",26,6,true)
  966.         bedrockLayer(opt.bedrock)
  967.         save_params()
  968.     elseif onclick_btn(_x,_y,"-Dungeon",34,6)    then
  969.         output("Toggle Dungeon") --#
  970.         sound_click()
  971.         opt.dungeon = not opt.dungeon
  972.         btn(opt.dungeon and string.char(26).."Dungeon" or "-Dungeon",37,6,true)
  973.         save_params()
  974.     --Utils
  975.     elseif onclick_btn(_x,_y,"Turtle",11,9)      then
  976.         output("Click: Turtle") --#
  977.         sound_click()
  978.         btn("Turtle",11,9,true)
  979.         while turtle_submenu() do
  980.             sleep(.1)
  981.         end
  982.     elseif onclick_btn(_x,_y,"Clear Mobs",20,9) then
  983.         output("Click: Clear Mobs") --#
  984.         sound_click()
  985.         btn("Clear Mobs",20,9,true)
  986.         kill(refChunk,"!Player")
  987.         kill(targetChunk,"!Player")
  988.     elseif onclick_btn(_x,_y,"Highlight chunk",33,9)then
  989.         output("Click: Highlight chunk") --#
  990.         sound_click()
  991.         btn("Highlight chunk",33,9,true)
  992.         highlightChunk(targetChunk)
  993.     end
  994.     sleep(.1)
  995. end
Add Comment
Please, Sign In to add comment