weaponrangelayer.cpp

Go to the documentation of this file.
00001 
00002 /***************************************************************************
00003  *                                                                         *
00004  *   This program is free software; you can redistribute it and/or modify  *
00005  *   it under the terms of the GNU General Public License as published by  *
00006  *   the Free Software Foundation; either version 2 of the License, or     *
00007  *   (at your option) any later version.                                   *
00008  *                                                                         *
00009  ***************************************************************************/
00010 
00011 
00012 #include "weaponrangelayer.h"
00013 #include "gamemap.h"
00014 #include "spfst.h"
00015 #include "iconrepository.h"
00016 
00017 void UnitWeaponRangeLayer::markField( const MapCoordinate& pos )
00018 {
00019    fields[pos] |= 1;
00020 }
00021 
00022 bool UnitWeaponRangeLayer::fieldVisible( const MapCoordinate& pos )
00023 {
00024 #ifdef karteneditor
00025    return true;
00026 #else
00027    return fieldvisiblenow ( gamemap->getField( pos ));
00028 #endif
00029 }
00030 
00031 
00032 bool UnitWeaponRangeLayer::addUnit( Vehicle* veh )
00033 {
00034    if ( fieldVisible( MapCoordinate(veh->xpos, veh->ypos ))) {
00035       int found = 0;
00036       for ( int i = 0; i < veh->typ->weapons.count; i++ ) {
00037          if ( veh->typ->weapons.weapon[i].shootable() ) {
00038             circularFieldIterator( gamemap,veh->getPosition(), veh->typ->weapons.weapon[i].maxdistance/minmalq, (veh->typ->weapons.weapon[i].mindistance+maxmalq-1)/maxmalq, FieldIterationFunctor( this, &UnitWeaponRangeLayer::markField )  );
00039             found++;
00040          }
00041       }
00042       if ( found )
00043          fields[veh->getPosition()] |= 2;
00044          
00045       return found;
00046    } else
00047       return false;
00048 };
00049 
00050 void UnitWeaponRangeLayer::reset()
00051 {
00052    fields.clear();
00053 }
00054 
00055 void UnitWeaponRangeLayer::operateField( GameMap* actmap, const MapCoordinate& pos )
00056    {
00057       if ( !pos.valid() )
00058          return;
00059       
00060       if ( gamemap && gamemap != actmap ) 
00061          reset();
00062 
00063       gamemap = actmap;
00064       
00065       if ( fields.find( pos ) != fields.end() ) {
00066          if ( fields[pos] & 2 ) {
00067             reset();
00068             setActive(false);
00069             statusMessage("Weapon range layer disabled");
00070             repaintMap();
00071             return;
00072          }
00073       }
00074       
00075       if ( actmap->getField( pos )->vehicle ) {
00076          if ( addUnit( actmap->getField( pos )->vehicle ) ) {
00077             setActive(true);
00078             statusMessage("Weapon range layer enabled");
00079             repaintMap();
00080          }
00081       }
00082    }
00083    
00084    UnitWeaponRangeLayer::UnitWeaponRangeLayer() : icon1 ( IconRepository::getIcon( "markedfield-red.png")), icon2 ( IconRepository::getIcon( "markedfield-red2.png")), gamemap(NULL) {
00085       // cursorMoved.connect( SigC::slot( *this, UnitWeaponRangeLayer::cursorMoved ));
00086    }
00087 
00088    void UnitWeaponRangeLayer::paintSingleField( const MapRenderer::FieldRenderInfo& fieldInfo,  int layer, const SPoint& pos )
00089    {
00090       if ( fieldInfo.gamemap != gamemap && gamemap) {
00091          reset();
00092          gamemap = NULL;
00093          return;
00094       }
00095       
00096       if ( fieldInfo.visibility >= visible_ago) {
00097          if ( fields.find( fieldInfo.pos ) != fields.end() ) {
00098             int p = fields[fieldInfo.pos];
00099             if ( p & 1 )
00100                fieldInfo.surface.Blit( icon1, pos );
00101             if ( p & 2 )
00102                fieldInfo.surface.Blit( icon2, pos );
00103          }
00104       }
00105    }
00106 

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