2020-08-12 12:40:59 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#
|
|
|
|
# NOTE! This copyright does *not* cover user models that use these Nasal
|
|
|
|
# services by normal function calls - this is merely considered normal use
|
|
|
|
# of the code, and does *not* fall under the heading of "derived work."
|
|
|
|
#
|
|
|
|
# Copyright (C) 2003-2008 by andy
|
|
|
|
# Copyright (C) 2008-2009 by mfranz
|
|
|
|
# Copyright (C) 2013 by janodesbois
|
|
|
|
# Copyright (C) 2014 by Anton Gomez Alvedro
|
|
|
|
|
2008-11-20 20:18:39 +00:00
|
|
|
##
|
|
|
|
# Constants.
|
|
|
|
#
|
|
|
|
var D2R = math.pi / 180; # degree to radian
|
|
|
|
var R2D = 180 / math.pi; # radian to degree
|
2008-12-07 14:44:52 +00:00
|
|
|
|
2008-11-20 20:18:39 +00:00
|
|
|
var FT2M = 0.3048; # feet to meter
|
2008-12-07 14:44:52 +00:00
|
|
|
var M2FT = 1 / FT2M;
|
2013-06-17 19:18:39 +00:00
|
|
|
var IN2M = FT2M / 12;
|
|
|
|
var M2IN = 1 / IN2M;
|
2008-12-14 23:45:33 +00:00
|
|
|
var NM2M = 1852; # nautical miles to meter
|
|
|
|
var M2NM = 1 / NM2M;
|
2008-12-07 14:44:52 +00:00
|
|
|
|
2008-12-17 01:03:32 +00:00
|
|
|
var KT2MPS = 0.5144444444; # knots to m/s
|
|
|
|
var MPS2KT = 1 / KT2MPS;
|
|
|
|
|
2013-10-25 21:48:03 +00:00
|
|
|
var FPS2KT = 0.5924838012958964; # fps to knots
|
|
|
|
var KT2FPS = 1 / FPS2KT;
|
|
|
|
|
2009-02-15 14:13:49 +00:00
|
|
|
var LB2KG = 0.45359237; # pounds to kg
|
2008-12-07 14:44:52 +00:00
|
|
|
var KG2LB = 1 / LB2KG;
|
|
|
|
|
|
|
|
var GAL2L = 3.785411784; # US gallons to liter
|
|
|
|
var L2GAL = 1 / GAL2L;
|
|
|
|
|
2009-05-05 18:33:43 +00:00
|
|
|
|
|
|
|
# container for local variables, so as not to clutter the global namespace
|
|
|
|
var __ = {};
|
|
|
|
|
2014-05-13 18:09:48 +00:00
|
|
|
##
|
|
|
|
# Aborts execution if <condition> evaluates to false.
|
|
|
|
# Prints an optional message if present, or just "assertion failed!"
|
|
|
|
#
|
|
|
|
var assert = func (condition, message=nil) {
|
|
|
|
message != nil or (message = "assertion failed!");
|
|
|
|
condition or die(message);
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-06-29 08:49:18 +00:00
|
|
|
if (ishash(obj) and obj["parents"] != nil) {
|
2020-05-29 18:04:49 +00:00
|
|
|
foreach(var c; obj.parents) {
|
2020-06-29 08:49:18 +00:00
|
|
|
if (c == class or isa(c, class)) return 1;
|
2020-05-29 18:04:49 +00:00
|
|
|
}
|
|
|
|
}
|
2003-12-01 14:36:22 +00:00
|
|
|
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) {
|
2020-06-29 08:49:18 +00:00
|
|
|
if (isa(node, props.Node)) node = node._g;
|
|
|
|
elsif (ishash(node)) node = props.Node.new(node)._g;
|
2007-06-07 16:59:07 +00:00
|
|
|
_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
|
|
|
|
|
|
|
##
|
2008-02-07 21:23:42 +00:00
|
|
|
# Utility. Does what you think it does.
|
2003-12-05 02:38:35 +00:00
|
|
|
#
|
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...) {
|
2020-06-29 08:49:18 +00:00
|
|
|
if (isa(node, props.Node))
|
|
|
|
node = node._g;
|
|
|
|
elsif (!isscalar(node) and !isghost(node))
|
2014-02-02 18:31:48 +00:00
|
|
|
die("bad argument to interpolate()");
|
2007-10-15 16:30:37 +00:00
|
|
|
_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
|
|
|
|
|
|
|
##
|
2007-10-16 15:24:30 +00:00
|
|
|
# Wrapper for the _setlistener function. Takes a property path string
|
|
|
|
# or props.Node object in arg[0] indicating the listened to property,
|
|
|
|
# a function in arg[1], an optional bool in arg[2], which triggers the
|
|
|
|
# function initially if true, and an optional integer in arg[3], which
|
|
|
|
# sets the listener's runtime behavior to "only trigger on change" (0),
|
2007-11-06 21:23:53 +00:00
|
|
|
# "always trigger on write" (1), and "trigger even when children are
|
2007-10-16 15:24:30 +00:00
|
|
|
# written to" (2).
|
2005-12-16 19:15:08 +00:00
|
|
|
#
|
2008-07-27 22:00:58 +00:00
|
|
|
var setlistener = func(node, fn, init = 0, runtime = 1) {
|
2020-05-29 18:04:49 +00:00
|
|
|
if (isa(node, props.Node)) node = node._g;
|
|
|
|
elsif (!isscalar(node) and !isghost(node))
|
2014-02-02 18:31:48 +00:00
|
|
|
die("bad argument to setlistener()");
|
2008-02-13 20:12:42 +00:00
|
|
|
var id = _setlistener(node, func(chg, lst, mode, is_child) {
|
2008-07-27 22:00:58 +00:00
|
|
|
fn(props.wrapNode(chg), props.wrapNode(lst), mode, is_child);
|
2007-10-15 16:30:37 +00:00
|
|
|
}, init, runtime);
|
2020-04-27 21:50:34 +00:00
|
|
|
|
2020-04-20 14:24:49 +00:00
|
|
|
var c = caller();
|
2020-04-27 21:50:34 +00:00
|
|
|
if (c != nil) {
|
|
|
|
logprint(LOG_DEBUG, "setting listener #",id," in ",c[2],":",c[3]);
|
|
|
|
}
|
2007-04-27 14:27:01 +00:00
|
|
|
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) {
|
2013-10-20 03:20:38 +00:00
|
|
|
if (contains(caller(1)[0], sym)) return 1;
|
|
|
|
var fn = caller(1)[1];
|
|
|
|
for (var l=0; (var frame = closure(fn, l)) != nil; l+=1)
|
|
|
|
if (contains(frame, sym)) return 1;
|
|
|
|
return 0;
|
2005-06-12 18:17:00 +00:00
|
|
|
}
|
2006-02-17 21:24:08 +00:00
|
|
|
|
|
|
|
|
2008-07-22 19:03:25 +00:00
|
|
|
##
|
|
|
|
# Returns reference to calling function. This allows a function to
|
|
|
|
# reliably call itself from a closure, rather than the global function
|
|
|
|
# with the same name.
|
|
|
|
#
|
|
|
|
var thisfunc = func caller(1)[1];
|
|
|
|
|
|
|
|
|
2008-07-23 15:43:45 +00:00
|
|
|
##
|
|
|
|
# Just what it says it is.
|
|
|
|
#
|
|
|
|
var printf = func print(call(sprintf, arg));
|
|
|
|
|
|
|
|
|
2009-02-15 14:13:49 +00:00
|
|
|
##
|
|
|
|
# Returns vector of hash values.
|
|
|
|
#
|
|
|
|
var values = func(hash) {
|
|
|
|
var vec = [];
|
|
|
|
foreach(var key; keys(hash)) append(vec, hash[key]);
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2020-04-20 14:24:49 +00:00
|
|
|
# printlog is depricated, use logprint instead
|
2009-02-15 14:13:49 +00:00
|
|
|
|
2006-02-18 00:12:09 +00:00
|
|
|
__.dbg_types = { none:0, bulk:1, debug:2, info:3, warn:4, alert:5 };
|
2020-04-20 14:24:49 +00:00
|
|
|
var printlog = func(level, msg...) {
|
|
|
|
var c = caller();
|
|
|
|
logprint(LOG_ALERT, "Deprecated printlog() call from ",c[2]~":"~c[3]~
|
|
|
|
", please use logprint instead.");
|
|
|
|
logprint([__.dbg_types[level]]~msg);
|
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.
|
|
|
|
#
|
2008-11-11 14:54:51 +00:00
|
|
|
settimer(func {
|
2007-08-06 22:50:06 +00:00
|
|
|
var path = getprop("/sim/fg-home") ~ "/Nasal";
|
2007-10-07 15:12:05 +00:00
|
|
|
if((var dir = directory(path)) == nil) return;
|
2008-07-30 19:33:42 +00:00
|
|
|
foreach(var file; sort(dir, cmp))
|
2008-08-10 17:53:41 +00:00
|
|
|
if(size(file) > 4 and substr(file, -4) == ".nas")
|
|
|
|
io.load_nasal(path ~ "/" ~ file, substr(file, 0, size(file) - 4));
|
2008-11-11 14:54:51 +00:00
|
|
|
}, 0);
|