intelligence.cpp 19.5 KB
Newer Older
1
/**********************************************************************************
2 3 4 5
  This file is part of the game 'KTron'

  Copyright (C) 1998-2000 by Matthias Kiefer <matthias.kiefer@gmx.de>
  Copyright (C) 2005 Benjamin C. Meyer <ben at meyerhome dot net>
6
  Copyright (C) 2008-2009 Stas Verberkt <legolas at legolasweb dot nl>
7 8 9 10 11

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

13 14 15 16
  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.
17

18 19 20 21
  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.

22
  *******************************************************************************/
23 24 25 26 27 28
  
#include "intelligence.h"

#include "tron.h"
#include "settings.h"

29
#include <KgDifficulty>
30 31

Intelligence::Intelligence()
32
    : m_random(QRandomGenerator::global()->generate())
33
{
Stas Verberkt's avatar
Stas Verberkt committed
34
	m_lookForward = 15;
35 36 37 38
}

void Intelligence::referenceTron(Tron *t)
{
Stas Verberkt's avatar
Stas Verberkt committed
39
	m_tron = t;
40 41 42 43 44 45 46 47
}

//
// Settings
//

/** retrieves the opponentSkill */
int Intelligence::opponentSkill() {
48 49
	switch (Kg::difficultyLevel()) {
		case KgDifficultyLevel::VeryEasy:
50 51
			return 1;
		default:
52
		case KgDifficultyLevel::Easy:
53
			return 1;
54
		case KgDifficultyLevel::Medium:
55
			return 2;
56
		case KgDifficultyLevel::Hard:
57
			return 3;
58
		case KgDifficultyLevel::VeryHard:
59 60 61 62 63 64 65 66 67 68
			return 3;
	}
}

//
// Algorithm helper function
//

void Intelligence::changeDirection(int playerNr,int dis_right,int dis_left)
{
Stas Verberkt's avatar
Stas Verberkt committed
69
   PlayerDirections::Direction currentDir = m_tron->getPlayer(playerNr)->getDirection();
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
   PlayerDirections::Direction sides[2];
   sides[0] = PlayerDirections::None;
   sides[1] = PlayerDirections::None;
   
   switch (currentDir)
   {
  		case PlayerDirections::Left:
    		//turns to either side
    		sides[0] = PlayerDirections::Down;
    		sides[1] = PlayerDirections::Up;
    		break;
  		case PlayerDirections::Right:
    		sides[0] = PlayerDirections::Up;
    		sides[1] = PlayerDirections::Down;
    		break;
  		case PlayerDirections::Up:
    		sides[0] = PlayerDirections::Left;
    		sides[1] = PlayerDirections::Right;
    		break;
  		case PlayerDirections::Down:
    		sides[0] = PlayerDirections::Right;
    		sides[1] = PlayerDirections::Left;
    		break;
		default:
		break;

  	}

   if(!(dis_left == 1 && dis_right == 1))
   {
			// change direction
101
			if (m_random.bounded(100) <= (100*dis_left)/(dis_left+dis_right))
102 103 104
			{
	  			if (dis_left != 1)
		    		// turn to the left
Stas Verberkt's avatar
Stas Verberkt committed
105
		    		m_tron->getPlayer(playerNr)->setDirection(sides[0]);
106 107
	  			else
	   	 		// turn to the right
Stas Verberkt's avatar
Stas Verberkt committed
108
	    			m_tron->getPlayer(playerNr)->setDirection(sides[1]);
109 110 111 112 113
	    	}
			else
			{
	  				if (dis_right != 1)
	  					// turn to the right
Stas Verberkt's avatar
Stas Verberkt committed
114
	    				m_tron->getPlayer(playerNr)->setDirection(sides[1]);
115 116
	  				else
	    				// turn to the left
Stas Verberkt's avatar
Stas Verberkt committed
117
	    				m_tron->getPlayer(playerNr)->setDirection(sides[0]);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
          }
    }
}

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

		// determines left and right side
		PlayerDirections::Direction sides[2];
		sides[0] = PlayerDirections::None;
		sides[1] = PlayerDirections::None;
		// 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;

