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