Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Done: Click for Pathfindind (walking and running!)


Giukko
 Share

Recommended Posts

UPDATE: fixed mouse coordinates just with CurX and CurY, and changed variables TX and TY with name TTX and TTY in order to not have conflicts with same name variables.

UPDATED: New AI. Thanks also to Herbert Glarner for his pathfinding code, which was written only for npcs, so I updated and changed various aspects of this code in order to use it for player character. Now when you click on map, character avoids obstacles, npcs and other players and this happens also for npcs when they search for the player to attack him (if npc AI option is on). An animation appears on the point where you click to highlight the destination target. If you click on an npc for targetting, character doesn't moves.
IMPORTANT NOTE: if you used my code before, now all the code on this forum page is changed, so you should copy all again.
Below the new source code:

Addictional note: on regard mouse x y problems, see my post: https://www.eclipseorigins.com/topic/86406/fixed-use-of-the-mouse
Hi all, here I show you the code in order to click on the map and move your character. ...and with double click character starts running :) PS: with this code also you can use keyboard without problems. Code is written to avoid character movements when you click on menus, chat-scroll and hotbar. I also added function which when you click on map all menu windows will be closed.

Here all the code parts:

1- install Visual Basic 6 on your computer

2- Open your Client folder and put this animation (or use the animation which you prefer) inside path: `data files->graphics->animation`. Here is the png file for this exaple: ![](https://i.imgur.com/ke3aFAR.png[/img]) Save this image with name: `13`

3- Start `Eclipse Renewal.exe`, login, check on `Eclipse Renewal Server` to have the owner status for your character, write on chat `/admin` and open Development Panel. Search for animations editor, create a new animation called `click` and in option -> `Layer 0 (below player)` use this parameters: `loop count = 1`, `frame count = 20`, `loop time = 70`, `sprite = 13`.
Remember the index number of your new animation, save all and close `Eclipse Renewal.exe` .

4- Download this file https://filehost.net/f8f8daebba360bb9 (is the new pathfinding module for Client version - another one is on Server.vbp only for npcs)and copy it in `src` folder which is inside `Client` folder.

4- open `Client.vbp`

5- Using Visual Basic, search for modules and right click on one of them, then choose `add`->`module`->`existing` and select inside `Client/src/` folder, your new `modPathfinding` file previously downloaded. Save `Client.vbp`.

6- Search inside module named `modTypes` for `Private Type PlayerRec` and before the `End Type` put this:
```
' pathfinding
arPath() As tPoint
hasPath As Boolean
pathLoc As Long
```

7- Search `modGlobals ` and in an empty space put:
```
' PlayerPathfinding
Public TTX As Long
Public TTY As Long
Public Hurry As Boolean
```

8- Search `modInput` and after any `end sub`, paste this code:
```
Public Sub CheckPlayerPathfinding()
Dim Anim As Byte
Dim MapNpcNum As Long
' //////////////////////////////////////////
' // This is used for Player pathfinding //
' //////////////////////////////////////////

'check for click on npcs, in this case character doesn't moves
For MapNpcNum = 0 To MAX_NPCS

If MapNpc(MapNpcNum).x = TTX And MapNpc(MapNpcNum).y = TTY Then
Hurry = False
mouseDown = False
Exit Sub
End If
Next

' if character reaches destination stop it
If TTX = GetPlayerX(MyIndex) And TTY = GetPlayerY(MyIndex) Then
Hurry = False
mouseDown = False
Exit Sub

Else
CreatePathMatrix

' animation on click
For Anim = 1 To MAX_BYTE

With AnimInstance(Anim)
.Animation = 13 ' or remeber the number on index animation which you created before
.x = TTX
.y = TTY
.LockType = 0
.lockindex = 0
.Used(0) = True
.Used(1) = True
End With
Next

' check for double click --> running!
If Hurry Then
ShiftDown = True
Else
ShiftDown = False
End If


' if map has a working matrix, use a* pathfinding
If mapMatrix(Player(MyIndex).Map).created Then

If GetPlayerX(MyIndex) <> TTX Or GetPlayerY(MyIndex) <> TTY Then
' map click has changed, re-find the path
Player(MyIndex).hasPath = APlus(CLng(GetPlayerX(MyIndex)), CLng(GetPlayerY(MyIndex)), CLng(TTX), CLng(TTY), Void, Player(MyIndex).arPath)
' set the player cur path location
If Player(MyIndex).hasPath Then Player(MyIndex).pathLoc = UBound(Player(MyIndex).arPath)
End If
' if has path, follow it
If Player(MyIndex).hasPath Then
' follow path
PlayerMoveAlongPath

End If
End If

End If

End Sub
```
9- Again in `modInput` search for `Public Sub CheckInputKeys()`, delete all this `Sub` and paste this one:
```
Public Sub CheckInputKeys()
' If debug mode, handle error then exit out
If Options.Debug = 1 Then On Error GoTo ErrorHandler


If GetKeyState(vbKeyShift) < 0 Then
ShiftDown = True
Else
ShiftDown = False
End If

If GetKeyState(vbKeyReturn) < 0 Then
CheckMapGetItem
End If

If GetKeyState(vbKeyControl) < 0 Then
Hurry = False
mouseDown = False
ControlDown = True
DirUp = False
DirDown = False
DirLeft = False
DirRight = False
Exit Sub
Else
ControlDown = False
End If

' da reimplementare per magia rapida (copia e incolla codice da versione precedente in chekattack)
If GetKeyState(vbKeyY) < 0 Then
Ypsilon = True
Else
Ypsilon = False
End If

'Move Up
If GetKeyState(vbKeyUp) < 0 Then
Hurry = False
mouseDown = False
DirUp = True
DirDown = False
DirLeft = False
DirRight = False
ControlDown = False
Exit Sub
Else
DirUp = False
End If

'Move Right
If GetKeyState(vbKeyRight) < 0 Then
Hurry = False
mouseDown = False
DirUp = False
DirDown = False
DirLeft = False
DirRight = True
ControlDown = False
Exit Sub
Else
DirRight = False
End If

'Move down
If GetKeyState(vbKeyDown) < 0 Then
Hurry = False
mouseDown = False
DirUp = False
DirDown = True
DirLeft = False
DirRight = False
ControlDown = False
Exit Sub
Else
DirDown = False
End If

'Move left
If GetKeyState(vbKeyLeft) < 0 Then
Hurry = False
mouseDown = False
DirUp = False
DirDown = False
DirLeft = True
DirRight = False
ControlDown = False
Exit Sub
Else
DirLeft = False
End If

' Error handler
Exit Sub
ErrorHandler:
HandleError "CheckInputKeys", "modInput", Err.Number, Err.Description, Err.Source, Err.HelpContext
Err.Clear
Exit Sub
End Sub
```
10- Now search in `modGeneral` for `Public Sub GuiDblClickSearch` and find this line:

`Dim lines As Long, count As Byte, Width As Long`

after this one, paste:
```
If GetGuiElement(X, Y) = 0 And GetBtnElement(X, Y) = 0 Then
'Pathfinding in running mode!
mouseDown = True
Hurry = True
TTX = CurX
TTY = CurY
For i = 1 To NumGUI
GUI(i).Visible = False
Next
For i = 1 To NumButtons
BTN(i).State = 0
Next
GUI(GUI_CHAT).Visible = True
GUI(GUI_HOTBAR).Visible = True
GUI(GUI_STATS).Visible = True
GUI(GUI_DRAGBOX).Visible = False
End If
```
11- Again in `modGeneral` search for `Public Sub GuiDownSearch` and find this line: `Select Case GetGuiElement(X, Y) `, after this one, paste:
```
Case GUI_CHARACTER
Exit Sub

Case GUI_OPTIONS
Exit Sub
```
12- Stay again in `modGeneral` and search for `Public Sub BtnDownSearch` and find this lines:

`For i = 1 To NumButtons`
`BTN(i).State = 0`
`Next`

after `next`, paste this code:
```
If GetGuiElement(X, Y) = 0 And GetBtnElement(X, Y) = 0 Then
mouseDown = True
Hurry = False
TTX = CurX
TTY = CurY
For i = 1 To NumGUI
GUI(i).Visible = False
Next
For i = 1 To NumButtons
BTN(i).State = 0
Next
GUI(GUI_CHAT).Visible = True
GUI(GUI_HOTBAR).Visible = True
GUI(GUI_STATS).Visible = True
GUI(GUI_DRAGBOX).Visible = False
End If
```
13- Search in `modGameLogic` inside `Sub GameLoop` for the line:
`Call CheckInputKeys ' Check which keys were pressed`

end after this one paste:
```
'Player Pathfinding
If mouseDown Then Call CheckPlayerPathfinding
```
14- That's it. You now have character pathfinding after any click and he/she starts running after double click and evitates obstacles ;)

15- Extras: I'll update this point for npcs avoids players and other npcs.

Please let me know if I forgot something :)
Link to comment
Share on other sites

  • 2 weeks later...
UPDATED: New AI. Thanks also to Herbert Glarner for his pathfinding code, which was written only for npcs, so I updated and changed various aspects of this code in order to use it for player character. Now when you click on map, character avoids obstacles, npcs and other players and this happens also for npcs when they search for the player to attack him (if npc AI option is on). An animation appears on the point where you click to highlight the destination target. If you click on an npc for targetting, character doesn't moves.
IMPORTANT NOTE: if you used my code before, now all the code on this forum page is changed, so you should copy all again.
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...