Skip to content
Navigation Menu
{{ message }}
forked from endless-sky/endless-sky
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHardpoint.cpp
More file actions
319 lines (233 loc) · 8.39 KB
/
Copy pathHardpoint.cpp
File metadata and controls
319 lines (233 loc) · 8.39 KB
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
/* Hardpoint.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.
*/
#include "Hardpoint.h"
#include "Audio.h"
#include "Effect.h"
#include "Outfit.h"
#include "pi.h"
#include "Projectile.h"
#include "Random.h"
#include "Ship.h"
#include "Visual.h"
#include <cmath>
#include <map>
using namespace std;
namespace {
// Create all the effects in the given list, at the given location, velocity, and angle.
void CreateEffects(const map<const Effect *, int> &m, Point pos, Point vel, Angle angle, vector<Visual> &visuals)
{
for(const auto &it : m)
for(int i = 0; i < it.second; ++i)
visuals.emplace_back(*it.first, pos, vel, angle);
}
}
// Constructor.
Hardpoint::Hardpoint(const Point &point, bool isTurret, const Outfit *outfit)
: outfit(outfit), point(point * .5), isTurret(isTurret)
{
}
// Get the weapon in this hardpoint. This returns null if there is none.
const Outfit *Hardpoint::GetOutfit() const
{
return outfit;
}
// Get the location, relative to the center of the ship, from which
// projectiles of this weapon should originate.
const Point &Hardpoint::GetPoint() const
{
return point;
}
// Get the convergence angle adjustment of this weapon (guns only, not turrets).
const Angle &Hardpoint::GetAngle() const
{
return angle;
}
// Get the angle this weapon ought to point at for ideal gun harmonization.
Angle Hardpoint::HarmonizedAngle() const
{
if(!outfit)
return Angle();
// Find the point of convergence of shots fired from this gun. That is,
// find the angle where the projectile's X offset will be zero when it
// reaches the very end of its range.
double d = outfit->Range();
// Projectiles with a range of zero should fire straight forward. A
// special check is needed to avoid divide by zero errors.
return Angle(d <= 0. ? 0. : -asin(point.X() / d) * TO_DEG);
}
// Find out if this is a turret hardpoint (whether or not it has a turret installed).
bool Hardpoint::IsTurret() const
{
return isTurret;
}
// Find out if this hardpoint has a homing weapon installed.
bool Hardpoint::IsHoming() const
{
return outfit && outfit->Homing();
}
// Find out if this hardpoint has an anti-missile installed.
bool Hardpoint::IsAntiMissile() const
{
return outfit && outfit->AntiMissile() > 0;
}
bool Hardpoint::CanAim() const
{
return outfit && outfit->TurretTurn();
}
// Check if this weapon is ready to fire.
bool Hardpoint::IsReady() const
{
return outfit && burstReload <= 0. && burstCount;
}
// Check if this weapon fired the last time it was able to fire. This is to
// figure out if the stream spacing timer should be applied or not.
bool Hardpoint::WasFiring() const
{
return wasFiring;
}
// Get the number of remaining burst shots before a full reload is required.
int Hardpoint::BurstRemaining() const
{
return burstCount;
}
// Perform one step (i.e. decrement the reload count).
void Hardpoint::Step()
{
if(!outfit)
return;
wasFiring = isFiring;
if(reload > 0.)
--reload;
// If the full reload time is elapsed, reset the burst counter.
if(reload <= 0.)
burstCount = outfit->BurstCount();
if(burstReload > 0.)
--burstReload;
// If the burst reload time has elapsed, this weapon will not count as firing
// continuously if it is not fired this frame.
if(burstReload <= 0.)
isFiring = false;
}
// Adjust this weapon's aim by the given amount, relative to its maximum
// "turret turn" rate.
void Hardpoint::Aim(double amount)
{
if(!outfit)
return;
angle += outfit->TurretTurn() * amount;
}
// Fire this weapon. If it is a turret, it automatically points toward
// the given ship's target. If the weapon requires ammunition, it will
// be subtracted from the given ship.
void Hardpoint::Fire(Ship &ship, vector<Projectile> &projectiles, vector<Visual> &visuals)
{
// Since this is only called internally by Armament (no one else has non-
// const access), assume Armament checked that this is a valid call.
Angle aim = ship.Facing();
// Get projectiles to start at the right position. They are drawn at an
// offset of (.5 * velocity) and that velocity includes the velocity of the
// ship that fired them.
Point start = ship.Position() + aim.Rotate(point) - .5 * ship.Velocity();
// Apply the aim and hardpoint offset.
aim += angle;
start += aim.Rotate(outfit->HardpointOffset());
// Create a new projectile, originating from this hardpoint.
projectiles.emplace_back(ship, start, aim, outfit);
// Create any effects this weapon creates when it is fired.
CreateEffects(outfit->FireEffects(), start, ship.Velocity(), aim, visuals);
// Update the reload and burst counters, and expend ammunition if applicable.
Fire(ship, start, aim);
}
// Fire an anti-missile. Returns true if the missile should be killed.
bool Hardpoint::FireAntiMissile(Ship &ship, const Projectile &projectile, vector<Visual> &visuals)
{
// Make sure this hardpoint really is an anti-missile.
int strength = outfit->AntiMissile();
if(!strength)
return false;
// Get the anti-missile range. Anti-missile shots always last a single frame,
// so their range is equal to their velocity.
double range = outfit->Velocity();
// Check if the missile is within range of this hardpoint.
Point start = ship.Position() + ship.Facing().Rotate(point);
Point offset = projectile.Position() - start;
if(offset.Length() > range)
return false;
// Firing effects are displayed at the anti-missile hardpoint that just fired.
Angle aim(offset);
angle = aim - ship.Facing();
start += aim.Rotate(outfit->HardpointOffset());
CreateEffects(outfit->FireEffects(), start, ship.Velocity(), aim, visuals);
// Figure out where the effect should be placed. Anti-missiles do not create
// projectiles; they just create a blast animation.
CreateEffects(outfit->HitEffects(), start + (.5 * range) * aim.Unit(), ship.Velocity(), aim, visuals);
// Die effects are displayed at the projectile, whether or not it actually "dies."
CreateEffects(outfit->DieEffects(), projectile.Position(), projectile.Velocity(), aim, visuals);
// Update the reload and burst counters, and expend ammunition if applicable.
Fire(ship, start, aim);
// Check whether the missile was destroyed.
return (Random::Int(strength) > Random::Int(projectile.MissileStrength()));
}
// Install a weapon here (assuming it is empty). This is only for
// Armament to call internally.
void Hardpoint::Install(const Outfit *outfit)
{
// If the given outfit is not a valid weapon, this hardpoint becomes empty.
// Also check that the type of the weapon (gun or turret) is right.
if(!outfit || !outfit->IsWeapon() || (isTurret == !outfit->Get("turret mounts")))
Uninstall();
else
{
// Reset all the reload counters.
this->outfit = outfit;
Reload();
// For fixed weapons, apply "gun harmonization," pointing them slightly
// inward so the projectiles will converge. For turrets, start them out
// pointing outward from the center of the ship.
if(!isTurret)
angle = HarmonizedAngle();
else
angle = Angle(point);
}
}
// Reload this weapon.
void Hardpoint::Reload()
{
reload = 0.;
burstReload = 0.;
burstCount = outfit ? outfit->BurstCount() : 0;
}
// Uninstall the outfit from this port (if it has one).
void Hardpoint::Uninstall()
{
outfit = nullptr;
}
// Update any counters that change when this projectile fires.
void Hardpoint::Fire(Ship &ship, const Point &start, const Angle &aim)
{
// Since this is only called internally, it is safe to assume that the
// outfit pointer is not null.
// Reset the reload count.
reload += outfit->Reload();
burstReload += outfit->BurstReload();
--burstCount;
isFiring = true;
// Anti-missile sounds can be specified either in the outfit itself or in
// the effect they create.
if(outfit->WeaponSound())
Audio::Play(outfit->WeaponSound(), start);
// Apply any "kick" from firing this weapon.
double force = outfit->FiringForce();
if(force)
ship.ApplyForce(aim.Unit() * -force);
// Expend any ammo that this weapon uses. Do this as the very last thing, in
// case the outfit is its own ammunition.
ship.ExpendAmmo(outfit);
}
You can’t perform that action at this time.
