View difference between Paste ID: 5dY2Lywd and gkz5sgZ8
SHOW: | | - or go back to the newest paste.
1
--[[ORE COLLECTING MINER V2.6 by BrunoZockt
2
3
MIT License
4
5
Copyright (c) 2018 Bruno Heberle
6
7
Permission is hereby granted, free of charge, to any person obtaining a copy
8
of this software and associated documentation files (the "Software"), to deal
9
in the Software without restriction, including without limitation the rights
10
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
copies of the Software, and to permit persons to whom the Software is
12
furnished to do so, subject to the following conditions:
13
14
The above copyright notice and this permission notice shall be included in all
15
copies or substantial portions of the Software.
16
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
SOFTWARE.
24
25
26
V1.0 released 22.07.2016 17kb
27
V1.1 released 16.08.2016 17kb
28
V2.0 released 07.10.2017 35kb
29
V2.1 released 15.12.2017 40kb
30
V2.2 released 17.12.2017 42kb
31
V2.3 released 11.02.2018 46kb
32
V2.4 released 19.06.2018 57kb
33
V2.5 released 15.07.2018 68kb
34
V2.5.1 released 19.08.2018 78kb
35
V2.6 released 16.09.2018 90kb
36
This program is a work in progress and
37
will automatically update itself.
38
If you have any problems, questions or
39
suggestions I'd be happy if you wrote
40
an e-mail to [email protected].
41
Feel free to use this program whenever
42
you want. If you want to use parts of
43
my program for developing, do so but
44
don't say it's yours - a sidenote
45
e.g. "by BrunoZockt" would be nice :)
46
If you want to understand this program
47
I recommend to read the variables first
48
and then the rest of the code from the
49
bottom to the top! Maybe I will add
50
some comments for better understanding
51
soon! The names of my functions and
52
variables may sound strange because
53
some are german and others are not
54
really creative, but I hope you
55
understand it anyways ;) ]]--
56
57
local HOST_ENV = _ENV or getfenv()
58
local OUR_ENV = {}
59
60
setmetatable(OUR_ENV, {__index = HOST_ENV, OUR_ENV = OUR_ENV})
61
setfenv(1, OUR_ENV)
62
63
-----###-----configurable variables-----###-----
64
65
settings = {
66
  ["language"] = "en",
67
  ["tunnelspace"] = 4,
68
  ["quantity"] = 10,
69
  ["length"] = 8,
70
  ["ignor"] = {"minecraft:stone", "minecraft:cobblestone", "minecraft:dirt", "minecraft:lava", "minecraft:flowing_lava", "minecraft:water", "minecraft:flowing_water", "minecraft:torch"},
71
  ["chestSelect"] = 3,
72
  ["torches"] = true,
73
  ["mainTorches"] = true,
74
  ["lateralTorches"] = 1,
75
  ["Autofuel"] = true,
76
  ["floor"] = true,
77
  ["trash"] = false,
78
  ["walls"] = false
79
}
80
81
-----###-----constants-----###-----
82
83
local programName = shell.getRunningProgram()
84
shell.run("pastebin get cR9vEiTc database/"..programName.."/lang")
85
local label = os.getComputerLabel() or "the turtle"
86
local chestSlot = 16
87
local fuelLevel = turtle.getFuelLevel()
88
local w, h = term.getSize()
89
local kill = false
90
chestList = {"minecraft:chest", "IronChest:BlockIronChest", "IronChest:BlockIronChest:1", "IronChest:BlockIronChest:2", "IronChest:BlockIronChest:3", "IronChest:BlockIronChest:4", "IronChest:BlockIronChest:5", "IronChest:BlockIronChest:6"}
91
enderChestList = {"EnderStorage:enderChest", "enderstorage:ender_storage", "ThermalExpansion:Strongbox", "ThermalExpansion:Tesseract"}
92
cardinal = {"North", "West", "South", "East"}
93
94
-----###-----mining variables-----###-----
95
96
variables = {
97
  ["searching"] = false,
98
  ["level"] = 0,
99
  ["direction"] = 1,
100
  ["cache"] = {},
101
  ["maxSpace"] = false,
102
  ["setup"] = false,
103
  ["index"] = 2,
104
  ["slots"] = {"_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_"},
105
  ["torchPositions"] = {},
106
  ["stats"] = {
107
    ["dug"] = 0,
108
    ["ores"] = 0,
109
    ["time"] = 0,
110
    ["moves"] = 0
111
  },
112
  ["TorchDemand"] = 0,
113
  ["orientation"] = {},
114
  ["CrosswayAmount"] = 0,
115
  ["FuelDemand"] = 0,
116
  ["startDay"] = 0,
117
  ["startTime"] = 0
118
}
119
120
-----###-----menu variables-----###-----
121
122
local select = 1
123
local menustate
124
local checkbox = false
125
local gone = {}
126
local timer = {}
127
local chest = {"enderchest", "chest", "none"}
128
local objects = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
129
local errors = {false, false, false, false, false, false, false, false}
130
local Check = {["Fuellevel"] = false, ["torches"] = false, ["chest"] = false}
131
132
local menuVars = {
133
  ["notificationCenter"] = {
134
    time = 0
135
  },
136
  ["drawOptions"] = {
137
    shift = 0,
138
    shift2 = 0,
139
    saved = false,
140
    scroll = 0,
141
    scroll2 = 0,
142
    focus = 100,
143
    ypos
144
  },
145
  ["drawPopup"] = {
146
    scroll = 0,
147
    lastscroll = 0,
148
    ypos = 9
149
  },
150
  ["drawNews"] = {
151
    scroll = 0
152
  }
153
}
154
155
156
-----###-----helpful functions-----###-----
157
158
function Sprint(str, xpos, ypos)
159
  term.setCursorPos(xpos, ypos)
160
  term.write(str)
