Cannot Resolve AI_ChooseProduction Function Errors

Luke1195

High Pharaoh
Joined
Dec 22, 2015
Messages
61
Location
Charlotte, North Carolina, United States
How do I use the AI_ChooseProduction python function??
Other functions seem to return either a boolean, or an attribute from a TypeList, e.g. TechType.NO_TECH.
But, when I have AI_ChooseProduction return UnitTypes.UNIT_WORKBOAT, I get an error saying:
"AttributeError: type object 'CvPythonExtensions.UnitTypes' has no attribute 'UNIT_WORKBOAT'".
Returning the integer value doesn't seem to work, same with putting the attribute in gc.getInfoForString("UNIT_WORKBOAT"). It just makes the city produce nothing.
gc.getInfoForString(UnitTypes.UNIT_WORKBOAT) returns the attribute error.
Why is the city producing nothing, when it is supposed to be producing a work boat?
I've gotten this to work before! but then I lost the bloody code !!
 
The answer is to use pushOrder, and return True, instead of returning the UnitType.
For example, pCity.pushOrder(OrderTypes.ORDER_TRAIN, 89, -1, False, False, False, True) would set the city to train a Work Boat.
I still am working on the code, though, because this code also needs to work for cities that are connected to multiple bodies of water.
The code is to resolve a glitch in the base game, where ai don't train work boats in cities built on freshwater lakes with resources.
 
Here is the code!!

myUnitNumber = gc.getInfoTypeForString("UNIT_WORKBOAT")
myPlot = pCity.plot()
myMap = gc.getMap()
myX = myPlot.getX()
myY = myPlot.getY()
myNorthPlot = myMap.plot(myX, myY + 1)
myEastPlot = myMap.plot(myX + 1, myY)
mySouthPlot = myMap.plot(myX, myY - 1)
myWestPlot = myMap.plot(myX - 1, myY)
myArea = pCity.waterArea()
myTeam = gc.getTeam(pCity.getTeam())
myTeamID = gc.getGame().getActiveTeam()
if(pCity.isCoastal(1) and myArea.getNumTiles() < 10):
print("Is a lake")
myFishCount = myArea.getNumBonuses(15)
print("myFishCount = " + str(myFishCount))
if(myNorthPlot.getBonusType(myTeamID) == 15 and myNorthPlot.getImprovementType() < 0):
myFishCount += 1
if(myEastPlot.getBonusType(myTeamID) == 15 and myEastPlot.getImprovementType() < 0):
myFishCount += 1
if(mySouthPlot.getBonusType(myTeamID) == 15 and mySouthPlot.getImprovementType() < 0):
myFishCount += 1
if(myWestPlot.getBonusType(myTeamID) == 15 and myWestPlot.getImprovementType() < 0):
myFishCount += 1
print("myFishCount after cardinal plots = " + str(myFishCount))
myOilCount = myArea.getNumBonuses(6)
print("myOilCount = " + str(myOilCount))
myNumUnits = myArea.getNumUnits()
print("myNumUnits = " + str(myNumUnits))
myFishingBoats = myArea.getNumImprovements(5)
myOffshorePlatforms = myArea.getNumImprovements(17)
print("myFishingBoats = " + str(myFishingBoats))
if(myFishCount > myFishingBoats and myTeam.isHasTech(25)):
print("More fish than boats")
print("Return:" + str(myUnitNumber))
pCity.pushOrder(OrderTypes.ORDER_TRAIN, myUnitNumber, -1, False, False, False, True)
print("Order pushed")
return True
elif(myOilCount > myOffshorePlatforms and myTeam.isHasTech(53)):
print("More oil than platforms")
print("Return:" + str(myUnitNumber))
pCity.pushOrder(OrderTypes.ORDER_TRAIN, myUnitNumber, -1, False, False, False, True)
print("Order pushed")
return True
 
Last edited:
Also, here is the code that works with the above code to force workboats to build fishing boats, and goes in the CvEventManager file:

