]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/ControlMap.cpp
Tweaks to fix directory change.
[flightgear.git] / src / FDM / YASim / ControlMap.cpp
1 #include "Jet.hpp"
2 #include "Thruster.hpp"
3 #include "PropEngine.hpp"
4 #include "Gear.hpp"
5 #include "Wing.hpp"
6 #include "Math.hpp"
7
8 #include "ControlMap.hpp"
9 namespace yasim {
10
11 ControlMap::~ControlMap()
12 {
13     for(int i=0; i<_inputs.size(); i++) {
14         Vector* v = (Vector*)_inputs.get(i);
15         for(int j=0; j<v->size(); j++)
16             delete (MapRec*)v->get(j);
17         delete v;
18     }
19
20     for(int i=0; i<_outputs.size(); i++) {
21         OutRec* o = (OutRec*)_outputs.get(i);
22         delete[] o->values;
23         delete o;
24     }
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 {
35     // See if the output object already exists
36     OutRec* out = 0;
37     for(int i=0; i<_outputs.size(); i++) {
38         OutRec* o = (OutRec*)_outputs.get(i);
39         if(o->object == object && o->type == type) {
40             out = o;
41             break;
42         }
43     }
44
45     // Create one if it doesn't
46     if(out == 0) {
47         out = new OutRec();
48         out->type = type;
49         out->object = object;
50         out->n = 0;
51         out->values = 0;
52         _outputs.add(out);
53     }
54
55     // Make space for the new input value
56     int idx = out->n++;
57     delete[] out->values;
58     out->values = new float[out->n];
59
60     // Add the new option tag
61     out->options.add((void*)options);
62
63     // Make a new input record
64     MapRec* map = new MapRec();
65     map->out = out;
66     map->idx = idx;
67
68     // And add it to the approproate vector.
69     Vector* maps = (Vector*)_inputs.get(input);
70     maps->add(map);
71 }
72
73 void ControlMap::reset()
74 {
75     // Set all the values to zero
76     for(int i=0; i<_outputs.size(); i++) {
77         OutRec* o = (OutRec*)_outputs.get(i);
78         for(int j=0; j<o->n; j++)
79             o->values[j] = 0;
80     }
81 }
82
83 void ControlMap::setInput(int input, float value)
84 {
85     Vector* maps = (Vector*)_inputs.get(input);
86     for(int i=0; i<maps->size(); i++) {
87         MapRec* map = (MapRec*)maps->get(i);
88         map->out->values[map->idx] = value;
89     }
90 }
91
92 void ControlMap::applyControls()
93 {
94     for(int outrec=0; outrec<_outputs.size(); outrec++) {
95         OutRec* o = (OutRec*)_outputs.get(outrec);
96         
97         // Generate a summed value.  Note the check for "split"
98         // control axes like ailerons.
99         float lval = 0, rval = 0;
100         for(int i=0; i<o->n; i++) {
101             float val = o->values[i];
102             int opt = (int)o->options.get(i);
103             if(opt & OPT_SQUARE)
104                 val = val * Math::abs(val);
105             if(opt & OPT_INVERT)
106                 val = -val;
107             lval += val;
108             if(opt & OPT_SPLIT)
109                 rval -= val;
110             else
111                 rval += val;
112         }
113
114         void* obj = o->object;
115         switch(o->type) {
116         case THROTTLE: ((Thruster*)obj)->setThrottle(lval);    break;
117         case MIXTURE:  ((Thruster*)obj)->setMixture(lval);     break;
118         case ADVANCE:  ((PropEngine*)obj)->setAdvance(lval);   break;
119         case REHEAT:   ((Jet*)obj)->setReheat(lval);           break;
120         case BRAKE:    ((Gear*)obj)->setBrake(lval);           break;
121         case STEER:    ((Gear*)obj)->setRotation(lval);        break;
122         case EXTEND:   ((Gear*)obj)->setExtension(lval);       break;
123         case SLAT:     ((Wing*)obj)->setSlat(lval);            break;
124         case FLAP0:    ((Wing*)obj)->setFlap0(lval, rval);     break;
125         case FLAP1:    ((Wing*)obj)->setFlap1(lval, rval);     break;
126         case SPOILER:  ((Wing*)obj)->setSpoiler(lval, rval);   break;
127         }
128     }
129 }
130
131 }; // namespace yasim