Deep_Blue
Knight
- Joined
- Aug 2, 2005
- Messages
- 750
.
Changes in Version 4.0
Resource Generator (V4.0)
This LUA component can be used to add new resources to the game without modifying "AssignStartingPlots.lua", this component is a standalone resource generator that uses its own algorithm to distribute the resource on the map. The new resource will be automatically spread on the map according to map size and defined terrain & feature Booleans in resource xml file.
This utility makes adding new resources more modular with no conflict between different MODs, also this utility is a workaround for adding a new luxury which is currently restricted in the game and hard to implement.
Adding a new resource was very painful because you have to edit "AssignStartingPlots.lua" which is a messy and big file and the code you have to add for the new resource is scattered all over the file. Also adding a new luxury was very hard that is because luxuries in the code are restricted (to make the luxuries balanced as said in file comments) so I found a workaround for this which is to add the resource manually on the map. Then the idea evolved and I decided to make a separate code to add any new resource easily without editing any lua files in the game.
This component make use of the defined Terrain booleans and feature booleans in resource xml file. These values are ignored in "AssignStartingPlots" and are redefined in the code and implemented in a rigid way that doesn't take in account changes of terrain/feature booleans in xml files.

1. Features:

2. Algorithm highlights:

3.Example:
Lets say we want to add Chocolate as a new luxury resource:
1- Add xml file for chocolate, here is an example:
Notes:
- I borrowed spices icons for this fast example, if you want to add your cutom resource icon check this guide by Kyoss.
- I am not expert in adding new Art so I borrowed spices art in <ArtDefineTag>
- In this example Chocolate is allowed on Grass Terrain , Jungle Feature and Marsh feature. You can customize this as you want.
2- Add Resource generator Lua files to your project:
In ModBuddy Right-click on each file then select properties and set 'Import into VFS' to 'True'
3- Edit "AddNewResource.lua" and use the following function to add our new luxury resource:
you can add as many resources as you want, for example:
4- In Mod properties add the following Content:
That’s it.. when you start a new game the resource will be automatically distributed on the map.

4. Advanced Options
Added new advanced options for more customization:
4.1 Terrain & Feature Excludes:
you can use additional option with the function:
4.2 Special Conditions
Only Flat Lands: the resource will be only placed on Flat Lands.
Only Hills: the resource will be only placed on Hills.

5.Screen Shots:
Download Link

