Looking for help with a unique unit

DoktorApplejuce

Champion of Kirkwall
Joined
Apr 9, 2015
Messages
582
Location
Canada
I'm looking to create a Unique Unit that has a 100% chance of converting barbarian units upon killing them, and a 25% chance of converting enemy units (from other civs and city-states).

I'm assuming that it would require Lua, but I am not very well versed with Lua.

Thanks in advance to anyone who can help me out with this.
 
One method is to give the unit a promotion that has
Code:
<CaptureDefeatedEnemy>true</CaptureDefeatedEnemy>

This does not, however, guarantee 100% 'capture' of the defeated enemy. It is based as I recall on the relative strength differences as to how high a % likelihood the defeated unit will be captured. Once the unit is 'captured', lua would then be used to decide what else to do with the unit, and if it were me I would also use Machiavelli24's UnitCreatedGood system. Basically what Machiavelli did was to create a much better "game event" that fires more reliably than the standard "unit created" event as provided by Firaxis.

I had this in a 'not for Publication' mod, as a promotion given out by a national wonder:
Spoiler :
  • You would not need the <UnitPromotions_UnitCombats> table since you would give your promotion to the unit directly within the <Unit_FreePromotions> table.
Code:
<GameData>
	<UnitPromotions>
		<Row>
			<Type>PROMOTION_CAPTURE</Type>
			<Description>TXT_KEY_PROMOTION_CAPTURE</Description>
			<Help>TXT_KEY_PROMOTION_CAPTURE_HELP</Help>
			<Sound>AS2D_IF_LEVELUP</Sound>
			<CannotBeChosen>true</CannotBeChosen>
			[COLOR="Blue"]<CaptureDefeatedEnemy>true</CaptureDefeatedEnemy>[/COLOR]
			<PortraitIndex>6</PortraitIndex>
			<IconAtlas>KRIS_SWORDSMAN_PROMOTION_ATLAS</IconAtlas>
			<PediaType>PEDIA_ATTRIBUTE</PediaType>
			<PediaEntry>TXT_KEY_PROMOTION_CAPTURE</PediaEntry>
		</Row>
	</UnitPromotions>
	<UnitPromotions_UnitCombats>
		<Row>
			<PromotionType>PROMOTION_CAPTURE</PromotionType>
			<UnitCombatType>UNITCOMBAT_MOUNTED</UnitCombatType>
		</Row>
		<Row>
			<PromotionType>PROMOTION_CAPTURE</PromotionType>
			<UnitCombatType>UNITCOMBAT_MELEE</UnitCombatType>
		</Row>
		<Row>
			<PromotionType>PROMOTION_CAPTURE</PromotionType>
			<UnitCombatType>UNITCOMBAT_GUN</UnitCombatType>
		</Row>
		<Row>
			<PromotionType>PROMOTION_CAPTURE</PromotionType>
			<UnitCombatType>UNITCOMBAT_RECON</UnitCombatType>
		</Row>
		<Row>
			<PromotionType>PROMOTION_CAPTURE</PromotionType>
			<UnitCombatType>UNITCOMBAT_ARMOR</UnitCombatType>
		</Row>
	</UnitPromotions_UnitCombats>
	<Language_en_US>
		<!-- PROMOTION_CAPTURE -->

		<Row Tag="TXT_KEY_PROMOTION_CAPTURE">
			<Text>Capture</Text>
		</Row>
		<Row Tag="TXT_KEY_PROMOTION_CAPTURE_HELP">
			<Text>Defeated enemy units join your side</Text>
		</Row>
		<Update>
			<Where Tag="TXT_KEY_EUPANEL_CAPTURE_CHANCE" />
			<Set Text="[COLOR_CYAN]Capture Chance if Defeated: {1_Percent}%[ENDCOLOR]" />
		</Update>
	</Language_en_US>
</GameData>
 
One method is to give the unit a promotion that has
Code:
<CaptureDefeatedEnemy>true</CaptureDefeatedEnemy>

This does not, however, guarantee 100% 'capture' of the defeated enemy. It is based as I recall on the relative strength differences as to how high a % likelihood the defeated unit will be captured. Once the unit is 'captured', lua would then be used to decide what else to do with the unit, and if it were me I would also use Machiavelli24's UnitCreatedGood system. Basically what Machiavelli did was to create a much better "game event" that fires more reliably than the standard "unit created" event as provided by Firaxis.

