Resource Generator (Lua component)

Deep_Blue

Knight
Joined
Aug 2, 2005
Messages
750
.
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.
    .
  • 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&#8217;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}

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.
 

Attachments

  • Luxury_added_small_map.jpg
    Luxury_added_small_map.jpg
    43.2 KB · Views: 10,057
  • Strategic_Resource_Added_Standard_Map.jpg
    Strategic_Resource_Added_Standard_Map.jpg
    119 KB · Views: 10,061
  • Boundaries_example.jpg
    Boundaries_example.jpg
    46.1 KB · Views: 9,810
Versions History

Version 4.0
Spoiler :

  • Fixed a problem where resources are regenerated again when a saved game is loaded..

Version 3.0
Spoiler :

  • Added Advanced Options to Exclude particular Terrain or Feature types.
  • Added Support for Only Flat Lands and Only Hills conditions.
  • Max Latitude in resource xml is now checked when distributing the resources.

Version 2.0
Spoiler :

  • Added full support for Strategic Resources.
  • Integrated Pre-game resource option (sparse,Abundant...etc) in Quantity Formula.
  • Integrated Number of Civilizations in Quantity Formula.
  • Quantity formula is now balanced for each resource type (Luxury,Strategic,Bonus).
  • Added support for defined Quantity types in xml file. Strategic resources are now distributed according to these quantity types.
  • Improved the precision of map scanning by increasing maximum map divisions from 9 regions to 320 regions.
 
* To do: I will add a full support for strategic resources with customizable conditions in next versions.
[/COLOR]

That would be awesome. I tested it with a new strategic resource on a duel map. only 1 was there. So the full support for strategic resources is a nice thing to see.

Another question: do you know if there will appear an icon for a new strategic resource on the top panel?

-nudden
 
So the full support for strategic resources is a nice thing to see.

I am planning to add this soon.

Another question: do you know if there will appear an icon for a new strategic resource on the top panel?

Customizing the icons on top panel is a tricky task it was discussed here, but I think I may have a workaround for this problem and I will post the method if it works.
 
Interesting. I have a question, though: Do I understand correctly that your new resources are added in addition to vanilla's luxuries?
 
Interesting. I have a question, though: Do I understand correctly that your new resources are added in addition to vanilla's luxuries?

yes your new resource is added after the map is generated with all vanilla resources.

If you want to delete a resource from vanilla you have to do that manually in "AssignStartingPlots".
 
So the luxuries don't follow the regionalisation pattern of the vanilla game? I like that pattern. I was working on "fixing" AssignStartingPlots a while back, for use as a "depended-on" mod (once they actually make that feature work). I had to pause for health and real-life reasons, but I intend to get back to it, and I think I'd worked out the actual logic for assigning resources; it should be possible to add more to the pool used in that file.

The bigger (as in more intractable) problem for true integration is the inability to add new FontIcons/IconFonts. The changelist for the upcoming patch seems to suggest this might be fixed.
 
So the luxuries don't follow the regionalisation pattern of the vanilla game? I like that pattern. I was working on "fixing" AssignStartingPlots a while back, for use as a "depended-on" mod (once they actually make that feature work). I had to pause for health and real-life reasons, but I intend to get back to it, and I think I'd worked out the actual logic for assigning resources; it should be possible to add more to the pool used in that file.

That is my concern, too. If you add a couple of luxuries you will end up with more and more happiness.
 
I know that bypassing AssignStartingPlots may result in unbalanced happiness, personally my fix to this is that for each 3 added luxuries I reduce happiness for all luxuries by 1. Which means if I add 3 luxuries the happiness from every luxury in the game will be reduced to 4 instead of 5.

anyway this is a good point I am thinking about implementing luxury balance functionality to future versions.
 
I know that bypassing AssignStartingPlots may result in unbalanced happiness, personally my fix to this is that for each 3 added luxuries I reduce happiness for all luxuries by 1. Which means if I add 3 luxuries the happiness from every luxury in the game will be reduced to 4 instead of 5.

anyway this is a good point I am thinking about implementing luxury balance functionality to future versions.

That sounds like a pretty sensible solution. I didn't mean to badmouth this mod, by the way, it certainly is very helpful for people who want to add new resources without having to slog through AssignStartingPlots (which is not a mission for the faint-hearted). :goodjob:

It's probably prudent to tell them to be careful not to overdo things, though.
 
Will it be possible to put a qty over a luxury resource just like a strategic resources ?

btw in your example you forgot to add xml for Improvement_ResourceType_Yields and Resource_Flavors.
 
interesting, are you able to modify the amount of a resource per resource. For example, can you change the amount of oil to say 7 or 8 using XML instead of LUA now? This would be nice.
 
Will it be possible to put a qty over a luxury resource just like a strategic resources ?

I dont think so.

btw in your example you forgot to add xml for Improvement_ResourceType_Yields and Resource_Flavors.

Yes I forgot to include Improvement_ResourceType_Yields, but Resource_Flavors were included.
 
interesting, are you able to modify the amount of a resource per resource. For example, can you change the amount of oil to say 7 or 8 using XML instead of LUA now? This would be nice.


yes you can change quantity for any strategic resource in CIV5Resources.xml at Resource_QuantityTypes:

Spoiler :
Code:
<Resource_QuantityTypes>
		<Row>
			<ResourceType>RESOURCE_IRON</ResourceType>
			<Quantity>6</Quantity>
		</Row>
		<Row>
			<ResourceType>RESOURCE_IRON</ResourceType>
			<Quantity>2</Quantity>
		</Row>
		<Row>
			<ResourceType>RESOURCE_HORSE</ResourceType>
			<Quantity>4</Quantity>
		</Row>
		<Row>
			<ResourceType>RESOURCE_HORSE</ResourceType>
			<Quantity>2</Quantity>
		</Row>
		<Row>
			<ResourceType>RESOURCE_COAL</ResourceType>
			<Quantity>6</Quantity>
		</Row>
		<Row>
			<ResourceType>RESOURCE_OIL</ResourceType>
			<Quantity>5</Quantity>
		</Row>
		<Row>
			<ResourceType>RESOURCE_ALUMINUM</ResourceType>
			<Quantity>5</Quantity>
		</Row>
		<Row>
			<ResourceType>RESOURCE_URANIUM</ResourceType>
			<Quantity>2</Quantity>
		</Row>
		<Row>
			<ResourceType>RESOURCE_URANIUM</ResourceType>
			<Quantity>1</Quantity>
		</Row>
	</Resource_QuantityTypes>
 
Is this usable as a normal mod then, or do I have to do something to use it?
Sorry, I'm not a programmer, just a consumer. :undecide:

This is not a mod but a lua component that you can use in your own mod. The usage process is simple you just use:

OnMapResourceGenerator("RESOURCE_MyResource");

read my first post for details.
 
Top Bottom