made Turtle a static class

This commit is contained in:
Djeeberjr 2023-08-01 13:19:01 +02:00
parent 3c59e045de
commit b8f5ffb93a
6 changed files with 60 additions and 73 deletions

View File

@ -7,27 +7,27 @@ using tink.CoreApi;
class Turtle extends CLIAppBase {
public function new() {
registerSyncSubcommand("forward", (args) -> {
return checkAvailable() && perform(kernel.turtle.Turtle.instance.forward());
return checkAvailable() && perform(kernel.turtle.Turtle.forward());
});
registerSyncSubcommand("back", (args) -> {
return checkAvailable() && perform(kernel.turtle.Turtle.instance.back());
return checkAvailable() && perform(kernel.turtle.Turtle.back());
});
registerSyncSubcommand("left", (args) -> {
return checkAvailable() && perform(kernel.turtle.Turtle.instance.turnLeft());
return checkAvailable() && perform(kernel.turtle.Turtle.turnLeft());
});
registerSyncSubcommand("right", (args) -> {
return checkAvailable() && perform(kernel.turtle.Turtle.instance.turnRight());
return checkAvailable() && perform(kernel.turtle.Turtle.turnRight());
});
registerSyncSubcommand("up", (args) -> {
return checkAvailable() && perform(kernel.turtle.Turtle.instance.up());
return checkAvailable() && perform(kernel.turtle.Turtle.up());
});
registerSyncSubcommand("down", (args) -> {
return checkAvailable() && perform(kernel.turtle.Turtle.instance.down());
return checkAvailable() && perform(kernel.turtle.Turtle.down());
});
}

View File

@ -28,10 +28,6 @@ class Init {
WindowManager.init();
MainTerm.instance = new MainTerm();
if (Turtle.isTurtle()) {
Turtle.instance = new Turtle();
}
Routing.init();
Net.init();

View File

@ -82,7 +82,7 @@ class INS {
public static function align():Promise<Noise> {
Log.info("Aligning INS");
return new Promise<Noise>((resolve, reject) -> {
if (Turtle.instance.getFuelLevel() < 2) {
if (Turtle.getFuelLevel() < 2) {
Log.warn("Not enough fuel to align");
reject(new Error("Not enough fuel to align"));
return null;
@ -132,15 +132,15 @@ class INS {
// -1 = not moved, 0 = back, 1 = forward, 2 = left, 3 = right
private static function tryMoving():Int {
if (Turtle.instance.back().isSuccess()) {
if (Turtle.back().isSuccess()) {
return 0;
} else if (Turtle.instance.forward().isSuccess()) {
} else if (Turtle.forward().isSuccess()) {
return 1;
} else {
Turtle.instance.turnLeft(); // TODO: Check if successfull
if (Turtle.instance.forward().isSuccess()) {
Turtle.turnLeft(); // TODO: Check if successfull
if (Turtle.forward().isSuccess()) {
return 2;
} else if (Turtle.instance.back().isSuccess()) {
} else if (Turtle.back().isSuccess()) {
return 3;
} else {
// Can't move
@ -165,20 +165,20 @@ class INS {
private static function moveBack(moved:Int) {
if (moved == 0) {
Turtle.instance.forward();
Turtle.forward();
// cc.Turtle.forward();
} else if (moved == 1) {
Turtle.instance.back();
Turtle.back();
// cc.Turtle.back();
} else if (moved == 2) {
Turtle.instance.back();
Turtle.back();
// cc.Turtle.back();
Turtle.instance.turnRight();
Turtle.turnRight();
// cc.Turtle.turnRight();
} else if (moved == 3) {
Turtle.instance.forward();
Turtle.forward();
// cc.Turtle.forward();
Turtle.instance.turnRight();
Turtle.turnRight();
// cc.Turtle.turnRight();
}
}

View File

@ -1,27 +1,18 @@
package kernel.turtle;
import kernel.log.Log;
import kernel.turtle.Types;
import kernel.gps.INS;
using tink.CoreApi;
class Turtle {
public static var instance:Turtle;
public static final MAX_SLOTS:Int = 16;
@:allow(kernel.Init)
private function new() {
if (!Turtle.isTurtle()) {
Log.warn("Tried to initialize Turtle, but it is not available.");
}
}
public static function isTurtle():Bool {
return cc.Turtle != null;
}
private function conterToOutcome(r:cc.Turtle.TurtleActionResult):Outcome<Noise, String> {
private static function conterToOutcome(r:cc.Turtle.TurtleActionResult):Outcome<Noise, String> {
if (r.successful) {
return Outcome.Success(null);
} else {
@ -33,7 +24,7 @@ class Turtle {
}
}
public function forward():Outcome<Noise, String> {
public static function forward():Outcome<Noise, String> {
var r = cc.Turtle.forward();
var r2 = conterToOutcome(r);
if (r2.isSuccess())
@ -41,7 +32,7 @@ class Turtle {
return r2;
}
public function back():Outcome<Noise, String> {
public static function back():Outcome<Noise, String> {
var r = cc.Turtle.back();
var r2 = conterToOutcome(r);
if (r2.isSuccess())
@ -49,7 +40,7 @@ class Turtle {
return r2;
}
public function up():Outcome<Noise, String> {
public static function up():Outcome<Noise, String> {
var r = cc.Turtle.up();
var r2 = conterToOutcome(r);
if (r2.isSuccess())
@ -57,7 +48,7 @@ class Turtle {
return r2;
}
public function down():Outcome<Noise, String> {
public static function down():Outcome<Noise, String> {
var r = cc.Turtle.down();
var r2 = conterToOutcome(r);
if (r2.isSuccess())
@ -65,7 +56,7 @@ class Turtle {
return r2;
}
public function turnLeft():Outcome<Noise, String> {
public static function turnLeft():Outcome<Noise, String> {
var r = cc.Turtle.turnLeft();
var r2 = conterToOutcome(r);
if (r2.isSuccess())
@ -73,7 +64,7 @@ class Turtle {
return r2;
}
public function turnRight():Outcome<Noise, String> {
public static function turnRight():Outcome<Noise, String> {
var r = cc.Turtle.turnRight();
var r2 = conterToOutcome(r);
if (r2.isSuccess())
@ -81,7 +72,7 @@ class Turtle {
return r2;
}
public function dig(dir:InteractDirections, ?toolSide:ToolSide):Outcome<Noise, String> {
public static function dig(dir:InteractDirections, ?toolSide:ToolSide):Outcome<Noise, String> {
var r:cc.Turtle.TurtleActionResult;
// FIXME: upstream needs to be fixed to accept ToolSide
@ -96,7 +87,7 @@ class Turtle {
return conterToOutcome(r);
}
public function place(dir:InteractDirections):Outcome<Noise, String> {
public static function place(dir:InteractDirections):Outcome<Noise, String> {
var r:cc.Turtle.TurtleActionResult;
switch dir {
case Front:
@ -109,7 +100,7 @@ class Turtle {
return conterToOutcome(r);
}
public function placeSign(dir:InteractDirections, text:String):Outcome<Noise, String> {
public static function placeSign(dir:InteractDirections, text:String):Outcome<Noise, String> {
var r:cc.Turtle.TurtleActionResult;
switch dir {
case Front:
@ -122,12 +113,12 @@ class Turtle {
return conterToOutcome(r);
}
public function drop(dir:InteractDirections, ?count:Int):Outcome<Noise, String> {
public static function drop(dir:InteractDirections, ?count:Int):Outcome<Noise, String> {
var r = cc.Turtle.drop(count);
return conterToOutcome(r);
}
public function selectSlot(slot:TurtleSlot):Outcome<Noise, Noise> {
public static function selectSlot(slot:TurtleSlot):Outcome<Noise, Noise> {
// TODO: slot in bounds?
var r = cc.Turtle.select(slot + 1);
@ -135,19 +126,19 @@ class Turtle {
return (r) ? Outcome.Success(null) : Outcome.Failure("Slot out of bounds");
}
public function getItemCount(?slot:TurtleSlot):Int {
public static function getItemCount(?slot:TurtleSlot):Int {
// TODO: slot in bounds?
return cc.Turtle.getItemCount(slot + 1);
}
public function getItemSpace(?slot:TurtleSlot):Int {
public static function getItemSpace(?slot:TurtleSlot):Int {
// TODO: slot in bounds?
return cc.Turtle.getItemSpace(slot + 1);
}
public function detect(dir:InteractDirections):Bool {
public static function detect(dir:InteractDirections):Bool {
switch dir {
case Front:
return cc.Turtle.detect();
@ -158,7 +149,7 @@ class Turtle {
}
}
public function compareToSlot(dir:InteractDirections):Bool {
public static function compareToSlot(dir:InteractDirections):Bool {
switch dir {
case Front:
return cc.Turtle.compare();
@ -169,7 +160,7 @@ class Turtle {
}
}
public function attack(dir:InteractDirections, ?toolSide:ToolSide):Outcome<Noise, String> {
public static function attack(dir:InteractDirections, ?toolSide:ToolSide):Outcome<Noise, String> {
var r:cc.Turtle.TurtleActionResult;
// FIXEM: upstream needs to be fixed to accept ToolSide
@ -185,7 +176,7 @@ class Turtle {
return conterToOutcome(r);
}
public function suckItem(dir:InteractDirections, ?ammount:Int):Outcome<Noise, String> {
public static function suckItem(dir:InteractDirections, ?ammount:Int):Outcome<Noise, String> {
// TODO: ammount in bounds?
var r:cc.Turtle.TurtleActionResult;
@ -201,40 +192,40 @@ class Turtle {
return conterToOutcome(r);
}
public function getFuelLevel():Int {
public static function getFuelLevel():Int {
var r = cc.Turtle.getFuelLevel(); // FIXME: can be a string. Has to be fixed upstream
return r;
}
public function refuel(?ammount:Int):Outcome<Noise, String> {
public static function refuel(?ammount:Int):Outcome<Noise, String> {
var r = cc.Turtle.refuel(ammount);
return conterToOutcome(r);
}
public function canRefultWithSlot():Bool {
public static function canRefultWithSlot():Bool {
var r = cc.Turtle.refuel(0);
return r.successful;
}
public function compareSlot(otherSlot:TurtleSlot):Bool {
public static function compareSlot(otherSlot:TurtleSlot):Bool {
return cc.Turtle.compareTo(otherSlot + 1);
}
public function transfer(from:TurtleSlot, to:TurtleSlot, ?count:Int):Outcome<Noise, Noise> {
this.selectSlot(from);
public static function transfer(from:TurtleSlot, to:TurtleSlot, ?count:Int):Outcome<Noise, Noise> {
selectSlot(from);
var r = cc.Turtle.transferTo(to + 1, count);
return r ? Outcome.Success(null) : Outcome.Failure(null);
}
public function getSelectedSlot():TurtleSlot {
public static function getSelectedSlot():TurtleSlot {
return cc.Turtle.getSelectedSlot() - 1;
}
public function getFuelLimit():Int {
public static function getFuelLimit():Int {
return cc.Turtle.getFuelLimit(); // FIXME: can be a string. Has to be fixed upstream
}
public function equip(side:ToolSide):Outcome<Noise, String> {
public static function equip(side:ToolSide):Outcome<Noise, String> {
switch side {
case Left:
return conterToOutcome(cc.Turtle.equipLeft());
@ -243,7 +234,7 @@ class Turtle {
}
}
public function inspect(dir:InteractDirections):Outcome<BlockInspect, String> {
public static function inspect(dir:InteractDirections):Outcome<BlockInspect, String> {
var r:cc.Turtle.TurtleInspectResult;
switch dir {
case Front:
@ -266,7 +257,7 @@ class Turtle {
});
}
public function getItemDetail(?detailed:Bool = false, ?slot:TurtleSlot):Option<ItemInspect> {
public static function getItemDetail(?detailed:Bool = false, ?slot:TurtleSlot):Option<ItemInspect> {
var r = cc.Turtle.getItemDetail(slot + 1); // FIXME: can take detailed as flag. Has to be fixed upstream
if (r == null) {
@ -281,7 +272,7 @@ class Turtle {
});
}
public function craft(?limit:Int = 64):Outcome<Noise, String> {
public static function craft(?limit:Int = 64):Outcome<Noise, String> {
if (limit < 1 || limit > 64) {
return Outcome.Failure("Crafting limit out of bounds");
}

View File

@ -71,25 +71,25 @@ class TurtleExecuter {
private function executeInst(instruction:TurtleInstruction):Outcome<Noise, String> {
switch instruction {
case Forward:
return Turtle.instance.forward();
return Turtle.forward();
case Back:
return Turtle.instance.back();
return Turtle.back();
case Up:
return Turtle.instance.up();
return Turtle.up();
case Down:
return Turtle.instance.down();
return Turtle.down();
case TurnLeft:
return Turtle.instance.turnLeft();
return Turtle.turnLeft();
case TurnRight:
return Turtle.instance.turnRight();
return Turtle.turnRight();
case Dig(dir):
return Turtle.instance.dig(dir);
return Turtle.dig(dir);
case Place(dir):
return Turtle.instance.place(dir);
return Turtle.place(dir);
case PlacseSign(dir, text):
return Turtle.instance.placeSign(dir, text);
return Turtle.placeSign(dir, text);
case Select(slot):
var r = Turtle.instance.selectSlot(slot);
var r = Turtle.selectSlot(slot);
if (r.isSuccess()) {
return Outcome.Success(null);
} else {

View File

@ -37,9 +37,9 @@ class TurtleExt {
var ret:Int = 0;
for (i in 0...Turtle.MAX_SLOTS) {
var slotItem = Turtle.instance.getItemDetail(i).orNull();
var slotItem = Turtle.getItemDetail(i).orNull();
if (slotItem != null && slotItem.name == item) {
ret += Turtle.instance.getItemSpace(i);
ret += Turtle.getItemSpace(i);
}
}