#
# Unit Naming 
# By Nexushyper
#
# v 1.00 
#
#(v 1.01 by Teg_Navanis)



from CvPythonExtensions import *

import CvUtil
import sys
import math
import PyHelpers

#######SD Tool Kit#######

import SdToolKitAdvanced
sdEcho         = SdToolKitAdvanced.sdEcho
sdModInit      = SdToolKitAdvanced.sdModInit
sdModLoad      = SdToolKitAdvanced.sdModLoad
sdModSave      = SdToolKitAdvanced.sdModSave
sdEntityInit   = SdToolKitAdvanced.sdEntityInit
sdEntityExists = SdToolKitAdvanced.sdEntityExists
sdEntityWipe   = SdToolKitAdvanced.sdEntityWipe
sdGetVal       = SdToolKitAdvanced.sdGetVal
sdSetVal       = SdToolKitAdvanced.sdSetVal

############################

PyPlayer = PyHelpers.PyPlayer
PyInfo = PyHelpers.PyInfo

ARMY1 = "Army1"
ARMY2 = "Army2"
ARMY3 = "Army3"
ARMY4 = "Army4"
CORP1 = "Corp1"
CORP2 = "Corp2"
CORP3 = "Corp3"
CORP4 = "Corp4"
DIV1 = "Div1"
DIV2 = "Div2"
DIV3 = "Div3"
DIV4 = "Div4"
FLEET1 = "Fleet1"
FLEET2 = "Fleet2"
FLEET3 = "Fleet3"
FLEET4 = "Fleet4"
NGROUP1 = "NGroup1"
NGROUP2 = "NGroup2"
NGROUP3 = "NGroup3"
NGROUP4 = "NGroup4"
NSQUAD1 = "NSquad1"
NSQUAD2 = "NSquad2"
NSQUAD3 = "NSquad3"
NSQUAD4 = "NSquad4"
WING1 = "Wing1"
WING2 = "Wing2"
WING3 = "Wing3"
WING4 = "Wing4"
AGROUP1 = "AGroup1"
AGROUP2 = "AGroup2"
AGROUP3 = "AGroup3"
AGROUP4 = "AGroup4"
ASQUAD1 = "ASquad1"
ASQUAD2 = "ASquad2"
ASQUAD3 = "ASquad3"
ASQUAD4 = "ASquad4"


gc = CyGlobalContext()

g_CNnumber = 1
g_CNAbreviation = 0
g_CNspacing = 1
g_CNNameDesc = 0


