1
0
Fork 0

Tidy up the code a bit

This commit is contained in:
ehofman 2003-12-21 13:42:01 +00:00
parent d0af0c5f58
commit 4c01e0e76a
4 changed files with 55 additions and 32 deletions

View file

@ -33,6 +33,19 @@ SG_USING_STD(string);
#include "AIAircraft.hxx" #include "AIAircraft.hxx"
const FGAIAircraft::PERF_STRUCT FGAIAircraft::settings[] = {
// light aircraft
{2.0, 2.0, 450.0, 1000.0, 70.0, 80.0, 100.0, 80.0, 60.0},
// ww2_fighter
{4.0, 2.0, 3000.0, 1500.0, 110.0, 180.0, 250.0, 200.0, 100.0},
// jet_transport
{5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0},
// jet_fighter
{7.0, 3.0, 4000.0, 2000.0, 150.0, 350.0, 500.0, 350.0, 150.0}
};
FGAIAircraft::FGAIAircraft() { FGAIAircraft::FGAIAircraft() {
// set heading and altitude locks // set heading and altitude locks
@ -57,7 +70,7 @@ void FGAIAircraft::update(double dt) {
FGAIBase::update(dt); FGAIBase::update(dt);
} }
void FGAIAircraft::SetPerformance(PERF_STRUCT ps) { void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
performance = ps; performance = ps;
} }
@ -83,8 +96,8 @@ void FGAIAircraft::Run(double dt) {
// adjust speed // adjust speed
double speed_diff = tgt_speed - speed; double speed_diff = tgt_speed - speed;
if (fabs(speed_diff) > 0.2) { if (fabs(speed_diff) > 0.2) {
if (speed_diff > 0.0) speed += performance.accel * dt; if (speed_diff > 0.0) speed += performance->accel * dt;
if (speed_diff < 0.0) speed -= performance.decel * dt; if (speed_diff < 0.0) speed -= performance->decel * dt;
} }
// convert speed to degrees per second // convert speed to degrees per second
@ -136,10 +149,10 @@ void FGAIAircraft::Run(double dt) {
double altitude_ft = altitude * 3.28084; double altitude_ft = altitude * 3.28084;
if (altitude_ft < tgt_altitude) { if (altitude_ft < tgt_altitude) {
tgt_vs = tgt_altitude - altitude_ft; tgt_vs = tgt_altitude - altitude_ft;
if (tgt_vs > performance.climb_rate) tgt_vs = performance.climb_rate; if (tgt_vs > performance->climb_rate) tgt_vs = performance->climb_rate;
} else { } else {
tgt_vs = tgt_altitude - altitude_ft; tgt_vs = tgt_altitude - altitude_ft;
if (tgt_vs < (-performance.descent_rate)) tgt_vs = -performance.descent_rate; if (tgt_vs < (-performance->descent_rate)) tgt_vs = -performance->descent_rate;
} }
} }

View file

@ -30,15 +30,32 @@ SG_USING_STD(string);
class FGAIAircraft : public FGAIBase { class FGAIAircraft : public FGAIBase {
private:
typedef struct {
double accel;
double decel;
double climb_rate;
double descent_rate;
double takeoff_speed;
double climb_speed;
double cruise_speed;
double descent_speed;
double land_speed;
} PERF_STRUCT;
public: public:
enum aircraft_e {LIGHT=0, WW2_FIGHTER, JET_TRANSPORT, JET_FIGHTER};
static const PERF_STRUCT settings[];
FGAIAircraft(); FGAIAircraft();
~FGAIAircraft(); ~FGAIAircraft();
bool init(); bool init();
void update(double dt); void update(double dt);
void SetPerformance(PERF_STRUCT ps); void SetPerformance(const PERF_STRUCT *ps);
void AccelTo(double speed); void AccelTo(double speed);
void PitchTo(double angle); void PitchTo(double angle);
void RollTo(double angle); void RollTo(double angle);
@ -53,7 +70,7 @@ private:
double dt; double dt;
PERF_STRUCT performance; const PERF_STRUCT *performance;
void Run(double dt); void Run(double dt);
double sign(double x); double sign(double x);

View file

@ -42,25 +42,29 @@ FGAIManager::~FGAIManager() {
void FGAIManager::init() { void FGAIManager::init() {
SGPropertyNode * node = fgGetNode("sim/ai", true); SGPropertyNode * node = fgGetNode("sim/ai", true);
for (int i = 0; i < node->nChildren(); i++) { for (int i = 0; i < node->nChildren(); i++) {
const SGPropertyNode * entry = node->getChild(i); const SGPropertyNode * entry = node->getChild(i);
if (!strcmp(entry->getName(), "entry")) { if (!strcmp(entry->getName(), "entry")) {
if (!strcmp(entry->getStringValue("type", ""), "aircraft")) { if (!strcmp(entry->getStringValue("type", ""), "aircraft")) {
FGAIAircraft* ai_plane = new FGAIAircraft; FGAIAircraft* ai_plane = new FGAIAircraft;
ai_list.push_back(ai_plane); ai_list.push_back(ai_plane);
if (!strcmp(entry->getStringValue("class", ""), "light")) {
PERF_STRUCT ps = {2.0, 2.0, 450.0, 1000.0, 70.0, 80.0, 100.0, 80.0, 60.0}; string model_class = entry->getStringValue("class", "");
ai_plane->SetPerformance(ps); if (model_class == "light") {
} else if (!strcmp(entry->getStringValue("class", ""), "ww2_fighter")) { ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
PERF_STRUCT ps = {4.0, 2.0, 3000.0, 1500.0, 110.0, 180.0, 250.0, 200.0, 100.0};
ai_plane->SetPerformance(ps); } else if (model_class == "ww2_fighter") {
} else if (!strcmp(entry->getStringValue("class", ""), "jet_transport")) { ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
PERF_STRUCT ps = {5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0};
ai_plane->SetPerformance(ps); } else if (model_class == "jet_transport") {
} else if (!strcmp(entry->getStringValue("class", ""), "jet_fighter")) { ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
PERF_STRUCT ps = {7.0, 3.0, 4000.0, 2000.0, 150.0, 350.0, 500.0, 350.0, 150.0};
ai_plane->SetPerformance(ps); } else if (model_class == "jet_fighter") {
ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
} }
ai_plane->setHeading(entry->getDoubleValue("heading")); ai_plane->setHeading(entry->getDoubleValue("heading"));
ai_plane->setSpeed(entry->getDoubleValue("speed-KTAS")); ai_plane->setSpeed(entry->getDoubleValue("speed-KTAS"));
ai_plane->setPath(entry->getStringValue("path")); ai_plane->setPath(entry->getStringValue("path"));

View file

@ -27,21 +27,10 @@
#include <Main/fg_props.hxx> #include <Main/fg_props.hxx>
#include <list> #include <list>
#include "AIBase.hxx" #include "AIBase.hxx"
#include "AIAircraft.hxx"
SG_USING_STD(list); SG_USING_STD(list);
struct PERF_STRUCT {
double accel;
double decel;
double climb_rate;
double descent_rate;
double takeoff_speed;
double climb_speed;
double cruise_speed;
double descent_speed;
double land_speed;
};
class FGAIManager : public SGSubsystem class FGAIManager : public SGSubsystem
{ {