• Our friends from AlphaCentauri2.info are in need of technical assistance. If you have experience with the LAMP stack and some hours to spare, please help them out and post here.

C++/Lua Request Thread

@MorpheusAK

Alright, so in terms of actual functionality, DLL would be the way to go.

But if you are looking for magic...

From what I can tell there are not a lot of useful, good, or even existent hooks for combat. However to achieve what you are looking for: Bonus vs barbs (as UU), UI functionality, and ?maybe? not too much overhang if you can avoid selecting and unselecting units as much as I do... you can try this--

Code:
-- Lua code for MorpheusAK
-- Last edited: 1/17/15
--------------
-- Set up globals to check for Lua dependent Civs.
--------------
bIsCivName = true; -- Note that this is backwards on purpose, this is because Lua defaults to true unless its explicitatly stated as false or is null.
---------------------------------------------------------------------------------------
-- Lua to check who is in game.
---------------------------------------------------------------------------------------
for id, checkPlayer in pairs(Players) do
	if(checkPlayer:IsEverAlive() and checkPlayer:GetCivilizationType == GameInfoTypes["CIVILIZATION_NAME_HERE"] and checkPlayer:IsHuman()) then bIsCivName = false;
	elseif(checkPlayer:IsEverAlive() and checkPlayer:GetCivilizationType == GameInfoTypes["CIVILIZATION_NAME_HERE"]) then
		checkPlayer:ChangeBarbarianCombatBonus(25); end --The AI has drastically different Barb bonuses anyways, this will be unnoticed by the human player.
end -- Modified to check for human also.
---------------------------------------------------------------------------------------
-- Lua to provide semi dynamic Barb bonus for a UU.
---------------------------------------------------------------------------------------
function UUSelectDirect (playerID, unitID)
	local pPlayer = Pplayers[playerID];
	local pUnit = pPlayer:GetUnitByID(unitID);
	
	if(pUnit ~= nil and pUnit:GetUnitType = GameInfoTypes["UNIT_TYPE_HERE"]) then
		pPlayer:ChangeBarbarianCombatBonus(33); --Unconfirmed. However the call exists in the LuaPlayer.h so it does something! :P
	else
		pPlayer:ChangeBarbarianCombatBonus(-33);
	end
	
	if bIsCivName then
		-- unsub if they are not in game. --Added check for human above.
		Events.SerialEventUnitFlagSelected.Remove(UUSelectDirect);
	end
end
---------------------------------------------------------------------------------------
-- Lua to provide semi dynamic Barb bonus for a UU.
---------------------------------------------------------------------------------------
function UUSelectFromHex (iHexX, iHexY)
	local pPlot = Map.GetPlot(ToGridFromHex(iHexX, iHexY));
	local skip = nil; -- Use this to prevent stacked units from causing unintended effects!
	
	for i = 0, pPlot:GetNumUnits() -1 do -- Cycle
		local pUnit = pPlot:GetUnit(i);
		if(pUnit ~= nil and pUnit:GetUnitType == GameInfoTypes["UNIT_TYPE_HERE"] and skip ~= nil) then
			skip = whatever;
			local pPlayer = pUnit:GetOwner();
			pPlayer:ChangeBarbarianCombatBonus(33);
		elseif skip ~= nil then
			pPlayer:ChangeBarbarianCombatBonus(-33);
		else end
	end
	
	if bIsCivName then
		-- unsub if they are not in game. --Added check for human above.
		Events.SerialEventHexSelected.Remove(UUSelectFromHex);
	end
end
Events.SerialEventUnitFlagSelected.Add(UUSelectDirect);
--Events.SerialEventHexSelected.Add(UUSelectFromHex); --Optional

--Unfortunately I do not think you can hook to the auto cycler :/
--This will mean that things could be weird when the auto cycler changes units for you...

Do note that I workaround the lack of AI selecting units (as this is a human player thing only) by just giving the AI of said faction a flat bonus vs Barbs. The AI gets a different bonus than the player normally anyways, so it should suffice without any game breaking, or back breaking :P, effects.

Hope this helps! (p.s. this may not be the best way, if someone else has better working hooks to grab onto do please comment!)

