Advanced Strategic Command
statistics.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 #include <cmath>
22 
23 #include "util/messaginghub.h"
24 #include "statistics.h"
25 #include "vehicle.h"
26 #include "viewcalculation.h"
27 #include "attack.h"
28 #include "spfst.h"
29 #include "widgets/textrenderer.h"
30 
31 
32 double StatisticsCalculator::strength( const ContainerBase* c, bool recurse )
33 {
34  double s = 0;
35  const Vehicle* veh = dynamic_cast<const Vehicle*>(c);
36  if ( veh ) {
38  AttackFormula af( c->getMap() );
39  s *= (af.strength_experience( veh->getExperience_offensive() ) + af.defense_experience( veh->getExperience_defensive())) + 1.0 ;
40  s *= af.strength_damage( veh->damage );
41  s/=10000;
42  }
43 
44  if ( recurse )
45  for ( ContainerBase::Cargo::const_iterator i = c->getCargo().begin(); i != c->getCargo().end(); ++i )
46  if ( *i )
47  s += strength( *i, recurse );
48 
49  return s;
50 }
51 
52 int StatisticsCalculator::unitCount( const ContainerBase* c, bool recurse )
53 {
54  int counter = 1;
55  if ( recurse )
56  for ( ContainerBase::Cargo::const_iterator i = c->getCargo().begin(); i != c->getCargo().end(); ++i )
57  if ( *i )
58  counter += unitCount( *i, recurse );
59  return counter;
60 }
61 
62 
64 {
66  for ( int r = 0; r < 3; ++r )
67  res.resource(r) += c->getAvailableResource( maxint, r, 0 );
68 
69  if ( recurse )
70  for ( ContainerBase::Cargo::const_iterator i = c->getCargo().begin(); i != c->getCargo().end(); ++i )
71  if ( *i )
72  res += resource(*i, recurse);
73 
74  return res;
75 }
76 
77 int StatisticsCalculator::unitCost( const ContainerBase* c, bool recurse )
78 {
80 
81  if ( recurse )
82  for ( ContainerBase::Cargo::const_iterator i = c->getCargo().begin(); i != c->getCargo().end(); ++i )
83  if ( *i )
84  res.material += unitCost(*i, recurse);
85 
86  return res.material;
87 }
88 
89 
91 {
92  ASCString msg;
93 
94  computeview ( actmap, 0, true );
95 
96  for ( int i = 0; i < actmap->getPlayerCount(); i++ ) {
97  if ( actmap->player[i].exist() ) {
98  msg += ASCString("#fontsize=14#Player ") + ASCString::toString( i ) + ": "+ actmap->player[i].getName() + "#fontsize=12#\n" ;
99  int notVisible = 0;
100  int fogOfWar = 0;
101  int visible = 0;
102  int viewDominance = 0;
103  for ( int x = 0; x < actmap->xsize; x++ )
104  for ( int y = 0; y < actmap->ysize; y++ ) {
105  MapField* fld = actmap->getField ( x, y );
106  VisibilityStates vs = fieldVisibility ( fld, i );
107  switch ( vs ) {
108  case visible_not:
109  ++notVisible;
110  break;
111  case visible_ago:
112  ++fogOfWar;
113  break;
114  default:
115  ++visible;
116  }
117 
118  if ( vs == visible_all || vs == visible_now ) {
119  int maxView = -1;
120  for ( int p = 0; p < actmap->getPlayerCount(); ++p )
121  if ( actmap->getPlayer(p).stat != Player::supervisor )
122  if ( fld->view[p].view > maxView )
123  maxView = fld->view[p].view;
124 
125  if ( fld->view[i].view == maxView )
126  ++viewDominance;
127  }
128 
129  }
130  msg += ASCString(" not visible: ") + ASCString::toString(notVisible ) + " fields\n";
131  msg += ASCString(" fog of war: ") + ASCString::toString(fogOfWar ) + " fields\n";
132  msg += ASCString(" visible: ") + ASCString::toString(visible ) + " fields\n";
133  msg += ASCString(" view dominant: ") + ASCString::toString(viewDominance ) + " fields\n\n";
134  }
135  }
136 
137  computeview ( actmap, 0 , false );
138 
139  return msg;
140 }
141 
143 {
144  ASCString message;
145  for ( int i = 0; i< gamemap->getPlayerCount(); ++i ) {
146  double strength = 0;
147  Resources r;
148  Resources total;
149  for ( Player::VehicleList::iterator j = gamemap->player[i].vehicleList.begin(); j != gamemap->player[i].vehicleList.end(); ++j ) {
150  strength += StatisticsCalculator::strength( *j, false );
151  r += (*j)->typ->productionCost;
152  total += StatisticsCalculator::resource( *j, false );
153  }
154 
155  for ( Player::BuildingList::iterator j = gamemap->player[i].buildingList.begin(); j != gamemap->player[i].buildingList.end(); ++j ) {
156  total += StatisticsCalculator::resource( *j, false );
157  }
158 
159  message += ASCString("#fontsize=14#Player ") + ASCString::toString( i ) + ": "+ gamemap->player[i].getName() + "#fontsize=12#\n" ;
160  message += "strength: ";
161  ASCString s;
162  s.format("%9.0f", ceil(strength) );
163  message += s + "\n";
164  message += "Unit production cost ";
165  for ( int k = 1; k < 2; ++k ) { // just material
166  message += resourceNames[k];
167  message += ": " + ASCString::toString(r.resource(k)/1000 ) + "k\n";
168  }
169  message += "Unit count: " + ASCString::toString( int( gamemap->player[i].vehicleList.size())) + "\n";
170  message += "Material index: " + ASCString::toString( total.material/1000 ) + "k\n";
171  message += "\n\n";
172 
173  }
174  return message;
175 }
176 
177 
178 
179 void pbpplayerstatistics( GameMap* gamemap )
180 {
181  ASCString msg;
182  {
183  StatusMessageWindowHolder smw = MessagingHub::Instance().infoMessageWindow( "calculating... " );
184 
185  msg = "#fontsize=18#Map Statistics for " + gamemap->maptitle + "#fontsize=12#\n\n";
186 
187  msg += "#fontsize=16#Visibility#fontsize=12#\n";
188  msg += getVisibilityStatistics( gamemap );
189 
190  msg += "#fontsize=16#Strength#fontsize=12#\n";
191  msg += getPlayerStrength( gamemap );
192  }
193 
194  ViewFormattedText vft ( "Map Statistics", msg, PG_Rect(-1,-1,600,600));
195  vft.Show();
196  vft.RunModal();
197 }
198 
Player & getPlayer(PlayerID p)
Definition: gamemap.h:257
int xsize
the size of the map
Definition: gamemap.h:201
int getExperience_defensive() const
Definition: vehicle.cpp:258
int computeview(GameMap *gamemap, int player_fieldcount_mask, bool disableShareView, const Context *context)
completely computes the view
map accessing and usage routines used by ASC and the mapeditor
int damage
Damage. 0 is no damage, when damage reaches 100 the container is destroyed.
the core formula, which weighs the different factory that go into the calculation ...
Definition: attack.h:42
int energy
Definition: typen.h:99
ASCString getName() const
returns the name of the player
Definition: player.cpp:274
struct MapField::View view[8]
VisibilityStates
the different states that a player's view on a field can have
Definition: typen.h:403
static int unitCount(const ContainerBase *c, bool recurse)
Definition: statistics.cpp:52
float strength_damage(int damage)
Definition: attack.cpp:116
a single field of the map
Definition: mapfield.h:26
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
A class that hols a MessageWindow. This class ensures that the window is removed if the last copy of ...
Definition: messaginghub.h:34
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
virtual int getAvailableResource(int amount, int resourcetype, int scope=1) const =0
static double strength(const ContainerBase *c, bool recurse)
Definition: statistics.cpp:32
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
VehicleList vehicleList
a list of all units
Definition: player.h:135
enum Player::PlayerStatus stat
Interface for all the fighting routines of ASC.
ASCString getPlayerStrength(GameMap *gamemap)
Definition: statistics.cpp:142
int material
Definition: typen.h:100
Player player[9]
Definition: gamemap.h:253
VisibilityStates fieldVisibility(const MapField *pe)
}@
Definition: spfst.cpp:440
int ysize
Definition: gamemap.h:201
ASCString getVisibilityStatistics(GameMap *actmap)
Definition: statistics.cpp:90
BuildingList buildingList
a list of all units
Definition: player.h:139
static int unitCost(const ContainerBase *c, bool recurse)
Definition: statistics.cpp:77
int & resource(int type)
Definition: typen.h:105
ASCString maptitle
the title of the map
Definition: gamemap.h:213
static Resources resource(const ContainerBase *c, bool recurse)
Definition: statistics.cpp:63
const VehicleType * typ
Definition: vehicle.h:83
const ContainerBaseType * baseType
the type descriping all non-instance specific properties of the container
Definition: containerbase.h:80
#define maxint
Definition: typen.h:462
const char * resourceNames[3]
the names of the different resources. Redundent, as we already have Resources::name ...
Definition: typen.cpp:79
GameMap * actmap
Definition: spfst.cpp:64
GameMap * getMap() const
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
Resources are basically the currency of ASC.
Definition: typen.h:97
const Cargo & getCargo() const
int getPlayerCount() const
Definition: gamemap.h:255
void pbpplayerstatistics(GameMap *gamemap)
Definition: statistics.cpp:179
bool exist() const
does the player exist at all
Definition: player.cpp:313
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
int getExperience_offensive() const
Definition: vehicle.cpp:253