Debian Codegraph @

Nasal/globals.nas

raw
1 # SPDX-License-Identifier: GPL-2.0-or-later
2 #
3 # NOTE! This copyright does *not* cover user models that use these Nasal
4 # services by normal function calls - this is merely considered normal use
5 # of the code, and does *not* fall under the heading of "derived work."
6 #
7 # Copyright (C) 2003-2008 by andy
8 # Copyright (C) 2008-2009 by mfranz
9 # Copyright (C) 2013 by janodesbois
10 # Copyright (C) 2014 by Anton Gomez Alvedro
11
12 ##
13 # Constants.
14 #
15 var D2R = math.pi / 180; # degree to radian
16 var R2D = 180 / math.pi; # radian to degree
17
18 var FT2M = 0.3048; # feet to meter
19 var M2FT = 1 / FT2M;
20 var IN2M = FT2M / 12;
21 var M2IN = 1 / IN2M;
22 var NM2M = 1852; # nautical miles to meter
23 var M2NM = 1 / NM2M;
24
25 var KT2MPS = 0.5144444444; # knots to m/s
26 var MPS2KT = 1 / KT2MPS;
27
28 var FPS2KT = 0.5924838012958964; # fps to knots
29 var KT2FPS = 1 / FPS2KT;
30
31 var LB2KG = 0.45359237; # pounds to kg
32 var KG2LB = 1 / LB2KG;
33
34 var GAL2L = 3.785411784; # US gallons to liter
35 var L2GAL = 1 / GAL2L;
36
37
38 # container for local variables, so as not to clutter the global namespace
39 var __ = {};
40
41 ##
42 # Aborts execution if <condition> evaluates to false.
43 # Prints an optional message if present, or just "assertion failed!"
44 #
45 var assert = func (condition, message=nil) {
46 message != nil or (message = "assertion failed!");
47 condition or die(message);
48 }
49
50 ##
51 # Returns true if the first object is an instance of the second
52 # (class) object. Example: isa(someObject, props.Node)
53 #
54 var isa = func(obj, class) {
55 if (ishash(obj) and obj["parents"] != nil) {
56 foreach(var c; obj.parents) {
57 if (c == class or isa(c, class)) return 1;
58 }
59 }
60 return 0;
61 }
62
63 ##
64 # Invokes a FlightGear command specified by the first argument. The
65 # second argument specifies the property tree to be passed to the
66 # command as its argument. It may be either a props.Node object or a
67 # string, in which case it specifies a path in the global property
68 # tree.
69 #
70 var fgcommand = func(cmd, node=nil) {
71 if (isa(node, props.Node)) node = node._g;
72 elsif (ishash(node)) node = props.Node.new(node)._g;
73 _fgcommand(cmd, node);
74 }
75
76 ##
77 # Returns the SGPropertyNode argument to the currently executing
78 # function. Wrapper for the internal _cmdarg function that retrieves
79 # the ghost handle to the argument and wraps it in a
80 # props.Node object.
81 #
82 var cmdarg = func { props.wrapNode(_cmdarg()) }
83
84 ##
85 # Utility. Does what you think it does.
86 #
87 var abs = func(v) { return v < 0 ? -v : v }
88
89 ##
90 # Convenience wrapper for the _interpolate function. Takes a
91 # single string or props.Node object in arg[0] indicating a target
92 # property, and a variable-length list of time/value pairs. Example:
93 #
94 # interpolate("/animations/radar/angle",
95 # 180, 1, 360, 1, 0, 0,
96 # 180, 1, 360, 1, 0, 0,
97 # 180, 1, 360, 1, 0, 0,
98 # 180, 1, 360, 1, 0, 0,
99 # 180, 1, 360, 1, 0, 0,
100 # 180, 1, 360, 1, 0, 0,
101 # 180, 1, 360, 1, 0, 0,
102 # 180, 1, 360, 1, 0, 0);
103 #
104 # This will swing the "radar dish" smoothly through 8 revolutions over
105 # 16 seconds. Note the use of zero-time interpolation between 360 and
106 # 0 to wrap the interpolated value properly.
107 #
108 var interpolate = func(node, val...) {
109 if (isa(node, props.Node))
110 node = node._g;
111 elsif (!isscalar(node) and !isghost(node))
112 die("bad argument to interpolate()");
113 _interpolate(node, val);
114 }
115
116
117 ##
118 # Wrapper for the _setlistener function. Takes a property path string
119 # or props.Node object in arg[0] indicating the listened to property,
120 # a function in arg[1], an optional bool in arg[2], which triggers the
121 # function initially if true, and an optional integer in arg[3], which
122 # sets the listener's runtime behavior to "only trigger on change" (0),
123 # "always trigger on write" (1), and "trigger even when children are
124 # written to" (2).
125 #
126 var setlistener = func(node, fn, init = 0, runtime = 1) {
127 if (typeof(fn) != "func") {
128 # This avoids unhelpful failures later on when we try to call <fn> - we
129 # get an error without a useful backtrace, because property callbacks
130 # use a new Nasal context.
131 die(sprintf("setlistener() called with non-function; typeof(fn)=%s", typeof(fn)));
132 }
133 if (isa(node, props.Node)) node = node._g;
134 elsif (!isscalar(node) and !isghost(node))
135 die("bad argument to setlistener()");
136 var id = _setlistener(node, func(chg, lst, mode, is_child) {
137 fn(props.wrapNode(chg), props.wrapNode(lst), mode, is_child);
138 }, init, runtime);
139
140 var c = caller();
141 if (c != nil) {
142 logprint(LOG_DEBUG, "setting listener #",id," in ",c[2],":",c[3]);
143 }
144 return id;
145 }
146
147
148 ##
149 # Returns true if the symbol name is defined in the caller, or the
150 # caller's lexical namespace. (i.e. defined("varname") tells you if
151 # you can use varname in an expression without a undefined symbol
152 # error.
153 #
154 var defined = func(sym) {
155 if (contains(caller(1)[0], sym)) return 1;
156 var fn = caller(1)[1];
157 for (var l=0; (var frame = closure(fn, l)) != nil; l+=1)
158 if (contains(frame, sym)) return 1;
159 return 0;
160 }
161
162
163 ##
164 # Returns reference to calling function. This allows a function to
165 # reliably call itself from a closure, rather than the global function
166 # with the same name.
167 #
168 var thisfunc = func caller(1)[1];
169
170
171 ##
172 # Just what it says it is.
173 #
174 var printf = func print(call(sprintf, arg));
175
176
177 ##
178 # Returns vector of hash values.
179 #
180 var values = func(hash) {
181 var vec = [];
182 foreach(var key; keys(hash)) append(vec, hash[key]);
183 return vec;
184 }
185
186 # printlog is depricated, use logprint instead
187
188 __.dbg_types = { none:0, bulk:1, debug:2, info:3, warn:4, alert:5 };
189 var printlog = func(level, msg...) {
190 var c = caller();
191 logprint(LOG_ALERT, "Deprecated printlog() call from ",c[2]~":"~c[3]~
192 ", please use logprint instead.");
193 logprint([__.dbg_types[level]]~msg);
194 }
195
196
197 ##
198 # Load and execute ~/.fgfs/Nasal/*.nas files in alphabetic order
199 # after all $FG_ROOT/Nasal/*.nas files were loaded.
200 #
201 settimer(func {
202 var path = getprop("/sim/fg-home") ~ "/Nasal";
203 if((var dir = directory(path)) == nil) return;
204 foreach(var file; sort(dir, cmp))
205 if(size(file) > 4 and substr(file, -4) == ".nas")
206 io.load_nasal(path ~ "/" ~ file, substr(file, 0, size(file) - 4));
207 }, 0);