Saturday, July 27, 2013

Technology Management

Tech

Cultura begins in the stone age and is meant to eventually go to the Early Modern age. However, for the first iteration it'll just be the paleolithic age. And for right now, let's take a look at the test UI for the technology management.

In a somewhat Diablo style interface, a list of technologies is shown and rather than be like Civilization where you have research points accumulating to a specific technology you instead receive tech points. Technologies which you can research have a "+" button next to them. I may have technologies which cost more than one point or alternatively give you less benefit per point making some technologies strictly superior to others.

I'd like to add arrows to show prerequisites more clearly and also technologies have "level" requirements not just specific tech requirements. For instance, pottery may require Level 5+ in scraper tool. For now, this screen works. When you add a tech point to tents you are then able to build tents. Once you exit from the screen and go back to the game and then go to the context menu you will see that tent indeed shows up.

Yay

Saturday, July 13, 2013

Building Construction

In order to finish off building construction to the alpha stage several pieces were needed:

  • Context Menu
  • Placement UI
  • Material Selection
  • Wealth tracking
  • Technologies

There were a few bug fixes involved: getting the wealth tracking working right, fixing the graphic for updating trees and so on. The wealth tracking revealed that my quadtree accidentally double inserted items. When I added silverleaf trees, I discovered I had a typo in my code that accidentally changed all trees to the graphic of the one that was just cut down. Imagine my surprise when all my silverleaf trees became pine trees!

Context Menu

The context menu interface has changed to be a keyboard based interface. I left the right-click to show functionality but the primary way to show it is to press the "B" key on your keyboard. The reason? I kept having units selected and then I would right-click only to order the units to do something I did not want instead of showing the right-click menu. Durr!

Basically the context menu shows the base options (which will eventually become icons) and then the sub-categories and then finally the final options. It will also let you quickly open up the faction management screen. I think there will also be a single keyboard shortcut to just go straight into the management screen but that will be for when I create those screens. For now just the context menu!

Opening up the context menu shows this:

We select build

We select Cultural buildings:

Finally, we select Inukshuk

Now we see we can place the building anywhere and also select the materials with which we can construct it.

After we place buildings we can see that there is a bunch of rocks that mark the construction site:

So we can see a preview when we move the mouse around where the building will be and in the lower right corner we see what materials we will be using to construct it. Eventually I will add a resource overlay in the top right corner so that you can see your wealth - liability. You can spend into debt and the display will show negative (indicating that you should get more resources).

Wealth Tracking

Wealth tracking is accurate to the game tick. In actions that affect faction wealth it updates it on the fly. This is a bit dangerous (if I miss a spot in the code then the wealth is incorrect). To make up for this, I have a debug assert that does a resource wealth audit every 100 game ticks. If there is a mismatch between calculation at that moment and the current cached value of wealth then an exception is thrown. This will help me debug any place I have missed proper wealth tracking.

Wealth is defined by having resources stored in structures (therefore units carrying items do not constitute as part of wealth). Liability is defined by structures placed and how much more resources they need. Once industry is added, liabilities will also include work orders and item requests.

Technologies

The technology UI will be next but for now, factions record their technology in a std::unordered_map where it is technology ID to tech level. Many techs are repeatable and so int to int is a reasonable structure. Technology limits how many materials you can select and what buildings you can construct. Right now the two test structures a inukshuks (which the faction begins with) and tents (which the faction does not have). So when you open up the menu, only buildings for which you have the technology show up. Additionally, attempting to select materials beyond the limit results in nothing happening (there will be an error message added in the future). I will also probably have to add information in the material selection on technology limits such as "You may select up 1 material per item type".

Thursday, July 11, 2013

Diplomacy

Let's take a look at Cultura's AI engine and focus on the diplomacy section.  When you trade or make deals how do they figure out how to react, how to figure out whether it's a good deal?

We start with a way rate to all items equally.  Any item is thought of in terms of how much it increases a faction's score.  What is score?  It is the health/happiness/administration provided by the deal item.

Value = health + happiness + administration

It might be possible for a faction to value these scores differently.  If health is low, health is more valuable.  Administration points are more difficult to obtain so they are more valuable.

Value = health * bias1 + happiness * bias2 + administration * bias3

Okay, but how do we calculate the score?  We can define a set of "primitive" functions that we can use:

A) Direct Function

Trade item directly represents goods that have a score rating that can be estimated.  For instance, each unit of Pine Cupboard provides +1 health, +1 happiness.  You are offered 200.  We have no bias.  Therefore it is worth 200 * 1 + 200 * 1 + 0 * 1 = 400.

