Advanced Strategic Command
weaponrangelayer.cpp
Go to the documentation of this file.
1 
2 /***************************************************************************
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2 of the License, or *
7  * (at your option) any later version. *
8  * *
9  ***************************************************************************/
10 
11 
12 #include "weaponrangelayer.h"
13 #include "gamemap.h"
14 #include "spfst.h"
15 #include "iconrepository.h"
16 
17 void UnitWeaponRangeLayer::markField( const MapCoordinate& pos )
18 {
19  fields[pos] |= 1;
20 }
21 
22 bool UnitWeaponRangeLayer::fieldVisible( const MapCoordinate& pos )
23 {
24 #ifdef karteneditor
25  return true;
26 #else
27  return fieldvisiblenow ( gamemap->getField( pos ));
28 #endif
29 }
30 
31 
32 bool UnitWeaponRangeLayer::addUnit( Vehicle* veh )
33 {
34  if ( fieldVisible( MapCoordinate(veh->xpos, veh->ypos ))) {
35  int found = 0;
36  for ( int i = 0; i < veh->typ->weapons.count; i++ ) {
37  if ( veh->typ->weapons.weapon[i].shootable() ) {
38  circularFieldIterator( gamemap,veh->getPosition(), veh->typ->weapons.weapon[i].maxdistance/minmalq, (veh->typ->weapons.weapon[i].mindistance+maxmalq-1)/maxmalq, FieldIterationFunctor( this, &UnitWeaponRangeLayer::markField ) );
39  found++;
40  }
41  }
42  if ( found )
43  fields[veh->getPosition()] |= 2;
44 
45  return found;
46  } else
47  return false;
48 };
49 
50 void UnitWeaponRangeLayer::reset()
51 {
52  fields.clear();
53 }
54 
56  {
57  if ( !pos.valid() )
58  return;
59 
60  if ( gamemap && gamemap != actmap )
61  reset();
62 
63  gamemap = actmap;
64 
65  if ( fields.find( pos ) != fields.end() ) {
66  if ( fields[pos] & 2 ) {
67  reset();
68  setActive(false);
69  statusMessage("Weapon range layer disabled");
70  repaintMap();
71  return;
72  }
73  }
74 
75  if ( actmap->getField( pos )->vehicle ) {
76  if ( addUnit( actmap->getField( pos )->vehicle ) ) {
77  setActive(true);
78  statusMessage("Weapon range layer enabled");
79  repaintMap();
80  }
81  }
82  }
83 
84  UnitWeaponRangeLayer::UnitWeaponRangeLayer() : icon1 ( IconRepository::getIcon( "markedfield-red.png")), icon2 ( IconRepository::getIcon( "markedfield-red2.png")), gamemap(NULL) {
85  // cursorMoved.connect( sigc::mem_fun( *this, UnitWeaponRangeLayer::cursorMoved ));
86  }
87 
88  void UnitWeaponRangeLayer::paintSingleField( const MapRenderer::FieldRenderInfo& fieldInfo, int layer, const SPoint& pos )
89  {
90  if ( fieldInfo.gamemap != gamemap && gamemap) {
91  reset();
92  gamemap = NULL;
93  return;
94  }
95 
96  if ( fieldInfo.visibility >= visible_ago) {
97  if ( fields.find( fieldInfo.pos ) != fields.end() ) {
98  int p = fields[fieldInfo.pos];
99  if ( p & 1 )
100  fieldInfo.surface.Blit( icon1, pos );
101  if ( p & 2 )
102  fieldInfo.surface.Blit( icon2, pos );
103  }
104  }
105  }
106 
void statusMessage(const ASCString &string)
virtual void setActive(bool active)
Definition: mapdisplay.h:117
map accessing and usage routines used by ASC and the mapeditor
Vehicle * vehicle
Definition: mapfield.h:89
bool valid() const
Definition: typen.h:221
UnitWeapon weapons
The weapons.
Definition: vehicletype.h:248
bool shootable(void) const
MapCoordinate3D getPosition() const
returns the units position
Definition: vehicle.cpp:1552
sigc::signal< void > repaintMap
}@
Definition: spfst.cpp:45
SingleWeapon weapon[16]
Definition: vehicletype.h:170
bool fieldvisiblenow(const MapField *pe, Vehicle *veh, int player)
{@
Definition: spfst.cpp:399
#define maxmalq
Constants that specify the layout of ASC.
Definition: typen.h:429
int mindistance
the minimal distance the weapon can shoot
Definition: vehicletype.h:114
Coordinate on the twodimensional map.
Definition: typen.h:202
Definition of THE central asc class: GameMap.
void circularFieldIterator(GameMap *gamemap, const MapCoordinate &center, int startDist, int stopDist, FieldIterationFunctor functor)
int maxdistance
the maximum distance the weapon can shoot
Definition: vehicletype.h:111
VisibilityStates visibility
Definition: mapdisplay.h:55
SDLmm::SPoint SPoint
Definition: surface.h:27
void operateField(GameMap *actmap, const MapCoordinate &pos)
int xpos
the position on the map
Definition: vehicle.h:124
const VehicleType * typ
Definition: vehicle.h:83
GameMap * actmap
Definition: spfst.cpp:64
int ypos
Definition: vehicle.h:124
void paintSingleField(const MapRenderer::FieldRenderInfo &fieldInfo, int layer, const SPoint &pos)
Loki::Functor< void, LOKI_TYPELIST_1(const MapCoordinate &) > FieldIterationFunctor
Definition: mapalgorithms.h:43
#define minmalq
Definition: typen.h:430
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