No plotIndex() method?

Baldyr

"Hit It"
Joined
Dec 5, 2009
Messages
5,530
Location
Sweden
I'm come to believe that plot IDs are the way to reference map plots - instead of using coordinates (in a tuple) or CyPlot instances. But there is one awkwardness associated with this, namely the lack of a plotIndex() method that takes a CyPlot instance as a parameter and returns the plot index number. There is, however, a CyCity.getCityPlotIndex() that makes no sense, since it still need the CyPlot instance as a parameter. So you'd use it like this:
Code:
pCity.getCityPlotIndex(pCity.plot())
:crazyeye:

So I should basically just invoke the CyCity method on a dummy CyCity instance whenever I need to fetch the plot ID of a CyPlot? :rolleyes:
Code:
CyCity().getCityPlotIndex(pPlot)
And for the record, I know about the CyMap.plot(), CyMap.plotByIndex() and CyMap.plotNum() methods. These are of course very helpful, but there really should be a CyPlot.getID() method also!

And while we're on the subject: What are CyMap.sPlot() and CyMap.sPlotByIndex()? It says that the s stands for "static" but what does it mean? :confused:
 
There are 2 different indexes here:

The one from CyMap.plotByIndex() and the like - This is an index in the map's array of plots. I guess that the reason that you don't have a getIndex() method on the plot itself is that this information belongs to the map, and is not an actual property of the plot. The same way that you don't have a getMap() for the plot.

The second index is the one of the city - where index 0 means the city plot itself, and the other 1..20 are ordered around the city.
This is a different number than the first type of index, and it's unique for the city. If you try sending a plot to a city for which the plot is not in the BFC - you'll get index -1. So you do need the actual city instance (and not just creating one with CyCity()).

This is how it's defined:
Code:
	int aaiXYCityPlot[CITY_PLOTS_DIAMETER][CITY_PLOTS_DIAMETER] =
	{
		{-1, 17, 18, 19, -1,},

		{16, 6, 7, 8, 20,},

		{15, 5, 0, 1, 9,},

		{14, 4, 3, 2, 10,},

		{-1, 13, 12, 11, -1,}
	};

'static' has a few meanings in C++, but in this case it means that the variable you get (of type CyPlot) is the same one - you do not create a new plot, but assign a new value to a single CyPlot value.

If, for example, you'll do this:
Code:
plot1 = CyMap().sPlot(1,1)
plot2 = CyMap().sPlot(2,2)

Then both plot1 and plot2 will actually point to the same plot, which is plot (2,2).
 
Ok, so I'm basically stuck at this:
Code:
CyMap().plotNum(pPlot.getX(), pPlot.getY())
Just wrap it up in an function and accept it, I guess.

But could you perhaps explain the static thing again, because I'm not getting it. What do I use with, say, the CyMap.sPlot() method? Why use a static thingy? :confused:
 
Ok, so I'm basically stuck at this:
Code:
CyMap().plotNum(pPlot.getX(), pPlot.getY())
Just wrap it up in an function and accept it, I guess.

This function converts the X,Y coordinates to a plot index in the map's plots array. Was that what you asked? Because I'm not sure I understand the question.

But could you perhaps explain the static thing again, because I'm not getting it. What do I use with, say, the CyMap.sPlot() method? Why use a static thingy? :confused:

Let's say for example, that you have a function which goes over all the plots in the map.
You could do it this way:
PHP:
map = CyMap()
for i in range(map.numPlots()):
    plot = map.plotByIndex(i)
    # bla bla bla

Then each time you call plotByIndex(), a new plot is allocated in memory, and will be freed sometime in the future.
This could sum up to a lot of memory, but you don't actually need this. You only need one plot at a time. Using sPlotByIndex() here will not cause any new memory allocation.
 
Ok, that cleared things up. Thanks! :goodjob:
 
But there is one awkwardness associated with this, namely the lack of a plotIndex() method that takes a CyPlot instance as a parameter and returns the plot index number. There is, however, a CyCity.getCityPlotIndex() thatmakes no sense, since it still need the CyPlot instance as a parameter.
Here you lost me. You have a pointer to a CyCity instance & and another to a CyPlot instance of a plot which belongs to this city.
Code:
iPlot = pCity.getCityPlotIndex(pPlot)
What's wrong with this?
 
Here you lost me. You have a pointer to a CyCity instance & and another to a CyPlot instance of a plot which belongs to this city.
Code:
iPlot = pCity.getCityPlotIndex(pPlot)
What's wrong with this?
I can't use it to fetch the plot index (int) of any CyPlot object. See what Asaf said - the CyCity.getCityPlotIndex() method doesn't return a regular plot index but rather a city specific one, where all the plots within the city's reach are indexed 0-20.
 
Of course "CityPlotIndex" doesn't work with plots which don't belong to any city. And it is indeed city specific.
But nevertheless very handy, the spiral is easy to memorize (eg. when searching around a known city for generated units :D) and the scalar index can be used comfortably in loops ...
(I saw #132 & #133 of cycity always as a pair)
 
Of course "CityPlotIndex" doesn't work with plots which don't belong to any city. And it is indeed city specific.
But nevertheless very handy, the spiral is easy to memorize (eg. when searching around a known city for generated units :D) and the scalar index can be used comfortably in loops ...

All true, unless you need a global index and not a city-specific index...
 
The reason why I'm convinced that the plot ID is the way to go, by the way, is that you can pickle integers, while you can't pickle CyPlot instances (or any other objects inherited from the C++ code). So instead of using a pair of plot coordinates to store plot information - or to convert between plot ID and CyPlot instance before and after pickling/unpickling - you can just use the integer index as the default and convert to CyPlot on-the-fly whenever you need one.
 
All true, unless you need a global index and not a city-specific index...
You are right, 'til now I needed only the city-specific one, love it, and just saw the opening post attributing it near 'useless' ...
A global index beyond city borders would be of course handy, too. (Just I missed to need it it till now)
iPlot = CyMap.getMapPlotIndex(pPlot),
maybe getAreaPlotIndex(pPlot) ??
 
I agree that pickling one number is better than two, but you can simply pickle the x,y coordinates. That is easy and should be sufficient.
 
Back
Top Bottom