There's a couple problems here:
Code:
function MurphysLaw()
[COLOR="Red"]local user = Game.GetActivePlayer()[/COLOR]
[COLOR="RoyalBlue"]--this gives you an integer; it would be best to call it something informative like iActivePlayer.
--To get a player object, you need another line: local activePlayer = Players[iActivePlayer][/COLOR]
local userCiv = Game.GetActiveCivilizationType()
[COLOR="red"]local Used = false -- will be removed when the button code is made.[/COLOR]
[COLOR="RoyalBlue"]--Keep in mind that this variable is only visible in the current scope.
--That's what "local" is for. By scope I mean from here to the 3rd "end" below
--(i.e., the end at the same indent). After that, Lua will assume you don't need
--the value ever again and will "garbage collect" it.[/COLOR]
if userCiv == GameInfoTypes["CIVILIZATION_EGYPT"] [COLOR="red"]and Used == false[/COLOR] then
[COLOR="RoyalBlue"]--Used will [B]always[/B] be false here. You just set it to false on the
--line above. How could it be anything other than false? (It can't)[/COLOR]
[COLOR="red"]local Used = true[/COLOR] -- will be removed when the button code is made.
[COLOR="RoyalBlue"]--This is not the same "Used" as the Used above. You have just created a new variable
--with the same name. The local means that it is only visible within this scope. [/COLOR]
local userCap = [COLOR="Red"]user[/COLOR]:GetCapitalCity()
[COLOR="RoyalBlue"]--This won't work because user was set above to be an integer; GetCapitalCity is a
--method that works on player objects, it won't work on an integer[/COLOR]
local userPop = userCap:GetPopulation()
[COLOR="RoyalBlue"]--The above works, but only on the capital city (is that intended?)[/COLOR]
userCap:SetPopulation((userPop + 2), true)
for iPlayer=[COLOR="red"]GameDefines.MAX_MAJOR_CIVS, GameDefines.MAX_CIV_PLAYERS-1[/COLOR], 1 do
[COLOR="RoyalBlue"]--This only loops through city states (the for loop below loops through major players)[/COLOR]
local playerCap = iPlayer:GetCapitalCity()
local playerPop = playerCap:GetPopulation()
if playerPop >= 3 then
playerCap:SetPopulation((playerPop - 2), true)
elseif playerPop == 2 then
playerCap:SetPopulation((playerPop - 1), true)
end
[COLOR="RoyalBlue"]--The code above only decreases pop for capital cities.[/COLOR]
end
end
for i = 0, GameDefines.MAX_MAJOR_CIVS - 1, 1 do
local pPlayer = Players[i]
if pPlayer:IsEverAlive() then
pPlayer:AddNotification(NotificationTypes.NOTIFICATION_XYZ, "A player has cast Burning Blood, increasing the population of their cities and decreasing the population of all others!", "A player has cast Burning Blood!")
end
end
Some other notes:
The function above is written to only ever work for the "active" player. Active player is the player whose computer the game is running on. In other words, the human in a single player game. So it will never work if an AI civ tries to use it. It would be better to set it up with an argument:
Code:
furnction RiverOfBlood(iPlayer)
local player = Players[iPlayer]
and then code it to give boost to player and penalty to everyone else.
To get all cities for a player, use:
Code:
for city in player:Cities() do
--This will loop through all cities owned by player.
--"city" is a local variable inside this loop
end
Keep in mind that local variables disappear outside of their scope. They can even become inaccessible within a scope if you reuse the name for another local variable (that's what bane_ does with Used above, though not intentionally).
You could make Used into a global variable (by omitting "local"), which would make it accessible outside the function. But that won't help here because it would still disappear when the player saves/exits the game. So you need to "persist" this information. There are several easy and more complicated solutions to this. The easy one is provided by the SaveGame database. Use it like this:
Code:
local SavedData = Modding.OpenSaveData()
SavedData:SetValue("UsedRiverOfBlood", 1)
You can find out if the spell has been cast with SavedData:GetValue("UsedRiverOfBlood") == 1. Remember to define SavedData as I did above. This will work even if the player exits/reloads.
Sorry for critiquing. I could just write the code. But I'd rather see you (and bane_) learn how to do it yourselves. It's not that hard if you understand variables and functions. A good exercise is to look at some actual functioning code and figure out (for yourself) what each line is actually doing.
That's easy: have the AI call the function RiverOfBlood(iPlayer) with iPlayer being the id of the AI casting the spell. The question for you is: When do you want the AI to use it?
On the UI, whoward69 has some pretty extensive tutorials on this. It's a bit daunting to learn. For me it takes about 15 minutes to add a button somewhere. Question for you is: Where do you want the button? (Note: even though it is hard to learn, Civ5 is easier to do this sort of thing than Civ4.)