Commit 707fcfe0 authored by Ian Wadham's avatar Ian Wadham

Implemented KGoldrunner rules. Started tidying up KGrGame class.

svn path=/branches/work/kgoldrunner/; revision=939612
parent b57e1fad
......@@ -486,16 +486,17 @@ void KGrCanvas::mouseReleaseEvent (QMouseEvent * mouseEvent)
emit mouseLetGo (mouseEvent->button());
}
QPoint KGrCanvas::getMousePos()
void KGrCanvas::getMousePos (int & i, int & j)
// QPoint KGrCanvas::getMousePos()
{
int i, j;
// int i, j;
QPoint p = mapFromGlobal (m->pos());
// In KGoldrunner, the top-left visible cell is [1,1]: in KGrSprite [0,0].
i = ((p.x() - topLeft.x()) / imgW) + 1;
j = ((p.y() - topLeft.y()) / imgH) + 1;
return (QPoint (i, j));
//
// return (QPoint (i, j));
}
void KGrCanvas::setMousePos (const int i, const int j)
......
......@@ -45,8 +45,6 @@ public:
const QString & systemDataDir);
virtual ~KGrCanvas();
QPoint getMousePos();
void setBaseScale();
void setTitle (const QString&);
......@@ -73,6 +71,7 @@ public:
inline void setGoldEnemiesRule (bool showIt) { enemiesShowGold = showIt;}
public slots:
void getMousePos (int & i, int & j);
void setMousePos (const int, const int);
void animate (bool missed);
void paintCell (const int i, const int j, const char type,
......
......@@ -828,10 +828,10 @@ bool KGrEditor::saveGameData (Owner o)
void KGrEditor::doEdit (int button)
{
// Mouse button down: start making changes.
QPoint p;
QPoint p (1, 1); // TODO - Fix mouse usage.
int i, j;
p = view->getMousePos();
// p = view->getMousePos();
i = p.x(); j = p.y();
switch (button) {
......@@ -855,10 +855,10 @@ void KGrEditor::doEdit (int button)
void KGrEditor::endEdit (int button)
{
// Mouse button released: finish making changes.
QPoint p;
QPoint p (1, 1); // TODO - Fix mouse usage.
int i, j;
p = view->getMousePos();
// p = view->getMousePos();
i = p.x(); j = p.y();
switch (button) {
......
......@@ -10,17 +10,13 @@
#include "kgrgame.h"
#include "kgrconsts.h"
// #include "kgrobject.h"
// #include "kgrfigure.h" // OBSOLESCENT - 09/1/09
// #include "kgrrunner.h" // OBSOLESCENT - 18/1/09
#include "kgrconsts.h" // OBSOLESCENT - 14/3/09
#include "kgrcanvas.h"
#include "kgrdialog.h"
#include "kgrsoundbank.h"
#include "kgreditor.h"
#include "kgrlevelplayer.h" // TESTING - 1/1/09
#include "kgrlevelplayer.h"
// Obsolete - #include <iostream.h>
#include <iostream>
#include <stdlib.h>
#include <ctype.h>
......@@ -84,8 +80,6 @@ KGrGame::KGrGame (KGrCanvas * theView,
// hero->setParent (this); // Delete hero when KGrGame is deleted.
// hero->setPlayfield (&playfield);
setBlankLevel (true); // Fill the playfield with blank walls.
// OBSOLESCENT - 18/1/09 enemy = NULL;
newLevel = true; // Next level will be a new one.
loading = true; // Stop input until it is loaded.
......@@ -115,20 +109,9 @@ KGrGame::KGrGame (KGrCanvas * theView,
#endif
// REPLACE - 9/1/09 Hero's game connections.
// connect (hero, SIGNAL (gotNugget (int)), SLOT (incScore (int)));
// connect (hero, SIGNAL (caughtHero()), SLOT (herosDead()));
// connect (hero, SIGNAL (haveAllNuggets()), SLOT (showHiddenLadders()));
// connect (hero, SIGNAL (leaveLevel()), SLOT (levelCompleted()));
dyingTimer = new QTimer (this);
connect (dyingTimer, SIGNAL (timeout()), SLOT (finalBreath()));
// Get the mouse position every 40 msec. It is used to steer the hero.
mouseSampler = new QTimer (this);
connect (mouseSampler, SIGNAL (timeout()), SLOT (readMousePos()));
mouseSampler->start (40);
srand (time (0)); // Initialise random number generator.
}
......@@ -155,7 +138,7 @@ void KGrGame::gameActions (int action)
break;
case KILL_HERO:
kDebug() << "KILL_HERO signal:" << action;
herosDead();
endLevel (DEAD);
break;
default:
break;
......@@ -388,6 +371,9 @@ void KGrGame::endLevel (const int result)
delete levelPlayer;
levelPlayer = 0;
}
else {
return; // Not playing a level.
}
if (result == WON_LEVEL) {
levelCompleted();
......@@ -399,8 +385,9 @@ void KGrGame::endLevel (const int result)
void KGrGame::herosDead()
{
if ((level < 1) || (lives <= 0))
if ((level < 1) || (lives <= 0)) {
return; // Game over: we are in the "ENDE" screen.
}
// Lose a life.
if (--lives > 0) {
......@@ -440,12 +427,9 @@ void KGrGame::herosDead()
}
// Game completely over: display the "ENDE" screen.
// OBSOLESCENT - 18/1/09 enemyCount = 0;
//todo enemies.clear(); // Stop the enemies catching the hero again ...
// while (!enemies.isEmpty())
// delete enemies.takeFirst(); // OBSOLETE - 9/1/09
// OBSOLESCENT - 29/1/09 view->deleteEnemySprites();
// TODO - The "ENDE" screen is displayed but it is frozen.
// TODO - If we restart the game on the "ENDE" screen then use the New
// Game dialog, the new level comes up but animation is haywire.
unfreeze(); // ... NOW we can unfreeze.
newLevel = true;
level = 0;
......@@ -459,7 +443,6 @@ void KGrGame::finalBreath()
// Fix bug 95202: Avoid re-starting if the player selected
// edit mode before the 1.5 seconds were up.
if (! editMode) {
// OBSOLESCENT - 18/1/09 enemyCount = 0; // Hero is dead: re-start the level.
loadLevel (level);
if (levelPlayer) {
levelPlayer->prepareToPlay();
......@@ -470,14 +453,9 @@ void KGrGame::finalBreath()
void KGrGame::showHiddenLadders()
{
// TODO - Move this line to KGrLevelPlayer.
effects->play (fx[LadderSound]);
int i, j;
for (i = 1; i < 21; i++)
for (j = 1; j < 29; j++)
; // OBSOLESCENT - 20/1/09 Need to compile after kgrobject.cpp removed.
// if (playfield[j][i]->whatIam() == HLADDER)
// ((KGrHladder *)playfield[j][i])->showLadder();
// TODO - Remove initSearchMatrix();
initSearchMatrix();
}
......@@ -490,7 +468,6 @@ void KGrGame::levelCompleted()
view->fadeOut();
}
//
void KGrGame::goUpOneLevel()
{
disconnect (view, SIGNAL (fadeFinished()), this, SLOT (goUpOneLevel()));
......@@ -514,26 +491,12 @@ void KGrGame::goUpOneLevel()
emit showLevel (level);
}
// TODO - Need to delete old level, sprites and graphics.
// OBSOLESCENT - 29/1/09 view->deleteEnemySprites();
newLevel = true;
loadLevel (level);
showTutorialMessages (level);
newLevel = false;
}
void KGrGame::loseNugget()
{
// OBSOLESCENT - 9/1/09 hero->loseNugget(); // Enemy trapped/dead and holding a nugget.
}
// OBSOLESCENT - 9/1/09
// KGrHero * KGrGame::getHero()
// {
// return (hero); // Return a pointer to the hero.
// }
void KGrGame::setControlMode (const Control mode)
{
controlMode = mode;
......@@ -605,43 +568,6 @@ void KGrGame::setMessageFreeze (bool on_off)
}
}
void KGrGame::setBlankLevel (bool playable)
{
// OBSOLESCENT - 20/1/09 KGrLevelGrid replaces playfield[][].
return; // Don't reference playfield[][].
for (int j = 0; j < 20; j++)
for (int i = 0; i < 28; i++) {
if (playable) {
// playfield[i+1][j+1] = new KGrFree (FREE, i+1, j+1, view);
}
else {
// playfield[i+1][j+1] = new KGrEditable (FREE);
view->paintCell (i+1, j+1, FREE);
}
// playfield[i+1][j+1]->setParent (this); // Delete if KGrGame dies.
// editObjArray[i+1][j+1] = FREE;
}
for (int j = 0; j < 30; j++) {
// playfield[j][0] = new KGrObject (BETON);
// playfield[j][0]->setParent (this); // Delete at end of KGrGame.
// editObjArray[j][0] = BETON;
// playfield[j][21] = new KGrObject (BETON);
// playfield[j][21]->setParent (this); // Delete at end of KGrGame.
// editObjArray[j][21] = BETON;
}
for (int i = 0; i < 22; i++) {
// playfield[0][i] = new KGrObject (BETON);
// playfield[0][i]->setParent (this); // Delete at end of KGrGame.
// editObjArray[0][i] = BETON;
// playfield[29][i] = new KGrObject (BETON);
// playfield[29][i]->setParent (this); // Delete at end of KGrGame.
// editObjArray[29][i] = BETON;
}
}
void KGrGame::newGame (const int lev, const int newGameIndex)
{
// Ignore player input from keyboard or mouse while the screen is set up.
......@@ -674,14 +600,6 @@ void KGrGame::newGame (const int lev, const int newGameIndex)
emit showScore (score);
emit showLevel (level);
// OBSOLESCENT - 18/1/09 enemyCount = 0;
//enemies.clear();
// OBSOLESCENT - 9/1/09 while (!enemies.isEmpty())
// OBSOLESCENT - 9/1/09 delete enemies.takeFirst();
// OBSOLESCENT - 29/1/09 view->deleteEnemySprites();
newLevel = true;;
loadLevel (level);
newLevel = false;
......@@ -756,7 +674,8 @@ int KGrGame::loadLevel (int levelNo)
view->fadeIn(); // Then run the fade-in animation.
startScore = score; // The score we will save, if asked.
kDebug() << "Prefix" << gameData->prefix << "index" << gameIndex << "of" << gameList.count();
kDebug() << "Prefix" << gameData->prefix << "index" << gameIndex
<< "of" << gameList.count();
levelPlayer = new KGrLevelPlayer (this);
......@@ -779,23 +698,6 @@ int KGrGame::loadLevel (int levelNo)
// disconnect (view, SIGNAL (mouseClick (int)), 0, 0);
// disconnect (view, SIGNAL (mouseLetGo (int)), 0, 0);
// if (newLevel) {
// OBSOLESCENT - 9/1/09 hero->setEnemyList (&enemies);
// QListIterator<KGrEnemy *> i (enemies);
// while (i.hasNext()) {
// OBSOLESCENT - 9/1/09 KGrEnemy * enemy = i.next();
// OBSOLESCENT - 9/1/09 enemy->setEnemyList (&enemies);
// }
// }
setTimings();
// Make a new sequence of all possible x co-ordinates for enemy rebirth.
// OBSOLESCENT - 9/1/09
// if (KGrFigure::reappearAtTop && (enemies.count() > 0)) {
// KGrEnemy::makeReappearanceSequence();
// }
// Set direction-flags to use during enemy searches.
initSearchMatrix();
......@@ -878,34 +780,9 @@ bool KGrGame::readLevelData (int levelNo, KGrLevelData & d)
}
// TODO - Connect these somewhere else in the code, e.g. in KGrLevelPlayer.
// connect (enemy, SIGNAL (lostNugget()), SLOT (loseNugget()));
// connect (enemy, SIGNAL (trapped (int)), SLOT (incScore (int)));
// connect (enemy, SIGNAL (killed (int)), SLOT (incScore (int)));
void KGrGame::setTimings()
{
// OBSOLESCENT - 9/1/09
// Timing * timing;
// int c = -1;
// OBSOLESCENT - 9/1/09
// if (KGrFigure::variableTiming) {
// c = enemies.count(); // Timing based on enemy count.
// c = (c > 5) ? 5 : c;
// timing = &(KGrFigure::varTiming[c]);
// }
// else {
// timing = &(KGrFigure::fixedTiming); // Fixed timing.
// }
// KGrHero::WALKDELAY = timing->hwalk;
// KGrHero::FALLDELAY = timing->hfall;
// KGrEnemy::WALKDELAY = timing->ewalk;
// KGrEnemy::FALLDELAY = timing->efall;
// KGrEnemy::CAPTIVEDELAY = timing->ecaptive;
// KGrBrick::HOLETIME = timing->hole;
}
void KGrGame::initSearchMatrix()
{
// OBSOLESCENT - 20/1/09 Should be replaced by KGrLevelGrid object.
......@@ -947,18 +824,6 @@ void KGrGame::initSearchMatrix()
// }
// }
}
void KGrGame::startPlaying() {
// OBSOLESCENT - 9/1/09
// if (! hero->started) {
// // Start the enemies and the hero.
// for (--enemyCount; enemyCount>=0; --enemyCount) {
// enemy=enemies.at (enemyCount);
// enemy->startSearching();
// }
// hero->start();
// }
}
QString KGrGame::getDirectory (Owner o)
{
......@@ -1016,52 +881,52 @@ QString KGrGame::getTitle()
return (levelTitle);
}
void KGrGame::readMousePos()
{
QPoint p;
int i, j;
// void KGrGame::readMousePos()
// {
// QPoint p;
// int i, j;
// If loading a level for play or editing, ignore mouse-position input.
if (loading) return;
// if (loading) return;
// If game control is currently by keyboard, ignore the mouse.
if ((controlMode == KEYBOARD) && (! editMode) && (! gameFrozen)) {
// TODO - Have internal clock in levelplayer. levelPlayer->tick ();
return;
}
// if ((controlMode == KEYBOARD) && (! editMode) && (! gameFrozen)) {
// // TODO - Have internal clock in levelplayer. levelPlayer->tick ();
// return;
// }
p = view->getMousePos();
i = p.x(); j = p.y();
// p = view->getMousePos();
// i = p.x(); j = p.y();
if (editMode) {
// Editing - check if we are in paint mode and have moved the mouse.
if (paintEditObj && ((i != oldI) || (j != oldJ))) {
// Force compile IDW insertEditObj (i, j, editObj);
oldI = i;
oldJ = j;
}
if (paintAltObj && ((i != oldI) || (j != oldJ))) {
// Force compile IDW insertEditObj (i, j, FREE);
oldI = i;
oldJ = j;
}
// Highlight the cursor position
// if (editMode) {
// // Editing - check if we are in paint mode and have moved the mouse.
// if (paintEditObj && ((i != oldI) || (j != oldJ))) {
// // Force compile IDW insertEditObj (i, j, editObj);
// oldI = i;
// oldJ = j;
// }
// if (paintAltObj && ((i != oldI) || (j != oldJ))) {
// // Force compile IDW insertEditObj (i, j, FREE);
// oldI = i;
// oldJ = j;
// }
// // Highlight the cursor position
}
else {
// Playing - if the level has started, control the hero.
// Track the mouse, even when the game is frozen.
// if (gameFrozen) return; // If game is stopped, do nothing.
// }
// else {
// // Playing - if the level has started, control the hero.
// // Track the mouse, even when the game is frozen.
// // if (gameFrozen) return; // If game is stopped, do nothing.
if (levelPlayer) { // OBSOLESCENT - 7/1/09 - Should be sure it exists.
levelPlayer->setTarget (i, j);
}
}
// if (levelPlayer) { // OBSOLESCENT - 7/1/09 - Should be sure it exists.
// levelPlayer->setTarget (i, j);
// }
// }
if (! gameFrozen) { // If game is stopped, do nothing.
// TODO - Have internal clock in levelplayer. levelPlayer->tick();
}
}
// if (! gameFrozen) { // If game is stopped, do nothing.
// // TODO - Have internal clock in levelplayer. levelPlayer->tick();
// }
// }
void KGrGame::kbControl (int dirn)
{
......@@ -1109,20 +974,6 @@ void KGrGame::kbControl (int dirn)
}
}
void KGrGame::heroAction (KBAction movement)
{
// OBSOLESCENT - 7/1/09
switch (movement) {
case KB_UP: break; // hero->setKey (UP); break;
case KB_DOWN: break; // hero->setKey (DOWN); break;
case KB_LEFT: break; // hero->setKey (LEFT); break;
case KB_RIGHT: break; // hero->setKey (RIGHT); break;
case KB_STOP: break; // hero->setKey (STAND); break;
case KB_DIGLEFT: break; // hero->setKey (STAND); hero->digLeft(); break;
case KB_DIGRIGHT: break; // hero->setKey (STAND); hero->digRight(); break;
}
}
/******************************************************************************/
/************************** SAVE AND RE-LOAD GAMES **************************/
/******************************************************************************/
......@@ -1135,6 +986,7 @@ void KGrGame::saveGame() // Save game ID, score and level.
i18n ("&Save Edits...")));
return;
}
// TODO - Smart save ...
// OBSOLESCENT - 7/1/09
// if (hero->started) {myMessage (view, i18n ("Save Game"),
// i18n ("Please note: for reasons of simplicity, your saved game "
......@@ -1975,19 +1827,5 @@ void KGrGame::myMessage (QWidget * parent, const QString &title, const QString &
setMessageFreeze (false);
}
// OBSOLESCENT - 31/1/09. Replaced by KGrGameData.
/******************************************************************************/
/*********************** COLLECTION DATA CLASS **************************/
/******************************************************************************/
//
// KGrCollection::KGrCollection (Owner o, const QString & n, const QString & p,
// const char s, int nl, const QString & a, const char sk = 'N')
// {
// // Holds information about a collection of KGoldrunner levels (i.e. a game).
// owner = o; name = n; prefix = p; settings = s; nLevels = nl;
// about = a; skill = sk;
// }
#include "kgrgame.moc"
// vi: set sw=4 :
......@@ -62,8 +62,6 @@ public:
int getLevel();
void startPlaying();
bool inMouseMode(); // True if the game is in mouse mode.
bool inEditMode(); // True if the game is in editor mode.
bool isLoading(); // True if a level is being loaded.
......@@ -112,8 +110,6 @@ public slots:
void showHiddenLadders(); // Show hidden ladders (nuggets gone).
void levelCompleted(); // Hero completed the level.
void goUpOneLevel(); // Start next level.
void loseNugget(); // Nugget destroyed (not collected).
void heroAction (KBAction movement);// Move hero under keyboard control.
void saveGame(); // Save game ID, score and level.
void loadGame(); // Re-load game, score and level.
......@@ -153,13 +149,10 @@ private slots:
private slots:
void finalBreath(); // Hero is dead: re-start the level.
void readMousePos(); // Timed reading of mouse position.
private:
void setBlankLevel (bool playable);
int loadLevel (int levelNo);
bool readLevelData (int levelNo, KGrLevelData & d);
void setTimings();
void initSearchMatrix();
void showTutorialMessages (int levelNo);
......@@ -200,7 +193,6 @@ private:
bool modalFreeze; // Stop game during dialog.
bool messageFreeze; // Stop game during message.
QTimer * mouseSampler; // Timer for mouse tracking.
QTimer * dyingTimer; // For pause when the hero dies.
int lgHighlight; // Row selected in "loadGame()".
......
......@@ -105,6 +105,8 @@ void KGrLevelPlayer::init (KGrCanvas * view, const Control mode,
view, SLOT (makeSprite (char, int, int)));
// Connect to the mouse-positioning code in the graphics.
connect (this, SIGNAL (getMousePos (int &, int &)),
view, SLOT (getMousePos (int &, int &)));
connect (this, SIGNAL (setMousePos (const int, const int)),
view, SLOT (setMousePos (const int, const int)));
......@@ -401,14 +403,15 @@ Direction KGrLevelPlayer::getDirection (int heroI, int heroJ)
return direction;
}
Direction KGrLevelPlayer::getEnemyDirection (int enemyI, int enemyJ)
Direction KGrLevelPlayer::getEnemyDirection (int enemyI, int enemyJ,
bool leftRightSearch)
{
int heroX, heroY, pointsPerCell;
pointsPerCell = hero->whereAreYou (heroX, heroY);
return rules->findBestWay (enemyI, enemyJ,
heroX / pointsPerCell, heroY / pointsPerCell,
grid);
grid, leftRightSearch);
}
bool KGrLevelPlayer::heroCaught (const int heroX, const int heroY)
......@@ -526,6 +529,10 @@ void KGrLevelPlayer::unstackEnemy (const int spriteId,
void KGrLevelPlayer::tick (bool missed, int scaledTime)
{
int i, j;
emit getMousePos (i, j);
setTarget (i, j);
if (playState != Playing) {
return;
}
......@@ -700,39 +707,6 @@ void KGrLevelPlayer::dbgControl (int code)
// OBSOLESCENT - 21/1/09 Can do this just by calling tick().
void KGrLevelPlayer::restart()
{
// bool temp;
// int i,j;
// if (editMode) // Can't move figures when in Edit Mode.
// return;
// temp = gameFrozen;
// gameFrozen = false; // Temporarily restart the game, by re-running
// any timer events that have been blocked.
// OBSOLESCENT - 7/1/09
// readMousePos(); // Set hero's direction.
// hero->doStep(); // Move the hero one step.
// OBSOLESCENT - 7/1/09
// j = enemies.count(); // Move each enemy one step.
// for (i = 0; i < j; i++) {
// enemy = enemies.at (i); // Need to use an index because called methods
// enemy->doStep(); // change the "current()" of the "enemies" list.
// }
// OBSOLESCENT - 20/1/09 Need to compile after kgrobject.cpp removed.
// for (i = 1; i <= 28; i++)
// for (j = 1; j <= 20; j++) {
// if ((playfield[i][j]->whatIam() == HOLE) ||
// (playfield[i][j]->whatIam() == USEDHOLE) ||
// (playfield[i][j]->whatIam() == BRICK))
// ((KGrBrick *)playfield[i][j])->doStep();
// }
// gameFrozen = temp; // If frozen was true, halt again, which gives a
// single-step effect, otherwise go on running.
}
void KGrLevelPlayer::bugFix()
......
......@@ -140,11 +140,12 @@ public:
*
* @param enemyI The column-number where the enemy is now (>=1).
* @param enemyJ The row-number where the enemy is now (>=1).
* @param leftRightSearch The search-direction (for KGoldrunner rules only).
*
* @return The required direction (values defined by enum Direction
* in file kgrglobals.h).
*/
Direction getEnemyDirection (int enemyI, int enemyJ);
Direction getEnemyDirection (int enemyI, int enemyJ, bool leftRightSearch);
/**
* Helper function for an enemy to pick up or drop gold or the hero to
......@@ -251,6 +252,7 @@ public:
signals:
void endLevel (const int result);
void getMousePos (int & i, int & j);
void setMousePos (const int i, const int j);
void animation (bool missed);
void paintCell (int i, int j, char tileType, int diggingStage = 0);
......
......@@ -22,7 +22,7 @@
KGrRuleBook::KGrRuleBook (QObject * parent)
:
QObject (parent),
QObject (parent),
mVariableTiming (true),
mAlwaysCollectNugget (true),
mRunThruHole (true),
......@@ -62,6 +62,8 @@ KGrTraditionalRules::KGrTraditionalRules (QObject * parent)
:
KGrRuleBook (parent)
{
mRules = TraditionalRules;
mVariableTiming = true; ///< More enemies imply less speed.
mAlwaysCollectNugget = true; ///< Enemies always collect nuggets.
mRunThruHole = true; ///< Enemy can run L/R through dug hole.
......@@ -81,7 +83,8 @@ KGrTraditionalRules::~KGrTraditionalRules()
Direction KGrTraditionalRules::findBestWay (const int eI, const int eJ,
const int hI, const int hJ,
KGrLevelGrid * pGrid)
KGrLevelGrid * pGrid,
bool /* leftRightSearch unused */)
// TODO - Should be const ... const KGrLevelGrid * pGrid)
{
grid = pGrid;
......@@ -479,8 +482,10 @@ bool KGrTraditionalRules::willNotFall (int x, int y)
KGrKGoldrunnerRules::KGrKGoldrunnerRules (QObject * parent)
: