Changeset 27 for trunk/UCore.pas


Ignore:
Timestamp:
Sep 29, 2011, 11:10:27 PM (13 years ago)
Author:
george
Message:
  • Fixed: Tank block in one direction near obstacle. It was not possible to reverse tank direction.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UCore.pas

    r26 r27  
    1414  EnergySteps = 4000;
    1515  EnergyDecreaseDig = 0.001;
     16  EnergyDecreaseShoot = 0.005;
    1617  ShieldSteps = 40;
    1718  ExplosionBulletCount = 100;
     
    1920  ExplosionBulletMaxSpeed = 0.5;
    2021  ExplosionBulletMinSpeed = 0.2;
    21   BulletExplosionRange = 3;
    22   ShootDelay = 0.2; // seconds
     22  BulletExplosionRange = 4;
     23  ShootDelay = 0.1; // seconds
    2324  DigDelay = 0.2; // seconds
    24   ShootDigDelay = 0.005; // seconds
    25   ShootEnergyDecrease = 0.01;
     25  ShootDigDelay = 0.05; // seconds
     26  MoveDelay = 0.05; // seconds
    2627  PlayerFrameWidth = 80;
    2728  PlayerFrameHeight = 80;
     
    7879  TMatterKind = (mkSpace, mkDirt, mkRock, mkBullet, mkTankBody,
    7980    mkHome, mkBorder);
     81
     82  TColisionState = record
     83    Blocking: Boolean;
     84    Diggable: Boolean;
     85  end;
    8086
    8187  { TPlayer }
     
    105111    LastShootTime: TDateTime;
    106112    LastDigTime: TDateTime;
     113    LastMoveTime: TDateTime;
    107114    Energy: Real;
    108115    LastEnergy: Real;
     
    113120    procedure Explosion(Position: TPoint; Distance: Integer);
    114121    procedure Control;
     122    function CheckNewDestination: Boolean;
    115123    procedure Tick;
    116124    procedure Paint;
    117125    procedure PlaceHouse;
    118     function CheckColision: TMatterIndex;
     126    function CheckColision: TColisionState;
    119127    procedure ShowTank;
    120128    procedure HideTank;
     
    131139    Player: Integer;
    132140    Color: TColor;
     141    Diggable: Boolean;
     142    Blocking: Boolean;
    133143  end;
    134144
     
    241251  repeat
    242252    Engine.Tick;
    243     Sleep(50);
     253    Sleep(10);
    244254  until Terminated;
    245255end;
     
    314324    Color := $0756b0;
    315325    Player := -1;
     326    Diggable := True;
    316327  end;
    317328  // Dirt2
     
    320331    Color := $2170c3;
    321332    Player := -1;
     333    Diggable := True;
    322334  end;
    323335  // Rock
     
    326338    Color := TColor($9a9a9a);
    327339    Player := -1;
     340    Blocking := True;
    328341  end;
    329342  // Bullet1
     
    351364    Color := clYellow;
    352365    Player := 0;
     366    Blocking := True;
    353367  end;
    354368  // Player 1 home
     
    357371    Color := TColor($00ff00);
    358372    Player := 0;
     373    Blocking := True;
    359374  end;
    360375  // Player 1 body1
     
    363378    Color := TColor($00ff00);
    364379    Player := 0;
     380    Blocking := True;
    365381  end;
    366382  // Player 1 body2
     
    369385    Color := TColor($00a000);
    370386    Player := 0;
     387    Blocking := True;
    371388  end;
    372389
     
    376393    Color := clYellow;
    377394    Player := 1;
     395    Blocking := True;
    378396  end;
    379397  // Player 2 home
     
    382400    Color :=  TColor($ff2c2c);
    383401    Player := 1;
     402    Blocking := True;
    384403  end;
    385404  // Player 2 body1
     
    388407    Color := TColor($ff2c2c);
    389408    Player := 1;
     409    Blocking := True;
    390410  end;
    391411  // Player 2 body2
     
    394414    Color := TColor($b60000);
    395415    Player := 1;
     416    Blocking := True;
    396417  end;
    397418
     
    401422    Color := clYellow;
    402423    Player := 2;
     424    Blocking := True;
    403425  end;
    404426  // Player 3 home
     
    407429    Color :=  TColor($0000ff);
    408430    Player := 2;
     431    Blocking := True;
    409432  end;
    410433  // Player 3 body1
     
    413436    Color := TColor($0000ff);
    414437    Player := 2;
     438    Blocking := True;
    415439  end;
    416440  // Player 3 body2
     
    419443    Color := TColor($0000a0);
    420444    Player := 2;
     445    Blocking := True;
    421446  end;
    422447
     
    426451    Color := clYellow;
    427452    Player := 3;
     453    Blocking := True;
    428454  end;
    429455  // Player 4 home
     
    432458    Color :=  TColor($ff2cff);
    433459    Player := 3;
     460    Blocking := True;
    434461  end;
    435462  // Player 4 body1
     
    438465    Color := TColor($ff2cff);
    439466    Player := 3;
     467    Blocking := True;
    440468  end;
    441469  // Player 4 body2
     
    444472    Color := TColor($b600b6);
    445473    Player := 3;
     474    Blocking := True;
    446475  end;
    447476
     
    451480    Color := clYellow;
    452481    Player := 4;
     482    Blocking := True;
    453483  end;
    454484  // Player 5 home
     
    457487    Color := TColor($ffff2c);
    458488    Player := 4;
     489    Blocking := True;
    459490  end;
    460491  // Player 5 body1
     
    463494    Color := TColor($ffff2c);
    464495    Player := 4;
     496    Blocking := True;
    465497  end;
    466498  // Player 5 body2
     
    469501    Color := TColor($b6b600);
    470502    Player := 4;
     503    Blocking := True;
    471504  end;
    472505
     
    476509    Color := clYellow;
    477510    Player := 5;
     511    Blocking := True;
    478512  end;
    479513  // Player 6 home
     
    482516    Color :=  TColor($2cffff);
    483517    Player := 5;
     518    Blocking := True;
    484519  end;
    485520  // Player 6 body1
     
    488523    Color := TColor($2cffff);
    489524    Player := 5;
     525    Blocking := True;
    490526  end;
    491527  // Player 6 body2
     
    494530    Color := TColor($00b6b6);
    495531    Player := 5;
     532    Blocking := True;
    496533  end;
    497534
     
    501538    Color := clYellow;
    502539    Player := 6;
     540    Blocking := True;
    503541  end;
    504542  // Player 7 home
     
    507545    Color :=  TColor($008cff);
    508546    Player := 6;
     547    Blocking := True;
    509548  end;
    510549  // Player 7 body1
     
    513552    Color := TColor($008cff);
    514553    Player := 6;
     554    Blocking := True;
    515555  end;
    516556  // Player 7 body2
     
    519559    Color := TColor($002da0);
    520560    Player := 6;
     561    Blocking := True;
    521562  end;
    522563
     
    526567    Color := clYellow;
    527568    Player := 7;
     569    Blocking := True;
    528570  end;
    529571  // Player 8 home
     
    532574    Color :=  TColor($d0d0d0);
    533575    Player := 7;
     576    Blocking := True;
    534577  end;
    535578  // Player 8 body1
     
    538581    Color := TColor($d0d0d0);
    539582    Player := 7;
     583    Blocking := True;
    540584  end;
    541585  // Player 8 body2
     
    544588    Color := TColor($707070);
    545589    Player := 7;
     590    Blocking := True;
    546591  end;
    547592end;
     
    658703  I: Integer;
    659704  Pos: TPoint;
     705  ColisionState: TColisionState;
    660706begin
    661707  if Exploded then Exit;
     
    679725    else if (Delta.X = -1) and (Delta.Y = -1) then NewDirection := 7;
    680726
    681     if NewDirection = Direction then
    682       NewPosition := Point(Position.X + Delta.X, Position.Y + Delta.Y)
    683       else NewPosition := Position;
    684     HideTank;
    685     Matter := CheckColision;
    686     if (Matter = miDirt1) and (
    687     (Engine.KeyBoard.KeyState[Ord(Keys.Shoot)] and
    688     ((Now - LastDigTime) > ShootDigDelay * OneSecond)) or
    689     (not Engine.KeyBoard.KeyState[Ord(Keys.Shoot)] and
    690     ((Now - LastDigTime) > DigDelay * OneSecond))) then begin
    691       Dig := not Dig;
    692       with Engine, World do
    693       Surface.Merge(Surface.CreateIndex(
    694         Position.X - TMatrixByte(DigMasks[Direction]).Count.X div 2,
    695         Position.Y - TMatrixByte(DigMasks[Direction]).Count.Y div 2),
    696         TMatrixByte(DigMasks[Direction]), DigProc);
    697       Energy := Energy - EnergyDecreaseDig;
    698       if Energy < 0 then Energy := 0;
    699       Engine.Redraw;
    700       LastDigTime := Now;
    701       Direction := NewDirection;
    702     end;
    703     if (Matter = miSpace) then begin
    704       Position := NewPosition;
    705       Direction := NewDirection;
    706       Engine.Redraw;
    707     end;
    708     ShowTank;
     727    if NewDirection = Direction then begin
     728      NewPosition := Point(Position.X + Delta.X, Position.Y + Delta.Y);
     729      CheckNewDestination;
     730    end else begin
     731        NewPosition := Position;
     732        if not CheckNewDestination then begin
     733          // if direction is changed then try to check movement plus one step
     734          // to prevent tank block
     735          NewPosition := Point(Position.X + Delta.X, Position.Y + Delta.Y);
     736          CheckNewDestination;
     737        end;
     738      end;
    709739  end;
    710740
     
    722752      LastShootTime := Now;
    723753
    724       Energy := Energy - ShootEnergyDecrease;
     754      Energy := Energy - EnergyDecreaseShoot;
    725755      if Energy < 0 then Energy := 0;
    726     end;
     756
     757      // Do not imedietelly clear dirt by bullet in front of tank
     758      //with Engine, World do
     759      //if TMatter(Matter[Surface.Items[Position]]).Diggable ;
     760    end;
     761end;
     762
     763function TPlayer.CheckNewDestination: Boolean;
     764var
     765  ColisionState: TColisionState;
     766begin
     767  Result := False;
     768  HideTank;
     769  ColisionState := CheckColision;
     770  if not ColisionState.Blocking then begin
     771    if ColisionState.Diggable then begin
     772      if (Engine.KeyBoard.KeyState[Ord(Keys.Shoot)] and
     773    ((Now - LastDigTime) > ShootDigDelay * OneSecond)) or
     774    (not Engine.KeyBoard.KeyState[Ord(Keys.Shoot)] and
     775    ((Now - LastDigTime) > DigDelay * OneSecond)) then begin
     776      Dig := not Dig;
     777      with Engine, World do
     778      Surface.Merge(Surface.CreateIndex(
     779        Position.X - TMatrixByte(DigMasks[Direction]).Count.X div 2,
     780        Position.Y - TMatrixByte(DigMasks[Direction]).Count.Y div 2),
     781        TMatrixByte(DigMasks[Direction]), DigProc);
     782      Energy := Energy - EnergyDecreaseDig;
     783      if Energy < 0 then Energy := 0;
     784      Engine.Redraw;
     785      LastDigTime := Now;
     786      Direction := NewDirection;
     787      Result := True;
     788      end;
     789    end else begin
     790      if ((Now - LastMoveTime) > MoveDelay * OneSecond) then begin
     791        Position := NewPosition;
     792        Direction := NewDirection;
     793        Result := True;
     794        Engine.Redraw;
     795        LastMoveTime := Now;
     796      end;
     797    end;
     798  end;
     799  ShowTank;
    727800end;
    728801
     
    776849      ItemsXY[LastPos.X, LastPos.Y] := Byte(miSpace);
    777850    LastPos := Pos;
     851    P := Trunc(Direction.Y);
    778852
    779853    Position.X := Position.X + Direction.X;
     
    794868      if (ItemsXY[Pos.X, Pos.Y] = Byte(miDirt1)) or
    795869        (ItemsXY[Pos.X, Pos.Y] = Byte(miDirt2)) then begin
    796           ItemsXY[Pos.X, Pos.Y] := Byte(miSpace);
     870          //ItemsXY[Pos.X, Pos.Y] := Byte(miSpace);
    797871          if StopByDirt then begin
    798872            Explosion(LastPos, BulletExplosionRange);
     
    804878          for P := 0 to Engine.Players.Count - 1 do
    805879          with TPlayer(Engine.Players[P]) do
    806           if (Id <> P) and
     880          if (Self.Id <> P) and
    807881            (TMatter(Engine.World.Matter[ItemsXY[Pos.X, Pos.Y]]).Kind = mkTankBody) and
    808882            (TMatter(Engine.World.Matter[ItemsXY[Pos.X, Pos.Y]]).Player = P) then
     
    815889    end;
    816890
     891    // Max position limit checking
    817892    with Engine.World.Surface do
    818893    if (Pos.X >= Count.X) or (Pos.X < 0) or
     
    823898      end;
    824899    ItemsXY[Pos.X, Pos.Y] := Byte(miBullet1);
    825     //Engine.World.Surface.ItemsXY[LastPos.X, LastPos.Y] := Byte(smBullet2);
     900    ItemsXY[LastPos.X, LastPos.Y] := Byte(miBullet2);
    826901    Engine.Redraw;
    827902  end;
     
    885960end;
    886961
    887 function TPlayer.CheckColision: TMatterIndex;
     962function TPlayer.CheckColision: TColisionState;
    888963var
    889964  X, Y: Integer;
    890965  XX, YY: Integer;
    891966begin
    892   Result := miSpace;
    893   with Engine.World, TTank(Tanks[NewDirection]) do
     967  Result.Diggable := False;
     968  Result.Blocking := False;
     969  with Engine, World, TTank(Tanks[NewDirection]) do
    894970  for Y := 0 to Image.Count.Y - 1 do
    895971  for X := 0 to Image.Count.X - 1 do begin
    896972    XX := X + NewPosition.X - Image.Count.X div 2;
    897973    YY := Y + NewPosition.Y - Image.Count.Y div 2;
    898     if (Image.ItemsXY[X, Y] > 0) and
    899     (Surface.ItemsXY[XX, YY] <> Byte(miSpace)) then
    900     begin
    901       Result := miDirt1;
    902       if (Surface.ItemsXY[XX, YY] <> Byte(miDirt1)) and
    903       (Surface.ItemsXY[XX, YY] <> Byte(miDirt2)) then
    904       begin
    905         Result := TMatterIndex(Surface.ItemsXY[XX, YY]);
    906         Exit;
    907       end;
     974    if Image.ItemsXY[X, Y] > 0 then begin
     975      if TMatter(Matter[Surface.ItemsXY[XX, YY]]).Blocking then
     976        Result.Blocking := True;
     977      if TMatter(Matter[Surface.ItemsXY[XX, YY]]).Diggable then
     978        Result.Diggable := True;
    908979    end;
    909980  end;
     
    9661037begin
    9671038  if not Exploded then begin
    968     for I := 0 to Distance * 2- 1 do begin
     1039    for I := 0 to Distance * 2 - 1 do begin
    9691040      NewBullet := TBullet.Create;
    9701041      NewBullet.Player := Self;
Note: See TracChangeset for help on using the changeset viewer.