summaryrefslogtreecommitdiffstats
path: root/klines/klines.cpp
blob: 43d60835c1c1ded61fa8f3d5a9fa9a5887844a15 (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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/***************************************************************************
    begin                : Fri May 19 2000
    copyright            : (C) 2000 by Roman Merzlyakov
    email                : [email protected]
    copyright            : (C) 2000 by Roman Razilov
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

/* changes
21.05.2000    Roman Razilov     Menu game/Next
*/
//
// The implementation of the KLines widget
//

#include <tqkeycode.h>
#include <tqlabel.h>
#include <tqpushbutton.h>
#include <tqtooltip.h>
#include <tqstring.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#include <tdeapplication.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdeconfig.h>
#include <tdemenubar.h>
#include <tdepopupmenu.h>
#include <highscore/kscoredialog.h>
#include <tdeaction.h>
#include <kstdaction.h>
#include <kstdgameaction.h>

#include "cfg.h"
#include "prefs.h"
#include <kstatusbar.h>
#include "klines.moc"

enum { Nb_Levels = 5 };
static const char *LEVEL[Nb_Levels] = {
    I18N_NOOP("Very Easy"), I18N_NOOP("Easy"), I18N_NOOP("Normal"), I18N_NOOP("Hard"),
    I18N_NOOP("Very Hard")
};


/*
   Creates the KLines widget and sets saved options (if any).
*/

KLines::KLines()
{
  mwidget = new MainWidget(this);
  setCentralWidget( mwidget );

  lsb = mwidget->GetLsb();
  connect(lsb, TQT_SIGNAL(endTurn()), this, TQT_SLOT(makeTurn()));
  connect(lsb, TQT_SIGNAL(eraseLine(int)), this, TQT_SLOT(addScore(int)));
  connect(lsb, TQT_SIGNAL(endGame()), this, TQT_SLOT(endGame()));
  connect(lsb, TQT_SIGNAL(userTurn()), this, TQT_SLOT(userTurn()));

  lPrompt = mwidget->GetPrompt();

  score = 0;
  score_undo = 0;
  bDemo = false;

  statusBar()->insertItem(i18n(" Score:"), 1, 1);
  statusBar()->setItemAlignment(1, AlignVCenter | AlignLeft);
  statusBar()->insertItem(i18n(" Level: "), 0, 1);
  statusBar()->setItemAlignment(0, AlignVCenter | AlignLeft);
 
  initTDEAction();

  connect(&demoTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotDemo()));

  setFocusPolicy(TQWidget::StrongFocus);
  setFocus();

  startGame();
}

/*
   Saves the options and destroys the KLines widget.
*/
KLines::~KLines()
{
  Prefs::setLevel(levelAction->currentItem()-2);
  Prefs::writeConfig();
}

