Initial porting to the new component API.
[tagua/yd.git] / src / entities / engineentity.cpp
blob97c6a34f725e4edc5c2926d156cff3d0bd7e071b
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"
13 #include <core/state.h>
14 #include <core/variant.h>
15 #include "game.h"
16 #include "engine.h"
18 #include <iostream>
19 #include <stack>
21 using namespace boost;
23 EngineEntity::EngineEntity(Variant* variant, const shared_ptr<Game>& game, const IColor* side,
24 const shared_ptr<Engine>& engine, AgentGroup* group)
25 : Entity(game)
26 , m_variant(variant)
27 , m_side(side)
28 , m_last_index(0)
29 , m_playing(false)
30 , m_engine(engine)
31 , m_dispatcher(group, this) { }
33 void EngineEntity::setup() {
34 // setup engine
35 m_engine->setNotifier(shared_from_this());
36 m_engine->start();
37 m_engine->reset();
39 // find all indexes of the current variation
40 std::stack<Index> m_indexes;
41 Index index = m_game->index();
42 while (index != 0) {
43 m_indexes.push(index);
44 std::cout << "pushed index " << index << std::endl;
45 index = index.prev();
48 // set engine state playing all moves
49 // in the current variation
50 while (!m_indexes.empty()) {
51 StatePtr state = m_game->position(index);
53 index = m_indexes.top();
54 m_indexes.pop();
56 m_engine->sendMove(m_game->move(index), state);
59 m_last_index = m_game->index();
60 checkPlaying();
63 void EngineEntity::notifyEngineMove(const QString& move_str) {
64 // FIXME implement notifyEngineMove
65 #if 0
66 if (position()->testMove(move)) {
67 Q_ASSERT(move);
68 AbstractPosition::Ptr ref = position();
69 AbstractPosition::Ptr pos = ref->clone();
70 pos->move(move);
71 m_game->add(move, pos);
72 m_last_index = m_game->index();
73 m_dispatcher.move(m_game->index());
75 else
76 ERROR("Engine attempted to execute an invalid move: " << move_str);
78 #endif
81 void EngineEntity::checkPlaying() {
82 if (!m_playing && m_side ==
83 m_game->position(m_last_index)->turn()) {
84 m_engine->play();
85 m_playing = true;
89 void EngineEntity::notifyMove(const Index& index) {
90 if (index == m_last_index) {
91 // TODO: check for consistency and update if necessary
93 else if (index.prev() == m_last_index) {
94 // play move
95 m_engine->sendMove(m_game->move(index), m_game->position(index.prev()));
96 m_last_index = index;
97 checkPlaying();
99 else {
100 // TODO: handle move notification in arbitrary indexes
101 WARNING("engine entity can't handle index " << index << " (m_last_index = " << m_last_index << ")");