Compare commits

...

3 Commits

Author SHA1 Message Date
90b4015ba1 get chunk from position 2024-10-20 22:20:33 +02:00
2dd85c2b26 added Tags class for list of classes 2024-10-20 22:18:23 +02:00
1d60e13792 idfk 2024-10-18 16:48:38 +02:00
4 changed files with 107 additions and 48 deletions

View File

@@ -1,7 +1,6 @@
package kernel.gps; package kernel.gps;
import lib.SinglePromise; import lib.SinglePromise;
import kernel.log.Log;
import kernel.turtle.Turtle; import kernel.turtle.Turtle;
import lib.WorldPos; import lib.WorldPos;
@@ -71,6 +70,9 @@ class INS {
} }
private static function move(dir:Null<WorldPos>) { private static function move(dir:Null<WorldPos>) {
if (alignPromise.isRunning()) {
return;
}
var pos = GPS.getPosition(); var pos = GPS.getPosition();
if (pos == null || dir == null) if (pos == null || dir == null)
return; return;
@@ -86,53 +88,45 @@ class INS {
return alignPromise.request(); return alignPromise.request();
} }
public static function startAlign():Promise<Noise> { public static function startAlign():Void {
return new Promise<Noise>((resolve, reject) -> { if (Turtle.getFuelLevel() < 2) {
if (Turtle.getFuelLevel() < 2) { alignPromise.reject(new Error("Not enough fuel to align"));
Log.warn("Not enough fuel to align"); return;
reject(new Error("Not enough fuel to align")); }
return null;
}
GPS.locate().handle((result) -> { GPS.locate().handle((result) -> {
switch result { switch result {
case Failure(err): case Failure(err):
Log.warn("GPS not available for 1st position"); alignPromise.reject(new Error("Failed to locate 1st positon: " + err));
reject(new Error("GPS not available")); return;
case Success(pos1):
var moved = tryMoving();
if (moved == -1) {
alignPromise.reject(new Error("Can't move"));
return; return;
case Success(pos1): }
var moved = tryMoving();
if (moved == -1) { GPS.locate().handle((result2) -> {
Log.warn("Can't move"); switch result2 {
reject(new Error("Can't move")); case Failure(err):
return; alignPromise.reject(new Error("GPS not available for 2nd position: " + err));
} return;
case Success(pos2):
GPS.locate().handle((result) -> { var cHeading = calcHeading(pos1, pos2, moved);
switch result { if (cHeading == null) {
case Failure(err): alignPromise.reject(new Error("Can't calculate heading"));
Log.warn("GPS not available for 2nd position");
reject(new Error("GPS not available for 2nd position"));
return; return;
case Success(pos2): }
var cHeading = calcHeading(pos1, pos2, moved);
if (cHeading == null) {
Log.error("Can't calculate heading");
reject(new Error("Can't calculate heading"));
return;
}
heading = cHeading; heading = cHeading;
moveBack(moved); moveBack(moved);
GPS.setINSPosition(pos1); GPS.setINSPosition(pos1);
resolve(Noise); alignPromise.resolve(Noise);
} }
}); });
} }
});
return null;
}); });
} }
@@ -172,20 +166,14 @@ class INS {
private static function moveBack(moved:Int) { private static function moveBack(moved:Int) {
if (moved == 0) { if (moved == 0) {
Turtle.forward(); Turtle.forward();
// cc.Turtle.forward();
} else if (moved == 1) { } else if (moved == 1) {
Turtle.back(); Turtle.back();
// cc.Turtle.back();
} else if (moved == 2) { } else if (moved == 2) {
Turtle.back(); Turtle.back();
// cc.Turtle.back();
Turtle.turnRight(); Turtle.turnRight();
// cc.Turtle.turnRight();
} else if (moved == 3) { } else if (moved == 3) {
Turtle.forward(); Turtle.forward();
// cc.Turtle.forward();
Turtle.turnRight(); Turtle.turnRight();
// cc.Turtle.turnRight();
} }
} }

View File

@@ -1,5 +1,6 @@
package lib; package lib;
import lib.Vec.Vec2;
import lib.Vec.Vec3; import lib.Vec.Vec3;
/** /**
@@ -51,6 +52,16 @@ abstract BlockPos(Vec3<Int>) from Vec3<Int> to Vec3<Int> {
return 'BlockPos(${this.x}, ${this.y}, ${this.z})'; return 'BlockPos(${this.x}, ${this.y}, ${this.z})';
} }
/**
Returns the chunk the position is in.
**/
public function chunk():Vec2<Int> {
var x = Math.floor(this.x / 16);
var z = Math.floor(this.z / 16);
return new Vec2(x, z);
}
/** /**
Returns a list of positions neighboring the block. No Diagonal. Returns a list of positions neighboring the block. No Diagonal.
**/ **/

49
src/lib/Tags.hx Normal file
View File

@@ -0,0 +1,49 @@
package lib;
import lib.PaN;
using Lambda;
using lua.Table;
/**
Represents a list a minecraft tags.
Needed because tags are SOMETIMES retuned not as arrays.
**/
@:forward
enum abstract Tags(Array<PaN>) from Array<PaN> to Array<PaN> {
inline public function new(arr:Array<String>) {
this = arr;
}
/**
From values that are object with the tag as the filed and the value set to true.
**/
@:from
public static function fromBoolTable(from:Table<String, Bool>) {
var rtn = [];
for (k => _ in Table.toMap(from)) {
rtn.push(k);
}
return new Tags(rtn);
}
/**
From values that are lua arrays.
**/
@:from
public static function fromIntTable(from:Table<Int, String>) {
return new Tags(from.toArray());
}
public function sortByName():Tags {
var copy = this.copy();
copy.sort((a:String, b:String) -> {
return if (a < b) -1 else 1;
});
return copy;
}
}

View File

@@ -1,5 +1,6 @@
package lib; package lib;
import lib.Vec.Vec2;
import lib.Vec.Vec3; import lib.Vec.Vec3;
/** /**
@@ -65,6 +66,16 @@ abstract WorldPos(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
return 'WorldPos(${this.x}, ${this.y}, ${this.z})'; return 'WorldPos(${this.x}, ${this.y}, ${this.z})';
} }
/**
Returns the chunk the position is in.
**/
public function chunk():Vec2<Int> {
var x = Math.floor(this.x / 16);
var z = Math.floor(this.z / 16);
return new Vec2(x, z);
}
public function round():WorldPos { public function round():WorldPos {
return new WorldPos({ return new WorldPos({
x: Math.fround(this.x), x: Math.fround(this.x),