changediplomaticstate.cpp

Go to the documentation of this file.
00001 /*
00002      This file is part of Advanced Strategic Command; http://www.asc-hq.de
00003      Copyright (C) 1994-2010  Martin Bickel  and  Marc Schellenberger
00004  
00005      This program is free software; you can redistribute it and/or modify
00006      it under the terms of the GNU General Public License as published by
00007      the Free Software Foundation; either version 2 of the License, or
00008      (at your option) any later version.
00009  
00010      This program is distributed in the hope that it will be useful,
00011      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013      GNU General Public License for more details.
00014  
00015      You should have received a copy of the GNU General Public License
00016      along with this program; see the file COPYING. If not, write to the 
00017      Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00018      Boston, MA  02111-1307  USA
00019 */
00020 
00021 
00022 #include "changediplomaticstate.h"
00023 
00024 #include "action-registry.h"
00025 #include "../gamemap.h"
00026 #include "../itemrepository.h"
00027 
00028 
00029 ChangeDiplomaticState::ChangeDiplomaticState( Player& fromplayer, Player& towardsPlayer, int queue, DiplomaticStates newState )
00030    : GameAction( fromplayer.getParentMap() )
00031 {
00032    this->fromPlayer = fromplayer.getPosition();
00033    this->towardsPlayer = towardsPlayer.getPosition();
00034    this->queued = queue;
00035    this->newState = newState;
00036    this->oldState = -1; 
00037 }
00038 
00039 void ChangeDiplomaticState::readData ( tnstream& stream )
00040 {
00041    stream.readInt();
00042    fromPlayer = stream.readInt();
00043    towardsPlayer = stream.readInt();
00044    queued = stream.readInt();
00045    newState = (DiplomaticStates) stream.readInt();
00046    oldState = stream.readInt();
00047 }
00048 
00049 
00050 void ChangeDiplomaticState::writeData ( tnstream& stream ) const
00051 {
00052    stream.writeInt( 1 );
00053    stream.writeInt( fromPlayer );
00054    stream.writeInt( towardsPlayer );
00055    stream.writeInt( queued );
00056    stream.writeInt( newState );
00057    stream.writeInt( oldState );
00058 }
00059 
00060 ActionResult ChangeDiplomaticState::runAction ( const Context& context )
00061 {
00062    DiplomaticStateVector& d = getMap()->getPlayer( fromPlayer ).diplomacy;
00063    
00064    if ( queued > 0)  {
00065       DiplomaticStateVector::QueuedStateChanges::iterator i =  d.queuedStateChanges.find( towardsPlayer );
00066       if ( i != d.queuedStateChanges.end() )
00067          oldState = i->second;
00068       else
00069          oldState = -1;
00070       
00071       d.queuedStateChanges[towardsPlayer] = newState;
00072    } else 
00073    if ( queued == 0 ) {
00074       oldState = d.getState( towardsPlayer );
00075       d.setState( towardsPlayer, newState );
00076    } else 
00077       if ( queued == -1 ) {
00078          DiplomaticStateVector::QueuedStateChanges::iterator i =  d.queuedStateChanges.find( towardsPlayer );
00079          if ( i != d.queuedStateChanges.end() ) {
00080             oldState = i->second;
00081             d.queuedStateChanges.erase( i );
00082          } else
00083             oldState = -1;
00084       }
00085       
00086    return ActionResult(0);
00087 }
00088 
00089 
00090 ActionResult ChangeDiplomaticState::undoAction( const Context& context )
00091 {
00092    DiplomaticStateVector& d = getMap()->getPlayer( fromPlayer ).diplomacy;
00093    
00094    if ( queued == 1 ) {
00095       DiplomaticStateVector::QueuedStateChanges::iterator i =  d.queuedStateChanges.find( towardsPlayer );
00096       if ( oldState == -1 )
00097          d.queuedStateChanges.erase( i );
00098       else
00099          i->second = (DiplomaticStates) oldState;
00100       
00101    } else 
00102    if ( queued == 0 ) {
00103       d.setState( towardsPlayer, (DiplomaticStates) oldState );
00104    } else
00105       if ( queued == -1 ) {
00106          if ( oldState < 0 || oldState >= diplomaticStateNum )
00107             return ActionResult(23101);
00108          d.queuedStateChanges[towardsPlayer] = (DiplomaticStates) oldState;  
00109       }
00110    
00111    return ActionResult(0);
00112 }
00113 
00114 
00115 ASCString ChangeDiplomaticState :: getCommandString() const 
00116 {
00117    ASCString c;
00118    c.format("changediplomaticstate ( %d %d %d %d )", fromPlayer, towardsPlayer, queued, (int)newState );
00119    return c;
00120 }
00121 
00122 GameActionID ChangeDiplomaticState::getID() const 
00123 {
00124    return ActionRegistry::ChangeDiplomaticState;   
00125 }
00126 
00127 ASCString ChangeDiplomaticState::getDescription() const
00128 {
00129    ASCString s = "Change diplomatic state between " + ASCString::toString(fromPlayer) + " and " + ASCString::toString(towardsPlayer) 
00130          + " to " + diplomaticStateNames[newState];
00131    return s;
00132 }
00133 
00134 
00135 namespace {
00136    const bool r1 = registerAction<ChangeDiplomaticState> ( ActionRegistry::ChangeDiplomaticState );
00137 }
00138 

Generated on Mon May 21 01:26:29 2012 for Advanced Strategic Command by  doxygen 1.5.1