I need to check whether or not any building in a given city belong to a certain BuildingClassType. Because there seems to be no CyBuilding class and therefore no CyCity.getBuilding() method. Even though there is a CyCity.getNumBuildings() there is now way to iterate though them. And while there is a CyCity.getNumBuilding() method there is no CyCity.getNumBuildingClass() method.
It seems that I basically have to go:
Is this really necessary or is there perhaps a better way of doing this?
Sure, I could do it this way also:
Thats basically just the same thing, but even worse as I would iterate through every single building type - every time. There ought to be something in the Python API to keep this iteration business to a minimum. (I don't currently know how to do this in C++ to make it faster. But perhaps it takes no measurable time at all to check 150 some items through CvInfoBase?)
There could at least be a CvBuildingClassInfo.getBuildingTypes(). Perhaps I could just build a dictionary of all building classes and their associated building types at initialization? Then I could fetch the values I need from there.
And finally add this:
Did I just solve my own problem? (It took a hour or so to write this post... )
It seems that I basically have to go:
Code:
def checkBuildingClass(pCity, eBuildingClass):
for building in range(gc.getNumBuildingInfos()):
if pCity.isHasBuilding(building):
if gc.getBuildingInfo(building).getBuildingClass() == eBuildingClass:
return True
Sure, I could do it this way also:
Code:
def checkBuildingClass(pCity, eBuildingClass):
for building in getBuildings(pCity):
if getBuildingClass(building) == eBuildingClass:
return True
def getBuildings(pCity):
lBuildingsList = []
for building in range (gc.getNumBuildingInfos()):
if pCity.isHasBuilding(building):
lBuildingsList.append(building)
return lBuildingsList
def getBuildingClass(eBuilding):
return gc.getBuildingInfo(eBuilding).getBuildingClass()
There could at least be a CvBuildingClassInfo.getBuildingTypes(). Perhaps I could just build a dictionary of all building classes and their associated building types at initialization? Then I could fetch the values I need from there.
Code:
global buildingClassIndex
buildingClassIndex = {}
for building in range(gc.getNumBuildingInfos()):
eBuildingClass = gc.getBuildingInfo(building).getBuildingClass()
buildingClassIndex[eBuildingClass] = buildingClassIndex.get(eBuildingClass, []).append(building)
Code:
def getClassBuildings(eBuildingClass):
return buildingClassIndex[eBuildingClass]