Advanced Strategic Command
spawnbuilding.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 "spawnbuilding.h"
23 #include "viewregistration.h"
24 #include "unitfieldregistration.h"
25 #include "removeobject.h"
26 #include "action-registry.h"
27 
28 #include "../vehicle.h"
29 #include "../gamemap.h"
30 #include "../viewcalculation.h"
31 
32 SpawnBuilding::SpawnBuilding( GameMap* gamemap, const MapCoordinate& position, int buildingTypeID, int owner )
33  : GameAction( gamemap ), pos(position)
34 {
35  this->buildingTypeID = buildingTypeID;
36  this->owner = owner;
37 
38 }
39 
40 
42 {
43  const BuildingType* bld = getMap()->getbuildingtype_byid( buildingTypeID );
44  ASCString res ;
45  if ( bld )
46  res = "Spawn Building of type " + bld->getName();
47  else
48  res = "Spawn Building " ;
49  res += " at " + pos.toString();
50  return res;
51 }
52 
53 
54 static const int SpawnBuildingStreamVersion = 1;
55 
57 {
58  int version = stream.readInt();
59  if ( version < 1 || version > SpawnBuildingStreamVersion )
60  throw tinvalidversion ( "SpawnBuilding", SpawnBuildingStreamVersion, version );
61 
62  buildingTypeID = stream.readInt();
63  pos.read( stream );
64  owner = stream.readInt();
65 };
66 
67 
68 void SpawnBuilding::writeData ( tnstream& stream ) const
69 {
71  stream.writeInt( buildingTypeID );
72  pos.write( stream );
73  stream.writeInt( owner );
74 };
75 
76 
78 {
80 }
81 
83 {
84  MapField* fld = getMap()->getField(pos);
85  if ( !fld )
86  return ActionResult( 21002, pos );
87 
88  const BuildingType* buildingType = getMap()->getbuildingtype_byid( buildingTypeID );
89  if ( !buildingType )
90  return ActionResult( 22500, "Building id is " + ASCString::toString(buildingTypeID));
91 
92  for ( int a = 0; a < 4; a++)
93  for ( int b = 0; b < 6; b++ )
94  if ( buildingType->fieldExists ( BuildingType::LocalCoordinate( a, b ) ) ) {
95  MapField* field = getMap()->getField( buildingType->getFieldCoordinate( pos, BuildingType::LocalCoordinate(a,b) ));
96  if (field == NULL)
97  return ActionResult(22501);
98  }
99 
100  for ( int a = 0; a < 4; a++)
101  for ( int b = 0; b < 6; b++ )
102  if ( buildingType->fieldExists ( BuildingType::LocalCoordinate( a, b ) ) ) {
103  MapCoordinate fpos = buildingType->getFieldCoordinate( pos, BuildingType::LocalCoordinate(a,b) );
104  MapField* field = getMap()->getField( fpos );
105 
106  vector<int> objectsToDelete;
107 
108  MapField::ObjectContainer::iterator i = field->objects.begin();
109  while ( i != field->objects.end()) {
110  if ( !i->typ->canExistBeneathBuildings )
111  objectsToDelete.push_back( i->typ->id );
112  i++;
113  };
114 
115  for ( vector<int>::iterator j = objectsToDelete.begin(); j != objectsToDelete.end(); ++j )
116  (new RemoveObject( getMap(), fpos, *j ))->execute( context );
117  }
118 
119 
120  Building* gbde = NULL;
121  if ( !getMap()->getField(pos)->building ) {
122  gbde = new Building ( getMap(), pos, buildingType, owner );
123 
124  gbde->plus = gbde->typ->defaultProduction;
125  gbde->maxplus = gbde->typ->defaultProduction;
126  gbde->bi_resourceplus = gbde->typ->defaultProduction;
127 
128  gbde->actstorage.fuel = 0;
129  gbde->actstorage.material = 0;
130  gbde->actstorage.energy = 0;
131  gbde->netcontrol = 0;
132  gbde->connection = 0;
133  gbde->visible = true;
134  gbde->setCompletion ( 0 );
135  }
136  else {
137  gbde = getMap()->getField(pos)->building;
138  if (gbde->getCompletion() < gbde->typ->construction_steps-1)
139  gbde->setCompletion( gbde->getCompletion()+1 );
140 
141  }
142 
143 
144  ActionResult res = (new ViewRegistration( gbde , ViewRegistration::AddView ))->execute( context );
145 
146  evaluateviewcalculation( getMap(), gbde->getPosition(), gbde->view, 0, false, &context );
147 
148 
149  return res;
150 }
151 
153 {
154  return getMap()->getField(pos)->building;
155 }
156 
157 
159 {
160  MapField* fld = getMap()->getField(pos);
161  if ( !fld )
162  return ActionResult( 21002, pos );
163 
164  const BuildingType* buildingType = getMap()->getbuildingtype_byid( buildingTypeID );
165  if ( !buildingType )
166  return ActionResult( 22500, "Building id is " + ASCString::toString(buildingTypeID));
167 
168  if ( !fld->building || fld->building->typ != buildingType )
169  return ActionResult( 22502 );
170 
171  if ( fld->building->getCompletion() > 0 )
172  fld->building->setCompletion( fld->building->getCompletion() -1 );
173  else
174  delete fld->building;
175 
176  return ActionResult(0);
177 }
178 
180 {
181  return ActionResult(0);
182 }
183 
184 
185 namespace {
186  const bool r1 = registerAction<SpawnBuilding> ( ActionRegistry::SpawnBuilding );
187 }
188 
int fuel
Definition: typen.h:101
virtual ActionResult verify()
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
const BuildingType * typ
Definition: buildings.h:48
int GameActionID
Definition: action.h:35
virtual void readData(tnstream &stream)
int energy
Definition: typen.h:99
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
A local coordinate referencing a single field that a building covers.
Definition: buildingtype.h:57
void read(tnstream &stream)
Definition: typen.h:213
GameMap * getMap()
Definition: action.h:92
ASCString toString(bool coordinates=false) const
Definition: typen.cpp:304
The interface for all kinds of IO stream.
a single field of the map
Definition: mapfield.h:26
int getCompletion() const
returns the completion of the building.
Definition: buildings.h:164
int netcontrol
a bitmapped variable containing the status of the resource-net connection.
Definition: buildings.h:57
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ASCString getDescription() const
Building * getBuilding()
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
The class describing properties that are common to all buildings of a certain kind.
Definition: buildingtype.h:35
ActionResult execute(const Context &context)
Definition: action.cpp:41
void write(tnstream &stream) const
Definition: typen.h:212
static const int SpawnBuildingStreamVersion
Coordinate on the twodimensional map.
Definition: typen.h:202
int evaluateviewcalculation(GameMap *gamemap, int player_fieldcount_mask, bool disableShareView, const Context *context)
evaluates the view on the whole map.
Resources maxplus
the maximum amount of Resources that the building can produce each turn in the ASC resource mode ; se...
int material
Definition: typen.h:100
void setCompletion(int completion, bool setupImages=true)
Sets the level of completion of the building.
Definition: buildings.cpp:259
virtual GameActionID getID() const
BuildingType * getbuildingtype_byid(int id)
Definition: gamemap.cpp:1805
Resources plus
the Resources that are produced each turn
int connection
bitmapped: are there events that are triggered by actions affecting this building ...
Definition: buildings.h:60
Resources actstorage
the current storage of Resources
Definition: buildings.h:91
Resources bi_resourceplus
the maximum amount of Resources that the building can produce each turn in the BI resource mode ; see...
int construction_steps
the number of stages that are required to construct a building using a construction unit...
Definition: buildingtype.h:89
Building * building
Definition: mapfield.h:102
ASCString getName() const
An actual building on the map, which references a BuildingType Buildings have an owner,.
Definition: buildings.h:38
A GameAction is an modification of something on a map.
Definition: action.h:45
virtual void writeData(tnstream &stream) const
virtual ActionResult undoAction(const Context &context)
bool visible
is the building visible? Building can be made invisible, but this feature should be used only in some...
Definition: buildings.h:63
virtual ActionResult runAction(const Context &context)
Resources defaultProduction
if a new building is constructed, this will be the resource production of the building ...
MapCoordinate3D getPosition() const
returns the position of the buildings entry
Definition: buildings.h:152
ObjectContainer objects
Definition: mapfield.h:124
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
MapField * getField(int x, int y)
Definition: gamemap.h:465