/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk

« back to all changes in this revision

Viewing changes to src/libvqdr/dice.vala

  • Committer: Gustav Hartvigsson
  • Date: 2021-09-15 17:10:52 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210915171052-yhdw16iyipyj5a5l
* Fixed Fastnumber's normalisation problem with the getters/setters

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 * A Simple Represetation of a dice.
25
25
 */
26
26
public class VQDR.Expression.Dice {
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) {
 
27
  public int times { get; set; }
 
28
  public int faces { get; set; }
 
29
  public int modifier { get; set; }
 
30
  
 
31
  
 
32
  public Dice (int times = 1, int faces = 6, int modifier = 0) {
33
33
    this.times = times;
34
34
    this.faces = faces;
35
35
    this.modifier = modifier;
36
36
  }
37
37
  
38
 
  public Dice.single (int32 faces) {
39
 
    this.times = 1;
40
 
    this.faces = faces;
41
 
    this.modifier = 0;
42
 
  }
43
 
  
44
 
  public uint32 roll () {
 
38
  public int roll () {
45
39
    if (faces <= 0) {
46
40
      return modifier;
47
41
    }
54
48
      return times + modifier;
55
49
    }
56
50
    
57
 
    uint32 retval = modifier;
 
51
    int retval = modifier;
58
52
    for (size_t i = 1; i <= times; i++) {
59
 
      uint32 r = Vee.Random.get_static_int () % faces;
 
53
      int r = (VQDR.Common.Random.get_static_int () % faces).abs ();
60
54
      
61
55
      retval += r;
62
56
    }
72
66
    StringBuilder retval = new StringBuilder ();
73
67
    
74
68
    if (times > 0) {
75
 
      retval.append (times.to_string ())
76
 
            .append_c ('d')
77
 
            .append (faces.to_string ());
 
69
      retval.append (times.to_string ()).append_c ('d').append (faces.to_string ());
78
70
    }
79
71
    if (modifier > 0) {
80
72
        retval.append_c ('+').append (modifier.to_string ());
82
74
        retval.append (modifier.to_string ());
83
75
    }
84
76
    
85
 
    return retval.str;
 
77
    return (string) retval.data;
86
78
  }
87
79
  
88
80
}