[PYTHONCOMP] Random Unit Names

Porges said:
Here's one that works with SimCutie's event manager:
Code:
#import utility functions
from CvPythonExtensions import *
import pickle			

def getCityNumber(city):
	cityDict = pickle.loads( city.getScriptData() )
	cityDict['unitNumber'] = cityDict['unitNumber'] + 1
	city.setScriptData( pickle.dumps(cityDict) )
	return cityDict['unitNumber']

def onUnitBuilt(argsList):
	city = argsList[0]

	unit = argsList[1]

	unit.setName(findOrdinal(getCityNumber(city)) + " " + unit.getName() + " of " + city.getName())
	return 0
	
def onCityBuilt(argsList):
	city = argsList[0]
	cityDict = {'unitNumber':0}
	city.setScriptData( pickle.dumps(cityDict) )
	return 0
	
def findOrdinal(num):
    t = 'th st nd rd th th th th th th'.split()
    if num % 100 in (11, 12, 13): #special case
        return '%dth' % num
    return str(num) + t[num % 10]

def EventHandlerRegister(eventManager,unused = None):
	return {'unitBuilt-':onUnitBuilt,'cityBuilt-':onCityBuilt}

Sets the names like "1st Worker of Moscow", "2nd Warrior of Moscow". The number is dependant upon all units built in the city.
I tried to use this code, but I can't find unit name, how can I know if my unit with name?
 
Mike, why are you using simcutie's event manager with this mod? You should use Dr. Elmer Jiggle's event manager. Anyways thats beside the point. Change your onUnitBuilt to be this.

Code:
def onUnitBuilt(argsList):
	city = argsList[0]

	unit = argsList[1]

	#unit.setName(findOrdinal(getCityNumber(city)) + " " + unit.getName() + " of " + city.getName())

	iOwner = gc.getPlayer(unit.getOwner())
	iCivilizationType = iOwner.getCivilizationType()
	unit.setName(RandomNameUtils.getRandomCivilizationName(iCivilizationType))

	#unit.setName(RandomNameUtils.getRandomName())

	return 0
 
onUnitBuilt is called when a unit is actually built at a city and onUnitCreated is called when a unit is created buy a goodie hut, world builder etc. This means that when a unit is built both onUnitBuilt and onUnitCreated are called, though I cannot remember right now off the top of my head in what order.
 
TheLopez said:
Mike, why are you using simcutie's event manager with this mod? You should use Dr. Elmer Jiggle's event manager.
Without sounding rude... why? IMO SimCutie's is better - it does a similar job, and is alot more pluggable. The only reason against using it may be because it isn't as mainstream as Doc Jiggle's. I personally work with SCs, as I find it easier to use.
 
When a unit is built in a city, onUnitCreated is called first, then onUnitBuilt is called. So if both event handlers give the unit a name, the onUnitBuilt name will be the one the unit ends up with. :)
 
I solved this problems. I gave any unit a "Wild" prefix and count it in onUnitCreated, then I check if it has a name in onUnitBuilt, count down "Wild" if it named, then gave a cityname.

As default onUnitBuild is called before custom onUnitBuilt, the CvAdvisorUtils.unitBuiltFeats(city, unit) in CvEventManager.py should be moved to custom's onUnitBuilt, or the prompt message will be wrong.
 
Hi, I coded as below to name my unit. My problem is The Great Person is also named so that I can't use GP MOD to show picture, what can I do? How can I check the unit type to know if it is a GP?
Code:
	def onUnitCreated(self, argsList):
		'Unit Completed'
		unit = argsList[0]
		cityname = PyPlayer(unit.getOwner()).getCivilizationShortDescription()

		zsName = getUnitNameWithCity(cityname, unit)
		unit.setName(zsName)

	def onUnitBuilt(self, argsList):
		'Unit Completed'
		city = argsList[0]
		unit = argsList[1]
		cityname = city.getName()

		if (len(unit.getNameNoDesc()) > 0):
			cityunitS = PyPlayer(unit.getOwner()).getCivilizationShortDescription() + PyInfo.UnitInfo(unit.getUnitType()).getDescription()
			unitCount = sdGetVal(zsModName, cityunitS, "Cnt")
			unitCount = unitCount - 1
			sdSetVal(zsModName, cityunitS, "Cnt", unitCount)
		
		zsName = getUnitNameWithCity(cityname, unit)
		unit.setName(zsName)
		CvAdvisorUtils.unitBuiltFeats(city, unit)
 
