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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/*
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 "directresearchcommand.h"
#include "action-registry.h"
#include "../gamemap.h"
#include "../itemrepository.h"
#include "../researchexecution.h"
bool DirectResearchCommand::available( const Player& player )
{
return player.research.activetechnology==NULL // there is no research currently going on
&& player.research.goal == NULL
&& player.research.progress >0; // and there are some research points to be spend
}
DirectResearchCommand::DirectResearchCommand( Player& player )
: Command( player.getParentMap() ), researchProgress(-1), targetTechnologyID(-1)
{
this->player = player.getPosition();
}
static const int directResearchCommandStreamVersion = 1;
void DirectResearchCommand::readData ( tnstream& stream )
{
Command::readData( stream );
int version = stream.readInt();
if ( version < 1 || version > directResearchCommandStreamVersion )
throw tinvalidversion ( "directResearchCommandStreamVersion", directResearchCommandStreamVersion, version );
researchProgress = stream.readInt();
targetTechnologyID = stream.readInt();
player = stream.readInt();
readClassContainer( immediatelyResearchedTechnologies, stream );
readClassContainer( techAdaptersMadeAvailable, stream );
}
void DirectResearchCommand::writeData ( tnstream& stream ) const
{
Command::writeData( stream );
stream.writeInt( directResearchCommandStreamVersion );
stream.writeInt( researchProgress );
stream.writeInt( targetTechnologyID );
stream.writeInt( player );
writeClassContainer( immediatelyResearchedTechnologies, stream );
writeClassContainer( techAdaptersMadeAvailable, stream );
}
vector<const Technology*> DirectResearchCommand::getAvailableTechnologies( bool longTerm )
{
vector<const Technology*> techs;
Player& p = getMap()->getPlayer(player);
for (int i = 0; i < technologyRepository.getNum(); i++) {
const Technology* tech = technologyRepository.getObject_byPos( i );
if ( tech ) {
if ( longTerm ) {
if ( tech->eventually_available( p.research, NULL ))
techs.push_back( tech );
} else {
ResearchAvailabilityStatus a = p.research.techAvailable ( tech );
if ( a == Available )
techs.push_back ( tech );
}
}
}
return techs;
}
void DirectResearchCommand::setTechnology( const Technology* tech )
{
targetTechnologyID = tech->id;
setState( SetUp );
}
ActionResult DirectResearchCommand::go ( const Context& context )
{
if ( getState() != SetUp )
return ActionResult(22000);
if ( !available( getMap()->getPlayer( player ) ))
return ActionResult( 23201 );
vector<const Technology*> techs = getAvailableTechnologies(true);
const Technology* newTech = technologyRepository.getObject_byID( targetTechnologyID );
if ( find ( techs.begin(), techs.end(), newTech ) == techs.end() )
return ActionResult(23200);
Research& research = getMap()->getPlayer( player ).research;
if ( newTech->techDependency.available( research )) {
research.activetechnology = newTech;
research.goal = NULL;
} else {
list<const Technology*> techs;
if ( newTech->eventually_available( research, &techs )) {
research.activetechnology = *techs.begin();
research.goal = newTech;
} else
return ActionResult( 23202 );
}
vector<const Technology*> researched;
techAdaptersMadeAvailable.clear();
runResearch( getMap()->getPlayer( player ), &researched, &techAdaptersMadeAvailable );
for ( vector<const Technology*>::iterator i = researched.begin(); i != researched.end(); i++ )
immediatelyResearchedTechnologies.push_back( (*i)->id );
setState( Completed );
return ActionResult(0);
}
ActionResult DirectResearchCommand::undoAction( const Context& context )
{
if ( getState() != Completed )
return ActionResult(22000);
Research& research = getMap()->getPlayer( player ).research;
for ( vector<int>::const_iterator i = immediatelyResearchedTechnologies.begin(); i != immediatelyResearchedTechnologies.end(); ++i ) {
Technology* t = technologyRepository.getObject_byID( *i );
if ( !t )
return ActionResult(21005, ASCString::toString(*i));
vector<int>::iterator j = find( research.developedTechnologies.begin(), research.developedTechnologies.end(), *i );
if ( j != research.developedTechnologies.end() )
research.developedTechnologies.erase(j);
else
return ActionResult(21204, t->name );
research.progress += t->researchpoints;
}
for ( vector<ASCString>::iterator i = techAdaptersMadeAvailable.begin(); i != techAdaptersMadeAvailable.end(); ++i ) {
Research::TriggeredTechAdapter::iterator ta = find( research.triggeredTechAdapter.begin(), research.triggeredTechAdapter.end(), *i );
if ( ta == research.triggeredTechAdapter.end() )
return ActionResult(21203, *i );
else
research.triggeredTechAdapter.erase( *i );
}
return Command::undoAction(context);
}
ASCString DirectResearchCommand :: getCommandString() const
{
ASCString c;
c.format("setResearchGoal ( map, %d, %d )", player, targetTechnologyID );
return c;
}
GameActionID DirectResearchCommand::getID() const
{
return ActionRegistry::DirectResearchCommand;
}
ASCString DirectResearchCommand::getDescription() const
{
ASCString s = "Choose technology " + ASCString::toString(targetTechnologyID) + " for player " + ASCString::toString(player);
return s;
}
namespace {
const bool r1 = registerAction<DirectResearchCommand> ( ActionRegistry::DirectResearchCommand );
}
|