User moves are now created by the entity.
[tagua/yd.git] / src / entities / gameentity.cpp
blobdb792a5d2c9feef3fffd33fdc42cd6abbb0bdb1a
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include "gameentity.h"
12 #include "premove.h"
13 #include "game.h"
14 #include "board.h"
15 #include "pgnparser.h"
17 using namespace boost;
19 GameEntity::GameEntity(const VariantPtr& variant, const boost::shared_ptr<Game>& game,
20 Board* chessboard, AgentGroup* group)
21 : UserEntity(game)
22 , m_variant(variant)
23 , m_chessboard(chessboard)
24 , m_dispatcher(group, this) {
27 QString GameEntity::save() const {
28 return m_game->pgn();
31 void GameEntity::loadPGN(const PGN& pgn) {
32 m_game->load(pgn);
35 AbstractPosition::Ptr GameEntity::doMove(AbstractMove::Ptr move) const {
36 AbstractPosition::Ptr newPosition = position()->clone();
37 newPosition->move(move);
38 return newPosition;
41 void GameEntity::executeMove(AbstractMove::Ptr move) {
42 AbstractPosition::Ptr ref = position();
43 AbstractPosition::Ptr pos = doMove(move);
44 m_game->add(move, pos);
45 m_dispatcher.move(m_game->index());
48 void GameEntity::addPremove(const NormalUserMove& m) {
49 m_premoveQueue = shared_ptr<Premove>(new Premove(m_variant->createNormalMove(m)));
52 void GameEntity::addPremove(const DropUserMove& m) {
53 m_premoveQueue = shared_ptr<Premove>(new Premove(m_variant->createDropMove(m)));
56 void GameEntity::cancelPremove() {
57 m_premoveQueue.reset();
58 m_chessboard->cancelPremove();
61 void GameEntity::notifyMove(const Index&) {
62 // the other player moved: execute premove
63 if (m_premoveQueue) {
64 AbstractMove::Ptr move = m_premoveQueue->execute(m_game->position());
65 if (move)
66 executeMove(move);
68 cancelPremove();
71 void GameEntity::notifyBack() { }
72 void GameEntity::notifyForward() { }
73 void GameEntity::notifyGotoFirst() { }
74 void GameEntity::notifyGotoLast() { }
76 NormalUserMove GameEntity::createMove(const Point& from, const Point& to) const {
77 NormalUserMove m = UserEntity::createMove(from, to);
78 return m;
81 AbstractMove::Ptr GameEntity::testMove(const NormalUserMove& move) const {
82 AbstractMove::Ptr m = m_variant->createNormalMove(move);
83 if (m && position()->testMove(m))
84 return m;
85 else
86 return AbstractMove::Ptr();
89 AbstractMove::Ptr GameEntity::testMove(const DropUserMove& move) const {
90 AbstractMove::Ptr m = m_variant->createDropMove(move);
91 if (m && position()->testMove(m))
92 return m;
93 else
94 return AbstractMove::Ptr ();
97 AbstractPiece::Ptr GameEntity::moveHint(AbstractMove::Ptr move) const {
98 return position()->moveHint(move);
101 bool GameEntity::testPremove(const NormalUserMove&) const {
102 return true; // TODO
105 bool GameEntity::testPremove(const DropUserMove&) const {
106 return true; // TODO
109 InteractionType GameEntity::validTurn(int pool) const {
110 return position()->droppable(m_turn_test, pool);
113 InteractionType GameEntity::validTurn(const Point& point) const {
114 // TODO: maybe replace this call with a new API position()->owner(point) ??
115 return position()->movable(m_turn_test, point);
118 bool GameEntity::movable(const Point& point) const {
119 if (!m_enabled) return false;
120 InteractionType action = validTurn(point);
121 return m_turn_test.premove() ? action != NoAction : action == Moving;
124 bool GameEntity::oneClickMoves() const {
125 return m_variant->simpleMoves();
128 bool GameEntity::gotoFirst() {
129 m_game->gotoFirst();
130 return true;
133 bool GameEntity::gotoLast() {
134 m_game->gotoLast();
135 return true;
138 bool GameEntity::goTo(const Index& index) {
139 m_game->goTo(index);
140 return true;
143 bool GameEntity::forward() {
144 return m_game->forward();
147 bool GameEntity::back() {
148 return m_game->back();
151 bool GameEntity::undo() {
152 if (m_editing_tools)
153 m_game->undo();
154 return true;
157 bool GameEntity::redo() {
158 if (m_editing_tools)
159 m_game->redo();
160 return true;
163 bool GameEntity::truncate() {
164 if (m_editing_tools)
165 m_game->truncate();
166 return true;
168 bool GameEntity::promoteVariation() {
169 if (m_editing_tools)
170 m_game->promoteVariation();
171 return true;
174 bool GameEntity::canDetach() const {
175 return true;