Hello community,
I'm working on a modded improvement, it can be placed outside territory but I had an issue where the improvement provided vision when it should not have. Initally I asked how I could fix this, but I managed to figure it out on my own, so I will leave my code here as a solution for future modders.
station_placed.lua
station_removed.lua
These are some additional threads I was looking at in an attempt to solve it myself
Absolutely! In CvPlot.cpp, in CvPlot::changeVisibilityCount() there should be the following -
After this it does stuff reserved only for tiles that are currently visible. You should see a call to setRevealed(eTeam, true, false, NO_TEAM, bUpatePlotGroups) just below there. The first bool "true" is the new status of revealed.
That if statement about OldVisible is where the game would handle stuff that happens to a tile...
Good day
I'm working on a modded improvement, it can be placed outside territory but I had an issue where the improvement provided vision when it should not have. Initally I asked how I could fix this, but I managed to figure it out on my own, so I will leave my code here as a solution for future modders.
station_placed.lua
-- Initialize the Event Listener for improvement placement
function OnImprovementPlaced(x, y, ImprovementType, playerID)
local pPlot = Map.GetPlot(x, y); --plot obj
local improvementIndex = GameInfo.Improvements["IMPROVEMENT_GAS_STATION"].Index;
if (pPlot:GetImprovementType() == improvementIndex) then
local plotIndex = Map.GetPlot(x, y):GetIndex(); --gets index of tile using x,y
local pv = PlayerVisibilityManager.GetPlayerVisibility(playerID); -- gets player visibility obj from the manager
local count = pv:GetVisibilityCount(plotIndex);
pv:ChangeVisibilityCount(plotIndex, -1); --uses function on pv obj to decrement count
local adjPlots = Map.GetAdjacentPlots(x, y); --generates array of adj tiles to current tile
for i = 1, 6, 1 do --always have 6 adj tiles
local adjIndex = adjPlots[i]:GetIndex(); --getting index of current adj tile
local pv = PlayerVisibilityManager.GetPlayerVisibility(playerID); --getting pv object
local count = pv:GetVisibilityCount(adjIndex);
pv:ChangeVisibilityCount(adjIndex, -1);
end
end
end
-- Hook into the improvement placement event
Events.ImprovementAddedToMap.Add(OnImprovementPlaced);
station_removed.lua
function OnImprovementRemoved(x, y, playerID)
local pPlot = Map.GetPlot(x, y); --plot obj
local plotIndex = Map.GetPlot(x, y):GetIndex(); --gets index of tile using x,y
local pv = PlayerVisibilityManager.GetPlayerVisibility(playerID); -- gets player visibility obj from the manager
local count = pv:GetVisibilityCount(plotIndex);
if(count < 1) then --if tile not visibile
pv:ChangeVisibilityCount(plotIndex, 1); --uses function on pv obj to increment count
end
local adjPlots = Map.GetAdjacentPlots(x, y); --generates array of adj tiles to current tile
for i = 1, 6, 1 do --always have 6 adj tiles
local adjIndex = adjPlots[i]:GetIndex(); --getting index of current adj tile
local pv = PlayerVisibilityManager.GetPlayerVisibility(playerID); --getting pv object
local count = pv:GetVisibilityCount(adjIndex);
if(count < 1) then --if tile not visibile
pv:ChangeVisibilityCount(adjIndex, 1);
end
end
end
-- Hook into the improvement placement event
Events.ImprovementRemovedFromMap.Add(OnImprovementRemoved);
These are some additional threads I was looking at in an attempt to solve it myself
Do you know if is it possible to "link' the visibility update mechanic with the discovery of some research?
Absolutely! In CvPlot.cpp, in CvPlot::changeVisibilityCount() there should be the following -
Code:
if(bOldVisible == isVisible(eTeam))
return;
After this it does stuff reserved only for tiles that are currently visible. You should see a call to setRevealed(eTeam, true, false, NO_TEAM, bUpatePlotGroups) just below there. The first bool "true" is the new status of revealed.
That if statement about OldVisible is where the game would handle stuff that happens to a tile...
Hi guys,
I have a small problem. Im making outpost mode where forts get adjacent territory. I added feature where if fort is pillaged or removed and its territory is not directly connected to any city, it will lose it. But I have problem with visibility. It removes owner, but area keeps visible. I tried to use RevealAll and then UnrevealAll in case there is some update problem or something, but it keeps visible even after that. Only when someone builds city in that area it gets removed. I tried to look for some functions or events that handle this but I found only some get functions and UI...
I have a small problem. Im making outpost mode where forts get adjacent territory. I added feature where if fort is pillaged or removed and its territory is not directly connected to any city, it will lose it. But I have problem with visibility. It removes owner, but area keeps visible. I tried to use RevealAll and then UnrevealAll in case there is some update problem or something, but it keeps visible even after that. Only when someone builds city in that area it gets removed. I tried to look for some functions or events that handle this but I found only some get functions and UI...
- Geekob
- help improvements modding plot functions visible
- Replies: 13
- Forum: Mod Creation Help
This will reveal map when game starts if that;'s what you want:
Code:
function Initialize()
if (Game.GetLocalPlayer() ~= -1) then
local pVis = PlayersVisibility[Game.GetLocalPlayer()];
for iPlotIndex = 0, Map.GetPlotCount()-1, 1 do
pVis:ChangeVisibilityCount(iPlotIndex, 1);
end
end
end
Initialize();
Good day
Last edited: