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 "CraftWeaponsState.h"
20 #include <sstream>
21 #include <cmath>
22 #include "../Engine/Game.h"
23 #include "../Resource/ResourcePack.h"
24 #include "../Engine/Language.h"
25 #include "../Engine/Palette.h"
26 #include "../Engine/Options.h"
27 #include "../Interface/TextButton.h"
28 #include "../Interface/Window.h"
29 #include "../Interface/Text.h"
30 #include "../Interface/TextList.h"
31 #include "../Ruleset/Ruleset.h"
32 #include "../Savegame/Craft.h"
33 #include "../Savegame/CraftWeapon.h"
34 #include "../Ruleset/RuleCraftWeapon.h"
35 #include "../Savegame/ItemContainer.h"
36 #include "../Savegame/Base.h"
37 
38 namespace OpenXcom
39 {
40 
41 /**
42  * Initializes all the elements in the Craft Weapons window.
43  * @param game Pointer to the core game.
44  * @param base Pointer to the base to get info from.
45  * @param craft ID of the selected craft.
46  * @param weapon ID of the selected weapon.
47  */
CraftWeaponsState(Game * game,Base * base,size_t craft,size_t weapon)48 CraftWeaponsState::CraftWeaponsState(Game *game, Base *base, size_t craft, size_t weapon) : State(game), _base(base), _craft(craft), _weapon(weapon), _weapons()
49 {
50 	_screen = false;
51 
52 	// Create objects
53 	_window = new Window(this, 220, 160, 50, 20, POPUP_BOTH);
54 	_btnCancel = new TextButton(140, 16, 90, 156);
55 	_txtTitle = new Text(208, 17, 56, 28);
56 	_txtArmament = new Text(76, 9, 66, 52);
57 	_txtQuantity = new Text(50, 9, 140, 52);
58 	_txtAmmunition = new Text(68, 17, 200, 44);
59 	_lstWeapons = new TextList(188, 80, 58, 68);
60 
61 	// Set palette
62 	setPalette("PAL_BASESCAPE", 4);
63 
64 	add(_window);
65 	add(_btnCancel);
66 	add(_txtTitle);
67 	add(_txtArmament);
68 	add(_txtQuantity);
69 	add(_txtAmmunition);
70 	add(_lstWeapons);
71 
72 	centerAllSurfaces();
73 
74 	// Set up objects
75 	_window->setColor(Palette::blockOffset(15)+6);
76 	_window->setBackground(_game->getResourcePack()->getSurface("BACK14.SCR"));
77 
78 	_btnCancel->setColor(Palette::blockOffset(15)+6);
79 	_btnCancel->setText(tr("STR_CANCEL_UC"));
80 	_btnCancel->onMouseClick((ActionHandler)&CraftWeaponsState::btnCancelClick);
81 	_btnCancel->onKeyboardPress((ActionHandler)&CraftWeaponsState::btnCancelClick, Options::keyCancel);
82 
83 	_txtTitle->setColor(Palette::blockOffset(15)+6);
84 	_txtTitle->setBig();
85 	_txtTitle->setAlign(ALIGN_CENTER);
86 	_txtTitle->setText(tr("STR_SELECT_ARMAMENT"));
87 
88 	_txtArmament->setColor(Palette::blockOffset(15)+6);
89 	_txtArmament->setText(tr("STR_ARMAMENT"));
90 
91 	_txtQuantity->setColor(Palette::blockOffset(15)+6);
92 	_txtQuantity->setText(tr("STR_QUANTITY_UC"));
93 
94 	_txtAmmunition->setColor(Palette::blockOffset(15)+6);
95 	_txtAmmunition->setText(tr("STR_AMMUNITION_AVAILABLE"));
96 	_txtAmmunition->setWordWrap(true);
97 
98 	_lstWeapons->setColor(Palette::blockOffset(13)+10);
99 	_lstWeapons->setArrowColor(Palette::blockOffset(15)+6);
100 	_lstWeapons->setColumns(3, 94, 50, 36);
101 	_lstWeapons->setSelectable(true);
102 	_lstWeapons->setBackground(_window);
103 	_lstWeapons->setMargin(8);
104 
105 	_lstWeapons->addRow(1, tr("STR_NONE_UC").c_str());
106 	_weapons.push_back(0);
107 
108 	const std::vector<std::string> &weapons = _game->getRuleset()->getCraftWeaponsList();
109 	for (std::vector<std::string>::const_iterator i = weapons.begin(); i != weapons.end(); ++i)
110 	{
111 		RuleCraftWeapon *w = _game->getRuleset()->getCraftWeapon(*i);
112 		if (_base->getItems()->getItem(w->getLauncherItem()) > 0)
113 		{
114 			_weapons.push_back(w);
115 			std::wostringstream ss, ss2;
116 			ss << _base->getItems()->getItem(w->getLauncherItem());
117 			if (w->getClipItem() != "")
118 			{
119 				ss2 << _base->getItems()->getItem(w->getClipItem());
120 			}
121 			else
122 			{
123 				ss2 << tr("STR_NOT_AVAILABLE");
124 			}
125 			_lstWeapons->addRow(3, tr(w->getType()).c_str(), ss.str().c_str(), ss2.str().c_str());
126 		}
127 	}
128 	_lstWeapons->onMouseClick((ActionHandler)&CraftWeaponsState::lstWeaponsClick);
129 }
130 
131 /**
132  *
133  */
~CraftWeaponsState()134 CraftWeaponsState::~CraftWeaponsState()
135 {
136 
137 }
138 
139 /**
140  * Returns to the previous screen.
141  * @param action Pointer to an action.
142  */
btnCancelClick(Action *)143 void CraftWeaponsState::btnCancelClick(Action *)
144 {
145 	_game->popState();
146 }
147 
148 /**
149  * Equips the weapon on the craft and returns to the previous screen.
150  * @param action Pointer to an action.
151  */
lstWeaponsClick(Action *)152 void CraftWeaponsState::lstWeaponsClick(Action *)
153 {
154 	CraftWeapon *current = _base->getCrafts()->at(_craft)->getWeapons()->at(_weapon);
155 	// Remove current weapon
156 	if (current != 0)
157 	{
158 		_base->getItems()->addItem(current->getRules()->getLauncherItem());
159 		_base->getItems()->addItem(current->getRules()->getClipItem(), current->getClipsLoaded(_game->getRuleset()));
160 		delete current;
161 		_base->getCrafts()->at(_craft)->getWeapons()->at(_weapon) = 0;
162 	}
163 
164 	// Equip new weapon
165 	if (_weapons[_lstWeapons->getSelectedRow()] != 0)
166 	{
167 		CraftWeapon *sel = new CraftWeapon(_weapons[_lstWeapons->getSelectedRow()], 0);
168 		sel->setRearming(true);
169 		_base->getItems()->removeItem(sel->getRules()->getLauncherItem());
170 		_base->getCrafts()->at(_craft)->getWeapons()->at(_weapon) = sel;
171 		if (_base->getCrafts()->at(_craft)->getStatus() == "STR_READY")
172 		{
173 			_base->getCrafts()->at(_craft)->setStatus("STR_REARMING");
174 		}
175 	}
176 
177 	_game->popState();
178 }
179 
180 }
181