Chopped jungles should turn into grassland instead of plains

TofuSojo

Chieftain
Joined
Sep 28, 2014
Messages
41
Location
Orlando, FL
I'm a new modder and just joined this forum. I hope this is how to do this.

I've been looking through the xml files hoping to find a way to mod it so chopping down a jungle turns it into a grassland hex instead of plains. I know not everyone would want this, but I very much would.

Does anyone know how this could be done? I worry I would have to mess with AssignStartingPlots.lua. I studied that bloated monstrosity, but I hope there is an easier way, ideally through xml.

Thanks
 

Bobert13

Prince
Joined
Feb 25, 2013
Messages
346
I'm pretty confident you'd need to rework map generation to force Grassland under Jungle and manually cludge the Food lower (to avoid 3 Food Jungle tiles). Then upon removal of the Jungle you'd have to update the Food production of the tile(s) back to what it should be. Luckily, ASP.lua does not need to be edited... However, you will need to replace the AddFeatures() function (which I believe is in MapGenerator.lua but I could be wrong; I'd suggest searching it). And you'd need to attach another script via InGameUIAddIn that fires regularly to "de-cludge" the Food change.

To my knowledge, changing terrain types mid-game can't be done (so letting the game keep forcing Plains under Jungle and then changing to Grassland upon clearing wouldn't work).

Edit: Also, I'd like to put in my two cents on the matter which is, Jungle locations typically lack production and while Plains aren't amazing tiles by any stretch, they may be the best you can work with for a given location. For myself, I prefer Plains over Grasslands in a Jungle setting as food isn't typically an issue. Plus there's the matter of super-tile Trading Posts (with too much food available to a Jungle location the Science production could get well out-of-hand).
 

PawelS

Ancient Druid
Joined
Dec 11, 2003
Messages
2,811
Location
Poland
The terrain under Jungles is changed to Plains in FeatureGenerator.lua, function FeatureGenerator:AdjustTerrainTypes() - if you make changes to this function that it doesn't change the terrain to plains, and add the modified version of FeatureGenerator.lua to your mod (with VFS set to true), the terrain under jungles should remain as grassland, at least when using the standard map scripts (custom map scripts may use their own functions for this purpose). Alternatively you can add Lua code to your mod that will change the terrain under jungles back to grassland when starting a game (which will work for all map scripts, and even custom maps), but I'm not sure if it will work properly in terms of graphics - updating the terrain graphics will probably require you to reload the game.

As Bobert13 noticed, you also need to change the yields from Jungle, unless you can accept 3 food from these tiles, which is too much imo.
 

TofuSojo

Chieftain
Joined
Sep 28, 2014
Messages
41
Location
Orlando, FL
Thanks for quick reply! I know C++ but haven't yet started any modding with lua, just xml stuff. PawelS's suggestion sounds simpler plus I like that it would affect all maps and I don't mind having to reload my game once to fix the graphics.

I wouldn't know how to code what you suggest, but I assume it would be along the lines of a loop that looks at each tile on the map and if Jungle is present, change its underlying terrain from Plains to Grassland? If that is only a few lines do you know what they would be or can you recommend a lua tutorial so I figure it out?

Oh and in any case, is there a free lua editor? It is unworkable in ModBuddy due to this terrible delay when typing anything in lua.
 

Bobert13

Prince
Joined
Feb 25, 2013
Messages
346
Pawel and I suggested you do the same thing, only he knew exactly which function in which file was responsible for forcing plains under Jungle. Lua is not hard to adjust to if you already know a programming language (especially C/C++). "Lua for Windows" is one coding extension for Lua, though I don't know if you can find a version intended for use with Lua 3.1 anymore (the version Civ uses). I and many others here would suggest you use notepad++ and use the game's lua.log to debug.
 

PawelS

Ancient Druid
Joined
Dec 11, 2003
Messages
2,811
Location
Poland
The code that changes the terrain under jungles to grassland would be something like this (untested):

Code:
	local width, height = Map.GetGridSize()
	local featureJungle = GameInfoTypes.FEATURE_JUNGLE
	local terrainGrass = GameInfoTypes.TERRAIN_GRASS

	for y = 0, height-1 do
		for x = 0, width-1 do
			local plot = Map.GetPlot(x, y)
			if (plot:GetFeatureType() == featureJungle) then
				plot:SetTerrainType(terrainGrass, false, true)
			end
		end
	end

