Mercurial > projects > openmelee
annotate ai/ai.d @ 26:88cca12cc8b9
added ship explode
author | zzzzrrr <mason.green@gmail.com> |
---|---|
date | Fri, 27 Mar 2009 19:26:01 -0400 |
parents | 441eb7672404 |
children |
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 |
24 | 33 import tango.util.container.LinkedList : LinkedList; |
12 | 34 import tango.io.Stdout : Stdout; |
24 | 35 import tango.math.Math : atan2, abs, PI, isNaN; |
12 | 36 |
14 | 37 import blaze.common.bzMath: bzVec2, bzClamp; |
19 | 38 import blaze.bzWorld : bzWorld; |
18 | 39 |
40 import openmelee.ai.steer : Steer; | |
41 import openmelee.ships.ship : Ship; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
42 |
24 | 43 alias LinkedList!(Ship) ObjectList; |
44 | |
45 struct Threat { | |
46 Ship target; | |
47 bzVec2 steering; | |
48 float distance = 0.0f; | |
49 float collisionTime = 0.0f; | |
50 float minSeparation = float.max; | |
51 bzVec2 relativePos; | |
52 bzVec2 relativeVel; | |
53 } | |
54 | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
55 class AI { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
56 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
57 Steer steer; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
58 Ship ship; |
14 | 59 float maxPredictionTime = 0.1f; |
19 | 60 bzVec2 st; |
61 | |
24 | 62 bool avoidRight; |
63 bool avoidLeft; | |
22 | 64 |
24 | 65 this(Ship ship, ObjectList objectList) { |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
66 this.ship = ship; |
24 | 67 steer = new Steer(ship, objectList); |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
68 } |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
69 |
26 | 70 // Elementary steering AI |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
71 void move(Ship enemy) { |
24 | 72 |
26 | 73 if(!ship) return; |
23 | 74 |
26 | 75 Threat threat; |
19 | 76 steer.update(); |
24 | 77 steer.collisionThreat(threat); |
78 st = threat.steering; | |
14 | 79 |
19 | 80 if(st == bzVec2.zeroVect) { |
24 | 81 if(avoidLeft || avoidRight) { |
82 avoidLeft = avoidRight = false; | |
83 ship.rBody.angularVelocity = 0.0f; | |
84 } | |
85 st = steer.targetEnemy(enemy.state, maxPredictionTime); | |
23 | 86 chase(enemy); |
24 | 87 return; |
19 | 88 } else { |
24 | 89 ship.state.turn = false; |
19 | 90 avoid(); |
24 | 91 return; |
19 | 92 } |
24 | 93 |
94 assert(0); | |
19 | 95 } |
96 | |
97 void chase(Ship enemy) { | |
98 | |
99 ship.state.target = st; | |
14 | 100 st = ship.rBody.localPoint(st); |
24 | 101 // Because ship's heading is 90 off rigid body's heading |
26 | 102 st = st.rotateLeft90(); |
12 | 103 float angle = atan2(st.x, st.y); |
24 | 104 |
14 | 105 if(abs(angle) > 0.05) { |
106 if(!ship.state.turn) { | |
107 if(st.x >= 0) { | |
108 ship.turnRight(); | |
109 ship.state.turn = true; | |
110 } else { | |
111 ship.state.turn = true; | |
112 ship.turnLeft(); | |
113 } | |
114 } | |
13 | 115 } else { |
116 ship.rBody.angularVelocity = 0.0f; | |
117 ship.state.turn = false; | |
118 } | |
119 | |
120 float range = (ship.state.position - enemy.state.position).length; | |
15 | 121 if(range > 2 && abs(angle) < PI/4) { |
14 | 122 ship.thrust(); |
13 | 123 } |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
124 } |
19 | 125 |
126 void avoid() { | |
127 | |
128 st = ship.rBody.localPoint(st); | |
24 | 129 // Because ship's heading is 90 off rigid body's heading |
26 | 130 st = st.rotateLeft90(); |
19 | 131 float angle = atan2(st.x, st.y); |
132 | |
24 | 133 if(st.x <= 0) { |
134 if(!avoidRight) { | |
22 | 135 ship.turnRight(); |
24 | 136 avoidRight = true; |
137 avoidLeft = false; | |
138 } | |
139 } else { | |
140 if(!avoidLeft) { | |
22 | 141 ship.turnLeft(); |
24 | 142 avoidLeft = true; |
143 avoidRight = false; | |
22 | 144 } |
19 | 145 } |
22 | 146 |
147 ship.thrust(); | |
19 | 148 } |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
149 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
diff
changeset
|
150 } |