]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/ControlMap.cpp
9cd985304aecc0aa42a6c88848f060d113acd2d6
[flightgear.git] / src / FDM / YASim / ControlMap.cpp
1 #include "Jet.hpp"
2 #include "Thruster.hpp"
3 #include "PropEngine.hpp"
4 #include "PistonEngine.hpp"
5 #include "Gear.hpp"
6 #include "Wing.hpp"
7 #include "Math.hpp"
8
9 #include "ControlMap.hpp"
10 namespace yasim {
11
12 ControlMap::~ControlMap()
13 {
14     int i;
15     for(i=0; i<_inputs.size(); i++) {
16         Vector* v = (Vector*)_inputs.get(i);
17         int j;
18         for(j=0; j<v->size(); j++)
19             delete (MapRec*)v->get(j);
20         delete v;
21     }
22
23     for(i=0; i<_outputs.size(); i++)
24         delete (OutRec*)_outputs.get(i);
25 }
26
27 int ControlMap::newInput()
28 {
29     Vector* v = new Vector();
30     return _inputs.add(v);
31 }
32
33 void ControlMap::addMapping(int input, int type, void* object, int options,
34                             float src0, float src1, float dst0, float dst1)
35 {
36     addMapping(input, type, object, options);
37
38     // The one we just added is last in the list (ugly, awful hack!)
39     Vector* maps = (Vector*)_inputs.get(input);
40     MapRec* m = (MapRec*)maps->get(maps->size() - 1);
41
42     m->src0 = src0;
43     m->src1 = src1;
44     m->dst0 = dst0;
45     m->dst1 = dst1;
46 }
47
48 void ControlMap::addMapping(int input, int type, void* object, int options)
49 {
50     // See if the output object already exists
51     OutRec* out = 0;
52     int i;
53     for(i=0; i<_outputs.size(); i++) {
54         OutRec* o = (OutRec*)_outputs.get(i);
55         if(o->object == object && o->type == type) {
56             out = o;
57             break;
58         }
59     }
60
61     // Create one if it doesn't
62     if(out == 0) {
63         out = new OutRec();
64         out->type = type;
65         out->object = object;
66         _outputs.add(out);
67     }
68     
69     // Make a new input record
70     MapRec* map = new MapRec();
71     map->out = out;
72     map->opt = options;
73     map->idx = out->maps.add(map);
74
75     // The default ranges differ depending on type!
76     map->src1 = map->dst1 = 1;
77     map->src0 = map->dst0 = 0;
78     if(type==FLAP0 || type==FLAP1 || type==STEER)
79         map->src0 = map->dst0 = -1;
80
81     // And add it to the approproate vectors.
82     Vector* maps = (Vector*)_inputs.get(input);
83     maps->add(map);
84 }
85
86 void ControlMap::reset()
87 {
88     // Set all the values to zero
89     for(int i=0; i<_outputs.size(); i++) {
90         OutRec* o = (OutRec*)_outputs.get(i);
91         for(int j=0; j<o->maps.size(); j++)
92             ((MapRec*)o->maps.get(j))->val = 0;
93     }
94 }
95
96 void ControlMap::setInput(int input, float val)
97 {
98     Vector* maps = (Vector*)_inputs.get(input);
99     for(int i=0; i<maps->size(); i++) {
100         MapRec* m = (MapRec*)maps->get(i);
101
102         float val2 = val;
103
104         // Do the scaling operation.  Clamp to [src0:src1], rescale to
105         // [0:1] within that range, then map to [dst0:dst1].
106         if(val2 < m->src0) val2 = m->src0;
107         if(val2 > m->src1) val2 = m->src1;
108         val2 = (val2 - m->src0) / (m->src1 - m->src0);
109         val2 = m->dst0 + val2 * (m->dst1 - m->dst0);
110
111         m->val = val2;
112     }
113 }
114
115 void ControlMap::applyControls()
116 {
117     int outrec;
118     for(outrec=0; outrec<_outputs.size(); outrec++) {
119         OutRec* o = (OutRec*)_outputs.get(outrec);
120         
121         // Generate a summed value.  Note the check for "split"
122         // control axes like ailerons.
123         float lval = 0, rval = 0;
124         int i;
125         for(i=0; i<o->maps.size(); i++) {
126             MapRec* m = (MapRec*)o->maps.get(i);
127             float val = m->val;
128
129             if(m->opt & OPT_SQUARE)
130                 val = val * Math::abs(val);
131             if(m->opt & OPT_INVERT)
132                 val = -val;
133             lval += val;
134             if(m->opt & OPT_SPLIT)
135                 rval -= val;
136             else
137                 rval += val;
138         }
139
140         void* obj = o->object;
141         switch(o->type) {
142         case THROTTLE: ((Thruster*)obj)->setThrottle(lval);        break;
143         case MIXTURE:  ((Thruster*)obj)->setMixture(lval);         break;
144         case STARTER:  ((Thruster*)obj)->setStarter(bool(lval));   break;
145         case MAGNETOS: ((PropEngine*)obj)->setMagnetos(int(lval)); break;
146         case ADVANCE:  ((PropEngine*)obj)->setAdvance(lval);       break;
147         case REHEAT:   ((Jet*)obj)->setReheat(lval);               break;
148         case VECTOR:   ((Jet*)obj)->setRotation(lval);             break;
149         case BRAKE:    ((Gear*)obj)->setBrake(lval);               break;
150         case STEER:    ((Gear*)obj)->setRotation(lval);            break;
151         case EXTEND:   ((Gear*)obj)->setExtension(lval);           break;
152         case SLAT:     ((Wing*)obj)->setSlat(lval);                break;
153         case FLAP0:    ((Wing*)obj)->setFlap0(lval, rval);         break;
154         case FLAP1:    ((Wing*)obj)->setFlap1(lval, rval);         break;
155         case SPOILER:  ((Wing*)obj)->setSpoiler(lval, rval);       break;
156         case BOOST:
157             ((Thruster*)obj)->getPistonEngine()->setBoost(lval);
158             break;
159         }
160     }
161 }
162
163 }; // namespace yasim