author | unc0rr |
Wed, 14 Feb 2007 20:05:20 +0000 | |
changeset 442 | 57ed1444606e |
parent 393 | db01cc79f278 |
child 498 | 9c8b385dc9a1 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
393 | 3 |
* Copyright (c) 2005-2007 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 4 |
* |
183 | 5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
4 | 8 |
* |
183 | 9 |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
4 | 13 |
* |
183 | 14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
4 | 17 |
*) |
18 |
||
19 |
unit uCollisions; |
|
20 |
interface |
|
351 | 21 |
uses uGears, uFloat; |
4 | 22 |
{$INCLUDE options.inc} |
53 | 23 |
const cMaxGearArrayInd = 255; |
4 | 24 |
|
70 | 25 |
type PGearArray = ^TGearArray; |
53 | 26 |
TGearArray = record |
27 |
ar: array[0..cMaxGearArrayInd] of PGear; |
|
28 |
Count: Longword |
|
29 |
end; |
|
4 | 30 |
|
53 | 31 |
procedure AddGearCI(Gear: PGear); |
32 |
procedure DeleteCI(Gear: PGear); |
|
33 |
function CheckGearsCollision(Gear: PGear): PGearArray; |
|
371 | 34 |
function TestCollisionXwithGear(Gear: PGear; Dir: LongInt): boolean; |
35 |
function TestCollisionYwithGear(Gear: PGear; Dir: LongInt): boolean; |
|
36 |
function TestCollisionY(Gear: PGear; Dir: LongInt): boolean; |
|
37 |
function TestCollisionXwithXYShift(Gear: PGear; ShiftX, ShiftY: hwFloat; Dir: LongInt): boolean; |
|
38 |
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt): boolean; |
|
4 | 39 |
|
40 |
implementation |
|
57 | 41 |
uses uMisc, uConsts, uLand, uLandGraphics; |
4 | 42 |
|
53 | 43 |
type TCollisionEntry = record |
371 | 44 |
X, Y, Radius: LongInt; |
53 | 45 |
cGear: PGear; |
46 |
end; |
|
351 | 47 |
|
4 | 48 |
const MAXRECTSINDEX = 255; |
49 |
var Count: Longword = 0; |
|
53 | 50 |
cinfos: array[0..MAXRECTSINDEX] of TCollisionEntry; |
51 |
ga: TGearArray; |
|
4 | 52 |
|
53 | 53 |
procedure AddGearCI(Gear: PGear); |
54 |
begin |
|
351 | 55 |
if Gear^.CollIndex < High(Longword) then exit; |
4 | 56 |
TryDo(Count <= MAXRECTSINDEX, 'Collision rects array overflow', true); |
53 | 57 |
with cinfos[Count] do |
4 | 58 |
begin |
351 | 59 |
X:= hwRound(Gear^.X); |
60 |
Y:= hwRound(Gear^.Y); |
|
61 |
Radius:= Gear^.Radius; |
|
62 | 62 |
FillRoundInLand(X, Y, Radius-1, $FF); |
4 | 63 |
cGear:= Gear |
64 |
end; |
|
351 | 65 |
Gear^.CollIndex:= Count; |
4 | 66 |
inc(Count) |
67 |
end; |
|
68 |
||
53 | 69 |
procedure DeleteCI(Gear: PGear); |
4 | 70 |
begin |
351 | 71 |
if Gear^.CollIndex < Count then |
53 | 72 |
begin |
351 | 73 |
with cinfos[Gear^.CollIndex] do FillRoundInLand(X, Y, Radius-1, 0); |
74 |
cinfos[Gear^.CollIndex]:= cinfos[Pred(Count)]; |
|
75 |
cinfos[Gear^.CollIndex].cGear^.CollIndex:= Gear^.CollIndex; |
|
76 |
Gear^.CollIndex:= High(Longword); |
|
53 | 77 |
dec(Count) |
78 |
end; |
|
4 | 79 |
end; |
80 |
||
53 | 81 |
function CheckGearsCollision(Gear: PGear): PGearArray; |
371 | 82 |
var mx, my: LongInt; |
4 | 83 |
i: Longword; |
351 | 84 |
Result: PGearArray; |
4 | 85 |
begin |
53 | 86 |
Result:= @ga; |
87 |
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
|
88 |
if Count = 0 then exit; |
351 | 89 |
mx:= hwRound(Gear^.X); |
90 |
my:= hwRound(Gear^.Y); |
|
4 | 91 |
|
92 |
for i:= 0 to Pred(Count) do |
|
53 | 93 |
with cinfos[i] do |
94 |
if (Gear <> cGear) and |
|
351 | 95 |
(sqrt(sqr(mx - x) + sqr(my - y)) <= Radius + Gear^.Radius) then |
4 | 96 |
begin |
53 | 97 |
ga.ar[ga.Count]:= cinfos[i].cGear; |
98 |
inc(ga.Count) |
|
4 | 99 |
end; |
351 | 100 |
CheckGearsCollision:= Result |
4 | 101 |
end; |
102 |
||
371 | 103 |
function TestCollisionXwithGear(Gear: PGear; Dir: LongInt): boolean; |
104 |
var x, y, i: LongInt; |
|
4 | 105 |
begin |
351 | 106 |
x:= hwRound(Gear^.X); |
107 |
if Dir < 0 then x:= x - Gear^.Radius |
|
108 |
else x:= x + Gear^.Radius; |
|
4 | 109 |
if (x and $FFFFF800) = 0 then |
110 |
begin |
|
351 | 111 |
y:= hwRound(Gear^.Y) - Gear^.Radius + 1; |
112 |
i:= y + Gear^.Radius * 2 - 2; |
|
4 | 113 |
repeat |
351 | 114 |
if (y and $FFFFFC00) = 0 then |
115 |
if Land[y, x] <> 0 then exit(true); |
|
4 | 116 |
inc(y) |
351 | 117 |
until (y > i); |
118 |
end; |
|
119 |
TestCollisionXwithGear:= false |
|
4 | 120 |
end; |
121 |
||
371 | 122 |
function TestCollisionXwithXYShift(Gear: PGear; ShiftX, ShiftY: hwFloat; Dir: LongInt): boolean; |
4 | 123 |
begin |
351 | 124 |
Gear^.X:= Gear^.X + ShiftX; |
125 |
Gear^.Y:= Gear^.Y + ShiftY; |
|
126 |
TestCollisionXwithXYShift:= TestCollisionXwithGear(Gear, Dir); |
|
127 |
Gear^.X:= Gear^.X - ShiftX; |
|
128 |
Gear^.Y:= Gear^.Y - ShiftY |
|
4 | 129 |
end; |
130 |
||
371 | 131 |
function TestCollisionYwithGear(Gear: PGear; Dir: LongInt): boolean; |
132 |
var x, y, i: LongInt; |
|
4 | 133 |
begin |
351 | 134 |
y:= hwRound(Gear^.Y); |
135 |
if Dir < 0 then y:= y - Gear^.Radius |
|
136 |
else y:= y + Gear^.Radius; |
|
4 | 137 |
if (y and $FFFFFC00) = 0 then |
138 |
begin |
|
351 | 139 |
x:= hwRound(Gear^.X) - Gear^.Radius + 1; |
140 |
i:= x + Gear^.Radius * 2 - 2; |
|
4 | 141 |
repeat |
351 | 142 |
if (x and $FFFFF800) = 0 then |
143 |
if Land[y, x] <> 0 then exit(true); |
|
4 | 144 |
inc(x) |
351 | 145 |
until (x > i); |
146 |
end; |
|
147 |
TestCollisionYwithGear:= false |
|
4 | 148 |
end; |
149 |
||
371 | 150 |
function TestCollisionY(Gear: PGear; Dir: LongInt): boolean; |
151 |
var x, y, i: LongInt; |
|
68 | 152 |
begin |
351 | 153 |
y:= hwRound(Gear^.Y); |
154 |
if Dir < 0 then y:= y - Gear^.Radius |
|
155 |
else y:= y + Gear^.Radius; |
|
68 | 156 |
if (y and $FFFFFC00) = 0 then |
157 |
begin |
|
351 | 158 |
x:= hwRound(Gear^.X) - Gear^.Radius + 1; |
159 |
i:= x + Gear^.Radius * 2 - 2; |
|
68 | 160 |
repeat |
351 | 161 |
if (x and $FFFFF800) = 0 then |
162 |
if Land[y, x] = COLOR_LAND then exit(true); |
|
68 | 163 |
inc(x) |
351 | 164 |
until (x > i); |
165 |
end; |
|
166 |
TestCollisionY:= false |
|
68 | 167 |
end; |
168 |
||
371 | 169 |
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt): boolean; |
4 | 170 |
begin |
351 | 171 |
Gear^.X:= Gear^.X + ShiftX; |
172 |
Gear^.Y:= Gear^.Y + ShiftY; |
|
173 |
TestCollisionYwithXYShift:= TestCollisionYwithGear(Gear, Dir); |
|
174 |
Gear^.X:= Gear^.X - ShiftX; |
|
175 |
Gear^.Y:= Gear^.Y - ShiftY |
|
4 | 176 |
end; |
177 |
||
178 |
end. |