git repos / blockattack-game

blame: source/code/puzzlehandler.cpp

normal view · raw

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