Skip to content

Commit

Permalink
Implement Drive and Afterburner
Browse files Browse the repository at this point in the history
Also DriveUpgrade and AfterburnerUpgrade
Move limits from Computer to Drive and Afterburner
Get rid of fuzzy numbers. MJ is exactly x100 game numbers in ship info.
Modify fuel efficiency in both ways to reduce tons on ship and increase actual usage.

A note on clamps in movable.cpp: these essentially check something is within min/max. With the resource class, most of these are now redundant.

Issues:
- Ship damage model is very severe on drives.
  • Loading branch information
royfalk committed Oct 21, 2024
1 parent 105c9f9 commit aadb810
Show file tree
Hide file tree
Showing 47 changed files with 1,820 additions and 830 deletions.
7 changes: 7 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -725,13 +725,18 @@ SET(LIBPYTHON

SET(LIBCOMPONENT
src/components/component.cpp
src/components/dummy_component.cpp
src/components/component_utils

src/components/energy_consumer.cpp
src/components/energy_container.cpp
src/components/reactor.cpp

src/components/afterburner.cpp
src/components/afterburner_upgrade.cpp
src/components/cloak.cpp
src/components/drive.cpp
src/components/drive_upgrade.cpp
src/components/ftl_drive.cpp
src/components/jump_drive.cpp
)
Expand Down Expand Up @@ -1749,6 +1754,8 @@ IF (USE_GTEST)
src/exit_unit_tests.cpp
src/components/tests/energy_container_tests.cpp
src/components/tests/balancing_tests.cpp
src/components/tests/drive_tests.cpp
src/components/tests/afterburner_tests.cpp
)

