Holy god the windows build better fucking work
[crack-attack.git] / src / Grid.h
blob64dc99fe04241d9f08e519b1332b28870fc43023
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 //MESSAGE("Begin x " << x << " y " << y << " state " << state);
161 //MESSAGE("Assert position is empty...");
162 assert(grid[x][y].state & GR_EMPTY);
163 //MESSAGE("Assert x is in width... " << GC_PLAY_WIDTH);
164 assert(x < GC_PLAY_WIDTH);
165 //MESSAGE("Assert y is in height... " << GC_PLAY_HEIGHT);
166 assert(y < GC_PLAY_HEIGHT);
167 //MESSAGE("Asserts completed");
168 grid[x][y].resident = resident;
169 grid[x][y].resident_type = GR_GARBAGE;
170 grid[x][y].state = state;
171 //MESSAGE("End");
174 static inline void remove ( int x, int y, void *resident )
176 assert(grid[x][y].resident == resident);
177 grid[x][y].resident = null;
178 grid[x][y].resident_type = GR_EMPTY;
179 grid[x][y].state = GR_EMPTY;
182 static inline void requestEliminationCheck ( Block &block,
183 ComboTabulator *combo = null )
185 check_registry[block.id].mark = true;
186 check_registry[block.id].combo = combo;
187 check_count++;
190 static inline bool checkSafeHeightViolation ( )
192 return top_effective_row >= GC_SAFE_HEIGHT - 1;
195 static inline void notifyImpact ( int y, int height )
197 int impact_top = y + height - 1;
199 if (top_effective_row < impact_top) {
200 top_effective_row = impact_top;
201 LevelLights::levelRaise(top_effective_row);
204 LevelLights::notifyImpact(y, height);
207 // top row with anything in it, including initially falling garbage; used to
208 // determine garbage drop height; updated in Grid::timeStep() and
209 // GarbageManager::newFallingGarbage()
210 static int top_occupied_row;
212 // top row that's holding blocks or landed garbage; used for level lights and
213 // safe height violation; updated in Grid::timeStep() and Grid::notifyImpact()
214 static int top_effective_row;
216 static bool gray_shatter;
218 private:
219 static void handleEliminationCheckRequest ( Block &block,
220 ComboTabulator *combo );
222 static void shatterGarbage_inline_split_ ( int x, int y, Garbage *due_to );
223 static inline void shatterGarbage ( int x, int y, Garbage *due_to = null )
225 if (!(stateAt(x, y) & GR_GARBAGE)) return;
226 shatterGarbage_inline_split_(x, y, due_to);
229 static GridElement grid[GC_PLAY_WIDTH][GC_PLAY_HEIGHT];
230 static CheckRegistryElement check_registry[GC_BLOCK_STORE_SIZE];
231 static int check_count;
233 static int shatter_count;
234 static int shatter_top;
235 static int shatter_bottom;
238 #endif