bzr branch
http://gegoxaren.bato24.eu/bzr/vqdr/trunk
|
19
by Gustav Hartvigsson
Added Round Up and Round Down function classes. |
1 |
/* dice.vala
|
2 |
*
|
|
3 |
* Copyright 2021 Gustav Hartvigsson <unknown@domain.org>
|
|
4 |
*
|
|
5 |
* This file is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU Lesser General Public License as
|
|
7 |
* published by the Free Software Foundation; either version 3 of the
|
|
8 |
* License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This file is distributed in the hope that it will be useful, but
|
|
11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 |
* Lesser General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU Lesser General Public
|
|
16 |
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17 |
*
|
|
18 |
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
19 |
*/
|
|
20 |
||
|
1
by Gustav Hartvigsson
* Initial code - far from done |
21 |
using GLib; |
22 |
||
|
19
by Gustav Hartvigsson
Added Round Up and Round Down function classes. |
23 |
/**
|
24 |
* A Simple Represetation of a dice.
|
|
25 |
*/
|
|
26 |
public class VQDR.Expression.Dice { |
|
|
52.1.3
by Gustav Hartvigsson
int -> int32 |
27 |
public uint32 times { get; set; } |
28 |
public uint32 faces { get; set; } |
|
29 |
public int32 modifier { get; set; } |
|
30 |
|
|
31 |
|
|
32 |
public Dice (uint32 times = 1, uint32 faces = 6, int32 modifier = 0) { |
|
|
19
by Gustav Hartvigsson
Added Round Up and Round Down function classes. |
33 |
this.times = times; |
34 |
this.faces = faces; |
|
35 |
this.modifier = modifier; |
|
36 |
} |
|
37 |
|
|
|
62
by Gustav Hartvigsson
various changes |
38 |
public Dice.single (int32 faces) { |
|
35
by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken. |
39 |
this.times = 1; |
40 |
this.faces = faces; |
|
41 |
this.modifier = 0; |
|
42 |
} |
|
43 |
|
|
|
60
by Gustav Hartvigsson
Added int32_abs () to Utils. |
44 |
public uint32 roll () { |
|
19
by Gustav Hartvigsson
Added Round Up and Round Down function classes. |
45 |
if (faces <= 0) { |
46 |
return modifier; |
|
|
1
by Gustav Hartvigsson
* Initial code - far from done |
47 |
} |
48 |
|
|
|
19
by Gustav Hartvigsson
Added Round Up and Round Down function classes. |
49 |
if (times <= 0) { |
|
1
by Gustav Hartvigsson
* Initial code - far from done |
50 |
return modifier; |
|
19
by Gustav Hartvigsson
Added Round Up and Round Down function classes. |
51 |
} |
52 |
|
|
53 |
if (faces == 1) { |
|
|
60
by Gustav Hartvigsson
Added int32_abs () to Utils. |
54 |
return times + modifier; |
|
19
by Gustav Hartvigsson
Added Round Up and Round Down function classes. |
55 |
} |
56 |
|
|
|
60
by Gustav Hartvigsson
Added int32_abs () to Utils. |
57 |
uint32 retval = modifier; |
|
19
by Gustav Hartvigsson
Added Round Up and Round Down function classes. |
58 |
for (size_t i = 1; i <= times; i++) { |
|
64
by Gustav Hartvigsson
[General] Major refactoring. Utils -> Vee nenaming. |
59 |
uint32 r = Vee.Random.get_static_int () % faces; |
|
19
by Gustav Hartvigsson
Added Round Up and Round Down function classes. |
60 |
|
61 |
retval += r; |
|
62 |
} |
|
63 |
|
|
64 |
return retval; |
|
65 |
} |
|
66 |
|
|
67 |
public string to_string () { |
|
68 |
if ((times == 0) && (faces == 0)) { |
|
69 |
return "0"; |
|
70 |
} |
|
71 |
|
|
72 |
StringBuilder retval = new StringBuilder (); |
|
73 |
|
|
74 |
if (times > 0) { |
|
|
57
by Gustav Hartvigsson
Made sure that no text rows exeed 80 columns. |
75 |
retval.append (times.to_string ()) |
76 |
.append_c ('d') |
|
77 |
.append (faces.to_string ()); |
|
|
19
by Gustav Hartvigsson
Added Round Up and Round Down function classes. |
78 |
} |
79 |
if (modifier > 0) { |
|
80 |
retval.append_c ('+').append (modifier.to_string ()); |
|
81 |
} else if (modifier < 0) { |
|
82 |
retval.append (modifier.to_string ()); |
|
83 |
} |
|
84 |
|
|
|
31
by Gustav Hartvigsson
Changed over the Variabel class to use FastNumers |
85 |
return retval.str; |
|
19
by Gustav Hartvigsson
Added Round Up and Round Down function classes. |
86 |
} |
87 |
|
|
|
1
by Gustav Hartvigsson
* Initial code - far from done |
88 |
}
|