tron.cpp 40.7 KB
Newer Older
1
/****************************************************************************
Stephan Kulow's avatar
Stephan Kulow committed
2
3
  This file is part of the game 'KTron'

4
  Copyright (C) 1998-2000 by Matthias Kiefer <matthias.kiefer@gmx.de>
Stephan Kulow's avatar
Stephan Kulow committed
5
6
7
8
9

  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.
10

Stephan Kulow's avatar
Stephan Kulow committed
11
12
13
14
  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.
15

Stephan Kulow's avatar
Stephan Kulow committed
16
17
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
Waldo Bastian's avatar
Waldo Bastian committed
18
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
Stephan Kulow's avatar
Stephan Kulow committed
19

20
21
22
23
  ***************************************************************************/
// Background
#include <kio/netaccess.h>
#include <kmessagebox.h>
Stephan Kulow's avatar
Stephan Kulow committed
24

25
// Normal class
26
27
28
#include <qtimer.h>

#include <kdebug.h>
Stephan Kulow's avatar
Stephan Kulow committed
29
#include <klocale.h>
30
#include <kapplication.h>
Stephan Kulow's avatar
Stephan Kulow committed
31
#include <kconfig.h>
Laurent Montel's avatar
Laurent Montel committed
32
#include <kcolordialog.h>
Andreas Beckermann's avatar
Andreas Beckermann committed
33
#include <kaction.h>
Stephan Kulow's avatar
Stephan Kulow committed
34
35
36

#include "tron.h"

37
#define TRON_FRAMESIZE 2
Stephan Kulow's avatar
Stephan Kulow committed
38

39
40
41
/**
 * init-functions
 **/
Stephan Kulow's avatar
Stephan Kulow committed
42

43
Tron::Tron(QWidget *parent,const char *name)
Stephan Kulow's avatar
Stephan Kulow committed
44
45
46
47
48
49
50
  : QWidget(parent,name)
{
  pixmap=0;
  playfield=0;
  beginHint=false;
  lookForward=15;

Waldo Bastian's avatar
Waldo Bastian committed
51
  random.setSeed(0);
Stephan Kulow's avatar
Stephan Kulow committed
52
53
54
55
56

  setFocusPolicy(QWidget::StrongFocus);
  setBackgroundMode(NoBackground);

  gameBlocked=false;
57
  rectSize=10;
Stephan Kulow's avatar
Stephan Kulow committed
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
  timer = new QTimer(this,"timer");
  loadSettings();
  connect(timer, SIGNAL(timeout()), SLOT(doMove()));
  QTimer::singleShot(15000, this,SLOT(showBeginHint()));
}

void Tron::loadSettings(){
  KConfig *config = kapp->config();
  config->setGroup("Game");
  // Colors
  QColor bg("black");
  QColor pl1("red");
  QColor pl2("blue");
  colors[0] = config->readColorEntry("Color_Background",&bg);
  colors[1] = config->readColorEntry("Color_Player1",&pl1);
  colors[2] = config->readColorEntry("Color_Player2",&pl2);
  reset();

  setPalette(colors[0]);
      
  changeWinnerColor = config->readBoolEntry("ChangeWinnerColor",true);
  blockAccelerator = config->readBoolEntry("AcceleratorBlocked",false);
  crashOnOppositeDir=config->readBoolEntry("OppositeDirCrashes",false);

  // Size
  int newSize = config->readNumEntry("RectSize",10);
  if(newSize!=rectSize){
    rectSize=newSize;
    createNewPlayfield();
    reset();
  }

  // Velocity
  int velocity=config->readNumEntry("Velocity",5);
  setVelocity(velocity);

  // Skill
  _skill=(Skill)config->readNumEntry("Skill",(int)Medium);

  // Style
  style=(TronStyle)config->readNumEntry("Style",(int) OLine);
  if(pixmap){
    updatePixmap();
    repaint();
  }
   
  // Backgroundimage
106
107
  setBackgroundPix(NULL);
  if(config->readBoolEntry("BackgroundImageChoice", false)){
Helge Deller's avatar
Helge Deller committed
108
    KURL url = config->readPathEntry("BackgroundImage");
109
110
111
112
113
114
115
116
    if(!url.isEmpty()){
      QString tmpFile;
      KIO::NetAccess::download(url, tmpFile);
      QPixmap pix(tmpFile);
      if(!pix.isNull()){
        setBackgroundPix(pix);
      } else {
  	QString msg=i18n("Wasn't able to load wallpaper\n%1");
117
118
	msg=msg.arg(tmpFile);
	KMessageBox::sorry(this, msg);
119
120
      }
      KIO::NetAccess::removeTempFile(tmpFile);
121
    }
122
    else setBackgroundPix(NULL);
123
124
125
  }
  setComputerplayer(One,config->readBoolEntry("Computerplayer1",true));
  setComputerplayer(Two,config->readBoolEntry("Computerplayer2",false));
Stephan Kulow's avatar
Stephan Kulow committed
126
127
128
129
130
131
132
133
134
135
}

Tron::~Tron()
{
  if(playfield)
    {
      delete []  playfield;
    }
  if(pixmap)
    delete pixmap;
136
137
  delete timer;

Stephan Kulow's avatar
Stephan Kulow committed
138
139
140
141
142
}

void Tron::createNewPlayfield()
{
  if(playfield)
143
    delete [] playfield;
Stephan Kulow's avatar
Stephan Kulow committed
144
145
146
147

  if(pixmap)
    delete pixmap;

148
  // field size
149
150
  fieldWidth=(width()-2*TRON_FRAMESIZE)/rectSize;
  fieldHeight=(height()-2*TRON_FRAMESIZE)/rectSize;
Stephan Kulow's avatar
Stephan Kulow committed
151

152
  // start positions
153
  playfield=new QMemArray<int>[fieldWidth];
Stephan Kulow's avatar
Stephan Kulow committed
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
  for(int i=0;i<fieldWidth;i++)
    playfield[i].resize(fieldHeight);

  pixmap=new QPixmap(size());
     pixmap->fill(colors[0]);

  //int min=(fieldWidth<fieldHeight) ? fieldWidth : fieldHeight;
  //lookForward=min/4;
}

void Tron::newGame()
{
  players[0].score=0;
  players[1].score=0;
  emit gameEnds(Nobody);
  reset();

  QTimer::singleShot(15000,this,SLOT(showBeginHint()));
}

