//transition function
void s3hexErosion(struct CALModel2D* s3hex, int i, int j)
{
	CALint s;
	CALreal d, p, erosion_depth;

	d = calGet2Dr(s3hex,Q.d,i,j);
	if (d > 0)
	{
		s = calGet2Di(s3hex,Q.s,i,j);
		if (s <  -1)
			calSetCurrent2Di(s3hex,Q.s,i,j,s+1);
		if (s == -1) {
			calSetCurrent2Di(s3hex,Q.s,i,j,0);
			doErosion(s3hex,i,j,d);
#ifdef ACTIVE_CELLS
			calAddActiveCell2D(s3hex,i,j);
#endif
		}

		p = calGet2Dr(s3hex,Q.p,i,j);
		if (p > P.mt) {
			erosion_depth = p * P.pef;
			if (erosion_depth > d)
				erosion_depth = d;
			doErosion(s3hex,i,j,erosion_depth);
		}
	}
}
float* getHeightmapData(int w, int h)
{
  width =w;height = h;
  scale = w/8000.f;
  int seed = 1;
  srand(seed);
  logi.clog(LOG_COLOR_LIGHT_BLUE,"Constructing heightmap, seed %d",seed);
  if (heights!=NULL)
    delete[] heights;
  heights = new float[width*height];

  for (int i=0;i<(width)*(height);i++) heights[i] = 0;
  heights[0] = 0.1;
  heights[width-1] = 0;
  heights[(height-1)*(width)] = 0.0;
  heights[(height-1)*(width)+height-1] = 0;

  int sz = width-1;
  int count = 1;while(1<<count<sz)count++;count--;
  float H = 0.9;
  float scaler = pow(2.f,H);
  float rng = 1.f;
  while (sz>1)
  {
    for (int i = 0;i<height-1;i+=sz)
      for (int j = 0;j<width-1;j+=sz)
      {
        float a = heights[i*width+j];
        float b = heights[i*width+j+sz];
        float c = heights[(i+sz)*width+j];
        float d = heights[(i+sz)*width+j+sz];

        float r = rng;
        if (width/sz<4)
          r = 0;
        heights[(i+sz/2)*width+(j+sz/2)] = (a+b+c+d)/4 + r * randf();
        float e = heights[(i+sz/2)*width+(j+sz/2)];
        heights[(i+sz/2)*width+(j)] = (a+c+e)/3 + r * randf();
        heights[(i)*width+(j+sz/2)] = (a+b+e)/3 + r * randf();
        heights[(i+sz/2)*width+(j+sz)] = (b+d+e)/3 + r * randf();
        heights[(i+sz)*width+(j+sz/2)] = (c+d+e)/3 + r * randf();
      }
    sz /= 2;
    count--;
    rng/=scaler;
  }
  for (int i = 0;i<width;i++)
    for (int j = 0; j<height;j++)
      heights[i*width+j] = 8*pow(abs((double)heights[i*width+j]),1.5)*0.7;
  doErosion(width,height,heights);

  return heights;
}