File: Armament.cpp

package info (click to toggle)
endless-sky 0.10.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 414,608 kB
  • sloc: cpp: 73,435; python: 893; xml: 666; sh: 271; makefile: 28
file content (314 lines) | stat: -rw-r--r-- 7,982 bytes parent folder | download
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
/* Armament.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 "Armament.h"

#include "FireCommand.h"
#include "Logger.h"
#include "Outfit.h"
#include "Ship.h"
#include "Weapon.h"

#include <algorithm>
#include <cmath>
#include <limits>

using namespace std;



// Add a gun hardpoint (fixed-direction weapon).
void Armament::AddGunPort(const Point &point, const Hardpoint::BaseAttributes &attributes,
	bool isUnder, const Outfit *outfit)
{
	hardpoints.emplace_back(point, attributes, false, isUnder, outfit);
}



// Add a turret hardpoint.
void Armament::AddTurret(const Point &point, const Hardpoint::BaseAttributes &attributes,
	bool isUnder, const Outfit *outfit)
{
	hardpoints.emplace_back(point, attributes, true, isUnder, outfit);
}



// This must be called after all the outfit data is loaded. If you add more
// of a given weapon than there are slots for it, the extras will not fire.
// But, the "gun ports" attribute should keep that from happening.
int Armament::Add(const Outfit *outfit, int count)
{
	// Make sure this really is a weapon.
	if(!count || !outfit || !outfit->IsWeapon())
		return 0;

	int existing = 0;
	int added = 0;
	bool isTurret = outfit->Get("turret mounts");
	// Do not equip weapons that do not define how they are mounted.
	if(!isTurret && !outfit->Get("gun ports"))
	{
		Logger::LogError("Error: Skipping unmountable outfit \"" + outfit->TrueName() + "\"."
			" Weapon outfits must specify either \"gun ports\" or \"turret mounts\".");
		return 0;
	}

	// To start out with, check how many instances of this weapon are already
	// installed. If "adding" a negative number of outfits, remove the installed
	// instances until the given number have been removed.
	for(Hardpoint &hardpoint : hardpoints)
	{
		if(hardpoint.GetOutfit() == outfit)
		{
			// If this slot has the given outfit in it and we need to uninstall
			// some of them, uninstall it. Otherwise, remember the fact that one
			// of these outfits is installed.
			if(count < 0)
			{
				hardpoint.Uninstall();
				++count;
				--added;
			}
			else
				++existing;
		}
		else if(!hardpoint.GetOutfit() && hardpoint.IsTurret() == isTurret)
		{
			// If this is an empty, compatible slot, and we're adding outfits,
			// install one of them here and decrease the count of how many we
			// have left to install.
			if(count > 0)
			{
				hardpoint.Install(outfit);
				--count;
				++added;
			}
		}
	}

	// If a stream counter already exists for this outfit (because we did not
	// just add the first one or remove the last one), do nothing.
	if(!existing)
	{
		// If this weapon is streamed, create a stream counter. If it is not
		// streamed, or if the last of this weapon has been uninstalled, erase the
		// stream counter (if there is one).
		if(added > 0 && outfit->IsStreamed())
			streamReload[outfit] = 0;
		else
			streamReload.erase(outfit);
	}
	return added;
}



// Call this once all the outfits have been loaded to make sure they are all
// set up properly (even the ones that were pre-assigned to a hardpoint).
void Armament::FinishLoading()
{
	for(Hardpoint &hardpoint : hardpoints)
		if(hardpoint.GetOutfit())
			hardpoint.Install(hardpoint.GetOutfit());

	ReloadAll();
}



// Reload all weapons (because a day passed in-game).
void Armament::ReloadAll()
{
	streamReload.clear();
	for(Hardpoint &hardpoint : hardpoints)
		if(hardpoint.GetOutfit())
		{
			hardpoint.Reload();

			// If this weapon is streamed, create a stream counter.
			const Outfit *outfit = hardpoint.GetOutfit();
			if(outfit->IsStreamed())
				streamReload[outfit] = 0;
		}
}



// Uninstall all weapons (because the weapon outfits have potentially changed).
void Armament::UninstallAll()
{
	for(Hardpoint &hardpoint : hardpoints)
		hardpoint.Uninstall();
}



// Swap the weapons in the given two hardpoints.
void Armament::Swap(unsigned first, unsigned second)
{
	// Make sure both of the given indices are in range, and that both slots are
	// the same type (gun vs. turret).
	if(first >= hardpoints.size())
		return;
	if(second >= hardpoints.size())
		return;
	if(hardpoints[first].IsTurret() != hardpoints[second].IsTurret())
		return;

	// Swap the weapons in the two hardpoints.
	const Outfit *outfit = hardpoints[first].GetOutfit();
	hardpoints[first].Install(hardpoints[second].GetOutfit());
	hardpoints[second].Install(outfit);
}



// Access the array of weapon hardpoints.
const vector<Hardpoint> &Armament::Get() const
{
	return hardpoints;
}



// Determine how many fixed gun hardpoints are on this ship.
int Armament::GunCount() const
{
	return hardpoints.size() - TurretCount();
}



// Determine how many turret hardpoints are on this ship.
int Armament::TurretCount() const
{
	int count = 0;
	for(const Hardpoint &hardpoint : hardpoints)
		count += hardpoint.IsTurret();
	return count;
}



// Determine the installed weaponry's reusable ammunition. That is, all ammo outfits that are not also
// weapons (as then they would be installed on hardpoints, like the "Nuclear Missile" and other one-shots).
set<const Outfit *> Armament::RestockableAmmo() const
{
	auto restockable = set<const Outfit *>{};
	for(const Hardpoint &hardpoint : hardpoints)
	{
		const Weapon *weapon = hardpoint.GetOutfit();
		if(weapon)
		{
			const Outfit *ammo = weapon->Ammo();
			if(ammo && !ammo->IsWeapon())
				restockable.emplace(ammo);
		}
	}
	return restockable;
}



// Adjust the aim of the turrets.
void Armament::Aim(const Ship &ship, const FireCommand &command)
{
	for(unsigned i = 0; i < hardpoints.size(); ++i)
		hardpoints[i].Aim(ship, command.Aim(i));
}



// Fire the given weapon, if it is ready.
void Armament::Fire(unsigned index, Ship &ship, vector<Projectile> &projectiles, vector<Visual> &visuals, bool jammed)
{
	// Don't check if the hardpoint jammed here, as the weapon may not even
	// attempt to fire due to stream reloading.
	if(!CheckHardpoint(index))
		return;

	// A weapon that has already started a burst ignores stream timing.
	if(!hardpoints[index].WasFiring())
	{
		auto it = streamReload.find(hardpoints[index].GetOutfit());
		if(it != streamReload.end())
		{
			if(it->second > 0)
				return;
			it->second += it->first->Reload() * hardpoints[index].BurstRemaining();
		}
	}
	if(jammed)
		hardpoints[index].Jam();
	else
		hardpoints[index].Fire(ship, projectiles, visuals);
}



bool Armament::FireAntiMissile(unsigned index, Ship &ship, const Projectile &projectile,
	vector<Visual> &visuals, bool jammed)
{
	if(!CheckHardpoint(index, jammed))
		return false;

	return hardpoints[index].FireAntiMissile(ship, projectile, visuals);
}



bool Armament::FireTractorBeam(unsigned index, Ship &ship, const Flotsam &flotsam,
	vector<Visual> &visuals, bool jammed)
{
	if(!CheckHardpoint(index, jammed))
		return false;

	return hardpoints[index].FireTractorBeam(ship, flotsam, visuals);
}



// Update the reload counters.
void Armament::Step(const Ship &ship)
{
	for(Hardpoint &hardpoint : hardpoints)
		hardpoint.Step();

	for(auto &it : streamReload)
	{
		int count = ship.OutfitCount(it.first);
		it.second -= count;
		// Always reload to the quickest firing interval.
		it.second = max(it.second, 1 - count);
	}
}



bool Armament::CheckHardpoint(unsigned index, bool jammed)
{
	if(index >= hardpoints.size() || !hardpoints[index].IsReady())
		return false;

	if(jammed)
	{
		hardpoints[index].Jam();
		return false;
	}

	return true;
}