diff --git a/source/code/BlockGame.cpp b/source/code/BlockGame.cpp index e24b28c..dbddcae 100644 --- a/source/code/BlockGame.cpp +++ b/source/code/BlockGame.cpp @@ -32,6 +32,7 @@ http://blockattack.sf.net #define CHAINPLACE 10000000 #include "BlockGame.hpp" +#include "puzzlehandler.hpp" #include @@ -390,11 +391,11 @@ void BlockGame::NewPuzzleGame(int level, unsigned int ticks) NewGame(ticks); puzzleMode = true; Level = level; - MovesLeft = nrOfMovesAllowed[Level]; + MovesLeft = PuzzleNumberOfMovesAllowed(Level); for (int i=0; i<6; i++) for (int j=0; j<12; j++) { - board[i][j+1] = puzzleLevels[Level][i][j]; + board[i][j+1] = PuzzleGetBrick(Level,i,j); } baseSpeed = 100000; speed = 100000; @@ -437,7 +438,7 @@ void BlockGame::retryLevel(unsigned int ticks) void BlockGame::nextLevel(unsigned int ticks) { if (puzzleMode) { - if (LeveladdOne("puzzlesSolved"); - puzzleCleared[Level] = true; - ofstream outfile; + } + if ((puzzleMode)&&(!bGameOver)&&BoardEmpty()) { + if (!singlePuzzle) { + PuzzleSetClear(Level); stageButtonStatus = SBpuzzleMode; - outfile.open(puzzleSavePath.c_str(), ios::binary |ios::trunc); - if (!outfile) - { - cerr << "Error writing to file: " << puzzleSavePath << endl; - } - else - { - for (int i=0; i(&tempBool), sizeof(bool)); - } - outfile.close(); - } } setPlayerWon(); } diff --git a/source/code/Makefile b/source/code/Makefile index c92daee..030061c 100644 --- a/source/code/Makefile +++ b/source/code/Makefile @@ -43,7 +43,7 @@ endif BASE_LIBS += -lphysfs -OFILES=main.o highscore.o ReadKeyboard.o joypad.o listFiles.o replay.o common.o stats.o CppSdlException.o CppSdlImageHolder.o Libs/NFont.o MenuSystem.o menudef.o +OFILES=main.o highscore.o ReadKeyboard.o joypad.o listFiles.o replay.o common.o stats.o CppSdlException.o CppSdlImageHolder.o Libs/NFont.o MenuSystem.o menudef.o puzzlehandler.o ifeq ($(CROSS),i686-pc-mingw32-) OFILES += winicon.res diff --git a/source/code/main.cpp b/source/code/main.cpp index 61af457..14cef66 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp @@ -71,6 +71,7 @@ http://blockattack.sf.net #include #include "CppSdlImageHolder.hpp" #include "MenuSystem.h" +#include "puzzlehandler.hpp" //if SHAREDIR is not used we look in current directory #ifndef SHAREDIR @@ -813,34 +814,6 @@ static string itoa2(int num) return converter.str(); } -/*Loads all the puzzle levels*/ -static int LoadPuzzleStages() -{ - //if(puzzleLoaded) - // return 1; - if (!PHYSFS_exists(((string)("puzzles/"+puzzleName)).c_str())) - { - cerr << "Warning: File not in blockattack.data: " << ("puzzles/"+puzzleName) << endl; - return -1; //file doesn't exist - } - PhysFS::ifstream inFile(((string)("puzzles/"+puzzleName)).c_str()); - - inFile >> nrOfPuzzles; - if (nrOfPuzzles>maxNrOfPuzzleStages) - nrOfPuzzles=maxNrOfPuzzleStages; - for (int k=0; (k> nrOfMovesAllowed[k]; - for (int i=11; i>=0; i--) - for (int j=0; j<6; j++) - { - inFile >> puzzleLevels[k][j][i]; - } - } - puzzleLoaded = true; - return 0; -} - /*Draws a image from on a given Surface. Takes source image, destination surface and coordinates*/ void DrawIMG(SDL_Surface *img, SDL_Surface *target, int x, int y) { @@ -1566,7 +1539,7 @@ public: if(puzzleMode && stageButtonStatus == SBpuzzleMode) { DrawIMG(bRetry,sBoard, cordRetryButton.x, cordRetryButton.y); - if(Level(&tempBool),sizeof(bool)); - puzzleCleared[i] = tempBool; - } - puzzleFile.close(); - } - else - { - tempBool = false; - for (int i=0; i0)); i--) theFileName[i]=0; - puzzleName = theFileName; + PuzzleSetName(theFileName); #if defined(__unix__) string home = getenv("HOME"); - puzzleSavePath = home+"/.gamesaves/blockattack/"+puzzleName+".save"; + PuzzleSetSavePath(home+"/.gamesaves/blockattack/"+PuzzleGetName()+".save"); #elif defined(_WIN32) string home = getMyDocumentsPath(); if (&home!=NULL) { - puzzleSavePath = home+"/My Games/blockattack/"+puzzleName+".save"; + PuzzleSetSavePath(home+"/My Games/blockattack/"+PuzzleGetName()+".save"); } else { - puzzleSavePath = puzzleName+".save"; + PuzzleSetSavePath(PuzzleGetName()+".save"); } #else - puzzleSavePath = puzzleName+".save"; + PuzzleSetSavePath(PuzzleGetName()+".save"); #endif } @@ -3297,23 +3254,23 @@ int main(int argc, char *argv[]) #if defined(__unix__) stageClearSavePath = home+"/.gamesaves/blockattack/stageClear.SCsave"; - puzzleSavePath = home+"/.gamesaves/blockattack/puzzle.levels.save"; + PuzzleSetSavePath(home+"/.gamesaves/blockattack/puzzle.levels.save"); #elif defined(_WIN32) if (&home!=NULL) { stageClearSavePath = home+"/My Games/blockattack/stageClear.SCsave"; - puzzleSavePath = home+"/My Games/blockattack/puzzle.levels.save"; + PuzzleSetSavePath(home+"/My Games/blockattack/puzzle.levels.save"); } else { stageClearSavePath = "stageClear.SCsave"; - puzzleSavePath = "puzzle.levels.save"; + PuzzleSetSavePath("puzzle.levels.save"); } #else stageClearSavePath = "stageClear.SCsave"; - puzzleSavePath = "puzzle.levels.save"; + PuzzleSetSavePath("puzzle.levels.save"); #endif - puzzleName="puzzle.levels"; + PuzzleSetName("puzzle.levels"); //Init SDL if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) diff --git a/source/code/mainVars.hpp b/source/code/mainVars.hpp index 704756b..b57b0d8 100644 --- a/source/code/mainVars.hpp +++ b/source/code/mainVars.hpp @@ -204,18 +204,11 @@ char player2name[30]; //paths static string stageClearSavePath; -static string puzzleSavePath; -static string puzzleName; //The filename of static const int nrOfStageLevels = 50; //number of stages in stage Clear -static const int maxNrOfPuzzleStages = 50; //Maximum number of puzzle stages vector stageCleared(nrOfStageLevels); //vector that tells if a stage is cleared vector stageTimes(nrOfStageLevels); //For statistical puposes vector stageScores(nrOfStageLevels); //--||-- -vector puzzleCleared(maxNrOfPuzzleStages); //vector that tells if puzzle cleared -vector nrOfMovesAllowed(maxNrOfPuzzleStages); //Moves to clear -int puzzleLevels[maxNrOfPuzzleStages][6][12]; //Contains board layout; -int nrOfPuzzles; //How many are there actually? bool twoPlayers; //True if two players are playing //Old mouse position: diff --git a/source/code/puzzlehandler.cpp b/source/code/puzzlehandler.cpp new file mode 100644 index 0000000..03250c1 --- /dev/null +++ b/source/code/puzzlehandler.cpp @@ -0,0 +1,106 @@ +#include "puzzlehandler.hpp" +#include +#include +#include +#include "stats.h" +#include //Abstract file system. To use containers +#include "physfs_stream.hpp" //To use C++ style file streams + +static const int maxNrOfPuzzleStages = 50; //Maximum number of puzzle stages +static std::string puzzleSavePath; +static std::string puzzleName; //The filename of +static std::vector puzzleCleared(maxNrOfPuzzleStages); //vector that tells if puzzle cleared +static std::vector nrOfMovesAllowed(maxNrOfPuzzleStages); //Moves to clear +static int puzzleLevels[maxNrOfPuzzleStages][6][12]; //Contains board layout; +static int nrOfPuzzles; //How many are there actually? + +int PuzzleNumberOfMovesAllowed(int level) { + return nrOfMovesAllowed.at(level); +} + +int PuzzleGetBrick(int level, int x, int y) { + return puzzleLevels[level][x][y]; +} + +int PuzzleGetNumberOfPuzzles() { + return nrOfPuzzles; +} + +bool PuzzleIsCleared(int level) { + return puzzleCleared.at(level); +} + +const std::string& PuzzleGetName() { + return puzzleName; +} + +void PuzzleSetName(const std::string& name) { + puzzleName = name; +} + +void PuzzleSetSavePath(const std::string& filepath) { + puzzleSavePath = filepath; +} + +void PuzzleSetClear(int Level) { + if(puzzleCleared[Level]==false) + Stats::getInstance()->addOne("puzzlesSolved"); + puzzleCleared[Level] = true; + std::ofstream outfile; + outfile.open(puzzleSavePath.c_str(), ios::binary |ios::trunc); + if (!outfile) + { + std::cerr << "Error writing to file: " << puzzleSavePath << endl; + } + else + { + for (int i=0; i(&tempBool), sizeof(bool)); + } + outfile.close(); + } +} + +/*Loads all the puzzle levels*/ +int LoadPuzzleStages( ) +{ + if (!PHYSFS_exists(((std::string)("puzzles/"+puzzleName)).c_str())) + { + std::cerr << "Warning: File not in blockattack.data: " << ("puzzles/"+puzzleName) << endl; + return -1; //file doesn't exist + } + PhysFS::ifstream inFile(((std::string)("puzzles/"+puzzleName)).c_str()); + + inFile >> nrOfPuzzles; + if (nrOfPuzzles>maxNrOfPuzzleStages) + nrOfPuzzles=maxNrOfPuzzleStages; + for (int k=0; (k> nrOfMovesAllowed[k]; + for (int i=11; i>=0; i--) + for (int j=0; j<6; j++) + { + inFile >> puzzleLevels[k][j][i]; + } + } + bool tempBool; + std::ifstream puzzleFile(puzzleSavePath.c_str(), std::ios::binary); + if (puzzleFile) + { + for (int i=0; (i(&tempBool),sizeof(bool)); + puzzleCleared[i] = tempBool; + } + puzzleFile.close(); + } + else + { + tempBool = false; + for (int i=0; i + +int PuzzleNumberOfMovesAllowed(int level); +int PuzzleGetBrick(int level, int x, int y); +bool PuzzleIsCleared(int level); +int LoadPuzzleStages(); +int PuzzleGetNumberOfPuzzles(); +void PuzzleSetClear(int level); +const std::string& PuzzleGetName(); +void PuzzleSetName(const std::string& name); +void PuzzleSetSavePath(const std::string& filepath); + +#endif /* PUZZLEHANDLER_HPP */ +