diff -rNu -X diff_ignore freeciv-1.12.0/server/savegame.c freeciv-scorelog/server/savegame.c --- freeciv-1.12.0/server/savegame.c Wed May 23 14:21:29 2001 +++ freeciv-scorelog/server/savegame.c Tue Aug 14 17:00:11 2001 @@ -30,6 +30,7 @@ #include "mem.h" #include "rand.h" #include "registry.h" +#include "scorelog.h" #include "shared.h" #include "support.h" #include "unit.h" @@ -1699,6 +1700,144 @@ } } +static void civscore_save_year(struct year_log *year, struct section_file *file) { + int i = 0; + int cplayer = 0, ctag = 0; + int curr_year = 0; + + if(!year) + return; + + curr_year = year->year; + for(i = 0, cplayer = 0;i < game.nplayers;i++) { + if(is_barbarian(&(game.players[i]))) + continue; + for(ctag = 0;ctag < num_tags;ctag++) { + secfile_insert_int(file, year->values[ctag][cplayer], + "log.y%dp%d.%s", curr_year, cplayer, tags[ctag]); + } + cplayer++; + } +} + +/***************************************************************** + Save everything we've got so far in the civscore_log data struct. + The first thing we do is save a string of the current years. + This way, when we load the game back up we can re-initialize the + civscore_log. +*****************************************************************/ +static void civscore_save(struct civscore *c_log, struct section_file *file) { + int i = 0; + int buf_pos = 0, year_len = 0; + int buf_size = 0; + int num_turns; + char * year_buf = NULL; + + /* Don't bother if we don't have any data to save */ + if(!c_log || !c_log->data || (c_log->num_turns == 0)) { + return; + } + + num_turns = c_log->num_turns; + secfile_insert_int(file, num_turns, "log.num_turns"); + + /* Assemble the list of turns. We do 6x the number of turns so there's + enough space for all the year numbers. */ + buf_size = (6 * num_turns) + 1; + year_buf = fc_malloc(buf_size); + year_buf[0] = '\0'; + + /* We don't do the last one in the loop because we don't want + to put a comma after it. */ + for(i = 0;i < (num_turns - 1);i++) { + year_len = my_snprintf(year_buf + buf_pos, buf_size - buf_pos, "%d ", + c_log->data[i].year); + buf_pos += year_len; + } + my_snprintf(year_buf + buf_pos, buf_size - buf_pos, "%d", + c_log->data[num_turns - 1].year); + secfile_insert_str(file, year_buf, "log.turn_labels"); + free(year_buf); + + /* Now put the actual year data in. Each year is a tabular entry in the file. + -4000,0 gets information about player 0 on year -4000, for example. */ + for(i = 0;i < c_log->num_turns;i++) { + civscore_save_year(&(c_log->data[i]), file); + } +} + +/*************************************************************** + Load data from the log section of the scorefile. This involves + some fun string parsing to make sure we get all the data from + all the tags. + NOTE: Need to add error handling!! +***************************************************************/ +static void civscore_load(struct section_file *file) { + char *turn_labels; + char *curr_p, *end_p; + + int i = 0, j = 0; + int num_players = 0; + int cplayer, ctag; + int num_turns = 0; + int *turn_years = NULL; + struct year_log tmp_year; + + /* At this point game_init() should have created a civscore_log + in the game struct. It won't (shouldn't?) have any data + in it, but if it does, clean it out. */ + if(game.civscore_log && game.civscore_log->data) { + destroy_scorelog(game.civscore_log); + } + /* At this point, we have an allocate game.civscore_log, but + no actual data in it. Time to fill it up. */ + + /* Is there even a log section in this game? */ + if(!section_file_lookup(file, "log.num_turns")) { + return; + } + + num_turns = secfile_lookup_int(file, "log.num_turns"); + if(num_turns <= 0) { + return; + } + + turn_labels = secfile_lookup_str(file, "log.turn_labels"); + turn_years = (int *)fc_malloc(num_turns * sizeof(int *)); + + /* We iterate through the labels using strtol to get the next value. + Since the years are separated by a space, we can set curr_p to + the next "invalid" character for each year. */ + for(i = 0, curr_p = turn_labels;i < num_turns;i++) { + turn_years[i] = (int)strtol(curr_p, &end_p, 10); + curr_p = end_p; + } + + /* How many non-barbarian players do we have? */ + for(i = 0;i < game.nplayers;i++) { + if(is_barbarian(&(game.players[i]))) + continue; + num_players++; + } + + /* We go through and create a temp log, and then tack that onto the + end of the game log. */ + for(i = 0;i < num_turns;i++) { + tmp_year.year = turn_years[i]; + tmp_year.values = (int **)fc_malloc(num_tags * sizeof(int **)); + for(ctag = 0;ctag < num_tags;ctag++) { + tmp_year.values[ctag] = (int *)fc_malloc(num_players * sizeof(int *)); + for(j = 0, cplayer = 0;j < game.nplayers;j++) { + if(is_barbarian(&(game.players[j]))) + continue; + tmp_year.values[ctag][cplayer] = secfile_lookup_int(file, "log.y%dp%d.%s", + turn_years[i], cplayer, tags[ctag]); + cplayer++; + } + } + scorelog_copy_year(game.civscore_log, &tmp_year); + } +} /*************************************************************** Assign values to ord_city and ord_map for each unit, so the @@ -2121,6 +2260,11 @@ game.player_idx=0; game.player_ptr=&game.players[0]; + /* Load the log */ + if(game.scorelog & 0x2) { + civscore_load(file); + } + return; } @@ -2288,4 +2432,8 @@ player_save(&game.players[i], i, file); } } + + /* Tack the scorelog onto the end of the save game file */ + if(game.scorelog & 0x2) + civscore_save(game.civscore_log, file); }