author | unc0rr |
Mon, 05 Dec 2005 21:46:15 +0000 | |
changeset 24 | 79c411363184 |
parent 12 | 366adfa1a727 |
child 37 | 2b7f2a43b999 |
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 |
{$IFDEF DEBUGFILE}AddFileLog('AddCR crects count = ' + inttostr(Count));{$ENDIF} |
|
65 |
TryDo(Count <= MAXRECTSINDEX, 'Collision rects array overflow', true); |
|
66 |
with crects[Count] do |
|
67 |
begin |
|
68 |
X:= round(Gear.X); |
|
69 |
Y:= round(Gear.Y); |
|
70 |
HWidth:= Gear.HalfWidth; |
|
71 |
HHeight:= Gear.HalfHeight; |
|
72 |
cGear:= Gear |
|
73 |
end; |
|
74 |
Gear.CollIndex:= Count; |
|
75 |
inc(Count) |
|
76 |
end; |
|
77 |
||
78 |
procedure UpdateCR(NewX, NewY: integer; Index: Longword); |
|
79 |
begin |
|
80 |
with crects[Index] do |
|
81 |
begin |
|
82 |
X:= NewX; |
|
83 |
Y:= NewY |
|
84 |
end |
|
85 |
end; |
|
86 |
||
87 |
procedure DeleteCR(Gear: PGear); |
|
88 |
begin |
|
89 |
{$IFDEF DEBUGFILE}AddFileLog('DelCR crects count = ' + inttostr(Count) + ' deleting ' + inttostr(Gear.CollIndex));{$ENDIF} |
|
90 |
if Gear.CollIndex < Pred(Count) then |
|
91 |
begin |
|
92 |
crects[Gear.CollIndex]:= crects[Pred(Count)]; |
|
93 |
crects[Gear.CollIndex].cGear.CollIndex:= Gear.CollIndex |
|
94 |
end; |
|
95 |
Gear.CollIndex:= High(Longword); |
|
96 |
dec(Count) |
|
97 |
end; |
|
98 |
||
99 |
function CheckGearsCollision(Gear: PGear; Dir: integer; forX: boolean): boolean; |
|
100 |
var x1, x2, y1, y2: integer; |
|
101 |
i: Longword; |
|
102 |
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
|
103 |
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
|
104 |
if Count = 0 then exit; |
4 | 105 |
x1:= round(Gear.X); |
106 |
y1:= round(Gear.Y); |
|
107 |
{if (Gear.State and gstOutOfHH) = 0 then |
|
108 |
begin |
|
109 |
p:= PHedgehog(Gear.Hedgehog)^.Gear; |
|
110 |
if (p <> nil) and |
|
111 |
((x1 + Gear.HalfWidth < round(p.X) - p.HalfWidth) |
|
112 |
or (x1 - Gear.HalfWidth > round(p.X) + p.HalfWidth) |
|
113 |
or (y1 - Gear.HalfHeight > round(p.Y) + p.HalfHeight) |
|
114 |
or (y1 + Gear.HalfHeight < round(p.Y) - p.HalfHeight)) then Gear.State:= Gear.State or gstOutOfHH; |
|
115 |
end; } |
|
116 |
if forX then |
|
117 |
begin |
|
118 |
x1:= x1 + Dir*Gear.HalfWidth; |
|
119 |
x2:= x1; |
|
120 |
y2:= y1 + Gear.HalfHeight - 1; |
|
121 |
y1:= y1 - Gear.HalfHeight + 1 |
|
122 |
end else |
|
123 |
begin |
|
124 |
y1:= y1 + Dir*Gear.HalfHeight; |
|
125 |
y2:= y1; |
|
126 |
x2:= x1 + Gear.HalfWidth - 1; |
|
127 |
x1:= x1 - Gear.HalfWidth + 1 |
|
128 |
end; |
|
129 |
||
130 |
for i:= 0 to Pred(Count) do |
|
131 |
with crects[i] do |
|
132 |
if (Gear.CollIndex <> i) |
|
133 |
// if ((p.Kind = gtHedgehog) and ((p.Hedgehog <> Gear.Hedgehog) or ((Gear.State and gstOutOfHH) <> 0))) |
|
134 |
and (x1 <= X + HWidth) |
|
135 |
and (x2 >= X - HWidth) |
|
136 |
and (y1 <= Y + HHeight) |
|
137 |
and (y2 >= Y - HHeight) then |
|
138 |
begin |
|
139 |
Result:= true; |
|
140 |
exit |
|
141 |
end; |
|
142 |
end; |
|
143 |
||
144 |
function HHTestCollisionYwithGear(Gear: PGear; Dir: integer): boolean; |
|
145 |
var x, y, i: integer; |
|
146 |
begin |
|
147 |
Result:= false; |
|
148 |
y:= round(Gear.Y); |
|
149 |
if Dir < 0 then y:= y - Gear.HalfHeight |
|
150 |
else y:= y + Gear.HalfHeight; |
|
151 |
||
152 |
if ((y - Dir) and $FFFFFC00) = 0 then |
|
153 |
begin |
|
154 |
x:= round(Gear.X); |
|
155 |
if (((x - Gear.HalfWidth) and $FFFFF800) = 0)and(Land[y - Dir, x - Gear.HalfWidth] <> 0) |
|
156 |
or(((x + Gear.HalfWidth) and $FFFFF800) = 0)and(Land[y - Dir, x + Gear.HalfWidth] <> 0) then |
|
157 |
begin |
|
158 |
Result:= true; |
|
159 |
exit |
|
160 |
end |
|
161 |
end; |
|
162 |
||
163 |
if (y and $FFFFFC00) = 0 then |
|
164 |
begin |
|
165 |
x:= round(Gear.X) - Gear.HalfWidth + 1; |
|
166 |
i:= x + Gear.HalfWidth * 2 - 2; |
|
167 |
repeat |
|
168 |
if (x and $FFFFF800) = 0 then Result:= Land[y, x]<>0; |
|
169 |
inc(x) |
|
170 |
until (x > i) or Result; |
|
171 |
if Result then exit; |
|
172 |
||
173 |
Result:= CheckGearsCollision(Gear, Dir, false) |
|
174 |
end |
|
175 |
end; |
|
176 |
||
177 |
function TestCollisionXwithGear(Gear: PGear; Dir: integer): boolean; |
|
178 |
var x, y, i: integer; |
|
179 |
begin |
|
180 |
Result:= false; |
|
181 |
x:= round(Gear.X); |
|
182 |
if Dir < 0 then x:= x - Gear.HalfWidth |
|
183 |
else x:= x + Gear.HalfWidth; |
|
184 |
if (x and $FFFFF800) = 0 then |
|
185 |
begin |
|
186 |
y:= round(Gear.Y) - Gear.HalfHeight + 1; {*} |
|
187 |
i:= y + Gear.HalfHeight * 2 - 2; {*} |
|
188 |
repeat |
|
189 |
if (y and $FFFFFC00) = 0 then Result:= Land[y, x]<>0; |
|
190 |
inc(y) |
|
191 |
until (y > i) or Result; |
|
192 |
if Result then exit; |
|
193 |
Result:= CheckGearsCollision(Gear, Dir, true) |
|
194 |
end |
|
195 |
end; |
|
196 |
||
197 |
function TestCollisionXwithXYShift(Gear: PGear; ShiftX, ShiftY: integer; Dir: integer): boolean; |
|
198 |
begin |
|
199 |
Gear.X:= Gear.X + ShiftX; |
|
200 |
Gear.Y:= Gear.Y + ShiftY; |
|
201 |
Result:= TestCollisionXwithGear(Gear, Dir); |
|
202 |
Gear.X:= Gear.X - ShiftX; |
|
203 |
Gear.Y:= Gear.Y - ShiftY |
|
204 |
end; |
|
205 |
||
206 |
function TestCollisionYwithGear(Gear: PGear; Dir: integer): boolean; |
|
207 |
var x, y, i: integer; |
|
208 |
begin |
|
209 |
Result:= false; |
|
210 |
y:= round(Gear.Y); |
|
211 |
if Dir < 0 then y:= y - Gear.HalfHeight |
|
212 |
else y:= y + Gear.HalfHeight; |
|
213 |
if (y and $FFFFFC00) = 0 then |
|
214 |
begin |
|
215 |
x:= round(Gear.X) - Gear.HalfWidth + 1; {*} |
|
216 |
i:= x + Gear.HalfWidth * 2 - 2; {*} |
|
217 |
repeat |
|
218 |
if (x and $FFFFF800) = 0 then Result:= Land[y, x]<>0; |
|
219 |
inc(x) |
|
220 |
until (x > i) or Result; |
|
221 |
if Result then exit; |
|
222 |
Result:= CheckGearsCollision(Gear, Dir, false); |
|
223 |
end |
|
224 |
end; |
|
225 |
||
226 |
function TestCollisionY(Gear: PGear; Dir: integer): boolean; |
|
227 |
var x, y, i: integer; |
|
228 |
begin |
|
229 |
Result:= false; |
|
230 |
y:= round(Gear.Y); |
|
231 |
if Dir < 0 then y:= y - Gear.HalfHeight |
|
232 |
else y:= y + Gear.HalfHeight; |
|
233 |
if (y and $FFFFFC00) = 0 then |
|
234 |
begin |
|
235 |
x:= round(Gear.X) - Gear.HalfWidth + 1; {*} |
|
236 |
i:= x + Gear.HalfWidth * 2 - 2; {*} |
|
237 |
repeat |
|
238 |
if (x and $FFFFF800) = 0 then Result:= Land[y, x]<>0; |
|
239 |
inc(x) |
|
240 |
until (x > i) or Result; |
|
241 |
end |
|
242 |
end; |
|
243 |
||
244 |
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: integer; Dir: integer): boolean; |
|
245 |
begin |
|
246 |
Gear.X:= Gear.X + ShiftX; |
|
247 |
Gear.Y:= Gear.Y + ShiftY; |
|
248 |
Result:= TestCollisionYwithGear(Gear, Dir); |
|
249 |
Gear.X:= Gear.X - ShiftX; |
|
250 |
Gear.Y:= Gear.Y - ShiftY |
|
251 |
end; |
|
252 |
||
253 |
end. |