discoverresources.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 "discoverresources.h"
00023 #include "action-registry.h"
00024 
00025 #include "../gamemap.h"
00026      
00027 DiscoverResources::DiscoverResources(  Vehicle* unit )
00028    : UnitAction( unit)
00029 {
00030    shareview = 1 << ( unit->color / 8);
00031    for ( int i = 0; i < 8; i++ )
00032       if ( i*8 != unit->color )
00033          if ( getMap()->player[i].exist() )
00034             if ( getMap()->getPlayer(unit).diplomacy.sharesView(i) )
00035                shareview += 1 << i;
00036 
00037 }
00038       
00039       
00040 ASCString DiscoverResources::getDescription() const
00041 {
00042    return "Discover Resources";
00043 }
00044 
00045 
00046 void DiscoverResources::ResourceViewState::read ( tnstream& stream ) 
00047 {
00048    pos.read( stream );
00049    fuel = stream.readInt();
00050    material = stream.readInt();
00051    visible = stream.readInt();
00052    player = stream.readInt();
00053 }
00054 
00055 void DiscoverResources::ResourceViewState::write ( tnstream& stream ) const
00056 {
00057    pos.write( stream );
00058    stream.writeInt( fuel );
00059    stream.writeInt( material );
00060    stream.writeInt( visible );
00061    stream.writeInt( player );
00062 }
00063 
00064 void DiscoverResources::ResourceViewState::initFromField( const MapField* fld, int player )
00065 {
00066    this->player = player;
00067    if ( fld->resourceview ) {
00068       fuel = fld->resourceview->fuelvisible[player];
00069       material = fld->resourceview->materialvisible[player];
00070       visible = fld->resourceview->visible & (1 << player);
00071    } else {
00072       fuel = 0;
00073       material = 0;
00074       visible = 0;
00075    }
00076 }
00077 
00078 void DiscoverResources::ResourceViewState::writeToField( MapField* fld )
00079 {
00080    if ( !fld->resourceview ) 
00081       fld->resourceview= new MapField::Resourceview;
00082    fld->resourceview->fuelvisible[player] = fuel;
00083    fld->resourceview->materialvisible[player] = material; 
00084    if ( visible )
00085       fld->resourceview->visible |= (1 << player);
00086    else
00087       fld->resourceview->visible &= ~(1 << player);
00088 }
00089 
00090       
00091 void DiscoverResources::readData ( tnstream& stream ) 
00092 {
00093    int version = stream.readInt();
00094    if ( version != 1 )
00095       throw tinvalidversion ( "DiscoverResources", 1, version );
00096    
00097    readClassContainer( newState, stream );
00098    readClassContainer( oldState, stream );
00099 };
00100       
00101       
00102 void DiscoverResources::writeData ( tnstream& stream ) const
00103 {
00104    stream.writeInt( 1 );
00105    writeClassContainer( newState, stream );
00106    writeClassContainer( oldState, stream );
00107 };
00108 
00109 
00110 GameActionID DiscoverResources::getID() const
00111 {
00112    return ActionRegistry::DiscoverResources;
00113 }
00114 
00115 
00116 
00117 void  DiscoverResources::testField( const MapCoordinate& mc )
00118 {
00119     MapField* fld = getMap()->getField ( mc );
00120     if ( !fld->building  ||  fld->building->color == getMap()->actplayer*8  ||  fld->building->color == 8*8)
00121        if ( !fld->vehicle  ||  fld->vehicle->color == getMap()->actplayer*8 ||  fld->vehicle->color == 8*8) {
00122           if ( !fld->resourceview )
00123              fld->resourceview = new MapField::Resourceview;
00124 
00125           for ( int c = 0; c < 8; c++ )
00126              if ( shareview & (1 << c) ) {
00127 
00128                 if ( !(fld->resourceview->visible & (1 << c)) || fld->resourceview->materialvisible[c] != fld->material || fld->resourceview->fuelvisible[c] != fld->fuel ) {
00129                    ResourceViewState rvs( fld, c);
00130                    rvs.pos = mc;
00131                    oldState.push_back( rvs );
00132 
00133 
00134                    ResourceViewState newView;
00135                    newView.fuel = fld->fuel;
00136                    newView.material = fld->material;
00137                    newView.visible = true;
00138                    newView.player = c;
00139                    newView.pos = mc;
00140 
00141                    newState.push_back( newView );
00142                    newView.writeToField ( fld );
00143                 }
00144              }
00145        }
00146 }
00147 
00148 
00149 ActionResult DiscoverResources::runAction( const Context& context )
00150 {
00151    if ( !getUnit()->typ->hasFunction( ContainerBaseType::DetectsMineralResources  ) )
00152       return ActionResult(311);
00153 
00154    circularFieldIterator( getMap(), getUnit()->getPosition(), 0, getUnit()->typ->digrange, FieldIterationFunctor(this, &DiscoverResources::testField ));
00155 
00156    return ActionResult(0);
00157 }
00158 
00159 
00160 ActionResult DiscoverResources::undoAction( const Context& context )
00161 {
00162    for ( ViewState::iterator i = oldState.begin(); i != oldState.end(); ++i  ) {
00163       MapField* fld = getMap()->getField( i->pos );
00164       /*
00165       if ( fld->visible != newState[i->first] ) {
00166          ASCString msg;
00167          msg.format( "; expected: %x ; found %x" , newState[i->first], fld->visible );
00168          throw ActionResult(21207, "Position is " + i->first.toString(true) + msg );
00169       }
00170       */
00171       i->writeToField( fld );
00172    }
00173    return ActionResult(0);
00174 }
00175 
00176 namespace {
00177    const bool r1 = registerAction<DiscoverResources> ( ActionRegistry::DiscoverResources );
00178 }
00179 

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