How do I loop through all tiles to check terrain type?

ww2commander

Emperor
Joined
Aug 23, 2003
Messages
1,243
Location
Australia
I was hoping someone can point me in the right direction on how I loop through all plots on my custom map to check the terrain type. I want to change the terrain type to something else depending on a specific turn (i.e. from summer to winter results in grassland turning into snow terrain).

The closest I came to something was this thread http://forums.civfanatics.com/showthread.php?t=435838...but they never explained how they loop through each plot or provide a complete example of what their discussing :(

I am a complete novice at LUA and am trying to learn things slowly. I spent countless hours searching the forum and looking at various other types of examples but cant figure it out.
 
Couple ways to do it, but I use:

Code:
	for iPlot = 0, Map.GetNumPlots() - 1 do
		local plot = Map.GetPlotByIndex(iPlot)

		local plotType = plot:GetPlotType()
		local featureType = plot:GetFeatureType()
		local improvementType = plot:GetImprovementType()
		
		--etc...

	end

You should be looking in other's mods for this kind of stuff. Download all the big ones. Then install Windows Grep. Then search for "plot" or whatever you happen to be working on at any time. Forums are less helpful for specific examples.

I'm not sure if graphics for plot type update correctly. I haven't tried it but I believe Spatz or someone else said it doesn't (until you re-load game). Features and Improvements update just fine though.
 
Much thanks Pazyryk. :goodjob:

I read about the refresh issue and I can live with the reload workaround.

I am trying to build a WWII Eastern Front scenario and need to write a LUA script that check the particular turn and then changes the terrain from summer to winter types.

I know such a large mod is a bit too much to bite off in one go but I have tried it before in Civ 3 and 4 without much luck due to limitations. At this stage, Civ 5 seems to offer most of the capability I require to make such a mod possible.

I don't want to ask people to help out with coding as there is too much on everyone's modding plate. Slowly I will figure it out through trial and error and hopefully learn how to program LUA.

Hopfeully my mod will see the light of day!
 
I'm not sure if graphics for plot type update correctly. I haven't tried it but I believe Spatz or someone else said it doesn't (until you re-load game). Features and Improvements update just fine though.

Correct. Features and Improvements add and remove graphics just fine. Resources are iffy; you can add them just fine, but removing doesn't always remove the graphic (at least until the next time you reload). Terrains don't update until you reload.
Note that I'm referring to the art definitions involved. If you decide to use the art definition for Cotton to represent your new improvement, it'll work just fine when the improvement is first placed, but it will NOT remove the graphic if the improvement is later deleted, because there's no "erase" function for resource graphics.

If you want to add seasonal art variations, then you could use a Feature overlay (like with Ice), but then you wouldn't be able to have Forests, Jungles, etc. on the same tiles. It'd be possible to add new Features with the same stats as Forests or Jungles, but then you run into problems with things like buildings that boost the yields of nearby forests/jungles (like the University). It'd be a pretty big headache.

Basically, every plot in Civ5 has five variables: Plot Type (land/sea/hill/mountain), Terrain Type (grassland, plain, desert, tundra, snow, lake, coast, ocean), Resource Type, Feature Type (marsh, forest, jungle, flood plain, atoll, ice, fallout, or natural wonders), and Improvement Type. Each hex can have no more than one value in any of these five categories, and every hex has a value for the first two. Adjusting Plot or Terrain type in Lua never updates graphics until the next time you load the game. Adjusting Resource Type only works when adding, not subtracting, and Feature or Improvement can be added or removed whenever you want.
 
Thank you very much for all that info Spatzimaus :goodjob:

That will definetily help me and the next person that comes along to work in this area of the game. I wish the pedias out there would be updated with more user friendly information in this form.

The coding to do this was indeed more simpler than I thought. I got it to work and it does change the terrain except for graphics as you both mentioned (tooltips show correct description for terrain).

Now I can implement a weather system in my mod which was critical to the Eastern Front campaign during WWII.

I am ok with having to save and load as this is a small sacrifice for an element I see as critical to gameplay and atmosphere. I will link this in with a popup message in similar style to Nemo's Red Front for Civ2 which got users to save/load during season changes.
 
Adjusting Resource Type only works when adding, not subtracting,

It's strange that these work differently than improvements. Both resource and improvement art are specified in the ArtDefine_LandmarkTypes DB table. So you have "deer without improvement", "deer with camp", "deer with pillaged camp" and so on. I wonder if you could get the resource art to go away by adding a "non-compatible" improvement (say a mine on deer) and then removing it.
 
It's strange that these work differently than improvements. Both resource and improvement art are specified in the ArtDefine_LandmarkTypes DB table. So you have "deer without improvement", "deer with camp", "deer with pillaged camp" and so on. I wonder if you could get the resource art to go away by adding a "non-compatible" improvement (say a mine on deer) and then removing it.