Notes
*If you want to modify the algorithm:
I tried to explain everything in the code in details to make it easier to modify but if you want any help then please post here. Please credit me if you are using a modified version of my code.
Changes in Version 4.0
Spoiler :
- Fixed a problem where resources are regenerated again when a saved game is loaded.
Resource Generator (V4.0)
This LUA component can be used to add new resources to the game without modifying "AssignStartingPlots.lua", this component is a standalone resource generator that uses its own algorithm to distribute the resource on the map. The new resource will be automatically spread on the map according to map size and defined terrain & feature Booleans in resource xml file.
This utility makes adding new resources more modular with no conflict between different MODs, also this utility is a workaround for adding a new luxury which is currently restricted in the game and hard to implement.
Adding a new resource was very painful because you have to edit "AssignStartingPlots.lua" which is a messy and big file and the code you have to add for the new resource is scattered all over the file. Also adding a new luxury was very hard that is because luxuries in the code are restricted (to make the luxuries balanced as said in file comments) so I found a workaround for this which is to add the resource manually on the map. Then the idea evolved and I decided to make a separate code to add any new resource easily without editing any lua files in the game.
This component make use of the defined Terrain booleans and feature booleans in resource xml file. These values are ignored in "AssignStartingPlots" and are redefined in the code and implemented in a rigid way that doesn't take in account changes of terrain/feature booleans in xml files.
1. Features:
- Add any new resource easily and with 1 line of code.
. - Automatically distributes the resource on the map using custom "ON Map Resource Generator" algorithm.
. - Adds quantity for a new resources depending on:
- Map size
[*]Pre-game resource option (sparse,Abundant...etc)
[*]Number of civilizations in the game.
- Map size
- Quantity formulas for added resources were carefully balanced after getting in game statistics for all map sizes, resource options, number of civilizations.
. - Full support for defined Quantity types in xml file. Strategic resources are distributed accorded to these quantity types.
. - Will use the defined Terrain Booleans and Feature Booleans in resource xml file. (These values are ignored in "AssignStartingPlots" !!)
. - Advanced Options to Exclude particular Terrain or Feature types. (more details in section#4)
. - Support for Only Flat Lands and Only Hills conditions (more details in section#4)
. - Does not modify any lua file in the game which makes adding a new resource more modular with no conflict between different mods.
.
2. Algorithm highlights:
Spoiler :
- Number of resources distributed is proportional to Map size, pre-game resource options, number of civilizations.
- Custom quantity formula for each resource type (luxury,strategic,bonus) these formulas were derived after collecting large number of stats from the game.
- For strategic resources quantity types are obtained from xml file, then the code will pick a quantity type randomly for each resource placement until all assigned quantity is depleted (example: for 12 Iron quantity distribution maybe= {6,6} or {2,2,6,2} .. and so on)
- Map is divided into a number of regions of equal areas and no more than 1 resource per region is placed.
- Maximum number of regions allowed is 320 which means maximum number for a resource to be distributed on any map is 320.
- The resource is placed according to defined Terrain Booleans and Feature Booleans in resource xml file.
- Randomness added when checking the plots of a region so that the resources don't get clustered on the edges of the regions in some cases (plots are not checked in order but checked according to a random series).
3.Example:
Lets say we want to add Chocolate as a new luxury resource:
1- Add xml file for chocolate, here is an example:
Spoiler :
Code:
<GameData>
<Resources>
<Row>
<Type>RESOURCE_CHOCOLATE</Type>
<Happiness>5</Happiness>
<Description>TXT_KEY_RESOURCE_CHOCOLATE</Description>
<Civilopedia>TXT_KEY_CIV5_RESOURCE_CHOCOLATE_TEXT</Civilopedia>
<Help>TXT_KEY_RESOURCE_CHOCOLATE_HELP</Help>
<ResourceClassType>RESOURCECLASS_LUXURY</ResourceClassType>
<ArtDefineTag>ART_DEF_RESOURCE_SPICES</ArtDefineTag>
<PlacementOrder>3</PlacementOrder>
<ConstAppearance>50</ConstAppearance>
<MinAreaSize>3</MinAreaSize>
<MaxLatitude>90</MaxLatitude>
<RandApp1>25</RandApp1>
<RandApp2>25</RandApp2>
<Player>67</Player>
<Unique>4</Unique>
<IconString>[ICON_RES_SPICES]</IconString>
<PortraitIndex>17</PortraitIndex>
<IconAtlas>RESOURCE_ATLAS</IconAtlas>
</Row>
</Resources>
<Resource_YieldChanges>
<Row>
<ResourceType>RESOURCE_CHOCOLATE</ResourceType>
<YieldType>YIELD_GOLD</YieldType>
<Yield>2</Yield>
</Row>
</Resource_YieldChanges>
<Resource_Flavors>
<Row>
<ResourceType>RESOURCE_CHOCOLATE</ResourceType>
<FlavorType>FLAVOR_HAPPINESS</FlavorType>
<Flavor>10</Flavor>
</Row>
</Resource_Flavors>
<Resource_TerrainBooleans>
<Row>
<ResourceType>RESOURCE_CHOCOLATE</ResourceType>
<TerrainType>TERRAIN_GRASS</TerrainType>
</Row>
</Resource_TerrainBooleans>
<Resource_FeatureBooleans>
<Row>
<ResourceType>RESOURCE_CHOCOLATE</ResourceType>
<FeatureType>FEATURE_JUNGLE</FeatureType>
</Row>
<Row>
<ResourceType>RESOURCE_CHOCOLATE</ResourceType>
<FeatureType>FEATURE_MARSH</FeatureType>
</Row>
</Resource_FeatureBooleans>
<Language_en_US>
<Row Tag="TXT_KEY_RESOURCE_CHOCOLATE">
<Text>Chocolate</Text>
</Row>
<Row Tag="TXT_KEY_RESOURCE_CHOCOLATE_HELP">
<Text>Luxury resource</Text>
</Row>
<Row Tag="TXT_KEY_CIV5_RESOURCE_CHOCOLATE">
<Text>Chocholate</Text>
</Row>
<Row Tag="TXT_KEY_CIV5_RESOURCE_CHOCOLATE_TEXT">
<Text>
Chocolate Is Yummy .. :)
</Text>
</Row>
<Row Tag="TXT_KEY_CIV5_RESOURCE_CHOCOLATE_TEXT1">
<Text>Always Visible</Text>
</Row>
<Row Tag="TXT_KEY_CIV5_RESOURCE_CHOCOLATE_TITLE1">
<Text>Technology Required to See</Text>
</Row>
<Row Tag="TXT_KEY_CIV5_RESOURCE_CHOCOLATE_TITLE2">
<Text>Location</Text>
</Row>
<Row Tag="TXT_KEY_CIV5_RESOURCE_CHOCOLATE_TITLE3">
<Text>Improvement</Text>
</Row>
<Row Tag="TXT_KEY_CIV5_RESOURCE_CHOCOLATE_TITLE4">
<Text>Yield Bonus</Text>
</Row>
<Row Tag="TXT_KEY_CIV5_RESOURCE_CHOCOLATE_TITLE5">
<Text>Happiness Bonus</Text>
</Row>
</Language_en_US>
<Improvement_ResourceTypes>
<Row>
<ImprovementType>IMPROVEMENT_PLANTATION</ImprovementType>
<ResourceType>RESOURCE_CHOCOLATE</ResourceType>
</Row>
</Improvement_ResourceTypes>
</GameData>
Notes:
- I borrowed spices icons for this fast example, if you want to add your cutom resource icon check this guide by Kyoss.
- I am not expert in adding new Art so I borrowed spices art in <ArtDefineTag>
- In this example Chocolate is allowed on Grass Terrain , Jungle Feature and Marsh feature. You can customize this as you want.
2- Add Resource generator Lua files to your project:
- AddNewResource.lua
- ResGenerator.lua
- ResGeneratorFunctions.lua
- SaveUtils.lua
In ModBuddy Right-click on each file then select properties and set 'Import into VFS' to 'True'
3- Edit "AddNewResource.lua" and use the following function to add our new luxury resource:
OnMapResourceGenerator("RESOURCE_CHOCOLATE");
you can add as many resources as you want, for example:
OnMapResourceGenerator("RESOURCE_MyResource1");
OnMapResourceGenerator("RESOURCE_MyResource2");
etc.
4- In Mod properties add the following Content:
Type: InGameUIAddin
FileName: AddNewResource.lua
That’s it.. when you start a new game the resource will be automatically distributed on the map.
4. Advanced Options
Added new advanced options for more customization:
4.1 Terrain & Feature Excludes:
you can use additional option with the function:
OnMapResourceGenerator("RESOURCE_MyResource" , Excludes )
Excludes is a table containing any Terrain or feature you do not want, put them in any order but they must have exact Type string in Civ5Terrains.xml and Civ5Features.xml otherwise they will be ignored.
Example: Copper not allowed on Desert or Jungle:
OnMapResourceGenerator("RESOURCE_COPPER" , {"TERRAIN_DESERT", "FEATURE_JUNGLE"} )
you can put as many Terrain & Feature Types you want in the table.
If you don't want to use advanced options just use the regular:
OnMapResourceGenerator("RESOURCE_COPPER")
4.2 Special Conditions
Only Flat Lands: the resource will be only placed on Flat Lands.
In the resource Xml set Flatlands tag to true as in the following example:
Spoiler :Code:<Resources> <Row> <Type>RESOURCE_CHOCOLATE</Type> <Happiness>5</Happiness> <Description>TXT_KEY_RESOURCE_CHOCOLATE</Description> <Civilopedia>TXT_KEY_CIV5_RESOURCE_CHOCOLATE_TEXT</Civilopedia> <Help>TXT_KEY_RESOURCE_CHOCOLATE_HELP</Help> <ResourceClassType>RESOURCECLASS_LUXURY</ResourceClassType> <ArtDefineTag>ART_DEF_RESOURCE_SPICES</ArtDefineTag> <PlacementOrder>3</PlacementOrder> <ConstAppearance>50</ConstAppearance> [B][COLOR="Blue"]<Flatlands>true</Flatlands>[/COLOR][/B] <MinAreaSize>3</MinAreaSize> <MaxLatitude>90</MaxLatitude> <RandApp1>25</RandApp1> <RandApp2>25</RandApp2> <Player>67</Player> <Unique>4</Unique> <IconString>[ICON_RES_SPICES]</IconString> <PortraitIndex>17</PortraitIndex> <IconAtlas>RESOURCE_ATLAS</IconAtlas> </Row> </Resources>
Only Hills: the resource will be only placed on Hills.
In the resource Xml set Hills tag to true as in the following example:
Spoiler :Code:<Resources> <Row> <Type>RESOURCE_CHOCOLATE</Type> <Happiness>5</Happiness> <Description>TXT_KEY_RESOURCE_CHOCOLATE</Description> <Civilopedia>TXT_KEY_CIV5_RESOURCE_CHOCOLATE_TEXT</Civilopedia> <Help>TXT_KEY_RESOURCE_CHOCOLATE_HELP</Help> <ResourceClassType>RESOURCECLASS_LUXURY</ResourceClassType> <ArtDefineTag>ART_DEF_RESOURCE_SPICES</ArtDefineTag> <PlacementOrder>3</PlacementOrder> <ConstAppearance>50</ConstAppearance> [B][COLOR="Blue"]<Hills>true</Hills>[/COLOR][/B] <MinAreaSize>3</MinAreaSize> <MaxLatitude>90</MaxLatitude> <RandApp1>25</RandApp1> <RandApp2>25</RandApp2> <Player>67</Player> <Unique>4</Unique> <IconString>[ICON_RES_SPICES]</IconString> <PortraitIndex>17</PortraitIndex> <IconAtlas>RESOURCE_ATLAS</IconAtlas> </Row> </Resources>
5.Screen Shots:
Spoiler :
Generic map divisions created with boundaries algorithm:

FireTuner output for adding a luxury on small map:

FireTuner output for adding a strategic resource on standard map:
Defined Quantity types for Saltpeter = {2,6}
FireTuner output for adding a luxury on small map:
FireTuner output for adding a strategic resource on standard map:
Defined Quantity types for Saltpeter = {2,6}
Download Link
Notes
*If you want to modify the algorithm:
I tried to explain everything in the code in details to make it easier to modify but if you want any help then please post here. Please credit me if you are using a modified version of my code.