home *** CD-ROM | disk | FTP | other *** search
/ WarCraft 2000 - Nuclear Epidemic / W2000.nrg / SOURCE.War2000 / path.cpp < prev    next >
Text File  |  1998-09-15  |  9KB  |  455 lines

  1. int GetRAngle(int dx,int dy,int Angle){
  2.     int ang;
  3.     int sx=abs(dx);
  4.     int sy=abs(dy);
  5.     if(!(dx||dy))return 0;
  6.     if(sx>sy){
  7.         ang=div(dy<<8,sx).quot;
  8.         if(dx<0)ang=1024-ang;
  9.     }else{
  10.         ang=512+div(dx<<8,sy).quot;
  11.         if(dy<0)ang=1792-ang;
  12.     };
  13.     ang=Angle-ang;
  14.     while(ang<0)ang+=2048;
  15.     return ang;
  16. };
  17. int GetLAngle(int dx,int dy,int Angle){
  18.     int ang;
  19.     int sx=abs(dx);
  20.     int sy=abs(dy);
  21.     if(!(dx||dy))return 0;
  22.     if(sx>sy){
  23.         ang=div(dy<<8,sx).quot;
  24.         if(dx<0)ang=1024-ang;
  25.     }else{
  26.         ang=512+div(dx<<8,sy).quot;
  27.         if(dy<0)ang=1792-ang;
  28.     };
  29.     ang-=Angle;
  30.     while(ang<0)ang+=2048;
  31.     return ang;
  32. };
  33. void OneObject::CreatePrePath(int x1,int y1){
  34.     PathAsks++;
  35.     TrMap[y][x]=0;
  36.     int sdx=x1-x;
  37.     int    sdy=y1-y;
  38.     int    Cum=0;
  39.     int Pps=0;
  40.     int sx=(sdx>0)?1:-1;
  41.     int sy=(sdy>0)?1:-1;
  42.     int dx=abs(sdx);
  43.     int dy=abs(sdy);
  44.     int    Mdx=dx;
  45.     int    Mdy=dy;
  46.     int    Mx=x;
  47.     int    My=y;
  48.     int    xx1=x;
  49.     int yy1=y;
  50.     int rx=sx;
  51.     int ry=sy;
  52.     if(dx>dy)ry=0;
  53.     if(dy>dx)rx=0;
  54.     int Angle0=GetLAngle(x1-x,y1-y,0);
  55.     int Angle;
  56.     int ddx,ddy;
  57.     int Lvp=0;
  58.     int Rvp=0;
  59.     bool LvpLast=false;
  60.     bool RvpLast=false;
  61.     if(PathX)free(PathX);
  62.     if(PathY)free(PathY);
  63.     PathX=NULL;
  64.     PathY=NULL;
  65.     NIPoints=0;
  66.     CurIPoint=0;
  67.     //±εσΣΦφ σ∞ δΦφΦσΘ φα≈αδⁿφ≤■ Φ Ωεφσ≈φ≤■ ≥ε≈ΩΦ. 
  68.     //╬∩≥Φ∞Φτα÷Φ  ≥εδⁿΩε ∩ε ±Ωε≡ε±≥Φ
  69.     __asm{
  70.         mov        ax,word ptr Mdx
  71.         mov        bx,word ptr Mdy
  72.         xor        edx,edx  //Pps
  73.         xor        ecx,ecx  //Cum
  74.         mov        si,word ptr Mx
  75.         mov        di,word ptr My
  76.         cmp        bx,ax
  77.         jae        Lp5xx
  78.         //dx>dy
  79.         mov        word ptr[xx+edx],si
  80.         mov        word ptr[yy+edx],di
  81.         inc        edx
  82.         or        ax,ax
  83.         jz        LoopsEnd
  84.         cmp        sy,0
  85.         jl        Lp3xx
  86.         cmp        sx,0
  87.         jl        Lp2begin
  88.         //dx>dy,sx>0,sy>0
  89. Lp1begin:
  90.         inc        si    //x++
  91.         add        cx,bx
  92.         cmp        cx,word ptr Mdx
  93.         jb        Lp1_1
  94.         sub        cx,word ptr Mdx
  95.         inc        di  //y++
  96. Lp1_1:
  97.         mov        word ptr[xx+edx],si
  98.         mov        word ptr[yy+edx],di
  99.         inc        edx
  100.         dec        ax
  101.         jnz        Lp1begin
  102.         jmp        LoopsEnd
  103. Lp2begin: //dx>dy,sx<0,sy>0
  104.         dec        si    //x--
  105.         add        cx,bx
  106.         cmp        cx,word ptr Mdx
  107.         jb        Lp2_1
  108.         sub        cx,word ptr Mdx
  109.         inc        di //y++
  110. Lp2_1:
  111.         mov        word ptr[xx+edx],si
  112.         mov        word ptr[yy+edx],di
  113.         inc        edx
  114.         dec        ax
  115.         jnz        Lp2begin
  116.         jmp        LoopsEnd
  117. Lp3xx:    //dy<0
  118.         cmp        sx,0
  119.         jl        Lp4begin
  120. Lp3begin: //dx>dy,sx>0,sy<0
  121.         inc        si    //x++
  122.         add        cx,bx
  123.         cmp        cx,word ptr Mdx
  124.         jb        Lp3_1
  125.         sub        cx,word ptr Mdx
  126.         dec        di //y--
  127. Lp3_1:
  128.         mov        word ptr[xx+edx],si
  129.         mov        word ptr[yy+edx],di
  130.         inc        edx
  131.         dec        ax
  132.         jnz        Lp3begin
  133.         jmp        LoopsEnd
  134. Lp4begin: //dx>dy,sx<0,sy<0
  135.         dec        si    //x--
  136.         add        cx,bx
  137.         cmp        cx,word ptr Mdx
  138.         jb        Lp4_1
  139.         sub        cx,word ptr Mdx
  140.         dec        di //y--
  141. Lp4_1:
  142.         mov        word ptr[xx+edx],si
  143.         mov        word ptr[yy+edx],di
  144.         inc        edx
  145.         dec        ax
  146.         jnz        Lp4begin
  147.         jmp        LoopsEnd
  148. Lp5xx:    //dx<dy
  149.         mov        word ptr[xx+edx],si
  150.         mov        word ptr[yy+edx],di
  151.         inc        edx
  152.         or        bx,bx
  153.         jz        LoopsEnd
  154.         cmp        sx,0
  155.         jl        Lp7xx
  156.         cmp        sy,0
  157.         jl        Lp6begin
  158. Lp5Begin:
  159.         inc        di    //y++
  160.         add        cx,ax
  161.         cmp        cx,word ptr dy
  162.         jb        Lp5_1
  163.         sub        cx,word ptr dy
  164.         inc        si    //x++
  165. Lp5_1:
  166.         mov        word ptr[xx+edx],si
  167.         mov        word ptr[yy+edx],di
  168.         inc        edx
  169.         dec        bx
  170.         jnz        Lp5begin
  171.         jmp        LoopsEnd
  172. Lp6Begin://sx>0,sy<0
  173.         dec        di    //y++
  174.         add        cx,ax
  175.         cmp        cx,word ptr dy
  176.         jb        Lp6_1
  177.         sub        cx,word ptr dy
  178.         inc        si    //x++
  179. Lp6_1:
  180.         mov        word ptr[xx+edx],si
  181.         mov        word ptr[yy+edx],di
  182.         inc        edx
  183.         dec        bx
  184.         jnz        Lp6begin
  185.         jmp        LoopsEnd
  186. Lp7xx:    //dx<0
  187.         cmp        sy,0
  188.         jl        Lp8begin
  189. Lp7Begin://dx<0,dy>0
  190.         inc        di    //y++
  191.         add        cx,ax
  192.         cmp        cx,word ptr dy
  193.         jb        Lp7_1
  194.         sub        cx,word ptr dy
  195.         dec        si    //x--
  196. Lp7_1:
  197.         mov        word ptr[xx+edx],si
  198.         mov        word ptr[yy+edx],di
  199.         inc        edx
  200.         dec        bx
  201.         jnz        Lp7begin
  202.         jmp        LoopsEnd
  203. Lp8Begin://dx<0,dy<0
  204.         dec        di    //y--
  205.         add        cx,ax
  206.         cmp        cx,word ptr dy
  207.         jb        Lp8_1
  208.         sub        cx,word ptr dy
  209.         dec        si    //x--
  210. Lp8_1:
  211.         mov        word ptr[xx+edx],si
  212.         mov        word ptr[yy+edx],di
  213.         inc        edx
  214.         dec        bx
  215.         jnz        Lp8begin
  216. loopsEnd:
  217.         //shr        edx,1
  218.         mov        Pps,edx
  219.     };
  220.         Pps--;
  221. /*this is a place for new code*/
  222.     //╬ß⌡εΣΦ∞ ±∩≡αΓα
  223.     bool RightPrefer=true;
  224.     int Rtx;//current point 
  225.     int Rty;
  226.     int Ltx;
  227.     int Lty;
  228.     byte Rpx[MaxP];//right path
  229.     byte Rpy[MaxP];
  230.     byte Lpx[MaxP];//left path
  231.     byte Lpy[MaxP];
  232.     int Rpp=1;//index of current point
  233.     bool LDoing;//=true if last point reached
  234.     bool RDoing;
  235.     byte Rdirc;//currend direction
  236.     byte Ldirc;
  237.     int Rmaxalt;//maximum alteration,right path
  238.     int Lmaxalt;//maximum alteration,left path
  239.     int Rppm=0;
  240.     int Lppm=0;
  241.     int Rcum=0;
  242.     int Rcum1=0;
  243.     int Lcum=0;
  244.     int Lcum1=0;
  245.     byte Trr=TrMap[y][x];
  246.     TrMap[y][x]=0;
  247.     //╚Σσ∞, ∩εΩα φσ ≤∩≡σ∞±  Γ ±≥σφΩ≤
  248.     __asm{
  249.         xor        ebx,ebx
  250.         mov        ecx,Pps
  251.         xor        edx,edx //Rpp
  252.         mov        al,[xx+edx]
  253.         mov        [Rpx+edx],al
  254.         mov        [Lpx+edx],al
  255.         mov        al,[yy+edx]
  256.         mov        [Rpy+edx],al
  257.         mov        [Lpy+edx],al
  258.         inc        edx
  259. uuu_Loop:
  260.         mov        al,[xx+edx]
  261.         mov        [Rpx+edx],al
  262.         mov        [Lpx+edx],al
  263.         mov        bl,al
  264.         mov        al,[yy+edx]
  265.         mov        [Rpy+edx],al
  266.         mov        [Lpy+edx],al
  267.         mov        bh,al
  268. //        add        bx,0101h
  269.         cmp        byte ptr[TrMap+ebx],0
  270.         jnz        uuu_end
  271.         inc        edx
  272.         loop    uuu_Loop        
  273. uuu_end:
  274.         //shr        edx,1
  275.         mov        Rpp,edx
  276.     };
  277.     Rtx=xx[Rpp-1];
  278.     Rty=yy[Rpp-1];
  279.     
  280.     // ┼±δΦ dx>dy,≥ε φα ΩαµΣε∞ °απ≤ dx Φτ∞σφ σ≥  ±≥≡επε φα 1
  281.     if(Rtx!=x1||Rty!=y1){
  282.         //LLock[y][x]=false;
  283.         Rpp-=1;
  284.         Rtx=xx[Rpp];
  285.         Rty=yy[Rpp];
  286.         Ltx=xx[Rpp];
  287.         Lty=yy[Rpp];
  288.         int Ppi=Rpp+1;
  289.         LDoing=true;
  290.         RDoing=true;
  291.         //╚∙σ∞, ∩εΩα φα⌡εΣΦ∞±  Γ ταφ ≥εΘ τεφσ
  292.         while(TrMap[yy[Ppi]][xx[Ppi]]&&Ppi<Pps)Ppi++;
  293.         if(Ppi>Pps)LDoing=false;//╩εφ.≥ε≈Ωα φσΣε±≥ΦµΦ∞α
  294.         int Xls=xx[Ppi-1];
  295.         int Yls=yy[Ppi-1];
  296.         //╙∩σ≡δΦ±ⁿ...┬√≈Φ±δ σ∞ φα∩≡αΓδσφΦσ ΣΓΦµσφΦ 
  297.         Rdirc=drr[(xx[Rpp+1]-xx[Rpp]+1)*3+yy[Rpp+1]-yy[Rpp]+1];
  298.         Ldirc=Rdirc;
  299.         //┬√ßΦ≡ασ∞ φα≈αδⁿφεσ φα∩≡αΓδσφΦσ-right
  300.         FillCirc(Rtx,Rty);
  301.         int dirc1=(Rdirc+1)&7;
  302.         for(int z=0;circ[dirc1]&&z<7;dirc1++,z++);
  303.         Rdirc=dirc1&7;
  304.         //-left
  305.         dirc1=8+((Ldirc+7)&7);
  306.         for(z=0;circ[dirc1]&&z<7;dirc1--,z++);
  307.         Ldirc=dirc1&7;
  308.         //╚Σσ∞ ∩ε ∩≡αΓε∞≤ Ω≡α■ Σε ≥σ⌡ ∩ε≡ ∩εΩα ΓφεΓⁿ φσ ∩σ≡σ±σ-
  309.         //≈σ∞±  ± ∩≡ ∞εΘ δΦφΦσΘ, ±εσΣΦφ ■∙σΘ φα≈αδⁿφ≤■ Φ Ωεφσ≈φ≤■
  310.         //≥ε≈ΩΦ
  311.         Rmaxalt=0;
  312.         Lmaxalt=0;
  313.         while(Rpp<MaxP-8&&LDoing&&RDoing){
  314.             //∩√≥ασ∞±  ∩εΓσ≡φ≤≥ⁿ φα∩≡αΓε
  315.             FillCirc(Rtx,Rty);
  316.             int dirc1=(Rdirc+7)&7;
  317.             for(int z=0;z<6&&circ[dirc1];dirc1++,z++);
  318.             Rdirc=dirc1&7;
  319.             Rpp++;
  320.             int Tdx=idrx[Rdirc];
  321.             int Tdy=idry[Rdirc];
  322.             Rcum-=sdy*Tdx;
  323.             Rcum+=sdx*Tdy;
  324.             Rtx+=Tdx;
  325.             Rty+=Tdy;
  326.             Rpx[Rpp]=Rtx;Rpy[Rpp]=Rty;
  327.             Angle=GetLAngle(Rtx-x,Rty-y,Angle0);
  328.             if(Angle>Rmaxalt){
  329.                 Rmaxalt=Angle;
  330.                 Rppm=Rpp;
  331.             };
  332.             //∩≡εΓσ≡ σ∞ ≤±δεΓΦσ ∩≡ ∞εΘ ΓΦΣΦ∞ε±≥Φ
  333.             ddx=Rtx-x;
  334.             ddy=Rty-y;
  335.             if(ddx>1)ddx=1;
  336.             if(ddx<-1)ddx=-1;
  337.             if(ddy>1)ddy=1;
  338.             if(ddy<-1)ddy=-1;
  339.             if(!TrMap[Rty+ddy][Rtx+ddx]){
  340.                 if(!RvpLast){
  341.                     Rvp=Rpp;
  342.                     RvpLast=true;
  343.                 };
  344.             }else RvpLast=false;
  345.             //the same, but left
  346.             FillCirc(Ltx,Lty);
  347.             dirc1=8+((Ldirc+1)&7);
  348.             for(z=0;z<6&&circ[dirc1];dirc1--,z++);
  349.             Ldirc=dirc1&7;
  350.             Tdx=idrx[Ldirc];
  351.             Tdy=idry[Ldirc];
  352.             Lcum+=sdy*Tdx;
  353.             Lcum-=sdx*Tdy;
  354.             Ltx+=Tdx;
  355.             Lty+=Tdy;
  356.             Lpx[Rpp]=Ltx;Lpy[Rpp]=Lty;
  357.             Angle=GetRAngle(Rtx-x,Rty-y,Angle0);
  358.             if(Angle>Lmaxalt){
  359.                 Lmaxalt=Angle;
  360.                 Lppm=Rpp;
  361.             };
  362.             //∩≡εΓσ≡ σ∞ ≤±δεΓΦσ ∩≡ ∞εΘ ΓΦΣΦ∞ε±≥Φ
  363.             ddx=Ltx-x;
  364.             ddy=Lty-y;
  365.             if(ddx>1)ddx=1;
  366.             if(ddx<-1)ddx=-1;
  367.             if(ddy>1)ddy=1;
  368.             if(ddy<-1)ddy=-1;
  369.             if(!TrMap[Lty+ddy][Ltx+ddx]){
  370.                 if(!LvpLast){
  371.                     Lvp=Rpp;
  372.                     LvpLast=true;
  373.                 };
  374.             }else RvpLast=false;
  375.             //┬√≈Φ±δ σ∞ τφα≈σφΦσ y φα ∩≡ ∞εΘ δΦφΦΦ, ±εε≥Γ. 
  376.             //Σαφφε∞≤ x
  377.             if(Rcum<=0&&Rcum1>=0){
  378.                 if(dx>dy){
  379.                     if(sx>0){
  380.                         if(Rtx>=Xls)RDoing=false;
  381.                     }else if(Rtx<=Xls)RDoing=false;
  382.                 }else{
  383.                     if(sy>0){
  384.                         if(Rty>=Yls)RDoing=false;
  385.                     }else if(Rty<=Yls)RDoing=false;
  386.                 };
  387.                 if(!RDoing)RightPrefer=true;
  388.             };
  389.             Rcum1=Rcum;
  390.             //the same for left
  391.             if(Lcum<=0&&Lcum1>=0){
  392.                 if(dx>dy){
  393.                     if(sx>0){
  394.                         if(Ltx>=Xls)LDoing=false;
  395.                     }else if(Ltx<=Xls)LDoing=false;
  396.                 }else{
  397.                     if(sy>0){
  398.                         if(Lty>=Yls)LDoing=false;
  399.                     }else if(Lty<=Yls)LDoing=false;
  400.                 };
  401.                 if(!LDoing)RightPrefer=false;
  402.             };
  403.             Lcum1=Lcum;
  404.         };
  405.         //LLock[y][x]=true;
  406.         if(Rpp<MaxP-9){
  407.             if(RightPrefer){
  408.                 if(Rppm+1<Rpp)Rppm+=1;else Rppm=Rpp;
  409.                 //memcpy(xx,Rpx,(Rppm+1));
  410.                 //memcpy(yy,Rpy,(Rppm+1));
  411.                 Pps=Rppm;
  412.             }else{
  413.                 if(Lppm+1<Rpp)Lppm+=1;else Lppm=Rpp;
  414.                 //memcpy(xx,Lpx,(Lppm+1));
  415.                 //memcpy(yy,Lpy,(Lppm+1));
  416.                 Pps=Lppm;
  417.                 Rvp=Lvp;
  418.                 if(Rvp<Pps)Rvp=Pps;
  419.                 
  420.             };
  421.             PathX=new byte [Rvp-Pps+1];
  422.             PathY=new byte [Rvp-Pps+1];
  423.             memcpy(PathX,&Rpx[Pps],Rvp-Pps+1);
  424.             memcpy(PathX,&Rpx[Pps],Rvp-Pps+1);
  425.             NIPoints=Rvp-Pps+1;
  426.             CurIPoint=0;
  427.         }else return;
  428.         //╬∩≥Φ∞Φτα÷Φ  ∩≤≥Φ
  429.         //Pps=Optima1(Pps);
  430.     };
  431.     TrMap[y][x]=Trr;
  432.     NowBuf=GetAsmBlock();
  433.     if(!int(NowBuf))return;
  434.     Ofst=0;
  435.     memcpy(&NowBuf[OneAsmSize-4],&Ofst,4);
  436.     InLineCom=NowBuf;
  437.     LineOffset=0;
  438.     cmLoadAnm(AnmGoKind,1,0);
  439.     //cmLoadAnm(1,1,0);
  440.     cmLoadAnm(AnmStandKind,0,0);
  441.     for(int i=1;i<=Pps;i++){
  442.         //byte (*XXX)[64];
  443.         //memcpy(&XXX,&NowBuf,4);
  444.         //cmSetDir(xx[i]-xx[i-1],yy[i]-yy[i-1]);
  445.         if(i==1)cmPerfAnm(0);//-------??????????????????????
  446.         cmSetXY(xx[i]-xx[i-1],yy[i]-yy[i-1]);
  447.         //cmPerfAnm(1);
  448.         //cmSetXYDirX(xx[i],yy[i],xx[i]-xx[i-1],yy[i]-yy[i-1],1);
  449.         //cmPerfAnm(1);
  450.     };
  451.     cmLoadAnm(AnmStandKind,0,0);
  452.     cmPerfAnm(0);
  453.     //cmDone();
  454.     cmRet();
  455. };