2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
11 #include "engineentity.h"
18 #include <core/state.h>
20 #include "components.h"
24 using namespace boost
;
26 EngineEntity::EngineEntity(Components
* components
, const shared_ptr
<Game
>& game
, const IColor
* side
,
27 const shared_ptr
<Engine
>& engine
, AgentGroup
* group
)
29 , m_components(components
)
34 , m_dispatcher(group
, this) { }
36 void EngineEntity::setup() {
38 m_engine
->setNotifier(shared_from_this());
42 // find all indexes of the current variation
43 std::stack
<Index
> m_indexes
;
44 Index index
= m_game
->index();
46 m_indexes
.push(index
);
47 kDebug() << "pushed index " << index
;
51 // set engine state playing all moves
52 // in the current variation
53 while (!m_indexes
.empty()) {
54 StatePtr state
= m_game
->position(index
);
56 index
= m_indexes
.top();
59 m_engine
->sendMove(m_game
->move(index
), state
);
62 m_last_index
= m_game
->index();
66 void EngineEntity::notifyEngineMove(const QString
& move_str
) {
67 // FIXME implement notifyEngineMove
69 if (position()->testMove(move
)) {
71 AbstractPosition::Ptr ref
= position();
72 AbstractPosition::Ptr pos
= ref
->clone();
74 m_game
->add(move
, pos
);
75 m_last_index
= m_game
->index();
76 m_dispatcher
.move(m_game
->index());
79 kError() << "Engine attempted to execute an invalid move:" << move_str
;
84 void EngineEntity::checkPlaying() {
85 if (!m_playing
&& m_side
==
86 m_game
->position(m_last_index
)->turn()) {
92 void EngineEntity::notifyMove(const Index
& index
) {
93 if (index
== m_last_index
) {
94 // TODO: check for consistency and update if necessary
96 else if (index
.prev() == m_last_index
) {
98 m_engine
->sendMove(m_game
->move(index
), m_game
->position(index
.prev()));
103 // TODO: handle move notification in arbitrary indexes
104 kWarning() << "engine entity can't handle index" << index
<< "(m_last_index =" << m_last_index
<< ")";