author | displacer |
Sat, 18 Nov 2006 23:48:24 +0000 | |
changeset 249 | ff85fa029541 |
parent 183 | 57c2ef19f719 |
child 351 | 29bc9c36ad5f |
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 |
* |
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 |
|
21 |
uses uGears; |
|
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; |
|
4 | 34 |
function TestCollisionXwithGear(Gear: PGear; Dir: integer): boolean; |
35 |
function TestCollisionYwithGear(Gear: PGear; Dir: integer): boolean; |
|
68 | 36 |
function TestCollisionY(Gear: PGear; Dir: integer): boolean; |
107 | 37 |
function TestCollisionXwithXYShift(Gear: PGear; ShiftX, ShiftY: Double; Dir: integer): boolean; |
4 | 38 |
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: integer; Dir: integer): boolean; |
39 |
||
40 |
implementation |
|
57 | 41 |
uses uMisc, uConsts, uLand, uLandGraphics; |
4 | 42 |
|
53 | 43 |
type TCollisionEntry = record |
44 |
X, Y, Radius: integer; |
|
45 |
cGear: PGear; |
|
46 |
end; |
|
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 |
|
54 | 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 |
59 |
X:= round(Gear.X); |
|
60 |
Y:= round(Gear.Y); |
|
53 | 61 |
Radius:= Gear.Radius; |
62 | 62 |
FillRoundInLand(X, Y, Radius-1, $FF); |
4 | 63 |
cGear:= Gear |
64 |
end; |
|
65 |
Gear.CollIndex:= Count; |
|
66 |
inc(Count) |
|
67 |
end; |
|
68 |
||
53 | 69 |
procedure DeleteCI(Gear: PGear); |
4 | 70 |
begin |
53 | 71 |
if Gear.CollIndex < Count then |
72 |
begin |
|
62 | 73 |
with cinfos[Gear.CollIndex] do FillRoundInLand(X, Y, Radius-1, 0); |
53 | 74 |
cinfos[Gear.CollIndex]:= cinfos[Pred(Count)]; |
75 |
cinfos[Gear.CollIndex].cGear.CollIndex:= Gear.CollIndex; |
|
76 |
Gear.CollIndex:= High(Longword); |
|
77 |
dec(Count) |
|
78 |
end; |
|
4 | 79 |
end; |
80 |
||
53 | 81 |
function CheckGearsCollision(Gear: PGear): PGearArray; |
82 |
var mx, my: integer; |
|
4 | 83 |
i: Longword; |
84 |
begin |
|
53 | 85 |
Result:= @ga; |
86 |
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
|
87 |
if Count = 0 then exit; |
53 | 88 |
mx:= round(Gear.X); |
89 |
my:= round(Gear.Y); |
|
4 | 90 |
|
91 |
for i:= 0 to Pred(Count) do |
|
53 | 92 |
with cinfos[i] do |
93 |
if (Gear <> cGear) and |
|
94 |
(sqrt(sqr(mx - x) + sqr(my - y)) <= Radius + Gear.Radius) then |
|
4 | 95 |
begin |
53 | 96 |
ga.ar[ga.Count]:= cinfos[i].cGear; |
97 |
inc(ga.Count) |
|
4 | 98 |
end; |
99 |
end; |
|
100 |
||
101 |
function TestCollisionXwithGear(Gear: PGear; Dir: integer): boolean; |
|
102 |
var x, y, i: integer; |
|
103 |
begin |
|
104 |
Result:= false; |
|
105 |
x:= round(Gear.X); |
|
68 | 106 |
if Dir < 0 then x:= x - Gear.Radius |
107 |
else x:= x + Gear.Radius; |
|
4 | 108 |
if (x and $FFFFF800) = 0 then |
109 |
begin |
|
64 | 110 |
y:= round(Gear.Y) - Gear.Radius + 1; |
111 |
i:= y + Gear.Radius * 2 - 2; |
|
4 | 112 |
repeat |
113 |
if (y and $FFFFFC00) = 0 then Result:= Land[y, x]<>0; |
|
114 |
inc(y) |
|
115 |
until (y > i) or Result; |
|
116 |
end |
|
117 |
end; |
|
118 |
||
107 | 119 |
function TestCollisionXwithXYShift(Gear: PGear; ShiftX, ShiftY: Double; Dir: integer): boolean; |
4 | 120 |
begin |
121 |
Gear.X:= Gear.X + ShiftX; |
|
122 |
Gear.Y:= Gear.Y + ShiftY; |
|
123 |
Result:= TestCollisionXwithGear(Gear, Dir); |
|
124 |
Gear.X:= Gear.X - ShiftX; |
|
125 |
Gear.Y:= Gear.Y - ShiftY |
|
126 |
end; |
|
127 |
||
128 |
function TestCollisionYwithGear(Gear: PGear; Dir: integer): boolean; |
|
129 |
var x, y, i: integer; |
|
130 |
begin |
|
131 |
Result:= false; |
|
132 |
y:= round(Gear.Y); |
|
53 | 133 |
if Dir < 0 then y:= y - Gear.Radius |
134 |
else y:= y + Gear.Radius; |
|
4 | 135 |
if (y and $FFFFFC00) = 0 then |
136 |
begin |
|
64 | 137 |
x:= round(Gear.X) - Gear.Radius + 1; |
68 | 138 |
i:= x + Gear.Radius * 2 - 2; |
4 | 139 |
repeat |
140 |
if (x and $FFFFF800) = 0 then Result:= Land[y, x]<>0; |
|
141 |
inc(x) |
|
142 |
until (x > i) or Result; |
|
143 |
end |
|
144 |
end; |
|
145 |
||
68 | 146 |
function TestCollisionY(Gear: PGear; Dir: integer): boolean; |
147 |
var x, y, i: integer; |
|
148 |
begin |
|
149 |
Result:= false; |
|
150 |
y:= round(Gear.Y); |
|
151 |
if Dir < 0 then y:= y - Gear.Radius |
|
152 |
else y:= y + Gear.Radius; |
|
153 |
if (y and $FFFFFC00) = 0 then |
|
154 |
begin |
|
155 |
x:= round(Gear.X) - Gear.Radius + 1; |
|
156 |
i:= x + Gear.Radius * 2 - 2; |
|
157 |
repeat |
|
158 |
if (x and $FFFFF800) = 0 then Result:= Land[y, x] = COLOR_LAND; |
|
159 |
inc(x) |
|
160 |
until (x > i) or Result; |
|
161 |
end |
|
162 |
end; |
|
163 |
||
4 | 164 |
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: integer; Dir: integer): boolean; |
165 |
begin |
|
166 |
Gear.X:= Gear.X + ShiftX; |
|
167 |
Gear.Y:= Gear.Y + ShiftY; |
|
168 |
Result:= TestCollisionYwithGear(Gear, Dir); |
|
169 |
Gear.X:= Gear.X - ShiftX; |
|
170 |
Gear.Y:= Gear.Y - ShiftY |
|
171 |
end; |
|
172 |
||
173 |
end. |