2008-11-23 09:18:03 +01:00
|
|
|
/*******************************************************************
|
|
|
|
* Eliot
|
|
|
|
* Copyright (C) 2008 Olivier Teulière
|
|
|
|
* 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>
|
2008-11-23 18:02:33 +01:00
|
|
|
#include <sstream>
|
2008-11-23 09:18:03 +01:00
|
|
|
|
|
|
|
#include "turn_cmd.h"
|
2012-02-19 16:51:25 +01:00
|
|
|
#include "command.h"
|
2008-11-23 09:18:03 +01:00
|
|
|
#include "player.h"
|
2012-02-19 18:34:37 +01:00
|
|
|
#include "debug.h"
|
2008-11-23 09:18:03 +01:00
|
|
|
|
|
|
|
|
2012-02-18 22:26:52 +01:00
|
|
|
INIT_LOGGER(game, TurnCmd);
|
|
|
|
|
|
|
|
|
2008-11-23 17:55:28 +01:00
|
|
|
TurnCmd::TurnCmd()
|
2012-02-19 18:34:37 +01:00
|
|
|
: m_firstNotExecuted(0)
|
2008-11-23 17:55:28 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
TurnCmd::~TurnCmd()
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(Command *cmd, m_commands)
|
|
|
|
{
|
|
|
|
delete cmd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TurnCmd::addAndExecute(Command *iCmd)
|
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
ASSERT(isFullyExecuted(), "Adding a command to a partially executed turn");
|
2008-11-23 09:18:03 +01:00
|
|
|
m_commands.push_back(iCmd);
|
|
|
|
iCmd->execute();
|
2012-02-19 18:34:37 +01:00
|
|
|
++m_firstNotExecuted;
|
2008-11-23 09:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-19 16:51:25 +01:00
|
|
|
void TurnCmd::execute()
|
2008-11-23 09:18:03 +01:00
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
for (unsigned i = m_firstNotExecuted; i < m_commands.size(); ++i)
|
2008-11-23 09:18:03 +01:00
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
Command *cmd = m_commands[i];
|
|
|
|
ASSERT(!cmd->isExecuted(), "Bug with m_firstNotExecuted");
|
|
|
|
cmd->execute();
|
2008-11-23 09:18:03 +01:00
|
|
|
}
|
2012-02-19 18:34:37 +01:00
|
|
|
m_firstNotExecuted = m_commands.size();
|
2008-11-23 09:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-19 16:51:25 +01:00
|
|
|
void TurnCmd::undo()
|
2008-11-23 09:18:03 +01:00
|
|
|
{
|
|
|
|
// Undo commands in the reverse order of execution
|
2012-02-19 18:34:37 +01:00
|
|
|
unsigned firstToUndo = m_firstNotExecuted - 1;
|
|
|
|
for (unsigned i = 0; i < m_firstNotExecuted; ++i)
|
|
|
|
{
|
|
|
|
Command *cmd = m_commands[firstToUndo - i];
|
|
|
|
ASSERT(cmd->isExecuted(), "Bug with m_firstNotExecuted");
|
|
|
|
cmd->undo();
|
|
|
|
}
|
|
|
|
m_firstNotExecuted = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TurnCmd::partialExecute()
|
|
|
|
{
|
|
|
|
for (unsigned i = m_firstNotExecuted; i < m_commands.size(); ++i)
|
|
|
|
{
|
|
|
|
Command *cmd = m_commands[i];
|
|
|
|
if (!cmd->isAutoExecutable())
|
|
|
|
break;
|
|
|
|
ASSERT(!cmd->isExecuted(), "Bug with m_firstNotExecuted");
|
|
|
|
cmd->execute();
|
|
|
|
++m_firstNotExecuted;
|
|
|
|
}
|
|
|
|
ASSERT(isPartiallyExecuted(), "Bug in partialExecute()");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TurnCmd::partialUndo()
|
|
|
|
{
|
|
|
|
// Lazy implementation :)
|
|
|
|
undo();
|
|
|
|
partialExecute();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TurnCmd::dropNonExecutedCommands()
|
|
|
|
{
|
|
|
|
while (m_commands.size() > m_firstNotExecuted)
|
|
|
|
{
|
|
|
|
delete m_commands.back();
|
|
|
|
m_commands.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-11 00:24:21 +01:00
|
|
|
void TurnCmd::dropFrom(const Command &iCmd)
|
|
|
|
{
|
|
|
|
// Find the command index
|
|
|
|
unsigned idx = m_commands.size();
|
|
|
|
for (unsigned i = 0; i < m_commands.size(); ++i)
|
|
|
|
{
|
|
|
|
if (m_commands[i] == &iCmd)
|
|
|
|
{
|
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT(idx != m_commands.size(), "Cannot find command to drop");
|
|
|
|
LOG_DEBUG("Deleting last turn commands, starting from " << idx);
|
|
|
|
|
|
|
|
while (m_commands.size() > idx)
|
|
|
|
{
|
|
|
|
if (m_commands.back()->isExecuted())
|
|
|
|
m_commands.back()->undo();
|
|
|
|
delete m_commands.back();
|
|
|
|
m_commands.pop_back();
|
|
|
|
}
|
|
|
|
if (m_firstNotExecuted > m_commands.size())
|
|
|
|
m_firstNotExecuted = m_commands.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-19 18:34:37 +01:00
|
|
|
bool TurnCmd::isFullyExecuted() const
|
|
|
|
{
|
|
|
|
return m_firstNotExecuted == m_commands.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TurnCmd::isPartiallyExecuted() const
|
|
|
|
{
|
|
|
|
if (isFullyExecuted())
|
|
|
|
return true;
|
|
|
|
return !m_commands[m_firstNotExecuted]->isAutoExecutable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TurnCmd::isNotAtAllExecuted() const
|
|
|
|
{
|
|
|
|
return m_firstNotExecuted == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TurnCmd::hasNonAutoExecCmd() const
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(Command *cmd, m_commands)
|
2008-11-23 09:18:03 +01:00
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
if (!cmd->isAutoExecutable())
|
|
|
|
return true;
|
2008-11-23 09:18:03 +01:00
|
|
|
}
|
2012-02-19 18:34:37 +01:00
|
|
|
return false;
|
2008-11-23 09:18:03 +01:00
|
|
|
}
|
|
|
|
|
2008-11-23 18:02:33 +01:00
|
|
|
|
2012-02-19 16:23:58 +01:00
|
|
|
bool TurnCmd::isHumanIndependent() const
|
2008-11-30 21:55:45 +01:00
|
|
|
{
|
|
|
|
BOOST_FOREACH(Command *cmd, m_commands)
|
|
|
|
{
|
2012-02-19 16:23:58 +01:00
|
|
|
if (!cmd->isHumanIndependent())
|
2008-11-30 21:55:45 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-23 18:02:33 +01:00
|
|
|
wstring TurnCmd::toString() const
|
|
|
|
{
|
|
|
|
wostringstream oss;
|
2011-01-30 00:47:20 +01:00
|
|
|
oss << L"TurnCmd:";
|
2008-11-23 18:02:33 +01:00
|
|
|
BOOST_FOREACH(Command *cmd, m_commands)
|
|
|
|
{
|
2012-02-19 18:34:37 +01:00
|
|
|
oss << endl << L" " << (cmd->isExecuted() ? "> " : " " )
|
|
|
|
<< (cmd->isAutoExecutable() ? "* " : " ") << cmd->toString();
|
2008-11-23 18:02:33 +01:00
|
|
|
}
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|