added TurtleExt
This commit is contained in:
		
							parent
							
								
									5ab1a46840
								
							
						
					
					
						commit
						3219738799
					
				
							
								
								
									
										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;
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user