summaryrefslogtreecommitdiffstats
path: root/knetwalk/src/mainwindow.cpp
blob: 13007ea6075fe30d97bd4717ac891e9edc4362f6 (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
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
/***************************************************************************
 *   Copyright (C) 2004, 2005 Andi Peredri                                 *
 *   [email protected]                                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2        *
 *   as published by the Free Software Foundation (see COPYING)            *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/

#include <tqaction.h>
#include <tqapplication.h>
#include <tqeventloop.h>
#include <tqgrid.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlineedit.h>
#include <tqprocess.h>
#include <tqpushbutton.h>
#include <tqsettings.h>
#include <tqsound.h>
#include <tqtimer.h>
#include <tqtoolbutton.h>
#include <tqwhatsthis.h>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <tdeglobal.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <highscore/kscoredialog.h>
#include <highscore/khighscore.h>
#include <kstdaction.h>
#include <tdeaction.h>
#include <kstdgameaction.h>
#include <tdemenubar.h>
#include <kstatusbar.h>
#include <kdebug.h>
#include <knotifyclient.h>
#include <knotifydialog.h>
#include <highscore/kexthighscore.h>

#include "settings.h"
#include "cell.h"
#include "mainwindow.h"

static TQMap<Cell::Dirs, Cell::Dirs> contrdirs;

MainWindow::MainWindow(TQWidget *parent, const char* name, WFlags /*fl*/) : 
	TDEMainWindow(parent, name, WStyle_NoBorder)
{
	m_clickcount = 0;
	
	contrdirs[Cell::U] = Cell::D;
	contrdirs[Cell::R] = Cell::L;
	contrdirs[Cell::D] = Cell::U;
	contrdirs[Cell::L] = Cell::R;

	KNotifyClient::startDaemon();
	
	KStdGameAction::gameNew(TQT_TQOBJECT(this), TQT_SLOT(slotNewGame()), actionCollection());
	
	KStdGameAction::highscores(TQT_TQOBJECT(this), TQT_SLOT(showHighscores()), actionCollection());
	KStdGameAction::quit(TQT_TQOBJECT(this), TQT_SLOT(close()), actionCollection());
	KStdGameAction::configureHighscores(TQT_TQOBJECT(this), TQT_SLOT(configureHighscores()), actionCollection());

	m_levels = KStdGameAction::chooseGameType(0, 0, actionCollection());
	TQStringList lst;
	lst += i18n("Novice");
	lst += i18n("Normal");
	lst += i18n("Expert");
	lst += i18n("Master");
	m_levels->setItems(lst);

	setFixedSize(minimumSizeHint());

	statusBar()->insertItem("abcdefghijklmnopqrst: 0  ",1);
	setAutoSaveSettings();
	createGUI();
	connect(m_levels, TQT_SIGNAL(activated(int)), this, TQT_SLOT(newGame(int)));
	

	TQWhatsThis::add(this, i18n("<h3>Rules of the Game</h3>"
			"<p>You are the system administrator and your goal"
			" is to connect each computer to the central server."
			"<p>Click the right mouse button to turn the cable"
			" in a clockwise direction, and the left mouse button"
			" to turn it in a counter-clockwise direction."
			"<p>Start the LAN with as few turns as possible!"));
	
	//const int cellsize = TDEGlobal::iconLoader()->loadIcon("knetwalk/background.png", TDEIcon::User, 32).width();
	const int cellsize = 32;
	const int gridsize = cellsize * MasterBoardSize + 2;

	TQGrid* grid = new TQGrid(MasterBoardSize, this);
	grid->setFrameStyle(TQFrame::Panel | TQFrame::Sunken);
	grid->setFixedSize(gridsize, gridsize);
	setCentralWidget(grid);

	Cell::initPixmaps();
	for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++)
	{
		board[i] = new Cell(grid, i);
		board[i]->setFixedSize(cellsize, cellsize);
		connect(board[i], TQT_SIGNAL(lClicked(int)), TQT_SLOT(lClicked(int)));
		connect(board[i], TQT_SIGNAL(rClicked(int)), TQT_SLOT(rClicked(int)));
		connect(board[i], TQT_SIGNAL(mClicked(int)), TQT_SLOT(mClicked(int)));
	}
	srand(time(0));

	slotNewGame();
}

void MainWindow::configureHighscores()
{
	KExtHighscore::configure(this);
}

void MainWindow::showHighscores()
{
	KExtHighscore::show(this);
}

void MainWindow::slotNewGame()
{
	newGame( Settings::skill() );
}