void Tron::reset()
{
  gamePaused=false;
  stopGame();

  players[0].reset();
  players[1].reset();

182
183
  // If playfield exists, then clean it
  // ans set start coordinates
Stephan Kulow's avatar
Stephan Kulow committed
184
185
186
187
188
189
  if(playfield)
  {
      int i;
      for(i=0;i<fieldWidth;i++)
	   	playfield[i].fill(BACKGROUND);

190
      // set start coordinates
Stephan Kulow's avatar
Stephan Kulow committed
191
192
193
194

      players[0].setCoordinates(fieldWidth/3, fieldHeight/2);
      players[1].setCoordinates(2*fieldWidth/3, fieldHeight/2);

195
196
197
198
      playfield[players[0].xCoordinate][players[0].yCoordinate]=
         PLAYER1 | TOP | BOTTOM | LEFT | RIGHT;
      playfield[players[1].xCoordinate][players[1].yCoordinate]=
         PLAYER2 | TOP | BOTTOM | LEFT | RIGHT;
Stephan Kulow's avatar
Stephan Kulow committed
199
200
201
202
203
204
205
206
207
208

      updatePixmap();
      update();
    }

    setFocus();

    emit gameReset();
}

209
210
211
212
213
214
215
216
void Tron::computerStart()
{
   if(isComputer(Both))
   {
      reset();
      startGame();
   }
}
Stephan Kulow's avatar
Stephan Kulow committed
217
218
219
220
221
222
223
224

/* *************************************************************** **
**                   	??? functions										 **
** *************************************************************** */

void Tron::startGame()
{
   gameEnded=false;
225
226
   beginHint=false;
   timer->start(velocity);
Stephan Kulow's avatar
Stephan Kulow committed
227
228
229
230
}

void Tron::stopGame()
{
231
232
   timer->stop();
   gameEnded=true;
233
234
   players[0].last_dir = None;
   players[1].last_dir = None;
Stephan Kulow's avatar
Stephan Kulow committed
235
236
237
238
239
}

void Tron::togglePause() // pause or continue game
{
  if(!gameEnded)
240
241
242
243
244
245
246
247
248
249
250
251
252
  {
     if(gamePaused)
     {
        gamePaused=false;
        update();
        timer->start(velocity);
     }
     else
     {
        gamePaused=true;
        timer->stop();
        update();
     }
Stephan Kulow's avatar
Stephan Kulow committed
253
254
255
256
257
  }
}

void Tron::showWinner(Player player)
{
258
   int i,j;
Stephan Kulow's avatar
Stephan Kulow committed
259

260
261
262
263
264
265
266
267
   if(player != Both && changeWinnerColor)
   {
      int winner;
      int looser;
      if(player==One)
      {
         winner=PLAYER1;
         looser=PLAYER2;
Stephan Kulow's avatar
Stephan Kulow committed
268
      }
269
270
271
272
      else
      {
         winner=PLAYER2;
         looser=PLAYER1;
Stephan Kulow's avatar
Stephan Kulow committed
273
274
      }

275
276
      for(i=0;i<fieldWidth;i++)
         for(j=0;j<fieldHeight;j++)
277
         {
278
279
280
281
282
283
284
285
286
            if(playfield[i][j]!=BACKGROUND)
            {
               // change player
               playfield[i][j] |= winner;
               playfield[i][j] &= ~looser;
            }
         }

      updatePixmap();
Stephan Kulow's avatar
Stephan Kulow committed
287
288
   }

289
   repaint();
Stephan Kulow's avatar
Stephan Kulow committed
290

291
   emit gameEnds(player);
Stephan Kulow's avatar
Stephan Kulow committed
292

293
294
295
296
   if(isComputer(Both))
   {
       QTimer::singleShot(1000,this,SLOT(computerStart()));
   }
Stephan Kulow's avatar
Stephan Kulow committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
}

/* *************************************************************** **
**                    paint functions										 **
** *************************************************************** */

void Tron::updatePixmap()
{
  int i,j;

  if(!bgPix.isNull())
  {
     int pw=bgPix.width();
     int ph=bgPix.height();
     for (int x = 0; x <= width(); x+=pw)
312
313
        for (int y = 0; y <= height(); y+=ph)
	    bitBlt(pixmap, x, y, &bgPix);
Stephan Kulow's avatar
Stephan Kulow committed
314
315
316
317
318
319
320
321
322
323
324
  }
  else
  {
    pixmap->fill(colors[0]);
  }

  QPainter p;
  p.begin(pixmap);

  // alle Pixel prfen und evt. zeichnen
  for(i=0;i<fieldWidth;i++)
325
326
327
328
329
     for(j=0;j<fieldHeight;j++)
     {
        if(playfield[i][j]!=BACKGROUND)
        {
           drawRect(p,i,j);
Stephan Kulow's avatar
Stephan Kulow committed
330
	}
331
     }
332

333
334
335
336
337
338
   // draw frame
   QColor light=parentWidget()->colorGroup().midlight();
   QColor dark=parentWidget()->colorGroup().mid();

   p.setPen(NoPen);
   p.setBrush(light);
339
340
      p.drawRect(width()-TRON_FRAMESIZE,0,TRON_FRAMESIZE,height());
   p.drawRect(0,height()-TRON_FRAMESIZE,width(),TRON_FRAMESIZE);
341
   p.setBrush(dark);
342
343
   p.drawRect(0,0,width(),TRON_FRAMESIZE);
   p.drawRect(0,0,TRON_FRAMESIZE,height());
Stephan Kulow's avatar
Stephan Kulow committed
344

345
   p.end();
Stephan Kulow's avatar
Stephan Kulow committed
346
347
}

348
// draw new player rects
Stephan Kulow's avatar
Stephan Kulow committed
349
350
void Tron::paintPlayers()
{
351
352
353
354
355
   QPainter p;
   p.begin(this);
   drawRect(p,players[0].xCoordinate,players[0].yCoordinate);
   drawRect(p,players[1].xCoordinate,players[1].yCoordinate);
   p.end();
Stephan Kulow's avatar
Stephan Kulow committed
356

357
358
359
360
   p.begin(pixmap);
   drawRect(p,players[0].xCoordinate,players[0].yCoordinate);
   drawRect(p,players[1].xCoordinate,players[1].yCoordinate);
   p.end();
Stephan Kulow's avatar
Stephan Kulow committed
361
362
363
364
}

