Lua Questions (Units and Messages)

Bismuth

Chieftain
Joined
May 20, 2016
Messages
9
I'm working on a mod leader and civ, and I've hit a roadblock where I am not able to figure out the lua needed for the traits.
The most critical problem I am having is that I cannot figure out how to properly refer to units. I have two functions I can't get working because I am stuck on this. In one function (Air-Raid part of Dice Game) I need to go through all of the player's units and damage them.
In another function (MagMassII) I need to check for the deletion of a unit I added then check through all units in the game and kill all full health units. I can't get the check part of this working, so I haven't tried anything for the latter.
I would appreciate any help with referring to units.

Another question I had is if it is possible to use lua to display a message in the same way as combat information or foreign intel? (Examples: "Our warrior(10 damage) defeated a barbarian warrior(15 damage)" or "Rumor has it China is now a Merchant Republic") This is important since my civ has random events each turn, and I would like the player to be notified of which event occurred.

And the last question I had is if it is possible to use lua to initiate anarchy for a player. This is not particularly important, but if it is possible I wanted to use it for one of my civ's random events.

I've attached my work-in-progress script files.
 

Attachments

  1. This is already a number
    Code:
    GameInfo.Units["UNIT_BUILDER"].Index
    so there is no need to "tonumber" it
  2. You're already getting the pPlayer:GetUnits() object here so you may as well make use of the variable:
    Code:
    local pUnits = pPlayer:GetUnits()
  3. you can re-write as
    Code:
    if dice<30 then
    	pUnits:Create(GameInfo.Units["UNIT_BUILDER"].Index, capitalCity:GetX(), capitalCity:GetY())
    elseif dice<60 then
    	pUnits:Create(GameInfo.Units["UNIT_SCOUT"].Index, capitalCity:GetX(), capitalCity:GetY())
    	--Ubiquitous (lose random amount of gold) (1.5%)
    .... etc ....
You can also "cache" the data for the Builder and the Scout at the beginning of the file, which should make for a small improvmeent in performance processing-time wise, but more importantly makes it eaiser to find and change the units you are going to create.
Spoiler :
Code:
local iBuilder = GameInfo.Units["UNIT_BUILDER"].Index
local iScout = GameInfo.Units["UNIT_SCOUT"].Index

function DiceRoll(playerId)
	local pPlayer = Players[playerId]
	local playerConfig = PlayerConfigurations[playerId]
	local capitalCity = pPlayer:GetCities():GetCapitalCity()
	local pTreasury = pPlayer:GetTreasury()
	local pCulture = pPlayer:GetCulture()
	local pEra = pPlayer:GetEra()
	local pUnits = pPlayer:GetUnits()

	math.randomseed(os.time())		 --set seed for rng
	math.random()					--extra math.randoms to progress the seed and provide a greater degree of randomness
	math.random()
	math.random()
	math.random()
	local dice = math.random(1000)   --generate random number in identifier "dice"

	if playerConfig:GetCivilizationTypeName() == "CIVILIZATION_OJ" then
	--charity, give a scout or builder (3% scout, 3% builder, total = 6% charity)
	--maybe check era and give rider/ranger in later eras
		if dice<30 then
			pUnits:Create(iBuilder, capitalCity:GetX(), capitalCity:GetY())
		elseif dice<60 then
			pUnits:Create(iScout, capitalCity:GetX(), capitalCity:GetY())
		--Ubiquitous (lose random amount of gold) (1.5%)
		elseif dice<75 then
			local loss = math.random(10,1000)
			if pTreasury:GetGoldBalance()>loss then
				pTreasury:ChangeGoldBalance(-loss)
			else
				pTreasury:SetGoldBalance(0)
			end
		--Air Raid, all units take damage 4%
		--NOT FUNCTIONAL
		elseif dice<1000 then
			--the problem here is that the following lines are going to fire 924 times out of 1000
			--all the player's units are going to go *poof* pretty quick
			for i,pIndividualUnit in pUnits:Members() do 
				pIndividualUnit:ChangeDamage(20)
			end
		end
	end
end

Events.PlayerTurnActivated.Add(DiceRoll)
 
Last edited:
Thank you both so much for your help :D! Very appreciative of the kind and knowledgeable community here!
 
Back
Top Bottom