Show Research Times in Game

A forum to discuss custom scenarios, campaigns and modding in general.

Moderators: Slitherine Core, The Lordz

Post Reply
Historion
Corporal - 5 cm Pak 38
Corporal - 5 cm Pak 38
Posts: 31
Joined: Wed Jul 30, 2014 10:41 pm

Show Research Times in Game

Post by Historion »

If You bought the game via Steam, the LUA-script files are located in folder "Steam\SteamApps\common\Commander The Great War\data\scripts".

Technologies are coded in "technology.lua".
Research-Mechanic is coded in "game/game_research.lua".

The syntax for coding technologies is self-explaining thanks to developer comments in the code.
Example : One of the first Techs to research in the 1914-scenario is "industrial_warfare" :

Code: Select all

  industrial_warfare =
  {
		stats =  --bonuses to stats
		{
			basedefense = 4,
		},
		unitTypes = {"infantry", "garrison", "cavalry"},
		cost = 2,       --Upgrade cost
		time = 100,  --time points to complete tech
		level = true,  -- increase unit level yes/no
  },
"industrial_warfare" costs 100 time points to complete.

The Research Speed is defined at the beginning of "game_research.lua" :

Code: Select all

gameplay.researchSpeed = 10
This means that (without Focus) it will take around 10 turns (10 x 10 = 100) to research "industrial_warfare".

The game usually only shows ResearchTimes when there are 5 turns or less left.
If you want to see the Research Times for all available Techs in the Research Window, You have to adjust the line with

Code: Select all

 if time <= 5 then 
in the following function coded in "game_research.lua":

Code: Select all

-- Returns time to complete
-- If time takes longer than 5 turns it will return -1
function GetTechTime(faction, techName)

  local increment = GetTechIncrement(faction, techName)

  if increment > 0 then
    local time = math.ceil((data.newTechnologies[techName].time - faction.luaData.researchProgress[techName]) / increment)
    if time <= 200 then  -- original code : 5
      return time
    else
      return -1
    end
  else
    return -1
  end

end
If You adjust and save the file "game_research.lua" and start a new game (1914, Central Powers, Austria), then You can see now the times for the first 3 Technologies in the Research Window :
Industrial Warfare (5/100) : 11 turns
Anti-Air Defense (0/250) : 28 turns
Barbed Wire (0/100) : 12 turns

The difference between "Industrial Warfare" and "Barbed Wire" can be explained. It is caused by the scenario definition in file "1914.lua" :

Code: Select all

  -- Austria
  ProgressTechnology(3, "industrial_warfare", 5)
Austria (= faction 3) already starts with 5 points in tech "industrial_warfare".

If You focus Research on Industrial Warfare, You increase the effort by 20% but reduce the research effort for the other 2 techs respectively :
Industrial Warfare (5/100) : 9 turns
Anti-Air Defense (0/250) : 31 turns
Barbed Wire (0/100) : 13 turns

In theory for this example the difference in Research-Points-Distribution between noFocus and Focus should be (10,10,10) to (12,9,9) (for a single lab).
In praxis it is rather (9,9,9) and (11,8,8). (I will explain in a 2nd post why the numbers in the game slightly differ from the theoretical calculated ones.)

Based on the observed times we can calculate that the effective research-speed (noFocus) for 1 lab is only 9 points instead of 10.
Industrial Warfare (5/100) : 11 turns : 5 + 11 x 9 = 104 > 100 -> Tech researched
Anti-Air Defense (0/250) : 28 turns : 28 x 9 = 252 > 250 -> Tech researched
Barbed Wire (0/100) : 12 turns : 12 x 9 = 108 > 100 -> Tech researched

... and with Focus :
Industrial Warfare (5/100) : 9 turns : 5 + 9 x 11 = 104 > 100 -> Tech researched
Anti-Air Defense (0/250) : 31 turns : 31 x 8 = 248
Barbed Wire (0/100) : 13 turns : 13 x 8 = 104 > 100 -> Tech researched


If You increase the number of labs in a category You can speed up Research.
Here are some numbers for NoFocus Research for the above techs from ingame:

# Labs : (Industrial Warfare, Anti-Air Defense, Barbed Wire)
1 : (11,28,12) (see above)
2 : (10,25,10)
3 : ( 9,23,10)
4 : ( 9,22, 9)
5 : ( 8,21, 9)
6 : ( 8,21, 9)
7 ... : ( 8,20, 8 )
Increasing the number of labs beyond 7 had no additional effect in this example. (Even 7 labs are very very expensive.)

The maximum effect is reached at around 7 Labs in a category and resuls in a reduction of Research Time by 1/3, e.g. from 12 turns down to 8 turns.
This equals to 12.5 - 13 points :
Barbed Wire (0/100) : 8 turns : 8 x 13 = 104 > 100
Anti-Air Defense (0/250) : 20 turns : 20 x 13 = 260 > 250

