]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CReplayData.cpp
Coalesce printf() statements in main.cpp, make them translatable.
[quix0rs-blobwars.git] / src / CReplayData.cpp
1 #include "headers.h"
2 #include <time.h>
3
4 ReplayData::ReplayData()
5 {
6         fp = NULL;
7
8         endOfReplay = false;
9         
10         fast = false;
11         
12         header.randomSeed = time(NULL);
13         header.version = VERSION;
14         header.release = RELEASE;
15         
16         filename[0] = 0;
17 }
18
19 ReplayData::~ReplayData()
20 {
21         if (replayMode == REPLAY_MODE::RECORD)
22         {
23                 save();
24                 rewind(fp);
25                 int size = fwrite(&header, sizeof(ReplayDataHeader), 1, fp);
26                 if (size != 1)
27                 {
28                         printf("Error saving replay data: %s\n", strerror(errno));
29                         exit(1);
30                 }
31         }
32         
33         if (replayMode != REPLAY_MODE::NONE)
34         {
35                 fclose(fp);
36         }
37 }
38
39 void ReplayData::printReplayInformation()
40 {
41         tm *timeinfo = localtime(&header.randomSeed);
42         printf("Recorded on : %s", asctime(timeinfo));
43         printf("Map         : %s\n", header.map);
44         printf("Score       : %d\n", header.score);
45         printf("Time        : %s\n", Math::formatTime(header.time));
46 }
47
48 void ReplayData::swapHeaderEndians()
49 {
50         #if UNIX
51         header.randomSeed       = SDL_SwapLE32(header.randomSeed);
52         header.version          = SDL_SwapLE32(header.version);
53         header.release          = SDL_SwapLE32(header.release);
54         header.skill            = SDL_SwapLE32(header.skill);
55         header.score            = SDL_SwapLE32(header.score);
56         #endif
57 }
58
59 void ReplayData::setMode(REPLAY_MODE::TYPE replayMode)
60 {
61         this->replayMode = replayMode;
62
63         if (replayMode == REPLAY_MODE::PLAYBACK)
64         {
65                 fp = fopen(filename, "rb");
66                 
67                 if (!fp)
68                 {
69                         printf("ERROR: Replay file '%s' could not be loaded.\n", filename);
70                         replayMode = REPLAY_MODE::NONE;
71                         return;
72                 }
73                 
74                 if (fread(&header, sizeof(ReplayDataHeader), 1, fp) != 1)
75                 {
76                         printf("ERROR: Replay file '%s' is corrupt\n", filename);
77                         replayMode = REPLAY_MODE::NONE;
78                         fclose(fp);
79                         return;
80                 }
81                 
82                 swapHeaderEndians();
83                 
84                 printf("\n==== REPLAY HEADER DATA ====\n");
85                 printReplayInformation();
86                 if ((header.version != VERSION) && (header.release != RELEASE))
87                 {
88                         printf("\nWARNING: Replay is from a different version (%f %d) and may not play back correctly\n", header.version, header.release);
89                 }
90                 printf("Press F5 to toggle Fast Playback\n");
91                 load();
92         }
93         else if (replayMode == REPLAY_MODE::RECORD)
94         {
95                 fp = fopen(filename, "wba");
96                 
97                 if (!fp)
98                 {
99                         printf("ERROR: Replay file '%s' could not be opened for writing.\n", filename);
100                         replayMode = REPLAY_MODE::NONE;
101                         return;
102                 }
103                 
104                 swapHeaderEndians();
105                 
106                 int size = fwrite(&header, sizeof(ReplayDataHeader), 1, fp);
107                 if (size != 1)
108                 {
109                         printf("Error writing replay data header: %s\n", strerror(errno));
110                         replayMode = REPLAY_MODE::NONE;
111                         fclose(fp);
112                         fp = NULL;
113                         return;
114                 }
115                 
116                 reset();
117         }
118 }
119
120 void ReplayData::reset()
121 {
122         index = 0;
123                 
124         for (int i = 0 ; i < DATA_LENGTH ; i++)
125         {
126                 for (int j = 0 ; j < CONTROL::MAX ; j++)
127                 {
128                         data[i][j] = -1;
129                 }
130         }
131         
132         for (int i = 0 ; i < CONTROL::MAX ; i++)
133         {
134                 data[0][i] = 0;
135         }
136 }
137
138 void ReplayData::read(int *c)
139 {
140         if (replayMode != REPLAY_MODE::PLAYBACK)
141         {
142                 return;
143         }
144         
145         if (endOfReplay)
146         {
147                 for (int i = 0 ; i < CONTROL::MAX ; i++)
148                 {
149                         c[i] = 0;
150                 }
151                 
152                 return;
153         }
154
155         for (int i = 0 ; i < CONTROL::MAX ; i++)
156         {
157                 c[i] = data[index][i];
158         }
159         
160         c[CONTROL::MAP] = 0;
161         c[CONTROL::PAUSE] = 0;
162 }
163
164 void ReplayData::set(int *c)
165 {
166         if (replayMode != REPLAY_MODE::RECORD)
167         {
168                 return;
169         }
170
171         for (int i = 0 ; i < CONTROL::MAX ; i++)
172         {
173                 data[index][i] = c[i];
174         }
175 }
176
177 void ReplayData::commit()
178 {
179         if ((endOfReplay) || (replayMode == REPLAY_MODE::NONE))
180         {
181                 return;
182         }
183         
184         index++;
185         
186         if (index < DATA_LENGTH)
187         {
188                 if (replayMode == REPLAY_MODE::PLAYBACK)
189                 {
190                         if (data[index][0] == -1)
191                         {
192                                 printf("===== END OF REPLAY =====\n");
193                                 endOfReplay = true;
194                                 exit(0);
195                                 return;
196                         }
197                 }
198                 else
199                 {
200                         for (int i = 0 ; i < CONTROL::MAX ; i++)
201                         {
202                                 data[index][i] = 0;
203                         }
204                 }
205         }
206         
207         if (index >= DATA_LENGTH)
208         {
209                 if (replayMode == REPLAY_MODE::PLAYBACK)
210                 {
211                         load();
212                 }
213                 else if (replayMode == REPLAY_MODE::RECORD)
214                 {
215                         save();
216                 }
217                 
218                 index = 0;
219         }
220 }
221
222 void ReplayData::load()
223 {
224         if (endOfReplay)
225         {
226                 return;
227         }
228
229         debug(("ReplayData::load()\n"));
230         
231         int size = fread(data, 1, DATA_LENGTH * CONTROL::MAX, fp);
232         
233         if (size != DATA_LENGTH * CONTROL::MAX)
234         {
235                 printf("Error reading replay data\n");
236                 exit(1);
237         }
238         
239         debug(("ReplayData::load() - Done (%d)\n", size));
240 }
241
242 void ReplayData::save()
243 {
244         debug(("ReplayData::save()\n"));
245         
246         int size = fwrite(data, 1, DATA_LENGTH * CONTROL::MAX, fp);
247         
248         if (size != DATA_LENGTH * CONTROL::MAX)
249         {
250                 printf("Error saving replay data\n");
251                 exit(1);
252         }
253         
254         debug(("ReplayData::save() - Done (%d)\n", size));
255         
256         reset();
257 }