Luxury / Strategic Resource Valuation

Recursive

Already Looping
Moderator
Supporter
Joined
Dec 19, 2017
Messages
4,683
Location
Antarctica
As requested, here is the current AI valuation for strategic and luxury resources, converted into pseudocode plaintext.

If forumgoers can suggest/agree on a better valuation, I can code it, within reason.

Strategic Resource - AI is Buying
Code:
Current Game Era = Average era of all majors. 0 in Ancient, 1 in Classical, 2 in Medieval, etc.


STEP 0: Obsolete Resources
- Obsolete resources are worth 0.


STEP 1: Base Value
- Base value is equal to current game era + 2.


STEP 2: Flavor Value
Total Flavor Value starts at 0.
# of Flavor Values starts at 0.

Loop through all AI non-diplomacy flavors. Skip any resource with a flavor value of 0 or less.
- Check what the AI's XML flavor value is for this flavor and add it to the resource value.
- Divide the sum by 5 and then add it to Total Flavor Value.
- Add 1 to # of Flavor Values.

If Total Flavor Value is greater than 0, divide Total Flavor Value by # of Flavor Values, and multiply the base value by the quotient.


STEP 3: GPT Surcharge
Divide the AI's net GPT by 5.

GPT Bonus = floor(0.5 + square root of quotient), or the base value, whichever is lower.

Add the GPT Bonus to the value.


STEP 4: Comparative Military Might
If seller has greater military might than the AI (uses base military might stat used in the score calculation, not strength perception from diplomacy AI), then multiply value by 8 and then divide by 10.


STEP 5: Sneak Attack
If AI is planning to sneak attack the seller, divide the value by 2.


STEP 6: Proximity
If AI's proximity to the seller is CLOSE or NEIGHBORS, multiply the value by 7 and then divide by 10.


STEP 7: Aluminum Bonus
If AI has completed the Apollo Program and the resource is Aluminum, multiply the value by 10 and then divide by 8.


STEP 8: Netherlands AI
- If AI is not Netherlands, skip this check.
- If AI's monopoly % for this resource is greater than or equal to the greatest monopoly % of all players, skip this check.
- If AI does NOT have a copy of this resource (not from an import), skip this check. (??????)
- If this resource's monopoly does not increase tile yields(???), skip this check.

