Ejemplo n.º 1
0
static void run() {
	solutions = alist_new();
	
	gettimeofday(&startTime, NULL);
	
	gamestate_t *gs = gamestate_new(5, coord_new(3, 2));
	search(gs, alist_new());
	
	gettimeofday(&endTime, NULL);
	
	printf("Games played:    %6ld\n", gamesPlayed);
	printf("Solutions found: %6d\n", solutions->size);
	printf("Time elapsed:    %6ldms\n", diff_usec(startTime, endTime) / 1000);
}
Ejemplo n.º 2
0
Archivo: memory.c Proyecto: rdebath/sgt
gamestate *gamestate_copy(gamestate *state) {
    gamestate *ret;

    ret = gamestate_new(state->width, state->height, state->flags);
    ret->status = state->status;
    memcpy(ret->leveldata, state->leveldata, ret->width * ret->height);
    ret->player_x = state->player_x;
    ret->player_y = state->player_y;
    ret->gold_got = state->gold_got;
    ret->gold_total = state->gold_total;
    ret->levnum = state->levnum;
    ret->title = state->title;
    ret->movenum = state->movenum;
    ret->sequence = smalloc(state->sequence_size);
    memcpy(ret->sequence, state->sequence, state->movenum);
    ret->sequence_size = state->sequence_size;

    return ret;
}
Ejemplo n.º 3
0
Archivo: engine.c Proyecto: rdebath/sgt
/*
 * Set up a game_state from a level.
 */
gamestate *init_game (level *lev) {
    gamestate *ret;
    int i, j;

    ret = gamestate_new(lev->width, lev->height, lev->flags);
    memcpy(ret->leveldata, lev->leveldata, lev->width * lev->height);
    ret->title = lev->title;
    ret->movenum = 0;
    ret->gold_got = 0;
    ret->status = PLAYING;
    ret->gold_total = 0;
    for (j = 0; j < ret->height; j++) {
	for (i = 0; i < ret->width; i++) {
	    if (ret->leveldata[j*ret->width+i] == '$')
		ret->gold_total++;
	    if (ret->leveldata[j*ret->width+i] == '@') {
		ret->player_x = i;
		ret->player_y = j;
	    }
	}
    }
    return ret;
}
Ejemplo n.º 4
0
gamestate *savepos_load(levelset *set, char *user, int savenum) {
    FILE *fp;
    char buf[FILENAME_MAX+10];
    char fname[FILENAME_MAX];
    gamestate *state;
    int nlines, levnum;
    int i, j;

    fname[sizeof(fname)-1] = '\0';
    strncpy(fname, SAVEDIR, sizeof(fname));
    strncpy(fname + strlen(fname), set->name, sizeof(fname)-strlen(fname));
    strncpy(fname + strlen(fname), ".", sizeof(fname)-strlen(fname));
    strncpy(fname + strlen(fname), user, sizeof(fname)-strlen(fname));
    sprintf(buf, ".%d", savenum+1);
    strncpy(fname + strlen(fname), buf, sizeof(fname)-strlen(fname));
    if (fname[sizeof(fname)-1] != '\0') {
	/* filename length overflow */
	return NULL;
    }

    fp = fopen(fname, "r");
    if (!fp) {
	/* can't open save file */
	return NULL;
    }

    state = NULL;
    nlines = 0;

    while (fgets(buf, sizeof(buf), fp)) {
	if (buf[strlen(buf)-1] != '\n') {
	    /* line length overflow in save file */
	    return NULL;
	}
	buf[strcspn(buf, "\r\n")] = '\0';
	if (state == NULL && !ishdr(buf, "Level: ")) {
	    /* Level: line not first in save file */
	    return NULL;
	}
	if (ishdr(buf, "Level: ")) {
	    levnum = atoi(buf+7);
	    state = gamestate_new(set->levels[levnum-1]->width,
				  set->levels[levnum-1]->height);
	    state->levnum = levnum;
	    state->title = set->levels[levnum-1]->title;
	    state->status = PLAYING;
	} else if (ishdr(buf, "Moves: ")) {
	    state->movenum = atoi(buf + 7);
	} else if (ishdr(buf, "Gold: ")) {
	    state->gold_got = atoi(buf + 6);
	} else if (ishdr(buf, "TotalGold: ")) {
	    state->gold_total = atoi(buf + 11);
	} else if (ishdr(buf, "Map: ")) {
	    if (state->leveldata == NULL) {
		/* map before size in save file */
		return NULL;
	    }
	    if ((int)strlen(buf + 5) != state->width) {
		/* wrong length map line in save file */
		return NULL;
	    }
	    if (nlines >= state->height) {
		/* too many map lines in save file */
		return NULL;
	    }
	    memcpy(state->leveldata + state->width * nlines,
		   buf + 5, state->width);
	    nlines++;
	} else {
	    /* unrecognised keyword in save file */
	    return NULL;
	}
    }
    if (nlines < state->height) {
	/* not enough map lines in save file */
	return NULL;
    }

    fclose(fp);

    /*
     * Find the player.
     */
    for (j = 0; j < state->height; j++) {
	for (i = 0; i < state->width; i++) {
	    if (state->leveldata[j*state->width+i] == '@') {
		state->player_x = i;
		state->player_y = j;
	    }
	}
    }

    return state;
}