(Considering existing stocks)
We already have 100 Pine Cupboards and we have 200 people.  Each person gets full benefit from 1 Pine Cupboard.  Additional pine cupboards only provide 1/10 the score.  Now the value is 100 * 1 + 100 * 1 + 0 * 1 = 200 and for the surplus amount is worth (100 * 1 + 100 * 1 + 0 * 1) / 10 = 20.  The value is 220.

B) Trade Opportunity Cost Function

The value of a particular good is the best trade you can get for it.  If there are no previous trades this value is unknown.  Thus the value is the HIGHEST score of all the goods you have traded it for.

For instance, say you have 50 oranges.  They can be traded for 1 pear (+1 health) or 1 mango (+2 health).  In the first trade it is worth 50 health.  The second trade is worth 100 health.  Therefore the trade opportunity cost is 100 value.  We are simply applying A (the direct function) to each set of goods it can be traded for.

Alright, so there's two primitives.

That's pretty good if we're exchange goods.  We calculate the value of what we are trading away.  Let's take a look at an example:

Goguryeo Offers:
200 mangoes

Asks:
50 silk

Direction Calculation
Okay, 1 mango = 2 health.  We have no existing mangoes.  Therefore it is worth 200.
Silk = 2 happiness.  We have lots of silk, overwhelming our people.  So it is worth 100 / 10 = 10.  It is worth 10.

Trade Calculation
We are being offered the mangoes.  We ignore this calculation.
The best other offer is durians which are 3 health each.  Therefore, 50 durians are 150 health.  Trade value is 150.

Using these scores we determine this deal is:

Goguryeo offers 200 and asks 150.  Deal is +50 from our perspective.  Therefore we should accept at a high percentage chance.

Alright but what about more complex deals?  War, threat and raiding.  The situation here is more difficult.  But we could try to break it down to this:

C) Threat Cost

The value of a change in relationship is the threat cost difference.  This can be both negative and positive for when relationship deteriorates or improves.  We figure out the power of an enemy military.  For now we might make a simple calculation:

Military = sum of unit power values * bias

The bias is affected by real data points.  If we fought a war previously and strength was underestimated we can push the bias value to a higher one.

We figure the amount of military needed to counter another faction to be:

MilitaryPowerNeeded = MilitaryPower * Threat

The value for threat can simply be the value of relationship score between the two factions normalized over a value range (between 0 and 1).  So for any change in relationship then the threat value will change and change the MilitaryPowerNeeded.  The difference between the new and old value is the MilitaryPowerNeededChange.  We then estimate how many goods are needed per point of MilitaryPower.  How?

ValuePerMilitaryPower = MilitaryPower / Value of Military Goods

We take the total military goods use calculations A (direct) and B (trade) to get the value of the goods, divide our military power by that and figure out the value per military point.  Let's look at an example:

Song Dynasty demands:
Submission

Okay, if we refuse then we expect -10 relationship which in threat terms means +0.1 threat.

Song Dynasty has 5000 militaryPower.  Current threat is 0.2 and we might expect +0.1 threat if we refuse or -0.1 threat if we accept.  So if we refuse, we need 500 militaryPower, if we accept then we need 500 military power less.

Currently military power of your faction is 2000, with goods valued at 4000.  Therefore each military power is worth 2 value.

So the trade deal is:

Accept: +1000
Refuse: -1000

Are we done?  Military calculations are more complicated than this, so no.

B2) Opportunity Cost of Labour

The reality is that military goods are a drag on an economy.  So we need a new calculation.  Things we could have built if we didn't make the military goods.  Let's call that calculation B2, the opportunity cost of a good in terms of production rather than trade.  If we freed up the labour used to make the military goods what could we have made and use the calculation of A and B to determine the value of those goods.

Total Good Production / Total Labour Used = labour cost of good

This cost is chained.  For instance, a Flour Mill = Gear + Spoke.  Gear = 10 stone.  Spoke = 5 wood.  Therefore Flour Mill = labour cost of each flour mill + 1 * labour cost of gear + 1 * labour cost of spoke + 10 * labour cost of stone + 5 * labour cost of wood.

Knowing that 1 unit of military power = goods whose labour cost is say 50, then we see the best highest value good we could have made instead.

We'll say that each military power could have been 3 honey bread which is worth 6 value.


D) Raid Value

We might want to attack someone for their goods.  In this case, we estimate loss of military power for goods gained.  We might simply come up with a random starting value and then shift it up/down according to actual raids conducted.  Militant powers would estimate higher at first, pacifist powers would estimate lower.  Real raids give real numbers.

