push 98438d20051787465e7a05f4aa66ab452afb1c14
[tagua/yd.git] / src / entities / engineentity.cpp
blobc7937399641d3a1f942517fbd37575330bda34f2
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include "engineentity.h"
12 #include <KDebug>
13 #include "game.h"
14 #include "engine.h"
16 #include <stack>
18 #include <core/state.h>
20 #include "components.h"
21 #include "game.h"
22 #include "engine.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)
28 : Entity(game)
29 , m_components(components)
30 , m_side(side)
31 , m_last_index(0)
32 , m_playing(false)
33 , m_engine(engine)
34 , m_dispatcher(group, this) { }
36 void EngineEntity::setup() {
37 // setup engine
38 m_engine->setNotifier(shared_from_this());
39 m_engine->start();
40 m_engine->reset();
42 // find all indexes of the current variation
43 std::stack<Index> m_indexes;
44 Index index = m_game->index();
45 while (index != 0) {
46 m_indexes.push(index);
47 kDebug() << "pushed index " << index;
48 index = index.prev();
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();
57 m_indexes.pop();
59 m_engine->sendMove(m_game->move(index), state);
62 m_last_index = m_game->index();
63 checkPlaying();
66 void EngineEntity::notifyEngineMove(const QString& move_str) {
67 // FIXME implement notifyEngineMove
68 #if 0
69 if (position()->testMove(move)) {
70 Q_ASSERT(move);
71 AbstractPosition::Ptr ref = position();
72 AbstractPosition::Ptr pos = ref->clone();
73 pos->move(move);
74 m_game->add(move, pos);
75 m_last_index = m_game->index();
76 m_dispatcher.move(m_game->index());
78 else
79 kError() << "Engine attempted to execute an invalid move:" << move_str;
81 #endif
84 void EngineEntity::checkPlaying() {
85 if (!m_playing && m_side ==
86 m_game->position(m_last_index)->turn()) {
87 m_engine->play();
88 m_playing = true;
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) {
97 // play move
98 m_engine->sendMove(m_game->move(index), m_game->position(index.prev()));
99 m_last_index = index;
100 checkPlaying();
102 else {
103 // TODO: handle move notification in arbitrary indexes
104 kWarning() << "engine entity can't handle index" << index << "(m_last_index =" << m_last_index << ")";