PageRenderTime 66ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/kdegames-4.8.97/kgoldrunner/src/kgrlevelplayer.cpp

#
C++ | 1277 lines | 1018 code | 133 blank | 126 comment | 272 complexity | a5d72cc0c0ba630e2a7d5eb8d70366e3 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0
  1. #include "kgrdebug.h"
  2. /****************************************************************************
  3. * Copyright 2009 Ian Wadham <iandw.au@gmail.com> *
  4. * *
  5. * This program is free software; you can redistribute it and/or *
  6. * modify it under the terms of the GNU General Public License as *
  7. * published by the Free Software Foundation; either version 2 of *
  8. * the License, or (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ****************************************************************************/
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. // Include kgrgame.h only to access flags KGrGame::bugFix and KGrGame::logging.
  21. #include "kgrgame.h"
  22. #include "kgrtimer.h"
  23. #include "kgrcanvas.h"
  24. #include "kgrlevelplayer.h"
  25. #include "kgrrulebook.h"
  26. #include "kgrlevelgrid.h"
  27. #include "kgrrunner.h"
  28. #include <KDebug>
  29. #include <KMessageBox> // TODO - Remove.
  30. #include <KRandomSequence>
  31. KGrLevelPlayer::KGrLevelPlayer (QObject * parent, KRandomSequence * pRandomGen)
  32. :
  33. QObject (parent),
  34. game (parent),
  35. randomGen (pRandomGen),
  36. hero (0),
  37. controlMode (MOUSE),
  38. holdKeyOption (CLICK_KEY),
  39. nuggets (0),
  40. playState (NotReady),
  41. recording (0),
  42. playback (false),
  43. targetI (1),
  44. targetJ (1),
  45. direction (NO_DIRECTION),
  46. newDirection (NO_DIRECTION),
  47. timer (0),
  48. digCycleTime (200), // Milliseconds per dig-timing cycle (default).
  49. digCycleCount (40), // Cycles while hole is fully open (default).
  50. digOpeningCycles (5), // Cycles for brick-opening animation.
  51. digClosingCycles (4), // Cycles for brick-closing animation.
  52. digKillingTime (2), // Cycle at which enemy/hero gets killed.
  53. dX (0), // X motion for KEYBOARD + HOLD_KEY option.
  54. dY (0) // Y motion for KEYBOARD + HOLD_KEY option.
  55. {
  56. t.start(); // IDW
  57. dbgLevel = 0;
  58. }
  59. int KGrLevelPlayer::playerCount = 0;
  60. KGrLevelPlayer::~KGrLevelPlayer()
  61. {
  62. qDeleteAll(dugBricks);
  63. dugBricks.clear(); //TODO: necessary?
  64. kDebug() << "LEVEL PLAYER BEING DELETED.";
  65. playerCount--;
  66. // TODO - Remove this debugging code.
  67. if (recording) {
  68. int ch = 0;
  69. for (int i = 0; i <= (recIndex + 1); i ++) {
  70. ch = (uchar)(recording->content.at(i));
  71. dbe1 "%03d ", ch);
  72. if (ch == 0)
  73. break;
  74. }
  75. dbe1 "\n%d bytes\n", recIndex + 1);
  76. int j = 0;
  77. while (j < recording->draws.size()) {
  78. ch = (uchar)(recording->draws.at(j));
  79. dbe1 "%03d ", ch);
  80. if (ch == 0)
  81. break;
  82. j++;
  83. }
  84. dbe1 "\n%d bytes\n", j);
  85. }
  86. }
  87. void KGrLevelPlayer::init (KGrCanvas * view,
  88. KGrRecording * pRecording,
  89. const bool pPlayback,
  90. const bool gameFrozen)
  91. {
  92. // TODO - Remove?
  93. playerCount++;
  94. if (playerCount > 1) {
  95. KMessageBox::information (view,
  96. QString("ERROR: KGrLevelPlayer Count = %1").arg(playerCount),
  97. "KGrLevelPlayer");
  98. }
  99. recording = pRecording;
  100. playback = pPlayback;
  101. // Create the internal model of the level-layout.
  102. grid = new KGrLevelGrid (this, recording);
  103. // Set mouse/keyboard/laptop control and click/hold option for keyboard.
  104. controlMode = recording->controlMode;
  105. holdKeyOption = recording->keyOption;
  106. dX = 0; // X motion for KEYBOARD + HOLD_KEY.
  107. dY = 0; // Y motion for KEYBOARD + HOLD_KEY.
  108. levelWidth = recording->width;
  109. levelHeight = recording->height;
  110. reappearIndex = levelWidth; // Initialise the enemy-rebirth code.
  111. reappearPos.fill (1, levelWidth);
  112. // Set the rules of this game.
  113. switch (recording->rules) {
  114. case TraditionalRules:
  115. rules = new KGrTraditionalRules (this);
  116. break;
  117. case KGoldrunnerRules:
  118. rules = new KGrKGoldrunnerRules (this);
  119. break;
  120. case ScavengerRules:
  121. rules = new KGrScavengerRules (this);
  122. break;
  123. }
  124. recIndex = 0;
  125. recCount = 0;
  126. randIndex = 0;
  127. T = 0;
  128. view->setGoldEnemiesRule (rules->enemiesShowGold());
  129. // Determine the access for hero and enemies to and from each grid-cell.
  130. grid->calculateAccess (rules->runThruHole());
  131. // Connect to code that paints grid cells and start-positions of sprites.
  132. connect (this, SIGNAL (paintCell(int,int,char,int)),
  133. view, SLOT (paintCell(int,int,char,int)));
  134. connect (this, SIGNAL (makeSprite(char,int,int)),
  135. view, SLOT (makeSprite(char,int,int)));
  136. // Connect to the mouse-positioning code in the graphics.
  137. connect (this, SIGNAL (getMousePos(int&,int&)),
  138. view, SLOT (getMousePos(int&,int&)));
  139. connect (this, SIGNAL (setMousePos(int,int)),
  140. view, SLOT (setMousePos(int,int)));
  141. // Show the layout of this level in the view (KGrCanvas).
  142. int wall = ConcreteWall;
  143. int enemyCount = 0;
  144. for (int j = wall ; j < levelHeight + wall; j++) {
  145. for (int i = wall; i < levelWidth + wall; i++) {
  146. char type = grid->cellType (i, j);
  147. // Hide false bricks.
  148. if (type == FBRICK) {
  149. type = BRICK;
  150. }
  151. // Count the gold in this level.
  152. if (type == NUGGET) {
  153. nuggets++;
  154. }
  155. // If the hero is here, leave the tile empty.
  156. if (type == HERO) {
  157. emit paintCell (i, j, FREE, 0);
  158. }
  159. // If an enemy is here, count him and leave the tile empty.
  160. else if (type == ENEMY) {
  161. enemyCount++;
  162. emit paintCell (i, j, FREE, 0);
  163. }
  164. // Or, just paint this tile.
  165. else {
  166. emit paintCell (i, j, type, 0);
  167. }
  168. }
  169. }
  170. // Set the timing rules, maybe based on the number of enemies.
  171. rules->setTiming (enemyCount);
  172. rules->getDigTimes (digCycleTime, digCycleCount);
  173. // Create the hero (always sprite 0), with the proper timing.
  174. for (int j = wall ; j < levelHeight + wall; j++) {
  175. for (int i = wall; i < levelWidth + wall; i++) {
  176. char type = grid->cellType (i, j);
  177. if (type == HERO) {
  178. if (hero == 0) {
  179. targetI = i;
  180. targetJ = j;
  181. heroId = emit makeSprite (HERO, i, j);
  182. hero = new KGrHero (this, grid, i, j, heroId, rules);
  183. hero->setNuggets (nuggets);
  184. if ((controlMode == MOUSE) || (controlMode == LAPTOP)) {
  185. emit setMousePos (targetI, targetJ);
  186. }
  187. grid->changeCellAt (i, j, FREE); // Hero now a sprite.
  188. }
  189. }
  190. }
  191. }
  192. // Create the enemies (sprites 1-n), with the proper timing.
  193. for (int j = wall ; j < levelHeight + wall; j++) {
  194. for (int i = wall; i < levelWidth + wall; i++) {
  195. char type = grid->cellType (i, j);
  196. if (type == ENEMY) {
  197. KGrEnemy * enemy;
  198. int id = emit makeSprite (ENEMY, i, j);
  199. enemy = new KGrEnemy (this, grid, i, j, id, rules);
  200. enemies.append (enemy);
  201. grid->changeCellAt (i, j, FREE); // Enemy now a sprite.
  202. grid->setEnemyOccupied (i, j, id);
  203. }
  204. }
  205. }
  206. // Connect the hero's and enemies' efforts to the graphics.
  207. connect (this, SIGNAL (gotGold(int,int,int,bool,bool)),
  208. view, SLOT (gotGold(int,int,int,bool,bool)));
  209. // Connect mouse-clicks from KGrCanvas to digging slot.
  210. connect (view, SIGNAL (mouseClick(int)), SLOT (doDig(int)));
  211. // Connect the hero and enemies (if any) to the animation code.
  212. connect (hero, SIGNAL (startAnimation (int, bool, int, int, int,
  213. Direction, AnimationType)),
  214. view, SLOT (startAnimation (int, bool, int, int, int,
  215. Direction, AnimationType)));
  216. foreach (KGrEnemy * enemy, enemies) {
  217. connect (enemy, SIGNAL (startAnimation (int, bool, int, int, int,
  218. Direction, AnimationType)),
  219. view, SLOT (startAnimation (int, bool, int, int, int,
  220. Direction, AnimationType)));
  221. }
  222. // Connect the scoring.
  223. connect (hero, SIGNAL (incScore(int)),
  224. game, SLOT (incScore(int)));
  225. foreach (KGrEnemy * enemy, enemies) {
  226. connect (enemy, SIGNAL (incScore(int)),
  227. game, SLOT (incScore(int)));
  228. }
  229. // Connect the sounds.
  230. connect (hero, SIGNAL (soundSignal(int,bool)),
  231. game, SLOT (playSound(int,bool)));
  232. // Connect the level player to the animation code (for use with dug bricks).
  233. connect (this, SIGNAL (startAnimation (int, bool, int, int, int,
  234. Direction, AnimationType)),
  235. view, SLOT (startAnimation (int, bool, int, int, int,
  236. Direction, AnimationType)));
  237. connect (this, SIGNAL (deleteSprite(int)),
  238. view, SLOT (deleteSprite(int)));
  239. // Connect the grid to the view, to show hidden ladders when the time comes.
  240. connect (grid, SIGNAL (showHiddenLadders(QList<int>,int)),
  241. view, SLOT (showHiddenLadders(QList<int>,int)));
  242. // Connect and start the timer. The tick() slot emits signal animation(),
  243. // so there is just one time-source for the model and the view.
  244. timer = new KGrTimer (this, TickTime); // TickTime def in kgrglobals.h.
  245. if (gameFrozen) {
  246. timer->pause(); // Pause is ON as level starts.
  247. }
  248. connect (timer, SIGNAL (tick(bool,int)), this, SLOT (tick(bool,int)));
  249. connect (this, SIGNAL (animation(bool)), view, SLOT (animate(bool)));
  250. if (! playback) {
  251. // Allow some time to view the level before starting a replay.
  252. recordInitialWaitTime (1500); // 1500 msec or 1.5 sec.
  253. }
  254. }
  255. void KGrLevelPlayer::startDigging (Direction diggingDirection)
  256. {
  257. int digI = 1;
  258. int digJ = 1;
  259. // We need the hero to decide if he CAN dig and if so, where.
  260. if (hero->dig (diggingDirection, digI, digJ)) {
  261. // The hero can dig as requested: the chosen brick is at (digI, digJ).
  262. grid->changeCellAt (digI, digJ, HOLE);
  263. // Start the brick-opening animation (non-repeating).
  264. int id = emit makeSprite (BRICK, digI, digJ);
  265. emit startAnimation (id, false, digI, digJ,
  266. (digOpeningCycles * digCycleTime), STAND, OPEN_BRICK);
  267. DugBrick * thisBrick = new DugBrick;
  268. DugBrick brick = {id, digCycleTime, digI, digJ,
  269. (digCycleCount + digOpeningCycles + digClosingCycles - 1),
  270. t.elapsed()}; // IDW test
  271. (* thisBrick) = brick;
  272. dugBricks.append (thisBrick);
  273. }
  274. }
  275. void KGrLevelPlayer::processDugBricks (const int scaledTime)
  276. {
  277. DugBrick * dugBrick;
  278. QMutableListIterator<DugBrick *> iterator (dugBricks);
  279. while (iterator.hasNext()) {
  280. dugBrick = iterator.next();
  281. dugBrick->cycleTimeLeft -= scaledTime;
  282. if (dugBrick->cycleTimeLeft < scaledTime) {
  283. dugBrick->cycleTimeLeft += digCycleTime;
  284. if (--dugBrick->countdown == digClosingCycles) {
  285. // Start the brick-closing animation (non-repeating).
  286. emit startAnimation (dugBrick->id, false,
  287. dugBrick->digI, dugBrick->digJ,
  288. (digClosingCycles * digCycleTime),
  289. STAND, CLOSE_BRICK);
  290. }
  291. if (dugBrick->countdown == digKillingTime) {
  292. // Close the hole and maybe capture the hero or an enemy.
  293. grid->changeCellAt (dugBrick->digI, dugBrick->digJ, BRICK);
  294. }
  295. if (dugBrick->countdown <= 0) {
  296. // Dispose of the dug brick and remove it from the list.
  297. emit deleteSprite (dugBrick->id);
  298. delete dugBrick;
  299. iterator.remove();
  300. }
  301. }
  302. }
  303. }
  304. void KGrLevelPlayer::prepareToPlay()
  305. {
  306. if ((controlMode == MOUSE) || (controlMode == LAPTOP)) {
  307. emit setMousePos (targetI, targetJ);
  308. }
  309. playState = Ready;
  310. }
  311. void KGrLevelPlayer::pause (bool stop)
  312. {
  313. if (stop) {
  314. timer->pause();
  315. }
  316. else {
  317. timer->resume();
  318. }
  319. }
  320. void KGrLevelPlayer::setTarget (int pointerI, int pointerJ)
  321. {
  322. // Mouse or other pointer device (eg. laptop touchpad) controls the hero.
  323. switch (playState) {
  324. case NotReady:
  325. // Ignore the pointer until KGrLevelPlayer is ready to start.
  326. break;
  327. case Ready:
  328. // Wait until the human player is ready to start playing.
  329. if ((pointerI == targetI) && (pointerJ == targetJ)) {
  330. // The pointer is still over the hero: do not start playing yet.
  331. break;
  332. }
  333. // The pointer moved: fall into "case Playing:" and start playing.
  334. else if (! playback) { // TODO - Remove debugging code (3 lines).
  335. T = 0;
  336. }
  337. playState = Playing;
  338. case Playing:
  339. // The human player is playing now.
  340. if (! playback) {
  341. record (3, pointerI, pointerJ);
  342. }
  343. targetI = pointerI;
  344. targetJ = pointerJ;
  345. break;
  346. }
  347. }
  348. void KGrLevelPlayer::doDig (int button)
  349. {
  350. // Click to end demo/playback mode.
  351. if (playback) {
  352. interruptPlayback();
  353. return;
  354. }
  355. // If not ready or game control is not by mouse, ignore mouse-clicks.
  356. if ((playState == NotReady) || (controlMode != MOUSE)) {
  357. return;
  358. }
  359. uchar recordByte = 0;
  360. playState = Playing;
  361. switch (button) {
  362. case Qt::LeftButton:
  363. recordByte = DIRECTION_CODE + DIG_LEFT;
  364. startDigging (DIG_LEFT);
  365. break;
  366. case Qt::RightButton:
  367. recordByte = DIRECTION_CODE + DIG_RIGHT;
  368. startDigging (DIG_RIGHT);
  369. break;
  370. default:
  371. break;
  372. }
  373. if (recordByte != 0) {
  374. // Record the digging action.
  375. record (1, recordByte);
  376. }
  377. }
  378. void KGrLevelPlayer::setDirectionByKey (const Direction dirn,
  379. const bool pressed)
  380. {
  381. // Keystrokes control the hero. KGrGame should avoid calling this during
  382. // playback, but better to be safe ...
  383. if (playback || (playState == NotReady) || (controlMode == MOUSE)) {
  384. return;
  385. }
  386. if ((dirn == DIG_LEFT) || (dirn == DIG_RIGHT)) {
  387. // Control mode is KEYBOARD or LAPTOP (hybrid: pointer + dig-keys).
  388. if (playState == Ready) {
  389. playState = Playing;
  390. T = 0;
  391. }
  392. if (controlMode == KEYBOARD) {
  393. // IDW What happens here if keyboard option is HOLD_KEY? What *should* happen?
  394. newDirection = STAND; // Stop a keyboard move when digging.
  395. }
  396. startDigging (dirn);
  397. record (1, (uchar) (DIRECTION_CODE + dirn));
  398. }
  399. else if (controlMode == KEYBOARD) {
  400. if (playState == Ready) {
  401. playState = Playing;
  402. T = 0;
  403. }
  404. // Start recording and acting on the new direction at the next tick.
  405. if ((holdKeyOption == CLICK_KEY) && pressed && (dirn != direction)) {
  406. newDirection = dirn;
  407. }
  408. else if (holdKeyOption == HOLD_KEY) {
  409. int sign = pressed ? +1 : -1;
  410. dX = dX + sign * movement [dirn][X];
  411. dY = dY + sign * movement [dirn][Y];
  412. }
  413. }
  414. }
  415. Direction KGrLevelPlayer::getDirection (int heroI, int heroJ)
  416. {
  417. if ((controlMode == MOUSE) || (controlMode == LAPTOP)) {
  418. int index = (playback) ? recIndex : recIndex - 2;
  419. dbe2 "T %04d recIndex %03d hero at [%02d, %02d] aiming at [%02d, %02d]\n",
  420. T, index, heroI, heroJ, targetI, targetJ);
  421. // If using a pointer device, calculate the hero's next direction,
  422. // starting from last pointer position and hero's current position.
  423. direction = setDirectionByDelta (targetI - heroI, targetJ - heroJ,
  424. heroI, heroJ);
  425. }
  426. else if ((controlMode == KEYBOARD) && (holdKeyOption == HOLD_KEY)) {
  427. // If using the hold/release key option, resolve diagonal directions
  428. // (if there are multi-key holds) into either horizontal or vertical.
  429. direction = setDirectionByDelta (dX, dY, heroI, heroJ);
  430. dbe2 "T %04d recIndex %03d delta [%02d, %02d] "
  431. "hero at [%02d, %02d] direction %d\n",
  432. T, recIndex - 1, dX, dY, heroI, heroJ, direction);
  433. }
  434. return direction;
  435. }
  436. Direction KGrLevelPlayer::setDirectionByDelta (const int di, const int dj,
  437. const int heroI, const int heroJ)
  438. {
  439. Direction dirn = STAND;
  440. if ((dj > 0) && (grid->heroMoves (heroI, heroJ) & dFlag [DOWN])) {
  441. dirn = DOWN;
  442. }
  443. else if ((dj < 0) && (grid->heroMoves (heroI, heroJ) & dFlag [UP])) {
  444. dirn = UP;
  445. }
  446. else if (di > 0) {
  447. dirn = RIGHT;
  448. }
  449. else if (di < 0) {
  450. dirn = LEFT;
  451. }
  452. else { // Note: di is zero, but dj is not necessarily zero.
  453. dirn = STAND;
  454. }
  455. return dirn;
  456. }
  457. void KGrLevelPlayer::recordInitialWaitTime (const int ms)
  458. {
  459. // Allow a pause for viewing when playback starts.
  460. recCount = ms / TickTime; // Convert milliseconds-->ticks.
  461. if (controlMode == KEYBOARD) {
  462. recording->content [recIndex++] = (uchar) (DIRECTION_CODE +
  463. NO_DIRECTION);
  464. recording->content [recIndex] = (uchar) recCount;
  465. recording->content [recIndex + 1] = (uchar) END_CODE;
  466. }
  467. else {
  468. recording->content [recIndex++] = (uchar) targetI;
  469. recording->content [recIndex++] = (uchar) targetJ;
  470. recording->content [recIndex] = (uchar) recCount;
  471. recording->content [recIndex + 1] = (uchar) END_CODE;
  472. }
  473. }
  474. void KGrLevelPlayer::record (const int bytes, const int n1, const int n2)
  475. {
  476. if (playback) {
  477. return;
  478. }
  479. // Check for repetition of a previous direction-key or pointer posn. (I, J).
  480. if (recIndex > 2)
  481. dbe3 "recCount %d bytes %d n1 %d n2 %d [recIndex-1] %d [recIndex-2] %d\n",
  482. recCount, bytes, n1, n2, (uchar) recording->content.at (recIndex - 1),
  483. (uchar) recording->content.at (recIndex - 2));
  484. if ((recCount > 0) && (bytes > 1) && (recCount < (END_CODE - 1)) &&
  485. (((bytes == 2) && (n1 == (uchar) recording->content [recIndex - 1])) ||
  486. ((bytes == 3) && (n1 == (uchar) recording->content [recIndex - 2]) &&
  487. (n2 == (uchar) recording->content [recIndex - 1]))
  488. )) {
  489. // Count repetitions, up to a maximum of (END_CODE - 1) = 254.
  490. recording->content [recIndex] = (uchar) (++recCount);
  491. if (bytes == 2) {
  492. dbe2 "T %04d recIndex %03d REC: codes --- %3d %3d - recCount++\n",
  493. T, recIndex - 1, (uchar)(recording->content.at (recIndex-1)),
  494. (uchar)(recording->content.at (recIndex)));
  495. }
  496. else if (bytes == 3) {
  497. dbe2 "T %04d recIndex %03d REC: codes %3d %3d %3d - recCount++\n",
  498. T, recIndex - 2, (uchar)(recording->content.at (recIndex-2)),
  499. (uchar)(recording->content.at (recIndex-1)),
  500. (uchar)(recording->content.at (recIndex)));
  501. }
  502. return;
  503. }
  504. // Record a single code or the first byte of a new doublet or triplet.
  505. recCount = 0;
  506. recording->content [++recIndex] = (uchar) n1;
  507. if (bytes == 3) {
  508. // Record another byte for a triplet (i.e. the pointer's J position).
  509. recording->content [++recIndex] = (uchar) n2;
  510. }
  511. if (bytes > 1) {
  512. // Record a repetition-count of 1 for a new doublet or triplet.
  513. recCount = 1;
  514. recording->content [++recIndex] = (uchar) recCount;
  515. }
  516. switch (bytes) {
  517. case 1:
  518. dbe2 "T %04d recIndex %03d REC: singleton %3d %x\n",
  519. T, recIndex, n1, n1);
  520. break;
  521. case 2:
  522. dbe2 "T %04d recIndex %03d REC: codes %3d %3d %3d - NEW DIRECTION\n",
  523. T, recIndex - 1, direction,
  524. (uchar)(recording->content.at (recIndex-1)),
  525. (uchar)(recording->content.at (recIndex)));
  526. break;
  527. case 3:
  528. dbe2 "T %04d recIndex %03d REC: codes %3d %3d %3d - NEW TARGET\n",
  529. T, recIndex - 2, (uchar)(recording->content.at (recIndex-2)),
  530. (uchar)(recording->content.at (recIndex-1)),
  531. (uchar)(recording->content.at (recIndex)));
  532. break;
  533. default:
  534. break;
  535. }
  536. // Add the end-of-recording code (= 255).
  537. recording->content [recIndex + 1] = (uchar) END_CODE;
  538. return;
  539. }
  540. Direction KGrLevelPlayer::getEnemyDirection (int enemyI, int enemyJ,
  541. bool leftRightSearch)
  542. {
  543. int heroX, heroY, pointsPerCell;
  544. pointsPerCell = hero->whereAreYou (heroX, heroY);
  545. return rules->findBestWay (enemyI, enemyJ,
  546. heroX / pointsPerCell, heroY / pointsPerCell,
  547. grid, leftRightSearch);
  548. }
  549. bool KGrLevelPlayer::heroCaught (const int heroX, const int heroY)
  550. {
  551. if (enemies.count() == 0) {
  552. return false;
  553. }
  554. int enemyX, enemyY, pointsPerCell_1;
  555. foreach (KGrEnemy * enemy, enemies) {
  556. pointsPerCell_1 = enemy->whereAreYou (enemyX, enemyY) - 1;
  557. if (((heroX < enemyX) ? ((heroX + pointsPerCell_1) >= enemyX) :
  558. (heroX <= (enemyX + pointsPerCell_1))) &&
  559. ((heroY < enemyY) ? ((heroY + pointsPerCell_1) > enemyY) :
  560. (heroY <= (enemyY + pointsPerCell_1)))) {
  561. dbk << "Caught by";
  562. enemy->showState();
  563. return true;
  564. }
  565. }
  566. return false;
  567. }
  568. KGrEnemy * KGrLevelPlayer::standOnEnemy (const int spriteId,
  569. const int x, const int y)
  570. {
  571. int minEnemies = (spriteId == heroId) ? 1 : 2;
  572. if (enemies.count() < minEnemies) {
  573. return 0;
  574. }
  575. int enemyX, enemyY, pointsPerCell;
  576. foreach (KGrEnemy * enemy, enemies) {
  577. pointsPerCell = enemy->whereAreYou (enemyX, enemyY);
  578. if (((enemyY == (y + pointsPerCell)) ||
  579. (enemyY == (y + pointsPerCell - 1))) &&
  580. (enemyX > (x - pointsPerCell)) &&
  581. (enemyX < (x + pointsPerCell))) {
  582. return enemy;
  583. }
  584. }
  585. return 0;
  586. }
  587. bool KGrLevelPlayer::bumpingFriend (const int spriteId, const Direction dirn,
  588. const int gridI, const int gridJ)
  589. {
  590. int dI = 0;
  591. int dJ = 0;
  592. switch (dirn) {
  593. case LEFT:
  594. dI = -1;
  595. break;
  596. case RIGHT:
  597. dI = +1;
  598. break;
  599. case UP:
  600. dJ = -1;
  601. break;
  602. case DOWN:
  603. dJ = +1;
  604. break;
  605. default:
  606. break;
  607. }
  608. int otherEnemy;
  609. if (dI != 0) {
  610. otherEnemy = grid->enemyOccupied (gridI + dI, gridJ);
  611. if (otherEnemy > 0) {
  612. dbk3 << otherEnemy << "at" << (gridI + dI) << gridJ
  613. << "dirn" << ((otherEnemy > 0) ?
  614. (enemies.at (otherEnemy - 1)->direction()) : 0)
  615. << "me" << spriteId << "dirn" << dirn;
  616. if (enemies.at (otherEnemy - 1)->direction() != dirn) {
  617. dbk3 << spriteId << "wants" << dirn << ":" << otherEnemy
  618. << "at" << (gridI + dI) << gridJ << "wants"
  619. << (enemies.at (otherEnemy - 1)->direction());
  620. return true;
  621. }
  622. }
  623. }
  624. if (dJ != 0) {
  625. otherEnemy = grid->enemyOccupied (gridI, gridJ + dJ);
  626. if (otherEnemy > 0) {
  627. dbk3 << otherEnemy << "at" << gridI << (gridJ + dJ)
  628. << "dirn" << ((otherEnemy > 0) ?
  629. (enemies.at (otherEnemy - 1)->direction()) : 0)
  630. << "me" << spriteId << "dirn" << dirn;
  631. if (enemies.at (otherEnemy - 1)->direction() != dirn) {
  632. dbk3 << spriteId << "wants" << dirn << ":" << otherEnemy
  633. << "at" << gridI << (gridJ + dJ) << "wants"
  634. << (enemies.at (otherEnemy - 1)->direction());
  635. return true;
  636. }
  637. }
  638. }
  639. return false;
  640. }
  641. void KGrLevelPlayer::unstackEnemy (const int spriteId,
  642. const int gridI, const int gridJ,
  643. const int prevEnemy)
  644. {
  645. dbe2 "KGrLevelPlayer::unstackEnemy (%02d at [%02d,%02d] prevEnemy %02d)\n",
  646. spriteId, gridI, gridJ, prevEnemy);
  647. int nextId = grid->enemyOccupied (gridI, gridJ);
  648. int prevId;
  649. while (nextId > 0) {
  650. prevId = enemies.at (nextId - 1)->getPrevInCell();
  651. dbe2 "Next %02d prev %02d\n", nextId, prevId);
  652. if (prevId == spriteId) {
  653. dbe2 " SET IDs - id %02d prev %02d\n", nextId, prevEnemy);
  654. enemies.at (nextId - 1)->setPrevInCell (prevEnemy);
  655. // break;
  656. }
  657. nextId = prevId;
  658. }
  659. }
  660. void KGrLevelPlayer::tick (bool missed, int scaledTime)
  661. {
  662. if (playback) { // Replay a recorded move.
  663. if (! doRecordedMove()) {
  664. playback = false;
  665. // TODO - Should we emit interruptDemo() in UNEXPECTED_END case?
  666. dbk << "Unexpected END_OF_RECORDING - or KILL_HERO ACTION.";
  667. return; // End of recording.
  668. }
  669. }
  670. else if ((controlMode == MOUSE) || (controlMode == LAPTOP)) {
  671. int i, j; // Make and record a live pointer-move.
  672. emit getMousePos (i, j);
  673. setTarget (i, j);
  674. }
  675. else if (controlMode == KEYBOARD) {
  676. if (holdKeyOption == CLICK_KEY) {
  677. if (newDirection != direction) {
  678. direction = newDirection;
  679. }
  680. }
  681. // If keyboard with holdKey option, record one of nine directions.
  682. else if (holdKeyOption == HOLD_KEY) {
  683. const Direction d [9] = {UP_LEFT, UP, UP_RIGHT,
  684. LEFT, STAND, RIGHT,
  685. DOWN_LEFT, DOWN, DOWN_RIGHT};
  686. direction = d [(3 * (dY + 1)) + (dX + 1)];
  687. }
  688. // Record the direction, but do not extend the initial wait-time.
  689. if ((direction != NO_DIRECTION) && (playState == Playing)) { // IDW
  690. // IDW Need a better condition here. (playState == Playing) was to stop
  691. // IDW the HOLD_KEY option recording a whole lot of STAND directions before
  692. // IDW the first key is pressed. (direction != NO_DIRECTION) is previous code.
  693. record (2, DIRECTION_CODE + direction);
  694. }
  695. }
  696. if (playState != Playing) {
  697. return;
  698. }
  699. T++;
  700. if (dugBricks.count() > 0) {
  701. processDugBricks (scaledTime);
  702. }
  703. HeroStatus status = hero->run (scaledTime);
  704. if ((status == WON_LEVEL) || (status == DEAD)) {
  705. // Unsolicited timer-pause halts animation immediately, regardless of
  706. // user-selected state. It's OK: KGrGame deletes KGrLevelPlayer v. soon.
  707. timer->pause();
  708. // Queued connection ensures KGrGame slot runs AFTER return from here.
  709. emit endLevel (status);
  710. kDebug() << "END OF LEVEL";
  711. return;
  712. }
  713. foreach (KGrEnemy * enemy, enemies) {
  714. enemy->run (scaledTime);
  715. }
  716. emit animation (missed);
  717. }
  718. int KGrLevelPlayer::runnerGotGold (const int spriteId,
  719. const int i, const int j,
  720. const bool hasGold, const bool lost)
  721. {
  722. if (hasGold) {
  723. dbk2 << "GOLD COLLECTED BY" << spriteId << "AT" << i << j;
  724. }
  725. else if (lost) {
  726. dbk2 << "GOLD LOST BY" << spriteId << "AT" << i << j;
  727. }
  728. else {
  729. dbk2 << "GOLD DROPPED BY" << spriteId << "AT" << i << j;
  730. }
  731. if (! lost) {
  732. grid->gotGold (i, j, hasGold); // Record pickup/drop on grid.
  733. }
  734. emit gotGold (spriteId, i, j, hasGold, lost); // Erase/show gold on screen.
  735. // If hero got gold, score, maybe show hidden ladders, maybe end the level.
  736. if ((spriteId == heroId) || lost) {
  737. if (--nuggets <= 0) {
  738. grid->placeHiddenLadders(); // All gold picked up or lost.
  739. }
  740. }
  741. if (lost) {
  742. hero->setNuggets (nuggets); // Update hero re lost gold.
  743. }
  744. return nuggets;
  745. }
  746. void KGrLevelPlayer::makeReappearanceSequence()
  747. {
  748. // The idea is to make each possible x co-ord come up once per levelWidth
  749. // reappearances of enemies. This is not truly random, but it reduces the
  750. // tedium in levels where you must keep killing enemies until a particular
  751. // x or range of x comes up (e.g. if they have to collect gold for you).
  752. // First put the positions in ascending sequence.
  753. for (int k = 0; k < levelWidth; k++) {
  754. reappearPos [k] = k + 1;
  755. }
  756. int z;
  757. int left = levelWidth;
  758. int temp;
  759. // Shuffle the co-ordinates of reappearance positions (1 to levelWidth).
  760. for (int k = 0; k < levelWidth; k++) {
  761. // Pick a random element from those that are left.
  762. z = (int) (randomByte ((uchar) left));
  763. // Exchange its value with the last of the ones left.
  764. temp = reappearPos [z];
  765. reappearPos [z] = reappearPos [left - 1];
  766. reappearPos [left - 1] = temp;
  767. left--;
  768. }
  769. dbk2 << "Randoms" << reappearPos;
  770. reappearIndex = 0;
  771. }
  772. void KGrLevelPlayer::enemyReappear (int & gridI, int & gridJ)
  773. {
  774. bool looking = true;
  775. int i, j, k;
  776. // Follow Traditional or Scavenger rules: enemies reappear at top.
  777. j = rules->reappearRow();
  778. // Randomly look for a free spot in the row. Limit the number of tries.
  779. for (k = 1; ((k <= 3) && looking); k++) {
  780. if (reappearIndex >= levelWidth) {
  781. makeReappearanceSequence(); // Get next array of random i.
  782. }
  783. i = reappearPos [reappearIndex++];
  784. switch (grid->cellType (i, j)) {
  785. case FREE:
  786. case HLADDER:
  787. looking = false;
  788. break;
  789. default:
  790. break;
  791. }
  792. }
  793. // If unsuccessful, choose the first free spot in the rows below.
  794. while ((j < levelHeight) && looking) {
  795. j++;
  796. i = 0;
  797. while ((i < levelWidth) && looking) {
  798. i++;
  799. switch (grid->cellType (i, j)) {
  800. case FREE:
  801. case HLADDER:
  802. looking = false;
  803. break;
  804. default:
  805. break;
  806. }
  807. }
  808. }
  809. dbk2 << "Reappear at" << i << j;
  810. gridI = i;
  811. gridJ = j;
  812. }
  813. uchar KGrLevelPlayer::randomByte (const uchar limit)
  814. {
  815. if (! playback) {
  816. uchar value = randomGen->getLong ((unsigned long) limit);
  817. // A zero-byte terminates recording->draws, so add 1 when recording ...
  818. dbe2 "Draw %03d, index %04d, limit %02d\n", value, randIndex, limit);
  819. recording->draws [randIndex++] = value + 1;
  820. return value;
  821. }
  822. else {
  823. dbe2 "Draw %03d, index %04d, limit %02d\n",
  824. (recording->draws.at (randIndex) - 1), randIndex, limit);
  825. // and subtract 1 when replaying.
  826. return ((uchar) recording->draws.at (randIndex++) - 1);
  827. }
  828. }
  829. bool KGrLevelPlayer::doRecordedMove()
  830. {
  831. int i, j;
  832. uchar code = recording->content [recIndex];
  833. while (true) {
  834. // Check for end of recording.
  835. if ((code == END_CODE) || (code == 0)) {
  836. dbe2 "T %04d recIndex %03d PLAY - END of recording\n",
  837. T, recIndex);
  838. emit endLevel (UNEXPECTED_END);
  839. return false;
  840. }
  841. // Simulate recorded mouse movement.
  842. if (code < DIRECTION_CODE) {
  843. // playState = Playing;
  844. if (recCount <= 0) {
  845. i = code;
  846. j = (uchar)(recording->content [recIndex + 1]);
  847. // targetI = code;
  848. // targetJ = (uchar)(recording->content [recIndex + 1]);
  849. recCount = (uchar)(recording->content [recIndex + 2]);
  850. dbe2 "T %04d recIndex %03d PLAY codes %d %d %d - NEW TARGET\n",
  851. T, recIndex, i, j, recCount);
  852. // T, recIndex, targetI, targetJ, recCount);
  853. setTarget (i, j);
  854. }
  855. else {
  856. dbe2 "T %04d recIndex %03d PLAY codes %d %d %d\n",
  857. T, recIndex, targetI, targetJ, recCount);
  858. }
  859. if (--recCount <= 0) {
  860. recIndex = recIndex + 3;
  861. dbe2 "T %04d recIndex %03d PLAY - next index\n",
  862. T, recIndex);
  863. }
  864. break;
  865. }
  866. // Simulate a key press or mouse button click.
  867. else if (code < MODE_CODE) {
  868. code = code - DIRECTION_CODE;
  869. if (code != direction) {
  870. playState = Playing;
  871. }
  872. if ((code == DIG_LEFT) || (code == DIG_RIGHT)) {
  873. dbe2 "T %04d recIndex %03d PLAY dig code %d\n",
  874. T, recIndex, code);
  875. startDigging ((Direction) (code));
  876. recIndex++;
  877. code = recording->content [recIndex];
  878. recCount = 0;
  879. continue;
  880. }
  881. else {
  882. if (recCount <= 0) {
  883. recCount = (uchar)(recording->content [recIndex + 1]);
  884. dbe2 "T %04d recIndex %03d PLAY codes %d %d - KEY PRESS\n",
  885. T, recIndex, code, recCount);
  886. direction = ((Direction) (code));
  887. dX = movement [direction][X];
  888. dY = movement [direction][Y];
  889. }
  890. else {
  891. dbe2 "T %04d recIndex %03d PLAY codes %d %d mode %d\n",
  892. T, recIndex, code, recCount, controlMode);
  893. }
  894. if (--recCount <= 0) {
  895. recIndex = recIndex + 2;
  896. dbe2 "T %04d recIndex %03d PLAY - next index\n",
  897. T, recIndex);
  898. }
  899. }
  900. break;
  901. }
  902. // Replay a change of control-mode.
  903. else if (code < KEY_OPT_CODE) {
  904. dbe2 "T %04d recIndex %03d PLAY control-mode code %d\n",
  905. T, recIndex, code);
  906. setControlMode (code - MODE_CODE);
  907. recIndex++;
  908. code = recording->content [recIndex];
  909. recCount = 0;
  910. continue;
  911. }
  912. // Replay a change of keyboard click/hold option.
  913. else if (code < ACTION_CODE) {
  914. dbe2 "T %04d recIndex %03d PLAY key-option code %d\n",
  915. T, recIndex, code);
  916. setHoldKeyOption (code - KEY_OPT_CODE + CLICK_KEY);
  917. recIndex++;
  918. code = recording->content [recIndex];
  919. recCount = 0;
  920. continue;
  921. }
  922. // Replay an action, such as KILL_HERO.
  923. else if (code < SPEED_CODE) {
  924. if (code == (ACTION_CODE + KILL_HERO)) {
  925. dbe2 "T %04d recIndex %03d PLAY kill-hero code %d\n",
  926. T, recIndex, code);
  927. emit endLevel (DEAD);
  928. return false;
  929. }
  930. }
  931. // Replay a change of speed.
  932. else {
  933. dbe2 "T %04d recIndex %03d PLAY speed-change code %d\n",
  934. T, recIndex, code);
  935. setTimeScale (code - SPEED_CODE);
  936. recIndex++;
  937. code = recording->content [recIndex];
  938. recCount = 0;
  939. continue;
  940. }
  941. }
  942. return true;
  943. }
  944. void KGrLevelPlayer::interruptPlayback()
  945. {
  946. // Check if still replaying the wait-time before the first move.
  947. if (playState != Playing) {
  948. return;
  949. }
  950. uchar code = recording->content [recIndex];
  951. // Check for end-of-recording already reached.
  952. if ((code == END_CODE) || (code == 0)) {
  953. return;
  954. }
  955. // Start debug stuff.
  956. dbk2 << "recIndex" << recIndex << "recCount" << recCount
  957. << "randIndex" << randIndex;
  958. int ch = 0;
  959. int i = 0;
  960. while (i < recording->content.size()) {
  961. ch = (uchar)(recording->content.at(i));
  962. dbe2 "%03d ", ch);
  963. if (ch == 0)
  964. break;
  965. i++;
  966. }
  967. dbe2 "\n%d bytes\n", i - 1);
  968. i = 0;
  969. while (i < recording->draws.size()) {
  970. ch = (uchar)(recording->draws.at(i));
  971. dbe2 "%03d ", ch);
  972. if (ch == 0)
  973. break;
  974. i++;
  975. }
  976. dbe2 "\n%d bytes\n", i - 1);
  977. // End debug stuff.
  978. if (recCount > 0) {
  979. if ((code >= DIRECTION_CODE) && (code < (DIRECTION_CODE + nDirections)))
  980. {
  981. // Set keyboard counter to show how many ticks have been replayed.
  982. recCount = (uchar)(recording->content [recIndex + 1]) - recCount;
  983. recording->content [recIndex + 1] = (uchar) (recCount);
  984. recIndex = recIndex + 1; // Count here if same key after pause.
  985. }
  986. else if (code < DIRECTION_CODE) {
  987. // Set mouse-move counter to show how many ticks have been replayed.
  988. recCount = (uchar)(recording->content [recIndex + 2]) - recCount;
  989. recording->content [recIndex + 2] = (uchar) (recCount);
  990. recIndex = recIndex + 2; // Count here if mouse in same position.
  991. }
  992. }
  993. recording->content [recIndex + 1] = END_CODE;
  994. for (int i = (recIndex + 2); i < recording->content.size(); i++) {
  995. recording->content [i] = 0;
  996. }
  997. for (int i = randIndex; i < recording->draws.size(); i++) {
  998. recording->draws [i] = 0;
  999. }
  1000. // Start debug stuff.
  1001. dbk2 << "recIndex" << recIndex << "recCount" << recCount
  1002. << "randIndex" << randIndex;
  1003. i = 0;
  1004. while (i < recording->content.size()) {
  1005. ch = (uchar)(recording->content.at(i));
  1006. dbe2 "%03d ", ch);
  1007. if (ch == 0)
  1008. break;
  1009. i++;
  1010. }
  1011. dbe2 "\n%d bytes\n", i - 1);
  1012. i = 0;
  1013. while (i < recording->draws.size()) {
  1014. ch = (uchar)(recording->draws.at(i));
  1015. dbe2 "%03d ", ch);
  1016. if (ch == 0)
  1017. break;
  1018. i++;
  1019. }
  1020. dbe2 "\n%d bytes\n", i - 1);
  1021. // End debug stuff.
  1022. playback = false;
  1023. emit interruptDemo();
  1024. dbk << "INTERRUPT - emit interruptDemo();";
  1025. }
  1026. void KGrLevelPlayer::killHero()
  1027. {
  1028. if (! playback) {
  1029. // Record that KILL_HERO is how the level ended.
  1030. record (1, ACTION_CODE + KILL_HERO);
  1031. emit endLevel (DEAD);
  1032. kDebug() << "END OF LEVEL";
  1033. }
  1034. }
  1035. void KGrLevelPlayer::setControlMode (const int mode)
  1036. {
  1037. controlMode = mode;
  1038. if (! playback) {
  1039. // Record the change of mode.
  1040. record (1, MODE_CODE + mode);
  1041. }
  1042. }
  1043. void KGrLevelPlayer::setHoldKeyOption (const int option)
  1044. {
  1045. holdKeyOption = option;
  1046. if (! playback) {
  1047. // Record the change of keyboard click/hold option.
  1048. record (1, KEY_OPT_CODE + option - CLICK_KEY);
  1049. }
  1050. }
  1051. void KGrLevelPlayer::setTimeScale (const int timeScale)
  1052. {
  1053. timer->setScale ((float) (timeScale * 0.1));
  1054. if (! playback) {
  1055. record (1, SPEED_CODE + timeScale);
  1056. }
  1057. }
  1058. /******************************************************************************/
  1059. /************************** AUTHORS' DEBUGGING AIDS **************************/
  1060. /******************************************************************************/
  1061. void KGrLevelPlayer::dbgControl (int code)
  1062. {
  1063. switch (code) {
  1064. case DO_STEP:
  1065. timer->step(); // Do one timer step only.
  1066. break;
  1067. case BUG_FIX:
  1068. bugFix(); // Turn a bug fix on/off dynamically.
  1069. break;
  1070. case LOGGING:
  1071. startLogging(); // Turn logging on/off.
  1072. break;
  1073. case S_POSNS:
  1074. showFigurePositions(); // Show everybody's co-ordinates.
  1075. break;
  1076. case S_HERO:
  1077. hero->showState(); // Show hero's co-ordinates and state.
  1078. break;
  1079. case S_OBJ:
  1080. showObjectState(); // Show an object's state.
  1081. break;
  1082. default:
  1083. showEnemyState (code - ENEMY_0); // Show enemy co-ords and state.
  1084. break;
  1085. }
  1086. }
  1087. void KGrLevelPlayer::bugFix()
  1088. {
  1089. // Toggle a bug fix on/off dynamically.
  1090. KGrGame::bugFix = (KGrGame::bugFix) ? false : true;
  1091. fprintf (stderr, "%s", (KGrGame::bugFix) ? "\n" : "");
  1092. fprintf (stderr, ">> Bug fix is %s\n", (KGrGame::bugFix) ? "ON" : "OFF\n");
  1093. }
  1094. void KGrLevelPlayer::startLogging()
  1095. {
  1096. // Toggle logging on/off dynamically.
  1097. KGrGame::logging = (KGrGame::logging) ? false : true;
  1098. fprintf (stderr, "%s", (KGrGame::logging) ? "\n" : "");
  1099. fprintf (stderr, ">> Logging is %s\n", (KGrGame::logging) ? "ON" : "OFF\n");
  1100. }
  1101. void KGrLevelPlayer::showFigurePositions()
  1102. {
  1103. hero->showState();
  1104. foreach (KGrEnemy * enemy, enemies) {
  1105. enemy->showState();
  1106. }
  1107. }
  1108. void KGrLevelPlayer::showObjectState()
  1109. {
  1110. int i = targetI;
  1111. int j = targetJ;
  1112. char here = grid->cellType (i, j);
  1113. Flags access = grid->heroMoves (i, j);
  1114. int enemyId = grid->enemyOccupied (i, j);
  1115. int enter = (access & ENTERABLE) ? 1 : 0;
  1116. int stand = (access & dFlag [STAND]) ? 1 : 0;
  1117. int u = (access & dFlag [UP]) ? 1 : 0;
  1118. int d = (access & dFlag [DOWN]) ? 1 : 0;
  1119. int l = (access & dFlag [LEFT]) ? 1 : 0;
  1120. int r = (access & dFlag [RIGHT]) ? 1 : 0;
  1121. fprintf (stderr,
  1122. "[%02d,%02d] [%c] %02x E %d S %d U %d D %d L %d R %d occ %02d\n",
  1123. i, j, here, access, enter, stand, u, d, l, r, enemyId);
  1124. Flags eAccess = grid->enemyMoves (i, j);
  1125. if (eAccess != access) {
  1126. access = eAccess;
  1127. enter = (access & ENTERABLE) ? 1 : 0;
  1128. stand = (access & dFlag [STAND]) ? 1 : 0;
  1129. u = (access & dFlag [UP]) ? 1 : 0;
  1130. d = (access & dFlag [DOWN]) ? 1 : 0;
  1131. l = (access & dFlag [LEFT]) ? 1 : 0;
  1132. r = (access & dFlag [RIGHT]) ? 1 : 0;
  1133. fprintf (stderr,
  1134. "[%02d,%02d] [%c] %02x E %d S %d U %d D %d L %d R %d Enemy\n",
  1135. i, j, here, access, enter, stand, u, d, l, r);
  1136. }
  1137. }
  1138. void KGrLevelPlayer::showEnemyState (int enemyId)
  1139. {
  1140. if (enemyId < enemies.count()) {
  1141. enemies.at(enemyId)->showState();
  1142. }
  1143. }
  1144. #include "kgrlevelplayer.moc"