void emMinesFileModel::OpenField(int x, int y, int z, bool saveFile)
{
	int x1,y1,z1,x2,y2,z2,f;

	f=GetField(x,y,z);
	if ((f&2)!=0) return;
	f|=2;
	f&=~4;
	SetField(x,y,z,f);
	if (GetSurroundings(x,y,z)==0) {
		z1=z-1; if (z1<0) z1=0;
		z2=z+2; if (z2>SizeZ) z2=SizeZ;
		y1=y-1; if (y1<0) y1=0;
		y2=y+2; if (y2>SizeY) y2=SizeY;
		x1=x-1; if (x1<0) x1=0;
		x2=x+2; if (x2>SizeX) x2=SizeX;
		for (z=z1; z<z2; z++) {
			for (y=y1; y<y2; y++) {
				for (x=x1; x<x2; x++) {
					OpenField(x,y,z,false);
				}
			}
		}
	}
	if (saveFile) Save(true);
}
void emMinesFileModel::StartGame(
	int sizeX, int sizeY, int sizeZ, int mineCount, bool saveFile
)
{
	int i,j,k,x,y,fcnt;

	ExtraDataValid=false;
	SizeX=sizeX;
	SizeY=sizeY;
	SizeZ=sizeZ;

	// Because they may have been clipped by the above assignments.
	sizeX=SizeX;
	sizeY=SizeY;
	sizeZ=SizeZ;

	fcnt=sizeX*sizeY*sizeZ;
	Fields.SetCount(fcnt);
	for (i=0; i<fcnt; i++) Fields[i].Set(0);

	if (mineCount>fcnt) mineCount=fcnt;
	for (i=0; i<mineCount; i++) {
		j=emGetIntRandom(0,fcnt-i-1);
		for (k=0; ; k++) {
			if (Fields[k].Get()==0) {
				j--;
				if (j<0) {
					Fields[k].Set(1);
					break;
				}
			}
		}
	}
	if (SizeZ>1) {
		for (y=0; y<SizeY; y++) for (x=0; x<SizeX; x++) {
			if (IsMine(x,y,0)) SetMark(x,y,0,false);
			else OpenField(x,y,0,false);
		}
	}
	if (saveFile) Save(true);
}
Beispiel #3
0
/* Initialise dirfile */
void InitialiseDirFile(int reset, unsigned long offset)
{
  dtrace("%i, %lu", reset, offset);
  FILE* fp;
  int fd;
  int j, i, is_bolo = 0;
#if DAS_CARDS > 0
  char field[FIELD_LEN];
  int bolo_node;
  char first_bolo_buf[16];
#endif
  char gpb[GPB_LEN];
  char ext[4] = "";

  fc = 0;

#ifdef HAVE_LIBZ
  if (rc.gzip_output) {
    defileclose = &gzclose;
    strcpy(ext, ".gz");
  } else
#endif
    defileclose = &close;

  rc.resume_at = -1;

  if (mkdir(rc.dirfile, 00755) < 0)
    if (!CheckWriteAllow(errno))
      berror(fatal, "cannot create dirfile `%s'", rc.dirfile);

  bprintf(info, "\nWriting to dirfile `%s'\n", rc.dirfile);

  rc.dirname = strdup(rc.dirfile);
  snprintf(rc.dirname, strlen(rc.dirfile), "%s", basename(rc.dirfile));

  for (i = 0; i < FAST_PER_SLOW + 10; ++i) {
    pre_buffer[i] = balloc(fatal, DiskFrameSize);
    pre_buffer[i][3] = i;
  }

  /* Reset the PreBuffer */
  PreBuffer(NULL);

  for (i = 0; i < FAST_PER_SLOW; ++i)
    fast_frame[i] = balloc(fatal, DiskFrameSize);

  /***********************************
   * create and fill the format file *
   ***********************************/
  sprintf(gpb, "%s/format", rc.dirfile);

  if ((fd = creat(gpb, 0666)) < 0)
    berror(fatal, "cannot create format file `%s/format'", rc.dirfile);

  PathSplit_r(rc.dirfile, NULL, gpb);

  WriteFormatFile(fd, atoi(gpb), offset / FAST_PER_SLOW);

#ifdef __SPIDER__
  if (rc.extra_format) {
    sprintf(gpb, "/INCLUDE /data/etc/spider/format.mce_mplex\n"
        "/INCLUDE /data/etc/spider/format.bolo_stats\n");
    if (write(fd, gpb, strlen(gpb)) < 0)
      berror(err, "Error writing to format file\n");
  }
#else
  /* no extra format for BLAST */
#endif

  if (close(fd) < 0)
    berror(fatal, "Error while closing format file");

  /* DEFILE_FLAGS */
  sprintf(gpb, "%s/DEFILE_FLAGS%s", rc.dirfile, ext);
  defile_field.size = 1;
  defile_field.fp = OpenField(1, 1, gpb);
  defile_field.i0 = 1;
  if (reset) {
    if (rc.resume_at > 0)
      defile_field.nw = rc.resume_at;
    else
      defile_field.nw = 0;
  }
  defile_field.i_in = defile_field.i_out = 0;
  defile_field.b = balloc(fatal, MAXBUF * sizeof(unsigned short));

  /* FASTSAMP */
  sprintf(gpb, "%s/FASTSAMP%s", rc.dirfile, ext);
  normal_fast[0].size = 2;

  normal_fast[0].fp = OpenField(1, 2, gpb);
  normal_fast[0].i0 = 1;
  if (reset) {
    if (rc.resume_at > 0)
      normal_fast[0].nw = rc.resume_at;
    else
      normal_fast[0].nw = 0;
  }

  normal_fast[0].i_in = normal_fast[0].i_out = 0;
  normal_fast[0].b = balloc(fatal, MAXBUF * sizeof(unsigned int));

  n_fast = 1;  //original form

  /* slow chs */
  for (i = 0; i < slowsPerBi0Frame; i++) {
    for (j = 0; j < FAST_PER_SLOW; j++) {
      if (strlen(SlowChList[i][j].field) > 0) {
        slow_fields[j][i].size = FieldSize(SlowChList[i][j].type,
            SlowChList[i][j].field);

        sprintf(gpb, "%s/%s%s", rc.dirfile, SlowChList[i][j].field, ext);
        slow_fields[j][i].fp = OpenField(0, slow_fields[j][i].size, gpb);
      } else
        slow_fields[j][i].fp = -1;

      slow_fields[j][i].i_in = slow_fields[j][i].i_out = 0;
      if (reset) {
        if (rc.resume_at > 0)
          slow_fields[j][i].nw = rc.resume_at / FAST_PER_SLOW;
        else
          slow_fields[j][i].nw = 0;
      }

      slow_fields[j][i].b = balloc(fatal, 2 * MAXBUF);

      slow_fields[j][i].i0 = SLOW_OFFSET + i;
    }
  }

  /* normal fast chs */

#if DAS_CARDS > 0
  sprintf(first_bolo_buf, "n%02dc00lo", DAS_START+1);
#endif
  for (i = 0; i < ccFast + ccWideFast; i++) {
#if DAS_CARDS > 0
    if (strcmp(FastChList[i].field, first_bolo_buf) == 0) {
      bolo_i0 = i + SLOW_OFFSET + slowsPerBi0Frame;
      is_bolo = 1;
    } else if (ccDecom > 0 && strcmp(FastChList[i].field,
          DecomChannels[0].field) == 0) {
      is_bolo = 0;
    }
#endif

    if (!is_bolo && strlen(FastChList[i].field) > 0) {
      normal_fast[n_fast].size = FieldSize(FastChList[i].type,
          FastChList[i].field);
      sprintf(gpb, "%s/%s%s", rc.dirfile, FastChList[i].field, ext);

      normal_fast[n_fast].fp = OpenField(1, normal_fast[n_fast].size, gpb);
      normal_fast[n_fast].i0 = i + SLOW_OFFSET + slowsPerBi0Frame;
      normal_fast[n_fast].i_in = normal_fast[n_fast].i_out = 0;
      if (reset) {
        if (rc.resume_at > 0)
          normal_fast[n_fast].nw = rc.resume_at;
        else
          normal_fast[n_fast].nw = 0;
      }

      normal_fast[n_fast].b = balloc(fatal, MAXBUF * 2 *
          normal_fast[n_fast].size);

      n_fast++;
    }
  }

#if DAS_CARDS > 0
  /* special (bolo) fast chs */
  bolo_node = DAS_START;
  for (i = 0; i < DAS_CARDS; i++) {
    bolo_node++;
    if (bolo_node%4 == 0) bolo_node++;
    for (j = 0; j < DAS_CHS; j++) {
      bolo_fields[i][j].size = 2;
      sprintf(field, "n%02dc%02d", bolo_node, j);
      sprintf(gpb, "%s/%s%s", rc.dirfile, field, ext);

      bolo_fields[i][j].fp = OpenField(1, 2, gpb);
      bolo_fields[i][j].i_in = bolo_fields[i][j].i_out = 0;

      if (reset) {
        if (rc.resume_at > 0)
          bolo_fields[i][j].nw = rc.resume_at;
        else
          bolo_fields[i][j].nw = 0;
      }

      bolo_fields[i][j].b = balloc(fatal, MAXBUF * 4);

      bolo_fields[i][j].i0 = bolo_i0 + i * (DAS_CARDS * 3 / 2) + j;
    }
  }
#endif

  if (rc.write_curfile) {
    // create the link file
    char lnkfile[1024];
    char curfile[1024];

    strncpy(curfile, rc.output_curfile, 1018);
    strcat(curfile, ".cur");

    strncpy(lnkfile, rc.output_curfile, 1018);
    strcat(lnkfile, ".lnk");
    unlink(lnkfile);
    if (symlink(rc.dirfile, lnkfile)<0) {
      berror(fatal, "could not create link from `%s' to `%s'",
             rc.dirfile, lnkfile);
    }
    // create the cur file
    if ((fp = fopen(curfile, "w")) == NULL)
      berror(fatal, "cannot create curfile `%s'", curfile);

    fprintf(fp, "%s\n", rc.dirfile);

    if (fclose(fp) < 0)
      berror(fatal, "cannot close curfile `%s'", curfile);
  }

  ri.dirfile_init = 1;
}