2008-11-23 17:55:28 +01:00
|
|
|
/*******************************************************************
|
|
|
|
* Eliot
|
2009-01-24 18:44:56 +01:00
|
|
|
* Copyright (C) 2009 Olivier Teulière
|
2008-11-23 17:55:28 +01:00
|
|
|
* Authors: Olivier Teulière <ipkiss @@ gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
#include "navigation.h"
|
|
|
|
#include "turn_cmd.h"
|
2012-02-19 16:51:25 +01:00
|
|
|
#include "command.h"
|
2009-01-24 11:28:20 +01:00
|
|
|
#include "game_exception.h"
|
2008-11-23 17:55:28 +01:00
|
|
|
#include "debug.h"
|
2008-11-23 18:02:33 +01:00
|
|
|
#include "encoding.h"
|
2008-11-23 17:55:28 +01:00
|
|
|
|
|
|
|
|
2011-01-30 00:47:20 +01:00
|
|
|
INIT_LOGGER(game, Navigation);
|
|
|
|
|
2012-02-19 18:34:37 +01:00
|
|
|
|
2008-11-23 17:55:28 +01:00
|
|
|
Navigation::Navigation()
|
|
|
|
: m_currTurn(0)
|
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
// Start with an empty turn
|
|
|
|
m_turnCommands.push_back(new TurnCmd);
|
2008-11-23 17:55:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Navigation::~Navigation()
|
|
|
|
{
|
2012-02-19 16:51:25 +01:00
|
|
|
BOOST_FOREACH(TurnCmd *c, m_turnCommands)
|
2008-11-23 17:55:28 +01:00
|
|
|
{
|
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Navigation::newTurn()
|
|
|
|
{
|
2011-01-30 00:47:20 +01:00
|
|
|
LOG_INFO("New turn");
|
2008-11-23 17:55:28 +01:00
|
|
|
lastTurn();
|
|
|
|
m_turnCommands.push_back(new TurnCmd);
|
|
|
|
++m_currTurn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Navigation::addAndExecute(Command *iCmd)
|
|
|
|
{
|
2009-01-24 11:28:20 +01:00
|
|
|
if (!isLastTurn())
|
|
|
|
throw GameException("Cannot add a command to an old turn");
|
2012-02-19 18:34:37 +01:00
|
|
|
m_turnCommands[m_currTurn]->addAndExecute(iCmd);
|
2008-11-23 17:55:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-30 21:51:05 +01:00
|
|
|
unsigned int Navigation::getCurrTurn() const
|
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
return m_currTurn;
|
2008-11-30 21:51:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int Navigation::getNbTurns() const
|
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
return m_turnCommands.size();
|
2008-11-30 21:51:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-23 18:08:12 +01:00
|
|
|
bool Navigation::isFirstTurn() const
|
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
return m_currTurn == 0 &&
|
|
|
|
m_turnCommands[m_currTurn]->isPartiallyExecuted() &&
|
2012-02-25 23:15:20 +01:00
|
|
|
(!m_turnCommands[m_currTurn]->hasNonAutoExecCmd() ||
|
2012-02-25 21:45:26 +01:00
|
|
|
!m_turnCommands[m_currTurn]->isFullyExecuted());
|
2008-11-23 18:08:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Navigation::isLastTurn() const
|
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
return m_currTurn == m_turnCommands.size() - 1 &&
|
|
|
|
m_turnCommands[m_currTurn]->isFullyExecuted();
|
2008-11-23 18:08:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-23 17:55:28 +01:00
|
|
|
void Navigation::prevTurn()
|
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
if (isFirstTurn())
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOG_DEBUG("Navigating to the previous turn");
|
|
|
|
TurnCmd *turn = m_turnCommands[m_currTurn];
|
|
|
|
if (turn->isFullyExecuted() && turn->hasNonAutoExecCmd())
|
2008-11-23 17:55:28 +01:00
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
ASSERT(isLastTurn(), "Unexpected turn state");
|
|
|
|
turn->partialUndo();
|
|
|
|
return;
|
2008-11-23 17:55:28 +01:00
|
|
|
}
|
2012-02-19 18:34:37 +01:00
|
|
|
|
|
|
|
ASSERT(m_currTurn > 0, "Trying to go before the first turn");
|
|
|
|
ASSERT(turn->isPartiallyExecuted(), "Unexpected turn state");
|
|
|
|
turn->undo();
|
|
|
|
|
|
|
|
--m_currTurn;
|
|
|
|
m_turnCommands[m_currTurn]->partialUndo();
|
2008-11-23 17:55:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Navigation::nextTurn()
|
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
if (isLastTurn())
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOG_DEBUG("Navigating to the next turn");
|
|
|
|
TurnCmd *turn = m_turnCommands[m_currTurn];
|
|
|
|
ASSERT(turn->isPartiallyExecuted(), "Unexpected turn state");
|
|
|
|
|
|
|
|
if (m_currTurn + 1 < m_turnCommands.size())
|
2008-11-23 17:55:28 +01:00
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
// Finish executing the current turn (if needed)
|
|
|
|
turn->execute();
|
|
|
|
|
2008-11-23 17:55:28 +01:00
|
|
|
++m_currTurn;
|
2012-02-19 18:34:37 +01:00
|
|
|
m_turnCommands[m_currTurn]->partialExecute();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ASSERT(!turn->isFullyExecuted(), "Unexpected turn state");
|
|
|
|
turn->execute();
|
2008-11-23 17:55:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Navigation::firstTurn()
|
|
|
|
{
|
2011-01-30 00:47:20 +01:00
|
|
|
LOG_DEBUG("Navigating to the first turn");
|
2012-02-19 18:34:37 +01:00
|
|
|
while (!isFirstTurn())
|
2008-11-23 17:55:28 +01:00
|
|
|
{
|
|
|
|
prevTurn();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Navigation::lastTurn()
|
|
|
|
{
|
2011-01-30 00:47:20 +01:00
|
|
|
LOG_DEBUG("Navigating to the last turn");
|
2012-02-19 18:34:37 +01:00
|
|
|
while (!isLastTurn())
|
2008-11-23 17:55:28 +01:00
|
|
|
{
|
|
|
|
nextTurn();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Navigation::clearFuture()
|
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
LOG_INFO("Erasing all the future turns");
|
2011-01-30 00:47:20 +01:00
|
|
|
|
2012-02-19 18:34:37 +01:00
|
|
|
// Replay the auto-execution turns (i.e. turns where only the AI was involved).
|
|
|
|
// This is needed for a correct handling of free games
|
2012-02-19 16:23:58 +01:00
|
|
|
while (!isLastTurn() && m_turnCommands[m_currTurn]->isHumanIndependent())
|
2012-02-19 18:34:37 +01:00
|
|
|
{
|
|
|
|
LOG_DEBUG("Replaying a human independent turn");
|
2008-11-30 21:55:45 +01:00
|
|
|
nextTurn();
|
2012-02-19 18:34:37 +01:00
|
|
|
}
|
2008-11-30 21:55:45 +01:00
|
|
|
|
2008-11-23 17:55:28 +01:00
|
|
|
// When there is no future, don't do anything
|
|
|
|
if (isLastTurn())
|
|
|
|
return;
|
|
|
|
|
2012-02-19 18:34:37 +01:00
|
|
|
// Destroy future turns
|
|
|
|
while (m_turnCommands.size() > m_currTurn + 1)
|
2008-11-23 17:55:28 +01:00
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
delete m_turnCommands.back();
|
2008-11-23 17:55:28 +01:00
|
|
|
m_turnCommands.pop_back();
|
|
|
|
}
|
2012-02-19 18:34:37 +01:00
|
|
|
|
|
|
|
TurnCmd *turn = m_turnCommands[m_currTurn];
|
|
|
|
|
|
|
|
// Destroy non executed commands for the current turn
|
|
|
|
ASSERT(turn->isPartiallyExecuted(), "Invalid state");
|
|
|
|
turn->dropNonExecutedCommands();
|
|
|
|
|
|
|
|
// Sanity checks
|
2008-11-23 17:55:28 +01:00
|
|
|
ASSERT(isLastTurn(),
|
|
|
|
"After removing the next turns, we should be at the last turn");
|
2012-02-19 18:34:37 +01:00
|
|
|
ASSERT(turn->isFullyExecuted(), "Invalid final state");
|
2008-11-23 17:55:28 +01:00
|
|
|
}
|
|
|
|
|
2008-11-23 18:02:33 +01:00
|
|
|
|
2012-03-11 00:24:21 +01:00
|
|
|
void Navigation::dropFrom(const Command &iCmd)
|
|
|
|
{
|
2012-03-23 07:08:02 +01:00
|
|
|
ASSERT(isLastTurn(), "Only possible in the last turn");
|
2012-03-11 00:24:21 +01:00
|
|
|
m_turnCommands.back()->dropFrom(iCmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-23 07:08:02 +01:00
|
|
|
void Navigation::dropCommand(const Command &iCmd)
|
|
|
|
{
|
|
|
|
m_turnCommands[m_currTurn]->dropCommand(iCmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Navigation::insertCommand(Command *iCmd)
|
|
|
|
{
|
|
|
|
m_turnCommands[m_currTurn]->insertCommand(iCmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-14 08:13:57 +01:00
|
|
|
void Navigation::replaceCommand(const Command &iOldCmd,
|
|
|
|
Command *iNewCmd)
|
|
|
|
{
|
|
|
|
m_turnCommands[m_currTurn]->replaceCommand(iOldCmd, iNewCmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-19 16:51:25 +01:00
|
|
|
const vector<TurnCmd *> & Navigation::getTurns() const
|
2009-11-29 17:01:31 +01:00
|
|
|
{
|
|
|
|
return m_turnCommands;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-19 18:34:37 +01:00
|
|
|
const TurnCmd & Navigation::getCurrentTurn() const
|
|
|
|
{
|
|
|
|
return *m_turnCommands[m_currTurn];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-23 18:02:33 +01:00
|
|
|
void Navigation::print() const
|
|
|
|
{
|
2012-04-12 09:26:02 +02:00
|
|
|
#ifdef USE_LOGGING
|
2011-01-30 00:47:20 +01:00
|
|
|
LOG_DEBUG("=== Commands history ===");
|
2012-02-19 18:34:37 +01:00
|
|
|
LOG_DEBUG("Current position at turn " << m_currTurn);
|
2008-11-23 21:13:02 +01:00
|
|
|
int index = 0;
|
2012-02-19 16:51:25 +01:00
|
|
|
BOOST_FOREACH(const TurnCmd *c, m_turnCommands)
|
2008-11-23 18:02:33 +01:00
|
|
|
{
|
2011-07-30 21:48:05 +02:00
|
|
|
LOG_DEBUG(index << " " << lfw(c->toString()));
|
2008-11-23 21:13:02 +01:00
|
|
|
++index;
|
2008-11-23 18:02:33 +01:00
|
|
|
}
|
2012-04-12 09:26:02 +02:00
|
|
|
#endif
|
2008-11-23 18:02:33 +01:00
|
|
|
}
|
|
|
|
|