diff --git a/src/bin/turtle/CircleMine.hx b/src/bin/turtle/CircleMine.hx new file mode 100644 index 0000000..d5616e5 --- /dev/null +++ b/src/bin/turtle/CircleMine.hx @@ -0,0 +1,158 @@ +package bin.turtle; + +import kernel.turtle.Turtle; +import lib.turtle.TurtleAppBase; + +@:build(macros.Binstore.includeBin("Circle Mine", ["circle-mine", "cm"])) +class CircleMine extends TurtleAppBase { + private var rings:Int; + private var skip:Int = 0; + private var depth:Int = 0; + + public function new() { + super(initFunc, turtleFunc); + } + + private function initFunc() { + if (handle.args.length < 1) { + handle.writeLine("Not enough args"); + handle.close(false); + return; + } + + this.rings = Std.parseInt(handle.args[0]); + if (this.rings == null) { + handle.writeLine("Failed to parse args"); + handle.close(false); + return; + } + + if (handle.args.length > 1) { + this.skip = Std.parseInt(handle.args[1]); + if (this.skip == null) { + handle.writeLine("Failed to parse args"); + handle.close(false); + return; + } + } + } + + private function turtleFunc() { + // Go down 1 layer + if (skip == 0) { + Turtle.dig(Down); + Turtle.down(); + Turtle.dig(Down); + } else { + // Move to outer ring + for (i in 0...(skip * 3) - 1) { + Turtle.forward(); + } + } + + // Start mining + for (i in skip...(rings + skip)) { + if (i == 0) { + center(); + handle.writeLine("Center done"); + } else { + circle(i * 3 + 1); + handle.writeLine('Ring $i done'); + } + } + + // Go back to starting pos. + for (i in 0...((rings + skip) * 3 - 1)) { + Turtle.back(); + } + + handle.close(); + } + + private function circle(radius:Int) { + step(); + step(); + frontDig(); + + // Do a 1/2 of a side of a ring + Turtle.turnRight(); + for (i in 0...radius) { + step(); + } + + // Do the other 2 sides + for (s in 0...3) { + Turtle.turnRight(); + for (i in 0...radius) { + step(); + } + + middle(); + + for (i in 0...radius) { + step(); + } + } + + // Complete the 1/2 of the first side + Turtle.turnRight(); + for (i in 0...radius) { + step(); + } + + Turtle.turnLeft(); + Turtle.forward(); + } + + private function middle() { + Turtle.turnRight(); + + Turtle.dig(Front); + Turtle.down(); + Turtle.dig(Front); + + Turtle.turnLeft(); + Turtle.turnLeft(); + + Turtle.dig(Front); + Turtle.up(); + Turtle.dig(Front); + + Turtle.turnRight(); + } + + private function step() { + Turtle.dig(Front); + Turtle.forward(); + Turtle.dig(Down); + } + + private function frontDig() { + Turtle.dig(Front); + Turtle.down(); + Turtle.dig(Front); + Turtle.up(); + } + + private function center() { + Turtle.dig(Down); + step(); + frontDig(); + Turtle.turnRight(); + + for (i in 0...3) { + step(); + Turtle.turnRight(); + step(); + Turtle.turnLeft(); + frontDig(); + Turtle.turnRight(); + } + + step(); + Turtle.turnRight(); + Turtle.forward(); + Turtle.turnLeft(); + Turtle.forward(); + } +}