def getNexusName(objUnit):
	

	unitName = ""

	unit = objUnit
	unitI = PyInfo.UnitInfo(unit.getUnitType())

	if (sdEntityExists("UnitNaming", "ARMY1") == False):
		#Since no record create entries
		army1 = 1
		nameArmy1 = {
							ARMY1 : army1 }	
		sdEntityInit("UnitNaming", "ARMY1", nameArmy1)
	
		army2 = 30
		nameArmy2 = {
							ARMY2 : army2 }	
		sdEntityInit("UnitNaming", "ARMY2", nameArmy2)
	
		army3 = 1
		nameArmy3 = {
							ARMY3 : army3 }		
		sdEntityInit("UnitNaming", "ARMY3", nameArmy3)

		army4 = 60
		nameArmy4 = {
							ARMY4 : army4 }		
		sdEntityInit("UnitNaming", "ARMY4", nameArmy4)
						
		corp1 = 1
		nameCorp1 = {
							CORP1 : corp1 }		
		sdEntityInit("UnitNaming", "CORP1", nameCorp1)
				
		corp2 = 5
		nameCorp2 = {
							CORP2 : corp2 }		
		sdEntityInit("UnitNaming", "CORP2", nameCorp2)
						
		corp3 = 1
		nameCorp3 = {
							CORP3 : corp3 }		
		sdEntityInit("UnitNaming", "CORP3", nameCorp3)

		corp4 = 25
		nameCorp4 = {
							CORP4 : corp4 }		
		sdEntityInit("UnitNaming", "CORP4", nameCorp4)
							
		div1 = 1
		nameDiv1 = {
							DIV1 : div1 }		
		sdEntityInit("UnitNaming", "DIV1", nameDiv1)
						
		div2 = 5
		nameDiv2 = {
							DIV2 : div2 }		
		sdEntityInit("UnitNaming", "DIV2", nameDiv2)
					
		div3 = 1
		nameDiv3 = {
							DIV3 : div3 }		
		sdEntityInit("UnitNaming", "DIV3", nameDiv3)

		div4 = 25
		nameDiv4 = {
							DIV4 : div4 }		
		sdEntityInit("UnitNaming", "DIV4", nameDiv4)


		fleet1 = 1
		nameFleet1 = {
							FLEET1 : fleet1 }		
		sdEntityInit("UnitNaming", "FLEET1", nameFleet1)
			
		fleet2 = 30
		nameFleet2 = {
							FLEET2 : fleet2 }		
		sdEntityInit("UnitNaming", "FLEET2", nameFleet2)
			
		fleet3 = 1
		nameFleet3 = {
							FLEET3 : fleet3 }		
		sdEntityInit("UnitNaming", "FLEET3", nameFleet3)
						
		fleet4 = 60
		nameFleet4 = {
							FLEET4 : fleet3 }		
		sdEntityInit("UnitNaming", "FLEET4", nameFleet4)

		ngroup1 = 1
		nameNGroup1 = {
							NGROUP1 : ngroup1 }		
		sdEntityInit("UnitNaming", "NGROUP1", nameNGroup1)

		ngroup2 = 5
		nameNGroup2 = {
							NGROUP2 : ngroup2 }		
		sdEntityInit("UnitNaming", "NGROUP2", nameNGroup2)
						
		ngroup3 = 1
		nameNGroup3 = {
							NGROUP3 : ngroup3 }		
		sdEntityInit("UnitNaming", "NGROUP3", nameNGroup3)
						
		ngroup4 = 25
		nameNGroup4 = {
							NGROUP4 : ngroup4 }		
		sdEntityInit("UnitNaming", "NGROUP4", nameNGroup4)

		nsquad1 = 1
		nameNSquad1 = {
							NSQUAD1 : nsquad1 }		
		sdEntityInit("UnitNaming", "NSQUAD1", nameNSquad1)
				
		nsquad2 = 5
		nameNSquad2 = {
							NSQUAD2 : nsquad2 }		
		sdEntityInit("UnitNaming", "NSQUAD2", nameNSquad2)
				
		nsquad3 = 1
		nameNSquad3 = {
							NSQUAD3 : nsquad3 }		
		sdEntityInit("UnitNaming", "NSQUAD3", nameNSquad3)

		nsquad4 = 25
		nameNSquad4 = {
							NSQUAD4 : nsquad4 }		
		sdEntityInit("UnitNaming", "NSQUAD4", nameNSquad4)

 
		wing1 = 1
		nameWing1 = {
							WING1 : wing1 }		
		sdEntityInit("UnitNaming", "WING1", nameWing1)
			
		wing2 = 30
		nameWing2 = {
							WING2 : wing2 }		
		sdEntityInit("UnitNaming", "WING2", nameWing2)
		
		wing3 = 1
		nameWing3 = {
							WING3 : wing3 }		
		sdEntityInit("UnitNaming", "WING3", nameWing3)

		wing4 = 60
		nameWing4 = {
							WING4 : wing4 }		
		sdEntityInit("UnitNaming", "WING4", nameWing4)
	
		agroup1 = 1
		nameAGroup1 = {
							AGROUP1 : agroup1 }		
		sdEntityInit("UnitNaming", "AGROUP1", nameAGroup1)
	
		agroup2 = 5
		nameAGroup2 = {
							AGROUP2 : agroup2 }		
		sdEntityInit("UnitNaming", "AGROUP2", nameAGroup2)
						
		agroup3 = 1
		nameAGroup3 = {
							AGROUP3 : agroup3 }		
		sdEntityInit("UnitNaming", "AGROUP3", nameAGroup3)
						
		agroup4 = 25
		nameAGroup4 = {
							AGROUP4 : agroup4 }		
		sdEntityInit("UnitNaming", "AGROUP4", nameAGroup4)

		asquad1 = 1
		nameASquad1 = {
							ASQUAD1 : asquad1 }		
		sdEntityInit("UnitNaming", "ASQUAD1", nameASquad1)
						
		asquad2 = 5
		nameASquad2 = {
							ASQUAD2 : asquad2 }		
		sdEntityInit("UnitNaming", "ASQUAD2", nameASquad2)
						
		asquad3 = 1
		nameASquad3 = {
							ASQUAD3 : asquad3 }		
		sdEntityInit("UnitNaming", "ASQUAD3", nameASquad3)
 
		asquad4 = 25
		nameASquad4 = {
							ASQUAD4 : asquad4 }		
		sdEntityInit("UnitNaming", "ASQUAD4", nameASquad4)


	#First check unit types
	#Second pull out numbers based on unit type
	#Then check naming conventions and name unit
	#Last run thru if elses to see what next numbers will be and store them
	#check if unit is land unit
	if (unit.getUnitClassType() >= 16 and unit.getUnitClassType() <= 43):
		numArmy1 = sdGetVal("UnitNaming", "ARMY1", ARMY1)
		numArmy2 = sdGetVal("UnitNaming", "ARMY2", ARMY2)
		numArmy3 = sdGetVal("UnitNaming", "ARMY3", ARMY3)
		numArmy4 = sdGetVal("UnitNaming", "ARMY4", ARMY4)
		numCorp1 = sdGetVal("UnitNaming", "CORP1", CORP1)
		numCorp2 = sdGetVal("UnitNaming", "CORP2", CORP2)
		numCorp3 = sdGetVal("UnitNaming", "CORP3", CORP3)
		numCorp4 = sdGetVal("UnitNaming", "CORP4", CORP4)
		numDiv1 = sdGetVal("UnitNaming", "DIV1", DIV1)
		numDiv2 = sdGetVal("UnitNaming", "DIV2", DIV2)
		numDiv3 = sdGetVal("UnitNaming", "DIV3", DIV3)
		numDiv4 = sdGetVal("UnitNaming", "DIV4", DIV4)
		if (g_CNnumber == 1):

			NumA = numberString[numArmy1]
			NumC = numberString[numCorp1]
			NumD = numberString[numDiv1]
		else:
			NumA = numArmy1
			NumC = numCorp1
			NumD = numDiv1

		if (g_CNAbreviation == 1):
			AbrA = "Army"
			AbrC = "Corp"
			AbrD = "Div"
		else:
			AbrA = "A"
			AbrC = "C"
			AbrD = "D"

		#name unit
		if (g_CNspacing == 0 and g_CNNameDesc == 1):
			unitName = unitI.getDescription() + " " + str(NumA) + str(AbrA) + " " + str(NumC) + str(AbrC) + " " + str(NumD) + str(AbrD)
		elif (g_CNspacing == 1 and g_CNNameDesc == 1):
			unitName = unitI.getDescription() + " " + str(NumA) + " " + str(AbrA) + " " + str(NumC) + " " + str(AbrC) + " " + str(NumD) + " " + str(AbrD)
		elif (g_CNspacing == 0 and g_CNNameDesc == 0):			
			unitName = str(NumA) + str(AbrA) + " " + str(NumC) + str(AbrC) + " " + str(NumD) + str(AbrD)
		elif (g_CNspacing == 1 and g_CNNameDesc == 0):
			unitName = str(NumA) + " " + str(AbrA) + " " + str(NumC) + " " + str(AbrC) + " " + str(NumD) + " " + str(AbrD)
						
		#up numbers for next unit
		if (numDiv1 < numDiv2):
			numDiv1 = numDiv1 + 1
			sdSetVal("UnitNaming", "DIV1", DIV1, numDiv1)
		else:
			numCorp1 = numCorp1 + 1
			numDiv1 = numDiv3
			sdSetVal("UnitNaming", "CORP1", CORP1, numCorp1)
			sdSetVal("UnitNaming", "DIV1", DIV1, numDiv1)
								
		if (numCorp1 > numCorp2):
			numArmy1 = numArmy1 + 1
			numCorp1 = numCorp3
			sdSetVal("UnitNaming", "ARMY1", ARMY1, numArmy1)
			sdSetVal("UnitNaming", "CORP1", CORP1, numCorp1)
								
		if (numArmy1 > numArmy2):
			numArmy1 = numArmy3
			numCorp2 = numCorp2 + 5
			numDiv2 = numDiv2 + 5
			numCorp3 = numCorp3 + 5
			numDiv3 = numDiv3 + 5
			numDiv1 = numDiv3	
			numCorp1 = numCorp3
			sdSetVal("UnitNaming", "ARMY1", ARMY1, numArmy1)
			sdSetVal("UnitNaming", "CORP1", CORP1, numCorp1)
			sdSetVal("UnitNaming", "DIV1", DIV1, numDiv1)
			sdSetVal("UnitNaming", "CORP2", CORP2, numCorp2)
			sdSetVal("UnitNaming", "DIV2", DIV2, numDiv2)
			sdSetVal("UnitNaming", "CORP3", CORP3, numCorp3)
			sdSetVal("UnitNaming", "DIV3", DIV3, numDiv3)

		if (numCorp2 > numCorp4):
			numArmy3 = numArmy3 + 30
			numArmy2 = numArmy2 + 30
			numCorp2 = numCorp2 - numCorp4
			numDiv2 = numDiv2 - numDiv4
			numCorp3 = numCorp3 - numCorp4
			numDiv3 = numDiv3 - numDiv4
			numArmy1 = numArmy3
			numDiv1 = numDiv3	
			numCorp1 = numCorp3	
			sdSetVal("UnitNaming", "ARMY3", ARMY3, numArmy3)
			sdSetVal("UnitNaming", "ARMY2", ARMY2, numArmy2)
			sdSetVal("UnitNaming", "CORP2", CORP2, numCorp2)
			sdSetVal("UnitNaming", "DIV2", DIV2, numDiv2)
			sdSetVal("UnitNaming", "CORP3", CORP3, numCorp3)
			sdSetVal("UnitNaming", "DIV3", DIV3, numDiv3)
			sdSetVal("UnitNaming", "ARMY1", ARMY1, numArmy1)
			sdSetVal("UnitNaming", "CORP1", CORP1, numCorp1)
			sdSetVal("UnitNaming", "DIV1", DIV1, numDiv1)
			
		if (numArmy3 > numArmy4):
			if (numArmy4 == 60):
				numArmy4 = numArmy4 + 60
				numArmy3 = numArmy3 - 60
				numCorp4 = numCorp4 + 25
				numDiv4 = numDiv4 + 25
				numCorp3 = numCorp3 + 25
				numDiv3 = numDiv3 + 25
				numArmy2 = numArmy2 - 60
				numCorp2 = numCorp2 + 25
				numDiv2 = numDiv2 + 25
				numArmy1 = numArmy3
				numDiv1 = numDiv3	
				numCorp1 = numCorp3
				sdSetVal("UnitNaming", "ARMY4", ARMY4, numArmy4)
				sdSetVal("UnitNaming", "CORP4", CORP4, numCorp4)
				sdSetVal("UnitNaming", "DIV4", DIV4, numDiv4)
				sdSetVal("UnitNaming", "ARMY3", ARMY3, numArmy3)
				sdSetVal("UnitNaming", "ARMY2", ARMY2, numArmy2)
				sdSetVal("UnitNaming", "CORP2", CORP2, numCorp2)
				sdSetVal("UnitNaming", "DIV2", DIV2, numDiv2)
				sdSetVal("UnitNaming", "CORP3", CORP3, numCorp3)
				sdSetVal("UnitNaming", "DIV3", DIV3, numDiv3)
				sdSetVal("UnitNaming", "ARMY1", ARMY1, numArmy1)
				sdSetVal("UnitNaming", "CORP1", CORP1, numCorp1)
				sdSetVal("UnitNaming", "DIV1", DIV1, numDiv1)

			else:
				numArmy3 = numArmy3 - numArmy4
				numArmy4 = numArmy4 + 60
				numCorp4 = numCorp4 + 25
				numDiv4 = numDiv4 + 25
				numArmy2 = numArmy2 - numArmy4
				numArmy1 = numArmy3
				numDiv1 = numDiv3	
				numCorp1 = numCorp3				
				sdSetVal("UnitNaming", "ARMY3", ARMY3, numArmy3)					
				sdSetVal("UnitNaming", "ARMY4", ARMY4, numArmy4)
				sdSetVal("UnitNaming", "CORP4", CORP4, numCorp4)
				sdSetVal("UnitNaming", "DIV4", DIV4, numDiv4)
				sdSetVal("UnitNaming", "ARMY2", ARMY2, numArmy2)
				sdSetVal("UnitNaming", "ARMY1", ARMY1, numArmy1)
				sdSetVal("UnitNaming", "CORP1", CORP1, numCorp1)
				sdSetVal("UnitNaming", "DIV1", DIV1, numDiv1)


	#check if unit is sea unit
	if (unit.getUnitClassType() >= 45 and unit.getUnitClassType() <= 54):
		numFleet1 = sdGetVal("UnitNaming", "FLEET1", FLEET1)
		numFleet2 = sdGetVal("UnitNaming", "FLEET2", FLEET2)
		numFleet3 = sdGetVal("UnitNaming", "FLEET3", FLEET3)
		numFleet4 = sdGetVal("UnitNaming", "FLEET4", FLEET4)
		numNGroup1 = sdGetVal("UnitNaming", "NGROUP1", NGROUP1)
		numNGroup2 = sdGetVal("UnitNaming", "NGROUP2", NGROUP2)
		numNGroup3 = sdGetVal("UnitNaming", "NGROUP3", NGROUP3)
		numNGroup4 = sdGetVal("UnitNaming", "NGROUP4", NGROUP4)
		numNSquad1 = sdGetVal("UnitNaming", "NSQUAD1", NSQUAD1)
		numNSquad2 = sdGetVal("UnitNaming", "NSQUAD2", NSQUAD2)
		numNSquad3 = sdGetVal("UnitNaming", "NSQUAD3", NSQUAD3)
		numNSquad4 = sdGetVal("UnitNaming", "NSQUAD4", NSQUAD4)
		if (g_CNnumber == 1):

			NumF = numberString[numFleet1]
			NumG = numberString[numNGroup1]
			NumS = numberString[numNSquad1]
		else:
			NumF = numFleet1
			NumG = numNGroup1
			NumS = numNSquad1

		if (g_CNAbreviation == 1):
			AbrF = "Fleet"
			AbrG = "Group"
			AbrS = "Squad"
		else:
			AbrF = "F"
			AbrG = "G"
			AbrS = "S"

		#name unit
		if (g_CNspacing == 0 and g_CNNameDesc == 1):
			unitName = unitI.getDescription() + " " + str(NumF) + str(AbrF) + " " + str(NumG) + str(AbrG) + " " + str(NumS) + str(AbrG)
		elif (g_CNspacing == 1 and g_CNNameDesc == 1):
			unitName = unitI.getDescription() + " " + str(NumF) + " " + str(AbrF) + " " + str(NumG) + " " + str(AbrG) + " " + str(NumS) + " " + str(AbrS)
		elif (g_CNspacing == 0 and g_CNNameDesc == 0):			
			unitName = str(NumF) + str(AbrF) + " " + str(NumG) + str(AbrG) + " " + str(NumS) + str(AbrG)
		elif (g_CNspacing == 1 and g_CNNameDesc == 0):
			unitName = str(NumF) + " " + str(AbrF) + " " + str(NumG) + " " + str(AbrG) + " " + str(NumS) + " " + str(AbrS)
			
		#up numbers for next unit
		if (numNSquad1 < numNSquad2):
			numNSquad1 = numNSquad1 + 1
			sdSetVal("UnitNaming", "NSQUAD1", NSQUAD1, numNSquad1)
		else:
			numNGroup1 = numNGroup1 + 1
			numNSquad1 = numNSquad3
			sdSetVal("UnitNaming", "NGROUP1", NGROUP1, numNGroup1)
			sdSetVal("UnitNaming", "NSQUAD1", NSQUAD1, numNSquad1)
								
		if (numNGroup1 > numNGroup2):
			numFleet1 = numFleet1 + 1
			numNGroup1 = numNGroup3
			sdSetVal("UnitNaming", "FLEET1", FLEET1, numFleet1)
			sdSetVal("UnitNaming", "NGROUP1", NGROUP1, numNGroup1)
								
		if (numFleet1 > numFleet2):
			numFleet1 = numFleet3
			numNGroup2 = numNGroup2 + 5
			numNSquad2 = numNSquad2 + 5
			numNGroup3 = numNGroup3 + 5
			numNSquad3 = numNSquad3 + 5
			numNSquad1 = numNSquad3	
			numNGroup1 = numNGroup1
			sdSetVal("UnitNaming", "FLEET1", FLEET1, numFleet1)
			sdSetVal("UnitNaming", "NGROUP1", NGROUP1, numNGroup1)
			sdSetVal("UnitNaming", "NSQUAD1", NSQUAD1, numNSquad1)
			sdSetVal("UnitNaming", "NGROUP2", NGROUP2, numNGroup2)
			sdSetVal("UnitNaming", "NSQUAD2", NSQUAD2, numNSquad2)
			sdSetVal("UnitNaming", "NGROUP3", NGROUP3, numNGroup3)
			sdSetVal("UnitNaming", "NSQUAD3", NSQUAD3, numNSquad3)

		if (numNGroup2 > numNGroup4):
			numFleet3 = numFleet3 + 30
			numFleet2 = numFleet2 + 30
			numNGroup2 = numNGroup2 - numNGroup4
			numNSquad2 = numNSquad2 - numNSquad4
			numNGroup3 = numNGroup3 - numNGroup4
			numNSquad3 = numNSquad3 - numNSquad4
			numFleet1 = numFleet3
			numNSquad1 = numNSquad3	
			numNGroup1 = numNGroup3	
			sdSetVal("UnitNaming", "FLEET3", FLEET3, numFleet3)
			sdSetVal("UnitNaming", "FLEET2", FLEET2, numFleet2)
			sdSetVal("UnitNaming", "NGROUP2", NGROUP2, numNGroup2)
			sdSetVal("UnitNaming", "NSQUAD2", NSQUAD2, numNSquad2)
			sdSetVal("UnitNaming", "NGROUP3", NGROUP3, numNGroup3)
			sdSetVal("UnitNaming", "NSQUAD3", NSQUAD3, numNSquad3)
			sdSetVal("UnitNaming", "FLEET1", FLEET1, numFleet1)
			sdSetVal("UnitNaming", "NGROUP1", NGROUP1, numNGroup1)
			sdSetVal("UnitNaming", "NSQUAD1", NSQUAD1, numNSquad1)
							

		if (numFleet3 > numFleet4):
			if (numFleet4 == 60):
				numFleet4 = numFleet4 + 60
				numFleet3 = numFleet3 - 60
				numNGroup4 = numNGroup4 + 25
				numNSquad4 = numNSquad4 + 25
				numNGroup3 = numNGroup3 + 25
				numNSquad3 = numNSquad3 + 25
				numFleet2 = numFleet2 - 60
				numNGroup2 = numNGroup2 + 25
				numNSquad2 = numNSquad2 + 25
				numFleet1 = numFleet3
				numNSquad1 = numNSquad3	
				numNGroup1 = numNGroup3
				sdSetVal("UnitNaming", "FLEET4", FLEET4, numFleet4)
				sdSetVal("UnitNaming", "NGROUP4", NGROUP4, numNGroup4)
				sdSetVal("UnitNaming", "NSQUAD4", NSQUAD4, numNSquad4)
				sdSetVal("UnitNaming", "FLEET3", FLEET3, numFleet3)
				sdSetVal("UnitNaming", "FLEET2", FLEET2, numFleet2)
				sdSetVal("UnitNaming", "NGROUP2", NGROUP2, numNGroup2)
				sdSetVal("UnitNaming", "NSQUAD2", NSQUAD2, numNSquad2)
				sdSetVal("UnitNaming", "NGROUP3", NGROUP3, numNGroup3)
				sdSetVal("UnitNaming", "NSQUAD3", NSQUAD3, numNSquad3)
				sdSetVal("UnitNaming", "FLEET1", FLEET1, numFleet1)
				sdSetVal("UnitNaming", "NGROUP1", NGROUP1, numNGroup1)
				sdSetVal("UnitNaming", "NSQUAD1", NSQUAD1, numNSquad1)

			else:
				numArmy3 = numFleet3 - numFleet4
				numArmy4 = numFleet4 + 60
				numNGroup4 = numNGroup4 + 25
				numNSquad4 = numNSquad4 + 25
				numArmy2 = numFleet2 - numFleet4
				numArmy1 = numFleet3
				numNSquad1 = numNSquad3	
				numNGroup1 = numNGroup3				
				sdSetVal("UnitNaming", "FLEET3", FLEET3, numFleet3)					
				sdSetVal("UnitNaming", "FLEET4", FLEET4, numFleet4)
				sdSetVal("UnitNaming", "NGROUP4", NGROUP4, numNGroup4)
				sdSetVal("UnitNaming", "NSQUAD4", NSQUAD4, numNSquad4)
				sdSetVal("UnitNaming", "FLEET2", FLEET2, numFleet2)
				sdSetVal("UnitNaming", "FLEET1", FLEET1, numFleet1)
				sdSetVal("UnitNaming", "NGROUP1", NGROUP1, numNGroup1)
				sdSetVal("UnitNaming", "NSQUAD1", NSQUAD1, numNSquad1)


	#check if unit is air unit
	if (unit.getUnitClassType() >= 55 and unit.getUnitClassType() <= 59):
		numWing1 = sdGetVal("UnitNaming", "WING1", WING1)
		numWing2 = sdGetVal("UnitNaming", "WING2", WING2)
		numWing3 = sdGetVal("UnitNaming", "WING3", WING3)
		numWing4 = sdGetVal("UnitNaming", "WING4", WING4)
		numAGroup1 = sdGetVal("UnitNaming", "AGROUP1", AGROUP1)
		numAGroup2 = sdGetVal("UnitNaming", "AGROUP2", AGROUP2)
		numAGroup3 = sdGetVal("UnitNaming", "AGROUP3", AGROUP3)
		numAGroup4 = sdGetVal("UnitNaming", "AGROUP4", AGROUP4)
		numASquad1 = sdGetVal("UnitNaming", "ASQUAD1", ASQUAD1)
		numASquad2 = sdGetVal("UnitNaming", "ASQUAD2", ASQUAD2)
		numASquad3 = sdGetVal("UnitNaming", "ASQUAD3", ASQUAD3)
		numASquad4 = sdGetVal("UnitNaming", "ASQUAD4", ASQUAD4)
		if (g_CNnumber == 1):

			NumW = numberString[numWing1]
			NumG = numberString[numAGroup1]
			NumS = numberString[numASquad1]
		else:
			NumW = numWing1
			NumG = numAGroup1
			NumS = numASquad1

		if (g_CNAbreviation == 1):
			AbrW = "wing"
			AbrG = "Group"
			AbrS = "Squad"
		else:
			AbrW = "W"
			AbrG = "G"
			AbrS = "S"

		#name unit
		if (g_CNspacing == 0 and g_CNNameDesc == 1):
			unitName = unitI.getDescription() + " " + str(NumW) + str(AbrW) + " " + str(NumG) + str(AbrG) + " " + str(NumS) + str(AbrS)
		elif (g_CNspacing == 1 and g_CNNameDesc == 1):
			unitName = unitI.getDescription() + " " + str(NumW) + " " + str(AbrW) + " " + str(NumG) + " " + str(AbrG) + " " + str(NumS) + " " + str(AbrS)
		elif (g_CNspacing == 0 and g_CNNameDesc == 0):			
			unitName = str(NumW) + str(AbrW) + " " + str(NumG) + str(AbrG) + " " + str(NumS) + str(AbrS)
		elif (g_CNspacing == 1 and g_CNNameDesc == 0):
			unitName = str(NumW) + " " + str(AbrW) + " " + str(NumG) + " " + str(AbrG) + " " + str(NumS) + " " + str(AbrS)

		#up numbers for next unit
		if (numASquad1 < numASquad2):
			numASquad1 = numASquad1 + 1
			sdSetVal("UnitNaming", "ASQUAD1", ASQUAD1, numASquad1)
		else:
			numAGroup1 = numAGroup1 + 1
			numASquad1 = numASquad3
			sdSetVal("UnitNaming", "AGROUP1", AGROUP1, numAGroup1)
			sdSetVal("UnitNaming", "ASQUAD1", ASQUAD1, numASquad1)
							
		if (numAGroup1 > numAGroup2):
			numWing1 = numWing1 + 1
			numAGroup1 = numAGroup3
			sdSetVal("UnitNaming", "WING1", WING1, numWing1)
			sdSetVal("UnitNaming", "AGROUP1", AGROUP1, numAGroup1)
								
		if (numWing1 > numWing2):
			numWing1 = numWing3
			numAGroup2 = numAGroup2 + 5
			numASquad2 = numASquad2 + 5
			numAGroup3 = numAGroup3 + 5
			numASquad3 = numASquad3 + 5
			numASquad1 = numASquad3	
			numAGroup1 = numAGroup1
			sdSetVal("UnitNaming", "WING1", WING1, numWing1)
			sdSetVal("UnitNaming", "AGROUP1", AGROUP1, numAGroup1)
			sdSetVal("UnitNaming", "ASQUAD1", ASQUAD1, numASquad1)
			sdSetVal("UnitNaming", "AGROUP2", AGROUP2, numAGroup2)
			sdSetVal("UnitNaming", "ASQUAD2", ASQUAD2, numASquad2)
			sdSetVal("UnitNaming", "AGROUP3", AGROUP3, numAGroup3)
			sdSetVal("UnitNaming", "ASQUAD3", ASQUAD3, numASquad3)

		if (numAGroup2 > numAGroup4):
			numWing3 = numWing3 + 30
			numWing2 = numWing2 + 30
			numAGroup2 = numAGroup2 - numAGroup4
			numASquad2 = numASquad2 - numASquad4
			numAGroup3 = numAGroup3 - numAGroup4
			numASquad3 = numASquad3 - numASquad4
			numWing1 = numWing3
			numASquad1 = numASquad3	
			numAGroup1 = numAGroup3	
			sdSetVal("UnitNaming", "WING3", WING3, numWing3)
			sdSetVal("UnitNaming", "WING2", WING2, numWing2)
			sdSetVal("UnitNaming", "AGROUP2", AGROUP2, numAGroup2)
			sdSetVal("UnitNaming", "ASQUAD2", ASQUAD2, numASquad2)
			sdSetVal("UnitNaming", "AGROUP3", AGROUP3, numAGroup3)
			sdSetVal("UnitNaming", "ASQUAD3", ASQUAD3, numASquad3)
			sdSetVal("UnitNaming", "WING1", WING1, numWing1)
			sdSetVal("UnitNaming", "AGROUP1", AGROUP1, numAGroup1)
			sdSetVal("UnitNaming", "ASQUAD1", ASQUAD1, numASquad1)
							

		if (numWing3 > numWing4):
			if (numWing4 == 60):
				numWing4 = numWing4 + 60
				numWing3 = numWing3 - 60
				numAGroup4 = numAGroup4 + 25
				numASquad4 = numASquad4 + 25
				numAGroup3 = numAGroup3 + 25
				numASquad3 = numASquad3 + 25
				numWing2 = numWing2 - 60
				numAGroup2 = numAGroup2 + 25
				numASquad2 = numASquad2 + 25
				numWing1 = numWing3
				numASquad1 = numASquad3	
				numAGroup1 = numAGroup3
				sdSetVal("UnitNaming", "WING4", WING4, numWing4)
				sdSetVal("UnitNaming", "AGROUP4", AGROUP4, numAGroup4)
				sdSetVal("UnitNaming", "ASQUAD4", ASQUAD4, numASquad4)
				sdSetVal("UnitNaming", "WING3", WING3, numWing3)
				sdSetVal("UnitNaming", "WING2", WING2, numWing2)
				sdSetVal("UnitNaming", "AGROUP2", AGROUP2, numAGroup2)
				sdSetVal("UnitNaming", "ASQUAD2", ASQUAD2, numASquad2)
				sdSetVal("UnitNaming", "AGROUP3", AGROUP3, numAGroup3)
				sdSetVal("UnitNaming", "ASQUAD3", ASQUAD3, numASquad3)
				sdSetVal("UnitNaming", "WING1", WING1, numWing1)
				sdSetVal("UnitNaming", "AGROUP1", AGROUP1, numAGroup1)
				sdSetVal("UnitNaming", "ASQUAD1", ASQUAD1, numASquad1)

			else:
				numArmy3 = numWing3 - numWing4
				numArmy4 = numWing4 + 60
				numAGroup4 = numAGroup4 + 25
				numASquad4 = numASquad4 + 25
				numArmy2 = numWing2 - numWing4
				numArmy1 = numWing3
				numASquad1 = numASquad3	
				numAGroup1 = numAGroup3				
				sdSetVal("UnitNaming", "WING3", WING3, numWing3)					
				sdSetVal("UnitNaming", "WING4", WING4, numWing4)
				sdSetVal("UnitNaming", "AGROUP4", AGROUP4, numAGroup4)
				sdSetVal("UnitNaming", "ASQUAD4", ASQUAD4, numASquad4)
				sdSetVal("UnitNaming", "WING2", WING2, numWing2)
				sdSetVal("UnitNaming", "WING1", WING1, numWing1)
				sdSetVal("UnitNaming", "AGROUP1", AGROUP1, numAGroup1)
				sdSetVal("UnitNaming", "ASQUAD1", ASQUAD1, numASquad1)

	return unitName

def numberString(iNumber):
	strNumber = str(iNumber)
	if (strNumber[len(strNumber) - 1] == 1):
		return strNumber + "st"
	if (strNumber[len(strNumber) - 1] == 2):
		return strNumber + "nd"
	if (strNumber[len(strNumber) - 1] == 3):
		return strNumber + "rd"
	else:
		return strNumber + "th"	
