#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() {
// set heading and altitude locks
FGAIBase::update(dt);
}
-void FGAIAircraft::SetPerformance(PERF_STRUCT ps) {
+void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
performance = ps;
}
// adjust speed
double speed_diff = tgt_speed - speed;
if (fabs(speed_diff) > 0.2) {
- 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->accel * dt;
+ if (speed_diff < 0.0) speed -= performance->decel * dt;
}
// convert speed to degrees per second
double altitude_ft = altitude * 3.28084;
if (altitude_ft < tgt_altitude) {
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 {
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;
}
}
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:
+
+ enum aircraft_e {LIGHT=0, WW2_FIGHTER, JET_TRANSPORT, JET_FIGHTER};
+ static const PERF_STRUCT settings[];
FGAIAircraft();
~FGAIAircraft();
bool init();
void update(double dt);
- void SetPerformance(PERF_STRUCT ps);
+ void SetPerformance(const PERF_STRUCT *ps);
void AccelTo(double speed);
void PitchTo(double angle);
void RollTo(double angle);
double dt;
- PERF_STRUCT performance;
+ const PERF_STRUCT *performance;
void Run(double dt);
double sign(double x);
void FGAIManager::init() {
SGPropertyNode * node = fgGetNode("sim/ai", true);
+
for (int i = 0; i < node->nChildren(); i++) {
const SGPropertyNode * entry = node->getChild(i);
+
if (!strcmp(entry->getName(), "entry")) {
if (!strcmp(entry->getStringValue("type", ""), "aircraft")) {
FGAIAircraft* ai_plane = new FGAIAircraft;
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};
- ai_plane->SetPerformance(ps);
- } else if (!strcmp(entry->getStringValue("class", ""), "ww2_fighter")) {
- 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 (!strcmp(entry->getStringValue("class", ""), "jet_transport")) {
- 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 (!strcmp(entry->getStringValue("class", ""), "jet_fighter")) {
- 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);
+
+ string model_class = entry->getStringValue("class", "");
+ if (model_class == "light") {
+ ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
+
+ } else if (model_class == "ww2_fighter") {
+ ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
+
+ } else if (model_class == "jet_transport") {
+ ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
+
+ } else if (model_class == "jet_fighter") {
+ ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
}
+
ai_plane->setHeading(entry->getDoubleValue("heading"));
ai_plane->setSpeed(entry->getDoubleValue("speed-KTAS"));
ai_plane->setPath(entry->getStringValue("path"));
ai_ballistic->setLatitude(entry->getDoubleValue("latitude"));
ai_ballistic->init();
}
- }
+ }
}
initDone = true;