summaryrefslogtreecommitdiffstats
path: root/ksirtet/ksirtet/board.cpp
blob: f50aa1a40bc24ff145cc74e687cd0509b817d8e7 (plain)
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
#include "board.h"
#include "board.moc"

#include "common/misc_ui.h"
#include "common/factory.h"
#include "piece.h"


using namespace KGrid2D;

KSBoard::KSBoard(bool graphic, TQWidget *parent)
    : Board(graphic, new GiftPool(parent), parent),
      filled(matrix().height()), linesRemoved(4)
{
    init();
}

void KSBoard::init()
{
  addRemoved = 0;
  _lastRemoved = 0;
  for (uint i=0; i<4; i++) {
    linesRemoved[i] = 0;
    _lastRemoved += 2<<i; // update
  }
}

void KSBoard::copy(const GenericTetris &g)
{
	Board::copy(g);
	filled = static_cast<const KSBoard &>(g).filled.copy(); // deep copy
}

void KSBoard::start(const GTInitData &data)
{
    init();
    Board::start(data);
}

void KSBoard::computeInfos()
{
    Board::computeInfos();

    filled.fill(0);
    for (uint j=0; j<firstClearLine(); j++)
        for (uint i=0; i<matrix().width(); i++) {
            Coord c(i, j);
            if ( matrix()[c] ) filled[j]++;
        }
}

bool KSBoard::needRemoving()
{
	for(uint j=0; j<firstClearLine(); j++)
		if ( filled[j]==matrix().width() ) return true;
	return false;
}

bool KSBoard::toBeRemoved(const Coord &c) const
{
    return ( filled[c.second]==matrix().width() );
}

void KSBoard::remove()
{
    uint nbFullLines = 0;
	for (uint k=0; k<firstClearLine(); k++)
        if ( filled[k]==matrix().width() ) nbFullLines++;

    Board::remove();

    linesRemoved[nbFullLines-1]++;
    _lastRemoved = 2<<(nbFullLines-1);
	addRemoved += nbFullLines;
    updateRemoved(nbRemoved() + nbFullLines);

    if ( isArcade() ) return;

	// Assign score according to level and nb of full lines (gameboy style)
	switch (nbFullLines) {
	 case 0: break;
	 case 1: updateScore( score() + 40   * level() ); break;
	 case 2: updateScore( score() + 100  * level() ); break;
	 case 3: updateScore( score() + 300  * level() ); break;
	 case 4: updateScore( score() + 1200 * level() ); break;
	}

    updateLevel();
}

bool KSBoard::toFall(const Coord &c) const
{
    return ( filled[c.second-1]==0 );
}

//-----------------------------------------------------------------------------
// Multiplayers methods
uint KSBoard::gift()
{
	uint g = (addRemoved>1 ? addRemoved-1 : 0);
	addRemoved = 0;
	return g;
}

void KSBoard::setGarbageBlock(const Coord &c)
{
   Block *gb = currentPiece()->garbageBlock();
   gb->sprite()->show();
   setBlock(c, gb);
}

bool KSBoard::_putGift(uint nb)
{
	if ( nbClearLines()==0 ) return false;

	// lift all the cases of one line
    // (nbClearLine!=0 --> firstClearLine!=height
	for (uint j=firstClearLine(); j>0; j--)
        for (uint i=0; i<matrix().width(); i++)	{
            Coord src(i, j-1);
            Coord dest(i, j);
            moveBlock(src, dest);
        }

	// fill the emptied low line with garbage :)
	uint _nb = nb;
	uint i;
	do {
		i = randomGarbage.getLong(matrix().width());
        Coord c(i, 0);
		if ( matrix()[c]!=0 ) continue;
        setGarbageBlock(c);
		_nb--;
	} while ( _nb!=0 );

	return true;
}

bool KSBoard::putGift(uint nb)
{
	for (uint k=0; k<nb; k++) {
		if ( !_putGift(matrix().width() - k - 1) ) return false;
        computeInfos();
    }
	return true;
}


//-----------------------------------------------------------------------------
const KSBoard::StageData KSBoard::STAGE_DATA[Nb_Stages] = {
    { 4,  1, false, { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0}},
    { 10, 1, false, { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0}},
    { 20, 1, false, { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0}},

    // 10 blocks on each sides
    { 10, 2, true,  {10,  0,  0,  0,  0,  0,  0,  0,  0, 10}},
    // 10 blocks height triangle
    { 15, 2, true,  { 0,  2,  4,  7, 10, 10,  7,  4,  2,  0}},
    // 10 blocks height opposed triangle
    { 20, 2, true,  {10,  7,  4,  2,  0,  0,  2,  4,  7, 10}}
};

uint KSBoard::normalTime() const
{
    if ( !isArcade() ) return Board::normalTime();
    return cfactory->cbi.baseTime / (1 + stageData().speed);
}

void KSBoard::arcadePrepare()
{
    if ( !stageData().hasPattern ) return;

    for (uint i=0; i<Width; i++) {
        Q_ASSERT( stageData().pattern[i]<=matrix().height() );
        for (uint j=0; j<stageData().pattern[i]; j++)
            setGarbageBlock(Coord(i, j));
    }
    main->update();
}