/*
   Init TDEAction objects (menubar, toolbar, shortcuts)
*/
void KLines::initTDEAction()
{
  KStdGameAction::gameNew(this, TQT_SLOT(startGame()), actionCollection());
  act_demo = KStdGameAction::demo(this, TQT_SLOT(startDemo()), actionCollection());
  act_demo->setText(i18n("Start &Tutorial"));
  KStdGameAction::highscores(this, TQT_SLOT(viewHighScore()), actionCollection());
  KStdGameAction::quit(this, TQT_SLOT(close()), actionCollection());
  endTurnAction = KStdGameAction::endTurn(this, TQT_SLOT(makeTurn()), actionCollection());
  showNextAction = new TDEToggleAction(i18n("&Show Next"), TDEShortcut(CTRL+Key_P),
                                this, TQT_SLOT(switchPrompt()), actionCollection(), "options_show_next");
  showNextAction->setCheckedState(i18n("Hide Next"));
  showNumberedAction = new TDEToggleAction(i18n("&Use Numbered Balls"), TDEShortcut(),
                                this, TQT_SLOT(switchNumbered()), actionCollection(), "options_show_numbered");
  undoAction = KStdGameAction::undo(this, TQT_SLOT(undo()), actionCollection());

  levelAction = KStdGameAction::chooseGameType(0, 0, actionCollection());
  TQStringList items;
  for (uint i=0; i<Nb_Levels; i++)
      items.append( i18n(LEVEL[i]) );
  levelAction->setItems(items);

  levelAction->setCurrentItem(Prefs::level()+2);
  showNextAction->setChecked(Prefs::showNext());
  showNumberedAction->setChecked(Prefs::numberedBalls());
  lPrompt->setPrompt(Prefs::showNext());

  (void)new TDEAction(i18n("Move Left"), Key_Left, lsb, TQT_SLOT(moveLeft()), actionCollection(), "left");
  (void)new TDEAction(i18n("Move Right"), Key_Right, lsb, TQT_SLOT(moveRight()), actionCollection(), "right");
  (void)new TDEAction(i18n("Move Up"), Key_Up, lsb, TQT_SLOT(moveUp()), actionCollection(), "up");
  (void)new TDEAction(i18n("Move Down"), Key_Down, lsb, TQT_SLOT(moveDown()), actionCollection(), "down");
  (void)new TDEAction(i18n("Move Ball"), Key_Space, lsb, TQT_SLOT(placePlayerBall()), actionCollection(), "place_ball");

  setupGUI( TDEMainWindow::Save | Keys | StatusBar | Create );
}

void KLines::startGame()
{
    score = 0;
    score_undo = 0;
    bUndo = true;
    bNewTurn = true;
    if(bDemo)
        stopDemo();

    bFirst = true;

    int level = levelAction->currentItem()-2;
    setLevel(level);

    lsb->setLevel(level);
    lsb->setGameOver(false);
    lsb->clearField();
    generateRandomBalls();
    placeBalls();
    generateRandomBalls();
    undoAction->setEnabled(false);
    endTurnAction->setEnabled(true);
    updateStat();
}

void KLines::setLevel(int level) {
    levelStr = i18n(LEVEL[level+2]);
    statusBar()->changeItem(i18n(" Level: %1").arg(levelStr), 0);
}

void KLines::startDemo()
{
    if (bDemo)
    {
       stopDemo();
       return;
    }
    score = 0;
    score_undo = 0;
    bUndo = true;
    bNewTurn = true;
    bDemo = true;
    act_demo->setText(i18n("Stop &Tutorial"));
    bFirst = true;

    levelStr = i18n("Tutorial");
    statusBar()->changeItem(i18n(" Level: %1").arg(levelStr), 0);

    lsb->startDemoMode();
    lsb->setGameOver(false);
    lsb->clearField();
    lsb->update();
    undoAction->setEnabled(false);
    endTurnAction->setEnabled(false);
    generateRandomBalls();

    demoStep = 0;
    demoTimer.start(1000, true);
}

void KLines::stopDemo()
{
    bDemo = false;
    lsb->hideDemoText();
    demoTimer.stop();
    statusBar()->changeItem(i18n(" Level: %1").arg(i18n("Tutorial - Stopped")), 0);
    act_demo->setText(i18n("Start &Tutorial"));
}

