local function nearestFriendlyCity(unit)
local bestCitySoFar = nil
local bestDistanceSoFar = 1000000
local function distance(tileA,tileB)
local xDist = tileA.x
local yDist = tileA.y
return math.sqrt(xDist^2+yDist^2)
end
for city in civ.iterateCities() do
if city.owner == unit.owner and distance(city.location,unit.location) < bestDistanceSoFar then
bestCitySoFar = city
bestDistanceSoFar = distance(city.location,unit.location)
end
end
return bestCitySoFar
end
console.nearestFriendlyCity=nearestFriendlyCity
local function nearestFriendyCity(tile,tribe)
local bestCity = nil
local bestDistance = 1000000
for city in civ.iterateCities() do
if city.owner == tribe then
local distance = gen.distance(city.location,tile)
if distance < bestDistance then
bestCity = city
bestDistance = distance
end
end
end
return bestCity
end
local function addShieldsToNearestCity(tile,tribe,shields)
local city = nearestFriendlyCity(tile,tribe)
if city then
city.shields = city.shields + shields
end
end
local function addFoodToNearestCity(tile,tribe,food)
local city = nearestFriendlyCity(tile,tribe)
if city then
city.food = city.food + food
end
end
local function bestowShieldBonus(city,bonus,turns)
cityData.counterSetValue(city,"bonusShields",bonus)
cityData.counterSetValue(city,"remainingTurnsForBonusShields",turns)
local city = nearestFriendlyCity(tile,tribe)
end
local shepherdFoodTable = gen.makeThresholdTable({
[0.75] = 3,
[0.50] = 5,
[0.25] = 7,
[0] = 10,
})
local rangerShieldsTable = gen.makeThresholdTable({
[0.75] = 3,
[0.50] = 5,
[0.25] = 7,
[0] = 10,
})
discreteEvents.onEnterTile(function (unit, previousTile, previousDomainSpec)
if math.random() < 0.25 then
if unit.type == unitAliases.Shepherd and unit.location.terrainType == 0 then
addFoodToNearestCity(unit.location,unit.owner,shepherdFoodTable[math.random()])
if unit.owner.isHuman then
civ.playSound("Food.wav")
end
end
end
if math.random() < 0.25 then
if unit.type == unitAliases.Shepherd and unit.location.terrainType == 1 then
addFoodToNearestCity(unit.location,unit.owner,shepherdFoodTable[math.random()])
if unit.owner.isHuman then
civ.playSound("Food.wav")
end
end
end
if math.random() < 0.25 then
if unit.type == unitAliases.Ranger and unit.location.terrainType == 3 then
addShieldsToNearestCity(unit.location,unit.owner,rangerShieldsTable[math.random()])
if unit.owner.isHuman then
civ.playSound("Production.wav")
end
end
end
if math.random() < 0.25 then
if unit.type == unitAliases.Ranger and unit.location.terrainType == 4 then
addShieldsToNearestCity(unit.location,unit.owner,rangerShieldsTable[math.random()])
if unit.owner.isHuman then
civ.playSound("Production.wav")
end
end
end
if math.random() < 0.25 then
if unit.type == unitAliases.Statesman1 and unit.location.improvements then
unit.owner.money = unit.owner.money + titheTable[math.random()]
end
end
if math.random() < 0.25 then
if unit.type == unitAliases.Statesman2 and unit.location.improvements then
unit.owner.money = unit.owner.money + titheTable[math.random()]
end
end
if math.random() < 0.25 then
if unit.type == unitAliases.Statesman3 and unit.location.improvements then
unit.owner.money = unit.owner.money + titheTable[math.random()]
end
end
if math.random() < 0.25 then
if unit.type == unitAliases.Statesman4 and unit.location.improvements then
unit.owner.money = unit.owner.money + titheTable[math.random()]
end
end
end)