| 1 | /* |
| 2 | =========================================================================== |
| 3 | blockattack - Block Attack - Rise of the Blocks |
| 4 | Copyright (C) 2005-2015 Poul Sander |
| 5 | |
| 6 | This program is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see http://www.gnu.org/licenses/ |
| 18 | |
| 19 | Source information and contacts persons can be found at |
| 20 | http://blockattack.net |
| 21 | =========================================================================== |
| 22 | */ |
| 23 | |
| 24 | #include "puzzlehandler.hpp" |
| 25 | #include "nlohmann/json.hpp" |
| 26 | #include <vector> |
| 27 | #include <iostream> |
| 28 | #include <sstream> |
| 29 | #include "stats.h" |
| 30 | #include <physfs.h> //Abstract file system. To use containers |
| 31 | #include <fmt/core.h> |
| 32 | #include "sago/SagoMisc.hpp" |
| 33 | |
| 34 | using json = nlohmann::json; |
| 35 | |
| 36 | const int maxNrOfPuzzleStages = 50; //Maximum number of puzzle stages |
| 37 | |
| 38 | static std::string puzzleSavePath; |
| 39 | static std::string puzzleName; //The filename of |
| 40 | static std::vector<bool> puzzleCleared(maxNrOfPuzzleStages); //vector that tells if puzzle cleared |
| 41 | static std::vector<int> nrOfMovesAllowed(maxNrOfPuzzleStages); //Moves to clear |
| 42 | static int puzzleLevels[maxNrOfPuzzleStages][6][12]; //Contains board layout; |
| 43 | static int nrOfPuzzles; //How many are there actually? |
| 44 | |
| 45 | int PuzzleNumberOfMovesAllowed(size_t level) { |
| 46 | if (level >= nrOfMovesAllowed.size()) { |
| 47 | return -1; |
| 48 | } |
| 49 | return nrOfMovesAllowed.at(level); |
| 50 | } |
| 51 | |
| 52 | void PuzzleNumberOfMovesAllowedSet(size_t level, int numbler_of_moves) { |
| 53 | if (level >= nrOfMovesAllowed.size()) { |
| 54 | return; |
| 55 | } |
| 56 | nrOfMovesAllowed[level] = numbler_of_moves; |
| 57 | } |
| 58 | |
| 59 | int PuzzleGetBrick(size_t level, int x, int y) { |
| 60 | if (level >= maxNrOfPuzzleStages || x >= 6 || y >= 12 || x < 0 || y < 0) { |
| 61 | return -1; |
| 62 | } |
| 63 | return puzzleLevels[level][x][y]; |
| 64 | } |
| 65 | |
| 66 | void PuzzleSetBrick(size_t level, int x, int y, int brick) { |
| 67 | if (level >= maxNrOfPuzzleStages || x >= 6 || y >= 12 || x < 0 || y < 0) { |
| 68 | return; |
| 69 | } |
| 70 | if (brick >= 7) { |
| 71 | return; |
| 72 | } |
| 73 | puzzleLevels[level][x][y] = brick; |
| 74 | } |
| 75 | |
| 76 | int PuzzleGetNumberOfPuzzles() { |
| 77 | return nrOfPuzzles; |
| 78 | } |
| 79 | |
| 80 | bool PuzzleIsCleared(size_t level) { |
| 81 | return puzzleCleared.at(level); |
| 82 | } |
| 83 | |
| 84 | const std::string& PuzzleGetName() { |
| 85 | return puzzleName; |
| 86 | } |
| 87 | |
| 88 | void PuzzleSetName(const std::string& name) { |
| 89 | puzzleName = name; |
| 90 | puzzleSavePath = name + ".json.save"; |
| 91 | } |
| 92 | |
| 93 | void LoadClearData() { |
| 94 | std::string readFileContent = sago::GetFileContent(puzzleSavePath.c_str()); |
| 95 | if (readFileContent.length()) { |
| 96 | json j = json::parse(readFileContent); |
| 97 | try { |
| 98 | j.at("cleared").get_to(puzzleCleared); |
| 99 | } catch (json::exception& e) { |
| 100 | std::cerr << "Failed to read \"" << puzzleSavePath << "\". File will be regenerated. Reason: " << e.what() << "\n"; |
| 101 | puzzleCleared.clear(); |
| 102 | } |
| 103 | } |
| 104 | else { |
| 105 | puzzleCleared.clear(); |
| 106 | } |
| 107 | puzzleCleared.resize(nrOfPuzzles); |
| 108 | } |
| 109 | |
| 110 | void SaveClearData() { |
| 111 | json j = json{ { "cleared", puzzleCleared } }; |
| 112 | const std::string s = j.dump(4); |
| 113 | sago::WriteFileContent(puzzleSavePath.c_str(), s); |
| 114 | } |
| 115 | |
| 116 | void PuzzleSetClear(size_t Level) { |
| 117 | if (puzzleCleared[Level]==false) { |
| 118 | Stats::getInstance()->addOne("puzzlesSolved"); |
| 119 | } |
| 120 | puzzleCleared[Level] = true; |
| 121 | SaveClearData(); |
| 122 | } |
| 123 | |
| 124 | /*Loads all the puzzle levels*/ |
| 125 | int LoadPuzzleStages( ) { |
| 126 | const std::string filename = fmt::format("puzzles/{}",puzzleName); |
| 127 | if (!PHYSFS_exists(filename.c_str())) { |
| 128 | std::cerr << "Warning: File not in blockattack.data: " << filename << "\n"; |
| 129 | return -1; //file doesn't exist |
| 130 | } |
| 131 | const std::string fileContent = sago::GetFileContent(filename.c_str()); |
| 132 | std::stringstream inFile(fileContent); |
| 133 | |
| 134 | inFile >> nrOfPuzzles; |
| 135 | if (nrOfPuzzles>maxNrOfPuzzleStages) { |
| 136 | nrOfPuzzles=maxNrOfPuzzleStages; |
| 137 | } |
| 138 | if (nrOfPuzzles < 0) { |
| 139 | nrOfPuzzles = 0; |
| 140 | } |
| 141 | for (int k=0; k<nrOfPuzzles ; k++) { |
| 142 | inFile >> nrOfMovesAllowed.at(k); |
| 143 | for (int i=11; i>=0; i--) |
| 144 | for (int j=0; j<6; j++) { |
| 145 | inFile >> puzzleLevels[k][j][i]; |
| 146 | } |
| 147 | } |
| 148 | LoadClearData(); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | int SavePuzzleStages() { |
| 153 | if (puzzleName.empty()) { |
| 154 | std::cerr << "Error: No puzzle name set. Cannot save.\n"; |
| 155 | return -1; |
| 156 | } |
| 157 | std::string fileContent = std::to_string(nrOfPuzzles) + "\n"; |
| 158 | for (int k=0; k<nrOfPuzzles ; k++) { |
| 159 | fileContent += std::to_string(nrOfMovesAllowed.at(k)) + "\n"; |
| 160 | for (int i=11; i>=0; i--) |
| 161 | for (int j=0; j<6; j++) { |
| 162 | fileContent += std::to_string(puzzleLevels[k][j][i]) + " "; |
| 163 | } |
| 164 | fileContent += "\n"; |
| 165 | } |
| 166 | sago::WriteFileContent(((std::string)("puzzles/"+puzzleName)).c_str(), fileContent); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | void EditorRemovePuzzle(size_t level) { |
| 171 | if (level >= nrOfPuzzles) { |
| 172 | return; |
| 173 | } |
| 174 | //Shift all puzzles up. |
| 175 | memmove(puzzleLevels[level], puzzleLevels[level+1], sizeof(puzzleLevels[0])*(nrOfPuzzles-level-1)); |
| 176 | nrOfPuzzles--; |
| 177 | } |
| 178 | |
| 179 | void EditorAddPuzzle(size_t level) { |
| 180 | if (nrOfPuzzles >= maxNrOfPuzzleStages) { |
| 181 | return; |
| 182 | } |
| 183 | if (level >= maxNrOfPuzzleStages) |
| 184 | { |
| 185 | level = 0; |
| 186 | } |
| 187 | memmove( puzzleLevels[level+1], puzzleLevels[level], sizeof(puzzleLevels[0])*(maxNrOfPuzzleStages-level-1) ); |
| 188 | nrOfPuzzles++; |
| 189 | for (size_t i = 0; i < 6; i++) { |
| 190 | for (size_t j =0; j < 12; j++) { |
| 191 | puzzleLevels[level][i][j] = -1; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void EditorResizePuzzleNumber(size_t number_of_puzzles) { |
| 197 | if (number_of_puzzles > 50) { |
| 198 | number_of_puzzles = 50; |
| 199 | } |
| 200 | nrOfPuzzles = number_of_puzzles; |
| 201 | } |