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