summaryrefslogtreecommitdiffstats
path: root/libksirtet/base/board.h
blob: 5ece752bff6e839251473ad0831967e33d2c20bc (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
#ifndef BASE_BOARD_H
#define BASE_BOARD_H

#include <tqtimer.h>
#include <tqcanvas.h>

#include "gtetris.h"

#include <kdemacros.h>

class SequenceArray;
class BlockInfo;

//-----------------------------------------------------------------------------
class TDE_EXPORT FixedCanvasView : public TQCanvasView
{
  TQ_OBJECT
  
public:
  FixedCanvasView(TQWidget *parent = 0, const char *name = 0);
  
  virtual TQSize sizeHint() const;
  
public slots:
  virtual void adjustSize();
};

//-----------------------------------------------------------------------------
class TDE_EXPORT BaseBoard : public FixedCanvasView, public GenericTetris
{
 TQ_OBJECT
  
 public:
    enum Direction { Left = 1, Right = 2, Up = 4, Down = 8, Nb_Direction = 4 };
 private:
    struct DirectionData {
        KGrid2D::SquareBase::Neighbour neighbour;
        Direction direction;
    };
    static const DirectionData DIRECTION_DATA[Nb_Direction];

 public:
    BaseBoard(bool graphic, TQWidget *parent);
    virtual ~BaseBoard();
    void copy(const GenericTetris &);

    void init(bool arcade);
    virtual void start(const GTInitData &);
    virtual void pause();
    virtual void unpause();
    virtual void stop();
    bool isGameOver() const { return state==GameOver; }
    bool isPaused() const { return state==Paused; }

    bool isArcade() const { return _arcade; }
    uint arcadeStage() const { return _arcadeStage; }
    bool arcadeStageDone() const { return _arcadeStageDone; }
    virtual uint arcadeTodo() const { return 0; }
    virtual uint arcadeDone() const { return 0; }
    
    virtual void settingsChanged();
    BlockInfo *next() const { return _next; }

    int firstColumnBlock(uint column) const;
    
 public slots:
    virtual void adjustSize();

 protected slots:
    virtual bool timeout(); // return true if treated

 signals:
    void updatePieceConfigSignal();
    void removedUpdated();
    void scoreUpdated();
    void gameOverSignal();

 protected:
    virtual bool beforeRemove(bool first);
    void _beforeRemove(bool first);
    enum AfterRemoveResult { Done, NeedAfterRemove, NeedRemoving };
    virtual AfterRemoveResult afterRemove(bool doAll, bool first);
    void _afterAfterRemove();
    virtual bool afterAfterRemove() = 0;
    virtual bool startTimer(); // return true if treated
    virtual bool toBeRemoved(const KGrid2D::Coord &) const = 0;
    virtual void remove();
    virtual bool toFall(const KGrid2D::Coord &) const = 0;//height>0 when called
    virtual bool doFall(bool doAll, bool first, bool lineByLine);
    virtual void gameOver();
    virtual void arcadePrepare() {}

    uint drawCode(const KGrid2D::Coord &) const;
    void computeNeighbours();
    void partialBlockFall(const KGrid2D::Coord &src, const KGrid2D::Coord &dest);

    // return the sizes of the groups (>=minSize)
    TQMemArray<uint> findGroups(KGrid2D::Square<int> &field, uint minSize,
							   bool exitAtFirstFound = false) const;
    // find group size and put -1 in the corresponding blocks (these blocks
    // should be 0 at start)
    uint findGroup(KGrid2D::Square<int> &field, const KGrid2D::Coord &) const;
    // set the size of the group in the blocks (these blocks should be -1
    // at start ie you should have called findGroup() before)
    void setGroup(KGrid2D::Square<int> &field, const KGrid2D::Coord &c,
                  uint nb) const;

    void updateRemoved(uint newRemoved);
    void updateScore(uint newScore);

    virtual void showBoard(bool show);
    void showCanvas(TQCanvas *c, bool show);

    enum BoardState { GameOver, Normal, Paused,
                      DropDown, BeforeGlue, AfterGlue, BeforeRemove,
                      AfterRemove, AfterGift };
    BoardState     state, _oldState;
    TQTimer         timer;
    SequenceArray *sequences;
    BlockInfo     *main, *_next;
    uint           loop;

 private:
    bool _arcade, _arcadeStageDone;
    uint _arcadeStage;

    void _afterRemove(bool first);
    void updatePieceConfig() { emit updatePieceConfigSignal(); }

    void _findGroup(KGrid2D::Square<int> &field, const KGrid2D::Coord &c,
                    uint &nb, bool set) const;
    void blockInGroup(KGrid2D::Square<int> &field, const KGrid2D::Coord &c,
                      uint value, uint &nb, bool ser) const;
};

#endif