Compare commits
12 Commits
b305594ea4
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
c665401fd8
|
|||
|
2799a0be3d
|
|||
|
e527dd5b6a
|
|||
|
fe17b4fd67
|
|||
|
90b4015ba1
|
|||
|
2dd85c2b26
|
|||
|
1d60e13792
|
|||
|
9d6e8a366b
|
|||
|
5167533c6d
|
|||
|
aac527ae89
|
|||
|
af6a4c840b
|
|||
|
afbd1dfd68
|
@@ -51,11 +51,12 @@ class GPS extends CLIAppBase {
|
|||||||
});
|
});
|
||||||
|
|
||||||
registerAsyncSubcommand("locate", (args) -> {
|
registerAsyncSubcommand("locate", (args) -> {
|
||||||
return kernel.gps.GPS.locate().map((pos) -> {
|
return kernel.gps.GPS.locate().map((result) -> {
|
||||||
if (pos != null) {
|
switch result {
|
||||||
handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}');
|
case Success(pos):
|
||||||
} else {
|
handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}');
|
||||||
handle.writeLine("Position not available");
|
case Failure(err):
|
||||||
|
handle.writeLine("Position not available: " + err);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package bin;
|
package bin;
|
||||||
|
|
||||||
|
import kernel.turtle.Types.ToolSide;
|
||||||
import lib.turtle.Helper;
|
import lib.turtle.Helper;
|
||||||
import kernel.turtle.TurtleMutex;
|
import kernel.turtle.TurtleMutex;
|
||||||
import kernel.turtle.Turtle;
|
import kernel.turtle.Turtle;
|
||||||
@@ -116,6 +117,28 @@ class TurtleCtl extends CLIAppBase {
|
|||||||
var len = args.getInt("length");
|
var len = args.getInt("length");
|
||||||
return asynPerform(Helper.combine.bind([Turtle.digEmpty.bind(Front), Turtle.forward, Turtle.digEmpty.bind(Up)]), len);
|
return asynPerform(Helper.combine.bind([Turtle.digEmpty.bind(Front), Turtle.forward, Turtle.digEmpty.bind(Up)]), len);
|
||||||
}, [Int("length")]);
|
}, [Int("length")]);
|
||||||
|
|
||||||
|
registerSyncSubcommand("inspect", (args) -> {
|
||||||
|
var res = Turtle.inspect(Front);
|
||||||
|
|
||||||
|
switch res {
|
||||||
|
case Failure(err):
|
||||||
|
handle.writeLine("Failed: " + err);
|
||||||
|
return false;
|
||||||
|
case Success(data):
|
||||||
|
handle.writeLine('Name: ${data.name}');
|
||||||
|
handle.writeLine("Tags:");
|
||||||
|
|
||||||
|
for (tag in data.tags.sortByName()) {
|
||||||
|
handle.writeLine(' ${tag}');
|
||||||
|
}
|
||||||
|
|
||||||
|
handle.writeLine("State:");
|
||||||
|
handle.writeLine(data.state);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function asynPerform(op:Void->Outcome<Noise, String>, times:Int):Future<Bool> {
|
private function asynPerform(op:Void->Outcome<Noise, String>, times:Int):Future<Bool> {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package kernel.gps;
|
package kernel.gps;
|
||||||
|
|
||||||
|
import lib.SingleTimeoutPromise;
|
||||||
import kernel.log.Log;
|
import kernel.log.Log;
|
||||||
import lib.KVStore;
|
import lib.KVStore;
|
||||||
import kernel.net.Net;
|
import kernel.net.Net;
|
||||||
@@ -15,13 +16,15 @@ using tink.CoreApi;
|
|||||||
You need at least 3 computers that know their position to determine the position of the computer.
|
You need at least 3 computers that know their position to determine the position of the computer.
|
||||||
**/
|
**/
|
||||||
class GPS {
|
class GPS {
|
||||||
|
private static inline final TIMEOUT:Int = 1;
|
||||||
|
|
||||||
private static var shouldRespond = true;
|
private static var shouldRespond = true;
|
||||||
private static var shouldDoWholeNumberCheck = true;
|
private static var shouldDoWholeNumberCheck = true;
|
||||||
private static var posAccuracy = 0; // 0 = unkown, 1 = (ins,best guess), 2 = (stored/manual,should be right), 3 = (gps,confirmed)
|
private static var posAccuracy = 0; // 0 = unkown, 1 = (ins,best guess), 2 = (stored/manual,should be right), 3 = (gps,confirmed)
|
||||||
private static var cachedPosition:WorldPos;
|
private static var cachedPosition:WorldPos;
|
||||||
private static var lastPositionResponse:Array<{pos:WorldPos, dist:Float}> = [];
|
private static var lastPositionResponse:Array<{pos:WorldPos, dist:Float}> = [];
|
||||||
|
|
||||||
private static var futureResolve:(pos:Null<WorldPos>) -> Void = null;
|
private static final locatePromise:SingleTimeoutPromise<WorldPos> = new SingleTimeoutPromise(TIMEOUT, brodcastPositionRequest);
|
||||||
|
|
||||||
@:allow(kernel.Init)
|
@:allow(kernel.Init)
|
||||||
private static function init() {
|
private static function init() {
|
||||||
@@ -58,18 +61,8 @@ class GPS {
|
|||||||
posAccuracy = 0;
|
posAccuracy = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function locate():Future<Null<WorldPos>> {
|
public static function locate():Promise<WorldPos> {
|
||||||
// TODO: implenet a timeout
|
return locatePromise.request();
|
||||||
// TODO: dont send a request twice if the last one is still pending or we moved
|
|
||||||
return new Future<Null<WorldPos>>((resolve) -> {
|
|
||||||
futureResolve = resolve;
|
|
||||||
sendPositionRequest();
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function resolveFuture(pos:Null<WorldPos>) {
|
|
||||||
futureResolve(pos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function persistCachedPositon() {
|
private static function persistCachedPositon() {
|
||||||
@@ -117,12 +110,17 @@ class GPS {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function sendPositionRequest() {
|
private static function brodcastPositionRequest() {
|
||||||
Net.brodcastGPSRequest();
|
Net.brodcastGPSRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
@:allow(kernel.net.Net)
|
@:allow(kernel.net.Net)
|
||||||
private static function handlePackage(pack:Package<Noise>, dist:Float, iface:INetworkInterface) {
|
private static function handlePackage(pack:Package<Noise>, dist:Null<Float>, iface:INetworkInterface) {
|
||||||
|
if (dist == null) {
|
||||||
|
// Message comes from another dimension and has no distance.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
switch (pack.type) {
|
switch (pack.type) {
|
||||||
case GPSRequest:
|
case GPSRequest:
|
||||||
if (!shouldRespond)
|
if (!shouldRespond)
|
||||||
@@ -132,13 +130,13 @@ class GPS {
|
|||||||
if (cachedPosition == null)
|
if (cachedPosition == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var response = new Package(Net.networkID, pack.fromID, pack.msgID, GPSResponse(cachedPosition), null, 0);
|
var response = new Package(Net.networkID, pack.fromID, pack.msgID, GPSResponse(cachedPosition.x, cachedPosition.y, cachedPosition.z,), null, 0);
|
||||||
iface.send(pack.fromID, Net.networkID, response);
|
iface.send(pack.fromID, Net.networkID, response);
|
||||||
case GPSResponse(pos):
|
case GPSResponse(x, y, z):
|
||||||
if (lastPositionResponse.contains({pos: pos, dist: dist}))
|
if (lastPositionResponse.contains({pos: {x: x, y: y, z: z}, dist: dist}))
|
||||||
return; // Ignore duplicate responses
|
return; // Ignore duplicate responses
|
||||||
|
|
||||||
lastPositionResponse.push({pos: pos, dist: dist});
|
lastPositionResponse.push({pos: {x: x, y: y, z: z}, dist: dist});
|
||||||
|
|
||||||
// TODO: wait for a few seconds before calculating the position, so we can get more responses
|
// TODO: wait for a few seconds before calculating the position, so we can get more responses
|
||||||
|
|
||||||
@@ -158,7 +156,7 @@ class GPS {
|
|||||||
cachedPosition = calculatedPosition;
|
cachedPosition = calculatedPosition;
|
||||||
posAccuracy = 3;
|
posAccuracy = 3;
|
||||||
|
|
||||||
resolveFuture(calculatedPosition);
|
locatePromise.resolve(calculatedPosition);
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -228,7 +226,7 @@ class GPS {
|
|||||||
var i = ex.dot(a2c);
|
var i = ex.dot(a2c);
|
||||||
var ey = (a2c - ex * i).normalize();
|
var ey = (a2c - ex * i).normalize();
|
||||||
var j = ey.dot(a2c);
|
var j = ey.dot(a2c);
|
||||||
var ez = ex.cross(ey);
|
var ez:WorldPos = ex.cross(ey);
|
||||||
|
|
||||||
var x = (r1 * r1 - r2 * r2 + d * d) / (2 * d);
|
var x = (r1 * r1 - r2 * r2 + d * d) / (2 * d);
|
||||||
var y = (r1 * r1 - r3 * r3 - x * x + (x - i) * (x - i) + j * j) / (2 * j);
|
var y = (r1 * r1 - r3 * r3 - x * x + (x - i) * (x - i) + j * j) / (2 * j);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package kernel.gps;
|
package kernel.gps;
|
||||||
|
|
||||||
import kernel.log.Log;
|
import lib.SinglePromise;
|
||||||
import kernel.turtle.Turtle;
|
import kernel.turtle.Turtle;
|
||||||
import lib.WorldPos;
|
import lib.WorldPos;
|
||||||
|
|
||||||
@@ -9,6 +9,7 @@ using tink.CoreApi;
|
|||||||
class INS {
|
class INS {
|
||||||
private static var heading:Null<WorldPos> = null;
|
private static var heading:Null<WorldPos> = null;
|
||||||
private static var alingment:Int = 1; // 0 = degraded, 1 = not aligned, 2 = aligned
|
private static var alingment:Int = 1; // 0 = degraded, 1 = not aligned, 2 = aligned
|
||||||
|
private static final alignPromise:SinglePromise<Noise> = new SinglePromise(startAlign);
|
||||||
|
|
||||||
@:allow(kernel.turtle.Turtle)
|
@:allow(kernel.turtle.Turtle)
|
||||||
private static function moveForward() {
|
private static function moveForward() {
|
||||||
@@ -69,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;
|
||||||
@@ -81,53 +85,48 @@ class INS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function align():Promise<Noise> {
|
public static function align():Promise<Noise> {
|
||||||
Log.info("Aligning INS");
|
return alignPromise.request();
|
||||||
return new Promise<Noise>((resolve, reject) -> {
|
}
|
||||||
if (Turtle.getFuelLevel() < 2) {
|
|
||||||
Log.warn("Not enough fuel to align");
|
public static function startAlign():Void {
|
||||||
reject(new Error("Not enough fuel to align"));
|
if (Turtle.getFuelLevel() < 2) {
|
||||||
return null;
|
alignPromise.reject(new Error("Not enough fuel to align"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
GPS.locate().handle((result) -> {
|
||||||
|
switch result {
|
||||||
|
case Failure(err):
|
||||||
|
alignPromise.reject(new Error("Failed to locate 1st positon: " + err));
|
||||||
|
return;
|
||||||
|
case Success(pos1):
|
||||||
|
var moved = tryMoving();
|
||||||
|
|
||||||
|
if (moved == -1) {
|
||||||
|
alignPromise.reject(new Error("Can't move"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
GPS.locate().handle((result2) -> {
|
||||||
|
switch result2 {
|
||||||
|
case Failure(err):
|
||||||
|
alignPromise.reject(new Error("GPS not available for 2nd position: " + err));
|
||||||
|
return;
|
||||||
|
case Success(pos2):
|
||||||
|
var cHeading = calcHeading(pos1, pos2, moved);
|
||||||
|
if (cHeading == null) {
|
||||||
|
alignPromise.reject(new Error("Can't calculate heading"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
heading = cHeading;
|
||||||
|
moveBack(moved);
|
||||||
|
GPS.setINSPosition(pos1);
|
||||||
|
|
||||||
|
alignPromise.resolve(Noise);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
GPS.locate().handle((pos1) -> {
|
|
||||||
Log.debug('pos1: $pos1');
|
|
||||||
if (pos1 == null) {
|
|
||||||
Log.warn("GPS not available for 1st position");
|
|
||||||
reject(new Error("GPS not available"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var moved = tryMoving();
|
|
||||||
|
|
||||||
if (moved == -1) {
|
|
||||||
Log.warn("Can't move");
|
|
||||||
reject(new Error("Can't move"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
GPS.locate().handle((pos2) -> {
|
|
||||||
Log.debug('pos2: $pos2');
|
|
||||||
if (pos2 == null) {
|
|
||||||
Log.warn("GPS not available for 2nd position");
|
|
||||||
reject(new Error("GPS not available for 2nd position"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
moveBack(moved);
|
|
||||||
GPS.setINSPosition(pos1);
|
|
||||||
|
|
||||||
resolve(Noise);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return null;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package kernel.net;
|
package kernel.net;
|
||||||
|
|
||||||
import lib.WorldPos;
|
|
||||||
|
|
||||||
typedef NetworkID = Int;
|
typedef NetworkID = Int;
|
||||||
typedef GenericPackage = Package<Dynamic>;
|
typedef GenericPackage = Package<Dynamic>;
|
||||||
|
|
||||||
@@ -12,7 +10,7 @@ enum PackageTypes {
|
|||||||
RouteDiscover(reachableIDs:Array<{id:NetworkID, cost:Int}>);
|
RouteDiscover(reachableIDs:Array<{id:NetworkID, cost:Int}>);
|
||||||
RouteDiscoverResponse(reachableIDs:Array<{id:NetworkID, cost:Int}>);
|
RouteDiscoverResponse(reachableIDs:Array<{id:NetworkID, cost:Int}>);
|
||||||
RouteDiscoverUpdate(reachableIDs:Array<{id:NetworkID, cost:Int}>);
|
RouteDiscoverUpdate(reachableIDs:Array<{id:NetworkID, cost:Int}>);
|
||||||
GPSResponse(pos:WorldPos);
|
GPSResponse(x:Float, y:Float, z:Float);
|
||||||
GPSRequest();
|
GPSRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
76
src/kernel/peripherals/GeoScanner.hx
Normal file
76
src/kernel/peripherals/GeoScanner.hx
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package kernel.peripherals;
|
||||||
|
|
||||||
|
import lib.BlockPos;
|
||||||
|
import lib.Tags;
|
||||||
|
import cc.Peripheral;
|
||||||
|
|
||||||
|
using tink.CoreApi;
|
||||||
|
using lua.Table;
|
||||||
|
|
||||||
|
@:structInit typedef ScanBlock = {
|
||||||
|
name:String,
|
||||||
|
tags:Tags,
|
||||||
|
pos:BlockPos
|
||||||
|
}
|
||||||
|
|
||||||
|
class GeoScanner implements IPeripheral {
|
||||||
|
public static inline final TYPE_NAME:String = "geoScanner";
|
||||||
|
|
||||||
|
private final addr:String;
|
||||||
|
|
||||||
|
public function new(addr:String) {
|
||||||
|
this.addr = addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAddr():String {
|
||||||
|
return this.addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getType():String {
|
||||||
|
return TYPE_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the current time remaining until then next scan() can be ran.
|
||||||
|
**/
|
||||||
|
public function getScanCooldown():Int {
|
||||||
|
return Peripheral.call(this.addr, "getScanCooldown");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the cost in FE for a scan with the given radius.
|
||||||
|
Returns null of scan of this radius is not posible.
|
||||||
|
**/
|
||||||
|
public function cost(radius:Int):Null<Int> {
|
||||||
|
return Peripheral.call(this.addr, "cost", radius);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns a list of data about all blocks in the radius.
|
||||||
|
X,Y,Z are relative to the geoscanner block.
|
||||||
|
Air blocks are not included.
|
||||||
|
List is unsorted.
|
||||||
|
**/
|
||||||
|
public function scan(radius:Int):Outcome<Array<ScanBlock>, String> {
|
||||||
|
// TODO: Handel fail state
|
||||||
|
var result:lua.Table<Int, {
|
||||||
|
x:Int,
|
||||||
|
y:Int,
|
||||||
|
z:Int,
|
||||||
|
name:String,
|
||||||
|
tags:lua.Table<Int, String>
|
||||||
|
}> = Peripheral.call(this.addr, "scan", radius);
|
||||||
|
|
||||||
|
return Success(result.toArray().map((e) -> {
|
||||||
|
name: e.name,
|
||||||
|
tags: Tags.fromIntTable(e.tags),
|
||||||
|
pos: new BlockPos(e.x, e.y, e.z)
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function chunkAnalyze():Outcome<Map<String, Int>, String> {
|
||||||
|
var result:lua.Table<String, Int> = Peripheral.call(this.addr, "chunkAnalyze");
|
||||||
|
|
||||||
|
return Success(result.toMap());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,9 @@
|
|||||||
package kernel.peripherals;
|
package kernel.peripherals;
|
||||||
|
|
||||||
|
import kernel.log.Log;
|
||||||
|
import lib.Debug;
|
||||||
|
import cc.Peripheral;
|
||||||
|
import lib.Tags;
|
||||||
import lib.Item;
|
import lib.Item;
|
||||||
import cc.periphs.ItemStorage.ReducedItemInfo;
|
import cc.periphs.ItemStorage.ReducedItemInfo;
|
||||||
import lua.Table;
|
import lua.Table;
|
||||||
@@ -22,7 +26,7 @@ class ItemInfo {
|
|||||||
class DetailedItemInfo extends ItemInfo {
|
class DetailedItemInfo extends ItemInfo {
|
||||||
public final displayName:String;
|
public final displayName:String;
|
||||||
public final maxCount:Int;
|
public final maxCount:Int;
|
||||||
public final tags:Array<String>;
|
public final tags:Tags;
|
||||||
public final durability:Null<Float>;
|
public final durability:Null<Float>;
|
||||||
public final damage:Null<Int>;
|
public final damage:Null<Int>;
|
||||||
public final maxDamage:Null<Int>;
|
public final maxDamage:Null<Int>;
|
||||||
@@ -32,10 +36,7 @@ class DetailedItemInfo extends ItemInfo {
|
|||||||
private function new(from:cc.periphs.ItemStorage.DetailedItemInfo) {
|
private function new(from:cc.periphs.ItemStorage.DetailedItemInfo) {
|
||||||
super(from);
|
super(from);
|
||||||
|
|
||||||
this.tags = [];
|
this.tags = Tags.fromBoolTable(from.tags);
|
||||||
for (k => _ in Table.toMap(from.tags)) {
|
|
||||||
this.tags.push(k);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.displayName = from.displayName;
|
this.displayName = from.displayName;
|
||||||
this.maxCount = from.maxCount;
|
this.maxCount = from.maxCount;
|
||||||
@@ -55,11 +56,9 @@ class Inventory implements IPeripheral {
|
|||||||
public static inline final TYPE_NAME:String = "inventory";
|
public static inline final TYPE_NAME:String = "inventory";
|
||||||
|
|
||||||
private final addr:String;
|
private final addr:String;
|
||||||
private final native:cc.periphs.ItemStorage;
|
|
||||||
|
|
||||||
public function new(addr:String) {
|
public function new(addr:String) {
|
||||||
this.addr = addr;
|
this.addr = addr;
|
||||||
this.native = cc.Peripheral.wrap(addr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAddr():String {
|
public function getAddr():String {
|
||||||
@@ -74,18 +73,18 @@ class Inventory implements IPeripheral {
|
|||||||
Get the size of this inventory.
|
Get the size of this inventory.
|
||||||
**/
|
**/
|
||||||
public function size():Int {
|
public function size():Int {
|
||||||
return this.native.size();
|
return Peripheral.call(this.addr, "size");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
List all items in this inventory.
|
List all items in this inventory.
|
||||||
**/
|
**/
|
||||||
public function list():Map<Int, ItemInfo> {
|
public function list():Map<Int, ItemInfo> {
|
||||||
var list = Table.toArray(this.native.list());
|
var list:Map<Int, ReducedItemInfo> = Table.toMap(Peripheral.call(this.addr, "list"));
|
||||||
var rtn:Map<Int, ItemInfo> = new Map();
|
var rtn:Map<Int, ItemInfo> = new Map();
|
||||||
|
|
||||||
for (k => v in list) {
|
for (k => v in list) {
|
||||||
rtn.set(k, new ItemInfo(v));
|
rtn.set(k - 1, new ItemInfo(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
return rtn;
|
return rtn;
|
||||||
@@ -95,7 +94,7 @@ class Inventory implements IPeripheral {
|
|||||||
Get detailed information about an item.
|
Get detailed information about an item.
|
||||||
**/
|
**/
|
||||||
public function getItemDetail(slot:Int):DetailedItemInfo {
|
public function getItemDetail(slot:Int):DetailedItemInfo {
|
||||||
return new DetailedItemInfo(this.native.getItemDetail(slot));
|
return new DetailedItemInfo(Peripheral.call(this.addr, "getItemDetail", slot + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -105,20 +104,20 @@ class Inventory implements IPeripheral {
|
|||||||
Keep in mind that this does not depend on that item stored in this slot.
|
Keep in mind that this does not depend on that item stored in this slot.
|
||||||
**/
|
**/
|
||||||
public function getItemLimit(slot:Int):Int {
|
public function getItemLimit(slot:Int):Int {
|
||||||
return this.native.getItemLimit(slot);
|
return Peripheral.call(this.addr, "getItemLimit", slot + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Push items from one inventory to another connected one.
|
Push items from one inventory to another connected one. Needs to be on the same wired network.
|
||||||
**/
|
**/
|
||||||
public function pushItems(toSlot:Int, to:String, ?limit:Int, ?toSlot:Int):Int {
|
public function pushItems(to:String, fromSlot:Int, ?limit:Int, ?toSlot:Int):Int {
|
||||||
return this.native.pushItems(to, toSlot, limit, toSlot);
|
return Peripheral.call(this.addr, "pushItems", to, fromSlot + 1, limit, toSlot);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Pull items from a connected inventory into this one.
|
Pull items from a connected inventory into this one. Needs to be on the same wired network.
|
||||||
**/
|
**/
|
||||||
public function pullItems(from:String, fromSlot:Int, ?limit:Int, ?toSlot:Int):Int {
|
public function pullItems(from:String, fromSlot:Int, ?limit:Int, ?toSlot:Int):Int {
|
||||||
return this.native.pullItems(from, fromSlot, limit, toSlot);
|
return Peripheral.call(this.addr, "pullItems", from, fromSlot + 1, limit, toSlot);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package kernel.peripherals;
|
package kernel.peripherals;
|
||||||
|
|
||||||
import kernel.log.Log;
|
|
||||||
import kernel.peripherals.Modem;
|
import kernel.peripherals.Modem;
|
||||||
import kernel.peripherals.Screen;
|
import kernel.peripherals.Screen;
|
||||||
|
|
||||||
@@ -83,6 +82,8 @@ class Peripheral {
|
|||||||
return getRedstone(addr);
|
return getRedstone(addr);
|
||||||
case Inventory.TYPE_NAME:
|
case Inventory.TYPE_NAME:
|
||||||
return getInventory(addr);
|
return getInventory(addr);
|
||||||
|
case GeoScanner.TYPE_NAME:
|
||||||
|
return getGeoScanner(addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@@ -180,4 +181,15 @@ class Peripheral {
|
|||||||
public static function getAllInventorys():Array<Inventory> {
|
public static function getAllInventorys():Array<Inventory> {
|
||||||
return [for (addr in findAddrByType(Inventory.TYPE_NAME)) new Inventory(addr)];
|
return [for (addr in findAddrByType(Inventory.TYPE_NAME)) new Inventory(addr)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getGeoScanner(addr:String):Null<GeoScanner> {
|
||||||
|
var addr = safeGetAddr(addr, GeoScanner.TYPE_NAME);
|
||||||
|
if (addr == null)
|
||||||
|
return null;
|
||||||
|
return new GeoScanner(addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getAllGeoScanners():Array<GeoScanner> {
|
||||||
|
return [for (addr in findAddrByType(GeoScanner.TYPE_NAME)) new GeoScanner(addr)];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/lib/Block.hx
Normal file
12
src/lib/Block.hx
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package lib;
|
||||||
|
|
||||||
|
@:structInit
|
||||||
|
class Block {
|
||||||
|
public final name:PaN;
|
||||||
|
public final tags:Tags;
|
||||||
|
|
||||||
|
public function new(name:String, tags:Tags) {
|
||||||
|
this.name = name;
|
||||||
|
this.tags = tags;
|
||||||
|
}
|
||||||
|
}
|
||||||
114
src/lib/BlockBlockMap.hx
Normal file
114
src/lib/BlockBlockMap.hx
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
package lib;
|
||||||
|
|
||||||
|
import kernel.peripherals.GeoScanner.ScanBlock;
|
||||||
|
import lib.BlockMap;
|
||||||
|
|
||||||
|
@:forward
|
||||||
|
abstract BlockBlockMap(BlockMap<Block>) {
|
||||||
|
public inline function new() {
|
||||||
|
this = new BlockMap<Block>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fromScan(scan:Array<ScanBlock>):BlockBlockMap {
|
||||||
|
var map:BlockBlockMap = new BlockBlockMap();
|
||||||
|
|
||||||
|
for (block in scan) {
|
||||||
|
map.set(block.pos, new Block(block.name, block.tags));
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Navigate from one point into another.
|
||||||
|
Returns the path to take.
|
||||||
|
Based on A*
|
||||||
|
**/
|
||||||
|
public function nav(from:BlockPos, to:BlockPos):Array<BlockPos> {
|
||||||
|
if (!canPass(to)) {
|
||||||
|
trace("End is blocked");
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
var openSet = new PriorityQueue();
|
||||||
|
openSet.insert(from, 0);
|
||||||
|
|
||||||
|
var cameFrom:BlockMap<BlockPos> = new BlockMap();
|
||||||
|
|
||||||
|
var gCost:BlockMap<Int> = new BlockMap(); // G cost is the distance from start
|
||||||
|
gCost.set(from, 0);
|
||||||
|
|
||||||
|
var fCost:BlockMap<Float> = new BlockMap(); // F cost is the combines cost of moving to this pos (G cost) + the distnace to the end
|
||||||
|
fCost.set(from, from.distance(to));
|
||||||
|
|
||||||
|
while (!openSet.isEmpty()) {
|
||||||
|
var current = openSet.extractMin(); // Check the pos with the lowest F cost
|
||||||
|
|
||||||
|
if (current.equals(to)) {
|
||||||
|
var totalPath = [];
|
||||||
|
var currentPathPos = to;
|
||||||
|
|
||||||
|
while (cameFrom.exists(currentPathPos)) {
|
||||||
|
totalPath.unshift(currentPathPos);
|
||||||
|
currentPathPos = cameFrom.get(currentPathPos);
|
||||||
|
}
|
||||||
|
totalPath.unshift(from);
|
||||||
|
|
||||||
|
return totalPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (neighbor in current.neighbors()) {
|
||||||
|
if (!canPass(neighbor)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var newGCost = gCost.get(current) + 1; // Movment cost to neighbor is always 1 as we can't move diagonally
|
||||||
|
|
||||||
|
if (newGCost < (gCost.get(neighbor) ?? 9999)) {
|
||||||
|
cameFrom.set(neighbor, current);
|
||||||
|
gCost.set(neighbor, newGCost);
|
||||||
|
fCost.set(neighbor, newGCost + neighbor.distance(to));
|
||||||
|
|
||||||
|
if (!openSet.containsElement(neighbor)) {
|
||||||
|
openSet.insert(neighbor, fCost.get(neighbor));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Some block can be passed by the turtle.
|
||||||
|
Some blocks get destroyed by it.
|
||||||
|
**/
|
||||||
|
public function canPass(k:BlockPos):Bool {
|
||||||
|
var block = this.get(k);
|
||||||
|
if (block == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch block.name {
|
||||||
|
case "minecraft:water":
|
||||||
|
return true;
|
||||||
|
case "minecaft:lava":
|
||||||
|
case "mimecraft:grass":
|
||||||
|
case "minecraft:hanging_roots":
|
||||||
|
case "minecraft:warped_roots":
|
||||||
|
case "minecraft:dead_bush":
|
||||||
|
case "minecraft:fern":
|
||||||
|
case "minecraft:vine":
|
||||||
|
case "minecraft:glow_lichen":
|
||||||
|
case "minecraft:seagrass":
|
||||||
|
case "minecraft:crimson_roots":
|
||||||
|
case "minecraft:nether_sprouts":
|
||||||
|
case "minecraft:snow":
|
||||||
|
case "minecraft:rose_bush":
|
||||||
|
case "minecraft:fire":
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
48
src/lib/BlockMap.hx
Normal file
48
src/lib/BlockMap.hx
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package lib;
|
||||||
|
|
||||||
|
import haxe.ds.IntMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Map values to positions in 3D space. For representing blocks from a scan see `BlockBlockMap`.
|
||||||
|
|
||||||
|
The only reason this exsists is that the abstract class for BlockPos does not
|
||||||
|
satisfy the constraint of HashMap. The hashCode function needs to be on a real class and not on
|
||||||
|
an abstract class. Thanks Obama.
|
||||||
|
**/
|
||||||
|
@:forward(iterator, clear)
|
||||||
|
abstract BlockMap<T>(IntMap<T>) {
|
||||||
|
/**
|
||||||
|
Creates a new HashMap.
|
||||||
|
**/
|
||||||
|
public inline function new() {
|
||||||
|
this = new IntMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
See `Map.set`
|
||||||
|
**/
|
||||||
|
@:arrayAccess public inline function set(k:BlockPos, v:T) {
|
||||||
|
this.set(k.hashCode(), v);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
See `Map.get`
|
||||||
|
**/
|
||||||
|
@:arrayAccess public inline function get(k:BlockPos) {
|
||||||
|
return this.get(k.hashCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
See `Map.exists`
|
||||||
|
**/
|
||||||
|
public inline function exists(k:BlockPos) {
|
||||||
|
return this.exists(k.hashCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
See `Map.remove`
|
||||||
|
**/
|
||||||
|
public inline function remove(k:BlockPos) {
|
||||||
|
return this.remove(k.hashCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package lib;
|
package lib;
|
||||||
|
|
||||||
|
import lib.Vec.Vec2;
|
||||||
import lib.Vec.Vec3;
|
import lib.Vec.Vec3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -7,46 +8,30 @@ import lib.Vec.Vec3;
|
|||||||
Basicly a wrapper for Vec3<Int> with some extra functions.
|
Basicly a wrapper for Vec3<Int> with some extra functions.
|
||||||
`Y` represents the height of the point.
|
`Y` represents the height of the point.
|
||||||
**/
|
**/
|
||||||
@:forward(x, y, z)
|
@:forward
|
||||||
abstract BlockPos(Vec3<Int>) from Vec3<Int> to Vec3<Int> {
|
abstract BlockPos(Vec3<Int>) from Vec3<Int> to Vec3<Int> {
|
||||||
public inline function new(x:Int, y:Int, z:Int) {
|
public inline function new(x:Int, y:Int, z:Int) {
|
||||||
this = new Vec3(x, y, z);
|
this = new Vec3(x, y, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
@:op(A + B)
|
@:op(A + B)
|
||||||
public function add(rhs:BlockPos):BlockPos {
|
public inline function add(rhs:BlockPos):BlockPos {
|
||||||
return {
|
return this.add(rhs);
|
||||||
y: this.y + rhs.y,
|
|
||||||
x: this.x + rhs.x,
|
|
||||||
z: this.z + rhs.z
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@:op(A - B)
|
@:op(A - B)
|
||||||
public function sub(rhs:BlockPos):BlockPos {
|
public inline function sub(rhs:BlockPos):BlockPos {
|
||||||
return {
|
return this.sub(rhs);
|
||||||
y: this.y - rhs.y,
|
|
||||||
x: this.x - rhs.x,
|
|
||||||
z: this.z - rhs.z
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@:op(A * B)
|
@:op(A * B)
|
||||||
public function multiplyScalar(rhs:Int):BlockPos {
|
public inline function multiplyScalar(rhs:Int):BlockPos {
|
||||||
return {
|
return this.multiplyScalar(rhs);
|
||||||
y: this.y * rhs,
|
|
||||||
x: this.x * rhs,
|
|
||||||
z: this.z * rhs
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@:op(-A)
|
@:op(-A)
|
||||||
public function negate():BlockPos {
|
public inline function negate():BlockPos {
|
||||||
return {
|
return this.negate();
|
||||||
y: -this.y,
|
|
||||||
x: -this.x,
|
|
||||||
z: -this.z
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@:op(A == B)
|
@:op(A == B)
|
||||||
@@ -59,22 +44,6 @@ abstract BlockPos(Vec3<Int>) from Vec3<Int> to Vec3<Int> {
|
|||||||
return !equals(rhs);
|
return !equals(rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dot(rhs:BlockPos):Float {
|
|
||||||
return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function cross(rhs:BlockPos):BlockPos {
|
|
||||||
return {
|
|
||||||
x: this.x * rhs.y - this.y * rhs.x,
|
|
||||||
y: this.y * rhs.z - this.z * rhs.y,
|
|
||||||
z: this.z * rhs.x - this.x * rhs.z,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public function length():Float {
|
|
||||||
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function hashCode():Int {
|
public function hashCode():Int {
|
||||||
return (this.x * 73856093) ^ (this.y * 19349663) ^ (this.z * 83492791);
|
return (this.x * 73856093) ^ (this.y * 19349663) ^ (this.z * 83492791);
|
||||||
}
|
}
|
||||||
@@ -83,8 +52,14 @@ 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})';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function distance(rhs:BlockPos):Float {
|
/**
|
||||||
return Math.sqrt(Math.pow(this.x - rhs.x, 2) + Math.pow(this.y - rhs.y, 2) + Math.pow(this.z - rhs.z, 2));
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -13,34 +13,22 @@ abstract ScreenPos(Vec2<Int>) from Vec2<Int> to Vec2<Int> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@:op(A + B)
|
@:op(A + B)
|
||||||
public function add(rhs:Vec2<Int>):ScreenPos {
|
public inline function add(rhs:Vec2<Int>):ScreenPos {
|
||||||
return new ScreenPos({
|
return this.add(rhs);
|
||||||
y: this.y + rhs.y,
|
|
||||||
x: this.x + rhs.x,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@:op(A - B)
|
@:op(A - B)
|
||||||
public function sub(rhs:Vec2<Int>):ScreenPos {
|
public inline function sub(rhs:Vec2<Int>):ScreenPos {
|
||||||
return new ScreenPos({
|
return this.sub(rhs);
|
||||||
y: this.y - rhs.y,
|
|
||||||
x: this.x - rhs.x,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@:op(A * B)
|
@:op(A * B)
|
||||||
public function multiply(rhs:Vec2<Int>):ScreenPos {
|
public inline function multiply(rhs:Vec2<Int>):ScreenPos {
|
||||||
return new ScreenPos({
|
return this.multiply(rhs);
|
||||||
y: this.y * rhs.y,
|
|
||||||
x: this.x * rhs.x,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@:op(-A)
|
@:op(-A)
|
||||||
public function negate():ScreenPos {
|
public inline function negate():ScreenPos {
|
||||||
return new ScreenPos({
|
return this.negate();
|
||||||
y: -this.y,
|
|
||||||
x: -this.x,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
55
src/lib/SinglePromise.hx
Normal file
55
src/lib/SinglePromise.hx
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package lib;
|
||||||
|
|
||||||
|
import kernel.EndOfLoop;
|
||||||
|
|
||||||
|
using tink.CoreApi;
|
||||||
|
|
||||||
|
class SinglePromise<T> {
|
||||||
|
private var inProgress:Bool = false;
|
||||||
|
private var interalPromise:Promise<T> = null;
|
||||||
|
private var interalResolve:T->Void = null;
|
||||||
|
private var interalReject:(Error->Void) = null;
|
||||||
|
private final activate:Void->Void;
|
||||||
|
|
||||||
|
public function new(activate:Void->Void) {
|
||||||
|
this.activate = activate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function request():Promise<T> {
|
||||||
|
if (this.inProgress) {
|
||||||
|
trace("Is progress");
|
||||||
|
return this.interalPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.inProgress = true;
|
||||||
|
|
||||||
|
this.interalPromise = new Promise((resolve, reject) -> {
|
||||||
|
this.interalResolve = resolve;
|
||||||
|
this.interalReject = reject;
|
||||||
|
|
||||||
|
this.activate();
|
||||||
|
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
return this.interalPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isRunning():Bool {
|
||||||
|
return this.inProgress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resolve(val:T) {
|
||||||
|
if (this.inProgress) {
|
||||||
|
this.inProgress = false;
|
||||||
|
this.interalResolve(val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function reject(err:Error) {
|
||||||
|
if (this.inProgress) {
|
||||||
|
this.inProgress = false;
|
||||||
|
this.interalReject(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
64
src/lib/SingleTimeoutPromise.hx
Normal file
64
src/lib/SingleTimeoutPromise.hx
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package lib;
|
||||||
|
|
||||||
|
import kernel.Timer;
|
||||||
|
|
||||||
|
using tink.CoreApi;
|
||||||
|
|
||||||
|
class SingleTimeoutPromise<T> {
|
||||||
|
private var inProgress:Bool = false;
|
||||||
|
private var interalPromise:Promise<T> = null;
|
||||||
|
private var interalResolve:T->Void = null;
|
||||||
|
private var interalReject:(Error->Void) = null;
|
||||||
|
private var timer:Timer = null;
|
||||||
|
|
||||||
|
private final activate:Void->Void;
|
||||||
|
private final timeout:Int;
|
||||||
|
|
||||||
|
public function new(timeout:Int, activate:Void->Void) {
|
||||||
|
this.activate = activate;
|
||||||
|
this.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function request():Promise<T> {
|
||||||
|
if (this.inProgress) {
|
||||||
|
return this.interalPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.inProgress = true;
|
||||||
|
|
||||||
|
this.interalPromise = new Promise((resolve, reject) -> {
|
||||||
|
this.interalResolve = resolve;
|
||||||
|
this.interalReject = reject;
|
||||||
|
|
||||||
|
this.activate();
|
||||||
|
|
||||||
|
this.timer = new Timer(this.timeout, () -> {
|
||||||
|
this.reject(new Error("Timeout"));
|
||||||
|
});
|
||||||
|
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
return this.interalPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isRunning():Bool {
|
||||||
|
return this.inProgress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resolve(val:T) {
|
||||||
|
if (this.inProgress) {
|
||||||
|
this.inProgress = false;
|
||||||
|
this.timer.cancle();
|
||||||
|
this.interalResolve(val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function reject(err:Error) {
|
||||||
|
if (this.inProgress) {
|
||||||
|
this.inProgress = false;
|
||||||
|
this.timer.cancle();
|
||||||
|
this.interalReject(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
49
src/lib/Tags.hx
Normal file
49
src/lib/Tags.hx
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,16 +8,96 @@ package lib;
|
|||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function add(rhs:Vec2<T>):Vec2<T> {
|
||||||
|
return {
|
||||||
|
y: this.y + rhs.y,
|
||||||
|
x: this.x + rhs.x,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sub(rhs:Vec2<T>):Vec2<T> {
|
||||||
|
return {
|
||||||
|
y: this.y - rhs.y,
|
||||||
|
x: this.x - rhs.x,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function multiply(rhs:Vec2<T>):Vec2<T> {
|
||||||
|
return {
|
||||||
|
y: this.y * rhs.y,
|
||||||
|
x: this.x * rhs.x,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function negate():Vec2<T> {
|
||||||
|
return {
|
||||||
|
y: -this.y,
|
||||||
|
x: -this.x,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@:structInit class Vec3<T:Float> {
|
@:structInit class Vec3<T:Float> {
|
||||||
public var x:T;
|
public final x:T;
|
||||||
public var y:T;
|
public final y:T;
|
||||||
public var z:T;
|
public final z:T;
|
||||||
|
|
||||||
public function new(x:T, y:T, z:T) {
|
public function new(x:T, y:T, z:T) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
this.z = z;
|
this.z = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function add(rhs:Vec3<T>):Vec3<T> {
|
||||||
|
return {
|
||||||
|
x: this.x + rhs.x,
|
||||||
|
y: this.y + rhs.y,
|
||||||
|
z: this.z + rhs.z
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sub(rhs:Vec3<T>):Vec3<T> {
|
||||||
|
return {
|
||||||
|
x: this.x - rhs.x,
|
||||||
|
y: this.y - rhs.y,
|
||||||
|
z: this.z - rhs.z
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function multiplyScalar(rhs:T):Vec3<T> {
|
||||||
|
return {
|
||||||
|
x: this.x * rhs,
|
||||||
|
y: this.y * rhs,
|
||||||
|
z: this.z * rhs
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function negate():Vec3<T> {
|
||||||
|
return {
|
||||||
|
x: -this.x,
|
||||||
|
y: -this.y,
|
||||||
|
z: -this.z
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dot(rhs:Vec3<T>):Float {
|
||||||
|
return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cross(rhs:Vec3<T>):Vec3<T> {
|
||||||
|
return {
|
||||||
|
x: this.y * rhs.z - this.z * rhs.y,
|
||||||
|
y: this.z * rhs.x - this.x * rhs.z,
|
||||||
|
z: this.x * rhs.y - this.y * rhs.x
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function length():Float {
|
||||||
|
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function distance(rhs:Vec3<T>):Float {
|
||||||
|
return Math.sqrt(Math.pow(this.x - rhs.x, 2) + Math.pow(this.y - rhs.y, 2) + Math.pow(this.z - rhs.z, 2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package lib;
|
package lib;
|
||||||
|
|
||||||
|
import lib.Vec.Vec2;
|
||||||
import lib.Vec.Vec3;
|
import lib.Vec.Vec3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -7,78 +8,46 @@ import lib.Vec.Vec3;
|
|||||||
Basicly a wrapper for Vec3<Float> with some extra functions.
|
Basicly a wrapper for Vec3<Float> with some extra functions.
|
||||||
`Y` represents the height of the point.
|
`Y` represents the height of the point.
|
||||||
**/
|
**/
|
||||||
@:forward(x, y, z)
|
@:forward
|
||||||
abstract WorldPos(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
|
abstract WorldPos(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
|
||||||
inline public function new(i:Vec3<Float>) {
|
public inline function new(v:Vec3<Float>) {
|
||||||
this = i;
|
this = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@:op(A + B)
|
@:op(A + B)
|
||||||
public function add(rhs:Vec3<Float>):WorldPos {
|
public inline function add(rhs:WorldPos):WorldPos {
|
||||||
return new WorldPos({
|
return this.add(rhs);
|
||||||
y: this.y + rhs.y,
|
|
||||||
x: this.x + rhs.x,
|
|
||||||
z: this.z + rhs.z
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@:op(A - B)
|
@:op(A - B)
|
||||||
public function sub(rhs:Vec3<Float>):WorldPos {
|
public inline function sub(rhs:Vec3<Float>):WorldPos {
|
||||||
return new WorldPos({
|
return this.sub(rhs);
|
||||||
y: this.y - rhs.y,
|
|
||||||
x: this.x - rhs.x,
|
|
||||||
z: this.z - rhs.z
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@:op(A * B)
|
@:op(A * B)
|
||||||
public function multiplyScalar(rhs:Float):WorldPos {
|
public inline function multiplyScalar(rhs:Float):WorldPos {
|
||||||
return new WorldPos({
|
return this.multiplyScalar(rhs);
|
||||||
y: this.y * rhs,
|
|
||||||
x: this.x * rhs,
|
|
||||||
z: this.z * rhs
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@:op(A / B)
|
@:op(A / B)
|
||||||
public function divideScalar(rhs:Float):WorldPos {
|
public function divideScalar(rhs:Float):WorldPos {
|
||||||
return new WorldPos({
|
return {
|
||||||
y: this.y / rhs,
|
y: this.y / rhs,
|
||||||
x: this.x / rhs,
|
x: this.x / rhs,
|
||||||
z: this.z / rhs
|
z: this.z / rhs
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@:op(-A)
|
@:op(-A)
|
||||||
public function negate():WorldPos {
|
public function negate():WorldPos {
|
||||||
return new WorldPos({
|
return this.negate();
|
||||||
y: -this.y,
|
|
||||||
x: -this.x,
|
|
||||||
z: -this.z
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public function dot(rhs:Vec3<Float>):Float {
|
|
||||||
return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function cross(rhs:Vec3<Float>):WorldPos {
|
|
||||||
return new WorldPos({
|
|
||||||
x: this.y * rhs.z - this.z * rhs.y,
|
|
||||||
y: this.z * rhs.x - this.x * rhs.z,
|
|
||||||
z: this.x * rhs.y - this.y * rhs.x
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function normalize():WorldPos {
|
public function normalize():WorldPos {
|
||||||
var l = length();
|
var l = this.length();
|
||||||
return multiplyScalar(1 / l);
|
return multiplyScalar(1 / l);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function length():Float {
|
|
||||||
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
|
||||||
}
|
|
||||||
|
|
||||||
@:op(A == B)
|
@:op(A == B)
|
||||||
public function equals(rhs:WorldPos):Bool {
|
public function equals(rhs:WorldPos):Bool {
|
||||||
return close(rhs);
|
return close(rhs);
|
||||||
@@ -97,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),
|
||||||
@@ -104,8 +83,4 @@ abstract WorldPos(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
|
|||||||
z: Math.fround(this.z)
|
z: Math.fround(this.z)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function distance(rhs:WorldPos):Float {
|
|
||||||
return Math.sqrt(Math.pow(this.x - rhs.x, 2) + Math.pow(this.y - rhs.y, 2) + Math.pow(this.z - rhs.z, 2));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user