Compare commits
2 Commits
1ad78c23bb
...
3219738799
| Author | SHA1 | Date | |
|---|---|---|---|
| 3219738799 | |||
| 5ab1a46840 |
@@ -1,5 +1,6 @@
|
||||
package lib.turtle;
|
||||
|
||||
import util.Pos;
|
||||
import util.Pos3;
|
||||
import kernel.turtle.Turtle;
|
||||
using tink.CoreApi;
|
||||
@@ -36,7 +37,11 @@ class TurtleExecuter {
|
||||
return blocks;
|
||||
}
|
||||
|
||||
public function getFinalOffset(): Pos3 {
|
||||
/**
|
||||
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};
|
||||
|
||||
@@ -54,7 +59,7 @@ class TurtleExecuter {
|
||||
}
|
||||
}
|
||||
|
||||
return pos;
|
||||
return {offset:pos,faceing: new Pos({x:forwardVec.x, y:forwardVec.z})};
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
|
||||
98
src/lib/turtle/TurtleExt.hx
Normal file
98
src/lib/turtle/TurtleExt.hx
Normal file
@@ -0,0 +1,98 @@
|
||||
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.instance.getItemDetail(i).orNull();
|
||||
if (slotItem != null && slotItem.name == item){
|
||||
ret += Turtle.instance.getItemSpace(i);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user