//xx,yy, and zz are the coordinates of the block
//MX,MY,MZ, are the size of the array in wich block info is stored
//entity[] is an array of 64 entities conatining all the meshes
//Ni Mesh index
function UpdateBlock(var xx,yy,zz)
{
var cn=0,Fa[6], Ni=0 ,i;
if(Block[xx][yy][zz] == 6)//6 means full and 3 means full and rendered
{
for(i=0;i<6;i++)Fa[i]=0;//Reset values
//Check if the Blocks arround are full(6||3) or empty(0),set the respective flag Fa[]
if(xx>0) if(Block[xx-1][yy][zz] != 6 && Block[xx-1][yy][zz] != 3){Fa[0]=1;cn=1;}
if(xx<MX)if(Block[xx+1][yy][zz] != 6 && Block[xx+1][yy][zz] != 3){Fa[1]=1;cn=1;}
if(yy>0) if(Block[xx][yy-1][zz] != 6 && Block[xx][yy-1][zz] != 3){Fa[2]=1;cn=1;}
if(yy<MY)if(Block[xx][yy+1][zz] != 6 && Block[xx][yy+1][zz] != 3){Fa[3]=1;cn=1;}
if(zz>0) if(Block[xx][yy][zz-1] != 6 && Block[xx][yy][zz-1] != 3){Fa[4]=1;cn=1;}
if(zz<MZ)if(Block[xx][yy][zz+1] != 6 && Block[xx][yy][zz+1] != 3){Fa[5]=1;cn=1;}
Ni=0;
//use all the flags(Fa[]) as if they were a single binary number
//and translate it into decimal system, in order to get the mesh index
for(i=0;i<6;i++)
if(Fa[i]!=0|(5-i)!=0)
Ni+= pow(Fa[i]*2,5-i);
//If at least one face is visible
if(cn==1)
{
//The block is full and has been rendered
Block[xx][yy][zz] = 3;
//Create the model
you=ent_create(CUBE_MDL,vector(xx*dbb,yy*dbb,zz*dbb),NULL);
//Set the apropiate mesh
ent_setmesh(you,ent_getmesh(entity[Ni],0,0),0,0);
}
}
}