Advanced Strategic Command
containerbasetype.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  containerbasetype.h - description
3  -------------------
4  begin : Sun Feb 18 2001
5  copyright : (C) 2001 by Martin Bickel
6  email : bickel@asc-hq.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "containerbasetype.h"
19 #include "textfiletags.h"
20 #include "textfile_evaluation.h"
21 #include "vehicletype.h"
22 #include "graphics/blitter.h"
23 
24 
26  { "training",
27  "internal vehicle production",
28  "ammunition production",
29  "internal unit repair",
30  "recycling",
31  "research",
32  "sonar",
33  "view satellites",
34  "view mines",
35  "wind power plant",
36  "solar power plant",
37  "matter converter",
38  "mining station",
39  "construct units that cannot move out",
40  "resource sink",
41  "external energy transfer",
42  "external material transfer",
43  "external fuel transfer",
44  "external ammo transfer",
45  "external repair",
46  "no object chaining",
47  "self destruct on conquer",
48  "paratrooper",
49  "mine-layer",
50  "cruiser landing",
51  "conquer buildings",
52  "move after attack",
53  "external vehicle production",
54  "construct specific buildings",
55  "icebreaker",
56  "cannot be refuelled in air",
57  "makes tracks",
58  "search for mineral resources automatically",
59  "no reactionfire",
60  "auto repair",
61  "Kamikaze only",
62  "immune to mines",
63  "jams only own field",
64  "move with reaction fire on",
65  "only move to and from transports",
66  "AutoHarvestObjects",
67  "No customization of production",
68  "Manual self-destruct",
69  "No removal of mines",
70  "No rotation of image",
71  NULL };
72 
73 
75 {
76  maxLoadableUnits = 0;
80  id = 0;
81  jamming = 0;
82  view = 0;
83  efficiencyfuel = 1024;
84  efficiencymaterial = 1024;
85 
89  vehicleCategoriesProduceable = 0xfffffff;
90  autoHarvest.range = 0;
91  autoHarvest.maxFieldsPerTurn = maxint;
93 }
94 
96 {
97  int f = int(function);
98  return features.test( f );
99 }
100 
102 {
103  bool result = (features & functions).any();
104  return result;
105 }
106 
108 {
109  features.set( int(function) );
110 }
111 
112 
113 
114 
116 {
117  if ( function < functionNum )
118  return ccontainerfunctions[function];
119  else
120  return NULL;
121 }
122 
123 
125 {
126  mode = 0;
127  height_abs = 0;
128  height_rel = 0;
129  container_height = 0;
131  dockingHeight_abs = 0;
132  dockingHeight_rel = 0;
133  disableAttack = false;
134  movecost = -1;
135 }
136 
137 
139 {
140  pc.addTagInteger( "Mode", mode, entranceModeNum, entranceModes );
141  pc.addTagInteger( "UnitHeightAbs", height_abs, choehenstufennum, heightTags );
142  pc.addInteger( "UnitHeightRel", height_rel, -100 );
143  pc.addTagInteger( "ContainerHeight", container_height, choehenstufennum, heightTags );
144  pc.addTagInteger( "CategoriesNOT", vehicleCategoriesLoadable, cmovemalitypenum, unitCategoryTags, true );
145  pc.addTagInteger( "DockingHeightAbs", dockingHeight_abs, choehenstufennum, heightTags, 0 );
146  pc.addInteger( "DockingHeightRel", dockingHeight_rel, -100 );
147  if ( pc.find( "RequireUnitFunction" )) {
148  int r = 0;
149  pc.addTagInteger( "RequireUnitFunction", r, VehicleType::legacyVehicleFunctionNum, vehicleAbilities, 0 );
150  requiresUnitFeature = VehicleType::convertOldFunctions(r, pc.getFileName() );
151  } else
152  if ( pc.find( "RequiresUnitFeature" ) || !pc.isReading() )
153  pc.addTagArray( "RequiresUnitFeature", requiresUnitFeature, ContainerBaseType::functionNum, containerFunctionTags );
154  else
155  requiresUnitFeature.reset();
156 
157  pc.addBool( "DisableAttack", disableAttack, false );
158  pc.addInteger( "MoveCost", movecost, -1 );
159  if ( movecost < 10 && movecost >= 0 )
160  fatalError ( "MoveCost for TransportationIO is lower than 10! file: " + pc.getFileName() );
161 }
162 
163 
165 {
166  pc.addBreakpoint();
167  pc.openBracket ( "Transportation" );
168  int num = entranceSystems.size();
169  pc.addInteger ( "EntranceSystemNum", num, 0 );
170  entranceSystems.resize(num);
171  for ( int i = 0; i < num; i++ ) {
172  pc.openBracket ( ASCString("EntranceSystem") + strrr(i) );
173  entranceSystems[i].runTextIO( pc );
174  pc.closeBracket();
175  }
176  pc.addInteger ( "MaxLoadableUnits", maxLoadableUnits, 0 );
177 
178  pc.addInteger ( "MaxLoadableUnitSize", maxLoadableUnitSize, maxint );
179  pc.addInteger ( "MaxLoadableMass", maxLoadableWeight, maxint );
181  pc.closeBracket();
182 
183  pc.addString( "Name", name );
184  pc.addString( "Description", description, "" );
185  pc.addString( "Infotext", infotext, "" );
186 
187  while ( infotext.find ( "#CRT#" ) != ASCString::npos )
188  infotext.replace ( infotext.find ( "#CRT#" ), 5, "\n" );
189  while ( infotext.find ( "#crt#" ) != ASCString::npos )
190  infotext.replace ( infotext.find ( "#crt#" ), 5, "\n" );
191  while ( infotext.find ( "\r" ) != ASCString::npos )
192  infotext.replace ( infotext.find ( "\r" ), 1, "" );
193 
194  pc.addInteger( "ID", id );
195 
196  if ( pc.find( "SecondaryIDs") || !pc.isReading())
197  pc.addIntegerArray("SecondaryIDs", secondaryIDs );
198 
199  pc.addInteger( "View", view );
200  if ( view > maxViewRange )
201  view = maxViewRange;
202 
203  pc.addInteger( "Jamming", jamming, 0 );
204  pc.addString( "InfoImage", infoImageFilename, "" );
205  pc.addString( "InfoImageSmall", infoImageSmallFilename, "" );
206 
207  if ( infoImageFilename.find_first_of("\n\r") != ASCString::npos )
208  fatalError ( "Invalid InfoImage filaname in file: " + pc.getFileName() );
209 
210  if ( infoImageSmallFilename.find_first_of("\n\r") != ASCString::npos )
211  fatalError ( "Invalid InfoImageSmall filaname in file: " + pc.getFileName() );
212 
213 
214  pc.openBracket ( "MaxResourceProduction" );
215  maxplus.runTextIO ( pc, Resources(0,0,0) );
216  pc.closeBracket ();
217 
218  pc.openBracket ( "ResourceExtractionEfficiency");
219  pc.addInteger( "Material", efficiencymaterial, 1024 );
220  pc.addInteger( "Fuel", efficiencyfuel, 1024 );
221  pc.closeBracket ();
222 
223  pc.openBracket ( "StorageCapacity" );
224  pc.openBracket( "BImode" );
225  bi_mode_tank.runTextIO ( pc, Resources(0,0,0) );
226  pc.closeBracket();
227  pc.openBracket ( "ASCmode" );
228  asc_mode_tank.runTextIO ( pc, Resources(0,0,0) );
229  pc.closeBracket();
230  pc.closeBracket ();
231 
232  pc.addInteger ( "MaxResearch", maxresearchpoints, 0 );
233  pc.addInteger ( "NominalResearch", nominalresearchpoints, maxresearchpoints/2 );
234  pc.addInteger ( "MaxResearchpointsDefault", defaultMaxResearchpoints, maxresearchpoints );
235 
236  pc.openBracket( "DefaultProduction" );
237  defaultProduction.runTextIO ( pc, Resources(0,0,0) );
238  pc.closeBracket();
239 
241 
242  pc.openBracket( "AutoHarvestObjects" );
243  pc.addIntRangeArray( "objects", autoHarvest.objectsHarvestable, false );
244  pc.addIntRangeArray( "objectGroups", autoHarvest.objectGroupsHarvestable, false );
245  pc.addInteger( "Range", autoHarvest.range, 0 );
246  pc.addInteger( "MaxFieldsPerTurn", autoHarvest.maxFieldsPerTurn, maxint);
247  pc.closeBracket();
248 
249 
250  pc.openBracket ( "Construction" );
251  pc.addIntRangeArray ( "VehiclesInternally", vehiclesInternallyProduceable, false );
252  productionEfficiency.runTextIO("ProductionEfficiency", pc, productionEfficiency );
253  pc.closeBracket();
254 
255  pc.addInteger("minFieldRepairDamage", minFieldRepairDamage , 0 );
256 }
257 
258 
260 {
261  if ( maxLoadableUnits > 0 )
262  if ( maxLoadableWeight > 0 )
263  if ( vehicleCategoriesStorable & (1<<fzt->movemalustyp) )
264  if ( maxLoadableUnitSize >= fzt->maxsize() )
265  return true;
266 
267  return false;
268 }
269 
270 int ContainerBaseType :: vehicleUnloadable ( const VehicleType* vehicleType, int carrierHeight ) const
271 {
272  int height = 0;
273 
274  int carrierBinHeight = 1 << carrierHeight;
275 
276  if ( vehicleFit ( vehicleType ))
277  for ( ContainerBaseType::EntranceSystems::const_iterator i = entranceSystems.begin(); i != entranceSystems.end(); i++ )
279  if ( (i->container_height & carrierBinHeight) || (i->container_height == 0))
280  if ( i->vehicleCategoriesLoadable & (1<<vehicleType->movemalustyp))
281  if ( vehicleType->hasAnyFunction(i->requiresUnitFeature) || i->requiresUnitFeature.none() ) {
282  if ( i->height_abs != 0 && i->height_rel != -100 ) {
283  int h = 0;
284  for ( int hh = 0; hh < 8; ++hh)
285  if ( getheightdelta(carrierHeight, hh) == i->height_rel )
286  h += 1 << hh;
287  height |= i->height_abs & h;
288  } else
289  if ( i->height_rel != -100 )
290  height |= 1 << (carrierHeight + i->height_rel) ;
291  else
292  height |= i->height_abs ;
293  }
294  return height & vehicleType->height;
295 }
296 
297 
299 
300 
302 {
303  int version = stream.readInt();
304  if ( version > containerBaseTypeVersion || version < 1 ) {
305  ASCString s = "invalid version for reading ContainerBaseType: ";
306  s += strrr ( version );
307  throw ASCmsgException ( s );
308  }
309  maxLoadableUnits = stream.readInt();
310  maxLoadableUnitSize = stream.readInt();
311  maxLoadableWeight = stream.readInt();
313  int num = stream.readInt();
314  entranceSystems.resize(num);
315  for ( int i = 0; i < num; i++ )
316  entranceSystems[i].read( stream );
317 
318  if ( version >= 2 )
319  infoImageFilename = stream.readString(true);
320 
321  if ( version >= 3 )
322  stream.readBitset( features );
323 
324  if ( version >= 4 )
326 
327  if ( version >= 5 )
328  readClassContainer( secondaryIDs, stream );
329 
330  if ( version >= 6 ) {
331  autoHarvest.range = stream.readInt();
332  readClassContainer( autoHarvest.objectsHarvestable, stream );
333  readClassContainer( autoHarvest.objectGroupsHarvestable, stream );
334  autoHarvest.maxFieldsPerTurn = stream.readInt();
335 
337  productionEfficiency.read( stream );
338  }
339  if ( version >= 7 )
341 
342  if ( version >= 8 )
343  minFieldRepairDamage = stream.readInt();
344  else
346 }
347 
348 void ContainerBaseType :: write ( tnstream& stream ) const
349 {
351  stream.writeInt( maxLoadableUnits );
352  stream.writeInt( maxLoadableUnitSize );
353  stream.writeInt( maxLoadableWeight );
355  stream.writeInt( entranceSystems.size() );
356  for ( int i = 0; i < entranceSystems.size(); i++ )
357  entranceSystems[i].write( stream );
358  stream.writeString( infoImageFilename );
359  stream.writeBitset( features );
362 
363  stream.writeInt( autoHarvest.range );
364  writeClassContainer( autoHarvest.objectsHarvestable, stream );
365  writeClassContainer( autoHarvest.objectGroupsHarvestable, stream );
366  stream.writeInt( autoHarvest.maxFieldsPerTurn );
367 
369  productionEfficiency.write( stream );
371  stream.writeInt( minFieldRepairDamage );
372 }
373 
375 
376 
378 {
379  int version = stream.readInt();
380  if ( version > containerBaseTypeTransportVersion || version < 1 ) {
381  ASCString s = "invalid version for reading ContainerBaseTypeTransportation: ";
382  s += strrr ( version );
383  throw ASCmsgException ( s );
384  }
385  mode = stream.readInt();
386  height_abs = stream.readInt();
387  height_rel = stream.readInt();
388  container_height = stream.readInt();
389  vehicleCategoriesLoadable = stream.readInt();
390  dockingHeight_abs = stream.readInt();
391  dockingHeight_rel = stream.readInt();
392  if ( version <= 2 ) {
393  int r = stream.readInt();
394  requiresUnitFeature = VehicleType::convertOldFunctions(r, stream.getLocation());
395  } else
396  stream.readBitset( requiresUnitFeature );
397 
398  disableAttack = stream.readInt();
399  if ( version >= 2 )
400  movecost = stream.readInt();
401  else
402  movecost = -1;
403 
404 }
405 
407 {
409  stream.writeInt ( mode );
410  stream.writeInt ( height_abs );
411  stream.writeInt ( height_rel );
412  stream.writeInt ( container_height );
413  stream.writeInt ( vehicleCategoriesLoadable );
414  stream.writeInt ( dockingHeight_abs );
415  stream.writeInt ( dockingHeight_rel );
416  stream.writeBitset ( requiresUnitFeature );
417  stream.writeInt ( disableAttack );
418  stream.writeInt ( movecost );
419 }
420 
421 
423 {
424  if ( mode == 1 )
425  return bi_mode_tank;
426  else
427  return asc_mode_tank;
428 }
429 
430 
ASCString infoImageSmallFilename
static const char * getFunctionName(ContainerFunctions function)
void write(tnstream &stream) const
Definition: typen.cpp:146
int defaultMaxResearchpoints
when a building of this type is placed on a map, its maxResearch property will be set to this value ...
int maxLoadableUnits
the maximum number of units that can be loaded
void addBool(const ASCString &name, bool &property)
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
void writeBitset(const std::bitset< N > &bs)
int maxsize(void) const
returns the maximum weight of this unit without fuel and material
const int choehenstufennum
The number of levels of height.
Definition: typen.h:67
virtual ASCString getLocation()
returns the location of the stream.
Definition: basestrm.cpp:274
void write(tnstream &stream) const
void runTextIO(const ASCString &name, PropertyContainer &pc)
Definition: typen.cpp:154
int getheightdelta(const ContainerBase *c1, const ContainerBase *c2)
calculate the height difference between two levels of height.
Definition: spfst.cpp:393
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
ResourceMatrix productionEfficiency
void runTextIO(PropertyContainer &pc)
registers the properties of this item for loading/writing into asctxt files
bool hasFunction(ContainerFunctions function) const
void addIntegerArray(const ASCString &name, vector< int > &property, bool required=true)
Functions to evaluate the parsed *.asctxt files.
vector< int > secondaryIDs
when loading a file and these IDs are encountered, this object will be used.
void runTextIO(PropertyContainer &pc)
Definition: typen.cpp:254
void addTagInteger(const ASCString &name, int &property, int tagNum, const char **tags, bool inverted=false)
struct ContainerBaseType::@5 autoHarvest
static const int legacyVehicleFunctionNum
Definition: vehicletype.h:187
bool vehicleFit(const VehicleType *type) const
can units of the given type be moved into this buildtype? This is a prerequisite - but not the only r...
static const int functionNum
The interface for all kinds of IO stream.
void runTextIO(PropertyContainer &pc)
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ASCString description
short description of the units role, for example "strategic bomber"
int vehicleCategoriesStorable
bitmapped: vehicle categories that can be stored the container
vector< IntRange > vehiclesInternallyProduceable
the default production capabilities of this container.
int view
the visibility range
virtual void writeString(const string &pc, bool binary=true)
writes the C++ String pc to the stream.
Definition: basestrm.cpp:545
int maxLoadableUnitSize
the maximum size of a unit that may enter the building
virtual ASCString getFileName()=0
void read(tnstream &stream)
Definition: typen.cpp:138
void setFunction(ContainerFunctions function)
The class describing properties that are common to all vehicles of a certain kind.
Definition: vehicletype.h:177
const char * entranceModes[ContainerBaseType::TransportationIO::entranceModeNum]
const int containerBaseTypeVersion
char * strrr(int a)
converts a to a string.
Definition: misc.cpp:66
int vehicleCategoriesProduceable
bitmapped: vehicle categories that can be produced in the container
int height_rel
a height of a loadable unit have this height relative to the container; -100 if not used ...
int vehicleUnloadable(const VehicleType *vehicleType, int carrierHeight) const
checks the unloading of a unit type
bool hasAnyFunction(std::bitset< 64 > functions) const
static BitSet convertOldFunctions(int abilities, const ASCString &location)
std::bitset< 64 > features
int movemalustyp
The category of the unit. Original used only to distinguish only between different movement costs for...
Definition: vehicletype.h:206
const int cmovemalitypenum
The number of vehicle categories; Each category has its own move malus.
Definition: typen.h:384
int jamming
the radar jamming power
const int maxViewRange
Definition: typen.h:558
ASCString infoImageFilename
the filename of an image that is shown in info dialogs
EntranceSystems entranceSystems
int efficiencymaterial
currently only used by mining stations: the efficiency of the resource extraction from the ground...
const char * heightTags[choehenstufennum]
const char * ccontainerfunctions[ContainerBaseType::functionNum+1]
void readClassContainer(C &c, tnstream &stream)
Definition: basestrm.h:752
int vehicleCategoriesLoadable
bitmapped: vehicle categories that may enter the container
Resources getStorageCapacity(int mode) const
returns the Storage capacity of the unit
const char * unitCategoryTags[cmovemalitypenum]
virtual ASCString readString(bool includeCR=false)
Reads and returns a string.
Definition: basestrm.cpp:535
int height
the levels of height which this unit can enter
void addInteger(const ASCString &name, int &property)
const int containerBaseTypeTransportVersion
void readBitset(std::bitset< N > &bs)
int nominalresearchpoints
the number of reseach points for which the plus settings apllies
virtual void closeBracket()
int height_abs
a loadable unit must be on this level of height; 0 if not used
#define maxint
Definition: typen.h:462
void writeClassContainer(const C &c, tnstream &stream)
Definition: basestrm.h:742
int maxresearchpoints
the maximum number of research points a research center may produce
virtual void openBracket(const ASCString &name)
ASCString infotext
an extensive information about the unit/building which may be several paragraphs long ...
Resources are basically the currency of ASC.
Definition: typen.h:97
void addString(const ASCString &name, ASCString &property)
const char * containerFunctionTags[ContainerBaseType::functionNum]
void addIntRangeArray(const ASCString &name, vector< IntRange > &property, bool required=true)
bool find(const ASCString &name)
std::bitset< 64 > BitSet
Definition: basictypes.h:48
int minFieldRepairDamage
if the unit is repaired in the field (that is, not inside a transport or building), it can only be repaired up to this level 0 would result in a complete repair being possible, 20 that it can only be restored to 20% damage (80% health).
int maxLoadableWeight
the maximum total weight of all loaded units
void fatalError(const ASCString &string)
void addTagArray(const ASCString &name, BitSet &property, int tagNum, const char **tags, bool inverted=false)
Resources defaultProduction
if a new building is constructed, this will be the resource production of the building ...
ASCString name
a short name, for example B-52
const char * vehicleAbilities[VehicleType::legacyVehicleFunctionNum]
deprecated
void write(tnstream &stream) const
write the binary representation of this item to the given stream
void read(tnstream &stream)
read the binary representation of this item from the given stream
int container_height
the container must be on this height
int efficiencyfuel
currently only used by mining stations: the efficiency of the resource extraction from the ground...