BIG FORMATING COMMIT
This commit is contained in:
@@ -7,16 +7,15 @@ import kernel.turtle.Turtle;
|
||||
using tink.CoreApi;
|
||||
|
||||
class TurtleExecuter {
|
||||
|
||||
private var instructions:Array<TurtleInstruction>;
|
||||
|
||||
public function new(instructions:Array<TurtleInstruction>) {
|
||||
this.instructions = instructions;
|
||||
}
|
||||
|
||||
public function getRequiredFuel(): Int {
|
||||
public function getRequiredFuel():Int {
|
||||
var fuel = 0;
|
||||
for(inst in instructions){
|
||||
for (inst in instructions) {
|
||||
if (inst == Forward || inst == Back || inst == Up || inst == Down) {
|
||||
fuel++;
|
||||
}
|
||||
@@ -25,9 +24,9 @@ class TurtleExecuter {
|
||||
return fuel;
|
||||
}
|
||||
|
||||
public function getRequiredBlocks(): Int {
|
||||
public function getRequiredBlocks():Int {
|
||||
var blocks = 0;
|
||||
for(inst in instructions){
|
||||
for (inst in instructions) {
|
||||
switch inst {
|
||||
case Place(_):
|
||||
blocks++;
|
||||
@@ -42,11 +41,11 @@ class TurtleExecuter {
|
||||
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};
|
||||
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){
|
||||
for (inst in instructions) {
|
||||
switch inst {
|
||||
case Forward:
|
||||
pos = pos + forwardVec;
|
||||
@@ -55,21 +54,21 @@ class TurtleExecuter {
|
||||
case TurnRight:
|
||||
forwardVec = {x: -forwardVec.z, z: forwardVec.x, y: forwardVec.y};
|
||||
case TurnLeft:
|
||||
forwardVec = {x: forwardVec.z, z: -forwardVec.x , y: forwardVec.y};
|
||||
forwardVec = {x: forwardVec.z, z: -forwardVec.x, y: forwardVec.y};
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
return {offset:pos,faceing: new Pos({x:forwardVec.x, y:forwardVec.z})};
|
||||
return {offset: pos, faceing: new Pos({x: forwardVec.x, y: forwardVec.z})};
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
for (inst in instructions){
|
||||
for (inst in instructions) {
|
||||
executeInst(inst);
|
||||
}
|
||||
}
|
||||
|
||||
private function executeInst(instruction: TurtleInstruction): Outcome<Noise,String> {
|
||||
private function executeInst(instruction:TurtleInstruction):Outcome<Noise, String> {
|
||||
switch instruction {
|
||||
case Forward:
|
||||
return Turtle.instance.forward();
|
||||
|
||||
@@ -8,11 +8,11 @@ using tink.CoreApi;
|
||||
Extends the Turtle kernel.
|
||||
**/
|
||||
class TurtleExt {
|
||||
public static function getFreeSlots(t: Turtle): Int {
|
||||
var ret: Int = 0;
|
||||
public static function getFreeSlots(t:Turtle):Int {
|
||||
var ret:Int = 0;
|
||||
|
||||
for (i in 0...Turtle.MAX_SLOTS){
|
||||
if (t.getItemCount(i) == 0){
|
||||
for (i in 0...Turtle.MAX_SLOTS) {
|
||||
if (t.getItemCount(i) == 0) {
|
||||
ret++;
|
||||
}
|
||||
}
|
||||
@@ -20,11 +20,11 @@ class TurtleExt {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static function canPickup(t: Turtle,item: Item): Bool {
|
||||
for (i in 0...Turtle.MAX_SLOTS){
|
||||
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){
|
||||
if (slotItem != null && slotItem.name == item) {
|
||||
if (t.getItemSpace(i) > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -33,12 +33,12 @@ class TurtleExt {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function canPickupCount(t: Turtle,item: Item): Int {
|
||||
var ret: Int = 0;
|
||||
public static function canPickupCount(t:Turtle, item:Item):Int {
|
||||
var ret:Int = 0;
|
||||
|
||||
for (i in 0...Turtle.MAX_SLOTS){
|
||||
for (i in 0...Turtle.MAX_SLOTS) {
|
||||
var slotItem = Turtle.instance.getItemDetail(i).orNull();
|
||||
if (slotItem != null && slotItem.name == item){
|
||||
if (slotItem != null && slotItem.name == item) {
|
||||
ret += Turtle.instance.getItemSpace(i);
|
||||
}
|
||||
}
|
||||
@@ -46,12 +46,12 @@ class TurtleExt {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static function getInventory(t: Turtle): Map<Int,{item: Item, count: Int, free: Int}> {
|
||||
var rtn: Map<Int,{item: Item, count: Int, free: Int}> = new Map();
|
||||
public static function getInventory(t:Turtle):Map<Int, {item:Item, count:Int, free:Int}> {
|
||||
var rtn:Map<Int, {item:Item, count:Int, free:Int}> = new Map();
|
||||
|
||||
for (i in 0...Turtle.MAX_SLOTS){
|
||||
for (i in 0...Turtle.MAX_SLOTS) {
|
||||
var slotItem = t.getItemDetail(i).orNull();
|
||||
if (slotItem != null){
|
||||
if (slotItem != null) {
|
||||
rtn.set(i, {item: slotItem.name, count: slotItem.count, free: t.getItemSpace(i)});
|
||||
}
|
||||
}
|
||||
@@ -63,36 +63,30 @@ class TurtleExt {
|
||||
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) {
|
||||
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){
|
||||
for (k1 => v1 in inv) {
|
||||
if (v1.free == 0 || v1.count == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for(k2 => v2 in inv){
|
||||
|
||||
if (k2 == k1){
|
||||
for (k2 => v2 in inv) {
|
||||
if (k2 == k1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (v1.item == v2.item && v2.free > 0){
|
||||
if (v1.item == v2.item && v2.free > 0) {
|
||||
// We can move an item in there
|
||||
t.transfer(k1,k2,v2.free);
|
||||
t.transfer(k1, k2, v2.free);
|
||||
|
||||
v1.count -= v2.free;
|
||||
if (v1.count <= 0){
|
||||
if (v1.count <= 0) {
|
||||
inv.remove(k1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ enum TurtleInstruction {
|
||||
Down;
|
||||
TurnLeft;
|
||||
TurnRight;
|
||||
Dig(dir: InteractDirections);
|
||||
Place(dir: InteractDirections);
|
||||
PlacseSign(dir: InteractDirections, text: String);
|
||||
Select(slot: TurtleSlot);
|
||||
Dig(dir:InteractDirections);
|
||||
Place(dir:InteractDirections);
|
||||
PlacseSign(dir:InteractDirections, text:String);
|
||||
Select(slot:TurtleSlot);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ 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;
|
||||
@@ -14,24 +13,21 @@ using tink.CoreApi;
|
||||
Save a set of turtle instructions to a string and execute them.
|
||||
**/
|
||||
class TurtleInstructionParser {
|
||||
public function new() {}
|
||||
|
||||
public function new() {
|
||||
|
||||
}
|
||||
|
||||
public function encode(instructions: Array<TurtleInstruction>): String {
|
||||
public function encode(instructions:Array<TurtleInstruction>):String {
|
||||
var s = "";
|
||||
|
||||
var times = 0;
|
||||
var lastInstruction: TurtleInstruction = null;
|
||||
var lastInstruction:TurtleInstruction = null;
|
||||
|
||||
for (inst in instructions){
|
||||
if (inst == lastInstruction){
|
||||
for (inst in instructions) {
|
||||
if (inst == lastInstruction) {
|
||||
times++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lastInstruction != null){
|
||||
if (lastInstruction != null) {
|
||||
var encoded = encodeInstruction(lastInstruction);
|
||||
s = s + '${times + 1}$encoded';
|
||||
}
|
||||
@@ -46,9 +42,9 @@ class TurtleInstructionParser {
|
||||
return s;
|
||||
}
|
||||
|
||||
private function encodeInstruction(inst: TurtleInstruction): String {
|
||||
for (k => v in cmdMap){
|
||||
if (v == inst){
|
||||
private function encodeInstruction(inst:TurtleInstruction):String {
|
||||
for (k => v in cmdMap) {
|
||||
if (v == inst) {
|
||||
return k;
|
||||
}
|
||||
}
|
||||
@@ -63,15 +59,15 @@ class TurtleInstructionParser {
|
||||
return "";
|
||||
}
|
||||
|
||||
private function encodeSlot(slot: TurtleSlot): String {
|
||||
private function encodeSlot(slot:TurtleSlot):String {
|
||||
return String.fromCharCode(slot + 97);
|
||||
}
|
||||
|
||||
public function parse(instructionsString: String): Array<TurtleInstruction> {
|
||||
var rtn: Array<TurtleInstruction> = [];
|
||||
var mfunc = NativeStringTools.gmatch(instructionsString,"%d+%D+");
|
||||
public function parse(instructionsString:String):Array<TurtleInstruction> {
|
||||
var rtn:Array<TurtleInstruction> = [];
|
||||
var mfunc = NativeStringTools.gmatch(instructionsString, "%d+%D+");
|
||||
|
||||
while(true){
|
||||
while (true) {
|
||||
var found = mfunc();
|
||||
|
||||
if (found == null) {
|
||||
@@ -82,7 +78,7 @@ class TurtleInstructionParser {
|
||||
var command = NativeStringTools.match(found, "%D+");
|
||||
|
||||
var cmd = cmdMap.get(command);
|
||||
if (cmd != null){
|
||||
if (cmd != null) {
|
||||
rtn = rtn.concat([for (i in 0...times) cmd]);
|
||||
}
|
||||
|
||||
@@ -96,7 +92,7 @@ class TurtleInstructionParser {
|
||||
return rtn;
|
||||
}
|
||||
|
||||
private static function parseSlot(slot: String): TurtleSlot {
|
||||
private static function parseSlot(slot:String):TurtleSlot {
|
||||
var slot = slot.charCodeAt(0) - 97;
|
||||
|
||||
if (slot < 0 || slot > Turtle.MAX_SLOTS) {
|
||||
@@ -107,19 +103,8 @@ class TurtleInstructionParser {
|
||||
return slot;
|
||||
}
|
||||
|
||||
private var cmdMap:Map<String,TurtleInstruction> = [
|
||||
"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),
|
||||
private var cmdMap:Map<String, TurtleInstruction> = [
|
||||
"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),
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user