2001-12-01 06:22:24 +00:00
|
|
|
#include "Jet.hpp"
|
|
|
|
#include "Thruster.hpp"
|
2001-12-06 18:13:24 +00:00
|
|
|
#include "PropEngine.hpp"
|
2001-12-24 13:54:03 +00:00
|
|
|
#include "PistonEngine.hpp"
|
2001-12-01 06:22:24 +00:00
|
|
|
#include "Gear.hpp"
|
|
|
|
#include "Wing.hpp"
|
|
|
|
#include "Math.hpp"
|
2003-05-16 17:27:17 +00:00
|
|
|
#include "Propeller.hpp"
|
2001-12-01 06:22:24 +00:00
|
|
|
|
|
|
|
#include "ControlMap.hpp"
|
|
|
|
namespace yasim {
|
|
|
|
|
|
|
|
ControlMap::~ControlMap()
|
|
|
|
{
|
2001-12-07 20:00:59 +00:00
|
|
|
int i;
|
|
|
|
for(i=0; i<_inputs.size(); i++) {
|
2001-12-01 06:22:24 +00:00
|
|
|
Vector* v = (Vector*)_inputs.get(i);
|
2001-12-07 20:00:59 +00:00
|
|
|
int j;
|
|
|
|
for(j=0; j<v->size(); j++)
|
2001-12-01 06:22:24 +00:00
|
|
|
delete (MapRec*)v->get(j);
|
|
|
|
delete v;
|
|
|
|
}
|
|
|
|
|
2001-12-24 13:54:03 +00:00
|
|
|
for(i=0; i<_outputs.size(); i++)
|
|
|
|
delete (OutRec*)_outputs.get(i);
|
2001-12-01 06:22:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ControlMap::newInput()
|
|
|
|
{
|
|
|
|
Vector* v = new Vector();
|
|
|
|
return _inputs.add(v);
|
|
|
|
}
|
|
|
|
|
2001-12-24 13:54:03 +00:00
|
|
|
void ControlMap::addMapping(int input, int type, void* object, int options,
|
|
|
|
float src0, float src1, float dst0, float dst1)
|
|
|
|
{
|
|
|
|
addMapping(input, type, object, options);
|
|
|
|
|
|
|
|
// The one we just added is last in the list (ugly, awful hack!)
|
|
|
|
Vector* maps = (Vector*)_inputs.get(input);
|
|
|
|
MapRec* m = (MapRec*)maps->get(maps->size() - 1);
|
|
|
|
|
|
|
|
m->src0 = src0;
|
|
|
|
m->src1 = src1;
|
|
|
|
m->dst0 = dst0;
|
|
|
|
m->dst1 = dst1;
|
|
|
|
}
|
|
|
|
|
2001-12-01 06:22:24 +00:00
|
|
|
void ControlMap::addMapping(int input, int type, void* object, int options)
|
|
|
|
{
|
|
|
|
// See if the output object already exists
|
|
|
|
OutRec* out = 0;
|
2001-12-07 20:00:59 +00:00
|
|
|
int i;
|
|
|
|
for(i=0; i<_outputs.size(); i++) {
|
2001-12-01 06:22:24 +00:00
|
|
|
OutRec* o = (OutRec*)_outputs.get(i);
|
|
|
|
if(o->object == object && o->type == type) {
|
|
|
|
out = o;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create one if it doesn't
|
|
|
|
if(out == 0) {
|
|
|
|
out = new OutRec();
|
|
|
|
out->type = type;
|
|
|
|
out->object = object;
|
2002-03-01 06:47:28 +00:00
|
|
|
out->oldL = out->oldR = out->time = 0;
|
2001-12-01 06:22:24 +00:00
|
|
|
_outputs.add(out);
|
|
|
|
}
|
2001-12-24 13:54:03 +00:00
|
|
|
|
2001-12-01 06:22:24 +00:00
|
|
|
// Make a new input record
|
|
|
|
MapRec* map = new MapRec();
|
|
|
|
map->out = out;
|
2001-12-24 13:54:03 +00:00
|
|
|
map->opt = options;
|
|
|
|
map->idx = out->maps.add(map);
|
|
|
|
|
|
|
|
// The default ranges differ depending on type!
|
2002-03-01 06:47:28 +00:00
|
|
|
map->src1 = map->dst1 = rangeMax(type);
|
|
|
|
map->src0 = map->dst0 = rangeMin(type);
|
2001-12-01 06:22:24 +00:00
|
|
|
|
2001-12-24 13:54:03 +00:00
|
|
|
// And add it to the approproate vectors.
|
2001-12-01 06:22:24 +00:00
|
|
|
Vector* maps = (Vector*)_inputs.get(input);
|
|
|
|
maps->add(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlMap::reset()
|
|
|
|
{
|
|
|
|
// Set all the values to zero
|
2001-12-24 13:54:03 +00:00
|
|
|
for(int i=0; i<_outputs.size(); i++) {
|
2001-12-01 06:22:24 +00:00
|
|
|
OutRec* o = (OutRec*)_outputs.get(i);
|
2001-12-24 13:54:03 +00:00
|
|
|
for(int j=0; j<o->maps.size(); j++)
|
2002-03-01 06:47:28 +00:00
|
|
|
((MapRec*)(o->maps.get(j)))->val = 0;
|
2001-12-01 06:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-24 13:54:03 +00:00
|
|
|
void ControlMap::setInput(int input, float val)
|
2001-12-01 06:22:24 +00:00
|
|
|
{
|
|
|
|
Vector* maps = (Vector*)_inputs.get(input);
|
2001-12-24 13:54:03 +00:00
|
|
|
for(int i=0; i<maps->size(); i++) {
|
|
|
|
MapRec* m = (MapRec*)maps->get(i);
|
|
|
|
|
|
|
|
float val2 = val;
|
|
|
|
|
|
|
|
// Do the scaling operation. Clamp to [src0:src1], rescale to
|
|
|
|
// [0:1] within that range, then map to [dst0:dst1].
|
|
|
|
if(val2 < m->src0) val2 = m->src0;
|
|
|
|
if(val2 > m->src1) val2 = m->src1;
|
|
|
|
val2 = (val2 - m->src0) / (m->src1 - m->src0);
|
|
|
|
val2 = m->dst0 + val2 * (m->dst1 - m->dst0);
|
|
|
|
|
|
|
|
m->val = val2;
|
2001-12-01 06:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-01 06:47:28 +00:00
|
|
|
int ControlMap::getOutputHandle(void* obj, int type)
|
|
|
|
{
|
|
|
|
for(int i=0; i<_outputs.size(); i++) {
|
|
|
|
OutRec* o = (OutRec*)_outputs.get(i);
|
|
|
|
if(o->object == obj && o->type == type)
|
|
|
|
return i;
|
|
|
|
}
|
2002-05-10 23:35:06 +00:00
|
|
|
return 0;
|
2002-03-01 06:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControlMap::setTransitionTime(int handle, float time)
|
|
|
|
{
|
|
|
|
((OutRec*)_outputs.get(handle))->time = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ControlMap::getOutput(int handle)
|
|
|
|
{
|
|
|
|
return ((OutRec*)_outputs.get(handle))->oldL;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ControlMap::getOutputR(int handle)
|
|
|
|
{
|
|
|
|
return ((OutRec*)_outputs.get(handle))->oldR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlMap::applyControls(float dt)
|
2001-12-01 06:22:24 +00:00
|
|
|
{
|
2001-12-07 20:00:59 +00:00
|
|
|
int outrec;
|
|
|
|
for(outrec=0; outrec<_outputs.size(); outrec++) {
|
2001-12-01 06:22:24 +00:00
|
|
|
OutRec* o = (OutRec*)_outputs.get(outrec);
|
|
|
|
|
|
|
|
// Generate a summed value. Note the check for "split"
|
|
|
|
// control axes like ailerons.
|
|
|
|
float lval = 0, rval = 0;
|
2001-12-07 20:00:59 +00:00
|
|
|
int i;
|
2001-12-24 13:54:03 +00:00
|
|
|
for(i=0; i<o->maps.size(); i++) {
|
|
|
|
MapRec* m = (MapRec*)o->maps.get(i);
|
|
|
|
float val = m->val;
|
|
|
|
|
|
|
|
if(m->opt & OPT_SQUARE)
|
2001-12-01 06:22:24 +00:00
|
|
|
val = val * Math::abs(val);
|
2001-12-24 13:54:03 +00:00
|
|
|
if(m->opt & OPT_INVERT)
|
2001-12-01 06:22:24 +00:00
|
|
|
val = -val;
|
|
|
|
lval += val;
|
2001-12-24 13:54:03 +00:00
|
|
|
if(m->opt & OPT_SPLIT)
|
2001-12-01 06:22:24 +00:00
|
|
|
rval -= val;
|
|
|
|
else
|
|
|
|
rval += val;
|
|
|
|
}
|
|
|
|
|
2002-03-01 06:47:28 +00:00
|
|
|
// If there is a finite transition time, clamp the values to
|
|
|
|
// the maximum travel allowed in this dt.
|
|
|
|
if(o->time > 0) {
|
|
|
|
float dl = lval - o->oldL;
|
|
|
|
float dr = rval - o->oldR;
|
|
|
|
float adl = Math::abs(dl);
|
|
|
|
float adr = Math::abs(dr);
|
|
|
|
|
|
|
|
float max = (dt/o->time) * (rangeMax(o->type) - rangeMin(o->type));
|
|
|
|
if(adl > max) dl = dl*max/adl;
|
|
|
|
if(adr > max) dr = dr*max/adr;
|
|
|
|
|
|
|
|
lval = o->oldL + dl;
|
|
|
|
rval = o->oldR + dr;
|
|
|
|
}
|
|
|
|
|
|
|
|
o->oldL = lval;
|
|
|
|
o->oldR = rval;
|
|
|
|
|
2001-12-01 06:22:24 +00:00
|
|
|
void* obj = o->object;
|
|
|
|
switch(o->type) {
|
2002-02-20 04:27:22 +00:00
|
|
|
case THROTTLE: ((Thruster*)obj)->setThrottle(lval); break;
|
|
|
|
case MIXTURE: ((Thruster*)obj)->setMixture(lval); break;
|
2002-05-10 23:35:06 +00:00
|
|
|
case STARTER: ((Thruster*)obj)->setStarter(lval != 0.0); break;
|
2002-02-27 00:18:57 +00:00
|
|
|
case MAGNETOS: ((PropEngine*)obj)->setMagnetos((int)lval); break;
|
2002-02-20 04:27:22 +00:00
|
|
|
case ADVANCE: ((PropEngine*)obj)->setAdvance(lval); break;
|
2003-05-16 17:27:17 +00:00
|
|
|
case PROPPITCH: ((PropEngine*)obj)->setPropPitch(lval); break;
|
2002-02-20 04:27:22 +00:00
|
|
|
case REHEAT: ((Jet*)obj)->setReheat(lval); break;
|
|
|
|
case VECTOR: ((Jet*)obj)->setRotation(lval); break;
|
|
|
|
case BRAKE: ((Gear*)obj)->setBrake(lval); break;
|
|
|
|
case STEER: ((Gear*)obj)->setRotation(lval); break;
|
|
|
|
case EXTEND: ((Gear*)obj)->setExtension(lval); break;
|
2002-05-23 19:30:56 +00:00
|
|
|
case CASTERING:((Gear*)obj)->setCastering(lval != 0); break;
|
2002-02-20 04:27:22 +00:00
|
|
|
case SLAT: ((Wing*)obj)->setSlat(lval); break;
|
|
|
|
case FLAP0: ((Wing*)obj)->setFlap0(lval, rval); break;
|
|
|
|
case FLAP1: ((Wing*)obj)->setFlap1(lval, rval); break;
|
|
|
|
case SPOILER: ((Wing*)obj)->setSpoiler(lval, rval); break;
|
2001-12-24 13:54:03 +00:00
|
|
|
case BOOST:
|
|
|
|
((Thruster*)obj)->getPistonEngine()->setBoost(lval);
|
|
|
|
break;
|
2001-12-01 06:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-01 06:47:28 +00:00
|
|
|
float ControlMap::rangeMin(int type)
|
|
|
|
{
|
|
|
|
// The minimum of the range for each type of control
|
|
|
|
switch(type) {
|
|
|
|
case FLAP0: return -1; // [-1:1]
|
|
|
|
case FLAP1: return -1;
|
|
|
|
case STEER: return -1;
|
|
|
|
case MAGNETOS: return 0; // [0:3]
|
|
|
|
default: return 0; // [0:1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float ControlMap::rangeMax(int type)
|
|
|
|
{
|
|
|
|
// The maximum of the range for each type of control
|
|
|
|
switch(type) {
|
|
|
|
case FLAP0: return 1; // [-1:1]
|
|
|
|
case FLAP1: return 1;
|
|
|
|
case STEER: return 1;
|
|
|
|
case MAGNETOS: return 3; // [0:3]
|
|
|
|
default: return 1; // [0:1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace yasim
|