161
end
162
163
function printCentered(str, ypos)
164
  term.setCursorPos(w/2 - #str/2 + 1, ypos)
165
  term.write(str)
166
end
167
168
function printRight(str, ypos, xoffset)
169
  if xoffset == nil then
170
    xoffset = 0
171
  end
172
  term.setCursorPos(w - xoffset - (#str - 1), ypos)
173
  term.write(str)
174
end
175
176
function printLeft(str, ypos)
177
  term.setCursorPos(1, ypos)
178
  term.write(str)
179
end
180
181
function printB(tab, y, stop, start)
182
  if start == nil then
183
    start = 1
184
  end
185
  if stop == nil then
186
    stop = w
187
  end
188
  local letters = 0
189
  for i in pairs(tab) do
190
    letters = letters + #tab[i]
191
  end
192
  local space = (stop-start)+1-letters
193
  term.setCursorPos(start, y)
194
  if (space/(#tab+1))%1 == 0 then
195
    for i in ipairs(tab) do
196
      term.write(string.rep(" ", space/(#tab+1))..tab[i])
197
    end
198
  elseif (space/(#tab))%2 == 0 then
199
    term.write(string.rep(" ", (space/#tab)/2))
200
    for i in ipairs(tab) do
201
      term.write(tab[i]..string.rep(" ", space/#tab))
202
    end
203
  elseif space == #tab-1 then
204
    term.write(tab[1])
205
    for i = 2, #tab do
206
      term.write(" "..tab[i])
207
    end
208
  else
209
    for i in ipairs(tab) do
210
      term.write(string.rep(" ", round(space/(#tab-i+2), "u"))..tab[i])
211
      space = space - round(space/(#tab-i+2), "u")
212
    end
213
  end
214
end
215
216
function printWrapped(tab, xpos, ypos, Space, sep)
217
  local sep = sep or " "
218
  if tab[1] == "-" then
219
    Sprint(tab[1]..sep, xpos, ypos)
220
    table.remove(tab, 1)
221
    xpos = xpos+2
222
    Space = Space-2
223
  end
224
  local leftSpace = Space
225
  local newxpos = xpos
226
  for word = 1, #tab do
227
    if Space < #tab[word] + #sep then
228
      return false
229
    elseif leftSpace >= #tab[word]+#sep then
230
      Sprint(tab[word]..sep, newxpos, ypos)
231
      leftSpace = leftSpace - (#tab[word]+#sep)
232
      newxpos = newxpos + #tab[word]+#sep
233
    else
234
      ypos = ypos + 1
235
      leftSpace = Space
236
      Sprint(tab[word]..sep, xpos, ypos)
237
      leftSpace = leftSpace - (#tab[word]+#sep)
238
      newxpos = xpos + (#tab[word]+#sep)
239
    end
240
  end
241
  return ypos
242
end
243
244
function printLine(xpos, start, stop, point, endPoint)
245
  for i = start, stop do
246
    Sprint("|", xpos, i)
247
  end
248
  if point ~= nil then
249
    Sprint(point, xpos, start)
250
    Sprint((endPoint or point), xpos, stop)
251
  end
252
end
253
254
function Splitter(inputstr, sep)
255
  if inputstr == nil then
256
    return nil
257
  elseif sep == nil then
258
    sep = "%s"
259
  end
260
  local t, i = {}, 1
261
  for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
262
    t[i] = str
263
    i = i + 1
264
  end
265
  return t
266
end
267
268
local Version = tonumber(Splitter(os.version())[2])
269
270
function ItemCount(Block, Slot)
271
  if Slot == 0 then
272
    return false
273
  elseif Slot == nil then
274
    return false
275
  end
276
  local loops = 1
277
  if type(Block) == "table" then
278
    loops = #Block
279
  end
280
  if Version >= 1.64 then
281
    for i = 1, loops do
282
      local Data = turtle.getItemDetail(Slot)
283
      if Data == nil then
284
        return 0
285
      elseif loops == 1 then
286
        if Data.name == Block then
287
          return Data.count
288
        end
289
      else
290
        if Data.name == Block[i] then
291
          return Data.count
292
        end
293
      end
294
    end
295
    return false
296
  else
297
    return turtle.getItemCount(Slot)
298
  end
299
end
300
301
function Color()
302
  if Version <= 1.45 then
303
    return false
304
  elseif term.isColor() then
305
    return true
306
  else
307
    return false
308
  end
309
end
310
311
function MoveObjects(deleted, mode)
312
  if mode == 1 then
313
    for k, v in ipairs(gone) do
314
      if v == deleted then
315
        return
316
      end
317
    end
318
    for k, v in ipairs(objects) do
319
      if k > deleted and k < 12 then
320
        objects[k] = objects[k] - 1
321
      end
322
    end
323
    table.insert(gone, deleted)
324
  elseif mode == 2 then
325
    for k, v in ipairs(gone) do
326
      if v == deleted then
327
        table.remove(gone, k)
328
        for k, v in ipairs(objects) do
329
          if k > deleted and k < 12 then
330
            objects[k] = objects[k] + 1
331
          end
332
        end
333
      end
334
    end
335
  end
336
end
337
338
function OreCounter()
339
  if variables.searching == true then
340
    variables.stats["ores"] = variables.stats["ores"] + 1
341
    if variables.stats["ores"] == 1 then
342
      print(lang.status(settings.language, 1))
343
    else
344
      print(lang.status(settings.language, 2, variables.stats["ores"]))
345
    end
346
  end
347
end
348
349
function SlotCalculator(code, param)
350
  local output = {}
351
  local item
352
  if param ~= nil and param ~= "check" then
353
    item = param
354
  elseif param == "check" then
355
    if code == "T" then
356
      item = "minecraft:torch"
357
    elseif code == "C" then
358
      if settings.chestSelect == 1 then
359
        item = enderChestList
360
      elseif settings.chestSelect == 2 then
361
        item = chestList
362
      end
363
    end
364
  end
365
  for n = 1, #variables.slots do
366
    if variables.slots[n] == code then
367
      if param == "empty" then
368
        table.insert(output, n)
369
      elseif param == nil then
370
        if turtle.getItemCount(n) > 0 then
371
          table.insert(output, n)
372
        end
373
      elseif ItemCount(item, n) ~= false and ItemCount(item, n) ~= 0 then
374
        table.insert(output, n)
375
      end
376
    end
377
  end
378
  return output
379
end
380
381
function refuel(Amount, Execute)
382
  local ActualAmount = turtle.getItemCount()
383
  if ActualAmount < Amount then
384
    if Execute == true then
385
      if turtle.refuel() == false then
386
        return false
387
      else
388
        while turtle.refuel() ~= false do
389
        end
390
        return ActualAmount
391
      end
392
    else
393
      return false
394
    end
395
  else
396
    turtle.refuel(Amount)
397
    return true
398
  end
399
end
400
401
function basicInputHandler(orientation, event, key)
402
  if event == "key" then
403
    if key == 28 then
404
      menustate = menu[menustate].options[select]
405
      select = 1
406
    elseif orientation == "vertical" then
407
      if key == 200 and select > 1 then
408
        select = select-1
409
      elseif key == 208 and select < #menu[menustate].options then
410
        select = select + 1
411
      end
412
    else
413
      if key == 203 and select > 1 then
414
        select = select - 1
415
      elseif key == 205 and select < #menu[menustate].options then
416
        select = select + 1
417
      end
418
    end
419
  end
420
end
421
422
function save(path, content, todolist)
423
	local file = fs.open(path, "w")
424
  if todolist then
425
    file.writeLine("todoList = "..textutils.serialize(content))
426
  elseif type(content) == "table" then
427
    for k, v in pairs(content) do
428
      if type(content[k]) == "table" then
429
        file.writeLine(k.." = "..textutils.serialize(content[k]))
430
      else
431
        file.writeLine(k.." = "..tostring(content[k]))
432
      end
433
    end
434
  else
435
	  file.writeLine(content)
436
  end
437
	file.close()
438
end
439
440
function translate(str, category)
441
  if category == "layout" then
442
    n = 35
443
  end
444
  for i = 1, n do
445
    if str == lang[category]("en", i) then
446
      return lang[category](settings.language, i)
447
    end
448
  end
449
end
450
451
function round(int, mode, dez)
452
  if dez == nil then
453
    dez = 1
454
  end
455
  if mode == "d" then
456
    mode = 0.4999
457
  elseif mode == "u" then
458
    mode = 0.5
459
  end
460
  return math.floor(int/dez+mode)*dez
461
end
462
463
local function compile(chunk) -- returns compiled chunk or nil and error message
464
  if type(chunk) ~= "string" then
465
    error("expected string, got ".. type(chunk), 2)
466
  end
467
468
  local function findChunkName(var)
469
    for k,v in pairs(HOST_ENV) do
470
      if v==var then
471
        return k
472
      end
473
    end
474
    return "Unknown chunk"
475
  end
476
477
  return load(chunk, findChunkName(chunk), "t", OUR_ENV)
478
end
479
480
function insert(code)
481
  table.insert(todoList, variables.index, code)
482
  variables.index = variables.index + 1
483
  save("database/OCM/resume/todoList", todoList, true)
484
end
485
486
-----###-----improved functions-----###-----
487
488
function dig(gravel)
489
  local a = false
490
  insert('OreCounter()')
491
  variables.stats["dug"] = variables.stats["dug"] + 1
492
  if gravel then
493
    while turtle.dig() do
494
      os.sleep(0.05)
495
      a = true
496
    end
497
    return a
498
  else
499
    return turtle.dig()
500
  end
501
end
502
503
function digUp()
504
  insert('OreCounter()')
505
  variables.stats["dug"] = variables.stats["dug"] + 1
506
  return turtle.digUp()
507
end
508
509
function digDown()
510
  insert('OreCounter()')
511
  variables.stats["dug"] = variables.stats["dug"] + 1
512
  return turtle.digDown()
513
end
514
515
function face(int)
516
  if int == variables.direction then
517
    return
518
  elseif (int + variables.direction)%2 == 0 then
519
    return turn()
520
  elseif math.abs(int - variables.direction) == 1 then
521
    if variables.direction < int then
522
      return left()
523
    else
524
      return right()
525
    end
526
  else
527
    if variables.direction < int then
528
      return right()
529
    else
530
      return left()
531
    end
532
  end
533
end
534
535
function right()
536
  turtle.turnRight()
537
  variables.direction = variables.direction - 1
538
  if variables.direction == 0 then
539
    variables.direction = 4
540
  end
541
end
542
543
function left()
544
  turtle.turnLeft()
545
  variables.direction = variables.direction + 1
546
  if variables.direction == 5 then
547
    variables.direction = 1
548
  end
549
end
550
551
function turn()
552
  left()
553
  left()
554
end
555
556
function Freeway()
557
  variables.searching = false
558
  insert([[while not turtle.forward() do
559
    if turtle.getFuelLevel() == 0 then
560
      Fuel()
561
    elseif dig() == false then
562
      turtle.attack()
563
    end
564
  end]])
565
  variables.stats["moves"] = variables.stats["moves"] + 1
566
end
567
568
function FreewayUp()
569
  variables.searching = false
570
  insert("variables.level = variables.level + 1")
571
  insert([[while not turtle.up() do
572
    if turtle.getFuelLevel() == 0 then
573
      Fuel()
574
    elseif digUp() == false then
575
      turtle.attackUp()
576
    end
577
  end]])
578
  variables.stats['moves'] = variables.stats['moves'] + 1
579
end
580
581
function FreewayDown()
582
  variables.searching = false
583
  insert("variables.level = variables.level - 1")
584
  insert([[while not turtle.down() do
585
    if turtle.getFuelLevel() == 0 then
586
      Fuel()
587
    elseif digDown() == false then
588
      turtle.attackDown()
589
    end
590
  end]])
591
  variables.stats['moves'] = variables.stats['moves'] + 1
592
end
593
594
function FreewayBack()
595
  variables.searching = false
596
  while not turtle.back() do
597
    if turtle.getFuelLevel() == 0 then
598
      Fuel()
599
    else
600
      turn()
601
      insert([[if dig() == false then
602
        for i = 1, 20 do
603
          turtle.attack()
604
          os.sleep(0.05)
605
        end
606
      end]])
607
      turn()
608
    end
609
  end
610
  variables.stats["moves"] = variables.stats["moves"] + 1
611
end
612
613
614
-----###-----drawMenus-----###-----
615
616
function notificationCenter()
617
  local advice = {
618
  lang.adv(settings.language, 1, ErrorSlot),
619
  lang.adv(settings.language, 2, 1),
620
  lang.adv(settings.language, 3, chestSlot),
621
  lang.adv(settings.language, 4, chestSlot),
622
  lang.adv(settings.language, 5, chestSlot),
623
  lang.adv(settings.language, 6, chestSlot),
624
  lang.adv(settings.language, 7, 1),
625
  lang.adv(settings.language, 8, 1)
626
  }
627
  if menuVars["notificationCenter"].time > 5 then
628
    for i = 1, #errors do
629
      if errors[i] ~= false then
630
        printWrapped(Splitter(advice[i], " "), 1, objects[8], w - 10)
631
        return
632
      end
633
    end
634
  end
635
  for i = 1, 4 do
636
    printLeft(lang.layout(settings.language, i), objects[i+7])
637
  end
638
  menuVars["notificationCenter"].time = menuVars["notificationCenter"].time + 0.5
639
end
640
641
function drawPopup(event, p1, p2, p3)
642
  local errorMessages = {
643
    lang.popup(settings.language, 1, ErrorSlot),
644
    lang.popup(settings.language, 2),
645
    lang.popup(settings.language, 1, chestSlot),
646
    lang.popup(settings.language, 3),
647
    lang.popup(settings.language, 1, chestSlot),
648
    lang.popup(settings.language, 4),
649
    lang.popup(settings.language, 5),
650
    lang.popup(settings.language, 6)
651
  }
652
  local leftBorder = round(w/9, "u")
653
  local rightBorder = round(w+1-w/9, "u")
654
  local winWidth = rightBorder-leftBorder-1
655
656
  menuVars["drawPopup"].lastscroll = menuVars["drawPopup"].scroll
657
658
  if event == "mouse_scroll" then
659
    menuVars["drawPopup"].scroll = menuVars["drawPopup"].scroll - p1
660
  elseif event == "mouse_click" and p1 == 1 then
661
    if p2 == leftBorder+winWidth then
662
      if p3 == 6 then
663
        menuVars["drawPopup"].scroll = menuVars["drawPopup"].scroll + 1
664
      elseif p3 == h-3 then
665
        menuVars["drawPopup"].scroll = menuVars["drawPopup"].scroll - 1
666
      end
667
    end
668
  elseif event == "key" then
669
    if p1 == 208 then
670
      menuVars["drawPopup"].scroll = menuVars["drawPopup"].scroll - 1
671
    elseif p1 == 200 then
672
      menuVars["drawPopup"].scroll = menuVars["drawPopup"].scroll + 1
673
    end
674
  end
675
  if menuVars["drawPopup"].scroll < -(menuVars["drawPopup"].ypos-menuVars["drawPopup"].lastscroll-(math.ceil((h-6)/10)+6)) then
676
    menuVars["drawPopup"].scroll = -(menuVars["drawPopup"].ypos-menuVars["drawPopup"].lastscroll-(math.ceil((h-6)/10)+6))
677
  elseif menuVars["drawPopup"].scroll > 0 then
678
    menuVars["drawPopup"].scroll = 0
679
  end
680
681
  printLine(leftBorder, 4, h-1, "+")
682
  printLine(rightBorder, 4, h-1, "+")
683
  menuVars["drawPopup"].ypos = math.ceil((h-6)/10)+5+menuVars["drawPopup"].scroll
684
  for i = 1, #errors do
685
    if errors[i] == true then
686
      menuVars["drawPopup"].ypos = printWrapped(Splitter("- "..errorMessages[i]), leftBorder+1, menuVars["drawPopup"].ypos+1, winWidth, " ")
687
    end
688
  end
689
  printCentered(string.rep("-", winWidth), 4)
690
  printCentered(string.rep("-", winWidth), h-1)
691
  printCentered(string.rep(" ", winWidth), 5)
692
  printCentered(string.rep(" ", winWidth), 6)
693
  printWrapped(Splitter(lang.popup(settings.language, 7, label)), leftBorder+1, math.ceil((h-6)/10)+4, winWidth, " ")
694
  printCentered(string.rep(" ", winWidth), h-3)
695
  printCentered(string.rep(" ", winWidth), 3)
696
  drawHeader()
697
  printLine(rightBorder-1, 6, h-3, "^", "v")
698
  if select == 1 then
699
    printB({">"..lang.popup(settings.language, 8).."<"," "..lang.popup(settings.language, 9).." ", " "..lang.popup(settings.language, 10).." "}, math.ceil((h-6)/10*9)+4, rightBorder-1, leftBorder+1)
700
  elseif select == 2 then
701
    printB({" "..lang.popup(settings.language, 8).." ",">"..lang.popup(settings.language, 9).."<", " "..lang.popup(settings.language, 10).." "}, math.ceil((h-6)/10*9)+4, rightBorder-1, leftBorder+1)
702
  else
703
    printB({" "..lang.popup(settings.language, 8).." "," "..lang.popup(settings.language, 9).." ", ">"..lang.popup(settings.language, 10).."<"}, math.ceil((h-6)/10*9)+4, rightBorder-1, leftBorder+1)
704
  end
705
end
706
707
function drawHeader()
708
  printCentered("ORE SEARCHING STRIP MINER by BrunoZockt", 1)
709
  printLeft(string.rep("-", w), 2)
710
end
711
712
function drawUpdate(event, p1, p2, p3)
713
  local leftBorder = round(w/6, "u")
714
  local rightBorder = round(w/6*5, "u")
715
  local winWidth = rightBorder-leftBorder-1
716
  local space = round(winWidth-13, "u")
717
  if event == "mouse_click" and p1 == 1 then
718
    if p2 == leftBorder+3 and p3 == math.ceil((h-6)/2)+5 then
719
      checkbox = not checkbox
720
    elseif p3 == math.ceil((h-6)/10*9)+4 then
721
      if p2 >= leftBorder+round(0.25*space, "u")+1 and p2 <= leftBorder+round(0.25*space, "u")+11 then
722
        if select == 1 then
723
          menustate = menu[menustate].options[select]
724
        else
725
          select = 1
726
        end
727
      elseif leftBorder+round(0.75*space, "u")+12 <= p2 and p2 <= leftBorder+round(0.75*space, "u")+13 then
728
        if select == 2 then
729
          menustate = menu[menustate].options[select]
730
        else
731
          select = 2
732
        end
733
      end
734
    end
735
  elseif event == "key" then
736
    if p1 == 203 and select > 1 then
737
      select = select - 1
738
    elseif p1 == 205 and select < #menu[menustate].options then
739
      select = select + 1
740
    elseif p1 == 28 then
741
      menustate = menu[menustate].options[select]
742
    end
743
  end
744
  printCentered(lang.layout(settings.language, 5), math.ceil((h-6)/6.9)+4)
745
  printCentered(string.rep("-", math.floor(w/3*2)), 4)
746
  printCentered(string.rep("-", math.floor(w/3*2)), 12)
747
  printLine(leftBorder, 4, 12, "+")
748
  printLine(rightBorder, 4, 12, "+")
749
  Sprint("_", leftBorder+3, math.ceil((h-6)/2)+4)
750
  Sprint("|_|"..lang.layout(settings.language, 6), leftBorder+2, math.ceil((h-6)/2)+5)
751
  if select == 1 then
752
    printB({">"..lang.layout(settings.language, 7).."<", " "..lang.layout(settings.language, 8).." "}, math.ceil((h-6)/10*9)+4, rightBorder-1, leftBorder+1)
753
  elseif select == 2 then
754
    printB({" "..lang.layout(settings.language, 7).." ", ">"..lang.layout(settings.language, 8).."<"}, math.ceil((h-6)/10*9)+4, rightBorder-1, leftBorder+1)
755
  end
756
  if checkbox == true then
757
    Sprint("X", leftBorder+3, math.ceil((h-6)/2)+5)
758
  end
759
end
760
761
function drawNews(event, p1, p2, p3)
762
  if event == "mouse_scroll" then
763
    menuVars["drawNews"].scroll = menuVars["drawNews"].scroll - p1
764
  elseif event == "key" then
765
    if p1 == 28 then
766
      menustate = menu[menustate].options[select]
767
      select = 1
768
      menuVars["drawNews"].scroll = 0
769
    elseif p1 == 208 then
770
      menuVars["drawNews"].scroll = menuVars["drawNews"].scroll - 1
771
    elseif p1 == 200 then
772
      menuVars["drawNews"].scroll = menuVars["drawNews"].scroll + 1
773
    end
774
  elseif event == "mouse_click" then
775
    if p1 == 1 then
776
      if p2 == w then
777
        if p3 == 3 then
778
          menuVars["drawNews"].scroll = menuVars["drawNews"].scroll + 1
779
        elseif p3 == h then
780
          menuVars["drawNews"].scroll = menuVars["drawNews"].scroll - 1
781
        end
782
      elseif p2 >= w/2-2 and p2 <= w/2+2 and p3 == h then
783
        menustate = menu[menustate].options[select]
784
        select = 1
785
        menuVars["drawNews"].scroll = 0
786
      end
787
    end
788
  end
789
  if menuVars["drawNews"].scroll > 0 then
790
    menuVars["drawNews"].scroll = 0
791
  elseif menuVars["drawNews"].scroll < -283 then
792
    menuVars["drawNews"].scroll = -283
793
  end
794
  Sprint("Patch 2.6", 1, 3+menuVars["drawNews"].scroll)
795
  Sprint("-----------", 1, 4+menuVars["drawNews"].scroll)
796
  Sprint("1.NEW FEATURES", 2, 6+menuVars["drawNews"].scroll)
797
  local ypos = printWrapped(Splitter("- The program will now continue automatically after a server restart."), 3, 7+menuVars["drawNews"].scroll, w-3, " ")
798
  ypos = printWrapped(Splitter("- To achieve this it is very important that there is always one torch available so that the turtle can orientate itself."), 4, ypos+1, w-4, " ")
799
  ypos = printWrapped(Splitter("- The program will also install my Startup Handler in 'startup' so that it can be automatically executed. Any file existing in 'startup' will be moved to 'old_startup' and called whenever there is no program that needs to be resumed."), 4, ypos+1, w-4, " ")
800
  ypos = printWrapped(Splitter("- Everything that needs to be done is written to an extern file, which also takes up some disk space (very unlikely to exceed 10kb). So make sure that there is some file space left."), 4, ypos+1, w-4, " ")
801
  ypos = printWrapped(Splitter("- The options 'chest' and 'enderchest' are now compatible with all the chests from the mods 'Iron chests' and 'Thermal Expansion'."), 4, ypos+1, w-4, " ")
802
  Sprint("2.OPTIMIZATIONS", 2, ypos+2)
803
  ypos = printWrapped(Splitter("- The path of the external files is now including your program name so that you could have multiple instances of the program on one turtle. The updater had to be modified aswell."), 3, ypos+3, w-3, " ")
804
  ypos = printWrapped(Splitter("- This page looks a lot cleaner now."), 3, ypos+3, w-3, " ")
805
  ypos = printWrapped(Splitter("- The variables are now ordered a lot better by splitting them into categories."), 3, ypos+1, w-3, " ")
806
  ypos = printWrapped(Splitter("- Many minor changes in program structure to save a few bytes."), 3, ypos+1, w-3, " ")
807
  Sprint("3.Bug fixes", 2, ypos+2)
808
  ypos = printWrapped(Splitter("- The 'place Walls'-option will now place walls in water or lava like it was originally supposed to (quite stupid mistake on my part)."), 3, ypos+3, w-3, " ")
809
  Sprint("Patch 2.5.1", 1, ypos+2)
810
  Sprint("-----------", 1, ypos+3)
811
  Sprint("1.NEW FEATURES", 2, ypos+5)
812
  ypos = printWrapped(Splitter("- You can now choose to let the turtle build walls and ceiling to prevent the hallways from being flooded by water or lava."), 3, ypos+6, w-3, " ")
813
  ypos = printWrapped(Splitter("- If torches need to be placed, the turtle will now determine it's cardial direction at the beginning of digging."), 3, ypos+1, w-3, " ")
814
  ypos = printWrapped(Splitter("- Torches will -now for real- be placed under all circumstances (see 'Bug fixes')."), 3, ypos+1, w-3, " ")
815
  Sprint("2.OPTIMIZATIONS", 2, ypos+2)
816
  ypos = printWrapped(Splitter("- When trash-option or chest-option is true, the turtle will now keep 1 Stack of cobblestone to place floor or walls, if selected."), 3, ypos+3, w-3, " ")
817
  ypos = printWrapped(Splitter("- Added the page counter in the bottom left"), 3, ypos+1, w-3, " ")
818
  Sprint("3.Bug fixes", 2, ypos+2)
819
  ypos = printWrapped(Splitter("- There was a 50% chance (turtle heading north or south) that 50% of the torches (either on the right or the left crosstunnel) were lost. That is because torches are by default placed heading west if possible. The turtle will now determine where it is heading and adjust the way it places torches accordingly so that it never loses a single torch."), 3, ypos+3, w-3, " ")
820
  ypos = printWrapped(Splitter("- When in default settings, 'chest' was set to 'none' there would be a slot for a chest anyways."), 3, ypos+1, w-3, " ")
821
  ypos = printWrapped(Splitter("- I missed two instances in my code that would cause a crash when no cobblestone was available. However the probability to hit this crash was soooo low that I'm sure nobody ever encountered it."), 3, ypos+1, w-3, " ")
822
  Sprint("Patch 2.5", 1, ypos+2)
823
  Sprint("---------", 1, ypos+3)
824
  Sprint("1.NEW FEATURES", 2, ypos+5)
825
  ypos = printWrapped(Splitter("- If you are using ComputerCraft Version 1.64 or higher, the turtle won't determine whether to mine a block or not by comparing it to the Ignor-slots but by comparing it with the new Ignor-list, which you can add blocks to. This is not only easier and more flexible but also a lot faster."), 3, ypos+6, w-3, " ")
826
  ypos = printWrapped(Splitter("- If you want to, the turtle will now throw away items contained on the Ignor-list."), 3, ypos+1, w-3, " ")
827
  ypos = printWrapped(Splitter("- The decision whether to empty the inventory into a chest, also takes the length of the lateral tunnel into account now, to prevent the loss of items."), 3, ypos+1, w-3, " ")
828
  ypos = printWrapped(Splitter("- Torches will now be placed immediatly after digging the tunnel and not only after searching through the tunnel, to prevent mobspawns."), 3, ypos+1, w-3, " ")
829
  ypos = printWrapped(Splitter("- Torches will now be placed under all circumstances, even if a block for the torch to hold on to has to be placed."), 3, ypos+1, w-3, " ")
830
  ypos = printWrapped(Splitter("- As soon as torch-slots become free because all its torches have been placed, it changes to an empty slot now, which makes it available for ores."), 3, ypos+1, w-3, " ")
831
  ypos = printWrapped(Splitter("- The program wont throw an error and terminate, when it doesn't have blocks it wants to place, it simply doesn't place them."), 3, ypos+1, w-3, " ")
832
  Sprint("2.OPTIMIZATIONS", 2, ypos+2)
833
  ypos = printWrapped(Splitter("- The turtle will also search right and left at the very first two blocks of the tunnel, just in case you are lazy."), 3, ypos+3, w-3, " ")
834
  ypos = printWrapped(Splitter("- When you decide to use no chest, the chest-slot will now be converted to an empty slot, available as inventory."), 3, ypos+1, w-3, " ")
835
  ypos = printWrapped(Splitter("- The floor will now be placed after digging out ores, so that there will definitly be no holes in the ground."), 3, ypos+1, w-3, " ")
836
  ypos = printWrapped(Splitter("- The inventory will now be cleared after finishing."), 3, ypos+1, w-3, " ")
837
  ypos = printWrapped(Splitter("- Optimized the dig() function to run faster, increasing the overall speed of the program drasticly."), 3, ypos+1, w-3, " ")
838
  Sprint("3.Bug fixes", 2, ypos+2)
839
  ypos = printWrapped(Splitter("- In the main hallway, there was a 25% chance that the block a torch was just placed on gets destroyed a second after, and thereby losing the torch."), 3, ypos+3, w-3, " ")
840
  ypos = printWrapped(Splitter("- In rare cases when selected normal chest, the turtle would try to return to its chest after the program had finished, resulting in the turtle vanishing to Australia."), 3, ypos+1, w-3, " ")
841
  ypos = printWrapped(Splitter("- Asking for fuel and taking that fuel wasn't always sync."), 3, ypos+1, w-3, " ")
842
  ypos = printWrapped(Splitter("- The turtle would sometimes put the coal it needs to fuel itself into the chest."), 3, ypos+1, w-3, " ")
843
  ypos = printWrapped(Splitter("- When a tunnellength <= 4 was chosen the turtle placed way too much torches."), 3, ypos+1, w-3, " ")
844
  ypos = printWrapped(Splitter("- Many variables weren't refreshed after changing some options, messing up the preparation page."), 3, ypos+1, w-3, " ")
845
  ypos = printWrapped(Splitter("- When running out of fuel the turtle would think that it encountered an obstacle, constantly digging and hitting the air in front."), 3, ypos+1, w-3, " ")
846
  Sprint("Patch 2.4", 1, ypos+2)
847
  Sprint("---------", 1, ypos+3)
848
  Sprint("1.NEW FEATURES", 2, ypos+5)
849
  ypos = printWrapped(Splitter("- Option to place a floor, if needed"), 3, ypos+6, w-3, " ")
850
  ypos = printWrapped(Splitter("- Option to change language. Currently available: english, german"), 3, ypos+1, w-3, " ")
851
  ypos = printWrapped(Splitter("- Added a scrollbar to the options page, too"), 3, ypos+1, w-3, " ")
852
  Sprint("2.OPTIMIZATIONS", 2, ypos+2)
853
  ypos = printWrapped(Splitter("- The positions of the Buttons are determined more intelligent now, therefore being evenly spaced on every possible monitor size"), 3, ypos+3, w-3, " ")
854
  ypos = printWrapped(Splitter("- The structure of code is more centralized now, which results in faster processing and less blocked storage"), 3, ypos+1, w-3, " ")
855
  ypos = printWrapped(Splitter("- The Buttons of the options page won't slide up anymore when there is enough space, was a stupid feature anyways..."), 3, ypos+1, w-3, " ")
856
  Sprint("3.Bug fixes", 2, ypos+2)
857
  ypos = printWrapped(Splitter("- When more than 12 stacks torches were needed the ignore slots were overwritten, and wouldn't reset even when the options were changed to need less torches"), 3, ypos+3, w-3, " ")
858
  ypos = printWrapped(Splitter("- Fixed a bug that caused the turtle to claim having found an ore, even though it had not"), 3, ypos+1, w-3, " ")
859
  ypos = printWrapped(Splitter("- Fixed a bug where the turtle would place torches at wrong positions when the options were changed before start"), 3, ypos+1, w-3, " ")
860
  ypos = printWrapped(Splitter("- Everytime after placing a torch the turtle would freeze for a few seconds (related to the bug above), this precious time is being saved now"), 3, ypos+1, w-3, " ")
861
  Sprint("Patch 2.3", 1, ypos+2)
862
  Sprint("---------", 1, ypos+3)
863
  Sprint("1.LAYOUT", 2, ypos+5)
864
  ypos = printWrapped(Splitter("- Popup on execution in case of Errors"), 3, ypos+6, w-3, " ")
865
  ypos = printWrapped(Splitter("- Tiny optical improvement on the scrollbar"), 3, ypos+1, w-3, " ")
866
  Sprint("2.NEW FEATURES", 2, ypos+2)
867
  ypos = printWrapped(Splitter("- Option to make the turtle place the floor when it's missing"), 3, ypos+3, w-3, " ")
868
  ypos = printWrapped(Splitter("- Captions in the startmenu are visible for at least 5 seconds now"), 3, ypos+1, w-3, " ")
869
  Sprint("3.Bug fixes", 2, ypos+2)
870
  ypos = printWrapped(Splitter("- Autofuel would always refuel 5 coal, no matter how much was needed"), 3, ypos+3, w-3, " ")
871
  ypos = printWrapped(Splitter("- Options sometimes couldn't be selected or the layout got ugly"), 3, ypos+1, w-3, " ")
872
  ypos = printWrapped(Splitter("- In the startmenu the displayed amount of necessary torches was wrong"), 3, ypos+1, w-3, " ")
873
  Sprint("Patch 2.2", 1, ypos+2)
874
  Sprint("---------", 1, ypos+3)
875
  Sprint("1.NEW LAYOUT", 2, ypos+5)
876
  ypos = printWrapped(Splitter("- The awesome update window!"), 3, ypos+6, w-3, " ")
877
  ypos = printWrapped(Splitter("-> Anti-Annoying-Checkbox ;)"), 4, ypos+1, w-4, " ")
878
  ypos = printWrapped(Splitter("- This awesome news window!"), 3, ypos+1, w-3, " ")
879
  ypos = printWrapped(Splitter("-> Very functional scrollbar (mouse_wheel, arrow-keys or arrow-buttons can be used)"), 4, ypos+1, w-4, " ")
880
  Sprint("2.NEW FEATURES", 2, ypos+2)
881
  ypos = printWrapped(Splitter("- Favorite options are now savable"), 3, ypos+3, w-3, " ")
882
  ypos = printWrapped(Splitter("- There is a new function that, when activated, places torches automatically: Perfectly spaced so no mobs can spawn!!!"), 3, ypos+1, w-3, " ")
883
  ypos = printWrapped(Splitter("- Torches on the main floor are placed instantly now, to prevent mobs from spawning"), 3, ypos+1, w-3, " ")
884
  Sprint("3.Bug fixes", 2, ypos+2)
885
  ypos = printWrapped(Splitter("- Putting wrong items into a torchslot resulted in a wrong Errormessage"), 3, ypos+3, w-3, " ")
886
  ypos = printWrapped(Splitter("- When normal chest was selected, the program crashed trying to deploy the chest"), 3, ypos+1, w-3, " ")
887
  printLeft(string.rep(" ", w-2), h)
888
  printLeft("Page "..tostring(math.ceil((-menuVars["drawNews"].scroll+1)/10)).."/"..tostring(math.ceil((ypos-menuVars["drawNews"].scroll)/10)-1), h)
889
  printCentered(">Ok<", h)
890
  printLine(w, 3, h, "^", "v")
891
  drawHeader()
892
end
893
894
function drawHome(event, p1, p2, p3)
895
  for i = 1, 3 do
896
    printCentered(lang.layout(settings.language, i+8), round((h-5)*(i/4), "u")+i+2)
897
  end
898
  printCentered(string.rep("-", #lang.layout(settings.language, select+8)), round((h-5)*(select/4), "u")+select+3)
899
end
900
901
function drawStart(event, p1, p2, p3)
902
  --Fuellevel
903
  if turtle.getFuelLevel() ~= "unlimited" then
904
    MoveObjects(3, 2)
905
    printLeft(lang.layout(settings.language, 12), objects[3])
906
    if settings.Autofuel then
907
      fuelLevel = turtle.getFuelLevel()
908
      if fuelLevel < 3*variables.FuelDemand/settings.quantity then
909
        Fuellevel = "low"
910
        errors[8] = true
911
        printCentered(lang.layout(settings.language, 13), objects[3])
912
      elseif fuelLevel >= variables.FuelDemand then
913
        Fuellevel = "perfect"
914
        errors[8] = false
915
      elseif fuelLevel > variables.FuelDemand*0.5 then
916
        Fuellevel = "ready"
917
        errors[8] = false
918
      else
919
        Fuellevel = "risky"
920
        errors[8] = false
921
      end
922
    else
923
      if fuelLevel >= variables.FuelDemand*1.5 then
924
        Fuellevel = "perfect"
925
      elseif fuelLevel >= variables.FuelDemand*1 then
926
        Fuellevel = "ready"
927
      else
928
        Fuellevel = "low"
929
        printCentered(lang.layout(settings.language, 13), objects[3])
930
      end
931
    end
932
    if help[1] == false then
933
      help[1] = fuelLevel
934
    end
935
    if Color() then
936
      if Fuellevel == "perfect" then
937
        term.setBackgroundColor(colors.lime)
938
      elseif Fuellevel == "ready" or Fuellevel == "risky" then
939
        term.setBackgroundColor(colors.yellow)
940
      elseif Fuellevel == "low" then
941
        term.setBackgroundColor(colors.red)
942
      end
943
      printRight(fuelLevel.."/"..variables.FuelDemand, objects[3])
944
      term.setBackgroundColor(colors.black)
945
    else
946
      if Fuellevel == "perfect" then
947
        printRight("+ "..fuelLevel.."/"..variables.FuelDemand, objects[3])
948
      elseif Fuellevel == "ready" or Fuellevel == "risky" then
949
        printRight("+/- "..fuelLevel.."/"..variables.FuelDemand, objects[3])
950
      elseif Fuellevel == "low" then
951
        printRight("- "..fuelLevel.."/"..variables.FuelDemand, objects[3])
952
      end
953
    end
954
  else
955
    MoveObjects(3, 1)
956
  end
957
958
  --Torches
959
  local TorchAmount = 0
960
  ErrorSlot = 0
961
  for i = 1, #variables.slots do
962
    if variables.slots[i] == "T" then
963
      if ItemCount("minecraft:torch", i) ~= false then
964
        TorchAmount = TorchAmount + ItemCount("minecraft:torch", i)
965
      else
966
        ErrorSlot = i
967
      end
968
    end
969
  end
970
  if ErrorSlot ~= 0 then
971
    errors[1] = true
972
  else
973
    errors[1] = false
974
  end
975
  if variables.TorchDemand ~= 0 then
976
    MoveObjects(4, 2)
977
    printLeft(lang.layout(settings.language, 14), objects[4])
978
    if TorchAmount < variables.TorchDemand then
979
      Check.torch = false
980
      if Color() then
981
        term.setBackgroundColor(colors.red)
982
      end
983
    else
984
      Check.torch = true
985
      if Color() then
986
        term.setBackgroundColor(colors.lime)
987
      end
988
    end
989
    printRight(TorchAmount.."/"..variables.TorchDemand, objects[4])
990
    if Check.torch == false then
991
      errors[7] = true
992
    else
993
      errors[7] = false
994
    end
995
    term.setBackgroundColor(colors.black)
996
  else
997
    MoveObjects(4, 1)
998
    errors[7] = false
999
  end
1000
1001
  --Chest
1002
  errors[2] = false
1003
  errors[3] = false
1004
  errors[4] = false
1005
  errors[5] = false
1006
  errors[6] = false
1007
  Check.chest = false
1008
  if settings.chestSelect == 1 then
1009
    MoveObjects(5, 2)
1010
    printLeft(lang.layout(settings.language, 15), objects[5])
1011
    if Version < 1.64 then
1012
      if turtle.getItemCount(chestSlot) == 0 then
1013
        errors[4] = true
1014
      else
1015
        Check.chest = true
1016
      end
1017
    else
1018
      if ItemCount("minecraft:ender_chest", chestSlot) ~= false and ItemCount("minecraft:ender_chest", chestSlot) > 0 then
1019
        errors[2] = true
1020
      elseif ItemCount(enderChestList, chestSlot) == false then
1021
        errors[3] = true
1022
      elseif ItemCount(enderChestList, chestSlot) == 0 then
1023
        errors[4] = true
1024
      else
1025
        Check.chest = true
1026
      end
1027
    end
1028
  elseif settings.chestSelect == 2 then
1029
    MoveObjects(5, 2)
1030
    printLeft(lang.layout(settings.language, 18), objects[5])
1031
    if ItemCount(chestList, chestSlot) == false then
1032
      errors[5] = true
1033
    elseif ItemCount(chestList, chestSlot) == 0 then
1034
      errors[6] = true
1035
    else
1036
      Check.chest = true
1037
    end
1038
  else
1039
    Check.chest = true
1040
    MoveObjects(5, 1)
1041
  end
1042
  if Check.chest == false then
1043
    if Color() then
1044
      term.setBackgroundColor(colors.red)
1045
    end
1046
    printRight(lang.layout(settings.language, 16), objects[5])
1047
  else
1048
    if Color() then
1049
      term.setBackgroundColor(colors.lime)
1050
    end
1051
    printRight(lang.layout(settings.language, 17), objects[5])
1052
  end
1053
  term.setBackgroundColor(colors.black)
1054
1055
  --Autofuel
1056
  if settings.Autofuel == true then
1057
    MoveObjects(6, 2)
1058
    printLeft(lang.layout(settings.language, 19), objects[6])
1059
    term.setBackgroundColor(colors.lime)
1060
    printRight(lang.layout(settings.language, 17), objects[6])
1061
    term.setBackgroundColor(colors.black)
1062
    if help[4] == false then
1063
      help[4] = turtle.getItemCount(AutofuelSlot)
1064
    end
1065
  else
1066
    MoveObjects(6, 1)
1067
  end
1068
1069
  printLeft(string.rep("-", w), objects[7])
1070
1071
  --Inventory
1072
  if Version < 1.64 then
1073
    for i = 1, #variables.slots do
1074
      if variables.slots[i] == "I" then
1075
        variables.slots[i] = "_"
1076
      end
1077
    end
1078
    for i = 1, settings.ignor do
1079
      variables.slots[i] = "I"
1080
    end
1081
  end
1082
  for k, v in ipairs(SlotCalculator("T", "empty")) do
1083
    variables.slots[v] = "_"
1084
  end
1085
  local stupid = SlotCalculator("_", "empty")
1086
  for k, v in ipairs(stupid) do
1087
    if k > #stupid-math.ceil(variables.TorchDemand/64) then
1088
      variables.slots[v] = "T"
1089
    end
1090
  end
1091
  notificationCenter()
1092
  printRight("|"..variables.slots[1].."|"..variables.slots[2].."|"..variables.slots[3].."|"..variables.slots[4].."|", objects[8])
1093
  printRight("|"..variables.slots[5].."|"..variables.slots[6].."|"..variables.slots[7].."|"..variables.slots[8].."|", objects[9])
1094
  printRight("|"..variables.slots[9].."|"..variables.slots[10].."|"..variables.slots[11].."|"..variables.slots[12].."|", objects[10])
1095
  printRight("|"..variables.slots[13].."|"..variables.slots[14].."|"..variables.slots[15].."|"..variables.slots[16].."|", objects[11])
1096
1097
  if objects[11] < 12 then
1098
    printLeft(string.rep("-", w), 12)
1099
  end
1100
1101
  --Buttons
1102
  if select == 1 then
1103
    printB({">"..lang.layout(settings.language, 20).."<", " "..lang.layout(settings.language, 10).." ", " "..lang.layout(settings.language, 21).." "}, objects[13])
1104
  elseif select == 2 then
1105
    printB({" "..lang.layout(settings.language, 20).." ", ">"..lang.layout(settings.language, 10).."<", " "..lang.layout(settings.language, 21).." "}, objects[13])
1106
  elseif select == 3 then
1107
    printB({" "..lang.layout(settings.language, 20).." ", " "..lang.layout(settings.language, 10).." ", ">"..lang.layout(settings.language, 21).."<"}, objects[13])
1108
  end
1109
1110
  --Action
1111
  if fuelLevel < 3*variables.FuelDemand/settings.quantity then
1112
    local FuelAmount = math.ceil((3*variables.FuelDemand/settings.quantity-fuelLevel)/80)
1113
    for i = 1, #SlotCalculator("_", "minecraft:coal") do
1114
      turtle.select(SlotCalculator("_", "minecraft:coal")[i])
1115
      local output = refuel(FuelAmount, true)
1116
      if output == true then
1117
        errors[8] = false
1118
        Fuellevel = "risky"
1119
      else
1120
        FuelAmount = FuelAmount - output
1121
      end
1122
    end
1123
  end
1124
end
1125
1126
function drawOptions(event, p1, p2, p3)
1127
  if menuVars["drawOptions"].focus == 100 then
1128
    calculateFuelDemand()
1129
    TorchCalculator()
1130
    if menuVars["drawOptions"].saved == true and select == 3 then
1131
      select = 2
1132
    end
1133
  end
1134
  if event == "mouse_scroll" then
1135
    if menuVars["drawOptions"].focus == 100 then
1136
      menuVars["drawOptions"].scroll = menuVars["drawOptions"].scroll - p1
1137
    elseif menuVars["drawOptions"].focus == 13+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift and ((p1 < 0 and menuVars["drawOptions"].scroll2 < 0) or (p1 >= 0 and h < menuVars["drawOptions"].ypos)) then
1138
      menuVars["drawOptions"].scroll2 = menuVars["drawOptions"].scroll2 - p1
1139
    end
1140
  elseif event == "mouse_click" and p1 == 1 and menuVars["drawOptions"].focus == 100 then
1141
    if p2 == w then
1142
      if p3 == 3 then
1143
        menuVars["drawOptions"].scroll = menuVars["drawOptions"].scroll + 1
1144
      elseif p3 == h then
1145
        menuVars["drawOptions"].scroll = menuVars["drawOptions"].scroll - 1
1146
      end
1147
    else
1148
      menuVars["drawOptions"].focus = p3
1149
      if menuVars["drawOptions"].focus > 15+menuVars["drawOptions"].shift+menuVars["drawOptions"].shift2 or menuVars["drawOptions"].focus > h-3 or menuVars["drawOptions"].focus < 3 then
1150
        menuVars["drawOptions"].focus = 100
1151
      end
1152
    end
1153
  elseif event == "key" then
1154
    if p1 == 28 and menuVars["drawOptions"].focus ~= 0 then
1155
      menuVars["drawOptions"].saved = false
1156
      menuVars["drawOptions"].focus = 100
1157
    elseif p1 == 208 and menuVars["drawOptions"].focus == 100 then
1158
      menuVars["drawOptions"].scroll = menuVars["drawOptions"].scroll - 1
1159
    elseif p1 == 200 and menuVars["drawOptions"].focus == 100 then
1160
      menuVars["drawOptions"].scroll = menuVars["drawOptions"].scroll + 1
1161
    else
1162
      if settings.torches == false then
1163
        if menuVars["drawOptions"].focus == 8+menuVars["drawOptions"].scroll then
1164
          if p1 == 203 and settings.mainTorches == false then
1165
            settings.mainTorches = true
1166
          elseif p1 == 205 and settings.mainTorches == true then
1167
            settings.mainTorches = false
1168
          end
1169
        end
1170
      end
1171
      menuVars["drawOptions"].shift2 = 0
1172
      if Version >= 1.64 then
1173
        if menuVars["drawOptions"].focus == 14+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
1174
          if p1 == 203 and settings.trash == false then
1175
            settings.trash = true
1176
          elseif p1 == 205 and settings.trash == true then
1177
            settings.trash = false
1178
          end
1179
        end
1180
      else
1181
        menuVars["drawOptions"].shift2 = menuVars["drawOptions"].shift2-1
1182
      end
1183
      if menuVars["drawOptions"].focus == 3+menuVars["drawOptions"].scroll then
1184
        if p1 == 203 and settings.language == "de" then
1185
          settings.language = "en"
1186
        elseif p1 == 205 and settings.language == "en" then
1187
          settings.language = "de"
1188
        end
1189
      elseif menuVars["drawOptions"].focus == 4+menuVars["drawOptions"].scroll then
1190
        if p1 == 203 and settings.tunnelspace == 4 then
1191
          settings.tunnelspace = 3
1192
        elseif p1 == 205 and settings.tunnelspace == 3 then
1193
          settings.tunnelspace = 4
1194
        end
1195
      elseif menuVars["drawOptions"].focus == 7+menuVars["drawOptions"].scroll then
1196
        if p1 == 203 and settings.chestSelect > 1 then
1197
          settings.chestSelect = settings.chestSelect - 1
1198
        elseif p1 == 205 and settings.chestSelect < #chest then
1199
          settings.chestSelect = settings.chestSelect + 1
1200
        end
1201
        for k, v in ipairs(chest) do
1202
          if k == settings.chestSelect then
1203
            v = true
1204
          else
1205
            v = false
1206
          end
1207
        end
1208
      elseif menuVars["drawOptions"].focus == 8+menuVars["drawOptions"].scroll then
1209
        if p1 == 203 and settings.torches == false then
1210
          settings.torches = true
1211
        elseif p1 == 205 and settings.torches == true then
1212
          settings.torches = false
1213
        end
1214
      elseif menuVars["drawOptions"].focus == 11+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
1215
        if p1 == 203 and settings.Autofuel == false then
1216
          settings.Autofuel = true
1217
        elseif p1 == 205 and settings.Autofuel == true then
1218
          settings.Autofuel = false
1219
        end
1220
      elseif menuVars["drawOptions"].focus == 12+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
1221
        if p1 == 203 and settings.floor == false then
1222
          settings.floor = true
1223
        elseif p1 == 205 and settings.floor == true then
1224
          settings.floor = false
1225
        end
1226
      elseif menuVars["drawOptions"].focus == 13+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
1227
        if p1 == 200 and menuVars["drawOptions"].scroll2 < 0 then
1228
          menuVars["drawOptions"].scroll2 = menuVars["drawOptions"].scroll2+1
1229
        elseif p1 == 208 and h < menuVars["drawOptions"].ypos then
1230
          menuVars["drawOptions"].scroll2 = menuVars["drawOptions"].scroll2-1
1231
        elseif Version >= 1.64 then
1232
          if p1 == 57 or p1 == 14 then
1233
            for i = 1, 16 do
1234
              local data = turtle.getItemDetail(i)
1235
              if data then
1236
                for k, v in ipairs(settings.ignor) do
1237
                  if v == data.name then
1238
                    table.remove(settings.ignor, k)
1239
                  end
1240
                end
1241
                if p1 == 57 then
1242
                  table.insert(settings.ignor, data.name)
1243
                end
1244
              end
1245
            end
1246
          end
1247
        end
1248
      elseif menuVars["drawOptions"].focus == 15+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
1249
        if p1 == 203 and settings.walls == false then
1250
          settings.walls = true
1251
        elseif p1 == 205 and settings.walls == true then
1252
          settings.walls = false
1253
        end
1254
      end
1255
    end
1256
  end
1257
  if menuVars["drawOptions"].scroll > 0 then
1258
    menuVars["drawOptions"].scroll = 0
1259
  elseif menuVars["drawOptions"].scroll < -(5+menuVars["drawOptions"].shift+menuVars["drawOptions"].shift2) then
1260
    menuVars["drawOptions"].scroll = -(5+menuVars["drawOptions"].shift+menuVars["drawOptions"].shift2)
1261
  end
1262
1263
  if settings.torches == false then
1264
    if menuVars["drawOptions"].focus == 9+menuVars["drawOptions"].scroll then
1265
      printLeft(lang.layout(settings.language, 31), menuVars["drawOptions"].focus)
1266
      if settings.mainTorches == true then
1267
        printRight(">"..lang.layout(settings.language, 29).."<  "..lang.layout(settings.language, 30).." ", menuVars["drawOptions"].focus)
1268
      else
1269
        printRight(lang.layout(settings.language, 29).."  >"..lang.layout(settings.language, 30).."<", menuVars["drawOptions"].focus)
1270
      end
1271
    elseif menuVars["drawOptions"].focus == 10+menuVars["drawOptions"].scroll then
1272
      printLeft(lang.layout(settings.language, 32), menuVars["drawOptions"].focus)
1273
      term.setCursorPos(w-2, menuVars["drawOptions"].focus)
1274
      settings.lateralTorches = tonumber(read())
1275
      term.clear()
1276
      drawHeader()
1277
      return drawOptions("key", 28)
1278
    end
1279
    menuVars["drawOptions"].shift = 0
1280
  else
1281
    menuVars["drawOptions"].shift = -2
1282
  end
1283
  if Version >= 1.64 then
1284
    if menuVars["drawOptions"].focus == 14+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
1285
      printLeft(lang.layout(settings.language, 39), menuVars["drawOptions"].focus)
1286
      if settings.trash == true then
1287
        printRight(">"..lang.layout(settings.language, 29).."<  "..lang.layout(settings.language, 30).." ", menuVars["drawOptions"].focus)
1288
      else
1289
        printRight(lang.layout(settings.language, 29).."  >"..lang.layout(settings.language, 30).."<", menuVars["drawOptions"].focus)
1290
      end
1291
    end
1292
  end
1293
  if menuVars["drawOptions"].focus == 3+menuVars["drawOptions"].scroll then
1294
    printLeft(lang.layout(settings.language, 34), menuVars["drawOptions"].focus)
1295
    if settings.language == "en" then
1296
      printRight(">en<   de ", menuVars["drawOptions"].focus)
1297
    else
1298
      printRight("en   >de<", menuVars["drawOptions"].focus)
1299
    end
1300
  elseif menuVars["drawOptions"].focus == 4+menuVars["drawOptions"].scroll then
1301
    printLeft(lang.layout(settings.language, 22), menuVars["drawOptions"].focus)
1302
    if settings.tunnelspace == 3 then
1303
      printRight(">2<  3 ", menuVars["drawOptions"].focus)
1304
    else
1305
      printRight("2  >3<", menuVars["drawOptions"].focus)
1306
    end
1307
  elseif menuVars["drawOptions"].focus == 5+menuVars["drawOptions"].scroll then
1308
    printLeft(lang.layout(settings.language, 23), menuVars["drawOptions"].focus)
1309
    term.setCursorPos(w-2, menuVars["drawOptions"].focus)
1310
    settings.quantity = tonumber(read())
1311
    term.clear()
1312
    drawHeader()
1313
    return drawOptions("key", 28)
1314
  elseif menuVars["drawOptions"].focus == 6+menuVars["drawOptions"].scroll then
1315
    printLeft(lang.layout(settings.language, 24), menuVars["drawOptions"].focus)
1316
    term.setCursorPos(w-2, menuVars["drawOptions"].focus)
1317
    settings.length = tonumber(read())
1318
    term.clear()
1319
    drawHeader()
1320
    return drawOptions("key", 28)
1321
  elseif menuVars["drawOptions"].focus == 7+menuVars["drawOptions"].scroll then
1322
    printLeft(lang.layout(settings.language, 18), menuVars["drawOptions"].focus)
1323
    variables.slots[16] = "C"
1324
    if settings.chestSelect == 1 then
1325
      printRight(">"..lang.layout(settings.language, 25).."< "..lang.layout(settings.language, 26).."  "..lang.layout(settings.language, 27).." ", menuVars["drawOptions"].focus)
1326
    elseif settings.chestSelect == 2 then
1327
      printRight(lang.layout(settings.language, 25).." >"..lang.layout(settings.language, 26).."< "..lang.layout(settings.language, 27).." ", menuVars["drawOptions"].focus)
1328
    else
1329
      printRight(lang.layout(settings.language, 25).."  "..lang.layout(settings.language, 26).." >"..lang.layout(settings.language, 27).."<", menuVars["drawOptions"].focus)
1330
      if variables.slots[16] == "C" then
1331
        variables.slots[16] = "_"
1332
      end
1333
    end
1334
  elseif menuVars["drawOptions"].focus == 8+menuVars["drawOptions"].scroll then
1335
    printLeft(lang.layout(settings.language, 28), menuVars["drawOptions"].focus)
1336
    if settings.torches == true then
1337
      printRight(">"..lang.layout(settings.language, 29).."< "..lang.layout(settings.language, 30).." ", menuVars["drawOptions"].focus)
1338
    else
1339
      printRight(" "..lang.layout(settings.language, 29).." >"..lang.layout(settings.language, 30).."<", menuVars["drawOptions"].focus)
1340
    end
1341
  elseif menuVars["drawOptions"].focus == 11+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
1342
    printLeft(lang.layout(settings.language, 19), menuVars["drawOptions"].focus)
1343
    if settings.Autofuel == true then
1344
      printRight(">"..lang.layout(settings.language, 29).."<   "..lang.layout(settings.language, 30).." ", menuVars["drawOptions"].focus)
1345
    else
1346
      printRight(lang.layout(settings.language, 29).."   >"..lang.layout(settings.language, 30).."<", menuVars["drawOptions"].focus)
1347
    end
1348
  elseif menuVars["drawOptions"].focus == 12+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
1349
    printLeft(lang.layout(settings.language, 33), menuVars["drawOptions"].focus)
1350
    if settings.floor == true then
1351
      printRight(">"..lang.layout(settings.language, 29).."<   "..lang.layout(settings.language, 30).." ", menuVars["drawOptions"].focus)
1352
    else
1353
      printRight(lang.layout(settings.language, 29).."   >"..lang.layout(settings.language, 30).."<", menuVars["drawOptions"].focus)
1354
    end
1355
  elseif menuVars["drawOptions"].focus == 13+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
1356
    if Version >= 1.64 then
1357
      printLeft(lang.layout(settings.language, 36), 3)
1358
      local ypos = printWrapped(textutils.unserialize(string.gsub(textutils.serialize(settings.ignor), "minecraft:(%a+)", function(q) return q.."," end)), 1, 4, w)
1359
      menuVars["drawOptions"].ypos = printWrapped(Splitter(lang.layout(settings.language, 37, label)), 1, ypos+2+menuVars["drawOptions"].scroll2, w-1)
1360
      for i = 1, ypos+1 do
1361
        printLeft(string.rep(" ", w), i)
1362
      end
1363
      drawHeader()
1364
      printLeft(lang.layout(settings.language, 36), 3)
1365
      printWrapped(textutils.unserialize(string.gsub(textutils.serialize(settings.ignor), "minecraft:(%a+_*%a*)", function(q) return q.."," end)), 1, 4, w)
1366
      printLine(w, ypos+2, h, "^", "v")
1367
    else
1368
      printLeft(lang.layout(settings.language, 38), menuVars["drawOptions"].focus)
1369
      term.setCursorPos(w-2, menuVars["drawOptions"].focus)
1370
      settings.ignor = tonumber(read())
1371
      term.clear()
1372
      drawHeader()
1373
      return drawOptions("key", 28)
1374
    end
1375
  elseif menuVars["drawOptions"].focus == 15+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
1376
    printLeft(lang.layout(settings.language, 40), menuVars["drawOptions"].focus)
1377
    if settings.walls == true then
1378
      printRight(">"..lang.layout(settings.language, 29).."<   "..lang.layout(settings.language, 30).." ", menuVars["drawOptions"].focus)
1379
    else
1380
      printRight(lang.layout(settings.language, 29).."   >"..lang.layout(settings.language, 30).."<", menuVars["drawOptions"].focus)
1381
    end
1382
  elseif menuVars["drawOptions"].focus == 100 then
1383
    printLeft(lang.layout(settings.language, 34), 3+menuVars["drawOptions"].scroll)
1384
    printLeft(lang.layout(settings.language, 22), 4+menuVars["drawOptions"].scroll)
1385
    printLeft(lang.layout(settings.language, 23), 5+menuVars["drawOptions"].scroll)
1386
    printLeft(lang.layout(settings.language, 24), 6+menuVars["drawOptions"].scroll)
1387
    printLeft(lang.layout(settings.language, 18), 7+menuVars["drawOptions"].scroll)
1388
    printLeft(lang.layout(settings.language, 28), 8+menuVars["drawOptions"].scroll)
1389
    if settings.torches == false then
1390
      printLeft(lang.layout(settings.language, 31), 9+menuVars["drawOptions"].scroll)
1391
      printLeft(lang.layout(settings.language, 32), 10+menuVars["drawOptions"].scroll)
1392
    end
1393
    printLeft(lang.layout(settings.language, 19), 11+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift)
1394
    printLeft(lang.layout(settings.language, 33), 12+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift)
1395
    printLeft("#"..lang.layout(settings.language, 36), 13+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift)
1396
    printLeft(lang.layout(settings.language, 39), 14+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift)
1397
    printLeft(lang.layout(settings.language, 40), 15+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift)
1398
    printRight(settings.language, 3+menuVars["drawOptions"].scroll, 1)
1399
    printRight(tostring(settings.tunnelspace-1), 4+menuVars["drawOptions"].scroll, 1)
1400
    printRight(tostring(settings.quantity), 5+menuVars["drawOptions"].scroll, 1)
1401
    printRight(tostring(settings.length), 6+menuVars["drawOptions"].scroll, 1)
1402
    printRight(translate(chest[tonumber(settings.chestSelect)], "layout"), 7+menuVars["drawOptions"].scroll, 1)
1403
    printRight(translate(tostring(settings.torches), "layout"), 8+menuVars["drawOptions"].scroll, 1)
1404
    if settings.torches == false then
1405
      printRight(translate(tostring(settings.mainTorches), "layout"), 9+menuVars["drawOptions"].scroll, 1)
1406
      printRight(tostring(settings.lateralTorches), 10+menuVars["drawOptions"].scroll, 1)
1407
    end
1408
    printRight(translate(tostring(settings.Autofuel), "layout"), 11+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift, 1)
1409
    printRight(translate(tostring(settings.floor), "layout"), 12+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift, 1)
1410
    if Version >= 1.64 then
1411
      printRight(tostring(#settings.ignor-4), 13+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift, 1)
1412
    else
1413
      printRight(tostring(settings.ignor), 13+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift, 1)
1414
    end
1415
    printRight(translate(tostring(settings.trash), "layout"), 14+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift, 1)
1416
    printRight(translate(tostring(settings.walls), "layout"), 15+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift, 1)
1417
    printLeft(string.rep(" ", w-1), h)
1418
    printLeft(string.rep(" ", w-1), h-1)
1419
    printLeft(string.rep(" ", w-1), h-2)
1420
    printB({lang.layout(settings.language, 9), lang.layout(settings.language, 21), lang.layout(settings.language, 35)}, 12)
1421
    if select == 1 then
1422
      printB({string.rep("_", #lang.layout(settings.language, 9)), string.rep(" ", #lang.layout(settings.language, 21)), string.rep(" ", #lang.layout(settings.language, 35))}, 13)
1423
    elseif select == 2 then
1424
      printB({string.rep(" ", #lang.layout(settings.language, 9)), string.rep("_", #lang.layout(settings.language, 21)), string.rep(" ", #lang.layout(settings.language, 35))}, 13)
1425
    else
1426
      printB({string.rep(" ", #lang.layout(settings.language, 9)), string.rep(" ", #lang.layout(settings.language, 21)), string.rep("_", #lang.layout(settings.language, 35))}, 13)
1427
    end
1428
    printLine(w, 3, h-2, "^", "v")
1429
    drawHeader()
1430
  end
1431
end
1432
1433
function drawResume(time)
1434
  term.setCursorPos(1, 3)
1435
  print("Program will resume in "..tostring(time).." seconds.\n")
1436
  print("Press Enter to stop resuming...")
1437
end
1438
1439
menu = {
1440
  ["Update"] = {
1441
    options = {"News", "Home"},
1442
    orientation = "horizontal",
1443
    draw = drawUpdate
1444
  },
1445
  ["News"] = {
1446
    options = {"Home"},
1447
    orientation = "horizontal",
1448
    draw = drawNews
1449
  },
1450
  ["Home"] = {
1451
    options = {"Start", "Options", "Quit"},
1452
    orientation = "vertical",
1453
    draw = drawHome
1454
  },
1455
  ["Start"] = {
1456
    options = {"Go!", "Options", "Home"},
1457
    orientation = "horizontal",
1458
    draw = drawStart
1459
  },
1460
  ["Options"] = {
1461
    options = {"Start", "Home", "Save as default"},
1462
    orientation = "horizontal",
1463
    draw = drawOptions
1464
  },
1465
  ["Popup"] = {
1466
    options = {"Go!", "Start", "Quit"},
1467
    orientation = "horizontal",
1468
    draw = drawPopup
1469
  }
1470
}
1471
1472
-----###-----sequences-----###-----
1473
1474
function Return()
1475
  face(1)
1476
  if #variables.cache == 0 then
1477
    return
1478
  elseif variables.cache[#variables.cache] == 5 then
1479
    insert('FreewayDown()')
1480
  elseif variables.cache[#variables.cache] == 6 then
1481
    insert('FreewayUp()')
1482
  else
1483
    insert('FreewayBack()')
1484
    insert('variables.direction = variables.cache[#variables.cache]')
1485
  end
1486
  insert('face(1)')
1487
  insert('table.remove(variables.cache, #variables.cache)')
1488
end
1489
1490
function compare(method, slotNum)
1491
  turtle.select(slotNum)
1492
  if method == "down" then
1493
    return turtle.compareDown()
1494
  elseif method == "up" then
1495
    return turtle.compareUp()
1496
  elseif method == "front" then
1497
    return turtle.compare()
1498
  end
1499
end
1500
1501
function CompareDown(walls)
1502
  if Version >= 1.64 then
1503
    local boolean, data = turtle.inspectDown()
1504
    if boolean then
1505
      for k, v in pairs(settings.ignor) do
1506
        if v == data.name then
1507
          return
1508
        end
1509
      end
1510
    else
1511
      return
1512
    end
1513
  elseif turtle.detectDown() then
1514
    for k, v in pairs(SlotCalculator("I")) do
1515
      if compare("down", k) == true then
1516
        return
1517
      end
1518
    end
1519
  else
1520
    return
1521
  end
1522
  insert('variables.searching = true')
1523
  insert('turtle.select(SlotCalculator("_", "empty")[1])')
1524
  insert('digDown()')
1525
  insert('FreewayDown()')
1526
  insert('table.insert(variables.cache, 6)')
1527
  insert('CompareAll("CompareUp()")')
1528
end
1529
1530
function CompareUp(walls)
1531
  if Version >= 1.64 then
1532
    local boolean, data = turtle.inspectUp()
1533
    if boolean then
1534
      for k, v in pairs(settings.ignor) do
1535
        if v == data.name then
1536
          return (walls and placeWall("placeUp")) or nil
1537
        end
1538
      end
1539
    else
1540
      return (walls and placeWall("placeUp")) or nil
1541
    end
1542
  elseif turtle.detectUp() then
1543
    for k, v in pairs(SlotCalculator("I")) do
1544
      if compare("up", k) == true then
1545
        return
1546
      end
1547
    end
1548
  else
1549
    return (walls and placeWall("placeUp")) or nil
1550
  end
1551
  insert('variables.searching = true')
1552
  insert('turtle.select(SlotCalculator("_", "empty")[1])')
1553
  insert('digUp()')
1554
  insert('FreewayUp()')
1555
  insert('table.insert(variables.cache, 5)')
1556
  if walls == true then
1557
    insert('placeWall()')
1558
  end
1559
  insert('CompareAll("CompareDown()")')
1560
end
1561
1562
function CompareRight(walls)
1563
  Compare(4, walls)
1564
end
1565
1566
function CompareBack(walls)
1567
  Compare(3, walls)
1568
end
1569
1570
function CompareLeft(walls)
1571
  Compare(2, walls)
1572
end
1573
1574
function Compare(facing, walls)
1575
  if facing == nil then
1576
    facing = 1
1577
  elseif type(facing) == "boolean" then
1578
    walls = facing
1579
    facing = 1
1580
  end
1581
  face(facing)
1582
  if Version >= 1.64 then
1583
    local boolean, data = turtle.inspect()
1584
    if boolean then
1585
      for k, v in pairs(settings.ignor) do
1586
        if v == data.name then
1587
          return (walls and placeWall("placeUp")) or nil
1588
        end
1589
      end
1590
    else
1591
      return (walls and placeWall()) or nil
1592
    end
1593
  elseif turtle.detect() then
1594
    for k, v in pairs(SlotCalculator("I")) do
1595
      if compare("front", k) == true then
1596
        return
1597
      end
1598
    end
1599
  else
1600
    return (walls and placeWall()) or nil
1601
  end
1602
  insert('variables.searching = true')
1603
  insert('turtle.select(SlotCalculator("_", "empty")[1])')
1604
  insert('dig()')
1605
  insert('Freeway()')
1606
  insert('table.insert(variables.cache, variables.direction)')
1607
  if walls == true then
1608
    insert('placeWall("placeUp")')
1609
  end
1610
  insert('variables.direction = 1')
1611
  insert('CompareAll("CompareBack()")')
1612
end
1613
1614
function CompareAll(...)
1615
  local exceptions = {...}
1616
  local walls
1617
  if type(exceptions[1]) == "boolean" then
1618
    walls = exceptions[1]
1619
    table.remove(exceptions, 1)
1620
  end
1621
  insert('Compare()')
1622
  insert('CompareLeft()')
1623
  insert('CompareBack()')
1624
  insert('CompareRight()')
1625
  insert('CompareUp()')
1626
  insert('CompareDown()')
1627
  insert('Return()')
1628
  local sub = 0
1629
  for j = 2, 8-sub do
1630
    for k, v in ipairs(exceptions) do
1631
      if v == todoList[j] then
1632
        table.remove(todoList, j)
1633
        table.remove(exceptions, k)
1634
        j = j - 1
1635
        sub = sub + 1
1636
      end
1637
    end
1638
  end
1639
  if walls == true then
1640
    for i = 2, 15 do
1641
      if todoList[i] == 'Return()' then
1642
        break
1643
      elseif string.find(todoList[i], 'placeWall(') == nil then
1644
        if string.find(todoList[i], 'Up') ~= nil then
1645
          table.insert(todoList, i+1, 'placeWall("placeUp")')
1646
        elseif string.find(todoList[i], 'Down') == nil then
1647
          table.insert(todoList, i+1, 'placeWall()')
1648
        end
1649
      end
1650
    end
1651
  end
1652
end
1653
1654
function trash(keptCobble)
1655
  for k, v in ipairs(SlotCalculator("_")) do
1656
    local data = turtle.getItemDetail(v)
1657
    for k2, v2 in pairs(settings.ignor) do
1658
      if v2 == data.name then
1659
        if not keptCobble and v2 == "minecraft:cobblestone" then
1660
          keptCobble = true
1661
        else
1662
          turtle.select(v)
1663
          turtle.drop()
1664
        end
1665
      end
1666
    end
1667
  end
1668
end
1669
1670
function Enderchest(keptCoal, keptCobble)
1671
  if (#SlotCalculator("_") >= #SlotCalculator("_", "empty")-1-math.ceil(settings.length/12) or keptCoal == true) and #SlotCalculator("C", "check") ~= 0 then
1672
    turtle.select(SlotCalculator("C", "check")[1])
1673
    if variables.level == 1 then
1674
      insert('FreewayDown()')
1675
    end
1676
    insert('FreewayBack()')
1677
    insert([[while not turtle.place() do
1678
      turtle.attack()
1679
    end]])
1680
    insert([[for k, v in ipairs(SlotCalculator("_")) do
1681
      if settings.Autofuel and not keptCoal and Version >= 1.64 and turtle.getItemDetail(v).name == "minecraft:coal" then
1682
        keptCoal = true
1683
      elseif not keptCobble and v == "minecraft:cobblestone" then
1684
        keptCobble = true
1685
      else
1686
        turtle.select(v)
1687
        while not turtle.drop() do
1688
          print(lang.status(settings.language, 3))
1689
          sleep(10)
1690
        end
1691
      end
1692
    end
1693
    turtle.select(SlotCalculator("C", "empty")[1])]])
1694
    insert('dig()')
1695
    insert('Freeway()')
1696
    insert([[turtle.select(SlotCalculator("_", "empty")[1])
1697
    print(lang.status(settings.language, 4))]])
1698
  end
1699
end
1700
1701
function NormalChest(keptCoal, keptCobble)
1702
  local function goToOrigin()
1703
    insert('turn()')
1704
    insert([[if variables.level == 1 then
1705
      FreewayDown()
1706
    end
1707
    for Way_Back = 1, variables.CrosswayAmount*settings.tunnelspace do
1708
      Freeway()
1709
    end]])
1710
    insert('turn()')
1711
  end
1712
  local function dropOff()
1713
    insert([[for k, v in ipairs(SlotCalculator("_")) do
1714
      if settings.Autofuel and not keptCoal and Version >= 1.64 and turtle.getItemDetail(v).name == "minecraft:coal" then
1715
        keptCoal = true
1716
      elseif not keptCobble and v == "minecraft:cobblestone" then
1717
        keptCobble = true
1718
      else
1719
        turtle.select(v)
1720
        while not turtle.dropDown() and turtle.getItemCount() ~= 0 do
1721
          print(lang.status(settings.language, 3))
1722
          sleep(10)
1723
        end
1724
      end
1725
    end]])
1726
  end
1727
  if variables.setup == true then
1728
    goToOrigin()
1729
    dropOff()
1730
    insert([[for Way_Back = 1, variables.CrosswayAmount*settings.tunnelspace do
1731
      Freeway()
1732
    end]])
1733
    insert('print(lang.status(settings.language, 4))')
1734
  elseif #SlotCalculator("C", "check") == 0 then
1735
      variables.setup = false
1736
  elseif #SlotCalculator("_") >= #SlotCalculator("_", "empty")-1-math.ceil(settings.length/12) then
1737
    goToOrigin()
1738
    insert('turtle.select(SlotCalculator("C", "check")[1])')
1739
    insert([[while not turtle.placeDown() do
1740
      digDown()
1741
    end]])
1742
    insert('variables.setup = true')
1743
    dropOff()
1744
    insert([[for Way_Back = 1, variables.CrosswayAmount*settings.tunnelspace do
1745
      Freeway()
1746
    end]])
1747
    insert('print(lang.status(settings.language, 4))')
1748
  end
1749
end
1750
1751
function Fuel()
1752
  local FuelAmount = math.ceil(3*variables.FuelDemand/settings.quantity/80)
1753
  fuelLevel = turtle.getFuelLevel()
1754
  if fuelLevel < 3*variables.FuelDemand/settings.quantity then
1755
    while true do
1756
      while #SlotCalculator("_", "minecraft:coal") > 0 do
1757
        turtle.select(SlotCalculator("_", "minecraft:coal")[1])
1758
        local output = refuel(FuelAmount, true)
1759
        if output == true then
1760
          print(lang.status(settings.language, 5))
1761
          return
1762
        else
1763
          FuelAmount = FuelAmount - output
1764
        end
1765
      end
1766
      term.clear()
1767
      term.setCursorPos(1,1)
1768
      print(lang.status(settings.language, 6))
1769
      write(lang.status(settings.language, 7))
1770
      for k, v in ipairs(SlotCalculator("_", "empty")) do
1771
        if k == #SlotCalculator("_", "empty") then
1772
          print(v)
1773
        else
1774
          write(v..", ")
1775
        end
1776
      end
1777
      os.pullEvent()
1778
    end
1779
  end
1780
end
1781
1782
function placeFloor()
1783
  if settings.floor == true and turtle.detectDown() == false and #SlotCalculator("_", "minecraft:cobblestone") ~= 0 then
1784
    turtle.select(SlotCalculator("_", "minecraft:cobblestone")[1])
1785
    turtle.placeDown()
1786
  end
1787
end
1788
1789
function placeWall(param)
1790
  if param == true then
1791
    param = "place"
1792
  else
1793
    param = param or "place"
1794
  end
1795
  if #SlotCalculator("_", "minecraft:cobblestone") > 0 then
1796
    turtle.select(SlotCalculator("_", "minecraft:cobblestone")[1])
1797
    turtle[param]()
1798
    return true
1799
  end
1800
  return false
1801
end
1802
1803
-----###-----Processing-----###-----
1804
function TorchCalculator()
1805
  local Tspace
1806
  if settings.torches == true then
1807
    if (settings.tunnelspace == 4 and settings.length <= 8) or (settings.tunnelspace == 3 and settings.length <= 10) then
1808
      settings.lateralTorches = 1
1809
    else
1810
      settings.lateralTorches = math.floor((settings.length-math.floor(16/settings.tunnelspace))/12)+1
1811
    end
1812
    if (settings.tunnelspace == 4 and settings.length <= 3) or (settings.tunnelspace == 3 and settings.length <= 4) then
1813
      settings.mainTorches = true
1814
      settings.lateralTorches = 0
1815
    elseif (settings.tunnelspace == 4 and ((settings.length+2)%12 <= 5 or settings.length == 9)) or (settings.tunnelspace == 3 and (settings.length+1)%12 <= 5) then
1816
      settings.mainTorches = true
1817
    else
1818
      settings.mainTorches = false
1819
    end
1820
  end
1821
  if settings.lateralTorches >= 1 then
1822
    if settings.mainTorches == true then
1823
      Tspace = round((settings.length+(settings.tunnelspace-1)-settings.lateralTorches)/(settings.lateralTorches+0.5), "u")
1824
    else
1825
      Tspace = (settings.length+(settings.tunnelspace-1)-settings.lateralTorches)/settings.lateralTorches
1826
    end
1827
    if Tspace == 11 then
1828
      variables.maxSpace = true
1829
    else
1830
      variables.maxSpace = false
1831
    end
1832
    local done = false
1833
    while not done do
1834
      variables.torchPositions = {}
1835
      table.insert(variables.torchPositions, round(Tspace/2, "d")+1)
1836
      for i = 2, settings.lateralTorches do
1837
        table.insert(variables.torchPositions, round(Tspace+1+variables.torchPositions[i-1], "d"))
1838
      end
1839
      if variables.torchPositions[#variables.torchPositions] > settings.length then
1840
        Tspace = Tspace - 0.1
1841
      else
1842
        done = true
1843
      end
1844
    end
1845
  else
1846
    variables.maxSpace = false
1847
    variables.torchPositions = {}
1848
  end
1849
  variables.TorchDemand = 2*(settings.lateralTorches*settings.quantity)+1
1850
  if settings.mainTorches == true then
1851
    variables.TorchDemand = variables.TorchDemand + settings.quantity
1852
  end
1853
  for k, v in ipairs(SlotCalculator("T", "empty")) do
1854
    variables.slots[v] = "_"
1855
  end
1856
  local stupid = SlotCalculator("_", "empty")
1857
  for k, v in ipairs(stupid) do
1858
    if k > #stupid-math.ceil(variables.TorchDemand/64) then
1859
      variables.slots[v] = "T"
1860
    end
1861
  end
1862
end
1863
1864
function calculateFuelDemand()
1865
  if settings.chestSelect == 1 then
1866
    variables.FuelDemand = ((settings.length*2+settings.tunnelspace)*3+3)*settings.quantity
1867
  elseif settings.chestSelect == 2 then
1868
    variables.FuelDemand = math.ceil(((settings.length*2+settings.tunnelspace)*3+2)*settings.quantity+settings.tunnelspace*(settings.quantity/2)^2)
1869
  else
1870
    variables.FuelDemand = ((settings.length*2+settings.tunnelspace)*3+2)*settings.quantity
1871
  end
1872
  if variables.maxSpace == true then
1873
    variables.FuelDemand = variables.FuelDemand + #variables.torchPositions
1874
  end
1875
end
1876
1877
1878
-----###-----Core functions-----###-----
1879
function update()
1880
  if not fs.exists("database/"..programName.."/state") then
1881
  	save("database/"..programName.."/state", "0")
1882
  end
1883
  file = fs.open("database/"..programName.."/state", "r")
1884
  local fileData = {}
1885
  local line = file.readLine()
1886
  repeat
1887
    table.insert(fileData, line)
1888
    line = file.readLine()
1889
  until line == nil
1890
  file.close()
1891
  updated = fileData[1]
1892
1893
  if updated == "0" then
1894
    kill = true
1895
    term.clear()
1896
    print("loading...")
1897
    shell.run("pastebin get cR9vEiTc database/"..programName.."/lang")
1898
    term.clear()
1899
    local function installAsStartup()
1900
      if fs.exists("startup") then
1901
        os.loadAPI("startup")
1902
        if startup.name == "SupHa - Startup Handler by BrunoZockt" then
1903
          for k, v in ipairs(startup.getList()) do
1904
            if v == programName then
1905
              return
1906
            end
1907
          end
1908
          startup.set(programName)
1909
        else
1910
          os.unloadAPI("startup")
1911
          fs.move("startup", "old_startup")
1912
          shell.run("pastebin get 19VG8eD6 startup")
1913
          os.loadAPI("startup")
1914
          startup.set("old_startup", "standart")
1915
          startup.set(programName)
1916
        end
1917
      else
1918
        shell.run("pastebin get 19VG8eD6 startup")
1919
        os.loadAPI("startup")
1920
        startup.set(programName)
1921
      end
1922
    end
1923
    installAsStartup()
1924
    if programName ~= "OCM" then
1925
      fs.delete("database/OCM")
1926
    end
1927
    if shell.run("database/"..programName.."/updaterV2") == false then
1928
      fs.delete("database/"..programName.."/updater")
1929
      term.clear()
1930
  	  print("loading...")
1931
      shell.run("pastebin get rUMt9siN database/"..programName.."/updaterV2")
1932
      term.clear()
1933
      shell.run("database/"..programName.."/updaterV2")
1934
    end
1935
  end
1936
end
1937
1938
function getVariables(path)
1939
  local extSettings = {}
1940
  if fs.exists("database/"..programName.."/"..path) then
1941
    file = fs.open("database/"..programName.."/"..path, "r")
1942
    local line = Splitter(file.readLine(), " = ")
1943
    repeat
1944
      if line[2] == "{" or line[2] == "{}" then
1945
        local str = line[2]
1946
        if line[2] == "{" then
1947
        repeat
1948
          local tline = file.readLine()
1949
          if tline ~= nil then
1950
            str = str..tline
1951
          end
1952
        until tline == "}"
1953
        end
1954
        extSettings[line[1]] = textutils.unserialize(str)
1955
      else
1956
        extSettings[line[1]] = line[2]
1957
      end
1958
      line = Splitter(file.readLine(), " = ")
1959
    until line == nil
1960
    file.close()
1961
    for k, v in pairs(extSettings) do
1962
      if type(extSettings[k]) ~= "table" then
1963
        if not tonumber(extSettings[k]) then
1964
          if extSettings[k] == "true" or extSettings[k] == "false" then
1965
            extSettings[k] = extSettings[k] == "true"
1966
          end
1967
        else
1968
          extSettings[k] = tonumber(extSettings[k])
1969
        end
1970
      end
1971
      if Splitter(path, "/")[#Splitter(path, "/")] == "settings" then
1972
        settings[k] = extSettings[k]
1973
      elseif Splitter(path, "/")[#Splitter(path, "/")] == "variables" then
1974
        variables[k] = extSettings[k]
1975
      end
1976
    end
1977
  end
1978
  if fs.exists("database/"..programName.."/updateCheckbox/V2.6") then
1979
    local file = fs.open("database/"..programName.."/updateCheckbox/V2.6", "r")
1980
    checkbox = file.readLine() == "true"
1981
    file.close()
1982
  end
1983
  if checkbox == true then
1984
    menustate = "Home"
1985
  else
1986
    menustate = "Update"
1987
  end
1988
  if settings.chestSelect < 3 and #SlotCalculator("C", "empty") == 0 then
1989
    variables.slots[16] = "C"
1990
  end
1991
  calculateFuelDemand()
1992
  TorchCalculator()
1993
end
1994
1995
function getList(path)
1996
  local file = fs.open("database/"..programName.."/"..path, "r")
1997
  todoList = textutils.unserialize(Splitter(file.readLine(), " = ")[2]..file.readAll())
1998
  file.close()
1999
end
2000
2001
function saveVars(path, content)
2002
  save("database/"..programName.."/"..path, content)
2003
end
2004
2005
function compStep(n)
2006
  if n == nil then
2007
    n = 1
2008
  else
2009
    n = tonumber(n)
2010
  end
2011
  for i = 1, n do
2012
    insert('turtle.select(SlotCalculator("T", "empty")[1])')
2013
    insert('Freeway()')
2014
    if variables.level == 0 then
2015
      insert('CompareAll("Compare()", "CompareBack()", "CompareUp()")')
2016
      insert('placeFloor()')
2017
      insert('turtle.select(SlotCalculator("T", "empty")[1])')
2018
      insert('FreewayUp()')
2019
      insert('CompareAll("Compare()", "CompareBack()", "CompareDown()")')
2020
    elseif variables.level == 1 then
2021
      insert('CompareAll("Compare()", "CompareBack()", "CompareDown()")')
2022
      insert('turtle.select(SlotCalculator("T", "empty")[1])')
2023
      insert('FreewayDown()')
2024
      insert('CompareAll("Compare()", "CompareBack()", "CompareUp()")')
2025
      insert('placeFloor()')
2026
    end
2027
  end
2028
end
2029
2030
function mainstep(n, first)
2031
  if n == nil then
2032
    n = 1
2033
  else
2034
    n = tonumber(n)
2035
  end
2036
  turtle.select(SlotCalculator("_", "empty")[1])
2037
  for i = 1, n do
2038
    insert('variables.searching = false')
2039
    insert('dig()')
2040
    insert('Freeway()')
2041
    insert([[if variables.level == 0 then
2042
      insert('placeFloor()')
2043
      insert('digUp()')
2044
      if ]]..tostring(first)..[[ and ]]..tostring(i)..[[ == 1 or (settings.walls == true and ]]..tostring(i)..[[ == 2 and settings.tunnelspace == 4) then
2045
        insert('CompareAll("Compare()", "CompareBack()", "CompareUp()")')
2046
      else
2047
        insert('CompareDown()')
2048
      end
2049
      insert('FreewayUp()')
2050
      if ]]..tostring(first)..[[ and ]]..tostring(i)..[[ == 1 or (settings.walls == true and ]]..tostring(i)..[[ == 2 and settings.tunnelspace == 4) then
2051
        insert('CompareAll("Compare()", "CompareBack()", "CompareDown()")')
2052
      else
2053
        insert('CompareUp()')
2054
        insert('placeWall("placeUp")')
2055
      end
2056
    elseif variables.level == 1 then
2057
      insert('digDown()')
2058
      if ]]..tostring(first)..[[ and ]]..tostring(i)..[[ == 1 or (settings.walls == true and ]]..tostring(i)..[[ == 2 and settings.tunnelspace == 4) then
2059
        insert('CompareAll("Compare()", "CompareBack()", "CompareDown()")')
2060
      else
2061
        insert('CompareUp()')
2062
        insert('placeWall("placeUp")')
2063
      end
2064
      if i == 2 and settings.mainTorches then
2065
        insert('variables.searching = false')
2066
        insert('dig(true)')
2067
      end
2068
      insert('FreewayDown()')
2069
      if ]]..tostring(first)..[[ and ]]..tostring(i)..[[ == 1 or (settings.walls == true and ]]..tostring(i)..[[ == 2 and settings.tunnelspace == 4) then
2070
        insert('CompareAll("Compare()", "CompareBack()", "CompareUp()")')
2071
      else
2072
        insert('CompareDown()')
2073
      end
2074
      insert('placeFloor()')
2075
    end]])
2076
    if settings.mainTorches == true then
2077
      if i == 2 and #SlotCalculator('T', 'check') ~= 0 then
2078
        insert([[turtle.select(SlotCalculator('T', 'check')[1])
2079
        if variables.level == 1 then
2080
          FreewayDown()
2081
        end]])
2082
        insert([[if not turtle.placeUp() then
2083
          insert('FreewayUp()')
2084
          insert('left()')
2085
          insert('turtle.select(SlotCalculator("_", "minecraft:cobblestone")[1])')
2086
          insert('dig()')
2087
          insert('turtle.place()')
2088
          insert('right()')
2089
          insert('FreewayDown()')
2090
          insert('turtle.select(SlotCalculator("T", "check")[1])')
2091
          insert('turtle.placeUp()')
2092
        end]])
2093
        insert([[for k, v in ipairs(SlotCalculator('T', 'empty')) do
2094
          variables.slots[v] = '_'
2095
        end
2096
        local stupid = SlotCalculator('_', 'empty')
2097
        for k, v in ipairs(stupid) do
2098
          if k > #stupid-math.ceil(variables.TorchDemand/64) then
2099
            variables.slots[v] = 'T'
2100
          end
2101
        end
2102
        variables.TorchDemand = variables.TorchDemand - 1]])
2103
      end
2104
    end
2105
  end
2106
end
2107
2108
function stepAside(n)
2109
  if n == nil then
2110
    n = 1
2111
  else
2112
    n = tonumber(n)
2113
  end
2114
  for i = 1, n do
2115
    insert('variables.searching = false')
2116
    insert('dig()')
2117
    insert('Freeway()')
2118
    if variables.level == 1 then
2119
      insert('digDown()')
2120
    elseif variables.level == 0 then
2121
      insert('placeFloor()')
2122
      insert('digUp()')
2123
    end
2124
  end
2125
end
2126
2127
function youJustGotLittUp(steps, func, left)
2128
  if left == nil then
2129
    left = false
2130
  end
2131
  for Steprepeat = 1, steps do
2132
    if func == stepAside then
2133
      insert('stepAside()')
2134
    end
2135
    for k, v in ipairs(variables.torchPositions) do
2136
      if ((func == stepAside and settings.length-v+1 == Steprepeat) or (func == compStep and v == Steprepeat)) and #SlotCalculator("T", "check") > 0 then
2137
        insert([[turtle.select(SlotCalculator("T", "check")[1])
2138
        if variables.maxSpace == true then
2139
          if settings.mainTorches == false and left == true then
2140
            if k%2 == #variables.torchPositions%2 then
2141
              if variables.level == 1 then
2142
                if variables.orientation[1] == "North" or variables.orientation[1] == "South" then
2143
                  insert('dig()')
2144
                end
2145
                insert('FreewayDown()')
2146
              elseif variables.orientation[1] == "North" or variables.orientation[1] == "South" then
2147
                insert('FreewayUp()')
2148
                insert('dig()')
2149
                insert('FreewayDown()')
2150
              end
2151
            else
2152
              if variables.level == 0 then
2153
                insert('FreewayUp()')
2154
              end
2155
            end
2156
          else
2157
            if k%2 == #variables.torchPositions%2 then
2158
              if variables.level == 0 then
2159
                insert('FreewayUp()')
2160
              end
2161
            else
2162
              if variables.level == 1 then
2163
                if variables.orientation[1] == "North" or variables.orientation[1] == "South" then
2164
                  insert('dig()')
2165
                end
2166
                insert('FreewayDown()')
2167
              elseif variables.orientation[1] == "North" or variables.orientation[1] == "South" then
2168
                insert('FreewayDown()')
2169
                insert('dig()')
2170
                insert('FreewayUp()')
2171
              end
2172
            end
2173
          end
2174
        elseif variables.orientation[1] == "North" or variables.orientation[1] == "South" then
2175
          if variables.level == 0 then
2176
            insert('FreewayUp()')
2177
          end
2178
        end]])
2179
        insert([[if variables.level == 1 then
2180
          turtle.placeDown()
2181
        elseif variables.level == 0 then
2182
          if not turtle.placeUp() then
2183
            insert('FreewayUp()')
2184
            insert('left()')
2185
            insert('turtle.select(SlotCalculator("_", "minecraft:cobblestone")[1])')
2186
            insert('dig()')
2187
            insert('turtle.place()')
2188
            insert('right()')
2189
            insert('FreewayDown()')
2190
            insert('turtle.select(SlotCalculator("T", "check")[1])')
2191
            insert('turtle.placeUp()')
2192
          end
2193
        end]])
2194
        insert([[for k, v in ipairs(SlotCalculator("T", "empty")) do
2195
          variables.slots[v] = "_"
2196
        end
2197
        local stupid = SlotCalculator("_", "empty")
2198
        for k, v in ipairs(stupid) do
2199
          if k > #stupid-math.ceil(variables.TorchDemand/64) then
2200
            variables.slots[v] = "T"
2201
          end
2202
        end]])
2203
        insert([[if func == compStep then
2204
          variables.TorchDemand = variables.TorchDemand - 1
2205
        end
2206
        turtle.select(SlotCalculator("_", "empty")[1])]])
2207
      end
2208
    end
2209
    if func == compStep then
2210
      if Steprepeat == settings.length then
2211
        insert('Freeway()')
2212
      else
2213
        insert('compStep()')
2214
      end
2215
    end
2216
  end
2217
end
2218
2219
function tunnel(first)
2220
  if settings.Autofuel == true then
2221
    insert('Fuel()')
2222
  end
2223
  if settings.trash == true then
2224
    insert('trash()')
2225
  end
2226
  if settings.chestSelect == 1 then
2227
    insert('Enderchest()')
2228
  elseif settings.chestSelect == 2 then
2229
    insert('NormalChest()')
2230
  end
2231
  insert("mainstep(settings.tunnelspace, "..tostring(first)..")")
2232
  insert("turtle.turnRight()")
2233
  insert('old_orientation = {}')
2234
  insert([[for k,v in ipairs(variables.orientation) do
2235
    old_orientation[k] = v
2236
  end]])
2237
  for i = 1, 4 do
2238
    insert('variables.orientation['..tostring(i)..'] = old_orientation[('..tostring(i)..'+2)%4+1]')
2239
  end
2240
  insert("youJustGotLittUp(settings.length, stepAside)")
2241
  insert([[if variables.level == 1 then
2242
    insert('CompareAll("CompareBack()", "CompareDown()")')
2243
    insert('FreewayDown()')
2244
    insert('CompareAll("CompareBack()", "CompareUp()")')
2245
  else
2246
    insert('CompareAll("CompareBack()", "CompareUp()")')
2247
    insert('FreewayUp()')
2248
    insert('CompareAll("CompareBack()", "CompareDown()")')
2249
  end]])
2250
  insert('face(3)')
2251
  insert('old_orientation = {}')
2252
  insert([[for k,v in ipairs(variables.orientation) do
2253
    old_orientation[k] = v
2254
  end]])
2255
  for i = 1, 4 do
2256
    insert('variables.orientation['..tostring(i)..'] = old_orientation[('..tostring(i)..'+1)%4+1]')
2257
  end
2258
  insert('variables.direction = 1')
2259
  insert("youJustGotLittUp(settings.length, compStep)")
2260
  insert("youJustGotLittUp(settings.length, stepAside, true)")
2261
  insert([[if variables.level == 1 then
2262
    insert('CompareAll("CompareBack()", "CompareDown()")')
2263
    insert('FreewayDown()')
2264
    insert('CompareAll("CompareBack()", "CompareUp()")')
2265
  else
2266
    insert('CompareAll("CompareBack()", "CompareUp()")')
2267
    insert('FreewayUp()')
2268
    insert('CompareAll("CompareBack()", "CompareDown()")')
2269
  end]])
2270
  insert('face(3)')
2271
  insert('old_orientation = {}')
2272
  insert([[for k,v in ipairs(variables.orientation) do
2273
    old_orientation[k] = v
2274
  end]])
2275
  for i = 1, 4 do
2276
    insert('variables.orientation['..tostring(i)..'] = old_orientation[('..tostring(i)..'+1)%4+1]')
2277
  end
2278
  insert('variables.direction = 1')
2279
  insert("youJustGotLittUp(settings.length, compStep, true)")
2280
  insert('turtle.turnLeft()')
2281
  insert('old_orientation = {}')
2282
  insert([[for k,v in ipairs(variables.orientation) do
2283
    old_orientation[k] = v
2284
  end]])
2285
  for i = 1, 4 do
2286
    insert('variables.orientation['..tostring(i)..'] = old_orientation[('..tostring(i)..')%4+1]')
2287
  end
2288
end
2289
2290
function go()
2291
  saveVars("resume/settings", settings)
2292
  variables.startDay = os.day()
2293
  variables.startTime = os.time()
2294
  for Tunnelrepeat = 1, settings.quantity do
2295
    insert("tunnel("..tostring(Tunnelrepeat == 1)..")")
2296
    insert([[variables.CrosswayAmount = variables.CrosswayAmount + 1
2297
    print(lang.status(settings.language, 8, variables.CrosswayAmount, settings.quantity))]])
2298
  end
2299
  insert('print(lang.status(settings.language, 9))')
2300
  insert('turn()')
2301
  insert([[if variables.level == 1 then
2302
    FreewayDown()
2303
  end]])
2304
  for Way_Back = 1, settings.quantity*settings.tunnelspace do
2305
    insert('Freeway()')
2306
  end
2307
  if settings.trash == true then
2308
    insert('trash()')
2309
  end
2310
  if settings.chestSelect == 1 then
2311
    insert("Enderchest(true)")
2312
  elseif settings.chestSelect == 2 then
2313
    insert("NormalChest(true, true)")
2314
  end
2315
  insert([[local endDay = os.day()
2316
  local endTime = os.time()
2317
  variables.stats["time"] = (endDay-variables.startDay)*24.000+(endTime-variables.startTime)]])
2318
end
2319
2320
function Navigation()
2321
  table.insert(timer, os.startTimer(0.05))
2322
  while true do
2323
    local tempState = menustate
2324
    local event, p1, p2, p3 = os.pullEvent()
2325
    while (event == "timer" and p1 ~= timer[#timer]) or event == "key_up" do
2326
      event, p1, p2, p3 = os.pullEvent()
2327
    end
2328
    if menuVars["drawOptions"].focus == 100 then
2329
      basicInputHandler(menu[menustate].orientation, event, p1)
2330
    end
2331
    if menustate == "Quit" then
2332
      break
2333
    elseif menustate == "Save as default" then
2334
      saveVars("settings", settings)
2335
      menuVars["drawOptions"].saved = true
2336
      menustate = "Options"
2337
    elseif menustate == "Go!" then
2338
      menustate = "Popup"
2339
      local ready = true
2340
      for i = 1, #errors do
2341
        if errors[i] == true then
2342
          ready = false
2343
        end
2344
      end
2345
      if (Fuellevel ~= "low" and ready == true) or tempState == "Popup" then
2346
        return execute()
2347
      end
2348
    end
2349
    term.clear()
2350
    drawHeader()
2351
    if tempState == menustate then
2352
      menu[menustate].draw(event, p1, p2, p3)
2353
    else
2354
      menu[menustate].draw()
2355
    end
2356
    if tempState == "Update" then
2357
      save("database/"..programName.."/updateCheckbox/V2.6", checkbox)
2358
    end
2359
    table.insert(timer, os.startTimer(0.5))
2360
  end
2361
end
2362
2363
function extractstats()
2364
  save("database/"..programName.."/variables.stats", variables.stats)
2365
end
2366
2367
function close()
2368
  fs.delete("database/"..programName.."/resume")
2369
  term.clear()
2370
  term.setCursorPos(1,1)
2371
  if Color() then
2372
    term.setTextColor(colors.yellow)
2373
    print(os.version())
2374
    term.setTextColor(colors.white)
2375
  else
2376
    print(os.version())
2377
  end
2378
  term.setCursorPos(1, 2)
2379
end
2380
2381
-----###-----Executive-----###------
2382
2383
local function compile(chunk) -- returns compiled chunk or nil and error message
2384
  if type(chunk) ~= "string" then
2385
    error("expected string, got ".. type(chunk), 2)
2386
  end
2387
2388
  local function findChunkName(var)
2389
    for k,v in pairs(HOST_ENV) do
2390
      if v==var then
2391
        return k
2392
      end
2393
    end
2394
    return "Unknown chunk"
2395
  end
2396
2397
  return load(chunk, findChunkName(chunk), "t", OUR_ENV)
2398
end
2399
2400
function getOrientation(resume)
2401
  if resume then
2402
    for i = 1, 2-variables.level do
2403
      insert('FreewayUp()')
2404
    end
2405
  end
2406
  if #SlotCalculator("T", "check") ~= 0 then
2407
    insert('Freeway()')
2408
    if Version >= 1.64 then
2409
      insert('FreewayUp()')
2410
      for i = 1, 3 do
2411
        insert('right()')
2412
        insert('dig(true)')
2413
      end
2414
      insert('right()')
2415
      insert([[if turtle.detect() == false then
2416
        turtle.select(SlotCalculator("_", "minecraft:cobblestone")[1])
2417
        turtle.place()
2418
      end]])
2419
      insert('FreewayDown()')
2420
      insert('turtle.select(SlotCalculator("T", "check")[1])')
2421
      insert('turtle.placeUp()')
2422
2423
      insert('torch = {"West", "East", "North", "South"}')
2424
      insert('_, data = turtle.inspectUp()')
2425
      insert('new_orientation = torch[data.metadata]')
2426
2427
      insert('digUp()')
2428
      insert('FreewayBack()')
2429
      insert('_, data, torch = nil, nil, nil')
2430
    else
2431
      insert('Freeway()')
2432
      insert('digDown()')
2433
      insert('FreewayUp()')
2434
      for i = 1, 4 do
2435
        insert([[if turtle.detect() == false then
2436
          turtle.select(SlotCalculator("_", "minecraft:cobblestone")[1])
2437
          turtle.place()
2438
        end]])
2439
        insert('right()')
2440
      end
2441
      insert('FreewayDown()')
2442
      insert('turtle.select(SlotCalculator("T", "check")[1])')
2443
      insert('turtle.placeUp()')
2444
2445
      insert('torch = {"West", "South", "East", "North"}')
2446
      for i = 1, 4 do
2447
        insert('Freeway()')
2448
        insert('digUp()')
2449
        insert('os.sleep(0.5)')
2450
        insert('FreewayBack()')
2451
        insert([[if turtle.detectUp() == false then
2452
          i = ]]..tostring(i)..[[
2453
          insert('turtle.select(SlotCalculator("T", "check")[1])')
2454
          insert('turtle.suckDown()')
2455
          insert('new_orientation = torch[i]')
2456
          insert('i = nil')
2457
        end]])
2458
        insert('turtle.turnLeft()')
2459
      end
2460
      insert('face(variables.direction)')
2461
      insert('FreewayBack()')
2462
    end
2463
  end
2464
  if resume then
2465
    for i = 1, 2-variables.level do
2466
      insert('FreewayDown()')
2467
    end
2468
    insert([[for k, v in pairs(cardinal) do
2469
      if v == new_orientation then
2470
        new_orientation = k
2471
      end
2472
      if v == variables.orientation[variables.direction] then
2473
        variables.orientation[variables.direction] = k
2474
      end
2475
    end]])
2476
    insert([[if new_orientation ~= variables.orientation[variables.direction] then
2477
      if math.abs(new_orientation-variables.orientation[variables.direction]) == 2 then
2478
        insert('turtle.turnLeft()')
2479
        insert('turtle.turnLeft()')
2480
      elseif (new_orientation-variables.orientation[variables.direction]+1)%4 == 0 then
2481
        insert('turtle.turnLeft()')
2482
      elseif (new_orientation-variables.orientation[variables.direction]-1)%4 == 0 then
2483
        insert('turtle.turnRight()')
2484
      end
2485
    end]])
2486
    insert('variables.orientation[variables.direction] = cardinal[variables.orientation[variables.direction]]')
2487
  else
2488
    insert([[for k, v in ipairs(cardinal) do
2489
      if new_orientation == v then
2490
        for i = 1, 4 do
2491
          table.insert(variables.orientation, cardinal[(k+i-2)%4+1])
2492
        end
2493
      end
2494
    end]])
2495
  end
2496
end
2497
2498
function execute(resume)
2499
  if not resume then
2500
    todoList = {"go()"}
2501
  end
2502
  table.insert(todoList, 1, 'getOrientation('..tostring(resume)..')')
2503
  while #todoList > 0 do
2504
    compile(todoList[1])()
2505
    table.remove(todoList, 1)
2506
    variables.index = 2
2507
    save("database/"..programName.."/resume/todoList", todoList, true)
2508
    saveVars("resume/variables", variables)
2509
  end
2510
end
2511
2512
function main()
2513
    shell.run("pastebin get cR9vEiTc database/"..programName.."/lang")
2514
  if fs.exists("database/"..programName.."/resume") then
2515
    os.loadAPI("database/"..programName.."/lang")
2516
    local countdown = 10
2517
    local count = {}
2518
    term.clear()
2519
    drawResume(countdown)
2520
    table.insert(count, os.startTimer(1))
2521
    repeat
2522
      local event, p1 = os.pullEvent()
2523
      while (event == "timer" and p1 ~= count[#count]) do
2524
        event, p1 = os.pullEvent()
2525
      end
2526
      if event == "key" then
2527
        if p1 == 28 then
2528
          fs.delete("database/"..programName.."/resume")
2529
          return main()
2530
        end
2531
      elseif event == "timer" and p1 == count[#count] then
2532
        countdown = countdown - 1
2533
      end
2534
      term.clear()
2535
      drawResume(countdown)
2536
      table.insert(count, os.startTimer(1))
2537
    until countdown == 0
2538
    getVariables("resume/settings")
2539
    getVariables("resume/variables")
2540
    getList("resume/todoList")
2541
    execute(true)
2542
  else
2543
    update()
2544
    if kill == true then
2545
      return
2546
    end
2547
    save("database/"..programName.."/state", "0")
2548
        shell.run("pastebin get cR9vEiTc database/"..programName.."/lang")
2549
    os.loadAPI("database/"..programName.."/lang")
2550
    getVariables("settings")
2551
    Navigation()
2552
  end
2553
  --extractstats()
2554
  close()
2555
end
2556
2557
main()