package hx.minepainter.sculpture;

import net.minecraft.init.Blocks;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

public class Operations {
	
	interface Rotation{
		int to(int from);
	}
	
	private static int[] rotationBuffer = new int[512]; 
	public static void rotate(Sculpture sculpture, Rotation rotation){
		for(int i = 0; i < 512; i++){
			int x = (i >> 8) & 7;
			int y = (i >> 4) & 7;
			int z = (i >> 0) & 7;
			
			rotationBuffer[i] = sculpture.getIndex(x,y,z);
		}
		
		for(int i = 0; i < 512; i++){
			int x = (i >> 8) & 7;
			int y = (i >> 4) & 7;
			int z = (i >> 0) & 7;
			
			sculpture.setIndex(x,y,z, rotationBuffer[i]);
		}
	}
	
	
	public static int[] raytrace(Sculpture sculpture, Vec3 start, Vec3 end){
		
		int[] xyz = new int[]{-1,-1,-1};
		
		for(int x = 0; x <= 8; x ++){

			Vec3 hit = start.getIntermediateWithXValue(end, x/8f);
			if(hit == null)continue;
			
			int y = (int)hit.yCoord;
			int z = (int)hit.zCoord;
			
			if(end.xCoord > start.xCoord)updateResult(sculpture, xyz,x,y,z,start);
			else updateResult(sculpture,xyz,x-1,y,z,start);
		}
		
		for(int y = 0; y <= 8; y ++){
			
			Vec3 hit = start.getIntermediateWithYValue(end, y/8f);
			
			int x = (int)hit.xCoord;
			int z = (int)hit.zCoord;
			
			if(end.yCoord > start.yCoord)updateResult(sculpture, xyz,x,y,z,start);
			else updateResult(sculpture, xyz,x,y-1,z,start);
		}
		
		for(int z = 0; z <= 8; z ++){
			
			Vec3 hit = start.getIntermediateWithZValue(end, z/8f);
			
			int x = (int)hit.xCoord;
			int y = (int)hit.yCoord;
			
			if(end.zCoord > start.zCoord)updateResult(sculpture, xyz,x,y,z,start);
			else updateResult(sculpture, xyz,x,y,z-1,start);
		}
		
		return xyz;
	}
	
	private static void updateResult(Sculpture sculpture, int[] xyz, int x,int y,int z, Vec3 start){
		if(!sculpture.contains(x, y, z))return;
		if(sculpture.getBlockAt(x, y, z, null) == Blocks.air)return;

		if(xyz[0] != -1){		
			if(x != xyz[0] && start.xCoord - x > 0 ==  xyz[0] - x > 0)return;
			if(y != xyz[1] && start.yCoord - y > 0 ==  xyz[1] - y > 0)return;
			if(z != xyz[2] && start.zCoord - z > 0 ==  xyz[2] - z > 0)return;
		}
		
		xyz[0] = x;xyz[1] = y; xyz[2] = z; 
	}
	
	
}

@@ -215,513 +215,8 @@
 %7B%0A%09%0A
-%09interface Rotation%7B%0A%09%09int to(int from);%0A%09%7D%0A%09%0A%09private static int%5B%5D rotationBuffer = new int%5B512%5D; %0A%09public static void rotate(Sculpture sculpture, Rotation rotation)%7B%0A%09%09for(int i = 0; i %3C 512; i++)%7B%0A%09%09%09int x = (i %3E%3E 8) & 7;%0A%09%09%09int y = (i %3E%3E 4) & 7;%0A%09%09%09int z = (i %3E%3E 0) & 7;%0A%09%09%09%0A%09%09%09rotationBuffer%5Bi%5D = sculpture.getIndex(x,y,z);%0A%09%09%7D%0A%09%09%0A%09%09for(int i = 0; i %3C 512; i++)%7B%0A%09%09%09int x = (i %3E%3E 8) & 7;%0A%09%09%09int y = (i %3E%3E 4) & 7;%0A%09%09%09int z = (i %3E%3E 0) & 7;%0A%09%09%09%0A%09%09%09sculpture.setIndex(x,y,z, rotationBuffer%5Bi%5D);%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09%0A%09p

remove trash code