Sequential Grid-Based Movement

0 favourites
From the Asset Store
Snap to visible grid - perfect solution for any game genre
  • 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.

    <img src="http://i.imgur.com/2ZUNnrW.png" border="0" />

    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:

    <img src="http://i.imgur.com/fp2igWg.png" border="0" />

    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:

    <img src="http://i.imgur.com/GGOqrDu.png" border="0" />

    Could you go into a little more detail on how this works? I assume the default values for dirs is just for placeholder.

  • 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.

  • <img src="http://i.imgur.com/3VVk1VC.png" border="0">

    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

    <img src="http://i.imgur.com/Ifco3i7.png" border="0">

    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:

    <img src="http://i.imgur.com/bEW1WLa.png" border="0">

    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.

    <img src="http://i.imgur.com/SIRGp3P.png" border="0">

    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.capx

  • 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

    .....
    .##..
    ..#p.
    .....
    

    So that's the map.

    Next I marked the moves. The numbers show the distance away from the starting location.

    .4323
    .##12
    .4#01
    43212
    

    I think you already solved this one but here's my pseudo code for how i generated this.

    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.
    

    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.

    .<<^>
    .##^>
    .v#p>
    <<<vv
    

    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.

  • 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.

    <img src="http://i.imgur.com/SIRGp3P.png" border="0" />

    My dirs = "" at all times, and I'm not sure why.

  • My issue lies in the backtracking, as I'm not sure what exactly the round(cos(90)) stuff is doing. Could you elaborate a bit more on that part?

    R0J0hound

  • I've done some more testing with the debugger. The problem isn't the backtracking - that part should be working. However, for some odd reason my string isn't being updated properly.

    <img src="http://i.imgur.com/b5paFcp.png" border="0">

    My dirs variable is a local static text.

    The PathToDestination function is only called once due to a 'trigger once' clause. Any idea why my string is always empty?

    BoardGame.capx

  • Here is a quick debug 101 for problems like this, or how I would approach finding out what is wrong.

    First, add log messages. This way you can easily see what is happening during runtime (the debugger has tool that can do similar stuff, but I am a bit old school). These log messages are displayed in browsers log and tell you what exactly is going on in your variables.

    <img src="https://dl.dropboxusercontent.com/u/19921470/debug2.PNG" border="0" />

    Here is the output:

    <img src="https://dl.dropboxusercontent.com/u/19921470/debug.PNG" border="0" />

    This suggests your problem is related to the second comparison in the event.

    Another thing: It seems you use separate functions (copy pasted?) for AI and player movement. It's generally bad practice to copy paste same code to two places, and you should look for a way to use same functions for AI and player programs if they do the same thing.

    And also; commenting your events is very, very useful when you come back to your project after two week vacation or show it off to people in the forums :P. I generally write short comment above every function about what it should do, what do the input variables mean and describe any return variables. At least for me this is a must, that way I don't have to spend anytime wondering what these events do :)

    Hope this helps, your game keeps getting better every time I see it!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Here is a quick debug 101 for problems like this, or how I would approach finding out what is wrong.

    First, add log messages. This way you can easily see what is happening during runtime (the debugger has tool that can do similar stuff, but I am a bit old school). These log messages are displayed in browsers log and tell you what exactly is going on in your variables.

    <img src="https://dl.dropboxusercontent.com/u/19921470/debug2.PNG" border="0" />

    I assume you mean debugger breakpoints? I have no idea how to set those, as I assumed they would be under the System object but couldn't find any breakpoint events :(

    Here is the output:

    <img src="https://dl.dropboxusercontent.com/u/19921470/debug.PNG" border="0" />

    This suggests your problem is related to the second comparison in the event.

    Another thing: It seems you use separate functions (copy pasted?) for AI and player movement. It's generally bad practice to copy paste same code to two places, and you should look for a way to use same functions for AI and player programs if they do the same thing.

    Yes, I use separate functions for AI and player movement. The main reasoning is that the player movement function checks for different things than the computer movement one. The computer movement function also has more parameters due to the AI needing more instructions for movement and targeting, etc.

    UPDATE: No idea why there is an error with the function call. Both functions appear to be working perfectly for highlighting movement.

    And also; commenting your events is very, very useful when you come back to your project after two week vacation or show it off to people in the forums :P. I generally write short comment above every function about what it should do, what do the input variables mean and describe any return variables. At least for me this is a must, that way I don't have to spend anytime wondering what these events do :)

    Almost everything in the project is commented. Some of the newer stuff that I'm trying to debug isn't, but I usually add comments once things are fixed up.

  • vee41

    I can't bookmark your post (wish I could), so just commenting so I can search it later.

    Brilliant approach to debugging. Learning so much from this project.

    Excal

    Browser - log - messages (edit) see vee41 below

    Hit F12 in chrome ;)

  • I assume you mean debugger breakpoints? I have no idea how to set those, as I assumed they would be under the System object but couldn't find any breakpoint events :(

    Right click on event block and you can toggle breakpoints. I still prefer Browser -> log messages though :)

    The error in function call is because no events are ran (comparisons prevent that). It's normal, no reason to get worried :)

  • The functions that trigger a console warning are the player highlighting function, computer highlighting function, and building a direction list.

    One thing about all three functions - they are recursive and call themselves until 'stop conditions' are met. However, they appear to be working perfectly, even if Chrome gives me a warning.

    As to the actual issue of the local static text 'dirs' always equaling "", I think I'm going to just make it global and see if the scope change solves the issue.

  • The functions that trigger a console warning are the player highlighting function, computer highlighting function, and building a direction list.

    One thing about all three functions - they are recursive and call themselves until 'stop conditions' are met. However, they appear to be working perfectly, even if Chrome gives me a warning.

    As to the actual issue of the local static text 'dirs' always equaling "", I think I'm going to just make it global and see if the scope change solves the issue.

    Like I demonstrated in the screenshots, the scope is not the issue. It is the comparison. And those log errors with functions are normal, they appear as no events are ran because comparisons at the start are false.

  • The comparison?

    The BoardSpaces condition?

  • Yup, the UID of initial boardspace matches the one at boardspaces xi yi. Thus, no more calls.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)