void MainWindow::newGame(int sk)
{
	if (sk==Settings::EnumSkill::Novice || sk==Settings::EnumSkill::Normal 
			|| sk==Settings::EnumSkill::Expert || sk==Settings::EnumSkill::Master)
	{
		Settings::setSkill(sk);
	}

	if(Settings::skill() == Settings::EnumSkill::Master) wrapped = true;
	else wrapped = false;
	
	KExtHighscore::setGameType(Settings::skill());
	
	Settings::writeConfig();
	
	m_clickcount = 0;
	TQString clicks = i18n("Click: %1");
	statusBar()->changeItem(clicks.arg(TQString::number(m_clickcount)),1);
	
	KNotifyClient::event(winId(), "startsound", i18n("New Game"));
	for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++)
	{
		board[i]->setDirs(Cell::None);
		board[i]->setConnected(false);
		board[i]->setRoot(false);
		board[i]->setLocked(false);
	}

	const int size = (Settings::skill() == Settings::EnumSkill::Novice) ? NoviceBoardSize :
		(Settings::skill() == Settings::EnumSkill::Normal) ? NormalBoardSize :
		(Settings::skill() == Settings::EnumSkill::Expert) ? ExpertBoardSize : MasterBoardSize;

	const int start = (MasterBoardSize - size) / 2;
	const int rootrow = rand() % size;
	const int rootcol = rand() % size;

	root = board[(start + rootrow) * MasterBoardSize + start + rootcol];
	root->setConnected(true);
	root->setRoot(true);

	while(true)
	{
		for(int row = start; row < start + size; row++)
			for(int col = start; col < start + size; col++)
				board[row * MasterBoardSize + col]->setDirs(Cell::Free);

		CellList list;
		list.append(root);
		if(rand() % 2) addRandomDir(list);

		while(!list.isEmpty())
		{
			if(rand() % 2)
			{
				addRandomDir(list);
				if(rand() % 2) addRandomDir(list);
				list.remove(list.begin());
			}
			else
			{
				list.append(list.first());
				list.remove(list.begin());
			}
		}

		int cells = 0;
		for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++)
		{
			Cell::Dirs d = board[i]->dirs();
			if((d != Cell::Free) && (d != Cell::None)) cells++;
		}
		if(cells >= MinimumNumCells) break;
	}

	for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++)
		board[i]->rotate((rand() % 4) * 90);
	updateConnections();
}

bool MainWindow::updateConnections()
{
	bool newconnection[MasterBoardSize * MasterBoardSize];
	for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++)
		newconnection[i] = false;

	CellList list;
	if(!root->isRotated())
	{
		newconnection[root->index()] = true;
		list.append(root);
	}
	while(!list.isEmpty())
	{
		Cell* cell = list.first();
		Cell* ucell = uCell(cell);
		Cell* rcell = rCell(cell);
		Cell* dcell = dCell(cell);
		Cell* lcell = lCell(cell);

		if((cell->dirs() & Cell::U) && ucell && (ucell->dirs() & Cell::D) &&
				!newconnection[ucell->index()] && !ucell->isRotated())
		{
			newconnection[ucell->index()] = true;
			list.append(ucell);
		}
		if((cell->dirs() & Cell::R) && rcell && (rcell->dirs() & Cell::L) &&
				!newconnection[rcell->index()] && !rcell->isRotated())
		{
			newconnection[rcell->index()] = true;
			list.append(rcell);
		}
		if((cell->dirs() & Cell::D) && dcell && (dcell->dirs() & Cell::U) &&
				!newconnection[dcell->index()] && !dcell->isRotated())
		{
			newconnection[dcell->index()] = true;
			list.append(dcell);
		}
		if((cell->dirs() & Cell::L) && lcell && (lcell->dirs() & Cell::R) &&
				!newconnection[lcell->index()] && !lcell->isRotated())
		{
			newconnection[lcell->index()] = true;
			list.append(lcell);
		}
		list.remove(list.begin());
	}

	bool isnewconnection = false;
	for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++)
	{
		if(!board[i]->isConnected() && newconnection[i])
			isnewconnection = true;
		board[i]->setConnected(newconnection[i]);
	}
	return isnewconnection;
}

