-- |
-- SPDX-License-Identifier: BSD-3-Clause
--
-- Utility for determining the center of
-- the map, outside the context of a
-- running game
module Swarm.Game.Scenario.Topography.Center where

import Control.Lens (view)
import Data.List.NonEmpty (NonEmpty)
import Data.Maybe (fromMaybe, listToMaybe)
import Swarm.Game.Location (Location, origin)
import Swarm.Game.Robot (trobotLocation)
import Swarm.Game.Scenario (ScenarioLandscape)
import Swarm.Game.State.Landscape (SubworldDescription, genRobotTemplates)
import Swarm.Game.Universe (Cosmic (..), SubworldName (DefaultRootSubworld))

-- | Determine view center for a static map
-- without reference to a 'GameState'
-- (i.e. outside the context of an active game)
determineStaticViewCenter ::
  ScenarioLandscape ->
  NonEmpty SubworldDescription ->
  Cosmic Location
determineStaticViewCenter :: ScenarioLandscape
-> NonEmpty SubworldDescription -> Cosmic Location
determineStaticViewCenter ScenarioLandscape
sLandscape NonEmpty SubworldDescription
worldTuples =
  Cosmic Location -> Maybe (Cosmic Location) -> Cosmic Location
forall a. a -> Maybe a -> a
fromMaybe Cosmic Location
defaultVC Maybe (Cosmic Location)
baseRobotLoc
 where
  theRobots :: [TRobot]
theRobots = ScenarioLandscape -> NonEmpty SubworldDescription -> [TRobot]
forall a b.
ScenarioLandscape -> NonEmpty (a, ([(Int, TRobot)], b)) -> [TRobot]
genRobotTemplates ScenarioLandscape
sLandscape NonEmpty SubworldDescription
worldTuples
  defaultVC :: Cosmic Location
defaultVC = SubworldName -> Location -> Cosmic Location
forall a. SubworldName -> a -> Cosmic a
Cosmic SubworldName
DefaultRootSubworld Location
forall (f :: * -> *) a. (Additive f, Num a) => Point f a
origin

  -- The first robot is guaranteed to be the base.
  baseRobotLoc :: Maybe (Cosmic Location)
  baseRobotLoc :: Maybe (Cosmic Location)
baseRobotLoc = do
    TRobot
theBaseRobot <- [TRobot] -> Maybe TRobot
forall a. [a] -> Maybe a
listToMaybe [TRobot]
theRobots
    Getting (Maybe (Cosmic Location)) TRobot (Maybe (Cosmic Location))
-> TRobot -> Maybe (Cosmic Location)
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting (Maybe (Cosmic Location)) TRobot (Maybe (Cosmic Location))
Lens' TRobot (Maybe (Cosmic Location))
trobotLocation TRobot
theBaseRobot