Stas Verberkt's avatar
Stas Verberkt committed
142
		switch (m_tron->getPlayer(playerNr)->getDirection())
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
		{
			case PlayerDirections::Left:
				//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
				sides[0] = PlayerDirections::Down;
				sides[1] = PlayerDirections::Up;
				break;
			case PlayerDirections::Right:
				flags[0] = 1;
				flags[1] = 0;
				flags[2] = 0;
				flags[3] = -1;
				flags[4] = 0;
				flags[5] = 1;
				sides[0] = PlayerDirections::Up;
				sides[1] = PlayerDirections::Down;
				break;
			case PlayerDirections::Up:
				flags[0] = 0;
				flags[1] = -1;
				flags[2] = -1;
				flags[3] = 0;
				flags[4] = 1;
				flags[5] = 0;
				sides[0] = PlayerDirections::Left;
				sides[1] = PlayerDirections::Right;
				break;
			case PlayerDirections::Down:
				flags[0] = 0;
				flags[1] = 1;
				flags[2] = 1;
				flags[3] = 0;
				flags[4] = -1;
				flags[5] = 0;
				sides[0] = PlayerDirections::Right;
				sides[1] = PlayerDirections::Left;
				break;
			default:
				break;
		}

		// check forward
Stas Verberkt's avatar
Stas Verberkt committed
196 197 198
		index[0] = m_tron->getPlayer(playerNr)->getX()+flags[0];
		index[1] = m_tron->getPlayer(playerNr)->getY()+flags[1];
		while (index[0] < m_tron->getPlayField()->getWidth() && index[0] >= 0 && index[1] < m_tron->getPlayField()->getHeight() && index[1] >= 0 && m_tron->getPlayField()->getObjectAt(index[0], index[1])->getObjectType() == ObjectType::Object)
199 200 201 202 203 204 205
		{
			dis_forward++;
			index[0] += flags[0];
			index[1] += flags[1];
		}

		// check left
Stas Verberkt's avatar
Stas Verberkt committed
206 207 208
		index[0] = m_tron->getPlayer(playerNr)->getX()+flags[2];
		index[1] = m_tron->getPlayer(playerNr)->getY()+flags[3];
		while (index[0] < m_tron->getPlayField()->getWidth() && index[0] >= 0 && index[1] < m_tron->getPlayField()->getHeight() && index[1] >= 0 && m_tron->getPlayField()->getObjectAt(index[0], index[1])->getObjectType() == ObjectType::Object)
209 210 211 212 213 214 215
		{
			dis_left++;
			index[0] += flags[2];
			index[1] += flags[3];
		}

		// check right
Stas Verberkt's avatar
Stas Verberkt committed
216 217 218
		index[0] = m_tron->getPlayer(playerNr)->getX()+flags[4];
		index[1] = m_tron->getPlayer(playerNr)->getY()+flags[5];
		while (index[0] < m_tron->getPlayField()->getWidth() && index[0] >= 0 && index[1] <  m_tron->getPlayField()->getHeight() && index[1] >= 0 && m_tron->getPlayField()->getObjectAt(index[0], index[1])->getObjectType() == ObjectType::Object)
219 220 221 222 223 224 225 226 227
		{
			dis_right++;
			index[0] += flags[4];
			index[1] += flags[5];
		}

		// distances to opponent
		int hor_dis=0; // negative is opponent to the right
		int vert_dis=0; // negative is opponent to the bottom
Stas Verberkt's avatar
Stas Verberkt committed
228 229
		hor_dis = m_tron->getPlayer(playerNr)->getX() - m_tron->getPlayer(opponent)->getX();
		vert_dis = m_tron->getPlayer(playerNr)->getY() - m_tron->getPlayer(opponent)->getY();
230 231 232 233 234 235 236 237

		int opForwardDis=0; // negative is to the back
		int opSideDis=0;  // negative is to the left
		bool opMovesOppositeDir=false;
		bool opMovesSameDir=false;
		bool opMovesRight=false;
		bool opMovesLeft=false;

Stas Verberkt's avatar
Stas Verberkt committed
238
		switch (m_tron->getPlayer(playerNr)->getDirection())
239 240 241 242
		{
			case PlayerDirections::Up:
				opForwardDis=vert_dis;
				opSideDis=-hor_dis;
Stas Verberkt's avatar
Stas Verberkt committed
243
				if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Down)
244
					opMovesOppositeDir=true;
Stas Verberkt's avatar
Stas Verberkt committed
245
				else if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Up)
246
					opMovesSameDir=true;
Stas Verberkt's avatar
Stas Verberkt committed
247
				else if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Left)
248
					opMovesLeft=true;
Stas Verberkt's avatar
Stas Verberkt committed
249
				else if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Right)