void MainWindow::addRandomDir(CellList& list)
{
	Cell* cell = list.first();
	Cell* ucell = uCell(cell);
	Cell* rcell = rCell(cell);
	Cell* dcell = dCell(cell);
	Cell* lcell = lCell(cell);

	typedef TQMap<Cell::Dirs, Cell*> CellMap;
	CellMap freecells;

	if(ucell && ucell->dirs() == Cell::Free) freecells[Cell::U] = ucell;
	if(rcell && rcell->dirs() == Cell::Free) freecells[Cell::R] = rcell;
	if(dcell && dcell->dirs() == Cell::Free) freecells[Cell::D] = dcell;
	if(lcell && lcell->dirs() == Cell::Free) freecells[Cell::L] = lcell;
	if(freecells.isEmpty()) return;

	CellMap::ConstIterator it = freecells.constBegin();
	for(int i = rand() % freecells.count(); i > 0; --i) ++it;

	cell->setDirs(Cell::Dirs(cell->dirs() | it.key()));
	it.data()->setDirs(contrdirs[it.key()]);
	list.append(it.data());
}

Cell* MainWindow::uCell(Cell* cell) const
{
	if(cell->index() >= MasterBoardSize)
		return board[cell->index() - MasterBoardSize];
	else if(wrapped)
		return board[MasterBoardSize * (MasterBoardSize - 1) + cell->index()];
	else return 0;
}

Cell* MainWindow::dCell(Cell* cell) const
{
	if(cell->index() < MasterBoardSize * (MasterBoardSize - 1))
		return board[cell->index() + MasterBoardSize];
	else if(wrapped)
		return board[cell->index() - MasterBoardSize * (MasterBoardSize - 1)];
	else return 0;
}

Cell* MainWindow::lCell(Cell* cell) const
{
	if(cell->index() % MasterBoardSize > 0)
		return board[cell->index() - 1];
	else if(wrapped)
		return board[cell->index() - 1 + MasterBoardSize];
	else return 0;
}

Cell* MainWindow::rCell(Cell* cell) const
{
	if(cell->index() % MasterBoardSize < MasterBoardSize - 1)
		return board[cell->index() + 1];
	else if(wrapped)
		return board[cell->index() + 1 - MasterBoardSize];
	else return 0;
}

void MainWindow::lClicked(int index)
{
	rotate(index, true);
}

void MainWindow::rClicked(int index)
{
	rotate(index, false);
}

void MainWindow::mClicked(int index)
{
	board[index]->setLocked( !board[index]->isLocked() );
}

void MainWindow::rotate(int index, bool toleft)
{
	const Cell::Dirs d = board[index]->dirs();
	if((d == Cell::Free) || (d == Cell::None) || isGameOver() || board[index]->isLocked() )
	{
		KNotifyClient::event(winId(), "clicksound");
		blink(index);
	}
	else
	{
		KNotifyClient::event(winId(), "turnsound");
		board[index]->rotate(toleft ? -6 : 6);
		updateConnections();
		for(int i = 0; i < 14; i++)
		{
			kapp->eventLoop()->processEvents(TQEventLoop::ExcludeUserInput);
			TQTimer::singleShot(20, board[index], TQT_SLOT(update()));
			kapp->eventLoop()->processEvents(TQEventLoop::ExcludeUserInput | TQEventLoop::WaitForMore);
			board[index]->rotate(toleft ? -6 : 6);
		}

		if (updateConnections())
			KNotifyClient::event(winId(), "connectsound");
		
		m_clickcount++;
		TQString clicks = i18n("Click: %1");
		statusBar()->changeItem(clicks.arg(TQString::number(m_clickcount)),1);
		
		if (isGameOver())
		{
			KNotifyClient::event(winId(), "winsound");
			blink(index);

			KExtHighscore::Score score(KExtHighscore::Won);
			score.setScore(m_clickcount);
			KExtHighscore::submitScore(score, this);
		}
	}
}

void MainWindow::blink(int index)
{
	for(int i = 0; i < board[index]->width() * 2; i += 2)
	{
		kapp->eventLoop()->processEvents(TQEventLoop::ExcludeUserInput);
		TQTimer::singleShot(20, board[index], TQT_SLOT(update()));
		kapp->eventLoop()->processEvents(TQEventLoop::ExcludeUserInput |
				TQEventLoop::WaitForMore);
		board[index]->setLight(i);
	}
	board[index]->setLight(0);
}

bool MainWindow::isGameOver()
{
	for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++)
	{
		const Cell::Dirs d = board[i]->dirs();
		if((d != Cell::Free) && (d != Cell::None) && !board[i]->isConnected())
			return false;
	}
	return true;
}

void MainWindow::closeEvent(TQCloseEvent* event)
{
	event->accept();
}

void MainWindow::configureNotifications()
{
	KNotifyDialog::configure(this);
}

#include "mainwindow.moc"