Let's start at the plot: You have 2 new members - m_pVillageOnPlot and m_pWorkingVillage.
A CvVillage can only be part of a single CvPlot, so you should write that village's data as part of the plot's data.
So in CvPlot::write:
Code:
if (m_pVillageOnPlot)
{
pStream->Write(true);
m_pVillageOnPlot->write(pStream);
}
else
{
pStream->Write(false);
}
And to read it first read a boolean of whether the village exists, and if so - create a new CvVillage and read it. Don't forget to delete the existing one first (use SAFE_DELETE for that) - whether you create a new one or not.
Now, m_pWorkingVillage is trickier, since it might point to another plot's village, so you don't want to save the data again (you don't want to create a copy of the village), and you can't write pointers to the stream (because they will be meaningless after you load it).
In the rest of Civ's code it is done with ID's - each city/unit/whatever has an int ID. In your case, I suggest using the X,Y coordinates of the plot which village is working yours.
Now, since you might read a worked plot before the working plot, that means that you can't immediately point to the village. You can, however, point immediately to the plot since all plots are first allocated and only the read (see CvMap::read). So I suggest that each plot will keep a 'working village plot' of type CvPlot* instead of a 'working village'. You can still access the working village directly (
m_pWorkingVillagePlot->getVillage(), or something), and it'll save you the need to make another pass over the plots.
It also means that you only need to save the plot's index, and not the two coordinates.
Then in your CvPlot::write():
Code:
if (m_pWorkingVillagePlot)
{
int nWorkingVillagePlotIndex = GC.getMap().plotNumINLINE(m_pWorkingVillagePlot->getX_INLINE(), m_pWorkingVillagePlot->getY_INLINE());
pStream->Write(nWorkingVillagePlotIndex );;
}
else
{
pStream->Write(-1);
}
And in your CvPlot::read():
Code:
pStream->Read(&nWorkingVillagePlotIndex);
if (nWorkingVillagePlotIndex >= 0)
{
m_pWorkingVillagePlot = GC.getMap().plotByIndexINLINE(nWorkingVillagePlotIndex);
}
else
{
m_pWorkingVillagePlot = NULL;
}
As for the CvVillage code, I didn't go over everything there, but a few points:
In the constructor:
Code:
CvPlot** m_ppWorkingPlots = new CvPlot*[4];
That defines a local variable and allocates memory into it. The pointer will disappear and create a memory leak when you leave the constructor, and more importantly - it will not assign anything to the class member. Remove the "CvPlot**" in the beginning.
BTW - why 4?
Code:
setPlotWorked(AI_getBestPlot());
setPlotWorked(AI_getBestPlot(AI_getBestPlot()));
Did you give default values to the parameters of AI_getBestPlot? Otherwise it will not compile.
---
You should really create a destructor for CvVillage in which you release all the memory you allocate in the class, otherwise you have a memory leak.
---
There's no need to read/write or even hold the x/y coordinates of the plot in CvVillage since you hold a pointer directly to the plot.
---
In CvVillage::write:
Code:
pStream->Write(m_piYields);
This should be:
Code:
pStream->Write([B]NUM_YIELD_TYPES[/B], m_piYields);
---
And can you please explain what the village is supposed to do?