void loadWorld(FILE* file){
  char* buf = NULL;
  char type;
  int x, y, i;
  size_t len;
  cell_t* cell;

  //init world array
  getline(&buf, &len, file);
  sscanf(buf, "%d", &worldSideLen);
  worldSize = worldSideLen * worldSideLen;
  world = (cell_t*)(malloc(worldSize * sizeof(cell_t)));

  //clear
  for(i = 0 ; i < worldSize ; i++){
    world[i].type = EMPTY;
    world[i].starvation = 0;
    world[i].breeding = 0;
    world[i].updateSize = 0;
  }

  //init cells
  while(getline(&buf, &len, file) != -1){
    sscanf(buf, "%d %d %c", &y, &x, &type);
    cell = getCell(x, y);
    cell->type = charToCellType(type);
    cell->breeding = 0;
    cell->starvation = wolfStarvationPeriod;
  }
}
예제 #2
0
void Field::init(const std::string& ASCIIMap) {
	xSize = ySize = 0;
	robotKilled = false;

	// Calculating map size
	int maxX = 0;
	int maxY = 0;
	int currX = 0;
	int currY = 0;
	bool bEndLine = false;
	int endLine = 0;
	bool containsRobot = false;
	bool containsLift = false;
	unsigned int lastn = 0;

	for (unsigned int i = 0; i < ASCIIMap.length(); i++) {
		switch (ASCIIMap[i]) {
		case '\n':
			if (currX == 0) {
				if (!bEndLine) {
					LOGINFO("Map parse: *Skipped after %d line", currY);
					bEndLine = true;
					endLine = currY;
					break;
				}
			} else if (bEndLine) {
				LOGERROR("Map parse: Empty string at %d line", endLine);
				throw FieldParseException();
			}
			if (maxX < currX)
				maxX = currX;
			currX = 0;
			currY++;
			lastn = i;
			break;
		case 'R':
			if (!containsRobot) {
				containsRobot = true;
				currX++;
			} else {
				LOGERROR("Map parse: map can't contain more than one robot");
				throw FieldParseException();
			}
			break;
		case 'L':
			if (!containsLift) {
				containsLift = true;
				currX++;
			} else {
				LOGERROR("Map parse: map can't contain more than one closed lift");
				throw FieldParseException();
			}
			break;
		case '*':
		case '\\':
		case '#':
		case '.':
		case ' ':
			currX++;
			break;
		case '\r':
			break;
		case 'O':
			LOGERROR("Map parse: map can't contain opened lifts");
			throw FieldParseException();
		default:
			LOGWARNING("Map parse: Unknown character \'%c\' at %d,%d.", ASCIIMap[i], currX, currY);
			throw FieldParseException();
		}
	}
	if ((!containsRobot) || (!containsLift)) {
		LOGERROR("Map parse: not all required objects exist");
		throw FieldParseException();
	}
	if (lastn < (ASCIIMap.length() - 1))
		currY++;
	if (maxX < currX)
		maxX = currX;
	maxY = currY;

	LOGINFO("Map parse: Estimated map size: %d,%d", maxX, maxY);
	xSize = maxX;
	ySize = maxY;
	field = new char*[ySize];
	for (unsigned int i = 0; i < ySize; i++) {
		field[i] = new char[xSize];
		std::fill_n(field[i], xSize, EMPTY);
	}

	currX = 0;
	currY = 0;
	for (unsigned int i = 0; i < ASCIIMap.length(); i++) {
		switch (ASCIIMap[i]) {
		case '\n':
			currX = 0;
			currY++;
			break;
		case '\\':
			lambdaCache.push_back(new Point(currX, currY));
			field[currY][currX] = charToCellType(ASCIIMap[i]);
			currX++;
			break;
		case '*':
			stoneCache.push_back(new Point(currX, currY));
			field[currY][currX] = charToCellType(ASCIIMap[i]);
			currX++;
			break;
		case 'R':
			robot = new Point(currX, currY);
			field[currY][currX] = charToCellType(ASCIIMap[i]);
			currX++;
			break;
		case 'L':
			lift = new Point(currX, currY);
			field[currY][currX] = charToCellType(ASCIIMap[i]);
			currX++;
			break;
		default:
			field[currY][currX] = charToCellType(ASCIIMap[i]);
			currX++;
			break;
		}
	}

	LOGINFO("Map successfully parsed");
}