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
66 out->oldL = out->oldR = out->time = 0;
70 // Make a new input record
71 MapRec* map = new MapRec();
74 map->idx = out->maps.add(map);
76 // The default ranges differ depending on type!
77 map->src1 = map->dst1 = rangeMax(type);
78 map->src0 = map->dst0 = rangeMin(type);
80 // And add it to the approproate vectors.
81 Vector* maps = (Vector*)_inputs.get(input);
85 void ControlMap::reset()
87 // Set all the values to zero
88 for(int i=0; i<_outputs.size(); i++) {
89 OutRec* o = (OutRec*)_outputs.get(i);
90 for(int j=0; j<o->maps.size(); j++)
91 ((MapRec*)(o->maps.get(j)))->val = 0;
95 void ControlMap::setInput(int input, float val)
97 Vector* maps = (Vector*)_inputs.get(input);
98 for(int i=0; i<maps->size(); i++) {
99 MapRec* m = (MapRec*)maps->get(i);
103 // Do the scaling operation. Clamp to [src0:src1], rescale to
104 // [0:1] within that range, then map to [dst0:dst1].
105 if(val2 < m->src0) val2 = m->src0;
106 if(val2 > m->src1) val2 = m->src1;
107 val2 = (val2 - m->src0) / (m->src1 - m->src0);
108 val2 = m->dst0 + val2 * (m->dst1 - m->dst0);
114 int ControlMap::getOutputHandle(void* obj, int type)
116 for(int i=0; i<_outputs.size(); i++) {
117 OutRec* o = (OutRec*)_outputs.get(i);
118 if(o->object == obj && o->type == type)
124 void ControlMap::setTransitionTime(int handle, float time)
126 ((OutRec*)_outputs.get(handle))->time = time;
129 float ControlMap::getOutput(int handle)
131 return ((OutRec*)_outputs.get(handle))->oldL;
134 float ControlMap::getOutputR(int handle)
136 return ((OutRec*)_outputs.get(handle))->oldR;
139 void ControlMap::applyControls(float dt)
142 for(outrec=0; outrec<_outputs.size(); outrec++) {
143 OutRec* o = (OutRec*)_outputs.get(outrec);
145 // Generate a summed value. Note the check for "split"
146 // control axes like ailerons.
147 float lval = 0, rval = 0;
149 for(i=0; i<o->maps.size(); i++) {
150 MapRec* m = (MapRec*)o->maps.get(i);
153 if(m->opt & OPT_SQUARE)
154 val = val * Math::abs(val);
155 if(m->opt & OPT_INVERT)
158 if(m->opt & OPT_SPLIT)
164 // If there is a finite transition time, clamp the values to
165 // the maximum travel allowed in this dt.
167 float dl = lval - o->oldL;
168 float dr = rval - o->oldR;
169 float adl = Math::abs(dl);
170 float adr = Math::abs(dr);
172 float max = (dt/o->time) * (rangeMax(o->type) - rangeMin(o->type));
173 if(adl > max) dl = dl*max/adl;
174 if(adr > max) dr = dr*max/adr;
183 void* obj = o->object;
185 case THROTTLE: ((Thruster*)obj)->setThrottle(lval); break;
186 case MIXTURE: ((Thruster*)obj)->setMixture(lval); break;
187 case STARTER: ((Thruster*)obj)->setStarter(lval != 0.0); break;
188 case MAGNETOS: ((PropEngine*)obj)->setMagnetos((int)lval); break;
189 case ADVANCE: ((PropEngine*)obj)->setAdvance(lval); break;
190 case REHEAT: ((Jet*)obj)->setReheat(lval); break;
191 case VECTOR: ((Jet*)obj)->setRotation(lval); break;
192 case BRAKE: ((Gear*)obj)->setBrake(lval); break;
193 case STEER: ((Gear*)obj)->setRotation(lval); break;
194 case EXTEND: ((Gear*)obj)->setExtension(lval); break;
195 case CASTERING:((Gear*)obj)->setCastering(lval != 0); break;
196 case SLAT: ((Wing*)obj)->setSlat(lval); break;
197 case FLAP0: ((Wing*)obj)->setFlap0(lval, rval); break;
198 case FLAP1: ((Wing*)obj)->setFlap1(lval, rval); break;
199 case SPOILER: ((Wing*)obj)->setSpoiler(lval, rval); break;
201 ((Thruster*)obj)->getPistonEngine()->setBoost(lval);
207 float ControlMap::rangeMin(int type)
209 // The minimum of the range for each type of control
211 case FLAP0: return -1; // [-1:1]
212 case FLAP1: return -1;
213 case STEER: return -1;
214 case MAGNETOS: return 0; // [0:3]
215 default: return 0; // [0:1]
219 float ControlMap::rangeMax(int type)
221 // The maximum of the range for each type of control
223 case FLAP0: return 1; // [-1:1]
224 case FLAP1: return 1;
225 case STEER: return 1;
226 case MAGNETOS: return 3; // [0:3]
227 default: return 1; // [0:1]