static void playback_start (char *file) { char buffer[1024]; sprintf (buffer, "recordings/%s", file); playback_free (); if (record_load (buffer)) { playback_stop (); gamestate = GAME_AMODE; return; } /* if (playback_dump ()) { playback_stop (); gamestate = GAME_AMODE; return; } playback_dump2 (buffer); */ frame_counter = 0; rec_buffer_counter = 0; record_state = REC_PLAYING; input_mask = 0; fprintf (stdout, "Starting playback\n"); gamestate = GAME_RUNNING; game_start (); }
struct highscorelist * highscores_free(struct highscorelist * list) { free(list->filename); list->filename = NULL; for(size_t i = 0; i < list->highscores_size; i++) if(list->highscores[i] != NULL) { free(list->highscores[i]->cave); if(list->highscores[i]->playback != NULL) list->highscores[i]->playback = playback_free(list->highscores[i]->playback); free(list->highscores[i]); } free(list->highscores); list->highscores = NULL; list->highscores_size = 0; list->highscores_dirty = 0; return NULL; }
void playback_quit (void) { gamestate = GAME_AMODE; playback_free (); }
static int highscore_add(struct highscorelist * list, time_t timestamp, int score, int diamond_score, uint32_t diamonds_collected, enum GAME_MODE game_mode, char const * const cave, int level, int starting_girls, trait_t traits, bool playback_dirty, int playback_id, struct playback * playback, char * notes) { int position; struct highscore_entry * entry; assert(cave != NULL); position = -1; entry = malloc(sizeof(struct highscore_entry)); assert(entry != NULL); if(entry != NULL) { entry->timestamp = timestamp; entry->score = score; entry->diamond_score = diamond_score; entry->diamonds_collected = diamonds_collected; entry->cave = strdup(cave); entry->starting_girls = starting_girls; entry->traits = traits; entry->level = level; entry->playback_dirty = playback_dirty; entry->playback_id = playback_id; entry->playback = playback; memset(entry->notes, '\0', sizeof entry->notes); if(notes != NULL) snprintf(entry->notes, sizeof entry->notes, "%s", notes); if(list->highscores_size < 999) { /* Upper limit of the list has not yet been reached. */ struct highscore_entry ** tmplist; tmplist = realloc(list->highscores, sizeof(struct highscore_entry *) * (list->highscores_size + 1)); if(tmplist != NULL) { size_t i; size_t j, pos; i = list->highscores_size; list->highscores = tmplist; list->highscores_size++; list->highscores[i] = entry; /* put it in right position */ pos = i; switch(game_mode) { case GAME_MODE_CLASSIC: case GAME_MODE_ADVENTURE: for(j = 0; j < list->highscores_size; j++) if(list->highscores[j]->score < list->highscores[i]->score) { pos = j; break; } break; case GAME_MODE_PYJAMA_PARTY: for(j = 0; j < list->highscores_size; j++) if(list->highscores[j]->timestamp < list->highscores[i]->timestamp) { pos = j; break; } break; } if(pos != i) { struct highscore_entry * tmp; tmp = list->highscores[i]; for(j = list->highscores_size - 1; j > pos; j--) list->highscores[j] = list->highscores[j - 1]; list->highscores[pos] = tmp; } position = pos; } else fprintf(stderr, "Failed to allocate memory for a new highscore entry: %s\n", strerror(errno)); } else { /* Upper limit of the list has been reached. */ size_t j, pos; pos = list->highscores_size; switch(game_mode) { case GAME_MODE_CLASSIC: case GAME_MODE_ADVENTURE: for(j = 0; j < list->highscores_size; j++) if(list->highscores[j]->score < score) { pos = j; break; } break; case GAME_MODE_PYJAMA_PARTY: for(j = 0; j < list->highscores_size; j++) if(list->highscores[j]->timestamp < timestamp) { pos = j; break; } break; } if(pos < list->highscores_size) { if(list->filename != NULL) { assert(list->highscores[list->highscores_size - 1] != NULL); if(list->highscores[list->highscores_size - 1] != NULL) { /* Remove the 999th highscore entry. */ assert(list->total.score >= (uint64_t) list->highscores[list->highscores_size - 1]->score); list->total.score -= list->highscores[list->highscores_size - 1]->score; if(globals.read_only == false) { /* Delete the 999th playback file because it'll be purged soon enough. */ char fn[128]; snprintf(fn, sizeof fn, "%s-%d.dgp", list->filename, list->highscores[list->highscores_size - 1]->playback_id); unlink(get_save_filename(fn)); } /* Free the 999th highscore entry. */ if(list->highscores[list->highscores_size - 1]->playback != NULL) list->highscores[list->highscores_size - 1]->playback = playback_free(list->highscores[list->highscores_size - 1]->playback); free(list->highscores[list->highscores_size - 1]); list->highscores[list->highscores_size - 1] = NULL; } } /* Move the highscores to make room for this new. */ for(j = list->highscores_size - 1; j > pos; j--) list->highscores[j] = list->highscores[j - 1]; list->highscores[pos] = entry; position = pos; } } if(position < 0) { /* Cleanup upon error. */ free(entry->cave); free(entry); } } else fprintf(stderr, "Failed to allocate memory for a new highscore entry: %s\n", strerror(errno)); if(position >= 0) list->total.score += score; return position; }