hedgewars/uAIAmmoTests.pas
changeset 3677 7d917b587547
parent 3476 1ec68b8d3bd1
child 3697 d5b30d6373fc
equal deleted inserted replaced
3675:857c9546a822 3677:7d917b587547
   101             );
   101             );
   102 
   102 
   103 const BadTurn = Low(LongInt) div 4;
   103 const BadTurn = Low(LongInt) div 4;
   104 
   104 
   105 implementation
   105 implementation
   106 uses uMisc, uAIMisc, uLand;
   106 uses uMisc, uAIMisc, uLand, uTeams;
   107 
   107 
   108 function Metric(x1, y1, x2, y2: LongInt): LongInt;
   108 function Metric(x1, y1, x2, y2: LongInt): LongInt;
   109 begin
   109 begin
   110 Metric:= abs(x1 - x2) + abs(y1 - y2)
   110 Metric:= abs(x1 - x2) + abs(y1 - y2)
   111 end;
   111 end;
   119     function CheckTrace: LongInt;
   119     function CheckTrace: LongInt;
   120     var x, y, dX, dY: hwFloat;
   120     var x, y, dX, dY: hwFloat;
   121         t: LongInt;
   121         t: LongInt;
   122         value: LongInt;
   122         value: LongInt;
   123     begin
   123     begin
   124     x:= Me^.X;
   124     with PHedgehog(Me^.Hedgehog)^ do
   125     y:= Me^.Y;
   125         begin
       
   126         x:= Me^.X + int2hwfloat(round(GetLaunchX(Ammo^[CurSlot, CurAmmo].AmmoType, hwSign(Me^.dX), Me^.Angle)));
       
   127         y:= Me^.Y + int2hwfloat(round(GetLaunchY(Ammo^[CurSlot, CurAmmo].AmmoType, Me^.Angle)))
       
   128         end;
   126     dX:= Vx;
   129     dX:= Vx;
   127     dY:= -Vy;
   130     dY:= -Vy;
   128     t:= rTime;
   131     t:= rTime;
   129     repeat
   132     repeat
   130       x:= x + dX;
   133       x:= x + dX;
   174 
   177 
   175     function CheckTrace: LongInt;
   178     function CheckTrace: LongInt;
   176     var x, y, dY: hwFloat;
   179     var x, y, dY: hwFloat;
   177         t: LongInt;
   180         t: LongInt;
   178     begin
   181     begin
   179     x:= Me^.X;
   182     with PHedgehog(Me^.Hedgehog)^ do
   180     y:= Me^.Y;
   183         begin
       
   184         x:= Me^.X + int2hwfloat(round(GetLaunchX(Ammo^[CurSlot, CurAmmo].AmmoType, hwSign(Me^.dX), Me^.Angle)));
       
   185         y:= Me^.Y + int2hwfloat(round(GetLaunchY(Ammo^[CurSlot, CurAmmo].AmmoType, Me^.Angle)))
       
   186         end;
   181     dY:= -Vy;
   187     dY:= -Vy;
   182     t:= TestTime;
   188     t:= TestTime;
   183     repeat
   189     repeat
   184       x:= x + Vx;
   190       x:= x + Vx;
   185       y:= y + dY;
   191       y:= y + dY;
   226 
   232 
   227     function CheckTrace: LongInt;
   233     function CheckTrace: LongInt;
   228     var x, y, dY: hwFloat;
   234     var x, y, dY: hwFloat;
   229         t: LongInt;
   235         t: LongInt;
   230     begin
   236     begin
   231     x:= Me^.X;
   237     with PHedgehog(Me^.Hedgehog)^ do
   232     y:= Me^.Y;
   238         begin
       
   239         x:= Me^.X + int2hwfloat(round(GetLaunchX(Ammo^[CurSlot, CurAmmo].AmmoType, hwSign(Me^.dX), Me^.Angle)));
       
   240         y:= Me^.Y + int2hwfloat(round(GetLaunchY(Ammo^[CurSlot, CurAmmo].AmmoType, Me^.Angle)))
       
   241         end;
   233     dY:= -Vy;
   242     dY:= -Vy;
   234     t:= TestTime;
   243     t:= TestTime;
   235     repeat
   244     repeat
   236       x:= x + Vx;
   245       x:= x + Vx;
   237       y:= y + dY;
   246       y:= y + dY;
   278 
   287 
   279     function CheckTrace: LongInt;
   288     function CheckTrace: LongInt;
   280     var x, y, dY: hwFloat;
   289     var x, y, dY: hwFloat;
   281         t: LongInt;
   290         t: LongInt;
   282     begin
   291     begin
   283     x:= Me^.X;
   292     with PHedgehog(Me^.Hedgehog)^ do
   284     y:= Me^.Y;
   293         begin
       
   294         x:= Me^.X + int2hwfloat(round(GetLaunchX(Ammo^[CurSlot, CurAmmo].AmmoType, hwSign(Me^.dX), Me^.Angle)));
       
   295         y:= Me^.Y + int2hwfloat(round(GetLaunchY(Ammo^[CurSlot, CurAmmo].AmmoType, Me^.Angle)))
       
   296         end;
   285     dY:= -Vy;
   297     dY:= -Vy;
   286     t:= TestTime;
   298     t:= TestTime;
   287     repeat
   299     repeat
   288       x:= x + Vx;
   300       x:= x + Vx;
   289       y:= y + dY;
   301       y:= y + dY;
   334 
   346 
   335     function CheckTrace: LongInt;
   347     function CheckTrace: LongInt;
   336     var x, y, dY: hwFloat;
   348     var x, y, dY: hwFloat;
   337         t: LongInt;
   349         t: LongInt;
   338     begin
   350     begin
   339     x:= Me^.X;
   351     with PHedgehog(Me^.Hedgehog)^ do
   340     y:= Me^.Y;
   352         begin
       
   353         x:= Me^.X + int2hwfloat(round(GetLaunchX(Ammo^[CurSlot, CurAmmo].AmmoType, hwSign(Me^.dX), Me^.Angle)));
       
   354         y:= Me^.Y + int2hwfloat(round(GetLaunchY(Ammo^[CurSlot, CurAmmo].AmmoType, Me^.Angle)))
       
   355         end;
   341     dY:= -Vy;
   356     dY:= -Vy;
   342     t:= TestTime;
   357     t:= TestTime;
   343     repeat
   358     repeat
   344       x:= x + Vx;
   359       x:= x + Vx;
   345       y:= y + dY;
   360       y:= y + dY;
   386 
   401 
   387     function CheckTrace: LongInt;
   402     function CheckTrace: LongInt;
   388     var x, y, dY: hwFloat;
   403     var x, y, dY: hwFloat;
   389         value: LongInt;
   404         value: LongInt;
   390     begin
   405     begin
   391         x:= Me^.X;
   406     with PHedgehog(Me^.Hedgehog)^ do
   392         y:= Me^.Y;
   407         begin
       
   408         x:= Me^.X + int2hwfloat(round(GetLaunchX(Ammo^[CurSlot, CurAmmo].AmmoType, hwSign(Me^.dX), Me^.Angle)));
       
   409         y:= Me^.Y + int2hwfloat(round(GetLaunchY(Ammo^[CurSlot, CurAmmo].AmmoType, Me^.Angle)))
       
   410         end;
   393         dY:= -Vy;
   411         dY:= -Vy;
   394 
   412 
   395         repeat
   413         repeat
   396             x:= x + Vx;
   414             x:= x + Vx;
   397             y:= y + dY;
   415             y:= y + dY;
   471     range: integer;
   489     range: integer;
   472 begin
   490 begin
   473 ap.ExplR:= 0;
   491 ap.ExplR:= 0;
   474 ap.Time:= 0;
   492 ap.Time:= 0;
   475 ap.Power:= 1;
   493 ap.Power:= 1;
   476 range:= Metric(hwRound(Me^.X), hwRound(Me^.Y), Targ.X, Targ.Y);
   494 with PHedgehog(Me^.Hedgehog)^ do
       
   495     begin
       
   496     x:= Me^.X + int2hwfloat(round(GetLaunchX(Ammo^[CurSlot, CurAmmo].AmmoType, hwSign(Me^.dX), Me^.Angle)));
       
   497     y:= Me^.Y + int2hwfloat(round(GetLaunchY(Ammo^[CurSlot, CurAmmo].AmmoType, Me^.Angle)))
       
   498     end;
       
   499 range:= Metric(hwRound(x), hwRound(y), Targ.X, Targ.Y);
   477 if ( range < MIN_RANGE ) or ( range > MAX_RANGE ) then exit(BadTurn);
   500 if ( range < MIN_RANGE ) or ( range > MAX_RANGE ) then exit(BadTurn);
   478 Vx:= (int2hwFloat(Targ.X) - Me^.X) * _1div1024;
   501 Vx:= (int2hwFloat(Targ.X) - x) * _1div1024;
   479 Vy:= (int2hwFloat(Targ.Y) - Me^.Y) * _1div1024;
   502 Vy:= (int2hwFloat(Targ.Y) - y) * _1div1024;
   480 x:= Me^.X;
       
   481 y:= Me^.Y;
       
   482 ap.Angle:= DxDy2AttackAngle(Vx, -Vy);
   503 ap.Angle:= DxDy2AttackAngle(Vx, -Vy);
   483 repeat
   504 repeat
   484   x:= x + vX;
   505   x:= x + vX;
   485   y:= y + vY;
   506   y:= y + vY;
   486   rx:= hwRound(x);
   507   rx:= hwRound(x);
   505 begin
   526 begin
   506 Level:= Level; // avoid compiler hint
   527 Level:= Level; // avoid compiler hint
   507 ap.ExplR:= 0;
   528 ap.ExplR:= 0;
   508 ap.Time:= 0;
   529 ap.Time:= 0;
   509 ap.Power:= 1;
   530 ap.Power:= 1;
       
   531 with PHedgehog(Me^.Hedgehog)^ do
       
   532     begin
       
   533     x:= Me^.X + int2hwfloat(round(GetLaunchX(Ammo^[CurSlot, CurAmmo].AmmoType, hwSign(Me^.dX), Me^.Angle)));
       
   534     y:= Me^.Y + int2hwfloat(round(GetLaunchY(Ammo^[CurSlot, CurAmmo].AmmoType, Me^.Angle)))
       
   535     end;
   510 if Abs(hwRound(Me^.X) - Targ.X) + Abs(hwRound(Me^.Y) - Targ.Y) < 80 then
   536 if Abs(hwRound(Me^.X) - Targ.X) + Abs(hwRound(Me^.Y) - Targ.Y) < 80 then
   511    exit(BadTurn);
   537    exit(BadTurn);
   512 t:= _0_5 / Distance(int2hwFloat(Targ.X) - Me^.X, int2hwFloat(Targ.Y) - Me^.Y);
   538 t:= _0_5 / Distance(int2hwFloat(Targ.X) - x, int2hwFloat(Targ.Y) - y);
   513 Vx:= (int2hwFloat(Targ.X) - Me^.X) * t;
   539 Vx:= (int2hwFloat(Targ.X) - x) * t;
   514 Vy:= (int2hwFloat(Targ.Y) - Me^.Y) * t;
   540 Vy:= (int2hwFloat(Targ.Y) - y) * t;
   515 x:= Me^.X;
       
   516 y:= Me^.Y;
       
   517 ap.Angle:= DxDy2AttackAngle(Vx, -Vy);
   541 ap.Angle:= DxDy2AttackAngle(Vx, -Vy);
   518 d:= 0;
   542 d:= 0;
   519 
   543 
   520 repeat
   544 repeat
   521   x:= x + vX;
   545   x:= x + vX;
   529 TestDesertEagle:= valueResult
   553 TestDesertEagle:= valueResult
   530 end;
   554 end;
   531 
   555 
   532 function TestBaseballBat(Me: PGear; Targ: TPoint; Level: LongInt; var ap: TAttackParams): LongInt;
   556 function TestBaseballBat(Me: PGear; Targ: TPoint; Level: LongInt; var ap: TAttackParams): LongInt;
   533 var valueResult: LongInt;
   557 var valueResult: LongInt;
       
   558     x, y: hwFloat;
   534 begin
   559 begin
   535 Level:= Level; // avoid compiler hint
   560 Level:= Level; // avoid compiler hint
   536 ap.ExplR:= 0;
   561 ap.ExplR:= 0;
   537 if (Level > 2) or (Abs(hwRound(Me^.X) - Targ.X) + Abs(hwRound(Me^.Y) - Targ.Y) > 25) then
   562 if (Level > 2) or (Abs(hwRound(Me^.X) - Targ.X) + Abs(hwRound(Me^.Y) - Targ.Y) > 25) then
   538    exit(BadTurn);
   563    exit(BadTurn);
   539 
   564 
   540 ap.Time:= 0;
   565 ap.Time:= 0;
   541 ap.Power:= 1;
   566 ap.Power:= 1;
   542 if (Targ.X) - hwRound(Me^.X) >= 0 then ap.Angle:=   cMaxAngle div 4
   567 with PHedgehog(Me^.Hedgehog)^ do
       
   568     begin
       
   569     x:= Me^.X + int2hwfloat(round(GetLaunchX(Ammo^[CurSlot, CurAmmo].AmmoType, hwSign(Me^.dX), Me^.Angle)));
       
   570     y:= Me^.Y + int2hwfloat(round(GetLaunchY(Ammo^[CurSlot, CurAmmo].AmmoType, Me^.Angle)))
       
   571     end;
       
   572 if (Targ.X) - hwRound(x) >= 0 then ap.Angle:=   cMaxAngle div 4
   543                                   else ap.Angle:= - cMaxAngle div 4;
   573                                   else ap.Angle:= - cMaxAngle div 4;
   544 valueResult:= RateShove(Me, hwRound(Me^.X) + 10 * hwSign(int2hwFloat(Targ.X) - Me^.X), hwRound(Me^.Y), 15, 30);
   574 valueResult:= RateShove(Me, hwRound(x) + 10 * hwSign(int2hwFloat(Targ.X) - x), hwRound(y), 15, 30);
   545 if valueResult <= 0 then valueResult:= BadTurn else inc(valueResult);
   575 if valueResult <= 0 then valueResult:= BadTurn else inc(valueResult);
   546 TestBaseballBat:= valueResult;
   576 TestBaseballBat:= valueResult;
   547 end;
   577 end;
   548 
   578 
   549 function TestFirePunch(Me: PGear; Targ: TPoint; Level: LongInt; var ap: TAttackParams): LongInt;
   579 function TestFirePunch(Me: PGear; Targ: TPoint; Level: LongInt; var ap: TAttackParams): LongInt;
   550 var i, valueResult: LongInt;
   580 var i, valueResult: LongInt;
       
   581     x, y: hwFloat;
   551 begin
   582 begin
   552 Level:= Level; // avoid compiler hint
   583 Level:= Level; // avoid compiler hint
   553 ap.ExplR:= 0;
   584 ap.ExplR:= 0;
   554 ap.Time:= 0;
   585 ap.Time:= 0;
   555 ap.Power:= 1;
   586 ap.Power:= 1;
   556 ap.Angle:= 0;
   587 ap.Angle:= 0;
   557 if (Abs(hwRound(Me^.X) - Targ.X) > 25)
   588 with PHedgehog(Me^.Hedgehog)^ do
   558 or (Abs(hwRound(Me^.Y) - 50 - Targ.Y) > 50) then
   589     begin
   559     begin
   590     x:= Me^.X + int2hwfloat(round(GetLaunchX(Ammo^[CurSlot, CurAmmo].AmmoType, hwSign(Me^.dX), Me^.Angle)));
   560     if TestColl(hwRound(Me^.Y), hwRound(Me^.Y) - 16, 6)
   591     y:= Me^.Y + int2hwfloat(round(GetLaunchY(Ammo^[CurSlot, CurAmmo].AmmoType, Me^.Angle)))
   561     and (RateShove(Me, hwRound(Me^.X) + 10 * hwSign(Me^.dX), hwRound(Me^.Y) - 40, 30, 30) = 0) then
   592     end;
       
   593 if (Abs(hwRound(x) - Targ.X) > 25)
       
   594 or (Abs(hwRound(y) - 50 - Targ.Y) > 50) then
       
   595     begin
       
   596     if TestColl(hwRound(x), hwRound(y) - 16, 6)
       
   597     and (RateShove(Me, hwRound(x) + 10 * hwSign(Me^.dX), hwRound(y) - 40, 30, 30) = 0) then
   562         valueResult:= Succ(BadTurn)
   598         valueResult:= Succ(BadTurn)
   563     else
   599     else
   564         valueResult:= BadTurn;
   600         valueResult:= BadTurn;
   565     exit(valueResult)
   601     exit(valueResult)
   566     end;
   602     end;
   567 
   603 
   568 valueResult:= 0;
   604 valueResult:= 0;
   569 for i:= 0 to 4 do
   605 for i:= 0 to 4 do
   570     valueResult:= valueResult + RateShove(Me, hwRound(Me^.X) + 10 * hwSign(int2hwFloat(Targ.X) - Me^.X),
   606     valueResult:= valueResult + RateShove(Me, hwRound(x) + 10 * hwSign(int2hwFloat(Targ.X) - x),
   571                                     hwRound(Me^.Y) - 20 * i - 5, 10, 30);
   607                                     hwRound(y) - 20 * i - 5, 10, 30);
   572 if valueResult <= 0 then
   608 if valueResult <= 0 then
   573     valueResult:= BadTurn
   609     valueResult:= BadTurn
   574 else
   610 else
   575     inc(valueResult);
   611     inc(valueResult);
   576 
   612