Intelligent robot movement.
[sdlbotor.git] / Robot.cpp
blob52f4e96fa56d0e6242ba52668fbcd1eb093fc919
1 #include "Robot.h"
2 #include "globalfunc.h"
3 #include "Map.h"
4 #include "Game.h"
6 #include <cstdio>
7 #include <cstdlib>
9 namespace botor
12 Robot::Robot( Uint8 X, Uint8 Y ) : LivingMapObject( X, Y )
14 walking = true;
15 MakeNewDir();
16 StartMovement();
19 float Robot::SPEED() { return ROBOT_SPEED; }
21 void Robot::StartMovement()
23 Sint16 sx,sy;
24 Map::transformM2S( mapX, mapY, sx, sy );
25 movementX = (float)sx;
26 movementY = (float)sy;
28 if( !isWalkable( mapX+vX, mapY+vY ) )
29 MakeNewDir();
31 mapX += vX;
32 mapY += vY;
33 if( isBitSet( Game::getMap()->tileAt( mapX, mapY )->typeMask, Tile::TILE_SLOW ) )
34 speed = SPEED() * 0.5f;
35 else
36 speed = SPEED();
38 moving = true;
42 bool Robot::isWalkable( Uint8 X, Uint8 Y )
44 Tile *t = Game::getMap()->tileAt(X, Y);
45 if(
46 (isBitSet( t->typeMask, Tile::TILE_WALKABLE ) ||
47 isBitSet( t->typeMask, Tile::TILE_EFENCE )) &&
48 !Game::getMap()->isRobotOn(X, Y)
51 if( isBitSet( t->typeMask, Tile::TILE_EFENCE ) )
53 return rand()%4 == 0;
56 return true;
58 else
59 return false;
62 void Robot::OnTile( Tile *t )
64 if( isBitSet( t->typeMask, Tile::TILE_EFENCE ) )
66 Game::getMap()->RemoveTile( mapX, mapY );
67 Die();
70 if( mapX == Game::getPlayer()->getMapX() &&
71 mapY == Game::getPlayer()->getMapY() )
73 if( Game::getPlayer()->Walking() )
75 if( rand()%3 )
77 Game::getPlayer()->Die();
80 else
81 Game::getPlayer()->Die();
83 if( rand()%4 == 0 )
84 Die();
87 MakeNewDir();
90 void Robot::MakeNewDir()
92 vX = sign( Game::getPlayer()->getMapX() - mapX );
93 vY = sign( Game::getPlayer()->getMapY() - mapY );
95 if( !isWalkable( mapX+vX, mapY+vY ) )
97 if( isWalkable( mapX, mapY+vY ) )
98 vX = 0;
99 else if( isWalkable( mapX+vX, mapY ) )
100 vY = 0;
101 else
104 for( Uint8 y = mapY-1; y <= mapY+1; y++ )
106 for( Uint8 x = mapX-1; x <= mapX+1; x++ )
108 if( isWalkable( x, y ) )
110 vX = sign( x - mapX );
111 vY = sign( y - mapY );
112 return;
116 Die();