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
|
/* FireCommand.cpp
Copyright (c) 2021 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 "FireCommand.h"
#include <cassert>
#include <cmath>
#include <vector>
using namespace std;
namespace {
template<typename T>
void SubsetAssign(std::vector<T> &lhs, const std::vector<T> &rhs) noexcept
{
const auto size = lhs.size() < rhs.size() ? lhs.size() : rhs.size();
for(size_t i = 0; i < size; ++i)
lhs[i] = rhs[i];
}
}
// Sets the specified amount of hardpoints desired.
void FireCommand::SetHardpoints(size_t count)
{
Clear();
weapon.Resize(count);
aim.resize(count);
assert(aim.size() == count && "aim size must match the requested count");
assert(weapon.Size() >= aim.size() && "weapon bits must be at least as big as the aim bits");
}
// Assigns the subset of other to this class that is no larger than
// this command's hardpoint size.
void FireCommand::UpdateWith(const FireCommand &other) noexcept
{
weapon.UpdateWith(other.weapon);
SubsetAssign(aim, other.aim);
}
// Reset this to an empty command.
void FireCommand::Clear()
{
weapon.Reset();
for(auto &it : aim)
it = '\0';
}
// Check if this command includes a command to fire the given weapon.
bool FireCommand::HasFire(int index) const noexcept
{
if(!IsIndexValid(index))
return false;
return weapon.Test(index);
}
// Add to this set of commands a command to fire the given weapon.
void FireCommand::SetFire(int index) noexcept
{
if(!IsIndexValid(index))
return;
weapon.Set(index);
}
// Check if any weapons are firing.
bool FireCommand::IsFiring() const noexcept
{
return weapon.Any();
}
// Gets the current turn rate of the turret at the given weapon index.
double FireCommand::Aim(int index) const noexcept
{
if(!IsIndexValid(index))
return 0;
return aim[index] / 127.;
}
// Set the turn rate of the turret with the given weapon index. A value of
// -1 or 1 means to turn at the full speed the turret is capable of.
void FireCommand::SetAim(int index, double amount) noexcept
{
if(!IsIndexValid(index))
return;
aim[index] = round(127. * max(-1., min(1., amount)));
}
bool FireCommand::IsIndexValid(int index) const noexcept
{
return index >= 0 && static_cast<size_t>(index) < aim.size();
}
|