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 "BaseDestroyedState.h"
20 #include "../Engine/Game.h"
21 #include "../Resource/ResourcePack.h"
22 #include "../Engine/Language.h"
23 #include "../Engine/Palette.h"
24 #include "../Engine/Surface.h"
25 #include "../Interface/Window.h"
26 #include "../Interface/Text.h"
27 #include "../Interface/TextButton.h"
28 #include "../Savegame/SavedGame.h"
29 #include "../Savegame/Base.h"
30 #include "../Savegame/Region.h"
31 #include "../Savegame/AlienMission.h"
32 #include "../Savegame/Ufo.h"
33 #include "../Ruleset/RuleRegion.h"
34 #include "../Engine/Options.h"
35 
36 namespace OpenXcom
37 {
38 
BaseDestroyedState(Game * game,Base * base)39 BaseDestroyedState::BaseDestroyedState(Game *game, Base *base) : State(game), _base(base)
40 {
41 	_screen = false;
42 
43 	// Create objects
44 	_window = new Window(this, 256, 160, 32, 20);
45 	_btnOk = new TextButton(100, 20, 110, 142);
46 	_txtMessage = new Text(224, 48, 48, 76);
47 
48 	// Set palette
49 	setPalette("PAL_GEOSCAPE", 7);
50 
51 	add(_window);
52 	add(_btnOk);
53 	add(_txtMessage);
54 
55 	centerAllSurfaces();
56 
57 	// Set up objects
58 	_window->setColor(Palette::blockOffset(8)+5);
59 	_window->setBackground(_game->getResourcePack()->getSurface("BACK15.SCR"));
60 
61 	_btnOk->setColor(Palette::blockOffset(8)+5);
62 	_btnOk->setText(tr("STR_OK"));
63 	_btnOk->onMouseClick((ActionHandler)&BaseDestroyedState::btnOkClick);
64 	_btnOk->onKeyboardPress((ActionHandler)&BaseDestroyedState::btnOkClick, Options::keyOk);
65 	_btnOk->onKeyboardPress((ActionHandler)&BaseDestroyedState::btnOkClick, Options::keyCancel);
66 
67 	_txtMessage->setAlign(ALIGN_CENTER);
68 	_txtMessage->setBig();
69 	_txtMessage->setWordWrap(true);
70 	_txtMessage->setColor(Palette::blockOffset(8)+5);
71 
72 	_txtMessage->setText(tr("STR_THE_ALIENS_HAVE_DESTROYED_THE_UNDEFENDED_BASE").arg(_base->getName()));
73 
74 	std::vector<Region*>::iterator k = _game->getSavedGame()->getRegions()->begin();
75 	for (; k != _game->getSavedGame()->getRegions()->end(); ++k)
76 	{
77 		if ((*k)->getRules()->insideRegion((base)->getLongitude(), (base)->getLatitude()))
78 		{
79 			break;
80 		}
81 	}
82 
83 	AlienMission* am = _game->getSavedGame()->getAlienMission((*k)->getRules()->getType(), "STR_ALIEN_RETALIATION");
84 	for (std::vector<Ufo*>::iterator i = _game->getSavedGame()->getUfos()->begin(); i != _game->getSavedGame()->getUfos()->end();)
85 	{
86 		if ((*i)->getMission() == am)
87 		{
88 			delete *i;
89 			i = _game->getSavedGame()->getUfos()->erase(i);
90 		}
91 		else
92 		{
93 			++i;
94 		}
95 	}
96 
97 	for (std::vector<AlienMission*>::iterator i = _game->getSavedGame()->getAlienMissions().begin();
98 		i != _game->getSavedGame()->getAlienMissions().end(); ++i)
99 	{
100 		if ((AlienMission*)(*i) == am)
101 		{
102 			delete (*i);
103 			_game->getSavedGame()->getAlienMissions().erase(i);
104 			break;
105 		}
106 	}
107 }
108 
109 /**
110  *
111  */
~BaseDestroyedState()112 BaseDestroyedState::~BaseDestroyedState()
113 {
114 }
115 
116 /**
117  * Returns to the previous screen.
118  * @param action Pointer to an action.
119  */
btnOkClick(Action *)120 void BaseDestroyedState::btnOkClick(Action *)
121 {
122 	_game->popState();
123 	for (std::vector<Base*>::iterator i = _game->getSavedGame()->getBases()->begin(); i != _game->getSavedGame()->getBases()->end(); ++i)
124 	{
125 		if ((*i) == _base)
126 		{
127 			delete (*i);
128 			_game->getSavedGame()->getBases()->erase(i);
129 			break;
130 		}
131 	}
132 }
133 
134 }
135