2003-12-01 14:36:22 +00:00
|
|
|
##
|
|
|
|
# Returns true if the first object is an instance of the second
|
|
|
|
# (class) object. Example: isa(someObject, props.Node)
|
|
|
|
#
|
2007-10-15 16:30:37 +00:00
|
|
|
var isa = func(obj, class) {
|
2007-06-07 16:59:07 +00:00
|
|
|
if(obj == nil or !contains(obj, "parents")) { return 0; }
|
2003-12-01 14:36:22 +00:00
|
|
|
foreach(c; obj.parents) {
|
|
|
|
if(c == class) { return 1; }
|
|
|
|
elsif(isa(obj, c)) { return 1; }
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
##
|
|
|
|
# Invokes a FlightGear command specified by the first argument. The
|
|
|
|
# second argument specifies the property tree to be passed to the
|
|
|
|
# command as its argument. It may be either a props.Node object or a
|
|
|
|
# string, in which case it specifies a path in the global property
|
|
|
|
# tree.
|
|
|
|
#
|
2007-10-15 16:30:37 +00:00
|
|
|
var fgcommand = func(cmd, node=nil) {
|
2007-06-07 16:59:07 +00:00
|
|
|
if(isa(node, props.Node)) node = node._g;
|
|
|
|
_fgcommand(cmd, node);
|
2003-12-01 14:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns the SGPropertyNode argument to the currently executing
|
|
|
|
# function. Wrapper for the internal _cmdarg function that retrieves
|
2007-06-22 18:49:38 +00:00
|
|
|
# the ghost handle to the argument and wraps it in a
|
2003-12-01 14:36:22 +00:00
|
|
|
# props.Node object.
|
|
|
|
#
|
2007-10-15 16:30:37 +00:00
|
|
|
var cmdarg = func { props.wrapNode(_cmdarg()) }
|
2003-12-05 02:38:35 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Utility. Does what it you think it does.
|
|
|
|
#
|
2007-10-15 16:30:37 +00:00
|
|
|
var abs = func(v) { return v < 0 ? -v : v }
|
2003-12-05 02:38:35 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Convenience wrapper for the _interpolate function. Takes a
|
|
|
|
# single string or props.Node object in arg[0] indicating a target
|
|
|
|
# property, and a variable-length list of time/value pairs. Example:
|
|
|
|
#
|
|
|
|
# interpolate("/animations/radar/angle",
|
|
|
|
# 180, 1, 360, 1, 0, 0,
|
|
|
|
# 180, 1, 360, 1, 0, 0,
|
|
|
|
# 180, 1, 360, 1, 0, 0,
|
|
|
|
# 180, 1, 360, 1, 0, 0,
|
|
|
|
# 180, 1, 360, 1, 0, 0,
|
|
|
|
# 180, 1, 360, 1, 0, 0,
|
|
|
|
# 180, 1, 360, 1, 0, 0,
|
|
|
|
# 180, 1, 360, 1, 0, 0);
|
|
|
|
#
|
|
|
|
# This will swing the "radar dish" smoothly through 8 revolutions over
|
|
|
|
# 16 seconds. Note the use of zero-time interpolation between 360 and
|
|
|
|
# 0 to wrap the interpolated value properly.
|
|
|
|
#
|
2007-10-15 16:30:37 +00:00
|
|
|
var interpolate = func(node, val...) {
|
|
|
|
if(isa(node, props.Node)) node = node._g;
|
|
|
|
elsif(typeof(node) != "scalar") return;
|
|
|
|
_interpolate(node, val);
|
2003-12-05 02:38:35 +00:00
|
|
|
}
|
2005-06-12 18:17:00 +00:00
|
|
|
|
2005-12-16 19:15:08 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Convenience wrapper for the _setlistener function. Takes a
|
|
|
|
# single string or props.Node object in arg[0] indicating the
|
2006-01-30 12:37:34 +00:00
|
|
|
# listened to property, a function in arg[1], and an optional
|
|
|
|
# bool in arg[2], which triggers the function initially if true.
|
2005-12-16 19:15:08 +00:00
|
|
|
#
|
2007-10-15 16:30:37 +00:00
|
|
|
var setlistener = func(node, fun, init=0, runtime=1) {
|
|
|
|
if(isa(node, props.Node)) node = node._g;
|
|
|
|
var id = _setlistener(node, func {
|
2007-10-15 18:32:20 +00:00
|
|
|
arg[0] = props.wrapNode(arg[0]);
|
|
|
|
arg[1] = props.wrapNode(arg[1]);
|
2007-10-15 16:30:37 +00:00
|
|
|
call(fun, arg);
|
|
|
|
}, init, runtime);
|
2007-04-27 14:27:01 +00:00
|
|
|
if(__.log_level <= 2) {
|
|
|
|
var c = caller(1);
|
|
|
|
print(sprintf("setting listener #%d in %s, line %s", id, c[2], c[3]))
|
|
|
|
}
|
|
|
|
return id;
|
2005-12-16 19:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-12 18:17:00 +00:00
|
|
|
##
|
|
|
|
# Returns true if the symbol name is defined in the caller, or the
|
|
|
|
# caller's lexical namespace. (i.e. defined("varname") tells you if
|
|
|
|
# you can use varname in an expression without a undefined symbol
|
|
|
|
# error.
|
|
|
|
#
|
2007-10-15 16:30:37 +00:00
|
|
|
var defined = func(sym) {
|
2005-06-12 18:17:00 +00:00
|
|
|
var fn = 1;
|
|
|
|
while((var frame = caller(fn)) != nil) {
|
|
|
|
if(contains(frame[0], sym)) { return 1; }
|
|
|
|
fn += 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2006-02-17 21:24:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Print log messages in appropriate --log-level.
|
|
|
|
# Usage: printlog("warn", "...");
|
2006-02-18 00:12:09 +00:00
|
|
|
# The underscore hash prevents helper functions/variables from
|
|
|
|
# needlessly polluting the global namespace.
|
2006-02-17 21:24:08 +00:00
|
|
|
#
|
2006-02-18 00:12:09 +00:00
|
|
|
__ = {};
|
|
|
|
__.dbg_types = { none:0, bulk:1, debug:2, info:3, warn:4, alert:5 };
|
|
|
|
__.log_level = __.dbg_types[getprop("/sim/logging/priority")];
|
2007-10-15 16:30:37 +00:00
|
|
|
var printlog = func(level, args...) {
|
2006-02-18 00:12:09 +00:00
|
|
|
if(__.dbg_types[level] >= __.log_level) { call(print, args); }
|
2006-02-17 22:05:32 +00:00
|
|
|
}
|
|
|
|
|
2006-02-17 21:24:08 +00:00
|
|
|
|
2007-08-06 22:50:06 +00:00
|
|
|
##
|
|
|
|
# Load and execute ~/.fgfs/Nasal/*.nas files in alphabetic order
|
|
|
|
# after all $FG_ROOT/Nasal/*.nas files were loaded.
|
|
|
|
#
|
|
|
|
_setlistener("/sim/signals/nasal-dir-initialized", func {
|
|
|
|
var path = getprop("/sim/fg-home") ~ "/Nasal";
|
2007-10-07 15:12:05 +00:00
|
|
|
if((var dir = directory(path)) == nil) return;
|
|
|
|
foreach(var file; sort(dir, cmp)) {
|
|
|
|
if(substr(file, -4) != ".nas") continue;
|
2007-08-06 22:50:06 +00:00
|
|
|
|
|
|
|
var module = substr(file, 0, size(file) - 4);
|
|
|
|
file = path ~ "/" ~ file;
|
|
|
|
printlog("info", ">>> executing local Nasal file ", file);
|
|
|
|
|
2007-10-07 15:12:05 +00:00
|
|
|
if(!contains(globals, module)) var locals = globals[module] = {};
|
|
|
|
elsif(typeof(globals[module]) == "hash") var locals = globals[module];
|
2007-08-06 22:50:06 +00:00
|
|
|
else var locals = {};
|
|
|
|
|
|
|
|
var err = [];
|
|
|
|
var code = call(func { compile(io.readfile(file), file) }, nil, err);
|
2007-10-07 15:12:05 +00:00
|
|
|
if(size(err)) {
|
2007-08-06 22:50:06 +00:00
|
|
|
print(file ~ ": " ~ err[0]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
call(bind(code, globals), nil, nil, locals, err);
|
|
|
|
debug.printerror(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|