示例#1
0
文件: smiley.c 项目: heronyang/smiley
/* 
 * Function: setup()
 * Setup the map from the file entered via argv.
 */
void setup(int argc, char *argv[]){

	// Chekc weather the user enter the right command
	if(argc!=2){
		printErrorAndExit(WRONG_USAGE);
	}

	// Scan in the size of the map
	printf("Read file map from: %s\n",argv[1]);
	FILE *fin = fopen(argv[1],"r");
	if(fin == NULL)	printErrorAndExit(FILE_READ_ERROR);
	if(fscanf(fin,"%d%d", &M, &N)!=2)
		printErrorAndExit(FILE_FORMAT_ERROR);

	// Setup Smiley's starting position on the bottom-left
	setSmileyPos();

	// Setup the map and the wall array
	Map = malloc2DArray(M,N);
	RWall = malloc2DArray(M-1,N);
	CWall = malloc2DArray(M,N-1);

	// Scan in the whole map
	readMap(fin);
	readWalls(fin);

	// Print out the map for debugging
	printMap();
	printWalls();

	// Function end here
}
示例#2
0
int populateGrid(dungeon_t* dungeonPtr) {
    if (malloc2DArray((void ***) &dungeonPtr->grid, sizeof(**dungeonPtr->grid), WIDTH, HEIGHT)) {
        return -1;
    }
    for (int y = 1; y < HEIGHT - 1; y++) {
        for (int x = 1; x < WIDTH - 1; x++) {
            dungeonPtr->grid[y][x].material = rock;
            dungeonPtr->grid[y][x].hardness = (uint8_t)(rand() % (ROCK_HARDNESS_MAX - 1) + 1);
            dungeonPtr->grid[y][x].monsterPtr = NULL;
        }
    }
    // Immutable border
    for (int y = 0;  y < HEIGHT; y++) {
        dungeonPtr->grid[y][0].material = rock;
        dungeonPtr->grid[y][0].hardness = ROCK_HARDNESS_IMMUTABLE;
        dungeonPtr->grid[y][0].monsterPtr = NULL;
        dungeonPtr->grid[y][WIDTH - 1].material = rock;
        dungeonPtr->grid[y][WIDTH - 1].hardness = ROCK_HARDNESS_IMMUTABLE;
        dungeonPtr->grid[y][WIDTH - 1].monsterPtr = NULL;
    }
    for (int x = 0; x < WIDTH; x++) {
        dungeonPtr->grid[0][x].material = rock;
        dungeonPtr->grid[0][x].hardness = ROCK_HARDNESS_IMMUTABLE;
        dungeonPtr->grid[0][x].monsterPtr = NULL;
        dungeonPtr->grid[HEIGHT - 1][x].material = rock;
        dungeonPtr->grid[HEIGHT - 1][x].hardness = ROCK_HARDNESS_IMMUTABLE;
        dungeonPtr->grid[HEIGHT - 1][x].monsterPtr = NULL;
    }

    populateRooms(*dungeonPtr);
    return 0;
}
示例#3
0
int loadDungeon(dungeon_t* dungeonPtr, char* fileName) {
    size_t bytesRead;
    char magicBytes[6];
    uint32_t version;
    uint32_t dataLen;
    // malloc2DArray could fail.  Should be checked.
    malloc2DArray((void ***) &dungeonPtr->grid, sizeof(**dungeonPtr->grid), WIDTH, HEIGHT);


    FILE* file = fopen(fileName, "r");
    if (file == NULL) {
        // error opening file
        return -1;
    }

    // read magic bytes
    bytesRead = fread(magicBytes, sizeof(char), 6, file);
    if (bytesRead < 6) {
        // error reading magic bytes
        return -2;
    }
    if (strncmp("RLG327", magicBytes, 6) != 0) {
        // invalid magic bytes
        return -3;
    }

    // read version
    bytesRead = fread(&version, sizeof(version), 1, file);
    if (bytesRead != 1) {
        // Error reading version
        return -4;
    }
    if (version != VERSION) {
        // file version is newer than this program's version
        return -5;
    }
    version = be32toh(version);

    // read data length
    bytesRead = fread(&dataLen, sizeof(dataLen), 1, file);
    if (bytesRead != 1) {
        // error reading data len
        return -6;
    }
    dataLen = be32toh(dataLen);
    if (dataLen < 1482) {
        // data is too short for the grid
        return -7;
    }

    // place the solid border
    for (int x = 0; x < WIDTH; x++) {
        dungeonPtr->grid[0][x].hardness = 255;
        dungeonPtr->grid[0][x].material = rock;
        dungeonPtr->grid[0][x].monsterPtr = NULL;
        dungeonPtr->grid[HEIGHT - 1][x].hardness = 255;
        dungeonPtr->grid[HEIGHT - 1][x].material = rock;
        dungeonPtr->grid[HEIGHT - 1][x].monsterPtr = NULL;
    }
    for (int y = 0; y < HEIGHT; y++) {
        dungeonPtr->grid[y][0].hardness = ROCK_HARDNESS_IMMUTABLE;
        dungeonPtr->grid[y][0].material = rock;
        dungeonPtr->grid[y][0].monsterPtr = NULL;
        dungeonPtr->grid[y][WIDTH - 1].hardness = ROCK_HARDNESS_IMMUTABLE;
        dungeonPtr->grid[y][WIDTH - 1].material = rock;
        dungeonPtr->grid[y][WIDTH - 1].monsterPtr = NULL;
    }

    // read the hardness grid
    for (int y = 0; y < HEIGHT - 2; y++) {
        for (int x = 0; x < WIDTH - 2; x++) {
            bytesRead = fread(&dungeonPtr->grid[y + 1][x + 1].hardness, sizeof((**dungeonPtr->grid).hardness), 1, file);
            if (bytesRead != 1) {
                // error reading hardness data
                return -8;
            }
            if (dungeonPtr->grid[y + 1][x + 1].hardness == 0) {
                // The cell is a corridor or a room.  For now it will be a corridor.
                dungeonPtr->grid[y + 1][x + 1].material = corridor;
            } else {
                dungeonPtr->grid[y + 1][x + 1].material = rock;
            }
            dungeonPtr->grid[y + 1][x + 1].monsterPtr = NULL;
        }
    }

    dataLen -= (HEIGHT - 2) * (WIDTH - 2) + 14;
    if (dataLen % 4 != 0) {
        // invalid data length for rooms
        return -9;
    }

    dungeonPtr->roomCount = dataLen / 4;
    dungeonPtr->rooms = malloc(sizeof(room_t) * dungeonPtr->roomCount);
    for (int r = 0; r < dungeonPtr->roomCount; r++) {
        bytesRead = fread(&dungeonPtr->rooms[r], sizeof(room_t), 1, file);
        if (bytesRead != 1) {
            // failure reading a room
            return -10;
        }
    }

    initMonsters(dungeonPtr);
    turnInit(dungeonPtr);

    return 0;
}