void Tron::drawRect(QPainter & p, int x, int y)
{
365
366
   int xOffset=x*rectSize+(width()-fieldWidth*rectSize)/2;
   int yOffset=y*rectSize+(height()-fieldHeight*rectSize)/2;
Stephan Kulow's avatar
Stephan Kulow committed
367

368
   int type=playfield[x][y];
Stephan Kulow's avatar
Stephan Kulow committed
369
370

   // find out which color to draw
371
372
373
374
375
376
377
378
379
380
381
382
   QColor toDraw;
   int player;
   if(type&PLAYER1) // check player bit
   {
      toDraw=colors[1];
      player=0;
   }
   else if(type&PLAYER2)
   {
      toDraw=colors[2];
      player=1;
   }
Stephan Kulow's avatar
Stephan Kulow committed
383
384
   else
   {
385
      kdDebug() << "No player defined in Tron::drawRect(...)" << endl;
Stephan Kulow's avatar
Stephan Kulow committed
386
387
388
      return;
   }

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
   switch(style)
   {
      case Line:
         p.setBrush(toDraw);
         p.setPen(toDraw);
         p.drawRect(xOffset,yOffset,rectSize,rectSize);
         break;
      case OLine:
      {
         p.setBrush(toDraw);
         p.setPen(toDraw);
         p.drawRect(xOffset,yOffset,rectSize,rectSize);
         p.setPen(toDraw.light());
         if(type&TOP)
         {
            p.drawLine(xOffset,yOffset,xOffset+rectSize-1,yOffset);
         }
         if(type&LEFT)
         {
            p.drawLine(xOffset,yOffset,xOffset,yOffset+rectSize-1);
         }
         p.setPen(toDraw.dark());
         if(type&RIGHT)
         {
            p.drawLine(xOffset+rectSize-1,yOffset,xOffset+rectSize-1,yOffset+rectSize-1);
         }
         if(type&BOTTOM)
         {
            p.drawLine(xOffset,yOffset+rectSize-1,xOffset+rectSize-1,yOffset+rectSize-1);
         }

         break;
      }
      case Circle:
         p.setBrush(toDraw);
         p.setPen(toDraw);
         p.drawEllipse(xOffset ,yOffset ,rectSize,rectSize);
         break;
      case ORect:
         p.setBrush(toDraw);
         p.setPen(toDraw.light());
         p.drawRect(xOffset,yOffset,rectSize,rectSize);
         p.setPen(toDraw.dark());
         p.drawLine(xOffset,yOffset+rectSize-1,xOffset+rectSize-1
             ,yOffset+rectSize-1);
         p.drawLine(xOffset+rectSize-1,yOffset,xOffset+rectSize-1,yOffset+rectSize-1);
         break;
Stephan Kulow's avatar
Stephan Kulow committed
436
437
438
439
440
441
442
    }
}

/* *************************************************************** **
**                    config functions										 **
** *************************************************************** */

Andreas Beckermann's avatar
Andreas Beckermann committed
443
void Tron::setActionCollection(KActionCollection *a)
Stephan Kulow's avatar
Stephan Kulow committed
444
{
Andreas Beckermann's avatar
Andreas Beckermann committed
445
   actionCollection = a;
Stephan Kulow's avatar
Stephan Kulow committed
446
447
448
449
450
451
}

void Tron::setBackgroundPix(QPixmap pix)
{
    bgPix=pix;

452
    if(pixmap!=0){
Stephan Kulow's avatar
Stephan Kulow committed
453
       updatePixmap();
454
455
456
      // most pictures have colors, that you can read white text
      setPalette(QColor("black"));
    }
Stephan Kulow's avatar
Stephan Kulow committed
457
458
459
}

// configure colors
460
// id = 0 = background, 1, and 2
Stephan Kulow's avatar
Stephan Kulow committed
461
462
bool Tron::changeColor(int id)
{
463
464
   QColor color;
   int result=KColorDialog::getColor(color);
Stephan Kulow's avatar
Stephan Kulow committed
465

466
   bool flag=false;
Stephan Kulow's avatar
Stephan Kulow committed
467

468
469
   if(result)  // if button 'ok' pressed
   {
Stephan Kulow's avatar
Stephan Kulow committed
470
      // backgroundcolor changed
471
472
473
474
475
476
477
478
479
480
481
482
483
484
      if(id==0)
      {
         colors[0]=color;
         setPalette(color);
         bgPix.resize(0,0);
      }
      else  // set other color
      {
         colors[id]=color;
      }
      updatePixmap();
      repaint();

      flag=true;
Stephan Kulow's avatar
Stephan Kulow committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
   }

   return flag;
}

void Tron::setVelocity(int newVel)            // set new velocity
{
  velocity=(10-newVel)*15;

  if(!gameEnded && !gamePaused)
    timer->changeInterval(velocity);
}

int Tron::getVelocity() const
{
  return 10-velocity/15;
}

503
void Tron::setComputerplayer(Player player, bool flag) {
Stephan Kulow's avatar
Stephan Kulow committed
504
  if(player==One)
505
    players[0].setComputer(flag);
Stephan Kulow's avatar
Stephan Kulow committed
506
  else if(player==Two)
507
    players[1].setComputer(flag);
Stephan Kulow's avatar
Stephan Kulow committed
508

509
  if(isComputer(Both))
510
      QTimer::singleShot(1000,this,SLOT(computerStart()));
Stephan Kulow's avatar
Stephan Kulow committed
511
512
513
514
515
}

bool Tron::isComputer(Player player)
{
   if(player==One)
516
     return players[0].computer;
Stephan Kulow's avatar
Stephan Kulow committed
517
   else if(player==Two)
518
     return players[1].computer;
Stephan Kulow's avatar
Stephan Kulow committed
519
520
521
   else if(player==Both)
   {
      if(players[0].computer && players[1].computer)
522
        return true;
Stephan Kulow's avatar
Stephan Kulow committed
523
524
   }

525
   return false;
Stephan Kulow's avatar
Stephan Kulow committed
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
}

/* *************************************************************** **
**                    moving functions										 **
** *************************************************************** */