EDIT: For some reason my formatting just goes out the window and all [ ] spaces before lines disappear? Is this just me?
 
It's because you are using
Spoiler :
instead of
Code:
[/QUOTE]

pfft. Thanks, makes that a lot easier to read. I was thinking that Spoiler would make it less obtrusive as people scan through. It would seem that code cuts it down to size anyways.
 
@INightShade Wow, thanks again for all the help. Wrote code and everything.

Unfortunately, what I'm specifically trying to do is to allow a promotion to grant a bonus vs barbarians, and not create a UU that gets a bonus.
 
is to allow a promotion to grant a bonus vs barbarians

You can view the options for promotions in Steam/steamapps/common/civ V/assets/dlc/expansion 2/gamedata/xml/units/promotions

Looking there, there is no table column for bonuses against barbarians. In order to add one, sql is required to add the table.
Then, DLL coding (CvPromotionClasses, CvUnits, and probably CvUnitCombat) to add the functionality of said column.

So, using the code above, and a little adaptation, you can circumvent making DLL changes if you are uncomfortable with such. Rather than checking for a UU, you can check if a unit has a certain promotion. Specifically using pUnit:IsHasPromotion(promotionID)

Armed with such, you can now just make a promotion that does nothing (or whatever else you want it to do) and either via xml or lua, add it to whichever units are relevant.
 
Hello! I am a budding modder who is working on his first mod. I finished the XML, but i need Lua for my mod, the Principality of Monaco.

Ability:La Côte d'Azur:During golden ages, the empire's culture, gold, and tourism outputs are the same according to whichever is highest. Receive a free random Cultural Great Person when first signing Open Border treaties with other Civilizations. May not gain settlers or annex cities.
Unit:Princes Carabiniers:Starts with the 'Pour Múnegu' which grants 1% defense for every 1 point of happiness. Does not obsolete.Replaces Infantry.
Building:Casino:Is not affected by trade routes. Yields 3 Gold to the city, and 25% more gold. Produces 1 Unhappiness.

Thanks!
 
Hi i already asked this question in other place, but it doesnt seem to get any responses.
i was looking for a way to disable the reformation of units, after an enemy attack. When half of units are destroyed then the remaining ones jump into new places according to formation defined.
There is an option for disabling this before attack called ReformBeforeCombat in ArtDefine_UnitMemberCombats. It works great. But there isn't anything called 'ReformAfterAttack' or 'ReformAfterCombat'.
Can this be done via SDK/LUA ? If yes could anyone quickly throw some code ? Thanks!
 
I received the following code in order for my custom civ (Orlais) to earn culture per turn in the capital for every cit-state ally. It's not doing that.

Code:
gRequiredCivName = "ORLAIS"
gBuildingForCode = GameInfoTypes.PALACE

--gTable = {}
--gTable.gNumberCSAllies = 0
--gTable.gNumberCSFriends = 0


function CSAlliesFriends(iPlayer)
	local pPlayer = Players[iPlayer]
	if pPlayer:GetCivilizationType() == GameInfoTypes[gRequiredCivName] then
		--local pCity = pPlayer:GetCapitalCity();
		gNumberCSAllies = 0
		gNumberCSFriends = 0
		iCivPlayerNumber = iPlayer
		for i = 0, GameDefines.MAX_PLAYERS - 1 do
			if i ~= iCivPlayerNumber then
				if Players[i]:IsMinorCiv() then
					local iFriend = Players[i]:GetMinorCivFriendshipLevelWithMajor(iCivPlayerNumber)
					--print("results from iFriend = Players[i]:GetMinorCivFriendshipLevelWithMajor(iCivPlayerNumber) is " .. tostring(iFriend))

					if iFriend == 1 then
						--print("Is a Friend")
						gNumberCSFriends = gNumberCSFriends + 1
					end

					if iFriend == 2 then
						--print("Is an Ally")
						gNumberCSAllies = gNumberCSAllies + 1
					end
				end
			end
		end

		-------------------------------------------------------------------------
		--Ally and Friend city commands here
		-------------------------------------------------------------------------

		for pCity in pPlayer:Cities() do
			if pCity:IsHasBuilding(gBuildingForCode) then
				pPlayer:ChangeJONSCulture(gNumberCSAllies * 2)
			end
		end

		--gTable.gNumberCSAllies = gNumberCSAllies
		--gTable.gNumberCSFriends = gNumberCSFriends
	end
