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 "ConfirmLandingState.h"
20 #include <sstream>
21 #include "../Engine/RNG.h"
22 #include "../Engine/Game.h"
23 #include "../Resource/ResourcePack.h"
24 #include "../Engine/Language.h"
25 #include "../Engine/Palette.h"
26 #include "../Engine/Surface.h"
27 #include "../Interface/Window.h"
28 #include "../Interface/Text.h"
29 #include "../Interface/TextButton.h"
30 #include "../Savegame/SavedBattleGame.h"
31 #include "../Savegame/SavedGame.h"
32 #include "../Savegame/Craft.h"
33 #include "../Savegame/Target.h"
34 #include "../Savegame/Ufo.h"
35 #include "../Savegame/Base.h"
36 #include "../Ruleset/Ruleset.h"
37 #include "../Savegame/TerrorSite.h"
38 #include "../Savegame/AlienBase.h"
39 #include "../Battlescape/BriefingState.h"
40 #include "../Battlescape/BattlescapeGenerator.h"
41 #include "../Geoscape/GeoscapeState.h"
42 #include "../Engine/Exception.h"
43 #include "../Engine/Options.h"
44 
45 namespace OpenXcom
46 {
47 
48 /**
49  * Initializes all the elements in the Confirm Landing window.
50  * @param game Pointer to the core game.
51  * @param craft Pointer to the craft to confirm.
52  * @param texture Texture of the landing site.
53  * @param shade Shade of the landing site.
54  */
ConfirmLandingState(Game * game,Craft * craft,int texture,int shade)55 ConfirmLandingState::ConfirmLandingState(Game *game, Craft *craft, int texture, int shade) : State(game), _craft(craft), _texture(texture), _shade(shade)
56 {
57 	_screen = false;
58 
59 	// Create objects
60 	_window = new Window(this, 216, 160, 20, 20, POPUP_BOTH);
61 	_btnYes = new TextButton(80, 20, 40, 150);
62 	_btnNo = new TextButton(80, 20, 136, 150);
63 	_txtMessage = new Text(206, 80, 25, 40);
64 	_txtBegin = new Text(206, 17, 25, 130);
65 
66 	// Set palette
67 	setPalette("PAL_GEOSCAPE", 3);
68 
69 	add(_window);
70 	add(_btnYes);
71 	add(_btnNo);
72 	add(_txtMessage);
73 	add(_txtBegin);
74 
75 	centerAllSurfaces();
76 
77 	// Set up objects
78 	_window->setColor(Palette::blockOffset(8)+5);
79 	_window->setBackground(_game->getResourcePack()->getSurface("BACK15.SCR"));
80 
81 	_btnYes->setColor(Palette::blockOffset(8)+5);
82 	_btnYes->setText(tr("STR_YES"));
83 	_btnYes->onMouseClick((ActionHandler)&ConfirmLandingState::btnYesClick);
84 	_btnYes->onKeyboardPress((ActionHandler)&ConfirmLandingState::btnYesClick, Options::keyOk);
85 
86 	_btnNo->setColor(Palette::blockOffset(8)+5);
87 	_btnNo->setText(tr("STR_NO"));
88 	_btnNo->onMouseClick((ActionHandler)&ConfirmLandingState::btnNoClick);
89 	_btnNo->onKeyboardPress((ActionHandler)&ConfirmLandingState::btnNoClick, Options::keyCancel);
90 
91 	_txtMessage->setColor(Palette::blockOffset(8)+10);
92 	_txtMessage->setSecondaryColor(Palette::blockOffset(8)+5);
93 	_txtMessage->setBig();
94 	_txtMessage->setAlign(ALIGN_CENTER);
95 	_txtMessage->setWordWrap(true);
96 	_txtMessage->setText(tr("STR_CRAFT_READY_TO_LAND_NEAR_DESTINATION")
97 						 .arg(_craft->getName(_game->getLanguage()))
98 						 .arg(_craft->getDestination()->getName(_game->getLanguage())));
99 
100 	_txtBegin->setColor(Palette::blockOffset(8)+5);
101 	_txtBegin->setBig();
102 	_txtBegin->setAlign(ALIGN_CENTER);
103 	_txtBegin->setText(tr("STR_BEGIN_MISSION"));
104 }
105 
106 /**
107  *
108  */
~ConfirmLandingState()109 ConfirmLandingState::~ConfirmLandingState()
110 {
111 
112 }
113 
114 /*
115  * Make sure we aren't returning to base.
116  */
init()117 void ConfirmLandingState::init()
118 {
119 	State::init();
120 	Base* b = dynamic_cast<Base*>(_craft->getDestination());
121 	if (b == _craft->getBase())
122 		_game->popState();
123 }
124 
125 /**
126  * Enters the mission.
127  * @param action Pointer to an action.
128  */
btnYesClick(Action *)129 void ConfirmLandingState::btnYesClick(Action *)
130 {
131 	_game->popState();
132 	Ufo* u = dynamic_cast<Ufo*>(_craft->getDestination());
133 	TerrorSite* t = dynamic_cast<TerrorSite*>(_craft->getDestination());
134 	AlienBase* b = dynamic_cast<AlienBase*>(_craft->getDestination());
135 
136 	SavedBattleGame *bgame = new SavedBattleGame();
137 	_game->getSavedGame()->setBattleGame(bgame);
138 	BattlescapeGenerator bgen = BattlescapeGenerator(_game);
139 	bgen.setWorldTexture(_texture);
140 	bgen.setWorldShade(_shade);
141 	bgen.setCraft(_craft);
142 	if (u != 0)
143 	{
144 		if(u->getStatus() == Ufo::CRASHED)
145 			bgame->setMissionType("STR_UFO_CRASH_RECOVERY");
146 		else
147 			bgame->setMissionType("STR_UFO_GROUND_ASSAULT");
148 		bgen.setUfo(u);
149 		bgen.setAlienRace(u->getAlienRace());
150 	}
151 	else if (t != 0)
152 	{
153 		bgame->setMissionType("STR_TERROR_MISSION");
154 		bgen.setTerrorSite(t);
155 		bgen.setAlienRace(t->getAlienRace());
156 	}
157 	else if (b != 0)
158 	{
159 		bgame->setMissionType("STR_ALIEN_BASE_ASSAULT");
160 		bgen.setAlienBase(b);
161 		bgen.setAlienRace(b->getAlienRace());
162 	}
163 	else
164 	{
165 		throw Exception("No mission available!");
166 	}
167 	bgen.run();
168 	_game->pushState(new BriefingState(_game, _craft));
169 }
170 
171 /**
172  * Returns the craft to base and closes the window.
173  * @param action Pointer to an action.
174  */
btnNoClick(Action *)175 void ConfirmLandingState::btnNoClick(Action *)
176 {
177 	_craft->returnToBase();
178 	_game->popState();
179 }
180 
181 }
182