#include <QApplication>
#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)
{
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)
// more levels
// make fixed pipes look different than non-fixed ones
// color theme
-// transparency
// --------------
// re-placing pieces
// graphical hints on what to do next
{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
void onItemClicked(QTableWidgetItem* item);
private:
- void initPieceCache();
static int pieceToId(const Piece* piece);
static const Piece* idToPiece(int id);
- static QHash<QPair<PieceType, int>, const Piece*> pieceCache;
QTableWidget* pieceUi;
QHash<const Piece*, int> pieceCounts;
};