aboutsummaryrefslogtreecommitdiff
path: root/calculette_aoo/lib/carac.ml
blob: a48c7346280ded8ebecf839f27616c9a043a3775 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
type cout_carac = int * int * int

type t = {
    value : int
  ; couts : cout_carac
  ; bonus : int
}

let create : ?bonus:int -> int -> cout_carac -> t =
 fun ?(bonus = 0) value couts -> { value; couts; bonus }

let incr ?(step = 1) t = { t with bonus = t.bonus + step }

(*
Evaluate the cost for the successives upgrades. 

I’m pretty sure this can be transformed into a linear function, but I do not see how…  

    c0 * t.bonus 
+   max 0 ((((t.bonus - 1) * 2) - 1) * c1)
+   ?

 *)
let cout : t -> int =
 fun t ->
  let c0, c1, c2 = t.couts in
  let rec c acc t =
    match t with
    | 0 -> acc
    | 1 -> acc + c0
    | 2 -> c (acc + c0 + c1) (t - 1)
    | 3 -> c (acc + c0 + (c1 * 2)) (t - 1)
    | n -> c (acc + c0 + (c1 * 2) + ((n - 3) * c2)) (t - 1)
  in
  c 0 t.bonus

let value t = t.value + t.bonus

let repr : Format.formatter -> t -> unit =
 fun out t -> Format.fprintf out "%d (+%d)" (value t) t.bonus