1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
/* ShipyardPanel.cpp
Copyright (c) 2014 by Michael Zahniser
Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ShipyardPanel.h"
#include "text/Alignment.h"
#include "comparators/BySeriesAndIndex.h"
#include "ClickZone.h"
#include "Color.h"
#include "Dialog.h"
#include "text/DisplayText.h"
#include "text/Font.h"
#include "text/FontSet.h"
#include "text/Format.h"
#include "GameData.h"
#include "Government.h"
#include "Mission.h"
#include "Phrase.h"
#include "Planet.h"
#include "PlayerInfo.h"
#include "Point.h"
#include "Screen.h"
#include "Ship.h"
#include "ShipNameDialog.h"
#include "image/Sprite.h"
#include "image/SpriteSet.h"
#include "shader/SpriteShader.h"
#include "text/Truncate.h"
#include "UI.h"
#include <algorithm>
class System;
using namespace std;
namespace {
// Label for the description field of the detail pane.
const string DESCRIPTION = "description";
}
ShipyardPanel::ShipyardPanel(PlayerInfo &player, Sale<Ship> stock)
: ShopPanel(player, false), modifier(0), shipyard(stock)
{
for(const auto &it : GameData::Ships())
catalog[it.second.Attributes().Category()].push_back(it.first);
for(pair<const string, vector<string>> &it : catalog)
sort(it.second.begin(), it.second.end(), BySeriesAndIndex<Ship>());
}
void ShipyardPanel::Step()
{
ShopPanel::Step();
ShopPanel::CheckForMissions(Mission::SHIPYARD);
if(GetUI()->IsTop(this))
DoHelp("shipyard");
}
int ShipyardPanel::TileSize() const
{
return SHIP_SIZE;
}
bool ShipyardPanel::HasItem(const string &name) const
{
const Ship *ship = GameData::Ships().Get(name);
return shipyard.Has(ship);
}
void ShipyardPanel::DrawItem(const string &name, const Point &point)
{
const Ship *ship = GameData::Ships().Get(name);
zones.emplace_back(point, Point(SHIP_SIZE, SHIP_SIZE), ship);
if(point.Y() + SHIP_SIZE / 2 < Screen::Top() || point.Y() - SHIP_SIZE / 2 > Screen::Bottom())
return;
DrawShip(*ship, point, ship == selectedShip);
}
int ShipyardPanel::DividerOffset() const
{
return 121;
}
int ShipyardPanel::DetailWidth() const
{
return 3 * ItemInfoDisplay::PanelWidth();
}
double ShipyardPanel::DrawDetails(const Point ¢er)
{
string selectedItem = "No Ship Selected";
const Font &font = FontSet::Get(14);
double heightOffset = 20.;
if(selectedShip)
{
shipInfo.Update(*selectedShip, player, collapsed.contains(DESCRIPTION), true);
selectedItem = selectedShip->DisplayModelName();
const Point spriteCenter(center.X(), center.Y() + 20 + TileSize() / 2);
const Point startPoint(center.X() - INFOBAR_WIDTH / 2 + 20, center.Y() + 20 + TileSize());
const Sprite *background = SpriteSet::Get("ui/shipyard selected");
SpriteShader::Draw(background, spriteCenter);
const Sprite *shipSprite = selectedShip->GetSprite();
if(shipSprite)
{
const float spriteScale = min(1.f, (INFOBAR_WIDTH - 60.f) / max(shipSprite->Width(), shipSprite->Height()));
const Swizzle *swizzle = selectedShip->CustomSwizzle()
? selectedShip->CustomSwizzle() : GameData::PlayerGovernment()->GetSwizzle();
SpriteShader::Draw(shipSprite, spriteCenter, spriteScale, swizzle);
}
const bool hasDescription = shipInfo.DescriptionHeight();
double descriptionOffset = hasDescription ? 40. : 0.;
if(hasDescription)
{
if(!collapsed.contains(DESCRIPTION))
{
descriptionOffset = shipInfo.DescriptionHeight();
shipInfo.DrawDescription(startPoint);
}
else
{
const Color &dim = *GameData::Colors().Get("medium");
font.Draw(DESCRIPTION, startPoint + Point(35., 12.), dim);
const Sprite *collapsedArrow = SpriteSet::Get("ui/collapsed");
SpriteShader::Draw(collapsedArrow, startPoint + Point(20., 20.));
}
// Calculate the ClickZone for the description and add it.
const Point descriptionDimensions(INFOBAR_WIDTH, descriptionOffset);
const Point descriptionCenter(center.X(), startPoint.Y() + descriptionOffset / 2);
const ClickZone<string> collapseDescription = ClickZone<string>(
descriptionCenter, descriptionDimensions, DESCRIPTION);
categoryZones.emplace_back(collapseDescription);
}
const Point attributesPoint(startPoint.X(), startPoint.Y() + descriptionOffset);
const Point outfitsPoint(startPoint.X(), attributesPoint.Y() + shipInfo.AttributesHeight());
shipInfo.DrawAttributes(attributesPoint);
shipInfo.DrawOutfits(outfitsPoint);
heightOffset = outfitsPoint.Y() + shipInfo.OutfitsHeight();
}
// Draw this string representing the selected ship (if any), centered in the details side panel
const Color &bright = *GameData::Colors().Get("bright");
const Point selectedPoint(center.X() - INFOBAR_WIDTH / 2, center.Y());
font.Draw({selectedItem, {INFOBAR_WIDTH, Alignment::CENTER, Truncate::MIDDLE}},
selectedPoint, bright);
return heightOffset;
}
ShopPanel::BuyResult ShipyardPanel::CanBuy(bool onlyOwned) const
{
if(!selectedShip)
return false;
int64_t cost = player.StockDepreciation().Value(*selectedShip, day);
// Check that the player has any necessary licenses.
int64_t licenseCost = LicenseCost(&selectedShip->Attributes());
if(licenseCost < 0)
return "Buying this ship requires a special license. "
"You will probably need to complete some sort of mission to get one.";
// Check if the player can't pay.
cost += licenseCost;
if(player.Accounts().Credits() < cost)
{
// Check if ships could be sold to pay for the new ship.
for(const auto &it : player.Ships())
cost -= player.FleetDepreciation().Value(*it, day);
if(player.Accounts().Credits() >= cost)
{
string ship = (player.Ships().size() == 1) ? "your current ship" : "some of your ships";
return "You do not have enough credits to buy this ship. "
"If you want to buy it, you must sell " + ship + " first.";
}
// Check if the license cost is the tipping point.
if(player.Accounts().Credits() >= cost - licenseCost)
return "You do not have enough credits to buy this ship, "
"because it will cost you an extra " + Format::Credits(licenseCost) +
" credits to buy the necessary licenses. "
"Consider checking if the bank will offer you a loan.";
return "You do not have enough credits to buy this ship. "
"Consider checking if the bank will offer you a loan.";
}
return true;
}
void ShipyardPanel::Buy(bool onlyOwned)
{
int64_t licenseCost = LicenseCost(&selectedShip->Attributes());
if(licenseCost < 0)
return;
modifier = Modifier();
string message;
if(licenseCost)
message = "Note: you will need to pay " + Format::CreditString(licenseCost)
+ " for the licenses required to operate this ship, in addition to its cost."
" If that is okay with you, go ahead and enter a name for your brand new ";
else
message = "Enter a name for your brand new ";
if(modifier == 1)
message += selectedShip->DisplayModelName() + "! (Or leave it blank to use a randomly chosen name.)";
else
message += selectedShip->PluralModelName() + "! (Or leave it blank to use randomly chosen names.)";
GetUI()->Push(new ShipNameDialog(this, &ShipyardPanel::BuyShip, message));
}
bool ShipyardPanel::CanSell(bool toStorage) const
{
return playerShip;
}
void ShipyardPanel::Sell(bool toStorage)
{
static const int MAX_LIST = 20;
int count = playerShips.size();
int initialCount = count;
string message;
if(!toStorage)
message = "Sell the ";
else if(count == 1)
message = "Sell the hull of the ";
else
message = "Sell the hulls of the ";
if(count == 1)
message += playerShip->GivenName();
else if(count <= MAX_LIST)
{
auto it = playerShips.begin();
message += (*it++)->GivenName();
--count;
if(count == 1)
message += " and ";
else
{
while(count-- > 1)
message += ",\n" + (*it++)->GivenName();
message += ",\nand ";
}
message += (*it)->GivenName();
}
else
{
auto it = playerShips.begin();
message += (*it++)->GivenName() + ",\n";
for(int i = 1; i < MAX_LIST - 1; ++i)
message += (*it++)->GivenName() + ",\n";
message += "and " + to_string(count - (MAX_LIST - 1)) + " other ships";
}
// To allow calculating the sale price of all the ships in the list,
// temporarily copy into a shared_ptr vector:
vector<shared_ptr<Ship>> toSell;
for(const auto &it : playerShips)
toSell.push_back(it->shared_from_this());
int64_t total = player.FleetDepreciation().Value(toSell, day, toStorage);
message += ((initialCount > 2) ? "\nfor " : " for ") + Format::CreditString(total) + "?";
if(toStorage)
{
message += " Any outfits will be placed in storage.";
GetUI()->Push(new Dialog(this, &ShipyardPanel::SellShipChassis, message, Truncate::MIDDLE));
}
else
GetUI()->Push(new Dialog(this, &ShipyardPanel::SellShipAndOutfits, message, Truncate::MIDDLE));
}
bool ShipyardPanel::CanSellMultiple() const
{
return false;
}
void ShipyardPanel::BuyShip(const string &name)
{
int64_t licenseCost = LicenseCost(&selectedShip->Attributes());
if(licenseCost)
{
player.Accounts().AddCredits(-licenseCost);
for(const string &licenseName : selectedShip->Attributes().Licenses())
if(!player.HasLicense(licenseName))
player.AddLicense(licenseName);
}
for(int i = 1; i <= modifier; ++i)
{
// If no name is given, choose a random name. Otherwise, if buying
// multiple ships, append a number to the given ship name.
string shipName = name;
if(name.empty())
shipName = GameData::Phrases().Get("civilian")->Get();
else if(modifier > 1)
shipName += " " + to_string(i);
player.BuyShip(selectedShip, shipName);
}
playerShip = &*player.Ships().back();
playerShips.clear();
playerShips.insert(playerShip);
CheckSelection();
}
void ShipyardPanel::SellShipAndOutfits()
{
SellShip(false);
}
void ShipyardPanel::SellShipChassis()
{
SellShip(true);
}
void ShipyardPanel::SellShip(bool toStorage)
{
for(Ship *ship : playerShips)
player.SellShip(ship, toStorage);
playerShips.clear();
playerShip = nullptr;
for(const shared_ptr<Ship> &ship : player.Ships())
if(ship->GetSystem() == player.GetSystem() && !ship->IsDisabled())
{
playerShip = ship.get();
break;
}
if(playerShip)
playerShips.insert(playerShip);
}
int ShipyardPanel::FindItem(const string &text) const
{
int bestIndex = 9999;
int bestItem = -1;
auto it = zones.begin();
for(unsigned int i = 0; i < zones.size(); ++i, ++it)
{
const Ship *ship = it->GetShip();
int index = Format::Search(ship->DisplayModelName(), text);
if(index >= 0 && index < bestIndex)
{
bestIndex = index;
bestItem = i;
if(!index)
return i;
}
}
return bestItem;
}
|