Tidy up the code a bit
This commit is contained in:
parent
d0af0c5f58
commit
4c01e0e76a
4 changed files with 55 additions and 32 deletions
|
@ -33,6 +33,19 @@ SG_USING_STD(string);
|
|||
|
||||
#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
|
||||
|
@ -57,7 +70,7 @@ void FGAIAircraft::update(double dt) {
|
|||
FGAIBase::update(dt);
|
||||
}
|
||||
|
||||
void FGAIAircraft::SetPerformance(PERF_STRUCT ps) {
|
||||
void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
|
||||
|
||||
performance = ps;
|
||||
}
|
||||
|
@ -83,8 +96,8 @@ void FGAIAircraft::Run(double dt) {
|
|||
// 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
|
||||
|
@ -136,10 +149,10 @@ void FGAIAircraft::Run(double dt) {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -30,15 +30,32 @@ SG_USING_STD(string);
|
|||
|
||||
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);
|
||||
|
@ -53,7 +70,7 @@ private:
|
|||
|
||||
double dt;
|
||||
|
||||
PERF_STRUCT performance;
|
||||
const PERF_STRUCT *performance;
|
||||
|
||||
void Run(double dt);
|
||||
double sign(double x);
|
||||
|
|
|
@ -42,25 +42,29 @@ FGAIManager::~FGAIManager() {
|
|||
|
||||
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"));
|
||||
|
|
|
@ -27,21 +27,10 @@
|
|||
#include <Main/fg_props.hxx>
|
||||
#include <list>
|
||||
#include "AIBase.hxx"
|
||||
#include "AIAircraft.hxx"
|
||||
|
||||
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
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue