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