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 "cancelresearchcommand.h" 00023 00024 #include "action-registry.h" 00025 #include "../gamemap.h" 00026 #include "../itemrepository.h" 00027 00028 CancelResearchCommand::CancelResearchCommand( GameMap* map ) 00029 : Command( map ), researchProgress(-1), activeTechnologyID(-1), targetTechnologyID(-1), player(-1) 00030 { 00031 } 00032 00033 00034 void CancelResearchCommand::setPlayer( const Player& player ) 00035 { 00036 this->player = player.getPosition(); 00037 setState( SetUp ); 00038 } 00039 00040 00041 void CancelResearchCommand::readData ( tnstream& stream ) 00042 { 00043 Command::readData( stream ); 00044 stream.readInt(); 00045 researchProgress = stream.readInt(); 00046 activeTechnologyID = stream.readInt(); 00047 targetTechnologyID = stream.readInt(); 00048 player = stream.readInt(); 00049 } 00050 00051 00052 void CancelResearchCommand::writeData ( tnstream& stream ) const 00053 { 00054 Command::writeData( stream ); 00055 stream.writeInt( 1 ); 00056 stream.writeInt( researchProgress ); 00057 stream.writeInt( activeTechnologyID ); 00058 stream.writeInt( targetTechnologyID ); 00059 stream.writeInt( player ); 00060 } 00061 00062 ActionResult CancelResearchCommand::go ( const Context& context ) 00063 { 00064 if ( getState() != SetUp ) 00065 return ActionResult(22000); 00066 00067 Research& research = getMap()->getPlayer( player ).research; 00068 00069 researchProgress = research.progress; 00070 if ( research.activetechnology ) 00071 activeTechnologyID = research.activetechnology->id; 00072 else 00073 activeTechnologyID = -1; 00074 00075 if ( research.goal ) 00076 targetTechnologyID = research.goal->id; 00077 else 00078 targetTechnologyID = -1; 00079 00080 00081 research.progress = 0; 00082 research.activetechnology = NULL; 00083 research.goal = NULL; 00084 00085 setState( Finished ); 00086 00087 return ActionResult(0); 00088 } 00089 00090 ActionResult CancelResearchCommand::preCheck() 00091 { 00092 Research& research = getMap()->getPlayer( player ).research; 00093 00094 int _progress = research.progress; 00095 int _activeTechnologyID; 00096 if ( research.activetechnology ) 00097 _activeTechnologyID = research.activetechnology->id; 00098 else 00099 _activeTechnologyID = -1; 00100 00101 int _targetTechnologyID; 00102 if ( research.goal ) 00103 _targetTechnologyID = research.goal->id; 00104 else 00105 _targetTechnologyID = -1; 00106 00107 00108 if ( _progress != researchProgress 00109 || _activeTechnologyID != activeTechnologyID 00110 || _targetTechnologyID != targetTechnologyID ) 00111 return ActionResult( 23000 ); 00112 00113 return ActionResult(0); 00114 } 00115 00116 ActionResult CancelResearchCommand::undoAction( const Context& context ) 00117 { 00118 if ( getState() != Finished ) 00119 return ActionResult(22000); 00120 00121 Research& research = getMap()->getPlayer( player ).research; 00122 00123 00124 if ( activeTechnologyID != -1 ) { 00125 Technology* tech = technologyRepository.getObject_byID( activeTechnologyID ); 00126 if ( !tech ) 00127 return ActionResult( 23001 ); 00128 else 00129 research.activetechnology = tech; 00130 } 00131 00132 if ( targetTechnologyID != -1 ) { 00133 Technology* tech = technologyRepository.getObject_byID( targetTechnologyID ); 00134 if ( !tech ) 00135 return ActionResult( 23001 ); 00136 else 00137 research.goal = tech; 00138 } 00139 00140 research.progress = researchProgress; 00141 00142 return Command::undoAction(context); 00143 } 00144 00145 00146 ASCString CancelResearchCommand :: getCommandString() const 00147 { 00148 ASCString c; 00149 c.format("cancelResearch ( map, %d )", player ); 00150 return c; 00151 } 00152 00153 GameActionID CancelResearchCommand::getID() const 00154 { 00155 return ActionRegistry::CancelResearchCommand; 00156 } 00157 00158 ASCString CancelResearchCommand::getDescription() const 00159 { 00160 ASCString s = "Cancel research for player " + ASCString::toString(player); 00161 return s; 00162 } 00163 00164 00165 namespace { 00166 const bool r1 = registerAction<CancelResearchCommand> ( ActionRegistry::CancelResearchCommand ); 00167 } 00168
1.5.1