Unit überfährt Tile trotz Pathfinding

Übersicht BlitzBasic Allgemein

Neue Antwort erstellen

Baschdi

Betreff: Unit überfährt Tile trotz Pathfinding

BeitragSa, Aug 02, 2008 12:17
Antworten mit Zitat
Benutzer-Profile anzeigen
Wie man an der langen Überschrift bereits bemerkt, handelt es sich hier um ein seltsames Problem. Ich hab die Engine Hochgeladen, dann kann sie jeder anschaun...

Das Problem ist folgendes. Wenn man das Programm startet, sieht man drei Units -eine grüne und zwei blaue- Zwei davon haben rote, blinkende Kreise, welche später durch Waffen und so weiter erstzt werden.
Diese haben vorerst keine Bedeutung.
Selektieren sie die "frei" Unit und bewegen sie diese zu einem weiter entfernten blauen (einzelnen !!!) Tile.
Dabei vorsicht, denn wenn die einheit den Gegner in ihrem Sichtfeld hat, dann läuft sie wieder zurück.... (*stolz*)

Naja und wen man nun unter dem Wasser-Tile steht und direkt auf das tile darüber klickt, dann läuft die Unit einfach durch. Wenn man aber das selbe von oben nach unten macht, dann läuft sie am Wasser vorbei. (?!)
Da stellt sich mir die Frage.... WARUM?

(über das sich langsam aufbauende FPS Problem bin ich mir bewusst und kann dieses auch lösen...)


Downloadlink:
Code: [AUSKLAPPEN]


http://www.file-upload.net/download-1015744/Project-RTS1.rar.html



mfg
Dank im Voraus
Baschdi



falls ihr den code davor schon wollt Very Happy

Code: [AUSKLAPPEN]


;Konstante
Const width=800
Const height=width*0.75
Const tile=32
;Auflösung
Graphics width,height,0,2
SetBuffer BackBuffer()
;Allgemein
Global gfx$="gfx\",sfx$="sfx\"
Global xback,yback,Bhigh=150,fps
;Map
Global mapX=32,mapY=32
Dim map(mapX,mapY)
Dim status(mapX,mapY)
Global tileset=LoadAnimImage(gfx+"tileset.bmp",tile,tile,0,4)
;Maus
Global mx,my,mTx,mTy
Global maus=LoadImage(gfx+"maus.bmp")
Global Imaus=LoadImage(gfx+"Imaus.bmp")
;Unit
Type unit
Field x,y,xDraw,yDraw,xTile,yTile
Field xZ,yZ,xLauf,yLauf,xAtt,yAtt,go
Field live,att,speed,reich,sicht,dauer,buf
Field typ,team,sel,action,entf,ziel,entf2
Field FightStep,uPfad
Field winkel,nummer,opfer
End Type
Global q=1
Global unit=LoadAnimImage(gfx+"unit.bmp",tile,tile,0,4) : MidHandle unit
Dim ImaUnit(8)
For i=0 To 8: ImaUnit(i)=CopyImage(unit) : RotateImage ImaUnit(i),i*45 : Next
;Pathfinding
Global PfadBank=CreateBank(2),Bsize
Global mapwidth=mapX,mapheight=mapY,fightstep,schalter

Dim MapData$(999),Fighter(72)

Dim sqrmap(999,999),nodemap(0,0),dirx(7),diry(7),dirz(7)
For i=0 To 7:Read dirx(i):Read diry(i):Read dirz(i):Next
Type node
Field parent.node,cost,x,y
End Type

Type open
Field node.node
End Type

Type path
Field node.node
End Type
Data 0,-1,0,  -1,0,0,  1,0,0,  0,1,0
Data -1,-1,1,  1,-1,1,  -1,1,1,  1,1,1
;Hauptschleife
LoadMap("test")
;NewUnit(2,2,0,1)
;NewUnit(2,3,0,1)
;NewUnit(2,4,0,1)

