Advanced Strategic Command
cancelresearchcommand.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 "cancelresearchcommand.h"
23 
24 #include "action-registry.h"
25 #include "../gamemap.h"
26 #include "../itemrepository.h"
27 
29  : Command( map ), researchProgress(-1), activeTechnologyID(-1), targetTechnologyID(-1), player(-1)
30 {
31 }
32 
33 
35 {
36  this->player = player.getPosition();
37  setState( SetUp );
38 }
39 
40 
42 {
43  Command::readData( stream );
44  stream.readInt();
45  researchProgress = stream.readInt();
46  activeTechnologyID = stream.readInt();
47  targetTechnologyID = stream.readInt();
48  player = stream.readInt();
49 }
50 
51 
53 {
54  Command::writeData( stream );
55  stream.writeInt( 1 );
56  stream.writeInt( researchProgress );
57  stream.writeInt( activeTechnologyID );
58  stream.writeInt( targetTechnologyID );
59  stream.writeInt( player );
60 }
61 
63 {
64  if ( getState() != SetUp )
65  return ActionResult(22000);
66 
67  Research& research = getMap()->getPlayer( player ).research;
68 
69  researchProgress = research.progress;
70  if ( research.activetechnology )
71  activeTechnologyID = research.activetechnology->id;
72  else
73  activeTechnologyID = -1;
74 
75  if ( research.goal )
76  targetTechnologyID = research.goal->id;
77  else
78  targetTechnologyID = -1;
79 
80 
81  research.progress = 0;
82  research.activetechnology = NULL;
83  research.goal = NULL;
84 
85  setState( Finished );
86 
87  return ActionResult(0);
88 }
89 
91 {
92  Research& research = getMap()->getPlayer( player ).research;
93 
94  int _progress = research.progress;
95  int _activeTechnologyID;
96  if ( research.activetechnology )
97  _activeTechnologyID = research.activetechnology->id;
98  else
99  _activeTechnologyID = -1;
100 
101  int _targetTechnologyID;
102  if ( research.goal )
103  _targetTechnologyID = research.goal->id;
104  else
105  _targetTechnologyID = -1;
106 
107 
108  if ( _progress != researchProgress
109  || _activeTechnologyID != activeTechnologyID
110  || _targetTechnologyID != targetTechnologyID )
111  return ActionResult( 23000 );
112 
113  return ActionResult(0);
114 }
115 
117 {
118  if ( getState() != Finished )
119  return ActionResult(22000);
120 
121  Research& research = getMap()->getPlayer( player ).research;
122 
123 
124  if ( activeTechnologyID != -1 ) {
125  Technology* tech = technologyRepository.getObject_byID( activeTechnologyID );
126  if ( !tech )
127  return ActionResult( 23001 );
128  else
129  research.activetechnology = tech;
130  }
131 
132  if ( targetTechnologyID != -1 ) {
133  Technology* tech = technologyRepository.getObject_byID( targetTechnologyID );
134  if ( !tech )
135  return ActionResult( 23001 );
136  else
137  research.goal = tech;
138  }
139 
140  research.progress = researchProgress;
141 
142  return Command::undoAction(context);
143 }
144 
145 
147 {
148  ASCString c;
149  c.format("cancelResearch ( map, %d )", player );
150  return c;
151 }
152 
154 {
156 }
157 
159 {
160  ASCString s = "Cancel research for player " + ASCString::toString(player);
161  return s;
162 }
163 
164 
165 namespace {
166  const bool r1 = registerAction<CancelResearchCommand> ( ActionRegistry::CancelResearchCommand );
167 }
168 
Player & getPlayer(PlayerID p)
Definition: gamemap.h:257
ActionResult undoAction(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
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
ASCString getCommandString() const
State getState() const
Definition: command.h:125
void setPlayer(const Player &player)
void writeData(tnstream &stream) const
GameMap * getMap()
Definition: action.h:92
ActionResult go(const Context &context)
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
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
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
ASCString getDescription() const
void readData(tnstream &stream)
Research research
the status of the scientific research
Definition: player.h:142
ActionResult preCheck()
preCheck is called before an action is redone and should check the preconditions for the action...
the different players in ASC. There may be 8 players (0..7) and neutral units (8) ...
Definition: player.h:99
int progress
Definition: research.h:189
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
GameActionID getID() const