Fixes for datatype size on amd64.
[crack-attack.git] / src / Grid.h
blob063433b03e163b9c3a5d1cf65fc0509903dd59ac
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>
32 #include "BlockManager.h"
33 #include "GarbageManager.h"
34 #include "LevelLights.h"
36 // grid element states
37 #define GR_EMPTY (1 << 0)
38 #define GR_BLOCK (1 << 1)
39 #define GR_GARBAGE (1 << 2)
40 #define GR_FALLING (1 << 3)
41 #define GR_IMMUTABLE (1 << 4)
42 #define GR_SHATTERING (1 << 5)
43 #define GR_HANGING (1 << 6)
45 // pattern types
46 #define PT_HORIZONTAL (1 << 0)
47 #define PT_VERTICAL (1 << 1)
49 class Block;
50 class Garbage;
51 class ComboTabulator;
53 class CheckRegistryElement {
54 public:
55 bool mark;
56 ComboTabulator *combo;
59 class GridElement {
60 public:
61 int state;
62 int resident_type;
63 void *resident;
66 /* static */ class Grid {
67 public:
68 static void gameStart ( );
69 static void timeStep ( );
70 static bool shiftGridUp ( );
72 static inline void dump ( )
74 std::cout << std::endl;
75 for (int y = GC_PLAY_HEIGHT; y--; ) {
76 for (int x = 0; x < GC_PLAY_WIDTH; x++)
77 switch (grid[x][y].state) {
78 case GR_EMPTY: std::cout << ' '; break;
79 case GR_BLOCK: std::cout << 'B'; break;
80 case GR_GARBAGE: std::cout << '@'; break;
81 case GR_FALLING: std::cout << '*'; break;
82 case GR_IMMUTABLE: std::cout << '#'; break;
83 case GR_SHATTERING: std::cout << 'X'; break;
84 case GR_HANGING: std::cout << '+'; break;
85 default: std::cout << '!'; break;
87 std::cout << std::endl;
89 std::cout << std::endl;
91 for (int n = 0; n < GC_PLAY_WIDTH; n++)
92 std::cout << blockAt(n, 0).state << std::endl;
95 static inline int stateAt ( int x, int y )
97 return grid[x][y].state;
100 static inline int residentTypeAt ( int x, int y )
102 return grid[x][y].resident_type;
105 static inline Block &blockAt ( int x, int y )
107 assert(grid[x][y].resident_type == GR_BLOCK);
108 return *((Block *) grid[x][y].resident);
111 static inline Garbage &garbageAt ( int x, int y )
113 #ifndef NDEBUG
114 if (!(grid[x][y].resident_type == GR_GARBAGE)) {
115 dump();
116 DUMP(x);
117 DUMP(y);
118 DUMP(grid[x][y].state);
119 DUMP(grid[x][y].resident_type);
120 DUMP(top_occupied_row);
121 DUMP(top_effective_row);
123 #endif
124 assert(grid[x][y].resident_type == GR_GARBAGE);
125 assert(y < GC_PLAY_HEIGHT);
126 return *((Garbage *) grid[x][y].resident);
129 static inline int flavorAt ( int x, int y )
131 assert(grid[x][y].state == GR_BLOCK);
132 return ((Block *) grid[x][y].resident)->flavor;
135 static inline bool matchAt ( int x, int y, Block &block )
137 assert(grid[x][y].state == GR_BLOCK);
138 return BlockManager::flavorMatch(block, *(Block *) grid[x][y].resident);
141 static inline void changeState ( int x, int y, void *resident, int state )
143 assert(grid[x][y].resident == resident);
144 grid[x][y].state = state;
147 static inline void addBlock ( int x, int y, Block *resident, int state )
149 assert(x < GC_PLAY_WIDTH);
150 assert(y < GC_PLAY_HEIGHT);
151 assert(grid[x][y].state & GR_EMPTY);
152 grid[x][y].resident = resident;
153 grid[x][y].resident_type = GR_BLOCK;
154 grid[x][y].state = state;
157 static inline void addGarbage ( int x, int y, Garbage *resident, int state )
159 assert(grid[x][y].state & GR_EMPTY);
160 assert(x < GC_PLAY_WIDTH);
161 assert(y < GC_PLAY_HEIGHT);
162 grid[x][y].resident = resident;
163 grid[x][y].resident_type = GR_GARBAGE;
164 grid[x][y].state = state;
167 static inline void remove ( int x, int y, void *resident )
169 assert(grid[x][y].resident == resident);
170 grid[x][y].resident = null;
171 grid[x][y].resident_type = GR_EMPTY;
172 grid[x][y].state = GR_EMPTY;
175 static inline void requestEliminationCheck ( Block &block,
176 ComboTabulator *combo = null )
178 check_registry[block.id].mark = true;
179 check_registry[block.id].combo = combo;
180 check_count++;
183 static inline bool checkSafeHeightViolation ( )
185 return top_effective_row >= GC_SAFE_HEIGHT - 1;
188 static inline void notifyImpact ( int y, int height )
190 int impact_top = y + height - 1;
192 if (top_effective_row < impact_top) {
193 top_effective_row = impact_top;
194 LevelLights::levelRaise(top_effective_row);
197 LevelLights::notifyImpact(y, height);
200 // top row with anything in it, including initially falling garbage; used to
201 // determine garbage drop height; updated in Grid::timeStep() and
202 // GarbageManager::newFallingGarbage()
203 static int top_occupied_row;
205 // top row that's holding blocks or landed garbage; used for level lights and
206 // safe height violation; updated in Grid::timeStep() and Grid::notifyImpact()
207 static int top_effective_row;
209 static bool gray_shatter;
211 private:
212 static void handleEliminationCheckRequest ( Block &block,
213 ComboTabulator *combo );
215 static void shatterGarbage_inline_split_ ( int x, int y, Garbage *due_to );
216 static inline void shatterGarbage ( int x, int y, Garbage *due_to = null )
218 if (!(stateAt(x, y) & GR_GARBAGE)) return;
219 shatterGarbage_inline_split_(x, y, due_to);
222 static GridElement grid[GC_PLAY_WIDTH][GC_PLAY_HEIGHT];
223 static CheckRegistryElement check_registry[GC_BLOCK_STORE_SIZE];
224 static int check_count;
226 static int shatter_count;
227 static int shatter_top;
228 static int shatter_bottom;
231 #endif