12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451 |
- //
- // FILE: CvMap.cpp
- // AUTHOR: Soren Johnson
- // PURPOSE: Game map class
- //-----------------------------------------------------------------------------
- // Copyright (c) 2004 Firaxis Games, Inc. All rights reserved.
- //-----------------------------------------------------------------------------
- //
- #include "CvGameCoreDLL.h"
- #include "CvMap.h"
- #include "CvCity.h"
- #include "CvPlotGroup.h"
- #include "CvGlobals.h"
- #include "CvGameAI.h"
- #include "CvPlayerAI.h"
- #include "CvRandom.h"
- #include "CvGameCoreUtils.h"
- #include "CvFractal.h"
- #include "CvPlot.h"
- #include "CvGameCoreUtils.h"
- #include "CvMap.h"
- #include "CvMapGenerator.h"
- #include "FAStarNode.h"
- #include "CvInitCore.h"
- #include "CvInfos.h"
- #include "FProfiler.h"
- #include "CyArgsList.h"
- #include "CvDLLEngineIFaceBase.h"
- #include "CvDLLIniParserIFaceBase.h"
- #include "CvDLLFAStarIFaceBase.h"
- #include "CvDLLFAStarIFaceBase.h"
- #include "CvDLLPythonIFaceBase.h"
- // Public Functions...
- CvMap::CvMap()
- {
- CvMapInitData defaultMapData;
- m_paiNumBonus = NULL;
- m_paiNumBonusOnLand = NULL;
- m_pMapPlots = NULL;
- reset(&defaultMapData);
- }
- CvMap::~CvMap()
- {
- uninit();
- }
- // FUNCTION: init()
- // Initializes the map.
- // Parameters:
- // pInitInfo - Optional init structure (used for WB load)
- // Returns:
- // nothing.
- void CvMap::init(CvMapInitData* pInitInfo/*=NULL*/)
- {
- int iX, iY;
- PROFILE("CvMap::init");
- gDLL->logMemState( CvString::format("CvMap::init begin - world size=%s, climate=%s, sealevel=%s, num custom options=%6",
- GC.getWorldInfo(GC.getInitCore().getWorldSize()).getDescription(),
- GC.getClimateInfo(GC.getInitCore().getClimate()).getDescription(),
- GC.getSeaLevelInfo(GC.getInitCore().getSeaLevel()).getDescription(),
- GC.getInitCore().getNumCustomMapOptions()).c_str() );
- gDLL->getPythonIFace()->callFunction(gDLL->getPythonIFace()->getMapScriptModule(), "beforeInit");
- //--------------------------------
- // Init saved data
- reset(pInitInfo);
- //--------------------------------
- // Init containers
- m_areas.init();
- //--------------------------------
- // Init non-saved data
- setup();
- //--------------------------------
- // Init other game data
- gDLL->logMemState("CvMap before init plots");
- m_pMapPlots = new CvPlot[numPlotsINLINE()];
- for (iX = 0; iX < getGridWidthINLINE(); iX++)
- {
- gDLL->callUpdater();
- for (iY = 0; iY < getGridHeightINLINE(); iY++)
- {
- plotSorenINLINE(iX, iY)->init(iX, iY);
- }
- }
- calculateAreas();
- gDLL->logMemState("CvMap after init plots");
- }
- void CvMap::uninit()
- {
- SAFE_DELETE_ARRAY(m_paiNumBonus);
- SAFE_DELETE_ARRAY(m_paiNumBonusOnLand);
- SAFE_DELETE_ARRAY(m_pMapPlots);
- m_areas.uninit();
- }
- // FUNCTION: reset()
- // Initializes data members that are serialized.
- void CvMap::reset(CvMapInitData* pInitInfo)
- {
- int iI;
- //--------------------------------
- // Uninit class
- uninit();
- //
- // set grid size
- // initially set in terrain cell units
- //
- m_iGridWidth = (GC.getInitCore().getWorldSize() != NO_WORLDSIZE) ? GC.getWorldInfo(GC.getInitCore().getWorldSize()).getGridWidth (): 0; //todotw:tcells wide
- m_iGridHeight = (GC.getInitCore().getWorldSize() != NO_WORLDSIZE) ? GC.getWorldInfo(GC.getInitCore().getWorldSize()).getGridHeight (): 0;
- // allow grid size override
- if (pInitInfo)
- {
- m_iGridWidth = pInitInfo->m_iGridW;
- m_iGridHeight = pInitInfo->m_iGridH;
- }
- else
- {
- // check map script for grid size override
- if (GC.getInitCore().getWorldSize() != NO_WORLDSIZE)
- {
- std::vector<int> out;
- CyArgsList argsList;
- argsList.add(GC.getInitCore().getWorldSize());
- bool ok = gDLL->getPythonIFace()->callFunction(gDLL->getPythonIFace()->getMapScriptModule(), "getGridSize", argsList.makeFunctionArgs(), &out);
- if (ok && !gDLL->getPythonIFace()->pythonUsingDefaultImpl() && out.size() == 2)
- {
- m_iGridWidth = out[0];
- m_iGridHeight = out[1];
- FAssertMsg(m_iGridWidth > 0 && m_iGridHeight > 0, "the width and height returned by python getGridSize() must be positive");
- }
- }
- // convert to plot dimensions
- if (GC.getNumLandscapeInfos() > 0)
- {
- m_iGridWidth *= GC.getLandscapeInfo(GC.getActiveLandscapeID()).getPlotsPerCellX();
- m_iGridHeight *= GC.getLandscapeInfo(GC.getActiveLandscapeID()).getPlotsPerCellY();
- }
- }
- m_iLandPlots = 0;
- m_iOwnedPlots = 0;
- if (pInitInfo)
- {
- m_iTopLatitude = pInitInfo->m_iTopLatitude;
- m_iBottomLatitude = pInitInfo->m_iBottomLatitude;
- }
- else
- {
- // Check map script for latitude override (map script beats ini file)
- long resultTop = -1, resultBottom = -1;
- bool okX = gDLL->getPythonIFace()->callFunction(gDLL->getPythonIFace()->getMapScriptModule(), "getTopLatitude", NULL, &resultTop);
- bool overrideX = !gDLL->getPythonIFace()->pythonUsingDefaultImpl();
- bool okY = gDLL->getPythonIFace()->callFunction(gDLL->getPythonIFace()->getMapScriptModule(), "getBottomLatitude", NULL, &resultBottom);
- bool overrideY = !gDLL->getPythonIFace()->pythonUsingDefaultImpl();
- if (okX && okY && overrideX && overrideY && resultTop != -1 && resultBottom != -1)
- {
- m_iTopLatitude = resultTop;
- m_iBottomLatitude = resultBottom;
- }
- }
- m_iTopLatitude = std::min(m_iTopLatitude, 90);
- m_iTopLatitude = std::max(m_iTopLatitude, -90);
- m_iBottomLatitude = std::min(m_iBottomLatitude, 90);
- m_iBottomLatitude = std::max(m_iBottomLatitude, -90);
- m_iNextRiverID = 0;
- //
- // set wrapping
- //
- m_bWrapX = true;
- m_bWrapY = false;
- if (pInitInfo)
- {
- m_bWrapX = pInitInfo->m_bWrapX;
- m_bWrapY = pInitInfo->m_bWrapY;
- }
- else
- {
- // Check map script for wrap override (map script beats ini file)
- long resultX = -1, resultY = -1;
- bool okX = gDLL->getPythonIFace()->callFunction(gDLL->getPythonIFace()->getMapScriptModule(), "getWrapX", NULL, &resultX);
- bool overrideX = !gDLL->getPythonIFace()->pythonUsingDefaultImpl();
- bool okY = gDLL->getPythonIFace()->callFunction(gDLL->getPythonIFace()->getMapScriptModule(), "getWrapY", NULL, &resultY);
- bool overrideY = !gDLL->getPythonIFace()->pythonUsingDefaultImpl();
- if (okX && okY && overrideX && overrideY && resultX != -1 && resultY != -1)
- {
- m_bWrapX = (resultX != 0);
- m_bWrapY = (resultY != 0);
- }
- }
- if (GC.getNumBonusInfos())
- {
- FAssertMsg((0 < GC.getNumBonusInfos()), "GC.getNumBonusInfos() is not greater than zero but an array is being allocated in CvMap::reset");
- FAssertMsg(m_paiNumBonus==NULL, "mem leak m_paiNumBonus");
- m_paiNumBonus = new int[GC.getNumBonusInfos()];
- FAssertMsg(m_paiNumBonusOnLand==NULL, "mem leak m_paiNumBonusOnLand");
- m_paiNumBonusOnLand = new int[GC.getNumBonusInfos()];
- for (iI = 0; iI < GC.getNumBonusInfos(); iI++)
- {
- m_paiNumBonus[iI] = 0;
- m_paiNumBonusOnLand[iI] = 0;
- }
- }
- m_areas.removeAll();
- }
- // FUNCTION: setup()
- // Initializes all data that is not serialized but needs to be initialized after loading.
- void CvMap::setup()
- {
- PROFILE("CvMap::setup");
- gDLL->getFAStarIFace()->Initialize(&GC.getPathFinder(), getGridWidthINLINE(), getGridHeightINLINE(), isWrapXINLINE(), isWrapYINLINE(), pathDestValid, pathHeuristic, pathCost, pathValid, pathAdd, NULL, NULL);
- gDLL->getFAStarIFace()->Initialize(&GC.getInterfacePathFinder(), getGridWidthINLINE(), getGridHeightINLINE(), isWrapXINLINE(), isWrapYINLINE(), pathDestValid, pathHeuristic, pathCost, pathValid, pathAdd, NULL, NULL);
- gDLL->getFAStarIFace()->Initialize(&GC.getStepFinder(), getGridWidthINLINE(), getGridHeightINLINE(), isWrapXINLINE(), isWrapYINLINE(), stepDestValid, stepHeuristic, stepCost, stepValid, stepAdd, NULL, NULL);
- gDLL->getFAStarIFace()->Initialize(&GC.getRouteFinder(), getGridWidthINLINE(), getGridHeightINLINE(), isWrapXINLINE(), isWrapYINLINE(), NULL, NULL, NULL, routeValid, NULL, NULL, NULL);
- gDLL->getFAStarIFace()->Initialize(&GC.getBorderFinder(), getGridWidthINLINE(), getGridHeightINLINE(), isWrapXINLINE(), isWrapYINLINE(), NULL, NULL, NULL, borderValid, NULL, NULL, NULL);
- gDLL->getFAStarIFace()->Initialize(&GC.getAreaFinder(), getGridWidthINLINE(), getGridHeightINLINE(), isWrapXINLINE(), isWrapYINLINE(), NULL, NULL, NULL, areaValid, NULL, joinArea, NULL);
- gDLL->getFAStarIFace()->Initialize(&GC.getPlotGroupFinder(), getGridWidthINLINE(), getGridHeightINLINE(), isWrapXINLINE(), isWrapYINLINE(), NULL, NULL, NULL, plotGroupValid, NULL, countPlotGroup, NULL);
- }
- //////////////////////////////////////
- // graphical only setup
- //////////////////////////////////////
- void CvMap::setupGraphical()
- {
- if (!GC.IsGraphicsInitialized())
- return;
- if (m_pMapPlots != NULL)
- {
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- gDLL->callUpdater(); // allow windows msgs to update
- plotByIndexINLINE(iI)->setupGraphical();
- }
- }
- }
- void CvMap::erasePlots()
- {
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->erase();
- }
- }
- void CvMap::setRevealedPlots(TeamTypes eTeam, bool bNewValue, bool bTerrainOnly)
- {
- PROFILE_FUNC();
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->setRevealed(eTeam, bNewValue, bTerrainOnly, NO_TEAM, false);
- }
- GC.getGameINLINE().updatePlotGroups();
- }
- void CvMap::setAllPlotTypes(PlotTypes ePlotType)
- {
- //float startTime = (float) timeGetTime();
- for(int i=0;i<numPlotsINLINE();i++)
- {
- plotByIndexINLINE(i)->setPlotType(ePlotType, false, false);
- }
- recalculateAreas();
- //rebuild landscape
- gDLL->getEngineIFace()->RebuildAllPlots();
- //mark minimap as dirty
- gDLL->getEngineIFace()->SetDirty(MinimapTexture_DIRTY_BIT, true);
- gDLL->getEngineIFace()->SetDirty(GlobeTexture_DIRTY_BIT, true);
-
- //float endTime = (float) timeGetTime();
- //OutputDebugString(CvString::format("[Jason] setAllPlotTypes: %f\n", endTime - startTime).c_str());
- }
- // XXX generalize these funcs? (macro?)
- void CvMap::doTurn()
- {
- PROFILE("CvMap::doTurn()")
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->doTurn();
- }
- }
- void CvMap::updateFlagSymbols()
- {
- PROFILE_FUNC();
- CvPlot* pLoopPlot;
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- pLoopPlot = plotByIndexINLINE(iI);
- if (pLoopPlot->isFlagDirty())
- {
- pLoopPlot->updateFlagSymbol();
- pLoopPlot->setFlagDirty(false);
- }
- }
- }
- void CvMap::updateFog()
- {
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->updateFog();
- }
- }
- void CvMap::updateVisibility()
- {
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->updateVisibility();
- }
- }
- void CvMap::updateSymbolVisibility()
- {
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->updateSymbolVisibility();
- }
- }
- void CvMap::updateSymbols()
- {
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->updateSymbols();
- }
- }
- void CvMap::updateMinimapColor()
- {
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->updateMinimapColor();
- }
- }
- void CvMap::updateSight(bool bIncrement)
- {
- for (int iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->updateSight(bIncrement, false);
- }
- GC.getGameINLINE().updatePlotGroups();
- }
- void CvMap::updateIrrigated()
- {
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->updateIrrigated();
- }
- }
- void CvMap::updateCenterUnit()
- {
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->updateCenterUnit();
- }
- }
- void CvMap::updateWorkingCity()
- {
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->updateWorkingCity();
- }
- }
- void CvMap::updateMinOriginalStartDist(CvArea* pArea)
- {
- PROFILE_FUNC();
- CvPlot* pStartingPlot;
- CvPlot* pLoopPlot;
- int iDist;
- int iI, iJ;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- pLoopPlot = plotByIndexINLINE(iI);
- if (pLoopPlot->area() == pArea)
- {
- pLoopPlot->setMinOriginalStartDist(-1);
- }
- }
- for (iI = 0; iI < MAX_CIV_PLAYERS; iI++)
- {
- pStartingPlot = GET_PLAYER((PlayerTypes)iI).getStartingPlot();
- if (pStartingPlot != NULL)
- {
- if (pStartingPlot->area() == pArea)
- {
- for (iJ = 0; iJ < numPlotsINLINE(); iJ++)
- {
- pLoopPlot = plotByIndexINLINE(iJ);
- if (pLoopPlot->area() == pArea)
- {
-
- //iDist = GC.getMapINLINE().calculatePathDistance(pStartingPlot, pLoopPlot);
- iDist = stepDistance(pStartingPlot->getX_INLINE(), pStartingPlot->getY_INLINE(), pLoopPlot->getX_INLINE(), pLoopPlot->getY_INLINE());
- if (iDist != -1)
- {
- //int iCrowDistance = plotDistance(pStartingPlot->getX_INLINE(), pStartingPlot->getY_INLINE(), pLoopPlot->getX_INLINE(), pLoopPlot->getY_INLINE());
- //iDist = std::min(iDist, iCrowDistance * 2);
- if ((pLoopPlot->getMinOriginalStartDist() == -1) || (iDist < pLoopPlot->getMinOriginalStartDist()))
- {
- pLoopPlot->setMinOriginalStartDist(iDist);
- }
- }
- }
- }
- }
- }
- }
- }
- void CvMap::updateYield()
- {
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->updateYield();
- }
- }
- void CvMap::verifyUnitValidPlot()
- {
- for (int iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->verifyUnitValidPlot();
- }
- }
- void CvMap::combinePlotGroups(PlayerTypes ePlayer, CvPlotGroup* pPlotGroup1, CvPlotGroup* pPlotGroup2)
- {
- CLLNode<XYCoords>* pPlotNode;
- CvPlotGroup* pNewPlotGroup;
- CvPlotGroup* pOldPlotGroup;
- CvPlot* pPlot;
- FAssertMsg(pPlotGroup1 != NULL, "pPlotGroup is not assigned to a valid value");
- FAssertMsg(pPlotGroup2 != NULL, "pPlotGroup is not assigned to a valid value");
- if (pPlotGroup1 == pPlotGroup2)
- {
- return;
- }
- if (pPlotGroup1->getLengthPlots() > pPlotGroup2->getLengthPlots())
- {
- pNewPlotGroup = pPlotGroup1;
- pOldPlotGroup = pPlotGroup2;
- }
- else
- {
- pNewPlotGroup = pPlotGroup2;
- pOldPlotGroup = pPlotGroup1;
- }
- pPlotNode = pOldPlotGroup->headPlotsNode();
- while (pPlotNode != NULL)
- {
- pPlot = plotSorenINLINE(pPlotNode->m_data.iX, pPlotNode->m_data.iY);
- pNewPlotGroup->addPlot(pPlot);
- pPlotNode = pOldPlotGroup->deletePlotsNode(pPlotNode);
- }
- }
- CvPlot* CvMap::syncRandPlot(int iFlags, int iArea, int iMinUnitDistance, int iTimeout)
- {
- CvPlot* pPlot;
- CvPlot* pTestPlot;
- CvPlot* pLoopPlot;
- bool bValid;
- int iCount;
- int iDX, iDY;
- pPlot = NULL;
- iCount = 0;
- while (iCount < iTimeout)
- {
- pTestPlot = plotSorenINLINE(GC.getGameINLINE().getSorenRandNum(getGridWidthINLINE(), "Rand Plot Width"), GC.getGameINLINE().getSorenRandNum(getGridHeightINLINE(), "Rand Plot Height"));
- FAssertMsg(pTestPlot != NULL, "TestPlot is not assigned a valid value");
- if ((iArea == -1) || (pTestPlot->getArea() == iArea))
- {
- bValid = true;
- if (bValid)
- {
- if (iMinUnitDistance != -1)
- {
- for (iDX = -(iMinUnitDistance); iDX <= iMinUnitDistance; iDX++)
- {
- for (iDY = -(iMinUnitDistance); iDY <= iMinUnitDistance; iDY++)
- {
- pLoopPlot = plotXY(pTestPlot->getX_INLINE(), pTestPlot->getY_INLINE(), iDX, iDY);
- if (pLoopPlot != NULL)
- {
- if (pLoopPlot->isUnit())
- {
- bValid = false;
- }
- }
- }
- }
- }
- }
- if (bValid)
- {
- if (iFlags & RANDPLOT_LAND)
- {
- if (pTestPlot->isWater())
- {
- bValid = false;
- }
- }
- }
- if (bValid)
- {
- if (iFlags & RANDPLOT_UNOWNED)
- {
- if (pTestPlot->isOwned())
- {
- bValid = false;
- }
- }
- }
- if (bValid)
- {
- if (iFlags & RANDPLOT_ADJACENT_UNOWNED)
- {
- if (pTestPlot->isAdjacentOwned())
- {
- bValid = false;
- }
- }
- }
- if (bValid)
- {
- if (iFlags & RANDPLOT_ADJACENT_LAND)
- {
- if (!(pTestPlot->isAdjacentToLand()))
- {
- bValid = false;
- }
- }
- }
- if (bValid)
- {
- if (iFlags & RANDPLOT_PASSIBLE)
- {
- if (pTestPlot->isImpassable())
- {
- bValid = false;
- }
- }
- }
- if (bValid)
- {
- if (iFlags & RANDPLOT_NOT_VISIBLE_TO_CIV)
- {
- if (pTestPlot->isVisibleToCivTeam())
- {
- bValid = false;
- }
- }
- }
- if (bValid)
- {
- if (iFlags & RANDPLOT_NOT_CITY)
- {
- if (pTestPlot->isCity())
- {
- bValid = false;
- }
- }
- }
- if (bValid)
- {
- pPlot = pTestPlot;
- break;
- }
- }
- iCount++;
- }
- return pPlot;
- }
- CvCity* CvMap::findCity(int iX, int iY, PlayerTypes eOwner, TeamTypes eTeam, bool bSameArea, bool bCoastalOnly, TeamTypes eTeamAtWarWith, DirectionTypes eDirection, CvCity* pSkipCity)
- {
- PROFILE_FUNC();
- CvCity* pLoopCity;
- CvCity* pBestCity;
- int iValue;
- int iBestValue;
- int iLoop;
- int iI;
- // XXX look for barbarian cities???
- iBestValue = MAX_INT;
- pBestCity = NULL;
- for (iI = 0; iI < MAX_PLAYERS; iI++)
- {
- if (GET_PLAYER((PlayerTypes)iI).isAlive())
- {
- if ((eOwner == NO_PLAYER) || (iI == eOwner))
- {
- if ((eTeam == NO_TEAM) || (GET_PLAYER((PlayerTypes)iI).getTeam() == eTeam))
- {
- for (pLoopCity = GET_PLAYER((PlayerTypes)iI).firstCity(&iLoop); pLoopCity != NULL; pLoopCity = GET_PLAYER((PlayerTypes)iI).nextCity(&iLoop))
- {
- if (!bSameArea || (pLoopCity->area() == plotINLINE(iX, iY)->area()) || (bCoastalOnly && (pLoopCity->waterArea() == plotINLINE(iX, iY)->area())))
- {
- if (!bCoastalOnly || pLoopCity->isCoastal(GC.getMIN_WATER_SIZE_FOR_OCEAN()))
- {
- if ((eTeamAtWarWith == NO_TEAM) || atWar(GET_PLAYER((PlayerTypes)iI).getTeam(), eTeamAtWarWith))
- {
- if ((eDirection == NO_DIRECTION) || (estimateDirection(dxWrap(pLoopCity->getX_INLINE() - iX), dyWrap(pLoopCity->getY_INLINE() - iY)) == eDirection))
- {
- if ((pSkipCity == NULL) || (pLoopCity != pSkipCity))
- {
- iValue = plotDistance(iX, iY, pLoopCity->getX_INLINE(), pLoopCity->getY_INLINE());
- if (iValue < iBestValue)
- {
- iBestValue = iValue;
- pBestCity = pLoopCity;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- return pBestCity;
- }
- CvSelectionGroup* CvMap::findSelectionGroup(int iX, int iY, PlayerTypes eOwner, bool bReadyToSelect, bool bWorkers)
- {
- CvSelectionGroup* pLoopSelectionGroup;
- CvSelectionGroup* pBestSelectionGroup;
- int iValue;
- int iBestValue;
- int iLoop;
- int iI;
- // XXX look for barbarian cities???
- iBestValue = MAX_INT;
- pBestSelectionGroup = NULL;
- for (iI = 0; iI < MAX_PLAYERS; iI++)
- {
- if (GET_PLAYER((PlayerTypes)iI).isAlive())
- {
- if ((eOwner == NO_PLAYER) || (iI == eOwner))
- {
- for(pLoopSelectionGroup = GET_PLAYER((PlayerTypes)iI).firstSelectionGroup(&iLoop); pLoopSelectionGroup != NULL; pLoopSelectionGroup = GET_PLAYER((PlayerTypes)iI).nextSelectionGroup(&iLoop))
- {
- if (!bReadyToSelect || pLoopSelectionGroup->readyToSelect())
- {
- if (!bWorkers || pLoopSelectionGroup->hasWorker())
- {
- iValue = plotDistance(iX, iY, pLoopSelectionGroup->getX(), pLoopSelectionGroup->getY());
- if (iValue < iBestValue)
- {
- iBestValue = iValue;
- pBestSelectionGroup = pLoopSelectionGroup;
- }
- }
- }
- }
- }
- }
- }
- return pBestSelectionGroup;
- }
- CvArea* CvMap::findBiggestArea(bool bWater)
- {
- CvArea* pLoopArea;
- CvArea* pBestArea;
- int iValue;
- int iBestValue;
- int iLoop;
- iBestValue = 0;
- pBestArea = NULL;
- for(pLoopArea = firstArea(&iLoop); pLoopArea != NULL; pLoopArea = nextArea(&iLoop))
- {
- if (pLoopArea->isWater() == bWater)
- {
- iValue = pLoopArea->getNumTiles();
- if (iValue > iBestValue)
- {
- iBestValue = iValue;
- pBestArea = pLoopArea;
- }
- }
- }
- return pBestArea;
- }
- int CvMap::getMapFractalFlags()
- {
- int wrapX = 0;
- if (isWrapXINLINE())
- {
- wrapX = (int)CvFractal::FRAC_WRAP_X;
- }
- int wrapY = 0;
- if (isWrapYINLINE())
- {
- wrapY = (int)CvFractal::FRAC_WRAP_Y;
- }
- return (wrapX | wrapY);
- }
- //"Check plots for wetlands or seaWater. Returns true if found"
- bool CvMap::findWater(CvPlot* pPlot, int iRange, bool bFreshWater)
- {
- PROFILE("CvMap::findWater()");
- CvPlot* pLoopPlot;
- int iDX, iDY;
- for (iDX = -(iRange); iDX <= iRange; iDX++)
- {
- for (iDY = -(iRange); iDY <= iRange; iDY++)
- {
- pLoopPlot = plotXY(pPlot->getX_INLINE(), pPlot->getY_INLINE(), iDX, iDY);
- if (pLoopPlot != NULL)
- {
- if (bFreshWater)
- {
- if (pLoopPlot->isFreshWater())
- {
- return true;
- }
- }
- else
- {
- if (pLoopPlot->isWater())
- {
- return true;
- }
- }
- }
- }
- }
- return false;
- }
- bool CvMap::isPlot(int iX, int iY) const
- {
- return isPlotINLINE(iX, iY);
- }
- int CvMap::numPlots() const
- {
- return numPlotsINLINE();
- }
- int CvMap::plotNum(int iX, int iY) const
- {
- return plotNumINLINE(iX, iY);
- }
- int CvMap::plotX(int iIndex) const
- {
- return (iIndex % getGridWidthINLINE());
- }
- int CvMap::plotY(int iIndex) const
- {
- return (iIndex / getGridWidthINLINE());
- }
- int CvMap::pointXToPlotX(float fX)
- {
- float fWidth, fHeight;
- gDLL->getEngineIFace()->GetLandscapeGameDimensions(fWidth, fHeight);
- return (int)(((fX + (fWidth/2.0f)) / fWidth) * getGridWidthINLINE());
- }
- float CvMap::plotXToPointX(int iX)
- {
- float fWidth, fHeight;
- gDLL->getEngineIFace()->GetLandscapeGameDimensions(fWidth, fHeight);
- return ((iX * fWidth) / ((float)getGridWidthINLINE())) - (fWidth / 2.0f) + (GC.getPLOT_SIZE() / 2.0f);
- }
- int CvMap::pointYToPlotY(float fY)
- {
- float fWidth, fHeight;
- gDLL->getEngineIFace()->GetLandscapeGameDimensions(fWidth, fHeight);
- return (int)(((fY + (fHeight/2.0f)) / fHeight) * getGridHeightINLINE());
- }
- float CvMap::plotYToPointY(int iY)
- {
- float fWidth, fHeight;
- gDLL->getEngineIFace()->GetLandscapeGameDimensions(fWidth, fHeight);
- return ((iY * fHeight) / ((float)getGridHeightINLINE())) - (fHeight / 2.0f) + (GC.getPLOT_SIZE() / 2.0f);
- }
- float CvMap::getWidthCoords()
- {
- return (GC.getPLOT_SIZE() * ((float)getGridWidthINLINE()));
- }
- float CvMap::getHeightCoords()
- {
- return (GC.getPLOT_SIZE() * ((float)getGridHeightINLINE()));
- }
- int CvMap::maxPlotDistance()
- {
- return std::max(1, plotDistance(0, 0, ((isWrapXINLINE()) ? (getGridWidthINLINE() / 2) : (getGridWidthINLINE() - 1)), ((isWrapYINLINE()) ? (getGridHeightINLINE() / 2) : (getGridHeightINLINE() - 1))));
- }
- int CvMap::maxStepDistance()
- {
- return std::max(1, stepDistance(0, 0, ((isWrapXINLINE()) ? (getGridWidthINLINE() / 2) : (getGridWidthINLINE() - 1)), ((isWrapYINLINE()) ? (getGridHeightINLINE() / 2) : (getGridHeightINLINE() - 1))));
- }
- int CvMap::getGridWidth() const
- {
- return getGridWidthINLINE();
- }
- int CvMap::getGridHeight() const
- {
- return getGridHeightINLINE();
- }
- int CvMap::getLandPlots()
- {
- return m_iLandPlots;
- }
- void CvMap::changeLandPlots(int iChange)
- {
- m_iLandPlots = (m_iLandPlots + iChange);
- FAssert(getLandPlots() >= 0);
- }
- int CvMap::getOwnedPlots()
- {
- return m_iOwnedPlots;
- }
- void CvMap::changeOwnedPlots(int iChange)
- {
- m_iOwnedPlots = (m_iOwnedPlots + iChange);
- FAssert(getOwnedPlots() >= 0);
- }
- int CvMap::getTopLatitude()
- {
- return m_iTopLatitude;
- }
- int CvMap::getBottomLatitude()
- {
- return m_iBottomLatitude;
- }
- int CvMap::getNextRiverID()
- {
- return m_iNextRiverID;
- }
- void CvMap::incrementNextRiverID()
- {
- m_iNextRiverID++;
- }
- bool CvMap::isWrapX()
- {
- return isWrapXINLINE();
- }
- bool CvMap::isWrapY()
- {
- return isWrapYINLINE();
- }
- bool CvMap::isWrap()
- {
- return isWrapINLINE();
- }
- WorldSizeTypes CvMap::getWorldSize()
- {
- return GC.getInitCore().getWorldSize();
- }
- ClimateTypes CvMap::getClimate()
- {
- return GC.getInitCore().getClimate();
- }
- SeaLevelTypes CvMap::getSeaLevel()
- {
- return GC.getInitCore().getSeaLevel();
- }
- int CvMap::getNumCustomMapOptions()
- {
- return GC.getInitCore().getNumCustomMapOptions();
- }
- CustomMapOptionTypes CvMap::getCustomMapOption(int iOption)
- {
- return GC.getInitCore().getCustomMapOption(iOption);
- }
- int CvMap::getNumBonuses(BonusTypes eIndex)
- {
- FAssertMsg(eIndex >= 0, "eIndex is expected to be non-negative (invalid Index)");
- FAssertMsg(eIndex < GC.getNumBonusInfos(), "eIndex is expected to be within maximum bounds (invalid Index)");
- return m_paiNumBonus[eIndex];
- }
- void CvMap::changeNumBonuses(BonusTypes eIndex, int iChange)
- {
- FAssertMsg(eIndex >= 0, "eIndex is expected to be non-negative (invalid Index)");
- FAssertMsg(eIndex < GC.getNumBonusInfos(), "eIndex is expected to be within maximum bounds (invalid Index)");
- m_paiNumBonus[eIndex] = (m_paiNumBonus[eIndex] + iChange);
- FAssert(getNumBonuses(eIndex) >= 0);
- }
- int CvMap::getNumBonusesOnLand(BonusTypes eIndex)
- {
- FAssertMsg(eIndex >= 0, "eIndex is expected to be non-negative (invalid Index)");
- FAssertMsg(eIndex < GC.getNumBonusInfos(), "eIndex is expected to be within maximum bounds (invalid Index)");
- return m_paiNumBonusOnLand[eIndex];
- }
- void CvMap::changeNumBonusesOnLand(BonusTypes eIndex, int iChange)
- {
- FAssertMsg(eIndex >= 0, "eIndex is expected to be non-negative (invalid Index)");
- FAssertMsg(eIndex < GC.getNumBonusInfos(), "eIndex is expected to be within maximum bounds (invalid Index)");
- m_paiNumBonusOnLand[eIndex] = (m_paiNumBonusOnLand[eIndex] + iChange);
- FAssert(getNumBonusesOnLand(eIndex) >= 0);
- }
- CvPlot* CvMap::plotByIndex(int iIndex) const
- {
- return plotByIndexINLINE(iIndex);
- }
- CvPlot* CvMap::plot(int iX, int iY) const
- {
- return plotINLINE(iX, iY);
- }
- CvPlot* CvMap::pointToPlot(float fX, float fY)
- {
- return plotINLINE(pointXToPlotX(fX), pointYToPlotY(fY));
- }
- int CvMap::getIndexAfterLastArea()
- {
- return m_areas.getIndexAfterLast();
- }
- int CvMap::getNumAreas()
- {
- return m_areas.getCount();
- }
- int CvMap::getNumLandAreas()
- {
- CvArea* pLoopArea;
- int iNumLandAreas;
- int iLoop;
- iNumLandAreas = 0;
- for(pLoopArea = GC.getMap().firstArea(&iLoop); pLoopArea != NULL; pLoopArea = GC.getMap().nextArea(&iLoop))
- {
- if (!(pLoopArea->isWater()))
- {
- iNumLandAreas++;
- }
- }
- return iNumLandAreas;
- }
- CvArea* CvMap::getArea(int iID)
- {
- return m_areas.getAt(iID);
- }
- CvArea* CvMap::addArea()
- {
- return m_areas.add();
- }
- void CvMap::deleteArea(int iID)
- {
- m_areas.removeAt(iID);
- }
- CvArea* CvMap::firstArea(int *pIterIdx, bool bRev)
- {
- return !bRev ? m_areas.beginIter(pIterIdx) : m_areas.endIter(pIterIdx);
- }
- CvArea* CvMap::nextArea(int *pIterIdx, bool bRev)
- {
- return !bRev ? m_areas.nextIter(pIterIdx) : m_areas.prevIter(pIterIdx);
- }
- void CvMap::recalculateAreas()
- {
- PROFILE("CvMap::recalculateAreas");
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->setArea(FFreeList::INVALID_INDEX);
- }
- m_areas.removeAll();
- calculateAreas();
- }
- void CvMap::resetPathDistance()
- {
- gDLL->getFAStarIFace()->ForceReset(&GC.getStepFinder());
- }
- // Super Forts begin *canal* *choke*
- int CvMap::calculatePathDistance(CvPlot *pSource, CvPlot *pDest, CvPlot *pInvalidPlot)
- // Super Forts end
- {
- FAStarNode* pNode;
- if (pSource == NULL || pDest == NULL)
- {
- return -1;
- }
- // Super Forts begin *canal* *choke*
- // 1 must be added because 0 is already being used as the default value for iInfo in GeneratePath()
- int iInvalidPlot = (pInvalidPlot == NULL) ? 0 : GC.getMapINLINE().plotNum(pInvalidPlot->getX_INLINE(), pInvalidPlot->getY_INLINE()) + 1;
- if (gDLL->getFAStarIFace()->GeneratePath(&GC.getStepFinder(), pSource->getX_INLINE(), pSource->getY_INLINE(), pDest->getX_INLINE(), pDest->getY_INLINE(), false, iInvalidPlot, true))
- // Super Forts end
- {
- pNode = gDLL->getFAStarIFace()->GetLastNode(&GC.getStepFinder());
- if (pNode != NULL)
- {
- return pNode->m_iData1;
- }
- }
- return -1; // no passable path exists
- }
- // Super Forts begin *canal* *choke*
- void CvMap::calculateCanalAndChokePoints()
- {
- int iI;
- for(iI = 0; iI < numPlotsINLINE(); iI++)
- {
- plotByIndexINLINE(iI)->calculateCanalValue();
- plotByIndexINLINE(iI)->calculateChokeValue();
- // TEMPORARY HARD CODE for testing purposes
- /*if((plotByIndexINLINE(iI)->getChokeValue() > 0) || (plotByIndexINLINE(iI)->getCanalValue() > 0))
- {
- ImprovementTypes eImprovement = (ImprovementTypes) (plotByIndexINLINE(iI)->isWater() ? GC.getInfoTypeForString("IMPROVEMENT_WHALING_BOATS") : GC.getInfoTypeForString("IMPROVEMENT_FORT"));
- plotByIndexINLINE(iI)->setImprovementType(eImprovement);
- }
- else
- {
- plotByIndexINLINE(iI)->setImprovementType(NO_IMPROVEMENT);
- }*/
- }
- }
- // Super Forts end
- /************************************************************************************************/
- /* BETTER_BTS_AI_MOD 08/21/09 jdog5000 */
- /* */
- /* Efficiency */
- /************************************************************************************************/
- // Plot danger cache
- void CvMap::invalidateIsActivePlayerNoDangerCache()
- {
- PROFILE_FUNC();
- int iI;
- CvPlot* pLoopPlot;
- for( iI = 0; iI < numPlotsINLINE(); iI++ )
- {
- pLoopPlot = GC.getMapINLINE().plotByIndexINLINE(iI);
- if( pLoopPlot != NULL )
- {
- pLoopPlot->setIsActivePlayerNoDangerCache(false);
- }
- }
- }
- void CvMap::invalidateIsTeamBorderCache(TeamTypes eTeam)
- {
- PROFILE_FUNC();
- int iI;
- CvPlot* pLoopPlot;
- for( iI = 0; iI < numPlotsINLINE(); iI++ )
- {
- pLoopPlot = GC.getMapINLINE().plotByIndexINLINE(iI);
- if( pLoopPlot != NULL )
- {
- pLoopPlot->setIsTeamBorderCache(eTeam, false);
- }
- }
- }
- /************************************************************************************************/
- /* BETTER_BTS_AI_MOD END */
- /************************************************************************************************/
- //
- // read object from a stream
- // used during load
- //
- void CvMap::read(FDataStreamBase* pStream)
- {
- CvMapInitData defaultMapData;
- // Init data before load
- reset(&defaultMapData);
- uint uiFlag=0;
- pStream->Read(&uiFlag); // flags for expansion
- pStream->Read(&m_iGridWidth);
- pStream->Read(&m_iGridHeight);
- pStream->Read(&m_iLandPlots);
- pStream->Read(&m_iOwnedPlots);
- pStream->Read(&m_iTopLatitude);
- pStream->Read(&m_iBottomLatitude);
- pStream->Read(&m_iNextRiverID);
- pStream->Read(&m_bWrapX);
- pStream->Read(&m_bWrapY);
- FAssertMsg((0 < GC.getNumBonusInfos()), "GC.getNumBonusInfos() is not greater than zero but an array is being allocated");
- pStream->Read(GC.getNumBonusInfos(), m_paiNumBonus);
- pStream->Read(GC.getNumBonusInfos(), m_paiNumBonusOnLand);
- if (numPlotsINLINE() > 0)
- {
- m_pMapPlots = new CvPlot[numPlotsINLINE()];
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- m_pMapPlots[iI].read(pStream);
- }
- }
- // call the read of the free list CvArea class allocations
- ReadStreamableFFreeListTrashArray(m_areas, pStream);
- setup();
- }
- // save object to a stream
- // used during save
- //
- void CvMap::write(FDataStreamBase* pStream)
- {
- uint uiFlag=0;
- pStream->Write(uiFlag); // flag for expansion
- pStream->Write(m_iGridWidth);
- pStream->Write(m_iGridHeight);
- pStream->Write(m_iLandPlots);
- pStream->Write(m_iOwnedPlots);
- pStream->Write(m_iTopLatitude);
- pStream->Write(m_iBottomLatitude);
- pStream->Write(m_iNextRiverID);
- pStream->Write(m_bWrapX);
- pStream->Write(m_bWrapY);
- FAssertMsg((0 < GC.getNumBonusInfos()), "GC.getNumBonusInfos() is not greater than zero but an array is being allocated");
- pStream->Write(GC.getNumBonusInfos(), m_paiNumBonus);
- pStream->Write(GC.getNumBonusInfos(), m_paiNumBonusOnLand);
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- m_pMapPlots[iI].write(pStream);
- }
- // call the read of the free list CvArea class allocations
- WriteStreamableFFreeListTrashArray(m_areas, pStream);
- }
- //
- // used for loading WB maps
- //
- void CvMap::rebuild(int iGridW, int iGridH, int iTopLatitude, int iBottomLatitude, bool bWrapX, bool bWrapY, WorldSizeTypes eWorldSize, ClimateTypes eClimate, SeaLevelTypes eSeaLevel, int iNumCustomMapOptions, CustomMapOptionTypes * aeCustomMapOptions)
- {
- CvMapInitData initData(iGridW, iGridH, iTopLatitude, iBottomLatitude, bWrapX, bWrapY);
- // Set init core data
- GC.getInitCore().setWorldSize(eWorldSize);
- GC.getInitCore().setClimate(eClimate);
- GC.getInitCore().setSeaLevel(eSeaLevel);
- GC.getInitCore().setCustomMapOptions(iNumCustomMapOptions, aeCustomMapOptions);
- // Init map
- init(&initData);
- }
- //////////////////////////////////////////////////////////////////////////
- // Protected Functions...
- //////////////////////////////////////////////////////////////////////////
- void CvMap::calculateAreas()
- {
- PROFILE("CvMap::calculateAreas");
- CvPlot* pLoopPlot;
- CvArea* pArea;
- int iArea;
- int iI;
- for (iI = 0; iI < numPlotsINLINE(); iI++)
- {
- pLoopPlot = plotByIndexINLINE(iI);
- gDLL->callUpdater();
- FAssertMsg(pLoopPlot != NULL, "LoopPlot is not assigned a valid value");
- if (pLoopPlot->getArea() == FFreeList::INVALID_INDEX)
- {
- pArea = addArea();
- pArea->init(pArea->getID(), pLoopPlot->isWater());
- iArea = pArea->getID();
- pLoopPlot->setArea(iArea);
- gDLL->getFAStarIFace()->GeneratePath(&GC.getAreaFinder(), pLoopPlot->getX_INLINE(), pLoopPlot->getY_INLINE(), -1, -1, pLoopPlot->isWater(), iArea);
- }
- }
- }
- // Private Functions...
|