2 #include "Thruster.hpp"
3 #include "PropEngine.hpp"
4 #include "PistonEngine.hpp"
5 #include "TurbineEngine.hpp"
10 #include "Propeller.hpp"
12 #include "ControlMap.hpp"
15 ControlMap::~ControlMap()
18 for(i=0; i<_inputs.size(); i++) {
19 Vector* v = (Vector*)_inputs.get(i);
21 for(j=0; j<v->size(); j++)
22 delete (MapRec*)v->get(j);
26 for(i=0; i<_outputs.size(); i++)
27 delete (OutRec*)_outputs.get(i);
30 int ControlMap::newInput()
32 Vector* v = new Vector();
33 return _inputs.add(v);
36 void ControlMap::addMapping(int input, int type, void* object, int options,
37 float src0, float src1, float dst0, float dst1)
39 addMapping(input, type, object, options);
41 // The one we just added is last in the list (ugly, awful hack!)
42 Vector* maps = (Vector*)_inputs.get(input);
43 MapRec* m = (MapRec*)maps->get(maps->size() - 1);
51 void ControlMap::addMapping(int input, int type, void* object, int options)
53 // See if the output object already exists
56 for(i=0; i<_outputs.size(); i++) {
57 OutRec* o = (OutRec*)_outputs.get(i);
58 if(o->object == object && o->type == type) {
64 // Create one if it doesn't
69 out->oldL = out->oldR = out->time = 0;
73 // Make a new input record
74 MapRec* map = new MapRec();
77 map->idx = out->maps.add(map);
79 // The default ranges differ depending on type!
80 map->src1 = map->dst1 = rangeMax(type);
81 map->src0 = map->dst0 = rangeMin(type);
83 // And add it to the approproate vectors.
84 Vector* maps = (Vector*)_inputs.get(input);
88 void ControlMap::reset()
90 // Set all the values to zero
91 for(int i=0; i<_outputs.size(); i++) {
92 OutRec* o = (OutRec*)_outputs.get(i);
93 for(int j=0; j<o->maps.size(); j++)
94 ((MapRec*)(o->maps.get(j)))->val = 0;
98 void ControlMap::setInput(int input, float val)
100 Vector* maps = (Vector*)_inputs.get(input);
101 for(int i=0; i<maps->size(); i++) {
102 MapRec* m = (MapRec*)maps->get(i);
106 // Do the scaling operation. Clamp to [src0:src1], rescale to
107 // [0:1] within that range, then map to [dst0:dst1].
108 if(val2 < m->src0) val2 = m->src0;
109 if(val2 > m->src1) val2 = m->src1;
110 val2 = (val2 - m->src0) / (m->src1 - m->src0);
111 val2 = m->dst0 + val2 * (m->dst1 - m->dst0);
117 int ControlMap::getOutputHandle(void* obj, int type)
119 for(int i=0; i<_outputs.size(); i++) {
120 OutRec* o = (OutRec*)_outputs.get(i);
121 if(o->object == obj && o->type == type)
127 void ControlMap::setTransitionTime(int handle, float time)
129 ((OutRec*)_outputs.get(handle))->time = time;
132 float ControlMap::getOutput(int handle)
134 return ((OutRec*)_outputs.get(handle))->oldL;
137 float ControlMap::getOutputR(int handle)
139 return ((OutRec*)_outputs.get(handle))->oldR;
142 void ControlMap::applyControls(float dt)
145 for(outrec=0; outrec<_outputs.size(); outrec++) {
146 OutRec* o = (OutRec*)_outputs.get(outrec);
148 // Generate a summed value. Note the check for "split"
149 // control axes like ailerons.
150 float lval = 0, rval = 0;
152 for(i=0; i<o->maps.size(); i++) {
153 MapRec* m = (MapRec*)o->maps.get(i);
156 if(m->opt & OPT_SQUARE)
157 val = val * Math::abs(val);
158 if(m->opt & OPT_INVERT)
161 if(m->opt & OPT_SPLIT)
167 // If there is a finite transition time, clamp the values to
168 // the maximum travel allowed in this dt.
170 float dl = lval - o->oldL;
171 float dr = rval - o->oldR;
172 float adl = Math::abs(dl);
173 float adr = Math::abs(dr);
175 float max = (dt/o->time) * (rangeMax(o->type) - rangeMin(o->type));
176 if(adl > max) dl = dl*max/adl;
177 if(adr > max) dr = dr*max/adr;
186 void* obj = o->object;
188 case THROTTLE: ((Thruster*)obj)->setThrottle(lval); break;
189 case MIXTURE: ((Thruster*)obj)->setMixture(lval); break;
190 case CONDLEVER: ((TurbineEngine*)((PropEngine*)obj)->getEngine())->setCondLever(lval); break;
191 case STARTER: ((Thruster*)obj)->setStarter(lval != 0.0); break;
192 case MAGNETOS: ((PropEngine*)obj)->setMagnetos((int)lval); break;
193 case ADVANCE: ((PropEngine*)obj)->setAdvance(lval); break;
194 case PROPPITCH: ((PropEngine*)obj)->setPropPitch(lval); break;
195 case PROPFEATHER: ((PropEngine*)obj)->setPropFeather((int)lval); break;
196 case REHEAT: ((Jet*)obj)->setReheat(lval); break;
197 case VECTOR: ((Jet*)obj)->setRotation(lval); break;
198 case BRAKE: ((Gear*)obj)->setBrake(lval); break;
199 case STEER: ((Gear*)obj)->setRotation(lval); break;
200 case EXTEND: ((Gear*)obj)->setExtension(lval); break;
201 case CASTERING:((Gear*)obj)->setCastering(lval != 0); break;
202 case SLAT: ((Wing*)obj)->setSlat(lval); break;
203 case FLAP0: ((Wing*)obj)->setFlap0(lval, rval); break;
204 case FLAP1: ((Wing*)obj)->setFlap1(lval, rval); break;
205 case SPOILER: ((Wing*)obj)->setSpoiler(lval, rval); break;
206 case COLLECTIVE: ((Rotor*)obj)->setCollective(lval); break;
207 case CYCLICAIL: ((Rotor*)obj)->setCyclicail(lval,rval); break;
208 case CYCLICELE: ((Rotor*)obj)->setCyclicele(lval,rval); break;
209 case ROTORENGINEON: ((Rotor*)obj)->setEngineOn((int)lval); break;
210 case REVERSE_THRUST: ((Jet*)obj)->setReverse(lval != 0); break;
212 ((PistonEngine*)((Thruster*)obj)->getEngine())->setBoost(lval);
218 float ControlMap::rangeMin(int type)
220 // The minimum of the range for each type of control
222 case FLAP0: return -1; // [-1:1]
223 case FLAP1: return -1;
224 case STEER: return -1;
225 case CYCLICELE: return -1;
226 case CYCLICAIL: return -1;
227 case COLLECTIVE: return -1;
228 case MAGNETOS: return 0; // [0:3]
229 default: return 0; // [0:1]
233 float ControlMap::rangeMax(int type)
235 // The maximum of the range for each type of control
237 case FLAP0: return 1; // [-1:1]
238 case FLAP1: return 1;
239 case STEER: return 1;
240 case MAGNETOS: return 3; // [0:3]
241 default: return 1; // [0:1]