author | unc0rr |
Tue, 20 Jun 2006 21:20:06 +0000 | |
changeset 67 | 3101306251e5 |
parent 64 | 9df467527ae5 |
child 68 | cbb93eb90304 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
64 | 3 |
* Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 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} |
|
53 | 38 |
const cMaxGearArrayInd = 255; |
4 | 39 |
|
53 | 40 |
type TDirection = record |
41 |
dX, dY: integer |
|
42 |
end; |
|
43 |
PGearArray = ^TGearArray; |
|
44 |
TGearArray = record |
|
45 |
ar: array[0..cMaxGearArrayInd] of PGear; |
|
46 |
Count: Longword |
|
47 |
end; |
|
4 | 48 |
|
53 | 49 |
procedure AddGearCI(Gear: PGear); |
50 |
procedure DeleteCI(Gear: PGear); |
|
51 |
function CheckGearsCollision(Gear: PGear): PGearArray; |
|
4 | 52 |
function HHTestCollisionYwithGear(Gear: PGear; Dir: integer): boolean; |
53 |
function TestCollisionXwithGear(Gear: PGear; Dir: integer): boolean; |
|
54 |
function TestCollisionYwithGear(Gear: PGear; Dir: integer): boolean; |
|
55 |
function TestCollisionXwithXYShift(Gear: PGear; ShiftX, ShiftY: integer; Dir: integer): boolean; |
|
56 |
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: integer; Dir: integer): boolean; |
|
57 |
||
58 |
implementation |
|
57 | 59 |
uses uMisc, uConsts, uLand, uLandGraphics; |
4 | 60 |
|
53 | 61 |
type TCollisionEntry = record |
62 |
X, Y, Radius: integer; |
|
63 |
cGear: PGear; |
|
64 |
end; |
|
65 |
||
4 | 66 |
const MAXRECTSINDEX = 255; |
67 |
var Count: Longword = 0; |
|
53 | 68 |
cinfos: array[0..MAXRECTSINDEX] of TCollisionEntry; |
69 |
ga: TGearArray; |
|
4 | 70 |
|
53 | 71 |
procedure AddGearCI(Gear: PGear); |
72 |
begin |
|
54 | 73 |
if Gear.CollIndex < High(Longword) then exit; |
4 | 74 |
TryDo(Count <= MAXRECTSINDEX, 'Collision rects array overflow', true); |
53 | 75 |
with cinfos[Count] do |
4 | 76 |
begin |
77 |
X:= round(Gear.X); |
|
78 |
Y:= round(Gear.Y); |
|
53 | 79 |
Radius:= Gear.Radius; |
62 | 80 |
FillRoundInLand(X, Y, Radius-1, $FF); |
4 | 81 |
cGear:= Gear |
82 |
end; |
|
83 |
Gear.CollIndex:= Count; |
|
84 |
inc(Count) |
|
85 |
end; |
|
86 |
||
53 | 87 |
procedure DeleteCI(Gear: PGear); |
4 | 88 |
begin |
53 | 89 |
if Gear.CollIndex < Count then |
90 |
begin |
|
62 | 91 |
with cinfos[Gear.CollIndex] do FillRoundInLand(X, Y, Radius-1, 0); |
53 | 92 |
cinfos[Gear.CollIndex]:= cinfos[Pred(Count)]; |
93 |
cinfos[Gear.CollIndex].cGear.CollIndex:= Gear.CollIndex; |
|
94 |
Gear.CollIndex:= High(Longword); |
|
95 |
dec(Count) |
|
96 |
end; |
|
4 | 97 |
end; |
98 |
||
53 | 99 |
function CheckGearsCollision(Gear: PGear): PGearArray; |
100 |
var mx, my: integer; |
|
4 | 101 |
i: Longword; |
102 |
begin |
|
53 | 103 |
Result:= @ga; |
104 |
ga.Count:= 0; |
|
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
|
105 |
if Count = 0 then exit; |
53 | 106 |
mx:= round(Gear.X); |
107 |
my:= round(Gear.Y); |
|
4 | 108 |
|
109 |
for i:= 0 to Pred(Count) do |
|
53 | 110 |
with cinfos[i] do |
111 |
if (Gear <> cGear) and |
|
112 |
(sqrt(sqr(mx - x) + sqr(my - y)) <= Radius + Gear.Radius) then |
|
4 | 113 |
begin |
53 | 114 |
ga.ar[ga.Count]:= cinfos[i].cGear; |
115 |
inc(ga.Count) |
|
4 | 116 |
end; |
117 |
end; |
|
118 |
||
119 |
function HHTestCollisionYwithGear(Gear: PGear; Dir: integer): boolean; |
|
120 |
var x, y, i: integer; |
|
121 |
begin |
|
122 |
Result:= false; |
|
123 |
y:= round(Gear.Y); |
|
53 | 124 |
if Dir < 0 then y:= y - Gear.Radius |
125 |
else y:= y + Gear.Radius; |
|
4 | 126 |
|
127 |
if ((y - Dir) and $FFFFFC00) = 0 then |
|
128 |
begin |
|
129 |
x:= round(Gear.X); |
|
53 | 130 |
if (((x - Gear.Radius) and $FFFFF800) = 0)and(Land[y - Dir, x - Gear.Radius] <> 0) |
131 |
or(((x + Gear.Radius) and $FFFFF800) = 0)and(Land[y - Dir, x + Gear.Radius] <> 0) then |
|
4 | 132 |
begin |
133 |
Result:= true; |
|
134 |
exit |
|
135 |
end |
|
136 |
end; |
|
137 |
||
138 |
if (y and $FFFFFC00) = 0 then |
|
139 |
begin |
|
53 | 140 |
x:= round(Gear.X) - Gear.Radius + 1; |
141 |
i:= x + Gear.Radius * 2 - 2; |
|
4 | 142 |
repeat |
143 |
if (x and $FFFFF800) = 0 then Result:= Land[y, x]<>0; |
|
144 |
inc(x) |
|
53 | 145 |
until (x > i) or Result |
4 | 146 |
end |
147 |
end; |
|
148 |
||
149 |
function TestCollisionXwithGear(Gear: PGear; Dir: integer): boolean; |
|
150 |
var x, y, i: integer; |
|
151 |
begin |
|
152 |
Result:= false; |
|
153 |
x:= round(Gear.X); |
|
64 | 154 |
if Dir < 0 then x:= x - Gear.Radius - 1 |
155 |
else x:= x + Gear.Radius + 1; |
|
4 | 156 |
if (x and $FFFFF800) = 0 then |
157 |
begin |
|
64 | 158 |
y:= round(Gear.Y) - Gear.Radius + 1; |
159 |
i:= y + Gear.Radius * 2 - 2; |
|
4 | 160 |
repeat |
161 |
if (y and $FFFFFC00) = 0 then Result:= Land[y, x]<>0; |
|
162 |
inc(y) |
|
163 |
until (y > i) or Result; |
|
164 |
end |
|
165 |
end; |
|
166 |
||
167 |
function TestCollisionXwithXYShift(Gear: PGear; ShiftX, ShiftY: integer; Dir: integer): boolean; |
|
168 |
begin |
|
169 |
Gear.X:= Gear.X + ShiftX; |
|
170 |
Gear.Y:= Gear.Y + ShiftY; |
|
171 |
Result:= TestCollisionXwithGear(Gear, Dir); |
|
172 |
Gear.X:= Gear.X - ShiftX; |
|
173 |
Gear.Y:= Gear.Y - ShiftY |
|
174 |
end; |
|
175 |
||
176 |
function TestCollisionYwithGear(Gear: PGear; Dir: integer): boolean; |
|
177 |
var x, y, i: integer; |
|
178 |
begin |
|
179 |
Result:= false; |
|
180 |
y:= round(Gear.Y); |
|
53 | 181 |
if Dir < 0 then y:= y - Gear.Radius |
182 |
else y:= y + Gear.Radius; |
|
4 | 183 |
if (y and $FFFFFC00) = 0 then |
184 |
begin |
|
64 | 185 |
x:= round(Gear.X) - Gear.Radius + 1; |
186 |
i:= x + Gear.Radius * 2 - 2; |
|
4 | 187 |
repeat |
188 |
if (x and $FFFFF800) = 0 then Result:= Land[y, x]<>0; |
|
189 |
inc(x) |
|
190 |
until (x > i) or Result; |
|
191 |
end |
|
192 |
end; |
|
193 |
||
194 |
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: integer; Dir: integer): boolean; |
|
195 |
begin |
|
196 |
Gear.X:= Gear.X + ShiftX; |
|
197 |
Gear.Y:= Gear.Y + ShiftY; |
|
198 |
Result:= TestCollisionYwithGear(Gear, Dir); |
|
199 |
Gear.X:= Gear.X - ShiftX; |
|
200 |
Gear.Y:= Gear.Y - ShiftY |
|
201 |
end; |
|
202 |
||
203 |
end. |