NewUnit(3,2,0,1)
NewUnit(3,3,1,2)
NewUnit(3,4,0,1)

;NewUnit(4,2,0,1)
;NewUnit(4,3,0,1)
;NewUnit(4,4,0,1)
Repeat
Cls
   Scrolling()
   UpdateMap()

   UpdateUnit()

   UpdateScreen()
   UpdateMaus()

   ;FPS
   ms=MilliSecs():
    If ms>mt mt=ms+502:fps=frame:frame=0 Else frame=frame+2         
Flip
Until KeyHit(1)
End
;Function LoadMap(nam$)
Function LoadMap(nam$)
   file=OpenFile("Map\"+nam$+".mfg")   
      For tx=0 To mapX
      For ty=0 To mapY
         map(tx,ty)=ReadInt(file)
         If map(tx,ty)>0 Then status(tx,ty)=1
      Next
      Next       
   CloseFile file
End Function
;Function UpdateMap()
Function UpdateMap()
   For tx=0 To mapX
   For ty=0 To mapY   
      DrawImage tileset,tx*tile-xback,ty*tile-yback,map(tx,ty)
      ;Text tx*tile-xback,ty*tile-yback,status(tx,ty)
   Next
   Next
End Function
;Function Scrolling()
Function Scrolling()
   If KeyDown(200) And MouseDown(1)=0 And yback>0 Then:
      yback=yback-4
   EndIf
   If KeyDown(208) And MouseDown(1)=0 And yback<mapY*tile-height+Bhigh Then:
      yback=yback+4
   EndIf
   If KeyDown(205) And MouseDown(1)=0 And xback<mapX*tile-width Then:
      xback=xback+4
   EndIf
   If KeyDown(203) And MouseDown(1)=0 And xback>0 Then:
      xback=xback-4
   EndIf
End Function
;Function UpdateMaus()
Function UpdateMaus()
   mx=MouseX()
   my=MouseY()
   DrawImage maus,mx,my
   DrawImage Imaus,mx,my
   mTx=(mx+xback)/tile
   mTy=(my+yback)/tile
End Function
;Function UpdateScreen()
Function UpdateScreen()
   Color 50,50,50
   Rect 0,height-Bhigh,width,Bhigh,1
   Color 150,150,150
   Rect 0,height-Bhigh,width,Bhigh,0
   ;Daten
   Text 5,height-Bhigh+5,"Maus: "+mx+"/"+my+" * Maus-Tile: "+mTx+"/"+mTy+" * Scoll: "+xback+"/"+yback+" * FPS: "+fps
   For u.unit=Each unit
   If u\sel=1 Then:
      Text 5,height-Bhigh+20,"Unit: "+u\x+"/"+u\y+" * Tile: "+u\xTile+"/"+u\yTile+" * Draw: "+u\xDraw+"/"+u\yDraw+" * "+u\action+"/"+u\ziel
      Text 5,height-Bhigh+35,"Path: "+u\xZ+"/"+u\yZ+" * Lauf: "+u\xLauf+"/"+u\yLauf+" * Att: "+u\xAtt+"/"+u\yAtt+" * "+u\FightStep+"/"+u\entf
      Text 5,height-Bhigh+50,"Numm: o"+u\opfer+"/ a"+u\nummer+" * ziel: "+u\ziel+"/"+u\entf+" * FPS: "+fps

   EndIf
   Next
End Function
;Function NewUnit(x,y,typ,team)
Function NewUnit(x,y,typ,team)
   u.unit=New unit
   u\x=x*tile
   u\y=y*tile
   u\xZ=u\x
   u\yZ=u\y
   u\typ=typ
   u\team=team
   u\speed=2
   u\entf=99999
   u\entf2=99997
   u\reich=5
   u\sicht=10   
   u\dauer=50
   u\nummer=q
   q=q+1
   u\uPfad=CreateBank(2)
End Function
;Function UpdateUnit()
Function UpdateUnit()
   For u.unit=Each unit
      ;Koordinaten
      u\xDraw=u\x-xback+tile/2 : u\yDraw=u\y-yback+tile/2
      u\xTile=u\x/tile  : u\yTile=u\y/tile
      ;Anzeigen
      If u\sel=1 Then Color 0,255,0 : Rect u\xDraw-tile/2,u\yDraw-tile/2,tile,tile,0
      DrawImage ImaUnit(u\winkel),u\xDraw,u\yDraw,u\typ   
      ;Selektieren
      If MouseDown(1) And ImagesCollide(maus,mx,my,0,IMAunit(u\winkel),u\xDraw,u\yDraw,0)=1 Then u\sel=1
      If MouseDown(1) And ImagesCollide(maus,mx,my,0,IMAunit(u\winkel),u\xDraw,u\yDraw,0)=0 Then u\sel=0       
      ;Aktion
      Select u\action
      Case 0 ; Stehen bleiben
         ;u\xZ=u\x    : u\yZ=u\y
         u\xLauf=u\xZ : u\yLauf=u\yZ
         u\xAtt=u\xZ  : u\yAtt=u\yZ
         u\FightStep=0
      Case 1 ; Laufen
         If u\go=0 Then
            Pathfinding(u\x/tile,u\y/tile,u\xLauf/tile,u\yLauf/tile):u\FightStep=PfadSpliner()
            Bsize=BankSize(PfadBank)   
            ResizeBank(u\uPfad,Bsize)
            CopyBank PfadBank, 0, u\uPfad, 0, Bsize
            u\go=1
         EndIf
         If u\xTile*tile=u\xZ And u\yTile*tile=u\yZ And u\fightstep>0 Then u\fightstep=u\fightstep-44
         If u\fightstep<0 Then u\fightstep=0
         If u\fightstep<1 Then: u\xZ=u\xLauf:u\yZ=u\yLauf
         ElseIf u\fightstep>0 Then:
            u\xZ=(PeekShort(u\uPfad,u\FightStep))
            u\yZ=(PeekShort(u\uPfad,u\FightStep+2))   
         EndIf
      Case 2 ; Verfolgen
         If u\go=0 Then:
            For a.unit=Each unit
            For o.unit=Each unit
               ;Zielsuche
               If a\team<>o\team And a\opfer=o\nummer Then:
               If a\entf<=a\sicht*tile And a\entf>a\reich*tile And a\go=0 Then:
                  a\go=1
                  a\xLauf=o\xTile*tile
                  a\yLauf=o\yTile*tile
                  Pathfinding(a\x/tile,a\y/tile,a\xLauf/tile,a\yLauf/tile):a\FightStep=PfadSpliner()
                  Bsize=BankSize(PfadBank)
                  ResizeBank(a\uPfad,Bsize)
                  CopyBank PfadBank, 0, a\uPfad, 0, Bsize                  
               EndIf
               EndIf
            Next
            Next             
         EndIf
         If u\entf<u\reich*tile Then u\action=0

         If u\xTile=u\xZ/tile And u\yTile=u\yZ/tile And u\fightstep>0 Then u\fightstep=u\fightstep-44
         If u\fightstep<0 Then u\fightstep=0
         If u\fightstep<1 Then: u\xLauf=u\xZ:u\yLauf=u\yZ
         ElseIf u\fightstep>0 Then:
            u\xZ=(PeekShort(u\uPfad,u\FightStep))
            u\yZ=(PeekShort(u\uPfad,u\FightStep+2))   
         EndIf         
      Case 3 ; Angreifen
      Case 4 ; Position Halten
         ;u\xZ=u\x    : u\yZ=u\y
         u\xLauf=u\xZ : u\yLauf=u\yZ
         u\xAtt=u\xZ  : u\yAtt=u\yZ
         u\FightStep=0      
      End Select
      ;Bewegen
         ;Oben -Rechts laufen
         If u\xZ > u\x And u\yZ < u\y Then: u\winkel=1 : u\x=u\x+u\speed : u\y=u\y-u\speed ;: status(u\xTile,u\yTile)=0
         ;Unten-Rechts laufen
         ElseIf u\xZ > u\x And u\yZ > u\y Then: u\winkel=3 : u\x=u\x+u\speed : u\y=u\y+u\speed ;: status(u\xTile,u\yTile)=0
         ;Unten-Links  laufen
         ElseIf u\xZ < u\x And u\yZ > u\y Then: u\winkel=5 : u\x=u\x-u\speed : u\y=u\y+u\speed ;: status(u\xTile,u\yTile)=0
         ;Oben -Links  laufe
         ElseIf u\xZ < u\x And u\yZ < u\y Then: u\winkel=7 : u\x=u\x-u\speed : u\y=u\y-u\speed ;: status(u\xTile,u\yTile)=0                            
         ;Oben   laufen
         ElseIf u\yZ < u\y Then: u\winkel=0 : u\y=u\y-u\speed ;: status(u\xTile,u\yTile+1)=0         
         ;Unten  laufen
         ElseIf u\yZ > u\y Then: u\winkel=4 : u\y=u\y+u\speed ;: status(u\xTile,u\yTile-1)=0         
         ;Rechts laufen
         ElseIf u\xZ > u\x Then: u\winkel=2 : u\x=u\x+u\speed ;: status(u\xTile-1,u\yTile)=0         
         ;Links  laufen
         ElseIf u\xZ < u\x Then: u\winkel=6 : u\x=u\x-u\speed ;: status(u\xTile+1,u\yTile)=0         
         EndIf
      ;Auslöser
      If u\sel=1
         If MouseHit(2) And status(mTx,mTy)=0 Then u\action=1:u\xLauf=mTx*tile:u\yLauf=mTy*tile:u\go=0
         If u\xLauf/tile=u\xTile And u\yLauf/tile=u\yTile Then u\action=0
         If KeyDown(2) Then u\action=4
      EndIf
      
      If u\xLauf/tile=u\xTile And u\yLauf/tile=u\yTile And u\action<>4 Then u\action=0
      If u\action=0 Then:      
         If u\entf<=u\sicht*tile And u\entf>u\reich*tile Then: u\action=2 : u\go=0
         ElseIf u\entf<=u\reich*tile Then: u\action=0:u\go=1
         EndIf         
      EndIf
      
      If u\entf<=u\reich*tile And u\action=2 Then u\action=0:u\go=1
      ;Entfernung
         For a.unit=Each unit
         For o.unit=Each unit
            If a\team<>o\team Then:
               If a\ziel=0 And o\xDraw<a\xDraw+a\reich*tile And o\xDraw>a\xDraw-a\reich*tile And o\yDraw<a\yDraw+a\reich*tile And o\yDraw>a\yDraw-a\reich*tile Then:      
                  a\ziel=1
                  ;Text o\xDraw,o\yDraw,"Der da"+a\nummer
                  a\opfer=o\nummer
               Else:
                  a\ziel=0
                  ;a\entf=9999999
               EndIf
               
            EndIf
            
            If a\opfer=o\nummer Then:   
               a\entf = Sqr((Abs(a\xDraw-o\xDraw)^2) + (Abs(a\yDraw-o\yDraw)^2) )
            EndIf
                        
         Next
         Next
      ;Angreifen
      For a.unit=Each unit
      For o.unit=Each unit
         If a\action<>1 And a\action<>2 Then:
            If a\entf<=a\reich*tile And a\opfer=o\nummer Then:
            
               a\buf=a\buf+1
               If a\buf>a\dauer Then a\buf=0:o\live=o\live-a\att
               If a\buf>1 And a\buf<10 Then Color 255,0,0:Oval o\xDraw+Rand(-5,5),o\yDraw+Rand(-5,5),8,8
 
            EndIf
         EndIf          
      Next
      Next
      ;Belegen
      ;status(u\xTile,u\yTile)=u\team
      
   Next
End Function
;Function Pathfinding(startx,starty,endx,endy)
Function Pathfinding(startx,starty,endx,endy)
  Delete Each node
  Delete Each open
  Delete Each path
  Dim nodemap(mapwidth,mapheight)
  If startx=endx And starty=endy Then Return

  node.node=New node
  node\x=startx
  node\y=starty
  open.open=New open
  open\node=node
  nodemap(startx,starty)=1

  .again2
  node=Null
  cost=2147483647
  For open=Each open
    delta=sqrmap(Abs(open\node\x-endx),Abs(open\node\y-endy))
    If open\node\cost+delta<cost Then
      cost=open\node\cost+delta
      node=open\node
      tempopen.open=open
    EndIf
  Next
  If node=Null Then Return
  Delete tempopen

  For i=0 To 7
    x=node\x+dirx(i)
    y=node\y+diry(i)
    If x=>0 And y=>0 And x<=mapwidth And y<=mapheight Then
      If status(x,y)=0 And nodemap(x,y)=0 Then
        If dirz(i)=1 Then
          If (status(x,node\y)=1 And status(node\x,y)=1) Then Goto jump2
        EndIf
        tempnode.node=New node
        tempnode\parent=node
        tempnode\cost=node\cost+1+dirz(i)
        tempnode\x=x
        tempnode\y=y
        open.open=New open
        open\node=tempnode
        nodemap(x,y)=1
        If x=endx And y=endy Then finish=1:Exit
        .jump2
      EndIf
    EndIf
  Next
  If finish=0 Then Goto again2

  While tempnode\parent<>Null
    path.path=New path
    path\node=tempnode
    tempnode=tempnode\parent
  Wend
  path.path=New path
  path\node=tempnode
End Function
;Function PfadSpliner()
Function PfadSpliner()
FreeBank(PfadBank):PfadBank=CreateBank(4)

    Color 200,0,0
    For path.path=Each path
      If path=Last path Then Exit
      tmp.path=Before path
      If tmp=Null Then
        x0=path\node\x
        y0=path\node\y
      Else
        x0=tmp\node\x
        y0=tmp\node\y
      EndIf
      x1=path\node\x
      y1=path\node\y
      tmp.path=After path
      x2=tmp\node\x
      y2=tmp\node\y
      tmp.path=After tmp
      If tmp=Null Then
        x3=x2
        y3=y2
      Else
        x3=tmp\node\x
        y3=tmp\node\y
      EndIf
      spline(X0,Y0,X1,Y1,X2,Y2,X3,Y3)
   Next
Return BankSize(PfadBank)-4

End Function
;Function spline(x1,y1,x2,y2,x3,y3,x4,y4)
Function spline(x1,y1,x2,y2,x3,y3,x4,y4)
Ras=32
x1=x1*Ras:y1=y1*Ras
x2=x2*Ras:y2=y2*Ras
x3=x3*Ras:y3=y3*Ras
x4=x4*Ras:y4=y4*Ras

For u#=0 To 1.1 Step .1

    u2#=u#*u#
    u3#=u#*u#*u#
    f1#=-0.5*u3#+1.0*u2#-0.5*u#
    f2#= 1.5*u3#-2.5*u2#+1.0
    f3#=-1.5*u3#+2.0*u2#+0.5*u#
    f4#= 0.5*u3#-0.5*u2#

    x=x1*f1#+x2*f2#+x3*f3#+x4*f4#
    y=y1*f1#+y2*f2#+y3*f3#+y4*f4#

   If ax<>x Or ay<>y Then
   Pos=BankSize(PfadBank):ResizeBank(PfadBank,Pos+4)
   PokeShort(PfadBank,Pos,x)
   PokeShort(PfadBank,Pos+2,y)
   EndIf
   
   ax=x:ay=y
Next
End Function

The_Baschdi@
Wer in Ogame is soll sofort zum Orden wechseln (D.O.) --- Alle Macht dem Orden

Neue Antwort erstellen


Übersicht BlitzBasic Allgemein

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group