Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Summer Competition] My AI!
#14
I need some help, Ive tryed several times to implement this tutorial but still cant get it to work for Elysium. Heres my code:
Code:
' /////////////////////////////////////////////
                ' // This is used for NPC walking/targetting //
                ' /////////////////////////////////////////////
                ' Make sure theres a npc with the map
                If Map(y).Npc(x) > 0 And MapNpc(y, x).Num > 0 Then
                    Target = MapNpc(y, x).Target
                    
                    ' Check to see if its time for the npc to walk
                    If Npc(NpcNum).Behavior  NPC_BEHAVIOR_SHOPKEEPER Then
                        ' Check to see if we are following a player or not
                        If Target > 0 Then
                            ' Check if the player is even playing, if so follow'm
                            If IsPlaying(Target) And GetPlayerMap(Target) = y Then
                                DidWalk = False
                                CloseToPlayer = False
                                If (GetPlayerY(Target) + 1 = MapNpc(y, x).y) And (GetPlayerX(Target) = MapNpc(y, x).x) Then
                                    CloseToPlayer = True
                                Else
                                    If (GetPlayerY(Target) - 1 = MapNpc(y, x).y) And (GetPlayerX(Target) = MapNpc(y, x).x) Then
                                        CloseToPlayer = True
                                    Else
                                        If (GetPlayerY(Target) = MapNpc(y, x).y) And (GetPlayerX(Target) + 1 = MapNpc(y, x).x) Then
                                            CloseToPlayer = True
                                        Else
                                            If (GetPlayerY(Target) = MapNpc(y, x).y) And (GetPlayerX(Target) - 1 = MapNpc(y, x).x) Then
                                                CloseToPlayer = True
                                            End If
                                        End If
                                    End If
                                End If                                
                                If Not CloseToPlayer Then
                                    o = MapNpc(Y, X).Y * (MAX_MAPX + 1) + MapNpc(Y, X).X
                                    d = GetPlayerY(Target) * (MAX_MAPX + 1) + GetPlayerX(Target)
                                    For i = 0 To NV
                                        VY = Int(i / (MAX_MAPX + 1))
                                        VX = i Mod (MAX_MAPX + 1)
                                        If (Map(y).Tile(VX, VY).Type = TILE_TYPE_BLOCKED Or Map(y).Tile(VX, VY).Type = TILE_TYPE_NPCAVOID Or Map(y).Tile(VX, VY).Type = TILE_TYPE_WARP Or (Map(y).Tile(VX, VY).Type = TILE_TYPE_KEY And TempTile(y).DoorOpen(VX, VY) = NO)) Then
                                            For J = 0 To NV
                                                Mat(J, i) = 0
                                                Mat(i, J) = 0
                                            Next J
                                        End If
                                        For J = 1 To MAX_PLAYERS
                                            If IsPlaying(J) Then
                                                If GetPlayerMap(J) = y Then
                                                    If GetPlayerX(J) = VX And GetPlayerY(J) = VY And J  Target Then
                                                        For K = 0 To NV
                                                            Mat(i, K) = 0
                                                            Mat(K, i) = 0
                                                        Next K
                                                    End If
                                                End If
                                            End If
                                        Next J
                                        For J = 1 To MAX_MAP_NPCS
                                            If Map(y).Npc(J) > 0 And MapNpc(y, J).Num > 0 Then
                                                If MapNpc(y, J).x = VX And MapNpc(y, J).y = VY And J  x Then
                                                    For K = 0 To NV
                                                        Mat(i, K) = 0
                                                        Mat(K, i) = 0
                                                    Next K
                                                End If
                                            End If
                                        Next J
                                    Next i
                                    Flag = 0
                                    PathLine(0) = d
                                    Start = 0
                                    Finish = 1
                                    If o  d Then
                                        While (PathLine(Start)  -1 And Flag = 0)
                                            VY = Int(PathLine(Start) / (MAX_MAPX + 1))
                                            VX = PathLine(Start) Mod (MAX_MAPX + 1)
                                            If VY >= 0 And VY < MAX_MAPY Then
                                                If Mat(PathLine(Start), PathLine(Start) + MAX_MAPX + 1) = 1 And Marked(PathLine(Start) + MAX_MAPX + 1) = 0 And Flag = 0 Then
                                                    Path(PathLine(Start) + MAX_MAPX + 1) = PathLine(Start)
                                                    PathLine(Finish) = PathLine(Start) + MAX_MAPX + 1
                                                    Marked(PathLine(Start) + MAX_MAPX + 1) = 1
                                                    Finish = Finish + 1
                                                    If PathLine(Finish - 1) = o Then
                                                        Flag = 1
                                                    End If
                                                End If
                                            End If
                                            
                                            VY = Int(PathLine(Start) / (MAX_MAPX + 1))
                                            VX = PathLine(Start) Mod (MAX_MAPX + 1)
                                            If VX > 0 And VX = 0 And VX < MAX_MAPX Then
                                                If Mat(PathLine(Start), PathLine(Start) + 1) = 1 And Marked(PathLine(Start) + 1) = 0 And Flag = 0 Then
                                                    Path(PathLine(Start) + 1) = PathLine(Start)
                                                    PathLine(Finish) = PathLine(Start) + 1
                                                    Marked(PathLine(Start) + 1) = 1
                                                    Finish = Finish + 1
                                                    If PathLine(Finish - 1) = o Then
                                                        Flag = 1
                                                    End If
                                                End If
                                            End If
                                            
                                            VY = Int(PathLine(Start) / (MAX_MAPX + 1))
                                            VX = PathLine(Start) Mod (MAX_MAPX + 1)
                                            If VY > 0 And VY  VY Then
                                                If CanNpcMove(y, x, DIR_UP) Then
                                                    Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                    DidWalk = True
                                                End If
                                            ElseIf MapNpc(y, x).y < VY Then
                                                ' Down
                                                If CanNpcMove(y, x, DIR_DOWN) Then
                                                    Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                    DidWalk = True
                                                End If
                                            ElseIf MapNpc(y, x).x > VX Then
                                                ' Left
                                                If CanNpcMove(y, x, DIR_LEFT) Then
                                                    Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                    DidWalk = True
                                                End If
                                            ElseIf MapNpc(y, x).x < VX Then
                                                ' Right
                                                If CanNpcMove(y, x, DIR_RIGHT) Then
                                                    Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                    DidWalk = True
                                                End If
                                            End If
                                                                              End If
                                ' Check if we can't move and if player is behind something and if we can just switch dirs
                                If Not DidWalk Then
                                    If MapNpc(y, x).x - 1 = GetPlayerX(Target) And MapNpc(y, x).y = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_LEFT Then
                                            Call NpcDir(y, x, DIR_LEFT)
                                        End If
                                        DidWalk = True
                                    End If
                                    If MapNpc(y, x).x + 1 = GetPlayerX(Target) And MapNpc(y, x).y = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_RIGHT Then
                                            Call NpcDir(y, x, DIR_RIGHT)
                                        End If
                                        DidWalk = True
                                    End If
                                    If MapNpc(y, x).x = GetPlayerX(Target) And MapNpc(y, x).y - 1 = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_UP Then
                                            Call NpcDir(y, x, DIR_UP)
                                        End If
                                        DidWalk = True
                                    End If
                                    If MapNpc(y, x).x = GetPlayerX(Target) And MapNpc(y, x).y + 1 = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_DOWN Then
                                            Call NpcDir(y, x, DIR_DOWN)
                                        End If
                                        DidWalk = True
                                    End If
                                    
                                    ' We could not move so player must be behind something, walk randomly.
                                    If Not DidWalk Then
                                        i = Int(Rnd * 2)
                                        If i = 1 Then
                                            i = Int(Rnd * 4)
                                            If CanNpcMove(y, x, i) Then
                                                Call NpcMove(y, x, i, MOVING_WALKING)
                                            End If
                                        End If
                                    End If
                                End If
                            Else
                                MapNpc(y, x).Target = 0
                            End If
                        Else
                            i = Int(Rnd * 4)
                            If i = 1 Then
                                i = Int(Rnd * 4)
                                If CanNpcMove(y, x, i) Then
                                    Call NpcMove(y, x, i, MOVING_WALKING)
                                End If
                            End If
                        End If
                    End If
                End If
Ive spent alot of time going through the scenarios and following the pathfinding, my main problem is that Flag never becomes 1. For some reason, my mat() causes subscript out of range. Because the InitServer sub is no longer used, I put this directly after Call SpawnAllMapNpcs in the main sub in modGeneral:
Code:
NV = (MAX_MAPX + 1) * (MAX_MAPY + 1) - 1
    ReDim Mat(0 To NV, 0 To NV) As Byte
    ReDim Marked(0 To NV) As Byte
    ReDim PathLine(0 To NV) As Integer
    ReDim Path(0 To NV) As Integer
Maybe thats the wrong place for elysium? Hope you can help, thanks!
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)