• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

kgoldrunner

  • sources
  • kde-4.14
  • kdegames
  • kgoldrunner
  • src
kgrlevelgrid.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright 2009 Ian Wadham <iandw.au@gmail.com> *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License *
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
16  ****************************************************************************/
17 
18 #include "kgrlevelgrid.h"
19 
20 KGrLevelGrid::KGrLevelGrid (QObject * parent, const KGrRecording * theLevelData)
21  :
22  QObject (parent)
23 {
24  // Put a concrete wall all round the layout: left, right, top and bottom.
25  // This saves ever having to test for being at the edge of the layout.
26  int inWidth = theLevelData->width;
27  width = inWidth + ConcreteWall * 2;
28 
29  int inHeight = theLevelData->height;
30  height = inHeight + ConcreteWall * 2;
31 
32  int size = width * height;
33 
34  layout.fill (CONCRETE, size);
35 
36  // Initialise the flags for each cell.
37  heroAccess.fill (0, size);
38  enemyAccess.fill (0, size);
39  enemyHere.fill (-1, size);
40 
41  // Copy the cells of the layout, but enclosed within the concrete wall.
42  int inRow = 0;
43  int outRow = width + ConcreteWall;
44 
45  for (int j = 0; j < inHeight; j++) {
46  for (int i = 0; i < inWidth; i++) {
47  char type = theLevelData->layout [inRow + i];
48  switch (type) {
49  case HLADDER:
50  // Change hidden ladders to FREE, but keep a list of them.
51  hiddenLadders.append (outRow + i);
52  type = FREE;
53  break;
54  case HENEMY:
55  // Change hidden enemies to BRICK, but keep a list of them.
56  hiddenEnemies.append (outRow + i);
57  type = BRICK;
58  break;
59  case FLASHING:
60  // Change flashing nuggets to NUGGET, but keep a list of them.
61  flashingGold.append (outRow + i);
62  type = NUGGET;
63  break;
64  }
65  layout [outRow + i] = type;
66  }
67  inRow = inRow + inWidth;
68  outRow = outRow + width;
69  }
70 }
71 
72 KGrLevelGrid::~KGrLevelGrid()
73 {
74 }
75 
76 // Inline functions (see kgrlevelgrid.h).
77 // char cellType (int i, int j)
78 // char heroMoves (int i, int j)
79 // char enemyMoves (int i, int j)
80 // void gotGold (const int i, const int j, const bool runnerHasGold)
81 //
82 // void setEnemyOccupied (int i, int j, const int spriteId)
83 // int enemyOccupied (int i, int j)
84 //
85 // int index (int i, int j)
86 
87 void KGrLevelGrid::calculateAccess (bool pRunThruHole)
88 {
89  runThruHole = pRunThruHole; // Save a copy of the runThruHole rule.
90 
91  char here;
92  bool canEnter;
93 
94  // Calculate which cells can be entered (N.B. USEDHOLE is a trapped enemy).
95  for (int j = 1; j < height - 1; j++) {
96  for (int i = 1; i < width - 1; i++) {
97  here = cellType (i, j);
98  canEnter = (here != BRICK) && (here != CONCRETE) &&
99  (here != FBRICK) && (here != USEDHOLE);
100  heroAccess [index (i, j)] = canEnter ? ENTERABLE : 0;
101  enemyAccess [index (i, j)] = canEnter ? ENTERABLE : 0;
102  }
103  }
104 
105  // Calculate the access *from* each cell to its neighbours.
106  for (int j = 1; j < height - 1; j++) {
107  for (int i = 1; i < width - 1; i++) {
108  calculateCellAccess (i, j);
109  }
110  }
111 }
112 
113 void KGrLevelGrid::changeCellAt (const int i, const int j, const char type)
114 {
115  int position = index (i, j);
116  bool canEnter = (type != BRICK) && (type != CONCRETE) &&
117  (type != FBRICK) && (type != USEDHOLE);
118  layout [position] = type;
119  heroAccess [position] = canEnter ? ENTERABLE : 0;
120  enemyAccess [position] = canEnter ? ENTERABLE : 0;
121 
122  calculateCellAccess (i, j); // Recalculate access *from* this cell
123  // and access *to* it
124  calculateCellAccess (i, j - 1); // from above,
125  calculateCellAccess (i - 1, j); // from left,
126  calculateCellAccess (i + 1, j); // from right,
127  calculateCellAccess (i, j + 1); // and from below.
128 }
129 
130 void KGrLevelGrid::calculateCellAccess (const int i, const int j)
131 {
132  Flags access = 0;
133  char here = cellType (i, j);
134  if (here == CONCRETE) {
135  // If edge-cell or other CONCRETE, no calculation (avoid index errors).
136  return;
137  }
138  char below = cellType (i, j + 1);
139 
140  access = heroMoves (i, j) & ENTERABLE;
141 
142  // Cannot enter brick, concrete or used hole: can drop into a false brick.
143  if (! (access & ENTERABLE) && (here != FBRICK)) {
144  access = 0;
145  }
146  // If can stand or hang on anything, allow down, left and right.
147  else if ((below == BRICK) || (below == CONCRETE) || (below == USEDHOLE) ||
148  (below == LADDER) || (here == LADDER) || (here == BAR)) {
149  access |= (dFlag [STAND] | dFlag [DOWN] |
150  dFlag [LEFT] | dFlag [RIGHT]);
151  }
152  // If cannot stand or hang, can go down (space or false brick) or
153  // maybe left or right (when standing on an enemy).
154  else {
155  access |= (dFlag [DOWN] | dFlag [LEFT] | dFlag [RIGHT]);
156  }
157  // Can only go up if there is a ladder here.
158  if (here == LADDER) {
159  access |= dFlag [UP];
160  }
161 
162  // Mask out directions that are blocked above, below, L or R, but not for
163  // concrete/brick at edge of grid (or elsewhere) to avoid indexing errors.
164  if (access != 0) {
165  if (! (heroMoves (i, j - 1) & ENTERABLE)) {
166  access = ~dFlag [UP] & access; // Cannot go up.
167  }
168  if (! (heroMoves (i - 1, j) & ENTERABLE)) {
169  access = ~dFlag [LEFT] & access; // Cannot go left.
170  }
171  if (! (heroMoves (i + 1, j) & ENTERABLE)) {
172  access = ~dFlag [RIGHT] & access; // Cannot go right.
173  }
174  if (! (heroMoves (i, j + 1) & ENTERABLE)) {
175  if (below != FBRICK) {
176  access = ~dFlag [DOWN] & access; // Cannot go down.
177  }
178  }
179  }
180 
181  heroAccess [index (i, j)] = access;
182 
183  // Enemy access is the same as the hero's when no holes are open.
184  enemyAccess [index (i, j)] = heroAccess [index (i, j)];
185 
186  if (here == USEDHOLE) {
187  enemyAccess [index (i, j)] = UP; // Can only climb out of hole.
188  }
189  else if (! runThruHole) { // Check the rule.
190  char mask;
191  mask = (cellType (i - 1, j) == HOLE) ? dFlag [LEFT] : 0;
192  mask = (cellType (i + 1, j) == HOLE) ? (dFlag [RIGHT] | mask) : mask;
193  enemyAccess [index (i, j)] &= ~mask; // Block access to holes at L/R.
194  }
195 }
196 
197 void KGrLevelGrid::placeHiddenLadders()
198 {
199  foreach (int offset, hiddenLadders) {
200  int i = offset % width;
201  int j = offset / width;
202  changeCellAt (i, j, LADDER);
203  }
204  emit showHiddenLadders (hiddenLadders, width);
205  hiddenLadders.clear();
206 }
207 
208 #include "kgrlevelgrid.moc"
QList::clear
void clear()
KGrLevelGrid::heroMoves
Flags heroMoves(int i, int j)
Definition: kgrlevelgrid.h:38
KGrLevelGrid::placeHiddenLadders
void placeHiddenLadders()
Definition: kgrlevelgrid.cpp:197
BAR
const char BAR
Definition: kgrglobals.h:39
KGrLevelGrid::cellType
char cellType(int i, int j)
Definition: kgrlevelgrid.h:34
KGrLevelGrid::KGrLevelGrid
KGrLevelGrid(QObject *parent, const KGrRecording *theLevelData)
Definition: kgrlevelgrid.cpp:20
KGrLevelGrid::calculateAccess
void calculateAccess(bool pRunThruHole)
Definition: kgrlevelgrid.cpp:87
kgrlevelgrid.h
QVector::fill
QVector< T > & fill(const T &value, int size)
HENEMY
const char HENEMY
Definition: kgrglobals.h:30
USEDHOLE
const char USEDHOLE
Definition: kgrglobals.h:41
KGrLevelGrid::changeCellAt
void changeCellAt(const int i, const int j, const char type)
Definition: kgrlevelgrid.cpp:113
LEFT
Definition: kgrglobals.h:174
QList::append
void append(const T &value)
NUGGET
const char NUGGET
Definition: kgrglobals.h:37
QObject
KGrRecording
KGrRecording structure: contains a record of play in a KGoldrunner level.
Definition: kgrglobals.h:115
KGrLevelGrid::~KGrLevelGrid
~KGrLevelGrid()
Definition: kgrlevelgrid.cpp:72
LADDER
const char LADDER
Definition: kgrglobals.h:36
HLADDER
const char HLADDER
Definition: kgrglobals.h:35
FLASHING
const char FLASHING
Definition: kgrglobals.h:38
BRICK
const char BRICK
Definition: kgrglobals.h:33
UP
Definition: kgrglobals.h:174
FBRICK
const char FBRICK
Definition: kgrglobals.h:34
HOLE
const char HOLE
Definition: kgrglobals.h:40
ConcreteWall
const int ConcreteWall
Definition: kgrglobals.h:168
KGrRecording::width
int width
Width of grid, in cells (at rec time).
Definition: kgrglobals.h:124
RIGHT
Definition: kgrglobals.h:174
ENTERABLE
const AccessFlag ENTERABLE
Definition: kgrglobals.h:185
KGrLevelGrid::showHiddenLadders
void showHiddenLadders(const QList< int > &ladders, const int width)
Flags
char Flags
Definition: kgrglobals.h:172
KGrRecording::height
int height
Height of grid, in cells (at rec time).
Definition: kgrglobals.h:125
KGrRecording::layout
QByteArray layout
Codes for the level layout (at rec time).
Definition: kgrglobals.h:126
CONCRETE
const char CONCRETE
Definition: kgrglobals.h:32
STAND
Definition: kgrglobals.h:174
DOWN
Definition: kgrglobals.h:174
FREE
const char FREE
Definition: kgrglobals.h:28
dFlag
const DirectionFlag dFlag[nDirections]
Definition: kgrglobals.h:178
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:24 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kgoldrunner

Skip menu "kgoldrunner"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal