View Full Version : QUESTION -- curbing unit proliferation


Ambreville
Jun 08, 2007, 03:21 PM
Is there a way to limit the total number of units a CIV can build altogether (all units of any types combined), based on the number of cities it controls or based on its current population. I know how to set a flat nationwide limit for individual units in the UnitClass file. I'm looking for something that is more flexible and yet still prevents CIVs from producing unlimited numbers of units. It would be more realistic on the one hand, but also would make some scenarios manageable for memory-challenged computers! ;)

Thanks!!

Zebra 9
Jun 08, 2007, 04:55 PM
Yes. This is done using the cannotTrain function in CvGameUtils.py. I think you would use code like:pPlayer = gc.getPlayer(city.getOwner())
maxUnits = pPlayer.getNumCities() * The Number Of Units Each City Can Support
if pPlayer.getNumUnits() >= maxUnits:
return True

Ambreville
Jun 08, 2007, 05:11 PM
Yes. This is done using the cannotTrain function in CvGameUtils.py. I think you would use code like:pPlayer = gc.getPlayer(city.getOwner())
maxUnits = pPlayer.getNumCities() * The Number Of Units Each City Can Support
if pPlayer.getNumUnits() >= maxUnits:
return True

Thanks!!

I'm not conversant at all with Python. Could I simply use the above in a notepad file ending with a .py extension?? :)

Where do I fill in the appropriate number of units in this coding??

Zebra 9
Jun 08, 2007, 05:33 PM
Well this code goes inside the cannotTrain function in CvGameUtils.py before the line that sayes return False. For the unit limit based on the number of cities a player has replace "The Number Of Units Each City Can Support" (In the above code) with the number of units each city can support. if you want to limit the number of units a civ can build all together use the below code:pPlayer = gc.getPlayer(city.getOwner())
maxUnits = The Number Of Units Each Civ Can Build
if pPlayer.getNumUnits() >= maxUnits:
return True Put this code in the same spot as the other example.

Ambreville
Jun 08, 2007, 06:00 PM
Well this code goes inside the cannotTrain function in CvGameUtils.py before the line that says return False. For the unit limit based on the number of cities a player has replace "The Number Of Units Each City Can Support" (In the above code) with the number of units each city can support. if you want to limit the number of units a civ can build all together use the below code:pPlayer = gc.getPlayer(city.getOwner())
maxUnits = The Number Of Units Each Civ Can Build
if pPlayer.getNumUnits() >= maxUnits:
return True Put this code in the same spot as the other example.

Like this??

def cannotTrain(self,argsList):
pCity = argsList[0]
eUnit = argsList[1]
bContinue = argsList[2]
bTestVisible = argsList[3]
pPlayer = gc.getPlayer(city.getOwner())
maxUnits = pPlayer.getNumCities() * 15
if pPlayer.getNumUnits() >= maxUnits:
return True
return False

Sorry for being dense here. I've no idea how to do this stuff...

So if this works, when a CIV reaches it MaxUnit value, the option to build more units will be greyed out?

I wonder if the AI will know to disband obsolete units in order to build newer ones. If not, and there are no other builds available for a city, what then? Willl the game crash? :)

Zebra 9
Jun 08, 2007, 06:09 PM
Change your code to look like:def cannotTrain(self,argsList):
pCity = argsList[0]
eUnit = argsList[1]
bContinue = argsList[2]
bTestVisible = argsList[3]
pPlayer = gc.getPlayer(pCity.getOwner())
maxUnits = pPlayer.getNumCities() * 15
if pPlayer.getNumUnits() >= maxUnits:
return True
return FalseIndentation is a big thing in python. If it's wrong you get at least 100 error messages (No joke).

EDIT: Now that I see the actual code I needed to fixe the city, there is a diff. between city and pCity.

Ambreville
Jun 08, 2007, 06:12 PM
(...) Indentation is a big thing in python. If it's wrong you get at least 100 error messages (No joke).

OK, will do. Thanks a lot for your help. Now I shall see what happens. :D

Shqype
Jun 08, 2007, 06:59 PM
Zebra 9, thanks for the snippets of code. This is a great idea, :goodjob:

Zebra 9
Jun 08, 2007, 07:02 PM
You're welcome. This one is going in my snippets thread. :D

Ambreville
Jun 08, 2007, 07:36 PM
You're welcome. This one is going in my snippets thread. :D

You have a snippets thread!? :lol:

How cool!

What's the link?

Ambreville
Jun 08, 2007, 07:53 PM
I used the modified python file, but nothing happened.

I launched the scenario, went into WorldBuilder, and added enough units to the active CIV to clearly exceed the max number of units. I then went back into play mode, and added more units to the city builds without apparently any restriction.

:confused:

---------------------------------------------

Never mind. I found out why I couldn't make it work.

Ahem... yes, I did remove the .txt extension that I inadvertently allowed to get added at the end of my python file. :hammer2:

All good now... really. I mean it this time.

...on the other hand. It would be nice if there were a way to add some information to help the player see how close to the max limit his units are, just like the national units (so many units left...). It would help if a message popped up explaining why a player cannot build any more units when the max limit has been reached. Other than this, the coding works well of course.

Many thanks!!

Shqype
Jun 08, 2007, 09:28 PM
I used the modified python file, but nothing happened.

I launched the scenario, went into WorldBuilder, and added enough units to the active CIV to clearly exceed the max number of units. I then went back into play mode, and added more units to the city builds without apparently any restriction.

:confused:

---------------------------------------------

Never mind. I found out why I couldn't make it work.

Ahem... yes, I did remove the .txt extension that I inadvertently allowed to get added at the end of my python file. :hammer2:

All good now... really. I mean it this time.

...on the other hand. It would be nice if there were a way to add some information to help the player see how close to the max limit his units are, just like the national units (so many units left...). It would help if a message popped up explaining why a player cannot build any more units when the max limit has been reached. Other than this, the coding works well of course.

Many thanks!!

With some fairly basic python knowledge, that shouldn't be too hard to do... At the "onBeginTurn" you could print to the screen a message that shows the current amount of units you have out of the maximum total. Or, you could have it constantly displaying somewhere...

Ambreville
Jun 08, 2007, 09:41 PM
With some fairly basic python knowledge, that shouldn't be too hard to do... At the "onBeginTurn" you could print to the screen a message that shows the current amount of units you have out of the maximum total. Or, you could have it constantly displaying somewhere...

It's not possible to have that info appear when mousing over a unit icon in the city screen, just like other national units (like missionaries or spies)?

Gaius Octavius
Jun 08, 2007, 11:52 PM
This is an extremely useful code. I'm glad you guys got it worked out--I can see lots of applications already.

Ambreville
Jun 10, 2007, 10:02 AM
Here's what I think would be a great way to monitor the unit balance:

http://forums.civfanatics.com/uploads/101290/Unit_Counter.gif

Sorry for the poor quality of the image...

This would stay on the upper left hand corner of the game screen. Can anyone pitch in for the coding needed to achieve this??

Many thanks!

Zebra 9
Jun 11, 2007, 10:01 AM
I'll have your code tomorrow w/ a possible MOD COMP.:thumbsup:

Zebra 9
Jun 11, 2007, 10:09 AM
Put my snippets thread in my sig. :thumbsup:

Shqype
Jun 11, 2007, 10:32 AM
Here's what I think would be a great way to monitor the unit balance:

http://forums.civfanatics.com/uploads/101290/Unit_Counter.gif

Sorry for the poor quality of the image...

This would stay on the upper left hand corner of the game screen. Can anyone pitch in for the coding needed to achieve this??

Many thanks!
Yea it is possible, and it's something I wanted to do for my mod, but it looks like Zebra will beat me to it. :)

Good job Zebra :goodjob:

Zebra 9
Jun 11, 2007, 10:42 AM
I've already got it displaying on screen, just need to make it INI configurable. :D

Shqype
Jun 11, 2007, 10:46 AM
Nice, INI configuration is not something I would have done. :lol:
I can't wait to see what you've done with it ;)

Ambreville
Jun 11, 2007, 12:07 PM
I'll have your code tomorrow w/ a possible MOD COMP.:thumbsup:


Awesome! Thanks a bunch! :goodjob:

Vrenir
Jun 12, 2007, 02:06 AM
In the thread on AI Exploring creating lag, it was mentioned that the other major cause (especially in the Modern Era) is the proliferation of units. Imposing a limit might be just the thing to get the game to run more smoothly.

If the limit could be adjusted by things like civics or buildings it would be even better. A civ wanting a greater army could make certain choices to increase their unit count. Civs not interested in that would build less units and thus cut down on turn time.

Vrenir
Jun 12, 2007, 02:07 AM
I accidentally double-posted, but don't actually know how to delete the second, so this is a pointless edit. Please ignore.

Ambreville
Jun 12, 2007, 07:01 AM
In the thread on AI Exploring creating lag, it was mentioned that the other major cause (especially in the Modern Era) is the proliferation of units. Imposing a limit might be just the thing to get the game to run more smoothly.

If the limit could be adjusted by things like civics or buildings it would be even better. A civ wanting a greater army could make certain choices to increase their unit count. Civs not interested in that would build less units and thus cut down on turn time.

Yes, I agree with you, although the ability to affect the unit limit through Civics/buildings et al, could defeat the limit's purpose.

Gaius Octavius
Jun 12, 2007, 09:29 AM
Not really. You could base it on population, which would work out to a zero net gain. In other words, the only way to be able to support more units would be to expand.... but the bonus you get from capturing an enemy's cities would be equal to the bonus he got from them before you did that. The only problem is that this would likely unbalance it in favor of huge civs, and we have enough of that already.

Ambreville
Jun 12, 2007, 09:36 AM
Not really. You could base it on population, which would work out to a zero net gain. In other words, the only way to be able to support more units would be to expand.... but the bonus you get from capturing an enemy's cities would be equal to the bonus he got from them before you did that. The only problem is that this would likely unbalance it in favor of huge civs, and we have enough of that already.

Of course, the unbalancing already exists are regards the rich getting richer. The "per-city-limitation" only seeks to prevent the nearly limitless ability to build units in any case. To really have an impact on the Huge-Civ/Huge-Army snowball effect, there would have to be a system of diminishing returns, whereas the more cities one Civ controls, the fewer additional units they would allow to build, down to perhaps one or two extra units per conquered city (basically a minimum needed to garrison new conquests).

---------------

Edit

This has to be set up in a tiered manner (the ten first cities each providing X number of units, the next 10 cities each providing X-1 units, the next 10 each providing X-2 units, etc...)

This approach allows a certain amount of "tailoring" of the settings to better fit a given scenario or time period.

Zebra 9
Jun 12, 2007, 11:44 AM
Great ideas. I'll probably use them in a future version. I'm about to upload. :thumbsup:


Watch my sig.

Ambreville
Jun 12, 2007, 12:15 PM
I'll have your code tomorrow w/ a possible MOD COMP.:thumbsup:

Any chance you could come up with that unit counter?? I thought that's what you meant.

http://forums.civfanatics.com/uploads/101290/Unit_Counter.gif

---------------

Edit -- never mind, I misread your other post.

Zebra 9
Jun 12, 2007, 12:46 PM
Here it is. (http://forums.civfanatics.com/showthread.php?t=226814)