roll.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // roll.hpp
  3. // dice-roll
  4. //
  5. // Created by Sam Jaffe on 12/1/18.
  6. // Copyright © 2018 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include <vector>
  10. #include "die.h"
  11. #include "random.h"
  12. namespace dice {
  13. enum class outcome {
  14. PASS,
  15. FAIL,
  16. };
  17. struct die_outcome {
  18. friend auto operator<=>(die_outcome const &, die_outcome const &) = default;
  19. int roll;
  20. int sides;
  21. };
  22. // Describe the actual result of rolling (+/-)NdM
  23. struct die_roll {
  24. // Collapse this roll into its actual value
  25. operator int() const;
  26. // Is this being added, or subtracted from the total
  27. sign sign;
  28. // Since this roll was composed on NdM, rolled.size() == N. Each element
  29. // of rolled is within the integer range [1, M].
  30. std::vector<die_outcome> rolled;
  31. };
  32. // Describe the actual result of rolling an arbitrary set of dice with mods
  33. struct dice_roll {
  34. // Collapse this roll into its actual value
  35. operator int() const;
  36. std::variant<int, outcome> result() const;
  37. // A vector of component roll results, each on representing a single NdM
  38. // expression.
  39. std::vector<die_roll> sub_rolls;
  40. // A vector of every modifier attached to the system.
  41. std::vector<mod> modifiers;
  42. difficulty_class dc;
  43. };
  44. class roller {
  45. public:
  46. roller();
  47. roller(engine::random && g);
  48. /**
  49. * @param d Some dice roll structure, containing any number of dice sets 'NdM'
  50. * as well as any number of roll modifiers (fixed numbers). Additionally,
  51. * can contain a repetition parameter.
  52. * @return A vector of actualized rolls, where `vector.size() == d.num`.
  53. */
  54. std::vector<dice_roll> operator()(dice const & d);
  55. private:
  56. engine::random gen;
  57. };
  58. /**
  59. * Print out the component elements of an actualized dice roll.
  60. * Use instead `out << int(r)` to print the final summation.
  61. */
  62. std::ostream & operator<<(std::ostream & out, dice_roll const & r);
  63. }