From a568c9cdd86f49c27376acba11f5fd839026008e Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sat, 5 Aug 2023 11:42:38 +0200 Subject: [PATCH] added example excavate app --- src/bin/turtle/Excavate.hx | 67 +++++++++++++++++++++++++++++++++ src/kernel/binstore/BinStore.hx | 6 ++- src/lib/turtle/Helper.hx | 17 +++++++++ src/lib/turtle/TurtleAppBase.hx | 34 +++++++++++++++++ 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 src/bin/turtle/Excavate.hx create mode 100644 src/lib/turtle/Helper.hx create mode 100644 src/lib/turtle/TurtleAppBase.hx diff --git a/src/bin/turtle/Excavate.hx b/src/bin/turtle/Excavate.hx new file mode 100644 index 0000000..f5a2bf9 --- /dev/null +++ b/src/bin/turtle/Excavate.hx @@ -0,0 +1,67 @@ +package bin.turtle; + +import lib.turtle.Helper; +import lib.turtle.TurtleAppBase; + +class Excavate extends TurtleAppBase { + private var x:Int = 0; + private var y:Int = 0; + private var z:Int = 0; + + public function new() { + super(init, turtleFunction); + } + + private function init() { + if (handle.args.length < 2) { + handle.writeLine("Usage: excavate "); + handle.close(false); + } + + var parsedX = Std.parseInt(handle.args[0]); + var parsedY = Std.parseInt(handle.args[1]); + var parsedZ = Std.parseInt(handle.args[2]); + + if (parsedX == null || parsedY == null || parsedZ == null) { + handle.writeLine("Invalid arguments"); + handle.close(false); + } + + this.x = parsedX; + this.y = parsedY; + this.z = parsedZ; + } + + private function turtleFunction() { + for (z in 0...this.z) { + for (y in 0...this.y) { + for (x in 0...this.x) { + kernel.turtle.Turtle.dig(Down); + + if (x < this.x - 1) { + kernel.turtle.Turtle.forward(); + } + } + + if (y < this.y - 1) { + if (y % 2 == 0) { + kernel.turtle.Turtle.turnRight(); + kernel.turtle.Turtle.forward(); + kernel.turtle.Turtle.turnRight(); + } else { + kernel.turtle.Turtle.turnLeft(); + kernel.turtle.Turtle.forward(); + kernel.turtle.Turtle.turnLeft(); + } + } + } + + kernel.turtle.Turtle.dig(Down); + kernel.turtle.Turtle.down(); + + Helper.times(kernel.turtle.Turtle.turnLeft, 2); + } + + handle.close(true); + } +} diff --git a/src/kernel/binstore/BinStore.hx b/src/kernel/binstore/BinStore.hx index 04ec1a0..4715f37 100644 --- a/src/kernel/binstore/BinStore.hx +++ b/src/kernel/binstore/BinStore.hx @@ -1,5 +1,6 @@ package kernel.binstore; +import bin.turtle.Excavate; import bin.turtle.Patrol; import bin.pathfinder.PFClient; import bin.ID; @@ -31,7 +32,7 @@ class BinStore { {c: Net, name: "Net", aliases: ["net"]}, {c: Redstone, name: "Redstone", aliases: ["redstone", "rs"]}, {c: Terminal, name: "Terminal", aliases: ["terminal", "term"]}, - {c: Turtle, name: "Turtle", aliases: ["turtle"]}, + {c: Turtle, name: "Turtle", aliases: ["turtle", "t"]}, {c: LSPS, name: "PM", aliases: ["lsps"]}, {c: Service, name: "Service", aliases: ["service", "srv"]}, {c: HelloWorldService, name: "HelloWorldService", aliases: ["hello-service"]}, @@ -43,7 +44,8 @@ class BinStore { {c: Res, name: "Res", aliases: ["res"]}, {c: ID, name: "ID", aliases: ["id"]}, {c: PFClient, name: "PFClient", aliases: ["pfclient"]}, - {c: Patrol, name: "Patrol", aliases: ["patrol"]} + {c: Patrol, name: "Patrol", aliases: ["patrol"]}, + {c: Excavate, name: "Excavate", aliases: ["excavate"]} ]; public static function getBinByName(name:String):Null { diff --git a/src/lib/turtle/Helper.hx b/src/lib/turtle/Helper.hx new file mode 100644 index 0000000..320c276 --- /dev/null +++ b/src/lib/turtle/Helper.hx @@ -0,0 +1,17 @@ +package lib.turtle; + +using tink.CoreApi; + +class Helper { + public static function times(func:Void->Outcome, times:Int):Outcome { + for (i in 0...times) { + switch (func()) { + case Success(_): + case Failure(err): + return Failure({step: i, error: err}); + } + } + + return Success(null); + } +} diff --git a/src/lib/turtle/TurtleAppBase.hx b/src/lib/turtle/TurtleAppBase.hx new file mode 100644 index 0000000..3a3a5c7 --- /dev/null +++ b/src/lib/turtle/TurtleAppBase.hx @@ -0,0 +1,34 @@ +package lib.turtle; + +import kernel.turtle.TurtleMutex; +import kernel.ps.ProcessHandle; +import kernel.ps.Process; + +abstract class TurtleAppBase implements Process { + private var handle:ProcessHandle; + + private var _initFunc:Void->Void; + private var _turtleFunc:Void->Void; + + public function new(initFunc:NullVoid>, turtleFunc:Void->Void) { + this._initFunc = initFunc; + this._turtleFunc = turtleFunc; + } + + public final function run(handle:ProcessHandle) { + this.handle = handle; + + if (!handle.claimTurtleMutex()) { + handle.writeLine("Failed to claim turtle mutex"); + handle.close(); + } + + if (this._initFunc != null) { + this._initFunc(); + } + + if (this._turtleFunc != null) { + TurtleMutex.runInTThread(this._turtleFunc); + } + } +}