250 251 252 253 254
					opMovesRight=true;
				break;
			case PlayerDirections::Down:
				opForwardDis=-vert_dis;
				opSideDis=hor_dis;
Stas Verberkt's avatar
Stas Verberkt committed
255
				if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Up)
256
					opMovesOppositeDir=true;
Stas Verberkt's avatar
Stas Verberkt committed
257
				else if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Down)
258
					opMovesSameDir=true;
Stas Verberkt's avatar
Stas Verberkt committed
259
				else if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Left)
260
					opMovesRight=true;
Stas Verberkt's avatar
Stas Verberkt committed
261
				else if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Right)
262 263 264 265 266
					opMovesLeft=true;
				break;
			case PlayerDirections::Left:
				opForwardDis=hor_dis;
				opSideDis=vert_dis;
Stas Verberkt's avatar
Stas Verberkt committed
267
				if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Right)
268
					opMovesOppositeDir=true;
Stas Verberkt's avatar
Stas Verberkt committed
269
				else if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Left)
270
					opMovesSameDir=true;
Stas Verberkt's avatar
Stas Verberkt committed
271
				else if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Down)
272
					opMovesLeft=true;
Stas Verberkt's avatar
Stas Verberkt committed
273
				else if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Up)
274 275 276 277 278
					opMovesRight=true;
				break;
			case PlayerDirections::Right:
				opForwardDis=-hor_dis;
				opSideDis=-vert_dis;
Stas Verberkt's avatar
Stas Verberkt committed
279
				if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Left)
280
					opMovesOppositeDir=true;
Stas Verberkt's avatar
Stas Verberkt committed
281
				else if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Right)
282
					opMovesSameDir=true;
Stas Verberkt's avatar
Stas Verberkt committed
283
				else if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Up)
284
					opMovesLeft=true;
Stas Verberkt's avatar
Stas Verberkt committed
285
				else if(m_tron->getPlayer(opponent)->getDirection()==PlayerDirections::Down)
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
					opMovesRight=true;
				break;
			default:
				break;
		}

		int doPercentage = 100;
		switch(opponentSkill())
		{
			case 1:
				// Never reached
				break;
			case 2:
				doPercentage=5;
				break;
			case 3:
				doPercentage=90;
				break;
		}

		// 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
Stas Verberkt's avatar
Stas Verberkt committed
313
				if(opSideDis>0 && opSideDis < opForwardDis && opSideDis < dis_right && opForwardDis < m_lookForward)
314
				{
315
					if (m_random.bounded(100) <= doPercentage || dis_forward==1)
Stas Verberkt's avatar
Stas Verberkt committed
316
						m_tron->getPlayer(playerNr)->setDirection(sides[1]); // turn right
317 318
				}
				// opponent is to the left and we have the chance to block the way
Stas Verberkt's avatar
Stas Verberkt committed
319
				else if(opSideDis<0 && -opSideDis < opForwardDis && -opSideDis < dis_left && opForwardDis < m_lookForward)
320
				{
321
					if (m_random.bounded(100) <= doPercentage || dis_forward==1)
Stas Verberkt's avatar
Stas Verberkt committed
322
						m_tron->getPlayer(playerNr)->setDirection(sides[0]); // turn left
323 324
				}
				// if we can do nothing, go forward
Stas Verberkt's avatar
Stas Verberkt committed
325
				else if(dis_forward < m_lookForward)
326 327 328 329
				{
					dis_forward = 100 - 100/dis_forward;

					if(!(dis_left == 1 && dis_right == 1))
330
						if (m_random.bounded(100) >= dis_forward || dis_forward == 1)
331 332 333 334
							changeDirection(playerNr,dis_right,dis_left);
				}
			}
			// opponent is in back of us and moves away: do nothing
Stas Verberkt's avatar
Stas Verberkt committed
335
			else if(dis_forward < m_lookForward)
336 337 338 339
			{
				dis_forward = 100 - 100/dis_forward;

				if(!(dis_left == 1 && dis_right == 1))
340
					if (m_random.bounded(100) >= dis_forward || dis_forward == 1)
341 342 343 344 345 346 347 348 349 350 351
							changeDirection(playerNr,dis_right,dis_left);
			}
		} // 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)
				{
352
					if (m_random.bounded(100) <= doPercentage || dis_forward==1)
Stas Verberkt's avatar
Stas Verberkt committed
353
						m_tron->getPlayer(playerNr)->setDirection(sides[1]); // turn right
354 355 356 357
				}
				// opponent is to the left and we have the chance to block the way
				else if(opSideDis<0 && -opSideDis < -opForwardDis && -opSideDis < dis_left)
				{
358
					if (m_random.bounded(100) <= doPercentage || dis_forward==1)
Stas Verberkt's avatar
Stas Verberkt committed
359
						m_tron->getPlayer(playerNr)->setDirection(sides[0]); // turn left
360 361
				}
				// if we can do nothing, go forward
