2005-04-06 08:18:06 +00:00
|
|
|
# These classes provide basic functions for use in aircraft specific
|
2005-03-30 09:53:00 +00:00
|
|
|
# Nasal context. Note that even if a class is called "door" or "light"
|
2005-04-06 08:18:06 +00:00
|
|
|
# this doesn't mean that it can't be used for other purposes.
|
2005-03-30 09:53:00 +00:00
|
|
|
#
|
2005-04-06 08:18:06 +00:00
|
|
|
# Class instances don't have to be assigned to variables. They do also
|
|
|
|
# work if they remain anonymous. It's even a good idea to keep them
|
|
|
|
# anonymous if you don't need further access to their members. On the
|
|
|
|
# other hand, you can assign the class and apply setters at the same time:
|
|
|
|
#
|
|
|
|
# aircraft.light.new("sim/model/foo/beacon"); # anonymous
|
|
|
|
# strobe = aircraft.light.new("sim/model/foo/strobe").cont().switch(1);
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Classes do create properties, but they don't usually overwrite the contents
|
|
|
|
# of an existing property. This makes it possible to preset them in
|
|
|
|
# a *-set.xml file or on the command line. For example:
|
|
|
|
#
|
|
|
|
# $ fgfs --aircraft=bo105 --prop:/controls/doors/door[0]/position-norm=1
|
2005-03-30 09:53:00 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
# Wherever a property argument can be given, this can either be a path,
|
2005-04-06 08:18:06 +00:00
|
|
|
# or a node (i.e. property node hash). In return, the property node can
|
|
|
|
# always be accessed directly as member "node", and turned into a path
|
|
|
|
# string with node.getPath():
|
2005-03-30 09:53:00 +00:00
|
|
|
#
|
|
|
|
# beacon = aircraft.light.new("sim/model/foo/beacon");
|
2005-04-06 08:18:06 +00:00
|
|
|
# print(beacon.node.getPath());
|
2005-03-30 09:53:00 +00:00
|
|
|
#
|
|
|
|
# strobe_node = props.globals.getNode("sim/model/foo/strobe", 1);
|
|
|
|
# strobe = aircraft.light.new(strobe_node, 0.05, 1.0);
|
|
|
|
#
|
|
|
|
#
|
2005-04-06 08:18:06 +00:00
|
|
|
# The classes implement only commonly used features, but are easy to
|
|
|
|
# extend, as all class members are accessible from outside. For example:
|
|
|
|
#
|
|
|
|
# # add custom property to door node:
|
|
|
|
# frontdoor.node.getNode("name", 1).setValue("front door");
|
|
|
|
#
|
|
|
|
# # add method to class instance (or base class -> aircraft.door.print)
|
|
|
|
# frontdoor.print = func { print(me.position.getValue()) };
|
|
|
|
#
|
|
|
|
#
|
2005-03-30 09:53:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
# helper functions
|
|
|
|
# ==============================================================================
|
2005-04-06 08:18:06 +00:00
|
|
|
|
2005-03-30 09:53:00 +00:00
|
|
|
# creates (if necessary) and returns a property node from arg[0],
|
|
|
|
# which can be a property node already, or a property path
|
|
|
|
#
|
|
|
|
makeNode = func {
|
|
|
|
if (isa(arg[0], props.Node)) {
|
|
|
|
return arg[0];
|
|
|
|
} else {
|
|
|
|
return props.globals.getNode(arg[0], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-06 08:18:06 +00:00
|
|
|
# returns arg[1]-th optional argument of vector arg[0] or default value arg[2]
|
|
|
|
#
|
|
|
|
optarg = func {
|
|
|
|
if (size(arg[0]) > arg[1] and arg[0][arg[1]] != nil) {
|
|
|
|
arg[0][arg[1]];
|
|
|
|
} else {
|
|
|
|
arg[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-30 09:53:00 +00:00
|
|
|
|
|
|
|
# door
|
|
|
|
# ==============================================================================
|
|
|
|
# class for objects moving at constant speed, with the ability to
|
2005-03-29 11:26:49 +00:00
|
|
|
# reverse moving direction at any point. Appropriate for doors, canopies, etc.
|
|
|
|
#
|
|
|
|
# SYNOPSIS:
|
|
|
|
# door.new(<property>, <swingtime> [, <startpos>]);
|
|
|
|
#
|
2005-04-06 08:18:06 +00:00
|
|
|
# property ... door node: property path or node
|
|
|
|
# swingtime ... time in seconds for full movement (0 -> 1)
|
|
|
|
# startpos ... initial position (default: 0)
|
2005-03-29 11:26:49 +00:00
|
|
|
#
|
|
|
|
# PROPERTIES:
|
2005-04-06 08:18:06 +00:00
|
|
|
# ./position-norm (double) (default: <startpos>)
|
|
|
|
# ./enabled (bool) (default: 1)
|
2005-03-29 11:26:49 +00:00
|
|
|
#
|
|
|
|
# EXAMPLE:
|
|
|
|
# canopy = aircraft.door.new("sim/model/foo/canopy", 5);
|
|
|
|
# canopy.open();
|
|
|
|
#
|
|
|
|
door = {
|
|
|
|
new : func {
|
2005-04-06 08:18:06 +00:00
|
|
|
m = { parents : [door] };
|
|
|
|
m.node = makeNode(arg[0]);
|
|
|
|
m.swingtime = arg[1];
|
|
|
|
m.positionN = m.node.getNode("position-norm", 1);
|
|
|
|
m.enabledN = m.node.getNode("enabled", 1);
|
|
|
|
if (m.enabledN.getValue() == nil) {
|
|
|
|
m.enabledN.setBoolValue(1);
|
2005-03-30 09:53:00 +00:00
|
|
|
}
|
2005-04-06 08:18:06 +00:00
|
|
|
pos = optarg(arg, 2, 0);
|
|
|
|
if (m.positionN.getValue() == nil) {
|
|
|
|
m.positionN.setDoubleValue(pos);
|
2005-03-30 09:53:00 +00:00
|
|
|
}
|
2005-04-06 08:18:06 +00:00
|
|
|
m.target = pos < 0.5;
|
|
|
|
return m;
|
2005-03-29 11:26:49 +00:00
|
|
|
},
|
2005-04-06 08:18:06 +00:00
|
|
|
# door.enable(bool) -> set ./enabled
|
|
|
|
enable : func { me.enabledN.setBoolValue(arg[0]); me },
|
2005-03-29 11:26:49 +00:00
|
|
|
|
2005-04-06 08:18:06 +00:00
|
|
|
# door.setpos(double) -> set ./position-norm without movement
|
|
|
|
setpos : func { me.positionN.setValue(arg[0]); me.target = arg[0] < 0.5; me },
|
2005-03-29 11:26:49 +00:00
|
|
|
|
2005-03-30 09:53:00 +00:00
|
|
|
# double door.getpos() -> return current position as double
|
2005-04-06 08:18:06 +00:00
|
|
|
getpos : func { me.positionN.getValue() },
|
2005-03-29 11:26:49 +00:00
|
|
|
|
2005-03-30 09:53:00 +00:00
|
|
|
# door.close() -> move to closed state
|
|
|
|
close : func { me.move(me.target = 0) },
|
2005-03-29 11:26:49 +00:00
|
|
|
|
2005-03-30 09:53:00 +00:00
|
|
|
# door.open() -> move to open state
|
|
|
|
open : func { me.move(me.target = 1) },
|
2005-03-29 11:26:49 +00:00
|
|
|
|
2005-03-30 09:53:00 +00:00
|
|
|
# door.toggle() -> move to opposite end position
|
2005-03-29 11:26:49 +00:00
|
|
|
toggle : func { me.move(me.target) },
|
|
|
|
|
2005-04-06 08:18:06 +00:00
|
|
|
# door.stop() -> stop movement
|
2005-06-30 00:18:20 +00:00
|
|
|
stop : func { interpolate(me.positionN) },
|
2005-04-06 08:18:06 +00:00
|
|
|
|
2005-03-30 09:53:00 +00:00
|
|
|
# door.move(double) -> move to arbitrary position
|
2005-03-29 11:26:49 +00:00
|
|
|
move : func {
|
|
|
|
time = abs(me.getpos() - arg[0]) * me.swingtime;
|
2005-04-06 08:18:06 +00:00
|
|
|
interpolate(me.positionN, arg[0], time);
|
2005-03-29 11:26:49 +00:00
|
|
|
me.target = !me.target;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-03-30 09:53:00 +00:00
|
|
|
|
|
|
|
# light
|
|
|
|
# ==============================================================================
|
|
|
|
# class for generation of pulsing values. Appropriate for controlling
|
|
|
|
# beacons, strobes, etc.
|
|
|
|
#
|
|
|
|
# SYNOPSIS:
|
|
|
|
# light.new(<property> [, <ontime> [, <offtime> [, <switch>]]]);
|
|
|
|
#
|
2005-04-06 08:18:06 +00:00
|
|
|
# property ... light node: property path or node
|
|
|
|
# ontime ... time that the light is on when blinking (default: 0.5 [s])
|
|
|
|
# offtime ... time that the light is off when blinking (default: <ontime>)
|
|
|
|
# switch ... property path or node to use as switch (default: ./enabled)
|
|
|
|
# instead of ./enabled
|
2005-03-30 09:53:00 +00:00
|
|
|
#
|
|
|
|
# PROPERTIES:
|
2005-04-06 08:18:06 +00:00
|
|
|
# ./state (bool) (default: 0)
|
|
|
|
# ./enabled (bool) (default: 0) except if <switch> given)
|
2005-03-30 09:53:00 +00:00
|
|
|
#
|
|
|
|
# EXAMPLES:
|
2005-04-06 08:18:06 +00:00
|
|
|
# aircraft.light.new("sim/model/foo/beacon", 0.4); # anonymous light
|
|
|
|
# strobe = aircraft.light.new("sim/model/foo/strobe", 0.05, 1.0,
|
|
|
|
# "controls/lighting/strobe");
|
|
|
|
# strobe.switch(1);
|
2005-03-30 09:53:00 +00:00
|
|
|
#
|
|
|
|
light = {
|
|
|
|
new : func {
|
2005-04-06 08:18:06 +00:00
|
|
|
m = { parents : [light] };
|
|
|
|
m.node = makeNode(arg[0]);
|
|
|
|
m.ontime = optarg(arg, 1, 0.5);
|
|
|
|
m.offtime = optarg(arg, 2, m.ontime);
|
2005-03-30 09:53:00 +00:00
|
|
|
if (size(arg) > 3 and arg[3] != nil) {
|
2005-04-06 08:18:06 +00:00
|
|
|
m.switchN = makeNode(arg[3]);
|
2005-03-30 09:53:00 +00:00
|
|
|
} else {
|
2005-04-06 08:18:06 +00:00
|
|
|
m.switchN = m.node.getNode("enabled", 1);
|
2005-03-30 09:53:00 +00:00
|
|
|
}
|
2005-04-06 08:18:06 +00:00
|
|
|
if (m.switchN.getValue() == nil) {
|
|
|
|
m.switchN.setBoolValue(0);
|
2005-03-30 09:53:00 +00:00
|
|
|
}
|
2005-04-06 08:18:06 +00:00
|
|
|
m.stateN = m.node.getNode("state", 1);
|
|
|
|
if (m.stateN.getValue() == nil) {
|
|
|
|
m.stateN.setBoolValue(0);
|
2005-03-30 09:53:00 +00:00
|
|
|
}
|
2005-04-06 08:18:06 +00:00
|
|
|
m.continuous = 0;
|
2006-12-03 22:20:16 +00:00
|
|
|
m.loopid = 0;
|
2006-12-04 10:42:05 +00:00
|
|
|
m.switch = -1;
|
2006-12-03 22:20:16 +00:00
|
|
|
m.switchL = setlistener(m.switchN, func { m._switch_() }, 1);
|
2005-04-06 08:18:06 +00:00
|
|
|
return m;
|
2005-03-30 09:53:00 +00:00
|
|
|
},
|
2006-12-03 22:20:16 +00:00
|
|
|
# class destructor
|
|
|
|
del : func {
|
|
|
|
removelistener(me.switchL);
|
|
|
|
},
|
2005-04-06 08:18:06 +00:00
|
|
|
# light.switch(bool) -> set light switch (also affects other lights
|
2005-03-30 09:53:00 +00:00
|
|
|
# that use the same switch)
|
2006-12-03 22:20:16 +00:00
|
|
|
switch : func { me.switchN.setBoolValue(arg[0]); me },
|
2005-03-30 09:53:00 +00:00
|
|
|
|
|
|
|
# light.toggle() -> toggle light switch
|
2006-12-03 22:20:16 +00:00
|
|
|
toggle : func { me.switchN.setBoolValue(!me.switchN.getValue()); me },
|
2005-03-30 09:53:00 +00:00
|
|
|
|
|
|
|
# light.cont() -> continuous light
|
2006-12-03 22:20:16 +00:00
|
|
|
cont : func { if (!me.continuous) { me.continuous = 1; me._switch_(); } me },
|
2005-03-30 09:53:00 +00:00
|
|
|
|
2005-04-06 08:18:06 +00:00
|
|
|
# light.blink() -> blinking light (default)
|
2006-12-03 22:20:16 +00:00
|
|
|
blink : func { if (me.continuous) { me.continuous = 0; me._switch_(); } me },
|
|
|
|
|
|
|
|
_switch_ : func {
|
2006-12-04 10:42:05 +00:00
|
|
|
var switch = me.switchN.getBoolValue();
|
|
|
|
if (me.switch == switch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
me.switch = switch;
|
2006-12-03 22:20:16 +00:00
|
|
|
me.loopid += 1;
|
|
|
|
if (me.continuous) {
|
2006-12-04 10:42:05 +00:00
|
|
|
me.stateN.setBoolValue(switch);
|
|
|
|
} elsif (switch) {
|
2006-12-03 22:20:16 +00:00
|
|
|
me._loop_(me.loopid);
|
|
|
|
} else {
|
|
|
|
me.stateN.setBoolValue(0);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_loop_ : func(id) {
|
|
|
|
if (id != me.loopid) {
|
|
|
|
return;
|
|
|
|
}
|
2006-12-04 10:42:05 +00:00
|
|
|
me.stateN.setBoolValue(var state = !me.stateN.getBoolValue());
|
|
|
|
settimer(func { me._loop_(id) }, state ? me.ontime : me.offtime);
|
2005-03-30 09:53:00 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-08-21 08:35:53 +00:00
|
|
|
|
2006-11-03 17:38:32 +00:00
|
|
|
# lowpass
|
|
|
|
# ==============================================================================
|
|
|
|
# class that implements a variable-interval EWMA (Exponentially Weighted
|
|
|
|
# Moving Average) lowpass filter with characteristics independent of the
|
|
|
|
# frame rate.
|
|
|
|
#
|
|
|
|
# SYNOPSIS:
|
|
|
|
# lowpass.new(<coefficient>);
|
|
|
|
#
|
|
|
|
# EXAMPLE:
|
2006-11-03 19:39:08 +00:00
|
|
|
# var lp = aircraft.lowpass.new(0.5);
|
2006-11-04 11:33:08 +00:00
|
|
|
# print(lp.filter(10)); # prints 10
|
2006-11-03 17:38:32 +00:00
|
|
|
# print(lp.filter(0));
|
|
|
|
#
|
|
|
|
lowpass = {
|
|
|
|
new : func(coeff) {
|
|
|
|
var m = { parents : [lowpass] };
|
|
|
|
m.dtN = props.globals.getNode("/sim/time/delta-realtime-sec", 1);
|
2006-11-04 11:33:08 +00:00
|
|
|
m.coeff = coeff >= 0 ? coeff : die("aircraft.lowpass(): coefficient must be >= 0");
|
2006-11-03 17:38:32 +00:00
|
|
|
m.value = nil;
|
|
|
|
return m;
|
|
|
|
},
|
|
|
|
# filter(raw_value) -> push new value, returns filtered value
|
|
|
|
filter : func(v) {
|
|
|
|
me.filter = me._filter_;
|
|
|
|
me.value = v;
|
|
|
|
},
|
|
|
|
# get() -> returns filtered value
|
|
|
|
get : func {
|
|
|
|
me.value;
|
|
|
|
},
|
2006-12-04 10:42:05 +00:00
|
|
|
# set() -> sets new average
|
|
|
|
set : func(v) {
|
|
|
|
me.value = v;
|
|
|
|
},
|
2006-11-03 17:38:32 +00:00
|
|
|
_filter_ : func(v) {
|
|
|
|
var dt = me.dtN.getValue();
|
|
|
|
var c = dt / (me.coeff + dt);
|
|
|
|
me.value = v * c + me.value * (1 - c);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-08-21 08:35:53 +00:00
|
|
|
# HUD control class to handle both HUD implementations.
|
|
|
|
#
|
|
|
|
HUDControl = {
|
|
|
|
new : func {
|
|
|
|
var m = { parents : [HUDControl] };
|
|
|
|
m.vis0N = props.globals.getNode("/sim/hud/visibility[0]", 1);
|
|
|
|
m.vis1N = props.globals.getNode("/sim/hud/visibility[1]", 1);
|
|
|
|
m.currcolN = props.globals.getNode("/sim/hud/current-color", 1);
|
|
|
|
m.paletteN = props.globals.getNode("/sim/hud/palette", 1);
|
|
|
|
m.brightnessN = props.globals.getNode("/sim/hud/color/brightness", 1);
|
|
|
|
m.currentN = m.vis0N;
|
|
|
|
return m;
|
|
|
|
},
|
|
|
|
cycle_color : func { # h-key
|
|
|
|
if (!me.currentN.getBoolValue()) { # if off, turn on
|
|
|
|
return me.currentN.setBoolValue(1);
|
|
|
|
}
|
|
|
|
var i = me.currcolN.getValue() + 1; # if through, turn off
|
|
|
|
if (i < 0 or i >= size(me.paletteN.getChildren("color"))) {
|
|
|
|
me.currentN.setBoolValue(0);
|
|
|
|
me.currcolN.setIntValue(0);
|
|
|
|
} else { # otherwise change color
|
|
|
|
me.currentN.setBoolValue(1);
|
|
|
|
me.currcolN.setIntValue(i);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
cycle_brightness : func { # H-key
|
|
|
|
var br = me.brightnessN.getValue() - 0.2;
|
|
|
|
me.brightnessN.setValue(br > 0.01 ? br : 1);
|
|
|
|
},
|
|
|
|
normal_type : func { # i-key
|
|
|
|
me.oldinit1();
|
|
|
|
me.vis0N.setBoolValue(1);
|
|
|
|
me.vis1N.setBoolValue(0);
|
|
|
|
me.currentN = me.vis0N;
|
|
|
|
},
|
|
|
|
cycle_type : func { # I-key
|
|
|
|
if (me.currentN == me.vis0N) {
|
|
|
|
me.vis0N.setBoolValue(0);
|
|
|
|
me.vis1N.setBoolValue(1);
|
|
|
|
me.currentN = me.vis1N;
|
|
|
|
} elsif (me.currentN == me.vis1N) {
|
|
|
|
me.vis0N.setBoolValue(1);
|
|
|
|
me.vis1N.setBoolValue(0);
|
|
|
|
me.oldinit2();
|
|
|
|
me.currentN = me.vis0N;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
oldinit1 : func { fgcommand("hud-init", props.Node.new()) },
|
|
|
|
oldinit2 : func { fgcommand("hud-init2", props.Node.new()) },
|
|
|
|
};
|
|
|
|
|
|
|
|
var HUD = nil;
|
|
|
|
settimer(func { HUD = HUDControl.new() }, 0);
|
|
|
|
|
|
|
|
|