forked from aishowdown/chickenAttack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viz.html
347 lines (263 loc) · 10.9 KB
/
viz.html
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<!DOCTYPE HTML>
<html>
<head>
<title>AI Showdown</title>
<script src='jquery.min.js'></script>
<script src='game_log.js'></script>
<script>
$(document).ready(function(){
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var canvas2 = document.getElementById('canvas2');
var ctx2 = canvas2.getContext('2d');
var rgb;
var roundSpeed = 50;
var oneColor = "#ff0000";
var twoColor = "#0000ff";
var round = 0;
var savedImage;
var borderWidth = 2;
var playerBorderWidth = 1;
var borderColor = "rgb("+200+","+150+","+150+")";
var startH = 114;
var endH = 23;
var MAP_START_Y = 100;
var CANVAS_WIDTH = 500;
var CANVAS_HEIGHT = 600;
var NUM_ROUNDS = 1000;
var startSat = 100;
var endSat = 21;
var startL = 15;
var endL = 40;
var paused = false;
var savedImage;
var json = window.game;
var playGame = function(){
var ctx = ctx2
//make base layer of map
round = 0;
NUM_ROUNDS = json.turns.length;
map = json.money_payout_rates;
for(var x = 0; x<map.length; x++){
for(var y = 0; y<map[x].length; y++){
var inter = Math.sqrt(2*map[x][y]-map[x][y]*map[x][y]);
var h = Math.floor(startH - (startH-endH) * inter);
var s = Math.floor(startSat - (startSat-endSat) * map[x][y]);
var l = Math.floor(startL - (startL-endL) * map[x][y]);
ctx.fillStyle="hsla("+h+","+s+"%,"+l+"%,1)";
ctx.fillRect(x*10,y*10+MAP_START_Y,10,10);
}
}
//draw spawn points
drawBase(ctx, "rgb("+150+","+100+","+50+")", json.p1_spawn[0], json.p1_spawn[1]);
drawBase(ctx, "rgb("+100+","+200+","+50+")", json.p2_spawn[0], json.p2_spawn[1]);
savedImage = ctx.getImageData(0, 0, CANVAS_HEIGHT, CANVAS_WIDTH);
var max = 0;
var maxGold = 0;
for(var turn = 0; turn<NUM_ROUNDS; turn++){
var total = 0;
for(var p1 = 0; p1<json.turns[turn].p1g.length; p1++){
total += parseInt(json.turns[turn].p1g[p1]);
}
if(json.turns[turn].p1m>maxGold){
maxGold = json.turns[turn].p1m;
}
if (total > max){
max = total
}
total = 0;
for(var p1 = 0; p1<json.turns[turn].p2g.length; p1++){
total += json.turns[turn].p2g[p1][2];
}
if (total > max){
max = total
}
if(json.turns[turn].p2m>maxGold){
maxGold = json.turns[turn].p2m;
}
}
ctx.fillStyle="rgb(0,0,0)";
ctx.fillRect(0, 0, CANVAS_WIDTH, MAP_START_Y);
for(var turn = 0; turn<json.turns.length; turn++){
var total = 0;
for(var p1 = 0; p1<json.turns[turn].p1g.length; p1++){
total += json.turns[turn].p1g[p1][2];
}
var mark = (total*MAP_START_Y/max);
ctx.fillStyle="rgb(255,0,0)";
ctx.fillRect((turn/json.turns.length)*CANVAS_WIDTH, MAP_START_Y-mark, 1, 1);
total = 0;
for(var p1 = 0; p1<json.turns[turn].p2g.length; p1++){
total += parseInt(json.turns[turn].p2g[p1]);
}
mark = (total*MAP_START_Y/max);
ctx.fillStyle="rgb(0,0,255)";
ctx.fillRect((turn/json.turns.length)*CANVAS_WIDTH, MAP_START_Y-mark, 1, 1);
mark = (json.turns[turn].p2m*MAP_START_Y/maxGold);
ctx.fillStyle="rgba(133, 212, 255, 1)";
ctx.fillRect((turn/json.turns.length)*CANVAS_WIDTH, MAP_START_Y-mark, 1, 1);
mark = (json.turns[turn].p1m*MAP_START_Y/maxGold);
ctx.fillStyle="rgba(255, 133, 133, 1)";
ctx.fillRect((turn/json.turns.length)*CANVAS_WIDTH, MAP_START_Y-mark, 1, 1);
}
playRound();
};
var drawAmounts = function(){
}
var drawBase = function(ctx, color, x, y){
ctx.fillStyle=borderColor;
ctx.fillRect(x*10,y*10+MAP_START_Y,10,10);
ctx.fillStyle=color;
ctx.fillRect(x*10+borderWidth,y*10+borderWidth+MAP_START_Y,10-(2*borderWidth),10-(2*borderWidth));
}
var drawPlayer = function(color, x, y){
ctx.fillStyle=color;
ctx.fillRect(x*10+playerBorderWidth,y*10+playerBorderWidth+MAP_START_Y,10-(2*playerBorderWidth),10-(2*playerBorderWidth));
}
var drawTroops = function(troops, h){
var gold = 0;
var food = 0;
for(var guys = 0; guys<troops.length; guys++){
//hsla(0, 100%, 83%, 1)
//hsla(237, 100%, 12%, 1)
var startL = 83;
var endL = 12;
var l = Math.max(startL-troops[guys][2], endL);
var color = "hsla("+h+",100%,"+l+"%,1)";
drawPlayer(color, troops[guys][0], troops[guys][1]);
gold += json.money_payout_rates[troops[guys][0]][troops[guys][1]];
food += 1-json.money_payout_rates[troops[guys][0]][troops[guys][1]];
}
return [gold, food];
}
var playRound = function(){
if(round>=NUM_ROUNDS){
playGame();
return;
}
console.log("round");
ctx.drawImage( canvas2, 0, 0);
ctx.fillStyle="rgba(250,250,250, 1)";
ctx.fillRect((round/json.turns.length)*CANVAS_WIDTH, 0, 2, MAP_START_Y);
//draw guys
var turn = json.turns[round];
var rates1 = drawTroops(turn.p1g, 0);
var rates2 = drawTroops(turn.p2g, 237);
var goldMax = Math.max(rates1[0],rates2[0])*2;
var foodMax = Math.max(rates1[1],rates2[1])*2;
ctx.fillStyle="rgb(255,0,0)";
var mark = MAP_START_Y-(rates1[1]* MAP_START_Y/foodMax);
ctx.fillRect(0, mark, 2, MAP_START_Y-mark);
ctx.fillStyle="rgb(0,0,255)";
mark = MAP_START_Y-(rates2[1]* MAP_START_Y/foodMax);
ctx.fillRect(2, mark, 2, MAP_START_Y-mark);
ctx.fillStyle="rgba(255, 133, 133, 1)";
mark = MAP_START_Y-(rates1[0]* MAP_START_Y/goldMax);
ctx.fillRect(4, mark, 2, MAP_START_Y-mark);
ctx.fillStyle="rgba(133, 212, 255, 1)";
mark = MAP_START_Y-(rates2[0]* MAP_START_Y/goldMax);
ctx.fillRect(6, mark, 2, MAP_START_Y-mark);
round++;
if(!paused){
setTimeout(playRound, roundSpeed)
}
};
playGame();
var runGame = function(){
json = eval("(" + $('textarea').text() + ")");
playGame();
}
$('#run').click(function(){
runGame();
});
var switchingArea = false;
$('canvas').bind('mousemove', function(e){
if(switchingArea){
var rect = ctx.canvas.getBoundingClientRect();
var x = e.clientX - rect.left;
var y = e.clientY - rect.top;
//console.log(x, y);
if(y<MAP_START_Y){
round = Math.max(0, Math.min(NUM_ROUNDS, parseInt((x/CANVAS_WIDTH)*NUM_ROUNDS)));
}
if(paused){
playRound();
}
}
return false;
});
$("#canvas").bind('mousedown', function(e){
var rect = ctx.canvas.getBoundingClientRect();
var x = e.clientX - rect.left;
var y = e.clientY - rect.top;
//console.log(x, y);
if(y<MAP_START_Y){
round = Math.max(0, Math.min(NUM_ROUNDS, parseInt((x/CANVAS_WIDTH)*NUM_ROUNDS)));
}
switchingArea = true;
if(paused){
playRound();
}
});
$("#canvas").bind('mouseup', function(e){
switchingArea = false;
});
$("#pause").bind('click', function(e){
if(paused){
paused = false;
setTimeout(playRound, roundSpeed)
}else{
paused = true;
}
});
$("#left").bind('click', function(e){
round-=2;
playRound();
});
$("#right").bind('click', function(e){
round++;
playRound();
});
$(window).bind('keydown', function(e){
if(!paused){
return;
}
e = e || window.event;
if (e.keyCode == '37') {
round-=2;
}
else if (e.keyCode == '39') {
// right arrow
round++;
}
playRound();
});
});
</script>
</head>
<body style='overflow:scroll;'>
<main>
<h1>A.I. Programming Competition</h1>
<div class='content' style='width:1000px;'>
<canvas id='canvas' width='500' height='600'></canvas>
<div>
<input id='pause' type='submit' value=' || '/>
<input id='left' type='submit' value='<-'/>
<input id='right' type='submit' value='->'/>
</div>
<div>
<h2>key</h2>
<span style='color: rgb(255,0,0)'>Number of Red Players</span><br />
<span style='color: rgb(0,0,255)'>Number of Blue Players</span><br />
<span style='color: rgba(255, 133, 133, 1)'>Number of Gold Red has</span><br />
<span style='color: rgba(133, 212, 255, 1)'>Number of Gold Blue has</span><br />
<span>* The bars in the top left indicate the rate at which players are earning the above</span><br />
<span>\/ Submit json below to run it instead</span>
</div>
<br /><br /><br />
<textarea style='width:1000px; height:400px;' class='replace'></textarea>
<input id='run' type='submit' />
<canvas style='display:none;' id='canvas2' width='500' height='600'></canvas>
</main>
</body>
</html>