/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk
35 by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken.
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 {
52.1.3 by Gustav Hartvigsson
int -> int32
11
    int32 roll_res = 0;
12
    int32 pool_size = 0;
13
    int32 successes = 0;
35 by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken.
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) {
36 by Gustav Hartvigsson
Refactoring.
24
        throw new Utils.ParamError.OUT_OF_BOUNDS
35 by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken.
25
          (@"$(this.get_type ().name ()) Pool index: $(get_pool_size (instance))");
26
    }
27
28
    bool is_roll_again;
52.1.3 by Gustav Hartvigsson
int -> int32
29
    int32 total_roll_number = 0;
35 by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken.
30
    // XXX
52.1.3 by Gustav Hartvigsson
int -> int32
31
    for (int32 i = 1; i <= pool_size; i++) {
35 by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken.
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) {
36 by Gustav Hartvigsson
Refactoring.
72
          throw new Utils.LoopError.TO_LONG 
35 by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken.
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
   */
52.1.3 by Gustav Hartvigsson
int -> int32
105
  protected virtual bool roll_again (Context instance, int32 roll_result, int32 pool_roll_number) {
35 by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken.
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
   */
52.1.3 by Gustav Hartvigsson
int -> int32
117
  protected abstract int32 get_pool_size (Context instance);
35 by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken.
118
119
  /**
120
   * Returns the index of the parameter that contains the pool_size.
121
   */
52.1.3 by Gustav Hartvigsson
int -> int32
122
  protected abstract int32 get_pool_index ();
35 by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken.
123
124
125
  /**
126
   * Perform a roll and return the result.
127
   */
52.1.3 by Gustav Hartvigsson
int -> int32
128
  protected abstract int32 get_roll (Context instance) throws GLib.Error;
35 by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken.
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
   */
52.1.3 by Gustav Hartvigsson
int -> int32
136
  protected abstract int32 count_successes (Context instance, int32 roll_result) throws GLib.Error;
35 by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken.
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
   */
52.1.2 by Gustav Hartvigsson
no more 's
153
  protected virtual int64 get_max_pool_size (Context instance) throws GLib.Error {
35 by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken.
154
    return get_pool_size (instance);
155
  }
156
157
}