end
GameEvents.PlayerDoTurn.Add(CSAlliesFriends)

Is there anything wrong in the code (I do very much trust the guy I got it from, but one of the small changes I made may have made it non-opperable)?

Alternatively, how do I get a Lua file to work (ie; VFS=True, OnModActivated, etc.)

Please note that I know less than nothing about Lua, so you'll probably have to dumb down your explanation for me.

Thank you in advance.
 
I received the following code in order for my custom civ (Orlais) to earn culture per turn in the capital for every cit-state ally. It's not doing that.

Code:
gRequiredCivName = [COLOR="Red"]"ORLAIS"[/COLOR]
gBuildingForCode = GameInfoTypes.[COLOR="red"]PALACE[/COLOR]

--gTable = {}
--gTable.gNumberCSAllies = 0
--gTable.gNumberCSFriends = 0


function CSAlliesFriends(iPlayer)
	local pPlayer = Players[iPlayer]
	if pPlayer:GetCivilizationType() == GameInfoTypes[gRequiredCivName] then
		--local pCity = pPlayer:GetCapitalCity();
		gNumberCSAllies = 0
		gNumberCSFriends = 0
		iCivPlayerNumber = iPlayer
		for i = 0, GameDefines.MAX_PLAYERS - 1 do
			if i ~= iCivPlayerNumber then
				if Players[i]:IsMinorCiv() then
					local iFriend = Players[i]:GetMinorCivFriendshipLevelWithMajor(iCivPlayerNumber)
					--print("results from iFriend = Players[i]:GetMinorCivFriendshipLevelWithMajor(iCivPlayerNumber) is " .. tostring(iFriend))

					if iFriend == 1 then
						--print("Is a Friend")
						gNumberCSFriends = gNumberCSFriends + 1
					end

					if iFriend == 2 then
						--print("Is an Ally")
						gNumberCSAllies = gNumberCSAllies + 1
					end
				end
			end
		end

		-------------------------------------------------------------------------
		--Ally and Friend city commands here
		-------------------------------------------------------------------------

		for pCity in pPlayer:Cities() do
			if pCity:IsHasBuilding(gBuildingForCode) then
				pPlayer:ChangeJONSCulture(gNumberCSAllies * 2)
			end
		end

		--gTable.gNumberCSAllies = gNumberCSAllies
		--gTable.gNumberCSFriends = gNumberCSFriends
	end
end
GameEvents.PlayerDoTurn.Add(CSAlliesFriends)

Is there anything wrong in the code (I do very much trust the guy I got it from, but one of the small changes I made may have made it non-opperable)?

Alternatively, how do I get a Lua file to work (ie; VFS=True, OnModActivated, etc.)

Please note that I know less than nothing about Lua, so you'll probably have to dumb down your explanation for me.

Thank you in advance.
You need "CIVILIZATION_ORLAIS", I would think. But regardless it needs to be exactly as you stated it in your XML (or SQL) for the <Type> under the table <Civilizations>. Second, the palace needs to be referred to as it is in the game's XML. It is BUILDING_PALACE not PALACE.

And you need to activate the lua as InGameUIAddin in the Content tab of ModBuddy. See Post #3 of whoward69's what ModBuddy setting for what file types tutorial

But since you are not tying the effect to a specific building that can be constructed in multiple cities you can just change this
Code:
		for pCity in pPlayer:Cities() do
			if pCity:IsHasBuilding(gBuildingForCode) then
				pPlayer:ChangeJONSCulture(gNumberCSAllies * 2)
			end
		end
to this
Code:
pPlayer:ChangeJONSCulture(gNumberCSAllies * 2)
And you would not even need this line
Code:
gBuildingForCode = GameInfoTypes.[COLOR="red"]PALACE[/COLOR]
since pPlayer:ChangeJONSCulture(gNumberCSAllies * 2) makes a direct instant adjustment to the player's culture 'score'.
 
