+ EnginePath = EPath;
+
+# ifndef macintosh
+ aircraftCfgFileName = AircraftPath + "/" + model + "/" + model + ".xml";
+# else
+ aircraftCfgFileName = AircraftPath + ";" + model + ";" + model + ".xml";
+# endif
+
+ FGConfigFile AC_cfg(aircraftCfgFileName);
+ if (!AC_cfg.IsOpen()) return false;
+
+ if (modelLoaded) {
+ DeAllocate();
+ Allocate();
+ }
+
+ result = Aircraft->Load(&AC_cfg);
+
+ if (result) {
+ modelLoaded = true;
+ } else {
+ cerr << fgred
+ << "FGFDMExec: Failed to load aircraft and/or engine model"
+ << fgdef << endl;
+ }
+
+ if (debug_lvl > 0) cout << "\n\nJSBSim startup complete\n\n";
+ return result;
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+bool FGFDMExec::LoadScript(string script)
+{
+ FGConfigFile Script(script);
+ string token="";
+ string aircraft="";
+ string initialize="";
+ bool result=false;
+ float dt=0.0;
+ int i;
+ struct condition *newCondition;
+
+ if (!Script.IsOpen()) return false;
+
+ Script.GetNextConfigLine();
+ ScriptName = Script.GetValue("name");
+ Scripted = true;
+ if (debug_lvl > 0) cout << "Reading Script File " << ScriptName << endl;
+
+ while (Script.GetNextConfigLine() != "EOF" && Script.GetValue() != "/runscript") {
+ token = Script.GetValue();
+ if (token == "use") {
+ if ((token = Script.GetValue("aircraft")) != "") {
+ aircraft = token;
+ if (debug_lvl > 0) cout << " Use aircraft: " << token << endl;
+ } else if ((token = Script.GetValue("initialize")) != "") {
+ initialize = token;
+ if (debug_lvl > 0) cout << " Use reset file: " << token << endl;
+ } else {
+ cerr << "Unknown 'use' keyword: \"" << token << "\"" << endl;
+ }
+ } else if (token == "run") {
+ StartTime = strtod(Script.GetValue("start").c_str(), NULL);
+ State->Setsim_time(StartTime);
+ EndTime = strtod(Script.GetValue("end").c_str(), NULL);
+ dt = strtod(Script.GetValue("dt").c_str(), NULL);
+ State->Setdt(dt);
+ Script.GetNextConfigLine();
+ token = Script.GetValue();
+ while (token != "/run") {
+
+ if (token == "when") {
+ Script.GetNextConfigLine();
+ token = Script.GetValue();
+ newCondition = new struct condition();
+ while (token != "/when") {
+ if (token == "parameter") {
+ newCondition->TestParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
+ newCondition->TestValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
+ newCondition->Comparison.push_back(Script.GetValue("comparison"));
+ } else if (token == "set") {
+ newCondition->SetParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
+ newCondition->SetValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
+ newCondition->Triggered.push_back(false);
+ newCondition->OriginalValue.push_back(0.0);
+ newCondition->newValue.push_back(0.0);
+ newCondition->StartTime.push_back(0.0);
+ newCondition->EndTime.push_back(0.0);
+ string tempCompare = Script.GetValue("type");
+ if (tempCompare == "FG_DELTA") newCondition->Type.push_back(FG_DELTA);
+ else if (tempCompare == "FG_BOOL") newCondition->Type.push_back(FG_BOOL);
+ else if (tempCompare == "FG_VALUE") newCondition->Type.push_back(FG_VALUE);
+ else newCondition->Type.push_back((eType)0);
+ tempCompare = Script.GetValue("action");
+ if (tempCompare == "FG_RAMP") newCondition->Action.push_back(FG_RAMP);
+ else if (tempCompare == "FG_STEP") newCondition->Action.push_back(FG_STEP);
+ else if (tempCompare == "FG_EXP") newCondition->Action.push_back(FG_EXP);
+ else newCondition->Action.push_back((eAction)0);
+
+ if (Script.GetValue("persistent") == "true")
+ newCondition->Persistent.push_back(true);
+ else
+ newCondition->Persistent.push_back(false);
+
+ newCondition->TC.push_back(strtod(Script.GetValue("tc").c_str(), NULL));
+
+ } else {
+ cerr << "Unrecognized keyword in script file: \" [when] " << token << "\"" << endl;
+ }
+ Script.GetNextConfigLine();
+ token = Script.GetValue();
+ }
+ Conditions.push_back(*newCondition);
+ Script.GetNextConfigLine();
+ token = Script.GetValue();
+
+ } else {
+ cerr << "Error reading script file: expected \"when\", got \"" << token << "\"" << endl;
+ }
+
+ }
+ } else {
+ cerr << "Unrecognized keyword in script file: \"" << token << "\" [runscript] " << endl;
+ }
+ }
+
+ if (aircraft == "") {
+ cerr << "Aircraft file not loaded in script" << endl;
+ exit(-1);
+ }
+
+ // print out conditions for double-checking if requested
+
+ if (debug_lvl > 0) {
+ vector <struct condition>::iterator iterConditions = Conditions.begin();
+ int count=0;
+
+ cout << "\n Script goes from " << StartTime << " to " << EndTime
+ << " with dt = " << dt << endl << endl;
+
+ while (iterConditions < Conditions.end()) {
+ cout << " Condition: " << count++ << endl;
+ cout << " if (";
+
+ for (i=0; i<iterConditions->TestValue.size(); i++) {
+ if (i>0) cout << " and" << endl << " ";
+ cout << "(" << State->paramdef[iterConditions->TestParam[i]]
+ << iterConditions->Comparison[i] << " "
+ << iterConditions->TestValue[i] << ")";
+ }
+ cout << ") then {" << endl;
+
+ for (i=0; i<iterConditions->SetValue.size(); i++) {
+ cout << " set" << State->paramdef[iterConditions->SetParam[i]]
+ << "to " << iterConditions->SetValue[i];
+
+ switch (iterConditions->Type[i]) {
+ case FG_VALUE:
+ cout << " (constant";
+ break;
+ case FG_DELTA:
+ cout << " (delta";
+ break;
+ case FG_BOOL:
+ cout << " (boolean";
+ break;
+ default:
+ cout << " (unspecified type";
+ }
+
+ switch (iterConditions->Action[i]) {
+ case FG_RAMP:
+ cout << " via ramp";
+ break;
+ case FG_STEP:
+ cout << " via step";
+ break;
+ case FG_EXP:
+ cout << " via exponential approach";
+ break;
+ default:
+ cout << " via unspecified action";
+ }
+
+ if (!iterConditions->Persistent[i]) cout << endl
+ << " once";
+ else cout << endl
+ << " repeatedly";
+
+ if (iterConditions->Action[i] == FG_RAMP ||
+ iterConditions->Action[i] == FG_EXP) cout << endl
+ << " with time constant "
+ << iterConditions->TC[i];
+ }
+ cout << ")" << endl << " }" << endl << endl;
+
+ iterConditions++;
+ }
+
+ cout << endl;
+ }
+
+ result = LoadModel("aircraft", "engine", aircraft);
+ if (!result) {
+ cerr << "Aircraft file " << aircraft << " was not found" << endl;
+ exit(-1);
+ }
+ if ( ! State->Reset("aircraft", aircraft, initialize))
+ State->Initialize(2000,0,0,0,0,0,0.5,0.5,40000);
+
+ return true;
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+void FGFDMExec::RunScript(void)
+{
+ vector <struct condition>::iterator iC = Conditions.begin();
+ bool truth;
+ bool WholeTruth;
+ int i;
+
+ int count=0;
+
+ float currentTime = State->Getsim_time();
+ float newSetValue;
+
+ while (iC < Conditions.end()) {
+ // determine whether the set of conditional tests for this condition equate
+ // to true
+ for (i=0; i<iC->TestValue.size(); i++) {
+ if (iC->Comparison[i] == "lt")
+ truth = State->GetParameter(iC->TestParam[i]) < iC->TestValue[i];
+ else if (iC->Comparison[i] == "le")
+ truth = State->GetParameter(iC->TestParam[i]) <= iC->TestValue[i];
+ else if (iC->Comparison[i] == "eq")
+ truth = State->GetParameter(iC->TestParam[i]) == iC->TestValue[i];
+ else if (iC->Comparison[i] == "ge")
+ truth = State->GetParameter(iC->TestParam[i]) >= iC->TestValue[i];
+ else if (iC->Comparison[i] == "gt")
+ truth = State->GetParameter(iC->TestParam[i]) > iC->TestValue[i];
+ else if (iC->Comparison[i] == "ne")
+ truth = State->GetParameter(iC->TestParam[i]) != iC->TestValue[i];
+ else
+ cerr << "Bad comparison" << endl;
+
+ if (i == 0) WholeTruth = truth;
+ else WholeTruth = WholeTruth && truth;
+
+ if (!truth && iC->Persistent[i] && iC->Triggered[i]) iC->Triggered[i] = false;
+ }
+
+ // if the conditions are true, do the setting of the desired parameters
+
+ if (WholeTruth) {
+ for (i=0; i<iC->SetValue.size(); i++) {
+ if ( ! iC->Triggered[i]) {
+ iC->OriginalValue[i] = State->GetParameter(iC->SetParam[i]);
+ switch (iC->Type[i]) {
+ case FG_VALUE:
+ iC->newValue[i] = iC->SetValue[i];
+ break;
+ case FG_DELTA:
+ iC->newValue[i] = iC->OriginalValue[i] + iC->SetValue[i];
+ break;
+ case FG_BOOL:
+ break;
+ default:
+ cerr << "Invalid Type specified" << endl;
+ break;
+ }
+ iC->Triggered[i] = true;
+ iC->StartTime[i] = currentTime;
+ }
+
+ switch (iC->Action[i]) {
+ case FG_RAMP:
+ newSetValue = (currentTime - iC->StartTime[i])/(iC->TC[i])
+ * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
+ if (newSetValue > iC->newValue[i]) newSetValue = iC->newValue[i];
+ break;
+ case FG_STEP:
+ newSetValue = iC->newValue[i];
+ break;
+ case FG_EXP:
+ newSetValue = (1 - exp(-(currentTime - iC->StartTime[i])/(iC->TC[i])))
+ * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
+ break;
+ default:
+ cerr << "Invalid Action specified" << endl;
+ break;
+ }
+ State->SetParameter(iC->SetParam[i], newSetValue);
+ }
+ }
+ iC++;
+ }