spawnbuilding.cpp

Go to the documentation of this file.
00001 /*
00002      This file is part of Advanced Strategic Command; http://www.asc-hq.de
00003      Copyright (C) 1994-2010  Martin Bickel  and  Marc Schellenberger
00004  
00005      This program is free software; you can redistribute it and/or modify
00006      it under the terms of the GNU General Public License as published by
00007      the Free Software Foundation; either version 2 of the License, or
00008      (at your option) any later version.
00009  
00010      This program is distributed in the hope that it will be useful,
00011      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013      GNU General Public License for more details.
00014  
00015      You should have received a copy of the GNU General Public License
00016      along with this program; see the file COPYING. If not, write to the 
00017      Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00018      Boston, MA  02111-1307  USA
00019 */
00020 
00021 
00022 #include "spawnbuilding.h"
00023 #include "viewregistration.h"
00024 #include "unitfieldregistration.h"
00025 #include "removeobject.h"
00026 #include "action-registry.h"
00027 
00028 #include "../vehicle.h"
00029 #include "../gamemap.h"
00030 #include "../viewcalculation.h"
00031      
00032 SpawnBuilding::SpawnBuilding( GameMap* gamemap, const MapCoordinate& position, int buildingTypeID, int owner )
00033    : GameAction( gamemap ), pos(position)
00034 {
00035    this->buildingTypeID = buildingTypeID;
00036    this->owner = owner;
00037    
00038 }
00039             
00040       
00041 ASCString SpawnBuilding::getDescription() const
00042 {
00043    const BuildingType* bld = getMap()->getbuildingtype_byid( buildingTypeID );
00044    ASCString res ;
00045    if ( bld )
00046       res = "Spawn Building of type " + bld->getName();
00047    else
00048       res = "Spawn Building " ;
00049    res += " at " + pos.toString();
00050    return  res;
00051 }
00052       
00053       
00054 static const int SpawnBuildingStreamVersion = 1;
00055       
00056 void SpawnBuilding::readData ( tnstream& stream ) 
00057 {
00058    int version = stream.readInt();
00059    if ( version < 1 || version > SpawnBuildingStreamVersion )
00060       throw tinvalidversion ( "SpawnBuilding", SpawnBuildingStreamVersion, version );
00061    
00062    buildingTypeID = stream.readInt();
00063    pos.read( stream );
00064    owner = stream.readInt();
00065 };
00066       
00067       
00068 void SpawnBuilding::writeData ( tnstream& stream ) const
00069 {
00070    stream.writeInt( SpawnBuildingStreamVersion );
00071    stream.writeInt( buildingTypeID );
00072    pos.write( stream );
00073    stream.writeInt( owner );
00074 };
00075 
00076 
00077 GameActionID SpawnBuilding::getID() const
00078 {
00079    return ActionRegistry::SpawnBuilding;
00080 }
00081 
00082 ActionResult SpawnBuilding::runAction( const Context& context )
00083 {
00084    MapField* fld = getMap()->getField(pos);
00085    if ( !fld )
00086       return ActionResult( 21002, pos );
00087    
00088    const BuildingType* buildingType = getMap()->getbuildingtype_byid( buildingTypeID );
00089    if ( !buildingType )
00090       return ActionResult( 22500, "Building id is " + ASCString::toString(buildingTypeID));
00091    
00092    for ( int a = 0; a < 4; a++)
00093       for ( int b = 0; b < 6; b++ ) 
00094          if ( buildingType->fieldExists ( BuildingType::LocalCoordinate( a, b ) ) ) {
00095             MapField* field = getMap()->getField( buildingType->getFieldCoordinate( pos, BuildingType::LocalCoordinate(a,b) ));
00096             if (field == NULL)
00097                return ActionResult(22501);
00098          }
00099 
00100    for ( int a = 0; a < 4; a++)
00101       for ( int b = 0; b < 6; b++ ) 
00102          if ( buildingType->fieldExists ( BuildingType::LocalCoordinate( a, b ) ) ) {
00103             MapCoordinate fpos = buildingType->getFieldCoordinate( pos, BuildingType::LocalCoordinate(a,b) );
00104             MapField* field = getMap()->getField( fpos );
00105 
00106             vector<int> objectsToDelete;
00107             
00108             MapField::ObjectContainer::iterator i = field->objects.begin();
00109             while ( i != field->objects.end()) {
00110                if ( !i->typ->canExistBeneathBuildings )
00111                   objectsToDelete.push_back( i->typ->id );
00112                i++;
00113             };
00114             
00115             for ( vector<int>::iterator j = objectsToDelete.begin(); j != objectsToDelete.end(); ++j )
00116                (new RemoveObject( getMap(), fpos, *j ))->execute( context );
00117       }
00118 
00119          
00120    Building* gbde = NULL;
00121    if ( !getMap()->getField(pos)->building ) {
00122       gbde = new Building ( getMap(), pos, buildingType, owner );
00123 
00124       gbde->plus = gbde->typ->defaultProduction;
00125       gbde->maxplus = gbde->typ->defaultProduction;
00126       gbde->bi_resourceplus = gbde->typ->defaultProduction;
00127 
00128       gbde->actstorage.fuel = 0;
00129       gbde->actstorage.material = 0;
00130       gbde->actstorage.energy = 0;
00131       gbde->netcontrol = 0;
00132       gbde->connection = 0;
00133       gbde->visible = true;
00134       gbde->setCompletion ( 0 );
00135    }
00136    else {
00137       gbde = getMap()->getField(pos)->building;
00138       if (gbde->getCompletion() < gbde->typ->construction_steps-1)
00139          gbde->setCompletion( gbde->getCompletion()+1 );
00140 
00141    }
00142 
00143    
00144    ActionResult res = (new ViewRegistration( gbde , ViewRegistration::AddView ))->execute( context );
00145    
00146    evaluateviewcalculation( getMap(), gbde->getPosition(), gbde->view, 0, false, &context );
00147    
00148    
00149    return res;
00150 }
00151 
00152 Building* SpawnBuilding::getBuilding()
00153 {
00154    return getMap()->getField(pos)->building; 
00155 }
00156 
00157 
00158 ActionResult SpawnBuilding::undoAction( const Context& context )
00159 {
00160    MapField* fld = getMap()->getField(pos);
00161    if ( !fld )
00162       return ActionResult( 21002, pos );
00163    
00164    const BuildingType* buildingType = getMap()->getbuildingtype_byid( buildingTypeID );
00165    if ( !buildingType )
00166       return ActionResult( 22500, "Building id is " + ASCString::toString(buildingTypeID));
00167    
00168    if ( !fld->building || fld->building->typ != buildingType )
00169       return ActionResult( 22502 );
00170    
00171    if ( fld->building->getCompletion() > 0 )
00172       fld->building->setCompletion( fld->building->getCompletion() -1 );
00173    else
00174       delete fld->building;
00175    
00176    return ActionResult(0);
00177 }
00178 
00179 ActionResult SpawnBuilding::verify()
00180 {
00181    return ActionResult(0);
00182 }
00183 
00184 
00185 namespace {
00186    const bool r1 = registerAction<SpawnBuilding> ( ActionRegistry::SpawnBuilding );
00187 }
00188 

Generated on Mon May 21 01:26:37 2012 for Advanced Strategic Command by  doxygen 1.5.1