bool Tron::crashed(int playerNr,int xInc, int yInc) const
{
  bool flag;
  int newX=players[playerNr].xCoordinate+xInc;
  int newY=players[playerNr].yCoordinate+yInc;

  if(newX<0 || newY <0 || newX>=fieldWidth || newY>=fieldHeight)
     flag=true;
  else if(playfield[newX][newY] != BACKGROUND)
    flag=true;
  else flag=false;

  return flag;
}

void Tron::switchDir(int playerNr,Direction newDirection)
{
  if(playerNr!=0 && playerNr != 1)
  {
551
     kdDebug() << "wrong playerNr" << endl;
Stephan Kulow's avatar
Stephan Kulow committed
552
553
554
     return;
  }

555
  if (crashOnOppositeDir==false)
556
  {
557
    if (newDirection==::Up && players[playerNr].last_dir==::Down)
558
      return;
559
    if (newDirection==::Down && players[playerNr].last_dir==::Up)
560
      return;
561
    if (newDirection==::Left && players[playerNr].last_dir==::Right)
562
      return;
563
    if (newDirection==::Right && players[playerNr].last_dir==::Left)
564
565
      return;
  }
Stephan Kulow's avatar
Stephan Kulow committed
566

567
  players[playerNr].dir=newDirection;
Stephan Kulow's avatar
Stephan Kulow committed
568
569
570
571
572
}

void Tron::updateDirections(int playerNr)
{
   if(playerNr==-1 || playerNr==0)
573
   {
574
575
      int x=players[0].xCoordinate;
      int y=players[0].yCoordinate;
Stephan Kulow's avatar
Stephan Kulow committed
576

577
578
579
580
      // necessary for drawing the 3d-line
      switch(players[0].dir)
      {
         // unset drawing flags in the moving direction
581
         case ::Up:
582
583
584
585
         {
  	    playfield[x][y] &= (~TOP);
  	    break;
       	 }
586
         case ::Down:
587
588
            playfield[x][y] &= (~BOTTOM);
            break;
589
         case ::Right:
590
591
            playfield[x][y] &= (~RIGHT);
            break;
592
         case ::Left:
593
594
595
            playfield[x][y] &= (~LEFT);
            break;
      }
596
      players[0].last_dir = players[0].dir;
597
598

   }
Stephan Kulow's avatar
Stephan Kulow committed
599
   if(playerNr==-1 || playerNr==1)
600
   {
601
602
      int x=players[1].xCoordinate;
      int y=players[1].yCoordinate;
603

604
605
606
607
      // necessary for drawing the 3d-line
      switch(players[1].dir)
      {
          // unset drawing flags in the moving direction
608
     	  case ::Up:
609
610
611
612
     	  {
             playfield[x][y] &= (~TOP);
             break;
       	  }
613
          case ::Down:
614
615
             playfield[x][y] &= (~BOTTOM);
             break;
616
          case ::Right:
617
618
             playfield[x][y] &= (~RIGHT);
             break;
619
          case ::Left:
620
621
622
             playfield[x][y] &= (~LEFT);
             break;
      }
623
      players[1].last_dir = players[1].dir;
624
625

   }
Stephan Kulow's avatar
Stephan Kulow committed
626

627
   paintPlayers();
Stephan Kulow's avatar
Stephan Kulow committed
628
629
630
631
632
633
634
635
}

/* *************************************************************** **
**                    			Events										 **
** *************************************************************** */

void Tron::paintEvent(QPaintEvent *e)
{
636
   bitBlt(this,e->rect().topLeft(),pixmap,e->rect());
Stephan Kulow's avatar
Stephan Kulow committed
637

638
639
640
641
642
643
644
645
646
   // if game is paused, print message
   if(gamePaused)
   {
      QString message=i18n("Game paused");
      QPainter p(this);
      QFontMetrics fm=p.fontMetrics();
      int w=fm.width(message);
      p.drawText(width()/2-w/2,height()/2,message);
   }
Stephan Kulow's avatar
Stephan Kulow committed
647

648
649
650
651
652
653
654
655
656
657
658
659
   // If game ended, print "Crash!"
   else if(gameEnded)
   {
      QString message=i18n("Crash!");
      QPainter p(this);
      int w=p.fontMetrics().width(message);
      for(int i=0;i<2;i++)
      {
         if(!players[i].alive)
     	 {
            int x=players[i].xCoordinate*rectSize+(width()%rectSize)/2-w/2;
            int y=players[i].yCoordinate*rectSize+(height()%rectSize)/2;
660
            if(players[i].dir==::Right)
661
662
663
            {
               x+=rectSize;
               y+=rectSize/2;
Stephan Kulow's avatar
Stephan Kulow committed
664
            }
665
            if(players[i].dir==::Left)
Stephan Kulow's avatar
Stephan Kulow committed
666
667
668
            {
               y+=rectSize/2;
            }
669
            if(players[i].dir==::Down)
670
671
672
673
            {
               y+=rectSize;
               x+=rectSize/2;
            }
674
            if(players[i].dir==::Up)
675
676
677
678
            {
               x+=rectSize/2;
               y+=rectSize/3;
            }
Stephan Kulow's avatar
Stephan Kulow committed
679

680

681
682
683
            p.drawText(x,y,message);
         }
      }
684

685
686
687
688
689
690
691
      // draw begin hint
      if(beginHint)
      {
         QString hint=i18n("Press any of your direction keys to start!");
         int x=p.fontMetrics().width(hint);
         x=(width()-x)/2;
         int y=height()/2;
692

693
694
695
         p.drawText(x,y,hint);
      }
   }
Stephan Kulow's avatar
Stephan Kulow committed
696
697
698
699
700
701
702
703
704
705
}

void Tron::resizeEvent(QResizeEvent *)
{
    createNewPlayfield();
    reset();
}

