private static String code_XR_S="0000000000000101111110100";
private static String code_XR_O="1000110011000000000000001";
private static String code_XU_S="1000000000000010011001011111";
private static String code_XU_O="0010001110011010000000000000";
private static String code_RL_S="111011000000000000000011110100";
private static String code_RL_O="000000010100011100001000000001";
private static String code_RD_S="100000010000000000000001101111110100";
private static String code_RD_O="001000110100111000110000000000000001";
private static String code_OR_S="00000";
private static String code_OR_O="10000";
private static String code_OD_S="0000";
private static String code_OD_O="1010";
private static String code_OL_S="0000";
private static String code_OL_O="1011";
private static String code_OU_S="00000";
private static String code_OU_O="10001";
private static String code_SR_S="0000";
private static String code_SR_O="1100";
private static String code_SD_S="0000";
private static String code_SD_O="1110";
private static String code_SL_S="0000";
private static String code_SL_O="1001";
private static String code_SU_S="0000";
private static String code_SU_O="1101";
private static String code_CN_S="0000";
private static String code_CN_O="1111";
private static String code_IT_S="00000";
private static String code_IT_O="10001";
private static String code_FT_S="1101110000000";
private static String code_FT_O="0000000110000";


public static void pathh(int fromx, int tox, int y, int typ, Lattice l){
    int dir_x = 1;
    int dir_y = 0;
    
    if (fromx < tox){
    	dir_x = 1;
    }else{
	int temp = fromx;
	fromx = tox;
	tox = temp;

    	dir_x = -1;
    }
    
    for (int x=fromx; x<=tox; x++){
    	vonNeumann s = (vonNeumann)l.getState(x,y,0);
    	s.mystate.dir_x = dir_x;
    	s.mystate.dir_y = dir_y;
    	s.mystate.typ = typ;
    	s.mystate.excited = false;
    }
}

public static void pathv(int x, int fromy, int toy,  int typ, Lattice l){
    int dir_x = 0;
    int dir_y = 1;
    
    if (fromy < toy){
    	dir_y = 1;
    }else{
	int temp = fromy;
	fromy = toy;
	toy = temp;

    	dir_y = -1;
    }
    
    for (int y=fromy; y<=toy; y++){
    	vonNeumann s = (vonNeumann)l.getState(x,y,0);
    	s.mystate.dir_x = dir_x;
    	s.mystate.dir_y = dir_y;
    	s.mystate.typ = typ;
    	s.mystate.excited = false;
    }
}

public static void pulser(int x, int y, String code, Lattice l){
    int len = 2*code.length();
    int dir_x = 0;
    int dir_y = 1;
    
    pathh(x, x+len+1, y+2, 1, l);
    pathh(x, x+len-2, y, 1, l);
    for (int i=0; i<code.length(); i++){
    	vonNeumann s1 = (vonNeumann)l.getState(x+2*i,y,0);
    	vonNeumann s2 = (vonNeumann)l.getState(x+2*i,y+1,0);
    	vonNeumann s3 = (vonNeumann)l.getState(x+2*i,y+2,0);
    	s1.mystate.typ = 3;
    	s1.mystate.dir_x = 0;
    	s1.mystate.dir_y = 0;

    	if (code.substring(i,i+1).equals("1")){
    	    s2.mystate.dir_x = dir_x;
    	    s2.mystate.dir_y = dir_y;
    	    
    	    s2.mystate.typ = 1;
    	}else{
    	    s2.mystate.typ = 0;
    	}
    }
    //vonNeumann s3 = (vonNeumann)l.getState(x-3,y,0);
    //s3.mystate.excited = true;
    //s3 = (vonNeumann)l.getState(x-2,y,0);
    //s3.mystate.excited = true;
    
}

