Record variant name in PGN file, so they can be read accordingly.
[tagua/yd.git] / src / entities / gameentity.cpp
blob8733de8fd03bcd58742834b6f68343547f8c77ab
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
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
29 ((m_variant->name() == "Chess") ? QString()
30 : ("[Variant \"" + m_variant->name()) + "\"]")
31 + m_game->pgn();
34 void GameEntity::loadPGN(const PGN& pgn) {
35 m_game->load(pgn);
38 AbstractPosition::Ptr GameEntity::doMove(AbstractMove::Ptr move) const {
39 AbstractPosition::Ptr newPosition = position()->clone();
40 newPosition->move(move);
41 return newPosition;
44 void GameEntity::executeMove(AbstractMove::Ptr move) {
45 AbstractPosition::Ptr ref = position();
46 AbstractPosition::Ptr pos = doMove(move);
47 m_game->add(move, pos);
48 m_dispatcher.move(m_game->index());
51 void GameEntity::addPremove(const NormalUserMove& m) {
52 m_premoveQueue = shared_ptr<Premove>(new Premove(m_variant->createNormalMove(m)));
55 void GameEntity::addPremove(const DropUserMove& m) {
56 m_premoveQueue = shared_ptr<Premove>(new Premove(m_variant->createDropMove(m)));
59 void GameEntity::cancelPremove() {
60 m_premoveQueue.reset();
61 m_chessboard->cancelPremove();
64 void GameEntity::notifyMove(const Index&) {
65 // the other player moved: execute premove
66 if (m_premoveQueue) {
67 AbstractMove::Ptr move = m_premoveQueue->execute(m_game->position());
68 if (move)
69 executeMove(move);
71 cancelPremove();
74 void GameEntity::notifyBack() { }
75 void GameEntity::notifyForward() { }
76 void GameEntity::notifyGotoFirst() { }
77 void GameEntity::notifyGotoLast() { }
79 NormalUserMove GameEntity::createMove(const Point& from, const Point& to) const {
80 NormalUserMove m = UserEntity::createMove(from, to);
81 m_variant->setupMove(m);
82 return m;
85 AbstractMove::Ptr GameEntity::testMove(const NormalUserMove& move) const {
86 AbstractMove::Ptr m = m_variant->createNormalMove(move);
87 if (m && position()->testMove(m))
88 return m;
89 else
90 return AbstractMove::Ptr();
93 AbstractMove::Ptr GameEntity::testMove(const DropUserMove& move) const {
94 AbstractMove::Ptr m = m_variant->createDropMove(move);
95 if (m && position()->testMove(m))
96 return m;
97 else
98 return AbstractMove::Ptr ();
101 AbstractPiece::Ptr GameEntity::moveHint(AbstractMove::Ptr move) const {
102 return position()->moveHint(move);
105 bool GameEntity::testPremove(const NormalUserMove&) const {
106 return true; // TODO
109 bool GameEntity::testPremove(const DropUserMove&) const {
110 return true; // TODO
113 InteractionType GameEntity::validTurn(int pool) const {
114 return position()->droppable(m_turn_test, pool);
117 InteractionType GameEntity::validTurn(const Point& point) const {
118 // TODO: maybe replace this call with a new API position()->owner(point) ??
119 return position()->movable(m_turn_test, point);
122 bool GameEntity::movable(const Point& point) const {
123 if (!m_enabled) return false;
124 InteractionType action = validTurn(point);
125 return m_turn_test.premove() ? action != NoAction : action == Moving;
128 bool GameEntity::oneClickMoves() const {
129 return m_variant->simpleMoves();
132 bool GameEntity::gotoFirst() {
133 m_game->gotoFirst();
134 return true;
137 bool GameEntity::gotoLast() {
138 m_game->gotoLast();
139 return true;
142 bool GameEntity::goTo(const Index& index) {
143 m_game->goTo(index);
144 return true;
147 bool GameEntity::forward() {
148 return m_game->forward();
151 bool GameEntity::back() {
152 return m_game->back();
155 bool GameEntity::undo() {
156 if (m_editing_tools)
157 m_game->undo();
158 return true;
161 bool GameEntity::redo() {
162 if (m_editing_tools)
163 m_game->redo();
164 return true;
167 bool GameEntity::truncate() {
168 if (m_editing_tools)
169 m_game->truncate();
170 return true;
172 bool GameEntity::promoteVariation() {
173 if (m_editing_tools)
174 m_game->promoteVariation();
175 return true;
178 bool GameEntity::canDetach() const {
179 return true;