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

View file

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