Advanced Strategic Command
clipboard.cpp
Go to the documentation of this file.
1 
5 /*
6  This file is part of Advanced Strategic Command; http://www.asc-hq.de
7  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
8 
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; see the file COPYING. If not, write to the
21  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22  Boston, MA 02111-1307 USA
23 */
24 
25 
26 #include "clipboard.h"
27 #include "vehicle.h"
28 #include "buildings.h"
29 #include "gamemap.h"
30 #include "spfst.h"
31 #include "dlg_box.h"
32 #include "statistics.h"
33 #include "spfst-legacy.h"
34 
35 
36 const char* clipboardFileExtension = "*.asc2clipboard";
37 const char* oldClipboardFileExtension = "*.ascclipboard";
38 
39 
40 ClipBoardBase::ClipBoardBase()
41 {
42  objectNum = 0;
43 // TClipboard* cb = Clipboard();
44 }
45 
47 {
48  buf.clear();
49  properties.clear();
50  objectNum = 0;
51 }
52 
53 
54 
55 
56 static int clipboardVersion = 1;
57 
58 static const int clipboardToken = 0x574f0769;
59 
60 void ClipBoardBase::setProperties( const ContainerBase* unit )
61 {
62  properties["strength"] = ASCString::toString( StatisticsCalculator::strength( unit, true ) );
63  properties["materialindex"] = ASCString::toString( StatisticsCalculator::resource( unit, true ).material / 1000 );
64  properties["productioncost"] = ASCString::toString( StatisticsCalculator::unitCost( unit, true ));
65  properties["unitcount"] = ASCString::toString( StatisticsCalculator::unitCount( unit, true ));
66  properties["name"] = unit->getName();
67  properties["player"] = unit->getMap()->getPlayer( unit ).getName();
68  properties["turn"] = ASCString::toString( unit->getMap()->time.turn() );
69  properties["map"] = unit->getMap()->maptitle;
70 
71 
72  ASCIIEncodingStream outerStream;
73  {
74  StreamCompressionFilter stream( &outerStream );
75  stream.writeInt( clipboardToken );
76  write( stream );
77  }
78  properties["data"] = outerStream.getResult();
79 }
80 
81 void ClipBoardBase::addUnit ( const Vehicle* unit )
82 {
83  MemoryStream stream ( &buf, tnstream::appending );
84  stream.writeInt( ClipVehicle );
85  unit->write ( stream );
86  objectNum++;
87 
88  setProperties( unit );
89 }
90 
92 {
93  MemoryStream stream ( &buf, tnstream::appending );
94  stream.writeInt( ClipBuilding );
95  bld->write ( stream );
96  objectNum++;
97 
98  setProperties( bld );
99 
100 }
101 
102 
104 {
105  Vehicle* veh = Vehicle::newFromStream( actmap, stream );
106 
107  // veh->networkid = actmap->getNewNetworkID();
108 
109  return veh;
110 }
111 
113 {
114  if ( !objectNum )
115  return NULL;
116 
117  MemoryStream stream ( &buf, tnstream::reading );
118  Type type = Type(stream.readInt());
119  if ( type == ClipVehicle )
120  return pasteUnit ( stream );
121 
122  return NULL;
123 }
124 
125 
127 {
128  if ( !objectNum )
129  return;
130 
131  MemoryStream stream ( &buf, tnstream::reading );
132  Type type = Type(stream.readInt());
133  if ( type == ClipVehicle ) {
134  MapField* fld = actmap->getField ( pos );
135  Vehicle* veh = pasteUnit ( stream );
136 
137  if ( veh->getOwner() == playerNum-1) {
138  warningMessage("pasting neutral units on the map is not allowed");
139  delete veh;
140  return;
141  }
142 
143  if ( !fieldAccessible ( fld, veh, -2, NULL, true ) && !actmap->getgameparameter( cgp_movefrominvalidfields) ) {
144  delete veh;
145  return;
146  }
147 
148  if ( fld->vehicle )
149  delete fld->vehicle;
150  fld->vehicle = veh;
151  veh->setnewposition( pos.x, pos.y );
152  }
153  if ( type == ClipBuilding ) {
154  Building* bld = Building::newFromStream ( actmap, stream, false );
155 
156  for ( int x = 0; x < 4; x++ )
157  for ( int y = 0; y < 6; y++ )
158  if ( bld->typ->fieldExists ( BuildingType::LocalCoordinate( x , y ) )) {
160  if ( !field ) {
161  delete bld;
162  displaymessage("building does not fit here", 1 );
163  return;
164  }
165 
166  /*
167  if ( !bld->typ->terrainaccess.accessible ( field->bdt ) ) {
168  delete bld;
169  displaymessage("building does can not be build here", 1 );
170  return;
171  }
172  */
173 
174 
175  if ( field->vehicle ) {
176  delete field->vehicle;
177  field->vehicle = NULL;
178  }
179  if ( field->building ) {
180  delete field->building;
181  field->building = NULL;
182  }
183  }
184 
185 
186  bld->chainbuildingtofield( pos );
187  }
188 }
189 
190 
192 {
193  stream.writeInt( clipboardVersion );
194  stream.writeInt( objectNum );
195  buf.writetostream ( &stream );
196 }
197 
199 {
200  stream.readInt(); // Version
201  objectNum = stream.readInt();
202  buf.readfromstream ( &stream );
203 }
204 
206 {
207  for ( map<ASCString,ASCString>::const_iterator i = properties.begin(); i != properties.end(); ++i ) {
208  ASCString temp = i->second;
209  pc.addString( i->first, temp );
210  }
211 }
212 
214 {
215  ASCString data;
216  pc.addString("data", data);
217 
218  ASCIIDecodingStream outerStream ( data );
219  StreamDecompressionFilter stream( &outerStream );
220 
221  int token = stream.readInt();
222  if ( token != clipboardToken )
223  throw ASCmsgException("Invalid clipboard file");
224 
225  read( stream );
226 }
227 
void write(tnstream &stream, bool includeLoadedUnits=true) const
Definition: vehicle.cpp:1121
int fieldAccessible(const MapField *field, const Vehicle *vehicle, int uheight, const bool *attacked, bool ignoreVisibility)
Definition: spfst.cpp:124
Player & getPlayer(PlayerID p)
Definition: gamemap.h:257
ASCString getResult()
Definition: basestrm.cpp:1786
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
MapCoordinate getFieldCoordinate(const MapCoordinate &entryOnMap, const LocalCoordinate &localCoordinate) const
returns the Mapcoordinate of a buildings field
Compresses data transparently into another stream (using bzip2 compression)
Definition: basestrm.h:600
void write(tnstream &stream)
Definition: clipboard.cpp:191
map accessing and usage routines used by ASC and the mapeditor
const BuildingType * typ
Definition: buildings.h:48
const char * oldClipboardFileExtension
Definition: clipboard.cpp:37
Vehicle * vehicle
Definition: mapfield.h:89
The clipboard for ASC.
void setnewposition(int x, int y)
checks whether the unit can construct a building of the given type.
Definition: vehicle.cpp:808
static const int clipboardToken
Definition: clipboard.cpp:58
virtual ASCString getName() const =0
void read(tnstream &stream)
Definition: clipboard.cpp:198
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 chainbuildingtofield(const MapCoordinate &entryPos, bool setupImages=true)
registers the building at the given position on the map
Definition: buildings.cpp:268
ASCString getName() const
returns the name of the player
Definition: player.cpp:274
Reading data from an ASCII encoded String.
Definition: basestrm.h:577
int turn() const
Definition: typen.h:181
int getOwner() const
returns the number of the player this vehicle/building belongs to
A local coordinate referencing a single field that a building covers.
Definition: buildingtype.h:57
static int unitCount(const ContainerBase *c, bool recurse)
Definition: statistics.cpp:52
void warningMessage(const ASCString &str)
The buildings which a placed on the map.
Interface for some basic classes from which all of ASC's dialogs are derived.
The interface for all kinds of IO stream.
a single field of the map
Definition: mapfield.h:26
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
void writetostream(tnstream *stream)
persist the contents in another stream
Definition: basestrm.cpp:2303
Deompresses data from a compressed stream (using bzip2 compression)
Definition: basestrm.h:616
static double strength(const ContainerBase *c, bool recurse)
Definition: statistics.cpp:32
void addBuilding(const Building *bld)
Definition: clipboard.cpp:91
bool fieldExists(const LocalCoordinate &localCoordinate) const
returns whether this building covers the given field
Definition: buildingtype.h:108
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
void place(const MapCoordinate &pos)
Definition: clipboard.cpp:126
void addUnit(const Vehicle *unit)
Definition: clipboard.cpp:81
Coordinate on the twodimensional map.
Definition: typen.h:202
void readProperties(PropertyContainer &pc)
Definition: clipboard.cpp:213
Definition of THE central asc class: GameMap.
Reads data from or writes data to a MemoryStreamStorage This allows a completely volatile storage of ...
Definition: basestrm.h:204
void displaymessage(const char *formatstring, int num,...)
displays a dialog box with a message
Definition: dlg_box.cpp:1849
void clear()
Definition: clipboard.cpp:46
static Vehicle * newFromStream(GameMap *gamemap, tnstream &stream, int forceNetworkID=-1)
generates a new unit, loads its parameters from the stream (where they have been previously written w...
Definition: vehicle.cpp:1728
int getgameparameter(GameParameter num) const
Definition: gamemap.cpp:1047
static int unitCost(const ContainerBase *c, bool recurse)
Definition: statistics.cpp:77
ASCString maptitle
the title of the map
Definition: gamemap.h:213
static Resources resource(const ContainerBase *c, bool recurse)
Definition: statistics.cpp:63
void readfromstream(tnstream *stream)
read persisted contents from another stream
Definition: basestrm.cpp:2316
Building * building
Definition: mapfield.h:102
GameMap * actmap
Definition: spfst.cpp:64
Vehicle * pasteUnit()
Definition: clipboard.cpp:112
GameMap * getMap() const
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
Data written to this stream will be encoded similar to base64 and can be retrieved as a string...
Definition: basestrm.h:557
An actual building on the map, which references a BuildingType Buildings have an owner,.
Definition: buildings.h:38
void addString(const ASCString &name, ASCString &property)
static int clipboardVersion
Definition: clipboard.cpp:56
static Building * newFromStream(GameMap *gamemap, tnstream &stream, bool chainToField=true)
Definition: buildings.cpp:523
const int playerNum
the maximum number of players that ASC can manage.
Definition: typen.h:54
void writeProperties(PropertyContainer &pc) const
Definition: clipboard.cpp:205
const char * clipboardFileExtension
Definition: clipboard.cpp:36
GameTime time
the time in the game, mesured in a turns and moves
Definition: gamemap.h:235
MapField * getField(int x, int y)
Definition: gamemap.h:465
void write(tnstream &stream, bool includeLoadedUnits=true) const
Definition: buildings.cpp:451