Exported QFont to lua, for border text and clocks
[tagua/yd.git] / src / entities / gameentity.cpp
blob8cb583d6abb4343656b6b327417fac9548879147
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"
16 #include "variants/xchess/piecetype.h"
18 using namespace boost;
20 GameEntity::GameEntity(VariantInfo* variant, const boost::shared_ptr<Game>& game,
21 Board* chessboard, AgentGroup* group)
22 : UserEntity(game, QUEEN)
23 , m_variant(variant)
24 , m_chessboard(chessboard)
25 , m_dispatcher(group, this) {
26 m_turn_test = shared_ptr<TurnTest>(new NoTurnTest);
29 QString GameEntity::save() const {
30 return m_game->pgn();
33 void GameEntity::loadPGN(const PGN& pgn) {
34 m_game->load(pgn);
37 AbstractPosition::Ptr GameEntity::doMove(AbstractMove::Ptr move) const {
38 AbstractPosition::Ptr newPosition = position()->clone();
39 newPosition->move(move);
40 return newPosition;
43 void GameEntity::executeMove(AbstractMove::Ptr move) {
44 AbstractPosition::Ptr ref = position();
45 AbstractPosition::Ptr pos = doMove(move);
46 m_game->add(move, pos);
47 m_dispatcher.move(move, ref);
50 void GameEntity::addPremove(const NormalUserMove& m) {
51 m_premoveQueue = shared_ptr<Premove>(new Premove(m_variant->createNormalMove(m)));
54 void GameEntity::addPremove(const DropUserMove& m) {
55 m_premoveQueue = shared_ptr<Premove>(new Premove(m_variant->createDropMove(m)));
58 void GameEntity::cancelPremove() {
59 m_premoveQueue.reset();
60 m_chessboard->cancelPremove();
63 void GameEntity::notifyMove(AbstractMove::Ptr, AbstractPosition::Ptr) {
64 // the other player moved: execute premove
65 if (m_premoveQueue) {
66 AbstractMove::Ptr move = m_premoveQueue->execute(m_game->position());
67 if (move)
68 executeMove(move);
70 cancelPremove();
73 void GameEntity::notifyBack() { }
74 void GameEntity::notifyForward() { }
75 void GameEntity::notifyGotoFirst() { }
76 void GameEntity::notifyGotoLast() { }
78 AbstractMove::Ptr GameEntity::testMove(const NormalUserMove& move) const {
79 AbstractMove::Ptr m = m_variant->createNormalMove(move);
80 if (m && position()->testMove(m))
81 return m;
82 else
83 return AbstractMove::Ptr();
86 AbstractMove::Ptr GameEntity::testMove(const DropUserMove& move) const {
87 AbstractMove::Ptr m = m_variant->createDropMove(move);
88 if (m && position()->testMove(m))
89 return m;
90 else
91 return AbstractMove::Ptr ();
94 AbstractPiece::Ptr GameEntity::moveHint(AbstractMove::Ptr move) const {
95 return position()->moveHint(move);
98 bool GameEntity::testPremove(const NormalUserMove&) const {
99 return true; // TODO
102 bool GameEntity::testPremove(const DropUserMove&) const {
103 return true; // TODO
106 InteractionType GameEntity::validTurn(int pool) const {
107 return position()->droppable(pool);
110 InteractionType GameEntity::validTurn(const Point& point) const {
111 return position()->movable(point);
114 bool GameEntity::movable(const Point& point) const {
115 if (!m_enabled) return false;
116 InteractionType action = validTurn(point);
117 return m_premove ? action != NoAction : action == Moving;
120 bool GameEntity::oneClickMoves() const {
121 return m_variant->simpleMoves();
124 bool GameEntity::gotoFirst() {
125 m_game->gotoFirst();
126 return true;
129 bool GameEntity::gotoLast() {
130 m_game->gotoLast();
131 return true;
134 bool GameEntity::goTo(const Index& index) {
135 m_game->goTo(index);
136 return true;
139 bool GameEntity::forward() {
140 return m_game->forward();
143 bool GameEntity::back() {
144 return m_game->back();
147 bool GameEntity::undo() {
148 if (m_editing_tools)
149 m_game->undo();
150 return true;
153 bool GameEntity::redo() {
154 if (m_editing_tools)
155 m_game->redo();
156 return true;
159 bool GameEntity::truncate() {
160 if (m_editing_tools)
161 m_game->truncate();
162 return true;
164 bool GameEntity::promoteVariation() {
165 if (m_editing_tools)
166 m_game->promoteVariation();
167 return true;
170 bool GameEntity::canDetach() const {
171 return true;