Stas Verberkt's avatar
Stas Verberkt committed
362
				else if(dis_forward < m_lookForward)
363 364 365 366
				{
					dis_forward = 100 - 100/dis_forward;

						if(!(dis_left == 1 && dis_right == 1))
367
							if (m_random.bounded(100) >= dis_forward || dis_forward == 1)
368 369 370 371
								changeDirection(playerNr,dis_right,dis_left);
				}
			}
			// opponent is in front of us and moves away
Stas Verberkt's avatar
Stas Verberkt committed
372
			else if(dis_forward < m_lookForward)
373 374 375 376
			{
				dis_forward = 100 - 100/dis_forward;

					if(!(dis_left == 1 && dis_right == 1))
377
						if (m_random.bounded(100) >= dis_forward || dis_forward == 1)
378 379 380 381 382 383 384 385 386 387 388
							changeDirection(playerNr,dis_right,dis_left);
			}
		} // end if(opMovesSameDir)
		else if(opMovesRight)
		{
			// opponent is in front of us
			if(opForwardDis>0)
			{
				// opponent is to the left
				if(opSideDis < 0 && -opSideDis < opForwardDis && -opSideDis < dis_left)
				{
Stas Verberkt's avatar
Stas Verberkt committed
389
					if(opForwardDis < m_lookForward && dis_left > m_lookForward)
390
					{
391
						if (m_random.bounded(100) <= doPercentage/2 || dis_forward==1)
392 393
							changeDirection(playerNr,dis_right,dis_left);
					}
Stas Verberkt's avatar
Stas Verberkt committed
394
					else if(dis_forward < m_lookForward)
395 396 397 398
					{
						dis_forward = 100 - 100/dis_forward;

							if(!(dis_left == 1 && dis_right == 1))
399
								if (m_random.bounded(100) >= dis_forward || dis_forward == 1)
400 401 402 403 404 405
									changeDirection(playerNr,dis_right,dis_left);
					}
				}
				// op is to the right and moves away, but maybe we can block him
				else if(opSideDis>=0 && opSideDis < dis_right)
				{
Stas Verberkt's avatar
Stas Verberkt committed
406
					if(opForwardDis < m_lookForward && dis_right > m_lookForward)
407
					{
408
						if (m_random.bounded(100) <= doPercentage/2 || dis_forward==1)
Stas Verberkt's avatar
Stas Verberkt committed
409
							m_tron->getPlayer(playerNr)->setDirection(sides[1]); // turn right
410
					}
Stas Verberkt's avatar
Stas Verberkt committed
411
					else if(dis_forward < m_lookForward)
412 413 414 415
					{
						dis_forward = 100 - 100/dis_forward;

							if(!(dis_left == 1 && dis_right == 1))
416
								if (m_random.bounded(100) >= dis_forward || dis_forward == 1)
417 418 419
									changeDirection(playerNr,dis_right,dis_left);
					}
				}
Stas Verberkt's avatar
Stas Verberkt committed
420
				else if(dis_forward < m_lookForward)
421 422 423 424
				{
					dis_forward = 100 - 100/dis_forward;

						if(!(dis_left == 1 && dis_right == 1))
425
							if (m_random.bounded(100) >= dis_forward || dis_forward == 1)
426 427 428 429 430 431 432
								changeDirection(playerNr,dis_right,dis_left);
				}
			}
			// opponent is in the back of us
			else
			{
				// opponent is right from us and we already blocked him
Stas Verberkt's avatar
Stas Verberkt committed
433
				if(opSideDis>0 && opForwardDis < m_lookForward && opSideDis < dis_right)
434
				{
435
					if (m_random.bounded(100) <= doPercentage/2 || dis_forward==1)
436 437
						changeDirection(playerNr,dis_right,dis_left);
				}
Stas Verberkt's avatar
Stas Verberkt committed
438
				else if(dis_forward < m_lookForward)
439 440 441 442
				{
					dis_forward = 100 - 100/dis_forward;

						if(!(dis_left == 1 && dis_right == 1))
443
							if (m_random.bounded(100) >= dis_forward || dis_forward == 1)
444 445 446 447 448 449 450 451 452 453 454 455
								changeDirection(playerNr,dis_right,dis_left);
				}
			}
		} // end if(opMovesRight)
		else if(opMovesLeft)
		{
			// opponent is in front of us
			if(opForwardDis>0)
			{
				// opponent is to the right, moves towards us and could block us
				if(opSideDis > 0 && opSideDis < opForwardDis && opSideDis < dis_right)
				{
Stas Verberkt's avatar
Stas Verberkt committed
456
					if(opForwardDis < m_lookForward && dis_right > m_lookForward)
457
					{
458
						if (m_random.bounded(100) <= doPercentage/2 || dis_forward==1)
459 460
							changeDirection(playerNr,dis_right,dis_left);
					}
Stas Verberkt's avatar
Stas Verberkt committed
461
					else if(dis_forward < m_lookForward)
462 463 464 465
					{
						dis_forward = 100 - 100/dis_forward;

						if(!(dis_left == 1 && dis_right == 1))
466
							if (m_random.bounded(100) >= dis_forward || dis_forward == 1)
467 468 469 470 471 472
								changeDirection(playerNr,dis_right,dis_left);
					}
				}
				// op is to the left and moves away, but maybe we can block him
				else if(opSideDis<=0 && opSideDis < dis_left)
				{
Stas Verberkt's avatar
Stas Verberkt committed
473
					if(opForwardDis < m_lookForward && dis_left > m_lookForward)
474
					{
475
						if (m_random.bounded(100) <= doPercentage/2 || dis_forward==1)
Stas Verberkt's avatar
Stas Verberkt committed
476
							m_tron->getPlayer(playerNr)->setDirection(sides[0]); // m_turn left
477
						}
Stas Verberkt's avatar
Stas Verberkt committed
478
					else if(dis_forward < m_lookForward)
479 480 481 482
					{
						dis_forward = 100 - 100/dis_forward;

						if(!(dis_left == 1 && dis_right == 1))
483
							if (m_random.bounded(100) >= dis_forward || dis_forward == 1)
484 485 486 487
								changeDirection(playerNr,dis_right,dis_left);
					}

				}
Stas Verberkt's avatar
Stas Verberkt committed
488
				else if(dis_forward < m_lookForward)
489 490 491 492
				{
					dis_forward = 100 - 100/dis_forward;

					if(!(dis_left == 1 && dis_right == 1))
493
						if (m_random.bounded(100) >= dis_forward || dis_forward == 1)
494 495 496 497 498 499 500
							changeDirection(playerNr,dis_right,dis_left);
				}
			}
			// opponent is in the back of us
			else //if(opForwardDis<=0)
			{
				// opponent is left from us and we already blocked him
Stas Verberkt's avatar
Stas Verberkt committed
501
				if(opSideDis<0 && opForwardDis < m_lookForward && -opSideDis < dis_left)
502
				{
503
					if (m_random.bounded(100) <= doPercentage/2 || dis_forward==1)
504 505
						changeDirection(playerNr,dis_right,dis_left);
				}
Stas Verberkt's avatar
Stas Verberkt committed
506
				else if(dis_forward < m_lookForward)
507 508 509 510
				{
					dis_forward = 100 - 100/dis_forward;

					if(!(dis_left == 1 && dis_right == 1))
511
						if (m_random.bounded(100) >= dis_forward || dis_forward == 1)
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
							changeDirection(playerNr,dis_right,dis_left);
				}
			}
		} // end if(opMovesLeft)

	}
	// This part is completely ported from
	// xtron-1.1 by Rhett D. Jacobs <rhett@hotel.canberra.edu.au>
	else // Settings::skill() == Settings::EnumSkill::Easy
	{
		PlayerDirections::Direction sides[2];
		sides[0] = PlayerDirections::None;
		sides[1] = PlayerDirections::None;
		int flags[6] = {0,0,0,0,0,0};
		int index[2];
		int dis_forward,  dis_left, dis_right;

		dis_forward = dis_left = dis_right = 1;

Stas Verberkt's avatar
Stas Verberkt committed
531
		switch (m_tron->getPlayer(playerNr)->getDirection()) {
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
			case PlayerDirections::Left:
				//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
				sides[0] = PlayerDirections::Down;
				sides[1] = PlayerDirections::Up;
				break;
			case PlayerDirections::Right:
				flags[0] = 1;
				flags[1] = 0;
				flags[2] = 0;
				flags[3] = -1;
				flags[4] = 0;
				flags[5] = 1;
				sides[0] = PlayerDirections::Up;
				sides[1] = PlayerDirections::Down;
				break;
			case PlayerDirections::Up:
				flags[0] = 0;
				flags[1] = -1;
				flags[2] = -1;
				flags[3] = 0;
				flags[4] = 1;
				flags[5] = 0;
				sides[0] = PlayerDirections::Left;
				sides[1] = PlayerDirections::Right;
				break;
			case PlayerDirections::Down:
				flags[0] = 0;
				flags[1] = 1;
				flags[2] = 1;
				flags[3] = 0;
				flags[4] = -1;
				flags[5] = 0;
				sides[0] = PlayerDirections::Right;
				sides[1] = PlayerDirections::Left;
				break;
			default:
				break;
		}

		// check forward
Stas Verberkt's avatar
Stas Verberkt committed
581 582 583
		index[0] = m_tron->getPlayer(playerNr)->getX() + flags[0];
		index[1] = m_tron->getPlayer(playerNr)->getY() + flags[1];
		while (index[0] < m_tron->getPlayField()->getWidth() && index[0] >= 0 && index[1] < m_tron->getPlayField()->getHeight() && index[1] >= 0 && m_tron->getPlayField()->getObjectAt(index[0], index[1])->getObjectType() == ObjectType::Object) {
584 585 586 587 588
			dis_forward++;
			index[0] += flags[0];
			index[1] += flags[1];
		}

Stas Verberkt's avatar
Stas Verberkt committed
589
		if (dis_forward < m_lookForward)
590 591 592 593
		{
			dis_forward = 100 - 100 / dis_forward;

			// check left
Stas Verberkt's avatar
Stas Verberkt committed
594 595 596
			index[0] = m_tron->getPlayer(playerNr)->getX() + flags[2];
			index[1] = m_tron->getPlayer(playerNr)->getY() + flags[3];
			while (index[0] < m_tron->getPlayField()->getWidth() && index[0] >= 0 && index[1] < m_tron->getPlayField()->getHeight() && index[1] >= 0 && m_tron->getPlayField()->getObjectAt(index[0], index[1])->getObjectType() == ObjectType::Object) {
597 598 599 600 601 602
				dis_left++;
				index[0] += flags[2];
				index[1] += flags[3];
			}

			// check right
Stas Verberkt's avatar
Stas Verberkt committed
603 604 605
			index[0] = m_tron->getPlayer(playerNr)->getX() + flags[4];
			index[1] = m_tron->getPlayer(playerNr)->getY() + flags[5];
			while (index[0] < m_tron->getPlayField()->getWidth() && index[0] >= 0 && index[1] <  m_tron->getPlayField()->getHeight() && index[1] >= 0 && m_tron->getPlayField()->getObjectAt(index[0], index[1])->getObjectType() == ObjectType::Object) {
606 607 608 609 610
				dis_right++;
				index[0] += flags[4];
				index[1] += flags[5];
			}
			if(!(dis_left == 1 && dis_right == 1)) {
611
				if (m_random.bounded(100) >= dis_forward || dis_forward == 0) {
612
					// change direction
613
					if (m_random.bounded(100) <= (100*dis_left)/(dis_left+dis_right)) {
614 615
						if (dis_left != 1)
							// turn to the left
Stas Verberkt's avatar
Stas Verberkt committed
616
							m_tron->getPlayer(playerNr)->setDirection(sides[0]);
617 618
						else
							// turn to the right
Stas Verberkt's avatar
Stas Verberkt committed
619
							m_tron->getPlayer(playerNr)->setDirection(sides[1]);
620 621 622 623
					}
					else {
						if (dis_right != 1)
							// turn to the right
Stas Verberkt's avatar
Stas Verberkt committed
624
							m_tron->getPlayer(playerNr)->setDirection(sides[1]);
625 626
						else
							// turn to the left
Stas Verberkt's avatar
Stas Verberkt committed
627
							m_tron->getPlayer(playerNr)->setDirection(sides[0]);
628 629 630 631 632
					}
				}
			}
		}
	}
Stas Verberkt's avatar
Stas Verberkt committed
633 634
}