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:
|
|
|
|
#
|
2006-12-06 15:58:03 +00:00
|
|
|
# aircraft.light.new("sim/model/foo/beacon", [1, 1]); # anonymous
|
|
|
|
# var strobe = aircraft.light.new("sim/model/foo/strobe", [1, 1]).cont().switch(1);
|
2005-04-06 08:18:06 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
# 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
|
|
|
#
|
2006-12-06 15:58:03 +00:00
|
|
|
# var beacon = aircraft.light.new("sim/model/foo/beacon", [1, 1]);
|
2005-04-06 08:18:06 +00:00
|
|
|
# print(beacon.node.getPath());
|
2005-03-30 09:53:00 +00:00
|
|
|
#
|
2006-12-06 15:58:03 +00:00
|
|
|
# var strobe_node = props.globals.getNode("sim/model/foo/strobe", 1);
|
|
|
|
# var strobe = aircraft.light.new(strobe_node, [0.05, 1.0]);
|
2005-03-30 09:53:00 +00:00
|
|
|
#
|
|
|
|
#
|
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
|
|
|
|
2007-07-06 20:31:14 +00:00
|
|
|
# constants
|
|
|
|
# ==============================================================================
|
|
|
|
var D2R = math.pi / 180;
|
|
|
|
var R2D = 180 / math.pi;
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
#
|
2007-05-11 18:04:51 +00:00
|
|
|
var makeNode = func(n) {
|
|
|
|
if (isa(n, props.Node))
|
|
|
|
return n;
|
|
|
|
else
|
|
|
|
return props.globals.getNode(n, 1);
|
2005-03-30 09:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-06 08:18:06 +00:00
|
|
|
# returns arg[1]-th optional argument of vector arg[0] or default value arg[2]
|
|
|
|
#
|
2007-05-11 18:04:51 +00:00
|
|
|
var optarg = func {
|
|
|
|
if (size(arg[0]) > arg[1] and arg[0][arg[1]] != nil)
|
2005-04-06 08:18:06 +00:00
|
|
|
arg[0][arg[1]];
|
2007-05-11 18:04:51 +00:00
|
|
|
else
|
2005-04-06 08:18:06 +00:00
|
|
|
arg[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-20 20:15:06 +00:00
|
|
|
|
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:
|
2006-12-06 15:58:03 +00:00
|
|
|
# var canopy = aircraft.door.new("sim/model/foo/canopy", 5);
|
2005-03-29 11:26:49 +00:00
|
|
|
# canopy.open();
|
|
|
|
#
|
2007-05-11 18:04:51 +00:00
|
|
|
var door = {
|
2005-03-29 11:26:49 +00:00
|
|
|
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);
|
2007-05-11 18:04:51 +00:00
|
|
|
if (m.enabledN.getValue() == nil)
|
2005-04-06 08:18:06 +00:00
|
|
|
m.enabledN.setBoolValue(1);
|
2007-05-11 18:04:51 +00:00
|
|
|
|
2008-02-05 12:39:56 +00:00
|
|
|
var pos = optarg(arg, 2, 0);
|
2007-05-11 18:04:51 +00:00
|
|
|
if (m.positionN.getValue() == nil)
|
2005-04-06 08:18:06 +00:00
|
|
|
m.positionN.setDoubleValue(pos);
|
2007-05-11 18:04:51 +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
|
2008-02-05 12:39:56 +00:00
|
|
|
enable : func(v) {
|
|
|
|
me.enabledN.setBoolValue(v);
|
|
|
|
me;
|
|
|
|
},
|
2005-04-06 08:18:06 +00:00
|
|
|
# door.setpos(double) -> set ./position-norm without movement
|
2008-02-05 12:39:56 +00:00
|
|
|
setpos : func(pos) {
|
|
|
|
me.positionN.setValue(pos);
|
|
|
|
me.target = pos < 0.5;
|
|
|
|
me;
|
|
|
|
},
|
2005-03-30 09:53:00 +00:00
|
|
|
# double door.getpos() -> return current position as double
|
2008-02-05 12:39:56 +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
|
2008-02-05 12:39:56 +00:00
|
|
|
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
|
2008-02-05 12:39:56 +00:00
|
|
|
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
|
2008-02-05 12:39:56 +00:00
|
|
|
toggle : func { me.move(me.target) },
|
2005-03-29 11:26:49 +00:00
|
|
|
|
2005-04-06 08:18:06 +00:00
|
|
|
# door.stop() -> stop movement
|
2008-02-05 12:39:56 +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
|
2008-02-05 12:39:56 +00:00
|
|
|
move : func(target) {
|
|
|
|
var pos = me.getpos();
|
|
|
|
if (pos != target) {
|
|
|
|
var time = abs(pos - target) * me.swingtime;
|
|
|
|
interpolate(me.positionN, target, 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:
|
2006-12-06 15:58:03 +00:00
|
|
|
# light.new(<property>, <pattern> [, <switch>]);
|
|
|
|
# light.new(<property>, <stretch>, <pattern> [, <switch>]);
|
2005-03-30 09:53:00 +00:00
|
|
|
#
|
2005-04-06 08:18:06 +00:00
|
|
|
# property ... light node: property path or node
|
2006-12-06 15:58:03 +00:00
|
|
|
# stretch ... multiplicator for all pattern values
|
|
|
|
# pattern ... array of on/off time intervals (in seconds)
|
2005-04-06 08:18:06 +00:00
|
|
|
# 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:
|
2006-12-06 15:58:03 +00:00
|
|
|
# aircraft.light.new("sim/model/foo/beacon", [0.4, 0.4]); # anonymous light
|
2006-12-12 22:42:52 +00:00
|
|
|
#-------
|
2006-12-06 15:58:03 +00:00
|
|
|
# var strobe = aircraft.light.new("sim/model/foo/strobe", [0.05, 0.05, 0.05, 1],
|
2005-04-06 08:18:06 +00:00
|
|
|
# "controls/lighting/strobe");
|
|
|
|
# strobe.switch(1);
|
2006-12-12 22:42:52 +00:00
|
|
|
#-------
|
|
|
|
# var switch = props.globals.getNode("controls/lighting/strobe", 1);
|
|
|
|
# var pattern = [0.02, 0.03, 0.02, 1];
|
|
|
|
# aircraft.light.new("sim/model/foo/strobe-top", 1.001, pattern, switch);
|
|
|
|
# aircraft.light.new("sim/model/foo/strobe-bot", 1.005, pattern, switch);
|
2006-12-06 15:58:03 +00:00
|
|
|
#
|
2007-05-11 18:04:51 +00:00
|
|
|
var light = {
|
2005-03-30 09:53:00 +00:00
|
|
|
new : func {
|
2005-04-06 08:18:06 +00:00
|
|
|
m = { parents : [light] };
|
|
|
|
m.node = makeNode(arg[0]);
|
2006-12-06 15:58:03 +00:00
|
|
|
var stretch = 1.0;
|
|
|
|
var c = 1;
|
|
|
|
if (typeof(arg[c]) == "scalar") {
|
|
|
|
stretch = arg[c];
|
|
|
|
c += 1;
|
|
|
|
}
|
|
|
|
if (typeof(arg[c]) != "vector") {
|
|
|
|
die("aircraft.nas: the arguments of aircraft.light.new() have changed!\n" ~
|
|
|
|
" *** BEFORE: aircraft.light.new(property, 0.1, 0.9, switch)\n" ~
|
|
|
|
" *** NOW: aircraft.light.new(property, [0.1, 0.9], switch)");
|
|
|
|
}
|
|
|
|
m.pattern = arg[c];
|
|
|
|
c += 1;
|
2007-05-11 18:04:51 +00:00
|
|
|
if (size(arg) > c and arg[c] != nil)
|
2006-12-06 15:58:03 +00:00
|
|
|
m.switchN = makeNode(arg[c]);
|
2007-05-11 18:04:51 +00:00
|
|
|
else
|
2005-04-06 08:18:06 +00:00
|
|
|
m.switchN = m.node.getNode("enabled", 1);
|
2007-05-11 18:04:51 +00:00
|
|
|
|
|
|
|
if (m.switchN.getValue() == nil)
|
2005-04-06 08:18:06 +00:00
|
|
|
m.switchN.setBoolValue(0);
|
2007-05-11 18:04:51 +00:00
|
|
|
|
2005-04-06 08:18:06 +00:00
|
|
|
m.stateN = m.node.getNode("state", 1);
|
2007-05-11 18:04:51 +00:00
|
|
|
if (m.stateN.getValue() == nil)
|
2005-04-06 08:18:06 +00:00
|
|
|
m.stateN.setBoolValue(0);
|
2007-05-11 18:04:51 +00:00
|
|
|
|
|
|
|
forindex (var i; m.pattern)
|
2006-12-06 15:58:03 +00:00
|
|
|
m.pattern[i] *= stretch;
|
2007-05-11 18:04:51 +00:00
|
|
|
|
2006-12-06 15:58:03 +00:00
|
|
|
m.index = 0;
|
2006-12-03 22:20:16 +00:00
|
|
|
m.loopid = 0;
|
2006-12-06 15:58:03 +00:00
|
|
|
m.continuous = 0;
|
2006-12-05 19:17:17 +00:00
|
|
|
m.lastswitch = 0;
|
2007-04-06 20:03:47 +00:00
|
|
|
m.seqcount = -1;
|
2007-04-06 21:53:46 +00:00
|
|
|
m.endstate = 0;
|
2007-04-06 20:03:47 +00:00
|
|
|
m.count = nil;
|
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-05 19:17:17 +00:00
|
|
|
switch : func(v) { me.switchN.setBoolValue(v); 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-05 19:17:17 +00:00
|
|
|
cont : func {
|
2006-12-05 19:32:44 +00:00
|
|
|
if (!me.continuous) {
|
|
|
|
me.continuous = 1;
|
|
|
|
me.loopid += 1;
|
|
|
|
me.stateN.setBoolValue(me.lastswitch);
|
|
|
|
}
|
2006-12-05 19:17:17 +00:00
|
|
|
me;
|
|
|
|
},
|
2005-03-30 09:53:00 +00:00
|
|
|
|
2005-04-06 08:18:06 +00:00
|
|
|
# light.blink() -> blinking light (default)
|
2007-04-06 21:33:22 +00:00
|
|
|
# light.blink(3) -> when switched on, only run three blink sequences;
|
|
|
|
# second optional arg defines state after the sequences
|
|
|
|
blink : func(count = -1, endstate = 0) {
|
2007-04-06 20:03:47 +00:00
|
|
|
me.seqcount = count;
|
2007-04-06 21:33:22 +00:00
|
|
|
me.endstate = endstate;
|
2006-12-05 19:32:44 +00:00
|
|
|
if (me.continuous) {
|
|
|
|
me.continuous = 0;
|
2006-12-06 15:58:03 +00:00
|
|
|
me.index = 0;
|
|
|
|
me.stateN.setBoolValue(0);
|
2006-12-05 19:32:44 +00:00
|
|
|
me.lastswitch and me._loop_(me.loopid += 1);
|
|
|
|
}
|
2006-12-05 19:17:17 +00:00
|
|
|
me;
|
|
|
|
},
|
2006-12-03 22:20:16 +00:00
|
|
|
|
|
|
|
_switch_ : func {
|
2006-12-04 10:42:05 +00:00
|
|
|
var switch = me.switchN.getBoolValue();
|
2006-12-05 19:17:17 +00:00
|
|
|
switch != me.lastswitch or return;
|
|
|
|
me.lastswitch = switch;
|
2006-12-03 22:20:16 +00:00
|
|
|
me.loopid += 1;
|
2006-12-05 19:17:17 +00:00
|
|
|
if (me.continuous or !switch) {
|
2006-12-04 10:42:05 +00:00
|
|
|
me.stateN.setBoolValue(switch);
|
|
|
|
} elsif (switch) {
|
2006-12-03 22:20:16 +00:00
|
|
|
me.stateN.setBoolValue(0);
|
2006-12-06 15:58:03 +00:00
|
|
|
me.index = 0;
|
2007-04-06 20:03:47 +00:00
|
|
|
me.count = me.seqcount;
|
2006-12-05 19:17:17 +00:00
|
|
|
me._loop_(me.loopid);
|
2006-12-03 22:20:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_loop_ : func(id) {
|
2006-12-05 19:17:17 +00:00
|
|
|
id == me.loopid or return;
|
2007-04-06 20:03:47 +00:00
|
|
|
if (!me.count) {
|
2007-04-06 21:33:22 +00:00
|
|
|
me.loopid += 1;
|
|
|
|
me.stateN.setBoolValue(me.endstate);
|
2007-04-06 20:03:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-12-13 16:15:27 +00:00
|
|
|
me.stateN.setBoolValue(me.index == 2 * int(me.index / 2));
|
2006-12-06 15:58:03 +00:00
|
|
|
settimer(func { me._loop_(id) }, me.pattern[me.index]);
|
|
|
|
if ((me.index += 1) >= size(me.pattern)) {
|
|
|
|
me.index = 0;
|
2007-04-06 20:03:47 +00:00
|
|
|
if (me.count > 0)
|
|
|
|
me.count -= 1;
|
2006-12-06 15:58:03 +00:00
|
|
|
}
|
2005-03-30 09:53:00 +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));
|
|
|
|
#
|
2007-05-11 18:04:51 +00:00
|
|
|
var lowpass = {
|
2006-11-03 17:38:32 +00:00
|
|
|
new : func(coeff) {
|
|
|
|
var m = { parents : [lowpass] };
|
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-12 22:42:52 +00:00
|
|
|
# set() -> sets new average and returns it
|
2006-12-04 10:42:05 +00:00
|
|
|
set : func(v) {
|
|
|
|
me.value = v;
|
|
|
|
},
|
2006-11-03 17:38:32 +00:00
|
|
|
_filter_ : func(v) {
|
2008-04-23 22:01:44 +00:00
|
|
|
var dt = getprop("/sim/time/delta-sec") or 0;
|
2006-11-03 17:38:32 +00:00
|
|
|
var c = dt / (me.coeff + dt);
|
|
|
|
me.value = v * c + me.value * (1 - c);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-07-06 20:31:14 +00:00
|
|
|
# angular lowpass
|
|
|
|
# ==============================================================================
|
|
|
|
# same as above, but for angles. Filters sin/cos separately and calculates the
|
2007-10-07 15:12:05 +00:00
|
|
|
# angle again from them. This avoids unexpected jumps from 179.99 to -180 degree.
|
2007-07-06 20:31:14 +00:00
|
|
|
#
|
|
|
|
var angular_lowpass = {
|
|
|
|
new : func(coeff) {
|
|
|
|
var m = { parents : [angular_lowpass] };
|
2007-07-06 21:28:07 +00:00
|
|
|
m.sin = lowpass.new(coeff);
|
|
|
|
m.cos = lowpass.new(coeff);
|
2008-04-23 22:01:44 +00:00
|
|
|
m.value = nil;
|
2007-07-06 20:31:14 +00:00
|
|
|
return m;
|
|
|
|
},
|
|
|
|
filter : func(v) {
|
|
|
|
v *= D2R;
|
2008-04-23 22:01:44 +00:00
|
|
|
me.value = math.atan2(me.sin.filter(math.sin(v)), me.cos.filter(math.cos(v))) * R2D;
|
2007-07-06 20:31:14 +00:00
|
|
|
},
|
|
|
|
set : func(v) {
|
|
|
|
v *= D2R;
|
|
|
|
me.sin.set(math.sin(v));
|
|
|
|
me.cos.set(math.cos(v));
|
|
|
|
},
|
|
|
|
get : func {
|
2008-04-23 22:01:44 +00:00
|
|
|
me.value;
|
2007-07-06 20:31:14 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2006-11-03 17:38:32 +00:00
|
|
|
|
2007-07-06 20:31:14 +00:00
|
|
|
# data
|
2007-01-20 20:15:06 +00:00
|
|
|
# ==============================================================================
|
|
|
|
# class that loads and saves properties to aircraft-specific data files in
|
|
|
|
# ~/.fgfs/aircraft-data/ (Unix) or %APPDATA%\flightgear.org\aircraft-data\.
|
|
|
|
# There's no public constructor, as the only needed instance gets created
|
|
|
|
# by the system.
|
|
|
|
#
|
|
|
|
# SYNOPSIS:
|
|
|
|
# data.add(<properties>);
|
|
|
|
# data.save([<interval>])
|
|
|
|
#
|
|
|
|
# properties ... about any combination of property nodes (props.Node)
|
|
|
|
# or path name strings, or lists or hashes of them,
|
|
|
|
# lists of lists of them, etc.
|
2007-01-22 16:49:28 +00:00
|
|
|
# interval ... save in <interval> minutes intervals, or only once
|
|
|
|
# if 'nil' or empty (and again at reinit/exit)
|
2007-01-20 20:15:06 +00:00
|
|
|
#
|
2007-01-21 20:06:05 +00:00
|
|
|
# SIGNALS:
|
|
|
|
# /sim/signals/save ... set to 'true' right before saving. Can be used
|
|
|
|
# to update values that are to be saved
|
|
|
|
#
|
2007-01-20 20:15:06 +00:00
|
|
|
# EXAMPLE:
|
|
|
|
# var p = props.globals.getNode("/sim/model", 1);
|
|
|
|
# var vec = [p, p];
|
|
|
|
# var hash = {"foo": p, "bar": p};
|
|
|
|
#
|
|
|
|
# # add properties
|
|
|
|
# aircraft.data.add("/sim/fg-root", p, "/sim/fg-home");
|
|
|
|
# aircraft.data.add(p, vec, hash, "/sim/fg-root");
|
|
|
|
#
|
|
|
|
# # now save only once (and at exit/reinit, which is automatically done)
|
2007-02-07 17:06:52 +00:00
|
|
|
# aircraft.data.save();
|
2007-01-20 20:15:06 +00:00
|
|
|
#
|
2007-02-07 17:06:52 +00:00
|
|
|
# # or save now and every 30 sec (and at exit/reinit)
|
2007-01-20 20:15:06 +00:00
|
|
|
# aircraft.data.save(0.5);
|
|
|
|
#
|
2007-05-11 18:04:51 +00:00
|
|
|
var data = {
|
|
|
|
init : func {
|
|
|
|
me.path = getprop("/sim/fg-home") ~ "/aircraft-data/" ~ getprop("/sim/aircraft") ~ ".xml";
|
|
|
|
me.signalN = props.globals.getNode("/sim/signals/save", 1);
|
|
|
|
me.catalog = [];
|
|
|
|
me.loopid = 0;
|
|
|
|
me.interval = 0;
|
2007-01-20 20:15:06 +00:00
|
|
|
|
2007-10-15 18:27:42 +00:00
|
|
|
setlistener("/sim/signals/reinit", func(n) { n.getBoolValue() and me._save_() });
|
2007-05-11 18:04:51 +00:00
|
|
|
setlistener("/sim/signals/exit", func { me._save_() });
|
2007-01-20 20:15:06 +00:00
|
|
|
},
|
|
|
|
load : func {
|
|
|
|
printlog("warn", "trying to load aircraft data from ", me.path, " (OK if not found)");
|
|
|
|
fgcommand("load", props.Node.new({ "file": me.path }));
|
|
|
|
},
|
2007-01-22 16:49:28 +00:00
|
|
|
save : func(v = nil) {
|
2007-05-08 14:22:54 +00:00
|
|
|
me.loopid += 1;
|
2007-06-20 15:16:29 +00:00
|
|
|
if (v == nil) {
|
|
|
|
me._save_();
|
|
|
|
} else {
|
|
|
|
me.interval = 60 * v;
|
|
|
|
me._loop_(me.loopid);
|
|
|
|
}
|
2007-01-20 20:15:06 +00:00
|
|
|
},
|
|
|
|
_loop_ : func(id) {
|
2007-05-08 14:22:54 +00:00
|
|
|
id == me.loopid or return;
|
2007-01-20 20:15:06 +00:00
|
|
|
me._save_();
|
|
|
|
settimer(func { me._loop_(id) }, me.interval);
|
|
|
|
},
|
|
|
|
_save_ : func {
|
|
|
|
size(me.catalog) or return;
|
2008-02-15 14:09:37 +00:00
|
|
|
printlog("debug", "saving aircraft data to ", me.path);
|
2007-06-06 21:40:47 +00:00
|
|
|
me.signalN.setBoolValue(1);
|
|
|
|
var args = props.Node.new({ "filename": me.path });
|
|
|
|
var data = args.getNode("data", 1);
|
2007-01-20 20:15:06 +00:00
|
|
|
foreach (var c; me.catalog) {
|
2007-05-11 18:04:51 +00:00
|
|
|
if (c[0] == `/`)
|
2007-01-20 20:15:06 +00:00
|
|
|
c = substr(c, 1);
|
2007-05-11 18:04:51 +00:00
|
|
|
|
2007-06-06 21:40:47 +00:00
|
|
|
props.copy(props.globals.getNode(c, 1), data.getNode(c, 1));
|
2007-01-20 20:15:06 +00:00
|
|
|
}
|
2007-06-06 21:40:47 +00:00
|
|
|
fgcommand("savexml", args);
|
2007-01-20 20:15:06 +00:00
|
|
|
},
|
|
|
|
add : func {
|
|
|
|
foreach (var a; arg) {
|
2007-01-22 16:49:28 +00:00
|
|
|
var t = typeof(a);
|
2007-01-20 20:15:06 +00:00
|
|
|
if (isa(a, props.Node)) {
|
|
|
|
append(me.catalog, a.getPath());
|
2007-01-22 16:49:28 +00:00
|
|
|
} elsif (t == "scalar") {
|
2007-01-20 20:15:06 +00:00
|
|
|
append(me.catalog, a);
|
2007-01-22 16:49:28 +00:00
|
|
|
} elsif (t == "vector") {
|
2007-05-11 18:04:51 +00:00
|
|
|
foreach (var i; a)
|
2007-01-20 20:15:06 +00:00
|
|
|
me.add(i);
|
2007-01-22 16:49:28 +00:00
|
|
|
} elsif (t == "hash") {
|
2007-05-11 18:04:51 +00:00
|
|
|
foreach (var i; keys(a))
|
2007-01-20 20:15:06 +00:00
|
|
|
me.add(a[i]);
|
|
|
|
} else {
|
2007-01-22 16:49:28 +00:00
|
|
|
die("aircraft.data.add(): invalid item of type " ~ t);
|
2007-01-20 20:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2007-01-22 16:49:28 +00:00
|
|
|
|
2007-01-21 20:06:05 +00:00
|
|
|
# timer
|
|
|
|
# ==============================================================================
|
|
|
|
# class that implements timer that can be started, stopped, reset, and can
|
|
|
|
# have its value saved to the aircraft specific data file. Saving the value
|
|
|
|
# is done automatically by the aircraft.Data class.
|
|
|
|
#
|
|
|
|
# SYNOPSIS:
|
2007-01-22 16:49:28 +00:00
|
|
|
# timer.new(<property> [, <resolution:double> [, <save:bool>]])
|
2007-01-21 20:06:05 +00:00
|
|
|
#
|
|
|
|
# <property> ... property path or props.Node hash that holds the timer value
|
2007-01-22 16:49:28 +00:00
|
|
|
# <resolution> ... timer update resolution -- interval in seconds in which the
|
|
|
|
# timer property is updated while running (default: 1 s)
|
2007-01-21 20:06:05 +00:00
|
|
|
# <save> ... bool that defines whether the timer value should be saved
|
|
|
|
# and restored next time, as needed for Hobbs meters
|
|
|
|
# (default: 1)
|
2007-01-22 16:49:28 +00:00
|
|
|
#
|
|
|
|
# EXAMPLES:
|
2007-01-21 20:06:05 +00:00
|
|
|
# var hobbs_turbine = aircraft.timer.new("/sim/time/hobbs/turbine[0]", 60);
|
|
|
|
# hobbs_turbine.start();
|
|
|
|
#
|
2007-01-22 16:49:28 +00:00
|
|
|
# aircraft.timer.new("/sim/time/hobbs/battery", 60).start(); # anonymous timer
|
2007-01-21 20:06:05 +00:00
|
|
|
#
|
2007-05-11 18:04:51 +00:00
|
|
|
var timer = {
|
2007-01-21 20:06:05 +00:00
|
|
|
new : func(prop, res = 1, save = 1) {
|
|
|
|
var m = { parents : [timer] };
|
2007-01-26 23:33:25 +00:00
|
|
|
m.node = makeNode(prop);
|
2007-05-11 18:04:51 +00:00
|
|
|
if (m.node.getType() == "NONE")
|
2007-01-26 23:33:25 +00:00
|
|
|
m.node.setDoubleValue(0);
|
2007-05-11 18:04:51 +00:00
|
|
|
|
2007-01-21 20:06:05 +00:00
|
|
|
m.systimeN = props.globals.getNode("/sim/time/elapsed-sec", 1);
|
|
|
|
m.last_systime = nil;
|
|
|
|
m.interval = res;
|
2007-05-08 14:22:54 +00:00
|
|
|
m.loopid = 0;
|
2007-01-21 20:06:05 +00:00
|
|
|
m.running = 0;
|
|
|
|
if (save) {
|
2007-01-26 23:33:25 +00:00
|
|
|
data.add(m.node);
|
2007-01-22 16:49:28 +00:00
|
|
|
m.saveL = setlistener("/sim/signals/save", func { m._save_() });
|
2007-01-21 20:06:05 +00:00
|
|
|
} else {
|
2007-01-22 16:49:28 +00:00
|
|
|
m.saveL = nil;
|
2007-01-21 20:06:05 +00:00
|
|
|
}
|
|
|
|
return m;
|
|
|
|
},
|
|
|
|
del : func {
|
2007-01-26 23:33:25 +00:00
|
|
|
me.stop();
|
2007-05-11 18:04:51 +00:00
|
|
|
if (me.saveL != nil)
|
2007-01-22 16:49:28 +00:00
|
|
|
removelistener(me.saveL);
|
2007-01-21 20:06:05 +00:00
|
|
|
},
|
|
|
|
start : func {
|
|
|
|
me.running and return;
|
|
|
|
me.last_systime = me.systimeN.getValue();
|
2007-05-08 14:22:54 +00:00
|
|
|
me.interval != nil and me._loop_(me.loopid);
|
2007-01-21 20:06:05 +00:00
|
|
|
me.running = 1;
|
|
|
|
me;
|
|
|
|
},
|
|
|
|
stop : func {
|
|
|
|
me.running or return;
|
|
|
|
me.running = 0;
|
2007-05-08 14:22:54 +00:00
|
|
|
me.loopid += 1;
|
2007-01-21 20:06:05 +00:00
|
|
|
me._apply_();
|
2008-02-05 12:39:56 +00:00
|
|
|
me;
|
2007-01-21 20:06:05 +00:00
|
|
|
},
|
|
|
|
reset : func {
|
2007-01-26 23:33:25 +00:00
|
|
|
me.node.setDoubleValue(0);
|
2007-01-21 20:06:05 +00:00
|
|
|
me.last_systime = me.systimeN.getValue();
|
|
|
|
},
|
|
|
|
_apply_ : func {
|
|
|
|
var sys = me.systimeN.getValue();
|
2007-01-26 23:33:25 +00:00
|
|
|
me.node.setDoubleValue(me.node.getValue() + sys - me.last_systime);
|
2007-01-21 20:06:05 +00:00
|
|
|
me.last_systime = sys;
|
|
|
|
},
|
|
|
|
_save_ : func {
|
2007-05-11 18:04:51 +00:00
|
|
|
if (me.running)
|
2007-01-21 20:06:05 +00:00
|
|
|
me._apply_();
|
|
|
|
},
|
|
|
|
_loop_ : func(id) {
|
2007-05-08 14:22:54 +00:00
|
|
|
id != me.loopid and return;
|
2007-01-21 20:06:05 +00:00
|
|
|
me._apply_();
|
|
|
|
settimer(func { me._loop_(id) }, me.interval);
|
|
|
|
},
|
|
|
|
};
|
2007-01-20 20:15:06 +00:00
|
|
|
|
|
|
|
|
2007-01-23 17:29:41 +00:00
|
|
|
|
2007-06-04 13:45:23 +00:00
|
|
|
# livery
|
|
|
|
# =============================================================================
|
|
|
|
# Class that maintains livery XML files (see English Electric Lightning for an
|
2007-06-05 14:21:12 +00:00
|
|
|
# example). The last used livery is saved on exit and restored next time. Livery
|
|
|
|
# files are regular PropertyList XML files whose properties are copied to the
|
|
|
|
# main tree (whereby the node types are ignored).
|
2007-06-04 13:45:23 +00:00
|
|
|
#
|
|
|
|
# SYNOPSIS:
|
2007-06-05 14:21:12 +00:00
|
|
|
# livery.init(<livery-dir> [, <name-path> [, <sort-path>]]);
|
2007-06-04 13:45:23 +00:00
|
|
|
#
|
|
|
|
# <livery-dir> ... directory with livery XML files, relative to $FG_ROOT
|
2007-06-05 14:21:12 +00:00
|
|
|
# <name-path> ... property path to the livery name in the livery files
|
|
|
|
# and the property tree (default: /sim/model/livery/name)
|
|
|
|
# <sort-path> ... property path to the sort criterion (default: same as
|
|
|
|
# <name-path> -- that is: alphabetic sorting)
|
2007-06-04 13:45:23 +00:00
|
|
|
#
|
|
|
|
# EXAMPLE:
|
|
|
|
# aircraft.livery.init("Aircraft/Lightning/Models/Liveries",
|
2007-06-05 14:21:12 +00:00
|
|
|
# "sim/model/livery/variant",
|
|
|
|
# "sim/model/livery/index"); # optional
|
2007-06-04 13:45:23 +00:00
|
|
|
#
|
|
|
|
# aircraft.livery.dialog.toggle();
|
2007-06-05 14:21:12 +00:00
|
|
|
# aircraft.livery.select("OEBH");
|
|
|
|
# aircraft.livery.next();
|
2007-06-04 13:45:23 +00:00
|
|
|
#
|
|
|
|
var livery = {
|
2007-06-05 14:21:12 +00:00
|
|
|
init : func(livery_dir, name_path = "sim/model/livery/name", sort_path = nil) {
|
2007-06-04 13:45:23 +00:00
|
|
|
me.dir = livery_dir;
|
|
|
|
if (me.dir[-1] != `/`)
|
|
|
|
me.dir ~= "/";
|
|
|
|
me.name_path = name_path;
|
2007-06-05 14:21:12 +00:00
|
|
|
me.sort_path = sort_path != nil ? sort_path : name_path;
|
2007-06-04 13:45:23 +00:00
|
|
|
me.rescan();
|
2007-06-05 14:21:12 +00:00
|
|
|
aircraft.data.add(name_path);
|
2007-06-04 13:45:23 +00:00
|
|
|
me.dialog = gui.Dialog.new("livery-select");
|
|
|
|
},
|
|
|
|
rescan : func {
|
2007-06-05 14:21:12 +00:00
|
|
|
me.data = [];
|
2007-06-12 22:08:18 +00:00
|
|
|
var path = getprop("/sim/fg-root") ~ "/" ~ me.dir;
|
|
|
|
foreach (var file; directory(path)) {
|
2007-06-04 13:45:23 +00:00
|
|
|
if (substr(file, -4) != ".xml")
|
|
|
|
continue;
|
2007-06-12 22:08:18 +00:00
|
|
|
var n = props.Node.new({ filename : path ~ file });
|
2007-06-04 13:45:23 +00:00
|
|
|
fgcommand("loadxml", n);
|
|
|
|
n = n.getNode("data");
|
2007-06-05 14:21:12 +00:00
|
|
|
|
2007-06-04 13:45:23 +00:00
|
|
|
var name = n.getNode(me.name_path);
|
2007-06-05 14:21:12 +00:00
|
|
|
var index = n.getNode(me.sort_path);
|
|
|
|
if (name == nil or index == nil)
|
2007-06-04 13:45:23 +00:00
|
|
|
continue;
|
2008-02-29 17:17:40 +00:00
|
|
|
append(me.data, [name.getValue(), index.getValue(), n.getValues(),
|
|
|
|
substr(file, 0, size(file) - 4)]);
|
2007-06-04 13:45:23 +00:00
|
|
|
}
|
2007-06-05 14:21:12 +00:00
|
|
|
me.data = sort(me.data, func(a, b) {
|
|
|
|
num(a[1]) == nil or num(b[1]) == nil ? cmp(a[1], b[1]) : a[1] - b[1];
|
|
|
|
});
|
|
|
|
me.select(getprop(me.name_path));
|
|
|
|
},
|
|
|
|
# select by index (out-of-bounds indices are wrapped)
|
|
|
|
set : func(i) {
|
2007-06-06 21:40:47 +00:00
|
|
|
if (i < 0)
|
2007-12-17 21:49:52 +00:00
|
|
|
i = size(me.data) - 1;
|
2007-06-06 21:40:47 +00:00
|
|
|
if (i >= size(me.data))
|
|
|
|
i = 0;
|
2008-03-01 12:01:23 +00:00
|
|
|
me.current = i;
|
2007-06-05 14:21:12 +00:00
|
|
|
props.globals.setValues(me.data[i][2]);
|
2008-02-29 17:17:40 +00:00
|
|
|
setprop("sim/model/livery/file", me.data[i][3]);
|
2007-06-04 13:45:23 +00:00
|
|
|
},
|
2007-06-05 14:21:12 +00:00
|
|
|
# select by name
|
2007-06-04 13:45:23 +00:00
|
|
|
select : func(name) {
|
2007-06-05 14:21:12 +00:00
|
|
|
forindex (var i; me.data)
|
|
|
|
if (me.data[i][0] == name)
|
|
|
|
me.set(i);
|
|
|
|
},
|
|
|
|
next : func {
|
|
|
|
me.set(me.current + 1);
|
|
|
|
},
|
|
|
|
previous : func {
|
|
|
|
me.set(me.current - 1);
|
2007-06-04 13:45:23 +00:00
|
|
|
},
|
2008-03-01 09:49:57 +00:00
|
|
|
};
|
|
|
|
|
2008-02-29 17:17:40 +00:00
|
|
|
|
2008-03-01 09:49:57 +00:00
|
|
|
|
|
|
|
# livery_update
|
|
|
|
# =============================================================================
|
|
|
|
# Class for maintaining liveries in MP aircraft. It is used in Nasal code that's
|
|
|
|
# embedded in aircraft animation XML files, and checks in intervals whether the
|
|
|
|
# parent aircraft has changed livery, in which case it changes the livery
|
|
|
|
# in the remote aircraft accordingly.
|
|
|
|
#
|
|
|
|
# SYNOPSIS:
|
|
|
|
# livery_update(<livery-dir> [, <interval:10> [, <func>]]);
|
|
|
|
#
|
|
|
|
# <livery-dir> ... directory with livery files, relative to $FG_ROOT
|
|
|
|
# <interval> ... checking interval in seconds (default: 10)
|
|
|
|
# <func> ... callback function that's called with the ./sim/model/livery/file
|
|
|
|
# contents as argument whenever the livery has changed. This can
|
|
|
|
# be used for post-processing.
|
|
|
|
#
|
|
|
|
# EXAMPLE:
|
|
|
|
# <nasal>
|
|
|
|
# <load>
|
|
|
|
# var livery_update = aircraft.livery_update.new(
|
|
|
|
# "Aircraft/R22/Models/Liveries", 30,
|
|
|
|
# func { print("R22 livery update") });
|
|
|
|
# </load>
|
|
|
|
#
|
|
|
|
# <unload>
|
|
|
|
# livery_update.stop();
|
|
|
|
# </unload>
|
|
|
|
# </nasal>
|
|
|
|
#
|
|
|
|
var livery_update = {
|
|
|
|
new : func(liveriesdir, interval = 10, callback = nil) {
|
|
|
|
var m = { parents : [livery_update] };
|
2008-02-29 17:17:40 +00:00
|
|
|
var root = cmdarg();
|
2008-03-01 09:49:57 +00:00
|
|
|
m.root = root.getPath();
|
|
|
|
m.fileN = root.getNode("sim/model/livery/file", 1);
|
|
|
|
m.dir = getprop("/sim/fg-root") ~ "/" ~ liveriesdir ~ "/";
|
|
|
|
m.interval = interval;
|
|
|
|
m.last = "";
|
|
|
|
m.running = 1;
|
|
|
|
m.callback = callback;
|
2008-02-29 17:17:40 +00:00
|
|
|
if (root.getName() == "multiplayer")
|
|
|
|
m._loop_();
|
|
|
|
return m;
|
|
|
|
},
|
2008-03-01 09:49:57 +00:00
|
|
|
stop : func {
|
|
|
|
me.running = 0;
|
|
|
|
},
|
2008-02-29 17:17:40 +00:00
|
|
|
_loop_ : func {
|
2008-03-01 09:49:57 +00:00
|
|
|
me.running or return;
|
|
|
|
var file = me.fileN.getValue();
|
|
|
|
if (file != nil and file != me.last) {
|
|
|
|
fgcommand("loadxml", props.Node.new({ filename: me.dir ~ file ~ ".xml",
|
|
|
|
targetnode: me.root }));
|
|
|
|
me.last = file;
|
|
|
|
if (me.callback != nil)
|
|
|
|
me.callback(file);
|
2008-02-29 17:17:40 +00:00
|
|
|
}
|
2008-03-01 09:49:57 +00:00
|
|
|
settimer(func { me._loop_() }, me.interval);
|
2008-02-29 17:17:40 +00:00
|
|
|
},
|
2007-06-04 13:45:23 +00:00
|
|
|
};
|
|
|
|
|
2008-02-23 18:18:55 +00:00
|
|
|
|
|
|
|
|
2008-02-15 14:09:37 +00:00
|
|
|
# formation
|
|
|
|
# =============================================================================
|
2008-02-23 18:18:55 +00:00
|
|
|
# A modification of the livery class. This class maintains formation
|
2008-02-15 14:09:37 +00:00
|
|
|
# XML files (see Blackburn Buccaneer for an example). Files are regular
|
|
|
|
# PropertyList XML files whose properties are copied to the
|
|
|
|
# main tree (whereby the node types are ignored).
|
|
|
|
#
|
|
|
|
# SYNOPSIS:
|
|
|
|
# formation.init(<formation-dir> [, <name-path> [, <sort-path>]]);
|
|
|
|
#
|
|
|
|
# <formation-dir> ... directory with livery XML files, relative to $FG_ROOT
|
2008-02-23 18:18:55 +00:00
|
|
|
# <name-path> ... property path to the livery name in the livery files
|
|
|
|
# and the property tree (default: /sim/model/formation/name)
|
|
|
|
# <sort-path> ... property path to the sort criterion (default: same as
|
|
|
|
# <name-path> -- that is: alphabetic sorting)
|
2008-02-15 14:09:37 +00:00
|
|
|
#
|
|
|
|
# EXAMPLE:
|
|
|
|
# aircraft.formation.init("Aircraft/Buccaneer/Formations",
|
2008-02-23 18:18:55 +00:00
|
|
|
# "sim/model/formation/variant",
|
|
|
|
# "sim/model/formation/index"); # optional
|
2008-02-15 14:09:37 +00:00
|
|
|
#
|
|
|
|
# aircraft.formation.dialog.toggle();
|
|
|
|
# aircraft.formation.select("take off");
|
|
|
|
# aircraft.formation.next();
|
|
|
|
#
|
|
|
|
var formation = {
|
|
|
|
init : func(formation_dir, name_path = "sim/model/formation/name", sort_path = nil) {
|
|
|
|
me.dir = formation_dir;
|
|
|
|
if (me.dir[-1] != `/`)
|
|
|
|
me.dir ~= "/";
|
|
|
|
me.name_path = name_path;
|
|
|
|
me.sort_path = sort_path != nil ? sort_path : name_path;
|
|
|
|
me.rescan();
|
|
|
|
aircraft.data.add(name_path);
|
|
|
|
me.dialog = gui.Dialog.new("formation-select");
|
|
|
|
},
|
|
|
|
rescan : func {
|
|
|
|
me.data = [];
|
|
|
|
var path = getprop("/sim/fg-root") ~ "/" ~ me.dir;
|
|
|
|
foreach (var file; directory(path)) {
|
|
|
|
if (substr(file, -4) != ".xml")
|
|
|
|
continue;
|
|
|
|
var n = props.Node.new({ filename : path ~ file });
|
|
|
|
fgcommand("loadxml", n);
|
|
|
|
n = n.getNode("data");
|
2007-06-04 13:45:23 +00:00
|
|
|
|
2008-02-15 14:09:37 +00:00
|
|
|
var name = n.getNode(me.name_path);
|
|
|
|
var index = n.getNode(me.sort_path);
|
|
|
|
if (name == nil or index == nil)
|
|
|
|
continue;
|
|
|
|
append(me.data, [name.getValue(), index.getValue(), n.getValues()]);
|
|
|
|
}
|
|
|
|
me.data = sort(me.data, func(a, b) {
|
|
|
|
num(a[1]) == nil or num(b[1]) == nil ? cmp(a[1], b[1]) : a[1] - b[1];
|
|
|
|
});
|
|
|
|
me.select(getprop(me.name_path));
|
|
|
|
},
|
|
|
|
# select by index (out-of-bounds indices are wrapped)
|
|
|
|
set : func(i) {
|
|
|
|
if (i < 0)
|
|
|
|
i = size(me.data) - 1;
|
|
|
|
if (i >= size(me.data))
|
|
|
|
i = 0;
|
|
|
|
props.globals.setValues(me.data[i][2]);
|
|
|
|
me.current = i;
|
|
|
|
},
|
|
|
|
# select by name
|
|
|
|
select : func(name) {
|
|
|
|
forindex (var i; me.data)
|
|
|
|
if (me.data[i][0] == name)
|
|
|
|
me.set(i);
|
|
|
|
},
|
|
|
|
next : func {
|
|
|
|
me.set(me.current + 1);
|
|
|
|
},
|
|
|
|
previous : func {
|
|
|
|
me.set(me.current - 1);
|
|
|
|
},
|
|
|
|
};
|
2007-06-05 14:21:12 +00:00
|
|
|
|
2008-02-23 18:18:55 +00:00
|
|
|
|
|
|
|
|
2007-05-08 09:52:13 +00:00
|
|
|
# steering
|
|
|
|
# =============================================================================
|
2007-05-08 14:22:54 +00:00
|
|
|
# Class that implements differential braking depending on rudder position.
|
|
|
|
# Note that this overrides the controls.applyBrakes() wrapper. If you need
|
|
|
|
# your own version, then override it again after the steering.init() call.
|
2007-05-08 09:52:13 +00:00
|
|
|
#
|
|
|
|
# SYNOPSIS:
|
|
|
|
# steering.init([<property> [, <threshold>]]);
|
|
|
|
#
|
|
|
|
# <property> ... property path or props.Node hash that enables/disables
|
|
|
|
# brake steering (usually bound to the js trigger button)
|
|
|
|
# <threshold> ... defines range (+- threshold) around neutral rudder
|
|
|
|
# position in which both brakes are applied
|
|
|
|
#
|
|
|
|
# EXAMPLES:
|
|
|
|
# aircraft.steering.init("/controls/gear/steering", 0.2);
|
|
|
|
# aircraft.steering.init();
|
|
|
|
#
|
|
|
|
var steering = {
|
|
|
|
init : func(switch = "/controls/gear/brake-steering", threshold = 0.3) {
|
|
|
|
me.threshold = threshold;
|
2007-05-08 14:22:54 +00:00
|
|
|
me.switchN = makeNode(switch);
|
|
|
|
me.switchN.setBoolValue(me.switchN.getBoolValue());
|
2007-05-08 09:52:13 +00:00
|
|
|
me.leftN = props.globals.getNode("/controls/gear/brake-left", 1);
|
|
|
|
me.rightN = props.globals.getNode("/controls/gear/brake-right", 1);
|
|
|
|
me.rudderN = props.globals.getNode("/controls/flight/rudder", 1);
|
|
|
|
me.loopid = 0;
|
2007-05-08 14:22:54 +00:00
|
|
|
|
|
|
|
controls.applyBrakes = func(v, w = 0) {
|
|
|
|
call(func(v, w) (w < 0 ? leftN : w > 0 ? rightN : switchN).setValue(v),
|
|
|
|
[v, w], nil, aircraft.steering);
|
|
|
|
}
|
2007-10-15 18:27:42 +00:00
|
|
|
setlistener(me.switchN, func(n) {
|
2007-05-08 09:52:13 +00:00
|
|
|
me.loopid += 1;
|
2007-10-15 18:27:42 +00:00
|
|
|
if (n.getValue())
|
2007-05-08 09:52:13 +00:00
|
|
|
me._loop_(me.loopid);
|
|
|
|
else
|
|
|
|
me.setbrakes(0, 0);
|
|
|
|
}, 1);
|
|
|
|
},
|
|
|
|
_loop_ : func(id) {
|
2007-05-08 14:22:54 +00:00
|
|
|
id == me.loopid or return;
|
2007-05-08 09:52:13 +00:00
|
|
|
var rudder = me.rudderN.getValue();
|
|
|
|
if (rudder > me.threshold)
|
|
|
|
me.setbrakes(0, rudder);
|
|
|
|
elsif (rudder < -me.threshold)
|
|
|
|
me.setbrakes(-rudder, 0);
|
|
|
|
else
|
|
|
|
me.setbrakes(1, 1);
|
|
|
|
|
|
|
|
settimer(func { me._loop_(id) }, 0);
|
|
|
|
},
|
|
|
|
setbrakes : func(left, right) {
|
|
|
|
me.leftN.setDoubleValue(left);
|
|
|
|
me.rightN.setDoubleValue(right);
|
|
|
|
},
|
|
|
|
};
|
2007-01-23 17:29:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-06-07 10:12:03 +00:00
|
|
|
# autotrim
|
|
|
|
# =============================================================================
|
|
|
|
# Singleton class that supports quick trimming and compensates for the lack
|
|
|
|
# of resistance/force feedback in most joysticks. Normally the pilot trims such
|
|
|
|
# that no real or artificially generated (by means of servo motors and spring
|
|
|
|
# preloading) forces act on the stick/yoke and it is in a comfortable position.
|
|
|
|
# This doesn't work well on computer joysticks.
|
|
|
|
#
|
|
|
|
# SYNOPSIS:
|
|
|
|
# autotrim.start(); # on key/button press
|
|
|
|
# autotrim.stop(); # on key/button release (mod-up)
|
|
|
|
#
|
|
|
|
# USAGE:
|
|
|
|
# (1) move the stick such that the aircraft is in an orientation that
|
|
|
|
# you want to trim for (forward flight, hover, ...)
|
|
|
|
# (2) press autotrim button and keep it pressed
|
|
|
|
# (3) move stick/yoke to neutral position (center)
|
|
|
|
# (4) release autotrim button
|
|
|
|
#
|
|
|
|
var autotrim = {
|
|
|
|
init : func {
|
|
|
|
me.elevator = me.Trim.new("elevator");
|
|
|
|
me.aileron = me.Trim.new("aileron");
|
|
|
|
me.rudder = me.Trim.new("rudder");
|
|
|
|
me.loopid = 0;
|
|
|
|
me.active = 0;
|
|
|
|
},
|
|
|
|
start : func {
|
|
|
|
me.active and return;
|
|
|
|
me.active = 1;
|
|
|
|
me.elevator.start();
|
|
|
|
me.aileron.start();
|
|
|
|
me.rudder.start();
|
|
|
|
me._loop_(me.loopid += 1);
|
|
|
|
},
|
|
|
|
stop : func {
|
|
|
|
me.active or return;
|
|
|
|
me.active = 0;
|
|
|
|
me.loopid += 1;
|
|
|
|
me.update();
|
|
|
|
},
|
|
|
|
_loop_ : func(id) {
|
|
|
|
id == me.loopid or return;
|
|
|
|
me.update();
|
|
|
|
settimer(func { me._loop_(id) }, 0);
|
|
|
|
},
|
|
|
|
update : func {
|
|
|
|
me.elevator.update();
|
|
|
|
me.aileron.update();
|
|
|
|
me.rudder.update();
|
|
|
|
},
|
|
|
|
Trim : {
|
|
|
|
new : func(name) {
|
|
|
|
var m = { parents : [ autotrim.Trim ] };
|
|
|
|
m.trimN = props.globals.getNode("/controls/flight/" ~ name ~ "-trim", 1);
|
|
|
|
m.ctrlN = props.globals.getNode("/controls/flight/" ~ name, 1);
|
|
|
|
return m;
|
|
|
|
},
|
|
|
|
start : func {
|
|
|
|
me.last = me.ctrlN.getValue();
|
|
|
|
},
|
|
|
|
update : func {
|
|
|
|
var v = me.ctrlN.getValue();
|
|
|
|
me.trimN.setDoubleValue(me.trimN.getValue() + me.last - v);
|
|
|
|
me.last = v;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-06-12 16:20:55 +00:00
|
|
|
|
2007-02-08 18:42:02 +00:00
|
|
|
# HUD control class to handle both HUD implementations
|
|
|
|
# ==============================================================================
|
2007-01-23 17:29:41 +00:00
|
|
|
#
|
2007-05-11 18:04:51 +00:00
|
|
|
var HUD = {
|
|
|
|
init : func {
|
|
|
|
me.vis0N = props.globals.getNode("/sim/hud/visibility[0]", 1);
|
|
|
|
me.vis1N = props.globals.getNode("/sim/hud/visibility[1]", 1);
|
|
|
|
me.currcolN = props.globals.getNode("/sim/hud/current-color", 1);
|
|
|
|
me.paletteN = props.globals.getNode("/sim/hud/palette", 1);
|
|
|
|
me.brightnessN = props.globals.getNode("/sim/hud/color/brightness", 1);
|
|
|
|
me.currentN = me.vis0N;
|
2007-01-23 17:29:41 +00:00
|
|
|
},
|
|
|
|
cycle_color : func { # h-key
|
2007-05-11 18:04:51 +00:00
|
|
|
if (!me.currentN.getBoolValue()) # if off, turn on
|
2007-01-23 17:29:41 +00:00
|
|
|
return me.currentN.setBoolValue(1);
|
2007-05-11 18:04:51 +00:00
|
|
|
|
2007-01-23 17:29:41 +00:00
|
|
|
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
|
2007-05-01 19:36:41 +00:00
|
|
|
me.is_active() or return;
|
2007-01-23 17:29:41 +00:00
|
|
|
var br = me.brightnessN.getValue() - 0.2;
|
|
|
|
me.brightnessN.setValue(br > 0.01 ? br : 1);
|
|
|
|
},
|
|
|
|
normal_type : func { # i-key
|
2007-05-01 19:36:41 +00:00
|
|
|
me.is_active() or return;
|
2007-01-23 17:29:41 +00:00
|
|
|
me.oldinit1();
|
|
|
|
me.vis0N.setBoolValue(1);
|
|
|
|
me.vis1N.setBoolValue(0);
|
|
|
|
me.currentN = me.vis0N;
|
|
|
|
},
|
|
|
|
cycle_type : func { # I-key
|
2007-05-01 19:36:41 +00:00
|
|
|
me.is_active() or return;
|
2007-01-23 17:29:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
},
|
2007-06-12 16:20:55 +00:00
|
|
|
oldinit1 : func { fgcommand("hud-init") },
|
|
|
|
oldinit2 : func { fgcommand("hud-init2") },
|
2007-05-01 19:36:41 +00:00
|
|
|
is_active : func { me.vis0N.getValue() or me.vis1N.getValue() },
|
2007-01-23 17:29:41 +00:00
|
|
|
};
|
|
|
|
|
2007-02-08 18:42:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
# module initialization
|
|
|
|
# ==============================================================================
|
|
|
|
#
|
|
|
|
|
2007-05-11 18:04:51 +00:00
|
|
|
_setlistener("/sim/signals/nasal-dir-initialized", func {
|
2007-01-23 17:29:41 +00:00
|
|
|
|
2007-02-08 18:42:02 +00:00
|
|
|
props.globals.getNode("/sim/time/delta-realtime-sec", 1).setDoubleValue(0.00000001);
|
2007-05-11 18:04:51 +00:00
|
|
|
HUD.init();
|
|
|
|
data.init();
|
2007-06-07 10:12:03 +00:00
|
|
|
autotrim.init();
|
2007-02-08 18:42:02 +00:00
|
|
|
|
|
|
|
if (getprop("/sim/startup/save-on-exit")) {
|
|
|
|
data.load();
|
|
|
|
var n = props.globals.getNode("/sim/aircraft-data");
|
2007-05-11 18:04:51 +00:00
|
|
|
if (n != nil)
|
|
|
|
foreach (var c; n.getChildren("path"))
|
|
|
|
if (c.getType() != "NONE")
|
2007-02-08 18:42:02 +00:00
|
|
|
data.add(c.getValue());
|
|
|
|
} else {
|
2007-05-11 18:04:51 +00:00
|
|
|
data._save_ = func {}
|
|
|
|
data._loop_ = func {}
|
2007-02-08 18:42:02 +00:00
|
|
|
}
|
|
|
|
});
|
2007-01-23 17:29:41 +00:00
|
|
|
|
|
|
|
|