-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer-streamoflife.sml
219 lines (173 loc) · 6.21 KB
/
player-streamoflife.sml
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
structure StreamOfLife :> LAYER =
struct
open LTG;
open Kompiler;
structure GS = GameState
infix 9 --
val op -- = Apply
val $ = Var
fun \ x exp = Lambda (x, exp)
infixr 1 `
fun a ` b = a b
(* Maybe should have a lower bound on what it will
consider valuable, and just heal/revive if there
are no current high-value targets. *)
fun scoreslot side (idx : int, s : LTG.stat) =
(idx,
(* XXX weighted! *)
if LTG.slotisdead side idx
then ~1000.0
else real (LTG.stat_left_applications s) +
real (LTG.stat_right_applications s) +
LTG.stat_damage_done s +
LTG.stat_healing_done s +
real (LTG.stat_iterations s) +
real (LTG.stat_gotten s))
val compare_scores = ListUtil.bysecond Real.compare
val compare_healths = ListUtil.bysecond Int.compare
val n = ref 0;
(* We're going to want to transfer life from the slot with the most
life to the slot with the least.
XXX? In case of tie, favor the lower numbers.
*)
fun findhighlowhealth side =
let val vitalities = #2 side
val vlist = List.tabulate (256, fn i => (i,Array.sub (vitalities,i)))
val (low, lowval) = ListUtil.min compare_healths vlist
val (high, highval) = ListUtil.max compare_healths vlist
(* only slosh back and forth between two slots. *)
(*
val (high,low) = if !n mod 2 = 0
then (0,1)
else (1,0)
val highval = Array.sub (vitalities, high)
val lowval = Array.sub (vitalities, low)
*)
in
((high,highval), (low,lowval))
end
fun finddeadslot side =
let val vitalities = #2 side
val vlist = List.tabulate (256, fn i => (i,Array.sub (vitalities,i)))
val (low, lowvalue) = ListUtil.min compare_healths vlist
in
if lowvalue <= 0 then SOME (low) else NONE
end
fun findreviverslot side =
let val vitalities = #2 side
val vlist = List.tabulate (253, fn i => (i,Array.sub (vitalities,i + 3)))
val (max, _) = ListUtil.max compare_healths vlist
in
max
end
val fnslot = 0
val target = ref 0
(* applies f[1] to f[0], putting result in f[1] *)
val applyregs = [LeftApply (K, 1),
LeftApply (S, 1),
RightApply (1, Get),
RightApply (1, Zero)]
(* applies (S f[1]) to f[0], putting result in f[1] *)
val sapplyregs = LeftApply(S,1) :: applyregs
(* copies f[1] to f[n] *)
fun copyregs1 n = [LeftApply (Put, n),
RightApply (n, Zero),
LeftApply (Succ, n),
LeftApply (Get, n)]
(* copies f[2] to f[n] *)
fun copyregs2 n = [LeftApply (Put, n),
RightApply (n, Zero),
LeftApply (Succ, n),
LeftApply (Succ, n),
LeftApply (Get, n)]
(* transfer vitality from i to j *)
fun help i j =
(compile (Int i) 1) @
[LeftApply (Help, 1)] @
(compile (Int j) 0) @
applyregs @
(compile (Int 2) 0) @
[LeftApply (Get, 0)] @
applyregs
val builtnum = ref 8192;
fun buildnum n put =
(if put then [LeftApply(Put,n)] else [] ) @
[ RightApply(n,Zero),
LeftApply(Succ,n), (* 1 *)
LeftApply(Dbl,n),
LeftApply(Dbl,n),
LeftApply(Dbl,n),
LeftApply(Dbl,n),
LeftApply(Dbl,n), (* 32 *)
LeftApply(Dbl,n), (* 64 *)
LeftApply(Dbl,n), (* 128 *)
LeftApply(Dbl,n), (* 256 *)
LeftApply(Dbl,n), (* 512 *)
LeftApply(Dbl,n), (* 1024 *)
LeftApply(Dbl,n), (* 2048 *)
LeftApply(Dbl,n), (* 4096 *)
LeftApply(Dbl,n) (* 8192 *)
]
val inithelps = (help 6 0) @
(help 8 1) @
(help 9 2) @
(help 10 0) @
(help 12 1) @
(help 16 2)
val oldinscontext = ref ` help 0 0
val instructions = ref ` (buildnum 2 false ) @ inithelps
(* fun someliveslot myside =
*)
val target = ref 0
fun init gs = ()
val reviving = ref false
fun updateinstructions gs =
let
val myside = GS.myside gs
val theirside = GS.theirside gs
val stats = GS.theirstats gs
val slots = List.tabulate (256, fn i =>
(i, LTG.statfor stats i))
val slots = map (scoreslot theirside) slots
val reviverslot = findreviverslot myside
val (best, _) = ListUtil.max compare_scores slots
val ((high, highval), ( low, lowval)) = findhighlowhealth myside
in case (finddeadslot myside, !reviving)
of (SOME(dead), false) =>
( reviving := true;
oldinscontext := !instructions;
instructions := (compile (Int dead) reviverslot) @
[LeftApply (Revive, reviverslot)]
)
| _ =>
if List.null (!instructions)
then ( (* eprint "restarting instructions\n"; *)
if highval < !builtnum
then (builtnum := Int.div(highval, 2) ;
instructions := ( compile (Int (! builtnum)) 2 )
)
else (if highval > 4 * (!builtnum)
then ( builtnum := (!builtnum) * 2;
instructions := [LeftApply (Dbl, 2)])
else
instructions := help high low )
)
else ()
end
fun taketurn gs =
let
val () = updateinstructions gs
val (ins,inses) = (List.hd (!instructions), List.tl (!instructions))
val _ = instructions := inses
val _ = case ins
of LeftApply (Revive, n) =>
( reviving := false;
instructions := (!oldinscontext)
)
| _ => ()
in
(* eprint ` turn2str ins ^ "\n"; *)
ins
end
end
structure Player = LayerFn(StreamOfLife)