Game end works this way now:
[crack-attack.git] / src / Grid.h
blob586eb22edf7c06947551b8c2e2f3557d30833154
1 /*
2 * Grid.h
3 * Daniel Nelson - 8/24/0
5 * Copyright (C) 2000 Daniel Nelson
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * Daniel Nelson - aluminumangel.org
22 * 174 W. 18th Ave.
23 * Columbus, OH 43210
26 #ifndef GRID_H
27 #define GRID_H
29 #include <cassert>
31 using namespace std;
33 #include "BlockManager.h"
34 #include "GarbageManager.h"
35 #include "LevelLights.h"
37 // grid element states
38 #define GR_EMPTY (1 << 0)
39 #define GR_BLOCK (1 << 1)
40 #define GR_GARBAGE (1 << 2)
41 #define GR_FALLING (1 << 3)
42 #define GR_IMMUTABLE (1 << 4)
43 #define GR_SHATTERING (1 << 5)
44 #define GR_HANGING (1 << 6)
46 // pattern types
47 #define PT_HORIZONTAL (1 << 0)
48 #define PT_VERTICAL (1 << 1)
50 class Block;
51 class Garbage;
52 class ComboTabulator;
54 class CheckRegistryElement {
55 public:
56 bool mark;
57 ComboTabulator *combo;
60 class GridElement {
61 public:
62 int state;
63 int resident_type;
64 void *resident;
67 /* static */ class Grid {
68 public:
69 static void gameStart ( );
70 static void timeStep ( );
71 static bool shiftGridUp ( );
73 static inline void dump ( )
75 cout << '\n';
76 for (int y = GC_PLAY_HEIGHT; y--; ) {
77 for (int x = 0; x < GC_PLAY_WIDTH; x++)
78 switch (grid[x][y].state) {
79 case GR_EMPTY: cout << ' '; break;
80 case GR_BLOCK: cout << 'B'; break;
81 case GR_GARBAGE: cout << '@'; break;
82 case GR_FALLING: cout << '*'; break;
83 case GR_IMMUTABLE: cout << '#'; break;
84 case GR_SHATTERING: cout << 'X'; break;
85 case GR_HANGING: cout << '+'; break;
86 default: cout << '!'; break;
88 cout << '\n';
90 cout << endl;
92 for (int n = 0; n < GC_PLAY_WIDTH; n++)
93 cout << blockAt(n, 0).state << endl;
96 static inline int stateAt ( int x, int y )
98 return grid[x][y].state;
101 static inline int residentTypeAt ( int x, int y )
103 return grid[x][y].resident_type;
106 static inline Block &blockAt ( int x, int y )
108 assert(grid[x][y].resident_type == GR_BLOCK);
109 return *((Block *) grid[x][y].resident);
112 static inline Garbage &garbageAt ( int x, int y )
114 #ifndef NDEBUG
115 if (!(grid[x][y].resident_type == GR_GARBAGE)) {
116 dump();
117 DUMP(x);
118 DUMP(y);
119 DUMP(grid[x][y].state);
120 DUMP(grid[x][y].resident_type);
121 DUMP(top_occupied_row);
122 DUMP(top_effective_row);
124 #endif
125 assert(grid[x][y].resident_type == GR_GARBAGE);
126 assert(y < GC_PLAY_HEIGHT);
127 return *((Garbage *) grid[x][y].resident);
130 static inline int flavorAt ( int x, int y )
132 assert(grid[x][y].state == GR_BLOCK);
133 return ((Block *) grid[x][y].resident)->flavor;
136 static inline bool matchAt ( int x, int y, Block &block )
138 assert(grid[x][y].state == GR_BLOCK);
139 return BlockManager::flavorMatch(block, *(Block *) grid[x][y].resident);
142 static inline void changeState ( int x, int y, void *resident, int state )
144 assert(grid[x][y].resident == resident);
145 grid[x][y].state = state;
148 static inline void addBlock ( int x, int y, Block *resident, int state )
150 assert(x < GC_PLAY_WIDTH);
151 assert(y < GC_PLAY_HEIGHT);
152 assert(grid[x][y].state & GR_EMPTY);
153 grid[x][y].resident = resident;
154 grid[x][y].resident_type = GR_BLOCK;
155 grid[x][y].state = state;
158 static inline void addGarbage ( int x, int y, Garbage *resident, int state )
160 assert(grid[x][y].state & GR_EMPTY);
161 assert(x < GC_PLAY_WIDTH);
162 assert(y < GC_PLAY_HEIGHT);
163 grid[x][y].resident = resident;
164 grid[x][y].resident_type = GR_GARBAGE;
165 grid[x][y].state = state;
168 static inline void remove ( int x, int y, void *resident )
170 assert(grid[x][y].resident == resident);
171 grid[x][y].resident = null;
172 grid[x][y].resident_type = GR_EMPTY;
173 grid[x][y].state = GR_EMPTY;
176 static inline void requestEliminationCheck ( Block &block,
177 ComboTabulator *combo = null )
179 check_registry[block.id].mark = true;
180 check_registry[block.id].combo = combo;
181 check_count++;
184 static inline bool checkSafeHeightViolation ( )
186 return top_effective_row >= GC_SAFE_HEIGHT - 1;
189 static inline void notifyImpact ( int y, int height )
191 int impact_top = y + height - 1;
193 if (top_effective_row < impact_top) {
194 top_effective_row = impact_top;
195 LevelLights::levelRaise(top_effective_row);
198 LevelLights::notifyImpact(y, height);
201 // top row with anything in it, including initially falling garbage; used to
202 // determine garbage drop height; updated in Grid::timeStep() and
203 // GarbageManager::newFallingGarbage()
204 static int top_occupied_row;
206 // top row that's holding blocks or landed garbage; used for level lights and
207 // safe height violation; updated in Grid::timeStep() and Grid::notifyImpact()
208 static int top_effective_row;
210 static bool gray_shatter;
212 private:
213 static void handleEliminationCheckRequest ( Block &block,
214 ComboTabulator *combo );
216 static void shatterGarbage_inline_split_ ( int x, int y, Garbage *due_to );
217 static inline void shatterGarbage ( int x, int y, Garbage *due_to = null )
219 if (!(stateAt(x, y) & GR_GARBAGE)) return;
220 shatterGarbage_inline_split_(x, y, due_to);
223 static GridElement grid[GC_PLAY_WIDTH][GC_PLAY_HEIGHT];
224 static CheckRegistryElement check_registry[GC_BLOCK_STORE_SIZE];
225 static int check_count;
227 static int shatter_count;
228 static int shatter_top;
229 static int shatter_bottom;
232 #endif