Advanced Strategic Command
vehicleproductionselection.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * This program is free software; you can redistribute it and/or modify *
4  * it under the terms of the GNU General Public License as published by *
5  * the Free Software Foundation; either version 2 of the License, or *
6  * (at your option) any later version. *
7  * *
8  ***************************************************************************/
9 
10 #include <pgtooltiphelp.h>
11 
12 #include "../gamemap.h"
13 #include "../containercontrols.h"
14 #include "../cannedmessages.h"
15 #include "../gameoptions.h"
16 #include "../dialog.h"
17 #include "../sg.h"
18 
20 
21 #include "../actions/buildproductionlinecommand.h"
22 #include "../actions/removeproductionlinecommand.h"
23 
25  : VehicleTypeSelectionItemFactory( plantResources, convertAndCreateArrays( produceableUnits, &items ), productionplant->getMap()->getCurrentPlayer() ),
26  fillResources(true),
27  fillAmmo(true),
28  plant(productionplant),
29  produceables( produceableUnits)
30 {
31 }
32 
33 
34 const VehicleProduction_SelectionItemFactory::Container& VehicleProduction_SelectionItemFactory::convertAndCreateArrays( const ConstructUnitCommand::Producables& from, Container** items )
35 {
36  *items = new Container();
37  return convertArrays( from, **items );
38 }
39 
40 const VehicleProduction_SelectionItemFactory::Container& VehicleProduction_SelectionItemFactory::convertArrays( const ConstructUnitCommand::Producables& from, Container& items )
41 {
42  items.clear();
43  for( ConstructUnitCommand::Producables::const_iterator i = from.begin(); i != from.end(); ++i )
44  if ( !(i->prerequisites.getValue() & ( ConstructUnitCommand::Lack::Research | ConstructUnitCommand::Lack::Unloadability )))
45  items.push_back ( i->type );
46  return items;
47 }
48 
49 
51 {
52  sigVehicleTypeSelected( type, mouse );
53 }
54 
56 {
57  if ( !widget )
58  return;
59 
60  const VehicleTypeBaseWidget* fw = dynamic_cast<const VehicleTypeBaseWidget*>(widget);
61  assert( fw );
63 }
64 
65 
67 {
69  return fillAmmo;
70  else
71  return false;
72 }
73 
75 {
76  fillAmmo = value;
78  return true;
79 }
80 
82 {
83  return fillResources;
84 }
85 
87 {
88  fillResources = value;
90  return true;
91 }
92 
94 {
95  convertArrays( produceables, *items );
96 }
97 
98 
100 {
101  for ( ConstructUnitCommand::Producables::const_iterator i = produceables.begin(); i != produceables.end(); ++i ) {
102  if ( i->type == type ) {
103  Resources cost = i->cost;
104  if ( fillResources )
105  cost += Resources( 0, type->getStorageCapacity(plant->getMap()->_resourcemode).material, type->getStorageCapacity(plant->getMap()->_resourcemode).fuel );
106 
107  if ( fillAmmo )
108  for ( int w = 0; w < type->weapons.count; ++w )
109  if ( type->weapons.weapon[w].requiresAmmo() ) {
110  int wt = type->weapons.weapon[w].getScalarWeaponType();
111  cost += Resources( ammoProductionCost[wt][0], ammoProductionCost[wt][1], ammoProductionCost[wt][2] ) * type->weapons.weapon[w].count;
112  }
113  return cost;
114  }
115  }
116  return Resources(0,0,0);
117 };
118 
119 
120 AddProductionLine_SelectionItemFactory::AddProductionLine_SelectionItemFactory( ContainerBase* my_plant, const Container& types ) : VehicleTypeSelectionItemFactory( my_plant->getResource(Resources(maxint,maxint,maxint), true), types, my_plant->getMap()->getCurrentPlayer() ), plant(my_plant)
121 {
122 
123 };
124 
126 {
127  auto_ptr<BuildProductionLineCommand> bplc ( new BuildProductionLineCommand(plant ));
128  bplc->setProduction( type );
129  ActionResult res = bplc->execute ( createContext( plant->getMap() ));
130  if ( res.successful() )
131  bplc.release();
132  else
133  displayActionError( res );
134 }
135 
137 {
139 };
140 
141 
143 {
144  vtSelected( vt, true );
145 }
146 
148 {
149  selected = vt;
150 
151  if ( !mouse ) // enter pressed
152  produce();
153 };
154 
156 {
157  if ( selected ) {
158  finallySelected = selected;
159  quitModalLoop(0);
160  return true;
161  } else
162  return false;
163 }
164 
165 
167 {
168  selected = NULL;
170 }
171 
173 {
174  selected = NULL;
175  QuitModal();
176  return true;
177 };
178 
180 {
183  factory->updateProducables();
184  isw->reLoad( true );
185 }
186 
187 bool VehicleProduction_SelectionWindow::eventKeyDown(const SDL_KeyboardEvent* key)
188 {
189  int mod = SDL_GetModState() & ~(KMOD_NUM | KMOD_CAPS | KMOD_MODE);
190  if ( mod )
191  return false;
192 
193  switch ( key->keysym.sym ) {
194  case SDLK_ESCAPE:
195  closeWindow();
196  return true;
197 
198  case SDLK_PLUS:
200  QuitModal();
201  return true;
202 
203  case SDLK_MINUS:
205  QuitModal();
206  return true;
207  default:;
208  }
209 
210  return false;
211 }
212 
213 
214 VehicleProduction_SelectionWindow::VehicleProduction_SelectionWindow( PG_Widget *parent, const PG_Rect &r, ContainerBase* plant, const ConstructUnitCommand::Producables& produceableUnits, bool internally )
215  : ASC_PG_Dialog( parent, r, "Choose Vehicle Type" ), selected(NULL), finallySelected(NULL), isw(NULL), factory(NULL), my_plant( plant ), produceables( produceableUnits )
216 {
217  factory = new VehicleProduction_SelectionItemFactory( plant->getResource(Resources(maxint,maxint,maxint), true), plant, produceableUnits );
218  if ( internally ) {
219  factory->setAmmoFilling( CGameOptions::Instance()->unitProduction.fillAmmo );
220  factory->setResourceFilling ( CGameOptions::Instance()->unitProduction.fillResources );
221  } else {
222  factory->setAmmoFilling( false );
223  factory->setResourceFilling ( false );
224  }
225 
226  factory->sigVehicleTypeSelected.connect ( sigc::mem_fun( *this, &VehicleProduction_SelectionWindow::vtSelected ));
227  factory->sigVehicleTypeMarked.connect ( sigc::mem_fun( *this, &VehicleProduction_SelectionWindow::vtMarked ));
228 
229  isw = new ItemSelectorWidget( this, PG_Rect(10, GetTitlebarHeight(), r.Width() - 20, r.Height() - GetTitlebarHeight() - 40), factory );
230  isw->sigQuitModal.connect( sigc::mem_fun( *this, &VehicleProduction_SelectionWindow::quitSignalled));
231 
232  int y = GetTitlebarHeight() + isw->Height();
233  if ( internally ) {
234  PG_CheckButton* fillRes = new PG_CheckButton( this, PG_Rect( 10, y + 2, r.Width() / 2 - 50, 20), "Fill with Resources" );
235  if ( factory->getResourceFilling() )
236  fillRes->SetPressed();
237  fillRes->sigClick.connect( sigc::mem_fun( *factory, &VehicleProduction_SelectionItemFactory::setResourceFilling ));
239  PG_CheckButton* fillAmmo = new PG_CheckButton( this, PG_Rect( 10, y + 20, r.Width() / 2 - 50, 20), "Fill with Ammo" );
240  if ( factory->getAmmoFilling() )
241  fillAmmo->SetPressed();
242  fillAmmo->sigClick.connect( sigc::mem_fun( *factory, &VehicleProduction_SelectionItemFactory::setAmmoFilling ));
243  } else
244  factory->setAmmoFilling( false );
245 
246  } else {
247  factory->setAmmoFilling( false );
248  factory->setResourceFilling ( false );
249  }
250 
251  factory->reloadAllItems.connect( sigc::mem_fun( *this, &VehicleProduction_SelectionWindow::reLoadAndUpdate ));
252 
253  PG_Rect rr ( r.Width() / 2 + 10, y + 2, (r.Width() - 20) - (r.Width() / 2 + 10) , 35);
254  PG_Button* b = new PG_Button( this, PG_Rect( rr.x + rr.h + 5, rr.y, rr.w - 40, rr.h ) , "Produce" );
255  b->sigClick.connect( sigc::hide( sigc::mem_fun( *this,&VehicleProduction_SelectionWindow::produce )));
256 
257  if ( !plant->baseType->hasFunction(ContainerBaseType::NoProductionCustomization) && internally ) {
258  PG_Button* b2 = new PG_Button( this, PG_Rect( rr.x, rr.y, rr.h, rr.h ), "+" );
259  b2->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &VehicleProduction_SelectionWindow::addProductionLine )));
260  new PG_ToolTipHelp( b2, "Add production line");
261 
262  PG_Button* b3 = new PG_Button( this, PG_Rect( rr.x - rr.h - 5, rr.y, rr.h, rr.h ), "-" );
263  b3->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &VehicleProduction_SelectionWindow::removeProductionLine )));
264  new PG_ToolTipHelp( b3, "Remove production line");
265  }
266 };
267 
268 
270 {
271  BuildProductionLineCommand bplc( my_plant );
272  {
273  ItemSelectorWindow isw( NULL, PG_Rect( 100, 150, 400, 400 ), "choose production line", new AddProductionLine_SelectionItemFactory( my_plant, bplc.productionLinesBuyable() ));
274  isw.Show();
275  isw.RunModal();
276  }
277  reLoadAndUpdate();
278  return true;
279 }
280 
282 {
283  if ( selected && choice_dlg("do you really want to remove this production line ?","~y~es","~n~o") == 1) {
284  auto_ptr<RemoveProductionLineCommand> rplc ( new RemoveProductionLineCommand( my_plant ));
285  rplc->setRemoval( selected );
286  ActionResult res = rplc->execute( createContext( my_plant->getMap() ));
287  if ( res.successful())
288  rplc.release();
289  else
290  displayActionError(res);
291  reLoadAndUpdate();
292  }
293  return true;
294 }
295 
296 
298 {
299  return factory->getAmmoFilling();
300 }
301 
303 {
304  return factory->getResourceFilling();
305 }
306 
void reLoad(bool show=false)
bool eventKeyDown(const SDL_KeyboardEvent *key)
static Resources resourcesNeeded(const ContainerBaseType *factory, const VehicleType *veh)
UnitWeapon weapons
The weapons.
Definition: vehicletype.h:248
Context createContext(GameMap *gamemap)
bool hasFunction(ContainerFunctions function) const
bool fillResources
Definition: gameoptions.h:186
sigc::signal< bool > sigQuitModal
int count
amount of ammunition the unit having this weapon can carry
Definition: vehicletype.h:117
SingleWeapon weapon[16]
Definition: vehicletype.h:170
void setAvailableResource(const Resources &plantResources)
void vtSelected(const VehicleType *vt, bool mouse)
int getScalarWeaponType(void) const
The class describing properties that are common to all vehicles of a certain kind.
Definition: vehicletype.h:177
virtual int getResource(int amount, int resourcetype, bool queryonly, int scope=1, int player=-1)=0
vector< const VehicleType * > Container
bool quitModalLoop(int value)
Definition: paradialog.cpp:545
const int ammoProductionCost[weaponTypeNum][3]
Definition: vehicletype.cpp:75
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
AddProductionLine_SelectionItemFactory(ContainerBase *my_plant, const Container &types)
bool requiresAmmo(void) const
int _resourcemode
how are Resources handled on this map 0= "ASC mode": complex system with mineral resources etc 1= "BI...
Definition: gamemap.h:250
sigc::signal< void, const VehicleType * > sigVehicleTypeMarked
virtual bool closeWindow()
Definition: paradialog.cpp:580
bool successful() const
void vehicleTypeSelected(const VehicleType *type, bool mouse)
static CGameOptions * Instance()
returns the only Instance
Definition: gameoptions.cpp:38
Resources getCost(const VehicleType *type)
Resources getStorageCapacity(int mode) const
returns the Storage capacity of the unit
const VehicleType * getVehicletype() const
sigc::signal< void, const VehicleType *, bool > sigVehicleTypeSelected
VehicleProduction_SelectionItemFactory(Resources plantResources, const ContainerBase *productionplant, const ConstructUnitCommand::Producables &produceableUnits)
const ContainerBaseType * baseType
the type descriping all non-instance specific properties of the container
Definition: containerbase.h:80
#define maxint
Definition: typen.h:462
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
void displayActionError(const ActionResult &result, const ASCString &additionalInfo)
Definition: dialog.cpp:2168
int choice_dlg(const ASCString &title, const ASCString &leftButton, const ASCString &rightButton)
Definition: dialog.cpp:616
VehicleProduction_SelectionWindow(PG_Widget *parent, const PG_Rect &r, ContainerBase *plant, const ConstructUnitCommand::Producables &produceableUnits, bool internally)
void vehicleTypeSelected(const VehicleType *type, bool mouse)
vector< const VehicleType * > productionLinesBuyable()
Resources getCost(const VehicleType *type)
vector< ConstructUnitCommand::ProductionEntry > Producables
void itemMarked(const SelectionWidget *widget, bool mouse)