/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: 2022-06-01 12:44:04 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20220601124404-mpv4761nr16f0duq
run_gdb.sh +x

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
21
using GLib;
2
22
 
3
 
namespace VQDR.Expression {
4
 
  
5
 
  /**
6
 
   * A Simple Represetation of a dice.
7
 
   */
8
 
  public class Dice {
9
 
    public int times { get; set; }
10
 
    public int faces { get; set; }
11
 
    public int modifier { get; set; }
12
 
    
13
 
    
14
 
    public Dice (int times = 1, int faces = 6, int modifier = 0) {
15
 
      this.times = times;
16
 
      this.faces = faces;
17
 
      this.modifier = modifier;
 
23
/**
 
24
 * A Simple Represetation of a dice.
 
25
 */
 
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) {
 
33
    this.times = times;
 
34
    this.faces = faces;
 
35
    this.modifier = modifier;
 
36
  }
 
37
  
 
38
  public Dice.singel (int32 faces) {
 
39
    this.times = 1;
 
40
    this.faces = faces;
 
41
    this.modifier = 0;
 
42
  }
 
43
  
 
44
  public int32 roll () {
 
45
    if (faces <= 0) {
 
46
      return modifier;
18
47
    }
19
48
    
20
 
    public int roll () {
21
 
      if (faces <= 0) {
 
49
    if (times <= 0) {
22
50
        return modifier;
23
 
      }
24
 
      
25
 
      if (times <= 0) {
26
 
          return modifier;
27
 
      }
28
 
      
29
 
      if (faces == 1) {
30
 
        return times + modifier;
31
 
      }
32
 
      
33
 
      int retval = modifier;
34
 
      for (size_t i = 1; i <= times; i++) {
35
 
        int r = (VQDR.Common.Random.get_static_int () % faces).abs ();
36
 
        retval += r;
37
 
        
38
 
      }
39
 
      
40
 
      return retval;
41
 
    }
42
 
    
43
 
    public string to_string () {
44
 
      if ((times == 0) && (faces == 0)) {
45
 
        return "0";
46
 
      }
47
 
      
48
 
      StringBuilder retval = new StringBuilder ();
49
 
      
50
 
      if (times > 0) {
51
 
        retval.append (times.to_string ()).append_c ('d').append (faces.to_string ());
52
 
      }
53
 
      if (modifier > 0) {
54
 
          retval.append_c ('+').append (modifier.to_string ());
55
 
      } else if (modifier < 0) {
56
 
          retval.append (modifier.to_string ());
57
 
      }
58
 
      
59
 
      return (string) retval.data;
60
 
    }
61
 
    
62
 
  }
 
51
    }
 
52
    
 
53
    if (faces == 1) {
 
54
      return ((int32) times) + modifier;
 
55
    }
 
56
    
 
57
    int32 retval = modifier;
 
58
    for (size_t i = 1; i <= times; i++) {
 
59
      // we have to do this ugly mess, as int32 is missing the abs () method.
 
60
      // bug: https://gitlab.gnome.org/GNOME/vala/-/issues/1328
 
61
      int32 r = ((int)(Utils.Random.get_static_int () % (int)faces)).abs ();
 
62
      
 
63
      retval += r;
 
64
    }
 
65
    
 
66
    return retval;
 
67
  }
 
68
  
 
69
  public string to_string () {
 
70
    if ((times == 0) && (faces == 0)) {
 
71
      return "0";
 
72
    }
 
73
    
 
74
    StringBuilder retval = new StringBuilder ();
 
75
    
 
76
    if (times > 0) {
 
77
      retval.append (times.to_string ()).append_c ('d').append (faces.to_string ());
 
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
    
 
85
    return retval.str;
 
86
  }
 
87
  
63
88
}