void Tron::keyPressEvent(QKeyEvent *e)
{
Andreas Beckermann's avatar
Andreas Beckermann committed
706
  KKey key(e);
Stephan Kulow's avatar
Stephan Kulow committed
707
708
  if(!players[1].computer)
  {
Andreas Beckermann's avatar
Andreas Beckermann committed
709
      if(actionCollection->action("Pl2Up")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
710
		{
711
	  		switchDir(1,::Up);
Stephan Kulow's avatar
Stephan Kulow committed
712
713
	  		players[1].keyPressed=true;
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
714
      else if(actionCollection->action("Pl2Left")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
715
	   {
716
	  		switchDir(1,::Left);
Stephan Kulow's avatar
Stephan Kulow committed
717
718
	  		players[1].keyPressed=true;
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
719
      else if(actionCollection->action("Pl2Right")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
720
		{
721
	  		switchDir(1,::Right);
Stephan Kulow's avatar
Stephan Kulow committed
722
723
	  		players[1].keyPressed=true;
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
724
      else if(actionCollection->action("Pl2Down")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
725
		{
726
	  		switchDir(1,::Down);
Stephan Kulow's avatar
Stephan Kulow committed
727
728
	  		players[1].keyPressed=true;
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
729
      else if(actionCollection->action("Pl2Ac")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
730
731
732
733
		{
		   if(!blockAccelerator)
	  			players[1].accelerated=true;

Andreas Beckermann's avatar
Andreas Beckermann committed
734
		}
Stephan Kulow's avatar
Stephan Kulow committed
735
736
737
738
  }

  if(!players[0].computer)
  {
Andreas Beckermann's avatar
Andreas Beckermann committed
739
      if(actionCollection->action("Pl1Left")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
740
		{
741
	  		switchDir(0,::Left);
Stephan Kulow's avatar
Stephan Kulow committed
742
743
	  		players[0].keyPressed=true;
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
744
      else if(actionCollection->action("Pl1Right")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
745
		{
746
	  		switchDir(0,::Right);
Stephan Kulow's avatar
Stephan Kulow committed
747
748
	  		players[0].keyPressed=true;
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
749
      else if(actionCollection->action("Pl1Up")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
750
		{
751
	  		switchDir(0,::Up);
Stephan Kulow's avatar
Stephan Kulow committed
752
753
	  		players[0].keyPressed=true;
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
754
      else if(actionCollection->action("Pl1Down")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
755
		{
756
	 		switchDir(0,::Down);
Stephan Kulow's avatar
Stephan Kulow committed
757
758
	  		players[0].keyPressed=true;
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
759
      else if(actionCollection->action("Pl1Ac")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
760
761
762
763
764
765
766
767
768
		{
		   if(!blockAccelerator)
	  			players[0].accelerated=true;
		}
  }

  e->ignore();  // if key is unknown: ignore

  // if both players press keys at the same time, start game...
769
770
771
772
773
774
  if(gameEnded && !gameBlocked)
  {
	if(players[0].keyPressed && players[1].keyPressed)
	{
		reset();
		startGame();
Stephan Kulow's avatar
Stephan Kulow committed
775
	}
776
777
778
779
780
781
782
  }
  // ...or continue
  else if(gamePaused)
  {
	if(players[0].keyPressed && players[1].keyPressed)
	{
	   togglePause();
Stephan Kulow's avatar
Stephan Kulow committed
783
	}
784
  }
Stephan Kulow's avatar
Stephan Kulow committed
785
786
787
788
}

void Tron::keyReleaseEvent(QKeyEvent * e)
{
Andreas Beckermann's avatar
Andreas Beckermann committed
789
  KKey key(e);
Stephan Kulow's avatar
Stephan Kulow committed
790
791
792

  if(!players[1].computer)
  {
Andreas Beckermann's avatar
Andreas Beckermann committed
793
794
795
796
      if(actionCollection->action("Pl2Ac")->shortcut().contains(key))
		{
			players[1].accelerated=false;
			return;
Stephan Kulow's avatar
Stephan Kulow committed
797
  		}
Andreas Beckermann's avatar
Andreas Beckermann committed
798
      else if(actionCollection->action("Pl2Left")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
799
		{
Andreas Beckermann's avatar
Andreas Beckermann committed
800
801
			players[1].keyPressed=false;
			return;
Stephan Kulow's avatar
Stephan Kulow committed
802
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
803
      else if(actionCollection->action("Pl2Right")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
804
805
806
807
		{
	  		players[1].keyPressed=false;
	  		return;
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
808
      else if(actionCollection->action("Pl2Up")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
809
810
811
812
		{
	  		players[1].keyPressed=false;
	  		return;
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
813
      else if(actionCollection->action("Pl2Down")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
814
815
816
817
818
819
		{
		  players[1].keyPressed=false;
		  return;
		}
  }

Andreas Beckermann's avatar
Andreas Beckermann committed
820
   if(!players[0].computer)
Stephan Kulow's avatar
Stephan Kulow committed
821
   {
Andreas Beckermann's avatar
Andreas Beckermann committed
822
823
      if(actionCollection->action("Pl1Left")->shortcut().contains(key))
		{
Stephan Kulow's avatar
Stephan Kulow committed
824
825
	 		players[0].keyPressed=false;
	 		return;
Andreas Beckermann's avatar
Andreas Beckermann committed
826
827
		}
      else if(actionCollection->action("Pl1Right")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
828
829
830
831
		{
	  		players[0].keyPressed=false;
	  		return;
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
832
      else if(actionCollection->action("Pl1Up")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
833
834
835
836
		{
	 		players[0].keyPressed=false;
	  		return;
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
837
      else if(actionCollection->action("Pl1Down")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
838
839
840
841
		{
	 		players[0].keyPressed=false;
	 		return;
		}
Andreas Beckermann's avatar
Andreas Beckermann committed
842
      else if(actionCollection->action("Pl1Ac")->shortcut().contains(key))
Stephan Kulow's avatar
Stephan Kulow committed
843
844
845
846
847
848
849
850
851
852
853
854
855
		{
	  		players[0].accelerated=false;
	  		return;
		}
 	}

   e->ignore();  // if pressed key is unknown, ignore it

}

// if playingfield looses keyboard focus, pause game
void Tron::focusOutEvent(QFocusEvent *)
{
856
   if(!gameEnded && !gamePaused)
Stephan Kulow's avatar
Stephan Kulow committed
857
   {
858
      togglePause();
Stephan Kulow's avatar
Stephan Kulow committed
859
860
861
862
   }
}

/* *************************************************************** **
863
**     				slots										 **
Stephan Kulow's avatar
Stephan Kulow committed
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
** *************************************************************** */

void Tron::unblockGame()
{
  gameBlocked=false;
}

void Tron::showBeginHint()
{
   if(gameEnded)
   {
      // show only at the beginning of a game
      if(players[0].score==0 && players[1].score==0)
      {
         beginHint=true;
         repaint();
      }
   }
}

// doMove() is called from QTimer
void Tron::doMove()
{
   int i;
  	for(i=0;i<2;i++)
  	{
  		// berprfen, ob Acceleratortaste gedrckt wurde...
      if(players[i].accelerated)
		{
893

Stephan Kulow's avatar
Stephan Kulow committed
894
895
896
897
898
899
900
901
902
903
904
905
906
		   updateDirections(i);

		   int newType; // determine type of rect to set
		   if(i==0)
		   {
		      newType=PLAYER1;
		   }
		   else
		   {
		      newType=PLAYER2;
		   }
	  		switch(players[i].dir)
	    	{
907
	    		case ::Up:
Stephan Kulow's avatar
Stephan Kulow committed
908
909
910
911
912
913
914
915
	      		if(crashed(i,0,-1))
		 				players[i].alive=false;
	      		else
	      		{
	      			players[i].yCoordinate--;
	      			newType|=(TOP | LEFT | RIGHT);
	      		}
	      		break;
916
	    		case ::Down:
Stephan Kulow's avatar
Stephan Kulow committed
917
918
919
920
921
922
923
	    			if(crashed(i,0,1))
	       			players[i].alive=false;
	    			else
	    			{
	    			   players[i].yCoordinate++;
	    			   newType |= (BOTTOM | LEFT | RIGHT);
	    			}
924

Stephan Kulow's avatar
Stephan Kulow committed
925
	      		break;
926
	    		case ::Left:
Stephan Kulow's avatar
Stephan Kulow committed
927
928
929
930
931
932
933
934
	       		if(crashed(i,-1,0))
		 				players[i].alive=false;
	       		else
	       		{
	       		   players[i].xCoordinate--;
	       		   newType |= (LEFT | TOP | BOTTOM);
	       		}
	      		break;
935
	    		case ::Right:
Stephan Kulow's avatar
Stephan Kulow committed
936
937
938
939
940
941
942
943
944
	      	if(crashed(i,1,0))
					players[i].alive=false;
	      	else
	      	{
	      	   players[i].xCoordinate++;
	      	   newType |= (RIGHT | TOP | BOTTOM);
	      	}
	      	break;
	    	}
945

Stephan Kulow's avatar
Stephan Kulow committed
946
947
948
949
950
951
952
953
954
955
956
957
958
959
	      if(players[i].alive)
      		playfield[players[i].xCoordinate][players[i].yCoordinate]=newType;
		}

  	}

	if(players[0].accelerated || players[1].accelerated)
 	{
       /* player collision check */
      if(!players[1].alive)
		{
	  		int xInc=0,yInc=0;
	  		switch(players[1].dir)
	    	{
960
	    		case ::Left:
Stephan Kulow's avatar
Stephan Kulow committed
961
962
	      		xInc = -1;
	      		break;
963
	    		case ::Right:
Stephan Kulow's avatar
Stephan Kulow committed
964
965
	      		xInc = 1;
	      		break;
966
	    		case ::Up:
Stephan Kulow's avatar
Stephan Kulow committed
967
968
	      		yInc = -1;
	      		break;
969
	    		case ::Down:
Stephan Kulow's avatar
Stephan Kulow committed
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
	      		yInc = 1;
	      		break;
	    	}
	  		if ((players[1].xCoordinate+xInc) == players[0].xCoordinate)
	    		if ((players[1].yCoordinate+yInc) == players[0].yCoordinate)
	      	{
					players[0].alive=false;
	      	}
		}

      paintPlayers();

      // crashtest
      if(!players[0].alive && !players[1].alive)
		{
		   stopGame();
	  		players[0].score++;
	  		players[1].score++;
	  		showWinner(Both);
		}
      else
      {
			for(i=0;i<2;i++)
	  		{
	    		if(!players[i].alive)
	      	{
	      	   stopGame();
					showWinner((i==0)? Two:One);
					players[i].score++;
	      	}
	  		}
	  	}


      if(gameEnded)
		{
	  		//this is for waiting 0,5s before starting next game
	  		gameBlocked=true;
	  		QTimer::singleShot(1000,this,SLOT(unblockGame()));
		}
  	}

  	// neue Spielerstandorte festlegen
  	for(i=0;i<2;i++)
   {
      if(players[i].computer)
			think(i);
   }

   updateDirections();

   for(i=0;i<2;i++)
   {
		int newType;
		if(i==0)
			newType=PLAYER1;
		else
			newType=PLAYER2;
1028

Stephan Kulow's avatar
Stephan Kulow committed
1029
1030
      switch(players[i].dir)
		{
1031
			case ::Up:
Stephan Kulow's avatar
Stephan Kulow committed
1032
1033
1034
1035
1036
1037
1038
1039
	      	if(crashed(i,0,-1))
					players[i].alive=false;
	      	else
	      	{
	      	   players[i].yCoordinate--;
	      	   newType |= (TOP | RIGHT | LEFT);
	      	}
	  			break;
1040
			case ::Down:
Stephan Kulow's avatar
Stephan Kulow committed
1041
1042
1043
1044
1045
1046
1047
1048
	      	if(crashed(i,0,1))
					players[i].alive=false;
	      	else
	      	{
	      	    players[i].yCoordinate++;
	      	    newType |= (BOTTOM | RIGHT | LEFT);
	      	}
	  			break;
1049
			case ::Left:
Stephan Kulow's avatar
Stephan Kulow committed
1050
1051
1052
1053
1054
1055
1056
1057
	      	if(crashed(i,-1,0))
					players[i].alive=false;
	      	else
	      	{
	      	   players[i].xCoordinate--;
	      	   newType |= (LEFT | TOP | BOTTOM);
	      	}
	  			break;
1058
			case ::Right:
Stephan Kulow's avatar
Stephan Kulow committed
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
	      	if(crashed(i,1,0))
					players[i].alive=false;
	      	else
	      	{
	      	   players[i].xCoordinate++;
	      	   newType |= (RIGHT | TOP | BOTTOM);
	      	}
	  			break;
		}
		if(players[i].alive)
			playfield[players[i].xCoordinate][players[i].yCoordinate]=newType;

 	}

  	/* player collision check */
  	if(!players[1].alive)
   {
      int xInc=0,yInc=0;
      switch(players[1].dir)
		{
1079
			case ::Left:
Stephan Kulow's avatar
Stephan Kulow committed
1080
	  			xInc = -1; break;
1081
			case ::Right:
Stephan Kulow's avatar
Stephan Kulow committed
1082
	  			xInc = 1; break;
1083
			case ::Up:
Stephan Kulow's avatar
Stephan Kulow committed
1084
	  			yInc = -1; break;
1085
			case ::Down:
Stephan Kulow's avatar
Stephan Kulow committed
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
	  			yInc = 1; break;
		}
      if ((players[1].xCoordinate+xInc) == players[0].xCoordinate)
			if ((players[1].yCoordinate+yInc) == players[0].yCoordinate)
	  		{
	    		players[0].alive=false;
	  		}
   }

  paintPlayers();

  if(!players[0].alive && !players[1].alive)
  {
      stopGame();
      players[0].score++;
      players[1].score++;
      showWinner(Both);
  }
	else
		for(i=0;i<2;i++)
    	{
			// crashtests
			if(!players[i].alive)
	  		{
	  		   stopGame();
	    		showWinner((i==0)? Two:One);
	    		players[i].score++;
	  		}
      }


  	if(gameEnded)
  	{
   	//this is for waiting 1s before starting next game
      gameBlocked=true;
      QTimer::singleShot(1000,this,SLOT(unblockGame()));
  	}

}

/* *************************************************************** **
**                 algoritm for the computerplayer                 **
** *************************************************************** */

// This part is partly ported from
// xtron-1.1 by Rhett D. Jacobs <rhett@hotel.canberra.edu.au>
void Tron::think(int playerNr)
{
if(_skill != Easy)
{
  int opponent=(playerNr==1)? 0 : 1;

  // determines left and right side
  Direction sides[2];
  // increments for moving to the different sides
  int flags[6]={0,0,0,0,0,0};
  int index[2];
  // distances to barrier
  int dis_forward,  dis_left, dis_right;

  dis_forward = dis_left = dis_right = 1;
1147
1148


Stephan Kulow's avatar
Stephan Kulow committed
1149
1150
  	switch (players[playerNr].dir)
   	{
1151
  			case ::Left:
Stephan Kulow's avatar
Stephan Kulow committed
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
    			//forward flags
    			flags[0] = -1;
    			flags[1] = 0;

    			//left flags
    			flags[2] = 0;
    			flags[3] = 1;

    			// right flags
    			flags[4] = 0;
    			flags[5] = -1;

    			//turns to either side
1165
1166
    			sides[0] = ::Down;
    			sides[1] = ::Up;
Stephan Kulow's avatar
Stephan Kulow committed
1167
    			break;
1168
  			case ::Right:
Stephan Kulow's avatar
Stephan Kulow committed
1169
1170
1171
1172
1173
1174
    			flags[0] = 1;
    			flags[1] = 0;
    			flags[2] = 0;
			   flags[3] = -1;
    			flags[4] = 0;
    			flags[5] = 1;
1175
1176
    			sides[0] = ::Up;
    			sides[1] = ::Down;
Stephan Kulow's avatar
Stephan Kulow committed
1177
    			break;
1178
  			case ::Up:
Stephan Kulow's avatar
Stephan Kulow committed
1179
1180
1181
1182
1183
1184
    			flags[0] = 0;
    			flags[1] = -1;
    			flags[2] = -1;
    			flags[3] = 0;
    			flags[4] = 1;
    			flags[5] = 0;
1185
1186
    			sides[0] = ::Left;
    			sides[1] = ::Right;
Stephan Kulow's avatar
Stephan Kulow committed
1187
    			break;
1188
  			case ::Down:
Stephan Kulow's avatar
Stephan Kulow committed
1189
1190
1191
1192
1193
1194
    			flags[0] = 0;
    			flags[1] = 1;
    			flags[2] = 1;
    			flags[3] = 0;
    			flags[4] = -1;
    			flags[5] = 0;
1195
1196
    			sides[0] = ::Right;
    			sides[1] = ::Left;
Stephan Kulow's avatar
Stephan Kulow committed
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
    			break;
  		}

  		// check forward
  		index[0] = players[playerNr].xCoordinate+flags[0];
  		index[1] = players[playerNr].yCoordinate+flags[1];
  		while (index[0] < fieldWidth && index[0] >= 0 &&
	 		index[1] < fieldHeight && index[1] >= 0 &&
	 		playfield[index[0]][index[1]] == BACKGROUND)
	 	{
    		dis_forward++;
    		index[0] += flags[0];
    		index[1] += flags[1];
  		}
1211

Stephan Kulow's avatar
Stephan Kulow committed
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
    	// check left
    	index[0] = players[playerNr].xCoordinate+flags[2];
    	index[1] = players[playerNr].yCoordinate+flags[3];
    while (index[0] < fieldWidth && index[0] >= 0 &&
	   index[1] < fieldHeight && index[1] >= 0 &&
	   playfield[index[0]][index[1]] == BACKGROUND) {
      dis_left++;
      index[0] += flags[2];
      index[1] += flags[3];
    }

    // check right
    index[0] = players[playerNr].xCoordinate+flags[4];
    index[1] = players[playerNr].yCoordinate+flags[5];
    while (index[0] < fieldWidth && index[0] >= 0 &&
	   index[1] <  fieldHeight && index[1] >= 0 &&
	   playfield[index[0]][index[1]] == BACKGROUND) {
      dis_right++;
      index[0] += flags[4];
      index[1] += flags[5];
    }
1233

Stephan Kulow's avatar
Stephan Kulow committed
1234
1235
1236
1237
1238
1239
  	// distances to opponent
  	int hor_dis=0; // negative is opponent to the right
  	int vert_dis=0; // negative is opponent to the bottom
  	hor_dis=players[playerNr].xCoordinate-players[opponent].xCoordinate;
  	vert_dis=players[playerNr].yCoordinate-players[opponent].yCoordinate;

1240
1241
  	int opForwardDis=0; // negative is to the back
  	int opSideDis=0;  // negative is to the left
Stephan Kulow's avatar
Stephan Kulow committed
1242
1243
1244
1245
  	bool opMovesOppositeDir=false;
  	bool opMovesSameDir=false;
  	bool opMovesRight=false;
  	bool opMovesLeft=false;
1246

Stephan Kulow's avatar
Stephan Kulow committed
1247
1248
  	switch(players[playerNr].dir)
  	{
1249
  	   case ::Up:
Stephan Kulow's avatar
Stephan Kulow committed
1250
1251
  	      opForwardDis=vert_dis;
  	      opSideDis=-hor_dis;
1252
  	      if(players[opponent].dir==::Down)
Stephan Kulow's avatar
Stephan Kulow committed
1253
  	         opMovesOppositeDir=true;
1254
  	      else if(players[opponent].dir==::Up)
Stephan Kulow's avatar
Stephan Kulow committed
1255
  	         opMovesSameDir=true;
1256
  	      else if(players[opponent].dir==::Left)
Stephan Kulow's avatar
Stephan Kulow committed
1257
  	         opMovesLeft=true;
1258
  	      else if(players[opponent].dir==::Right)
Stephan Kulow's avatar
Stephan Kulow committed
1259
1260
  	         opMovesRight=true;
  	      break;
1261
  	   case ::Down:
Stephan Kulow's avatar
Stephan Kulow committed
1262
1263
  	      opForwardDis=-vert_dis;
  	      opSideDis=hor_dis;
1264
  	      if(players[opponent].dir==::Up)
Stephan Kulow's avatar
Stephan Kulow committed
1265
  	         opMovesOppositeDir=true;
1266
  	      else if(players[opponent].dir==::Down)
Stephan Kulow's avatar
Stephan Kulow committed
1267
  	         opMovesSameDir=true;
1268
  	      else if(players[opponent].dir==::Left)
Stephan Kulow's avatar
Stephan Kulow committed
1269
  	         opMovesRight=true;
1270
  	      else if(players[opponent].dir==::Right)
Stephan Kulow's avatar
Stephan Kulow committed
1271
1272
  	         opMovesLeft=true;
  	      break;
1273
  	   case ::Left:
Stephan Kulow's avatar
Stephan Kulow committed
1274
1275
  	      opForwardDis=hor_dis;
  	      opSideDis=vert_dis;
1276
  	      if(players[opponent].dir==::Right)
Stephan Kulow's avatar
Stephan Kulow committed
1277
  	         opMovesOppositeDir=true;
1278
  	      else if(players[opponent].dir==::Left)
Stephan Kulow's avatar
Stephan Kulow committed
1279
  	         opMovesSameDir=true;
1280
  	      else if(players[opponent].dir==::Down)
Stephan Kulow's avatar
Stephan Kulow committed
1281
  	         opMovesLeft=true;
1282
  	      else if(players[opponent].dir==::Up)
Stephan Kulow's avatar
Stephan Kulow committed
1283
1284
  	         opMovesRight=true;
  	      break;
1285
  	   case ::Right:
Stephan Kulow's avatar
Stephan Kulow committed
1286
1287
  	      opForwardDis=-hor_dis;
  	      opSideDis=-vert_dis;
1288
  	      if(players[opponent].dir==::Left)
Stephan Kulow's avatar
Stephan Kulow committed
1289
  	         opMovesOppositeDir=true;
1290
  	      else if(players[opponent].dir==::Right)
Stephan Kulow's avatar
Stephan Kulow committed
1291
  	         opMovesSameDir=true;
1292
  	      else if(players[opponent].dir==::Up)
Stephan Kulow's avatar
Stephan Kulow committed
1293
  	         opMovesLeft=true;
1294
  	      else if(players[opponent].dir==::Down)
Stephan Kulow's avatar
Stephan Kulow committed
1295
1296
1297
  	         opMovesRight=true;
  	      break;
  	}
1298

1299
  	int doPercentage=100;
Stephan Kulow's avatar
Stephan Kulow committed
1300
1301
1302
1303
  	if(_skill==Medium)
  		doPercentage=5;
  	else if(_skill==Hard)
  		doPercentage=90;
1304

Stephan Kulow's avatar
Stephan Kulow committed
1305
1306
1307
1308
1309
1310
1311
1312
1313
  	// if opponent moves the opposite direction as we
   if(opMovesOppositeDir)
   {
      // if opponent is in front
      if(opForwardDis>0)
      {
         // opponent is to the right and we have the chance to block the way
         if(opSideDis>0 && opSideDis < opForwardDis && opSideDis < dis_right && opForwardDis < lookForward)
         {
Waldo Bastian's avatar
Waldo Bastian committed
1314
            if ((int)random.getLong(100) <= doPercentage || dis_forward==1)
Stephan Kulow's avatar
Stephan Kulow committed
1315
1316
1317
1318
1319
               switchDir(playerNr,sides[1]); // turn right
         }
         // opponent is to the left and we have the chance to block the way
         else if(opSideDis<0 && -opSideDis < opForwardDis && -opSideDis < dis_left && opForwardDis < lookForward)
         {
Waldo Bastian's avatar
Waldo Bastian committed
1320
            if ((int)random.getLong(100) <= doPercentage || dis_forward==1)
Stephan Kulow's avatar
Stephan Kulow committed
1321
1322
1323
1324
1325
1326
               switchDir(playerNr,sides[0]); // turn left
         }
         // if we can do nothing, go forward
         else if(dis_forward < lookForward)
         {
      		dis_forward = 100 - 100/dis_forward;
1327

Stephan Kulow's avatar
Stephan Kulow committed
1328
    			if(!(dis_left == 1 && dis_right == 1))
Waldo Bastian's avatar
Waldo Bastian committed
1329
      			if ((int)random.getLong(100) >= dis_forward || dis_forward == 1)
Stephan Kulow's avatar
Stephan Kulow committed
1330
1331
1332
1333
1334
1335
1336
1337
1338
         			changeDirection(playerNr,dis_right,dis_left);
         }
      }
      // opponent is in back of us and moves away: do nothing
      else if(dis_forward < lookForward)
      {
      	dis_forward = 100 - 100/dis_forward;

    		if(!(dis_left == 1 && dis_right == 1))
Waldo Bastian's avatar
Waldo Bastian committed
1339
      		if ((int)random.getLong(100) >= dis_forward || dis_forward == 1)
1340
        			changeDirection(playerNr,dis_right,dis_left);
Stephan Kulow's avatar
Stephan Kulow committed
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
      }
   } // end  if(opMovesOppositeDir)

   else if(opMovesSameDir)
   {
      // if opponent is to the back
      if(opForwardDis < 0)
      {
        	// opponent is to the right and we have the chance to block the way
         if(opSideDis>0 && opSideDis < -opForwardDis && opSideDis < dis_right)
         {