print("Begin Game Turn")
myMap = gc.getMap()
myUnitNumber = gc.getInfoTypeForString("UNIT_WORKBOAT")
fishPlot1 = myMap.plot(26,50)
fishPlot2 = myMap.plot(28,48)
fishPlot3 = myMap.plot(70,21)
fishPlot4 = myMap.plot(70,27)
fishPlot5 = myMap.plot(80,45)
fishPlot6 = myMap.plot(78,48)
fishPlot7 = myMap.plot(100,54)
fishPlot8 = myMap.plot(31,22) #Lake Titicaca for Settler'sEarthBasemap
myFishingBoats = gc.getInfoTypeForString("BUILD_FISHING_BOATS")
myBoatsImprovement = gc.getInfoTypeForString("IMPROVEMENT_FISHING_BOATS")
oilPlot1 = myMap.plot(79,46)
oilPlot2 = myMap.plot(80,49)
myOffshorePlatform = gc.getInfoTypeForString("BUILD_OFFSHORE_PLATFORM")
myPlatformImprovement = gc.getInfoTypeForString("IMPROVEMENT_OFFSHORE_PLATFORM")
myPlots = [fishPlot1, fishPlot2, fishPlot3, fishPlot4, fishPlot5, fishPlot6, fishPlot7, fishPlot8, oilPlot1, oilPlot2]
for myPlot in myPlots: #Cycle through all freshwater resources not connected to coast
myUnit = myPlot.getUnit(0)
myTeam = myUnit.getTeam()
print(myUnit.getUnitType())
if(myUnit.getUnitType() == myUnitNumber):
print("is workboat")
if(myPlot.getImprovementType() < 0 and myPlot.getBonusType(myUnit.getTeam()) > 0):
print("has bonus")
if(myUnit.canBuild(myPlot, myFishingBoats, True)):
print("canBuild")
myPlot.setImprovementType(myBoatsImprovement) #Manually set the improvements
print("set boats")
myUnit.kill(True, myTeam) #Manually kill the workboats
print("killed fishing workboat")
elif(myUnit.canBuild(myPlot, myOffshorePlatform, True)):
print("canBuild platform")
myPlot.setImprovementType(myPlatformImprovement)
print("set platform")
myUnit.kill(True, myTeam)
print("killed oil workboat")
elif(myPlot.getImprovementType() >= 0):
myUnit.kill(True, myTeam)
print("killed workboat, existing improvement")
cityOLakesPlot1 = myMap.plot(27,48) #This is Detroit, which is connected to two isolated freshwater lakes with bonuses, so separate code is needed for Lake Erie
cityOLakesPlot2 = myMap.plot(28,49) #Add more code later for this maize plot north of Lake Erie, which only matters in Settler's Earth Basemap
myCityOLakesPlots = [cityOLakesPlot1, cityOLakesPlot2]
myUnit = cityOLakesPlot1.getUnit(0)
myTeam = myUnit.getTeam()
myMission = MissionTypes.MISSION_MOVE_TO
myMissionAI = MissionAITypes.NO_MISSIONAI
print(myUnit.getUnitType())
if(myUnit.getUnitType() == myUnitNumber):
print("is workboat")
myGroup = myUnit.getGroup()
print("units in group: ", myGroup.getNumUnits())
print(myGroup.canMoveInto(fishPlot2, False))
myGroup.pushMission(myMission, 28, 48, 1, False, True, myMissionAI, cityOLakesPlot1, myUnit) #This may not work on all computers, so manually sets improvements and kills boats later
print("mission pushed")
print(fishPlot2.getUnit(0).getUnitType())
myGroup.pushMoveToMission(28, 48)
print("mission pushed")
print(fishPlot2.getUnit(0).getUnitType())
fishPlot2.setImprovementType(myBoatsImprovement)
print("set boats")
myUnit.kill(True, myTeam)
print("killed fishing workboat")


However note this code works directly with the map. If you wanted to use it with a custom map instead of the ones in my mod, you'd need to rewrite the coordinates for each resource.
 
Back
Top Bottom