+ int save_file_fd;
+
+ if((save_file_fd=open(file,O_CREAT|O_WRONLY|O_TRUNC))<0)
+ {
+ puts("cannot open save file");
+ return -23;
+ }
+ if(write(save_file_fd,&(display->max_x),sizeof(u32))<sizeof(u32))
+ {
+ puts("failed saving max x");
+ return -23;
+ }
+ if(write(save_file_fd,&(display->max_y),sizeof(u32))<sizeof(u32))
+ {
+ puts("failed saving max y");
+ return -23;
+ }
+ if(write(save_file_fd,&(display->max_z),sizeof(u32))<sizeof(u32))
+ {
+ puts("failed saving max z");
+ return -23;
+ }
+ if(write(save_file_fd,display->cell_p,display->max_x*display->max_y*display->max_z*sizeof(cell))!=display->max_x*display->max_y*display->max_z*sizeof(cell))
+ {
+ puts("saving file failed");
+ return -23;
+ }
+ close(save_file_fd);
+ puts("saved file");
+ return 23;
+}
+
+int distrib_c_conc(cell *cell_p,int c_c0,int c_slope,u32 c_conc,u32 x_max,u32 y_max,u32 z_max)
+{
+ int i,j;
+ u32 area,c_area,total,count;
+ u32 x,y,z,sum_c_z;
+
+ total=0;
+ area=x_max*y_max;
+ sum_c_z=c_c0*z_max+c_slope*gr;
+
+ for(i=0;i<z_max;i++)
+ {
+ count=0;
+ c_area=(c_conc*(c_c0+(i+1)*c_slope))/sum_c_z;
+ for(j=0;j<area;j++)
+ {
+ if(!(cell_p+j+i*area)->status&AMORPH) count++;
+ }
+ for(j=0;j<area;j++)
+ {
+ if(!(cell_p+j+i*area)->status&AMORPH)
+ {
+ (cell_p+j+i*area)->conc=c_area/count;
+ total+=c_area/count;
+ }
+ }
+ j=0;
+ while(j<c_area%count)
+ {
+ x=rand_get(x_max);
+ y=rand_get(y_max);
+ if(!(cell_p+x+y*x_max+i*area)->status&AMORPH)
+ {
+ (cell_p+x+y*x_max+i*area)->conc+=1;
+ total++;
+ j++;
+ }
+ }
+ }
+ i=0;
+ while(i<c_conc-total)
+ {
+ x=rand_get(x_max);
+ y=rand_get(y_max);
+ z=rand_get_lgp(c_slope,c_c0,z_max);
+ if(!(cell_p+x+y*x_max+z*area)->status&AMORPH)
+ {
+ (cell_p+x+y*x_max+z*area)->conc+=1;
+ i++;
+ }
+ }