Advanced Strategic Command
renamecontainercommand.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 "renamecontainercommand.h"
23 
24 #include "../vehicle.h"
25 #include "../mapfield.h"
26 #include "../gamemap.h"
27 #include "../viewcalculation.h"
28 #include "../spfst.h"
29 #include "../mapdisplayinterface.h"
30 #include "action-registry.h"
31 #include "../itemrepository.h"
32 #include "../containercontrols.h"
33 #include "consumeresource.h"
34 #include "servicecommand.h"
35 #include "convertcontainer.h"
36 
38 {
39  if ( !container )
40  return false;
41 
42  return true;
43 }
44 
45 
46 RenameContainerCommand :: RenameContainerCommand ( ContainerBase* container )
47  : ContainerCommand ( container )
48 {
49 
50 }
51 
52 
53 
54 void RenameContainerCommand::setName( const ASCString& publicName, const ASCString& privateName )
55 {
56  this->publicName = publicName;
57  this->privateName = privateName;
58  setState( SetUp );
59 }
60 
61 
63 {
64  if ( getState() != SetUp )
65  return ActionResult(22000);
66 
67  if ( !avail( getContainer() ))
68  return ActionResult(22800);
69 
70  ContainerBase* container = getContainer();
71 
72  originalPublicName = container->name;
73  originalPrivateName = container->privateName;
74  container->name = publicName;
75  container->privateName = privateName;
76 
77  setState( Finished );
78 
79  return ActionResult(0);
80 }
81 
83 {
84  ContainerBase* container = getContainer();
85  if ( !container )
86  return ActionResult(23600);
87 
88  container->name = originalPublicName;
89  container->privateName = originalPrivateName;
90 
91  return ContainerCommand::undoAction( context );
92 }
93 
94 
95 static const int RenameContainerCommandVersion = 1;
96 
98 {
100  int version = stream.readInt();
101  if ( version > RenameContainerCommandVersion )
102  throw tinvalidversion ( "RenameContainerCommand", RenameContainerCommandVersion, version );
103  originalPublicName = stream.readString();
104  originalPrivateName = stream.readString();
105  publicName = stream.readString();
106  privateName = stream.readString();
107 }
108 
110 {
111  ContainerCommand::writeData( stream );
113  stream.writeString( originalPublicName );
114  stream.writeString( originalPrivateName );
115  stream.writeString( publicName );
116  stream.writeString( privateName );
117 }
118 
119 
121 {
122  ASCString c;
123  c.format("renameContainer ( map, %d, '%s', '%s' )", getContainerID(), publicName.c_str(), privateName.c_str() );
124  return c;
125 
126 }
127 
129 {
131 }
132 
134 {
135  ASCString s = "Rename ";
136 
137  if ( getContainer(true) ) {
138  s += getContainer()->baseType->getName();
139  } else
140  s += originalPublicName;
141 
142  s += " to " + publicName;
143 
144  return s;
145 }
146 
147 namespace
148 {
149  const bool r1 = registerAction<RenameContainerCommand> ( ActionRegistry::RenameContainerCommand );
150 }
151 
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 name
a name given by the user or the map creator
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
State getState() const
Definition: command.h:125
ASCString getCommandString() const
void readData(tnstream &stream)
The interface for all kinds of IO stream.
ActionResult go(const Context &context)
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ActionResult undoAction(const Context &context)
int getContainerID() const
virtual void writeString(const string &pc, bool binary=true)
writes the C++ String pc to the stream.
Definition: basestrm.cpp:545
void setName(const ASCString &publicName, const ASCString &privateName)
virtual ActionResult undoAction(const Context &context)
Definition: command.cpp:60
void setState(State state)
Definition: command.cpp:44
void writeData(tnstream &stream) const
virtual ASCString readString(bool includeCR=false)
Reads and returns a string.
Definition: basestrm.cpp:535
ContainerBase * getContainer(bool dontThrow=false)
static bool avail(const ContainerBase *container)
ASCString getName() const
ASCString privateName
a name given by the user which is only visible to him and his allies.
const ContainerBaseType * baseType
the type descriping all non-instance specific properties of the container
Definition: containerbase.h:80
void readData(tnstream &stream)
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
static const int RenameContainerCommandVersion
void writeData(tnstream &stream) const