Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Good way to calculate unit possible moves. (Tile)  (Read 2019 times)
0 Members and 1 Guest are viewing this topic.
Offline SpuTTer

Senior Member


Medals: 1


Lazy Middle Class Intellectual


« Posted 2004-04-19 06:12:48 »

I've got a tile based map and I'd like to get a decent method to calculate all the possible squares that the  unit may move it.

Lets say that the unit can move 5 spaces.

Each tile has a movement 'cost'. So some tiles are harder than others to pass through.

Im trying to come up with a good method so that if you click on the unit, it will show all the possible locations the player can move.

I've been searching and I keep findind pathfinding A to B algorhythms, but nothing as general as what Im talking about.

I guess I could use A star and calculate the current player square vs every square on the map, but that seems wasteful.

I was trying to figure it out without posting here, but oh well. Hopefully someone else has done this before!

Thanks in advance!

Sacramento Volleyball
"Whitty phrase goes here."
Offline kevglass

JGO Kernel


Medals: 121
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2004-04-19 06:43:24 »

You might want to try implementing a "dijkstra search", search google for it.

I've used it in the past with really good results in comparison to A*. However, you should also be able to use A* with a cap to work out just squares in a certain region.

A* code from Cas: http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=share;action=display;num=1037669044

Kev

Offline digitprop

Junior Member





« Reply #2 - Posted 2004-04-19 09:23:46 »

I would suggest the following (correct me if this is stupid): Use a recursive algorithm, always considering the four (or eight) tiles adjacent to the current one. Determine for each if the player can move onto it, and the remaining available 'movement points', based on the movement costs for the tile. Store the movement points for the tiles in a 2D array.

Then, do the same for each of the adjacent tiles as center tile. Keep track of already visited tiles, and replace the remaining 'movement points' for a tile if the already stored value is higher than the currently calculated value (in that case, you have discovered a cheaper route to the same tile).

If the 'movement points' for a tile change, make sure to recalculate all adjacent tiles (you may want to keep track of unvisited / to be recalculated tiles in a list), because their accessibility may have changed, too.

This may sound like a lot of processing, but in reality - depending on the costs of movement and the initially available movement points - the number of reachable tiles (and thus recursions) should be quite low.

M. Fischer . www.digitprop.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SpuTTer

Senior Member


Medals: 1


Lazy Middle Class Intellectual


« Reply #3 - Posted 2004-04-19 16:40:13 »

Thanks to both of you. I had seen Cas' code, and researched the "dijkstra Algorhythm", but I wasnt sure if that would be applied to what I was doing becasue everyone always mentioned it for Point A->B.

I appreciate your idea as well digitprop. I was considering doing something similar, but wasnt sure if it was extremely inefficient.

More input appreciated. I will research Cas' code some more and look into doing my own method.

Sacramento Volleyball
"Whitty phrase goes here."
Offline D.t.O

Junior Member




Psych'd about Java Games


« Reply #4 - Posted 2004-04-20 05:11:55 »

Quote
I appreciate your idea as well digitprop. I was considering doing something similar, but wasnt sure if it was extremely inefficient.

It does sound inefficient at first, but it really depends on how far your unit is going to move. If the average move is, like in your example, only 5 spaces, then a simple KISS-style recursive algorithm should do the job. Depth first search (which I think this algorithm is) has, indeed, an exponential order of growth in terms of speed (not too much space is used though). Nevertheless, just like in the traditional Queens problem, the number of choices (again, this depends on how far a unit can move and how expensive the tiles are) will be relatively low at each step.

Now it sounds like I'm repeating what digitprop said. I guess I'm trying to say that Depth first search is not too bad if the number of choices dramatically decreases at some point.


EDIT: add some stuff:
Also, I don't think speed should matter too much, although it grows exponentially. Computers these days are relatively fast Smiley.
I think the only major problem you could encouter using this (or any) recursive approach is getting too big of a method call stack.

Enjoy.
Regards,
     - D.t.O
Offline SpuTTer

Senior Member


Medals: 1


Lazy Middle Class Intellectual


« Reply #5 - Posted 2004-04-20 06:48:28 »

Thanks for your input. Im  thinking I'm going to try to implement something like you and digit are talking about. I'd rather have something easy and that I can completely understand rather than a complex algorhythm, at least for now.

Thanks for the tips.

Sacramento Volleyball
"Whitty phrase goes here."
Offline digitprop

Junior Member





« Reply #6 - Posted 2004-04-20 11:28:01 »

I would keep it as simple as possible if it is not obviously a severe bottleneck. You could even make it pluggable via an interface, so that you can later replace the algorithm easily - if you find that it takes up too much time.

'Optimize last', as the XP people say.

M. Fischer . www.digitprop.com
Offline SpuTTer

Senior Member


Medals: 1


Lazy Middle Class Intellectual


« Reply #7 - Posted 2004-04-20 17:07:45 »

Yeah, that makes sense. I can always make it more robust later!!!

PS,

100th post ;o

Sacramento Volleyball
"Whitty phrase goes here."
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (14 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!