1 /*
2  *      boatstabitem.h
3  *
4  *      Copyright 2014 David Vachulka <arch_dvx@users.sourceforge.net>
5  *
6  *      This program is free software; you can redistribute it and/or modify
7  *      it under the terms of the GNU General Public License as published by
8  *      the Free Software Foundation; either version 3 of the License, or
9  *      (at your option) any later version.
10  *
11  *      This program is distributed in the hope that it will be useful,
12  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *      GNU General Public License for more details.
15  *
16  *      You should have received a copy of the GNU General Public License
17  *      along with this program; if not, write to the Free Software
18  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *      MA 02110-1301, USA.
20  */
21 
22 
23 #ifndef BOATSTABITEM_H
24 #define BOATSTABITEM_H
25 
26 #include "engine/ircengine.h"
27 #include "dxtabitem.h"
28 
29 static const int bcolumns = 10;
30 static const int brows = 10;
31 static const int boats = 10;
32 
33 enum CELLTYPE {
34     CLEAR = 0,
35     BOAT,
36     FIRE,
37     WATER,
38     HIT,
39     PLACED
40 };
41 
42 struct BoatsCell
43 {
44     BoatsCell(int x1 = -1, int y1 = -1, CELLTYPE type1 = CLEAR)
xBoatsCell45     :   x(x1),
46         y(y1),
47         type(type1)
48     {
49     }
50 
51     bool operator==(const BoatsCell& cell) const
52     {
53         return x == cell.x && y == cell.y && type == cell.type;
54     }
55 
56     int x;
57     int y;
58     CELLTYPE type;
59 };
60 
61 struct Boat
62 {
BoatBoat63     Boat(int x1, int y1, int cells1, bool rotated1)
64     :   x(x1),
65         y(y1),
66         cells(cells1),
67         rotated(rotated1)
68     {
69     }
70 
BoatBoat71     Boat()
72     : x(-1),
73       y(-1),
74       cells(0),
75       rotated(false)
76     {
77     }
78 
79     bool operator ==(const Boat& boat) const
80     {
81         return x == boat.x && y == boat.y && cells == boat.cells
82                 && rotated == boat.rotated;
83     }
84 
isValidBoat85     bool isValid() const
86     {
87         return x != -1 && y != -1;
88     }
89 
constainsCellBoat90     bool constainsCell(int x1, int y1)
91     {
92         if(!isValid())
93             return false;
94         if(rotated)
95         {
96             return x1 == x && y1 >= y && y1 < y+cells;
97         }
98         else
99         {
100             return y1 == y && x1 >= x && x1 < x+cells;
101         }
102     }
103 
104     int x;
105     int y;
106     int cells;
107     bool rotated;
108 };
109 
110 class Boats : public QWidget
111 {
112     Q_OBJECT
113 public:
114     Boats(bool first, QWidget *parent = 0);
115 
116     void setCellType(int x, int y, bool my, CELLTYPE type);
117     CELLTYPE getCellType(int x, int y, bool my);
118     bool first() const;
119     void setFirst(bool first);
120     bool onFire() const;
121     void setOnFire(bool onFire);
122     bool readyToPlay() const;
123     void setReadyToPlay(bool readyToPlay);
124     bool placeBoats() const;
125     void setPlaceBoats(bool placeBoats);
126     bool boatDestroyed(int x, int y);
127     void clearCellsAndBoats();
128     bool gameOver();
129 signals:
130     void ready();
131     void statusText(const QString &text);
132     void fire(int x, int y);
133 protected:
134     void mousePressEvent(QMouseEvent* event);
135     void mouseMoveEvent(QMouseEvent* event);
136     void paintEvent(QPaintEvent*);
137     void resizeEvent(QResizeEvent* event);
138 private:
139     int m_cell;
140     BoatsCell m_mycells[bcolumns][brows];
141     BoatsCell m_hiscells[bcolumns][brows];
142     Boat m_boats[boats];
143     bool m_first; //I fire first
144     bool m_placeBoats;
145     bool m_rotated; //Place rotated boat
146     int m_previousX; //Placed boat
147     int m_previousY; //Placed boat
148     int m_cells; //Placed boat
149     bool m_onFire;
150     bool m_readyToPlay;
151 
152     void placeMyBoats();
153     QColor getCellColor(CELLTYPE type);
154     bool isRightForPlaceBoat(int x, int y);
155 };
156 
157 class BoatsTabItem: public dxTabItem
158 {
159     Q_OBJECT
160     friend class dxirc;
161 public:
162     BoatsTabItem(const QString &name, bool first, TabWidget *tabbook, int id=0, IrcEngine *engine=0, QWidget *parent = 0);
163     virtual ~BoatsTabItem();
164 
getServerName()165     QString getServerName() { return m_engine ? m_engine->getServerName() : ""; }
getServerPort()166     int getServerPort() { return m_engine ? m_engine->getServerPort() : 0; }
getNickName()167     QString getNickName() { return m_engine ? m_engine->getNickName() : ""; }
getRealServerName()168     QString getRealServerName() { return m_engine ? m_engine->getNetworkName() : ""; }
getType()169     TYPE getType() { return m_type; }
getEngine()170     IrcEngine* getEngine() { return m_engine; }
getTabName()171     QString getTabName() { return m_name; }
172     void leftGame();
173     bool isSameServer(IrcEngine *engine);
174 signals:
175     void newname(QString);
176 private:
177     IrcEngine *m_engine;
178     TYPE m_type;
179     QString m_name;
180     QLabel *m_status;
181     Boats *m_gamecanvas;
182     QPushButton *m_newGame;
183 
184     void sendCtcp(const QString &msg);
185 private slots:
186     //IrcEngine slots
187     void slotIrcNick(QString nick, QString newnick);
188     void slotIrcQuit(QString nick);
189     void slotIrc401(QString nick);
190     void slotIrcBoats(QString nick, QString position, QString status);
191     void slotIrcBoatsReady(QString nick);
192     void slotIrcBoatsLeft(QString nick);
193     void slotIrcBoatsDestoyed(QString nick);
194     void slotIrcBoatsNewGame(QString nick);
195     void slotIrcBoatsWin(QString nick);
196     //Boats slots
197     void slotFire(int x, int y);
198     void slotStatusText(QString text);
199     void slotReady();
200     //Other slots
201     void slotNewGame();
202 };
203 
204 #endif // BOATSTABITEM_H
205