From e5b990ae612626fe090c4064c8f85a907002d6f5 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Fri, 3 May 2024 22:45:26 +0200 Subject: [PATCH] added CircleMine bin --- src/bin/turtle/CircleMine.hx | 158 +++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 src/bin/turtle/CircleMine.hx 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(); + } +}