If this check was not skipped, multiply the value by (100 + 2x the AI's current monopoly % for this resource) and then divide by 100.

[!] This code is really weird and appears to be bugged.


STEP 9: Resource Ratio
Skip this check if AI is Netherlands and AI's monopoly % for this resource is less than the greatest monopoly % of all players.

Tally up the amounts the AI *and the seller* have of this resource, INCLUDING imports and SUBTRACTING exports (this can be higher than the amount listed in the trade screen).

Subtract the quantity being sold from the AI's amount.

Resource Ratio = (Seller Amount * 100) / AI Amount, capped between 80 and 200.

Multiply the value by 100 and then divide by the resource ratio.


STEP 10: Approach
If AI's TRUE approach towards the seller is...
- Friendly/Afraid: Increase value by 10%
- Neutral/Deceptive: No change
- Guarded: Decrease value by 20%
- Hostile: Decrease value by 50%
- Planning War: Decrease value by 50% (note: stacks with / 2 penalty for sneak attack)


STEP 11: Resource Deficit
If AI currently has a deficit for this resource, increase value by 50%.


STEP 12: Game Speed / Quantity
Multiply the value by the quantity and then by the deal duration.


STEP 13: Diminishing Returns
Calculate how many resources the AI is already receiving from existing deals with this seller.

Multiply the value by 100 - (10 * the number of existing resources, or 90, whichever is lower).

Divide the value by 100.

[!] Note: This appears bugged when it comes to deal renewals; it does not appear to exclude the amount in the renewed deal.


STEP 14: Divide by 10
Divide the value by 10, rounding down.


At this point, return the value.

Strategic Resource - AI is Selling
Code:
Current Game Era = Average era of all majors. 0 in Ancient, 1 in Classical, 2 in Medieval, etc.


STEP 0: Obsolete Resources
- If the resource is obsolete, return IMPOSSIBLE!


STEP 1: Base Value
- Base value is equal to current game era + 2.


STEP 2: Flavor Value
Total Flavor Value starts at 0.
# of Flavor Values starts at 0.

Loop through all AI non-diplomacy flavors. Skip any resource with a flavor value of 0 or less.
- Check what the AI's XML flavor value is for this flavor and add it to the resource value.
- Divide the sum by 5 and then add it to Total Flavor Value.
- Add 1 to # of Flavor Values.

If Total Flavor Value is greater than 0, divide Total Flavor Value by # of Flavor Values, and multiply the base value by the quotient.


STEP 3: GPT Surcharge
Divide the buyer's net GPT by 4.

GPT Bonus = floor(0.5 + square root of quotient), or the base value, whichever is lower.

Add the GPT Bonus to the value.


STEP 4: Last Few Copies Bonus
Tally up the amount the AI has of this resource, INCLUDING imports and SUBTRACTING exports (this can be higher than the amount listed in the trade screen).

- If after the trade the AI would have none of this resource left, return IMPOSSIBLE!

- If after the trade the AI would have 1 or 2 of this resource left, multiply value by 10.


STEP 5: Comparative Military Might
If buyer has greater military might than the AI (uses base military might stat used in the score calculation, not strength perception from diplomacy AI), then multiply value by 10 and then divide by 7.


STEP 6: Sneak Attack
If AI is planning to sneak attack the buyer, multiply the value by 2.


STEP 7: Proximity
If AI's proximity to the buyer is CLOSE or NEIGHBORS, multiply the value by 10 and then divide by 9.


STEP 8: Aluminum Exception
If AI is going for science victory or the other team has completed the Apollo Program, return IMPOSSIBLE! to selling Aluminum


STEP 9: How Much Do We Have?
Tally up the amount the AI has of this resource, INCLUDING imports and SUBTRACTING exports (this can be higher than the amount listed in the trade screen).

If after the trade the AI would have less than 10 of this resource left:
- Add (10 - # left of this resource) * (10 - # left of this resource) to the value.


STEP 10: Resource Ratio
Tally up the amounts the AI *and the buyer* have of this resource, INCLUDING imports and SUBTRACTING exports (this can be higher than the amount listed in the trade screen).

Subtract the quantity being sold from the AI's amount.

Resource Ratio = (Buyer Amount * 100) / Seller Amount, capped between 50 and 200.

Multiply the value by the resource ratio and then divide by 100.


STEP 11: Approach
If AI's TRUE approach towards the buyer is...
- Friendly/Afraid: Decrease value by 10%
- Neutral: No change
- Guarded: Increase value by 25%
- Deceptive: Increase value by 50%
- Hostile: Increase value by 100%
- Planning War: Increase value by 200% (note: stacks with 2x bonus for sneak attack)


STEP 12: Game Speed / Quantity
Multiply the value by the quantity and then by the deal duration.


STEP 13: Divide by 10
Divide the value by 10, rounding down.


At this point, return the value.

Luxury Resource - AI is Buying
Code:
Current Game Era = Average era of all majors. 0 in Ancient, 1 in Classical, 2 in Medieval, etc.

STEP 0: Impossible! Checks
- If the luxury is banned by the World Congress, deal value is IMPOSSIBLE!


STEP 1: Base Value
Base Value = ((2 + current game era) * deal duration) / 2


STEP 2: Flavor Value
Total Flavor Value starts at 0.
# of Flavor Values starts at 0.

Loop through all AI non-diplomacy flavors. Skip any resource with a flavor value of 0 or less.
- Check what the AI's XML flavor value is for this flavor and add it to the resource value.
- Divide the sum by 6 and then add it to Total Flavor Value.
- Add 1 to # of Flavor Values.

If Total Flavor Value is greater than 0, divide Total Flavor Value by # of Flavor Values, and multiply the base value by the quotient.

[!] Not actually sure this section of code does much...doesn't look like it's needed.


STEP 3: GPT Surcharge
Divide the AI's net GPT by 4.

GPT Bonus = floor(0.25 + square root of quotient), or the base value, whichever is lower.

Add the GPT Bonus to the value.


STEP 4: Global Unhappiness Bonus
If the AI's empire is unhappy and they have global unhappiness from ideology/war weariness, multiply the amount of global unhappiness by 2 and add it to the value.

[!] This is...weird. Why only factor in global unhappiness? And why so little value?


STEP 5: Netherlands AI
- If AI is not Netherlands, skip this check.
- If AI's monopoly % for this resource is greater than or equal to the greatest monopoly % of all players, skip this check.
- If AI has a copy of this resource (not from an import), skip this check.
- If this resource's monopoly does not increase tile yields(???), skip this check.

If this check was not skipped, multiply the value by (100 + the AI's current monopoly % for this resource) and then divide by 100.

[!] This code is also weird.


STEP 6: Religion Bonus
- Yield Bonus Factor: Starts at 1. If our founded/conquered religion (or majority religion if none is founded/conquered) gives a yield bonus for each luxury resource we control, add 1 to Yield Bonus Factor for each yield that gets a bonus.

- If Yield Bonus Factor is > 1, then multiply the value by Yield Bonus Factor and then divide by 2.


STEP 7: Comparative Resource Happiness
- Count the amount of Happiness each player is gaining from resources.

- If seller has more resource happiness than AI (or an equal amount), multiply value by 11 and divide by 10.

- If AI has more resource happiness than seller, multiply value by 10 and divide by 11.


STEP 8: Approach
If AI's TRUE approach towards the seller is...
- Friendly/Afraid: Increase value by 10%
- Neutral/Deceptive: No change
- Guarded/Hostile: Decrease value by 25%
- Planning War: Decrease value by 50%


At this point, return the value.

Luxury Resource - AI is Selling
Code:
Current Game Era = Average era of all majors. 0 in Ancient, 1 in Classical, 2 in Medieval, etc.

STEP 0: Impossible! Checks
- If AI is unhappy and it only has one copy of this resource, deal value is IMPOSSIBLE!
- If the luxury is banned by the World Congress, deal value is IMPOSSIBLE!


STEP 1: Base Value
Base Value = ((2 + current game era) * deal duration) / 2


STEP 2: Flavor Value
Total Flavor Value starts at 0.
# of Flavor Values starts at 0.

Loop through all AI non-diplomacy flavors. Skip any resource with a flavor value of 0 or less.
- Check what the AI's XML flavor value is for this flavor and add it to the resource value.
- Divide the sum by 6 and then add it to Total Flavor Value.
- Add 1 to # of Flavor Values.

If Total Flavor Value is greater than 0, divide Total Flavor Value by # of Flavor Values, and multiply the base value by the quotient.

[!] Not actually sure this section of code does much...doesn't look like it's needed.


STEP 3: GPT Surcharge
Divide the buyer's net GPT by 3.

GPT Bonus = floor(0.25 + square root of quotient), or the base value, whichever is lower.

Add the GPT Bonus to the value.


STEP 4: Last Copy Bonus
If this is the AI's last copy of the resource:
- Yield Bonus Factor: Starts at 0. If our founded/conquered religion (or majority religion if none is founded/conquered) gives a yield bonus for each luxury resource we control, add 1 to Yield Bonus Factor for each yield that gets a bonus.

- Multiply the value by (3 + Yield Bonus Factor).


STEP 5: Comparative Resource Happiness
- Count the amount of Happiness each player is gaining from resources.

- If buyer has more resource happiness than AI (or an equal amount), multiply value by 10 and divide by 11.

- If AI has more resource happiness than buyer, multiply value by 11 and divide by 10.


STEP 6: Approach
If AI's TRUE approach towards the buyer is...
- Friendly/Afraid: Decrease value by 10%
- Neutral/Deceptive: No change
- Guarded: Increase value by 50%
- Hostile: Increase value by 100%
- Planning War: Increase value by 150%


At this point, return the value.
 
STEP 5: Sneak Attack
If AI is planning to sneak attack the seller, divide the value by 2.

Could it allow the player to suspect a sneak attack?
 
STEP 5: Sneak Attack
If AI is planning to sneak attack the seller, divide the value by 2.

Could it allow the player to suspect a sneak attack?

It could.
 
I see that for strategics there's nothing in there that considers how much the AI already has. It should definitely factor that in. Or rather, isn't this the most important factor? And is it possible for it to also include sources that are already in their territory but are not yet improved? at a reduced rate

also similar for luxuries on the unimproved thing. always feels like cheating when I rush to sell a guy perfume before he improves the perfume right next to his capital

STEP 5: Sneak Attack
If AI is planning to sneak attack the seller, divide the value by 2.

Could it allow the player to suspect a sneak attack?
you can see this more clearly in luxuries. When I go to sell a neutral/friendly guy a luxury and it's worth only 80 gold to him I'm like "oh ok this guy hates me now. better be careful"
 
Last edited:
there should also be a tech check for the strategic. Iron should not be sellable to anyone that hasn't researched bronze working, and it probably should be worth very little (or nothing?) to anyone that hasn't researched iron working to actually use it.
 
So maybe we should remove it to avoid exploit by human player.
you can see this more clearly in luxuries. When I go to sell a neutral/friendly guy a luxury and it's worth only 80 gold to him I'm like "oh ok this guy hates me now. better be careful"

It is possible to use surface instead of true approach. The downside is that the AI may end up paying more than they actually want to pay in the interests of deception, which you could argue is a bad decision.

I see that for strategics there's nothing in there that considers how much the AI already has. It should definitely factor that in. Or rather, isn't this the most important factor? And is it possible for it to also include sources that are already in their territory but are not yet improved? at a reduced rate

also similar for luxuries on the unimproved thing. always feels like cheating when I rush to sell a guy perfume before he improves the perfume right next to his capital

Keep in mind that this requires looping through every tile on the map, checking to see if it's in the AI's territory, and then checking to see if it has a resource the AI can see, and then checking to see if it's the matching resource. That's a performance drain, especially if the AI has to calculate for every resource. It's not impossible to do, but it would likely have to be cached in memory (each player * each resource) and is likely why the code doesn't already exist.

See correction below

there should also be a tech check for the strategic. Iron should not be sellable to anyone that hasn't researched bronze working, and it probably should be worth very little (or nothing?) to anyone that hasn't researched iron working to actually use it.

The first is easy to do; the second is much harder to do. You would have to loop through every tech, loop through everything unlocked with each tech, and then see if any of it requires that resource. You could also do a similar procedure with units or buildings, but either way, it's a lot of iterating. Again, not impossible, but more complicated than you may think.

So maybe we should remove it to avoid exploit by human player.

To clarify, I am looking for a new valuation in the same way I wrote above. I'm not going to rewrite it myself - I can do the coding, but the valuation is up to you guys to figure out.
 
Last edited:
The first is easy to do; the second is much harder to do. You would have to loop through every tech, loop through everything unlocked with each tech, and then see if any of it requires that resource. You could also do a similar procedure with units or buildings, but either way, it's a lot of iterating. Again, not impossible, but more complicated than you may think.
I figured you'd just have a pre set list of earliest-possible techs for each strategic. Like, for iron, the only tech you need to check is iron working. Anything else that uses iron can't be gotten without getting that tech first. For horses, it's animal husbandry unless the civ's UU replaces chariot archers.
But I didn't consider making it compatible with mods that change the tech tree. That indeed complicates things.

Keep in mind that this requires looping through every tile on the map, checking to see if it's in the AI's territory, and then checking to see if it has a resource the AI can see, and then checking to see if it's the matching resource. That's a performance drain, especially if the AI has to calculate for every resource. It's not impossible to do, but it would likely have to be cached in memory (each player * each resource) and is likely why the code doesn't already exist.
really? I assumed that what's in the AI's territory was already stored somewhere as part of what's needed for general functioning. Well, even if that's not the case, I don't know how much of a performance hit we're talking, but I'd think it's still worth it if it means getting trade working properly.
 
If "Strategic Resource - AI is Buying" STEP 11 = "No Deficit" then trade for that resource should not be initiated by the AI
 
It is possible to use surface instead of true approach. The downside is that the AI may end up paying more than they actually want to pay in the interests of deception, which you could argue is a bad decision.
War cancels deals anyway, so it's not that much of an extra payment. I say use surface approach if they intend to declare war. true approach otherwise.
Does the AI have an estimate of how long it will be until they declare war? if so then it could use some kind of sliding scale based on that
 
Keep in mind that this requires looping through every tile on the map, checking to see if it's in the AI's territory, and then checking to see if it has a resource the AI can see, and then checking to see if it's the matching resource. That's a performance drain, especially if the AI has to calculate for every resource. It's not impossible to do, but it would likely have to be cached in memory (each player * each resource) and is likely why the code doesn't already exist.
Code for this exists; the top panel luxury/strategic section uses it. At any given time, you know what resources you have, those that exist in your borders, and whether or not they are improved.

Huh, nevermind. Apparently when you mouse-over the luxury/strategic section of TopPanel, it literally runs through every plot in every city you own to see if it has a resource and if it's improved. In LUA, no less.
 
Last edited:
Huh, nevermind. Apparently when you mouse-over the luxury/strategic section of TopPanel, it literally runs through every plot in every city to see if it has a resource and if it's improved. In LUA, no less.
well, running through every plot in every city is better than running through every plot in the entire map. would be better if it's in compiled code though
 
The AI shouldn't buy strategics unless it has negative resources?

I don't think this is the worst idea, though maybe not literally negative resources. I'd be thinking more along the lines of "number of resources I have" minus "number of resources I want" and they should only buy strategics if that number is negative.
 
Is this something easily accessible from EconomicAI (or whatever AI is deciding what it wants to produce)?
 
really? I assumed that what's in the AI's territory was already stored somewhere as part of what's needed for general functioning. Well, even if that's not the case, I don't know how much of a performance hit we're talking, but I'd think it's still worth it if it means getting trade working properly.

Correction - it would require at minimum looping through every plot owned by every city; I forgot we could use the "ring" method, expanding out from the city center. That is definitely less of a hit, but it would still need to be done for every resource. It can be optimized, but my point is that it requires complicated new code. You can still request it, but don't consider it guaranteed.

However, to be completely thorough, you would need to include tiles that are not in the range of cities and were added via citadel or similar effect.

Is this something easily accessible from EconomicAI (or whatever AI is deciding what it wants to produce)?

AI doesn't make longterm decisions like this IIRC.
 
Last edited:
Does the AI have an estimate of how long it will be until they declare war? if so then it could use some kind of sliding scale based on that

They do not, but a function for this could be created.

Is there an existing code that checks lux needed to trigger WLTKD?

Yes.
 
AI doesn't make longterm decisions like this IIRC.
It still decides that turn whether it wants to build something that requires resources, and is turned away when it finds out it doesn't have enough. If that need was stored in a member variable that could be accessed at the end of the turn for when it starts making a deal, it could at least have those resources for the next turn's AI to use. Some care would need to be taken to make sure it doesn't add to the need pile every time it has to skip over due to lack of resources.

I might be wrong, but the AI asks itself that instant whether it needs another eg. swordsman, so it looks at its current quantity of swordsmen and adds the number of swordsmen it's currently building and checks if that's enough. If it doesn't have enough swordsmen and it doesn't have enough resources, then it can add +1 swordsmen to its desired array. This array would then be added to the swordsman total for when the next city decides whether it should be making another swordsman. The array would also be used for the deal AI when it checks to see how many resources it needs.

I assumed here that the deal AI happens at the end of the turn, which I think is wrong, but it shouldn't matter. The "desired list" would get cleared after the Deal AI is finished making what deals it can.
 
Top Bottom