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