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 "InterceptState.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 "../Interface/TextList.h"
29 #include "../Savegame/Base.h"
30 #include "../Savegame/Craft.h"
31 #include "../Ruleset/RuleCraft.h"
32 #include "../Savegame/SavedGame.h"
33 #include "../Engine/Options.h"
34 #include "Globe.h"
35 #include "SelectDestinationState.h"
36 #include "ConfirmDestinationState.h"
37 #include "../Basescape/BasescapeState.h"
38 
39 namespace OpenXcom
40 {
41 
42 /**
43  * Initializes all the elements in the Intercept window.
44  * @param game Pointer to the core game.
45  * @param globe Pointer to the Geoscape globe.
46  * @param base Pointer to base to show contained crafts (NULL to show all crafts).
47  * @param target Pointer to target to intercept (NULL to ask user for target).
48  */
InterceptState(Game * game,Globe * globe,Base * base,Target * target)49 InterceptState::InterceptState(Game *game, Globe *globe, Base *base, Target *target) : State(game), _globe(globe), _base(base), _target(target), _crafts()
50 {
51 	_screen = false;
52 
53 	// Create objects
54 	_window = new Window(this, 320, 140, 0, 30, POPUP_HORIZONTAL);
55 	_btnCancel = new TextButton(_base ? 142 : 288, 16, 16, 146);
56 	_btnGotoBase = new TextButton(142, 16, 162, 146);
57 	_txtTitle = new Text(300, 17, 10, 46);
58 	_txtCraft = new Text(86, 9, 14, 70);
59 	_txtStatus = new Text(70, 9, 100, 70);
60 	_txtBase = new Text(80, 9, 170, 70);
61 	_txtWeapons = new Text(80, 17, 238, 62);
62 	_lstCrafts = new TextList(288, 64, 8, 78);
63 
64 	// Set palette
65 	setPalette("PAL_GEOSCAPE", 4);
66 
67 	add(_window);
68 	add(_btnCancel);
69 	add(_btnGotoBase);
70 	add(_txtTitle);
71 	add(_txtCraft);
72 	add(_txtStatus);
73 	add(_txtBase);
74 	add(_txtWeapons);
75 	add(_lstCrafts);
76 
77 	centerAllSurfaces();
78 
79 	// Set up objects
80 	_window->setColor(Palette::blockOffset(15)-1);
81 	_window->setBackground(_game->getResourcePack()->getSurface("BACK12.SCR"));
82 
83 	_btnCancel->setColor(Palette::blockOffset(8)+5);
84 	_btnCancel->setText(tr("STR_CANCEL"));
85 	_btnCancel->onMouseClick((ActionHandler)&InterceptState::btnCancelClick);
86 	_btnCancel->onKeyboardPress((ActionHandler)&InterceptState::btnCancelClick, Options::keyCancel);
87 	_btnCancel->onKeyboardPress((ActionHandler)&InterceptState::btnCancelClick, Options::keyGeoIntercept);
88 
89 	_btnGotoBase->setColor(Palette::blockOffset(8)+5);
90 	_btnGotoBase->setText(tr("STR_GO_TO_BASE"));
91 	_btnGotoBase->onMouseClick((ActionHandler)&InterceptState::btnGotoBaseClick);
92 	_btnGotoBase->setVisible(_base != 0);
93 
94 	_txtTitle->setColor(Palette::blockOffset(15)-1);
95 	_txtTitle->setAlign(ALIGN_CENTER);
96 	_txtTitle->setBig();
97 	_txtTitle->setText(tr("STR_LAUNCH_INTERCEPTION"));
98 
99 	_txtCraft->setColor(Palette::blockOffset(8)+5);
100 	_txtCraft->setText(tr("STR_CRAFT"));
101 
102 	_txtStatus->setColor(Palette::blockOffset(8)+5);
103 	_txtStatus->setText(tr("STR_STATUS"));
104 
105 	_txtBase->setColor(Palette::blockOffset(8)+5);
106 	_txtBase->setText(tr("STR_BASE"));
107 
108 	_txtWeapons->setColor(Palette::blockOffset(8)+5);
109 	_txtWeapons->setText(tr("STR_WEAPONS_CREW_HWPS"));
110 
111 	_lstCrafts->setColor(Palette::blockOffset(15)-1);
112 	_lstCrafts->setSecondaryColor(Palette::blockOffset(8)+10);
113 	_lstCrafts->setColumns(4, 86, 70, 80, 46);
114 	_lstCrafts->setSelectable(true);
115 	_lstCrafts->setBackground(_window);
116 	_lstCrafts->setMargin(6);
117 	_lstCrafts->onMouseClick((ActionHandler)&InterceptState::lstCraftsLeftClick);
118 	_lstCrafts->onMouseClick((ActionHandler)&InterceptState::lstCraftsRightClick, SDL_BUTTON_RIGHT);
119 
120 	int row = 0;
121 	for (std::vector<Base*>::iterator i = _game->getSavedGame()->getBases()->begin(); i != _game->getSavedGame()->getBases()->end(); ++i)
122 	{
123 		if (_base != 0 && (*i) != _base)
124 			continue;
125 		for (std::vector<Craft*>::iterator j = (*i)->getCrafts()->begin(); j != (*i)->getCrafts()->end(); ++j)
126 		{
127 			std::wostringstream ss;
128 			if ((*j)->getNumWeapons() > 0)
129 			{
130 				ss << L'\x01' << (*j)->getNumWeapons() << L'\x01';
131 			}
132 			else
133 			{
134 				ss << 0;
135 			}
136 			ss << "/";
137 			if ((*j)->getNumSoldiers() > 0)
138 			{
139 				ss << L'\x01' << (*j)->getNumSoldiers() << L'\x01';
140 			}
141 			else
142 			{
143 				ss << 0;
144 			}
145 			ss << "/";
146 			if ((*j)->getNumVehicles() > 0)
147 			{
148 				ss << L'\x01' << (*j)->getNumVehicles() << L'\x01';
149 			}
150 			else
151 			{
152 				ss << 0;
153 			}
154 			_crafts.push_back(*j);
155 			_lstCrafts->addRow(4, (*j)->getName(_game->getLanguage()).c_str(), tr((*j)->getStatus()).c_str(), (*i)->getName().c_str(), ss.str().c_str());
156 			if ((*j)->getStatus() == "STR_READY")
157 			{
158 				_lstCrafts->setCellColor(row, 1, Palette::blockOffset(8)+10);
159 			}
160 			row++;
161 		}
162 	}
163 }
164 
165 /**
166  *
167  */
~InterceptState()168 InterceptState::~InterceptState()
169 {
170 
171 }
172 
173 /**
174  * Closes the window.
175  * @param action Pointer to an action.
176  */
btnCancelClick(Action *)177 void InterceptState::btnCancelClick(Action *)
178 {
179 	_game->popState();
180 }
181 
182 /**
183  * Goes to the base for the respective craft.
184  * @param action Pointer to an action.
185  */
btnGotoBaseClick(Action *)186 void InterceptState::btnGotoBaseClick(Action *)
187 {
188 	_game->popState();
189 	_game->pushState(new BasescapeState(_game, _base, _globe));
190 }
191 
192 /**
193  * Pick a target for the selected craft.
194  * @param action Pointer to an action.
195  */
lstCraftsLeftClick(Action *)196 void InterceptState::lstCraftsLeftClick(Action *)
197 {
198 	Craft* c = _crafts[_lstCrafts->getSelectedRow()];
199 	if (c->getStatus() == "STR_READY" || ((c->getStatus() == "STR_OUT" || Options::craftLaunchAlways) && !c->getLowFuel()))
200 	{
201 		_game->popState();
202 		if (_target == 0)
203 		{
204 			_game->pushState(new SelectDestinationState(_game, c, _globe));
205 		}
206 		else
207 		{
208 			_game->pushState(new ConfirmDestinationState(_game, c, _target));
209 		}
210 	}
211 }
212 
213 /**
214  * Centers on the selected craft.
215  * @param action Pointer to an action.
216  */
lstCraftsRightClick(Action *)217 void InterceptState::lstCraftsRightClick(Action *)
218 {
219 	Craft* c = _crafts[_lstCrafts->getSelectedRow()];
220 	if (c->getStatus() == "STR_OUT")
221 	{
222 		_globe->center(c->getLongitude(), c->getLatitude());
223 		_game->popState();
224 	}
225 }
226 
227 }
228