1 /*
2  * Copyright 2010-2014 OpenXcom Developers.
3  *
4  * This file is part of OpenXcom.
5  *
6  * OpenXcom is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * OpenXcom is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with OpenXcom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "PlaceFacilityState.h"
20 #include <sstream>
21 #include "../Engine/Game.h"
22 #include "../Resource/ResourcePack.h"
23 #include "../Engine/Language.h"
24 #include "../Engine/Palette.h"
25 #include "../Interface/TextButton.h"
26 #include "../Interface/Window.h"
27 #include "../Interface/Text.h"
28 #include "BaseView.h"
29 #include "../Savegame/Base.h"
30 #include "../Savegame/BaseFacility.h"
31 #include "../Ruleset/RuleBaseFacility.h"
32 #include "../Savegame/SavedGame.h"
33 #include "../Menu/ErrorMessageState.h"
34 #include "../Engine/Options.h"
35 #include <limits>
36 
37 namespace OpenXcom
38 {
39 
40 /**
41  * Initializes all the elements in the Place Facility window.
42  * @param game Pointer to the core game.
43  * @param base Pointer to the base to get info from.
44  * @param rule Pointer to the facility ruleset to build.
45  */
PlaceFacilityState(Game * game,Base * base,RuleBaseFacility * rule)46 PlaceFacilityState::PlaceFacilityState(Game *game, Base *base, RuleBaseFacility *rule) : State(game), _base(base), _rule(rule)
47 {
48 	_screen = false;
49 
50 	// Create objects
51 	_window = new Window(this, 128, 160, 192, 40);
52 	_view = new BaseView(192, 192, 0, 8);
53 	_btnCancel = new TextButton(112, 16, 200, 176);
54 	_txtFacility = new Text(110, 9, 202, 50);
55 	_txtCost = new Text(110, 9, 202, 62);
56 	_numCost = new Text(110, 17, 202, 70);
57 	_txtTime = new Text(110, 9, 202, 90);
58 	_numTime = new Text(110, 17, 202, 98);
59 	_txtMaintenance = new Text(110, 9, 202, 118);
60 	_numMaintenance = new Text(110, 17, 202, 126);
61 
62 	// Set palette
63 	setPalette("PAL_BASESCAPE", 6);
64 
65 	add(_window);
66 	add(_view);
67 	add(_btnCancel);
68 	add(_txtFacility);
69 	add(_txtCost);
70 	add(_numCost);
71 	add(_txtTime);
72 	add(_numTime);
73 	add(_txtMaintenance);
74 	add(_numMaintenance);
75 
76 	centerAllSurfaces();
77 
78 	// Set up objects
79 	_window->setColor(Palette::blockOffset(13)+10);
80 	_window->setBackground(_game->getResourcePack()->getSurface("BACK01.SCR"));
81 
82 	_view->setTexture(_game->getResourcePack()->getSurfaceSet("BASEBITS.PCK"));
83 	_view->setBase(_base);
84 	_view->setSelectable(rule->getSize());
85 	_view->onMouseClick((ActionHandler)&PlaceFacilityState::viewClick);
86 
87 	_btnCancel->setColor(Palette::blockOffset(13)+10);
88 	_btnCancel->setText(tr("STR_CANCEL"));
89 	_btnCancel->onMouseClick((ActionHandler)&PlaceFacilityState::btnCancelClick);
90 	_btnCancel->onKeyboardPress((ActionHandler)&PlaceFacilityState::btnCancelClick, Options::keyCancel);
91 
92 	_txtFacility->setColor(Palette::blockOffset(13)+10);
93 	_txtFacility->setText(tr(_rule->getType()));
94 
95 	_txtCost->setColor(Palette::blockOffset(13)+10);
96 	_txtCost->setText(tr("STR_COST_UC"));
97 
98 	_numCost->setColor(Palette::blockOffset(13));
99 	_numCost->setBig();
100 	_numCost->setText(Text::formatFunding(_rule->getBuildCost()));
101 
102 	_txtTime->setColor(Palette::blockOffset(13)+10);
103 	_txtTime->setText(tr("STR_CONSTRUCTION_TIME_UC"));
104 
105 	_numTime->setColor(Palette::blockOffset(13));
106 	_numTime->setBig();
107 	_numTime->setText(tr("STR_DAY", _rule->getBuildTime()));
108 
109 	_txtMaintenance->setColor(Palette::blockOffset(13)+10);
110 	_txtMaintenance->setText(tr("STR_MAINTENANCE_UC"));
111 
112 	_numMaintenance->setColor(Palette::blockOffset(13));
113 	_numMaintenance->setBig();
114 	_numMaintenance->setText(Text::formatFunding(_rule->getMonthlyCost()));
115 }
116 
117 /**
118  *
119  */
~PlaceFacilityState()120 PlaceFacilityState::~PlaceFacilityState()
121 {
122 
123 }
124 
125 /**
126  * Returns to the previous screen.
127  * @param action Pointer to an action.
128  */
btnCancelClick(Action *)129 void PlaceFacilityState::btnCancelClick(Action *)
130 {
131 	_game->popState();
132 }
133 
134 /**
135  * Processes clicking on facilities.
136  * @param action Pointer to an action.
137  */
viewClick(Action *)138 void PlaceFacilityState::viewClick(Action *)
139 {
140 	if (!_view->isPlaceable(_rule))
141 	{
142 		_game->popState();
143 		_game->pushState(new ErrorMessageState(_game, "STR_CANNOT_BUILD_HERE", _palette, Palette::blockOffset(15)+1, "BACK01.SCR", 6));
144 	}
145 	else if (_game->getSavedGame()->getFunds() < _rule->getBuildCost())
146 	{
147 		_game->popState();
148 		_game->pushState(new ErrorMessageState(_game, "STR_NOT_ENOUGH_MONEY", _palette, Palette::blockOffset(15)+1, "BACK01.SCR", 6));
149 	}
150 	else
151 	{
152 		BaseFacility *fac = new BaseFacility(_rule, _base);
153 		fac->setX(_view->getGridX());
154 		fac->setY(_view->getGridY());
155 		fac->setBuildTime(_rule->getBuildTime());
156 		_base->getFacilities()->push_back(fac);
157 		if (Options::allowBuildingQueue)
158 		{
159 			if (_view->isQueuedBuilding(_rule)) fac->setBuildTime(std::numeric_limits<int>::max());
160 			_view->reCalcQueuedBuildings();
161 		}
162 		_game->getSavedGame()->setFunds(_game->getSavedGame()->getFunds() - _rule->getBuildCost());
163 		_game->popState();
164 	}
165 }
166 
167 }
168