From e487f0426d9de022b759ae6a529b10a72c31de8f Mon Sep 17 00:00:00 2001 From: Paolo Capriotti Date: Tue, 20 Nov 2007 14:25:15 +0100 Subject: [PATCH] Initial porting to the new component API. Note: merging changes prior to this commit may be a nightmare. A lot of files have been moved to core/, and a lot of types and method names have changed. If you have changes in hlvariant/, they will probably merge cleanly, but still have to be rewritten to be adapted to the new API. If your changes touch files like board.cpp (which has been renamed to chessboard.cpp, btw), piecepool.cpp, game.cpp or graphicalsystem.cpp, then good luck :) --- src/CMakeLists.txt | 61 ++-- src/agent.h | 1 - src/agentgroup.h | 1 - src/animation.h | 2 +- src/animationfactory.cpp | 202 ----------- src/animationfactory.h | 223 ------------ src/animationsettings.cpp | 29 ++ src/{graphicalapi.cpp => animationsettings.h} | 18 +- src/{board.cpp => chessboard.cpp} | 169 +++++---- src/{board.h => chessboard.h} | 48 +-- src/chesstable.cpp | 15 +- src/chesstable.h | 14 +- src/controllers/abstract.h | 14 +- src/controllers/editgame.cpp | 147 ++++---- src/controllers/editgame.h | 24 +- src/controllers/editposition.h | 1 - src/core/CMakeLists.txt | 3 + src/core/board.h | 2 + src/core/color.cpp | 4 + src/core/color.h | 7 + src/core/defaultpolicy.cpp | 32 ++ src/core/defaultpolicy.h | 29 ++ src/{ => core}/interactiontype.h | 0 src/core/move.cpp | 4 +- src/core/move.h | 8 +- src/core/moveserializer.h | 3 +- src/{graphicalapi.cpp => core/policy.cpp} | 6 +- src/core/policy.h | 43 +++ .../poolcollection_fwd.h} | 10 +- src/core/repository.h | 19 +- src/core/state.h | 7 +- src/{graphicalapi.cpp => core/state_fwd.h} | 10 +- src/core/taguaapi.h | 3 +- src/{ => core}/turnpolicy.cpp | 6 +- src/{ => core}/turnpolicy.h | 14 +- src/core/variant.h | 5 + src/engine.h | 11 +- src/engineinfo.cpp | 18 +- src/engineinfo.h | 14 +- src/entities/engineentity.cpp | 19 +- src/entities/engineentity.h | 10 +- src/entities/entity.cpp | 2 +- src/entities/entity.h | 5 +- src/entities/examinationentity.cpp | 29 +- src/entities/examinationentity.h | 29 +- src/entities/gameentity.cpp | 89 ++--- src/entities/gameentity.h | 40 ++- src/entities/icsentity.cpp | 76 ++-- src/entities/icsentity.h | 33 +- src/entities/userentity.cpp | 8 - src/entities/userentity.h | 29 +- src/fwd.h | 59 --- src/game.cpp | 68 ++-- src/game.h | 26 +- src/{graphicalapi.cpp => game_fwd.h} | 11 +- src/game_p.h | 8 +- src/gnushogiengine.cpp | 17 +- src/gnushogiengine.h | 14 +- src/graphicalapi.h | 115 ------ src/graphicalgame.cpp | 43 ++- src/graphicalgame.h | 3 + src/graphicalsystem.cpp | 166 ++++----- src/graphicalsystem.h | 69 ++-- src/hlvariant/animator.h | 2 - src/hlvariant/dropanimator.h | 3 +- src/hlvariant/graphicalapi_unwrapped.h | 2 +- src/hlvariant/movefactory.h | 1 - src/hlvariant/tagua_wrapped.h | 2 - src/icsapi.h | 18 +- src/{graphicalapi.cpp => icsapi_fwd.h} | 10 +- src/icsconnection.cpp | 14 +- src/icsgamedata.cpp | 13 +- src/icsgamedata.h | 5 +- src/{premove.h => icslistener.h} | 25 +- src/mainwindow.cpp | 34 +- src/mainwindow.h | 4 +- src/movefactory.h | 1 - src/piecepool.cpp | 44 ++- src/piecepool.h | 13 +- src/poolinfo.h | 7 +- src/positioninfo.cpp | 13 +- src/positioninfo.h | 10 +- src/pref_theme.cpp | 33 +- src/pref_theme.h | 5 +- src/premove.cpp | 27 -- src/tagua.h | 397 --------------------- src/tagua_legacy.cpp | 282 --------------- src/tagua_legacy.h | 118 ------ src/ui.cpp | 4 +- src/ui.h | 13 +- src/usermove.h | 1 - src/variants.cpp | 122 +++---- src/variants.h | 13 +- src/variants/chess/chess.cpp | 10 +- src/variants/chess/namer.h | 3 +- src/variants/chess/state.cpp | 6 + src/variants/chess/state.h | 2 + src/variants/chess/validator.h | 3 +- src/xboardengine.cpp | 33 +- src/xboardengine.h | 15 +- 100 files changed, 1144 insertions(+), 2359 deletions(-) delete mode 100644 src/animationfactory.cpp delete mode 100644 src/animationfactory.h create mode 100644 src/animationsettings.cpp copy src/{graphicalapi.cpp => animationsettings.h} (54%) rename src/{board.cpp => chessboard.cpp} (80%) rename src/{board.h => chessboard.h} (92%) create mode 100644 src/core/defaultpolicy.cpp create mode 100644 src/core/defaultpolicy.h rename src/{ => core}/interactiontype.h (100%) copy src/{graphicalapi.cpp => core/policy.cpp} (71%) create mode 100644 src/core/policy.h copy src/{graphicalapi.cpp => core/poolcollection_fwd.h} (60%) copy src/{graphicalapi.cpp => core/state_fwd.h} (65%) rename src/{ => core}/turnpolicy.cpp (85%) rename src/{ => core}/turnpolicy.h (81%) delete mode 100644 src/fwd.h copy src/{graphicalapi.cpp => game_fwd.h} (68%) delete mode 100644 src/graphicalapi.h rename src/{graphicalapi.cpp => icsapi_fwd.h} (67%) rename src/{premove.h => icslistener.h} (50%) delete mode 100644 src/premove.cpp delete mode 100644 src/tagua.h delete mode 100644 src/tagua_legacy.cpp delete mode 100644 src/tagua_legacy.h rewrite src/variants.cpp (65%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ae2d525..7161c2f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,45 +22,42 @@ set(tagua_SRC entities/entity.cpp entities/engineentity.cpp entities/icsentity.cpp - - hlvariant/chess/variant.cpp - hlvariant/chess/san.cpp - hlvariant/chess/icsverbose.cpp - hlvariant/chess/move.cpp - hlvariant/chess/gamestate.cpp - hlvariant/chess/piece.cpp - hlvariant/chess/actions.cpp - - hlvariant/dummy/variant.cpp - - hlvariant/crazyhouse/variant.cpp - hlvariant/crazyhouse/piece.cpp - - hlvariant/minichess5/variant.cpp - - hlvariant/shogi/variant.cpp - hlvariant/shogi/piece.cpp - hlvariant/shogi/shogiactions.cpp +# +# hlvariant/chess/variant.cpp +# hlvariant/chess/san.cpp +# hlvariant/chess/icsverbose.cpp +# hlvariant/chess/move.cpp +# hlvariant/chess/gamestate.cpp +# hlvariant/chess/piece.cpp +# hlvariant/chess/actions.cpp +# +# hlvariant/dummy/variant.cpp +# +# hlvariant/crazyhouse/variant.cpp +# hlvariant/crazyhouse/piece.cpp +# +# hlvariant/minichess5/variant.cpp +# +# hlvariant/shogi/variant.cpp +# hlvariant/shogi/piece.cpp +# hlvariant/shogi/shogiactions.cpp +# +# hlvariant/minishogi/variant.cpp +# +# hlvariant/sho-shogi/variant.cpp +# +# hlvariant/tori-shogi/variant.cpp +# hlvariant/tori-shogi/piece.cpp - hlvariant/minishogi/variant.cpp - - hlvariant/sho-shogi/variant.cpp - - hlvariant/tori-shogi/variant.cpp - hlvariant/tori-shogi/piece.cpp - - animationfactory.cpp constrainedtext.cpp movelist.cpp infodisplay.cpp engineinfo.cpp - premove.cpp mainanimation.cpp random.cpp sprite.cpp pref_movelist.cpp option.cpp - graphicalapi.cpp graphicalsystem.cpp agentgroup.cpp graphicalgame.cpp @@ -96,7 +93,7 @@ set(tagua_SRC movelist_textual.cpp icsconnection.cpp mainwindow.cpp - board.cpp + chessboard.cpp common.cpp pgnparser.cpp movement.cpp @@ -107,13 +104,11 @@ set(tagua_SRC themeinfo.cpp namedsprite_utils.cpp icsgamedata.cpp - turnpolicy.cpp decoratedmove.cpp variants.cpp actioncollection.cpp tabwidget.cpp - - tagua_legacy.cpp + animationsettings.cpp ) # remove spurious xpressive warnings diff --git a/src/agent.h b/src/agent.h index c6efe3a..35faf38 100644 --- a/src/agent.h +++ b/src/agent.h @@ -11,7 +11,6 @@ #ifndef AGENT_H #define AGENT_H -#include "fwd.h" class Index; class Agent { diff --git a/src/agentgroup.h b/src/agentgroup.h index 43c2ce5..5b7b39a 100644 --- a/src/agentgroup.h +++ b/src/agentgroup.h @@ -14,7 +14,6 @@ #include #include #include "weakset.h" -#include "tagua.h" #include "agent.h" class AgentGroup { diff --git a/src/animation.h b/src/animation.h index e792490..4a3f570 100644 --- a/src/animation.h +++ b/src/animation.h @@ -18,8 +18,8 @@ #include #include +#include #include "sprite.h" -#include "fwd.h" class Movement; class Random; diff --git a/src/animationfactory.cpp b/src/animationfactory.cpp deleted file mode 100644 index 9f4b384..0000000 --- a/src/animationfactory.cpp +++ /dev/null @@ -1,202 +0,0 @@ -#include "animationfactory.h" - -#include -#include "animation.h" -#include "pointconverter.h" -#include "indexconverter.h" -#include "graphicalapi.h" -#include "mastersettings.h" - -namespace Common { - AnimationPtr appear(const AnimationSettings& s, const NamedSprite& sprite, Animate::AnimationType type) { - if (!s.fading) - type = Animate::Instant; - - switch (type) { - case Animate::Normal: - return AnimationPtr(new FadeAnimation(sprite.sprite(), 0, 255)); - case Animate::Instant: - default: - return AnimationPtr(new DropAnimation(sprite.sprite())); - } - } - - AnimationPtr disappear(const AnimationSettings& s, const NamedSprite& sprite, Animate::AnimationType type) { - if (!s.fading) - type = Animate::Instant; - - switch (type) { - case Animate::Normal: - return AnimationPtr(new FadeAnimation(sprite.sprite(), 255, 0)); - case Animate::Instant: - default: - return AnimationPtr(new CaptureAnimation(sprite.sprite())); - } - } -}; - -AnimationSettings::AnimationSettings() { - reload(); -} - -void AnimationSettings::reload() { - Settings s = settings().group("animations"); - - enabled = s.flag("enabled", true); - maxSequence = - s.group("sequence").flag("enabled", true) - ? s.group("sequence")["max"].value() - : 0; - movement = s["movement"].flag("enabled", true); - explode = s["explode"].flag("enabled", true); - fading = s["fading"].flag("enabled", true); - transform = s["transform"].flag("enabled", true); -} - -AnimationFactory::AnimationFactory(GraphicalAPI* api) -: m_api(api) { - m_group = AnimationGroupPtr(new AnimationGroup); -} - -AnimationGroupPtr AnimationFactory::group() const { - return m_group; -} - -void AnimationFactory::setGroup(const AnimationGroupPtr& group) { - m_group = group; -} - -void AnimationFactory::addPreAnimation(const Animate::Scheme& scheme, Animate::AnimationType type) { - m_group->addPreAnimation(m_api->animate(scheme, type)); -} - -void AnimationFactory::addPostAnimation(const Animate::Scheme& scheme, Animate::AnimationType type) { - m_group->addPostAnimation(m_api->animate(scheme, type)); -} - -AnimationFactory::operator AnimationGroupPtr() const { - return group(); -} - -namespace Animate { - - Scheme::~Scheme() { } - - move::move(const NamedSprite& sprite, const Point& to, int type) - : m_sprite(sprite) - , m_to(to) - , m_type(type) { } - - AnimationPtr move::run(const AnimationSettings& s, const PointConverter* converter, AnimationType type) const { - int mov_type = m_type; - if (!s.movement) - type = Instant; - else if (!s.transform) { - mov_type &= ~Rotating; - } - - switch (type) { - case Normal: { - MovementAnimation* mov; - QPoint destination = converter->toReal(m_to); - Point origin = converter->toLogical(m_sprite.sprite()->pos() + - Point(converter->squareSize(), converter->squareSize()) / 2); - if ((mov_type & LShaped) && origin != m_to) { - mov = new KnightMovementAnimation(m_sprite.sprite(), destination, mov_type & Rotating); - } - else { - mov = new MovementAnimation(m_sprite.sprite(), destination, mov_type & Rotating); - } - return AnimationPtr(mov); - } - case Instant: - default: - return AnimationPtr(new InstantAnimation(m_sprite.sprite(), converter->toReal(m_to))); - } - } - - appear::appear(const NamedSprite& sprite) - : m_sprite(sprite) { } - - AnimationPtr appear::run(const AnimationSettings& s, const PointConverter*, AnimationType type) const { - return Common::appear(s, m_sprite, type); - } - - disappear::disappear(const NamedSprite& sprite) - : m_sprite(sprite) { } - - AnimationPtr disappear::run(const AnimationSettings& s, const PointConverter*, AnimationType type) const { - return Common::disappear(s, m_sprite, type); - } - - destroy::destroy(const NamedSprite& sprite) - : m_sprite(sprite) { } - - AnimationPtr destroy::run(const AnimationSettings& s, const PointConverter*, AnimationType type) const { - if (!s.explode) { - return Common::disappear(s, m_sprite, type); - } - - switch (type) { - case Normal: - return AnimationPtr(new ExplodeAnimation(m_sprite.sprite(), Random::instance())); - case Instant: - default: - return AnimationPtr(new CaptureAnimation(m_sprite.sprite())); - } - } - - morph::morph(const NamedSprite& sprite, const NamedSprite& new_sprite) - : m_sprite(sprite) - , m_new_sprite(new_sprite) { } - - AnimationPtr morph::run(const AnimationSettings& s, const PointConverter*, AnimationType type) const { - if (!s.fading) - type = Instant; - - switch (type) { - case Normal: - return AnimationPtr(new CrossFadingAnimation(m_sprite.sprite(), m_new_sprite.sprite())); - case Instant: - default: - return AnimationPtr(new PromotionAnimation(m_sprite.sprite(), m_new_sprite.sprite())); - } - } - - namespace Pool { - - move::move(const NamedSprite& sprite, int to) - : m_sprite(sprite) - , m_to(to) { } - - AnimationPtr move::run(const AnimationSettings& s, const IndexConverter* converter, AnimationType type) const { - if (!s.movement) - type = Instant; - - switch (type) { - case Normal: - return AnimationPtr(new MovementAnimation(m_sprite.sprite(), converter->toReal(m_to))); - case Instant: - default: - return AnimationPtr(new InstantAnimation(m_sprite.sprite(), converter->toReal(m_to))); - } - } - - appear::appear(const NamedSprite& sprite) - : m_sprite(sprite) { } - - AnimationPtr appear::run(const AnimationSettings& s, const IndexConverter*, AnimationType type) const { - return Common::appear(s, m_sprite, type); - } - - disappear::disappear(const NamedSprite& sprite) - : m_sprite(sprite) { } - - AnimationPtr disappear::run(const AnimationSettings& s, const IndexConverter*, AnimationType type) const { - return Common::disappear(s, m_sprite, type); - } - - } - -} - diff --git a/src/animationfactory.h b/src/animationfactory.h deleted file mode 100644 index 0d5a6cd..0000000 --- a/src/animationfactory.h +++ /dev/null @@ -1,223 +0,0 @@ -/* - Copyright (c) 2007 Paolo Capriotti - (c) 2007 Maurizio Monge - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. -*/ - -#ifndef ANIMATIONFACTORY_H -#define ANIMATIONFACTORY_H - -#include "animation.h" -#include "fwd.h" -#include - -class NamedSprite; -class PointConverter; -class IndexConverter; -class GraphicalAPI; -class AnimationSettings; - -namespace Animate { - - /** - * AnimationType distinguishes between those animations which honor settings - * and those that are instantaneous regardless of user preferences. - */ - enum AnimationType { - Normal, /// Honor user settings. - Instant /// Instantaneous animation. - }; - - /** - * @brief A scheme of animations. - * - * Animation schemes are descriptions of an animation, i.e. what the animation - * should suggest to the user. The actual Animation class implementing the scheme - * depends of various factors, such as the AnimationType that the animator imposes - * and user settings. - */ - class Scheme { - public: - virtual ~Scheme(); - - /** - * Convert the scheme into an actual animation which can be enqueued in the - * animation system, or grouped into an AnimationGroup. - */ - virtual AnimationPtr run(const AnimationSettings& s, const PointConverter*, AnimationType) const = 0; - }; - -} - -struct AnimationSettings { - bool enabled; - - int maxSequence; - bool movement; - bool explode; - bool fading; - bool transform; - - AnimationSettings(); - void reload(); -}; - -/** - * @brief A convenience wrapper around an AnimationGroup, useful for Animators. - * - * An AnimationFactory is a wrapper around AnimationGroup that accepts shemes instead - * of Animations in its addPreAnimation() and addPostAnimation() methods. - * It offers a convenient syntax to an animator when composing animations into groups. - */ -class AnimationFactory { - AnimationGroupPtr m_group; - GraphicalAPI* m_api; -public: - AnimationFactory(GraphicalAPI* api); - - /** \return The wrapped animation group. */ - AnimationGroupPtr group() const; - - /** Change the wrapped animation group. */ - void setGroup(const AnimationGroupPtr& group); - - /** - * Add a pre-animation to the group. - * \param scheme The scheme producing the animation to be added. - * \param type The AnimationType to be used when creating the animation. - */ - void addPreAnimation(const Animate::Scheme& scheme, Animate::AnimationType type = Animate::Normal); - - /** - * Add a post-animation to the group. - * \param scheme The scheme producing the animation to be added. - * \param type The AnimationType to be used when creating the animation. - */ - void addPostAnimation(const Animate::Scheme& scheme, Animate::AnimationType type = Animate::Normal); - - /** - * Implicitly convert the object to an AnimationGroup shared pointer, - * using the group() member function. - */ - operator AnimationGroupPtr() const; -}; - -namespace Animate { - - /** - * @brief A movement animation scheme. - * - * Used to animate the movement of a piece on the board to a destination square. - */ - class move : public Scheme { - public: - enum MovementType { - Straight = 0x00, - Rotating = 0x01, - LShaped = 0x02 - }; - private: - const NamedSprite& m_sprite; - Point m_to; - int m_type; - public: - move(const NamedSprite& sprite, const Point& to, int type = Straight); - virtual AnimationPtr run(const AnimationSettings& s, const PointConverter* converter, AnimationType type) const; - }; - - /** - * @brief Appear animation scheme. - * - * The appear scheme visualizes a new piece coming into existence. It is - * used, for example, when retracting a capture in chess, to restore the captured - * piece on the chessboard. - */ - class appear : public Scheme { - const NamedSprite& m_sprite; - public: - appear(const NamedSprite& sprite); - virtual AnimationPtr run(const AnimationSettings& s, const PointConverter* converter, AnimationType type) const; - }; - - /** - * @brief Disappear animation scheme. - * - * The disappear scheme visualizes a piece being removed from the board. - */ - class disappear : public Scheme { - const NamedSprite& m_sprite; - public: - disappear(const NamedSprite& sprite); - virtual AnimationPtr run(const AnimationSettings& s, const PointConverter* converter, AnimationType type) const; - }; - - /** - * @brief Animation scheme destroying a piece. - * - * This animation scheme is used to destroy a piece, removing it from the - * board. It is similar to the disappear scheme, but can provide an additional - * effect like an explosion. - */ - class destroy : public Scheme { - const NamedSprite& m_sprite; - public: - destroy(const NamedSprite& sprite); - virtual AnimationPtr run(const AnimationSettings& s, const PointConverter* converter, AnimationType type) const; - }; - - /** - * @brief Animation scheme for changing a piece into another. - * - * The morph animation scheme is used when a piece changes, like in chess promotions. - */ - class morph : public Scheme { - const NamedSprite& m_sprite; - const NamedSprite& m_new_sprite; - public: - morph(const NamedSprite& sprite, const NamedSprite& new_sprite); - virtual AnimationPtr run(const AnimationSettings& s, const PointConverter* converter, AnimationType type) const; - }; - - /** - * Animations of the pool. - */ - namespace Pool { - - class Scheme { - public: - virtual ~Scheme() { } - virtual AnimationPtr run(const AnimationSettings& s, const IndexConverter* converter, AnimationType) const = 0; - }; - - class move : public Scheme { - const NamedSprite& m_sprite; - int m_to; - public: - move(const NamedSprite& sprite, int to); - virtual AnimationPtr run(const AnimationSettings& s, const IndexConverter*, AnimationType type) const; - }; - - class appear : public Scheme { - const NamedSprite& m_sprite; - public: - appear(const NamedSprite& sprite); - virtual AnimationPtr run(const AnimationSettings& s, const IndexConverter*, AnimationType type) const; - }; - - class disappear : public Scheme { - const NamedSprite& m_sprite; - public: - disappear(const NamedSprite& sprite); - virtual AnimationPtr run(const AnimationSettings& s, const IndexConverter*, AnimationType type) const; - }; - } - - -} - - -#endif // ANIMATIONFACTORY_H diff --git a/src/animationsettings.cpp b/src/animationsettings.cpp new file mode 100644 index 0000000..a76bc97 --- /dev/null +++ b/src/animationsettings.cpp @@ -0,0 +1,29 @@ +/* + Copyright (c) 2007 Paolo Capriotti + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#include "animationsettings.h" +#include "mastersettings.h" + +AnimationSettings::AnimationSettings() { + reload(); +} + +void AnimationSettings::reload() { + Settings s = settings().group("animations"); + + enabled = s.flag("enabled", true); + maxSequence = + s.group("sequence").flag("enabled", true) + ? s.group("sequence")["max"].value() + : 0; + movement = s["movement"].flag("enabled", true); + explode = s["explode"].flag("enabled", true); + fading = s["fading"].flag("enabled", true); + transform = s["transform"].flag("enabled", true); +} diff --git a/src/graphicalapi.cpp b/src/animationsettings.h similarity index 54% copy from src/graphicalapi.cpp copy to src/animationsettings.h index 374cb61..fb19f75 100644 --- a/src/graphicalapi.cpp +++ b/src/animationsettings.h @@ -1,6 +1,5 @@ /* Copyright (c) 2007 Paolo Capriotti - (c) 2007 Maurizio Monge This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -8,7 +7,20 @@ (at your option) any later version. */ -#include "graphicalapi.h" +#ifndef ANIMATIONSETTINGS_H +#define ANIMATIONSETTINGS_H -GraphicalAPI::~GraphicalAPI() { } +struct AnimationSettings { + bool enabled; + + int maxSequence; + bool movement; + bool explode; + bool fading; + bool transform; + + AnimationSettings(); + void reload(); +}; +#endif // ANIMATIONSETTINGS_H diff --git a/src/board.cpp b/src/chessboard.cpp similarity index 80% rename from src/board.cpp rename to src/chessboard.cpp index 793a071..292b029 100644 --- a/src/board.cpp +++ b/src/chessboard.cpp @@ -14,14 +14,16 @@ #include #include +#include +#include + #include "mastersettings.h" -#include "board.h" +#include "chessboard.h" #include "sprite.h" #include "animation.h" #include "pointconverter.h" #include "entities/userentity.h" #include "mainanimation.h" -#include "premove.h" #include "constrainedtext.h" using namespace boost; @@ -30,7 +32,9 @@ using namespace boost; class BoardTags : public std::map > > { }; -Board::Board(const AnimationSettings& animSettings, KGameCanvasAbstract* parent) +ChessBoard::ChessBoard(Variant* variant, + const AnimationSettings& animSettings, + KGameCanvasAbstract* parent) : ClickableCanvas(parent) , m_flipped(false) , m_square_size(0) @@ -39,7 +43,7 @@ Board::Board(const AnimationSettings& animSettings, KGameCanvasAbstract* parent) , m_hinting_pos(Point::invalid()) , selection(Point::invalid()) , lastSelection(Point::invalid()) -, m_dropped_pool(-1) +, m_dropped_pool(0) , m_dropped_index(-1) , m_anim_settings(animSettings) { @@ -59,10 +63,12 @@ Board::Board(const AnimationSettings& animSettings, KGameCanvasAbstract* parent) m_pieces_group = new KGameCanvasGroup(this); m_pieces_group->show(); + m_namer = requestComponent(variant, "namer"); + settingsChanged(); } -Board::~Board() { +ChessBoard::~ChessBoard() { delete m_pieces_group; while(!m_canvas_background->items()->isEmpty()) @@ -76,7 +82,7 @@ Board::~Board() { delete m_main_animation; } -void Board::settingsChanged() { +void ChessBoard::settingsChanged() { Settings s_anim = settings().group("animations"); int speed = (s_anim["speed"] | 16); int smoothness = (s_anim["smoothness"] | 16); @@ -89,7 +95,7 @@ void Board::settingsChanged() { recreateBorder(); } -void Board::updateBackground() { +void ChessBoard::updateBackground() { while(!m_canvas_background->items()->isEmpty()) delete m_canvas_background->items()->first(); @@ -112,11 +118,11 @@ void Board::updateBackground() { } } -void Board::enqueue(const shared_ptr& anim) { +void ChessBoard::enqueue(const shared_ptr& anim) { m_main_animation->addAnimation(anim); } -void Board::adjustSprite(const Point& p, bool immediate) { +void ChessBoard::adjustSprite(const Point& p, bool immediate) { SpritePtr sprite = m_sprites[p].sprite(); if(!sprite) @@ -129,7 +135,7 @@ void Board::adjustSprite(const Point& p, bool immediate) { ); } -boost::shared_ptr Board::addTag(const QString& name, Point pt, bool over) { +boost::shared_ptr ChessBoard::addTag(const QString& name, Point pt, bool over) { if(!m_sprites.valid(pt)) return boost::shared_ptr(); @@ -147,15 +153,15 @@ boost::shared_ptr Board::addTag(const QString& name, Point pt return item; } -void Board::clearTags(const QString& name) { +void ChessBoard::clearTags(const QString& name) { m_tags->erase(name); } -void Board::clearTags() { +void ChessBoard::clearTags() { m_tags->clear(); } -void Board::setTags(const QString& name, Point p1, Point p2, Point p3, +void ChessBoard::setTags(const QString& name, Point p1, Point p2, Point p3, Point p4, Point p5, Point p6 ) { //TODO: maybe this could be optimized a bit clearTags(name); @@ -167,7 +173,7 @@ void Board::setTags(const QString& name, Point p1, Point p2, Point p3, addTag(name, p6); } -void Board::recreateBorder() { +void ChessBoard::recreateBorder() { m_border_text.clear(); while(!m_canvas_border_text->items()->isEmpty()) delete m_canvas_border_text->items()->first(); @@ -207,7 +213,7 @@ void Board::recreateBorder() { updateBorder(); } -void Board::updateBorder() { +void ChessBoard::updateBorder() { while(!m_canvas_border->items()->isEmpty()) delete m_canvas_border->items()->first(); @@ -256,37 +262,37 @@ void Board::updateBorder() { } } -void Board::createGrid(Point p, const QStringList& border_coords) { +void ChessBoard::createGrid(Point p, const QStringList& border_coords) { m_border_coords = border_coords; m_sprites = PieceGrid(p.x,p.y); recreateBorder(); } -QPixmap Board::loadSprite(const QString& id) { +QPixmap ChessBoard::loadSprite(const QString& id) { return m_loader.piecePixmap(id, m_flipped); } -QRect Board::computeRect(Point p) const { +QRect ChessBoard::computeRect(Point p) const { QPoint realPoint = converter()->toReal(p); return squareRect(realPoint.x(), realPoint.y()); } -QRect Board::squareRect(int x, int y) const { +QRect ChessBoard::squareRect(int x, int y) const { return QRect(x, y, m_square_size, m_square_size); } -QRegion Board::computeRegion(Point p) const { +QRegion ChessBoard::computeRegion(Point p) const { return QRegion(computeRect(p)); } // selection -void Board::setSelection(const Point& p) { +void ChessBoard::setSelection(const Point& p) { lastSelection = selection; selection = p; setTags("selection", p); } -void Board::cancelSelection() { +void ChessBoard::cancelSelection() { lastSelection = selection; selection = Point::invalid(); clearTags("selection"); @@ -294,29 +300,19 @@ void Board::cancelSelection() { // premove -void Board::setPremove(const NormalUserMove& premove) { - m_premove_from = premove.from; - m_premove_to = premove.to; +void ChessBoard::setPremove(const Move& premove) { + m_premove_from = premove.src(); + m_premove_to = premove.dst(); setTags("premove", m_premove_from, m_premove_to); } -void Board::setPremove(const DropUserMove& premove) { - m_premove_from = Point::invalid(); - m_premove_to = premove.to; - setTags("premove", m_premove_to); -} - -void Board::setPremove(const Premove& premove) { - setPremove(premove.toUserMove()); -} - -void Board::cancelPremove() { +void ChessBoard::cancelPremove() { m_premove_from = Point::invalid(); m_premove_to = Point::invalid(); clearTags("premove"); } -void Board::updateSprites() { +void ChessBoard::updateSprites() { // adjust piece positions for (Point i = m_sprites.first(); i <= m_sprites.last(); i = m_sprites.next(i)) { boost::shared_ptr p = m_sprites[i].sprite(); @@ -329,7 +325,7 @@ void Board::updateSprites() { } } -void Board::updateTags() { +void ChessBoard::updateTags() { if(!m_square_size) return; @@ -342,11 +338,10 @@ void Board::updateTags() { } -bool Board::doMove(const NormalUserMove& m) { - if (m_entity.lock()->oneClickMoves() || m_entity.lock()->validTurn(m.from) == Moving) { - AbstractMove::Ptr mv = m_entity.lock()->testMove(m); - if (mv) { - m_entity.lock()->executeMove(mv); +bool ChessBoard::doMove(Move& m) { + if (m_entity.lock()->oneClickMoves() || m_entity.lock()->validTurn(m.src()) == Moving) { + if (m_entity.lock()->testMove(m)) { + m_entity.lock()->executeMove(m); return true; } } @@ -358,7 +353,7 @@ bool Board::doMove(const NormalUserMove& m) { } -void Board::onResize(int new_size, int border_size, int border_text_near, +void ChessBoard::onResize(int new_size, int border_size, int border_text_near, int border_text_far, bool force_reload) { if(m_square_size == new_size && !force_reload) return; @@ -390,7 +385,7 @@ void Board::onResize(int new_size, int border_size, int border_text_near, updateBorder(); } -void Board::onMousePress(const QPoint& pos, int button) { +void ChessBoard::onMousePress(const QPoint& pos, int button) { Point point = converter()->toLogical(pos); if (!m_sprites.valid(point)) return; @@ -399,7 +394,7 @@ void Board::onMousePress(const QPoint& pos, int button) { if (button == Qt::LeftButton) { if (m_entity.lock()->oneClickMoves()) { - NormalUserMove m = m_entity.lock()->createMove(Point::invalid(), point); + Move m(Point::invalid(), point); doMove(m); } else { @@ -416,7 +411,7 @@ void Board::onMousePress(const QPoint& pos, int button) { else if (selection != Point::invalid()) { piece = m_sprites[selection].sprite(); Q_ASSERT(piece); - NormalUserMove m = m_entity.lock()->createMove(selection, point); + Move m(selection, point); switch(m_entity.lock()->validTurn(selection)) { @@ -454,7 +449,7 @@ void Board::onMousePress(const QPoint& pos, int button) { //END right click } -void Board::onMouseRelease(const QPoint& pos, int button) { +void ChessBoard::onMouseRelease(const QPoint& pos, int button) { Point point = converter()->toLogical(pos); //BEGIN left click @@ -478,12 +473,12 @@ void Board::onMouseRelease(const QPoint& pos, int button) { } else { - NormalUserMove m = m_entity.lock()->createMove(m_drag_info->from, point); + Point dst = point; if (!m_sprites.valid(point)) - m.to = Point::invalid(); - - switch(m_entity.lock()->validTurn(m_drag_info->from)) { + dst = Point::invalid(); + Move m(m_drag_info->from, dst); + switch (m_entity.lock()->validTurn(m.src())) { case Moving: if (doMove(m)) moved = true; @@ -520,7 +515,7 @@ void Board::onMouseRelease(const QPoint& pos, int button) { //END left button } -void Board::onMouseMove(const QPoint& pos, int /*button*/) { +void ChessBoard::onMouseMove(const QPoint& pos, int /*button*/) { Point point = converter()->toLogical(pos); if (m_drag_info) { @@ -536,10 +531,10 @@ void Board::onMouseMove(const QPoint& pos, int /*button*/) { m_drag_info->sprite->moveTo(pos - QPoint(m_square_size / 2, m_square_size / 2) ); // highlight valid moves - NormalUserMove move = m_entity.lock()->createMove(m_drag_info->from, point); + Move move(m_drag_info->from, point); bool valid = m_sprites.valid(point); if (valid) { - InteractionType action = m_entity.lock()->validTurn(m_drag_info->from); + InteractionType action = m_entity.lock()->validTurn(move.src()); if (action == Moving) valid = m_entity.lock()->testMove(move); } @@ -553,11 +548,11 @@ void Board::onMouseMove(const QPoint& pos, int /*button*/) { if(point == m_hinting_pos) return; - AbstractPiece::Ptr hint; + Piece hint; if (m_sprites.valid(point)) { - if (AbstractMove::Ptr move = m_entity.lock()->testMove( - m_entity.lock()->createMove(Point::invalid(), point))) { + Move move(Point::invalid(), point); + if (m_entity.lock()->testMove(move)) { // set move hint hint = m_entity.lock()->moveHint(move); } @@ -567,29 +562,31 @@ void Board::onMouseMove(const QPoint& pos, int /*button*/) { } } -void Board::onPositionChanged() { +void ChessBoard::onPositionChanged() { if (m_entity.lock() && m_entity.lock()->oneClickMoves() && m_sprites.valid(m_hinting_pos)) { - AbstractPiece::Ptr hint; + Piece hint; - if (AbstractMove::Ptr move = m_entity.lock()->testMove( - m_entity.lock()->createMove(Point::invalid(), m_hinting_pos))) { + Move move(Point::invalid(), m_hinting_pos); + if (m_entity.lock()->testMove(move)) { // set move hint hint = m_entity.lock()->moveHint(move); } - updateHinting(m_hinting_pos, hint); + updateHinting(move.dst(), hint); } } -void Board::onMouseLeave() { - updateHinting(Point::invalid(), AbstractPiece::Ptr()); +void ChessBoard::onMouseLeave() { + updateHinting(Point::invalid(), Piece()); } -void Board::updateHinting(Point pt, AbstractPiece::Ptr piece) { +void ChessBoard::updateHinting(Point pt, Piece piece) { if(!m_sprites.valid(pt)) - piece = AbstractPiece::Ptr(); + piece = Piece(); + - if(!piece || !m_sprites.valid(pt)) { + QString piece_name = m_namer->name(piece); + if(piece != Piece() || !m_sprites.valid(pt)) { if(m_hinting.sprite()) { if(1 /*BROKEN m_anim_fade*/) enqueue( boost::shared_ptr(new FadeAnimation(m_hinting.sprite(), @@ -603,9 +600,9 @@ void Board::updateHinting(Point pt, AbstractPiece::Ptr piece) { } else { if(pt == m_hinting_pos) { - if(!(piece->name() == m_hinting.name())) { - m_hinting = NamedSprite(piece->name(), m_hinting.sprite()); - m_hinting.sprite()->setPixmap(loadSprite(piece->name())); + if (piece_name != m_hinting.name()) { + m_hinting = NamedSprite(piece_name, m_hinting.sprite()); + m_hinting.sprite()->setPixmap(loadSprite(piece_name)); } } else { @@ -617,14 +614,14 @@ void Board::updateHinting(Point pt, AbstractPiece::Ptr piece) { enqueue( boost::shared_ptr(new CaptureAnimation(m_hinting.sprite())) ); } - QPixmap pix = loadSprite(piece->name()); + QPixmap pix = loadSprite(piece_name); SpritePtr sprite(new Sprite(pix, piecesGroup(), converter()->toReal(pt))); sprite->setOpacity(160); sprite->raise(); sprite->show(); m_hinting_pos = pt; - m_hinting = NamedSprite(piece->name(), sprite); + m_hinting = NamedSprite(piece_name, sprite); /*if(m_anim_fade) enqueue( boost::shared_ptr(new FadeAnimation(m_hinting.sprite(), @@ -637,14 +634,14 @@ void Board::updateHinting(Point pt, AbstractPiece::Ptr piece) { } } -void Board::reset() { +void ChessBoard::reset() { clearTags(); cancelSelection(); cancelPremove(); m_main_animation->stop(); } -void Board::flip(bool flipped) +void ChessBoard::flip(bool flipped) { if (m_flipped != flipped) { m_flipped = flipped; @@ -663,15 +660,14 @@ void Board::flip(bool flipped) } } -void Board::draggingOn(int pool, int index, const QPoint& point) { +void ChessBoard::draggingOn(const IColor* pool, int index, const QPoint& point) { Point to = converter()->toLogical(point); if (m_sprites.valid(to)) - switch(m_entity.lock()->validTurn(pool)) { + switch (m_entity.lock()->validTurn(pool)) { case Moving: { - DropUserMove m = m_entity.lock()->createDrop(pool, index, to); - AbstractMove::Ptr mv = m_entity.lock()->testMove(m); - if (mv) { + Move m(pool, index, to); + if (m_entity.lock()->testMove(m)) { setTags("validmove", to); return; } @@ -689,7 +685,7 @@ void Board::draggingOn(int pool, int index, const QPoint& point) { clearTags("validmove"); } -bool Board::dropOn(int pool, int index, const QPoint& point) { +bool ChessBoard::dropOn(const IColor* pool, int index, const QPoint& point) { Point to = converter()->toLogical(point); if (!m_sprites.valid(to)) @@ -697,20 +693,19 @@ bool Board::dropOn(int pool, int index, const QPoint& point) { clearTags("validmove"); - switch(m_entity.lock()->validTurn(pool)) { + switch (m_entity.lock()->validTurn(pool)) { case Moving: { - DropUserMove m = m_entity.lock()->createDrop(pool, index, to); - AbstractMove::Ptr mv = m_entity.lock()->testMove(m); - if (mv) { - m_entity.lock()->executeMove(mv); + Move m(pool, index, to); + if (m_entity.lock()->testMove(m)) { + m_entity.lock()->executeMove(m); return true; } break; } case Premoving: { - DropUserMove m = m_entity.lock()->createDrop(pool, index, to); + Move m(pool, index, to); if (m_entity.lock()->testPremove(m)) { m_entity.lock()->addPremove(m); setPremove(m); diff --git a/src/board.h b/src/chessboard.h similarity index 92% rename from src/board.h rename to src/chessboard.h index 9eafaed..7f4084c 100644 --- a/src/board.h +++ b/src/chessboard.h @@ -8,16 +8,20 @@ (at your option) any later version. */ -#ifndef BOARD_H -#define BOARD_H +#ifndef CHESSBOARD_H +#define CHESSBOARD_H #include #include - #include #include +#include #include -#include "kgamecanvas.h" + +#include +#include +#include + #include "entities/userentity.h" #include "common.h" #include "grid.h" @@ -26,15 +30,17 @@ #include "pointconverter.h" #include "pixmaploader.h" #include "clickablecanvas.h" -#include -class DragInfo; class Animation; -class UserEntity; -class ConstrainedText; class AnimationSettings; - class BoardTags; +class ConstrainedText; +class DragInfo; +class IColor; +class INamer; +class UserEntity; +class Variant; + typedef boost::shared_ptr BoardTagsPtr; /** @@ -45,7 +51,7 @@ typedef boost::shared_ptr BoardTagsPtr; * You can set custom tags for each square. * @sa Table, @sa PiecePool, @sa Sprite */ -class Board : public QObject, public ClickableCanvas, protected PointConverter { +class ChessBoard : public QObject, public ClickableCanvas, protected PointConverter { Q_OBJECT public: @@ -150,10 +156,12 @@ private: QStringList m_border_coords; - int m_dropped_pool; + const IColor* m_dropped_pool; int m_dropped_index; const AnimationSettings& m_anim_settings; + + INamer* m_namer; /** the text items for the border */ std::vector m_border_text; @@ -167,7 +175,7 @@ private: /** this internal function creates a new border, if needed */ void recreateBorder(); - void updateHinting(Point pt, AbstractPiece::Ptr piece); + void updateHinting(Point pt, Piece piece); /** this internal function updates the sprites after the board has been resized */ void updateSprites(); @@ -175,9 +183,7 @@ private: /** this internal function updates the tags after the board has been resized */ void updateTags(); - void setPremove(const NormalUserMove& move); - void setPremove(const DropUserMove& move); - void setPremove(const class Premove& move); + void setPremove(const Move& move); void updatePremove(); void setSelection(const Point& p); @@ -186,12 +192,12 @@ private: QRect squareRect(int x, int y) const; /** this function tries to notify the move to the entity */ - bool doMove(const NormalUserMove&); + bool doMove(Move&); public: /** constructor, requires the canvas parent */ - Board(const AnimationSettings& animSettings, KGameCanvasAbstract* parent); - ~Board(); + ChessBoard(Variant* variant, const AnimationSettings& animSettings, KGameCanvasAbstract* parent); + ~ChessBoard(); /** set the board flipping */ void flip(bool flipped); @@ -271,10 +277,10 @@ public: /** Notifies to the board that a certain piece is being dragged over the board */ - void draggingOn(int pool, int index, const QPoint& p); + void draggingOn(const IColor* pool, int index, const QPoint& p); /** Executes a drop. This function id typically called by by a PiecePool */ - bool dropOn(int pool, int index, const QPoint& point); + bool dropOn(const IColor* pool, int index, const QPoint& point); /** mouse release event */ virtual void onMouseRelease(const QPoint& pos, int button); @@ -305,4 +311,4 @@ Q_SIGNALS: void error(ErrorCode code); }; -#endif //BOARD_H +#endif // CHESSBOARD_H diff --git a/src/chesstable.cpp b/src/chesstable.cpp index c7f13aa..3e32334 100644 --- a/src/chesstable.cpp +++ b/src/chesstable.cpp @@ -12,6 +12,10 @@ #include #include #include + +#include +#include + #include "chesstable.h" #include "game.h" #include "gameinfo.h" @@ -24,7 +28,7 @@ using namespace boost; -ChessTable::ChessTable(QWidget* parent) +ChessTable::ChessTable(Variant* variant, QWidget* parent) : KGameCanvasWidget(parent) , m_wallpaper(NULL) , m_current(NULL) @@ -34,7 +38,7 @@ ChessTable::ChessTable(QWidget* parent) setMouseTracking(true); // create m_board - m_board = new Board(m_anim_settings, this); + m_board = new ChessBoard(variant, m_anim_settings, this); m_board->show(); // create move list @@ -52,8 +56,11 @@ ChessTable::ChessTable(QWidget* parent) // m_info->show(); // create pools - for(int i=0;i<2;i++) { - m_pools[i] = new PiecePool(i, m_board, m_anim_settings, this); + // FIXME what to do for more than 2 players? + for (int i = 0; i < 2; i++) { + const IColor* color = requestComponent( + variant, "player/" + QString::number(i)); + m_pools[i] = new PiecePool(color, m_board, m_anim_settings, this); m_pools[i]->show(); } diff --git a/src/chesstable.h b/src/chesstable.h index 50a3be5..c74ed3c 100644 --- a/src/chesstable.h +++ b/src/chesstable.h @@ -11,13 +11,11 @@ #ifndef CHESSTABLE_H #define CHESSTABLE_H -#include - -#include "kgamecanvas.h" +#include +#include "animationsettings.h" #include "positioninfo.h" -#include "board.h" +#include "chessboard.h" #include "common.h" -#include "animationfactory.h" class PiecePool; class Player; @@ -35,7 +33,7 @@ class ChessTable : public KGameCanvasWidget { KGameCanvasTiledPixmap* m_wallpaper; Clock* m_clocks[2]; - Board* m_board; + ChessBoard* m_board; PiecePool* m_pools[2]; MoveList::Table* m_movelist; InfoDisplay* m_info; @@ -50,10 +48,10 @@ class ChessTable : public KGameCanvasWidget { AnimationSettings m_anim_settings; public: - ChessTable(QWidget* parent = 0); + explicit ChessTable(Variant* variant, QWidget* parent = 0); ~ChessTable(); - inline Board* board() const { return m_board; } + inline ChessBoard* board() const { return m_board; } inline PiecePool* pool(int index) const { return m_pools[index]; } inline MoveList::Table* moveListTable() const { return m_movelist; } inline Clock* clock(int index) const { return m_clocks[index]; } diff --git a/src/controllers/abstract.h b/src/controllers/abstract.h index 8e7b7cc..e08904c 100644 --- a/src/controllers/abstract.h +++ b/src/controllers/abstract.h @@ -12,17 +12,18 @@ #define CONTROLLER_H #include -#include +#include +#include #include "common.h" #include "entitytoken.h" -#include "fwd.h" +class ActionCollection; class ChessTable; -class UserEntity; class Engine; +class IColor; class PGN; -class ActionCollection; class UI; +class UserEntity; /** * @b Controller is the base abstract class for all controllers in Tagua. @@ -90,7 +91,7 @@ public: * @return current position, or a null shared_ptr if no current position * concept is defined for this controller. */ - virtual PositionPtr currentPosition() const { return PositionPtr(); } + virtual StatePtr currentPosition() const { return StatePtr(); } /** * Change turn. Used for example in edit position mode. @@ -164,7 +165,8 @@ public: * @return a token which uniquely identifies the engine entity * and can be used to remove it from the entity list. */ - virtual EntityToken addPlayingEngine(int /*side*/, const boost::shared_ptr&) { return EntityToken(); } + virtual EntityToken addPlayingEngine(const IColor* /*side*/, + const boost::shared_ptr&) { return EntityToken(); } /** * Remove an entity from the controller list. diff --git a/src/controllers/editgame.cpp b/src/controllers/editgame.cpp index 45b4174..2077827 100644 --- a/src/controllers/editgame.cpp +++ b/src/controllers/editgame.cpp @@ -10,6 +10,14 @@ #include "editgame.h" +#include + +#include +#include +#include +#include +#include + #include "graphicalgame.h" #include "graphicalsystem.h" #include "xboardengine.h" @@ -42,7 +50,7 @@ public: if (index.totalNumMoves() >= 2) m_view->run(); - m_view->changeClock(m_game->position(index)->turn()); + m_view->changeClock(m_game->position(index)->turn()->index()); } virtual void notifyBack() { } virtual void notifyForward() { } @@ -71,22 +79,23 @@ public: }; EditGameController::EditGameController(ChessTable* view, - const VariantPtr& variant, - AbstractPosition::Ptr startingPosition) + Variant* variant, + const StatePtr& startingPosition) : Controller(view) , m_variant(variant) { - AbstractPosition::Ptr position; - if (!startingPosition) { - position = m_variant->createPosition(); - position->setup(); + StatePtr state = startingPosition; + if (!state) { + IStateFactory* factory = requestComponent( + m_variant, "state_factory"); + state = StatePtr(factory->createState()); + state->setup(); } - else position = startingPosition; - m_graphical = shared_ptr(new GraphicalSystem(m_view, position, m_variant)); + m_graphical = shared_ptr(new GraphicalSystem(m_view, state, m_variant)); m_game = shared_ptr(new GraphicalGame(m_graphical.get(), m_view->moveListTable())); - m_game->reset(position); + m_game->reset(state); m_entity = shared_ptr(new GameEntity(m_variant, m_game, m_view->board(), &m_agents)); m_entity->enableEditingTools(true); @@ -94,17 +103,18 @@ EditGameController::EditGameController(ChessTable* view, m_graphical->setup(m_entity); m_game->setEntity(m_entity); - - init(AbstractPosition::Ptr()); + init(state); } EditGameController::~EditGameController() { } -void EditGameController::init(AbstractPosition::Ptr startingPosition) { - m_players[0] = m_entity; - m_players[1] = m_entity; - m_entity->turnTest().setSimplePolicy(0, true); - m_entity->turnTest().setSimplePolicy(1, true); +void EditGameController::init(const StatePtr& startingPosition) { + QList colors = m_variant->repository()->listComponents("player"); + foreach (Component* color, colors) { + IColor* c = requestInterface(color); + m_players[c] = m_entity; + m_entity->turnTest().setSimplePolicy(c, true); + } if (startingPosition) { // TODO update to the starting position @@ -121,21 +131,17 @@ void EditGameController::init(AbstractPosition::Ptr startingPosition) { } ActionCollection* EditGameController::variantActions() const { - return m_variant->actions(); + return 0; // FIXME restore variant actions + // return m_variant->actions(); } QString EditGameController::variant() const { - PositionPtr pos = m_game->position(); - if (pos) - return pos->variant(); - else - return QString(); + return m_variant->name(); } void EditGameController::onNavigation() { } -EntityToken EditGameController::addPlayingEngine(int side, const shared_ptr& engine) { - Q_ASSERT(side == 0 || side == 1); +EntityToken EditGameController::addPlayingEngine(const IColor* side, const shared_ptr& engine) { if (!engine) return EntityToken(); @@ -159,28 +165,15 @@ EntityToken EditGameController::addPlayingEngine(int side, const shared_ptr& engine) { -// Q_ASSERT(engine.use_count() == 1); -// shared_ptr entity(new EngineEntity(m_variant, m_game, -1, engine, &m_agents)); -// m_agents.addAgent(entity); -// EntityToken res(m_entities.insert(entity).first); -// -// engine->setNotifier(entity); -// engine->start(); -// engine->setBoard(m_game->position(), 0, 0); -// engine->startAnalysis(); -// -// return res; -// } - void EditGameController::removeEntity(const EntityToken& token) { shared_ptr entity = token.entity(); if (entity) { // check players - for (int i = 0; i < 2; i++) { - if (entity == m_players[i]) { - m_players[i] = m_entity; - m_entity->turnTest().setSimplePolicy(i, true); + for (Players::iterator it = m_players.begin(), end = m_players.end(); + it != end; ++it) { + if (entity == it->second) { + it->second = m_entity; + m_entity->turnTest().setSimplePolicy(it->first, true); } } @@ -193,8 +186,8 @@ void EditGameController::removeEntity(const EntityToken& token) { std::cout << "there are " << m_entities.size() << " entities left" << std::endl; } -bool EditGameController::addICSPlayer(int side, int game_number, const shared_ptr& connection) { - Q_ASSERT(side == 0 || side == 1); +bool EditGameController::addICSPlayer(const IColor* side, int game_number, + const shared_ptr& connection) { if (m_players[side]->canDetach()) { shared_ptr entity(new ICSEntity(m_variant, m_game, side, game_number, connection, &m_agents)); @@ -207,7 +200,8 @@ bool EditGameController::addICSPlayer(int side, int game_number, const shared_pt connection->setListener(game_number, entity); entity->setupTurnTest(m_entity->turnTest()); - m_view->flip(m_players[1] == m_entity); // flip if we're black! + // FIXME: flip if we are black when playing in ICS +// m_view->flip(m_players[1] == m_entity); // flip if we're black! m_agents.addAgent(m_clock_agent); } @@ -225,43 +219,45 @@ bool EditGameController::addICSPlayer(int side, int game_number, const shared_pt } bool EditGameController::setExaminationMode(int game_number, const shared_ptr& connection) { - std::cout << "[controller " << this << "] setting examination mode" << std::endl; - if (m_players[0]->canDetach() && - m_players[1]->canDetach()) { + kDebug() << "setting examination mode"; + + if (detachAllPlayerEntities()) { shared_ptr entity(new ExaminationEntity(m_variant, m_game, game_number, connection, &m_agents)); if (entity->attach()) { m_entity = entity; m_view->setEntity(entity); m_agents.addAgent(entity); - m_players[0] = entity; - m_players[1] = entity; - + for (Players::iterator it = m_players.begin(), end = m_players.end(); + it != end; ++it) { + it->second = entity; + } + connection->setListener(game_number, entity); m_view->flip(false); m_entity->turnTest().clear(); return true; } - else - std::cout << "** could not attach examination entity **" << std::endl; + else { + kWarning() << "Could not attach examination entity"; + } } - else - std::cout << "** could not detach entity **" << std::endl; return false; } bool EditGameController::setObserveMode(int game_number, const shared_ptr& connection) { - if (m_players[0]->canDetach() && - m_players[1]->canDetach()) { + if (detachAllPlayerEntities()) { shared_ptr entity(new ObservingEntity(m_variant, m_game, game_number, connection, &m_agents)); if (entity->attach()) { m_agents.addAgent(entity); - m_players[0] = entity; - m_players[1] = entity; + for (Players::iterator it = m_players.begin(), end = m_players.end(); + it != end; ++it) { + it->second = entity; + } connection->setListener(game_number, entity); m_view->flip(false); @@ -269,10 +265,10 @@ bool EditGameController::setObserveMode(int game_number, const shared_ptrdetach(); - m_players[0]->detach(); - m_players[1]->detach(); + detachAllPlayerEntities(); foreach (shared_ptr entity, m_entities) entity->detach(); @@ -308,11 +303,12 @@ shared_ptr EditGameController::end() { } // return to edit game mode - m_players[0] = m_entity; - m_players[1] = m_entity; - m_entity->turnTest().setSimplePolicy(0, true); - m_entity->turnTest().setSimplePolicy(1, true); - + for (Players::iterator it = m_players.begin(), end = m_players.end(); + it != end; ++it) { + it->second = m_entity; + m_entity->turnTest().setSimplePolicy(it->first, true); + } + return Controller::end(); } @@ -328,3 +324,16 @@ void EditGameController::setUI(UI& ui) { void EditGameController::activate() { m_game->onActionStateChange(); } + + +bool EditGameController::detachAllPlayerEntities() { + for (Players::iterator it = m_players.begin(), end = m_players.end(); + it != end; ++it) { + if (!it->second->canDetach()) { + kWarning() << "Could not detach entity" << it->second.get(); + return false; + } + } + + return true; +} diff --git a/src/controllers/editgame.h b/src/controllers/editgame.h index 54437cd..9290989 100644 --- a/src/controllers/editgame.h +++ b/src/controllers/editgame.h @@ -11,18 +11,22 @@ #ifndef EDITGAMECONTROLLER_H #define EDITGAMECONTROLLER_H +#include #include +#include +#include #include "abstract.h" #include "agentgroup.h" -#include "tagua.h" class Entity; class Engine; class Agent; +class IColor; class ICSConnection; class UserEntity; class GraphicalGame; class GraphicalSystem; +class Variant; /** @@ -36,31 +40,35 @@ class GraphicalSystem; * should register it as an agent, adding it to @code m_agents. */ class EditGameController : public Controller { - void init(AbstractPosition::Ptr); + void init(const StatePtr& state); boost::shared_ptr m_clock_agent; boost::shared_ptr m_update_agent; + typedef std::map > Players; protected: virtual void onNavigation(); boost::shared_ptr m_entity; boost::shared_ptr m_graphical; AgentGroup m_agents; - VariantPtr m_variant; + Variant* m_variant; boost::shared_ptr m_game; virtual boost::shared_ptr entity() const { return m_entity; } - boost::shared_ptr m_players[2]; + Players m_players; std::set > m_entities; + + bool detachAllPlayerEntities(); public: - EditGameController(ChessTable*, const VariantPtr& variant, - AbstractPosition::Ptr startingPos = AbstractPosition::Ptr()); + EditGameController(ChessTable*, Variant* variant, + const StatePtr& startingPos = StatePtr()); ~EditGameController(); virtual ActionCollection* variantActions() const; virtual QString variant() const; - EntityToken addPlayingEngine(int side, const boost::shared_ptr& engine); + EntityToken addPlayingEngine(const IColor* side, const boost::shared_ptr& engine); // EntityToken addAnalysingEngine(const boost::shared_ptr& engine); void removeEntity(const EntityToken& token); - bool addICSPlayer(int side, int game_number, const boost::shared_ptr& connection); + bool addICSPlayer(const IColor* side, int game_number, + const boost::shared_ptr& connection); bool setExaminationMode(int game_number, const boost::shared_ptr& connection); bool setObserveMode(int game_number, const boost::shared_ptr& connection); diff --git a/src/controllers/editposition.h b/src/controllers/editposition.h index 8e4eb74..5d8b5a4 100644 --- a/src/controllers/editposition.h +++ b/src/controllers/editposition.h @@ -13,7 +13,6 @@ #include "abstract.h" #include -#include "tagua.h" #include #include diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index cb908e9..7e56a34 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -5,6 +5,7 @@ set(taguacore_SRCS board.cpp color.cpp component.cpp + defaultpolicy.cpp defaultpool.cpp defaultstate.cpp icsverbose.cpp @@ -15,6 +16,7 @@ set(taguacore_SRCS pathinfo.cpp piece.cpp point.cpp + policy.cpp pool.cpp poolcollection.cpp repository.cpp @@ -24,6 +26,7 @@ set(taguacore_SRCS taguaapi.cpp taguaobject.cpp type.cpp + turnpolicy.cpp validator.cpp variant.cpp diff --git a/src/core/board.h b/src/core/board.h index 2a004ac..d2377a8 100644 --- a/src/core/board.h +++ b/src/core/board.h @@ -19,6 +19,8 @@ class Piece; +// TODO: make Board a component + /** * @brief A grid of pieces. * diff --git a/src/core/color.cpp b/src/core/color.cpp index 9bccd7e..3d862fc 100644 --- a/src/core/color.cpp +++ b/src/core/color.cpp @@ -21,6 +21,8 @@ White* White::self() { return &s_instance; } +int White::index() const { return 0; } + Black::Black() { } QString Black::name() const { return "black"; } @@ -29,3 +31,5 @@ Black* Black::self() { static Black s_instance; return &s_instance; } + +int Black::index() const { return 1; } diff --git a/src/core/color.h b/src/core/color.h index 4a1aca0..3c7df64 100644 --- a/src/core/color.h +++ b/src/core/color.h @@ -27,6 +27,11 @@ public: * @return The color name. */ virtual QString name() const = 0; + + /** + * @return The color index. + */ + virtual int index() const = 0; }; class TAGUA_EXPORT White : public Component, public IColor { @@ -34,6 +39,7 @@ Q_OBJECT White(); public: virtual QString name() const; + virtual int index() const; static White* self(); }; @@ -43,6 +49,7 @@ Q_OBJECT Black(); public: virtual QString name() const; + virtual int index() const; static Black* self(); }; diff --git a/src/core/defaultpolicy.cpp b/src/core/defaultpolicy.cpp new file mode 100644 index 0000000..a57e835 --- /dev/null +++ b/src/core/defaultpolicy.cpp @@ -0,0 +1,32 @@ +/* + Copyright (c) 2007 Paolo Capriotti + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#include "defaultpolicy.h" +#include "board.h" +#include "piece.h" +#include "state.h" + +InteractionType DefaultPolicy::movable(const IState* state, + const TurnTest& test, + const Point& p) const { + Piece piece = state->board()->get(p); + if (piece == Piece() || !test(piece.color())) + return NoAction; + + return piece.color() == state->turn() ? Moving : Premoving; +} + +InteractionType DefaultPolicy::droppable(const IState* state, + const TurnTest& test, + const IColor* pool) const { + if (!test(pool)) + return NoAction; + + return pool == state->turn() ? Moving : Premoving; +} diff --git a/src/core/defaultpolicy.h b/src/core/defaultpolicy.h new file mode 100644 index 0000000..52bb4cb --- /dev/null +++ b/src/core/defaultpolicy.h @@ -0,0 +1,29 @@ +/* + Copyright (c) 2007 Paolo Capriotti + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#ifndef CORE__DEFAULTPOLICY_H +#define CORE__DEFAULTPOLICY_H + +#include "component.h" +#include "policy.h" + +class TAGUA_EXPORT DefaultPolicy : public Component, public IPolicy { +Q_OBJECT +public Q_SLOTS: + virtual InteractionType movable(const IState* state, + const TurnTest& test, + const Point& p) const; + + virtual InteractionType droppable(const IState* state, + const TurnTest& test, + const IColor* pool) const; +}; + +#endif // CORE__DEFAULTPOLICY_H + diff --git a/src/interactiontype.h b/src/core/interactiontype.h similarity index 100% rename from src/interactiontype.h rename to src/core/interactiontype.h diff --git a/src/core/move.cpp b/src/core/move.cpp index 435d0df..0eb7f7b 100644 --- a/src/core/move.cpp +++ b/src/core/move.cpp @@ -30,7 +30,7 @@ Move::Move(const IColor* pool, int index, const Point& dst) , m_promotion(0) , m_pool(pool) , m_index(index) { } - + Point Move::src() const { return m_src; } Point Move::dst() const { return m_dst; } @@ -74,4 +74,4 @@ bool Move::operator==(const Move& mv) const { bool Move::operator!=(const Move& mv) const { return !(operator==(mv)); -} \ No newline at end of file +} diff --git a/src/core/move.h b/src/core/move.h index d43ea88..420ab1e 100644 --- a/src/core/move.h +++ b/src/core/move.h @@ -17,13 +17,13 @@ #include "piece.h" class TAGUA_EXPORT Move : public TaguaObject { - const Point m_src; - const Point m_dst; + Point m_src; + Point m_dst; const IType* m_promotion; // for drops const IColor* m_pool; - const int m_index; + int m_index; Piece m_drop; QString m_type; @@ -31,7 +31,7 @@ public: Move(); Move(const Point& src, const Point& dst, IType* promotion = 0); Move(const IColor* pool, int index, const Point& dst); - + Point src() const; Point dst() const; Piece drop() const; diff --git a/src/core/moveserializer.h b/src/core/moveserializer.h index 2d92e88..ea431a8 100644 --- a/src/core/moveserializer.h +++ b/src/core/moveserializer.h @@ -12,6 +12,7 @@ #define CORE__MOVE_SERIALIZER_H #include +#include "export.h" class IState; class Move; @@ -19,7 +20,7 @@ class Move; /** * @brief MoveSerializer takes care of converting moves to a displayable form and vice-versa. */ -class IMoveSerializer { +class TAGUA_EXPORT IMoveSerializer { public: virtual ~IMoveSerializer(); diff --git a/src/graphicalapi.cpp b/src/core/policy.cpp similarity index 71% copy from src/graphicalapi.cpp copy to src/core/policy.cpp index 374cb61..bb8e0d5 100644 --- a/src/graphicalapi.cpp +++ b/src/core/policy.cpp @@ -1,6 +1,5 @@ /* Copyright (c) 2007 Paolo Capriotti - (c) 2007 Maurizio Monge This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -8,7 +7,6 @@ (at your option) any later version. */ -#include "graphicalapi.h" - -GraphicalAPI::~GraphicalAPI() { } +#include "policy.h" +IPolicy::~IPolicy() { } diff --git a/src/core/policy.h b/src/core/policy.h new file mode 100644 index 0000000..416fb33 --- /dev/null +++ b/src/core/policy.h @@ -0,0 +1,43 @@ +/* + Copyright (c) 2007 Paolo Capriotti + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#ifndef CORE__POLICY_H +#define CORE__POLICY_H + +#include "interactiontype.h" +#include "turnpolicy.h" + +class IState; +class IType; +class Point; + +/** + * @brief A policy defines what pieces are movable in a given TurnTest. + */ +class TAGUA_EXPORT IPolicy { +public: + virtual ~IPolicy(); + + /** + * @return Whether the piece in the given square can be moved. + */ + virtual InteractionType movable(const IState* state, + const TurnTest& test, + const Point& p) const = 0; + + /** + * @return Whether it is possible to drop from the given pool. + */ + virtual InteractionType droppable(const IState* state, + const TurnTest& test, + const IColor* pool) const = 0; +}; + +#endif // CORE__POLICY_H + diff --git a/src/graphicalapi.cpp b/src/core/poolcollection_fwd.h similarity index 60% copy from src/graphicalapi.cpp copy to src/core/poolcollection_fwd.h index 374cb61..6d902b9 100644 --- a/src/graphicalapi.cpp +++ b/src/core/poolcollection_fwd.h @@ -1,6 +1,5 @@ /* Copyright (c) 2007 Paolo Capriotti - (c) 2007 Maurizio Monge This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -8,7 +7,12 @@ (at your option) any later version. */ -#include "graphicalapi.h" +#ifndef POOLCOLLECTION_FWD_H +#define POOLCOLLECTION_FWD_H -GraphicalAPI::~GraphicalAPI() { } +#include +class IPoolCollection; +typedef boost::shared_ptr PoolCollectionPtr; + +#endif // POOLCOLLECTION_FWD_H diff --git a/src/core/repository.h b/src/core/repository.h index 3027716..3d2284a 100644 --- a/src/core/repository.h +++ b/src/core/repository.h @@ -3,11 +3,9 @@ #include #include -#include "component_fwd.h" +#include "component.h" #include "export.h" -class Component; - /** * @brief A hierarchical collection of component defining a variant. * @@ -57,13 +55,18 @@ public: QList listComponents(const QString& path) const; }; +// template +// Interface* requestInterface(Component* component) { +// Interface* iface = dynamic_cast(component); +// if (iface) +// return iface; +// else +// return new typename Interface::Adaptor(iface); +// } + template Interface* requestInterface(Component* component) { - Interface* iface = dynamic_cast(component); - if (iface) - return iface; - else - return new typename Interface::Adaptor(iface); + return dynamic_cast(component); } #endif // CORE__REPOSITORY_H diff --git a/src/core/state.h b/src/core/state.h index af03e0f..45e76b0 100644 --- a/src/core/state.h +++ b/src/core/state.h @@ -58,6 +58,11 @@ public: * @return Whether two states are equal. */ virtual bool equals(IState* other) const = 0; + + /** + * Make this state equal to the given one. + */ + virtual void assign(const IState* other) = 0; /** * Play a move. @@ -75,7 +80,7 @@ public: virtual const IColor* opponent(const IColor* color) const = 0; /** - * @return Set next turn as the current turn. + * Set next turn as the current turn. */ virtual void advanceTurn() = 0; }; diff --git a/src/graphicalapi.cpp b/src/core/state_fwd.h similarity index 65% copy from src/graphicalapi.cpp copy to src/core/state_fwd.h index 374cb61..2b5426b 100644 --- a/src/graphicalapi.cpp +++ b/src/core/state_fwd.h @@ -1,6 +1,5 @@ /* Copyright (c) 2007 Paolo Capriotti - (c) 2007 Maurizio Monge This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -8,7 +7,12 @@ (at your option) any later version. */ -#include "graphicalapi.h" +#ifndef CORE__STATE_FWD_H +#define CORE__STATE_FWD_H -GraphicalAPI::~GraphicalAPI() { } +#include +class IState; +typedef boost::shared_ptr StatePtr; + +#endif // CORE__STATE_FWD_H diff --git a/src/core/taguaapi.h b/src/core/taguaapi.h index d7c6f3e..c86cdfd 100644 --- a/src/core/taguaapi.h +++ b/src/core/taguaapi.h @@ -18,6 +18,7 @@ #include "point.h" typedef boost::shared_ptr SpritePtr; +class IColor; class IState; class Piece; @@ -93,7 +94,7 @@ public: /** * @return the piece of the pool that has been dropped, or (-1,-1) */ - virtual std::pair droppedPoolPiece() = 0; + virtual std::pair droppedPoolPiece() = 0; virtual AnimationPtr group(const QString& flags) const = 0; virtual AnimationPtr appear(const NamedSprite& sprite, diff --git a/src/turnpolicy.cpp b/src/core/turnpolicy.cpp similarity index 85% rename from src/turnpolicy.cpp rename to src/core/turnpolicy.cpp index a9a2817..3bf814e 100644 --- a/src/turnpolicy.cpp +++ b/src/core/turnpolicy.cpp @@ -23,7 +23,7 @@ namespace TurnPolicy { Premove::~Premove() { } - bool Collection::operator()(int turn) const { + bool Collection::operator()(const IColor* turn) const { Policies::const_iterator it = m_policies.find(turn); if (it == m_policies.end()) { return false; @@ -33,11 +33,11 @@ namespace TurnPolicy { } } - void Collection::setPolicy(int turn, const PolicyPtr& policy) { + void Collection::setPolicy(const IColor* turn, const PolicyPtr& policy) { m_policies[turn] = policy; } - void Collection::setSimplePolicy(int turn, bool value) { + void Collection::setSimplePolicy(const IColor* turn, bool value) { setPolicy(turn, boost::shared_ptr(new Simple(value))); } diff --git a/src/turnpolicy.h b/src/core/turnpolicy.h similarity index 81% rename from src/turnpolicy.h rename to src/core/turnpolicy.h index 79996b5..d110a09 100644 --- a/src/turnpolicy.h +++ b/src/core/turnpolicy.h @@ -15,13 +15,15 @@ #include #include "export.h" +class IColor; + namespace TurnPolicy { /** * An abstract class encapsulating the user ability * to move in a given turn. */ - class Abstract { + class TAGUA_EXPORT Abstract { public: virtual ~Abstract(); virtual bool check() const = 0; @@ -31,7 +33,7 @@ namespace TurnPolicy { * Premove policy. Used to check whether premoving is allowed. * It is a global policy. */ - class Premove { + class TAGUA_EXPORT Premove { public: virtual ~Premove(); virtual bool check() const = 0; @@ -39,15 +41,15 @@ namespace TurnPolicy { class TAGUA_EXPORT Collection { typedef boost::shared_ptr PolicyPtr; - typedef std::map Policies; + typedef std::map Policies; typedef boost::shared_ptr PremovePolicyPtr; Policies m_policies; PremovePolicyPtr m_premove; public: - bool operator()(int turn) const; - void setPolicy(int turn, const PolicyPtr& policy); - void setSimplePolicy(int turn, bool value); + bool operator()(const IColor* turn) const; + void setPolicy(const IColor* turn, const PolicyPtr& policy); + void setSimplePolicy(const IColor* turn, bool value); void clear(); void setPremovePolicy(const PremovePolicyPtr& policy); diff --git a/src/core/variant.h b/src/core/variant.h index 3a507fa..558836f 100644 --- a/src/core/variant.h +++ b/src/core/variant.h @@ -34,4 +34,9 @@ public: Repository* repository(); }; +template +Interface* requestComponent(Variant* var, const QString& component) { + return requestInterface(var->repository()->getComponent(component)); +} + #endif // CORE__VARIANT_H diff --git a/src/engine.h b/src/engine.h index 8f81c5d..ad7d260 100644 --- a/src/engine.h +++ b/src/engine.h @@ -15,11 +15,10 @@ #include #include - +#include #include "console.h" -#include "tagua.h" - +class Move; class EngineNotifier; /** @@ -66,16 +65,16 @@ public: /** * Send a move to the engine. */ - virtual void sendMove(AbstractMove::Ptr move, AbstractPosition::Ptr ref) = 0; + virtual void sendMove(const Move& move, const StatePtr& ref) = 0; /** * Back up a move. * @param pos The position before the move. Used if the engine * does not support the undo command. */ - virtual void backUp(AbstractPosition::Ptr pos) = 0; + virtual void backUp(const StatePtr& pos) = 0; - virtual void setBoard(AbstractPosition::Ptr pos, int halfmove, int fullmove) = 0; + virtual void setBoard(const StatePtr& pos) = 0; virtual void startAnalysis() = 0; virtual void stopAnalysis() = 0; diff --git a/src/engineinfo.cpp b/src/engineinfo.cpp index ebf718e..7fd919e 100644 --- a/src/engineinfo.cpp +++ b/src/engineinfo.cpp @@ -31,21 +31,13 @@ void EngineDetails::save(Settings s) { s["work-path"] = workPath; } -void EngineInfo::playAsWhite() { - playAs(0); -} - -void EngineInfo::playAsBlack() { - playAs(1); -} - -void EngineInfo::playAs(int player) { +void EngineInfo::playAs(Variant* variant, const IColor* player) { if (m_token[player].valid()) { m_ui.removeEntity(m_token[player]); m_token[player] = EntityToken(); } else { - m_token[player] = m_ui.addPlayingEngine(player, engine()); + m_token[player] = m_ui.addPlayingEngine(player, engine(variant)); } } @@ -63,12 +55,12 @@ EngineInfo::EngineInfo(const EngineDetails& details, UI& ui) , m_details(details) , m_ui(ui) { } -shared_ptr EngineInfo::engine() { +shared_ptr EngineInfo::engine(Variant* variant) { shared_ptr res; if (m_details.type == EngineDetails::XBoard) - res = shared_ptr(new XBoardEngine(m_details.path, QStringList())); + res = shared_ptr(new XBoardEngine(variant, m_details.path, QStringList())); else if (m_details.type == EngineDetails::GNUShogi) - res = shared_ptr(new GNUShogiEngine(m_details.path, QStringList())); + res = shared_ptr(new GNUShogiEngine(variant, m_details.path, QStringList())); else { ERROR("Unimplemented engine type " << EngineDetails::typeName(m_details.type)); return shared_ptr(); diff --git a/src/engineinfo.h b/src/engineinfo.h index d14e6af..2c91929 100644 --- a/src/engineinfo.h +++ b/src/engineinfo.h @@ -11,6 +11,7 @@ #ifndef ENGINEINFO_H #define ENGINEINFO_H +#include #include #include #include @@ -18,7 +19,9 @@ #include "settings.h" class Engine; +class IColor; class UI; +class Variant; struct EngineDetails { enum EngineType { @@ -78,17 +81,14 @@ class EngineInfo : public QObject { Q_OBJECT EngineDetails m_details; UI& m_ui; - EntityToken m_token[2]; + typedef std::map Tokens; + Tokens m_token; protected: - virtual boost::shared_ptr engine(); - void playAs(int player); + virtual boost::shared_ptr engine(Variant* variant); + void playAs(Variant* variant, const IColor* player); public: EngineInfo(const EngineDetails& details, UI& ui); void setWorkPath(const QString& wp) { m_details.workPath = wp; } -public Q_SLOTS: - void playAsWhite(); - void playAsBlack(); -// void analyze(); }; #endif // ENGINEINFO_H diff --git a/src/entities/engineentity.cpp b/src/entities/engineentity.cpp index f468a00..97c6a34 100644 --- a/src/entities/engineentity.cpp +++ b/src/entities/engineentity.cpp @@ -9,6 +9,9 @@ */ #include "engineentity.h" + +#include +#include #include "game.h" #include "engine.h" @@ -17,7 +20,7 @@ using namespace boost; -EngineEntity::EngineEntity(const VariantPtr& variant, const shared_ptr& game, int side, +EngineEntity::EngineEntity(Variant* variant, const shared_ptr& game, const IColor* side, const shared_ptr& engine, AgentGroup* group) : Entity(game) , m_variant(variant) @@ -45,17 +48,12 @@ void EngineEntity::setup() { // set engine state playing all moves // in the current variation while (!m_indexes.empty()) { - PositionPtr pos = m_game->position(index); - std::cout << "REF:" << std::endl; - pos->dump(); + StatePtr state = m_game->position(index); index = m_indexes.top(); m_indexes.pop(); - MovePtr move = m_game->move(index); - std::cout << "move = " << move->toString("simple", pos) << std::endl; - - m_engine->sendMove(move, pos); + m_engine->sendMove(m_game->move(index), state); } m_last_index = m_game->index(); @@ -63,7 +61,8 @@ void EngineEntity::setup() { } void EngineEntity::notifyEngineMove(const QString& move_str) { - AbstractMove::Ptr move = position()->getMove(move_str); + // FIXME implement notifyEngineMove + #if 0 if (position()->testMove(move)) { Q_ASSERT(move); AbstractPosition::Ptr ref = position(); @@ -75,6 +74,8 @@ void EngineEntity::notifyEngineMove(const QString& move_str) { } else ERROR("Engine attempted to execute an invalid move: " << move_str); + + #endif } void EngineEntity::checkPlaying() { diff --git a/src/entities/engineentity.h b/src/entities/engineentity.h index 1f4398c..554a05a 100644 --- a/src/entities/engineentity.h +++ b/src/entities/engineentity.h @@ -16,16 +16,16 @@ #include "enginenotifier.h" #include "entity.h" #include "index.h" -#include "tagua.h" -#include "fwd.h" class Engine; +class IColor; +class Variant; class EngineEntity : public Entity , public Agent , public EngineNotifier { - VariantPtr m_variant; - int m_side; + Variant* m_variant; + const IColor* m_side; Index m_last_index; bool m_playing; boost::shared_ptr m_engine; @@ -35,7 +35,7 @@ class EngineEntity : public Entity public: boost::shared_ptr engine() { return m_engine; } - EngineEntity(const VariantPtr& variant, const boost::shared_ptr&, int side, + EngineEntity(Variant* variant, const boost::shared_ptr&, const IColor* side, const boost::shared_ptr& engine, AgentGroup* group); void setup(); diff --git a/src/entities/entity.cpp b/src/entities/entity.cpp index 32dcb76..b92b48a 100644 --- a/src/entities/entity.cpp +++ b/src/entities/entity.cpp @@ -22,7 +22,7 @@ GamePtr Entity::game() const { return m_game; } -PositionPtr Entity::position() const { +StatePtr Entity::position() const { return m_game->position(); } diff --git a/src/entities/entity.h b/src/entities/entity.h index 2ad8b5e..7cca676 100644 --- a/src/entities/entity.h +++ b/src/entities/entity.h @@ -11,7 +11,8 @@ #ifndef ENTITY_H #define ENTITY_H -#include "fwd.h" +#include +#include "game_fwd.h" #include "usermove.h" class Entity { @@ -39,7 +40,7 @@ public: virtual void detach() { } GamePtr game() const; - PositionPtr position() const; + StatePtr position() const; }; #endif // ENTITY_H diff --git a/src/entities/examinationentity.cpp b/src/entities/examinationentity.cpp index 74b180f..f906e79 100644 --- a/src/entities/examinationentity.cpp +++ b/src/entities/examinationentity.cpp @@ -8,22 +8,23 @@ (at your option) any later version. */ +#include +#include + #include "examinationentity.h" #include "game.h" #include "icsconnection.h" #include "positioninfo.h" #include "pgnparser.h" -#include "hlvariant/chess/piece.h" #include "icsapi.h" #include -ExaminationEntity::ExaminationEntity(const VariantPtr& variant, const boost::shared_ptr& game, +ExaminationEntity::ExaminationEntity(Variant* variant, const boost::shared_ptr& game, int game_number, const boost::shared_ptr& connection, AgentGroup* group) : UserEntity(game) , m_variant(variant) -, m_icsapi(variant->icsAPI()) , m_game_number(game_number) , m_connection(connection) , m_dispatcher(group, this) { @@ -38,21 +39,17 @@ void ExaminationEntity::loadPGN(const PGN& pgn) { m_game->load(pgn); } -AbstractMove::Ptr ExaminationEntity::testMove(const NormalUserMove&) const { - return AbstractMove::Ptr(); -} -AbstractMove::Ptr ExaminationEntity::testMove(const DropUserMove&) const { - return AbstractMove::Ptr(); +bool ExaminationEntity::testMove(Move&) const { + return false; } -bool ExaminationEntity::testPremove(const NormalUserMove&) const { return false; } -bool ExaminationEntity::testPremove(const DropUserMove&) const { return false; } -void ExaminationEntity::executeMove(boost::shared_ptr) { } -void ExaminationEntity::addPremove(const NormalUserMove&) { } -void ExaminationEntity::addPremove(const DropUserMove&) { } +bool ExaminationEntity::testPremove(const Move&) const { return false; } +void ExaminationEntity::executeMove(const Move&) { } +void ExaminationEntity::addPremove(const Move&) { } void ExaminationEntity::cancelPremove() { } + InteractionType ExaminationEntity::validTurn(const Point&) const { return NoAction; } -InteractionType ExaminationEntity::validTurn(int) const { return NoAction; } +InteractionType ExaminationEntity::validTurn(const IColor*) const { return NoAction; } bool ExaminationEntity::movable(const Point&) const { return false; } bool ExaminationEntity::jump(const Index&) { return true; } @@ -98,14 +95,14 @@ void ExaminationEntity::notifyStyle12(const PositionInfo& style12, bool /*is_sta m_dispatcher.clockUpdate(style12.whiteTime, style12.blackTime); if (style12.index() > 0) { - MovePtr last_move = m_icsapi->parseVerbose(style12.lastMove, style12.position); + Move last_move = m_icsapi->parseVerbose(style12.lastMove, style12.position); m_game->insert(last_move, style12.position, style12.index()); } m_game->goTo(style12.index()); } void ExaminationEntity::notifyPool(const class PoolInfo&) { /*TODO */ } -void ExaminationEntity::notifyMoveList(int, AbstractPosition::Ptr, const PGN&){ /* TODO */ } +void ExaminationEntity::notifyMoveList(int, const StatePtr&, const PGN&){ /* TODO */ } void ExaminationEntity::notifyMove(const Index&) { } void ExaminationEntity::notifyBack() { } void ExaminationEntity::notifyForward() { } diff --git a/src/entities/examinationentity.h b/src/entities/examinationentity.h index 35e4333..2313c81 100644 --- a/src/entities/examinationentity.h +++ b/src/entities/examinationentity.h @@ -11,40 +11,37 @@ #ifndef EXAMINATIONENTITY_H #define EXAMINATIONENTITY_H -#include "userentity.h" -#include "tagua.h" -#include "icslistener.h" #include "agent.h" #include "agentgroup.h" -#include "fwd.h" +#include "userentity.h" +#include "icsapi_fwd.h" +#include "icslistener.h" class ICSConnection; +class Variant; class ExaminationEntity : public UserEntity , public ICSListener { - VariantPtr m_variant; + Variant* m_variant; ICSAPIPtr m_icsapi; int m_game_number; boost::shared_ptr m_connection; AgentGroupDispatcher m_dispatcher; public: - ExaminationEntity(const VariantPtr&, const boost::shared_ptr&, + ExaminationEntity(Variant*, const boost::shared_ptr&, int, const boost::shared_ptr&, AgentGroup*); virtual QString save() const; virtual void loadPGN(const PGN&); - virtual AbstractMove::Ptr testMove(const NormalUserMove&) const; - virtual AbstractMove::Ptr testMove(const DropUserMove&) const; - virtual bool testPremove(const NormalUserMove&) const; - virtual bool testPremove(const DropUserMove&) const; - virtual void executeMove(boost::shared_ptr); - virtual void addPremove(const NormalUserMove&); - virtual void addPremove(const DropUserMove&); + virtual bool testMove(Move&) const; + virtual bool testPremove(const Move&) const; + virtual void executeMove(const Move&); + virtual void addPremove(const Move&); virtual void cancelPremove(); virtual InteractionType validTurn(const Point&) const; - virtual InteractionType validTurn(int) const; + virtual InteractionType validTurn(const IColor*) const; virtual bool movable(const Point&) const; virtual bool jump(const Index&); virtual bool gotoFirst(); @@ -58,8 +55,8 @@ public: virtual bool promoteVariation(); virtual void notifyStyle12(const PositionInfo&, bool is_starting); - virtual void notifyPool(const class PoolInfo&); - virtual void notifyMoveList(int, AbstractPosition::Ptr, const PGN&); + virtual void notifyPool(const PoolInfo&); + virtual void notifyMoveList(int, const StatePtr&, const PGN&); virtual void notifyClockUpdate(int, int) { } virtual void notifyMove(const Index&); diff --git a/src/entities/gameentity.cpp b/src/entities/gameentity.cpp index 0325739..ba86583 100644 --- a/src/entities/gameentity.cpp +++ b/src/entities/gameentity.cpp @@ -9,19 +9,26 @@ */ #include "gameentity.h" -#include "premove.h" + +#include +#include +#include +#include + #include "game.h" -#include "board.h" +#include "chessboard.h" #include "pgnparser.h" using namespace boost; -GameEntity::GameEntity(const VariantPtr& variant, const boost::shared_ptr& game, - Board* chessboard, AgentGroup* group) +GameEntity::GameEntity(Variant* variant, const boost::shared_ptr& game, + ChessBoard* chessboard, AgentGroup* group) : UserEntity(game) , m_variant(variant) , m_chessboard(chessboard) , m_dispatcher(group, this) { + m_validator = requestComponent(m_variant, "validator"); + m_policy = requestComponent(m_variant, "policy"); } QString GameEntity::save() const { @@ -32,38 +39,32 @@ void GameEntity::loadPGN(const PGN& pgn) { m_game->load(pgn); } -AbstractPosition::Ptr GameEntity::doMove(AbstractMove::Ptr move) const { - AbstractPosition::Ptr newPosition = position()->clone(); - newPosition->move(move); - return newPosition; +StatePtr GameEntity::doMove(const Move& move) const { + StatePtr newState(position()->clone()); + newState->move(move); + return newState; } -void GameEntity::executeMove(AbstractMove::Ptr move) { - AbstractPosition::Ptr ref = position(); - AbstractPosition::Ptr pos = doMove(move); +void GameEntity::executeMove(const Move& move) { + StatePtr ref = position(); + StatePtr pos = doMove(move); m_game->add(move, pos); m_dispatcher.move(m_game->index()); } -void GameEntity::addPremove(const NormalUserMove& m) { - m_premoveQueue = shared_ptr(new Premove(m_variant->createNormalMove(m))); -} - -void GameEntity::addPremove(const DropUserMove& m) { - m_premoveQueue = shared_ptr(new Premove(m_variant->createDropMove(m))); +void GameEntity::addPremove(const Move& m) { + m_premoveQueue = m; } void GameEntity::cancelPremove() { - m_premoveQueue.reset(); + m_premoveQueue = Move(); m_chessboard->cancelPremove(); } void GameEntity::notifyMove(const Index&) { // the other player moved: execute premove - if (m_premoveQueue) { - AbstractMove::Ptr move = m_premoveQueue->execute(m_game->position()); - if (move) - executeMove(move); + if (m_premoveQueue != Move() && testMove(m_premoveQueue)) { + executeMove(m_premoveQueue); } cancelPremove(); } @@ -73,47 +74,25 @@ void GameEntity::notifyForward() { } void GameEntity::notifyGotoFirst() { } void GameEntity::notifyGotoLast() { } -NormalUserMove GameEntity::createMove(const Point& from, const Point& to) const { - NormalUserMove m = UserEntity::createMove(from, to); - m_variant->setupMove(m); - return m; -} - -AbstractMove::Ptr GameEntity::testMove(const NormalUserMove& move) const { - AbstractMove::Ptr m = m_variant->createNormalMove(move); - if (m && position()->testMove(m)) - return m; - else - return AbstractMove::Ptr(); -} - -AbstractMove::Ptr GameEntity::testMove(const DropUserMove& move) const { - AbstractMove::Ptr m = m_variant->createDropMove(move); - if (m && position()->testMove(m)) - return m; - else - return AbstractMove::Ptr (); -} - -AbstractPiece::Ptr GameEntity::moveHint(AbstractMove::Ptr move) const { - return position()->moveHint(move); +bool GameEntity::testMove(Move& move) const { + return m_validator->legal(position().get(), move); } -bool GameEntity::testPremove(const NormalUserMove&) const { - return true; // TODO +Piece GameEntity::moveHint(const Move&) const { +// return position()->moveHint(move); + return Piece(); // FIXME implement move hinting } -bool GameEntity::testPremove(const DropUserMove&) const { - return true; // TODO +bool GameEntity::testPremove(const Move&) const { + return true; // FIXME implement premove checking } -InteractionType GameEntity::validTurn(int pool) const { - return position()->droppable(m_turn_test, pool); +InteractionType GameEntity::validTurn(const IColor* pool) const { + return m_policy->droppable(position().get(), m_turn_test, pool); } InteractionType GameEntity::validTurn(const Point& point) const { - // TODO: maybe replace this call with a new API position()->owner(point) ?? - return position()->movable(m_turn_test, point); + return m_policy->movable(position().get(), m_turn_test, point); } bool GameEntity::movable(const Point& point) const { @@ -123,7 +102,7 @@ bool GameEntity::movable(const Point& point) const { } bool GameEntity::oneClickMoves() const { - return m_variant->simpleMoves(); + return false; // FIXME support one-click moves } bool GameEntity::gotoFirst() { diff --git a/src/entities/gameentity.h b/src/entities/gameentity.h index 293ee9b..b312486 100644 --- a/src/entities/gameentity.h +++ b/src/entities/gameentity.h @@ -11,23 +11,28 @@ #ifndef GAMEENTITY_H #define GAMEENTITY_H -#include "userentity.h" +#include #include "agent.h" #include "agentgroup.h" -#include "fwd.h" +#include "userentity.h" -class Board; +class ChessBoard; +class IPolicy; +class IValidator; +class Variant; class GameEntity : public UserEntity { - VariantPtr m_variant; - Board* m_chessboard; + Variant* m_variant; + IPolicy* m_policy; + IValidator* m_validator; + ChessBoard* m_chessboard; AgentGroupDispatcher m_dispatcher; - boost::shared_ptr m_premoveQueue; + Move m_premoveQueue; - AbstractPosition::Ptr doMove(AbstractMove::Ptr move) const; + StatePtr doMove(const Move& move) const; public: - GameEntity(const VariantPtr& variant, const boost::shared_ptr& game, - Board* chessboard, AgentGroup* group); + GameEntity(Variant* variant, const boost::shared_ptr& game, + ChessBoard* chessboard, AgentGroup* group); /** * Return a PGN for the game. @@ -39,9 +44,8 @@ public: */ virtual void loadPGN(const PGN& pgn); - virtual void executeMove(AbstractMove::Ptr move); - virtual void addPremove(const NormalUserMove& m); - virtual void addPremove(const DropUserMove& m); + virtual void executeMove(const Move& move); + virtual void addPremove(const Move& m); virtual void cancelPremove(); virtual void notifyClockUpdate(int, int) { } @@ -51,14 +55,12 @@ public: virtual void notifyGotoFirst(); virtual void notifyGotoLast(); - virtual NormalUserMove createMove(const Point& from, const Point& to) const; - virtual AbstractMove::Ptr testMove(const NormalUserMove&) const; - virtual AbstractMove::Ptr testMove(const DropUserMove&) const; - virtual AbstractPiece::Ptr moveHint(AbstractMove::Ptr move) const; - virtual bool testPremove(const NormalUserMove&) const; - virtual bool testPremove(const DropUserMove&) const; + virtual bool testMove(Move&) const; + virtual bool testPremove(const Move&) const; + virtual Piece moveHint(const Move& move) const; + virtual InteractionType validTurn(const Point&) const; - virtual InteractionType validTurn(int) const; + virtual InteractionType validTurn(const IColor*) const; virtual bool movable(const Point&) const; virtual bool oneClickMoves() const; diff --git a/src/entities/icsentity.cpp b/src/entities/icsentity.cpp index b48d103..e9b47f9 100644 --- a/src/entities/icsentity.cpp +++ b/src/entities/icsentity.cpp @@ -9,6 +9,11 @@ */ #include "icsentity.h" + +#include +#include +#include +#include #include "game.h" #include "icsconnection.h" #include "positioninfo.h" @@ -50,38 +55,43 @@ bool ICSPremovePolicy::check() const { } -ICSEntity::ICSEntity(const VariantPtr& variant, const shared_ptr& game, - int side, int gameNumber, +ICSEntity::ICSEntity(Variant* variant, const shared_ptr& game, + const IColor* side, int gameNumber, const shared_ptr& connection, AgentGroup* group) : Entity(game) , m_variant(variant) -, m_icsapi(variant->icsAPI()) , m_connection(connection) , m_side(side) , m_game_number(gameNumber) , m_editing_mode(false) , m_dispatcher(group, this) { Q_ASSERT(m_icsapi); + m_validator = requestComponent(m_variant, "validator"); + m_serializer = requestComponent(m_variant, + "move_serializer/simple"); + m_san_serializer = requestComponent(m_variant, + "move_serializer/san"); } -AbstractPosition::Ptr ICSEntity::position() const { +StatePtr ICSEntity::position() const { return m_game->position(); } -void ICSEntity::executeMove(AbstractMove::Ptr) { } +void ICSEntity::executeMove(const Move&) { } -void ICSEntity::updateGame(const Index& index, AbstractMove::Ptr icsMove, - AbstractPosition::Ptr icsPos) { - if (index > 0 && m_game->containsIndex(index.prev()) && icsMove) { - if (AbstractPosition::Ptr position = m_game->position(index.prev())) { - position = position->clone(); +void ICSEntity::updateGame(const Index& index, Move& icsMove, + const StatePtr& icsPos) { + if (index > 0 && m_game->containsIndex(index.prev()) && icsMove != Move()) { + if (StatePtr position = m_game->position(index.prev())) { + position = StatePtr(position->clone()); - if (position->testMove(icsMove)) { + if (m_validator->legal(position.get(), icsMove)) { position->move(icsMove); - icsPos->copyPoolFrom(position); +// icsPos->copyPoolFrom(position); + // FIXME implement copyPoolFrom - if (!position->equals(icsPos)) + if (!position->equals(icsPos.get())) std::cout << "[inconsistency] computed position differs from expected!" << std::endl; } else @@ -116,16 +126,16 @@ void ICSEntity::notifyStyle12(const PositionInfo& style12, bool is_starting) { m_dispatcher.clockUpdate(style12.whiteTime, style12.blackTime); // get last move verbose notation - AbstractMove::Ptr last_move; + Move last_move; if (!is_starting) last_move = m_icsapi->parseVerbose(style12.lastMove, style12.position); if (style12.index() > 0 && m_game->containsIndex(style12.index() - 1) - && last_move && m_variant->name() != "Dummy") { - AbstractPosition::Ptr position = m_game->position(style12.index() - 1); + && last_move != Move() && m_variant->name() != "Dummy") { + StatePtr position = m_game->position(style12.index() - 1); if (position) { - AbstractMove::Ptr mv = position->getMove(style12.lastMoveSAN); - if (!mv || !mv->equals(last_move)) { + Move mv = m_san_serializer->deserialize(style12.lastMoveSAN, position.get()); + if (mv == Move() || mv == last_move) { std::cout << "[server inconsistency] SAN for last move is different from verbose notation" << std::endl; @@ -146,23 +156,17 @@ void ICSEntity::notifyPool(const PoolInfo& pi) { return; // BROKEN if (m_game->containsIndex(pi.m_pos_index)) { - AbstractPosition::Ptr p = m_game->position(pi.m_pos_index); + StatePtr p = m_game->position(pi.m_pos_index); //BROKEN //p->setPool(pi.m_pool); m_game->insert(m_game->move(pi.m_pos_index), p, pi.m_pos_index ); } } -void ICSEntity::notifyMoveList(int num, AbstractPosition::Ptr pos, const PGN& pgn) { +void ICSEntity::notifyMoveList(int num, const StatePtr& pos, const PGN& pgn) { if (m_game_number != num) return; - if(pos->variant() != m_variant->name()) { - std::cout << "Mismatch for move list, this variant is \"" << m_variant->name() << "\",\n" - " got move list for \"" << pos->variant() << "\"" << std::endl; - return; - } - - Game g; + Game g(m_variant); g.reset(pos); g.load(pgn); @@ -180,7 +184,12 @@ bool ICSEntity::canDetach() const { bool ICSEntity::attach() { // an ICS player can attach only if no move for that side has been made - //std::cout << "[ICS attach] index = " << m_game->lastMainlineIndex() << std::endl; + return m_game->lastMainlineIndex() == 0; + +/* TODO implement advanced ICSEntity attach mechanism as soon as the + needed variant ICS API is added */ +#if 0 + if (m_game->lastMainlineIndex() == 0) return true; if (m_game->lastMainlineIndex() == 1 && m_side == 1) { // white already played, inform black @@ -188,12 +197,15 @@ bool ICSEntity::attach() { return true; } return false; +#endif } void ICSEntity::notifyMove(const Index& index) { if (!canEdit()) { - m_connection->sendText(m_game->move(index)->toString( - "simple", m_game->position(index.prev()))); + QString mv = m_serializer->serialize( + m_game->move(index), + m_game->position(index.prev()).get()); + m_connection->sendText(mv); } } @@ -215,9 +227,9 @@ void ICSEntity::setupTurnTest(TurnTest& test) const { } -ObservingEntity::ObservingEntity(const VariantPtr& variant, const shared_ptr& game, +ObservingEntity::ObservingEntity(Variant* variant, const shared_ptr& game, int gameNumber,const shared_ptr& connection, AgentGroup* group) -: ICSEntity(variant, game, -1, gameNumber, connection, group) +: ICSEntity(variant, game, 0, gameNumber, connection, group) , m_attached(true) { } void ObservingEntity::detach() { diff --git a/src/entities/icsentity.h b/src/entities/icsentity.h index abf8ae0..6410519 100644 --- a/src/entities/icsentity.h +++ b/src/entities/icsentity.h @@ -11,30 +11,37 @@ #ifndef ICSENTITY_H #define ICSENTITY_H +#include #include "entity.h" #include "agent.h" #include "agentgroup.h" +#include "icsapi_fwd.h" #include "icslistener.h" -#include "tagua.h" -#include "fwd.h" +class IColor; +class IMoveSerializer; +class IValidator; class ICSConnection; +class Move; +class Variant; class ICSEntity : public Entity , public Agent , public ICSListener { protected: - VariantPtr m_variant; + Variant* m_variant; + IValidator* m_validator; ICSAPIPtr m_icsapi; + IMoveSerializer* m_serializer; + IMoveSerializer* m_san_serializer; boost::shared_ptr m_connection; - int m_side; + const IColor* m_side; int m_game_number; bool m_editing_mode; AgentGroupDispatcher m_dispatcher; - void updateGame(const Index& index, AbstractMove::Ptr icsMove, - AbstractPosition::Ptr icsPos); + void updateGame(const Index& index, Move& icsMove, const StatePtr& icsPos); enum UpdateType { MoveForward, @@ -47,16 +54,16 @@ protected: UpdateType getUpdate(const Index& index); public: - ICSEntity(const VariantPtr&, const boost::shared_ptr&, - int, int, + ICSEntity(Variant*, const boost::shared_ptr&, + const IColor*, int, const boost::shared_ptr&, AgentGroup*); - virtual void executeMove(AbstractMove::Ptr); + virtual void executeMove(const Move&); virtual void notifyStyle12(const PositionInfo&, bool is_starting); - virtual void notifyPool(const class PoolInfo&); - virtual void notifyMoveList(int, AbstractPosition::Ptr, const PGN&); + virtual void notifyPool(const PoolInfo&); + virtual void notifyMoveList(int, const StatePtr&, const PGN&); - virtual AbstractPosition::Ptr position() const; + virtual StatePtr position() const; virtual void notifyClockUpdate(int, int) { } virtual void notifyMove(const Index&); @@ -79,7 +86,7 @@ class ObservingEntity : public ICSEntity { protected: bool m_attached; public: - ObservingEntity(const VariantPtr&, const boost::shared_ptr&, + ObservingEntity(Variant*, const boost::shared_ptr&, int gameNumber, const boost::shared_ptr&, AgentGroup*); ~ObservingEntity(); diff --git a/src/entities/userentity.cpp b/src/entities/userentity.cpp index 849227b..65916e7 100644 --- a/src/entities/userentity.cpp +++ b/src/entities/userentity.cpp @@ -18,14 +18,6 @@ void UserEntity::enableEditingTools(bool value) { m_editing_tools = value; } -NormalUserMove UserEntity::createMove(const Point& from, const Point& to) const { - return NormalUserMove(from, to); -} - -DropUserMove UserEntity::createDrop(int pool, int index, const Point& to) const { - return DropUserMove(pool, index, to); -} - void UserEntity::handleRightClick(const Point&) const { } bool UserEntity::canDetach() const { return true; } diff --git a/src/entities/userentity.h b/src/entities/userentity.h index 55e27da..1d81ea0 100644 --- a/src/entities/userentity.h +++ b/src/entities/userentity.h @@ -14,12 +14,13 @@ #ifndef GAMEBASEDRULES_H #define GAMEBASEDRULES_H +#include +#include +#include +#include + #include "entity.h" -#include "fwd.h" -#include "tagua.h" -#include "usermove.h" #include "agent.h" -#include "turnpolicy.h" class PGN; @@ -43,23 +44,16 @@ public: */ virtual void loadPGN(const PGN& pgn) = 0; - virtual NormalUserMove createMove(const Point& from, const Point& to) const; - virtual DropUserMove createDrop(int pool, int index, const Point& to) const; - - virtual AbstractMove::Ptr testMove(const NormalUserMove& m) const = 0; - virtual AbstractMove::Ptr testMove(const DropUserMove& m) const = 0; - - virtual bool testPremove(const NormalUserMove& m) const = 0; - virtual bool testPremove(const DropUserMove&) const = 0; + virtual bool testMove(Move& m) const = 0; - virtual void executeMove(AbstractMove::Ptr move) = 0; - virtual void addPremove(const NormalUserMove& m) = 0; - virtual void addPremove(const DropUserMove& m) = 0; + virtual bool testPremove(const Move& m) const = 0; + virtual void executeMove(const Move& move) = 0; + virtual void addPremove(const Move& m) = 0; virtual void cancelPremove() = 0; virtual void handleRightClick(const Point&) const; virtual InteractionType validTurn(const Point&) const = 0; - virtual InteractionType validTurn(int) const = 0; + virtual InteractionType validTurn(const IColor*) const = 0; virtual bool movable(const Point&) const = 0; virtual bool oneClickMoves() const { return false; } @@ -110,9 +104,8 @@ public: /** * Move hint. - * @sa AbstractPosition::moveHint */ - virtual AbstractPiece::Ptr moveHint(AbstractMove::Ptr) const { return AbstractPiece::Ptr(); } + virtual Piece moveHint(const Move&) const { return Piece(); } virtual bool canDetach() const; }; diff --git a/src/fwd.h b/src/fwd.h deleted file mode 100644 index 4dc9b99..0000000 --- a/src/fwd.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. -*/ - -#ifndef FWD_H -#define FWD_H - -#include - -typedef boost::shared_ptr MovePtr; -typedef boost::shared_ptr PositionPtr; -typedef boost::shared_ptr PoolPtr; -typedef boost::shared_ptr PiecePtr; -typedef boost::shared_ptr AnimatorPtr; -typedef boost::shared_ptr ICSAPIPtr; -typedef boost::shared_ptr VariantPtr; - -template class UnwrappedGraphicalAPI; -template -struct UnwrappedGraphicalAPIPtr { - typedef boost::shared_ptr > type; -}; - -namespace HLVariant { - template class UnwrappedGraphicalAPI; - template - struct UnwrappedGraphicalAPIPtr { - typedef boost::shared_ptr > type; - }; -} - -typedef boost::shared_ptr AnimationPtr; -typedef boost::shared_ptr AnimationGroupPtr; -typedef boost::shared_ptr DropAnimationPtr; -typedef boost::shared_ptr PromotionAnimationPtr; -typedef boost::shared_ptr CaptureAnimationPtr; -typedef boost::shared_ptr InstantAnimationPtr; -typedef boost::shared_ptr DelayAnimationPtr; -typedef boost::shared_ptr MovementAnimationPtr; -typedef boost::shared_ptr FadeAnimationPtr; -typedef boost::shared_ptr GrowAnimationPtr; -typedef boost::shared_ptr ExplodeAnimationPtr; -typedef boost::shared_ptr TeleportAnimationPtr; -typedef boost::shared_ptr CrossFadingAnimationPtr; - -namespace Animate { class Scheme; } -typedef boost::shared_ptr SchemePtr; - -typedef boost::shared_ptr SpritePtr; - -typedef boost::shared_ptr GamePtr; - -#endif //FWD_H diff --git a/src/game.cpp b/src/game.cpp index 96cc237..a55e09f 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -18,9 +18,15 @@ #else #include #endif + +#include +#include +#include +#include +#include + #include "variants.h" #include "pgnparser.h" -#include "tagua.h" #include "game.h" #include "game_p.h" @@ -28,9 +34,12 @@ using namespace GamePrivate; -Game::Game() +Game::Game(Variant* variant) : current(-1) , undo_pos(0) { + m_validator = requestComponent(variant, "validator"); + m_serializer = requestComponent( + variant, "moveserializer/compact"); } Game::~Game() { @@ -104,10 +113,10 @@ void Game::testMove(const Index& ix) { if (ix != Index(0)) { Entry *e1 = fetch(ix.prev()); Entry *e2 = fetch(ix); - if(!e1 || !e2 || !e1->position || !e2->move) + if (!e1 || !e2 || !e1->position || e2->move == Move()) return; - if (!e1->position->testMove(e2->move)) + if (!m_validator->legal(e1->position.get(), e2->move)) ERROR("invalid move added to game history!"); } } @@ -143,28 +152,28 @@ bool Game::containsIndex(const Index& index) const { return !!fetch(index); } -MovePtr Game::move() const { +Move Game::move() const { return move(current); } -MovePtr Game::move(const Index& index) const { +Move Game::move(const Index& index) const { Entry *e = (Entry*)fetch(index); if(!e) { ERROR("Index out of range!"); - return MovePtr(); + return Move(); } return e->move; } -PositionPtr Game::position() const { +StatePtr Game::position() const { return position(current); } -PositionPtr Game::position(const Index& index) const { +StatePtr Game::position(const Index& index) const { Entry *e = (Entry*)fetch(index); if(!e) { ERROR("Index " << index << " out of range!"); - return PositionPtr(); + return StatePtr(); } return e->position; } @@ -182,13 +191,13 @@ QString Game::comment(const Index& index) const { return e->comment; } -void Game::reset(PositionPtr pos) { +void Game::reset(StatePtr pos) { Q_ASSERT(pos); undo_pos = 0; undo_history.clear(); history.clear(); - history.push_back( Entry(MovePtr(), pos) ); + history.push_back( Entry(Move(), pos) ); current = Index(0); onCurrentIndexChanged(); } @@ -607,7 +616,7 @@ void Game::truncate(const Index& ix) { } } -void Game::add(MovePtr m, PositionPtr pos) { +void Game::add(const Move& m, const StatePtr& pos) { Q_ASSERT(pos); Index old_c = current; @@ -626,7 +635,7 @@ void Game::add(MovePtr m, PositionPtr pos) { onCurrentIndexChanged(old_c); } /* we are playing the move that is already next in the mainline */ - else if( (*vec)[at+1].position && (*vec)[at+1].position->equals(pos)) { + else if( (*vec)[at+1].position && (*vec)[at+1].position->equals(pos.get())) { current = current.next(); onCurrentIndexChanged(old_c); /* no need to test the move */ @@ -638,7 +647,7 @@ void Game::add(MovePtr m, PositionPtr pos) { /* check if a variations with this move already exists. */ for(Variations::iterator it = e->variations.begin(); it != e->variations.end(); ++it) if(it->second.size() > 0 && it->second[0].position - && it->second[0].position->equals(pos) ) { + && it->second[0].position->equals(pos.get()) ) { current = current.next(it->first); onCurrentIndexChanged(old_c); @@ -655,7 +664,7 @@ void Game::add(MovePtr m, PositionPtr pos) { } } -bool Game::insert(MovePtr m, PositionPtr pos, const Index& at) { +bool Game::insert(const Move& m, const StatePtr& pos, const Index& at) { Entry *e = fetch(at); if(!e) { @@ -681,7 +690,7 @@ bool Game::insert(MovePtr m, PositionPtr pos, const Index& at) { undo_pos = 0; undo_history.clear(); } - bool res = e->position && e->position->equals(pos); + bool res = e->position && e->position->equals(pos.get()); e->move = m; e->position = pos; testMove(at); @@ -761,8 +770,10 @@ QString Game::variationPgn(const History& vec, const Entry& e, for (int i = start; i < static_cast(vec.size()); i++) { const Entry& preve = (i > start) ? vec[i-1] : e; - QString mv = (vec[i].move && preve.position) ? - vec[i].move->toString("compact", preve.position ) : "???"; + + QString mv = (vec[i].move != Move() && preve.position) + ? m_serializer->serialize(vec[i].move, preve.position.get()) + : "???"; #if 0 if (ix == current) mv = "[[" + mv + "]]"; @@ -801,7 +812,7 @@ QString Game::pgn() const { void Game::load(const PGN& pgn) { std::map::const_iterator var = pgn.m_tags.find("Variant"); - VariantPtr vi; + Variant* vi; if (var == pgn.m_tags.end()) { vi = Variants::instance().get("chess"); @@ -812,10 +823,11 @@ void Game::load(const PGN& pgn) { } std::map::const_iterator fen = pgn.m_tags.find("FEN"); - PositionPtr pos; + StatePtr pos; if(var == pgn.m_tags.end()) { - pos = vi->createPosition(); + IStateFactory* fact = requestComponent(vi, "state_factory"); + pos = StatePtr(fact->createState()); pos->setup(); } #if 0 // BROKEN @@ -830,7 +842,7 @@ void Game::load(const PGN& pgn) { load(pos, pgn); } -void Game::load(PositionPtr pos, const PGN& pgn) { +void Game::load(StatePtr pos, const PGN& pgn) { current = Index(0); undo_history.clear(); undo_pos = 0; @@ -856,7 +868,7 @@ void Game::load(PositionPtr pos, const PGN& pgn) { history[0].position = pos; } else - history.push_back( Entry(MovePtr(), pos) ); + history.push_back( Entry(Move(), pos) ); QString vcomment; std::vector var_stack; @@ -903,13 +915,13 @@ void Game::load(PositionPtr pos, const PGN& pgn) { else if(pm->m_number && pm->m_number!=n+1) ERROR("Move number mismatch!"); - PositionPtr pos = position(); - MovePtr m = pos->getMove(pm->m_move); + StatePtr pos = position(); +Move m = m_serializer->deserialize(pm->m_move, pos.get()); - if(!m || !pos->testMove(m)) + if (m == Move() || !m_validator->legal(pos.get(), m)) break; - PositionPtr newPos = pos->clone(); + StatePtr newPos(pos->clone()); newPos->move(m); int at; diff --git a/src/game.h b/src/game.h index 5de902e..0fa3612 100644 --- a/src/game.h +++ b/src/game.h @@ -14,9 +14,13 @@ #include #include #include -#include "fwd.h" +#include +#include #include "index.h" +class IMoveSerializer; +class IValidator; +class Variant; class PGN; namespace GamePrivate { @@ -67,9 +71,11 @@ public: virtual void onAvailableUndo(bool); virtual void onAvailableRedo(bool); + IValidator* m_validator; + IMoveSerializer* m_serializer; public: /** Constructor, creates an empty game*/ - Game(); + explicit Game(Variant* variant); /** destructor */ virtual ~Game(); @@ -84,16 +90,16 @@ public: bool containsIndex(const Index& index) const; /** \return the current move */ - MovePtr move() const; + Move move() const; /** \return the move at the given index */ - MovePtr move(const Index& index) const; + Move move(const Index& index) const; /** \return the current position */ - PositionPtr position() const; + StatePtr position() const; /** \return the position at the given index */ - PositionPtr position(const Index& index) const; + StatePtr position(const Index& index) const; /** \return the current comment */ QString comment() const; @@ -102,7 +108,7 @@ public: QString comment(const Index& index) const; /** clears the games, and puts \a pos as root position */ - void reset(PositionPtr pos); + void reset(StatePtr pos); /** undo */ void undo(); @@ -151,10 +157,10 @@ public: /** adds a new move+position after the current one, on the main line if possible, or else in a new variation */ - void add(MovePtr move, PositionPtr pos); + void add(const Move& move, const StatePtr& pos); /** forces a move+position at in certain index */ - bool insert(MovePtr move, PositionPtr pos, const Index& index); + bool insert(const Move& move, const StatePtr& pos, const Index& index); /** \return true if we cannot go forward */ bool lastPosition() const; @@ -178,7 +184,7 @@ public: QString pgn() const; /** loads a pgn in the current game */ - void load(PositionPtr, const PGN& pgn); + void load(StatePtr, const PGN& pgn); /** loads a pgn in the current game */ void load(const PGN& pgn); diff --git a/src/graphicalapi.cpp b/src/game_fwd.h similarity index 68% copy from src/graphicalapi.cpp copy to src/game_fwd.h index 374cb61..b6f56cd 100644 --- a/src/graphicalapi.cpp +++ b/src/game_fwd.h @@ -1,6 +1,5 @@ /* Copyright (c) 2007 Paolo Capriotti - (c) 2007 Maurizio Monge This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -8,7 +7,13 @@ (at your option) any later version. */ -#include "graphicalapi.h" +#ifndef GAME_FWD_H +#define GAME_FWD_H -GraphicalAPI::~GraphicalAPI() { } +#include +class Game; +typedef boost::shared_ptr GamePtr; + + +#endif // GAME_FWD_H diff --git a/src/game_p.h b/src/game_p.h index a446ae8..ec4efe8 100644 --- a/src/game_p.h +++ b/src/game_p.h @@ -13,6 +13,8 @@ #include #include +#include +#include #include "game.h" namespace GamePrivate { @@ -25,14 +27,14 @@ typedef std::map VComments; */ class Entry { public: - MovePtr move; - PositionPtr position; + Move move; + StatePtr position; QString comment; Variations variations; VComments vcomments; int last_var_id; - Entry(MovePtr move, PositionPtr position) + Entry(const Move& move, const StatePtr& position) : move(move) , position(position) , last_var_id(0) { } diff --git a/src/gnushogiengine.cpp b/src/gnushogiengine.cpp index 2bc1282..a0739c6 100644 --- a/src/gnushogiengine.cpp +++ b/src/gnushogiengine.cpp @@ -14,6 +14,8 @@ #include #include +#include +#include #include "enginenotifier.h" #include "foreach.h" @@ -21,7 +23,9 @@ using namespace boost; QRegExp GNUShogiEngine::m_move_pattern("[0-9]*\\. \\.\\.\\. ([^ ]*) .*"); -GNUShogiEngine::GNUShogiEngine(const QString& path, const QStringList& arguments) +GNUShogiEngine::GNUShogiEngine(Variant* variant, + const QString& path, + const QStringList& arguments) : Engine(path, arguments) , m_analysing(false) { // set default features @@ -45,6 +49,9 @@ GNUShogiEngine::GNUShogiEngine(const QString& path, const QStringList& arguments connect(this, SIGNAL(receivedCommand(const QString&)), this, SLOT(processCommand(const QString&))); + + m_serializer = requestComponent( + variant, "moveserializer/simple"); } GNUShogiEngine::~GNUShogiEngine() { @@ -82,12 +89,12 @@ void GNUShogiEngine::processCommand(const QString& command) { } } -void GNUShogiEngine::sendMove(AbstractMove::Ptr move, AbstractPosition::Ptr ref) { - QString move_str = move->toString("simple", ref); +void GNUShogiEngine::sendMove(const Move& move, const StatePtr& ref) { + QString move_str = m_serializer->serialize(move, ref.get()); sendCommand(move_str); } -void GNUShogiEngine::backUp(AbstractPosition::Ptr) { +void GNUShogiEngine::backUp(const StatePtr&) { sendCommand("undo"); } @@ -102,7 +109,7 @@ void GNUShogiEngine::stopAnalysis() { m_analysing = false; } -void GNUShogiEngine::setBoard(AbstractPosition::Ptr, int, int) { +void GNUShogiEngine::setBoard(const StatePtr&) { #if 0 if (m_features.setboard) { sendCommand(QString("setboard %1").arg(pos->fen(halfmove, fullmove))); diff --git a/src/gnushogiengine.h b/src/gnushogiengine.h index 242f446..16e0bb1 100644 --- a/src/gnushogiengine.h +++ b/src/gnushogiengine.h @@ -12,9 +12,10 @@ #define GNUSHOGIENGINE_H #include "engine.h" -#include "tagua.h" +class IMoveSerializer; class QRegExp; +class Variant; /** * @brief A shogi engine compatible with gnushogi. @@ -46,6 +47,7 @@ Q_OBJECT Features m_features; bool m_analysing; + IMoveSerializer* m_serializer; static QRegExp m_move_pattern; protected: @@ -63,7 +65,9 @@ public: /** * Create an gnushogi compatible engine. */ - GNUShogiEngine(const QString& path, const QStringList& arguments); + GNUShogiEngine(Variant* variant, + const QString& path, + const QStringList& arguments); /** * Terminate the engine. @@ -73,16 +77,16 @@ public: /** * Send a move to the engine. */ - virtual void sendMove(AbstractMove::Ptr move, AbstractPosition::Ptr ref); + virtual void sendMove(const Move& move, const StatePtr& ref); /** * Back up a move. * @param pos The position before the move. Used if the engine * does not support the undo command. */ - virtual void backUp(AbstractPosition::Ptr pos); + virtual void backUp(const StatePtr& pos); - virtual void setBoard(AbstractPosition::Ptr pos, int halfmove, int fullmove); + virtual void setBoard(const StatePtr& pos); /** diff --git a/src/graphicalapi.h b/src/graphicalapi.h deleted file mode 100644 index fcca342..0000000 --- a/src/graphicalapi.h +++ /dev/null @@ -1,115 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. -*/ - -#ifndef GRAPHICALAPI_H -#define GRAPHICALAPI_H - -#include -#include -#include "animationfactory.h" -#include "tagua.h" -#include -#include "fwd.h" - -typedef boost::shared_ptr SpritePtr; - -/** - * This class defines the interface that will be used by the animator to modify - * tagua graphics. - */ -class GraphicalAPI { -public: - virtual ~GraphicalAPI(); - - /** - * \return the current abstract position. - */ - virtual AbstractPosition::Ptr position() const = 0; - - /** - * \return a sprite at the position \a index in the graphical pool. - */ - virtual NamedSprite getSprite(const Point& p) = 0; - - /** - * Removes a sprite at the position \a index in the graphical pool. - * \return the newly created sprite. - */ - virtual NamedSprite takeSprite(const Point& p) = 0; - - /** - * Sets the piece at the position \a index in the graphical pool. - * \return the newly created sprite. - */ - virtual NamedSprite setPiece(const Point& p, const AbstractPiece* piece, bool show) = 0; - - /** - * Create a new piece, but do not add it to the graphical system. - * \return the newly created sprite. - */ - virtual NamedSprite createPiece(const Point& p, const AbstractPiece* piece, bool show) = 0; - - /** - * Sets the sprite at the position \a index in the graphical pool. - * \return the newly created sprite. - */ - virtual void setSprite(const Point& p, const NamedSprite& sprite) = 0; - - /** - * \return how many sprites are contained in the pool - */ - virtual int poolSize(int pool) = 0; - - /** - * \return the sprite at the position \a index in the graphical pool. - */ - virtual NamedSprite getPoolSprite(int pool, int index) = 0; - - /** - * Removes the sprite at the position \a index in the graphical pool. - */ - virtual void removePoolSprite(int pool, int index) = 0; - - /** - * Removes the sprite at the position \a index in the graphical pool (only for drops). - * \return the removed sprite. - */ - virtual NamedSprite takePoolSprite(int pool, int index) = 0; - - /** - * Inserts a sprite at the position \a index in the graphical pool. - * \return the newly created sprite. - */ - virtual NamedSprite insertPoolPiece(int pool, int index, const AbstractPiece* piece) = 0; - - /** - * \return the piece of the pool that has been dropped, or (-1,-1) - */ - virtual std::pair droppedPoolPiece() = 0; - - virtual AnimationPtr group(const QString& flags) const = 0; - virtual AnimationPtr appear(const NamedSprite& sprite, - const QString& flags) const = 0; - virtual AnimationPtr disappear(const NamedSprite& sprite, - const QString& flags) const = 0; - virtual AnimationPtr move(const NamedSprite& sprite, - const Point& destination, - const QString& flags) const = 0; - virtual AnimationPtr move(const NamedSprite& sprite, - int destination, - const QString& flags) const = 0; - virtual AnimationPtr morph(const NamedSprite& sprite1, - const NamedSprite& sprite2, - const QString& flags) const = 0; - - virtual AnimationPtr animate(const Animate::Scheme& scheme, Animate::AnimationType type) = 0; -}; - -#endif //GRAPHICALAPI_H diff --git a/src/graphicalgame.cpp b/src/graphicalgame.cpp index fe76aed..192317f 100644 --- a/src/graphicalgame.cpp +++ b/src/graphicalgame.cpp @@ -9,6 +9,13 @@ */ #include + +#include +#include +#include +#include +#include + #include "graphicalgame.h" #include "game.h" #include "game_p.h" @@ -50,14 +57,19 @@ public: GraphicalGame::GraphicalGame(GraphicalSystem* graphical, MoveList::Table* m) -: Game() +: Game(graphical->variant()) , m_graphical(graphical) , m_movelist(m) , m_anim_sequence(false) { m_action_state = 0; + + m_decorator = requestComponent( + graphical->variant(), "move_serializer/decorated"); + if(m_movelist) { m_movelist->reset(); - m_movelist->setLayoutStyle(graphical->m_variant->moveListLayout()); + // FIXME restore move list layout + m_movelist->setLayoutStyle(0 /*graphical->m_variant->moveListLayout()*/); m_movelist->setNotifier( static_cast(this) ); m_movelist->show(); } @@ -100,13 +112,14 @@ void GraphicalGame::onAddedInternal(const Index& ix, bool confirm_promotion) { Index index = ix; for(int i=at;i<(int)vec->size();i++) { Entry* e = &(*vec)[i]; - PositionPtr prev = position(index.prev()); + StatePtr prev = position(index.prev()); DecoratedMove mv( - (e->move && prev) ? - e->move->toString("decorated", prev) : + (e->move != Move() && prev) ? + m_decorator->serialize(e->move, prev.get()) : (e->position ? "(-)" : "???")); - int turn = prev ? prev->turn() : (index.totalNumMoves()+1)%2; + int turn = prev ? prev->turn()->index() : (index.totalNumMoves()+1)%2; + //mv += " " + QString::number(turn); m_movelist->setMove(index, turn, mv, e->comment, confirm_promotion); @@ -138,14 +151,18 @@ void GraphicalGame::onEntryChanged(const Index& at, int propagate) { Entry* pe = fetch(at.prev()); - AbstractPosition::Ptr last_pos; + StatePtr last_pos; if (pe) last_pos = pe->position; DecoratedMove mv( - (e->move && last_pos) ? - e->move->toString("decorated", last_pos) : + (e->move != Move() && last_pos) ? + m_decorator->serialize(e->move, last_pos.get()) : (e->position ? "(-)" : "???")); - int turn = last_pos ? last_pos->turn() : (at.totalNumMoves()+1)%2; + + int turn = last_pos ? + last_pos->turn()->index() : + (at.totalNumMoves()+1)%2; + m_movelist->setMove(at, turn, mv, e->comment); if(at == current && e->position) m_graphical->warp(e->move, e->position); @@ -227,14 +244,16 @@ void GraphicalGame::onCurrentIndexChanged(const Index& old_c) { if(can_animate) for(int i=1;i<=steps.first;i++) - if( !move(old_c.prev(i-1)) || !position(old_c.prev(i))) { + if( move(old_c.prev(i-1)) == Move() || + !position(old_c.prev(i))) { can_animate = false; break; } if(can_animate) for(int i=steps.second-1;i>=0;i--) - if( !move(current.prev(i)) || !position(current.prev(i))) { + if( move(current.prev(i)) == Move() || + !position(current.prev(i))) { can_animate = false; break; } diff --git a/src/graphicalgame.h b/src/graphicalgame.h index 6636b70..cfb90a7 100644 --- a/src/graphicalgame.h +++ b/src/graphicalgame.h @@ -19,6 +19,7 @@ class GraphicalSystem; class CtrlAction; +class IMoveSerializer; class UserEntity; class GraphicalGame; class ActionStateObserver; @@ -45,6 +46,8 @@ private: bool m_anim_sequence; int m_anim_sequence_max; + IMoveSerializer* m_decorator; + boost::shared_ptr m_ctrl; boost::weak_ptr m_listener_entity; boost::shared_ptr m_action_state_observer; diff --git a/src/graphicalsystem.cpp b/src/graphicalsystem.cpp index 0e8ba05..d28fe04 100644 --- a/src/graphicalsystem.cpp +++ b/src/graphicalsystem.cpp @@ -12,12 +12,16 @@ #include +#include +#include +#include #include #include +#include #include #include "chesstable.h" -#include "board.h" +#include "chessboard.h" #include "clock.h" #include "piecepool.h" #include "pointconverter.h" @@ -26,64 +30,55 @@ #include "pref_theme.h" #include "movelist_table.h" #include "mastersettings.h" -#include "tagua_legacy.h" using namespace boost; //BEGIN GraphicalSystem GraphicalSystem::GraphicalSystem(ChessTable* view, - AbstractPosition::Ptr startingPosition, - const VariantPtr& variant) + const StatePtr& startingPosition, + Variant* variant) : m_view(view) , m_variant(variant) { - m_pos = startingPosition->clone(); - Point s = m_pos->size(); + m_state = StatePtr(startingPosition->clone()); + Point s = m_state->board()->size(); for(int i=0;iset(Point(i,j), AbstractPiece::Ptr()); + m_state->board()->set(Point(i,j), Piece()); m_board = view->board(); - m_board->createGrid(m_pos->size(), m_pos->borderCoords()); + m_board->createGrid(m_state->board()->size(), m_state->board()->borderCoords()); m_board->reset(); m_view->pool(0)->clear(); m_view->pool(1)->clear(); - // setup namer - VariantAdaptor* adapt = dynamic_cast(m_variant.get()); - if (adapt) { - Variant* var = adapt->variant(); - m_namer = dynamic_cast(var->repository()->getComponent("namer")); - } - else - m_namer = 0; - - m_animator = m_variant->createAnimator(this); + m_namer = requestComponent(variant, "namer"); + IAnimatorFactory* fact = requestComponent( + variant, "animator_factory"); + m_animator = fact->create(this, m_namer); settings().onChange(this, "settingsChanged", "Loader::Theme"); settingsChanged(); if (startingPosition) - warp(AbstractMove::Ptr(), startingPosition); + warp(Move(), startingPosition); } GraphicalSystem::~GraphicalSystem() { + delete m_animator; } const PointConverter* GraphicalSystem::converter() const { return m_board->converter(); } -AbstractPosition::Ptr GraphicalSystem::position() const { - return m_pos; +const IState* GraphicalSystem::state() const { + return m_state.get(); } void GraphicalSystem::settingsChanged() { - /* recreate the animator to reload its settings */ - m_animator = m_variant->createAnimator(this); - ThemeInfo theme = PrefTheme::getBestTheme(m_variant, "pieces"); ThemeInfo sqtheme = PrefTheme::getBestTheme(m_variant, "squares"); ThemeInfo figtheme = PrefTheme::getBestTheme(m_variant, "figurines"); @@ -126,17 +121,18 @@ NamedSprite GraphicalSystem::takeSprite(const Point& p) { return retv; } -NamedSprite GraphicalSystem::setPiece(const Point& p, const AbstractPiece* piece, bool show) { +NamedSprite GraphicalSystem::setPiece(const Point& p, const Piece& piece, bool show) { return m_board->m_sprites[p] = createPiece(p, piece, show); } -NamedSprite GraphicalSystem::createPiece(const Point& p, const AbstractPiece* piece, bool show) { - Q_ASSERT(piece); +NamedSprite GraphicalSystem::createPiece(const Point& p, const Piece& piece, bool show) { + Q_ASSERT(piece != Piece()); if (!m_board->m_sprites.valid(p)) return NamedSprite(); - NamedSprite s(piece->name(), SpritePtr(new Sprite(m_board->loadSprite(piece->name()), - m_board->piecesGroup(), m_board->converter()->toReal(p))) ); + QString name = m_namer->name(piece); + NamedSprite s(name, SpritePtr(new Sprite(m_board->loadSprite(name), + m_board->piecesGroup(), m_board->converter()->toReal(p)))); if (show) s.sprite()->show(); return s; @@ -165,99 +161,94 @@ NamedSprite GraphicalSystem::takePoolSprite(int pool, int index) { return m_view->pool(pool)->takeSprite(index); } -NamedSprite GraphicalSystem::insertPoolPiece(int pool, int index, const AbstractPiece* piece) { +NamedSprite GraphicalSystem::insertPoolPiece(int pool, int index, const Piece& piece) { PiecePool *pl = m_view->pool(pool); - QPixmap px = pl->loadSprite(piece->name()); -// QPixmap px = pl->m_loader(piece->name()); + QString name = m_namer->name(piece); + QPixmap px = pl->loadSprite(name); - NamedSprite s( piece->name(), SpritePtr( new Sprite( px, pl, QPoint() ) ) ); + NamedSprite s(name, SpritePtr(new Sprite(px, pl, QPoint()))); pl->insertSprite(index, s); return s; } -std::pair GraphicalSystem::droppedPoolPiece() { - return std::pair(m_board->m_dropped_pool, m_board->m_dropped_index); -} - -AnimationPtr GraphicalSystem::animate(const Animate::Scheme& scheme, Animate::AnimationType type) { - return scheme.run(m_view->animationSettings(), converter(), type); +std::pair GraphicalSystem::droppedPoolPiece() { + return std::pair(m_board->m_dropped_pool, m_board->m_dropped_index); } -void GraphicalSystem::forward(const AbstractMove::Ptr& move, - const AbstractPosition::Ptr& pos, +void GraphicalSystem::forward(const Move& move, + const StatePtr& pos, const SpritePtr& /*movingSprite*/) { - AbstractPiece::Ptr sel1 = m_pos->get(m_board->selection); + Piece sel1 = m_state->board()->get(m_board->selection); - if (move) { - AnimationPtr animation = m_animator->forward(pos, move); - //??? animation->setChainAbortions(false); + if (move != Move()) { + AnimationPtr animation = m_animator->forward(move, pos.get()); m_board->enqueue(animation); - m_board->setTags("highlighting", move->toUserMove().from, move->toUserMove().to); + m_board->setTags("highlighting", move.src(), move.dst()); - m_pos->copyFrom(pos); + m_state->assign(pos.get()); } else - warp(AbstractMove::Ptr(), pos); + warp(Move(), pos); - AbstractPiece::Ptr sel2 = m_pos->get(m_board->selection); - if(!(sel1 && sel2 && sel1->equals(sel2))) + Piece sel2 = m_state->board()->get(m_board->selection); + if(!(sel1 != Piece() && sel2 != Piece() && sel1 == sel2)) m_board->cancelSelection(); m_board->cancelPremove(); - m_view->updateTurn(pos->turn()); + m_view->updateTurn(pos->turn()->index()); m_board->onPositionChanged(); } -void GraphicalSystem::back(const AbstractMove::Ptr& lastMove, - const AbstractMove::Ptr& move, - const AbstractPosition::Ptr& pos) { - AbstractPiece::Ptr sel1 = m_pos->get(m_board->selection); +void GraphicalSystem::back(const Move& lastMove, + const Move& move, + const StatePtr& state) { + Piece sel1 = m_state->board()->get(m_board->selection); - if (move) { - AnimationPtr animation = m_animator->back(pos, move); + if (move != Move()) { + AnimationPtr animation = m_animator->back(move, state.get()); //??? animation->setChainAbortions(false); m_board->enqueue(animation); - m_pos->copyFrom(pos); + m_state->assign(state.get()); } else - warp(lastMove, pos); + warp(lastMove, state); - if (lastMove) - m_board->setTags("highlighting", lastMove->toUserMove().from, lastMove->toUserMove().to); + if (lastMove != Move()) + m_board->setTags("highlighting", lastMove.src(), lastMove.dst()); else m_board->clearTags("highlighting"); - AbstractPiece::Ptr sel2 = m_pos->get(m_board->selection); - if(!(sel1 && sel2 && sel1->equals(sel2))) + Piece sel2 = m_state->board()->get(m_board->selection); + if(!(sel1 != Piece() && sel2 != Piece() && sel1 == sel2)) m_board->cancelSelection(); m_board->cancelPremove(); - m_view->updateTurn(pos->turn()); + m_view->updateTurn(state->turn()->index()); m_board->onPositionChanged(); } -void GraphicalSystem::warp(const AbstractMove::Ptr& lastMove, - const AbstractPosition::Ptr& pos) { +void GraphicalSystem::warp(const Move& lastMove, + const StatePtr& state) { - AbstractPiece::Ptr sel1 = m_pos->get(m_board->selection); + Piece sel1 = m_state->board()->get(m_board->selection); - AnimationPtr animation = m_animator->warp(pos); + AnimationPtr animation = m_animator->warp(state.get()); //??? animation->setChainAbortions(false); if (animation) { m_board->enqueue(animation); } - m_pos->copyFrom(pos); + m_state->assign(state.get()); - if (lastMove) - m_board->setTags("highlighting", lastMove->toUserMove().from, lastMove->toUserMove().to); + if (lastMove != Move()) + m_board->setTags("highlighting", lastMove.src(), lastMove.dst()); else m_board->clearTags("highlighting"); - AbstractPiece::Ptr sel2 = m_pos->get(m_board->selection); - if(!(sel1 && sel2 && sel1->equals(sel2))) + Piece sel2 = m_state->board()->get(m_board->selection); + if(!(sel1 != Piece() && sel2 != Piece() && sel1 == sel2)) m_board->cancelSelection(); - m_view->updateTurn(pos->turn()); + m_view->updateTurn(state->turn()->index()); m_board->onPositionChanged(); } @@ -269,28 +260,9 @@ void GraphicalSystem::adjustSprite(const Point& p) { m_board->enqueue(animation); } -void GraphicalSystem::setTurn(int turn) { - m_pos->setTurn(turn); - m_view->updateTurn(m_pos->turn()); -} - - -const IState* GraphicalSystem::state() const { - StateAdaptor* adaptor = dynamic_cast(m_pos.get()); - if (!adaptor) return 0; - return adaptor->state(); -} - -NamedSprite GraphicalSystem::setPiece(const Point& p, const Piece& piece, bool show) { - return m_board->m_sprites[p] = createPiece(p, piece, show); -} - -NamedSprite GraphicalSystem::createPiece(const Point& p, const Piece& piece, bool show) { - return createPiece(p, new PieceAdaptor(piece), show); -} - -NamedSprite GraphicalSystem::insertPoolPiece(int pool, int index, const Piece& piece) { - return insertPoolPiece(pool, index, new PieceAdaptor(piece)); +void GraphicalSystem::setTurn(const IColor* turn) { + m_state->setTurn(turn); + m_view->updateTurn(m_state->turn()->index()); } AnimationPtr GraphicalSystem::group(const QString&) const { @@ -357,3 +329,5 @@ AnimationPtr GraphicalSystem::morph(const NamedSprite& sprite1, return AnimationPtr(new PromotionAnimation(sprite1.sprite(), sprite2.sprite())); } + +Variant* GraphicalSystem::variant() { return m_variant; } diff --git a/src/graphicalsystem.h b/src/graphicalsystem.h index 01da1db..9e8d540 100644 --- a/src/graphicalsystem.h +++ b/src/graphicalsystem.h @@ -13,19 +13,23 @@ #include #include -#include "graphicalapi.h" -#include #include +#include +#include +#include +class ChessBoard; class ChessTable; -class Board; -class UserEntity; +class IAnimator; class INamer; class IState; +class Move; +class PointConverter; +class UserEntity; +class Variant; -class GraphicalSystem : public QObject, - public GraphicalAPI, - public TaguaAPI { +class GraphicalSystem : public QObject, + private TaguaAPI { Q_OBJECT public: @@ -34,16 +38,16 @@ public: ChessTable* m_view; /** The current board */ - Board* m_board; + ChessBoard* m_board; - /** The position data */ - AbstractPosition::Ptr m_pos; + /** The current state */ + StatePtr m_state; /** The variant specific animator */ - AbstractAnimator::Ptr m_animator; + IAnimator* m_animator; /** The current variant */ - VariantPtr m_variant; + Variant* m_variant; INamer* m_namer; @@ -71,7 +75,7 @@ private: /** * \return the current abstract position. (interface for GraphicalAPI) */ - virtual AbstractPosition::Ptr position() const; + virtual const IState* state() const; /** * \return a sprite at the position \a index in the graphical pool. (interface for GraphicalAPI) @@ -88,13 +92,13 @@ private: * Sets the piece at the position \a index in the graphical pool. (interface for GraphicalAPI) * \return the newly created sprite. */ - virtual NamedSprite setPiece(const Point& p, const AbstractPiece* piece, bool show); + virtual NamedSprite setPiece(const Point& p, const Piece& piece, bool show); /** * Create a new piece, but do not add it to the graphical system. * \return the newly created sprite. */ - virtual NamedSprite createPiece(const Point& p, const AbstractPiece* piece, bool show); + virtual NamedSprite createPiece(const Point& p, const Piece& piece, bool show); /** * Sets the sprite at the position \a index in the graphical pool. (interface for GraphicalAPI) @@ -128,56 +132,45 @@ private: * Inserts a sprite at the position \a index in the graphical pool. (interface for GraphicalAPI) * \return the newly created sprite. */ - virtual NamedSprite insertPoolPiece(int pool, int index, const AbstractPiece* piece); + virtual NamedSprite insertPoolPiece(int pool, int index, const Piece& piece); /** * \return the piece of the pool that has been dropped, or (-1,-1). (interface for GraphicalAPI) */ - virtual std::pair droppedPoolPiece(); + virtual std::pair droppedPoolPiece(); - - /** - * Create an animation from a scheme. - */ - virtual AnimationPtr animate(const Animate::Scheme& scheme, Animate::AnimationType type); public: /** Constructor */ - GraphicalSystem(ChessTable* view, AbstractPosition::Ptr startingPosition, - const VariantPtr& variant); + GraphicalSystem(ChessTable* view, const StatePtr& startingPosition, Variant* variant); virtual ~GraphicalSystem(); /** Sets the reference entity */ void setup(const boost::shared_ptr& entity); /** Goes forward playing a move, that has to be legal and checked */ - void forward(const AbstractMove::Ptr& move, - const AbstractPosition::Ptr& pos, + void forward(const Move& move, + const StatePtr& pos, const SpritePtr& = SpritePtr()); /** Goes back undoing a move, that has to be legal and checked */ - void back(const AbstractMove::Ptr& lastMove, - const AbstractMove::Ptr& move, - const AbstractPosition::Ptr& pos); + void back(const Move& lastMove, + const Move& move, + const StatePtr& pos); /** Warps to a new position */ - void warp(const AbstractMove::Ptr& lastMove, - const AbstractPosition::Ptr& pos); + void warp(const Move& lastMove, + const StatePtr& pos); /** Adjusts a sprite to the correct position */ void adjustSprite(const Point& p); /** Sets the current turn */ - void setTurn(int turn); + void setTurn(const IColor* turn); // TAGUA API const INamer* namer() const { return m_namer; } - - virtual const IState* state() const; - virtual NamedSprite setPiece(const Point&, const Piece&, bool); - virtual NamedSprite createPiece(const Point&, const Piece&, bool); - virtual NamedSprite insertPoolPiece(int, int, const Piece&); // TODO: factor the following animation related functions // out into a separate class @@ -195,6 +188,8 @@ public: virtual AnimationPtr morph(const NamedSprite& sprite1, const NamedSprite& sprite2, const QString& flags) const; + + Variant* variant(); }; diff --git a/src/hlvariant/animator.h b/src/hlvariant/animator.h index 8b4ea42..b2a62b2 100644 --- a/src/hlvariant/animator.h +++ b/src/hlvariant/animator.h @@ -13,9 +13,7 @@ #include #include "animation.h" -#include "fwd.h" #include "variantdata.h" -#include "animationfactory.h" #include "common.h" class NamedSprite; diff --git a/src/hlvariant/dropanimator.h b/src/hlvariant/dropanimator.h index fec4069..0533587 100644 --- a/src/hlvariant/dropanimator.h +++ b/src/hlvariant/dropanimator.h @@ -1,4 +1,4 @@ -/* +w/* Copyright (c) 2007 Paolo Capriotti (c) 2007 Maurizio Monge @@ -13,7 +13,6 @@ #define HLVARIANT__DROPANIMATOR_H #include -#include "animationfactory.h" #include "common.h" namespace HLVariant { diff --git a/src/hlvariant/graphicalapi_unwrapped.h b/src/hlvariant/graphicalapi_unwrapped.h index 41b34c8..c250719 100644 --- a/src/hlvariant/graphicalapi_unwrapped.h +++ b/src/hlvariant/graphicalapi_unwrapped.h @@ -11,7 +11,7 @@ #ifndef HLVARIANT__UNWRAPPEDGRAPHICALAPI_H #define HLVARIANT__UNWRAPPEDGRAPHICALAPI_H -#include "graphicalapi.h" +#include #include "variantdata.h" namespace HLVariant { diff --git a/src/hlvariant/movefactory.h b/src/hlvariant/movefactory.h index 0aed44b..f1762a6 100644 --- a/src/hlvariant/movefactory.h +++ b/src/hlvariant/movefactory.h @@ -11,7 +11,6 @@ #ifndef HLVARIANT__MOVEFACTORY_H #define HLVARIANT__MOVEFACTORY_H -#include "tagua.h" #include "usermove.h" #include "variantdata.h" diff --git a/src/hlvariant/tagua_wrapped.h b/src/hlvariant/tagua_wrapped.h index ac4c6dd..7e01174 100644 --- a/src/hlvariant/tagua_wrapped.h +++ b/src/hlvariant/tagua_wrapped.h @@ -1,8 +1,6 @@ #ifndef HLVARIANT__TAGUA_WRAPPED_H #define HLVARIANT__TAGUA_WRAPPED_H -#include "tagua.h" -#include "fwd.h" #include "movefactory.h" #include "nopool.h" #include "variantdata.h" diff --git a/src/icsapi.h b/src/icsapi.h index be12416..2f232e3 100644 --- a/src/icsapi.h +++ b/src/icsapi.h @@ -11,7 +11,7 @@ #ifndef ICSAPI_H #define ICSAPI_H -#include "tagua.h" +#include class ICSAPI { public: @@ -26,7 +26,7 @@ public: * \param bqCastle Queen-side castling is possible for black * \param ep En-passant square. */ - virtual PositionPtr createChessboard( + virtual StatePtr createChessboard( int turn, bool wkCastle, bool wqCastle, @@ -41,7 +41,7 @@ public: * meaning is defined by the variant. * \return A newly created piece. */ - virtual PiecePtr createPiece(const QString& description) = 0; + virtual Piece createPiece(const QString& description) = 0; /** * Parse ICS verbose notation. @@ -49,17 +49,7 @@ public: * \param turn The player who is moving. * \return A move object corresponding to the given verbose representation. */ - virtual MovePtr parseVerbose(const QString& str, const PositionPtr& ref) = 0; -}; - -// wrapper - -template -class WrappedICSAPI : public ICSAPI { -public: - virtual PositionPtr createChessboard(int, bool, bool, bool, bool, const Point&); - virtual PiecePtr createPiece(const QString& description); - virtual MovePtr parseVerbose(const QString&, const PositionPtr&) { return MovePtr(); } + virtual Move parseVerbose(const QString& str, const StatePtr& ref) = 0; }; #endif // ICSAPI_H diff --git a/src/graphicalapi.cpp b/src/icsapi_fwd.h similarity index 67% rename from src/graphicalapi.cpp rename to src/icsapi_fwd.h index 374cb61..19a901d 100644 --- a/src/graphicalapi.cpp +++ b/src/icsapi_fwd.h @@ -1,6 +1,5 @@ /* Copyright (c) 2007 Paolo Capriotti - (c) 2007 Maurizio Monge This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -8,7 +7,12 @@ (at your option) any later version. */ -#include "graphicalapi.h" +#ifndef ICSAPI_FWD_H +#define ICSAPI_FWD_H -GraphicalAPI::~GraphicalAPI() { } +#include +class ICSAPI; +typedef boost::shared_ptr ICSAPIPtr; + +#endif // ICSAPI_FWD_H diff --git a/src/icsconnection.cpp b/src/icsconnection.cpp index 7d2f8d1..0fe6e6f 100644 --- a/src/icsconnection.cpp +++ b/src/icsconnection.cpp @@ -12,6 +12,10 @@ #include #include +#include +#include +#include + #include "icsconnection.h" #include "poolinfo.h" #include "positioninfo.h" @@ -239,7 +243,7 @@ void ICSConnection::process(QString str) { PoolInfo pool_info(m_games, str); if (pool_info.m_valid) { // BROKEN - if (!pool_info.m_added_piece) { + if (pool_info.m_added_piece != Piece()) { if (shared_ptr listener = m_games[pool_info.m_game_num].listener.lock()) listener->notifyPool(pool_info); } @@ -300,7 +304,7 @@ void ICSConnection::process(QString str) { case MoveListMoves: if (test(move_list_terminator, str)){ if (shared_ptr listener = m_games[m_move_list_game_num].listener.lock()) { - AbstractPosition::Ptr p; + StatePtr p; if (m_move_list_position_info) p = m_move_list_position_info->position; else { @@ -309,8 +313,10 @@ void ICSConnection::process(QString str) { ERROR("BUG: Received move list for unknown game " << m_move_list_game_num); } else { - VariantPtr variant = gi->second.variant; - p = variant->createPosition(); + Variant* variant = gi->second.variant; + IStateFactory* fact = requestComponent( + variant, "state_factory"); + p = StatePtr(fact->createState()); p->setup(); } } diff --git a/src/icsgamedata.cpp b/src/icsgamedata.cpp index 2f5fb09..c4d66c8 100644 --- a/src/icsgamedata.cpp +++ b/src/icsgamedata.cpp @@ -9,9 +9,7 @@ */ #include "icsgamedata.h" -#include "tagua.h" #include "gameinfo.h" -#include "variants.h" ICSGameData::ICSGameData() : index(0) { @@ -24,14 +22,5 @@ ICSGameData::ICSGameData(int index, const QString& type) } void ICSGameData::setType(const QString& type) { - variant = Variants::instance().get(GameInfo::variantCode(type)); - if (!variant) { - ERROR("BUG: No variant corresponding to " << type); - } - else if (!(icsapi = variant->icsAPI())) { - // There's no ICSAPI in this variant. - // This means we cannot use it for ICS stuff, so we - // fall back to the dummy variant. - // TODO - } + // TODO ICSGameData::setType } diff --git a/src/icsgamedata.h b/src/icsgamedata.h index d3e3933..077b1f4 100644 --- a/src/icsgamedata.h +++ b/src/icsgamedata.h @@ -14,14 +14,13 @@ #include #include -#include "fwd.h" +class Variant; class ICSListener; struct ICSGameData { int index; - VariantPtr variant; - ICSAPIPtr icsapi; + Variant* variant; boost::weak_ptr listener; ICSGameData(); diff --git a/src/premove.h b/src/icslistener.h similarity index 50% rename from src/premove.h rename to src/icslistener.h index 5bc8e9b..4b1a1d1 100644 --- a/src/premove.h +++ b/src/icslistener.h @@ -8,20 +8,21 @@ (at your option) any later version. */ -#ifndef PREMOVE_H -#define PREMOVE_H +#ifndef ICSLISTENER_H +#define ICSLISTENER_H -#include "tagua.h" +#include -class Premove { -protected: - AbstractMove::Ptr m_move; -public: - Premove(AbstractMove::Ptr move); - virtual ~Premove() { } +class PositionInfo; +class PoolInfo; +class PGN; - virtual AbstractMove::Ptr execute(AbstractPosition::Ptr); - virtual class NormalUserMove toUserMove() const; +class ICSListener { +public: + virtual ~ICSListener() { } + virtual void notifyStyle12(const PositionInfo&, bool is_starting = false) = 0; + virtual void notifyPool(const PoolInfo&) = 0; + virtual void notifyMoveList(int, const StatePtr&, const PGN&) = 0; }; -#endif // PREMOVE_H +#endif // ICSLISTENER_H diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c65c0da..66b6b24 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -29,6 +29,10 @@ #include #include +#include +#include +#include + #include "actioncollection.h" #include "chesstable.h" #include "console.h" @@ -80,7 +84,8 @@ MainWindow::MainWindow(const QString& variant) addDockWidget(Qt::LeftDockWidgetArea, movelist_dock, Qt::Vertical); movelist_dock->show(); - ChessTable* board = new ChessTable; + Variant* v = Variants::instance().get(variant); + ChessTable* board = new ChessTable(v); board->setFocus(); @@ -97,14 +102,13 @@ MainWindow::MainWindow(const QString& variant) settings().onChange(this, "settingsChanged"); connect(board, SIGNAL(error(ErrorCode)), this, SLOT(displayErrorMessage(ErrorCode))); - //BROKEN connect(board->clock(), SIGNAL(labelClicked(int)), &ui(), SLOT(setTurn(int))); setupActions(); setupGUI(); setupEngineMenu(); // start in edit game mode - newGame(variant, AbstractPosition::Ptr(), true); + newGame(v->name(), StatePtr(), true); updateVariantActions(); } @@ -301,16 +305,17 @@ void MainWindow::cleanupGame() { ui().end(); } -bool MainWindow::newGame(const QString& variantName, AbstractPosition::Ptr startingPos, +bool MainWindow::newGame(const QString& variantName, + const StatePtr& startingPos, bool newTab) { - VariantPtr variant = Variants::instance().get(variantName); + Variant* variant = Variants::instance().get(variantName); if (!variant) { WARNING("no variant " << variantName << " found"); variant = Variants::instance().get("chess"); } if (variant) { - ChessTable* board = newTab ? new ChessTable : table(); + ChessTable* board = newTab ? new ChessTable(variant) : table(); QString text = QString("Editing %1 game").arg(variant->name().toLower()); shared_ptr controller(new EditGameController( @@ -345,14 +350,16 @@ void MainWindow::editPosition() { void MainWindow::setupGame(const GameInfo* gameInfo, const PositionInfo& style12) { QString variantCode = gameInfo->variant(); - VariantPtr variant = Variants::instance().get(variantCode); + Variant* variant = Variants::instance().get(variantCode); shared_ptr controller(new EditGameController( table(), variant)); Q_ASSERT(style12.relation == PositionInfo::NotMyMove || style12.relation == PositionInfo::MyMove); // set opponent side - int side = (style12.relation == PositionInfo::MyMove ^ style12.turn == 0) ? 0 : 1; + const IColor* turn = style12.position->turn(); + const IColor* side = style12.relation == PositionInfo::MyMove ? + style12.position->opponent(turn) : turn; if (controller->addICSPlayer(side, style12.gameNumber, m_connection)) { ui().setController(controller); @@ -378,10 +385,11 @@ void MainWindow::setupExaminedGame(const GameInfo* gameInfo, const PositionInfo& } void MainWindow::setupObservedGame(const GameInfo* g, const PositionInfo& style12) { - std::auto_ptr board(new ChessTable); + Variant* variant = Variants::instance().get(g->variant()); + std::auto_ptr board(new ChessTable(variant)); shared_ptr controller(new EditGameController( - board.get(), Variants::instance().get(g->variant()))); + board.get(), variant)); if (controller->setObserveMode(style12.gameNumber, m_connection)) { board->setPlayers(Player(style12.whitePlayer, -1), Player(style12.blackPlayer, -1)); @@ -398,7 +406,7 @@ void MainWindow::setupPGN(const QString& s) { PGN pgn(s); std::map::const_iterator var = pgn.m_tags.find("Variant"); - VariantPtr variant; + Variant* variant; if (var == pgn.m_tags.end()) { variant = Variants::instance().get("chess"); @@ -628,10 +636,10 @@ void MainWindow::prompt() { } void MainWindow::newGame() { - AbstractPosition::Ptr pos = ui().position(); + StatePtr pos = ui().position(); scoped_ptr dialog(new NewGame(this)); if (dialog->exec() == QDialog::Accepted) { - if (!newGame(dialog->variant(), PositionPtr(), dialog->newTab())) + if (!newGame(dialog->variant(), StatePtr(), dialog->newTab())) QMessageBox::information(this, i18n("Error"), i18n("Error creating game")); } } diff --git a/src/mainwindow.h b/src/mainwindow.h index 227d62a..cea3633 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -21,6 +21,8 @@ #include #include "boost/shared_ptr.hpp" +#include + #include "ui.h" #include "common.h" #include "export.h" @@ -118,7 +120,7 @@ public Q_SLOTS: void setupExaminedGame(const GameInfo*, const PositionInfo&); void setupObservedGame(const GameInfo*, const PositionInfo&); void newGame(); - bool newGame(const QString& var, AbstractPosition::Ptr, bool); + bool newGame(const QString& var, const StatePtr&, bool); void loadGame(); void saveGame(); void saveGameAs(); diff --git a/src/movefactory.h b/src/movefactory.h index 036cc08..b309a57 100644 --- a/src/movefactory.h +++ b/src/movefactory.h @@ -11,7 +11,6 @@ #ifndef MOVEFACTORY_H #define MOVEFACTORY_H -#include "tagua.h" #include "usermove.h" template diff --git a/src/piecepool.cpp b/src/piecepool.cpp index 7cb376f..5d04688 100644 --- a/src/piecepool.cpp +++ b/src/piecepool.cpp @@ -9,12 +9,17 @@ */ -#include "board.h" #include "piecepool.h" -PiecePool::PiecePool(int num, Board* b, const AnimationSettings& animSettings, KGameCanvasAbstract* parent) +#include +#include +#include "chessboard.h" + +PiecePool::PiecePool(const IColor* pool, ChessBoard* b, + const AnimationSettings& animSettings, + KGameCanvasAbstract* parent) : ClickableCanvas(parent) -, m_pool_num(num) +, m_pool(pool) , m_board(b) , m_flipped(false) , m_square_size(0) @@ -92,10 +97,6 @@ void PiecePool::clear() { m_sprites.clear(); } -void PiecePool::animate(const Animate::Pool::Scheme& scheme, Animate::AnimationType type) { - m_main_animation->addAnimation(scheme.run(m_anim_settings, this, type)); -} - void PiecePool::insertSprite(int index, const NamedSprite& nsprite) { if(m_dragged && index > m_dragged_index) index--; @@ -110,12 +111,15 @@ void PiecePool::insertSprite(int index, const NamedSprite& nsprite) { for(int i = m_sprites.size()-1; i > index; i--) { // double speed = (1.0 + 1.0 / (i - index + 1)) * 0.4; m_sprites[i] = m_sprites[i-1]; - animate(Animate::Pool::move(m_sprites[i], i)); + // FIXME restore pool animations +// animate(Animate::Pool::move(m_sprites[i], i)); } m_sprites[index] = nsprite; - animate(Animate::Pool::move(m_sprites[index], index), Animate::Instant); - animate(Animate::Pool::appear(m_sprites[index])); + + // FIXME restore pool animations +// animate(Animate::Pool::move(m_sprites[index], index), Animate::Instant); +// animate(Animate::Pool::appear(m_sprites[index])); } @@ -188,12 +192,14 @@ NamedSprite PiecePool::takeSpriteAt(int index) { NamedSprite retv = m_sprites[index]; if (!retv) return NamedSprite(); - animate(Animate::Pool::disappear(retv)); + // FIXME restore pool animations +// animate(Animate::Pool::disappear(retv)); for(int i = index; i < (int)m_sprites.size()-1; i++) { // double speed = (1.0 + 1.0 / (i - index + 1)) * 0.4; m_sprites[i] = m_sprites[i+1]; - animate(Animate::Pool::move(m_sprites[i], i)); + // FIXME restore pool animations +// animate(Animate::Pool::move(m_sprites[i], i)); } m_sprites.resize(m_sprites.size()-1); @@ -207,7 +213,8 @@ void PiecePool::cancelDragging(bool fadeOff) { if (fadeOff) { NamedSprite phantom = duplicate(m_dragged); - animate(Animate::Pool::disappear(phantom)); + // FIXME restore pool animations +// animate(Animate::Pool::disappear(phantom)); } m_dragged.sprite()->putInCanvas(this); @@ -227,7 +234,8 @@ void PiecePool::flipAndMoveBy(QPoint p) { for(int i=0;i<(int)m_sprites.size(); i++) { SpritePtr p = m_sprites[i].sprite(); p->setPixmap(loadSprite(m_sprites[i].name())); - animate(Animate::Pool::move(m_sprites[i], i), Animate::Instant); + // FIXME restore pool animations +// animate(Animate::Pool::move(m_sprites[i], i), Animate::Instant); } } @@ -251,13 +259,13 @@ void PiecePool::onMouseRelease(const QPoint& pos, int button) { if (button != Qt::LeftButton || !m_dragged) return; - m_board->m_dropped_pool = m_pool_num; + m_board->m_dropped_pool = m_pool; m_board->m_dropped_index = m_dragged_index; /* did the board take this sprite? */ - m_board->dropOn( m_pool_num, m_dragged_index, pos + this->pos() - m_board->pos() ); + m_board->dropOn( m_pool, m_dragged_index, pos + this->pos() - m_board->pos() ); - m_board->m_dropped_pool = -1; + m_board->m_dropped_pool = 0; m_board->m_dropped_index = -1; cancelDragging(true); @@ -297,7 +305,7 @@ void PiecePool::onMouseMove(const QPoint& pos, int /*button*/) { m_dragged.sprite()->moveTo(pos + this->pos() - m_board->pos() - QPoint(m_dragged.sprite()->pixmap().width(), m_dragged.sprite()->pixmap().height() ) / 2 ); - m_board->draggingOn( m_pool_num, m_dragged_index, pos + this->pos() - m_board->pos() ); + m_board->draggingOn( m_pool, m_dragged_index, pos + this->pos() - m_board->pos() ); } } diff --git a/src/piecepool.h b/src/piecepool.h index fd042a9..2d2a490 100644 --- a/src/piecepool.h +++ b/src/piecepool.h @@ -12,13 +12,16 @@ #define PIECEPOOL_H #include -#include "animationfactory.h" #include "clickablecanvas.h" #include "indexconverter.h" #include "mainanimation.h" #include "namedsprite_utils.h" #include "pixmaploader.h" +class AnimationSettings; +class ChessBoard; +class IColor; + /** * @class PiecePool * @brief The class representing a crazyhouse-like piece pool. @@ -31,13 +34,13 @@ class PiecePool : public ClickableCanvas , private IndexConverter { private: - int m_pool_num; + const IColor* m_pool; /** displayed m_sprites */ std::vector m_sprites; /** refrence board */ - class Board* m_board; + ChessBoard* m_board; /** true if flipped */ bool m_flipped; @@ -80,14 +83,12 @@ private: /** finds to which index corresponds the point p, or -1 if corresponds to none */ virtual int toLogical(const QPoint& p) const; - - void animate(const Animate::Pool::Scheme& scheme, Animate::AnimationType type = Animate::Normal); public: friend class GraphicalSystem; friend class ChessTable; /** Constructor, requires the board the pool will be attached to */ - PiecePool(int num, Board* b, const AnimationSettings&, KGameCanvasAbstract* parent); + PiecePool(const IColor* pool, ChessBoard* b, const AnimationSettings&, KGameCanvasAbstract* parent); ~PiecePool(); /** returns the sprite loader */ diff --git a/src/poolinfo.h b/src/poolinfo.h index 320e22b..29ec0a0 100644 --- a/src/poolinfo.h +++ b/src/poolinfo.h @@ -13,8 +13,9 @@ #include #include +#include +#include #include "index.h" -#include "tagua.h" #include "icsgamedata.h" class PoolInfo { @@ -22,8 +23,8 @@ public: bool m_valid; int m_game_num; Index m_pos_index; - PoolPtr m_pool[2]; - PiecePtr m_added_piece; + PoolCollectionPtr m_pools; + Piece m_added_piece; static QRegExp s_pattern; PoolInfo(const std::map& games, const QString& s); diff --git a/src/positioninfo.cpp b/src/positioninfo.cpp index a881802..9977f1c 100644 --- a/src/positioninfo.cpp +++ b/src/positioninfo.cpp @@ -9,6 +9,12 @@ */ #include "positioninfo.h" + +#include + +#include +#include + #include "variants.h" #include "gameinfo.h" #include "icsapi.h" @@ -88,13 +94,14 @@ bool PositionInfo::load(std::map& games, const QString& str) { ICSAPIPtr icsapi; if (gi == games.end()) { - WARNING("Received style12 for unknown game " << gn); + kWarning() << "Received style12 for unknown game" << gn; // create a gameinfo of type "dummy" gi = games.insert(std::make_pair(gn, ICSGameData(gn, ""))).first; new_game = true; } - icsapi = gi->second.icsapi; + // FIXME load icsapi +// icsapi = gi->second.icsapi; std::vector rows; for (uint i = 0; i < 8; ++i) @@ -120,7 +127,7 @@ bool PositionInfo::load(std::map& games, const QString& str) { position = icsapi->createChessboard(turn, wkCastle, wqCastle, bkCastle, bqCastle, enPassantSquare); for (uint i = 0; i < 8; ++i) { for (uint j = 0; j < rows[i].row.size(); ++j) { - position->set(Point(j,i), rows[i].row[j]); + position->board()->set(Point(j,i), rows[i].row[j]); } } diff --git a/src/positioninfo.h b/src/positioninfo.h index 4bfbc97..db633b8 100644 --- a/src/positioninfo.h +++ b/src/positioninfo.h @@ -16,9 +16,11 @@ #include #include -#include "tagua.h" +#include +#include +#include +#include "icsapi_fwd.h" #include "icsgamedata.h" -#include "fwd.h" class PositionInfo { public: @@ -62,7 +64,7 @@ public: class PositionRow { public: - std::vector row; + std::vector row; explicit PositionRow(const ICSAPIPtr& icsapi, const QString& str); }; @@ -85,7 +87,7 @@ public: int gameNumber; QString whitePlayer; QString blackPlayer; - AbstractPosition::Ptr position; + StatePtr position; Point enPassantSquare; Relation relation; diff --git a/src/pref_theme.cpp b/src/pref_theme.cpp index 93c0fa3..203abcd 100644 --- a/src/pref_theme.cpp +++ b/src/pref_theme.cpp @@ -10,17 +10,21 @@ #include #include -#include #include +#include + +#include #include +#include + #include "foreach.h" #include "mastersettings.h" #include "luaapi/loader.h" #include "variants.h" -#include "tagua.h" #include "pref_theme.h" +typedef boost::shared_ptr VariantPtr; PrefTheme::ThemeInfoList PrefTheme::to_theme_info_list(const QStringList& files, const Settings& s) { //std::cout << "about to examine " << files.size() << " desktop files" << std::endl; @@ -51,9 +55,8 @@ PrefTheme::ThemeInfoList PrefTheme::to_theme_info_list(const QStringList& files, ThemeInfo theme_info = ThemeInfo::fromDesktopFile(files[i]); all_themes << theme_info; - if (theme_info.name.isEmpty()) { - ERROR("No name property in " << files[i]); - } + if (theme_info.name.isEmpty()) + kError() << "No name property in" << files[i]; } } @@ -88,7 +91,7 @@ OptList PrefTheme::get_file_options(const QString& f, bool reload_defaults) { lua_context.runFile(f); if(lua_context.error()) { - ERROR(lua_context.errorString()); + kError() << lua_context.errorString(); lua_context.clearError(); m_new_theme_options[f] = OptList(); @@ -97,7 +100,7 @@ OptList PrefTheme::get_file_options(const QString& f, bool reload_defaults) { OptList o = lua_context.getValue("options", 0, NULL, true); if(lua_context.error()) { - ERROR(lua_context.errorString()); + kError() << lua_context.errorString(); lua_context.clearError(); } @@ -240,7 +243,7 @@ void PrefTheme::update_list_view(QListWidget* list, const ThemeInfoList& themes, void PrefTheme::variantChanged() { QString category = comboVariant->itemData(comboVariant->currentIndex()).toString(); - VariantFactory* vi = Variants::instance().getFactory(category); + VariantPtr vi(Variants::instance().get(category)); if (!vi) { for (CategoryMap::iterator cit = m_categories.begin(); cit != m_categories.end(); ++cit) { @@ -257,7 +260,7 @@ void PrefTheme::variantChanged() { } QString variant_name = vi->name(); - QString variant_proxy = vi->themeProxy(); + QString variant_proxy = vi->proxy(); SettingMap variants = settings().group("variants").map("variant", "name"); Settings var = variants.insert(variant_name); bool check_proxy = variant_name != variant_proxy; @@ -287,15 +290,15 @@ void PrefTheme::variantChanged() { } } -ThemeInfo PrefTheme::getBestTheme(const VariantPtr& vi, const QString& category) { +ThemeInfo PrefTheme::getBestTheme(Variant* vi, const QString& category) { QString tag = category + "-theme"; QString deftag = category + "-use-def"; QString variant_name = vi->name(); - QString variant_proxy_name = vi->themeProxy(); + QString variant_proxy_name = vi->proxy(); SettingMap variants = settings().group("variants").map("variant", "name"); - if (variant_name != vi->themeProxy() && + if (variant_name != vi->proxy() && (variants.insert(variant_name)[deftag] | true) ) - variant_name = vi->themeProxy(); + variant_name = vi->proxy(); Settings var_settings = variants.insert(variant_name); if (var_settings[tag] && QFile::exists(var_settings[tag].value()) ) { @@ -370,7 +373,7 @@ void PrefThemeCategory::themeChanged() { m_label->setText(m_themes[i].description); QString c = m_parent->comboVariant->itemData(m_parent->comboVariant->currentIndex()).toString(); - VariantFactory* vi = Variants::instance().getFactory(c); + VariantPtr vi(Variants::instance().get(c)); if(vi) m_new_themes[vi->name()] = m_themes[i].desktopFile; @@ -397,7 +400,7 @@ void PrefThemeCategory::themeChecked(bool ck) { m_label->setEnabled(!ck); QString c = m_parent->comboVariant->itemData(m_parent->comboVariant->currentIndex()).toString(); - VariantFactory* vi = Variants::instance().getFactory(c); + VariantPtr vi(Variants::instance().get(c)); if (vi) m_new_use_def[vi->name()] = ck; themeChanged(); diff --git a/src/pref_theme.h b/src/pref_theme.h index 034038b..f6044f7 100644 --- a/src/pref_theme.h +++ b/src/pref_theme.h @@ -20,11 +20,10 @@ #include "themeinfo.h" #include "ui_pref_theme.h" #include "ui_pref_theme_page.h" -#include "fwd.h" -class VariantInfo; class Settings; class QAction; +class Variant; class PrefTheme : public QWidget , private Ui::PrefTheme { @@ -52,7 +51,7 @@ public: void apply(); - static ThemeInfo getBestTheme(const VariantPtr& vi, const QString&); + static ThemeInfo getBestTheme(Variant* vi, const QString&); }; diff --git a/src/premove.cpp b/src/premove.cpp deleted file mode 100644 index 82b613c..0000000 --- a/src/premove.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. -*/ - -#include "premove.h" -#include "usermove.h" - -Premove::Premove(AbstractMove::Ptr move) -: m_move(move) { } - -AbstractMove::Ptr Premove::execute(AbstractPosition::Ptr position) { - if (position->testMove(m_move)) { - return m_move; - } - else return AbstractMove::Ptr(); -} - -NormalUserMove Premove::toUserMove() const { - return m_move->toUserMove(); -} - diff --git a/src/tagua.h b/src/tagua.h deleted file mode 100644 index b26b234..0000000 --- a/src/tagua.h +++ /dev/null @@ -1,397 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. -*/ - -#ifndef LOWLEVEL_H -#define LOWLEVEL_H - -#include -#include -#include -#include -#include -#include "usermove.h" -#include "index.h" -#include "option.h" -#include "decoratedmove.h" -#include "interactiontype.h" -#include "turnpolicy.h" -#include "fwd.h" - -class GraphicalAPI; -class ICSAPI; -class ActionCollection; - -/** - * @file tagua.h - * @brief Low level abstract classes used by the interface framework. - * - * This file includes definitions for abstract classes used from within - * the interface framework to access variant details using dynamic time - * polymorphism. - * - * One of the big problems with this approach is what I call 'lack of - * functional dependency support' for runtime polymorphism. - * What is missing is the ability to override a virtual member function - * in a derived class changing its signature. Or better, it is possible - * to only change its return type with a covariant type, while it would - * often be useful to change any of its argument types with covariant or - * even controvariant types. - * For controvariant types, I foresee no problems, and all type checking - * can be done statically (of course the method has to be dynamically - * bound) as they are now. - * When using covariant types, the compiler could insert appropriate - * dynamic cast's and type checks in the calling code, when there's no - * way to know the exact type of an argument at compile time. - * - * The file @ref tagua_wrapped.h tries to address this problem doing part - * of the compiler's work. One exception is that type checking code - * is inserted in a method wrapper, instead of the calling place, so - * even calling a method of a noncasted instance would cause some - * upcast / downcast overhead; this is negligible, though, because - * that scenario is rather unlikely, since wrapped classes are always - * kept in variables of abstract types. - */ - - -/** - * @brief A superclass for all the piece classes. - */ -class AbstractPiece { -public: - typedef boost::shared_ptr Ptr; - virtual ~AbstractPiece() { } - - /** - * Piece equality. Return false if the second pointer - * is null. - */ - virtual bool equals(const PiecePtr& other) const = 0; - - /** - * Return a unique key for the piece. - * Used to store pixmaps. - */ - virtual QString name() const = 0; - - /** - * Create a deep copy of the piece. - */ - virtual PiecePtr clone() const = 0; -}; - - -/** - * @brief A superclass for all the move classes. - * - * An abstract move is simply something that can serialize - * itself either as SAN notation, or as coordinate notation. - */ -class AbstractMove { -public: - typedef boost::shared_ptr Ptr; - virtual ~AbstractMove() { } - - /** - * Serialize the move. - * \param rep The type of representation to use. - * \param ref A reference position. Its meaning is defined by the serializer. - */ - virtual QString toString(const QString& rep, const PositionPtr& ref) const = 0; - - /** - * Convert the move to a normal user move. Used to - * perform move highlighting. - */ - virtual NormalUserMove toUserMove() const = 0; - - /** - * Checks if the two moves are equal. - */ - virtual bool equals(const MovePtr& other) const = 0; - -}; - -/** - * @brief Superclass for pools - * - * A general interface for pools. - */ -class AbstractPool { -public: - typedef boost::shared_ptr Ptr; - virtual ~AbstractPool() {} - - /** - * \return the number of items in the pool - */ - virtual int size() = 0; - - /** - * Inserts a piece in the pool, preferably at the position \a pref_index. - * But the pool can be unpredictable and the piece can be placed at an arbitrary position. - * \return the position at which the item was placed. - */ - virtual int insert(int pref_index, const PiecePtr& piece) = 0; - - /** - * Gets the piece at the position \a index in the pool. - */ - virtual PiecePtr get(int index) = 0; - - /** - * Removes the piece at the position \a index in the pool. - * \return the removed piece. - */ - virtual PiecePtr take(int index) = 0; -}; - - - -/** - * @brief A superclass for all the position classes. - * - * A general interface for positions. It is used from within - * the graphical framework to validate external input wrt - * the played variant. - */ -class AbstractPosition { -public: - typedef boost::shared_ptr Ptr; - typedef boost::shared_ptr PoolPtr; - virtual ~AbstractPosition() { } - - /** - * Return board size in logical units. This could be - * static if C++ permitted virtual static functions. - */ - virtual Point size() const = 0; - - /** - * Returns the text to be used for the border surrounding this position in a board - */ - virtual QStringList borderCoords() const = 0; - - /** - * Create the starting piece setup. - */ - virtual void setup() = 0; - - /** - * Retrieve the piece on square @a p. - * Return a null pointer if that square is empty. - */ - virtual PiecePtr get(const Point& p) const = 0; - - /** - * Set a piece on the board. - */ - virtual void set(const Point& p, const PiecePtr& piece) = 0; - - /** - * \return an interface to modify the pool of the board relative to \a player - */ - virtual PoolPtr pool(int player) = 0; - - /** - * Set a position pool, copying it from a given position. - */ - virtual void copyPoolFrom(const PositionPtr& pos) = 0; - - /** - * \return 1 if the piece can be moved, -1 if could be moved in the future (premove), or else 0. - */ - virtual InteractionType movable(const TurnPolicy::Collection& test, const Point& p) const = 0; - - /** - * \return 1 if this pool can be dropped, -1 if could be dropped in the future (premove), or else 0. - */ - virtual InteractionType droppable(const TurnPolicy::Collection& test, int) const = 0; - - /** - * \return an id corresponding to the player - * who is in turn. - */ - virtual int turn() const = 0; - - /** - * Sets the player on move - */ - virtual void setTurn(int) = 0; - - /** - * \return the player that just moved - */ - virtual int previousTurn() const = 0; - - /** - * Switch to the next player. - */ - virtual void switchTurn() = 0; - - /** - * Check move legality. Set move fields as needed. - * Return whether the move is legal. - * This function should return immediately if \a m - * has already been tested. - */ - virtual bool testMove(const MovePtr& m) const = 0; - - /** - * Execute move \a m. Assume that \a m is legal and tested. - */ - virtual void move(const MovePtr& m) = 0; - - /** - * Create a deep copy of the position. - */ - virtual PositionPtr clone() const = 0; - - /** - * Make the position equal to the given one. - */ - virtual void copyFrom(const PositionPtr&) = 0; - - /** - * Tests if two positions are equal. - */ - virtual bool equals(const PositionPtr& p) const = 0; - - /** - * Return a move from an algebraic notation, or a null pointer. - */ - virtual MovePtr getMove(const QString&) const = 0; - - /** - * Return a string representing the current state of the position. - */ - virtual QString state() const = 0; - - /** - * A piece somehow representing or related to the move - * which has to be drawn by the interface. - * Examples: - * * for one click moves, the piece that would - * appear on the square. - * * for promotions in chesslike variants, the promoted - * piece. - * @note Return a null pointer if the move has no valid - * piece hint defined. - */ - virtual PiecePtr moveHint(const MovePtr&) const = 0; - - /** - * Variant introspection - */ - virtual QString variant() const = 0; - - /** - * For debugging - */ - virtual void dump() const = 0; -}; - -class AnimationGroup; - -/** - * @brief An abstract superclass for all animator classes. - * - * An animator is a class which is given a difference between - * a graphical and a logical position, and schedules an animation - * which graphically does an update. - * If the difference is due to a move, the animator tries to - * create a smooth animation which could possibly work for many - * chess-like variants, and then fills in the gaps with a direct - * update. - */ -class AbstractAnimator { -public: - typedef boost::shared_ptr Ptr; - virtual ~AbstractAnimator() { } - - /** - * Sync the graphical position with the given position. - */ - virtual AnimationPtr warp(const PositionPtr&) = 0; - - /** - * Animate forward syncing to the given position. - */ - virtual AnimationPtr forward(const PositionPtr&, const MovePtr&) = 0; - - /** - * Animate back syncing to the given position. - */ - virtual AnimationPtr back(const PositionPtr&, const MovePtr&) = 0; -}; - - -class VariantInfo { -public: - virtual ~VariantInfo() { } - virtual PositionPtr createPosition() = 0; - virtual PositionPtr createCustomPosition(const OptList& l) = 0; - virtual void forallPieces(class PieceFunction&) = 0; - virtual int moveListLayout() const = 0; - virtual AnimatorPtr createAnimator(GraphicalAPI* graphical_api) = 0; - virtual MovePtr createNormalMove(const NormalUserMove&) = 0; - virtual MovePtr createDropMove(const DropUserMove&) = 0; - virtual MovePtr getVerboseMove(int turn, const class VerboseNotation&) const = 0; - - /** - * \return if moves are done by just clicking - */ - virtual bool simpleMoves() = 0; - - /** - * \return the name of the variant - */ - virtual QString name() const = 0; - - /** - * \return the name of the theme proxy variant, ie the variant whose theme can be used - * for the current one (for instance crazyhouse can use chess themes). - */ - virtual QString themeProxy() const = 0; - - /** - * \return the (subvariant) options that can be specified for position creation, such as board size, etc - */ - virtual OptList positionOptions() const = 0; - - /** - * \return The ICS API for this variant, or a null pointer, if the variant does not support - * ICS (in that case a Dummy variant will be used). - */ - virtual ICSAPIPtr icsAPI() const = 0; - - /** - * Setup a list of variant specific actions to be displayed on a game toolbar - * and menu. - */ - virtual ActionCollection* actions() = 0; - - /** - * Add variant specific information (i.e. promotionType) to a move. - */ - virtual void setupMove(NormalUserMove& m) const = 0; -}; - - -class VariantFactory { -public: - virtual ~VariantFactory() { } - virtual VariantInfo* createVariant() const = 0; - virtual QString name() const = 0; - virtual QString themeProxy() const = 0; - virtual bool hidden() const = 0; -}; - - -#endif // LOWLEVEL_H diff --git a/src/tagua_legacy.cpp b/src/tagua_legacy.cpp deleted file mode 100644 index 68ab696..0000000 --- a/src/tagua_legacy.cpp +++ /dev/null @@ -1,282 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. -*/ - -#include "tagua_legacy.h" - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "graphicalsystem.h" - -VariantAdaptor::VariantAdaptor(Variant* variant) -: m_variant(variant) { - m_state_factory = dynamic_cast( - m_variant->repository()->getComponent("state_factory")); - m_validator = dynamic_cast( - m_variant->repository()->getComponent("validator")); -} - -VariantInfo* VariantAdaptor::createVariant() const { - return const_cast(this); -} - -QString VariantAdaptor::name() const { - return m_variant->name(); -} - -QString VariantAdaptor::themeProxy() const { - return m_variant->proxy(); -} - - -bool VariantAdaptor::hidden() const { - return m_variant->hidden(); -} - -PositionPtr VariantAdaptor::createPosition() { - return PositionPtr(new StateAdaptor(m_state_factory->createState(), m_validator)); -} - -PositionPtr VariantAdaptor::createCustomPosition(const OptList& l) { - return createPosition(); -} - -void VariantAdaptor::forallPieces(class PieceFunction& f) { - // this function is never used -} - -int VariantAdaptor::moveListLayout() const { - return 0; // FIXME -} - -AnimatorPtr VariantAdaptor::createAnimator(GraphicalAPI* api) { - GraphicalSystem* s = dynamic_cast(api); - const INamer* namer = s->namer(); - IAnimatorFactory* fact = dynamic_cast( - m_variant->repository()->getComponent("animator_factory")); - return AnimatorPtr(new AnimatorAdaptor(fact->create(s, namer))); -} - -MovePtr VariantAdaptor::createNormalMove(const NormalUserMove& m) { - return MovePtr(new MoveAdaptor(Move(m.from, m.to, 0))); // FIXME promotion -} - -MovePtr VariantAdaptor::createDropMove(const DropUserMove& m) { - return MovePtr(new MoveAdaptor(Move(reinterpret_cast(m.pool), - m.piece_index, - m.to))); -} - -MovePtr VariantAdaptor::getVerboseMove(int turn, const class VerboseNotation&) const { - return MovePtr(); // FIXME -} - -bool VariantAdaptor::simpleMoves() { - return false; -} - -OptList VariantAdaptor::positionOptions() const { - return OptList(); -} - -ICSAPIPtr VariantAdaptor::icsAPI() const { - return ICSAPIPtr(); // FIXME -} - -ActionCollection* VariantAdaptor::actions() { - return 0; -} - -void VariantAdaptor::setupMove(NormalUserMove& m) const { } - -Variant* VariantAdaptor::variant() { return m_variant; } - - -MoveAdaptor::MoveAdaptor(const Move& move) -: m_move(move) { } - -QString MoveAdaptor::toString(const QString& rep, const PositionPtr& ref) const { - return ""; -} - -NormalUserMove MoveAdaptor::toUserMove() const { - return NormalUserMove(m_move.src(), m_move.dst()); -} - -bool MoveAdaptor::equals(const MovePtr& other_) const { - MoveAdaptor* other = dynamic_cast(other_.get()); - if (!other) return false; - return m_move == other->move(); -} - - -StateAdaptor::StateAdaptor(IState* state, IValidator* validator) -: m_state(state) -, m_validator(validator) { } - - - -Point StateAdaptor::size() const { - return m_state->board()->size(); -} - -QStringList StateAdaptor::borderCoords() const { - QStringList retv; - Point p = size(); - for (int i = 0; i < 8; i++) - retv << Point(i, 0).col(); - for (int i = 1; i <= 8; i++) - retv << QString::number(i); - return retv + retv; -} - -void StateAdaptor::setup() { - m_state->setup(); -} - -PiecePtr StateAdaptor::get(const Point& p) const { -// m_state->board()->get(p); - return PiecePtr(); // FIXME -} - -void StateAdaptor::set(const Point& p, const PiecePtr& piece) { - -// m_state->board()->set(p, piece); - // FIXME -} - -PoolPtr StateAdaptor::pool(int player) { - return PoolPtr(); // FIXME -} - -void StateAdaptor::copyPoolFrom(const PositionPtr& pos) { -} - -InteractionType StateAdaptor::movable(const TurnPolicy::Collection& test, const Point& p) const { - return Moving; // FIXME -} - -InteractionType StateAdaptor::droppable(const TurnPolicy::Collection& test, int) const { - return Moving; // FIXME -} - -int StateAdaptor::turn() const { - return reinterpret_cast(m_state->turn()); -} - -void StateAdaptor::setTurn(int) { - // FIXME -} - -int StateAdaptor::previousTurn() const { - return turn(); // FIXME -} - -void StateAdaptor::switchTurn() { - m_state->advanceTurn(); -} - -bool StateAdaptor::testMove(const MovePtr& m) const { - MoveAdaptor* move = dynamic_cast(m.get()); - if (!move) return false; - return m_validator->legal(m_state, move->move()); -} - -void StateAdaptor::move(const MovePtr& m) { - MoveAdaptor* move = dynamic_cast(m.get()); - if (move) { - m_state->move(move->move()); - } -} - -PositionPtr StateAdaptor::clone() const { - return PositionPtr(new StateAdaptor(m_state->clone(), m_validator)); -} - -void StateAdaptor::copyFrom(const PositionPtr&) { - // FIXME -} - -bool StateAdaptor::equals(const PositionPtr& p) const { - IState* state = dynamic_cast(p.get()); - if (!state) return false; - return m_state->equals(state); -} - -MovePtr StateAdaptor::getMove(const QString&) const { - return MovePtr(); // FIXME -} - -QString StateAdaptor::state() const { - return "playing"; // FIXME -} - -PiecePtr StateAdaptor::moveHint(const MovePtr&) const { - return PiecePtr(); // FIXME -} - -QString StateAdaptor::variant() const { - return ""; // FIXME -} - -void StateAdaptor::dump() const { - // FIXME -} - -IState* StateAdaptor::state() { - return m_state; -} - - -PieceAdaptor::PieceAdaptor(const Piece& piece) -: m_piece(piece) { } - -bool PieceAdaptor::equals(const PiecePtr& other_) const { - PieceAdaptor* other = dynamic_cast(other_.get()); - if (!other) return false; - return other->m_piece == m_piece; -} - -QString PieceAdaptor::name() const { - return m_piece.color()->name() + "_" + m_piece.type()->name(); - // FIXME use the correct component to compose piece name -} - -PiecePtr PieceAdaptor::clone() const { - return PiecePtr(new PieceAdaptor(m_piece)); -} - -AnimatorAdaptor::AnimatorAdaptor(IAnimator* animator) -: m_animator(animator) { } - -AnimationPtr AnimatorAdaptor::warp(const PositionPtr& pos) { - StateAdaptor* state = dynamic_cast(pos.get()); - return m_animator->warp(state->state()); -} - -AnimationPtr AnimatorAdaptor::forward(const PositionPtr& pos, const MovePtr& _move) { - StateAdaptor* state = dynamic_cast(pos.get()); - MoveAdaptor* move = dynamic_cast(_move.get()); - return m_animator->forward(move->move(), state->state()); -} - -AnimationPtr AnimatorAdaptor::back(const PositionPtr& pos, const MovePtr& _move) { - StateAdaptor* state = dynamic_cast(pos.get()); - MoveAdaptor* move = dynamic_cast(_move.get()); - return m_animator->back(move->move(), state->state()); -} diff --git a/src/tagua_legacy.h b/src/tagua_legacy.h deleted file mode 100644 index 22f11da..0000000 --- a/src/tagua_legacy.h +++ /dev/null @@ -1,118 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. -*/ - -#ifndef TAGUA_LEGACY_H -#define TAGUA_LEGACY_H - -#include -#include -#include "tagua.h" - -class IAnimator; -class IState; -class IStateFactory; -class IValidator; -class Variant; - -class VariantAdaptor : public VariantFactory, public VariantInfo { - Variant* m_variant; - IStateFactory* m_state_factory; - IValidator* m_validator; -public: - VariantAdaptor(Variant* variant); - - virtual VariantInfo* createVariant() const; - virtual QString name() const; - virtual QString themeProxy() const; - virtual bool hidden() const; - - virtual PositionPtr createPosition(); - virtual PositionPtr createCustomPosition(const OptList& l); - virtual void forallPieces(class PieceFunction&); - virtual int moveListLayout() const; - virtual AnimatorPtr createAnimator(GraphicalAPI* graphical_api); - virtual MovePtr createNormalMove(const NormalUserMove&); - virtual MovePtr createDropMove(const DropUserMove&); - virtual MovePtr getVerboseMove(int turn, const class VerboseNotation&) const; - virtual bool simpleMoves(); - virtual OptList positionOptions() const; - virtual ICSAPIPtr icsAPI() const; - virtual ActionCollection* actions(); - virtual void setupMove(NormalUserMove& m) const; - - Variant* variant(); -}; - -class MoveAdaptor : public AbstractMove { - Move m_move; -public: - MoveAdaptor(const Move& move); - - virtual QString toString(const QString& rep, const PositionPtr& ref) const; - virtual NormalUserMove toUserMove() const; - virtual bool equals(const MovePtr& other) const; - - Move& move() { return m_move; } - const Move& move() const { return m_move; } -}; - - -class StateAdaptor : public AbstractPosition { - IState* m_state; - IValidator* m_validator; -public: - StateAdaptor(IState* state, IValidator* validator); - - virtual Point size() const; - virtual QStringList borderCoords() const; - virtual void setup(); - virtual PiecePtr get(const Point& p) const; - virtual void set(const Point& p, const PiecePtr& piece); - virtual PoolPtr pool(int player); - virtual void copyPoolFrom(const PositionPtr& pos); - virtual InteractionType movable(const TurnPolicy::Collection& test, const Point& p) const; - virtual InteractionType droppable(const TurnPolicy::Collection& test, int) const; - virtual int turn() const; - virtual void setTurn(int); - virtual int previousTurn() const; - virtual void switchTurn(); - virtual bool testMove(const MovePtr& m) const; - virtual void move(const MovePtr& m); - virtual PositionPtr clone() const; - virtual void copyFrom(const PositionPtr&); - virtual bool equals(const PositionPtr& p) const; - virtual MovePtr getMove(const QString&) const; - virtual QString state() const; - virtual PiecePtr moveHint(const MovePtr&) const; - virtual QString variant() const; - virtual void dump() const; - - IState* state(); -}; - -class PieceAdaptor : public AbstractPiece { - Piece m_piece; -public: - PieceAdaptor(const Piece& piece); - virtual bool equals(const PiecePtr& other) const; - virtual QString name() const; - virtual PiecePtr clone() const; -}; - -class AnimatorAdaptor : public AbstractAnimator { - IAnimator* m_animator; -public: - AnimatorAdaptor(IAnimator* animator); - virtual AnimationPtr warp(const PositionPtr&); - virtual AnimationPtr forward(const PositionPtr&, const MovePtr&); - virtual AnimationPtr back(const PositionPtr&, const MovePtr&); -}; - -#endif // TAGUA_LEGACY_H diff --git a/src/ui.cpp b/src/ui.cpp index d5bfaa7..4c59c1e 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -163,7 +163,7 @@ void UI::setTurn(int turn) { controller()->setTurn(turn); } -AbstractPosition::Ptr UI::position() const { +StatePtr UI::position() const { return controller()->currentPosition(); } @@ -179,7 +179,7 @@ ActionCollection* UI::variantActions() const { return controller()->variantActions(); } -EntityToken UI::addPlayingEngine(int side, const shared_ptr& engine) { +EntityToken UI::addPlayingEngine(const IColor* side, const shared_ptr& engine) { return controller()->addPlayingEngine(side, engine); } diff --git a/src/ui.h b/src/ui.h index 725a704..4a2df1b 100644 --- a/src/ui.h +++ b/src/ui.h @@ -13,16 +13,17 @@ #include #include +#include +#include #include "common.h" #include "controllers/entitytoken.h" -#include "tagua.h" -#include +class ActionCollection; +class ActionStateObserver; class Controller; class Engine; -class ActionCollection; class KActionCollection; -class ActionStateObserver; +class IColor; /** * @brief Utility class to handle GUI actions. @@ -114,9 +115,9 @@ public Q_SLOTS: void setStartingPosition(); void copyPosition(); void pastePosition(); - AbstractPosition::Ptr position() const; + StatePtr position() const; - EntityToken addPlayingEngine(int side, const boost::shared_ptr& engine); + EntityToken addPlayingEngine(const IColor* side, const boost::shared_ptr& engine); // EntityToken addAnalysingEngine(const boost::shared_ptr& engine); void removeEntity(const EntityToken& token); diff --git a/src/usermove.h b/src/usermove.h index ee4dccb..0e172cb 100644 --- a/src/usermove.h +++ b/src/usermove.h @@ -12,7 +12,6 @@ #define USERMOVE_H #include -#include "fwd.h" #include struct NormalUserMove { diff --git a/src/variants.cpp b/src/variants.cpp dissimilarity index 65% index 9fa75b6..17fa856 100644 --- a/src/variants.cpp +++ b/src/variants.cpp @@ -1,80 +1,42 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. -*/ - -#include "variants.h" -#include -#include - -using namespace HLVariant; - -#define DECLARE_FACTORY(VARIANT) \ - namespace HLVariant { \ - namespace VARIANT { \ - VariantFactory* createFactory(); \ - } \ - } -DECLARE_FACTORY(Chess) -DECLARE_FACTORY(Minichess5) -DECLARE_FACTORY(Crazyhouse) -DECLARE_FACTORY(Dummy) -DECLARE_FACTORY(Shogi) -DECLARE_FACTORY(MiniShogi) -DECLARE_FACTORY(ShoShogi) -DECLARE_FACTORY(ToriShogi) -#undef DECLARE_FACTORY - -Variant* variant_factory(); - -Variants::Variants() { - addFactory(Chess::createFactory()); - addFactory(Minichess5::createFactory()); - addFactory(Crazyhouse::createFactory()); - addFactory(Dummy::createFactory()); - addFactory(Shogi::createFactory()); - addFactory(MiniShogi::createFactory()); - addFactory(ShoShogi::createFactory()); - addFactory(ToriShogi::createFactory()); - - addFactory(new VariantAdaptor(variant_factory())); -} - -Variants& Variants::instance() { - static Variants inst; - return inst; -} - -VariantFactory* Variants::getFactory(const QString& name) const { - Factories::const_iterator it = m_factories.find(name); - if (it != m_factories.end()) { - return it->second; - } - - return 0; -} - -VariantPtr Variants::get(const QString& name) const { - VariantFactory* factory = getFactory(name); - return factory ? VariantPtr(factory->createVariant()) : VariantPtr(); -} - -void Variants::addFactory(VariantFactory* factory) { - m_factories[factory->name()] = factory; -} - -QStringList Variants::all() const { - QStringList s; - for (Factories::const_iterator end = m_factories.end(), it = m_factories.begin(); it != end; ++it) { - if (!it->second->hidden()) - s << it->first; - } - - return s; -} - +/* + Copyright (c) 2006 Paolo Capriotti + (c) 2006 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#include "variants.h" +#include + +Variant* chess_variant_factory(); + +Variants::Variants() { + addFactory("Chess", &chess_variant_factory); +} + +Variants& Variants::instance() { + static Variants inst; + return inst; +} + +Variant* Variants::get(const QString& name) const { + VariantFactory* factory = m_factories.find(name)->second; + return (*factory)(); +} + +void Variants::addFactory(const QString& name, VariantFactory* factory) { + m_factories[name] = factory; +} + +QStringList Variants::all() const { + QStringList s; + for (Factories::const_iterator end = m_factories.end(), it = m_factories.begin(); it != end; ++it) { + s << it->first; + } + + return s; +} + diff --git a/src/variants.h b/src/variants.h index e2f1575..8a95eb2 100644 --- a/src/variants.h +++ b/src/variants.h @@ -12,10 +12,12 @@ #ifndef VARIANTS_H #define VARIANTS_H -#include +#include #include -#include "tagua.h" -#include "fwd.h" + +class Variant; + +typedef Variant* VariantFactory(); class Variants { class case_comparer { @@ -30,10 +32,9 @@ class Variants { Variants(); public: - VariantPtr get(const QString& name) const; - VariantFactory* getFactory(const QString& name) const; + Variant* get(const QString& name) const; - void addFactory(VariantFactory* factory); + void addFactory(const QString& name, VariantFactory* factory); /** * \return a list of all non-hidden variants. diff --git a/src/variants/chess/chess.cpp b/src/variants/chess/chess.cpp index c88809d..8569e28 100644 --- a/src/variants/chess/chess.cpp +++ b/src/variants/chess/chess.cpp @@ -8,6 +8,7 @@ (at your option) any later version. */ +#include #include #include #include "animator.h" @@ -19,12 +20,12 @@ using namespace Chess; -KDE_EXPORT Variant* variant_factory() { - Variant* v = new Variant("chess2", "Chess"); +KDE_EXPORT Variant* chess_variant_factory() { + Variant* v = new Variant("Chess"); Repository* repo = v->repository(); - repo->addComponent("color/white", White::self()); - repo->addComponent("color/black", Black::self()); + repo->addComponent("player/0", White::self()); + repo->addComponent("player/1", Black::self()); repo->addComponent("type/king", King::self()); repo->addComponent("type/queen", Queen::self()); @@ -37,6 +38,7 @@ KDE_EXPORT Variant* variant_factory() { repo->addComponent("validator", new Validator); repo->addComponent("animator_factory", new AnimatorFactory); repo->addComponent("namer", new Namer); + repo->addComponent("policy", new DefaultPolicy); return v; } diff --git a/src/variants/chess/namer.h b/src/variants/chess/namer.h index 2075ba0..7acf045 100644 --- a/src/variants/chess/namer.h +++ b/src/variants/chess/namer.h @@ -16,7 +16,8 @@ namespace Chess { class Namer : public Component, public INamer { -public: +Q_OBJECT +public Q_SLOTS: virtual QString name(const Piece& piece) const; }; diff --git a/src/variants/chess/state.cpp b/src/variants/chess/state.cpp index 35389fc..a41877d 100644 --- a/src/variants/chess/state.cpp +++ b/src/variants/chess/state.cpp @@ -74,6 +74,12 @@ bool State::equals(IState* other) const { m_flags == other->flags(); } +void State::assign(const IState* other) { + m_board = *other->board(); + m_turn = other->turn(); + m_flags = other->flags(); +} + void State::move(const Move& m) { Piece piece = m_board.get(m.src()); if (piece == Piece()) return; diff --git a/src/variants/chess/state.h b/src/variants/chess/state.h index bde270e..d6f5ce1 100644 --- a/src/variants/chess/state.h +++ b/src/variants/chess/state.h @@ -42,6 +42,8 @@ public Q_SLOTS: virtual void setTurn(const IColor* turn); virtual bool equals(IState* other) const; + + virtual void assign(const IState* other); virtual void move(const Move& move); diff --git a/src/variants/chess/validator.h b/src/variants/chess/validator.h index 2ad566e..bb5281d 100644 --- a/src/variants/chess/validator.h +++ b/src/variants/chess/validator.h @@ -17,7 +17,8 @@ namespace Chess { class Validator : public Component, public IValidator { -public: +Q_OBJECT +public Q_SLOTS: virtual bool pseudolegal(const IState* state, Move& move) const; virtual bool legal(const IState* state, Move& move) const; diff --git a/src/xboardengine.cpp b/src/xboardengine.cpp index 0319d62..6bea3e0 100644 --- a/src/xboardengine.cpp +++ b/src/xboardengine.cpp @@ -14,6 +14,9 @@ #include #include +#include +#include + #include "enginenotifier.h" #include "foreach.h" @@ -21,9 +24,13 @@ using namespace boost; QRegExp XBoardEngine::m_move_pattern("My move is: (.*)"); -XBoardEngine::XBoardEngine(const QString& path, const QStringList& arguments) +XBoardEngine::XBoardEngine(Variant* variant, + const QString& path, + const QStringList& arguments) : Engine(path, arguments) -, m_analysing(false) { +, m_analysing(false) +, m_variant(variant) +, m_serializer(0) { // set default features m_features.ping = false; m_features.setboard = false; @@ -146,16 +153,24 @@ void XBoardEngine::processCommand(const QString& command) { if (shared_ptr notifier = m_notifier.lock()) notifier->notifyEngineMove(m_move_pattern.cap(1)); } + + + // set serializer + QString serializer("move_serializer/"); + serializer += m_features.san ? "san" : "simple"; + m_serializer = requestComponent(m_variant, serializer); } -void XBoardEngine::sendMove(AbstractMove::Ptr move, AbstractPosition::Ptr ref) { - QString move_str = move->toString(m_features.san ? "compact" : "simple", ref); - if (m_features.usermove) - move_str = "usermove " + move_str; - sendCommand(move_str); +void XBoardEngine::sendMove(const Move& move, const StatePtr& ref) { + if (m_serializer) { + QString move_str = m_serializer->serialize(move, ref.get()); + if (m_features.usermove) + move_str = "usermove " + move_str; + sendCommand(move_str); + } } -void XBoardEngine::backUp(AbstractPosition::Ptr) { +void XBoardEngine::backUp(const StatePtr&) { sendCommand("undo"); } @@ -170,7 +185,7 @@ void XBoardEngine::stopAnalysis() { m_analysing = false; } -void XBoardEngine::setBoard(AbstractPosition::Ptr, int, int) { +void XBoardEngine::setBoard(const StatePtr&) { #if 0 if (m_features.setboard) { sendCommand(QString("setboard %1").arg(pos->fen(halfmove, fullmove))); diff --git a/src/xboardengine.h b/src/xboardengine.h index 643d996..9c62072 100644 --- a/src/xboardengine.h +++ b/src/xboardengine.h @@ -12,9 +12,10 @@ #define XBOARDENGINE_H #include "engine.h" -#include "tagua.h" class QRegExp; +class IMoveSerializer; +class Variant; /** * @brief A chess engine compatible with xboard. @@ -56,6 +57,8 @@ Q_OBJECT Features m_features; bool m_analysing; + Variant* m_variant; + IMoveSerializer* m_serializer; static QRegExp m_move_pattern; protected: @@ -73,7 +76,9 @@ public: /** * Create an xboard compatible engine. */ - XBoardEngine(const QString& path, const QStringList& arguments); + XBoardEngine(Variant* variant, + const QString& path, + const QStringList& arguments); /** * Terminate the engine. @@ -83,16 +88,16 @@ public: /** * Send a move to the engine. */ - virtual void sendMove(AbstractMove::Ptr move, AbstractPosition::Ptr ref); + virtual void sendMove(const Move& move, const StatePtr& ref); /** * Back up a move. * @param pos The position before the move. Used if the engine * does not support the undo command. */ - virtual void backUp(AbstractPosition::Ptr pos); + virtual void backUp(const StatePtr& pos); - virtual void setBoard(AbstractPosition::Ptr pos, int halfmove, int fullmove); + virtual void setBoard(const StatePtr& pos); /** -- 2.11.4.GIT