Mercurial > projects > openmelee
annotate ai/ai.d @ 23:e79347dd38a3
steering
author | zzzzrrr <mason.green@gmail.com> |
---|---|
date | Thu, 26 Mar 2009 20:35:58 -0400 |
parents | 4fce5596d1f6 |
children | 441eb7672404 |
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, |
12 | 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LED TO, |
11
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 | 31 module openmelee.ai.ai; |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
32 |
12 | 33 import tango.io.Stdout : Stdout; |
13 | 34 import tango.math.Math : atan2, abs, PI; |
12 | 35 |
14 | 36 import blaze.common.bzMath: bzVec2, bzClamp; |
19 | 37 import blaze.bzWorld : bzWorld; |
18 | 38 |
39 import openmelee.ai.steer : Steer; | |
40 import openmelee.ships.ship : Ship; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
41 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
42 class AI { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
43 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
44 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
45 Steer steer; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
46 Ship ship; |
14 | 47 float maxPredictionTime = 0.1f; |
19 | 48 bzWorld m_world; |
49 bzVec2 st; | |
50 | |
22 | 51 bool avoidTurn; |
52 | |
19 | 53 this(Ship ship, bzWorld world) { |
54 m_world = world; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
55 this.ship = ship; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
56 steer = new Steer(ship); |
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 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
59 void move(Ship enemy) { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
60 |
23 | 61 st = bzVec2.zeroVect; |
62 | |
15 | 63 // Elementary steering AI |
19 | 64 steer.update(); |
20 | 65 //st = steer.steerToAvoidObstacles(0.25, m_world.bodyList); |
23 | 66 //st = steer.avoid(10.0, m_world.bodyList); |
67 //st = steer.getSteering(m_world.bodyList); | |
14 | 68 |
19 | 69 if(st == bzVec2.zeroVect) { |
22 | 70 avoidTurn = false; |
23 | 71 st = steer.steerForPursuit(enemy.state, maxPredictionTime); |
72 chase(enemy); | |
19 | 73 } else { |
74 avoid(); | |
75 } | |
76 } | |
77 | |
78 void chase(Ship enemy) { | |
79 | |
80 ship.state.target = st; | |
14 | 81 st = ship.rBody.localPoint(st); |
82 float x = st.x; | |
83 float y = st.y; | |
84 st = -bzVec2(y,-x); | |
12 | 85 float angle = atan2(st.x, st.y); |
13 | 86 |
14 | 87 if(abs(angle) > 0.05) { |
88 if(!ship.state.turn) { | |
89 if(st.x >= 0) { | |
90 ship.turnRight(); | |
91 ship.state.turn = true; | |
92 } else { | |
93 ship.state.turn = true; | |
94 ship.turnLeft(); | |
95 } | |
96 } | |
13 | 97 } else { |
98 ship.rBody.angularVelocity = 0.0f; | |
99 ship.state.turn = false; | |
100 } | |
101 | |
102 float range = (ship.state.position - enemy.state.position).length; | |
15 | 103 if(range > 2 && abs(angle) < PI/4) { |
14 | 104 ship.thrust(); |
13 | 105 } |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
106 } |
19 | 107 |
108 void avoid() { | |
109 | |
110 st = ship.rBody.localPoint(st); | |
111 float x = st.x; | |
112 float y = st.y; | |
113 st = -bzVec2(y,-x); | |
114 float angle = atan2(st.x, st.y); | |
115 | |
22 | 116 if(!avoidTurn) { |
23 | 117 if(st.x >= 0) { |
22 | 118 ship.turnRight(); |
119 } else { | |
120 ship.turnLeft(); | |
121 } | |
122 avoidTurn = true; | |
19 | 123 } |
22 | 124 |
125 ship.thrust(); | |
19 | 126 } |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
127 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
128 } |