/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/function/abstract_pool_token.vala

  • Committer: Gustav Hartvigsson
  • Date: 2021-11-11 19:49:21 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20211111194921-ayb9vuhi7wzfucx0
a few things: FastNumbers, simplifications and AbstractPoolToken.

* Swinched Context over to FastNumber

* simmplified logic in Variabel.compare () Variabel.equals ()
  and Variabel.equals_values.

* Tests: Don't use 'new' with structs.

* The Dice class now has a .singel constructor.

* Put RollAndKeepFunctionToken in the corret namespace.

* Added test function for constant value token to the
  token test program.

* Fixed error with test path for GLib.Test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using VQDR.Expression;
 
2
 
 
3
public abstract class VQDR.Expression.AbstractPoolToken : FunctionToken {
 
4
  protected static Dice stadard_dice = new Dice.singel (10);
 
5
 
 
6
  protected AbstractPoolToken () {
 
7
    base ();
 
8
  }
 
9
 
 
10
  protected override void evaluate_self (Context instance) throws GLib.Error {
 
11
    int roll_res = 0;
 
12
    int pool_size = 0;
 
13
    int successes = 0;
 
14
 
 
15
    this.result_value.number = 0;
 
16
 
 
17
    StringBuilder strbldr = new StringBuilder ();
 
18
    strbldr.append (SYM_BEGIN);
 
19
 
 
20
    init_sequence (instance);
 
21
 
 
22
    pool_size = get_pool_size (instance);
 
23
    if (pool_size > MAX_TOKEN_ITERATIONS) {
 
24
        throw new Common.ParamError.OUT_OF_BOUNDS
 
25
          (@"$(this.get_type ().name ()) Pool index: $(get_pool_size (instance))");
 
26
    }
 
27
 
 
28
    bool is_roll_again;
 
29
    int total_roll_number = 0;
 
30
    // XXX
 
31
    for (int i = 1; i <= pool_size; i++) {
 
32
      is_roll_again = false;
 
33
      do {
 
34
        if (strbldr.len < MAX_TOKEN_STRING_LENGTH) {
 
35
          if (is_roll_again) {
 
36
            strbldr.append (SYM_EXPLODE);
 
37
          } else if (i > 1) {
 
38
            strbldr.append (SYM_SEP);
 
39
          }
 
40
        }
 
41
 
 
42
        roll_res = get_roll (instance);
 
43
 
 
44
        successes = count_successes (instance, roll_res);
 
45
 
 
46
        if (strbldr.len < MAX_TOKEN_STRING_LENGTH) {
 
47
          strbldr.append (roll_res.to_string ());
 
48
        }
 
49
 
 
50
        /*
 
51
         * place a Success mark for every roll that successeeded.
 
52
         */
 
53
        for (var j = 0; j < successes; j++) {
 
54
          if (strbldr.len < MAX_TOKEN_STRING_LENGTH) {
 
55
            strbldr.append (SYM_SUCCESS);
 
56
          }
 
57
        }
 
58
 
 
59
        /*
 
60
         * place a Failure mark for every failed roll.
 
61
         */
 
62
        for (var j = 0; j > successes; j--) {
 
63
          if (strbldr.len < MAX_TOKEN_STRING_LENGTH) {
 
64
            strbldr.append (SYM_FAILURE);
 
65
          }
 
66
        }
 
67
 
 
68
        this.result_value.number += successes;
 
69
        
 
70
        total_roll_number++;
 
71
        if (total_roll_number > MAX_TOKEN_ITERATIONS) {
 
72
          throw new Common.LoopError.TO_LONG 
 
73
            (@"$(get_function_name (this.get_type ())): Loop took more than $MAX_TOKEN_ITERATIONS to complete.");
 
74
        }
 
75
        is_roll_again = true;
 
76
      } while (roll_again (instance, roll_res, i));
 
77
 
 
78
      end_sequence (instance);
 
79
      if (get_max_pool_size (instance) < 1) {
 
80
        result_max_value.number = 1;
 
81
      } else {
 
82
        result_max_value.number = get_max_pool_size (instance);
 
83
      }
 
84
 
 
85
      if (strbldr.len < MAX_TOKEN_STRING_LENGTH) {
 
86
        strbldr.append (SYM_END);
 
87
        result_string = strbldr.str;
 
88
      } else {
 
89
        result_string = SYM_TRUNK_BEGIN + result_value.number.to_string () + SYM_TRUNK_END;
 
90
      }
 
91
 
 
92
    } // end for loop
 
93
 
 
94
  }
 
95
 
 
96
  /**
 
97
   * Tell if another roll is required.
 
98
   *
 
99
   * @param instance         the instance of the context to run in.
 
100
   * @param roll_result      the result to be evaluated.
 
101
   * @param pool_roll_number the number of rolls according to the pool size (zero base)
 
102
   *
 
103
   * @return ''true'' if another roll is required, ''false'' otherwise.
 
104
   */
 
105
  protected virtual bool roll_again (Context instance, int roll_result, int pool_roll_number) {
 
106
    return false;
 
107
  }
 
108
 
 
109
  /**
 
110
   * Used to initalize a specific values.
 
111
   */
 
112
  protected abstract void init_sequence (Context instance);
 
113
 
 
114
  /**
 
115
   * Return the pool size, or the number of rolls to performe.
 
116
   */
 
117
  protected abstract int get_pool_size (Context instance);
 
118
 
 
119
  /**
 
120
   * Returns the index of the parameter that contains the pool_size.
 
121
   */
 
122
  protected abstract int get_pool_index ();
 
123
 
 
124
 
 
125
  /**
 
126
   * Perform a roll and return the result.
 
127
   */
 
128
  protected abstract int get_roll (Context instance) throws GLib.Error;
 
129
 
 
130
  /**
 
131
   * Count the number of successes obtained with this roll.
 
132
   * (can be negative)
 
133
   *
 
134
   * @return the number of Successes for this roll.
 
135
   */
 
136
  protected abstract int count_successes (Context instance, int roll_result) throws GLib.Error;
 
137
 
 
138
  /**
 
139
   * Used to evetually performe controls after the pool rolls.
 
140
   */
 
141
  protected virtual void end_sequence (Context instance) throws GLib.Error {
 
142
    // NOOP
 
143
  }
 
144
 
 
145
  /**
 
146
   * Returns the maximum size of the pool.
 
147
   *
 
148
   * This should be equal to ''get_pool_size ()'', but the implementation is
 
149
   * allowed to specify any pool size.
 
150
   *
 
151
   * @return the maximum pool size.
 
152
   */
 
153
  protected virtual long get_max_pool_size (Context instance) throws GLib.Error {
 
154
    return get_pool_size (instance);
 
155
  }
 
156
 
 
157
}