2 #include "Thruster.hpp"
3 #include "PropEngine.hpp"
4 #include "PistonEngine.hpp"
5 #include "TurbineEngine.hpp"
8 #include "Launchbar.hpp"
12 #include "Propeller.hpp"
14 #include "ControlMap.hpp"
17 ControlMap::~ControlMap()
20 for(i=0; i<_inputs.size(); i++) {
21 Vector* v = (Vector*)_inputs.get(i);
23 for(j=0; j<v->size(); j++)
24 delete (MapRec*)v->get(j);
28 for(i=0; i<_outputs.size(); i++)
29 delete (OutRec*)_outputs.get(i);
32 int ControlMap::newInput()
34 Vector* v = new Vector();
35 return _inputs.add(v);
38 void ControlMap::addMapping(int input, int type, void* object, int options,
39 float src0, float src1, float dst0, float dst1)
41 addMapping(input, type, object, options);
43 // The one we just added is last in the list (ugly, awful hack!)
44 Vector* maps = (Vector*)_inputs.get(input);
45 MapRec* m = (MapRec*)maps->get(maps->size() - 1);
53 void ControlMap::addMapping(int input, int type, void* object, int options)
55 // See if the output object already exists
58 for(i=0; i<_outputs.size(); i++) {
59 OutRec* o = (OutRec*)_outputs.get(i);
60 if(o->object == object && o->type == type) {
66 // Create one if it doesn't
71 out->oldL = out->oldR = out->time = 0;
75 // Make a new input record
76 MapRec* map = new MapRec();
79 map->idx = out->maps.add(map);
81 // The default ranges differ depending on type!
82 map->src1 = map->dst1 = rangeMax(type);
83 map->src0 = map->dst0 = rangeMin(type);
85 // And add it to the approproate vectors.
86 Vector* maps = (Vector*)_inputs.get(input);
90 void ControlMap::reset()
92 // Set all the values to zero
93 for(int i=0; i<_outputs.size(); i++) {
94 OutRec* o = (OutRec*)_outputs.get(i);
95 for(int j=0; j<o->maps.size(); j++)
96 ((MapRec*)(o->maps.get(j)))->val = 0;
100 void ControlMap::setInput(int input, float val)
102 Vector* maps = (Vector*)_inputs.get(input);
103 for(int i=0; i<maps->size(); i++) {
104 MapRec* m = (MapRec*)maps->get(i);
108 // Do the scaling operation. Clamp to [src0:src1], rescale to
109 // [0:1] within that range, then map to [dst0:dst1].
110 if(val2 < m->src0) val2 = m->src0;
111 if(val2 > m->src1) val2 = m->src1;
112 val2 = (val2 - m->src0) / (m->src1 - m->src0);
113 val2 = m->dst0 + val2 * (m->dst1 - m->dst0);
119 int ControlMap::getOutputHandle(void* obj, int type)
121 for(int i=0; i<_outputs.size(); i++) {
122 OutRec* o = (OutRec*)_outputs.get(i);
123 if(o->object == obj && o->type == type)
129 void ControlMap::setTransitionTime(int handle, float time)
131 ((OutRec*)_outputs.get(handle))->time = time;
134 float ControlMap::getOutput(int handle)
136 return ((OutRec*)_outputs.get(handle))->oldL;
139 float ControlMap::getOutputR(int handle)
141 return ((OutRec*)_outputs.get(handle))->oldR;
144 void ControlMap::applyControls(float dt)
147 for(outrec=0; outrec<_outputs.size(); outrec++) {
148 OutRec* o = (OutRec*)_outputs.get(outrec);
150 // Generate a summed value. Note the check for "split"
151 // control axes like ailerons.
152 float lval = 0, rval = 0;
154 for(i=0; i<o->maps.size(); i++) {
155 MapRec* m = (MapRec*)o->maps.get(i);
158 if(m->opt & OPT_SQUARE)
159 val = val * Math::abs(val);
160 if(m->opt & OPT_INVERT)
163 if(m->opt & OPT_SPLIT)
169 // If there is a finite transition time, clamp the values to
170 // the maximum travel allowed in this dt.
172 float dl = lval - o->oldL;
173 float dr = rval - o->oldR;
174 float adl = Math::abs(dl);
175 float adr = Math::abs(dr);
177 float max = (dt/o->time) * (rangeMax(o->type) - rangeMin(o->type));
178 if(adl > max) dl = dl*max/adl;
179 if(adr > max) dr = dr*max/adr;
188 void* obj = o->object;
190 case THROTTLE: ((Thruster*)obj)->setThrottle(lval); break;
191 case MIXTURE: ((Thruster*)obj)->setMixture(lval); break;
192 case CONDLEVER: ((TurbineEngine*)((PropEngine*)obj)->getEngine())->setCondLever(lval); break;
193 case STARTER: ((Thruster*)obj)->setStarter(lval != 0.0); break;
194 case MAGNETOS: ((PropEngine*)obj)->setMagnetos((int)lval); break;
195 case ADVANCE: ((PropEngine*)obj)->setAdvance(lval); break;
196 case PROPPITCH: ((PropEngine*)obj)->setPropPitch(lval); break;
197 case PROPFEATHER: ((PropEngine*)obj)->setPropFeather((int)lval); break;
198 case REHEAT: ((Jet*)obj)->setReheat(lval); break;
199 case VECTOR: ((Jet*)obj)->setRotation(lval); break;
200 case BRAKE: ((Gear*)obj)->setBrake(lval); break;
201 case STEER: ((Gear*)obj)->setRotation(lval); break;
202 case EXTEND: ((Gear*)obj)->setExtension(lval); break;
203 case HEXTEND: ((Hook*)obj)->setExtension(lval); break;
204 case LEXTEND: ((Launchbar*)obj)->setExtension(lval); break;
205 case CASTERING:((Gear*)obj)->setCastering(lval != 0); break;
206 case SLAT: ((Wing*)obj)->setSlat(lval); break;
207 case FLAP0: ((Wing*)obj)->setFlap0(lval, rval); break;
208 case FLAP1: ((Wing*)obj)->setFlap1(lval, rval); break;
209 case SPOILER: ((Wing*)obj)->setSpoiler(lval, rval); break;
210 case COLLECTIVE: ((Rotor*)obj)->setCollective(lval); break;
211 case CYCLICAIL: ((Rotor*)obj)->setCyclicail(lval,rval); break;
212 case CYCLICELE: ((Rotor*)obj)->setCyclicele(lval,rval); break;
213 case ROTORBRAKE: ((Rotorgear*)obj)->setRotorBrake(lval); break;
214 case ROTORENGINEON: ((Rotorgear*)obj)->setEngineOn((int)lval); break;
215 case REVERSE_THRUST: ((Jet*)obj)->setReverse(lval != 0); break;
217 ((PistonEngine*)((Thruster*)obj)->getEngine())->setBoost(lval);
220 ((PistonEngine*)((Thruster*)obj)->getEngine())->setWastegate(lval);
226 float ControlMap::rangeMin(int type)
228 // The minimum of the range for each type of control
230 case FLAP0: return -1; // [-1:1]
231 case FLAP1: return -1;
232 case STEER: return -1;
233 case CYCLICELE: return -1;
234 case CYCLICAIL: return -1;
235 case COLLECTIVE: return -1;
236 case MAGNETOS: return 0; // [0:3]
237 default: return 0; // [0:1]
241 float ControlMap::rangeMax(int type)
243 // The maximum of the range for each type of control
245 case FLAP0: return 1; // [-1:1]
246 case FLAP1: return 1;
247 case STEER: return 1;
248 case MAGNETOS: return 3; // [0:3]
249 default: return 1; // [0:1]