1
0
Fork 0

AIShip: Maintenance

default dtor
YawTo() seems to be unused
ensure all members are initialized
_path and _elevation_m were hiding parent members
config.h is not used

TODO: string compare not via strcmp()
TODO: remove magic number
This commit is contained in:
Scott Giese 2022-01-15 14:06:39 -06:00
parent e1d35b26d5
commit 8ba35b2553
2 changed files with 87 additions and 93 deletions

View file

@ -17,10 +17,6 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <cmath>
#ifdef _MSC_VER
@ -43,47 +39,40 @@
using std::string;
FGAIShip::FGAIShip(object_type ot) :
// allow HOT to be enabled
FGAIBase(ot, true),
_waiting(false),
_new_waypoint(true),
_tunnel(false),
_initial_tunnel(false),
_restart(false),
_hdg_constant(0.01),
_limit(100),
_elevation_m(0),
_elevation_ft(0),
_tow_angle(0),
_missed_count(0),
_wp_range(0),
_dt_count(0),
_next_run(0),
_roll_constant(0.001),
_roll_factor(-0.0083335),
_old_range(0),
_range_rate(0),
_missed_time_sec(30),
_day(86400),
_lead_angle(0),
_xtrack_error(0),
_curr_alt(0),
_prev_alt(0),
_until_time(""),
_fp_init(false),
_missed(false)
FGAIShip::FGAIShip(object_type ot) : // allow HOT to be enabled
FGAIBase(ot, true),
_waiting(false),
_new_waypoint(true),
_tunnel(false),
_initial_tunnel(false),
_restart(false),
_hdg_constant(0.01),
_limit(100),
_elevation_ft(0),
_tow_angle(0),
_missed_count(0),
_wp_range(0),
_dt_count(0),
_next_run(0),
_roll_constant(0.001),
_roll_factor(-0.0083335),
_old_range(0),
_range_rate(0),
_missed_time_sec(30),
_day(86400),
_lead_angle(0),
_xtrack_error(0),
_curr_alt(0),
_prev_alt(0),
_until_time(""),
_fp_init(false),
_missed(false)
{
invisible = false;
}
FGAIShip::~FGAIShip() {
_elevation_m = 0.0;
invisible = false;
}
void FGAIShip::readFromScenario(SGPropertyNode* scFileNode) {
if (!scFileNode)
return;
@ -283,9 +272,8 @@ void FGAIShip::Run(double dt) {
if (_fp_init)
ProcessFlightPlan(dt);
const string& type = getTypeString();
auto type = getTypeString();
double alpha;
double rudder_limit;
double raw_roll;
@ -346,9 +334,8 @@ void FGAIShip::Run(double dt) {
_rudder = -45;
//we assume that at slow speed ships will manoeuvre using engines/bow thruster
if(type == "ship" || type == "carrier" || type == "escort"){
// we assume that at slow speed ships will manoeuvre using engines/bow thruster
if (type == "ship" || type == "carrier" || type == "escort") { // TODO: fragile code... should this be an enum???
if (fabs(speed)<=5)
_sp_turn_radius_ft = _fixed_turn_radius;
else {
@ -356,7 +343,6 @@ void FGAIShip::Run(double dt) {
// we need to allow for negative speeds
_sp_turn_radius_ft = 10 * pow ((fabs(speed) - 15), 2) + turn_radius_ft;
}
} else {
if (fabs(speed) <= 40)
@ -367,7 +353,6 @@ void FGAIShip::Run(double dt) {
}
}
if (_rudder <= -0.25 || _rudder >= 0.25) {
// adjust turn radius for _rudder angle. The equation is even more approximate.
float a = 19;
@ -377,7 +362,7 @@ void FGAIShip::Run(double dt) {
_rd_turn_radius_ft = (a * exp(b * fabs(_rudder)) + c) * _sp_turn_radius_ft;
// calculate the angle, alpha, subtended by the arc traversed in time dt
alpha = ((speed * 1.686 * dt) / _rd_turn_radius_ft) * SG_RADIANS_TO_DEGREES;
double alpha = ((speed * 1.686 * dt) / _rd_turn_radius_ft) * SG_RADIANS_TO_DEGREES;
//cout << _name << " alpha " << alpha << endl;
// make sure that alpha is applied in the right direction
hdg += alpha * sign(_rudder);
@ -428,13 +413,11 @@ void FGAIShip::Run(double dt) {
}
// set the _rudder limit by speed
if (type == "ship" || type == "carrier" || type == "escort"){
if (type == "ship" || type == "carrier" || type == "escort") { // TODO: fragile code... should this be an enum???
if (speed <= 40)
rudder_limit = (-0.825 * speed) + 35;
else
rudder_limit = 2;
} else
rudder_limit = 20;
@ -471,8 +454,10 @@ void FGAIShip::RollTo(double angle) {
tgt_roll = angle;
}
#if 0
void FGAIShip::YawTo(double angle) {
}
#endif
void FGAIShip::ClimbTo(double altitude) {
tgt_altitude_ft = altitude;
@ -646,7 +631,6 @@ void FGAIShip::ProcessFlightPlan(double dt) {
_next_run = 0.05 + (0.025 * sg_random());
double until_time_sec = 0;
_missed = false;
// check to see if we've reached the point for our next turn
@ -701,7 +685,7 @@ void FGAIShip::ProcessFlightPlan(double dt) {
curr = fp->getCurrentWaypoint();
next = fp->getNextWaypoint();
}else if(_next_name == "END" || fp->getNextWaypoint() == 0) {
} else if(_next_name == "END" || fp->getNextWaypoint() == 0) {
if (_repeat) {
SG_LOG(SG_AI, SG_INFO, "AIShip: "<< _name << " Flightplan repeating ");
@ -717,7 +701,7 @@ void FGAIShip::ProcessFlightPlan(double dt) {
_missed_count = 0;
_lead_angle = 0;
AccelTo(prev->getSpeed());
} else if (_restart){
} else if (_restart) {
SG_LOG(SG_AI, SG_INFO, "AIShip: " << _name << " Flightplan restarting ");
_missed_count = 0;
initFlightPlan();
@ -758,7 +742,7 @@ void FGAIShip::ProcessFlightPlan(double dt) {
} else if (_next_name == "WAITUNTIL") {
time_sec = getDaySeconds();
until_time_sec = processTimeString(next->getTime());
double until_time_sec = processTimeString(next->getTime());
_until_time = next->getTime();
setUntilTime(next->getTime());
if (until_time_sec > time_sec) {
@ -809,7 +793,7 @@ void FGAIShip::ProcessFlightPlan(double dt) {
setWPPos();
object_type type = getType();
if (type != 10)
if (type != 10) // TODO: magic number
AccelTo(prev->getSpeed());
_curr_alt = curr->getAltitude();
@ -845,12 +829,12 @@ bool FGAIShip::initFlightPlan() {
curr = fp->getCurrentWaypoint(); //second waypoint
next = fp->getNextWaypoint(); //third waypoint (might not exist!)
while (curr->getName() == "WAIT" || curr->getName() == "WAITUNTIL") { // don't wait when initialising
while (curr && (curr->getName() == "WAIT" || curr->getName() == "WAITUNTIL")) { // don't wait when initialising
SG_LOG(SG_AI, SG_DEBUG, "AIShip: " << _name << " re-initializing waypoints ");
fp->IncrementWaypoint(false);
curr = fp->getCurrentWaypoint();
next = fp->getNextWaypoint();
} // end while loop
}
if (!_start_time.empty()){
_start_sec = processTimeString(_start_time);
@ -878,16 +862,20 @@ bool FGAIShip::initFlightPlan() {
return false;
}
} else {
} else if (prev) {
setLatitude(prev->getLatitude());
setLongitude(prev->getLongitude());
setSpeed(prev->getSpeed());
}
setWPNames();
if (prev && curr) {
setHeading(getCourse(prev->getLatitude(), prev->getLongitude(), curr->getLatitude(), curr->getLongitude()));
_wp_range = getRange(prev->getLatitude(), prev->getLongitude(), curr->getLatitude(), curr->getLongitude());
_old_range = _wp_range;
}
_range_rate = 0;
_hdg_lock = true;
_missed = false;
@ -895,6 +883,7 @@ bool FGAIShip::initFlightPlan() {
_new_waypoint = true;
SG_LOG(SG_AI, SG_ALERT, "AIShip: " << _name << " done initialising waypoints " << _tunnel);
if (prev)
init = true;
@ -902,7 +891,6 @@ bool FGAIShip::initFlightPlan() {
return true;
else
return false;
} // end of initialization
@ -1073,23 +1061,18 @@ void FGAIShip::setWPPos() {
return;
}
double elevation_m = 0;
wppos.setLatitudeDeg(curr->getLatitude());
wppos.setLongitudeDeg(curr->getLongitude());
wppos.setElevationM(0);
if (curr->getOn_ground()){
if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(wppos, 3000),
elevation_m, NULL, 0)){
wppos.setElevationM(elevation_m);
double elevation_m = 0;
if (globals->get_scenery()->get_elevation_m(
SGGeod::fromGeodM(wppos, 3000), elevation_m, NULL, 0)) {
wppos.setElevationM(elevation_m);
}
//cout << curr->name << " setting measured elev " << elevation_m << endl;
} else {
wppos.setElevationM(curr->getAltitude());
//cout << curr->name << " setting FP elev " << elevation_m << endl;
}
curr->setAltitude(wppos.getElevationM());

View file

@ -19,21 +19,22 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_AISHIP_HXX
#define _FG_AISHIP_HXX
#pragma once
#include <simgear/scene/material/mat.hxx>
#include "AIBase.hxx"
#include "AIFlightPlan.hxx"
#include <simgear/scene/material/mat.hxx>
class FGAIManager;
class FGAIShip : public FGAIBase {
public:
FGAIShip(object_type ot = otShip);
virtual ~FGAIShip();
FGAIShip(object_type ot = object_type::otShip);
virtual ~FGAIShip() = default;
const char* getTypeString(void) const override { return "ship"; }
void readFromScenario(SGPropertyNode* scFileNode) override;
bool init(ModelSearchOrder searchOrder) override;
@ -48,7 +49,11 @@ public:
void AccelTo(double speed);
void PitchTo(double angle);
void RollTo(double angle);
#if 0
void YawTo(double angle);
#endif
void ClimbTo(double altitude);
void TurnTo(double heading);
void setCurrName(const std::string&);
@ -70,22 +75,26 @@ public:
double sign(double x);
bool _hdg_lock;
bool _serviceable;
bool _hdg_lock = false;
bool _serviceable = false;
bool _waiting;
bool _new_waypoint;
bool _tunnel, _initial_tunnel;
bool _restart;
const char* getTypeString(void) const override { return "ship"; }
double _rudder_constant, _speed_constant, _hdg_constant, _limit ;
double _elevation_m, _elevation_ft;
double _missed_range, _tow_angle, _wait_count, _missed_count,_wp_range;
double _rudder_constant = 0.0;
double _speed_constant = 0.0;
double _hdg_constant, _limit;
double _elevation_ft;
double _missed_range = 0.0;
double _tow_angle;
double _wait_count = 0.0;
double _missed_count,_wp_range;
double _dt_count, _next_run;
FGAIWaypoint* prev; // the one behind you
FGAIWaypoint* curr; // the one ahead
FGAIWaypoint* next; // the next plus 1
FGAIWaypoint* prev = nullptr; // the one behind you
FGAIWaypoint* curr = nullptr; // the one ahead
FGAIWaypoint* next = nullptr; // the next plus 1
protected:
@ -112,28 +121,30 @@ private:
bool initFlightPlan();
bool advanceFlightPlan (double elapsed_sec, double day_sec);
float _rudder, _tgt_rudder;
float _rudder = 0.0f;
float _tgt_rudder = 0.0f;
double _roll_constant, _roll_factor;
double _sp_turn_radius_ft, _rd_turn_radius_ft, _fixed_turn_radius;
double _sp_turn_radius_ft = 0.0;
double _rd_turn_radius_ft = 0.0;
double _fixed_turn_radius = 0.0;
double _old_range, _range_rate;
double _missed_time_sec;
double _start_sec;
double _start_sec = 0.0;
double _day;
double _lead_angle;
double _lead_angle_gain, _lead_angle_limit, _proportion;
double _course;
double _lead_angle_gain = 0.0;
double _lead_angle_limit = 0.0;
double _proportion = 0.0;
double _course = 0.0;
double _xtrack_error;
double _curr_alt, _prev_alt;
std::string _prev_name, _curr_name, _next_name;
std::string _path;
std::string _start_time, _until_time;
bool _repeat;
bool _repeat = false;
bool _fp_init;
bool _missed;
};
#endif // _FG_AISHIP_HXX