2
2
* Hedgewars, a free turn based strategy game
3
* Copyright (c) 2005-2010 Andrey Korotaev <unC0Rr@gmail.com>
3
* Copyright (c) 2004-2011 Andrey Korotaev <unC0Rr@gmail.com>
5
5
* This program is free software; you can redistribute it and/or modify
6
6
* it under the terms of the GNU General Public License as published by
44
44
function TestCollisionXKick(Gear: PGear; Dir: LongInt): boolean;
45
45
function TestCollisionYKick(Gear: PGear; Dir: LongInt): boolean;
47
function TestCollisionX(Gear: PGear; Dir: LongInt): boolean;
47
48
function TestCollisionY(Gear: PGear; Dir: LongInt): boolean;
49
function TestCollisionXwithXYShift(Gear: PGear; ShiftX: hwFloat; ShiftY: LongInt; Dir: LongInt): boolean;
50
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt): boolean;
50
function TestCollisionXwithXYShift(Gear: PGear; ShiftX: hwFloat; ShiftY: LongInt; Dir: LongInt; withGear: boolean = true): boolean;
51
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt; withGear: boolean = true): boolean;
52
function calcSlopeTangent(Gear: PGear; collisionX, collisionY: LongInt; var outDeltaX, outDeltaY: LongInt; TestWord: LongWord): Boolean;
53
function TestRectancleForObstacle(x1, y1, x2, y2: LongInt; landOnly: boolean): boolean;
54
function CalcSlopeTangent(Gear: PGear; collisionX, collisionY: LongInt; var outDeltaX, outDeltaY: LongInt; TestWord: LongWord): Boolean;
55
uses uConsts, uLandGraphics, uVariables, uDebug;
57
uses uConsts, uLandGraphics, uVariables, uDebug, uGears;
57
59
type TCollisionEntry = record
58
60
X, Y, Radius: LongInt;
62
const MAXRECTSINDEX = 511;
64
const MAXRECTSINDEX = 1023;
63
65
var Count: Longword;
64
66
cinfos: array[0..MAXRECTSINDEX] of TCollisionEntry;
67
69
procedure AddGearCI(Gear: PGear);
69
72
if Gear^.CollisionIndex >= 0 then exit;
70
73
TryDo(Count <= MAXRECTSINDEX, 'Collision rects array overflow', true);
79
82
Gear^.CollisionIndex:= Count;
84
// mines are the easiest way to overflow collision
85
if (Count > (MAXRECTSINDEX-20)) then
88
while (t <> nil) and (t^.Kind <> gtMine) do
90
if (t <> nil) then DeleteGear(t)
83
94
procedure DeleteCI(Gear: PGear);
284
function TestCollisionXwithXYShift(Gear: PGear; ShiftX: hwFloat; ShiftY: LongInt; Dir: LongInt): boolean;
295
function TestCollisionXwithXYShift(Gear: PGear; ShiftX: hwFloat; ShiftY: LongInt; Dir: LongInt; withGear: boolean = true): boolean;
286
297
Gear^.X:= Gear^.X + ShiftX;
287
298
Gear^.Y:= Gear^.Y + int2hwFloat(ShiftY);
288
TestCollisionXwithXYShift:= TestCollisionXwithGear(Gear, Dir);
300
TestCollisionXwithXYShift:= TestCollisionXwithGear(Gear, Dir)
301
else TestCollisionXwithXYShift:= TestCollisionX(Gear, Dir);
289
302
Gear^.X:= Gear^.X - ShiftX;
290
303
Gear^.Y:= Gear^.Y - int2hwFloat(ShiftY)
305
function TestCollisionX(Gear: PGear; Dir: LongInt): boolean;
306
var x, y, i: LongInt;
308
x:= hwRound(Gear^.X);
309
if Dir < 0 then x:= x - Gear^.Radius
310
else x:= x + Gear^.Radius;
311
if (x and LAND_WIDTH_MASK) = 0 then
313
y:= hwRound(Gear^.Y) - Gear^.Radius + 1;
314
i:= y + Gear^.Radius * 2 - 2;
316
if (y and LAND_HEIGHT_MASK) = 0 then
317
if Land[y, x] > 255 then exit(true);
321
TestCollisionX:= false
293
324
function TestCollisionY(Gear: PGear; Dir: LongInt): boolean;
294
325
var x, y, i: LongInt;
309
340
TestCollisionY:= false
312
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt): boolean;
343
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt; withGear: boolean = true): boolean;
314
345
Gear^.X:= Gear^.X + int2hwFloat(ShiftX);
315
346
Gear^.Y:= Gear^.Y + int2hwFloat(ShiftY);
316
TestCollisionYwithXYShift:= TestCollisionYwithGear(Gear, Dir);
347
if withGear then TestCollisionYwithXYShift:= TestCollisionYwithGear(Gear, Dir)
348
else TestCollisionYwithXYShift:= TestCollisionY(Gear, Dir);
317
349
Gear^.X:= Gear^.X - int2hwFloat(ShiftX);
318
350
Gear^.Y:= Gear^.Y - int2hwFloat(ShiftY)
322
function calcSlopeTangent(Gear: PGear; collisionX, collisionY: LongInt; var outDeltaX, outDeltaY: LongInt; TestWord: LongWord): boolean;
353
function TestRectancleForObstacle(x1, y1, x2, y2: LongInt; landOnly: boolean): boolean;
376
if (hasBorder and ((y1 < 0) or (x1 < 0) or (x2 > LAND_WIDTH))) then
381
if ((y and LAND_HEIGHT_MASK) = 0) and ((x and LAND_WIDTH_MASK) = 0)
382
and (Land[y, x] > TestWord) then
385
TestRectancleForObstacle:= false
388
function CalcSlopeTangent(Gear: PGear; collisionX, collisionY: LongInt; var outDeltaX, outDeltaY: LongInt; TestWord: LongWord): boolean;
323
389
var ldx, ldy, rdx, rdy: LongInt;
324
390
i, j, mx, my, li, ri, jfr, jto, tmpo : ShortInt;
325
391
tmpx, tmpy: LongWord;