1 line
5.5 KiB
JavaScript
1 line
5.5 KiB
JavaScript
var mineGui_mine=false,mineGui_curmap,mineGui_curndx,mineGui_moves="";var mineGui_movesBackup=[];var mineGui_customNdx=1;var mineGui_canvas=true;var mineGui_imgSources={sprites:"sprites.png"};var mineGui_spriteOffset={R:5,"#":7,"*":6,"\\":2,L:3,O:4,".":0," ":1,A:8,B:9,C:10,D:11,E:12,F:13,G:14,H:15,I:16,"1":17,"2":18,"3":19,"4":20,"5":21,"6":22,"7":23,"8":24,"9":25,W:26,"!":27};var mineGui_images={};var mineGui_imagesNeed=0;function mineGui_loadImg(b,c){var a;++mineGui_imagesNeed;mineGui_images[b]=a=new Image();a.onload=function(){if(0==--mineGui_imagesNeed){mineGui_show()}};a.src=c}function mineGui_start(){var b=document.getElementById("mineGui");var e=document.getElementById("mineGui_selectMap");var c=document.getElementById("mineGui_moves");var a=document.getElementById("mineGui_data");var f={L:1,U:1,R:1,D:1,A:1,W:1,S:1};var d;document.getElementById("mineGui_addData").onclick=mineGui_addCustomMap;e.onchange=function(){var l=e.options[e.selectedIndex].text;mineGui_setMap(mineMaps[l],e.selectedIndex);e.blur()};var g=function(){if(c.value==mineGui_moves){return}mineGui_moves="";var k=c.value.toUpperCase(),l;for(l=0;l<k.length;++l){if(f[k[l]]){mineGui_moves+=k[l]}}mineGui_updateMine()};var h=function(k){window.setTimeout(g,0)};c.onchange=h;c.onpaste=h;c.onkeypress=h;document.body.onkeydown=function(k){if(document.activeElement===a){return}if(k.ctrlKey||k.altKey||k.metaKey){return}var m=true;var l=String.fromCharCode(k.keyCode);if(f[l]){mineGui_move(l)}else{if(l=="C"){mineGui_moves="";mineGui_updateMine()}else{if(l=="T"){mineGui_canvas=!mineGui_canvas;console.log("canvas show",mineGui_canvas);mineGui_show()}else{switch(k.keyCode){case 8:mineGui_moves=mineGui_moves.slice(0,-1);mineGui_updateMine();break;case 33:if(e.selectedIndex>0){e.selectedIndex--;e.onchange()}break;case 34:if(e.selectedIndex+1<e.options.length){e.selectedIndex++;e.onchange()}break;case 37:if(mineGui_mine.validMove("L")){mineGui_move("L")}break;case 38:if(mineGui_mine.validMove("U")){mineGui_move("U")}break;case 39:if(mineGui_mine.validMove("R")){mineGui_move("R")}break;case 40:if(mineGui_mine.validMove("D")){mineGui_move("D")}break;default:m=false;break}}}}if(m){k.preventDefault();return true}};for(d in mineGui_imgSources){if(mineGui_imgSources.hasOwnProperty(d)){mineGui_loadImg(d,mineGui_imgSources[d])}}for(d in mineMaps){if(mineMaps.hasOwnProperty(d)){var j=document.createElement("option");j.textContent=d;e.appendChild(j)}}e.selectedIndex=0;e.onchange()}function mineGui_addCustomMap(){var d=document.getElementById("mineGui_data").value;var f;var c=document.getElementById("mineGui_selectMap");document.getElementById("mineGui_addDataResult").textContent="";try{f=new Mine(d)}catch(b){document.getElementById("mineGui_addDataResult").textContent="Adding map failed: "+b;return}mineMaps["custom"+mineGui_customNdx]=d;var a=document.createElement("option");a.textContent="custom"+mineGui_customNdx;c.appendChild(a);mineGui_customNdx++;window.setTimeout(function(){c.selectedIndex=c.options.length-1;c.onchange()},0)}function mineGui_move(a){for(i=0;i<a.length;++i){if(mineGui_mine.state!=Mine.ALIVE){break}mineGui_moves+=a[i];mineGui_mine.move(a[i])}mineGui_show()}function mineGui_setMap(b,a){mineGui_movesBackup[mineGui_curndx]=mineGui_moves;mineGui_curmap=b;mineGui_curndx=a;mineGui_moves=mineGui_movesBackup[mineGui_curndx];if(!mineGui_moves){mineGui_moves=""}mineGui_updateMine()}function mineGui_updateMine(){mineGui_mine=new Mine(mineGui_curmap);var a=mineGui_moves;mineGui_moves="";for(i=0;i<a.length;++i){if(mineGui_mine.state!=Mine.ALIVE){break}mineGui_moves+=a[i];mineGui_mine.move(a[i])}mineGui_show()}function mineGui_show(){if(!mineGui_mine){return}var c=Math.max(mineGui_mine.water_level,0);var b=mineGui_mine.toString().split(/\n/);var h=document.getElementById("mineGui_mineCanvas");var f=document.getElementById("mineGui_mineMap");if(mineGui_canvas&&0==mineGui_imagesNeed){h.style.display="";h.style.visibility="visible";f.style.display="none";f.style.visibility="hidden";h.width=16*mineGui_mine.width;h.height=16*mineGui_mine.height;var j=h.getContext("2d");j.globalAlpha=1;j.fillStyle="black";j.fillRect(0,0,h.width,h.height);var d=mineGui_images.sprites;for(var e=0;e<mineGui_mine.height;++e){for(var g=0;g<mineGui_mine.width;++g){j.drawImage(d,0,mineGui_spriteOffset[b[e][g]]*16,16,16,16*g,16*e,16,16)}}j.fillStyle="#0000FF";j.globalAlpha=0.3;e=16*(mineGui_mine.height-c);j.fillRect(0,e,h.width,h.height-e)}else{h.style.display="none";h.style.visibility="hidden";f.style.display="";f.style.visibility="visible"}if(c>0){document.getElementById("mineGui_mineMapWater").textContent=b.splice(-c).join("\n")}else{document.getElementById("mineGui_mineMapWater").textContent=""}document.getElementById("mineGui_mineMapDry").textContent=b.join("\n");document.getElementById("mineGui_moves").value=mineGui_moves;var a="";switch(mineGui_mine.state){case Mine.ALIVE:a="Still mining";break;case Mine.LOST:a="Robot broken - "+mineGui_mine.reason;break;case Mine.ABORTED:a="Aborted";break;case Mine.WON:a="Won - "+mineGui_mine.reason;break}document.getElementById("mineGui_meta").textContent=mineGui_mine.metaText();document.getElementById("mineGui_state").textContent=a;document.getElementById("mineGui_score").textContent=mineGui_mine.score;document.getElementById("mineGui_scoreMoves").textContent=mineGui_mine.moves;document.getElementById("mineGui_scoreLambdas").textContent=mineGui_mine.found_lambdas;document.getElementById("mineGui_razors").textContent=mineGui_mine.razors;document.getElementById("mineGui_belowWater").textContent=mineGui_mine.moves_below_water}; |