void KLines::slotDemo()
{
    bool newBalls = false;
    int ballColors = -1;
    int clickX = 0;
    int clickY = 0;
    TQString msg;
    demoStep++;
    if ((demoStep % 2) == 0)
    {
       lsb->hideDemoText();
       demoTimer.start(1000, true);
       return;
    }
    if (demoStep == 1)
    {
       msg = i18n("The goal of the game is to put\n"
                  "5 balls of the same color in line.");
    }
    else if (demoStep == 3)
    {
       newBalls = true;
    }
    else if (demoStep == 5)
    {
       msg = i18n("You can make horizontal, vertical\n"
                  "and diagonal lines.");
    }
    else if (demoStep == 7)
    {
       newBalls = true;
    }
    else if (demoStep == 9)
    {
       msg = i18n("Each turn, three new balls are placed on the board.");
    }
    else if (demoStep == 11)
    {
       newBalls = true;
    }
    else if (demoStep == 13)
    {
       msg = i18n("Every turn, you can move one ball.");
    }
    else if (demoStep == 15)
    {
       newBalls = true;
       ballColors = 56;
    }
    else if (demoStep == 17)
    {
       msg = i18n("To move a ball, click on it with the mouse,\n"
                  "then click where you want the ball to go.");
    }
    else if (demoStep == 19)
    {
       clickX = 6;
       clickY = 6;
    }
    else if (demoStep == 21)
    {
       clickX = 6;
       clickY = 9;
    }
    else if (demoStep == 23)
    {
       msg = i18n("You just moved the blue ball!");
    }
    else if (demoStep == 25)
    {
       newBalls = true;
    }
    else if (demoStep == 27)
    {
       msg = i18n("Balls can be moved to every position on the board,\n"
                  "as long as there are no other balls in their way.");
    }
    else if (demoStep == 29)
    {
       clickX = 4;
       clickY = 3;
       demoStep++;
    }
    else if (demoStep == 31)
    {
       clickX = 7;
       clickY = 9;
    }
    else if (demoStep == 33)
    {
       msg = i18n("Now we only need one more blue ball.");
    }
    else if (demoStep == 35)
    {
       newBalls = true;
    }
    else if (demoStep == 37)
    {
       msg = i18n("It seems to be our lucky day!");
    }
    else if (demoStep == 39)
    {
       clickX = 8;
       clickY = 2;
       demoStep++;
    }
    else if (demoStep == 41)
    {
       clickX = 8;
       clickY = 9;
    }
    else if (demoStep == 43)
    {
       msg = i18n("Hurray! And away they go!\n"
                  "Now lets try the green balls.");
    }
    else if (demoStep == 45)
    {
       clickX = 8;
       clickY = 7;
       demoStep++;
    }
    else if (demoStep == 47)
    {
       clickX = 4;
       clickY = 5;
       lsb->demoAdjust(42);
    }
    else if (demoStep == 49)
    {
       newBalls = true;
    }
    else if (demoStep == 51)
    {
       msg = i18n("Now you try!\n"
                  "Click on the green ball and move it to the others!");
       demoStep++;
    }
    else if (demoStep == 53)
    {
       lsb->hideDemoText();
       lsb->adjustDemoMode(true, false);
       demoStep++;
    }
    else if (demoStep == 55)
    {
       msg = i18n("Almost, try again!");
       demoStep -= 4;
    }
    else if (demoStep == 57)
    {
       msg = i18n("Very good!");
    }
    else if (demoStep == 59)
    {
       msg = i18n("Whenever you complete a line you get an extra turn.");
    }
    else if (demoStep == 61)
    {
       msg = i18n("This is the end of this tutorial.\n"
                  "Feel free to finish the game!");
       demoStep++;
    }
    else if (demoStep == 63)
    {
       lsb->hideDemoText();
       lsb->adjustDemoMode(true, true);
       bDemo = false;
       act_demo->setText(i18n("Start &Tutorial"));
    }

    if (!msg.isEmpty())
    {
       lsb->showDemoText(msg);
       demoTimer.start(3500 + msg.contains("\n")*1500, true);
       return;
    }
    if (newBalls)
    {
       placeBalls();
       if (ballColors == -1)
       {
          generateRandomBalls();
       }
       else
       {
          for( int i = 0 ; i < BALLSDROP ; i++ )
          {
             nextBalls[i] = ballColors % 10;
             ballColors = ballColors / 10;
             lPrompt->SetBalls(nextBalls);
          }
       }

       updateStat();
       demoTimer.start(1000, true);
       return;
    }
    if (clickX)
    {
       lsb->demoClick(clickX-1, clickY-1);
       if (hasFocus())
          demoTimer.start(1000, true);
       return;
    }
}

void KLines::focusOutEvent(TQFocusEvent *ev)
{
   if (bDemo)
   {
      lsb->hideDemoText();
      demoTimer.stop();
      statusBar()->changeItem(i18n(" Level: %1").arg(i18n("Tutorial - Paused")), 0);
   }
   TDEMainWindow::focusOutEvent(ev);
}

