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
b8a4e6f1 Poul Sander 2025-06-21 10:31 59
void PuzzleSetBrick(size_t level, int x, int y, int brick) {
b8a4e6f1 Poul Sander 2025-06-21 10:31 60
	if (level >= maxNrOfPuzzleStages || x >= 6 || y >= 12 || x < 0 || y < 0) {
b8a4e6f1 Poul Sander 2025-06-21 10:31 61
		return;
b8a4e6f1 Poul Sander 2025-06-21 10:31 62
	}
b8a4e6f1 Poul Sander 2025-06-21 10:31 63
	if (brick >= 7) {
b8a4e6f1 Poul Sander 2025-06-21 10:31 64
		return;
b8a4e6f1 Poul Sander 2025-06-21 10:31 65
	}
b8a4e6f1 Poul Sander 2025-06-21 10:31 66
	puzzleLevels[level][x][y] = brick;
b8a4e6f1 Poul Sander 2025-06-21 10:31 67
}
b8a4e6f1 Poul Sander 2025-06-21 10:31 68
055218be sago007 2015-08-22 18:44 69
int PuzzleGetNumberOfPuzzles() {
055218be sago007 2015-08-22 18:44 70
	return nrOfPuzzles;
055218be sago007 2015-08-22 18:44 71
}
055218be sago007 2015-08-22 18:44 72
52352a94 Poul Sander 2025-06-03 19:04 73
bool PuzzleIsCleared(size_t level) {
055218be sago007 2015-08-22 18:44 74
	return puzzleCleared.at(level);
055218be sago007 2015-08-22 18:44 75
}
055218be sago007 2015-08-22 18:44 76
055218be sago007 2015-08-22 18:44 77
const std::string& PuzzleGetName() {
055218be sago007 2015-08-22 18:44 78
	return puzzleName;
055218be sago007 2015-08-22 18:44 79
}
055218be sago007 2015-08-22 18:44 80
055218be sago007 2015-08-22 18:44 81
void PuzzleSetName(const std::string& name) {
055218be sago007 2015-08-22 18:44 82
	puzzleName = name;
63acc19f sago007 2016-02-15 17:48 83
	puzzleSavePath = name + ".json.save";
63acc19f sago007 2016-02-15 17:48 84
}
63acc19f sago007 2016-02-15 17:48 85
63acc19f sago007 2016-02-15 17:48 86
void LoadClearData() {
63acc19f sago007 2016-02-15 17:48 87
	std::string readFileContent = sago::GetFileContent(puzzleSavePath.c_str());
5cdbc29b Poul Sander 2025-05-30 20:06 88
	if (readFileContent.length()) {
9f711750 Poul Sander 2024-05-12 12:35 89
		json j = json::parse(readFileContent);
9f711750 Poul Sander 2024-05-12 12:35 90
		try {
9f711750 Poul Sander 2024-05-12 12:35 91
			j.at("cleared").get_to(puzzleCleared);
9f711750 Poul Sander 2024-05-12 12:35 92
		} catch (json::exception& e) {
9f711750 Poul Sander 2024-05-12 12:35 93
			std::cerr << "Failed to read \"" << puzzleSavePath << "\". File will be regenerated. Reason: " << e.what() << "\n";
9f711750 Poul Sander 2024-05-12 12:35 94
			puzzleCleared.clear();
63acc19f sago007 2016-02-15 17:48 95
		}
63acc19f sago007 2016-02-15 17:48 96
	}
63acc19f sago007 2016-02-15 17:48 97
	else {
63acc19f sago007 2016-02-15 17:48 98
		puzzleCleared.clear();
63acc19f sago007 2016-02-15 17:48 99
	}
63acc19f sago007 2016-02-15 17:48 100
	puzzleCleared.resize(nrOfPuzzles);
63acc19f sago007 2016-02-15 17:48 101
}
63acc19f sago007 2016-02-15 17:48 102
63acc19f sago007 2016-02-15 17:48 103
void SaveClearData() {
9f711750 Poul Sander 2024-05-12 12:35 104
	json j = json{ { "cleared", puzzleCleared } };
5cdbc29b Poul Sander 2025-05-30 20:06 105
	const std::string s = j.dump(4);
9f711750 Poul Sander 2024-05-12 12:35 106
	sago::WriteFileContent(puzzleSavePath.c_str(), s);
055218be sago007 2015-08-22 18:44 107
}
055218be sago007 2015-08-22 18:44 108
52352a94 Poul Sander 2025-06-03 19:04 109
void PuzzleSetClear(size_t Level) {
ecef5838 sago007 2015-11-24 20:01 110
	if (puzzleCleared[Level]==false) {
055218be sago007 2015-08-22 18:44 111
		Stats::getInstance()->addOne("puzzlesSolved");
acd79baf sago007 2015-11-22 19:37 112
	}
055218be sago007 2015-08-22 18:44 113
	puzzleCleared[Level] = true;
63acc19f sago007 2016-02-15 17:48 114
	SaveClearData();
055218be sago007 2015-08-22 18:44 115
}
055218be sago007 2015-08-22 18:44 116
055218be sago007 2015-08-22 18:44 117
/*Loads all the puzzle levels*/
ecef5838 sago007 2015-11-24 20:01 118
int LoadPuzzleStages( ) {
5cdbc29b Poul Sander 2025-05-30 20:06 119
	const std::string filename = fmt::format("puzzles/{}",puzzleName);
5cdbc29b Poul Sander 2025-05-30 20:06 120
	if (!PHYSFS_exists(filename.c_str())) {
5cdbc29b Poul Sander 2025-05-30 20:06 121
		std::cerr << "Warning: File not in blockattack.data: " << filename << "\n";
055218be sago007 2015-08-22 18:44 122
		return -1; //file doesn't exist
055218be sago007 2015-08-22 18:44 123
	}
5cdbc29b Poul Sander 2025-05-30 20:06 124
	const std::string fileContent = sago::GetFileContent(filename.c_str());
f1091f34 sago007 2016-03-25 20:56 125
	std::stringstream inFile(fileContent);
055218be sago007 2015-08-22 18:44 126
055218be sago007 2015-08-22 18:44 127
	inFile >> nrOfPuzzles;
acd79baf sago007 2015-11-22 19:37 128
	if (nrOfPuzzles>maxNrOfPuzzleStages) {
055218be sago007 2015-08-22 18:44 129
		nrOfPuzzles=maxNrOfPuzzleStages;
acd79baf sago007 2015-11-22 19:37 130
	}
c35c2b10 sago007 2016-03-31 18:49 131
	if (nrOfPuzzles < 0) {
c35c2b10 sago007 2016-03-31 18:49 132
		nrOfPuzzles = 0;
c35c2b10 sago007 2016-03-31 18:49 133
	}
c35c2b10 sago007 2016-03-31 18:49 134
	for (int k=0; k<nrOfPuzzles ; k++) {
c35c2b10 sago007 2016-03-31 18:49 135
		inFile >> nrOfMovesAllowed.at(k);
055218be sago007 2015-08-22 18:44 136
		for (int i=11; i>=0; i--)
ecef5838 sago007 2015-11-24 20:01 137
			for (int j=0; j<6; j++) {
055218be sago007 2015-08-22 18:44 138
				inFile >> puzzleLevels[k][j][i];
055218be sago007 2015-08-22 18:44 139
			}
055218be sago007 2015-08-22 18:44 140
	}
63acc19f sago007 2016-02-15 17:48 141
	LoadClearData();
055218be sago007 2015-08-22 18:44 142
	return 0;
63acc19f sago007 2016-02-15 17:48 143
}
dcdbd31f Poul Sander 2025-04-04 18:45 144
dcdbd31f Poul Sander 2025-04-04 18:45 145
int SavePuzzleStages() {
5cdbc29b Poul Sander 2025-05-30 20:06 146
	if (puzzleName.empty()) {
dcdbd31f Poul Sander 2025-04-04 18:45 147
		std::cerr << "Error: No puzzle name set. Cannot save.\n";
dcdbd31f Poul Sander 2025-04-04 18:45 148
		return -1;
dcdbd31f Poul Sander 2025-04-04 18:45 149
	}
dcdbd31f Poul Sander 2025-04-04 18:45 150
	std::string fileContent = std::to_string(nrOfPuzzles) + "\n";
dcdbd31f Poul Sander 2025-04-04 18:45 151
	for (int k=0; k<nrOfPuzzles ; k++) {
dcdbd31f Poul Sander 2025-04-04 18:45 152
		fileContent += std::to_string(nrOfMovesAllowed.at(k)) + "\n";
dcdbd31f Poul Sander 2025-04-04 18:45 153
		for (int i=11; i>=0; i--)
dcdbd31f Poul Sander 2025-04-04 18:45 154
			for (int j=0; j<6; j++) {
dcdbd31f Poul Sander 2025-04-04 18:45 155
				fileContent += std::to_string(puzzleLevels[k][j][i]) + " ";
dcdbd31f Poul Sander 2025-04-04 18:45 156
			}
dcdbd31f Poul Sander 2025-04-04 18:45 157
		fileContent += "\n";
dcdbd31f Poul Sander 2025-04-04 18:45 158
	}
dcdbd31f Poul Sander 2025-04-04 18:45 159
	sago::WriteFileContent(((std::string)("puzzles/"+puzzleName)).c_str(), fileContent);
dcdbd31f Poul Sander 2025-04-04 18:45 160
	return 0;
dcdbd31f Poul Sander 2025-04-04 18:45 161
}