summaryrefslogtreecommitdiffstats
path: root/kbattleship/kbattleship/kbattleshipview.cpp
blob: 35e1a06a3c624a8bc54b68635abf716441cd50fc (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
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
/***************************************************************************
                             kbattleshipview.cpp
                             -------------------
    Developers: (c) 2000-2001 Nikolas Zimmermann <wildfox@kde.org>
                (c) 2000-2001 Daniel Molkentin <molkentin@kde.org>

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tqlayout.h>

#include <klocale.h>
#include <kdebug.h>
#include "kbattleship.h"
#include "kbattleshipview.moc"

KBattleshipView::KBattleshipView(TQWidget *tqparent, const char *name, bool draw) 
	: TQWidget(tqparent, name, WResizeNoErase), m_drawGrid(draw)
{
	setFixedSize(20 * 32 + 30, 10 * 32 + 20);
	setBackgroundMode(NoBackground);
	setMouseTracking(true);
	installEventFilter(this);

	m_decide = false;
	m_lastX = 0;
	m_lastY = 0;
	m_battlefield = 0;
}

KBattleshipView::~KBattleshipView()
{
	delete m_battlefield;
}

void KBattleshipView::startDrawing()
{
	m_battlefield = new KBattleField(this, m_drawGrid);
}

void KBattleshipView::clearField()
{
	m_battlefield->clearOwnField();
	m_battlefield->clearEnemyField();
}

int KBattleshipView::ownFieldState(int fieldx, int fieldy)
{
	return m_battlefield->ownState(fieldx, fieldy);
}

int KBattleshipView::enemyFieldState(int &fieldx, int &fieldy)
{
	return m_battlefield->enemyState(fieldx, fieldy);
}

void KBattleshipView::previewShip(int fieldx, int fieldy, int type, bool rotate)
{
	m_battlefield->setPreviewState(fieldx, fieldy, type, rotate);
}

void KBattleshipView::changeOwnFieldData(int fieldx, int fieldy, int type)
{
	m_battlefield->setOwnState(fieldx, fieldy, type);
	m_battlefield->drawField();
}

void KBattleshipView::changeEnemyFieldData(int fieldx, int fieldy, int type)
{
	m_battlefield->setEnemyState(fieldx, fieldy, type);
	m_battlefield->drawField();
}

void KBattleshipView::drawEnemyShipsAI(KShipList *list)
{
	KShip *ship;
	int state;
	int grid = m_battlefield->gridSize();
	int width = m_battlefield->enemyRect().width() / grid;
	int height = m_battlefield->enemyRect().height() / grid;

	for(int i = 0; i < width; i++)
	{
		for(int j = 0; j < height; j++)
		{
			ship = list->shipAt(i, j);
			state = enemyFieldState(i, j);
			if (ship && state != KBattleField::HIT && state != KBattleField::DEATH)
			{
				changeEnemyFieldData(i, j, ship->shipTypeEnum(i, j));
			}
		}
	}
}

void KBattleshipView::drawEnemyShipsHuman(KMessage *msg, KShipList *list)
{
	int posx, posy, placedLeft;
	bool left;
	int i = 3;
	while (!msg->field(TQString("ship%1").tqarg(i)).isNull())
	{
		posx = msg->field(TQString("ship%1").tqarg(i)).section(" ", 0, 0).toInt();
		posy = msg->field(TQString("ship%1").tqarg(i)).section(" ", 1, 1).toInt();
		placedLeft = msg->field(TQString("ship%1").tqarg(i)).section(" ", 2, 2).toInt();
		if (placedLeft == 0) left = false;
		else left = true;
		list->addNewShip(!left, posx, posy);
		i--;
	}
	drawEnemyShipsAI(list);
}

KMessage *KBattleshipView::getAliveShips(KShipList *list)
{
	KShip *ship;
	TQString shipPos, shipNum;
	int shipType;
	int grid = m_battlefield->gridSize();
	int width = m_battlefield->enemyRect().width() / grid;
	int height = m_battlefield->enemyRect().height() / grid;
	KMessage *msg = new KMessage(KMessage::WON);
	bool shipsFound[4] = {false, false, false, false};

	for(int i = 0; i < width; i++)
	{
		for(int j = 0; j < height; j++)
		{
			ship = list->shipAt(i, j);
			shipType = list->shipTypeAt(i, j);
			if (ship && !shipsFound[shipType])
			{
				shipPos.sprintf("%d %d %d", i, j, ship->placedLeft());
				shipsFound[shipType] = true;
				shipNum.sprintf("ship%d",shipType);
				msg->addField(shipNum, shipPos);
			}
		}
	}
	return msg;
}

bool KBattleshipView::eventFilter(TQObject *object, TQEvent *event)
{
	if(event->type() == TQEvent::KeyPress && m_decide)
	{
		TQKeyEvent *keyEvent = TQT_TQKEYEVENT(event);
		if(keyEvent->key() == Key_Shift){
			emit sigMouseOverField(m_lastX, m_lastY);
			emit changeShipPlacementDirection();
		}
	}
	else if(event->type() == TQEvent::KeyRelease && m_decide)
	{
		TQKeyEvent *keyEvent = TQT_TQKEYEVENT(event);
		if(keyEvent->key() == Key_Shift){
			emit sigMouseOverField(m_lastX, m_lastY);
			emit changeShipPlacementDirection();
		}
	}
	else if(event->type() == TQEvent::MouseButtonRelease)
	{
		m_decide = false;

		TQMouseEvent *mouseEvent = TQT_TQMOUSEEVENT(event);

		if(mouseEvent->button() == Qt::RightButton){
			emit sigMouseOverField(m_lastX, m_lastY);
			emit changeShipPlacementDirection();
			return true;
		}
		
		if(mouseEvent->button() != Qt::LeftButton)
			return false;

		TQPoint point(mouseEvent->x(), mouseEvent->y());
		TQRect ownRect = m_battlefield->ownRect();
		TQRect enemyRect = m_battlefield->enemyRect();

		TQRect newRect;

		int fieldx = 0;
		int fieldy = 0;

		if(ownRect.tqcontains(point))
			newRect = ownRect;
		else if(enemyRect.tqcontains(point))
			newRect = enemyRect;
		else
			return false;

		int j = -1;

		for(int i = newRect.left(); i <= newRect.right(); i += m_battlefield->gridSize())
		{
			j++;
			TQRect tempRect(i, newRect.top(), m_battlefield->gridSize(), newRect.bottom() - newRect.top());

			if(tempRect.tqcontains(point))
			{
				fieldx = j;
				break;
			}
		}

		j = -1;

		for(int i = newRect.top(); i <= newRect.bottom(); i += m_battlefield->gridSize())
		{
			j++;
			TQRect tempRect(newRect.left(), i, newRect.right() - newRect.left(), m_battlefield->gridSize());

			if(tempRect.tqcontains(point))
			{
				fieldy = j;
				break;
			}
		}

		if( newRect == ownRect)
			emit sigOwnFieldClicked(fieldx, fieldy);
		else if(newRect == enemyRect)
			emit sigEnemyFieldClicked(fieldx, fieldy);
		
		return true;
	}
	else if(event->type() == TQEvent::MouseMove)
	{
		setFocus();
		m_decide = true;

		TQMouseEvent *mouseEvent = TQT_TQMOUSEEVENT(event);

		TQPoint point(mouseEvent->x(), mouseEvent->y());
		TQRect ownRect = m_battlefield->ownRect();

		int fieldx = 0;
		int fieldy = 0;

		if(ownRect.tqcontains(point))
		{
			int j = -1;

			for(int i = ownRect.left(); i <= ownRect.right(); i += m_battlefield->gridSize())
			{
				j++;
				TQRect tempRect(i, ownRect.top(), m_battlefield->gridSize(), ownRect.bottom() - ownRect.top());

				if(tempRect.tqcontains(point))
				{
					fieldx = j;
					break;
				}
			}

			j = -1;

			for(int i = ownRect.top(); i <= ownRect.bottom(); i += m_battlefield->gridSize())
			{
				j++;
				TQRect tempRect(ownRect.left(), i, ownRect.right() - ownRect.left(), m_battlefield->gridSize());

				if(tempRect.tqcontains(point))
				{
					fieldy = j;
					break;
				}
			}

			m_lastX = fieldx;
			m_lastY = fieldy;

			emit sigMouseOverField(fieldx, fieldy);
		}
		else
			m_battlefield->drawField();

		return true;
	}
	else if(event->type() == TQEvent::Paint)
	{
		m_battlefield->drawField();
		return true;
	}

	return TQWidget::eventFilter(object, event);
}