1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
/*
This file is part of Advanced Strategic Command; http://www.asc-hq.de
Copyright (C) 1994-2008 Martin Bickel and Marc Schellenberger
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; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
#include "action.h"
#include "../vehicle.h"
#include "action-registry.h"
#include "actioncontainer.h"
GameAction::GameAction( GameMap* map )
: gamemap(map), done(false)
{
static int counter = 0;
sequenceNumber = ++counter;
}
void GameAction::addChild( GameAction* action )
{
children.push_back ( action );
}
ActionResult GameAction::execute( const Context& context )
{
if ( context.parentAction )
context.parentAction->addChild( this );
Context c ( context, this );
try {
displayLogMessage(4, "executing " + getDescription() + "\n");
ActionResult result = runAction( c );
Command* command = dynamic_cast<Command*>(this);
if ( context.actionContainer && result.successful() && !context.parentAction && command)
context.actionContainer->add( command );
return result;
} catch ( ActionResult res ) {
return res;
}
}
ActionResult GameAction::undoChildren( const Context& context )
{
ActionResult res(0);
for ( Children::reverse_iterator i = children.rbegin(); i != children.rend(); ++i ) {
ActionResult r = (*i)->undo( context );
if ( !r.successful() && res.successful() )
res = r;
}
return res;
}
static void mergeActionFailures( ActionResult& failure, const ActionResult& res )
{
if ( failure.successful() && !res.successful())
failure = res;
}
ActionResult GameAction::undo( const Context& context )
{
// if there is one failuire, we'll preserve it
ActionResult failure(0);
if ( undoOrderChildFirst() ) {
ActionResult r = undoChildren(context);
mergeActionFailures( failure, r );
}
displayLogMessage(4, "undoing #" + ASCString::toString(sequenceNumber) + " " + getDescription() + "\n");
ActionResult res = undoAction( context );
if ( !res.successful() )
warningMessage("error undoing " + getDescription() + "\n" + res.getMessage() );
mergeActionFailures( failure, res );
if ( !undoOrderChildFirst() ) {
ActionResult r = undoChildren(context);
mergeActionFailures( failure, r );
}
return failure;
}
/*
GameActionID GameAction::getID() const
{
return ActionRegistry::Root;
}
*/
const int currentGameActionVersion = 1;
const int gameActionToken = 0x12003496;
void GameAction::read ( tnstream& stream )
{
displayLogMessage(1, "reading " + ASCString::toString(getID()) + "\n");
int version = stream.readInt();
if ( version > currentGameActionVersion )
throw tinvalidversion ( "GameAction", currentGameActionVersion, version );
readData( stream );
int token = stream.readInt();
if ( token != gameActionToken )
throw tinvalidversion ("GameActionToken for id " + ASCString::toString( getID() ), gameActionToken, token );
int childNum = stream.readInt();
for ( int i = 0; i < childNum; i++ ) {
GameAction* child = readFromStream( stream, getMap() );
addChild( child );
}
}
void GameAction::write ( tnstream& stream ) const
{
stream.writeInt( getID() );
stream.writeInt( currentGameActionVersion );
writeData( stream );
stream.writeInt( gameActionToken );
stream.writeInt( children.size() );
for ( int i = 0; i < children.size(); ++i )
children[i]->write( stream );
}
GameAction* GameAction::readFromStream( tnstream& stream, GameMap* map )
{
GameActionID id = GameActionID( stream.readInt() );
GameAction* action = gameActionFactory::Instance().createObject( id, map );
action->read( stream );
return action;
}
void GameAction::deleteChildren()
{
for ( Children::iterator i = children.begin(); i != children.end(); ++i )
delete *i;
children.clear();
}
|