~ubuntu-branches/ubuntu/wily/hedgewars/wily

« back to all changes in this revision

Viewing changes to hedgewars/uCollisions.pas

  • Committer: Package Import Robot
  • Author(s): Dmitry E. Oboukhov
  • Date: 2011-09-23 10:16:55 UTC
  • mfrom: (1.2.11 upstream)
  • Revision ID: package-import@ubuntu.com-20110923101655-3977th2gc5n0a3pv
Tags: 0.9.16-1
* New upstream version.
 + Downloadable content! Simply click to install any content.
   New voices, hats, maps, themes, translations, music, scripts...
   Hedgewars is now more customisable than ever before! As time goes
   by we will be soliciting community content to feature on this page,
   so remember to check it from time to time. If you decide you want
   to go back to standard Hedgewars, just remove the Data directory
   from your Hedgewars config directory.
 + 3-D rendering! Diorama-like rendering of the game in a variety
   of 3D modes. Let us know which ones work best for you, we didn't
   really have the equipment to test them all.
 + Resizable game window.
 + New utilities! The Time Box will remove one of your hedgehogs
   from the game for a while, protecting from attack until it returns,
   somewhere else on the map. Land spray will allow you to build bridges,
   seal up holes, or just make life unpleasant for your enemies.
 + New single player: Bamboo Thicket, That Sinking Feeling, Newton and
   the Tree and multi-player: The Specialists, Space Invaders,
   Racer - scripts! And a ton more script hooks for scripters
 + New twists on old weapons. Drill strike, seduction and fire have
   been adjusted. Defective mines have been added, rope can attach to
   hogs/crates/barrels again, grenades now have variable bounce (use
   precise key + 1-5). Portal gun is now more usable in flight and
   all game actions are a lot faster.
 + New theme - Golf, dozens of new community hats and a new
   localised Default voice, Ukranian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
(*
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>
4
4
 *
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
22
22
interface
23
23
uses uFloat, uTypes;
24
24
 
25
 
const cMaxGearArrayInd = 255;
 
25
const cMaxGearArrayInd = 1023;
26
26
 
27
27
type PGearArray = ^TGearArray;
28
28
    TGearArray = record
44
44
function  TestCollisionXKick(Gear: PGear; Dir: LongInt): boolean;
45
45
function  TestCollisionYKick(Gear: PGear; Dir: LongInt): boolean;
46
46
 
 
47
function  TestCollisionX(Gear: PGear; Dir: LongInt): boolean;
47
48
function  TestCollisionY(Gear: PGear; Dir: LongInt): boolean;
48
49
 
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;
51
52
 
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;
53
55
 
54
56
implementation
55
 
uses uConsts, uLandGraphics, uVariables, uDebug;
 
57
uses uConsts, uLandGraphics, uVariables, uDebug, uGears;
56
58
 
57
59
type TCollisionEntry = record
58
60
            X, Y, Radius: LongInt;
59
61
            cGear: PGear;
60
62
            end;
61
63
 
62
 
const MAXRECTSINDEX = 511;
 
64
const MAXRECTSINDEX = 1023;
63
65
var Count: Longword;
64
66
    cinfos: array[0..MAXRECTSINDEX] of TCollisionEntry;
65
67
    ga: TGearArray;
66
68
 
67
69
procedure AddGearCI(Gear: PGear);
 
70
var t: PGear;
68
71
begin
69
72
if Gear^.CollisionIndex >= 0 then exit;
70
73
TryDo(Count <= MAXRECTSINDEX, 'Collision rects array overflow', true);
77
80
    cGear:= Gear
78
81
    end;
79
82
Gear^.CollisionIndex:= Count;
80
 
inc(Count)
 
83
inc(Count);
 
84
// mines are the easiest way to overflow collision
 
85
if (Count > (MAXRECTSINDEX-20)) then
 
86
    begin
 
87
    t:= GearsList;
 
88
    while (t <> nil) and (t^.Kind <> gtMine) do 
 
89
        t:= t^.NextGear;
 
90
    if (t <> nil) then DeleteGear(t)
 
91
    end;
81
92
end;
82
93
 
83
94
procedure DeleteCI(Gear: PGear);
281
292
   end
282
293
end;
283
294
 
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;
285
296
begin
286
297
Gear^.X:= Gear^.X + ShiftX;
287
298
Gear^.Y:= Gear^.Y + int2hwFloat(ShiftY);
288
 
TestCollisionXwithXYShift:= TestCollisionXwithGear(Gear, Dir);
 
299
if withGear then 
 
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)
291
304
end;
 
305
function TestCollisionX(Gear: PGear; Dir: LongInt): boolean;
 
306
var x, y, i: LongInt;
 
307
begin
 
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
 
312
   begin
 
313
   y:= hwRound(Gear^.Y) - Gear^.Radius + 1;
 
314
   i:= y + Gear^.Radius * 2 - 2;
 
315
   repeat
 
316
     if (y and LAND_HEIGHT_MASK) = 0 then
 
317
        if Land[y, x] > 255 then exit(true);
 
318
     inc(y)
 
319
   until (y > i);
 
320
   end;
 
321
TestCollisionX:= false
 
322
end;
292
323
 
293
324
function TestCollisionY(Gear: PGear; Dir: LongInt): boolean;
294
325
var x, y, i: LongInt;
309
340
TestCollisionY:= false
310
341
end;
311
342
 
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;
313
344
begin
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)
319
351
end;
320
352
 
321
 
 
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;
 
354
var x, y: LongInt;
 
355
    TestWord: LongWord;
 
356
begin
 
357
if landOnly then
 
358
    TestWord:= 255
 
359
else
 
360
    TestWord:= 0;
 
361
 
 
362
if x1 > x2 then
 
363
begin
 
364
    x  := x1;
 
365
    x1 := x2;
 
366
    x2 := x;
 
367
end;
 
368
 
 
369
if y1 > y2 then
 
370
begin
 
371
    y  := y1;
 
372
    y1 := y2;
 
373
    y2 := y;
 
374
end;
 
375
 
 
376
if (hasBorder and ((y1 < 0) or (x1 < 0) or (x2 > LAND_WIDTH))) then
 
377
    exit(true);
 
378
 
 
379
for y := y1 to y2 do
 
380
    for x := x1 to x2 do
 
381
        if ((y and LAND_HEIGHT_MASK) = 0) and ((x and LAND_WIDTH_MASK) = 0)
 
382
          and (Land[y, x] > TestWord) then
 
383
            exit(true);
 
384
 
 
385
TestRectancleForObstacle:= false
 
386
end;
 
387
 
 
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;