forked from jadonk/bonescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
160 lines (139 loc) · 4.35 KB
/
functions.js
File metadata and controls
160 lines (139 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// Copyright (C) 2012 - Cabin Programs, Ken Keller
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// Bits and Bytes
// lowByte(value)
// highByte(value)
// bitRead(value, bitnum)
// bitWrite(value, bitnum, bitdata)
// bitSet(value, bitnum)
// bitClear(value, bitnum)
// bit(bitnum)
//
// Trigonometry
// sin(radians)
// cos(radians)
// tan(radians)
//
// Math
// min(x,y)
// max(x,y)
// abs(x)
// constrain(x, a, b)
// map(value, fromLow, fromHigh, toLow, toHigh)
// pow(x, y)
// sqrt(x)
//
// Random Numbers
// randomSeed(x)
// random(min, max)
// random(max)
//
// Returns the lower 8 bits of value
lowByte = exports.lowByte = function(value) {
return ( value & 0x0ff)
};
// Returns the value shifted right by 8 bits
highByte = exports.highByte = function(value) {
return ( lowByte(value >>8))
};
// Returns the value of the bit number specified (return:0 or 1)
bitRead = exports.bitRead = function(value, bitnum) {
return ((value>>bitnum)&0x01);
};
// Returns value with bit changed to specified data
bitWrite = exports.bitWrite = function(value, bitnum, bitdata) {
value = value & ~(0x01<<bitnum);
bitdata = (bitdata & 0x01) << bitnum;
return (value | bitdata);
};
// Returns value with specified bit set
bitSet = exports.bitSet = function(value, bitnum) {
return(value | (0x01 << bitnum));
};
// Returns value with specified bit clear
bitClear = exports.bitClear = function(value, bitnum) {
return(value & (~(0x01 << bitnum)));
};
//Returns a value with one specified bit number set
bit = exports.bit = function(bitnum) {
return(0x01<<bitnum);
};
// Returns the sine of an angle (in radians).
sin = exports.sin = function(radians) {
return (Math.sin(radians));
};
// Returns the cos of an angle (in radians).
cos = exports.cos = function(radians) {
return (Math.cos(radians));
};
// Returns the tan of an angle (in radians).
tan = exports.tan = function(radians) {
return (Math.tan(radians));
};
// Returns the the minimum of x or y
min = exports.min = function(x,y) {
return (Math.min(x,y));
};
// Returns the the maximum of x or y
max = exports.max = function(x,y) {
return (Math.max(x,y));
};
// Returns the the absolute value of x
abs = exports.abs = function(x) {
return (Math.abs(x));
};
// Returns a value constrained within the range of a to b
// Returns: x if x is between a and b
// a if x is less than a
// b if x is greater than b
constrain = exports.constrain = function(x,a,b) {
if (x>b) x=b;
else if (x<a) x=a;
return (x);
};
// Returns a value re-mapped from one range to another
map = exports.map = function(value, fromLow, fromHigh, toLow, toHigh) {
return( toLow + (((value-fromLow)*(toHigh-toLow))/(fromHigh-fromLow)));
};
// Returns x raised to y power
pow = exports.pow = function(x,y) {
return (Math.pow(x,y));
};
// Returns the aquare root of x
sqrt = exports.sqrt = function(x) {
return (Math.sqrt(x));
};
// Returns nothing
randomSeed = exports.randomSeed = function(x) {
// empty - javascript has not randam seed function
};
// Returns a pseudo-random number
// Valid calls: random(max)
// random(min, max)
random = exports.random = function(min, max) {
if (isNaN(max))
{
max = min;
min = 0;
}
return ((Math.random()*(max-min))+min);
};