summaryrefslogtreecommitdiffstats
path: root/ktron/ktron.cpp
blob: 85478071c0b48a9fab82e7d787118f21b1b6bed1 (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
/* ****************************************************************************
   This file is part of the game 'KTron'

  Copyright (C) 1998-2000 by Matthias Kiefer <[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.

  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.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

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

#include "ktron.h"

#include <tdeconfigdialog.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdeaction.h>
#include <kstdgameaction.h>
#include <tdeapplication.h>
#include <kstatusbar.h>

// Settings
#include "settings.h"
#include "general.h"
#include "ai.h"
#include "appearance.h"

#define ID_STATUS_BASE 40
#define MESSAGE_TIME 2000
#define WINNING_DIFF 5

/**
 * Constuctor
 */ 
KTron::KTron(TQWidget *parent, const char *name) : TDEMainWindow(parent, name) {
  playerPoints[0]=playerPoints[1]=0;

  tron=new Tron(this, "Tron");
  connect(tron,TQ_SIGNAL(gameEnds(Player)),TQ_SLOT(changeStatus(Player)));
  setCentralWidget(tron);
  tron->setMinimumSize(200,180);

  // create statusbar
  statusBar()->insertItem("abcdefghijklmnopqrst: 0  ",ID_STATUS_BASE+1);
  statusBar()->insertItem("abcdefghijklmnopqrst: 0  ",ID_STATUS_BASE+2);

  // We match up keyboard events ourselves in Tron::keyPressEvent()
  // We must disable the actions, otherwise we don't get the keyPressEvent's
  TDEAction *act;
  act = new TDEAction(i18n("Player 1 Up"), Key_R, 0, 0, actionCollection(), "Pl1Up");
  act->setEnabled(false);
  act = new TDEAction(i18n("Player 1 Down"), Key_F, 0, 0, actionCollection(), "Pl1Down");
  act->setEnabled(false);
  act = new TDEAction(i18n("Player 1 Right"), Key_G, 0, 0, actionCollection(), "Pl1Right");
  act->setEnabled(false);
  act = new TDEAction(i18n("Player 1 Left"), Key_D, 0, 0, actionCollection(), "Pl1Left");
  act->setEnabled(false);
  act = new TDEAction(i18n("Player 1 Accelerator"), Key_A, 0, 0, actionCollection(), "Pl1Ac");
  act->setEnabled(false);
 
  act = new TDEAction(i18n("Player 2 Up"), Key_Up, 0, 0, actionCollection(), "Pl2Up");
  act->setEnabled(false);
  act = new TDEAction(i18n("Player 2 Down"), Key_Down, 0, 0, actionCollection(), "Pl2Down");
  act->setEnabled(false);
  act = new TDEAction(i18n("Player 2 Right"), Key_Right, 0, 0, actionCollection(), "Pl2Right");
  act->setEnabled(false);
  act = new TDEAction(i18n("Player 2 Left"), Key_Left, 0, 0, actionCollection(), "Pl2Left");
  act->setEnabled(false);
  act = new TDEAction(i18n("Player 2 Accelerator"), Key_0, 0, 0, actionCollection(), "Pl2Ac");
  act->setEnabled(false);

  tron->setActionCollection(actionCollection());

  KStdGameAction::pause(tron, TQ_SLOT(togglePause()), actionCollection());
  KStdGameAction::gameNew( tron, TQ_SLOT( newGame() ), actionCollection() );
  KStdGameAction::quit(this, TQ_SLOT( close() ), actionCollection());
  KStdAction::preferences(this, TQ_SLOT(showSettings()), actionCollection());

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

void KTron::loadSettings() {
   playerName[0]=Settings::namePlayer1();
   if ( playerName[0].isEmpty() )
       playerName[0] = i18n("Player 1");
   playerName[1]=Settings::namePlayer2();
   if ( playerName[1].isEmpty() )
       playerName[1] = i18n("Player 2");
   
   updateStatusbar();
}

void KTron::updateStatusbar(){
  for(int i=0;i<2;i++){
    Player player;
    player=(i==0?One:Two);

    TQString name;
    if(tron->isComputer(Both))
      name=i18n("Computer(%1)").arg(i+1);
    else if(tron->isComputer(player))
      name=i18n("Computer");
    else
      name=playerName[i];
    TQString string = TQString("%1: %2").arg(name).arg(playerPoints[i]);
    statusBar()->changeItem(string,ID_STATUS_BASE+i+1);
  }
}

void KTron::changeStatus(Player player) {
  // if player=Nobody, then new game
  if(player==Nobody){
    playerPoints[0]=playerPoints[1]=0;
    updateStatusbar();
    return;
  }
  
  if(player==One)
    playerPoints[0]++;
  else if(player==Two)
    playerPoints[1]++;
  else if(player==Both){
    playerPoints[0]++;
    playerPoints[1]++;
  }

  updateStatusbar();

  if(playerPoints[0]>=WINNING_DIFF && playerPoints[1] < playerPoints[0]-1)
    showWinner(One);
  else if(playerPoints[1]>=WINNING_DIFF && playerPoints[0] < playerPoints[1]-1)
    showWinner(Two);
}

void KTron::showWinner(Player winner){
  if(tron->isComputer(Both) || (winner != One && winner != Two))
    return;

  TQString loserName = i18n("KTron");
  int loser = Two;
  if(winner == Two)
    loser = One;
  if(!tron->isComputer(((Player)loser)))
    loserName = playerName[loser];
  
  TQString winnerName = i18n("KTron");
  if(!tron->isComputer(winner))
    winnerName = playerName[winner];
  
  TQString message=i18n("%1 has won!").arg(winnerName);
  statusBar()->message(message,MESSAGE_TIME);

  message = i18n("%1 has won versus %2 with %3 : %4 points!");
      message=message.arg(winnerName).arg(loserName);
      message=message.arg(playerPoints[winner]).arg(playerPoints[loser]);
  
  KMessageBox::information(this, message, i18n("Winner"));
  tron->newGame();
}

void KTron::paletteChange(const TQPalette &/*oldPalette*/){
   update();
   tron->updatePixmap();
   tron->update();
}

/**
 * Show Settings dialog.
 */
void KTron::showSettings(){
  if(TDEConfigDialog::showDialog("settings"))
    return;
  
  TDEConfigDialog *dialog = new TDEConfigDialog(this, "settings", Settings::self());
  dialog->addPage(new General(0, "General"), i18n("General"), "package_settings");
  dialog->addPage(new Ai(0, "Ai"), i18n("A.I."), "preferences-desktop-personal");
  dialog->addPage(new Appearance(0, "Appearance"), i18n("Appearance"), "style");
  connect(dialog, TQ_SIGNAL(settingsChanged()), tron, TQ_SLOT(loadSettings()));
  connect(dialog, TQ_SIGNAL(settingsChanged()), this, TQ_SLOT(loadSettings()));
  dialog->show();
}

#include "ktron.moc"