How to get the territory where the unit is?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get the territory where the unit is?

kmat
Hei

How do I get the territory where the unit is. Do I have to go through all the territories to a player and check where the unit is, or is there a method for it? I could not find any methods for it in the Unit, TripleAUnit, GameMap and Territory classes.
Reply | Threaded
Open this post in threaded view
|

Re: How to get the territory where the unit is?

ComradeKev
Administrator
The closest thing I can find is a Match that tells if a collectino of units is in a specified territory...

Collection<Unit> unitsInTerr = Match.getMatches(units, Matches.unitIsInTerritory(territory));

You could get all the territories in the map and iterate through them determining if your collection of units is in each one.  It's not the best solution, but it's workable.

If it were me, I'd probably try to write a new Match criteria from which I could get the answer.  Though I don't know offhand how I'd approach it.

Kev
If emailing me at ComradeKev at yahoo.com , please add TripleA to the subject line
Reply | Threaded
Open this post in threaded view
|

Re: How to get the territory where the unit is?

darune
I ran it this problem as well and needed more than one lookup. (given some units, where are they located)

What i did was to create a HashMap<Unit, Territory> with all the units on the map. This is more efficient if you need more than just a few lookups. Naturally the hashmap has to be recreated (or maintained) once the game state changes (not a big issue in my case).