Address some valgrind-detected problems, flag others for later.
[tagua/yd.git] / src / core / defaultstate.cpp
blob357f78ac8bc0260563adc5b9af14cdc41de4d5fc
1 #include "behaviour.h"
2 #include "board.h"
3 #include "defaultstate.h"
4 #include "defaulttype.h"
6 std::vector<const Point*> DefaultState::theoreticalMoves(const Piece& piece,
7 const Point& src) const {
8 std::vector<const Point*> destinations;
10 const IBehaviour* behaviour = this->behaviour();
11 if (!behaviour) return destinations; // empty
13 const std::vector<MoveDefinition> * defs =
14 dynamic_cast<const DefaultType*>(piece.type())->movesDefinitions();
15 if (!defs) return destinations; // empty
17 //kDebug() << "looking at moves from " << src;
18 for (unsigned direction=0; direction < defs->size(); direction++)
19 for (int distance=1;
20 (*defs)[direction].distance ? (distance <= (*defs)[direction].distance) : 1;
21 distance++) {
22 Point* candidate = new Point(src.x + ((*defs)[direction].x * distance *
23 -behaviour->direction(piece.color()).y),
24 src.y + ((*defs)[direction].y * distance *
25 behaviour->direction(piece.color()).y));
26 if (!board()->valid(*candidate)) {
27 // end of board: don't look further in that direction
28 delete candidate;
29 break;
32 destinations.push_back(candidate);
34 // stop looking in direction if there is a piece blocking move here
35 const Piece otherPiece = board()->get(*candidate);
36 if (otherPiece != Piece())
37 break;
40 return destinations;