void KLines::focusInEvent(TQFocusEvent *ev)
{
   if (bDemo)
   {
      statusBar()->changeItem(i18n(" Level: %1").arg(levelStr), 0);
      slotDemo();
   }
   TDEMainWindow::focusInEvent(ev);
}

void KLines::stopGame()
{
    if (!lsb->gameOver())
       endGame();
    startGame();
}

void KLines::searchBallsLine()
{
}

void KLines::generateRandomBalls()
{
    score_undo = score;
    for( int i = 0 ; i < BALLSDROP ; i++ )
    {
      nextBalls_undo[i] = nextBalls[i];
      nextBalls[i] = bUndo ?
            lsb->random(NCOLORS) :
            nextBalls_redo[i];
    }
    lPrompt->SetBalls(nextBalls);
}

void KLines::placeBalls()
{
    lsb->placeBalls(nextBalls);
}

void KLines::undo()
{
    if (lsb->gameOver())
       return;
    if (!bUndo)
      return;
    for( int i = 0 ; i < BALLSDROP ; i++ )
    {
      nextBalls_redo[i] = nextBalls[i];
      nextBalls[i] = nextBalls_undo[i];
    }
    score = score_undo;
    updateStat();
    lPrompt->SetBalls(nextBalls);
    lsb->undo();
    switchUndo(FALSE);
}

void KLines::makeTurn()
{
    if (bDemo)
    {
       lsb->adjustDemoMode(false, false);
       demoTimer.start(100, true);
    }
    if (lsb->gameOver())
       return;
    if (!bDemo){
       placeBalls();
       if(sender() != lsb)
         lsb->saveUndo();
    }
    bNewTurn = true;
}

void KLines::userTurn()
{
    if(bNewTurn)
    {
       bNewTurn = false;
       if (!bDemo)
          generateRandomBalls();
       if (!bFirst && !bDemo)
          switchUndo(true);
    }
    bFirst = false;
}

void KLines::addScore(int ballsErased)
{   if(ballsErased >= 5){
      score += 2*ballsErased*ballsErased - 20*ballsErased + 60 ;
      if( !lPrompt->getState() ) score+= 1;
      updateStat();
    };
    if (bDemo)
    {
      lsb->adjustDemoMode(false, false);
      demoStep += 2;
      if (hasFocus())
         demoTimer.start(100, true);
    }
}

void KLines::updateStat()
{
    statusBar()->changeItem(i18n(" Score: %1").arg(score), 1);
}

void KLines::viewHighScore()
{
    KScoreDialog d(KScoreDialog::Name | KScoreDialog::Score | KScoreDialog::Level, this);
    d.exec();
}

void KLines::endGame()
{
    lsb->setGameOver(true);
    lsb->repaint(false);

    if (bDemo)
       return;

    KScoreDialog d(KScoreDialog::Name | KScoreDialog::Score | KScoreDialog::Level, this);
    KScoreDialog::FieldInfo scoreInfo;
    scoreInfo.insert(KScoreDialog::Level, levelStr);
    if (d.addScore(score, scoreInfo, true))
        d.exec();
}

void KLines::switchPrompt()
{
    Prefs::setShowNext(!Prefs::showNext());
    lPrompt->setPrompt(Prefs::showNext());
    showNextAction->setChecked(Prefs::showNext());
}

void KLines::switchNumbered()
{
    Prefs::setNumberedBalls(!Prefs::numberedBalls());
    lPrompt->setPrompt(Prefs::showNext());
    showNumberedAction->setChecked(Prefs::numberedBalls());
    mwidget->updatePix();
}

void KLines::switchUndo(bool bu)
{
    bUndo = bu;
    undoAction->setEnabled(bu);
}

void KLines::keyPressEvent(TQKeyEvent *e)
{
    if (lsb->gameOver() && (e->key() == TQt::Key_Space))
    {
        e->accept();
        startGame();
        return;
    }
    TDEMainWindow::keyPressEvent(e);
}