diff --git a/src/lib/turtle/TurtleExecuter.hx b/src/lib/turtle/TurtleExecuter.hx deleted file mode 100644 index 0a94d9b..0000000 --- a/src/lib/turtle/TurtleExecuter.hx +++ /dev/null @@ -1,102 +0,0 @@ -package lib.turtle; - -import lib.Pos; -import lib.Pos3; -import kernel.turtle.Turtle; - -using tink.CoreApi; - -class TurtleExecuter { - private var instructions:Array; - - public function new(instructions:Array) { - this.instructions = instructions; - } - - public function getRequiredFuel():Int { - var fuel = 0; - for (inst in instructions) { - if (inst == Forward || inst == Back || inst == Up || inst == Down) { - fuel++; - } - } - - return fuel; - } - - public function getRequiredBlocks():Int { - var blocks = 0; - for (inst in instructions) { - switch inst { - case Place(_): - blocks++; - default: - } - } - - return blocks; - } - - /** - Return the offset of the turtle after executing the instructions. - The origin is the starting position of the turtle. - **/ - public function getFinalOffset():{offset:Pos3, faceing:Pos} { - var pos:Pos3 = {x: 0, y: 0, z: 0}; - var forwardVec:Pos3 = {x: 1, y: 0, z: 0}; - - for (inst in instructions) { - switch inst { - case Forward: - pos = pos + forwardVec; - case Back: - pos = pos - forwardVec; - case TurnRight: - forwardVec = {x: -forwardVec.z, z: forwardVec.x, y: forwardVec.y}; - case TurnLeft: - forwardVec = {x: forwardVec.z, z: -forwardVec.x, y: forwardVec.y}; - default: - } - } - - return {offset: pos, faceing: new Pos({x: forwardVec.x, y: forwardVec.z})}; - } - - public function execute() { - for (inst in instructions) { - executeInst(inst); - } - } - - private function executeInst(instruction:TurtleInstruction):Outcome { - switch instruction { - case Forward: - return Turtle.forward(); - case Back: - return Turtle.back(); - case Up: - return Turtle.up(); - case Down: - return Turtle.down(); - case TurnLeft: - return Turtle.turnLeft(); - case TurnRight: - return Turtle.turnRight(); - case Dig(dir): - return Turtle.dig(dir); - case Place(dir): - return Turtle.place(dir); - case PlacseSign(dir, text): - return Turtle.placeSign(dir, text); - case Select(slot): - var r = Turtle.selectSlot(slot); - if (r.isSuccess()) { - return Outcome.Success(null); - } else { - return Outcome.Failure("Failed to select slot"); - } - } - - return Outcome.Failure("Unknown instruction: " + instruction); - } -} diff --git a/src/lib/turtle/TurtleExt.hx b/src/lib/turtle/TurtleExt.hx deleted file mode 100644 index 071dc8e..0000000 --- a/src/lib/turtle/TurtleExt.hx +++ /dev/null @@ -1,92 +0,0 @@ -package lib.turtle; - -import kernel.turtle.Turtle; - -using tink.CoreApi; - -/** - Extends the Turtle kernel. -**/ -class TurtleExt { - public static function getFreeSlots(t:Turtle):Int { - var ret:Int = 0; - - for (i in 0...Turtle.MAX_SLOTS) { - if (t.getItemCount(i) == 0) { - ret++; - } - } - - return ret; - } - - public static function canPickup(t:Turtle, item:Item):Bool { - for (i in 0...Turtle.MAX_SLOTS) { - var slotItem = t.getItemDetail(i).orNull(); - if (slotItem != null && slotItem.name == item) { - if (t.getItemSpace(i) > 0) { - return true; - } - } - } - - return false; - } - - public static function canPickupCount(t:Turtle, item:Item):Int { - var ret:Int = 0; - - for (i in 0...Turtle.MAX_SLOTS) { - var slotItem = Turtle.getItemDetail(i).orNull(); - if (slotItem != null && slotItem.name == item) { - ret += Turtle.getItemSpace(i); - } - } - - return ret; - } - - public static function getInventory(t:Turtle):Map { - var rtn:Map = new Map(); - - for (i in 0...Turtle.MAX_SLOTS) { - var slotItem = t.getItemDetail(i).orNull(); - if (slotItem != null) { - rtn.set(i, {item: slotItem.name, count: slotItem.count, free: t.getItemSpace(i)}); - } - } - - return rtn; - } - - /** - Optimize the turtle inventory. - TODO: does not work 100% yet. Can be optimized more i think. Not that it needs to be optimized. - **/ - public static function tidy(t:Turtle) { - var inv = getInventory(t); - // For each item in the inventory - for (k1 => v1 in inv) { - if (v1.free == 0 || v1.count == 0) { - continue; - } - - for (k2 => v2 in inv) { - if (k2 == k1) { - continue; - } - - if (v1.item == v2.item && v2.free > 0) { - // We can move an item in there - t.transfer(k1, k2, v2.free); - - v1.count -= v2.free; - if (v1.count <= 0) { - inv.remove(k1); - break; - } - } - } - } - } -} diff --git a/src/lib/turtle/TurtleInstruction.hx b/src/lib/turtle/TurtleInstruction.hx deleted file mode 100644 index fe028e5..0000000 --- a/src/lib/turtle/TurtleInstruction.hx +++ /dev/null @@ -1,17 +0,0 @@ -package lib.turtle; - -import kernel.turtle.Types.InteractDirections; -import kernel.turtle.Types.TurtleSlot; - -enum TurtleInstruction { - Forward; - Back; - Up; - Down; - TurnLeft; - TurnRight; - Dig(dir:InteractDirections); - Place(dir:InteractDirections); - PlacseSign(dir:InteractDirections, text:String); - Select(slot:TurtleSlot); -} diff --git a/src/lib/turtle/TurtleInstructionParser.hx b/src/lib/turtle/TurtleInstructionParser.hx deleted file mode 100644 index 10e3d1d..0000000 --- a/src/lib/turtle/TurtleInstructionParser.hx +++ /dev/null @@ -1,110 +0,0 @@ -package lib.turtle; - -// Check usage of NativeStringTools -// here https://api.haxe.org/lua/NativeStringTools.html -// and here http://lua-users.org/wiki/StringLibraryTutorial -import kernel.turtle.Types.TurtleSlot; -import kernel.turtle.Turtle; -import lua.NativeStringTools; - -using tink.CoreApi; - -/** - Save a set of turtle instructions to a string and execute them. -**/ -class TurtleInstructionParser { - public function new() {} - - public function encode(instructions:Array):String { - var s = ""; - - var times = 0; - var lastInstruction:TurtleInstruction = null; - - for (inst in instructions) { - if (inst == lastInstruction) { - times++; - continue; - } - - if (lastInstruction != null) { - var encoded = encodeInstruction(lastInstruction); - s = s + '${times + 1}$encoded'; - } - - times = 0; - lastInstruction = inst; - } - - var encoded = encodeInstruction(lastInstruction); - s = s + '${times + 1}$encoded'; - - return s; - } - - private function encodeInstruction(inst:TurtleInstruction):String { - for (k => v in cmdMap) { - if (v == inst) { - return k; - } - } - - switch inst { - case Select(slot): - return "m" + encodeSlot(slot); - default: - return ""; - } - - return ""; - } - - private function encodeSlot(slot:TurtleSlot):String { - return String.fromCharCode(slot + 97); - } - - public function parse(instructionsString:String):Array { - var rtn:Array = []; - var mfunc = NativeStringTools.gmatch(instructionsString, "%d+%D+"); - - while (true) { - var found = mfunc(); - - if (found == null) { - break; - } - - var times = Std.parseInt(NativeStringTools.match(found, "%d+")); - var command = NativeStringTools.match(found, "%D+"); - - var cmd = cmdMap.get(command); - if (cmd != null) { - rtn = rtn.concat([for (i in 0...times) cmd]); - } - - switch command.charAt(0) { - case "m": // select - var slot = parseSlot(command.charAt(1)); - rtn = rtn.concat([for (i in 0...times) Select(slot)]); - } - } - - return rtn; - } - - private static function parseSlot(slot:String):TurtleSlot { - var slot = slot.charCodeAt(0) - 97; - - if (slot < 0 || slot > Turtle.MAX_SLOTS) { - // TODO: better error handling - throw new Error("Invalid slot: " + slot); - } - - return slot; - } - - private var cmdMap:Map = [ - "a" => Forward, "b" => Back, "c" => TurnLeft, "d" => TurnRight, "e" => Up, "f" => Down, "g" => Dig(Front), "h" => Dig(Up), "i" => Dig(Down), - "j" => Place(Front), "k" => Place(Up), "l" => Place(Down), - ]; -}