public static void dualPulser(int x, int y, String code1, String code2, Lattice l){
    int len = 2*Math.max(code1.length(),code2.length());
    int dir_x = 0;
    int dir_y = 1;
    
    pathh(x, x+len-2, y, 1, l);
    pathh(x, x+len-2, y+2, 1, l);
    pathh(x, x+len-1, y+4, 1, l);

    for (int i=0; i<len/2; i++){
    	vonNeumann s1 = (vonNeumann)l.getState(x+2*i,y,0);
    	vonNeumann s2 = (vonNeumann)l.getState(x+2*i,y+1,0);
    	vonNeumann s3 = (vonNeumann)l.getState(x+2*i,y+2,0);
    	vonNeumann s4 = (vonNeumann)l.getState(x+2*i,y+3,0);
    	vonNeumann s5 = (vonNeumann)l.getState(x+2*i,y+4,0);
    	s3.mystate.typ = 3;
    	s3.mystate.dir_x = 0;
    	s3.mystate.dir_y = 0;

    	if (i<code2.length() && code2.substring(i,i+1).equals("1")){
    	    s2.mystate.dir_x = dir_x;
    	    s2.mystate.dir_y = -dir_y;
    	    
    	    s2.mystate.typ = 1;
    	}else{
    	    s2.mystate.typ = 0;
    	}


    	if (i<code1.length() && code1.substring(i,i+1).equals("1")){
    	    s4.mystate.dir_x = dir_x;
    	    s4.mystate.dir_y = dir_y;
    	    
    	    s4.mystate.typ = 1;
    	}else{
    	    s4.mystate.typ = 0;
    	}
	
    }
    pathv(x+len, y, y+1, 1, l);
    pathv(x+len, y+4, y+2, 1, l);


    vonNeumann s6;
    s6 = (vonNeumann)l.getState(x+len-1,y,0);
    s6.mystate.typ = 3;

    s6 = (vonNeumann)l.getState(x+len,y+2,0);
    s6.mystate.typ = 3;
    s6 = (vonNeumann)l.getState(x+len,y+1,0);
    s6.mystate.typ = 3;

    s6 = (vonNeumann)l.getState(x+len+1,y+1,0);
    s6.mystate.typ = 1;
    	s6.mystate.dir_x = 1;
    	s6.mystate.dir_y = 0;
    s6 = (vonNeumann)l.getState(x+len+1,y+2,0);
    s6.mystate.typ = 2;
     	s6.mystate.dir_x = 1;
    	s6.mystate.dir_y = 0;
    s6 = (vonNeumann)l.getState(x+len+2,y+1,0);
    s6.mystate.typ = 1;
    	s6.mystate.dir_x = 1;
    	s6.mystate.dir_y = 0;
    s6 = (vonNeumann)l.getState(x+len+2,y+2,0);
    s6.mystate.typ = 2;
     	s6.mystate.dir_x = 0;
    	s6.mystate.dir_y = -1;
   
    
}


public static void loop(int x, int y, int length, Lattice l){
    
    pathh(x, x+2, y, 1, l);
    pathh(x+1, x, y+length-1, 1, l);
    pathv(x+1, y, y+length-2, 1, l);
    pathv(x, y+length-1, y+1, 1, l);

    vonNeumann s1 = (vonNeumann)l.getState(x+1,y,0);
    s1.mystate.typ = 3;
    s1.mystate.dir_x = 0;
    s1.mystate.dir_y = 0;
}

public static void slalom(int x, int y, int height, int paths, Lattice l){
    for (int i=0; i<paths; i++){
    	if ((i%2) == 0){
    	    pathv(x+i, y, y+height, 1, l);
    	    pathh(x+i, x+i+1, y+height, 1, l);
    	}else{
    	    pathv(x+i, y+height, y, 1, l);
    	    pathh(x+i, x+i+1, y, 1, l);
    	}
    }
}

public static void RTCO(int x, int y, Lattice l){
    final String code =	"     ^    "+
    	    	    	" Cv>>C<Cv "+
    	    	    	" ^C^  ^^C "+
			" >>C>>C>v "+
			">C ^Cv^ v "+
			" v ^^C^ C>"+
			" >>C>>C>^ "+
			" Cv^  ^Cv "+
			" ^C^C>^^C "+
			"    ^     ";
			
    int dir_x = 0;
    int dir_y = 1;
    
    for (int i=0; i<10; i++){
    	for (int j=0; j<10; j++){
    	    vonNeumann s = (vonNeumann)l.getState(x+i,y+j,0);
    	    switch(code.charAt((9-j)*10+i)){
	    	case '^': s.mystate.typ = 1;
		    	  s.mystate.dir_x = 0;
    	    	    	  s.mystate.dir_y = 1;
    	    	    	  break;
	    	case 'v': s.mystate.typ = 1;
		    	  s.mystate.dir_x = 0;
    	    	    	  s.mystate.dir_y = -1;
    	    	    	  break;
	    	case '>': s.mystate.typ = 1;
		    	  s.mystate.dir_x = 1;
    	    	    	  s.mystate.dir_y = 0;
    	    	    	  break;
	    	case '<': s.mystate.typ = 1;
		    	  s.mystate.dir_x = -1;
    	    	    	  s.mystate.dir_y = 0;
    	    	    	  break;
	    	case 'C': s.mystate.typ = 3;
		    	  s.mystate.dir_x = 0;
    	    	    	  s.mystate.dir_y = 0;
    	    	    	  break;
    	    	default: break;
	    }
	}
    }

    //vonNeumann s3 = (vonNeumann)l.getState(x-3,y,0);
    //s3.mystate.excited = true;
    //s3 = (vonNeumann)l.getState(x-2,y,0);
    //s3.mystate.excited = true;
    
}

