From 7de31a1f0bd847a4df9c00de2f6fd7dac1524945 Mon Sep 17 00:00:00 2001 From: Marja Hassinen Date: Sat, 2 Jan 2010 21:12:58 +0200 Subject: [PATCH] Simplifying level reading & more intro levels. --- data/intro.dat | 2 ++ data/intro01.dat | 4 ++-- data/intro01.leg | 2 +- data/intro02.dat | 8 ++++---- data/intro03.dat | 19 +++++++++++++++++++ data/intro04.dat | 19 +++++++++++++++++++ data/intro04.leg | 1 + src/game.cpp | 52 ++++++++++++++++++++++++++++++---------------------- src/game.h | 6 ++---- 9 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 data/intro03.dat create mode 100644 data/intro04.dat create mode 100644 data/intro04.leg diff --git a/data/intro.dat b/data/intro.dat index b4edef8..c767537 100644 --- a/data/intro.dat +++ b/data/intro.dat @@ -1,2 +1,4 @@ intro01 intro02 +intro03 +intro04 diff --git a/data/intro01.dat b/data/intro01.dat index 60553a7..d0e7192 100644 --- a/data/intro01.dat +++ b/data/intro01.dat @@ -1,8 +1,8 @@ 5 7 2 -12 0 0 -14 4 6 +5 180 0 0 +6 0 4 6 6 1 9 2 9 diff --git a/data/intro01.leg b/data/intro01.leg index 068fd9e..f68c2fb 100644 --- a/data/intro01.leg +++ b/data/intro01.leg @@ -1 +1 @@ -Build a pipe from the start
to the end . \ No newline at end of file +Build a pipe from the start
to the end . \ No newline at end of file diff --git a/data/intro02.dat b/data/intro02.dat index d291a33..8ffefd4 100644 --- a/data/intro02.dat +++ b/data/intro02.dat @@ -1,10 +1,10 @@ 5 7 4 -12 4 2 -14 4 5 -3 1 1 -7 2 3 +5 180 4 2 +6 0 4 5 +2 270 1 1 +3 0 2 3 6 1 9 2 9 diff --git a/data/intro03.dat b/data/intro03.dat new file mode 100644 index 0000000..7c82c0c --- /dev/null +++ b/data/intro03.dat @@ -0,0 +1,19 @@ +5 +7 +5 +5 270 0 0 +6 90 4 6 +2 0 0 6 +2 180 4 0 +4 0 2 3 +9 +1 9 +2 9 +3 9 +4 9 +5 9 +6 9 +7 9 +8 9 +9 9 +100 diff --git a/data/intro04.dat b/data/intro04.dat new file mode 100644 index 0000000..69cbded --- /dev/null +++ b/data/intro04.dat @@ -0,0 +1,19 @@ +5 +7 +5 +5 270 0 0 +6 90 4 6 +2 0 0 6 +2 180 4 0 +2 0 2 3 +9 +1 4 +2 4 +3 4 +4 4 +5 4 +6 4 +7 8 +8 4 +9 4 +100 diff --git a/data/intro04.leg b/data/intro04.leg new file mode 100644 index 0000000..7bd962e --- /dev/null +++ b/data/intro04.leg @@ -0,0 +1 @@ +But remember that the limits for available pieces
restrict your choises! \ No newline at end of file diff --git a/src/game.cpp b/src/game.cpp index fddf831..3a71e3f 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -25,6 +25,21 @@ #include #include +const Piece* findPiece(PieceType type, int rotation) +{ + static QHash, 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(ppieces[i].type, ppieces[i].rotation), &ppieces[i]); + } + QPair key(type, rotation); + if (pieceCache.contains(key)) + return pieceCache[key]; + return 0; + +} QString pieceToIconId(const Piece* piece, bool flow1 = false, bool flow2 = false) { @@ -203,15 +218,11 @@ void GameField::indicateFlow(int row, int col, Direction dir) label->setPixmap(QPixmap(iconId)); } -QHash, 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(); @@ -243,16 +254,7 @@ const Piece* AvailablePieces::idToPiece(int id) { int rotation = (id % 4)*90; PieceType type = (PieceType)(id / 4); - QPair 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(ppieces[i].type, ppieces[i].rotation), &ppieces[i]); + return findPiece(type, rotation); } void AvailablePieces::initGame(int count, AvailablePiece* pieces) @@ -344,17 +346,24 @@ void GameController::startLevel(QString fileName) 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) @@ -655,7 +664,6 @@ void LevelSwitcher::onLevelFailed() // more levels // make fixed pipes look different than non-fixed ones // color theme -// transparency // -------------- // re-placing pieces // graphical hints on what to do next diff --git a/src/game.h b/src/game.h index e253ee0..99af4b2 100644 --- a/src/game.h +++ b/src/game.h @@ -83,9 +83,9 @@ static const Piece ppieces[] = { {PieceCross, 0, true, 3, 0, // 7 {DirLeft, DirRight, DirUp, DirDown}}, {PieceCorners, 0, true, 4, 0, // 8 - {DirLeft, DirUp, DirRight, DirDown}}, + {DirLeft, DirDown, DirRight, DirUp}}, {PieceCorners, 90, true, 4, 1, // 9 - {DirUp, DirRight, DirLeft, DirDown}}, + {DirUp, DirLeft, DirRight, DirDown}}, {PieceStart, 0, false, 0, 0, // 10 {DirLeft, DirNone, DirNone, DirNone}}, {PieceStart, 90, false, 0, 0, // 11 @@ -171,11 +171,9 @@ private slots: void onItemClicked(QTableWidgetItem* item); private: - void initPieceCache(); static int pieceToId(const Piece* piece); static const Piece* idToPiece(int id); - static QHash, const Piece*> pieceCache; QTableWidget* pieceUi; QHash pieceCounts; }; -- 1.7.9.5