⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uclass.pas

📁 机器人足球赛比赛平台源代码
💻 PAS
📖 第 1 页 / 共 3 页
字号:

        dists[i, j] := distancebetween(Robots[i, j].absolutex,
                                       Robots[i, j].absolutey,
                                       Ball.x,
                                       Ball.y);
                                       
        if dists[i, j] > CRADIUS then
          dists[i, j] := -0.0001
        else
        begin
          bThereIsPlayerCanEffectBall := true;
          if mindist > dists[i,j] then
          begin
            backteamid := i;
            backplayerid := j;
            mindist := dists[i,j];
          end;
          fansumdist := fansumdist + (CRADIUS - DISTS[i,j]);
          dists[i,j] := CRADIUS - dists[i,j];
        end;
      end;
      
  if (bThereIsPlayerCanEffectBall) then
  begin        // 有人能够碰到球
    inc(iHMPETB);
    if Ball.speedmo > CVCANBALL then
    begin      // 不能够控球
       irandseed := random;
       if irandseed < (ball.speedmo - CVCANBALL)/(CVCANNOTBALL - CVCANBALL) then
       begin ///////////////橦到了球。
            crt := backteamid;
            crn := backPlayerid;
            ballcrashplayer(robots[backteamid,backPlayerId].absolutex,
                            robots[backteamid,backPlayerID].absolutey,
                            ball.posx,
                            ball.posy,
                            ball.vx,
                            ball.vy,
                            newvx,
                            newvy);
            ball.vx := newvx;
            ball.vy := newvy;

            while distancebetween(robots[backteamid,backPlayerID].absolutex,
                                  robots[backteamid,backPlayerid].absolutey,
                                  Ball.x, Ball.y) < CRADIUS  do
            begin
//              if  robots[backteamid,backPlayerid].absolutey>2500 then
//               showmessage('1');

              ball.x := ball.posx + round(ball.vx);
              ball.y := ball.posy + round(ball.vy);

            end;
       end else
       begin  //控制,并处理球
         crt := backteamid;
         crn := backplayerid;
         robots[backteamid, backPlayerid].doAction;
         while distancebetween(robots[backteamid,backPlayerID].absolutex,
                               robots[backteamid,backPlayerid].absolutey,
                               Ball.x, Ball.y) < CRADIUS  do
         begin
//            if  robots[backteamid,backPlayerid].absolutey>2500 then
//               showmessage('2');
           ball.x := ball.posx + round(ball.vx);
           ball.y := ball.posy + round(ball.vy);
         end;
       end;
    end
    else
    begin      //  能够控球
//       if  Ball.y>2500 then
//               showmessage('3');
      if ifinPZ(1 , ball.x, Ball.y)
        and (distancebetween(Ball.x, Ball.y, Robots[1,1].absolutex,
                             Robots[1,1].absolutey) < CRADIUS)then
        begin  //  1队守门员控球

            crt := 1;
            crn := 1;
            robots[1,1].doAction;
            temp:=sqr(robots[1,1].absolutex - ball.x)
                 + sqr(robots[1,1].absolutey - ball.y );
            while (sqrt(temp) < CRADIUS ) do
            begin
              ball.x := ball.posx + round(ball.vx);
              ball.y := ball.posy + round(ball.vy);
              temp:=sqr(robots[1,1].absolutex - ball.x)
                    + sqr(robots[1,1].absolutey - ball.y );
            end;
        end
      else if ifinPZ(2, ball.x, Ball.y)
        and (distancebetween(Ball.x, Ball.y, Robots[2,1].absolutex,
             Robots[2,1].absolutey) < CRADIUS) then
        begin  //  2队守门员控球
           crt := 2;
           crn := 1;
           robots[2,1].doAction;
           temp:=sqr(robots[2,1].absolutex - ball.x)
               + sqr(robots[2,1].absolutey - ball.y );
           while (sqrt(temp) < CRADIUS ) do
           begin
             ball.x := ball.posx + round(ball.vx);
             ball.y := ball.posy + round(ball.vy);
             temp:=sqr(robots[2,1].absolutex - ball.x)
                 + sqr(robots[2,1].absolutey - ball.y );
           end;
        end
      else     //  球员争球
        begin
          irandseed := random * fansumdist;
          curfansum := 0;
          for i:=1 to 2 do
            for j:=1 to CROBOTNUM do
              begin
                if dists[i,j] >= 0 then
                begin
                  curfansum := curfansum + dists[i,j];
                  if curfansum >= irandseed then
                  begin
                    crt := i;
                    crn := j;
                    robots[i,j].doAction;
                    goto g1;
                  end;
                end;
              end;
          showmessage('error');
          g1:
          while distancebetween(robots[backteamid,backPlayerID].absolutex,
                                robots[backteamid,backPlayerid].absolutey,
                                Ball.x, Ball.y) < CRADIUS  do
          begin