(Note : All mentioned Research Times are taken from the ingame Research Window on turn 1.)
Historion
Corporal - 5 cm Pak 38
Corporal - 5 cm Pak 38
Posts: 31
Joined: Wed Jul 30, 2014 10:41 pm

Re: Show Research Times in Game

Post by Historion »

In the file "game_research.lua" the function "GetTechIncrement" is defined which calculates the Tech Increment (= Research Points) for the current turn.
"GetTechIncrement" is called by function ManageResearch and by function GetTechTime (see post above).
According to the comments ("FIXME") the function might not be in its final state (work in progress?).

Code: Select all

gameplay.researchSpeed = 10

Code: Select all

-- Lab boost function graph:
-- (x/4)/sqrt(1+(x/4)^2)*8
-- 

Code: Select all

function GetTechIncrement(faction, techName)

  local techClass = data.newTechnologies[techName].class

  -- Research speed bonus starts from 2 labs, 0 at 1 lab
  local labs = faction.luaData.labs[techClass] - 1

  -- Lab research speed bonus calculation
  --FIXME: should not be "labs - 1" AGAIN here, but change requires resetting of all tech times
  local labIncrement = ((labs-1)/3)/math.sqrt(1+math.pow((labs-1)/3,2))*3

  -- Total progress of tech this turn
  local totalIncrement = gameplay.researchSpeed + labIncrement

  -- If there currently is a focused tech in this tech class
  if faction.luaData.currentTech[techClass] ~= -1 then

    -- If the current tech is the focused techn speed bonus
    if faction.luaData.currentTech[techClass] == techName then

      -- 20% increase
      local boost = totalIncrement * 20 / 100

      totalIncrement = totalIncrement + boost

    -- If another tech is focused, speed is slowed down
    else

      local loss
      -- This should never happen cos if there is only 1 tech left than there cant be another thats being focused
      if GetCategoryTechlineCount(faction, techClass) == 1 then
        loss = 0
      else
        loss = (totalIncrement * 20 / 100) / (GetCategoryTechlineCount(faction, techClass) - 1)
      end

      totalIncrement = totalIncrement - loss

    end

  end

  return totalIncrement
end
We have seen in the opening post that Research Speed (NoFocus) is in range between around 9 points (1 lab) and 13 points (7 labs) per turn per tech while gameplay.researchSpeed = 10.
This might be a bug or a feature ...

In the line

Code: Select all

 -- Research speed bonus starts from 2 labs, 0 at 1 lab
  local labs = faction.luaData.labs[techClass] - 1
the number of labs for a certain tech category / class is read and is stored in a local (temporary) variable called "labs" but reduced by 1. This means for a class with 1 lab that labs = 0.

In the next lines

Code: Select all

  -- Lab research speed bonus calculation
  --FIXME: should not be "labs - 1" AGAIN here, but change requires resetting of all tech times
  local labIncrement = ((labs-1)/3)/math.sqrt(1+math.pow((labs-1)/3,2))*3
the term (labs - 1) is used, which has the value "0 - 1 = -1" in our standard case with 1 lab.
labIncrement = -1/3 / sqrt(1 + (-1/3)^2 ) * 3 = -1 / sqrt(10/9) = ca. - 0,95

Code: Select all

-- Total progress of tech this turn
  local totalIncrement = gameplay.researchSpeed + labIncrement
So for 1 lab the effective research speed is reduced from 10 (gameplay.researchSpeed) to "10 - 0,95 = 9,05 points" ... which is in-line with our observations above. (Bug or Feature?)

If research speed for 1 lab shall be the gameplay.researchSpeed (10) than the variable "labs" should contain the full number of labs and should not be reduced by 1.

Code: Select all

  -- Research speed bonus starts from 2 labs, 0 at 1 lab
  local labs = faction.luaData.labs[techClass]
Historion
Corporal - 5 cm Pak 38
Corporal - 5 cm Pak 38
Posts: 31
Joined: Wed Jul 30, 2014 10:41 pm

Re: Show Research Times in Game

Post by Historion »

Before adjusting the effective research speed in the game we should take a look at the research system :

In scenario-file "1914.lua" the maximum number of turns is restricted to 118 :

Code: Select all

scenario.turnLimit = 118
If you start research on turn 1 with 1 lab, you can accumulate around 118 x 9 = 1062 research points (no Focus) or 118 x 11 = 1298 research points (with Focus).
With 2 labs it is around 118 x 10 = 1180 research points (no Focus) or 118 x 12 = 1416 research points (with Focus) but at double costs (upkeep 8 pp instead of 4 pp).
With 7 labs it is around 118 x 12,5 = 1475 research points (no Focus) or 118 x 15 = 1770 research points (with Focus) but at enormous costs (upkeep 28 pp instead of 4 pp).

(Note :
A lab from start costs around 118 x 4 = 472 pp upkeep. If you pay upkeep for your units, a lab costs the same upkeep as an additional infantry unit. If your unit upkeep is below the 70 pp free upkeep (actual value), having the pp and not building a lab allows you to add an additional infantry unit every 5 turns until the current free upkeep limit of 70 is reached. This is very useful for all factions who start with a small army as serbia or even Austria.)

