Advanced Strategic Command
researchexecution.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 <stdio.h>
22 #include <cstring>
23 #include <math.h>
24 #include <stdarg.h>
25 #include <ctime>
26 
27 #include "buildingtype.h"
28 #include "vehicletype.h"
29 #include "typen.h"
30 
31 #include "newfont.h"
32 #include "spfst.h"
33 #include "loaders.h"
34 #include "misc.h"
35 #include "controls.h"
36 #include "stack.h"
37 #include "dlg_box.h"
38 #include "dialog.h"
39 #include "attack.h"
40 #include "gamedlg.h"
41 #include "gameoptions.h"
42 #include "ai/ai.h"
43 #include "errors.h"
44 #include "viewcalculation.h"
45 #include "replay.h"
46 #include "resourcenet.h"
47 #include "itemrepository.h"
48 #include "strtmesg.h"
49 #include "messagedlg.h"
50 #include "gameevent_dialogs.h"
51 #include "cannedmessages.h"
52 #include "mapdisplay.h"
53 #include "player.h"
54 
55 #include "dialogs/choosetech.h"
56 
58 
60 {
61  presenter = techPresenter;
62  techPresenter = NULL;
63 }
64 
66 {
67  techPresenter = presenter;
68 }
69 
71 {
72  techPresenter = presenter;
73 }
74 
75 
76 static bool anyTechAvailable( const Player& player )
77 {
78  for (int i = 0; i < technologyRepository.getNum(); i++) {
79  const Technology* tech = technologyRepository.getObject_byPos( i );
80  if ( tech ) {
82  if ( a == Available )
83  return true;
84  }
85  }
86  return false;
87 }
88 
89 
92  std::list<const VehicleType*> units;
93  std::list<const BuildingType*> buildings;
94  Player& p;
95  public:
96  NewGadgetDetection( Player& player ) : p(player )
97  {
98  for ( int i=0; i < vehicleTypeRepository.getNum() ; i++ ) {
99  const VehicleType* v = vehicleTypeRepository.getObject_byPos ( i );
100  if ( !v->techDependency.available( p.research ))
101  units.push_back( v );
102  }
103 
104  for ( int i=0; i < buildingTypeRepository.getNum() ; i++ ) {
105  const BuildingType* b = buildingTypeRepository.getObject_byPos ( i );
106  if ( !b->techDependency.available( p.research ))
107  buildings.push_back( b );
108  }
109 
110  }
111 
114  {
115  for ( int i=0; i < vehicleTypeRepository.getNum() ; i++ ) {
116  const VehicleType* v = vehicleTypeRepository.getObject_byPos ( i );
117  if ( !v->techDependency.available( p.research ))
118  units.remove( v );
119  }
120 
121  for ( int i=0; i < buildingTypeRepository.getNum() ; i++ ) {
122  const BuildingType* b = buildingTypeRepository.getObject_byPos ( i );
123  if ( !b->techDependency.available( p.research ))
124  buildings.remove( b );
125  }
126 
127  storage.buildings = buildings;
128  storage.units = units;
129  }
130 };
131 
132 
133 
134 
136 {
137  if ( !player.research.goal )
138  return NULL;
139 
140  if ( player.research.techResearched( player.research.goal->id )) {
141  player.research.goal = NULL;
142  return NULL;
143  }
144 
145  list<const Technology*> techs;
146  if ( player.research.goal->eventually_available( player.research, &techs ))
147  return *techs.begin();
148  else
149  return NULL;
150 }
151 
152 
153 void runResearch( Player& player, vector<const Technology*>* newTechs, vector<ASCString>* newTechAdapter )
154 {
155  Research& research = player.research;
156 
157  if ( research.activetechnology && research.techResearched( research.activetechnology->id ) ) {
158  /* the current research may suddenly be researched already if the map was modified by the ditor */
159  research.progress = max( 0, research.progress - research.activetechnology->researchpoints ) ;
160  research.activetechnology = NULL;
161  if ( !anyTechAvailable( player ) )
162  return;
163  }
164 
165  while ( research.activetechnology && (research.progress >= research.activetechnology->researchpoints)) {
166 
167  const Technology* newTech = research.activetechnology;
168  research.activetechnology = NULL;
169 
170  if ( newTechs )
171  newTechs->push_back( newTech );
172 
173  NewGadgetDetection ngd( player );
174 
175  vector<ASCString> techAdapters = research.addanytechnology( newTech );
176  if( newTechAdapter )
177  newTechAdapter->insert( newTechAdapter->end(), techAdapters.begin(), techAdapters.end() );
178 
179  if ( techPresenter ) {
180  TechnologyPresenter::Gadgets newItemsAvailable;
181  ngd.evaluate( newItemsAvailable );
182  techPresenter->showTechnology( newTech, newItemsAvailable );
183  }
184 
185  research.progress -= newTech->researchpoints;
186 
187  research.activetechnology = getNextTechnologyTowardsGoal( player );
188  }
189 
190 }
191 
192 
194 {
195  Research& research = player.research;
196 
197  while ( research.activetechnology == NULL && research.progress && anyTechAvailable( player ) )
198  if ( !chooseSingleTechnology( player ))
199  return;
200 }
201 
202 
203 
static bool anyTechAvailable(const Player &player)
checks, which vehicle and building types are newly available.
The (base-) classes which are thrown as exceptions.
void checkForNewResearch(Player &player)
Let's the user select new technology for research if the current research projects are finished...
void evaluate(TechnologyPresenter::Gadgets &storage)
checks, which vehicle are now available that where not available when constructor was called...
map accessing and usage routines used by ASC and the mapeditor
miscellaneous functions which are used by ASC and all its tools
NewGadgetDetection(Player &player)
static TechnologyPresenter * techPresenter
TechAdapterDependency techDependency
bool eventually_available(const Research &res, list< const Technology * > *dependencies) const
Definition: research.cpp:574
vector< ASCString > addanytechnology(const Technology *tech)
adds the technology to the list of available technologies
Definition: research.cpp:849
ItemRepositoryLoader< BuildingType > buildingTypeRepository("buildingtype")
Interface for some basic classes from which all of ASC's dialogs are derived.
ItemRepositoryLoader< Technology > technologyRepository("technology")
std::list< const BuildingType * > buildings
ResearchAvailabilityStatus
Definition: research.h:45
Handling the connection of buildings by pipelines, powerlines etc.
bool chooseSingleTechnology(Player &player)
lets the player choose an available technology
Definition: choosetech.cpp:341
virtual void showTechnology(const Technology *tech, const Gadgets &newGadgetsAvailable)=0
The class describing properties that are common to all vehicles of a certain kind.
Definition: vehicletype.h:177
std::list< const VehicleType * > units
The class describing properties that are common to all buildings of a certain kind.
Definition: buildingtype.h:35
ItemRepositoryLoader< VehicleType > vehicleTypeRepository("vehicletype")
Interface for recording and playing replays.
Interface to game event dialogs.
const Technology * activetechnology
the technology that is currently being researched
Definition: research.h:191
const Technology * goal
the technology that is defined as long-term goal
Definition: research.h:194
Interface for all the fighting routines of ASC.
The interface for the buildingtype class.
ResearchAvailabilityStatus techAvailable(const Technology *tech) const
Definition: research.cpp:834
void runResearch(Player &player, vector< const Technology * > *newTechs, vector< ASCString > *newTechAdapter)
progresses in the current research, automatically chosing technologies towards the research goal...
void setResearchPresenter(TechnologyPresenter *presenter)
registers the class that will present newly researched technologies to the user
Interface for all the dialog boxes used by the game and the mapeditor.
Research research
the status of the scientific research
Definition: player.h:142
The interface for the artificial intelligence of ASC.
procedure for loading and writing savegames, maps etc.
bool techResearched(int id) const
Definition: research.cpp:757
int researchpoints
Definition: research.h:135
const T & max(const T &a, const T &b, const T &c)
Definition: misc.h:97
the different players in ASC. There may be 8 players (0..7) and neutral units (8) ...
Definition: player.h:99
bool available(const Research &research) const
Definition: research.cpp:412
int progress
Definition: research.h:189
Interface for a small general-purpose stack (not type safe)
const Technology * getNextTechnologyTowardsGoal(Player &player)