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