Code viewer for World: Battleship (clone by Thoma...

// Cloned by Cbum on 5 Dec 2022 from World "Battleship (clone by Thomas Hazekamp)" by Thomas Hazekamp 
// Please leave this clone trail here.
 
const MUSICFILE="/uploads/aaroncrawford/Sea_Shanty.mp3";MUSICFILE.volume=.2,AB.backgroundMusic(MUSICFILE);const MissSounds="/uploads/aaroncrawford/MissSound.mp3";var MissSound=new Audio(MissSounds);const HitSounds="/uploads/aaroncrawford/HitSound.mp3";var HitSound=new Audio(HitSounds);ABWorld.drawCameraControls=!1,AB.drawRunControls=!1,AB.maxSteps=1e4,AB.clockTick=100;const skycolor="lightyellow",boxcolor="/uploads/hazekat2/water.jpg",targetbox="/uploads/aaroncrawford/target_tile.png",hitbox="/uploads/aaroncrawford/tile2.png",missbox="/uploads/aaroncrawford/tile3.png",LIGHTCOLOR=16777215,SKYCOLOR=39219,red=39219,gridsize=8,squaresize=400;var positioning1=[],positioning2=[];const MAXPOS=gridsize*squaresize,skyboxConst=3*MAXPOS;ABHandler.GROUNDZERO=!0;const maxRadiusConst=10*MAXPOS,startRadiusConst=.8*MAXPOS,startRadius=5500,maxRadius=500*startRadius;var tile_texture,b1z,b1x,myturn,GRID1=new Array(gridsize),GRID2=new Array(gridsize),keep=[0,7],p1score=0,p2score=0,alreadyHit=[],alreadyMissed=[];function initSkybox(){var e=[new THREE.MeshBasicMaterial({map:THREE.ImageUtils.loadTexture("/uploads/seanhutchinson/skyrender0001.bmp"),side:THREE.BackSide}),new THREE.MeshBasicMaterial({map:THREE.ImageUtils.loadTexture("/uploads/seanhutchinson/skyrender0004.bmp"),side:THREE.BackSide}),new THREE.MeshBasicMaterial({map:THREE.ImageUtils.loadTexture("/uploads/seanhutchinson/skyrender0003.bmp"),side:THREE.BackSide}),new THREE.MeshBasicMaterial({map:THREE.ImageUtils.loadTexture("/uploads/seanhutchinson/skyrender0006.bmp"),side:THREE.BackSide}),new THREE.MeshBasicMaterial({map:THREE.ImageUtils.loadTexture("/uploads/seanhutchinson/skyrender0005.bmp"),side:THREE.BackSide}),new THREE.MeshBasicMaterial({map:THREE.ImageUtils.loadTexture("/uploads/seanhutchinson/skyrender0002.bmp"),side:THREE.BackSide})],t=new THREE.BoxGeometry(skyboxConst,skyboxConst,skyboxConst),a=new THREE.MeshFaceMaterial(e),o=new THREE.Mesh(t,a);threeworld.scene.add(o)}function initScene(){new THREE.Color;ABWorld.init3d(startRadius,maxRadius,skycolor);var e=new THREE.LoadingManager,t=new THREE.OBJLoader(e);t.load("/uploads/aaroncrawford/fishing-boat.obj",buildboat1),t.load("/uploads/aaroncrawford/fishing-boat.obj",buildboat2),t.load("/uploads/aaroncrawford/fishing-boat.obj",buildboat3),t.load("/uploads/aaroncrawford/fishing-boat.obj",buildboat4),(new THREE.TextureLoader).load(boxcolor,function(e){e.minFilter=THREE.LinearFilter,tile_texture=e,asynchFinished()&&GridMaker()}),(new THREE.TextureLoader).load(targetbox,function(e){e.minFilter=THREE.LinearFilter,target_texture=e,asynchFinished()&&TargetMaker(0,7)});var a=new THREE.AmbientLight;ABWorld.scene.add(a)}function Team1(){var e=[positioning1,p1score,!1];AB.socket&&AB.socket.connected&&AB.socketOut(e)}function Team2(){var e=[positioning1,p1score,!0];AB.socket&&AB.socket.connected&&AB.socketOut(e)}function CheckHit(e,t){testIfAlreadyHit=!1,testIfBoatHit=!1;for(let t=0;t<alreadyHit.length;t++)e[0]==alreadyHit[t][0]&&e[1]==alreadyHit[t][1]&&(testIfAlreadyHit=!0);testIfAlreadyHit||(e[0]==t[0][0]&&(e[1]!=t[0][1]-1&&e[1]!=t[0][1]+1&&e[1]!=t[0][1]||(alreadyHit.push(e),HitConfirm(e[0],e[1]),testIfBoatHit=!0,console.log("HIT boat 1"),p1score+=1,HitSound.play(),myturn=!1)),e[0]==t[1][0]&&(e[1]!=t[1][1]-1&&e[1]!=t[1][1]+1&&e[1]!=t[1][1]||(alreadyHit.push(e),HitConfirm(e[0],e[1]),testIfBoatHit=!0,console.log("HIT boat 2"),p1score+=1,HitSound.play(),myturn=!1)),e[1]==t[2][1]&&(e[0]!=t[2][0]-1&&e[0]!=t[2][0]+1&&e[0]!=t[2][0]||(alreadyHit.push(e),HitConfirm(e[0],e[1]),testIfBoatHit=!0,console.log("HIT boat 3"),p1score+=1,HitSound.play(),myturn=!1)),e[1]==t[3][1]&&(e[0]!=t[3][0]-1&&e[0]!=t[3][0]+1&&e[0]!=t[3][0]||(alreadyHit.push(e),HitConfirm(e[0],e[1]),testIfBoatHit=!0,console.log("HIT boat 4"),p1score+=1,HitSound.play(),myturn=!1)),testIfBoatHit||(alreadyMissed.push(e),MissedTarget(e[0],e[1]),MissSound.play(),myturn=!1))}function MissedTarget(e,t){(new THREE.TextureLoader).load(missbox,function(a){a.minFilter=THREE.LinearFilter,miss_texture=a,asynchFinished()&&(shape=new THREE.BoxGeometry(squaresize,squaresize,squaresize),thecube=new THREE.Mesh(shape),thecube.material=new THREE.MeshBasicMaterial({map:miss_texture}),thecube.position.copy(translate2(e,t)),ABWorld.scene.add(thecube))})}function ReplaceTarget(e,t){testIfAlreadyMissed=!1;for(let a=0;a<alreadyMissed.length;a++)e==alreadyMissed[a][0]&&t==alreadyMissed[a][1]&&(testIfAlreadyMissed=!0);testIfAlreadyHit=!1;for(let a=0;a<alreadyHit.length;a++)e==alreadyHit[a][0]&&t==alreadyHit[a][1]&&(testIfAlreadyHit=!0);testIfAlreadyHit||testIfAlreadyMissed?testIfAlreadyHit&&!testIfAlreadyMissed?(shape=new THREE.BoxGeometry(squaresize,squaresize,squaresize),thecube=new THREE.Mesh(shape),thecube.material=new THREE.MeshBasicMaterial({map:hit_texture}),thecube.position.copy(translate2(e,t)),ABWorld.scene.add(thecube)):(shape=new THREE.BoxGeometry(squaresize,squaresize,squaresize),thecube=new THREE.Mesh(shape),thecube.material=new THREE.MeshBasicMaterial({map:miss_texture}),thecube.position.copy(translate2(e,t)),ABWorld.scene.add(thecube)):(shape=new THREE.BoxGeometry(squaresize,squaresize,squaresize),thecube=new THREE.Mesh(shape),thecube.material=new THREE.MeshBasicMaterial({map:tile_texture}),thecube.position.copy(translate2(e,t)),ABWorld.scene.add(thecube))}function TargetMaker(e,t){return shape=new THREE.BoxGeometry(squaresize,squaresize,squaresize),thecube=new THREE.Mesh(shape),thecube.material=new THREE.MeshBasicMaterial({map:target_texture}),thecube.position.copy(translate2(e,t)),ABWorld.scene.add(thecube),[e,t]}function HitConfirm(e,t){(new THREE.TextureLoader).load(hitbox,function(a){a.minFilter=THREE.LinearFilter,hit_texture=a,asynchFinished()&&(shape=new THREE.BoxGeometry(squaresize,squaresize,squaresize),thecube=new THREE.Mesh(shape),thecube.material=new THREE.MeshBasicMaterial({map:hit_texture}),thecube.position.copy(translate2(e,t)),ABWorld.scene.add(thecube))})}function GridMaker(){for(i=0;i<gridsize;i++)for(j=0;j<gridsize;j++)(i<=gridsize-1||j<=gridsize-1)&&(shape=new THREE.BoxGeometry(squaresize,squaresize,squaresize),thecube=new THREE.Mesh(shape),thecube.material=new THREE.MeshBasicMaterial({map:tile_texture}),thecube.position.copy(translate1(i,j)),ABWorld.scene.add(thecube));for(i=0;i<gridsize;i++)for(j=0;j<gridsize;j++)(i<=gridsize-1||j<=gridsize-1)&&(shape=new THREE.BoxGeometry(squaresize,squaresize,squaresize),thecube=new THREE.Mesh(shape),thecube.material=new THREE.MeshBasicMaterial({map:tile_texture}),thecube.position.copy(translate2(i,j)),ABWorld.scene.add(thecube))}function buildboat1(e){e.scale.multiplyScalar(100),e.traverse(paintBoat),boat1=e,threeworld.scene.add(boat1),positioning1.push(drawBoat1(boat1))}function buildboat2(e){e.scale.multiplyScalar(100),e.traverse(paintBoat),boat2=e,threeworld.scene.add(boat2),positioning1.push(drawBoat2(boat2))}function buildboat3(e){e.scale.multiplyScalar(100),e.traverse(paintBoat),boat3=e,boat3.rotateY(1.6),threeworld.scene.add(boat3),positioning1.push(drawBoat3(boat3))}function buildboat4(e){e.scale.multiplyScalar(100),e.traverse(paintBoat),boat4=e,boat4.rotateY(1.6),threeworld.scene.add(boat4),positioning1.push(drawBoat4(boat4))}function paintBoat(e){e instanceof THREE.Mesh&&(e.material.map=THREE.ImageUtils.loadTexture("/uploads/aaroncrawford/wood.jpg"))}function drawBoat1(){b1j=getRandomPositionVerticleZ(),b1i=getRandomPositionVerticleX();var e=translateBoats(b1i*squaresize),t=translateBoats(b1j*squaresize),a=1.2*squaresize;return boat1.position.x=e,boat1.position.y=a,boat1.position.z=t,b1j=5-b1j,[b1i,b1j]}function drawBoat2(){if(b1i=getRandomPositionVerticleX(),b1j=getRandomPositionVerticleZ(),b1i==positioning1[0][0])for(;5-b1j>=positioning1[0][1]-2&&5-b1j<=positioning1[0][1]+2;)b1j=getRandomPositionVerticleZ();var e=translateBoats(b1i*squaresize),t=translateBoats(b1j*squaresize),a=1.2*squaresize;return boat2.position.x=e,boat2.position.y=a,boat2.position.z=t,b1j=5-b1j,[b1i,b1j]}function drawBoat3(){if(b1j=getRandomPositionHorizontalZ(),b1i=getRandomPositionHorizontalX(),b1i>=positioning1[0][0]-1&&b1i<=positioning1[0][0]+1||b1i>=positioning1[1][0]-1&&b1i<=positioning1[1][0]+1)for(;5-b1j>=positioning1[0][1]-1&&5-b1j<=positioning1[0][1]+1||5-b1j>=positioning1[1][1]-1&&5-b1j<=positioning1[1][1]+1;)b1j=getRandomPositionHorizontalZ();var e=translateBoats(b1i*squaresize),t=translateBoats(b1j*squaresize),a=1.2*squaresize;return boat3.position.x=e,boat3.position.y=a,boat3.position.z=t,b1j=5-b1j,[b1i,b1j]}function drawBoat4(){if(b1j=getRandomPositionHorizontalZ(),b1i=getRandomPositionHorizontalX(),b1i>=positioning1[0][0]-1&&b1i<=positioning1[0][0]+1||b1i>=positioning1[1][0]-1&&b1i<=positioning1[1][0]+1||b1i>=positioning1[2][0]-2&&b1i<=positioning1[2][0]+2)for(;5-b1j>=positioning1[0][1]-1&&5-b1j<=positioning1[0][1]+1||5-b1j>=positioning1[1][1]-1&&5-b1j<=positioning1[1][1]+1||5-b1j==positioning1[2][1];)b1j=getRandomPositionHorizontalZ();var e=translateBoats(b1i*squaresize),t=translateBoats(b1j*squaresize),a=1.2*squaresize;return boat4.position.x=e,boat4.position.y=a,boat4.position.z=t,b1j=5-b1j,[b1i,b1j]}function getRandomPositionVerticleZ(){return min=Math.ceil(-1),max=Math.floor(4),Math.floor(Math.random()*(max-min+1)+min)}function getRandomPositionVerticleX(){return min=Math.ceil(0),max=Math.floor(7),Math.floor(Math.random()*(max-min+1)+min)}function getRandomPositionHorizontalZ(){return min=Math.ceil(-2),max=Math.floor(5),Math.floor(Math.random()*(max-min+1)+min)}function getRandomPositionHorizontalX(){return min=Math.ceil(1),max=Math.floor(6),Math.floor(Math.random()*(max-min+1)+min)}function asynchFinished(){return!!tile_texture}function translate1(e,t){var a=new THREE.Vector3;return t-=2,a.y=0,a.x=e*squaresize-MAXPOS/2,a.z=t*squaresize-MAXPOS/2,a}function translate2(e,t){var a=new THREE.Vector3;return a.y=t*squaresize-MAXPOS/2+2e3,a.x=e*squaresize-MAXPOS/2,a.z=-4e3,a}function translateBoats(e){return e-MAXPOS/2}function splashScreenStartMenu(){return"<h1>Rules:</h1>\n    <ul>\n        <li>Both players must select a team before they can begin the game</li>\n        <li>Players will take turns</li>\n        <li>First player to get 12 hits wins the game</li>\n    </ul>\n    NOTE: Both players need to select start before choosing a team!<br>"}function splashScreenEndMenu(){return 12==p1score?"<h1 style='text-align: center'>WINNER WINNER CHICKEN DINNER!</h1> <h3 style='text-align: center'>The game is over</h3>":"<h1 style='text-align: center'>Mission Failed We'll Get'em Next Time!</h1> <h3 style='text-align: center'>The game is over</h3>"}function fullMatch(){var e;return e="<h1>The game is currently full, please try again later</h1>",e+="<button onclick='refreshButton();'  class=ab-largenormbutton > Try again </button>"}function refreshButton(){location.reload()}AB.newSplash(splashScreenStartMenu()),AB.world.newRun=function(){AB.socketStart(),BOXHEIGHT=squaresize,threeworld.init3d(startRadiusConst,maxRadiusConst,39219);var e=new THREE.DirectionalLight(16777215,3);threeworld.scene.add(e),initSkybox(),initScene(),AB.runReady=!1},AB.world.nextStep=function(){whosTurn=myturn?'<span style="color:green"> It is your turn!</span>':'<span style="color:red">It is the other players turn!</span>',AB.msg('<h3> Please wait until the other player has selected start before choosing your team!</h3><p>You may have to double/spam click when selcting your team</p><button onclick=Team1();  class=ab-largenormbutton > Team1 </button> <button onclick=Team2();  class=ab-largenormbutton > Team2 </button> <h2><h2>You have <span style="color:green">'+p1score+' hit(s) </span> on their boat(s)<br>They have <span style="color:red">'+p2score+" hit(s) </span> on your boat(s)</h2><h3><u>"+whosTurn+"</u></h3><h1>Controls:</h1>\n    <ol>\n        <li>Use arrow keys to move around the attack board (when it is your go)</li>\n        <li>Press enter to hit the current position</li>\n        <li>Use mouse 1 (left click) to move the camera angle</li>\n    </ol>");12!=p1score&&12!=p2score||(AB.abortRun=!0),document.onkeydown=function(e){if(e=e||window.event,myturn)if("38"==e.keyCode)keep[1]<7&&(ReplaceTarget(keep[0],keep[1]),keep=TargetMaker(keep[0],keep[1]+1));else if("40"==e.keyCode)keep[1]>0&&(ReplaceTarget(keep[0],keep[1]),keep=TargetMaker(keep[0],keep[1]-1));else if("37"==e.keyCode)keep[0]>0&&(ReplaceTarget(keep[0],keep[1]),keep=TargetMaker(keep[0]-1,keep[1]));else if("39"==e.keyCode)keep[0]<7&&(ReplaceTarget(keep[0],keep[1]),keep=TargetMaker(keep[0]+1,keep[1]));else if("13"==e.keyCode&&(temp=myturn,CheckHit(keep,positioning2),!myturn)){var t=[positioning1,p1score,temp];AB.socket&&AB.socket.connected&&AB.socketOut(t)}}},AB.world.endRun=function(){AB.newSplash(splashScreenEndMenu());setTimeout(function(){location.reload()},1e4)},AB.splashClick(function(){AB.runReady=!0,AB.removeSplash()}),AB.socketIn=function(e){positioning2=e[0],p2score=e[1],myturn=e[2]},AB.socketUserlist=function(e){console.log(e.length),console.log(e),e.length>2&&AB.splashHtml(fullMatch())};