• Modeling special events, options and player choice in game

    From Tuukka Turto@21:1/5 to All on Mon Oct 15 21:05:08 2018
    I'm working on a system that could represent special events, available options to those events and player choice in a game. For example:

    data CropsViltedEvent = CropsVilted

    data CropsViltedOptions = HarvestNow
    | TryToNurseCrops

    Essentially, if crops have vilted, player has possibility to harvest what's left or try to nurse them back to alive and possibly later get almost full harvest.

    There are many different kinds of events and they are all with similar structure (event, 1 or more options to choose from and result which
    in the example is represented as IO ()).

    I tried to make a type class for this:

    class SpecialEvent a where
    eventDescription :: a -> Text
    eventOptions :: a -> [?]
    handleChoice :: a -> ? -> IO ()

    But don't know how to handle ? parts (options and player choices) corrently.
    I would like to have CropsViltedEvent and CropsViltedOptions somehow linked together, so it wouldn't be possible to respond to CropsViltedEvent with
    some choice related to completely different event.

    One option I could think of is using multiple type parameters:

    class SpecialEvent a b where
    eventDescription :: a -> b -> Text
    eventOptions :: a -> [b]
    handleChoice :: a -> b -> IO ()

    but then I need to pass in both event and options to get the event
    description.

    What else options would I have? I haven't been programming in Haskell that much, so I might be missing something completely obvious.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mark Carroll@21:1/5 to Tuukka Turto on Tue Oct 16 08:19:48 2018
    On 15 Oct 2018, Tuukka Turto wrote:

    I would like to have CropsViltedEvent and CropsViltedOptions somehow linked together, so it wouldn't be possible to respond to CropsViltedEvent with
    some choice related to completely different event.

    This part makes me wonder if
    https://wiki.haskell.org/Functional_dependencies
    is what you want.

    -- Mark

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tuukka Turto@21:1/5 to Mark Carroll on Tue Oct 16 09:26:50 2018
    On Tuesday, 16 October 2018 10:19:49 UTC+3, Mark Carroll wrote:
    On 15 Oct 2018, Tuukka Turto wrote:

    I would like to have CropsViltedEvent and CropsViltedOptions somehow linked together, so it wouldn't be possible to respond to CropsViltedEvent with some choice related to completely different event.

    This part makes me wonder if
    https://wiki.haskell.org/Functional_dependencies
    is what you want.

    -- Mark

    Thanks, this seems to be exactly what I was looking for.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)