Setting Attributes for New Unit

Why exactly are you iterating dPromotionsList? I would assume that you would want the dictionary:
Code:
{
"COMBAT1" : (1, "COMBAT"),
"COMBAT2 : (2, "COMBAT"),
"COMBAT3 : (3, "COMBAT"),
"COMBAT4 : (4, "COMBAT"),
"COMBAT5 : (5, "COMBAT"),
"COMBAT6 : (6, "COMBAT"),
"COMBAT7 : (7, "COMBAT"),
"GUERILLA1 : (1, "GUERILLA"),
"GUERILLA2 : (2, "GUERILLA"),
"GUERILLA3 : (3, "GUERILLA"),
}
...to return the index values of PromotionTypes.PROMOTION_COMBAT7 and PromotionTypes.PROMOTION_GUERILLA3. Because you wanna do something with these enums, right?


This:

Code:
dPromotionList = {
		"PROMOTION_COMBAT1": (1, "COMBAT"),
		"PROMOTION_COMBAT2": (2, "COMBAT"),
		"PROMOTION_COMBAT3": (3, "COMBAT"),
		"PROMOTION_COMBAT4": (4, "COMBAT"),
		"PROMOTION_COMBAT5": (5, "COMBAT"),
		"PROMOTION_COMBAT6": (6, "COMBAT"),
		"PROMOTION_GUERILLA1": (1, "GUERILLA"),
		"PROMOTION_GUERILLA2": (2, "GUERILLA"),
		"PROMOTION_GUERILLA3": (3, "GUERILLA")
}

I shortened the list for the sake of brevity. I want to group the strGroup, which will summarize the promotion group to use elsewhere. I will also use the entire list in multiple functions. The group is key to my logic, as it allows me to determie if a promotion belongs to a group. Randum checks for group promotions are handled differently from promotions that do not belong to a group. Single promotions are identified in strGroup as "NONE".
 
You could have a shortened dictionary with only the last promotion level entries in place. (Perhaps by iterating the dictionary in reversed order and skipping iPromotionLevel number of entries.)

Or you could have a second data structure indexing the dictionary. It would only contain references to valid dictionary entries.

Unfortunately I don't have the time to make any examples now, but I'll be back if you can't figure it out. :)
 
You could have a shortened dictionary with only the last promotion level entries in place. (Perhaps by iterating the dictionary in reversed order and skipping iPromotionLevel number of entries.)

Or you could have a second data structure indexing the dictionary. It would only contain references to valid dictionary entries.

Unfortunately I don't have the time to make any examples now, but I'll be back if you can't figure it out. :)

I tried to index the data table, but it bombed out. :sad: I'm using a separate (temporary) data table with the values I need until the summary of strGroup can be figured out.
 
You have the loop already, apparently, so you may as well just build a list in it (or you could make it a set):
Code:
lGroups = list()
for szPromotion, (iPromotionLevel, strGroup) in dPromotionList.iteritems():
	if strGroup not in lGroups:
		lGroups.append(strGroup)
	[I]other stuff...[/I]

Also, you can remove duplicates from a list, converting it to a unique set of elements, by converting the list to a set. This works because the elements of a set must be unique and when the set is made it just throws out duplicates:
Code:
setOfGroups = set( listOfGroupsWithDuplciates)
You might want to convert the set back to a list, depending on what you plan to do with it:
Code:
listOfGroups = list( set( listOfGroupsWithDuplciates))
(Haven't tried that, but it ought to work.)

Also, you don't even have to directly extract a list of all of the groups from your dictionary first. You should be able to do it with a list comprehension to build a list that is used to initialize the set. So if you are not using the for loop you posted for anything else, you could replace it. Perhaps something like this:
Code:
setOfGroups = set([strGroup for iPromotionLevel, strGroup in dPromotionList.itervalues()])
(Untested, but it should work or be very close to something that does.)
 
You have the loop already, apparently, so you may as well just build a list in it (or you could make it a set):
Code:
lGroups = list()
for szPromotion, (iPromotionLevel, strGroup) in dPromotionList.iteritems():
	if strGroup not in lGroups:
		lGroups.append(strGroup)
	[I]other stuff...[/I]

Also, you can remove duplicates from a list, converting it to a unique set of elements, by converting the list to a set. This works because the elements of a set must be unique and when the set is made it just throws out duplicates:
Code:
setOfGroups = set( listOfGroupsWithDuplciates)
You might want to convert the set back to a list, depending on what you plan to do with it:
Code:
listOfGroups = list( set( listOfGroupsWithDuplciates))
(Haven't tried that, but it ought to work.)

Also, you don't even have to directly extract a list of all of the groups from your dictionary first. You should be able to do it with a list comprehension to build a list that is used to initialize the set. So if you are not using the for loop you posted for anything else, you could replace it. Perhaps something like this:
Code:
setOfGroups = set([strGroup for iPromotionLevel, strGroup in dPromotionList.itervalues()])
(Untested, but it should work or be very close to something that does.)

Yipee! :) It worked! :dance: Here is the function:

Code:
def runGroupListCheck(kUnit):		
	lGroups = list()
	
	for szPromotion, (iPromotionLevel, strGroup) in dPromotionList.iteritems():
		# Setup a summary list of group promotions  
		if strGroup not in lGroups:
			lGroups.append(strGroup)
			# Does the Unit have this group promotion? 
			for iProm in range(gc.getNumPromotionInfos()):
				if kUnit.isHasPromotion(iProm):
					GroupName = getPromotionGroup(iProm)
					#CyInterface().addImmediateMessage(str(GroupName), "")
					if GroupName == strGroup:
						# Yup!  Run promotion group checks.
						doPromotionGroupRandumChecks(kUnit, strGroup)
						#CyInterface().addImmediateMessage("Here I am!", "")
						break

I have deleted the extra obsolete data table in lieu of this excellent code. I am getting very close to finishing my new Army mod! My next python challenge will be to get the AI to create Army units, which it will definitely use.

:thanx:
 
Back
Top Bottom