You need "CIVILIZATION_ORLAIS", I would think. But regardless it needs to be exactly as you stated it in your XML (or SQL) for the <Type> under the table <Civilizations>. Second, the palace needs to be referred to as it is in the game's XML. It is BUILDING_PALACE not PALACE.

And you need to activate the lua as InGameUIAddin in the Content tab of ModBuddy. See Post #3 of whoward69's what ModBuddy setting for what file types tutorial

But since you are not tying the effect to a specific building that can be constructed in multiple cities you can just change this
Code:
		for pCity in pPlayer:Cities() do
			if pCity:IsHasBuilding(gBuildingForCode) then
				pPlayer:ChangeJONSCulture(gNumberCSAllies * 2)
			end
		end
to this
Code:
pPlayer:ChangeJONSCulture(gNumberCSAllies * 2)
And you would not even need this line
Code:
gBuildingForCode = GameInfoTypes.[COLOR="red"]PALACE[/COLOR]
since pPlayer:ChangeJONSCulture(gNumberCSAllies * 2) makes a direct instant adjustment to the player's culture 'score'.

Thank you LeeS! (I didn't want to hijack that other guy's thread)

It's working now, but for some reason, in the overhead UI, it's showing that I'm only recieving one culture per turn, despite the fact that the counter goes up by three each turn (upon a having a single city-state ally). I'm getting the right amount of culture, so it really doesn't matter, but it is something worth noting, I suppose.

Thank you one more time, for good measure.
 
Thank you LeeS! (I didn't want to hijack that other guy's thread)

It's working now, but for some reason, in the overhead UI, it's showing that I'm only recieving one culture per turn, despite the fact that the counter goes up by three each turn (upon a having a single city-state ally). I'm getting the right amount of culture, so it really doesn't matter, but it is something worth noting, I suppose.

Thank you one more time, for good measure.

Addendum: I went back to your other post and I now see where you mentioned the UI thing. I guess it helps to read things thoroughly.
 
Thank you LeeS! (I didn't want to hijack that other guy's thread)

It's working now, but for some reason, in the overhead UI, it's showing that I'm only recieving one culture per turn, despite the fact that the counter goes up by three each turn (upon a having a single city-state ally). I'm getting the right amount of culture, so it really doesn't matter, but it is something worth noting, I suppose.

Thank you one more time, for good measure.
That's because the method is a direct change to the player's culture 'score'. It won't show in the +x side of the culture per turn because the game has no idea that an lua program is going to add an additional +2 or however many culture. I did mention that on that other thread.
 
Hello! I am a budding modder who is working on his first mod. I finished the XML, but i need Lua for my mod, the Principality of Monaco.

Ability:La Côte d'Azur:During golden ages, the empire's culture, gold, and tourism outputs are the same according to whichever is highest. Receive a free random Cultural Great Person when first signing Open Border treaties with other Civilizations. May not gain settlers or annex cities.
Unit:Princes Carabiniers:Starts with the 'Pour Múnegu' which grants 1% defense for every 1 point of happiness. Does not obsolete.Replaces Infantry.
Building:Casino:Is not affected by trade routes. Yields 3 Gold to the city, and 25% more gold. Produces 1 Unhappiness.

Thanks!

Alright, I am by no means even versed in Lua, but I do know a couple things about XML, and I can tell you that the no settlers/annexing thing can be defined in XML.


Wherever you have your trait defined, you'll want to add the following row:
Code:
<Type>TRAIT_YOURTRAITNAME</Type>
		<Description>TXT_KEY_TRAIT_YOURTRAITNAME</Description>
		<ShortDescription>TXT_KEY_TRAIT_YOURTRAITNAME_SHORT</ShortDescription>
		[COLOR="Blue"]<NoAnnexing>true</NoAnnexing>[/COLOR]

obviously replace the "YOURTRAITNAME" with whatever you've defined your trait as.

Also, add
Code:
	<Trait_NoTrain>
		<Row>
			<TraitType>TRAIT_YOURTRAITNAME</TraitType>
			<UnitClassType>UNITCLASS_SETTLER</UnitClassType>
		</Row>
	</Trait_NoTrain>

