hedgewars/uCollisions.pas
changeset 2948 3f21a9dc93d0
parent 2716 b9ca1bfca24f
child 3038 4e48c276a468
equal deleted inserted replaced
2947:803b277e4894 2948:3f21a9dc93d0
    23 uses uGears, uFloat;
    23 uses uGears, uFloat;
    24 
    24 
    25 const cMaxGearArrayInd = 255;
    25 const cMaxGearArrayInd = 255;
    26 
    26 
    27 type PGearArray = ^TGearArray;
    27 type PGearArray = ^TGearArray;
    28 	TGearArray = record
    28     TGearArray = record
    29 			ar: array[0..cMaxGearArrayInd] of PGear;
    29             ar: array[0..cMaxGearArrayInd] of PGear;
    30 			Count: Longword
    30             Count: Longword
    31 			end;
    31             end;
    32 
    32 
    33 procedure init_uCollisions;
    33 procedure init_uCollisions;
    34 procedure free_uCollisions;
    34 procedure free_uCollisions;
    35 
    35 
    36 procedure AddGearCI(Gear: PGear);
    36 procedure AddGearCI(Gear: PGear);
    51 
    51 
    52 implementation
    52 implementation
    53 uses uMisc, uConsts, uLand, uLandGraphics, uConsole;
    53 uses uMisc, uConsts, uLand, uLandGraphics, uConsole;
    54 
    54 
    55 type TCollisionEntry = record
    55 type TCollisionEntry = record
    56 			X, Y, Radius: LongInt;
    56             X, Y, Radius: LongInt;
    57 			cGear: PGear;
    57             cGear: PGear;
    58 			end;
    58             end;
    59 
    59 
    60 const MAXRECTSINDEX = 511;
    60 const MAXRECTSINDEX = 511;
    61 var Count: Longword;
    61 var Count: Longword;
    62     cinfos: array[0..MAXRECTSINDEX] of TCollisionEntry;
    62     cinfos: array[0..MAXRECTSINDEX] of TCollisionEntry;
    63     ga: TGearArray;
    63     ga: TGearArray;
    65 procedure AddGearCI(Gear: PGear);
    65 procedure AddGearCI(Gear: PGear);
    66 begin
    66 begin
    67 if Gear^.CollisionIndex >= 0 then exit;
    67 if Gear^.CollisionIndex >= 0 then exit;
    68 TryDo(Count <= MAXRECTSINDEX, 'Collision rects array overflow', true);
    68 TryDo(Count <= MAXRECTSINDEX, 'Collision rects array overflow', true);
    69 with cinfos[Count] do
    69 with cinfos[Count] do
    70 	begin
    70     begin
    71 	X:= hwRound(Gear^.X);
    71     X:= hwRound(Gear^.X);
    72 	Y:= hwRound(Gear^.Y);
    72     Y:= hwRound(Gear^.Y);
    73 	Radius:= Gear^.Radius;
    73     Radius:= Gear^.Radius;
    74 	ChangeRoundInLand(X, Y, Radius - 1, true);
    74     ChangeRoundInLand(X, Y, Radius - 1, true);
    75 	cGear:= Gear
    75     cGear:= Gear
    76 	end;
    76     end;
    77 Gear^.CollisionIndex:= Count;
    77 Gear^.CollisionIndex:= Count;
    78 inc(Count)
    78 inc(Count)
    79 end;
    79 end;
    80 
    80 
    81 procedure DeleteCI(Gear: PGear);
    81 procedure DeleteCI(Gear: PGear);
    82 begin
    82 begin
    83 if Gear^.CollisionIndex >= 0 then
    83 if Gear^.CollisionIndex >= 0 then
    84 	begin
    84     begin
    85 	with cinfos[Gear^.CollisionIndex] do
    85     with cinfos[Gear^.CollisionIndex] do
    86 		ChangeRoundInLand(X, Y, Radius - 1, false);
    86         ChangeRoundInLand(X, Y, Radius - 1, false);
    87 	cinfos[Gear^.CollisionIndex]:= cinfos[Pred(Count)];
    87     cinfos[Gear^.CollisionIndex]:= cinfos[Pred(Count)];
    88 	cinfos[Gear^.CollisionIndex].cGear^.CollisionIndex:= Gear^.CollisionIndex;
    88     cinfos[Gear^.CollisionIndex].cGear^.CollisionIndex:= Gear^.CollisionIndex;
    89 	Gear^.CollisionIndex:= -1;
    89     Gear^.CollisionIndex:= -1;
    90 	dec(Count)
    90     dec(Count)
    91 	end;
    91     end;
    92 end;
    92 end;
    93 
    93 
    94 function CheckGearsCollision(Gear: PGear): PGearArray;
    94 function CheckGearsCollision(Gear: PGear): PGearArray;
    95 var mx, my: LongInt;
    95 var mx, my: LongInt;
    96 	i: Longword;
    96     i: Longword;
    97 begin
    97 begin
    98 CheckGearsCollision:= @ga;
    98 CheckGearsCollision:= @ga;
    99 ga.Count:= 0;
    99 ga.Count:= 0;
   100 if Count = 0 then exit;
   100 if Count = 0 then exit;
   101 mx:= hwRound(Gear^.X);
   101 mx:= hwRound(Gear^.X);
   102 my:= hwRound(Gear^.Y);
   102 my:= hwRound(Gear^.Y);
   103 
   103 
   104 for i:= 0 to Pred(Count) do
   104 for i:= 0 to Pred(Count) do
   105 	with cinfos[i] do
   105     with cinfos[i] do
   106 		if (Gear <> cGear) and
   106         if (Gear <> cGear) and
   107 			(sqr(mx - x) + sqr(my - y) <= sqr(Radius + Gear^.Radius)) then
   107             (sqr(mx - x) + sqr(my - y) <= sqr(Radius + Gear^.Radius)) then
   108 				begin
   108                 begin
   109 				ga.ar[ga.Count]:= cinfos[i].cGear;
   109                 ga.ar[ga.Count]:= cinfos[i].cGear;
   110 				inc(ga.Count)
   110                 inc(ga.Count)
   111 				end
   111                 end
   112 end;
   112 end;
   113 
   113 
   114 function TestCollisionXwithGear(Gear: PGear; Dir: LongInt): boolean;
   114 function TestCollisionXwithGear(Gear: PGear; Dir: LongInt): boolean;
   115 var x, y, i: LongInt;
   115 var x, y, i: LongInt;
   116 	TestWord: LongWord;
   116     TestWord: LongWord;
   117 begin
   117 begin
   118 if Gear^.IntersectGear <> nil then
   118 if Gear^.IntersectGear <> nil then
   119    with Gear^ do
   119    with Gear^ do
   120         if (hwRound(IntersectGear^.X) + IntersectGear^.Radius < hwRound(X) - Radius) or
   120         if (hwRound(IntersectGear^.X) + IntersectGear^.Radius < hwRound(X) - Radius) or
   121            (hwRound(IntersectGear^.X) - IntersectGear^.Radius > hwRound(X) + Radius) then
   121            (hwRound(IntersectGear^.X) - IntersectGear^.Radius > hwRound(X) + Radius) then
   312 Gear^.Y:= Gear^.Y - int2hwFloat(ShiftY)
   312 Gear^.Y:= Gear^.Y - int2hwFloat(ShiftY)
   313 end;
   313 end;
   314 
   314 
   315 procedure init_uCollisions;
   315 procedure init_uCollisions;
   316 begin
   316 begin
   317 	Count:= 0;
   317     Count:= 0;
   318 end;
   318 end;
   319 
   319 
   320 procedure free_uCollisions;
   320 procedure free_uCollisions;
   321 begin
   321 begin
   322 
   322