]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/YASim/ControlMap.cpp
Fix for bug 1304 - crash loading XML route
[flightgear.git] / src / FDM / YASim / ControlMap.cpp
index 9cd985304aecc0aa42a6c88848f060d113acd2d6..c300c4c754b90b58476f5b7a813472b503cdeb0b 100644 (file)
@@ -1,10 +1,20 @@
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include "Jet.hpp"
 #include "Thruster.hpp"
 #include "PropEngine.hpp"
 #include "PistonEngine.hpp"
+#include "TurbineEngine.hpp"
 #include "Gear.hpp"
+#include "Hook.hpp"
+#include "Launchbar.hpp"
 #include "Wing.hpp"
+#include "Rotor.hpp"
 #include "Math.hpp"
+#include "Propeller.hpp"
+#include "Hitch.hpp"
 
 #include "ControlMap.hpp"
 namespace yasim {
@@ -63,6 +73,7 @@ void ControlMap::addMapping(int input, int type, void* object, int options)
        out = new OutRec();
        out->type = type;
        out->object = object;
+        out->oldL = out->oldR = out->time = 0;
        _outputs.add(out);
     }
     
@@ -73,10 +84,8 @@ void ControlMap::addMapping(int input, int type, void* object, int options)
     map->idx = out->maps.add(map);
 
     // The default ranges differ depending on type!
-    map->src1 = map->dst1 = 1;
-    map->src0 = map->dst0 = 0;
-    if(type==FLAP0 || type==FLAP1 || type==STEER)
-       map->src0 = map->dst0 = -1;
+    map->src1 = map->dst1 = rangeMax(type);
+    map->src0 = map->dst0 = rangeMin(type);
 
     // And add it to the approproate vectors.
     Vector* maps = (Vector*)_inputs.get(input);
@@ -89,7 +98,7 @@ void ControlMap::reset()
     for(int i=0; i<_outputs.size(); i++) {
        OutRec* o = (OutRec*)_outputs.get(i);
        for(int j=0; j<o->maps.size(); j++)
-           ((MapRec*)o->maps.get(j))->val = 0;
+           ((MapRec*)(o->maps.get(j)))->val = 0;
     }
 }
 
@@ -112,7 +121,32 @@ void ControlMap::setInput(int input, float val)
     }
 }
 
