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
|
/* Flotsam.cpp
Copyright (c) 2016 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 "Flotsam.h"
#include "Angle.h"
#include "Effect.h"
#include "GameData.h"
#include "Outfit.h"
#include "Random.h"
#include "Ship.h"
#include "image/SpriteSet.h"
#include "Visual.h"
#include <cmath>
using namespace std;
const int Flotsam::TONS_PER_BOX = 5;
// Constructors for flotsam carrying either a commodity or an outfit.
Flotsam::Flotsam(const string &commodity, int count, const Government *sourceGovernment)
: commodity(commodity), count(count), sourceGovernment(sourceGovernment)
{
lifetime = Random::Int(3600) + 7200;
// Scale lifetime in proportion to the expected amount per box.
if(count != TONS_PER_BOX)
lifetime = sqrt(count * (1. / TONS_PER_BOX)) * lifetime;
}
Flotsam::Flotsam(const Outfit *outfit, int count, const Government *sourceGovernment)
: outfit(outfit), count(count), sourceGovernment(sourceGovernment)
{
// The more the outfit costs, the faster this flotsam should disappear.
int lifetimeBase = 3000000000 / (outfit->Cost() * count + 1000000);
lifetime = Random::Int(lifetimeBase) + lifetimeBase + 600;
}
// Place this flotsam, and set the given ship as its source. This is a
// separate function because a ship may queue up flotsam to dump but take
// several frames before it finishes dumping it all.
void Flotsam::Place(const Ship &source)
{
this->source = &source;
Place(source, Angle::Random().Unit() * (2. * Random::Real()) - 2. * source.Unit());
}
// Place this flotsam with its starting position at the specified bay of the source ship,
// instead of the center of the ship.
void Flotsam::Place(const Ship &source, size_t bayIndex)
{
Place(source);
if(source.Bays().size() > bayIndex)
position += source.Facing().Rotate(source.Bays()[bayIndex].point);
}
// Place flotsam coming from something other than a ship. Optionally specify
// the maximum relative velocity, or the exact relative velocity as a vector.
void Flotsam::Place(const Body &source, double maxVelocity)
{
Place(source, Angle::Random().Unit() * (maxVelocity * Random::Real()));
}
void Flotsam::Place(const Body &source, const Point &dv)
{
position = source.Position();
velocity = source.Velocity() + dv;
angle = Angle::Random();
spin = Angle::Random(10.);
// Special case: allow a harvested outfit item to define its flotsam sprite
// using the field that usually defines a secondary weapon's icon.
if(outfit && outfit->FlotsamSprite())
SetSprite(outfit->FlotsamSprite());
else
SetSprite(SpriteSet::Get("effect/box"));
SetFrameRate(4. * (1. + Random::Real()));
}
// Move the object one time-step forward.
void Flotsam::Move(vector<Visual> &visuals)
{
position += velocity;
velocity *= drag;
angle += spin;
--lifetime;
if(lifetime > 0)
return;
// This flotsam has reached the end of its life.
const Effect *effect = GameData::Effects().Get("flotsam death");
for(int i = 0; i < 3; ++i)
{
Angle smokeAngle = Angle::Random();
velocity += smokeAngle.Unit() * Random::Real();
visuals.emplace_back(*effect, position, velocity, smokeAngle);
}
MarkForRemoval();
}
void Flotsam::SetVelocity(const Point &velocity)
{
this->velocity = velocity;
}
// This is the one ship that cannot pick up this flotsam.
const Ship *Flotsam::Source() const
{
return source;
}
// Ships from this Government should not pick up this flotsam because it
// was explicitly dumped by a member of this government.
const Government *Flotsam::SourceGovernment() const
{
return sourceGovernment;
}
// This is what the flotsam contains:
const string &Flotsam::CommodityType() const
{
return commodity;
}
const Outfit *Flotsam::OutfitType() const
{
return outfit;
}
int Flotsam::Count() const
{
return count;
}
// This is how big one "unit" of the flotsam is (in tons). If a ship has
// less than this amount of space, it can't pick up anything here.
double Flotsam::UnitSize() const
{
return outfit ? outfit->Mass() : 1.;
}
double Flotsam::Mass() const
{
return Count() * UnitSize();
}
// Transfer contents to the collector ship. The flotsam velocity is
// stabilized in proportion to the amount being transferred.
int Flotsam::TransferTo(Ship *collector)
{
int amount = outfit ?
collector->Cargo().Add(outfit, count) :
collector->Cargo().Add(commodity, count);
Point relative = collector->Velocity() - velocity;
double proportion = static_cast<double>(amount) / count;
velocity += relative * proportion;
count -= amount;
// If this flotsam is now empty, remove it.
if(count <= 0)
MarkForRemoval();
return amount;
}
|