I had this in a 'not for Publication' mod, as a promotion given out by a national wonder:
Spoiler :
  • You would not need the <UnitPromotions_UnitCombats> table since you would give your promotion to the unit directly within the <Unit_FreePromotions> table.
Code:
<GameData>
	<UnitPromotions>
		<Row>
			<Type>PROMOTION_CAPTURE</Type>
			<Description>TXT_KEY_PROMOTION_CAPTURE</Description>
			<Help>TXT_KEY_PROMOTION_CAPTURE_HELP</Help>
			<Sound>AS2D_IF_LEVELUP</Sound>
			<CannotBeChosen>true</CannotBeChosen>
			[COLOR="Blue"]<CaptureDefeatedEnemy>true</CaptureDefeatedEnemy>[/COLOR]
			<PortraitIndex>6</PortraitIndex>
			<IconAtlas>KRIS_SWORDSMAN_PROMOTION_ATLAS</IconAtlas>
			<PediaType>PEDIA_ATTRIBUTE</PediaType>
			<PediaEntry>TXT_KEY_PROMOTION_CAPTURE</PediaEntry>
		</Row>
	</UnitPromotions>
	<UnitPromotions_UnitCombats>
		<Row>
			<PromotionType>PROMOTION_CAPTURE</PromotionType>
			<UnitCombatType>UNITCOMBAT_MOUNTED</UnitCombatType>
		</Row>
		<Row>
			<PromotionType>PROMOTION_CAPTURE</PromotionType>
			<UnitCombatType>UNITCOMBAT_MELEE</UnitCombatType>
		</Row>
		<Row>
			<PromotionType>PROMOTION_CAPTURE</PromotionType>
			<UnitCombatType>UNITCOMBAT_GUN</UnitCombatType>
		</Row>
		<Row>
			<PromotionType>PROMOTION_CAPTURE</PromotionType>
			<UnitCombatType>UNITCOMBAT_RECON</UnitCombatType>
		</Row>
		<Row>
			<PromotionType>PROMOTION_CAPTURE</PromotionType>
			<UnitCombatType>UNITCOMBAT_ARMOR</UnitCombatType>
		</Row>
	</UnitPromotions_UnitCombats>
	<Language_en_US>
		<!-- PROMOTION_CAPTURE -->

		<Row Tag="TXT_KEY_PROMOTION_CAPTURE">
			<Text>Capture</Text>
		</Row>
		<Row Tag="TXT_KEY_PROMOTION_CAPTURE_HELP">
			<Text>Defeated enemy units join your side</Text>
		</Row>
		<Update>
			<Where Tag="TXT_KEY_EUPANEL_CAPTURE_CHANCE" />
			<Set Text="[COLOR_CYAN]Capture Chance if Defeated: {1_Percent}%[ENDCOLOR]" />
		</Update>
	</Language_en_US>
</GameData>

Alright, awesome, thanks. I'll go ahead and look up that Machiavelli24's mod, and see what I can see.
 
I mean for D'Applejuice's need, an lua program would be needed that would use Machiavelli's system as its "GameEvent". The lua function would determine whether the captured units are kept, destroyed, whatever.
 
Lua function I copy/pasted from something else I had, so it is not tested in the exact form as presented but I don't believe there are any errors. Place the code in an lua file and set the file up as an InGameUIAddin. You would need to change from CIVILIZATION_AMERICA to the correct name for your civilization. Once everything is verified to work correctly, you should probably change
Code:
local bDebugging = true
to
Code:
local bDebugging = false
otherwise you will bury the player and the lua log with messages.
Spoiler :
Code:
local bDebugging = true
local iRequiredCivilization = GameInfoTypes.CIVILIZATION_AMERICA
local iKeepPercentageAI = 25
local iKeepPercentageBarb = 100

--------------------------------------------------------------------------------
-- Unit Created Function
--------------------------------------------------------------------------------