-void ControlMap::applyControls()
+int ControlMap::getOutputHandle(void* obj, int type)
+{
+    for(int i=0; i<_outputs.size(); i++) {
+       OutRec* o = (OutRec*)_outputs.get(i);
+       if(o->object == obj && o->type == type)
+           return i;
+    }
+    return 0;
+}
+
+void ControlMap::setTransitionTime(int handle, float time)
+{
+    ((OutRec*)_outputs.get(handle))->time = time;
+}
+
+float ControlMap::getOutput(int handle)
+{
+    return ((OutRec*)_outputs.get(handle))->oldL;
+}
+
+float ControlMap::getOutputR(int handle)
+{
+    return ((OutRec*)_outputs.get(handle))->oldR;
+}
+
+void ControlMap::applyControls(float dt)
 {
     int outrec;
     for(outrec=0; outrec<_outputs.size(); outrec++) {
@@ -137,27 +171,111 @@ void ControlMap::applyControls()
                rval += val;
        }
 
+        // If there is a finite transition time, clamp the values to
+        // the maximum travel allowed in this dt.
+        if(o->time > 0) {
+            float dl = lval - o->oldL;
+            float dr = rval - o->oldR;
+            float adl = Math::abs(dl);
+            float adr = Math::abs(dr);
+        
+            float max = (dt/o->time) * (rangeMax(o->type) - rangeMin(o->type));
+            if(adl > max) dl = dl*max/adl;
+            if(adr > max) dr = dr*max/adr;
+
+            lval = o->oldL + dl;
+            rval = o->oldR + dr;
+        }
+
+        o->oldL = lval;
+        o->oldR = rval;
+
        void* obj = o->object;
        switch(o->type) {
        case THROTTLE: ((Thruster*)obj)->setThrottle(lval);        break;
        case MIXTURE:  ((Thruster*)obj)->setMixture(lval);         break;
-       case STARTER:  ((Thruster*)obj)->setStarter(bool(lval));   break;
-       case MAGNETOS: ((PropEngine*)obj)->setMagnetos(int(lval)); break;
+    case CONDLEVER: ((TurbineEngine*)((PropEngine*)
+                        obj)->getEngine())->setCondLever(lval); break;
+       case STARTER:  ((Thruster*)obj)->setStarter(lval != 0.0);  break;
+       case MAGNETOS: ((PropEngine*)obj)->setMagnetos((int)lval); break;
        case ADVANCE:  ((PropEngine*)obj)->setAdvance(lval);       break;
+        case PROPPITCH: ((PropEngine*)obj)->setPropPitch(lval);    break;
+        case PROPFEATHER: ((PropEngine*)obj)->setPropFeather((int)lval); break;
        case REHEAT:   ((Jet*)obj)->setReheat(lval);               break;
        case VECTOR:   ((Jet*)obj)->setRotation(lval);             break;
        case BRAKE:    ((Gear*)obj)->setBrake(lval);               break;
        case STEER:    ((Gear*)obj)->setRotation(lval);            break;
        case EXTEND:   ((Gear*)obj)->setExtension(lval);           break;
+       case HEXTEND:  ((Hook*)obj)->setExtension(lval);           break;
+       case LEXTEND:  ((Launchbar*)obj)->setExtension(lval);      break;
+    case LACCEL:   ((Launchbar*)obj)->setAcceleration(lval);   break;
+       case CASTERING:((Gear*)obj)->setCastering(lval != 0);      break;
        case SLAT:     ((Wing*)obj)->setSlat(lval);                break;
        case FLAP0:    ((Wing*)obj)->setFlap0(lval, rval);         break;
+       case FLAP0EFFECTIVENESS: ((Wing*)obj)->setFlap0Effectiveness(lval); break;
        case FLAP1:    ((Wing*)obj)->setFlap1(lval, rval);         break;
+       case FLAP1EFFECTIVENESS: ((Wing*)obj)->setFlap1Effectiveness(lval); break;
        case SPOILER:  ((Wing*)obj)->setSpoiler(lval, rval);       break;
+        case COLLECTIVE:   ((Rotor*)obj)->setCollective(lval);     break;
+        case CYCLICAIL:    ((Rotor*)obj)->setCyclicail(lval,rval); break;
+        case CYCLICELE:    ((Rotor*)obj)->setCyclicele(lval,rval); break;
+        case TILTPITCH:    ((Rotor*)obj)->setTiltPitch(lval);      break;
+        case TILTYAW:      ((Rotor*)obj)->setTiltYaw(lval);        break;
+        case TILTROLL:     ((Rotor*)obj)->setTiltRoll(lval);       break;
+        case ROTORBALANCE:
+                           ((Rotor*)obj)->setRotorBalance(lval);   break;
+        case ROTORBRAKE:   ((Rotorgear*)obj)->setRotorBrake(lval); break;
+        case ROTORENGINEON: 
+                        ((Rotorgear*)obj)->setEngineOn((int)lval); break;
+        case ROTORENGINEMAXRELTORQUE: 
+              ((Rotorgear*)obj)->setRotorEngineMaxRelTorque(lval); break;
+        case ROTORRELTARGET:
+                       ((Rotorgear*)obj)->setRotorRelTarget(lval); break;
+       case REVERSE_THRUST: ((Jet*)obj)->setReverse(lval != 0);   break;
        case BOOST:
-           ((Thruster*)obj)->getPistonEngine()->setBoost(lval);
+           ((PistonEngine*)((Thruster*)obj)->getEngine())->setBoost(lval);
            break;
+        case WASTEGATE:
+            ((PistonEngine*)((Thruster*)obj)->getEngine())->setWastegate(lval);
+            break;
+        case WINCHRELSPEED: ((Hitch*)obj)->setWinchRelSpeed(lval); break;
+        case HITCHOPEN:    ((Hitch*)obj)->setOpen(lval!=0);       break;
+        case PLACEWINCH:    ((Hitch*)obj)->setWinchPositionAuto(lval!=0); break;
+        case FINDAITOW:     ((Hitch*)obj)->findBestAIObject(lval!=0); break;
        }
     }
 }
 
-}; // namespace yasim
+float ControlMap::rangeMin(int type)
+{
+    // The minimum of the range for each type of control
+    switch(type) {
+    case FLAP0:    return -1;  // [-1:1]
+    case FLAP1:    return -1;
+    case STEER:    return -1;
+    case CYCLICELE: return -1;
+    case CYCLICAIL: return -1;
+    case COLLECTIVE: return -1;
+    case WINCHRELSPEED: return -1;
+    case MAGNETOS: return 0;   // [0:3]
+    case FLAP0EFFECTIVENESS: return 1;  // [0:10]
+    case FLAP1EFFECTIVENESS: return 1;  // [0:10]
+    default:       return 0;   // [0:1]
+    }
+}
+
+float ControlMap::rangeMax(int type)
+{
+    // The maximum of the range for each type of control
+    switch(type) {
+    case FLAP0:    return 1; // [-1:1]
+    case FLAP1:    return 1;
+    case STEER:    return 1;
+    case MAGNETOS: return 3; // [0:3]
+    case FLAP0EFFECTIVENESS: return 10;//  [0:10]
+    case FLAP1EFFECTIVENESS: return 10;//  [0:10]
+    default:       return 1; // [0:1]
+    }
+}
+
+} // namespace yasim