-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
325 lines (295 loc) · 9.08 KB
/
main.js
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
include("functional.js");
include("bool.js");
include("list.js");
include("map");
include("item");
include("target");
include("math.js");
include("chips");
include("game-state.js");
include("pq.js");
include('Funno');
include('danger');
global opin_move = 0, opout_move = 0;
global opin_attack = 0, opout_attack = 0;
global opin_select = 0, opout_select = 0;
global totalPop;
totalPop =@ 0;
global ACTIONS_QUEUE = [];
main();
function getTargets(@state, @item) {
var area = getItemArea(item);
var validCells = [];
var _getTargets =@ ITEMS_TARGETS[item];
if (typeOf(_getTargets) === TYPE_FUNCTION) {
validCells =@ _getTargets(state);
}
else {
debugE("getTargets not implemented for " + getItemName(item));
}
return validCells;
}
function getMove(@gameState) {
if (getSelf(gameState)[MP] === 0) { return []; }
return [function() {
var costs, moves;
getMovementListFromState(gameState, moves, costs);
return daMap(function(cell) { return function() {
var moved = costs[cell];
var self =@ getSelf(gameState);
__obstacles[self[POS]] = false;
__obstacles[cell] = true;
self[MP] -= moved;
self[POS] = cell;
self[AREA_POINT] = getApplicableArea(AREA_POINT)(cell);
self[AREA_CIRCLE_1] = getApplicableArea(AREA_CIRCLE_1)(cell);
self[AREA_CIRCLE_2] = getApplicableArea(AREA_CIRCLE_2)(cell);
self[AREA_CIRCLE_3] = getApplicableArea(AREA_CIRCLE_3)(cell);
return [delay(compose(moveTowardCell)(function(x) {
//mark(x, COLOR_YELLOW);
debug("M -> " + x);
return x;
}))(cell)];
};})(moves);
}];
}
function getAction(@gameState) {
var itemList =@ getActionListFromState(gameState);
var self =@ getSelf(gameState);
var pos = self[POS];
return aMap(delay(function(@item) {
var targets =@ getTargets(gameState, item);
return daMap(function(target) {
return function() {
var ret = [];
var cost = getItemCost(item);
if (isWeapon(item) && item !== self[EQ]) {
cost += 1;
push(ret, delay(setWeapon)(item));
removeElement(self[UNEQ], item);
push(self[UNEQ], self[EQ]);
self[EQ] = item;
}
updateState(self[TP])(subTo(cost));
aIter(ITEMS_EFFECT[item](gameState, self, target))(gameState[S_ALL]);
if (isChip(item)) {
self[CHIPS][item] = getChipCooldown(item);
}
if (cBulbes[item]) {
push(ret, function() {
summon(item, target, function() {
aIter(yield)(shift(ACTIONS_QUEUE));
});
});
}
else {
push(ret, delay(compose(useItemOnCell(item))(function(x){
//mark(x, BEST_COLOR);
debug(getItemName(item) + " -> " + target);
return x;
}))(target));
}
return ret;
};
})(targets);
}))(itemList);
}
function getActions(@gameState) {
var ret = ["actions": [], "state": gameState];
var clonedState =@ ret["state"];
var actions_queue =@ ret["actions"];
do {
var actions =@ (actions_queue[count(actions_queue)] = []);
//for(var i = 0; i < 15; i++) {
while (true) {
var breakout = false;
opin_move += getOperations();
var list =@ getMove(clonedState);
list += list; // I want more chance to move.
opout_move += getOperations();
opin_attack += getOperations();
list += getAction(clonedState);
opout_attack += getOperations();
//if (list === []) { break; }
var curratedList =@ list; //aFilter(notEqual([]))(list);
opin_select += getOperations();
do {
var actionGenerator =@ curratedList[randInt(0, count(curratedList) + 1)];
if (actionGenerator === null) { breakout =@ true; break; }
var actionsGenerated =@ actionGenerator();
if (actionsGenerated !== []) {
actions += actionsGenerated[randInt(0, count(actionsGenerated))]();
break ;
}
} while (true);
opout_select += getOperations();
if (breakout) { break ; }
}
clonedState[S_SELF] = clonedState[S_ORDER][getSelf(clonedState)[ORDER]];
} while (getSelf(clonedState)[SUMMON]);
return ret;
}
function sumState(@all, @ls, @stat) {
return arrayFoldLeft(ls, function(@tlife, @leek) {
return tlife + all[leek][stat];
}, 0);
}
function getStats(@all, @ls) {
var zeroDiv = defaultDiv(0);
return function(@sHP, @sTTP, @sTMP, @sASH, @sRSH, @sSTR, @sAGI, @sminHPR) {
return arrayFoldLeft(ls, function(@_, @leek) {
var lstat =@ all[leek];
var lHP =@ lstat[HP];
var lTHP =@ lstat[THP];
var hpr = lHP / lTHP;
sHP += lHP;
sTTP += all[leek][TTP];
sTMP += all[leek][TMP];
sASH += all[leek][ASH];
sRSH += all[leek][RSH];
sSTR += all[leek][STR];
hpr < sminHPR ? sminHPR =@ hpr : null;
}, null);
};}
function applyEffects(@wearer) {
aIter(applyEffect(wearer))(wearer[EFFS]);
return wearer;
}
function evaluateState(o) {
var oID = getSelf(o)[ID];
var os =@ o[S_ALL];
aIter(compose(removeDead(o))(applyEffects))(os);
var oeAlives = count(o[S_ENEMIES]);
var oaAlives = count(o[S_ALLIES]);
var oeLifes = sumState(os, o[S_ENEMIES], HP);
var oeTTP = sumState(os, o[S_ENEMIES], TTP);
var oeTMP = sumState(os, o[S_ENEMIES], TMP);
var oeMinLife;
var oaLifes = sumState(os, o[S_ALLIES], HP);
var oaTTP = sumState(os, o[S_ALLIES], TTP);
var oaTMP = sumState(os, o[S_ALLIES], TMP);
var oaASH = sumState(os, o[S_ALLIES], ASH);
var oaRSH = sumState(os, o[S_ALLIES], RSH);
var oaSTR = sumState(os, o[S_ALLIES], STR);
var oaAGI = sumState(os, o[S_ALLIES], AGI);
var oneOr = defaultDiv(1);
var thousandOr = defaultDiv(1000);
var infDiv = defaultDiv(-log(0));
var ninfDiv = defaultDiv(log(0));
return /*memo1*/(function(@state) {
var xs =@ state[S_ALL];
var dmgOnSelf = averageDmgFromLeeksOnCell(access(ENEMY))(xs[oID][POS])(xs);
xs[oID][HP] -= dmgOnSelf;
aIter(compose(removeDead(state))(applyEffects))(xs);
var enemies =@ state[S_ENEMIES];
var allies =@ state[S_ALLIES];
var xeAlives = count(enemies);
var xaAlives = count(allies);
var xeLifes = 0;
var xeTTP = 0;
var xeTMP = 0;
var xeminHPR = xeAlives < 1 ? 0 : -log(0);
getStats(xs, enemies)(xeLifes, xeTTP, xeTMP, 0, 0, 0, 0, xeminHPR);
xeminHPR /= 13;
xeminHPR += 12 / 13;
var xaLifes = 0;
var xaTTP = 0;
var xaTMP = 0;
var xaASH = 0;
var xaRSH = 0;
var xaSTR = 0;
var xaAGI = 0;
var xaminHPR = xaAlives < 1 ? 0 : -log(0);
getStats(xs, allies)
(xaLifes, xaTTP, xaTMP, xaASH, xaRSH, xaSTR, xaAGI, xaminHPR);
xaminHPR = 1 - xaminHPR;
xaminHPR /= 13;
xaminHPR += 12 / 13;
var xSelfHPR = xs[oID] === null ? -log(0) : xs[oID][THP] / min(xs[oID][THP], 1 * xs[oID][HP]);
var xeDist = 1;
var xaDist = 1;
var spos = xs[oID][POS];
if (spos !== null) {
var getDist =@ function(@e) {
return getPathLength(spos, xs[e][POS]);
};
xeDist =@ arrayMin(aMap(getDist)(enemies));
xeDist =@ max(1, (100 + max(0, xeDist - 7 - getSelf(state)[TMP])) / 100);
if (xaAlives > 1) {
xaDist =@ sum(aMap(getDist)(allies)) / xaAlives;
xaDist =@ max(1, (100 + max(0, xaDist - getSelf(state)[TMP])) / 100);
}
}
return (1 + xeDist)
* (1 + xaDist)
* (1 + ninfDiv(xeLifes)(oeLifes))
* (1 + ninfDiv(xeAlives)(oeAlives))
* (1 + xeminHPR)
* (1 + infDiv(oaLifes)(xaLifes))
* (1 + infDiv(oaAlives)(xaAlives))
* (1 + xaminHPR)
* (1 + xSelfHPR)
* (1 + oneOr(oaASH)(xaASH))
* (1 + oneOr(oaRSH)(xaRSH))
* (1 + oneOr(oaSTR)(xaSTR))
* (1 + oneOr(oaAGI)(xaAGI))
* (1 + oneOr(xeTTP)(oeTTP))
* (1 + oneOr(xeTMP)(oeTMP))
* (1 + infDiv(oaTTP)(xaTTP))
* (1 + infDiv(oaTMP)(xaTMP));
});
}
function main() {
if (getWeapon() === null) {
setWeapon(arraySort(getWeapons(), function(@x, @y) {
var xr = getWeaponMaxRange(x);
var yr = getWeaponMaxRange(y);
return xr > yr ? -1 : xr === yr ? 0 : 1;
})[0]);
}
initObstacles(aMap(getCell)(getAliveAllies() + getAliveEnemies()));
var baseObstacles = __obstacles;
var gameState = generateGameState();
debug(getOperations() + 'op');
var evaluate = evaluateState(gameState);
var population = pqEmpty();
var popInsert = pqInsert(population);
var popSelect = pqPop(population);
var existing = [];
opin_move = 0;
opout_move = 0;
opin_attack = 0;
opout_attack = 0;
opin_select = 0;
opout_select = 0;
var opin_evaluate = 0, opout_evaluate = 0;
var opin_prio = 0, opout_prio = 0;
var maxOp = 18500000;
while (getOperations() < maxOp) {
__obstacles = baseObstacles;
var actions =@ getActions(gameState);
opin_evaluate += getOperations();
var value = evaluate(actions["state"]);
opout_evaluate += getOperations();
totalPop++;
opin_prio += getOperations();
popInsert(value, actions);
opout_prio += getOperations();
}
debugC((opout_move - opin_move) * 100 / OPERATIONS_LIMIT, COLOR_GREEN);
debugC((opout_attack - opin_attack) * 100 / OPERATIONS_LIMIT, COLOR_RED);
debugC((opout_select - opin_select) * 100 / OPERATIONS_LIMIT, COLOR_BLUE);
debugC((opout_select - opin_move) * 100 / OPERATIONS_LIMIT, 0);
debugC((opout_evaluate - opin_evaluate) * 100 / OPERATIONS_LIMIT, BEST_COLOR);
debugC((opout_prio - opin_prio) * 100 / OPERATIONS_LIMIT, 0);
debug("totalPop: " + totalPop);
debug('');
var value;
var elected = population[0](value);
debug('value = ' + value);
debugC('action count: ' + count(elected['actions'][0]), BEST_COLOR);
ACTIONS_QUEUE =@ elected['actions'];
aIter(yield)(shift(ACTIONS_QUEUE));
sayShit();
}
debug(getOperations() + 'op');