Advanced Strategic Command
directresearchcommand.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 
22 #include "directresearchcommand.h"
23 
24 #include "action-registry.h"
25 #include "../gamemap.h"
26 #include "../itemrepository.h"
27 #include "../researchexecution.h"
28 
30 {
31  return player.research.activetechnology==NULL // there is no research currently going on
32  && player.research.goal == NULL
33  && player.research.progress >0; // and there are some research points to be spend
34 
35 }
36 
37 
38 DirectResearchCommand::DirectResearchCommand( Player& player )
39  : Command( player.getParentMap() ), researchProgress(-1), targetTechnologyID(-1)
40 {
41  this->player = player.getPosition();
42 }
43 
44 
46 
48 {
49  Command::readData( stream );
50  int version = stream.readInt();
51  if ( version < 1 || version > directResearchCommandStreamVersion )
52  throw tinvalidversion ( "directResearchCommandStreamVersion", directResearchCommandStreamVersion, version );
53 
54  researchProgress = stream.readInt();
55  targetTechnologyID = stream.readInt();
56  player = stream.readInt();
57  readClassContainer( immediatelyResearchedTechnologies, stream );
58  readClassContainer( techAdaptersMadeAvailable, stream );
59 }
60 
61 
63 {
64  Command::writeData( stream );
66  stream.writeInt( researchProgress );
67  stream.writeInt( targetTechnologyID );
68  stream.writeInt( player );
69  writeClassContainer( immediatelyResearchedTechnologies, stream );
70  writeClassContainer( techAdaptersMadeAvailable, stream );
71 }
72 
73 
74 vector<const Technology*> DirectResearchCommand::getAvailableTechnologies( bool longTerm )
75 {
76  vector<const Technology*> techs;
77 
78  Player& p = getMap()->getPlayer(player);
79 
80  for (int i = 0; i < technologyRepository.getNum(); i++) {
81  const Technology* tech = technologyRepository.getObject_byPos( i );
82  if ( tech ) {
83  if ( longTerm ) {
84  if ( tech->eventually_available( p.research, NULL ))
85  techs.push_back( tech );
86  } else {
88  if ( a == Available )
89  techs.push_back ( tech );
90  }
91  }
92  }
93  return techs;
94 }
95 
96 
98 {
99  targetTechnologyID = tech->id;
100  setState( SetUp );
101 }
102 
103 
105 {
106  if ( getState() != SetUp )
107  return ActionResult(22000);
108 
109  if ( !available( getMap()->getPlayer( player ) ))
110  return ActionResult( 23201 );
111 
112  vector<const Technology*> techs = getAvailableTechnologies(true);
113  const Technology* newTech = technologyRepository.getObject_byID( targetTechnologyID );
114  if ( find ( techs.begin(), techs.end(), newTech ) == techs.end() )
115  return ActionResult(23200);
116 
117  Research& research = getMap()->getPlayer( player ).research;
118 
119  if ( newTech->techDependency.available( research )) {
120  research.activetechnology = newTech;
121  research.goal = NULL;
122  } else {
123  list<const Technology*> techs;
124  if ( newTech->eventually_available( research, &techs )) {
125  research.activetechnology = *techs.begin();
126  research.goal = newTech;
127  } else
128  return ActionResult( 23202 );
129  }
130 
131 
132  vector<const Technology*> researched;
133  techAdaptersMadeAvailable.clear();
134  runResearch( getMap()->getPlayer( player ), &researched, &techAdaptersMadeAvailable );
135  for ( vector<const Technology*>::iterator i = researched.begin(); i != researched.end(); i++ )
136  immediatelyResearchedTechnologies.push_back( (*i)->id );
137 
138 
139  setState( Finished );
140 
141  return ActionResult(0);
142 }
143 
144 
146 {
147  if ( getState() != Finished )
148  return ActionResult(22000);
149 
150  Research& research = getMap()->getPlayer( player ).research;
151 
152  for ( vector<int>::const_iterator i = immediatelyResearchedTechnologies.begin(); i != immediatelyResearchedTechnologies.end(); ++i ) {
153 
154  Technology* t = technologyRepository.getObject_byID( *i );
155  if ( !t )
156  return ActionResult(21005, ASCString::toString(*i));
157 
158  vector<int>::iterator j = find( research.developedTechnologies.begin(), research.developedTechnologies.end(), *i );
159  if ( j != research.developedTechnologies.end() )
160  research.developedTechnologies.erase(j);
161  else
162  return ActionResult(21204, t->name );
163 
164  research.progress += t->researchpoints;
165  }
166 
167  for ( vector<ASCString>::iterator i = techAdaptersMadeAvailable.begin(); i != techAdaptersMadeAvailable.end(); ++i ) {
168  Research::TriggeredTechAdapter::iterator ta = find( research.triggeredTechAdapter.begin(), research.triggeredTechAdapter.end(), *i );
169  if ( ta == research.triggeredTechAdapter.end() )
170  return ActionResult(21203, *i );
171  else
172  research.triggeredTechAdapter.erase( *i );
173  }
174 
175  return Command::undoAction(context);
176 }
177 
178 
180 {
181  ASCString c;
182  c.format("setResearchGoal ( map, %d, %d )", player, targetTechnologyID );
183  return c;
184 }
185 
187 {
189 }
190 
192 {
193  ASCString s = "Choose technology " + ASCString::toString(targetTechnologyID) + " for player " + ASCString::toString(player);
194  return s;
195 }
196 
197 
198 namespace {
199  const bool r1 = registerAction<DirectResearchCommand> ( ActionRegistry::DirectResearchCommand );
200 }
201 
vector< const Technology * > getAvailableTechnologies(bool longTerm)
returns the technologies that can be researched by the player
Player & getPlayer(PlayerID p)
Definition: gamemap.h:257
ActionResult go(const Context &context)
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
int GameActionID
Definition: action.h:35
the command is totally done
Definition: command.h:120
ASCString getCommandString() const
GameActionID getID() const
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
int getPosition() const
Definition: player.h:112
ActionResult undoAction(const Context &context)
void writeData(tnstream &stream) const
bool eventually_available(const Research &res, list< const Technology * > *dependencies) const
Definition: research.cpp:574
vector< int > developedTechnologies
Definition: research.h:183
State getState() const
Definition: command.h:125
GameMap * getMap()
Definition: action.h:92
The interface for all kinds of IO stream.
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
A Command is an action that the player initiates.
Definition: command.h:114
ItemRepositoryLoader< Technology > technologyRepository("technology")
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ResearchAvailabilityStatus
Definition: research.h:45
bool available(const Research &research) const
Definition: research.cpp:61
ASCString getDescription() const
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
TechDependency techDependency
Definition: research.h:143
const Technology * activetechnology
the technology that is currently being researched
Definition: research.h:191
virtual void readData(tnstream &stream)
Definition: command.cpp:31
const Technology * goal
the technology that is defined as long-term goal
Definition: research.h:194
virtual ActionResult undoAction(const Context &context)
Definition: command.cpp:60
void setState(State state)
Definition: command.cpp:44
virtual void writeData(tnstream &stream) const
Definition: command.cpp:38
static const int directResearchCommandStreamVersion
void readClassContainer(C &c, tnstream &stream)
Definition: basestrm.h:752
ResearchAvailabilityStatus techAvailable(const Technology *tech) const
Definition: research.cpp:834
void runResearch(Player &player, vector< const Technology * > *newTechs, vector< ASCString > *newTechAdapter)
progresses in the current research, automatically chosing technologies towards the research goal...
Research research
the status of the scientific research
Definition: player.h:142
void setTechnology(const Technology *tech)
static bool available(const Player &player)
ASCString name
Definition: research.h:137
void writeClassContainer(const C &c, tnstream &stream)
Definition: basestrm.h:742
int researchpoints
Definition: research.h:135
the different players in ASC. There may be 8 players (0..7) and neutral units (8) ...
Definition: player.h:99
void readData(tnstream &stream)
int progress
Definition: research.h:189