//            if robots[backteamid,backPlayerid].absolutey>2500 then
//              showmessage('6');
            ball.x := ball.posx + round(ball.vx);
            ball.y := ball.posy + round(ball.vy);
          end;
        end;
    end;
  end else
  begin  // 没有人能碰到球
  end;
end;

{ TRobot }

constructor TRobot.Create(aowner: TObject);
begin
  inherited create;
  {things to do}
  owner := aowner;
  acc := 0;
  vx := 0;
  vy := 0;
  preaction := 0;
  param0 := 0;
  param1 := 0;
  param2 := 0;
end;


procedure TRobot.doAction;
var
  newvx, newvy : double;
begin
  if param0 < CMINBALLV then
    param0 := CMINBALLV;
  if preaction = 1 then
    begin
      speedmotospeedxy(absolutex, absolutey, param1, param2, param0, newvx, newvy);
    end
  else if preaction = 2 then
    begin
      speedmotospeedxy2(absolutex, absolutey, param1, param2, param0, newvx, newvy);
    end
  else begin
         newvx := (owner as TMatchField).ball.vx;
         newvy := (owner as TMatchField).ball.vy;
       end;
  giveballerror(newvx, newvy, (owner as TMatchField).ball.vx, (owner as TMatchField).ball.vy);    
  vx := 0;
  vy := 0;
end;

procedure TRobot.doMove;
var //vactually : double;
    //vold      : double;
    //atv       : double; //目标速度的莫
    //dist      : double; //目标位置和当前位置的距离
    ax        : double; //加速度x分量
    ay        : double; //加速度y分量
    mVdif     : double; //速度的差的莫
begin
    ///////////////////////////////////////////////////////////////////////////
    ///                 加速                                                ///
    ///////////////////////////////////////////////////////////////////////////
   mVdif:=sqr(tvx - vx) + sqr(tvy - vy);
   mVdif := sqrt(mVdif);

   if mVdif < CMINNEARZERO then
   begin
     vx := tvx;
     vy := tvy;
   end else if mVdif <= CMAXACC then
             begin
               vx := tvx;
               vy := tvy;
             end else
             begin
               ax := CMAXACC * (tvx - vx) / mVdif;
               ay := CMAXACC * (tvy - vy) / mVdif;
               vx := vx + ax;
               vy := vy + ay;
             end;

    //////////////////////////////////////////////////////////////////////////////
    ///                    移动                                                 //
    //////////////////////////////////////////////////////////////////////////////
    x := x + round(vx);
    y := y + round(vy);
    //////////////////////////////////////////////////////////////////////////////
    ///                    边界判断                                             //
    //////////////////////////////////////////////////////////////////////////////
    if x < 0 then x := 0
    else if x > CFLDLEN then x := CFLDLEN;

    if y < 0 then y := 0
    else if y > CFLDWIDTH then y := CFLDWIDTH;

end;

function TRobot.getabsolutex: integer;
begin
  result := 0;
  if team = 1 then
    result := x;
  if team = 2 then
    result := CFLDLEN - x;
end;

function TRobot.getabsolutey: integer;
begin
  result := 0;
  if team = 1 then
    result := y;
  if team = 2 then
    result := CFLDWIDTH - y;
end;

procedure TRobot.move(TargetX, TargetY, TargetV: integer);
var
//  vtx, vty : double; //目标速度的x,y分量
  dist     : double; //当前位置和目标位置的距离
