From b8f5ffb93a2f3de11707040dafa296eb280a358d Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Tue, 1 Aug 2023 13:19:01 +0200 Subject: [PATCH] made Turtle a static class --- src/bin/Turtle.hx | 12 +++--- src/kernel/Init.hx | 4 -- src/kernel/gps/INS.hx | 24 +++++------ src/kernel/turtle/Turtle.hx | 69 ++++++++++++++------------------ src/lib/turtle/TurtleExecuter.hx | 20 ++++----- src/lib/turtle/TurtleExt.hx | 4 +- 6 files changed, 60 insertions(+), 73 deletions(-) diff --git a/src/bin/Turtle.hx b/src/bin/Turtle.hx index ec3109b..c1e46a4 100644 --- a/src/bin/Turtle.hx +++ b/src/bin/Turtle.hx @@ -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()); }); } diff --git a/src/kernel/Init.hx b/src/kernel/Init.hx index 73f462b..c704775 100644 --- a/src/kernel/Init.hx +++ b/src/kernel/Init.hx @@ -28,10 +28,6 @@ class Init { WindowManager.init(); MainTerm.instance = new MainTerm(); - if (Turtle.isTurtle()) { - Turtle.instance = new Turtle(); - } - Routing.init(); Net.init(); diff --git a/src/kernel/gps/INS.hx b/src/kernel/gps/INS.hx index f6a5c80..6b9e1df 100644 --- a/src/kernel/gps/INS.hx +++ b/src/kernel/gps/INS.hx @@ -82,7 +82,7 @@ class INS { public static function align():Promise { Log.info("Aligning INS"); return new Promise((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(); } } diff --git a/src/kernel/turtle/Turtle.hx b/src/kernel/turtle/Turtle.hx index 79a6584..3a97a9c 100644 --- a/src/kernel/turtle/Turtle.hx +++ b/src/kernel/turtle/Turtle.hx @@ -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 { + private static function conterToOutcome(r:cc.Turtle.TurtleActionResult):Outcome { if (r.successful) { return Outcome.Success(null); } else { @@ -33,7 +24,7 @@ class Turtle { } } - public function forward():Outcome { + public static function forward():Outcome { var r = cc.Turtle.forward(); var r2 = conterToOutcome(r); if (r2.isSuccess()) @@ -41,7 +32,7 @@ class Turtle { return r2; } - public function back():Outcome { + public static function back():Outcome { var r = cc.Turtle.back(); var r2 = conterToOutcome(r); if (r2.isSuccess()) @@ -49,7 +40,7 @@ class Turtle { return r2; } - public function up():Outcome { + public static function up():Outcome { var r = cc.Turtle.up(); var r2 = conterToOutcome(r); if (r2.isSuccess()) @@ -57,7 +48,7 @@ class Turtle { return r2; } - public function down():Outcome { + public static function down():Outcome { var r = cc.Turtle.down(); var r2 = conterToOutcome(r); if (r2.isSuccess()) @@ -65,7 +56,7 @@ class Turtle { return r2; } - public function turnLeft():Outcome { + public static function turnLeft():Outcome { var r = cc.Turtle.turnLeft(); var r2 = conterToOutcome(r); if (r2.isSuccess()) @@ -73,7 +64,7 @@ class Turtle { return r2; } - public function turnRight():Outcome { + public static function turnRight():Outcome { 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 { + public static function dig(dir:InteractDirections, ?toolSide:ToolSide):Outcome { 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 { + public static function place(dir:InteractDirections):Outcome { 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 { + public static function placeSign(dir:InteractDirections, text:String):Outcome { 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 { + public static function drop(dir:InteractDirections, ?count:Int):Outcome { var r = cc.Turtle.drop(count); return conterToOutcome(r); } - public function selectSlot(slot:TurtleSlot):Outcome { + public static function selectSlot(slot:TurtleSlot):Outcome { // 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 { + public static function attack(dir:InteractDirections, ?toolSide:ToolSide):Outcome { 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 { + public static function suckItem(dir:InteractDirections, ?ammount:Int):Outcome { // 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 { + public static function refuel(?ammount:Int):Outcome { 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 { - this.selectSlot(from); + public static function transfer(from:TurtleSlot, to:TurtleSlot, ?count:Int):Outcome { + 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 { + public static function equip(side:ToolSide):Outcome { switch side { case Left: return conterToOutcome(cc.Turtle.equipLeft()); @@ -243,7 +234,7 @@ class Turtle { } } - public function inspect(dir:InteractDirections):Outcome { + public static function inspect(dir:InteractDirections):Outcome { var r:cc.Turtle.TurtleInspectResult; switch dir { case Front: @@ -266,7 +257,7 @@ class Turtle { }); } - public function getItemDetail(?detailed:Bool = false, ?slot:TurtleSlot):Option { + public static function getItemDetail(?detailed:Bool = false, ?slot:TurtleSlot):Option { 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 { + public static function craft(?limit:Int = 64):Outcome { if (limit < 1 || limit > 64) { return Outcome.Failure("Crafting limit out of bounds"); } diff --git a/src/lib/turtle/TurtleExecuter.hx b/src/lib/turtle/TurtleExecuter.hx index c0f4012..0a94d9b 100644 --- a/src/lib/turtle/TurtleExecuter.hx +++ b/src/lib/turtle/TurtleExecuter.hx @@ -71,25 +71,25 @@ class TurtleExecuter { private function executeInst(instruction:TurtleInstruction):Outcome { 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 { diff --git a/src/lib/turtle/TurtleExt.hx b/src/lib/turtle/TurtleExt.hx index bc9573a..071dc8e 100644 --- a/src/lib/turtle/TurtleExt.hx +++ b/src/lib/turtle/TurtleExt.hx @@ -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); } }