2008-11-23 09:18:03 +01:00
|
|
|
/*******************************************************************
|
|
|
|
* Eliot
|
2012-10-07 16:25:41 +02:00
|
|
|
* Copyright (C) 2008-2012 Olivier Teulière
|
2008-11-23 09:18:03 +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>
|
2008-11-23 18:02:33 +01:00
|
|
|
#include <sstream>
|
2012-03-14 08:13:57 +01:00
|
|
|
#include <typeinfo>
|
2008-11-23 09:18:03 +01:00
|
|
|
|
2012-10-06 11:14:01 +02:00
|
|
|
#include "turn.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-10-06 11:29:52 +02:00
|
|
|
INIT_LOGGER(game, Turn);
|
2012-02-18 22:26:52 +01:00
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
Turn::Turn()
|
2012-02-19 18:34:37 +01:00
|
|
|
: m_firstNotExecuted(0)
|
2008-11-23 17:55:28 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
Turn::~Turn()
|
2008-11-23 09:18:03 +01:00
|
|
|
{
|
|
|
|
BOOST_FOREACH(Command *cmd, m_commands)
|
|
|
|
{
|
|
|
|
delete cmd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
void Turn::addAndExecute(Command *iCmd)
|
2008-11-23 09:18:03 +01:00
|
|
|
{
|
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-10-06 11:29:52 +02:00
|
|
|
void Turn::execute()
|
2008-11-23 09:18:03 +01:00
|
|
|
{
|
2012-03-23 07:34:56 +01:00
|
|
|
execTo(m_commands.size());
|
|
|
|
ASSERT(isFullyExecuted(), "Bug in execute()");
|
2008-11-23 09:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
void Turn::undo()
|
2008-11-23 09:18:03 +01:00
|
|
|
{
|
2012-03-23 07:34:56 +01:00
|
|
|
undoTo(0);
|
|
|
|
ASSERT(isNotAtAllExecuted(), "Bug in undo()");
|
2012-02-19 18:34:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
void Turn::partialExecute()
|
2012-02-19 18:34:37 +01:00
|
|
|
{
|
2012-03-23 07:34:56 +01:00
|
|
|
execTo(findIndexFirstNaec());
|
2012-02-19 18:34:37 +01:00
|
|
|
ASSERT(isPartiallyExecuted(), "Bug in partialExecute()");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
void Turn::partialUndo()
|
2012-02-19 18:34:37 +01:00
|
|
|
{
|
|
|
|
// Lazy implementation :)
|
|
|
|
undo();
|
|
|
|
partialExecute();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
void Turn::dropNonExecutedCommands()
|
2012-02-19 18:34:37 +01:00
|
|
|
{
|
2012-03-23 07:34:56 +01:00
|
|
|
if (!isFullyExecuted())
|
|
|
|
dropFrom(m_firstNotExecuted);
|
2012-02-19 18:34:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
void Turn::dropFrom(const Command &iCmd)
|
2012-03-11 00:24:21 +01:00
|
|
|
{
|
|
|
|
// Find the command index
|
2012-03-14 08:13:57 +01:00
|
|
|
unsigned idx = findIndex(iCmd);
|
2012-03-11 00:24:21 +01:00
|
|
|
ASSERT(idx != m_commands.size(), "Cannot find command to drop");
|
2012-03-14 08:13:57 +01:00
|
|
|
|
2012-03-23 07:34:56 +01:00
|
|
|
dropFrom(idx);
|
2012-03-11 00:24:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
void Turn::dropCommand(const Command &iCmd)
|
2012-03-23 07:08:02 +01:00
|
|
|
{
|
|
|
|
ASSERT(iCmd.isInsertable(), "Only insertable commands can be dropped");
|
|
|
|
ASSERT(iCmd.isAutoExecutable(), "Non auto-executable commands cannot be dropped");
|
|
|
|
|
|
|
|
// Find the command index
|
|
|
|
unsigned idx = findIndex(iCmd);
|
|
|
|
ASSERT(idx != m_commands.size(), "Cannot find command to drop");
|
|
|
|
|
|
|
|
LOG_DEBUG("Dropping single command");
|
|
|
|
|
|
|
|
// Undo commands after the interesting one (included)
|
2012-03-23 07:34:56 +01:00
|
|
|
unsigned tmpIdx = undoTo(idx);
|
2012-03-23 07:08:02 +01:00
|
|
|
ASSERT(!iCmd.isExecuted(), "Logic error");
|
|
|
|
|
|
|
|
// Drop the command
|
|
|
|
delete m_commands[idx];
|
|
|
|
m_commands.erase(m_commands.begin() + idx);
|
|
|
|
|
|
|
|
// We have deleted one command, so the index should be decreased
|
2012-03-23 07:34:56 +01:00
|
|
|
--tmpIdx;
|
2012-03-23 07:08:02 +01:00
|
|
|
|
|
|
|
// Re-execute the commands
|
2012-03-23 07:34:56 +01:00
|
|
|
execTo(tmpIdx);
|
2012-03-23 07:08:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
void Turn::insertCommand(Command *iCmd)
|
2012-03-23 07:08:02 +01:00
|
|
|
{
|
|
|
|
ASSERT(iCmd->isInsertable(), "Only insertable commands can be inserted");
|
|
|
|
ASSERT(iCmd->isAutoExecutable(), "Non auto-executable commands cannot be inserted");
|
|
|
|
|
|
|
|
// Find the insertion index
|
|
|
|
unsigned idx = findIndexFirstNaec();
|
|
|
|
|
|
|
|
LOG_DEBUG("Inserting command");
|
|
|
|
|
|
|
|
// Undo commands after the interesting one (included)
|
2012-03-23 07:34:56 +01:00
|
|
|
unsigned tmpIdx = undoTo(idx);
|
2012-03-23 07:08:02 +01:00
|
|
|
|
|
|
|
// Insert the command (possibly at the end, if there is no NAEC)
|
|
|
|
if (idx == m_commands.size())
|
|
|
|
m_commands.push_back(iCmd);
|
|
|
|
else
|
|
|
|
m_commands.insert(m_commands.begin() + idx, iCmd);
|
|
|
|
|
|
|
|
// We have inserted one command, so the index should be increased
|
2012-03-23 07:34:56 +01:00
|
|
|
++tmpIdx;
|
2012-03-23 07:08:02 +01:00
|
|
|
|
|
|
|
// Re-execute the commands
|
2012-03-23 07:34:56 +01:00
|
|
|
execTo(tmpIdx);
|
2012-03-23 07:08:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
void Turn::replaceCommand(const Command &iOldCmd,
|
2012-03-14 08:13:57 +01:00
|
|
|
Command *iNewCmd)
|
|
|
|
{
|
|
|
|
ASSERT(string(typeid(iOldCmd).name()) == string(typeid(*iNewCmd).name()),
|
|
|
|
"The commands should be of the same type (" +
|
|
|
|
string(typeid(iOldCmd).name()) + " vs. " +
|
|
|
|
string(typeid(*iNewCmd).name()));
|
|
|
|
|
|
|
|
unsigned idx = findIndex(iOldCmd);
|
|
|
|
ASSERT(idx != m_commands.size(), "Cannot find command");
|
|
|
|
|
|
|
|
// Undo commands after the interesting one (included)
|
2012-03-23 07:34:56 +01:00
|
|
|
unsigned tmpIdx = undoTo(idx);
|
2012-03-14 08:13:57 +01:00
|
|
|
ASSERT(!iOldCmd.isExecuted(), "Logic error");
|
|
|
|
|
|
|
|
// Replace the command
|
|
|
|
delete m_commands[idx];
|
|
|
|
m_commands[idx] = iNewCmd;
|
|
|
|
|
|
|
|
// Re-execute the commands
|
2012-03-23 07:34:56 +01:00
|
|
|
execTo(tmpIdx);
|
2012-03-14 08:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
bool Turn::isFullyExecuted() const
|
2012-02-19 18:34:37 +01:00
|
|
|
{
|
|
|
|
return m_firstNotExecuted == m_commands.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
bool Turn::isPartiallyExecuted() const
|
2012-02-19 18:34:37 +01:00
|
|
|
{
|
|
|
|
if (isFullyExecuted())
|
|
|
|
return true;
|
|
|
|
return !m_commands[m_firstNotExecuted]->isAutoExecutable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
bool Turn::isNotAtAllExecuted() const
|
2012-02-19 18:34:37 +01:00
|
|
|
{
|
|
|
|
return m_firstNotExecuted == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
bool Turn::hasNonAutoExecCmd() const
|
2012-02-19 18:34:37 +01:00
|
|
|
{
|
2012-03-23 07:08:02 +01:00
|
|
|
return findIndexFirstNaec() != m_commands.size();
|
2008-11-23 09:18:03 +01:00
|
|
|
}
|
|
|
|
|
2008-11-23 18:02:33 +01:00
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
bool Turn::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
unsigned Turn::findIndex(const Command &iCmd) const
|
2012-03-14 08:13:57 +01:00
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < m_commands.size(); ++i)
|
|
|
|
{
|
|
|
|
if (m_commands[i] == &iCmd)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return m_commands.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
unsigned Turn::findIndexFirstNaec() const
|
2012-03-23 07:08:02 +01:00
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < m_commands.size(); ++i)
|
|
|
|
{
|
|
|
|
if (!m_commands[i]->isAutoExecutable())
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return m_commands.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
unsigned Turn::execTo(unsigned iNewFirstNotExec)
|
2012-03-23 07:34:56 +01:00
|
|
|
{
|
|
|
|
ASSERT(iNewFirstNotExec <= m_commands.size(), "Invalid index");
|
|
|
|
unsigned oldVal = m_firstNotExecuted;
|
|
|
|
while (m_firstNotExecuted < iNewFirstNotExec)
|
|
|
|
{
|
|
|
|
Command *cmd = m_commands[m_firstNotExecuted];
|
|
|
|
ASSERT(!cmd->isExecuted(), "Bug with m_firstNotExecuted");
|
|
|
|
cmd->execute();
|
|
|
|
++m_firstNotExecuted;
|
|
|
|
}
|
|
|
|
return oldVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
unsigned Turn::undoTo(unsigned iNewFirstNotExec)
|
2012-03-23 07:34:56 +01:00
|
|
|
{
|
|
|
|
unsigned oldVal = m_firstNotExecuted;
|
|
|
|
while (m_firstNotExecuted > iNewFirstNotExec)
|
|
|
|
{
|
|
|
|
--m_firstNotExecuted;
|
|
|
|
Command *cmd = m_commands[m_firstNotExecuted];
|
|
|
|
ASSERT(cmd->isExecuted(), "Bug with m_firstNotExecuted");
|
|
|
|
cmd->undo();
|
|
|
|
}
|
|
|
|
return oldVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
void Turn::dropFrom(unsigned iFirstToDrop)
|
2012-03-23 07:34:56 +01:00
|
|
|
{
|
|
|
|
ASSERT(iFirstToDrop < m_commands.size(), "Invalid index");
|
|
|
|
LOG_DEBUG("Deleting turn commands, starting from index " << iFirstToDrop);
|
|
|
|
|
|
|
|
undoTo(iFirstToDrop);
|
|
|
|
while (m_commands.size() > iFirstToDrop)
|
|
|
|
{
|
|
|
|
delete m_commands.back();
|
|
|
|
m_commands.pop_back();
|
|
|
|
}
|
|
|
|
ASSERT(m_firstNotExecuted <= m_commands.size(), "Invalid state");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 11:29:52 +02:00
|
|
|
wstring Turn::toString() const
|
2008-11-23 18:02:33 +01:00
|
|
|
{
|
|
|
|
wostringstream oss;
|
|
|
|
BOOST_FOREACH(Command *cmd, m_commands)
|
|
|
|
{
|
2012-10-05 12:38:55 +02:00
|
|
|
oss << endl << L" "
|
|
|
|
<< (cmd->isExecuted() ? L"| " : L" " )
|
|
|
|
<< (cmd->isAutoExecutable() ? L"* " : L" ")
|
|
|
|
<< (cmd->isHumanIndependent() ? L" " : L"H ")
|
|
|
|
<< cmd->toString();
|
2008-11-23 18:02:33 +01:00
|
|
|
}
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|