As an example: We raided Song Dynasty and gained 300 rice.  Each rice is +1 health.  We lost 20 military power (calculation is the same as discussed in C).  Let's say each military power is 2 value.  We gained 300 value and lost 40 value.  The gain is 260 value.  What is the raid value?  The value is 260/20 which means each military power is worth 13 points.

Like trading, we take the opportunity cost calculation.  The best raid value is the one used.  We pretend for now that Song Dynasty is the best place to raid.  What's second best?  We'll say it is a nation called Silla.  With Silla, each lost military power results in 8 value.

Notice that a tougher target means higher losses and will lower Raid Value, so it inherently takes into account both the prosperity of a target and its defences.

That's great we have a way of figuring out military power expended and the value we get out of it.  But the problem is, how much military power are we going to expend?

Now we can combine the two calculations of C and D.

Song Dynasty demands submission.  We can submit and free up 500 military power.  Therefore submitting is valued at 3000 by calculation C (military goods no longer needed and we value it by A, B and B2).  Therefore the final value is 3000.

Or we can reject.  By calculation C that is valued at -3000.  But it means our raids continue.  In order to break even we must expend at least 3000 / 13 = 230 military power.  So the question now is whether we are willing to risk that much military power.  A faction can set a military power risk threshold.  Let's say Gogoryeo is willing to risk 300 military power.  A -3000 value is not enough to justify the stop to border attacks against Song Dynasty.  If instead Gogoryeo is only willing to risk 100 military power then it is no longer justified to reject submission.

So finally we have a faction's thresholds.  This isn't a primitive calculation.

E) Military Deployment Perceptions

Factions have two values against one another.  The first is the threshold of military they wish to deploy against someone and feel it is still reasonable as a percentage of their military power.  The second is how much military they believe an enemy nation is deploying against them.

Say you are aggravating Song Dynasty.  You injure your relations with them but you know they are already fighting a conflict with Jin Empire.  So around 70% of the Song military is deployed against Jin and of the 30% left they are deployed in various ways.  You notice that only 5% of their military is deployed against you (Gogoryeo).

Song Dynasty has 5000 military power.  Only 250 military power is deployed against you.  According to previous conflicts you trade losses at a ratio of 2:1.  For every military power you lose, they lose 2.  That means you need 125 military power to take care of this threat.

If the actual threat value is higher than this, this means they are temporarily weakly deployed against you for whatever reason.  So if threat value says you need 250 military power but military deployment perception says 125 military power, take a value in-between.  We can keep it simple and just say the half-way point.

You combine D and E and figure out that the requirement of troops is 187.5 and furthermore, increases in threat value are not as significant because if military deployment has not changed then you perceive the military threat to be less than it could be.

What else is important about E?  Shifts in military deployment from your actions.  If you sign a deal to aid someone else in their war against an enemy, you see benefit from decreased enemy deployment along your border.  You might also incite war between two factions, using this calculation to see the benefit versus the cost of goods needed to convince the others to go to war.

Alright well that was quite a bit.

With these we can compile more complicated formulas by putting them together.  For instance:

1) Damage Infliction

Song Dynasty dislikes the Gogoryeo and denounces them.  They believe this will cause the Silla to take a more aggressive stance against the Gogoryeo because then the Gorgoryeo kingdom must redeploy military against Song.

For Song Dynasty it sees by D (threat value) a value of -200 due to increased military required against Gogoryeo.  However, it looks at E (military deployment) between Gogoryeo and its neighbours.  It figures that this begins to draw military away from its other borders and invite attack because their deployment levels drop below some of the military thresholds of its neighbours, such as Silla.

So, it calculates that military losses from such conflict will drop the total size of the Gogoryeo kingdom and as such then lower their troops along the Song-Gogoryeo border.  This freed up military in the long-term is calculated by D (threat value) and is then seen to be +300.

Threatening Gogoryeo is then worth +100.

2) Accepting Roman Protectorate Status

The Romans promise military protection but then we (the Gauls) must pay tax.  By calculations A and B of the non-military goods sent we figure out that the value is -500.  In return the Romans promise protection.

Between the Gauls and the Romans, we see a threat level drop.  Using calculation D we figure out that the freed up military is than valued at +300.

The Romans also post a garrison in our lands.  Depending on our trust of the Romans we may view this as completely not a threat (100% trust) or an insidious invasion plan (0% trust).  It's likely somewhere in-between.  We calculate via D and/or E that this lowers our military deployment and so is worth +700 but due to our trust level (70%), it is lowered to +490.

The total deal is worth +290.