<p>So far we have been concentrating on separate pieces of the game. Now it’s time to put some of them together as a simulation.</p>
<h2 id="overview-of-simulation">Overview of simulation</h2>
<p>Simulation is done in discrete steps. Each step is roughly 1 earth month (completely arbitrary decision). Shorter than that and there might not be enough happening during turns to keep things interesting. Much longer than that and player might not have enough control on how to react things.</p>
<p>In any case, current time is stored in database in table <code>time</code>. There should be only one row in that table at any given time. And that row has only one value, current time. Time is stored as integer as I didn’t want to deal with problems that you get when adding fractions to a float time after time. So current time (March 2019) would be <code>2019.3</code> in game terms and stored as <code>20193</code> in database.</p>
<p>Main processing is done in function called <code>processTurn</code> that is shown below. It advances time for one decimal month, removes all expired statuses as explained in <a href="https://hackerpublicradio.org/eps.php?id=2768">episode 2768</a> and then loads all factions.</p>
<p>After that, various steps of the simulation are carried out for all loaded factions. These include handling special events as explained in <a href="https://hackerpublicradio.org/eps.php?id=2748">episode 2748</a> and doing observations and report writing in manner described <a href="https://hackerpublicradio.org/eps.php?id=2703">episode 2703</a>.</p>
<pre><code>processTurn :: (BaseBackend backend ~ SqlBackend,
    BackendCompatible SqlBackend backend, PersistUniqueRead backend,
    PersistQueryWrite backend,
    PersistQueryRead backend, PersistStoreWrite backend, MonadIO m) =&gt;
    ReaderT backend m Time
processTurn = do
    newTime &lt;- advanceTime
    _ &lt;- removeExpiredStatuses newTime
    factions &lt;- selectList [] [ Asc FactionId ]
    _ &lt;- mapM (handleFactionEvents newTime) factions
    mapM_ handleFactionFood factions
    mapM_ (handleFactionConstruction newTime) factions
    _ &lt;- mapM (addSpecialEvents newTime) factions
    -- Doing observations should always be done last to ensure players have
    -- recent reports of property they have full control, ie. planets.
    -- Otherwise it&#39;s possible that they&#39;ll receive reports that are one
    -- turn out of sync.
    mapM_ (handleFactionObservations newTime) factions
    return newTime</code></pre>
<h2 id="more-mapping">More mapping</h2>
<p>Remember <code>map</code> and <code>fmap</code> that are used to run a function to each element in a list or general structure? <code>mapM</code> works in a similar way, but is used in monadic context. In <code>processTurn</code> function, we’re dealing with input and output and have IO monad present to allow us to do that (<code>MonadIO m</code> part of the type signature).</p>
<p>If you step back a bit and squint a bit, then <code>map :: (a -&gt; b) -&gt; [a] -&gt; [b]</code> and <code>fmap :: (a -&gt; b) -&gt; f a -&gt; f b</code> and <code>mapM :: Monad m =&gt; (a -&gt; m b) -&gt; t a -&gt; m (t b)</code> look pretty similar. Each take a function, structure and produce a new structure which values were created by running the given function for each element of the original structure.</p>
<p>The difference is that <code>map</code> works only for lists, <code>fmap</code> works for functors (that were covered in <a href="https://hackerpublicradio.org/eps.php?id=2778">episode 2778</a>) and <code>mapM</code> works for structures in monadic context.</p>
<p>Best way to contact me nowadays is either by email or through fediverse where I’m Tuula@mastodon.social.</p>
