#include <QTableWidget>
#include <QListWidget>
#include <QLabel>
-#include <QFile>
#include <QPushButton>
#include <QApplication>
+#include <QFile>
+#include <QDir>
#include <QDebug>
+const Piece* findPiece(PieceType type, int rotation)
+{
+ static QHash<QPair<PieceType, int>, const Piece*> pieceCache;
+
+ // Fill the cache on the first run
+ if (pieceCache.size() == 0) {
+ for (int i = 0; ppieces[i].type != PiecesEnd; ++i)
+ pieceCache.insert(QPair<PieceType, int>(ppieces[i].type, ppieces[i].rotation), &ppieces[i]);
+ }
+ QPair<PieceType, int> key(type, rotation);
+ if (pieceCache.contains(key))
+ return pieceCache[key];
+ return 0;
+
+}
QString pieceToIconId(const Piece* piece, bool flow1 = false, bool flow2 = false)
{
if (row < 0 || col < 0 || row >= rows || col >= cols) {
return;
}
+ if (dir == DirDone || dir == DirFailed || dir == DirPassed) {
+ // No need to indicate these pseudo-directions
+ return;
+ }
+
int index = toIndex(row, col);
if (dir != DirNone && (field[index].piece->flows[0] == dir || field[index].piece->flows[1] == dir)) {
field[index].flow[0] = true;
field[index].piece = ppieces;
field[index].flow[0] = true;
field[index].flow[1] = false;
-
}
else {
qWarning() << "Indicate flow: illegal direction" << row << col << dir;
label->setPixmap(QPixmap(iconId));
}
-QHash<QPair<PieceType, int>, const Piece*> AvailablePieces::pieceCache;
-
AvailablePieces::AvailablePieces(QTableWidget* ui)
: pieceUi(ui)
{
connect(pieceUi, SIGNAL(itemClicked(QTableWidgetItem*)), this, SLOT(onItemClicked(QTableWidgetItem*)));
- initPieceCache();
-
// Setup ui
qDebug() << pieceUi->rowCount() << pieceUi->columnCount();
{
int rotation = (id % 4)*90;
PieceType type = (PieceType)(id / 4);
- QPair<PieceType, int> key(type, rotation);
- if (!pieceCache.contains(key))
- return ppieces;
- return pieceCache[key];
-}
-
-void AvailablePieces::initPieceCache()
-{
- for (int i = 0; ppieces[i].type != PiecesEnd; ++i)
- pieceCache.insert(QPair<PieceType, int>(ppieces[i].type, ppieces[i].rotation), &ppieces[i]);
+ return findPiece(type, rotation);
}
void AvailablePieces::initGame(int count, AvailablePiece* pieces)
neededFlow = 0;
int prePlacedCount = 0;
gameData >> prePlacedCount;
- qDebug() << rows << cols;
if (prePlacedCount < 2 || prePlacedCount > 100)
- qFatal("Error reading game file: piece count000");
+ qFatal("Error reading game file: piece count");
PrePlacedPiece* prePlaced = new PrePlacedPiece[prePlacedCount];
for (int i = 0; i < prePlacedCount; ++i) {
- int ix = 0;
- gameData >> ix;
- if (ix < 0 || ix >= noPieces)
- qFatal("Error reading game file: no of pieces");
- prePlaced[i].piece = &ppieces[ix];
+ int type = 0;
+ gameData >> type;
+ if (type < 0 || type >= PiecesEnd)
+ qFatal("Error reading game file: type of pre-placed piece");
+
+ int rotation = 0;
+ gameData >> rotation;
+ if (rotation != 0 && rotation != 90 && rotation != 180 && rotation != 270)
+ qFatal("Error reading game file: rotation of pre-placed piece");
+
+ prePlaced[i].piece = findPiece((PieceType)type, rotation);
+ if (!prePlaced[i].piece)
+ qFatal("Error reading game file: invalid pre-placed piece");
// Record that the liquid must flow through this pre-placed
// piece (if it can)
doneButton->setEnabled(true);
timer.start();
levelRunning = true;
+ file.close();
}
void GameController::onTimeout()
levelWidget(levelWidget), levelList(levelList), levelStartButton(levelStartButton),
startWidget(startWidget), startTitle(startTitle), startLabel(startLabel), startButton(startButton),
levelLabel(levelLabel), scoreLabel(scoreLabel),
- levelCollections(levelCollections), level(0), totalScore(0)
+ levelCollections(levelCollections), curColl(0), level(0), totalScore(0)
{
connect(levelStartButton, SIGNAL(clicked()), this, SLOT(onLevelCollectionChosen()));
connect(startButton, SIGNAL(clicked()), this, SLOT(onStartClicked()));
connect(gameController, SIGNAL(levelPassed(int)), this, SLOT(onLevelPassed(int)));
connect(gameController, SIGNAL(levelFailed()), this, SLOT(onLevelFailed()));
- startTitle->setText("Starting a new game.");
- scoreLabel->setText("0");
+ readSavedGames();
chooseLevelCollection();
}
bool first = true;
foreach (const QString& collection, levelCollections) {
QListWidgetItem *newItem = new QListWidgetItem();
- newItem->setText(collection);
+
+ // Check how many levels the user has already passed
+ int passed = 0;
+ if (savedGames.contains(collection)) {
+ passed = savedGames[collection];
+ }
+
+ newItem->setText(collection + " " + QString::number(passed) + " / " /*+ y*/);
levelList->addItem(newItem); // transfers ownership
- if (first) {
+ if (first) { // TODO: and passed < total
levelList->setCurrentItem(newItem);
first = false;
}
void LevelSwitcher::onLevelCollectionChosen()
{
levelWidget->hide();
- QString collection = levelList->currentItem()->text();
- QFile file(QString(LEVDIR) + "/" + collection + ".dat");
+ curColl = levelList->currentItem()->text().split(" ").first();
+ QFile file(QString(LEVDIR) + "/" + curColl + ".dat");
+ qDebug() << "Trying to read" << file.fileName();
if (!file.exists())
qFatal("Error reading game file: doesn't exist");
file.open(QIODevice::ReadOnly);
levels << levelData.readLine();
level = 0;
+ // Go to the level the user has not yet passed
+ if (savedGames.contains(curColl)) {
+ level = savedGames[curColl];
+ if (level >= levels.size() - 1) {
+ level = 0;
+ }
+ }
+
totalScore = 0;
+ startTitle->setText("Starting a new game.");
+ scoreLabel->setText("0");
+ file.close();
initiateLevel();
}
if (level < levels.size() - 1) {
++ level;
startTitle->setText(QString("Level ") + QString::number(level) + QString(" passed, proceeding to level ") + QString::number(level+1));
+ // Record that the level has been passed, so that the user can
+ // start again
+ savedGames.insert(curColl, level);
+ writeSavedGames();
+
initiateLevel();
}
else {
startLabel->setText("Start a new game?");
startButton->disconnect();
connect(startButton, SIGNAL(clicked()), this, SLOT(chooseLevelCollection()));
+ // Record that all levels have been passed
+ savedGames.insert(curColl, levels.size() - 1);
+ writeSavedGames();
+
level = 0;
startWidget->show();
}
initiateLevel();
}
+void LevelSwitcher::readSavedGames()
+{
+ QFile file(QDir::homePath() + "/.evilplumber");
+ if (!file.exists()) {
+ qWarning() << "Save file doesn't exist";
+ return;
+ }
+ file.open(QIODevice::ReadOnly);
+ QTextStream saveData(&file);
+ QString collection = 0;
+ int level = 0;
+ while (!saveData.atEnd()) {
+ saveData >> collection;
+ saveData >> level;
+ qDebug() << "Got saved game: " << collection << level;
+ savedGames.insert(collection, level);
+ }
+ file.close();
+}
+
+void LevelSwitcher::writeSavedGames()
+{
+ QFile file(QDir::homePath() + "/.evilplumber");
+ file.open(QIODevice::Truncate | QIODevice::WriteOnly);
+ QTextStream saveData(&file);
+ foreach (const QString& collection, savedGames.keys()) {
+ saveData << collection << " " << savedGames[collection] << endl;
+ }
+ file.close();
+}
+
// Todo next:
// better graphics
// save & load
// more levels
// make fixed pipes look different than non-fixed ones
// color theme
-// transparency
// --------------
// re-placing pieces
// graphical hints on what to do next