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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*
replay.cpp
Copyright (C) 2005 Poul Sander
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
Poul Sander
R�veh�jvej 36, V. 1111
2800 Kgs. Lyngby
DENMARK
blockattack@poulsander.com
*/
/*
Handles replay
*/
#include <string.h>
#include "replay.h"
Replay::Replay()
{
nrOfFrames=0;
isLoaded = false;
}
Replay::Replay(const Replay& r)
{
bps = r.bps;
finalPack = r.finalPack;
nrOfFrames = r.nrOfFrames;
isLoaded = r.isLoaded;
theResult = r.theResult;
strncpy(name,r.name,sizeof(name));
}
Uint32 Replay::getNumberOfFrames()
{
return nrOfFrames;
}
void Replay::setFrameSecTo(Uint32 miliseconds, boardPackage bp)
{
if (isLoaded)
return;
int framesToSet = (miliseconds*FRAMESPERSEC)/1000;
for (int i=nrOfFrames;i<framesToSet;i++)
{
bps.push_back(bp);
}
nrOfFrames=framesToSet;
}
void Replay::setFinalFrame(boardPackage bp,int theStatus)
{
if (isLoaded)
return;
finalPack = bp;
switch (theStatus)
{
case 1:
theResult = winner;
break;
case 2:
theResult = looser;
break;
case 3:
theResult = draw;
break;
default:
theResult = gameOver;
};
}
//Returns the frame to the current time, if time too high the final frame is returned
boardPackage Replay::getFrameSec(Uint32 miliseconds)
{
int frameToGet = (miliseconds*FRAMESPERSEC)/1000;
if (!(frameToGet<nrOfFrames))
return getFinalFrame();
return bps.at(frameToGet);
}
bool Replay::isFinnished(Uint32 miliseconds)
{
int frameToGet = (miliseconds*FRAMESPERSEC)/1000;
if (!(frameToGet<nrOfFrames))
return true;
return false;
}
boardPackage Replay::getFinalFrame()
{
return finalPack;
}
int Replay::getFinalStatus()
{
return theResult;
}
bool Replay::saveReplay(string filename)
{
//Saving as fileversion 3
cout << "Saving as version 3 save file" << endl;
ofstream saveFile;
saveFile.open(filename.c_str(),ios::binary|ios::trunc);
if (saveFile)
{
Uint32 version = 3;
boardPackage bp;
saveFile.write(reinterpret_cast<char*>(&version),sizeof(Uint32)); //Fileversion
Uint8 nrOfReplays = 1;
saveFile.write(reinterpret_cast<char*>(&nrOfReplays),sizeof(Uint8)); //nrOfReplaysIn File
saveFile.write(reinterpret_cast<char*>(&nrOfFrames),sizeof(Uint32)); //Nr of frames in file
for (int i=0; i<nrOfFrames && i<bps.size();i++)
{ //Writing frames
bp = bps.at(i);
saveFile.write(reinterpret_cast<char*>(&bp),sizeof(bp));
}
saveFile.write(reinterpret_cast<char*>(&finalPack),sizeof(finalPack));
saveFile.write(reinterpret_cast<char*>(&theResult),sizeof(theResult));
saveFile.write(reinterpret_cast<char*>(&name),sizeof(name));
saveFile.close();
return true;
}
else
{
return false;
}
}
bool Replay::saveReplay(string filename,Replay p2)
{
//Saving as fileversion 3
cout << "Saving as version 3 save file (2 players)" << endl;
ofstream saveFile;
saveFile.open(filename.c_str(),ios::binary|ios::trunc);
if (saveFile)
{
Uint32 version = 3;
boardPackage bp;
saveFile.write(reinterpret_cast<char*>(&version),sizeof(Uint32)); //Fileversion
Uint8 nrOfReplays = 2;
saveFile.write(reinterpret_cast<char*>(&nrOfReplays),sizeof(Uint8)); //nrOfReplaysIn File
saveFile.write(reinterpret_cast<char*>(&nrOfFrames),sizeof(Uint32)); //Nr of frames in file
for (int i=0; i<nrOfFrames && i<bps.size();i++)
{ //Writing frames
bp = bps.at(i);
saveFile.write(reinterpret_cast<char*>(&bp),sizeof(bp));
}
saveFile.write(reinterpret_cast<char*>(&finalPack),sizeof(finalPack));
saveFile.write(reinterpret_cast<char*>(&theResult),sizeof(theResult));
saveFile.write(reinterpret_cast<char*>(&name),sizeof(name));
///Player 2 starts here!!!!!!!!!!!!!!!!!!!!!!
saveFile.write(reinterpret_cast<char*>(&p2.nrOfFrames),sizeof(Uint32)); //Nr of frames in file
for (int i=0; (i<p2.nrOfFrames)&& i<p2.bps.size();i++)
{ //Writing frames
bp = p2.bps.at(i);
saveFile.write(reinterpret_cast<char*>(&bp),sizeof(bp));
}
saveFile.write(reinterpret_cast<char*>(&p2.finalPack),sizeof(finalPack));
saveFile.write(reinterpret_cast<char*>(&p2.theResult),sizeof(theResult));
saveFile.write(reinterpret_cast<char*>(&p2.name),sizeof(name));
saveFile.close();
return true;
}
else
{
return false;
}
}
bool Replay::loadReplay(string filename)
{
isLoaded = true;
ifstream loadFile;
loadFile.open(filename.c_str(),ios::binary);
if (loadFile)
{
Uint32 version;
loadFile.read(reinterpret_cast<char*>(&version),sizeof(Uint32));
switch (version)
{
case 3:
cout << "Loading a version 3 save game" << endl;
Uint8 nrOfPlayers;
boardPackage bp;
loadFile.read(reinterpret_cast<char*>(&nrOfPlayers),sizeof(Uint8));
loadFile.read(reinterpret_cast<char*>(&nrOfFrames),sizeof(Uint32));
bps.clear();
for (int i=0; (i<nrOfFrames);i++)
{
loadFile.read(reinterpret_cast<char*>(&bp),sizeof(bp));
bps.push_back(bp);
}
loadFile.read(reinterpret_cast<char*>(&finalPack),sizeof(finalPack));
loadFile.read(reinterpret_cast<char*>(&theResult),sizeof(theResult));
loadFile.read(reinterpret_cast<char*>(&name),sizeof(name));
break;
default:
cout << "Unknown version" << endl;
return false;
};
loadFile.close();
cout << "Loaded 1 player" << endl;
}
else
{
cout << "File not found or couldn't open: " << filename << endl;
return false;
}
}
bool Replay::loadReplay2(string filename)
{
isLoaded = true;
ifstream loadFile;
loadFile.open(filename.c_str(),ios::binary);
if (loadFile)
{
Uint32 version;
loadFile.read(reinterpret_cast<char*>(&version),sizeof(Uint32));
switch (version)
{
case 3:
Uint8 nrOfPlayers;
boardPackage bp;
loadFile.read(reinterpret_cast<char*>(&nrOfPlayers),sizeof(Uint8));
if (nrOfPlayers<2)
{
loadFile.close();
cout << "Only one player in replay" << endl;
return false;
}
cout << "loading player 2" << endl;
loadFile.read(reinterpret_cast<char*>(&nrOfFrames),sizeof(Uint32));
for (int i=0; (i<nrOfFrames);i++)
{
loadFile.read(reinterpret_cast<char*>(&bp),sizeof(bp));
//bps.push_back(bp); We have already read player 1 with another function
}
loadFile.read(reinterpret_cast<char*>(&finalPack),sizeof(finalPack));
loadFile.read(reinterpret_cast<char*>(&theResult),sizeof(theResult));
loadFile.read(reinterpret_cast<char*>(&name),sizeof(name));
loadFile.read(reinterpret_cast<char*>(&nrOfFrames),sizeof(Uint32));
bps.reserve(nrOfFrames);
for (int i=0; (i<nrOfFrames);i++)
{
loadFile.read(reinterpret_cast<char*>(&bp),sizeof(bp));
bps.push_back(bp);
}
loadFile.read(reinterpret_cast<char*>(&finalPack),sizeof(finalPack));
loadFile.read(reinterpret_cast<char*>(&theResult),sizeof(theResult));
loadFile.read(reinterpret_cast<char*>(&name),sizeof(name));
break;
default:
cout << "Unknown version: " << version << endl;
return false;
};
loadFile.close();
}
else
{
cout << "File not found or couldn't open: " << filename << endl;
return false;
}
}
|