I know, but I think it's that the art define itself is simply constructed in a way that it doesn't allow for this sort of erasure. The internal code might be looking for a very specific "here's the negative area you'd need to clear when removing this" sort of thing, and it's just not present in that particular type of xml file. All we'd really need is a set of Redraw functions; redraw the whole map, redraw a single hex (which'd also involve redrawing the adjacent six hexes, given how terrain types bleed), and so on. Without those, though, we're stuck with all of these problems, where the only way to force a redraw is to reload the game.

As for the second idea, I don't think so. I've tried something very similar to that, and you're left with overlapping artwork, where it just displays both. That's why I think the problem is purely a failure in the "remove this art definition" stage. For instance, I'd used a resource (Spices, I think) as placeholder art for my "Plant a Forest" Improvement, which'd then be replaced by an actual Forest (Feature). You'd now see a forest that had spices sticking out of it, even after placing a lumbermill on the forest. The same went with Jungles and the Cotton graphic, even after adding a trading post. It's low-impact enough that I just ignore it in practice, since no one will mistake that forest for an actual spice deposit.
 
For instance, I'd used a resource (Spices, I think) as placeholder art for my "Plant a Forest" Improvement, which'd then be replaced by an actual Forest (Feature). You'd now see a forest that had spices sticking out of it, even after placing a lumbermill on the forest. The same went with Jungles and the Cotton graphic, even after adding a trading post. It's low-impact enough that I just ignore it in practice, since no one will mistake that forest for an actual spice deposit.

Yes but you're replacing a resource with a feature here. That is not the same as replacing a resource with an improvement (specifically, an improvement that never occurs on that resource). Actually, I don't know where Feature art is held (don't see it anywhere in DebugDatabase) but it is definitely not in ArtDefine_Landmarks where both resources and improvements are held. I believe that Features are drawn independently from Resources/Improvements. However, I don't believe that the engine is capable of drawing a mine+deer because there is no such art resource. There is "deer with camp", "deer with pillaged camp", "gold with mine", "coal with mine", etc., but there is no "deer with mine" art resource.

Now your lumbermill and trading post examples seems like a counter to what I'm saying, but these are handled differently in ArtDefine_Landmarks then camps or mines. Specifically, these have a column (forgot the name, perhaps "Resource") that has the entry RESOURCE_ALL. I think this means that they can exist with any resource. Camps and mines are different. In fact, there are no "camp" art files because it is all embedded with deer, elephant and fur art. Mines are different and have unique art when combined with each possible resource.

Now, even though resources and improvements are handled together in ArtDefine_Landmarks, they are clearly working differently in terms of updating. What I imagine happens is that when you change an improvement (or feature) then the engine fires off a command to graphics to do something. When you remove a resource, the engine just sits there doing nothing. But the way resource art is basically embedded with some improvements (but not others) I have the feeling that you can "trick" the engine into updating resource art. Perhaps. Or not.
 
Yes but you're replacing a resource with a feature here.

No, I'm replacing an Improvement that happens to be using the art definition for a resource. I've similarly made Features that use art definitions from Improvements (the Monolith in my mod), and I tried a resource that used the graphic of a Feature at one point. My point was that based on the combinations I'd used, the problem seems to be integral to the art definition itself, regardless of what type of entity is using that particular definition at the moment; if the Spices art definition simply has no option for deletion, while all Improvement and Feature definitions do, then it'd explain what I've seen. Of course, it's possible that the problem was purely caused by the fact that I was mixing definition types, so who knows. That's the aggravating part; obviously we can remove resource deposits (since FireTuner lets you do that with a simple right-click in the resource plopper), but doing so through Lua leaves that artifact.

However, I don't believe that the engine is capable of drawing a mine+deer because there is no such art resource.

Correct. I've run into this in my own mod; even if you try modding that XML file, it won't allow new combinations despite the fact that for some resources there doesn't seem to be any combinatoric graphics. (That is, A+B is simply the A graphic with the B graphic laid on top, not A + B + C, where C is something unique to the A+B combo.) I'd wanted to be able to use the uranium graphic for a new water resource that was harvested by an offshore platform, and it just wouldn't work. I ended up having to make an aquatic improvement that used the Mine graphic, which WOULD add correctly to those water-based crystals (although it looks really strange).

In fact, there are no "camp" art files because it is all embedded with deer, elephant and fur art. Mines are different and have unique art when combined with each possible resource.

I've tried spoofing this by having it switch to other combinations (so Uranium + Offshore Platform would use the art definition with the Oil+Offshore combo graphic instead), and it didn't work. I put these attempts on hold for a while, since the game engine has bigger problems with water resources (specifically, that if you add a new water resource the AI will not know how to use a work boat to harvest it), but I've mostly got those straightened out now. So I might try again soon, or at least try to figure out how to create a better combination graphic.

But the way resource art is basically embedded with some improvements (but not others) I have the feeling that you can "trick" the engine into updating resource art. Perhaps. Or not.

The problem with this is that the game seems to use a lot of false entries that its engine knows about but which aren't apparent in the XML. The best examples of this are hills and mountains; they're in the Features file in a "Fake Features" table simply to enable their art definitions, but they're NOT treated as Features by the actual code; Hill and Mountain are, instead, Plot types to allow stacking with other Features. Obviously, the game has no problem placing a forest on a hill, so it looks to me like there's quite a bit of internal overriding being done. I'm not saying that it's not worth trying, but there's just a disturbingly large amount of logic that we don't have access to.

But hey, it's worth a shot. If I had the time I'd try it myself, but I'm going to be pretty busy for the next week or so.
 
Back
Top Bottom