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 "ItemsArrivingState.h"
20 #include <sstream>
21 #include <algorithm>
22 #include "../Engine/Game.h"
23 #include "../Resource/ResourcePack.h"
24 #include "../Engine/Language.h"
25 #include "../Engine/Palette.h"
26 #include "../Interface/TextButton.h"
27 #include "../Interface/Window.h"
28 #include "../Interface/Text.h"
29 #include "../Interface/TextList.h"
30 #include "../Savegame/SavedGame.h"
31 #include "../Savegame/Base.h"
32 #include "../Savegame/ItemContainer.h"
33 #include "../Savegame/Transfer.h"
34 #include "../Savegame/Craft.h"
35 #include "../Savegame/CraftWeapon.h"
36 #include "../Savegame/Vehicle.h"
37 #include "../Ruleset/Ruleset.h"
38 #include "../Ruleset/RuleItem.h"
39 #include "../Ruleset/RuleCraftWeapon.h"
40 #include "GeoscapeState.h"
41 #include "../Engine/Options.h"
42 #include "../Basescape/BasescapeState.h"
43 
44 namespace OpenXcom
45 {
46 
47 /**
48  * Initializes all the elements in the Items Arriving window.
49  * @param game Pointer to the core game.
50  * @param state Pointer to the Geoscape state.
51  */
ItemsArrivingState(Game * game,GeoscapeState * state)52 ItemsArrivingState::ItemsArrivingState(Game *game, GeoscapeState *state) : State(game), _state(state), _base(0)
53 {
54 	_screen = false;
55 
56 	// Create objects
57 	_window = new Window(this, 320, 184, 0, 8, POPUP_BOTH);
58 	_btnOk = new TextButton(142, 16, 16, 166);
59 	_btnGotoBase = new TextButton(142, 16, 162, 166);
60 	_txtTitle = new Text(310, 17, 5, 18);
61 	_txtItem = new Text(114, 9, 16, 34);
62 	_txtQuantity = new Text(54, 9, 152, 34);
63 	_txtDestination = new Text(112, 9, 212, 34);
64 	_lstTransfers = new TextList(271, 112, 14, 50);
65 
66 	// Set palette
67 	setPalette("PAL_GEOSCAPE", 6);
68 
69 	add(_window);
70 	add(_btnOk);
71 	add(_btnGotoBase);
72 	add(_txtTitle);
73 	add(_txtItem);
74 	add(_txtQuantity);
75 	add(_txtDestination);
76 	add(_lstTransfers);
77 
78 	centerAllSurfaces();
79 
80 	// Set up objects
81 	_window->setColor(Palette::blockOffset(8)+5);
82 	_window->setBackground(_game->getResourcePack()->getSurface("BACK13.SCR"));
83 
84 	_btnOk->setColor(Palette::blockOffset(8)+5);
85 	_btnOk->setText(tr("STR_OK"));
86 	_btnOk->onMouseClick((ActionHandler)&ItemsArrivingState::btnOkClick);
87 	_btnOk->onKeyboardPress((ActionHandler)&ItemsArrivingState::btnOkClick, Options::keyCancel);
88 
89 	_btnGotoBase->setColor(Palette::blockOffset(8)+5);
90 	_btnGotoBase->setText(tr("STR_GO_TO_BASE"));
91 	_btnGotoBase->onMouseClick((ActionHandler)&ItemsArrivingState::btnGotoBaseClick);
92 	_btnGotoBase->onKeyboardPress((ActionHandler)&ItemsArrivingState::btnGotoBaseClick, Options::keyOk);
93 
94 	_txtTitle->setColor(Palette::blockOffset(8)+5);
95 	_txtTitle->setBig();
96 	_txtTitle->setAlign(ALIGN_CENTER);
97 	_txtTitle->setText(tr("STR_ITEMS_ARRIVING"));
98 
99 	_txtItem->setColor(Palette::blockOffset(8)+5);
100 	_txtItem->setText(tr("STR_ITEM"));
101 
102 	_txtQuantity->setColor(Palette::blockOffset(8)+5);
103 	_txtQuantity->setText(tr("STR_QUANTITY_UC"));
104 
105 	_txtDestination->setColor(Palette::blockOffset(8)+5);
106 	_txtDestination->setText(tr("STR_DESTINATION_UC"));
107 
108 	_lstTransfers->setColor(Palette::blockOffset(8)+10);
109 	_lstTransfers->setArrowColor(Palette::blockOffset(8)+5);
110 	_lstTransfers->setColumns(3, 155, 41, 98);
111 	_lstTransfers->setSelectable(true);
112 	_lstTransfers->setBackground(_window);
113 	_lstTransfers->setMargin(2);
114 
115 	for (std::vector<Base*>::iterator i = _game->getSavedGame()->getBases()->begin(); i != _game->getSavedGame()->getBases()->end(); ++i)
116 	{
117 		for (std::vector<Transfer*>::iterator j = (*i)->getTransfers()->begin(); j != (*i)->getTransfers()->end();)
118 		{
119 			if ((*j)->getHours() == 0)
120 			{
121 				_base = (*i);
122 
123 				// Check if we have an automated use for an item
124 				if ((*j)->getType() == TRANSFER_ITEM)
125 				{
126 					RuleItem *item = _game->getRuleset()->getItem((*j)->getItems());
127 					for (std::vector<Craft*>::iterator c = (*i)->getCrafts()->begin(); c != (*i)->getCrafts()->end(); ++c)
128 					{
129 						// Check if it's ammo to reload a craft
130 						if ((*c)->getStatus() == "STR_READY")
131 						{
132 							for (std::vector<CraftWeapon*>::iterator w = (*c)->getWeapons()->begin(); w != (*c)->getWeapons()->end(); ++w)
133 							{
134 								if ((*w) != 0 && (*w)->getRules()->getClipItem() == item->getType() && (*w)->getAmmo() < (*w)->getRules()->getAmmoMax())
135 								{
136 									(*w)->setRearming(true);
137 									(*c)->setStatus("STR_REARMING");
138 								}
139 							}
140 						}
141 						// Check if it's ammo to reload a vehicle
142 						for (std::vector<Vehicle*>::iterator v = (*c)->getVehicles()->begin(); v != (*c)->getVehicles()->end(); ++v)
143 						{
144 							std::vector<std::string>::iterator ammo = std::find((*v)->getRules()->getCompatibleAmmo()->begin(), (*v)->getRules()->getCompatibleAmmo()->end(), item->getType());
145 							if (ammo != (*v)->getRules()->getCompatibleAmmo()->end() && (*v)->getAmmo() < item->getClipSize())
146 							{
147 								int used = std::min((*j)->getQuantity(), item->getClipSize() - (*v)->getAmmo());
148 								(*v)->setAmmo((*v)->getAmmo() + used);
149 								// Note that the items have already been delivered, so we remove them from the base, not the transfer
150 								_base->getItems()->removeItem(item->getType(), used);
151 							}
152 						}
153 					}
154 				}
155 
156 				// Remove transfer
157 				std::wostringstream ss;
158 				ss << (*j)->getQuantity();
159 				_lstTransfers->addRow(3, (*j)->getName(_game->getLanguage()).c_str(), ss.str().c_str(), (*i)->getName().c_str());
160 				delete *j;
161 				j = (*i)->getTransfers()->erase(j);
162 			}
163 			else
164 			{
165 				++j;
166 			}
167 		}
168 	}
169 }
170 
171 /**
172  *
173  */
~ItemsArrivingState()174 ItemsArrivingState::~ItemsArrivingState()
175 {
176 
177 }
178 
179 /**
180  * Returns to the previous screen.
181  * @param action Pointer to an action.
182  */
btnOkClick(Action *)183 void ItemsArrivingState::btnOkClick(Action *)
184 {
185 	_game->popState();
186 }
187 
188 /**
189  * Goes to the base for the respective transfer.
190  * @param action Pointer to an action.
191  */
btnGotoBaseClick(Action *)192 void ItemsArrivingState::btnGotoBaseClick(Action *)
193 {
194 	_state->timerReset();
195 	_game->popState();
196 	_game->pushState(new BasescapeState(_game, _base, _state->getGlobe()));
197 }
198 
199 }
200