Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] NPC should run from Player...
#1
I've been trying again and again but the NPC simply doesn't do what I want it to do.
Well, that's something quite vital for my game, because I make some wildlife simulation online.
So the prey-npc (it's a seperate type declared in the corresponding module) should run (or walk, whatever) away from
the player if he/she is in the right distance. I took out the distance stuff because I'm not sure if it was causing the
problems but well, it's still not working.

That code below should check if the npc got prey-behavior and if not it's behaving normally.
Otherwise the prey will take a good run away from the player. That's the plan.

I'm not sure though why it's not doing what it's supposed to Cry Any ideas?


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
                                
                                i = Int(Rnd * 5)
                                
                                ' Lets move the npc
                                If Npc(NpcNum).Behavior  NPC_BEHAVIOR_PREY Then
                                Select Case i
                                    Case 0
                                        ' Up
                                        If MapNpc(y, x).y > GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_UP) Then
                                                Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Down
                                        If MapNpc(y, x).y < GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_DOWN) Then
                                                Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Left
                                        If MapNpc(y, x).x > GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_LEFT) Then
                                                Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Right
                                        If MapNpc(y, x).x < GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_RIGHT) Then
                                                Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                    
                                    Case 1
                                        ' Right
                                        If MapNpc(y, x).x < GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_RIGHT) Then
                                                Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Left
                                        If MapNpc(y, x).x > GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_LEFT) Then
                                                Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Down
                                        If MapNpc(y, x).y < GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_DOWN) Then
                                                Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Up
                                        If MapNpc(y, x).y > GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_UP) Then
                                                Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        
                                    Case 2
                                        ' Down
                                        If MapNpc(y, x).y < GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_DOWN) Then
                                                Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Up
                                        If MapNpc(y, x).y > GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_UP) Then
                                                Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Right
                                        If MapNpc(y, x).x < GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_RIGHT) Then
                                                Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Left
                                        If MapNpc(y, x).x > GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_LEFT) Then
                                                Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                    
                                    Case 3
                                        ' Left
                                        If MapNpc(y, x).x > GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_LEFT) Then
                                                Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Right
                                        If MapNpc(y, x).x < GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_RIGHT) Then
                                                Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Up
                                        If MapNpc(y, x).y > GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_UP) Then
                                                Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Down
                                        If MapNpc(y, x).y < GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_DOWN) Then
                                                Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                End Select
                             Else
                                Target = 1 ' For testing it's set to 1..
                                           ' it's the first Player online.
                                          
                                     ' Down / Up
                                     If GetPlayerY(Target) = MapNpc(y, x).y Then
                                        If MapNpc(y, x).y > GetPlayerY(Target) Then
                                           If CanNpcMove(y, x, DIR_DOWN) Then
                                              Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                              DidWalk = True
                                           End If
                                        ElseIf MapNpc(y, x).y < GetPlayerY(Target) Then
                                           If CanNpcMove(y, x, DIR_UP) Then
                                              Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                              DidWalk = True
                                           End If
                                        End If
                                        
                                     ' Right / Left
                                     ElseIf GetPlayerY(Target) = MapNpc(y, x).y Then
                                        If MapNpc(y, x).x > GetPlayerX(Target) Then
                                           If CanNpcMove(y, x, DIR_RIGHT) Then
                                              Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                              DidWalk = True
                                           End If
                                        ElseIf MapNpc(y, x).x < GetPlayerX(Target) Then
                                           If CanNpcMove(y, x, DIR_LEFT) Then
                                              Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                              DidWalk = True
                                           End If
                                        End If
                                     End If
                                End If
Reply
#2
Look at the range code for the "attack on sight" npcs. That might be your problem. Then all you have to do is set the range when you edit the npc and then check if the player is in that range, get the direction in which that player is located, and go the opposite direction. Big Grin
Reply
#3
oo I thought I tried that... well, gotta look again I guess..
I'll edit this if I find something!

EDIT: oo' That was quite scaring... little thing caused a big problem.. I actually had forgotten to add the type Prey as condition to the part where the target is set :lol: Thanks for the tip! Wouldn't have found it without it Big Grin
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)