after </Traits>

and just one sec on the building... that, I think I can do fairly easily.
 
Hello! I am a budding modder who is working on his first mod. I finished the XML, but i need Lua for my mod, the Principality of Monaco.

Ability:La Côte d'Azur:During golden ages, the empire's culture, gold, and tourism outputs are the same according to whichever is highest. Receive a free random Cultural Great Person when first signing Open Border treaties with other Civilizations. May not gain settlers or annex cities.
Unit:Princes Carabiniers:Starts with the 'Pour Múnegu' which grants 1% defense for every 1 point of happiness. Does not obsolete.Replaces Infantry.
Building:Casino:Is not affected by trade routes. Yields 3 Gold to the city, and 25% more gold. Produces 1 Unhappiness.

Thanks!

Code:
<GameData>
	<Buildings>
		<Row>
			<Type>BUILDING_YOURBUILDING</Type>
			<BuildingClass>BUILDINGCLASS_BANK</BuildingClass>
			<FreeStartEra>ERA_FUTURE</FreeStartEra>
			<Cost>200</Cost>
			<GoldMaintenance>0</GoldMaintenance>
			<Happiness>-1</Happiness>
			<PrereqTech>TECH_BANKING</PrereqTech>
			<Help>TXT_KEY_BUILDING_YOURBUILDING_HELP</Help>
			<Description>TXT_KEY_BUILDING_YOURBUILDING</Description>
			<Civilopedia>TXT_KEY_CIV5_BUILDINGS_YOURBUILDING_TEXT</Civilopedia>
			<Strategy>TXT_KEY_BUILDING_YOURBUILDING_STRATEGY</Strategy>
			<ArtDefineTag>BANK</ArtDefineTag>
			<MinAreaSize>-1</MinAreaSize>
			<HurryCostModifier>100</HurryCostModifier>
			<IconAtlas>WHAT_EVER_YOUVE_DEFINED_AS_YOUR_ICON_ATLAS</IconAtlas>
			<NeverCapture>True</NeverCapture>
			<PortraitIndex>what ever number (writen as an integer) you have defined</PortraitIndex>
		</Row>
	</Buildings>
	<Building_Flavors>
		<Row>
			<BuildingType>BUILDING_YOURBUILDING</BuildingType>
			<FlavorType>FLAVOR_GOLD</FlavorType>
			<Flavor>8</Flavor>
		</Row>
	</Building_Flavors>
	<Building_YieldChanges>
		<Row>
			<BuildingType>BUILDING_YOURBUILDING</BuildingType>
			<YieldType>YIELD_GOLD</YieldType>
			<Yield>3</Yield>
		</Row>
	</Building_YieldChanges>
	<Building_YieldModifiers>
		<Row>
			<BuildingType>BUILDING_YOURBUILDING</BuildingType>
			<YieldType>YIELD_GOLD</YieldType>
			<Yield>25</Yield>
		</Row>
	</Building_YieldModifiers>
</GameData>

That should do the trick, I believe, for your building. Obviously change out "YOURBUILDING" for whatever you have your building defined as, and make sure you put in the correct Icon atlas defines.
 
I'm not 100% sure, since I've not tried this, but I don't believe the building's <Happiness> column accepts negative numbers. I don't recall offhand where I read about it before, though.
 
I'm not 100% sure, since I've not tried this, but I don't believe the building's <Happiness> column accepts negative numbers. I don't recall offhand where I read about it before, though.

Would it be an <Unhappiness> column then? I've honestly never done anything to increase unhappiness before with mods.
 
I don't believe an <Unhappiness> column exists within the building's table -- the closest is the <UnhappinessModifier> column used by the Forbidden Palace, but that one is only usable to reduce the amount of Unhappiness, not increase it. I've tried using that column before and placing a positive number in it, but it would not have any effect.
 
I'm trying to make a trait that does.

"Cities are immune to foreign religious spread once you found a regelion, +15% Combat Strength Vs. Civs whom follow different religion."

Any help would be appreciated.
 
Back
Top Bottom