begin
/////////////////////////////
///   2002-1-21日之前为以下三行
//    tv := TargetV;
//    vx := 0.001 * (TargetX - x);
//    vy := 0.001 * (TargetY - y);
////////////////////////////////////
////////////////////////////////
///   2002-1-21日修改
////////////////////////////////
   if (TargetX > CFLDLEN + 1000)
    or(TargetX < -1000) then
     exit;
   if (TargetY > CFLDWIDTH + 1000)
    or(TargetY < -1000) then
     exit;
   if (TargetV > CMAXV + 100)
    or(TargetV < -1000) then
     exit;
   dist:= sqr(TargetX - x)+sqr(TargetY - y);
   dist := sqrt(dist);

   if dist < CMINNEARZERO then
   begin
     tvx := 0;
     tvy := 0;
     self.targetX := TargetX;
     self.TargetY := TargetY;
   end else
   begin
     tvx := TargetV * (TargetX - x) / dist;
     tvy := TargetV * (TargetY - y) / dist;
     self.targetX := TargetX;
     self.TargetY := TargetY;
   end;
{   dist := sqrt(sqr(TargetX - x)+sqr(TargetY - y));
   if dist < CMINNEARZERO then
      exit;

   tv := TargetV;
   vtx := TargetV * (TargetX - x) / dist;
   vtx := TargetV * (TargetY - y) / dist;

   if sqrt(sqr(vtx - vx) + sqr(vty - vy)) <= CMAXACC then
   begin
     vx := vtx;
     vy := vty;
   end else
   begin
     vx :=
     vy :=
   end;}
end;

procedure TRobot.SetPreAction(ipretype, prm0, prm1, prm2: integer);
begin
  preaction := ipretype;
  param0 := prm0;
  param1 := prm1;
  param2 := prm2;
end;

function TRobot.tv: double;
var
  temp:double;
begin
  temp:=sqr(tvx)+sqr(tvy);
  result := sqrt(temp);
end;

{ TBall }

constructor TBall.Create(owner: TObject);
begin
  inherited Create;
  self.owner := owner;
  x := CFLDLEN div 2;
  y := CFLDWIDTH div 2;
end;

procedure TBall.doMove(var bGoal :boolean;var goalteam :integer);
var acc, actv, oldv : double;
    oldx, oldy : integer;
    igoalteam  : integer;
begin
  bGoal := false;
  goalteam := 0;
  ///////////////////////////////////////////////////////
  ///                   移动                           //
  ///////////////////////////////////////////////////////
  oldx := x;
  oldy := y;
  x := x + round(vx);
  y := y + round(vy);

  ///////////////////////////////////////////////////////
  ///                   是否进球                       //
  ///////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////
  ///                   减速                           //
  ///////////////////////////////////////////////////////
  goalteam := 0;
  if (x < 0) or (x > CFLDLEN) then
  begin
    igoalteam := ifGoal(oldx, oldy, x, y);
    if igoalteam <> 0 then
      begin
        bGoal := true;
        goalteam := igoalteam;
        exit;
      end
    else
      begin
        if (x < 0) and (vx < 0) then
        begin
          vx := -vx;
          x := -x;
        end;
        if (x > CFLDLEN) and (vx >0) then
        begin
          vx := -vx;
          x := 2 * CFLDLEN - x;
        end;
      end;

  end;

  if (y < 0) or (y > CFLDWIDTH) then
    begin
       if (y < 0) and (vy < 0) then
       begin
         vy := -vy;
         y := -y;
       end;
       if (y > CFLDWIDTH) and (vy > 0) then
       begin
         vy := -vy;
         y := 2 * CFLDWIDTH - y;
       end;
    end;
  actv:=sqr(vx) + sqr(vy);
  actv := sqrt(actv);
  oldv := actv;
  acc := -CBALLACC*(actv - CMINBALLV);
  actv := actv + acc;

  if actv < CMINBALLV then
     actv := CMINBALLV;
  vx := vx * abs(actv / oldv);
  vy := vy * abs(actv / oldv);
end;

function TBall.speedmo: double;

var
  temp:double;
begin
  temp:=sqr(vx) + sqr(vy);
  result := sqrt(temp);
end;

function distancebetween(x0,y0,x1,y1:integer):double;
var
  tx0,ty0,tx1,ty1,temp:double;
begin
  tx0:=x0;
  ty0:=y0;
  tx1:=x1;
  ty1:=y1;
  temp:=sqr(tx0-tx1)+sqr(ty0-ty1);
  if temp<0 then
  begin
   showmessage(floattostr(tx0));
   showmessage(floattostr(tx1));
   showmessage(floattostr(ty0));
   showmessage(floattostr(ty1));
  end
  else
    result := sqrt(temp);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -