print ("REMOVABLE DISTRICTS LOADED");
local function OnProjectCompleted(playerID, cityID, projectIndex, buildingIndex, locX, locY, bCanceled)
local pPlayer = Players[playerID];
local sProjectTypeCompleted = GameInfo.Projects[projectIndex].ProjectType;
-- print("project "..sProjectTypeCompleted.." completed "..playerID.." projectIndex:"..tostring(projectIndex).." buildingIndex:"..tostring(buildingIndex).." bCanceled:"..tostring(bCanceled))
if pPlayer:IsHuman() and string.find(sProjectTypeCompleted, "PROJECT_REMOVE_DISTRICT_") then
local sCivType = PlayerConfigurations[playerID]:GetCivilizationTypeName();
local pCity = pPlayer:GetCities():FindID( cityID );
local pCityDistricts :table = pCity:GetDistricts();
local sDistrictName = ""
local count = 0; -- first loop is "DISTRICT"
local pPlot;
local x, y;
for n in string.gmatch(sProjectTypeCompleted.."_", "(.-)_") do
if count>2 then
if count==3 then
sDistrictName = sDistrictName..n
else
sDistrictName = sDistrictName.."_"..n
end
end
count = count+1;
end
local sDistrictType = "DISTRICT_"..sDistrictName --GameInfo.Projects[projectIndex].PrereqDistrict; -- removed PrereqDistrict to allow removing it if you only placed the construction site
local iDistrictID = GameInfo.Districts[sDistrictType].Index;
for row in GameInfo.RD_civType_uniqueDistrictType() do
if row.CivType == sCivType and row.ReplacedDistrictType == sDistrictType then
iDistrictID = GameInfo.Districts[row.UniqueDistrictType].Index;
break;
end
end
local pCityDistricts = pCity:GetDistricts();
if pCityDistricts:HasDistrict( iDistrictID ) then
local buildQueue = pCity:GetBuildQueue()
pCityDistricts:RemoveDistrict(iDistrictID); -- does not remove construction site
buildQueue:RemoveDistrict(iDistrictID); -- removes construction site
end
elseif not pPlayer:IsHuman() and string.find(sProjectTypeCompleted, "PROJECT_REMOVE_DISTRICT_") then
print("error: An AI build the remove district project! Write code to disable it for AI!");
end
end
Events.CityProjectCompleted.Add(OnProjectCompleted);
local function OnnProductionChanged(playerID, cityID, productionID, objectID, bCanceled) -- since PrereqDistrict is removed, prevent players from doing a project without having the district placed
if productionID==3 then -- it is a project
local sProjectType = GameInfo.Projects[objectID];
if sProjectType ~= nil then
productionName = sProjectType.ProjectType;
if string.find(productionName, "PROJECT_REMOVE_DISTRICT_") then
local pPlayer = Players[playerID];
local sCivType = PlayerConfigurations[playerID]:GetCivilizationTypeName();
local pCity = pPlayer:GetCities():FindID( cityID );
local pCityDistricts :table = pCity:GetDistricts();
local sDistrictName = ""
local count = 0; -- first loop is "DISTRICT"
for n in string.gmatch(productionName.."_", "(.-)_") do
if count>2 then
if count==3 then
sDistrictName = sDistrictName..n
else
sDistrictName = sDistrictName.."_"..n
end
end
count = count+1;
end
local sDistrictType = "DISTRICT_"..sDistrictName --GameInfo.Projects[projectIndex].PrereqDistrict; -- removed PrereqDistrict to allow removing it if you only placed the construction site
local iDistrictID = GameInfo.Districts[sDistrictType].Index;
for row in GameInfo.RD_civType_uniqueDistrictType() do
if row.CivType == sCivType and row.ReplacedDistrictType == sDistrictType then
-- print(row.UniqueDistrictType .." unique ");
iDistrictID = GameInfo.Districts[row.UniqueDistrictType].Index;
break;
end
end
local pCityDistricts = pCity:GetDistricts();
local buildQueue = pCity:GetBuildQueue();
if not pCityDistricts:HasDistrict( iDistrictID ) then
buildQueue:FinishProgress();
else -- if there is already a construction site or finished
local pDistrict = pCityDistricts:GetDistrict(iDistrictID)
if pDistrict~=nil and not pDistrict:IsComplete() then -- if it is still under construction, allow instant removal. otherwise continue production with normal production costs to remove
buildQueue:FinishProgress();
end
end
end
end
end
-- print("production changed "..playerID..", productionID: "..tostring(productionID).." objectID:"..tostring(objectID).." bCanceled:"..tostring(bCanceled));
end
Events.CityProductionChanged.Add(OnnProductionChanged);