public static void initialize(Lattice l, int option){
    int lx = l.getX();
    int ly = l.getY();
    int lz = l.getZ();
    
    colorInit();
    iconInit();
    
    for (int x=0; x<lx; x++){
    for (int y=0; y<ly; y++){
    for (int z=0; z<lz; z++){
        vonNeumann s =  ((vonNeumann)l.getState(x,y,z));
        if (true){
            s.init(0,false,0,0,0,3);
        }
    }}}
    if (option == 0){
    try{
    	loop(1,3,10,l);

    	pathv(2,2,1,1,l);

    	pulser(4, 3, "110011", l);
    	vonNeumann s1 = (vonNeumann)l.getState(2,5,0);
    	s1.mystate.excited = true;
     	s1 = (vonNeumann)l.getState(2,6,0);
    	//s1.mystate.excited = true;
     	s1 = (vonNeumann)l.getState(2,8,0);
    	//s1.mystate.excited = true;
   	
   	slalom(14,5,8,3,l);
    }catch(ArrayIndexOutOfBoundsException e){
    }catch(NullPointerException npe){
    }
    }
    if (option == 1){
    try{
    	loop(1,4,5,l);

    	pathv(2,3,1,1,l);

    	// pulser(4, 4, "10000101", l);
    	pulser(4, 4, "1111110101", l);
    	vonNeumann s1 = (vonNeumann)l.getState(2,5,0);
    	s1.mystate.excited = true;
   	slalom(20,6,8,3,l);
    	
    	pulser(4, 0, "101111", l);
    	pathh(17,18,2,1, l);
    	pathh(19,20,2,2, l);
    	s1 = (vonNeumann)l.getState(19,2,0);
    	s1.mystate.typ = 3;

    }catch(ArrayIndexOutOfBoundsException e){
    }catch(NullPointerException npe){
    }
    }
    if (option == 2){
    try{

	loop(1,1,3,l);


    	pathh(3,9,1,1, l);
    	pathv(10,1,2,1, l);
    	

	//loop(1,7,4,l);
    	//pathh(3,5,7,1, l);

    	((vonNeumann)l.getState(1,2,0)).mystate.excited = true;

    	((vonNeumann)l.getState(1,7,0)).mystate.excited = true;

    	RTCO(6,2,l);
    	((vonNeumann)l.getState(7,4,0)).mystate.attribut_cstate = 1;
    	((vonNeumann)l.getState(7,9,0)).mystate.excited = true;
    	((vonNeumann)l.getState(10,6,0)).mystate.excited = true;
    	((vonNeumann)l.getState(14,9,0)).mystate.attribut_cstate = 1;
    	((vonNeumann)l.getState(13,3,0)).mystate.excited = true;


    }catch(ArrayIndexOutOfBoundsException e){
    }catch(NullPointerException npe){
    }
    }
    if (option == 3){
    try{
    	//loop(1,3,10,l);

    	pathh(2,4,3,1,l);
    	String s=code_XR_S+"0000"+code_CN_S+"0000"+code_RL_S;
    	String o=code_XR_O+"0000"+code_CN_O+"0000"+code_RL_O;
	
    	dualPulser(4, 1, s,o, l);
    	vonNeumann s1 = (vonNeumann)l.getState(2,3,0);
    	s1.mystate.excited = true;
     	s1 = (vonNeumann)l.getState(2,6,0);
    	//s1.mystate.excited = true;
     	s1 = (vonNeumann)l.getState(2,8,0);
    	//s1.mystate.excited = true;
   	
    }catch(ArrayIndexOutOfBoundsException e){
    	System.out.println(e);
    }catch(NullPointerException npe){
    }
    }
}
