Initial porting to the new component API.
[tagua/yd.git] / src / entities / gameentity.cpp
blobba865836b426f248aaf5f3233fa794860e9a7cff
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"
13 #include <core/policy.h>
14 #include <core/state.h>
15 #include <core/validator.h>
16 #include <core/variant.h>
18 #include "game.h"
19 #include "chessboard.h"
20 #include "pgnparser.h"
22 using namespace boost;
24 GameEntity::GameEntity(Variant* variant, const boost::shared_ptr<Game>& game,
25 ChessBoard* chessboard, AgentGroup* group)
26 : UserEntity(game)
27 , m_variant(variant)
28 , m_chessboard(chessboard)
29 , m_dispatcher(group, this) {
30 m_validator = requestComponent<IValidator>(m_variant, "validator");
31 m_policy = requestComponent<IPolicy>(m_variant, "policy");
34 QString GameEntity::save() const {
35 return m_game->pgn();
38 void GameEntity::loadPGN(const PGN& pgn) {
39 m_game->load(pgn);
42 StatePtr GameEntity::doMove(const Move& move) const {
43 StatePtr newState(position()->clone());
44 newState->move(move);
45 return newState;
48 void GameEntity::executeMove(const Move& move) {
49 StatePtr ref = position();
50 StatePtr pos = doMove(move);
51 m_game->add(move, pos);
52 m_dispatcher.move(m_game->index());
55 void GameEntity::addPremove(const Move& m) {
56 m_premoveQueue = m;
59 void GameEntity::cancelPremove() {
60 m_premoveQueue = Move();
61 m_chessboard->cancelPremove();
64 void GameEntity::notifyMove(const Index&) {
65 // the other player moved: execute premove
66 if (m_premoveQueue != Move() && testMove(m_premoveQueue)) {
67 executeMove(m_premoveQueue);
69 cancelPremove();
72 void GameEntity::notifyBack() { }
73 void GameEntity::notifyForward() { }
74 void GameEntity::notifyGotoFirst() { }
75 void GameEntity::notifyGotoLast() { }
77 bool GameEntity::testMove(Move& move) const {
78 return m_validator->legal(position().get(), move);
81 Piece GameEntity::moveHint(const Move&) const {
82 // return position()->moveHint(move);
83 return Piece(); // FIXME implement move hinting
86 bool GameEntity::testPremove(const Move&) const {
87 return true; // FIXME implement premove checking
90 InteractionType GameEntity::validTurn(const IColor* pool) const {
91 return m_policy->droppable(position().get(), m_turn_test, pool);
94 InteractionType GameEntity::validTurn(const Point& point) const {
95 return m_policy->movable(position().get(), m_turn_test, point);
98 bool GameEntity::movable(const Point& point) const {
99 if (!m_enabled) return false;
100 InteractionType action = validTurn(point);
101 return m_turn_test.premove() ? action != NoAction : action == Moving;
104 bool GameEntity::oneClickMoves() const {
105 return false; // FIXME support one-click moves
108 bool GameEntity::gotoFirst() {
109 m_game->gotoFirst();
110 return true;
113 bool GameEntity::gotoLast() {
114 m_game->gotoLast();
115 return true;
118 bool GameEntity::goTo(const Index& index) {
119 m_game->goTo(index);
120 return true;
123 bool GameEntity::forward() {
124 return m_game->forward();
127 bool GameEntity::back() {
128 return m_game->back();
131 bool GameEntity::undo() {
132 if (m_editing_tools)
133 m_game->undo();
134 return true;
137 bool GameEntity::redo() {
138 if (m_editing_tools)
139 m_game->redo();
140 return true;
143 bool GameEntity::truncate() {
144 if (m_editing_tools)
145 m_game->truncate();
146 return true;
148 bool GameEntity::promoteVariation() {
149 if (m_editing_tools)
150 m_game->promoteVariation();
151 return true;
154 bool GameEntity::canDetach() const {
155 return true;