ADD_LIBRARY(vegastrike-testing
Expand Down
4 changes: 2 additions & 2 deletions engine/src/cmd/ai/aggressive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ bool AggressiveAI::ProcessLogicItem(const AIEvents::AIEvresult &item) {
value = (pdmag - parent->rSize() - targ->rSize());
float myvel = PosDifference.Dot(parent->GetVelocity() - targ->GetVelocity()) / value; ///pdmag;
if (myvel > 0) {
value -= myvel * myvel / (2 * (parent->limits.retro / parent->getMass()));
value -= myvel * myvel / (2 * (parent->drive.retro / parent->getMass()));

Check failure

Code scanning / CodeQL

Multiplication result converted to larger type High

Multiplication result may overflow 'float' before it is converted to 'double'.
}
} else {
value = 10000;
Expand Down Expand Up @@ -1727,7 +1727,7 @@ void AggressiveAI::Execute() {
mag = 1 / mag;
}
parent->SetVelocity(
parent->GetVelocity() * (mag * parent->GetComputerData().max_speed() / getTimeCompression()));
parent->GetVelocity() * (mag * parent->MaxSpeed() / getTimeCompression()));
parent->NetLocalForce = parent->NetForce = Vector(0, 0, 0);
}
target = parent->Target();
Expand Down
4 changes: 2 additions & 2 deletions engine/src/cmd/ai/fire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ bool FireAt::PursueTarget(Unit *un, bool leader) {
bool CanFaceTarget(Unit *su, Unit *targ, const Matrix &matrix) {
return true;

float limitmin = su->limits.limitmin;
float limitmin = su->limit_min;
if (limitmin > -.99) {
QVector pos = (targ->Position() - su->Position()).Normalize();
QVector pnorm = pos.Cast();
Vector structurelimits = su->limits.structurelimits;
Vector structurelimits = su->structure_limits;
Vector worldlimit = TransformNormal(matrix, structurelimits);
if (pnorm.Dot(worldlimit) < limitmin) {
return false;
Expand Down
48 changes: 24 additions & 24 deletions engine/src/cmd/ai/flybywire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,16 @@ FlyByWire::FlyByWire() : MatchVelocity(Vector(0, 0, 0), Vector(0, 0, 0), true, f
}

void FlyByWire::Stop(float per) {
SetDesiredVelocity(Vector(0, 0, per * parent->GetComputerData().max_speed()), true);
SetDesiredVelocity(Vector(0, 0, per * parent->MaxSpeed()), true);

parent->GetComputerData().set_speed = per * parent->GetComputerData().max_speed();
parent->GetComputerData().set_speed = per * parent->MaxSpeed();
}

void FlyByWire::Right(float per) {
desired_ang_velocity +=
(-per
* (per
> 0 ? parent->GetComputerData().max_yaw_left : parent->GetComputerData().max_yaw_right)
> 0 ? parent->drive.max_yaw_left : parent->drive.max_yaw_right)
/ getTimeCompression()) * Vector(
0,
1,
Expand All @@ -212,7 +212,7 @@ void FlyByWire::Up(float per) {
desired_ang_velocity +=
(-per
* (per
> 0 ? parent->GetComputerData().max_pitch_down : parent->GetComputerData().max_pitch_up)
> 0 ? parent->drive.max_pitch_down : parent->drive.max_pitch_up)
/ getTimeCompression()) * Vector(
1,
0,
Expand All @@ -223,7 +223,7 @@ void FlyByWire::RollRight(float per) {
desired_ang_velocity +=
(-per
* (per
> 0 ? parent->GetComputerData().max_roll_left : parent->GetComputerData().max_roll_right)
> 0 ? parent->drive.max_roll_left : parent->drive.max_roll_right)
/ getTimeCompression()) * Vector(
0,
0,
Expand All @@ -235,9 +235,9 @@ void FlyByWire::Afterburn(float per) {

afterburn = (per > .1);
if (!sheltonslide && !inertial_flight_model) {
desired_velocity = Vector(0, 0, cpu->set_speed + per * (cpu->max_ab_speed() - cpu->set_speed));
desired_velocity = Vector(0, 0, cpu->set_speed + per * (parent->MaxAfterburnerSpeed() - cpu->set_speed));
} else if (inertial_flight_model) {
DirectThrust += Vector(0, 0, parent->limits.afterburn * per);
DirectThrust += Vector(0, 0, parent->afterburner.thrust * per);
}
if (parent == _Universe->AccessCockpit()->GetParent()) {
//printf("afterburn is %d\n",afterburn); // DELETEME WTF all this force feedback code and its unused.
Expand All @@ -254,22 +254,22 @@ void FlyByWire::MatchSpeed(const Vector &vec) {
Computer *cpu = &parent->GetComputerData();

cpu->set_speed = (vec).Magnitude();
if (cpu->set_speed > cpu->max_speed()) {
cpu->set_speed = cpu->max_speed();
if (cpu->set_speed > parent->MaxSpeed()) {
cpu->set_speed = parent->MaxSpeed();
}
}

void FlyByWire::Accel(float per) {
Computer *cpu = &parent->GetComputerData();

cpu->set_speed += per * cpu->max_speed() * simulation_atom_var; //SIMULATION_ATOM?
if (cpu->set_speed > cpu->max_speed()) {
cpu->set_speed = cpu->max_speed();
cpu->set_speed += per * parent->MaxSpeed() * simulation_atom_var; //SIMULATION_ATOM?
if (cpu->set_speed > parent->MaxSpeed()) {
cpu->set_speed = parent->MaxSpeed();
}
static float reverse_speed_limit =
XMLSupport::parse_float(vs_config->getVariable("physics", "reverse_speed_limit", "1.0"));
if (cpu->set_speed < -cpu->max_speed() * reverse_speed_limit) {
cpu->set_speed = -cpu->max_speed() * reverse_speed_limit;
if (cpu->set_speed < -parent->MaxSpeed() * reverse_speed_limit) {
cpu->set_speed = -parent->MaxSpeed() * reverse_speed_limit;
}
afterburn = false;

Expand All @@ -279,30 +279,30 @@ void FlyByWire::Accel(float per) {
#define FBWABS(m) (m >= 0 ? m : -m)

void FlyByWire::ThrustRight(float percent) {
DesiredShiftVelocity.i = parent->GetComputerData().max_speed() * percent;
DesiredShiftVelocity.i = parent->MaxSpeed() * percent;
}

void FlyByWire::ThrustUp(float percent) {
DesiredShiftVelocity.j = parent->GetComputerData().max_speed() * percent;
DesiredShiftVelocity.j = parent->MaxSpeed() * percent;
}

void FlyByWire::ThrustFront(float percent) {
DesiredShiftVelocity.k = parent->GetComputerData().max_speed() * percent;
DesiredShiftVelocity.k = parent->MaxSpeed() * percent;
}

void FlyByWire::DirectThrustRight(float percent) {
DirectThrust.i = parent->limits.lateral * percent;
DirectThrust.i = parent->drive.lateral * percent;
}

void FlyByWire::DirectThrustUp(float percent) {
DirectThrust.j = parent->limits.vertical * percent;
DirectThrust.j = parent->drive.vertical * percent;
}

void FlyByWire::DirectThrustFront(float percent) {
if (percent > 0) {
DirectThrust.k = parent->limits.forward * percent;
DirectThrust.k = parent->drive.forward * percent;
} else {
DirectThrust.k = parent->limits.retro * percent;
DirectThrust.k = parent->drive.retro * percent;
}
}

Expand All @@ -312,8 +312,8 @@ void FlyByWire::Execute() {
if (!inertial_flight_model) {
//Must translate the thrust values to velocities, which is somewhat cumbersome.
Vector Limit(
parent->limits.lateral, parent->limits.vertical,
((DirectThrust.k > 0) ? parent->limits.forward : parent->limits.retro)
parent->drive.lateral, parent->drive.vertical,
((DirectThrust.k > 0) ? parent->drive.forward : parent->drive.retro)
);
if (Limit.i <= 1) {
Limit.i = 1;
Expand All @@ -330,7 +330,7 @@ void FlyByWire::Execute() {
DirectThrust.k / Limit.k
);
//Now, scale so that maximum shift velocity is max_speed
DesiredDrift *= parent->GetComputerData().max_speed();
DesiredDrift *= parent->MaxSpeed();
//And apply
DesiredShiftVelocity += DesiredDrift;
}
Expand Down
2 changes: 1 addition & 1 deletion engine/src/cmd/ai/flyjoystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void FlyByJoystick::Execute() {
float(expamountc * (exp(expfactorc * axis_value) - 1) / norm
+ pamountc * pow(axis_value, pfactorc));
}
cpu->set_speed = axis_value * cpu->max_speed();
cpu->set_speed = axis_value * parent->MaxSpeed();
desired_velocity = Vector(0, 0, cpu->set_speed);
}
}
Expand Down
28 changes: 13 additions & 15 deletions engine/src/cmd/ai/hard_coded_scripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void BarrelRoll(Order *aisc, Unit *un) {
bool afterburn = useAfterburner();
broll->MatchSpeed(Vector(0,
0,
afterburn ? un->GetComputerData().max_ab_speed() : un->GetComputerData().max_speed()));
afterburn ? un->MaxAfterburnerSpeed() : un->MaxSpeed()));
broll->Afterburn(afterburn);
}

Expand All @@ -246,7 +246,7 @@ static void EvadeWavy(Order *aisc, Unit *un, bool updown, bool ab) {
bool afterburn = ab && useAfterburner();
broll->MatchSpeed(Vector(0,
0,
afterburn ? un->GetComputerData().max_ab_speed() : un->GetComputerData().max_speed()));
afterburn ? un->MaxAfterburnerSpeed() : un->MaxSpeed()));
broll->Afterburn(afterburn);
}

Expand Down Expand Up @@ -330,11 +330,10 @@ class LoopAround : public Orders::FaceTargetITTS {
Vector r = targ->cumulative_transformation_matrix.getR();
bool afterburn = useAfterburner() && this->afterburn;
bool ab_needed =
force_afterburn || targ->GetVelocity().MagnitudeSquared() > parent->GetComputerData().max_speed();
force_afterburn || targ->GetVelocity().MagnitudeSquared() > parent->MaxSpeed();
m.SetDesiredVelocity(Vector(0, 0, afterburn
&& ab_needed ? parent->GetComputerData().max_ab_speed()
: parent->GetComputerData().
max_speed()), true);
&& ab_needed ? parent->MaxAfterburnerSpeed()
: parent->MaxSpeed()), true);
float spseed, grange = 0, mrange = 0;
parent->getAverageGunSpeed(spseed, grange, mrange);
if (r.Dot(relloc) < 0) {
Expand Down Expand Up @@ -429,7 +428,7 @@ class LoopAroundAgro : public Orders::FaceTargetITTS {
Vector r = targ->cumulative_transformation_matrix.getR();
bool afterburn = useAfterburner() && this->afterburn;
bool ab_needed =
force_afterburn || targ->GetVelocity().MagnitudeSquared() > parent->GetComputerData().max_speed();
force_afterburn || targ->GetVelocity().MagnitudeSquared() > parent->MaxSpeed();
if (r.Dot(relloc) < 0) {
FaceTargetITTS::Execute();
m.SetAfterburn(afterburn && ab_needed);
Expand Down Expand Up @@ -530,11 +529,10 @@ class FacePerpendicular : public Orders::FaceTargetITTS {
Vector r = targ->cumulative_transformation_matrix.getR();
bool afterburn = useAfterburner() && this->afterburn;
bool ab_needed =
force_afterburn || targ->GetVelocity().MagnitudeSquared() > parent->GetComputerData().max_speed();
force_afterburn || targ->GetVelocity().MagnitudeSquared() > parent->MaxSpeed();
m.SetDesiredVelocity(Vector(0, 0, afterburn
&& ab_needed ? parent->GetComputerData().max_ab_speed()
: parent->GetComputerData().
max_speed()), true);
&& ab_needed ? parent->MaxAfterburnerSpeed()
: parent->MaxSpeed()), true);
float speed, grange = 0, mrange = 0;
parent->getAverageGunSpeed(speed, grange, mrange);
if (r.Dot(relloc) < 0) {
Expand Down Expand Up @@ -585,15 +583,15 @@ void RollLeft(Order *aisc, Unit *un) {
if (un->aistate) {
AddOrd(un->aistate,
un,
new Orders::ExecuteFor(new Orders::MatchRoll(un->GetComputerData().max_roll_right, false), 1.0f));
new Orders::ExecuteFor(new Orders::MatchRoll(un->drive.max_roll_right, false), 1.0f));
}
}

void RollRight(Order *aisc, Unit *un) {
if (un->aistate) {
AddOrd(un->aistate,
un,
new Orders::ExecuteFor(new Orders::MatchRoll(-un->GetComputerData().max_roll_left, false), 1.0f));
new Orders::ExecuteFor(new Orders::MatchRoll(-un->drive.max_roll_left, false), 1.0f));
}
}

Expand All @@ -602,7 +600,7 @@ void RollLeftHard(Order *aisc, Unit *un) {
if (un->aistate) {
AddOrd(un->aistate,
un,
new Orders::ExecuteFor(new Orders::MatchRoll(un->GetComputerData().max_roll_right, false), durvar));
new Orders::ExecuteFor(new Orders::MatchRoll(un->drive.max_roll_right, false), durvar));
}
}

Expand All @@ -611,7 +609,7 @@ void RollRightHard(Order *aisc, Unit *un) {
if (un->aistate) {
AddOrd(un->aistate,
un,
new Orders::ExecuteFor(new Orders::MatchRoll(-un->GetComputerData().max_roll_left, false), durvar));
new Orders::ExecuteFor(new Orders::MatchRoll(-un->drive.max_roll_left, false), durvar));
}
}

Expand Down
Loading

0 comments on commit aadb810

Please sign in to comment.