(It's a modified version of the aforementioned code from FeatureGenerator.lua)

The problem is that I don't know which event to hook it up to, hopefully there are people reading this who can help you in this regard :) Perhaps it's even possible to do it before the graphics is initialized so you won't have to reload the game...

Oh and in any case, is there a free lua editor? It is unworkable in ModBuddy due to this terrible delay when typing anything in lua.

I use jEdit for editing Lua files, it highlights the syntax like ModBuddy but doesn't have the lag. I heard that Notepad++ is good too.
 

TofuSojo

Chieftain
Joined
Sep 28, 2014
Messages
41
Location
Orlando, FL
I'm happy to say I can actually understand that code, seems very straight-forward. Though I'm curious what the 2nd and 3rd arguments in the SetTerrainType function are for?

As for which hook to use, any towards the end of the AssignStartingPlots lua? After all tiles have been assigned? I've not done any lua mods yet, so this is all very new to me, but I got Notepadd++ and am looking for some tutorials on here and elsewhere to start learning it.

Anyone have any idea where the next step might be? Where to have PawelS's function go?
 

PawelS

Ancient Druid
Joined
Dec 11, 2003
Messages
2,811
Location
Poland
Hmm, I think if you add it somewhere in AssignStartingPlots.lua (for example in FixSugarJungles(), where you can put it into the existing for loops instead of creating new ones), it should work properly, probably without the need to reload the game.

I meant adding it to an event using InGameUIAddin, so you don't have to modify AssignStartingPlots.lua, and it would work for non-random maps too. But, as I mentioned earlier, I don't know which event it would be.
 

TofuSojo

Chieftain
Joined
Sep 28, 2014
Messages
41
Location
Orlando, FL
Yeah, I would like to not replace the AssignStartingPlots.lua. Btw, has anyone just remade that monster so that it's readable and mod-friendly? I have the mod More Luxury Resources, which replaces it to add a dozen new luxuries to the game and love it.

I just looked through all the events and the only one that stands out to me as only happening once and at game start is BeforeModsActivate. Might that work? It doesn't have to be perfect and I don't mind having to reload the game to reset graphics. Right now I just want to see some lua modding and get it to work.

So is InGameUIAddin the name of the function I would write in lua or do I create my own name? I assume not or how will the compiler know what it is?
 

PawelS

Ancient Druid
Joined
Dec 11, 2003
Messages
2,811
Location
Poland
Yeah, I would like to not replace the AssignStartingPlots.lua. Btw, has anyone just remade that monster so that it's readable and mod-friendly? I have the mod More Luxury Resources, which replaces it to add a dozen new luxuries to the game and love it.

I don't know if anyone remade it for general modding purposes. I did it for my unreleased mod (G&K version, I'm currently working on BNW version) by removing all functions that place resources on the map and replacing them by a simple and purely random algorithm that takes data from a database table and doesn't take into account things like civ placement at all - perhaps it makes the game less balanced, but also it makes the world feel more natural, without resources artificially placed at starting locations just to help a civ along, which often turns the capitals into super-cities, much better than any other city.

I just looked through all the events and the only one that stands out to me as only happening once and at game start is BeforeModsActivate. Might that work? It doesn't have to be perfect and I don't mind having to reload the game to reset graphics. Right now I just want to see some lua modding and get it to work.

So is InGameUIAddin the name of the function I would write in lua or do I create my own name? I assume not or how will the compiler know what it is?

I found a thread where the question about the right event is asked. It seems you don't need any events - you can just write some code in an InGameUIAddin outside of functions, and it will get executed when the game starts, but also when you load a saved game. This is not a big issue in case of changing the terrain under jungles, it can just increase the load time a little. I think you can add a check if the game turn is 0 to prevent that increase in load times.

If you want to use an event, you create a function with your own name (or without a name at all) and add it to the event. You can find examples of using this method in many existing mods. (Btw there is no compiler as Lua is an interpreted language).
 

TofuSojo

Chieftain
Joined
Sep 28, 2014
Messages
41
Location
Orlando, FL
It worked exactly as you said, your code too. Just had to reload the game and the graphics reloaded, but they had the yields of grassland not plains from the start.

One final question. So if I understand all this, there are two choices. Keep it as it is, a lua script run at game start, which requires a restart to reload graphics OR add a function to AssignStartingPlots.lua to avoid the need to reload graphics. The latter would make my incompatible with any that alters that lua file though, right?
 

PawelS

Ancient Druid
Joined
Dec 11, 2003
Messages
2,811
Location
Poland
There is also a third choice - removing the change of terrain from FeatureGenerator.lua - but it probably won't work with some map scripts that use custom functions for this purpose.

These three are all possible choices according to my knowledge.
 
Top Bottom