Sequential Grid-Based Movement

Get help using Construct 2

Post » Fri Aug 16, 2013 10:56 am

You could also use an older Pathfinding Plugin.

I couldn't upgrade my turn based grid game to the new pathfinding because I store every step of the path in an array. The new one only shows nodes at the turns. (still working in my game in r141.)

00Rez's plugin may do what you need.
http://www.scirra.com/forum/plugin-fast-path-finding-preview_topic50820_page1.html
B
233
S
62
G
33
Posts: 902
Reputation: 40,398

Post » Fri Aug 16, 2013 9:15 pm

@vee41,

Can you explain in more detail what happens in the loop? Specifically, the For loop that goes through pathfinding nodes.

I'm having some weird marker placements but my code is almost exactly the same as in your example.
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234

Post » Sat Aug 17, 2013 4:33 am

I haven't opened the capx but you should be able to get it to work without any path finding at all. I mean when you highlight all the possible moves for a piece you already have a number for how many spaces away it is from the piece.
So if you select a spot four spaces away you can select a spot three spaces away next to that. The process can be repeated to find all the grid positions in the path. So you end up with a list of positions that you could move to in sequence with the move to behavior.

At least that's a thought. I don't have access to c2 ATM to implement it.
B
92
S
32
G
109
Posts: 5,290
Reputation: 70,991

Post » Sat Aug 17, 2013 10:00 am

@R0J0hound, removing pathfinding would greatly ease up on CPU demands. I like this idea.

I'm actually probably going to scrap this overall thread idea and have the player only be able to select a square next to the currently selected object. This prevents suboptimal pathing when there are 2 or more ways to get to a certain square. It also gives the player full control of his objects.Excal2013-08-17 10:00:44
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234

Post » Mon Aug 19, 2013 3:48 am

Had a tinker with the idea and it does end up with an optimal move path. I also tested out some ideas i had for implementing turned based mechanics. It may have some use to give someone some ideas...
https://dl.dropboxusercontent.com/u/5426011/examples18/turn_based.capx
r139, 43 events

I agree moving your units one space at a time would give the player more control and could be a better control mechanic.
B
92
S
32
G
109
Posts: 5,290
Reputation: 70,991

Post » Fri Sep 13, 2013 8:21 am

@R0j0hound, I'm reviving this thread because the issue has come up again.

I resolved player movement by requiring the player to select squares one at a time. I also believe this is the most optimal thing to do, as multiple paths to a location definitely require player decision.

However, I am now working on implementing the AI for the computer player and need to discover how to move a computer object to a designated spot.



So here you see the computer has selected one of its objects and highlighted the possible moves for that object. There are two relevant global variables:



ComputerProgram is the UID of the object the computer has selected.
ComputerTarget is the UID of a player-controlled object that the current computer object is trying to get to.

It won't be hard for me to do a distance check and find out which highlighted square is closest to the target. However, I need a way to find out how the computer object is going to move there (what sequence of highlighted squares to move to). I fired up your .capx and found this section to be of great interest:



Could you go into a little more detail on how this works? I assume the default values for dirs is just for placeholder.Excal2013-09-13 08:22:38
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234

Post » Mon Sep 16, 2013 12:42 am

I'll try to figure out what is going on, but an explanation of implementation from someone who has implemented direction backtracking would be nice.Excal2013-09-16 00:42:49
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234

Post » Tue Sep 17, 2013 7:15 am



Added two new fields for the highlight function. Each highlighted square now shows how far away it is from the current active object, as well as which direction:

1 = left, 2 = right, 3 = up, 4 = down



In the above picture, the computer AI selects an object based on highest range, determines the closest player object to it, and sets it as the target. Then it highlights all available moves the program can move:



However, what it needs to do next is construct a direction list so it knows how to move from its current square to the square closest to the target.



I call the PathToDestination function earlier (scroll up).

However, my "dirs" variable always equals "" and I'm not sure why. Can someone take a look?

@vee41 maybe? I know, I ask you too much :(
@R0J0hound, my events are based on your example, except instead of creating the squares on the board and then moving, I highlight the squares, then assign distance and direction variables only to the highlighted squares.

BoardGame.capxExcal2013-09-17 07:20:16
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234

Post » Tue Sep 17, 2013 8:08 am

What i did is when creating the list of possible moves i stored the direction back to the previous move position. My capx kind of obscures the idea so here's a general idea of what I did.

Here's some ascii art to show the map and perhaps what values could be in the array. Note: i didn't use these values in my capx.
key:
. is empty
# is a wall
p is a player
[code]
.....
.##..
..#p.
.....
[/code]
So that's the map.

Next I marked the moves. The numbers show the distance away from the starting location.
[code]
.4323
.##12
.4#01
43212
[/code]
I think you already solved this one but here's my pseudo code for how i generated this.
[code]
mark the players position as 0
for i from 0 to 3
   loop though all positions marked with i
      for the four positions around each
        if it doesn't have a value and it's not a wall then
            set it's value to i+1
            set it's dir to the direction from the position with value i.
[/code]

So here is what the dirs could look like. There can be multiple paths with the same distance, but only one will be arbitrarily chosen.
[code]
.<<^>
.##^>
.v#p>
<<<vv
[/code]
In my code I used 0-3 instead so I could just multiply it by 90 and get an actual direction.

The backtracking is then just a matter of:
1 Starting a the destination
2 look up the direction at the current position.
3 save direction to a list.
4 follow direction backward one spot
5 If current position isn't the starting position then repeat at 2

Hope that helps a bit to show what I was doing... It may just be noise though.
B
92
S
32
G
109
Posts: 5,290
Reputation: 70,991

Post » Tue Sep 17, 2013 8:48 am

@R0J0hound

Thanks for the more detailed explanation.

I've gotten the direction and distance working, but it's building the backwards list that isn't working for me.



My dirs = "" at all times, and I'm not sure why.
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 30 guests