author | koda |
Sat, 24 Nov 2012 18:31:49 +0100 | |
changeset 8124 | 499151efdd72 |
parent 7674 | aead327f1e1a |
child 8397 | 5b273af3ac95 |
permissions | -rw-r--r-- |
7660 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
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 |
|
8 |
* |
|
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. |
|
13 |
* |
|
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 |
|
17 |
*) |
|
18 |
||
19 |
{$INCLUDE "options.inc"} |
|
20 |
unit uGearsHandlersRope; |
|
21 |
interface |
|
22 |
||
23 |
uses uTypes; |
|
24 |
||
25 |
procedure doStepRope(Gear: PGear); |
|
26 |
||
27 |
implementation |
|
28 |
uses uConsts, uFloat, uCollisions, uVariables, uGearsList, uSound, uGearsUtils, |
|
29 |
uAmmos, uDebug, uUtils, uGearsHedgehog, uGearsRender; |
|
30 |
||
31 |
procedure doStepRopeAfterAttack(Gear: PGear); |
|
32 |
var |
|
33 |
HHGear: PGear; |
|
34 |
begin |
|
35 |
HHGear := Gear^.Hedgehog^.Gear; |
|
36 |
if ((HHGear^.State and gstHHDriven) = 0) |
|
37 |
or (CheckGearDrowning(HHGear)) |
|
38 |
or (TestCollisionYwithGear(HHGear, 1) <> 0) then |
|
39 |
begin |
|
40 |
DeleteGear(Gear); |
|
41 |
isCursorVisible := false; |
|
42 |
ApplyAmmoChanges(HHGear^.Hedgehog^); |
|
43 |
exit |
|
44 |
end; |
|
45 |
||
46 |
HedgehogChAngle(HHGear); |
|
47 |
||
48 |
if TestCollisionXwithGear(HHGear, hwSign(HHGear^.dX)) then |
|
49 |
SetLittle(HHGear^.dX); |
|
50 |
||
51 |
if HHGear^.dY.isNegative and (TestCollisionYwithGear(HHGear, -1) <> 0) then |
|
52 |
HHGear^.dY := _0; |
|
53 |
HHGear^.X := HHGear^.X + HHGear^.dX; |
|
54 |
HHGear^.Y := HHGear^.Y + HHGear^.dY; |
|
55 |
HHGear^.dY := HHGear^.dY + cGravity; |
|
56 |
||
57 |
if (GameFlags and gfMoreWind) <> 0 then |
|
58 |
HHGear^.dX := HHGear^.dX + cWindSpeed / HHGear^.Density; |
|
59 |
||
60 |
if (Gear^.Message and gmAttack) <> 0 then |
|
61 |
begin |
|
62 |
Gear^.X := HHGear^.X; |
|
63 |
Gear^.Y := HHGear^.Y; |
|
64 |
||
65 |
ApplyAngleBounds(Gear^.Hedgehog^, amRope); |
|
66 |
||
67 |
Gear^.dX := SignAs(AngleSin(HHGear^.Angle), HHGear^.dX); |
|
68 |
Gear^.dY := -AngleCos(HHGear^.Angle); |
|
69 |
Gear^.Friction := _4_5 * cRopePercent; |
|
70 |
Gear^.Elasticity := _0; |
|
71 |
Gear^.State := Gear^.State and (not gsttmpflag); |
|
72 |
Gear^.doStep := @doStepRope; |
|
73 |
end |
|
74 |
end; |
|
75 |
||
76 |
procedure RopeDeleteMe(Gear, HHGear: PGear); |
|
77 |
begin |
|
78 |
with HHGear^ do |
|
79 |
begin |
|
80 |
Message := Message and (not gmAttack); |
|
81 |
State := (State or gstMoving) and (not gstWinner); |
|
82 |
end; |
|
83 |
DeleteGear(Gear) |
|
84 |
end; |
|
85 |
||
86 |
procedure RopeWaitCollision(Gear, HHGear: PGear); |
|
87 |
begin |
|
88 |
with HHGear^ do |
|
89 |
begin |
|
90 |
Message := Message and (not gmAttack); |
|
91 |
State := State or gstMoving; |
|
92 |
end; |
|
93 |
RopePoints.Count := 0; |
|
94 |
Gear^.Elasticity := _0; |
|
95 |
Gear^.doStep := @doStepRopeAfterAttack |
|
96 |
end; |
|
97 |
||
98 |
procedure doStepRopeWork(Gear: PGear); |
|
99 |
var |
|
100 |
HHGear: PGear; |
|
101 |
len, tx, ty, nx, ny, ropeDx, ropeDy, mdX, mdY: hwFloat; |
|
102 |
lx, ly, cd: LongInt; |
|
103 |
haveCollision, |
|
104 |
haveDivided: boolean; |
|
105 |
||
106 |
begin |
|
107 |
if GameTicks mod 4 <> 0 then exit; |
|
108 |
||
109 |
HHGear := Gear^.Hedgehog^.Gear; |
|
110 |
||
111 |
if ((HHGear^.State and gstHHDriven) = 0) |
|
112 |
or (CheckGearDrowning(HHGear)) or (Gear^.PortalCounter <> 0) then |
|
113 |
begin |
|
114 |
PlaySound(sndRopeRelease); |
|
115 |
RopeDeleteMe(Gear, HHGear); |
|
116 |
exit |
|
117 |
end; |
|
118 |
||
119 |
HHGear^.dX.QWordValue:= HHGear^.dX.QWordValue shl 2; |
|
120 |
HHGear^.dY.QWordValue:= HHGear^.dY.QWordValue shl 2; |
|
121 |
if (Gear^.Message and gmLeft <> 0) and (not TestCollisionXwithGear(HHGear, -1)) then |
|
122 |
HHGear^.dX := HHGear^.dX - _0_0032; |
|
123 |
||
124 |
if (Gear^.Message and gmRight <> 0) and (not TestCollisionXwithGear(HHGear, 1)) then |
|
125 |
HHGear^.dX := HHGear^.dX + _0_0032; |
|
126 |
||
127 |
// vector between hedgehog and rope attaching point |
|
128 |
ropeDx := HHGear^.X - Gear^.X; |
|
129 |
ropeDy := HHGear^.Y - Gear^.Y; |
|
130 |
||
131 |
if TestCollisionYwithGear(HHGear, 1) = 0 then |
|
132 |
begin |
|
133 |
||
134 |
// depending on the rope vector we know which X-side to check for collision |
|
135 |
// in order to find out if the hog can still be moved by gravity |
|
136 |
if ropeDx.isNegative = RopeDy.IsNegative then |
|
137 |
cd:= -1 |
|
138 |
else |
|
139 |
cd:= 1; |
|
140 |
||
141 |
// apply gravity if there is no obstacle |
|
142 |
if not TestCollisionXwithGear(HHGear, cd) then |
|
143 |
HHGear^.dY := HHGear^.dY + cGravity * 16; |
|
144 |
||
145 |
if (GameFlags and gfMoreWind) <> 0 then |
|
146 |
// apply wind if there's no obstacle |
|
147 |
if not TestCollisionXwithGear(HHGear, hwSign(cWindSpeed)) then |
|
148 |
HHGear^.dX := HHGear^.dX + cWindSpeed * 16 / HHGear^.Density; |
|
149 |
end; |
|
150 |
||
151 |
mdX := ropeDx + HHGear^.dX; |
|
152 |
mdY := ropeDy + HHGear^.dY; |
|
153 |
len := _1 / Distance(mdX, mdY); |
|
154 |
// rope vector plus hedgehog direction vector normalized |
|
155 |
mdX := mdX * len; |
|
156 |
mdY := mdY * len; |
|
157 |
||
158 |
// for visual purposes only |
|
159 |
Gear^.dX := mdX; |
|
160 |
Gear^.dY := mdY; |
|
161 |
||
162 |
///// |
|
163 |
tx := HHGear^.X; |
|
164 |
ty := HHGear^.Y; |
|
165 |
||
166 |
if ((Gear^.Message and gmDown) <> 0) and (Gear^.Elasticity < Gear^.Friction) then |
|
167 |
if not (TestCollisionXwithGear(HHGear, hwSign(ropeDx)) |
|
168 |
or (TestCollisionYwithGear(HHGear, hwSign(ropeDy)) <> 0)) then |
|
169 |
Gear^.Elasticity := Gear^.Elasticity + _1_2; |
|
170 |
||
171 |
if ((Gear^.Message and gmUp) <> 0) and (Gear^.Elasticity > _30) then |
|
172 |
if not (TestCollisionXwithGear(HHGear, -hwSign(ropeDx)) |
|
173 |
or (TestCollisionYwithGear(HHGear, -hwSign(ropeDy)) <> 0)) then |
|
174 |
Gear^.Elasticity := Gear^.Elasticity - _1_2; |
|
175 |
||
176 |
HHGear^.X := Gear^.X + mdX * Gear^.Elasticity; |
|
177 |
HHGear^.Y := Gear^.Y + mdY * Gear^.Elasticity; |
|
178 |
||
179 |
HHGear^.dX := HHGear^.X - tx; |
|
180 |
HHGear^.dY := HHGear^.Y - ty; |
|
181 |
//// |
|
182 |
||
183 |
||
184 |
haveDivided := false; |
|
185 |
// check whether rope needs dividing |
|
186 |
||
187 |
len := Gear^.Elasticity - _5; |
|
188 |
nx := Gear^.X + mdX * len; |
|
189 |
ny := Gear^.Y + mdY * len; |
|
190 |
tx := mdX * _1_2; // should be the same as increase step |
|
191 |
ty := mdY * _1_2; |
|
192 |
||
193 |
while len > _3 do |
|
194 |
begin |
|
195 |
lx := hwRound(nx); |
|
196 |
ly := hwRound(ny); |
|
197 |
if ((ly and LAND_HEIGHT_MASK) = 0) and ((lx and LAND_WIDTH_MASK) = 0) and ((Land[ly, lx] and $FF00) <> 0) then |
|
198 |
begin |
|
199 |
ny := _1 / Distance(ropeDx, ropeDy); |
|
200 |
// old rope pos |
|
201 |
nx := ropeDx * ny; |
|
202 |
ny := ropeDy * ny; |
|
203 |
||
204 |
with RopePoints.ar[RopePoints.Count] do |
|
205 |
begin |
|
206 |
X := Gear^.X; |
|
207 |
Y := Gear^.Y; |
|
208 |
if RopePoints.Count = 0 then |
|
209 |
RopePoints.HookAngle := DxDy2Angle(Gear^.dY, Gear^.dX); |
|
210 |
b := (nx * HHGear^.dY) > (ny * HHGear^.dX); |
|
211 |
dLen := len |
|
212 |
end; |
|
213 |
||
214 |
with RopePoints.rounded[RopePoints.Count] do |
|
215 |
begin |
|
216 |
X := hwRound(Gear^.X); |
|
217 |
Y := hwRound(Gear^.Y); |
|
218 |
end; |
|
219 |
||
220 |
Gear^.X := Gear^.X + nx * len; |
|
221 |
Gear^.Y := Gear^.Y + ny * len; |
|
222 |
inc(RopePoints.Count); |
|
223 |
TryDo(RopePoints.Count <= MAXROPEPOINTS, 'Rope points overflow', true); |
|
224 |
Gear^.Elasticity := Gear^.Elasticity - len; |
|
225 |
Gear^.Friction := Gear^.Friction - len; |
|
226 |
haveDivided := true; |
|
227 |
break |
|
228 |
end; |
|
229 |
nx := nx - tx; |
|
230 |
ny := ny - ty; |
|
231 |
||
232 |
// len := len - _1_2 // should be the same as increase step |
|
233 |
len.QWordValue := len.QWordValue - _1_2.QWordValue; |
|
234 |
end; |
|
235 |
||
236 |
if not haveDivided then |
|
237 |
if RopePoints.Count > 0 then // check whether the last dividing point could be removed |
|
238 |
begin |
|
239 |
tx := RopePoints.ar[Pred(RopePoints.Count)].X; |
|
240 |
ty := RopePoints.ar[Pred(RopePoints.Count)].Y; |
|
241 |
mdX := tx - Gear^.X; |
|
242 |
mdY := ty - Gear^.Y; |
|
243 |
if RopePoints.ar[Pred(RopePoints.Count)].b xor (mdX * (ty - HHGear^.Y) > (tx - HHGear^.X) * mdY) then |
|
244 |
begin |
|
245 |
dec(RopePoints.Count); |
|
246 |
Gear^.X := RopePoints.ar[RopePoints.Count].X; |
|
247 |
Gear^.Y := RopePoints.ar[RopePoints.Count].Y; |
|
248 |
Gear^.Elasticity := Gear^.Elasticity + RopePoints.ar[RopePoints.Count].dLen; |
|
249 |
Gear^.Friction := Gear^.Friction + RopePoints.ar[RopePoints.Count].dLen; |
|
250 |
||
251 |
// restore hog position |
|
252 |
len := _1 / Distance(mdX, mdY); |
|
253 |
mdX := mdX * len; |
|
254 |
mdY := mdY * len; |
|
255 |
||
256 |
HHGear^.X := Gear^.X - mdX * Gear^.Elasticity; |
|
257 |
HHGear^.Y := Gear^.Y - mdY * Gear^.Elasticity; |
|
258 |
end |
|
259 |
end; |
|
260 |
||
261 |
haveCollision := false; |
|
262 |
if TestCollisionXwithGear(HHGear, hwSign(HHGear^.dX)) then |
|
263 |
begin |
|
264 |
HHGear^.dX := -_0_6 * HHGear^.dX; |
|
265 |
haveCollision := true |
|
266 |
end; |
|
267 |
if TestCollisionYwithGear(HHGear, hwSign(HHGear^.dY)) <> 0 then |
|
268 |
begin |
|
269 |
HHGear^.dY := -_0_6 * HHGear^.dY; |
|
270 |
haveCollision := true |
|
271 |
end; |
|
272 |
||
273 |
if haveCollision and (Gear^.Message and (gmLeft or gmRight) <> 0) and (Gear^.Message and (gmUp or gmDown) <> 0) then |
|
274 |
begin |
|
275 |
HHGear^.dX := SignAs(hwAbs(HHGear^.dX) + _0_8, HHGear^.dX); |
|
276 |
HHGear^.dY := SignAs(hwAbs(HHGear^.dY) + _0_8, HHGear^.dY) |
|
277 |
end; |
|
278 |
||
279 |
len := hwSqr(HHGear^.dX) + hwSqr(HHGear^.dY); |
|
280 |
if len > _10 then |
|
281 |
begin |
|
282 |
len := _3_2 / hwSqrt(len); |
|
283 |
HHGear^.dX := HHGear^.dX * len; |
|
284 |
HHGear^.dY := HHGear^.dY * len; |
|
285 |
end; |
|
286 |
||
287 |
haveCollision:= ((hwRound(Gear^.Y) and LAND_HEIGHT_MASK) = 0) and ((hwRound(Gear^.X) and LAND_WIDTH_MASK) = 0) and ((Land[hwRound(Gear^.Y), hwRound(Gear^.X)]) <> 0); |
|
288 |
||
289 |
if not haveCollision then |
|
290 |
begin |
|
291 |
// backup gear location |
|
292 |
tx:= Gear^.X; |
|
293 |
ty:= Gear^.Y; |
|
294 |
||
295 |
if RopePoints.Count > 0 then |
|
296 |
begin |
|
297 |
// set gear location to the remote end of the rope, the attachment point |
|
298 |
Gear^.X:= RopePoints.ar[0].X; |
|
299 |
Gear^.Y:= RopePoints.ar[0].Y; |
|
300 |
end; |
|
301 |
||
302 |
CheckCollision(Gear); |
|
303 |
// if we haven't found any collision yet then check the other side too |
|
304 |
if (Gear^.State and gstCollision) = 0 then |
|
305 |
begin |
|
306 |
Gear^.dX.isNegative:= not Gear^.dX.isNegative; |
|
307 |
Gear^.dY.isNegative:= not Gear^.dY.isNegative; |
|
308 |
CheckCollision(Gear); |
|
309 |
Gear^.dX.isNegative:= not Gear^.dX.isNegative; |
|
310 |
Gear^.dY.isNegative:= not Gear^.dY.isNegative; |
|
311 |
end; |
|
312 |
||
313 |
haveCollision:= (Gear^.State and gstCollision) <> 0; |
|
314 |
||
315 |
// restore gear location |
|
316 |
Gear^.X:= tx; |
|
317 |
Gear^.Y:= ty; |
|
318 |
end; |
|
319 |
||
320 |
// if the attack key is pressed, lose rope contact as well |
|
321 |
if (Gear^.Message and gmAttack) <> 0 then |
|
322 |
haveCollision:= false; |
|
323 |
||
324 |
HHGear^.dX.QWordValue:= HHGear^.dX.QWordValue shr 2; |
|
325 |
HHGear^.dY.QWordValue:= HHGear^.dY.QWordValue shr 2; |
|
7674
aead327f1e1a
fix for issue 293 : "rope stuck after picking crate"
sheepluva
parents:
7662
diff
changeset
|
326 |
if (not haveCollision) and ((Gear^.State and gsttmpFlag) <> 0) then |
7660 | 327 |
begin |
328 |
begin |
|
329 |
PlaySound(sndRopeRelease); |
|
330 |
if Gear^.Hedgehog^.CurAmmoType <> amParachute then |
|
331 |
RopeWaitCollision(Gear, HHGear) |
|
332 |
else |
|
333 |
RopeDeleteMe(Gear, HHGear) |
|
334 |
end |
|
335 |
end |
|
336 |
else |
|
337 |
if (Gear^.State and gsttmpFlag) = 0 then |
|
338 |
Gear^.State := Gear^.State or gsttmpFlag; |
|
339 |
end; |
|
340 |
||
341 |
procedure RopeRemoveFromAmmo(Gear, HHGear: PGear); |
|
342 |
begin |
|
343 |
if (Gear^.State and gstAttacked) = 0 then |
|
344 |
begin |
|
345 |
OnUsedAmmo(HHGear^.Hedgehog^); |
|
346 |
Gear^.State := Gear^.State or gstAttacked |
|
347 |
end; |
|
348 |
ApplyAmmoChanges(HHGear^.Hedgehog^) |
|
349 |
end; |
|
350 |
||
351 |
procedure doStepRopeAttach(Gear: PGear); |
|
352 |
var |
|
353 |
HHGear: PGear; |
|
354 |
tx, ty, tt: hwFloat; |
|
355 |
begin |
|
356 |
Gear^.X := Gear^.X - Gear^.dX; |
|
357 |
Gear^.Y := Gear^.Y - Gear^.dY; |
|
358 |
Gear^.Elasticity := Gear^.Elasticity + _1; |
|
359 |
||
360 |
HHGear := Gear^.Hedgehog^.Gear; |
|
361 |
DeleteCI(HHGear); |
|
362 |
||
363 |
if (HHGear^.State and gstMoving) <> 0 then |
|
364 |
begin |
|
365 |
if TestCollisionXwithGear(HHGear, hwSign(HHGear^.dX)) then |
|
366 |
SetLittle(HHGear^.dX); |
|
367 |
if HHGear^.dY.isNegative and (TestCollisionYwithGear(HHGear, -1) <> 0) then |
|
368 |
HHGear^.dY := _0; |
|
369 |
||
370 |
HHGear^.X := HHGear^.X + HHGear^.dX; |
|
371 |
Gear^.X := Gear^.X + HHGear^.dX; |
|
372 |
||
373 |
if TestCollisionYwithGear(HHGear, 1) <> 0 then |
|
374 |
begin |
|
375 |
CheckHHDamage(HHGear); |
|
376 |
HHGear^.dY := _0 |
|
377 |
//HHGear^.State:= HHGear^.State and (not (gstHHJumping or gstHHHJump)); |
|
378 |
end |
|
379 |
else |
|
380 |
begin |
|
381 |
HHGear^.Y := HHGear^.Y + HHGear^.dY; |
|
382 |
Gear^.Y := Gear^.Y + HHGear^.dY; |
|
383 |
HHGear^.dY := HHGear^.dY + cGravity; |
|
384 |
if (GameFlags and gfMoreWind) <> 0 then |
|
385 |
HHGear^.dX := HHGear^.dX + cWindSpeed / HHGear^.Density |
|
386 |
end; |
|
387 |
||
388 |
tt := Gear^.Elasticity; |
|
389 |
tx := _0; |
|
390 |
ty := _0; |
|
391 |
while tt > _20 do |
|
392 |
begin |
|
393 |
if ((hwRound(Gear^.Y+ty) and LAND_HEIGHT_MASK) = 0) and ((hwRound(Gear^.X+tx) and LAND_WIDTH_MASK) = 0) and ((Land[hwRound(Gear^.Y+ty), hwRound(Gear^.X+tx)] and $FF00) <> 0) then |
|
394 |
begin |
|
395 |
Gear^.X := Gear^.X + tx; |
|
396 |
Gear^.Y := Gear^.Y + ty; |
|
397 |
Gear^.Elasticity := tt; |
|
398 |
Gear^.doStep := @doStepRopeWork; |
|
399 |
PlaySound(sndRopeAttach); |
|
400 |
with HHGear^ do |
|
401 |
begin |
|
402 |
State := State and (not (gstAttacking or gstHHJumping or gstHHHJump)); |
|
403 |
Message := Message and (not gmAttack) |
|
404 |
end; |
|
405 |
||
406 |
RopeRemoveFromAmmo(Gear, HHGear); |
|
407 |
||
408 |
tt := _0; |
|
409 |
exit |
|
410 |
end; |
|
411 |
tx := tx + Gear^.dX + Gear^.dX; |
|
412 |
ty := ty + Gear^.dY + Gear^.dY; |
|
413 |
tt := tt - _2; |
|
414 |
end; |
|
415 |
end; |
|
416 |
||
7662 | 417 |
if Gear^.Elasticity < _20 then Gear^.CollisionMask:= $FF00 |
418 |
else Gear^.CollisionMask:= $FF7F; |
|
7660 | 419 |
CheckCollision(Gear); |
420 |
||
421 |
if (Gear^.State and gstCollision) <> 0 then |
|
422 |
if Gear^.Elasticity < _10 then |
|
423 |
Gear^.Elasticity := _10000 |
|
424 |
else |
|
425 |
begin |
|
426 |
Gear^.doStep := @doStepRopeWork; |
|
427 |
PlaySound(sndRopeAttach); |
|
428 |
with HHGear^ do |
|
429 |
begin |
|
430 |
State := State and (not (gstAttacking or gstHHJumping or gstHHHJump)); |
|
431 |
Message := Message and (not gmAttack) |
|
432 |
end; |
|
433 |
||
434 |
RopeRemoveFromAmmo(Gear, HHGear); |
|
435 |
||
436 |
exit |
|
437 |
end; |
|
438 |
||
439 |
if (Gear^.Elasticity > Gear^.Friction) |
|
440 |
or ((Gear^.Message and gmAttack) = 0) |
|
441 |
or ((HHGear^.State and gstHHDriven) = 0) |
|
442 |
or (HHGear^.Damage > 0) then |
|
443 |
begin |
|
444 |
with Gear^.Hedgehog^.Gear^ do |
|
445 |
begin |
|
446 |
State := State and (not gstAttacking); |
|
447 |
Message := Message and (not gmAttack) |
|
448 |
end; |
|
449 |
DeleteGear(Gear); |
|
450 |
exit; |
|
451 |
end; |
|
452 |
if CheckGearDrowning(HHGear) then DeleteGear(Gear) |
|
453 |
end; |
|
454 |
||
455 |
procedure doStepRope(Gear: PGear); |
|
456 |
begin |
|
457 |
Gear^.dX := - Gear^.dX; |
|
458 |
Gear^.dY := - Gear^.dY; |
|
459 |
Gear^.doStep := @doStepRopeAttach; |
|
460 |
PlaySound(sndRopeShot) |
|
461 |
end; |
|
462 |
||
463 |
end. |