changeview.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 "changeview.h"
00023 #include "action-registry.h"
00024 
00025 #include "../gamemap.h"
00026      
00027 ChangeView::ChangeView( GameMap* gamemap, const ViewState& state )
00028    : GameAction( gamemap )
00029 {
00030    newState = state;
00031 }
00032       
00033       
00034 ASCString ChangeView::getDescription() const
00035 {
00036    return "Change view";
00037 }
00038       
00039       
00040 void ChangeView::readData ( tnstream& stream ) 
00041 {
00042    int version = stream.readInt();
00043    if ( version != 1 )
00044       throw tinvalidversion ( "ChangeView", 1, version );
00045    
00046    int count = stream.readInt();
00047    for ( int i = 0; i< count; ++i ) {
00048       MapCoordinate pos;
00049       pos.read( stream );
00050       newState[pos] = stream.readInt();
00051    }
00052    
00053    count = stream.readInt();
00054    for ( int i = 0; i< count; ++i ) {
00055       MapCoordinate pos;
00056       pos.read( stream );
00057       oldState[pos] = stream.readInt();
00058    }
00059 
00060 };
00061       
00062       
00063 void ChangeView::writeData ( tnstream& stream ) const
00064 {
00065    stream.writeInt( 1 );
00066    stream.writeInt( newState.size() );
00067    for ( ViewState::const_iterator i = newState.begin(); i != newState.end(); ++i  ) {
00068       i->first.write( stream );
00069       stream.writeInt( i->second );
00070    }
00071    
00072    stream.writeInt( oldState.size() );
00073    for ( ViewState::const_iterator i = oldState.begin(); i != oldState.end(); ++i  ) {
00074       i->first.write( stream );
00075       stream.writeInt( i->second );
00076    }
00077    
00078 };
00079 
00080 
00081 GameActionID ChangeView::getID() const
00082 {
00083    return ActionRegistry::ChangeView;
00084 }
00085 
00086 ActionResult ChangeView::runAction( const Context& context )
00087 {
00088    for ( ViewState::iterator i = newState.begin(); i != newState.end(); ++i  ) {
00089       MapField* fld = getMap()->getField( i->first );
00090       oldState[i->first] = fld->visible;
00091       fld->visible = newState[i->first];
00092    }
00093    return ActionResult(0);
00094 }
00095 
00096 
00097 ActionResult ChangeView::undoAction( const Context& context )
00098 {
00099    for ( ViewState::iterator i = newState.begin(); i != newState.end(); ++i  ) {
00100       MapField* fld = getMap()->getField( i->first );
00101       if ( fld->visible != newState[i->first] ) {
00102          ASCString msg;
00103          msg.format( "; expected: %x ; found %x" , newState[i->first], fld->visible );
00104          throw ActionResult(21207, "Position is " + i->first.toString(true) + msg );
00105       }
00106       fld->visible = oldState[i->first];
00107    }
00108    return ActionResult(0);
00109 }
00110 
00111 namespace {
00112    const bool r1 = registerAction<ChangeView> ( ActionRegistry::ChangeView );
00113 }
00114 

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