// Last edited on DATE TIME by USER
// Processed by remove-cam-lights

#include "textures.inc"
#include "debug.inc"
background{ color rgb < 0.75, 0.80, 0.85 > }

#declare tx_xadrez =
  texture{
    pigment{ checker color rgb < 0.10, 0.32, 0.60 >, color rgb < 1.00, 0.97, 0.90 > }
    finish{ diffuse 0.9 ambient 0.1 }
    scale 1.0
  }

#declare tam_bit =<2,1,2>;
#declare tam_borda = 0.3;

#declare random_seed = seed (1153);
#macro random()
  rand(random_seed)
#end

#macro make_bit(frac, errado)
  cylinder {
    <        0, 0, 0>
    <tam_bit.x, 0, 0>
    0.1*tam_bit.y
    texture {  Chrome_Metal  }
  }

  #if ( errado )
    #local frac = (1-frac)*0.8 + 0.1;
  #end

  sphere {
    <(.25+0.5*frac)*tam_bit.x,0,0> 0.45*tam_bit.y

    #if ( errado )
      texture {  Rusty_Iron  }
    #else
      texture {  Jade  }
    #end
  }
#end

#macro make_col(nrows, number, prob_erro_bit)
  #local row=0;
  #local _number=int(number);
  #local frac = number - _number;

  #while (row<nrows)
    union {
      #if (mod(_number,2) = 0)
        make_bit(frac  , (random() < prob_erro_bit) )
        #local frac=0;
      #else
        make_bit(1-frac, (random() < prob_erro_bit) )
      #end
      translate <0, (row+.5)*tam_bit.y, 0>
    }
    #local _number=int(_number/2);
    #local row=row+1;
  #end
#end

#macro make_cols(ncols, nrows, numbers, prob_erro_bit)
  #local col=0;
  #while (col<ncols)
    union {
      make_col( nrows, numbers[col], prob_erro_bit )
      translate <col*(tam_borda+tam_bit.x), 0, 0>
    }
    #local col=col+1;
  #end
#end

#macro abaco(numbers, prob_defeito_bit)
  #local ncols = dimension_size(numbers, 1);
  #local maxNum=0;
  #local i=0;
  #while (i<ncols)
    #local maxNum=max(maxNum, numbers[i]);
    #local i=i+1;
  #end
  #local maxNum=int(maxNum)+1; //Garante para os números após a virgula

  #local nrows=0;
  #while (maxNum > 0)
    #local nrows=nrows+1;
    #local maxNum=int(maxNum/2);
  #end

  #local largura_total = ncols *tam_bit.x + (ncols+1)*tam_borda;
  #local altura_total  = nrows*tam_bit.y + 2*tam_borda;

  union { //Ábaco

    difference {

      box {
        <            0,            0, -tam_bit.z/2>
        <largura_total, altura_total, +tam_bit.z/2>
        texture { Cherry_Wood }
      }

      #local col=0;
      #while (col<ncols)
          box {
            <        0,                0, -tam_bit.z>
            <tam_bit.x, nrows*tam_bit.y, +tam_bit.z>
            texture { Cherry_Wood }

            translate <tam_borda + col*(tam_borda+tam_bit.x), tam_borda, 0>
          }
        #local col=col+1;
      #end
    }

    union {
      make_cols( ncols, nrows, numbers, prob_defeito_bit )
      translate <tam_borda, tam_borda, 0>
    }

    translate <-largura_total/2, -altura_total/2, 0>
    scale<-1,1,1>
  }
#end

#declare time      = 10*clock;
#declare time_int  = int(time);
#declare time_frac = time-time_int;

#declare Digits =  array[6] {18+time,27+time,16+time,37+time,0+time,31+time};
object {
  abaco(Digits, 0.05)
  rotate <-90, 0, 0>
}

box{ <-20,-20,-1>, <+20,+20,0> texture{ tx_xadrez } translate <0,0,-3.5> }

#declare camera_ang = pi/4 + pi/3*clock;
#include "camlight.inc"
#declare centro_cena = <-2+2*clock,0,0>;
#declare raio_cena = 12 - 4*clock;
#declare dir_camera = < cos(camera_ang), sin(camera_ang), 0.3*clock>;
#declare dist_camera = 50.0 - 40*clock;
#declare intens_luz = 1.00;
camlight(centro_cena, raio_cena, dir_camera, dist_camera , z, intens_luz)