Getting an extra bonus from goody huts

EricProton

Chieftain
Joined
May 28, 2017
Messages
2
Hi all!

I'm looking to create a unique scout that gets a second goody hut bonus when they activate a tribal village. I'm pretty sure this is something I'm going to need to use lua to do, but I don't have a ton of experience with lua.

This is what I've cobbled together with my limited knowledge, but it (unsurprisingly) doesn't seem to be working yet. Any help figuring out what I need to get this functioning would be greatly appreciated!

Code:
function OnImprovementActivated(x, y, player, unit, improvement, owner, unk1, unk2)
    local localPlayer = Game.GetLocalPlayer()

    if player == localPlayer then
        local unitType = GetUnitType(player, unit)

        if unitType == "UNIT_SPECIAL_SCOUT" then
            if improvement == GameInfo.Improvements["IMPROVEMENT_GOODY_HUT"].Index then
                GoodyHutReward(player, unit, 0, 0)
            end
        end
    end
end

function Initialize()
    Events.ImprovementActivated.Add(OnImprovementActivated)

end
 
Part of your trouble is a lot of incorrect assumptions about what type of data is sent to the function when the event fires.

The fourth argument is an ID # for the individual unit (if any) that activated (stepped on) the goody hut. From that you have to get the unit's "object" in order to then determine whether the type of unit was the special scout

Game.GetLocalPlayer() always gives you '0' (the human player) for a single-player game, or the Player ID # of the current human who is in the hot-seat at that time in a hot-seat game. In standard multiplayer I am not sure what it gives.

Skeleton structure for what you are looking for, driven ultimately by whether or not the unit that stepped on the goody hut was a UNIT_SPECIAL_SCOUT:
Code:
local iScoutSpecialIndex = GameInfo.Units["UNIT_SPECIAL_SCOUT"].Index
local iGoodyHutIndex = GameInfo.Improvements["IMPROVEMENT_GOODY_HUT"].Index


--==========================================================================================================================================================
-- OnImprovementActivated function
-- Events.ImprovementActivated executes when a goody hut or a barb camp is removed
--	one would assume from the event name it also fires for a completion of a farm, for example, but it does not
-- sorting is still needed for whether the improvement that was 'activated' was a hut or a barb camp
--==========================================================================================================================================================
function OnImprovementActivated(iX, iY, iPlayer, iUnitID, iImprovementIndex, iInteger1, iInteger2, iInteger3)
	if iImprovementIndex ~= iGoodyHutIndex then return end
	if (iPlayer == -1) or (iUnitID == -1) then return end
	local pPlayer = Players[iPlayer]
	local pUnit = pPlayer:GetUnits():FindID(iUnitID)
	if (pUnit:GetType() == iScoutSpecialIndex) then
		--DO STUFF HERE
	end
end
--==========================================================================================================================================================
-- LoadScreenClose hook events
--==========================================================================================================================================================
function OnLoadScreenClose()
	Events.ImprovementActivated.Add(OnImprovementActivated)
end
Events.LoadScreenClose.Add(OnLoadScreenClose)
If some other player captures or somehow otherwise acquires a UNIT_SPECIAL_SCOUT, that player would be able to get the benefit when the UNIT_SPECIAL_SCOUT steps on a goody.

This will print the "Civilization" name for player number 0 (the human player in single-player games) in the form of CIVILIZATION_AMERICA, CIVILIZATION_FRANCE, etc.:
Code:
print(PlayerConfigurations[0]:GetCivilizationTypeName())
If you wanted to make the OnImprovementActivated function print the name of the civ whose unit (if any) stepped on a goody hut, you would alter the previous code to as this. It is necessary to check for the iPlayer and iUnitID not being -1 because in certain conditions (such as a city claimed a plot with a goody hut) one or both of these arguments is assigned a value of -1 and you cannot look for a player or a Unit ID# of -1.:
Code:
local iScoutSpecialIndex = GameInfo.Units["UNIT_SPECIAL_SCOUT"].Index
local iGoodyHutIndex = GameInfo.Improvements["IMPROVEMENT_GOODY_HUT"].Index


--==========================================================================================================================================================
-- OnImprovementActivated function
-- Events.ImprovementActivated executes when a goody hut or a barb camp is removed
--	one would assume from the event name it also fires for a completion of a farm, for example, but it does not
-- sorting is still needed for whether the improvement that was 'activated' was a hut or a barb camp
--==========================================================================================================================================================
function OnImprovementActivated(iX, iY, iPlayer, iUnitID, iImprovementIndex, iInteger1, iInteger2, iInteger3)
	if iImprovementIndex ~= iGoodyHutIndex then return end
	if (iPlayer == -1) or (iUnitID == -1) then return end
	print(PlayerConfigurations[iPlayer]:GetCivilizationTypeName() .. " stepped on a goody hut")
	local pPlayer = Players[iPlayer]
	local pUnit = pPlayer:GetUnits():FindID(iUnitID)
	if (pUnit:GetType() == iScoutSpecialIndex) then
		--DO STUFF HERE
	end
end
--==========================================================================================================================================================
-- LoadScreenClose hook events
--==========================================================================================================================================================
function OnLoadScreenClose()
	Events.ImprovementActivated.Add(OnImprovementActivated)
end
Events.LoadScreenClose.Add(OnLoadScreenClose)
So for example if a unit from CIVILIZATION_AMERICA stepped on a goody hut, you would get CIVILIZATION_AMERICA stepped on a goody hut printed into the game's lua.log file.

Spoiler Game.GetLocalPlayer :
let's not muddy the waters with quibbles about the way I explained how it works wrt using Live Tuner to re-assign current human player and similar corner-case issues
 
Back
Top Bottom