I did it by code as below:
Code:
	def onUnitCreated(self, argsList):
		'Unit Completed'
		unit = argsList[0]

		if (len(unit.getNameNoDesc()) > 0):
			return

		prefix = PyPlayer(unit.getOwner()).getCivilizationShortDescription()

		zsName = getUnitNameWithCity(prefix, unit)
		unit.setName(zsName)
 
TheLopez said:
Random Unit Names Mod
By: TheLopez

Last Updated 06/09/06

Version: v0.5.2
Patch Compatibility: v1.52, 1.61
MP Compatible: ?
Download Mod v0.5.2


Description:

This mod will give any unit created in the game a unique name. The current
version of this mod can generate over well to many names to count. There are
two ways to use this mod. The first is to just call the getRandomName method
which will return just that, a random name. The second and new way to use this
mod is to use the getRandomCivilizationName passing in the civilization type ID
number. This mod should NOT be used as a stand-alone mod. You should just use
the RandomNameUtils.py file instead. The other files are just used to show
how the RandomNameUtils.py can be used.

Installation Instructions:

1) Unzip this into the "civ4_install_folder\Mods\" folder.
2) Open the CivilizationIV.ini configuration file
3) Change the Mod line to read: Mod = Mods\Random Unit Names
4) Load the game.
5) Then play as normal.


-----Version Information-----

-----v0.5.x------

- Tweaked the Japan names so they don't always produce really long names

- Added random name generation support for all of the original civilizations
included in the original game. If someone wants to add other names they will
have to do it themselves :p.


Spoiler :

-----v0.4------

- Forked off the mercenaries mod random name generation code

- Stripped the mercenary specific code from the mod


-----===Credits & Thanks===-----

- Exavier
Composite Mod - readme.txt format

hi, guys. very nice work.
but i am playing WL now.
Do we have another patch which the function is same as this for WL?
 
If someone wants to add other names they will have to do it themselves :p.

I was wondering where you came up with the names for each of the civilizations. I googled around a bit and found first names easily, but usually way too many, and usually designed for naming your baby. :lol: Last names and middle names are much harder to come up with. :sad:
 
This discussion was last winter. Did anyone make progress on it?
I try to do something similar manually when I play, but bog down when I have to mobilize for a big war.
:blush:
Personally, I would like a mod that numbers units by type,

For example, the first archer you build would be named "1st Militia"

Militia would be city defense units.
Infantry would be walking attack units (Swordsman, etc)
Cavalry would be mounted units.

etc etc.

So the idea is early in the game you create a Horse Archer which gets named "5th Cavalry"
Eventually, that unit could upgrade into a gunship, but would have the historical name "5th Cavalry"

Just like the Scottish Black Watch used to use muskets, now they use Armoured Personnel Carriers.

Hey Lopez, give me insight as to how you did this one and maybe I'll do my idea myself.


Hmmm..... well that is a very interesting idea. When I put out the random unit names mod I just ripped out the code from my Merc. Mod and made it available since someone had requested it. I am planning to extend it by making the random names a bit more specific depending on the civilization for the unit. Though I could introduce an option to do the type of naming that you are asking for as well. Let me mull this over and I'll see what I can do after I release my Merc. mod.



Well, you can download the mod and see for yourself how I did it.
 
This discussion was last winter. Did anyone make progress on it?
I try to do something similar manually when I play, but bog down when I have to mobilize for a big war.
:blush:

Nexus did something like this. I don't think he ever separated out the final version of his component. See here.

On the other hand, you can find it included in the best combined unit naming pack: JRandomNames by Jeckel.

I'm not sure if any of these work in Warlords.
 
Top Bottom