roll.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. bool dropped{false};
  20. int roll;
  21. int sides;
  22. };
  23. // Describe the actual result of rolling (+/-)NdM
  24. struct die_roll {
  25. // Collapse this roll into its actual value
  26. operator int() const;
  27. // Is this being added, or subtracted from the total
  28. sign sign;
  29. // Since this roll was composed on NdM, rolled.size() == N. Each element
  30. // of rolled is within the integer range [1, M].
  31. std::vector<die_outcome> rolled;
  32. };
  33. // Describe the actual result of rolling an arbitrary set of dice with mods
  34. struct dice_roll {
  35. // Collapse this roll into its actual value
  36. operator int() const;
  37. std::variant<int, outcome> result() const;
  38. // A vector of component roll results, each on representing a single NdM
  39. // expression.
  40. std::vector<die_roll> sub_rolls;
  41. // A vector of every modifier attached to the system.
  42. std::vector<mod> modifiers;
  43. difficulty_class dc;
  44. };
  45. class roller {
  46. public:
  47. roller();
  48. roller(engine::random && g);
  49. /**
  50. * @param d Some dice roll structure, containing any number of dice sets 'NdM'
  51. * as well as any number of roll modifiers (fixed numbers). Additionally,
  52. * can contain a repetition parameter.
  53. * @return A vector of actualized rolls, where `vector.size() == d.num`.
  54. */
  55. std::vector<dice_roll> operator()(dice const & d);
  56. private:
  57. engine::random gen;
  58. };
  59. /**
  60. * Print out the component elements of an actualized dice roll.
  61. * Use instead `out << int(r)` to print the final summation.
  62. */
  63. std::ostream & operator<<(std::ostream & out, dice_roll const & r);
  64. }