To complete certain Tech-Lines it costs 1000 - 1500 research points, others are cheaper but may not be available from start. So researching some of the high level technologies with an economically justifiable effort is limited to technology leaders. Technology leaders are set in the scenario file by giving them free start techs, e.g. in "1914.lua" :

Code: Select all

 -- France
  ProgressTechnology(0, "industrial_warfare", 5)
  UnlockTechnology(0, "all_centerline_armament")
  ProgressTechnology(0, "torpedo_bulge", 4)
  ProgressTechnology(0, "caterpillar_combat_vehicle", 5)
  UnlockTechnology(0, "bomb_rack")

  -- Britain
  ProgressTechnology(1, "industrial_warfare", 10)
  UnlockTechnology(1, "all_centerline_armament")
  ProgressTechnology(1, "torpedo_bulge", 20)
  UnlockTechnology(1, "diesel_electric_propulsion")
  ProgressTechnology(1, "caterpillar_combat_vehicle", 10)
  UnlockTechnology(1, "armoured_car_chassis")

  -- Germany
  UnlockTechnology(2, "industrial_warfare")
  ProgressTechnology(2, "grenade", 15)
  UnlockTechnology(2, "hydro_pneumatic_howitzer")
  ProgressTechnology(2, "chlorine_gas", 10)
  UnlockTechnology(2, "all_centerline_armament")
  ProgressTechnology(2, "torpedo_bulge", 20)
  UnlockTechnology(2, "diesel_electric_propulsion")
  UnlockTechnology(2, "rigid_airship")
  ProgressTechnology(2, "armed_fighter", 30)
France gets the bomber, Britain the armoured car and Germany the airship plus a bonus on infantry and artillery stats.
All 3 start with bonus tech for their naval units.

Increasing the overall research speed or the benefit from additional labs too much may easily result in a game where all major factions unlock all techs in midgame and from then on have the same units with the same stats at hand which would remove some flavor from gameplay.
Historion
Corporal - 5 cm Pak 38
Corporal - 5 cm Pak 38
Posts: 31
Joined: Wed Jul 30, 2014 10:41 pm

Re: Show Research Times in Game

Post by Historion »

In the following post I want to improve the function "GetTechIncrement" with the goal to make research speed more easily configurable in the file "game_research.lua".
There are 2 constants defined at the start of In the file:

Code: Select all

gameplay.researchSpeed = 10
gameplay.researchBoost = 4
There is also a mathmatical function for research boost with with diminishing returns proposed in a comment :

Code: Select all

-- Lab boost function graph:
-- (x/4)/sqrt(1+(x/4)^2)*8
(If you want to see the graph you can enter the function on site http://fooplot.com.)

I will combine these things in first part of function "GetTechIncrement".
If there are no labs, research is 0 points.
For 1 lab the research is given by value of gameplay.researchSpeed (10).
For more than 1 lab the research is limited to a maximum value of gameplay.researchSpeed (10) + gameplay.researchBoost (4) with diminishing returns.

Code: Select all

function GetTechIncrement(faction, techName)

  local techClass = data.newTechnologies[techName].class
  local labs = faction.luaData.labs[techClass]  

  -- No Research without lab
  if labs <= 0 then
     return 0
  end

  -- Research speed bonus starts with 2nd lab, therefore (labs-1)
  local labIncrement = ((labs-1)/4)/math.sqrt(1+math.pow((labs-1)/4,2)) * gameplay.researchBoost
  
  -- Total progress of tech this turn
  local totalIncrement = gameplay.researchSpeed + labIncrement 

  --- the rest of the function is unchanged ----

  -- If there currently is a focused tech in this tech class
  if faction.luaData.currentTech[techClass] ~= -1 then

    -- If the current tech is the focused techn speed bonus
    if faction.luaData.currentTech[techClass] == techName then

      -- 20% increase
      local boost = totalIncrement * 20 / 100

      totalIncrement = totalIncrement + boost

    -- If another tech is focused, speed is slowed down
    else

      local loss
      -- This should never happen cos if there is only 1 tech left than there cant be another thats being focused
      if GetCategoryTechlineCount(faction, techClass) == 1 then
        loss = 0
      else
        loss = (totalIncrement * 20 / 100) / (GetCategoryTechlineCount(faction, techClass) - 1)
      end

      totalIncrement = totalIncrement - loss

    end

  end

  return totalIncrement
end
Now you can easily adjust research speed by changing the 2 parameters without having to check the complex math formula every time for relevant value to adjust.

Code: Select all

gameplay.researchSpeed = 10
gameplay.researchBoost = 4
For example, if you want to force players in a scenario to invest more PP into research, you can lower researchSpeed to maybe 8 and increase researchBoost to 12.
Post Reply

Return to “Commander the Great War : Mods & Scenario Design”