author | unc0rr |
Thu, 05 Jan 2006 15:54:22 +0000 | |
changeset 37 | 2b7f2a43b999 |
parent 12 | 366adfa1a727 |
child 38 | c1ec4b15d70e |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* Distributed under the terms of the BSD-modified licence: |
|
6 |
* |
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 |
* of this software and associated documentation files (the "Software"), to deal |
|
9 |
* with the Software without restriction, including without limitation the |
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is |
|
12 |
* furnished to do so, subject to the following conditions: |
|
13 |
* |
|
14 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
15 |
* this list of conditions and the following disclaimer. |
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
17 |
* this list of conditions and the following disclaimer in the documentation |
|
18 |
* and/or other materials provided with the distribution. |
|
19 |
* 3. The name of the author may not be used to endorse or promote products |
|
20 |
* derived from this software without specific prior written permission. |
|
21 |
* |
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 |
*) |
|
33 |
||
34 |
unit uCollisions; |
|
35 |
interface |
|
36 |
uses uGears; |
|
37 |
{$INCLUDE options.inc} |
|
38 |
||
39 |
type TCollisionEntry = record |
|
40 |
X, Y, HWidth, HHeight: integer; |
|
41 |
cGear: PGear; |
|
42 |
end; |
|
43 |
||
44 |
procedure AddGearCR(Gear: PGear); |
|
45 |
procedure UpdateCR(NewX, NewY: integer; Index: Longword); |
|
46 |
procedure DeleteCR(Gear: PGear); |
|
47 |
function CheckGearsCollision(Gear: PGear; Dir: integer; forX: boolean): boolean; |
|
48 |
function HHTestCollisionYwithGear(Gear: PGear; Dir: integer): boolean; |
|
49 |
function TestCollisionXwithGear(Gear: PGear; Dir: integer): boolean; |
|
50 |
function TestCollisionYwithGear(Gear: PGear; Dir: integer): boolean; |
|
51 |
function TestCollisionXwithXYShift(Gear: PGear; ShiftX, ShiftY: integer; Dir: integer): boolean; |
|
52 |
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: integer; Dir: integer): boolean; |
|
53 |
function TestCollisionY(Gear: PGear; Dir: integer): boolean; |
|
54 |
||
55 |
implementation |
|
56 |
uses uMisc, uConsts, uLand; |
|
57 |
||
58 |
const MAXRECTSINDEX = 255; |
|
59 |
var Count: Longword = 0; |
|
60 |
crects: array[0..MAXRECTSINDEX] of TCollisionEntry; |
|
61 |
||
62 |
procedure AddGearCR(Gear: PGear); |
|
63 |
begin |
|
64 |
TryDo(Count <= MAXRECTSINDEX, 'Collision rects array overflow', true); |
|
65 |
with crects[Count] do |
|
66 |
begin |
|
67 |
X:= round(Gear.X); |
|
68 |
Y:= round(Gear.Y); |
|
69 |
HWidth:= Gear.HalfWidth; |
|
70 |
HHeight:= Gear.HalfHeight; |
|
71 |
cGear:= Gear |
|
72 |
end; |
|
73 |
Gear.CollIndex:= Count; |
|
74 |
inc(Count) |
|
75 |
end; |
|
76 |
||
77 |
procedure UpdateCR(NewX, NewY: integer; Index: Longword); |
|
78 |
begin |
|
79 |
with crects[Index] do |
|
80 |
begin |
|
81 |
X:= NewX; |
|
82 |
Y:= NewY |
|
83 |
end |
|
84 |
end; |
|
85 |
||
86 |
procedure DeleteCR(Gear: PGear); |
|
87 |
begin |
|
88 |
if Gear.CollIndex < Pred(Count) then |
|
89 |
begin |
|
90 |
crects[Gear.CollIndex]:= crects[Pred(Count)]; |
|
91 |
crects[Gear.CollIndex].cGear.CollIndex:= Gear.CollIndex |
|
92 |
end; |
|
93 |
Gear.CollIndex:= High(Longword); |
|
94 |
dec(Count) |
|
95 |
end; |
|
96 |
||
97 |
function CheckGearsCollision(Gear: PGear; Dir: integer; forX: boolean): boolean; |
|
98 |
var x1, x2, y1, y2: integer; |
|
99 |
i: Longword; |
|
100 |
begin |
|
12
366adfa1a727
Fix reading out of bounds of the collisions array. This fixes flying hedgehogs and not moving after explosion
unc0rr
parents:
4
diff
changeset
|
101 |
Result:= false; |
366adfa1a727
Fix reading out of bounds of the collisions array. This fixes flying hedgehogs and not moving after explosion
unc0rr
parents:
4
diff
changeset
|
102 |
if Count = 0 then exit; |
4 | 103 |
x1:= round(Gear.X); |
104 |
y1:= round(Gear.Y); |
|
37 | 105 |
|
4 | 106 |
if forX then |
107 |
begin |
|
108 |
x1:= x1 + Dir*Gear.HalfWidth; |
|
109 |
x2:= x1; |
|
110 |
y2:= y1 + Gear.HalfHeight - 1; |
|
111 |
y1:= y1 - Gear.HalfHeight + 1 |
|
112 |
end else |
|
113 |
begin |
|
114 |
y1:= y1 + Dir*Gear.HalfHeight; |
|
115 |
y2:= y1; |
|
116 |
x2:= x1 + Gear.HalfWidth - 1; |
|
117 |
x1:= x1 - Gear.HalfWidth + 1 |
|
118 |
end; |
|
119 |
||
120 |
for i:= 0 to Pred(Count) do |
|
121 |
with crects[i] do |
|
122 |
if (Gear.CollIndex <> i) |
|
123 |
and (x1 <= X + HWidth) |
|
124 |
and (x2 >= X - HWidth) |
|
125 |
and (y1 <= Y + HHeight) |
|
126 |
and (y2 >= Y - HHeight) then |
|
127 |
begin |
|
128 |
Result:= true; |
|
129 |
exit |
|
130 |
end; |
|
131 |
end; |
|
132 |
||
133 |
function HHTestCollisionYwithGear(Gear: PGear; Dir: integer): boolean; |
|
134 |
var x, y, i: integer; |
|
135 |
begin |
|
136 |
Result:= false; |
|
137 |
y:= round(Gear.Y); |
|
138 |
if Dir < 0 then y:= y - Gear.HalfHeight |
|
139 |
else y:= y + Gear.HalfHeight; |
|
140 |
||
141 |
if ((y - Dir) and $FFFFFC00) = 0 then |
|
142 |
begin |
|
143 |
x:= round(Gear.X); |
|
144 |
if (((x - Gear.HalfWidth) and $FFFFF800) = 0)and(Land[y - Dir, x - Gear.HalfWidth] <> 0) |
|
145 |
or(((x + Gear.HalfWidth) and $FFFFF800) = 0)and(Land[y - Dir, x + Gear.HalfWidth] <> 0) then |
|
146 |
begin |
|
147 |
Result:= true; |
|
148 |
exit |
|
149 |
end |
|
150 |
end; |
|
151 |
||
152 |
if (y and $FFFFFC00) = 0 then |
|
153 |
begin |
|
154 |
x:= round(Gear.X) - Gear.HalfWidth + 1; |
|
155 |
i:= x + Gear.HalfWidth * 2 - 2; |
|
156 |
repeat |
|
157 |
if (x and $FFFFF800) = 0 then Result:= Land[y, x]<>0; |
|
158 |
inc(x) |
|
159 |
until (x > i) or Result; |
|
160 |
if Result then exit; |
|
161 |
||
162 |
Result:= CheckGearsCollision(Gear, Dir, false) |
|
163 |
end |
|
164 |
end; |
|
165 |
||
166 |
function TestCollisionXwithGear(Gear: PGear; Dir: integer): boolean; |
|
167 |
var x, y, i: integer; |
|
168 |
begin |
|
169 |
Result:= false; |
|
170 |
x:= round(Gear.X); |
|
171 |
if Dir < 0 then x:= x - Gear.HalfWidth |
|
172 |
else x:= x + Gear.HalfWidth; |
|
173 |
if (x and $FFFFF800) = 0 then |
|
174 |
begin |
|
175 |
y:= round(Gear.Y) - Gear.HalfHeight + 1; {*} |
|
176 |
i:= y + Gear.HalfHeight * 2 - 2; {*} |
|
177 |
repeat |
|
178 |
if (y and $FFFFFC00) = 0 then Result:= Land[y, x]<>0; |
|
179 |
inc(y) |
|
180 |
until (y > i) or Result; |
|
181 |
if Result then exit; |
|
182 |
Result:= CheckGearsCollision(Gear, Dir, true) |
|
183 |
end |
|
184 |
end; |
|
185 |
||
186 |
function TestCollisionXwithXYShift(Gear: PGear; ShiftX, ShiftY: integer; Dir: integer): boolean; |
|
187 |
begin |
|
188 |
Gear.X:= Gear.X + ShiftX; |
|
189 |
Gear.Y:= Gear.Y + ShiftY; |
|
190 |
Result:= TestCollisionXwithGear(Gear, Dir); |
|
191 |
Gear.X:= Gear.X - ShiftX; |
|
192 |
Gear.Y:= Gear.Y - ShiftY |
|
193 |
end; |
|
194 |
||
195 |
function TestCollisionYwithGear(Gear: PGear; Dir: integer): boolean; |
|
196 |
var x, y, i: integer; |
|
197 |
begin |
|
198 |
Result:= false; |
|
199 |
y:= round(Gear.Y); |
|
200 |
if Dir < 0 then y:= y - Gear.HalfHeight |
|
201 |
else y:= y + Gear.HalfHeight; |
|
202 |
if (y and $FFFFFC00) = 0 then |
|
203 |
begin |
|
204 |
x:= round(Gear.X) - Gear.HalfWidth + 1; {*} |
|
205 |
i:= x + Gear.HalfWidth * 2 - 2; {*} |
|
206 |
repeat |
|
207 |
if (x and $FFFFF800) = 0 then Result:= Land[y, x]<>0; |
|
208 |
inc(x) |
|
209 |
until (x > i) or Result; |
|
210 |
if Result then exit; |
|
211 |
Result:= CheckGearsCollision(Gear, Dir, false); |
|
212 |
end |
|
213 |
end; |
|
214 |
||
215 |
function TestCollisionY(Gear: PGear; Dir: integer): boolean; |
|
216 |
var x, y, i: integer; |
|
217 |
begin |
|
218 |
Result:= false; |
|
219 |
y:= round(Gear.Y); |
|
220 |
if Dir < 0 then y:= y - Gear.HalfHeight |
|
221 |
else y:= y + Gear.HalfHeight; |
|
222 |
if (y and $FFFFFC00) = 0 then |
|
223 |
begin |
|
224 |
x:= round(Gear.X) - Gear.HalfWidth + 1; {*} |
|
225 |
i:= x + Gear.HalfWidth * 2 - 2; {*} |
|
226 |
repeat |
|
227 |
if (x and $FFFFF800) = 0 then Result:= Land[y, x]<>0; |
|
228 |
inc(x) |
|
229 |
until (x > i) or Result; |
|
230 |
end |
|
231 |
end; |
|
232 |
||
233 |
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: integer; Dir: integer): boolean; |
|
234 |
begin |
|
235 |
Gear.X:= Gear.X + ShiftX; |
|
236 |
Gear.Y:= Gear.Y + ShiftY; |
|
237 |
Result:= TestCollisionYwithGear(Gear, Dir); |
|
238 |
Gear.X:= Gear.X - ShiftX; |
|
239 |
Gear.Y:= Gear.Y - ShiftY |
|
240 |
end; |
|
241 |
||
242 |
end. |