# hpr2969 :: Crewing a spaceship in Haskell

### How to calculate amount of crew needed for a spaceship

Hosted by tuturto on Thursday 2019-12-19 is flagged as Clean and is released under a CC-BY-SA license.

Listen in ogg, spx, or mp3 format. | Comments (0)

### Part of the series: Haskell

A series looking into the Haskell (programming language)

## Intro

Every spaceship in game needs a crew to operate it. Smaller ships with fewer components require less crew than huge ones with lots of components.

## Types

Unit stats lists amount of crew required to operate a spaceship and if they need sleeping quarters.

``````data UnitStats = UnitStats
{ unitStatsMinimumCrew :: ![CrewRequirement]
, unitStatsNominalCrew :: ![CrewRequirement]
, unitStatsCrewSpace :: !TotalCrewSpace
, unitStatsCrewSpaceRequired :: !CrewSpaceReq

Different positions a crew can have is an enumeration:

``````data CrewPosition =
Commander
| Navigator
| Signaler
| SensorOperator
| Gunner
| Doctor
| Nurse
| Driver
| Helmsman
| Artificer
| Crew
| Passenger
deriving (Show, Read, Eq, Enum, Bounded)
derivePersistField "CrewPosition"``````

Rank of a crew member isn’t a military rank, but rather their position in ship’s internal hierarchy:

``````data CrewRank =
SecondClass
| FirstClass
| Senior
| Chief
deriving (Show, Read, Eq, Enum, Bounded)
derivePersistField "CrewRank"``````

Amount of crew is newtype that helps me not to mix different types of numbers with each other.

``````newtype CrewAmount = CrewAmount { unCrewAmount :: Int }
deriving (Show, Read, Eq, Ord, Num)``````

Total crew space of a ship is divided to three different types: steerage, standard and luxury.

``````data TotalCrewSpace = TotalCrewSpace
{ totalCrewSpaceSteerage :: !(CrewSpace SteerageQuarters)
, totalCrewSpaceStandard :: !(CrewSpace StandardQuarters)
, totalCrewSpaceLuxury :: !(CrewSpace LuxuryQuarters)

Again, crew space is newtype so I don’t mix different types of numbers with each other.

``````data CrewSpace a =
CrewSpace { unCrewSpace :: CrewAmount }

I could have modeled fact that vehicle might need crew space with `Bool`, but having a descriptive name and type is more to my liking.

``````data CrewSpaceReq =
CrewSpaceRequired
| CrewSpaceOptional
derivePersistField "CrewSpaceReq"``````

The fact that single person could manage multiple components is reflected by `ComponentCrewReq` having `Double` instead of `Integer`

``````-- | Crew requirements for a component
data ComponentCrewReq =
ComponentCrewReq CrewPosition Double

## In closing

If you have questions, comments or feedback, easiest way to catch me nowdays is by email or in fediverse where I’m `tuturto@mastodon.social`

## Show Transcript

Automatically generated using whisper

``whisper --model tiny --language en hpr2969.wav``

<< First, < Previous, Latest >>