Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Dice.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_DICE_H
22 #define GF_DICE_H
23 
24 #include <cassert>
25 #include <climits>
26 
27 #include "Portability.h"
28 #include "Random.h"
29 #include "StringRef.h"
30 
31 namespace gf {
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 inline namespace v1 {
34 #endif
35 
43  class GF_API Dice {
44  public:
55  constexpr explicit Dice(int faces, int count = 1, int modifier = 0)
56  : m_faces(faces)
57  , m_count(count)
58  , m_modifier(modifier)
59  {
60 
61  }
62 
68  constexpr Dice(StringRef str)
69  : m_faces(0)
70  , m_count(0)
71  , m_modifier(0)
72  {
73  std::size_t i = 0;
74  std::size_t sz = str.getSize();
75 
76  while (i < sz && '0' <= str[i] && str[i] <= '9') {
77  m_count = m_count * 10 + (str[i] - '0');
78  ++i;
79  }
80 
81  if (i == sz) {
82  return;
83  }
84 
85  if (str[i] != 'd' && str[i] != 'D') {
86  return;
87  }
88 
89  if (m_count == 0) {
90  m_count = 1;
91  }
92 
93  ++i; // eat 'd'
94 
95  while (i < sz && '0' <= str[i] && str[i] <= '9') {
96  m_faces = m_faces * 10 + (str[i] - '0');
97  ++i;
98  }
99 
100  if (i == sz) {
101  return;
102  }
103 
104  if (str[i] != '+') {
105  return;
106  }
107 
108  ++i; // eat '+'
109 
110  while (i < sz && '0' <= str[i] && str[i] <= '9') {
111  m_modifier = m_modifier * 10 + (str[i] - '0');
112  ++i;
113  }
114  }
115 
121  constexpr int getFaces() const {
122  return m_faces;
123  }
124 
130  constexpr int getCount() const {
131  return m_count;
132  }
133 
139  constexpr int getModifier() const {
140  return m_modifier;
141  }
142 
148  int roll(gf::Random& random) const;
149 
150  private:
151  int m_faces;
152  int m_count;
153  int m_modifier;
154  };
155 
156 
157  namespace literals {
158 
169  constexpr gf::Dice operator"" _dice(const char *str, std::size_t sz) {
170  return gf::Dice(StringRef(str, sz));
171  }
172 
179  constexpr gf::Dice operator"" _d4(unsigned long long int count) {
180  assert(count <= INT_MAX);
181  return gf::Dice(4, static_cast<int>(count));
182  }
183 
190  constexpr gf::Dice operator"" _d6(unsigned long long int count) {
191  assert(count <= INT_MAX);
192  return gf::Dice(6, static_cast<int>(count));
193  }
194 
201  constexpr gf::Dice operator"" _d8(unsigned long long int count) {
202  assert(count <= INT_MAX);
203  return gf::Dice(8, static_cast<int>(count));
204  }
205 
212  constexpr gf::Dice operator"" _d10(unsigned long long int count) {
213  assert(count <= INT_MAX);
214  return gf::Dice(10, static_cast<int>(count));
215  }
216 
223  constexpr gf::Dice operator"" _d12(unsigned long long int count) {
224  assert(count <= INT_MAX);
225  return gf::Dice(12, static_cast<int>(count));
226  }
227 
234  constexpr gf::Dice operator"" _d20(unsigned long long int count) {
235  assert(count <= INT_MAX);
236  return gf::Dice(20, static_cast<int>(count));
237  }
238 
245  constexpr gf::Dice operator"" _d100(unsigned long long int count) {
246  assert(count <= INT_MAX);
247  return gf::Dice(100, static_cast<int>(count));
248  }
249  }
250 
251 #ifndef DOXYGEN_SHOULD_SKIP_THIS
252 }
253 #endif
254 }
255 
256 #endif // GF_DICE_H
A random engine.
Definition: Random.h:47
constexpr int getCount() const
Get the count of dice.
Definition: Dice.h:130
constexpr int getModifier() const
Get the modifier.
Definition: Dice.h:139
constexpr Dice(int faces, int count=1, int modifier=0)
Constructor.
Definition: Dice.h:55
constexpr int getFaces() const
Get the number of faces.
Definition: Dice.h:121
The namespace for gf classes.
Definition: Action.h:35
constexpr Dice(StringRef str)
Constructor.
Definition: Dice.h:68
A set of dice that can be rolled.
Definition: Dice.h:43
A constant reference to a string and its size.
Definition: StringRef.h:41
constexpr std::size_t getSize() const noexcept
Get the size of the string.
Definition: StringRef.h:136