<h2 id="background">Background</h2>
<p>This is rather large topic, so I split it in two episodes. Next one should follow in two weeks if everything goes as planned. First part is about modeling research, while second part concentrates on how things change over time.</p>
<p>There’s three types of research: engineering, natural sciences and social sciences. Research costs points that are produced by various buildings.</p>
<h2 id="implementation">Implementation</h2>
<p>There’s three database tables, which are defined below:</p>
<pre><code>CurrentResearch
    type Technology
    progress Int
    factionId FactionId

AvailableResearch
    type Technology
    category TopResearchCategory
    factionId FactionId

CompletedResearch
    type Technology
    level Int
    factionId FactionId
    date Int</code></pre>
<h2 id="data-types">Data types</h2>
<p><code>Technology</code> is enumeration of all possible technologies. Knowing these enable player to build specific buildings and space ships, enact various laws and so on. In the end this will be (hopefully) large list of technologies.</p>
<pre><code>data Technology =
    HighSensitivitySensors
    | SideChannelSensors
    | HighTensileMaterials
    | SatelliteTechnology
    | BawleyHulls
    | SchoonerHulls
    | CaravelHulls
    ...
    deriving (Show, Read, Eq, Enum, Bounded, Ord)</code></pre>
<p>All research belong to one of the top categories that are shown below:</p>
<pre><code>data TopResearchCategory =
    Eng
    | NatSci
    | SocSci
    deriving (Show, Read, Eq, Ord)</code></pre>
<p><code>ResearchCategory</code> is more fine grained division of research. Each of the categories is further divided into sub-categories. Only <code>EngineeringSubField</code> is shown below, but other two are similarly divided.</p>
<pre><code>data ResearchCategory =
    Engineering EngineeringSubField
    | NaturalScience NaturalScienceSubField
    | SocialScience SocialScienceSubField
    deriving (Show, Read, Eq)

data EngineeringSubField =
    Industry
    | Materials
    | Propulsion
    | FieldManipulation
    deriving (Show, Read, Eq)</code></pre>
<p><code>ResearchScore</code> is measure of how big some research is. It has type parameter <code>a</code> that is used to further quantify what kind of <code>ResearchScore</code> we’re talking about.</p>
<pre><code>newtype ResearchScore a = ResearchScore { unResearchScore :: Int }
    deriving (Show, Read, Eq, Ord, Num)</code></pre>
<p><code>TotalResearchScore</code> is record of three different types of researches. I’m not sure if I should keep it as a record of three fields or if I should change it so that only one of those values can be present at any given time.</p>
<pre><code>data TotalResearchScore a = TotalResearchScore
    { totalResearchScoreEngineering :: ResearchScore EngineeringCost
    , totalResearchScoreNatural :: ResearchScore NaturalScienceCost
    , totalResearchScoreSocial :: ResearchScore SocialScienceCost
    }
    deriving (Show, Read, Eq)</code></pre>
<p>Following singleton values are used with <code>ResearchScore</code> and <code>TotalResearchScore</code> to quantify what kind of value we’re talking about.</p>
<pre><code>data EngineeringCost = EngineeringCost
    deriving (Show, Read, Eq)

data NaturalScienceCost = NaturalScienceCost
    deriving (Show, Read, Eq)

data SocialScienceCost = SocialScienceCost
    deriving (Show, Read, Eq)

data ResearchCost = ResearchCost
    deriving (Show, Read, Eq)

data ResearchProduction = ResearchProduction
    deriving (Show, Read, Eq)

data ResearchLeft = ResearchLeft
    deriving (Show, Read, Eq)</code></pre>
<p>Finally there’s <code>Research</code>, which is a record that uses many of the types introduced earlier. It describes what <code>Technology</code> is unlocked upon completion, what’s the cost is and if there are any technologies that have to have been researched before this research can start. The tier of research isn’t currently used for anything, but I have vague plans what to do about it in the future.</p>
<pre><code>data Research = Research
    { researchName :: Text
    , researchType :: Technology
    , researchCategory :: ResearchCategory
    , researchAntecedents :: [Technology]
    , researchCost :: TotalResearchScore ResearchCost
    , researchTier :: ResearchTier
    }
    deriving (Show, Read, Eq)</code></pre>
<h2 id="tech-tree">Tech tree</h2>
<p>Putting all this together, we can define a list of <code>Research</code>. Since finding an entry from this list based on research type of it is such a common operation, we also define another data structure for this specific purpose. <code>Map</code> in other programming languages is often known as dictionary, associative array or hash map. It stores key-value - pairs. In our case <code>Technology</code> is used as key and <code>Research</code> as value. We define it based on the list previously defined:</p>
<pre><code>techMap :: Map.Map Technology Research
techMap = Map.fromList $ (\x -&gt; (researchType x, x)) &lt;$&gt; unTechTree techTree</code></pre>
<p>Next time we’ll look into how to actually use all these types and data that were defined.</p>
