added example excavate app

This commit is contained in:
Djeeberjr 2023-08-05 11:42:38 +02:00
parent 44772557ba
commit a568c9cdd8
4 changed files with 122 additions and 2 deletions

View File

@ -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 <front> <right> <down>");
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);
}
}

View File

@ -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<Bin> {

17
src/lib/turtle/Helper.hx Normal file
View File

@ -0,0 +1,17 @@
package lib.turtle;
using tink.CoreApi;
class Helper {
public static function times(func:Void->Outcome<Noise, String>, times:Int):Outcome<Noise, {step:Int, error:String}> {
for (i in 0...times) {
switch (func()) {
case Success(_):
case Failure(err):
return Failure({step: i, error: err});
}
}
return Success(null);
}
}

View File

@ -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:Null<Void->Void>, 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);
}
}
}