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