2 #include "Thruster.hpp"
3 #include "PropEngine.hpp"
4 #include "PistonEngine.hpp"
9 #include "ControlMap.hpp"
12 ControlMap::~ControlMap()
15 for(i=0; i<_inputs.size(); i++) {
16 Vector* v = (Vector*)_inputs.get(i);
18 for(j=0; j<v->size(); j++)
19 delete (MapRec*)v->get(j);
23 for(i=0; i<_outputs.size(); i++)
24 delete (OutRec*)_outputs.get(i);
27 int ControlMap::newInput()
29 Vector* v = new Vector();
30 return _inputs.add(v);
33 void ControlMap::addMapping(int input, int type, void* object, int options,
34 float src0, float src1, float dst0, float dst1)
36 addMapping(input, type, object, options);
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);
48 void ControlMap::addMapping(int input, int type, void* object, int options)
50 // See if the output object already exists
53 for(i=0; i<_outputs.size(); i++) {
54 OutRec* o = (OutRec*)_outputs.get(i);
55 if(o->object == object && o->type == type) {
61 // Create one if it doesn't
69 // Make a new input record
70 MapRec* map = new MapRec();
73 map->idx = out->maps.add(map);
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;
81 // And add it to the approproate vectors.
82 Vector* maps = (Vector*)_inputs.get(input);
86 void ControlMap::reset()
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;
96 void ControlMap::setInput(int input, float val)
98 Vector* maps = (Vector*)_inputs.get(input);
99 for(int i=0; i<maps->size(); i++) {
100 MapRec* m = (MapRec*)maps->get(i);
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);
115 void ControlMap::applyControls()
118 for(outrec=0; outrec<_outputs.size(); outrec++) {
119 OutRec* o = (OutRec*)_outputs.get(outrec);
121 // Generate a summed value. Note the check for "split"
122 // control axes like ailerons.
123 float lval = 0, rval = 0;
125 for(i=0; i<o->maps.size(); i++) {
126 MapRec* m = (MapRec*)o->maps.get(i);
129 if(m->opt & OPT_SQUARE)
130 val = val * Math::abs(val);
131 if(m->opt & OPT_INVERT)
134 if(m->opt & OPT_SPLIT)
140 void* obj = o->object;
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;
157 ((Thruster*)obj)->getPistonEngine()->setBoost(lval);
163 }; // namespace yasim