-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadsave.c
151 lines (119 loc) · 3.38 KB
/
loadsave.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Race for the Galaxy AI
*
* Copyright (C) 2009-2011 Keldon Jones
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "rftg.h"
/*
* Load a game from the given filename.
*/
int load_game(game *g, char *filename)
{
FILE *fff;
player *p_ptr;
char buf[1024];
char version[1024];
int i, j;
/* Open file for reading */
fff = fopen(filename, "r");
/* Check for failure */
if (!fff) return -1;
/* Read header */
fgets(buf, 1024, fff);
/* Check for correct header */
if (strcmp(buf, "RFTG Save\n")) return -1;
/* Read version line */
fgets(version, 1024, fff);
/* Strip newline from version */
version[strlen(version) - 1] = '\0';
/* Check for too new version */
if (strcmp(version, VERSION) > 0) return -1;
/* Check for too old version */
if (strcmp(version, "0.7.2") < 0) return -1;
/* Read random seed information */
fscanf(fff, "%u\n", &g->start_seed);
/* Read game setup information */
fscanf(fff, "%d %d\n", &g->num_players, &g->expanded);
fscanf(fff, "%d %d %d\n", &g->advanced, &g->goal_disabled,
&g->takeover_disabled);
/* Clear simulation flag */
g->simulation = 0;
/* Load over players */
for (i = 0; i < g->num_players; i++)
{
/* Get player pointer */
p_ptr = &g->p[i];
/* Read choice log size */
fscanf(fff, "%d", &p_ptr->choice_size);
/* Loop over choice log entries */
for (j = 0; j < p_ptr->choice_size; j++)
{
/* Read choice log entry */
fscanf(fff, "%d", &p_ptr->choice_log[j]);
}
/* Reset choice log position */
p_ptr->choice_pos = 0;
}
/* Close file */
fclose(fff);
/* Success */
return 0;
}
/*
* Save a game to the given filename.
*/
int save_game(game *g, char *filename, int player_us)
{
FILE *fff;
player *p_ptr;
int i, j, n;
/* Open file for writing */
fff = fopen(filename, "w");
/* Check for failure */
if (!fff) return -1;
/* Write header information */
fputs("RFTG Save\n", fff);
fprintf(fff, "%s\n", VERSION);
/* Write start of game random seed */
fprintf(fff, "%u\n", g->start_seed);
/* Write game setup information */
fprintf(fff, "%d %d\n", g->num_players, g->expanded);
fprintf(fff, "%d %d %d\n", g->advanced, g->goal_disabled,
g->takeover_disabled);
/* Loop over players */
for (i = 0; i < g->num_players; i++)
{
/* Get player index to save next */
n = (player_us + i) % g->num_players;
/* Get player pointer */
p_ptr = &g->p[n];
/* Write size of choice log */
fprintf(fff, "%d ", p_ptr->choice_size);
/* Loop over choice log entries */
for (j = 0; j < p_ptr->choice_size; j++)
{
/* Write choice log entry */
fprintf(fff, "%d ", p_ptr->choice_log[j]);
}
/* Finish line */
fprintf(fff, "\n");
}
/* Close file */
fclose(fff);
/* Success */
return 0;
}