You'd need a pathfinding algorithm (such as A*) to find the best route to get there, and to hold at 2 squares away, you'd simply check before the monster took its next move how far away it was.
Printable View
You'd need a pathfinding algorithm (such as A*) to find the best route to get there, and to hold at 2 squares away, you'd simply check before the monster took its next move how far away it was.
Could you write it?
These are the variables:
Player(1).X Player(1).Y Player(2).X Player(2).Y
Monster(m).mX Monster(m).mY
Thank you,
Arie.
No.
Writing a pathfinding routine is not a small task. You'd need to research it. If you check out the 'get shortest distance' thread I posted a link to good tutorial site there.
Good luck :)
Does anyone have a sample for this?
Arie.
Well I've an idea.
You draw a line between the monster and the player. Convert the line into the closest connected blocks on the grid. Then if any of the boxes you've chosen have a block on them, then try going around it either side to reconnect up the line again.
I made this code but it doesn't work in the best way...
here it is:
Tell me what's wrong...Code:Dim Flag1 As Boolean
Flag1 = False
If Abs(Player(1).X - .mX) + Abs(Player(1).Y - .mY) > _
Abs(Player(2).X - .mX) + Abs(Player(2).Y - .mY) Or NumberOfPlayers = 1 Then
If Abs(Player(1).X - .mX) > Abs(Player(1).Y - .mY) Then
If Player(1).X > .mX Or mBlock(.mX - 1, .mY, rmMonster) Then .mDirection = dRight Else .mDirection = dLeft
Flag1 = True
Else
If (Player(1).Y < .mY And mBlock(.mX, .mY - 1, rmMonster)) Or _
(Player(1).Y > .mY And mBlock(.mX, .mY + 1, rmMonster)) Then
If Player(1).X > .mX Then .mDirection = dRight Else .mDirection = dLeft
Flag1 = True
GoTo NextIf
End If
End If
If Abs(Player(1).X - .mX) <= Abs(Player(1).Y - .mY) Then
If Player(1).Y > .mY Or mBlock(.mX, .mY - 1, rmMonster) Then Debug.Print "Hey!": .mDirection = dDown Else .mDirection = dUp
Flag1 = True
Else
If (Player(1).X < .mX And mBlock(.mX - 1, .mY, rmMonster)) Or _
(Player(1).X > .mX And mBlock(.mX + 1, .mY, rmMonster)) Then
If Player(1).Y > .mY Then .mDirection = dDown Else .mDirection = dUp
Flag1 = True
GoTo NextIf
End If
End If
NextIf:
End If
If Abs(Player(1).X - .mX) + Abs(Player(1).Y - .mY) <= _
Abs(Player(2).X - .mX) + Abs(Player(2).Y - .mY) And Flag1 = False Then
If Abs(Player(2).X - .mX) > Abs(Player(2).Y - .mY) Then
If Player(2).X > .mX Then .mDirection = dRight Else .mDirection = dLeft
Flag1 = True
Else
If (Player(2).Y < .mY And mBlock(.mX, .mY - 1, rmMonster)) Or _
(Player(2).Y > .mY And mBlock(.mX, .mY + 1, rmMonster)) Then
If Player(2).X > .mX Then .mDirection = dRight Else .mDirection = dLeft
Flag1 = True
GoTo NextIf2
End If
End If
If Abs(Player(2).X - .mX) <= Abs(Player(2).Y - .mY) Then
If Player(2).Y > .mY Then .mDirection = dDown Else .mDirection = dUp
Flag1 = True
Else
If (Player(2).X < .mX And mBlock(.mX - 1, .mY, rmMonster)) Or _
(Player(2).X > .mX And mBlock(.mX + 1, .mY, rmMonster)) Then
If Player(2).Y > .mY Then .mDirection = dDown Else .mDirection = dUp
Flag1 = True
GoTo NextIf2
End If
End If
NextIf2:
End If
.mMoving = Flag1
Thank you,
Arie.