function KeepCapturedUnits(playerID, unitID, hexVec, unitType, cultureType, civID, primaryColor, secondaryColor, unitFlagIndex, fogState, selected, military, notInvisible)
	local pPlayer = Players[playerID]
	local pUnit = pPlayer:GetUnitByID(unitID)
	if(pPlayer == nil or
		pUnit == nil or
		pUnit:IsDead()) then
		return
	end
	local playerCivilizationType = Players[playerID]:GetCivilizationType()
	local iOriginalOwner = pUnit:GetOriginalOwner()
	if bDebugging then
		local Message = "In function KeepCapturedUnits: Unit's Original Owner was " .. iOriginalOwner")
		print(Message)
		if (pPlayer:IsHuman()) then
			Events.GameplayAlertMessage(Message)
		end
	end
	if playerCivilizationType == iRequiredCivilization then
		local bKeepUnit = false
		if iOriginalOwner == 63 then
			if iKeepPercentageBarb == 100 then
				bKeepUnit = true
			else
				if math.random(100) <= iKeepPercentageBarb then
					bKeepUnit = true
				end
			end
		else
			if iOriginalOwner == playerID then
				return
			else
				if math.random(100) <= iKeepPercentageAI then
					bKeepUnit = true
				end
			end
		end
		if not bKeepUnit then
			pUnit:Kill()
			if bDebugging then
				local Message = "A Captured Unit whose original player owner # was " .. iOriginalOwner .. " has been removed from play")
				print(Message)
				if (pPlayer:IsHuman()) then
					Events.GameplayAlertMessage(Message)
				end
			end
		end
	end
end
LuaEvents.SerialEventUnitCreatedGood.Add(KeepCapturedUnits);

print("The code for KeepCapturedUnits loaded to the end")
  1. In using Machiavelli's system, you make no changes whatever to the code of Machiavelli's files.
  2. Add the two files from Machiavelli's UnitCreatedGood system into your mod:
    • Promotion_Created.xml
    • SerialEventUnitCreatedGood.lua
  3. The XML should be activated using the normal "OnModActivated", and the lua file should be set as an "InGameUIAddin".
  4. Highlighted in blue are the relevant parts from the modinfo file from one of my mods. In Greeen is the file with the code that used Machiavelli's system as its "Game Event" and is the exact implimentation method you need in ModBuddy to get the provided code within the spoiler to work.
    Spoiler :
    Code:
      <Files>
        <File md5="92867CB592BDD9C43EB7EF90BC13F7D1" import="0">Great Admirals/Text_GAdmirals.xml</File>
        <File md5="FD9451761C7565A8EC92D2208E3EF2D4" import="0">Great Admirals/UnitNames_GAdmirals.xml</File>
        <File md5="A1B0325B4EBCDFBB3AA381AFD69137BB" import="0">Great Generals/Text_GGenerals.xml</File>
        <File md5="A7B4BD4C4E27030CE31B365B957ABDE3" import="0">Great Generals/UnitNames_GGenerals.xml</File>
        [color="green"]<File md5="F7FE406A4DF8D32B6E2D8405A0A70088" import="0">LUA/Great People Names.lua</File>[/color]
        [color="blue"]<File md5="2E3B50BF5A346AA3CFAFF0D0083B0015" import="0">SerialEventGoodFiles/Promotion_Created.xml</File>
        <File md5="43E891B6B301EFF4F23F4AAA63972887" import="0">SerialEventGoodFiles/SerialEventUnitCreatedGood.lua</File>[/color]
        <File md5="37438941B2672936C437207A8888B5C4" import="0">SQL/CleanUp.sql</File>
        <File md5="8C2C337A61777792463DBCFAF7FE2510" import="0">SQL/UniqueUnitNames_CivColumn.sql</File>
      </Files>
      <Actions>
        <OnModActivated>
          <UpdateDatabase>SQL/UniqueUnitNames_CivColumn.sql</UpdateDatabase>
          [color="blue"]<UpdateDatabase>SerialEventGoodFiles/Promotion_Created.xml</UpdateDatabase>[/color]
          <UpdateDatabase>Great Generals/UnitNames_GGenerals.xml</UpdateDatabase>
          <UpdateDatabase>Great Admirals/UnitNames_GAdmirals.xml</UpdateDatabase>
          <UpdateDatabase>Great Admirals/Text_GAdmirals.xml</UpdateDatabase>
          <UpdateDatabase>Great Generals/Text_GGenerals.xml</UpdateDatabase>
          <UpdateDatabase>SQL/CleanUp.sql</UpdateDatabase>
        </OnModActivated>
      </Actions>
      <EntryPoints>
        [color="blue"]<EntryPoint type="InGameUIAddin" file="SerialEventGoodFiles/SerialEventUnitCreatedGood.lua">
          <Name>UnitCreatedGood</Name>
          <Description>Unit Created Good</Description>
        </EntryPoint>[/color]
        [color="green"]<EntryPoint type="InGameUIAddin" file="LUA/Great People Names.lua">
          <Name>Great General Names</Name>
          <Description>Great General Names</Description>
        </EntryPoint>[/color]
      </EntryPoints>
    </Mod>
 
Back
Top Bottom