annotate ai/utilities.d @ 19:08ddf9e71b88

steer to avoid
author zzzzrrr <mason.green@gmail.com>
date Wed, 25 Mar 2009 14:44:47 -0400
parents 7f74e064dad5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
1 /*
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2009, Mason Green (zzzzrrr)
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
3 * http://www.dsource.org/projects/openmelee
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
4 *
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
5 * All rights reserved.
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
6 *
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
7 * Redistribution and use in source and binary forms, with or without modification,
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
8 * are permitted provided that the following conditions are met:
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
9 *
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
10 * * Redistributions of source code must retain the above copyright notice,
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
11 * this list of conditions and the following disclaimer.
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
12 * * Redistributions in binary form must reproduce the above copyright notice,
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
13 * this list of conditions and the following disclaimer in the documentation
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
14 * and/or other materials provided with the distribution.
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
15 * * Neither the name of the polygonal nor the names of its contributors may be
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
16 * used to endorse or promote products derived from this software without specific
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
17 * prior written permission.
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
18 *
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
30 */
18
7f74e064dad5 refactored code
zzzzrrr <mason.green@gmail.com>
parents: 11
diff changeset
31 module openmelee.ai.utilities;
11
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
32
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
33 import tango.math.random.Kiss : Kiss;
19
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
34 import blaze.common.bzMath : bzVec2, bzDot;
11
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
35
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
36 float scalarRandomWalk(float initial, float walkspeed, float min, float max) {
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
37
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
38 float next = initial + (((randomRange(0, 1) * 2) - 1) * walkspeed);
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
39 if (next < min) return min;
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
40 if (next > max) return max;
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
41 return next;
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
42 }
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
43
19
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
44 // return component of vector perpendicular to a unit basis vector
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
45 // IMPORTANT NOTE: assumes "basis" has unit magnitude(length==1)
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
46 bzVec2 perpendicularComponent(bzVec2 vector, bzVec2 unitBasis) {
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
47 return (vector - parallelComponent(vector, unitBasis));
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
48 }
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
49
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
50 // return component of vector parallel to a unit basis vector
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
51 // IMPORTANT NOTE: assumes "basis" has unit magnitude (length == 1)
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
52 bzVec2 parallelComponent(bzVec2 vector, bzVec2 unitBasis) {
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
53 float projection = bzDot(vector, unitBasis);
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
54 return unitBasis * projection;
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
55 }
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
56
11
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
57 // ----------------------------------------------------------------------------
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
58 // classify a value relative to the interval between two bounds:
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
59 // returns -1 when below the lower bound
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
60 // returns 0 when between the bounds (inside the interval)
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
61 // returns +1 when above the upper bound
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
62 int intervalComparison(float x, float lowerBound, float upperBound)
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
63 {
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
64 if (x < lowerBound) return -1;
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
65 if (x > upperBound) return +1;
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
66 return 0;
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
67 }
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
68
19
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
69 float square(float x) {
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
70 return x * x;
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
71 }
08ddf9e71b88 steer to avoid
zzzzrrr <mason.green@gmail.com>
parents: 18
diff changeset
72
11
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
73 T randomRange(T = int) (T min, T max)
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
74 {
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
75 return min + Kiss.instance.natural() % (max + 1 - min);
d998bf1b0654 Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff changeset
76 }