Getting adjacent units

AW Arcaeca

Deus Vult
Joined
Mar 10, 2013
Messages
3,019
Location
Operation Padlock ground zero
Basically for a UU I just need help with this part: How do you find all adjacent units, and how do you select a specific unit out of them?

The idea here is I'm looking for a way to make a UU receive a bonus if adjacent to cavalry (or anything else that is UNITCOMBAT_MOUNTED), sort of like the discipline promotion. But I can't find any lua functions for getting adjacent units except Unit.IsFriendlyAdjacentUnit, but that doesn't seem like exactly what I need...

Anyway, trying to use that function, I wrote up this code, but something tells me it won't work :p:
Code:
function ThisCivUUBonusAdjacentCavalry(playerID)
	local pPlayer = Players[playerID]
	for pUnit in pPlayer:GetUnits() do
		local mUnit = pPlayer:GetUnits()
		if pUnit:IsFriendlyAdjacentUnit([mUnit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_MOUNTED]) then
			pUnit:SetHasPromotion(GameInfoTypes.PROMOTION_THIS_PROMOTION, true)
		else
			pUnit:SetHasPromotion(GameInfoTypes.PROMOTION_THIS_PROMOTION, false)
		end
	end
end
GameEvents.PlayerDoTurn.Add(ThisCivUUBonusAdjacentCavalry)
I don't think it'll work because
1) assigning 2 variables to pPlayer:GetUnits() - does that even work?
2)Probably not the right function anyway, and
3) It doesn't even look like it's structured right.
But, you know, what do you more experienced coders think?
 
With PlayerDoTurn, you will achieve that units starting turn next to mounted units will have combat bonus.

Code:
	for pUnit in pPlayer:GetUnits() do
		if pUnit:IsCombat() then
			pUnit:SetHasPromotion(GameInfoTypes.PROMOTION_THIS_PROMOTION, false)
			for iUnit in pPlayer:GetUnits() do
				if iUnit ~= pUnit then
					if iUnit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_MOUNTED then
						if Map.PlotDistance(pUnit:GetX(), pUnit:GetY(), iUnit:GetX(), iUnit:GetY()) == 1 then
						pUnit:SetHasPromotion(GameInfoTypes.PROMOTION_THIS_PROMOTION, true)
break;
						end
					end
				end
			end
		end
	end
 
Back
Top Bottom