1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
#include "board.h"
#include "board.moc"
#include <knotifyclient.h>
#include <klocale.h>
#include <kzoommainwindow.h>
#include "piece.h"
#include "factory.h"
#include "baseprefs.h"
using namespace KGrid2D;
//-----------------------------------------------------------------------------
FixedCanvasView::FixedCanvasView(TQWidget *parent, const char *name)
: TQCanvasView(parent, name, WNoAutoErase)
{}
TQSize FixedCanvasView::sizeHint() const
{
if ( canvas()==0 ) return TQSize();
return canvas()->size() + 2 * TQSize(frameWidth(), frameWidth());
}
void FixedCanvasView::adjustSize()
{
setFixedSize(sizeHint());
}
//-----------------------------------------------------------------------------
const BaseBoard::DirectionData BaseBoard::DIRECTION_DATA[Nb_Direction] = {
{ SquareBase::Left, Left },
{ SquareBase::Right, Right },
{ SquareBase::Down, Up },
{ SquareBase::Up, Down }
};
BaseBoard::BaseBoard(bool graphic, TQWidget *parent)
: FixedCanvasView(parent, "board"),
GenericTetris(bfactory->bbi.width, bfactory->bbi.height,
bfactory->bbi.withPieces, graphic),
state(GameOver), timer(this), sequences(0), main(0), _next(0),
_arcade(false)
{
if (graphic) {
setVScrollBarMode(AlwaysOff);
setHScrollBarMode(AlwaysOff);
setFrameStyle( TQFrame::Panel | TQFrame::Sunken );
sequences = new SequenceArray;
main = new BlockInfo(*sequences);
setCanvas(main);
if (bfactory->bbi.withPieces)
_next = new BlockInfo(*sequences);
setBlockInfo(main, _next);
connect(&timer, TQT_SIGNAL(timeout()), TQT_SLOT(timeout()));
Piece::info().loadColors();
KZoomMainWindow::addWidget(this);
}
}
void BaseBoard::copy(const GenericTetris &g)
{
GenericTetris::copy(g);
state = static_cast<const BaseBoard &>(g).state;
}
void BaseBoard::settingsChanged()
{
Q_ASSERT( graphic() );
Piece::info().loadColors();
}
void BaseBoard::adjustSize()
{
int size = BasePrefs::blockSize();
sequences->setBlockSize(size);
main->resize(matrix().width() * size, matrix().height() * size);
for (uint i=0; i<matrix().width(); i++)
for (uint j=0; j<firstClearLine(); j++) {
Coord c(i, j);
if ( matrix()[c]==0 ) continue;
partialMoveBlock(c, TQPoint(0, 0));
}
if (_next) {
Coord c = Piece::info().maxSize() + Coord(2, 2);
_next->resize(c.first * size, c.second * size);
_nextPiece->moveCenter();
}
FixedCanvasView::adjustSize();
}
BaseBoard::~BaseBoard()
{
if ( graphic() ) {
setBlockInfo(0, 0); // destruct all sprites before deleting canvas
delete _next;
delete main;
delete sequences;
}
}
void BaseBoard::init(bool arcade)
{
_arcade = arcade;
_arcadeStageDone = false;
}
void BaseBoard::start(const GTInitData &data)
{
Q_ASSERT( graphic() );
if ( !_arcadeStageDone || _arcadeStage==bfactory->bbi.nbArcadeStages )
_arcadeStage = 0;
_arcadeStageDone = false;
state = Normal;
GenericTetris::start(data); // NB: the timer is started by updateLevel !
if (_arcade) arcadePrepare();
}
void BaseBoard::stop()
{
timer.stop();
state = GameOver;
}
void BaseBoard::pause()
{
Q_ASSERT( graphic() );
timer.stop();
_oldState = state;
state = Paused;
showBoard(false);
}
void BaseBoard::gameOver()
{
stop();
emit gameOverSignal();
}
void BaseBoard::showCanvas(TQCanvas *c, bool show)
{
TQCanvasItemList l = c->allItems();
TQCanvasItemList::Iterator it;
for (it=l.begin(); it!=l.end(); ++it) {
if (show) (*it)->show();
else (*it)->hide();
}
c->update();
}
void BaseBoard::showBoard(bool show)
{
showCanvas(main, show);
}
void BaseBoard::unpause()
{
Q_ASSERT( graphic() );
showBoard(true);
state = _oldState;
startTimer();
}
void BaseBoard::updateRemoved(uint newRemoved)
{
GenericTetris::updateRemoved(newRemoved);
emit removedUpdated();
}
void BaseBoard::updateScore(uint newScore)
{
GenericTetris::updateScore(newScore);
emit scoreUpdated();
}
int BaseBoard::firstColumnBlock(uint col) const
{
for (int j=firstClearLine()-1; j>=0; j--) {
Coord c(col, j);
if ( matrix()[c]!=0 ) return j;
}
return -1;
}
//-----------------------------------------------------------------------------
void BaseBoard::_beforeRemove(bool first)
{
if ( graphic() ) {
state = ( beforeRemove(first) ? BeforeRemove : Normal );
if ( state==BeforeRemove ) {
startTimer();
return;
}
}
remove();
_afterRemove(true);
}
void BaseBoard::remove()
{
for (uint j=0; j<firstClearLine(); j++)
for (uint i=0; i<matrix().width(); i++) {
Coord c(i, j);
if ( matrix()[c]==0 || !toBeRemoved(c) ) continue;
removeBlock(c);
}
computeInfos();
if ( graphic() ) {
main->update();
KNotifyClient::event(winId(), "removed", i18n("Blocks removed"));
}
}
bool BaseBoard::doFall(bool doAll, bool first, bool lineByLine)
{
Q_ASSERT( !lineByLine || !doAll );
if ( !doAll ) {
if (first) loop = 0;
else loop++;
}
bool final = (doAll || lineByLine
|| loop==bfactory->bbi.nbFallStages);
for (uint i=0; i<matrix().width(); i++) {
// compute heights
// we must separate this computation since toFall() can depend
// directly on the disposition of blocks under the current one
// (for e.g. in kfouleggs)
// we do not rely on firstClearLine() here since this method is
// used in kfouleggs to make gift blocks fall down ...
uint h = 0;
TQMemArray<uint> heights(matrix().height());
for (uint j=1; j<matrix().height(); j++) { // first line cannot fall
Coord src(i, j);
if ( toFall(src) ) h++;
heights[j] = h;
}
// do move
for (uint j=1; j<matrix().height(); j++) {
Coord src(i, j);
if( heights[j]==0 || matrix()[src]==0 ) continue;
if (lineByLine) final = false;
uint k = j - (lineByLine ? 1 : heights[j]);
Coord dest(i, k);
if ( final || lineByLine ) moveBlock(src, dest);
else partialBlockFall(src, dest);
}
}
if (final) computeInfos();
return final;
}
void BaseBoard::_afterRemove(bool first)
{
AfterRemoveResult r = afterRemove(!graphic(), first);
switch (r) {
case Done:
state = Normal;
_afterAfterRemove();
return;
case NeedAfterRemove:
state = AfterRemove;
startTimer();
return;
case NeedRemoving:
_beforeRemove(true);
return;
}
}
BaseBoard::AfterRemoveResult BaseBoard::afterRemove(bool doAll, bool first)
{
return (doFall(doAll, first, false) ? Done : NeedAfterRemove);
}
void BaseBoard::_afterAfterRemove()
{
if ( isArcade() && arcadeDone()>=arcadeTodo() ) {
_arcadeStage++;
_arcadeStageDone = true;
gameOver();
return;
}
if ( !afterAfterRemove() ) gameOver();
else if ( graphic() ) startTimer();
}
bool BaseBoard::timeout()
{
Q_ASSERT( graphic() );
if ( state==GameOver ) return true;
switch (state) {
case BeforeRemove: _beforeRemove(FALSE); break;
case AfterRemove: _afterRemove(FALSE); break;
default: return false;
}
main->update();
return true;
}
bool BaseBoard::startTimer()
{
Q_ASSERT( graphic() );
if ( state==GameOver ) return true;
switch (state) {
case BeforeRemove:
timer.start(bfactory->bbi.beforeRemoveTime, true);
break;
case AfterRemove:
timer.start(bfactory->bbi.afterRemoveTime, true);
break;
default:
return false;
}
return true;
}
bool BaseBoard::beforeRemove(bool first)
{
if (first) loop = 0;
else loop++;
for (uint j=0; j<firstClearLine(); j++)
for (uint i=0; i<matrix().width(); i++) {
Coord c(i, j);
if ( toBeRemoved(c) ) matrix()[c]->toggleLight();
}
return ( loop!=bfactory->bbi.nbToggles );
}
//-----------------------------------------------------------------------------
void BaseBoard::partialBlockFall(const Coord &src, const Coord &dest)
{
Q_ASSERT( loop<bfactory->bbi.nbFallStages );
float c = float(loop+1) / bfactory->bbi.nbFallStages * BasePrefs::blockSize();
int xdec = dest.first - src.first;
int ydec = src.second - dest.second;
TQPoint p(int(xdec * c), int(ydec * c));
partialMoveBlock(src, p);
}
uint BaseBoard::findGroup(Square<int> &field, const Coord &c) const
{
uint nb = 0;
_findGroup(field, c, nb, false);
return nb;
}
void BaseBoard::setGroup(Square<int> &field, const Coord &c, uint nb) const
{
_findGroup(field, c, nb, true);
}
void BaseBoard::_findGroup(Square<int> &field, const Coord &c,
uint &nb, bool set) const
{
if (!set) nb++;
field[c] = (set ? (int)nb : -1);
uint value = matrix()[c]->value();
CoordList n = matrix().neighbours(c, true, true);
for (CoordList::const_iterator i = n.begin(); i!=n.end(); ++i)
blockInGroup(field, *i, value, nb, set);
}
void BaseBoard::blockInGroup(Square<int> &field, const Coord &c, uint value,
uint &nb, bool set) const
{
if ( matrix()[c]==0 ) return;
if ( matrix()[c]->value()!=value ) return;
if ( field[c]!=(set ? -1 : 0) ) return;
_findGroup(field, c, nb, set);
}
TQMemArray<uint> BaseBoard::findGroups(Square<int> &field, uint minSize,
bool exitAtFirstFound) const
{
field.fill(0);
TQMemArray<uint> groups;
for (uint j=0; j<firstClearLine(); j++)
for (uint i=0; i<matrix().width(); i++) {
Coord c(i, j);
if ( matrix()[c]==0 || matrix()[c]->isGarbage() ) continue;
if ( field[c]!=0 ) continue;
uint nb = findGroup(field, c);
setGroup(field, c, nb);
if ( nb>=minSize ) {
uint s = groups.size();
groups.resize(s+1);
groups[s] = nb;
if (exitAtFirstFound) return groups;
}
}
return groups;
}
uint BaseBoard::drawCode(const Coord &c) const
{
uint v = matrix()[c]->value();
uint code = 0;
for (uint i=0; i<Nb_Direction; i++) {
Coord nc = SquareBase::neighbour(c, DIRECTION_DATA[i].neighbour);
if ( !matrix().inside(nc) || matrix()[nc]==0
|| matrix()[nc]->value()!=v ) continue;
code |= DIRECTION_DATA[i].direction;
}
return code;
}
void BaseBoard::computeNeighbours()
{
for (uint j=0; j<firstClearLine(); j++)
for (uint i=0; i<matrix().width(); i++) {
Coord c(i, j);
if ( matrix()[c]==0 || matrix()[c]->isGarbage() ) continue;
matrix()[c]->sprite()->setFrame( drawCode(c) );
}
}
|