diff --git a/source/code/BlockGame.cpp b/source/code/BlockGame.cpp index 72d7635..6c4eff0 100644 --- a/source/code/BlockGame.cpp +++ b/source/code/BlockGame.cpp @@ -1782,7 +1782,15 @@ void BlockGame::UpdateInternal(unsigned int newtick) { } void BlockGame::DoAction (const BlockGameAction& action) { - replayInfo.actions.push_back(action); + if (action.action == BlockGameAction::Action::UPDATE && action.tick < ticks+50) { + return; //Ignore if this is an update and not high enough + } + if (action.action == BlockGameAction::Action::UPDATE && replayInfo.actions.size() > 0 && replayInfo.actions.back().action == action.action) { + replayInfo.actions.back() = action; + } + else { + replayInfo.actions.push_back(action); + } if (action.action == BlockGameAction::Action::UPDATE) { UpdateInternal(action.tick); } diff --git a/source/code/main.cpp b/source/code/main.cpp index 34f94a7..ca6167e 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp @@ -70,6 +70,7 @@ http://www.blockattack.net #include "ReadKeyboard.h" //Reads text from keyboard #include "stats.h" //Saves general stats //#include "uploadReplay.h" //Takes care of everything libcurl related +#include "replayhandler.hpp" #include "common.h" #include "gamecontroller.h" @@ -1020,6 +1021,7 @@ static BlockGameSdl* player2; static bool registerEndlessHighscore = false; static bool registerTTHighscorePlayer1 = false; static bool registerTTHighscorePlayer2 = false; +static bool saveReplay = false; static void StartSinglePlayerEndless() { //1 player - endless @@ -1051,6 +1053,7 @@ static void StartSinglePlayerTimeTrial() { player1->name = player1name; player2->name = player2name; registerTTHighscorePlayer1 = true; + saveReplay = true; } static int StartSinglePlayerPuzzle(int level) { @@ -2027,6 +2030,16 @@ int runGame(int gametype, int level) { theTopScoresEndless.addScore(theGame.name, theGame.GetScore()); theGame.EndlessHighscoreEvent(); } + if (theGame.isGameOver() && saveReplay) { + if (twoPlayers && theGame2.isGameOver()) { + saveReplay = false; + SaveReplay(theGame.GetBlockGameInfo(), theGame2.GetBlockGameInfo()); + } + if (!twoPlayers) { + saveReplay = false; + SaveReplay(theGame.GetBlockGameInfo()); + } + } //Once evrything has been checked, update graphics DrawEverything(xsize,ysize,&theGame,&theGame2); diff --git a/source/code/replayhandler.cpp b/source/code/replayhandler.cpp new file mode 100644 index 0000000..ade9f4b --- /dev/null +++ b/source/code/replayhandler.cpp @@ -0,0 +1,51 @@ +/* +=========================================================================== +blockattack - Block Attack - Rise of the Blocks +Copyright (C) 2005-2016 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, see http://www.gnu.org/licenses/ + +Source information and contacts persons can be found at +http://www.blockattack.net +=========================================================================== +*/ + +#include "replayhandler.hpp" +#include +#include "cereal/archives/json.hpp" +#include "sago/SagoMisc.hpp" + +static void SaveReplayToFile(const SavedReplayStruct& sr, const char* filename) { + std::stringstream ss; + { + cereal::JSONOutputArchive archive(ss,cereal::JSONOutputArchive::Options::NoIndent()); + archive(cereal::make_nvp("savedReplay", sr)); + } + sago::WriteFileContent(filename, ss.str()); +} + +void SaveReplay(const BlockGameInfo& game1) { + SavedReplayStruct sr; + sr.numberOfPlayers = 1; + sr.playerInfo.push_back(game1); + SaveReplayToFile(sr, "SavedSinglePlayerGame"); +} + +void SaveReplay(const BlockGameInfo& game1, const BlockGameInfo& game2) { + SavedReplayStruct sr; + sr.numberOfPlayers = 2; + sr.playerInfo.push_back(game1); + sr.playerInfo.push_back(game2); + SaveReplayToFile(sr, "SavedTwoPlayerGame"); +} \ No newline at end of file diff --git a/source/code/replayhandler.hpp b/source/code/replayhandler.hpp new file mode 100644 index 0000000..8b8db90 --- /dev/null +++ b/source/code/replayhandler.hpp @@ -0,0 +1,39 @@ +/* +=========================================================================== +blockattack - Block Attack - Rise of the Blocks +Copyright (C) 2005-2016 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, see http://www.gnu.org/licenses/ + +Source information and contacts persons can be found at +http://www.blockattack.net +=========================================================================== +*/ + +#include "BlockGame.hpp" +#include "cereal/types/vector.hpp" + +struct SavedReplayStruct { + int numberOfPlayers = 0; + std::vector playerInfo; + template + void serialize( Archive & ar ) + { + ar( CEREAL_NVP(numberOfPlayers), CEREAL_NVP(playerInfo) ); + } +}; + +void SaveReplay(const BlockGameInfo& game1); + +void SaveReplay(const BlockGameInfo& game1, const BlockGameInfo& game2); \ No newline at end of file