var canvas; const WAITING = 0; const RUNNING = 1; const REVIEWING = 2; const TESTING = 3; const FINISH = 4; const UP = 0; const RIGHT = 1; const DOWN = 2; const LEFT = 3; const RED = "R"; const BLUE = "B"; const GREEN = "G"; const YELLOW = "Y"; const PURPLE = "P"; const PINK = "PI"; const BLACK = "BL"; const GRAY = "GR"; const ALIVE = 0; const DEAD = 1; const DONE = 2; var BOT_X = -1 var BOT_Y = -1 var BOT_DIR = RIGHT var BOT_STATE = ALIVE var BOT_TAPE = "RB" var BOT_QUEUE = [] var BELTDOWN = {type:"BELT",direction:DOWN/*,direction2 - defines a secondary direction. For two belts at once.*/} var BELTRIGHT = {type:"BELT",direction:RIGHT} var BELTUP = {type:"BELT",direction:UP} var BELTLEFT = {type:"BELT",direction:LEFT} var BRANCHDOWN = {type:"BRANCH",direction:DOWN,color1:RED,color2:BLUE} //color 1 points clockwise(right), color 2 points counter-clockwise(left) var BRANCHLEFT = {type:"BRANCH",direction:LEFT,color1:RED,color2:BLUE} var BRANCHRIGHT = {type:"BRANCH",direction:RIGHT,color1:RED,color2:BLUE} var BRANCHUP = {type:"BRANCH",direction:UP,color1:RED,color2:BLUE} var WRITERDOWN = {type:"WRITER",direction:DOWN,color:RED/*overwrite - if turned on, the writer overwrites the current tape position instead of appending.*/} var WRITERLEFT = {type:"WRITER",direction:LEFT,color:RED} var WRITERRIGHT = {type:"WRITER",direction:RIGHT,color:RED} var WRITERUP = {type:"WRITER",direction:UP,color:RED} var DEF_BRANCHUP_RB = {img:IMAGE_BRANCH,color1:RED,color2:BLUE,type:"BRANCH"} var DEF_BRANCHUP_BR = {img:IMAGE_BRANCH,color1:BLUE,color2:RED,type:"BRANCH"} var DEF_BRANCHUP_GY = {img:IMAGE_BRANCH,color1:GREEN,color2:YELLOW,type:"BRANCH"} var DEF_BRANCHUP_YG = {img:IMAGE_BRANCH,color1:YELLOW,color2:GREEN,type:"BRANCH"} var DEF_BRANCHUP_PPI = {img:IMAGE_BRANCH,color1:PURPLE,color2:PINK,type:"BRANCH"} var DEF_BRANCHUP_PIP = {img:IMAGE_BRANCH,color1:PINK,color2:PURPLE,type:"BRANCH"} var DEF_BRANCHUP_BLGR = {img:IMAGE_BRANCH,color1:BLACK,color2:GRAY,type:"BRANCH"} var DEF_BRANCHUP_GRBL = {img:IMAGE_BRANCH,color1:GRAY,color2:BLACK,type:"BRANCH"} var DEF_WRITERRIGHT_R = {img:IMAGE_WRITER,color1:RED,type:"WRITER"} var DEF_WRITERRIGHT_B = {img:IMAGE_WRITER,color1:BLUE,type:"WRITER"} var DEF_WRITERRIGHT_G = {img:IMAGE_WRITER,color1:GREEN,type:"WRITER"} var DEF_WRITERRIGHT_Y = {img:IMAGE_WRITER,color1:YELLOW,type:"WRITER"} var DEF_WRITERRIGHT_P = {img:IMAGE_WRITER,color1:PURPLE,type:"WRITER"} var DEF_WRITERRIGHT_PI = {img:IMAGE_WRITER,color1:PINK,type:"WRITER"} var DEF_WRITERRIGHT_BL = {img:IMAGE_WRITER,color1:BLACK,type:"WRITER"} var DEF_WRITERRIGHT_GR = {img:IMAGE_WRITER,color1:GRAY,type:"WRITER"} var DEF_CONVEYOR = {img:IMAGE_CONVEYOR,type:"BELT"} var GRID_W = 32 var GRID_H = 32 var GRID_X = 20 var GRID_Y = 20 var LAST_MOUSE_X=0; var LAST_MOUSE_Y=0; var ITEM_DIRECTION=RIGHT; var SUBMENU = { visible:false, width:0, height:0, buttons:[] } var BUTTON_SELECTED = undefined var ITEM_SELECTED = undefined var CONVEYOR_BUILD_BUTTON = {img:IMAGE_CONVEYOR,x:-1,y:-1,w:-1,h:-1,lastselected:DEF_CONVEYOR} var BRANCH_BUILD_BUTTON = {img:IMAGE_BRANCH,x:-1,y:-1,w:-1,h:-1,submenu_buttons:[DEF_BRANCHUP_RB,DEF_BRANCHUP_BR,DEF_BRANCHUP_GY,DEF_BRANCHUP_YG,DEF_BRANCHUP_PPI,DEF_BRANCHUP_PIP,DEF_BRANCHUP_BLGR,DEF_BRANCHUP_GRBL],lastselected:undefined} var WRITER_BUILD_BUTTON = {img:IMAGE_WRITER,x:-1,y:-1,w:-1,h:-1,submenu_buttons:[DEF_WRITERRIGHT_R,DEF_WRITERRIGHT_B,DEF_WRITERRIGHT_G,DEF_WRITERRIGHT_Y,DEF_WRITERRIGHT_P,DEF_WRITERRIGHT_PI,DEF_WRITERRIGHT_BL,DEF_WRITERRIGHT_GR],lastselected:undefined} var MENU = { visible:true, buttons:[CONVEYOR_BUILD_BUTTON,BRANCH_BUILD_BUTTON,WRITER_BUILD_BUTTON] } var lastGameUpdate = 0; var gameSpeed = 1000/1; var gameState=RUNNING; var gameStage=0; var LEVEL0 = [ [{},{},{},{},{},], [{},{},{},{},{},], [{},{},{},{},{},], [{},{},{},{},{},], [{},{},{},{},{},],] var LEVEL1 = [ [{},{},{...BELTDOWN},{...BELTLEFT},{...BELTLEFT},], [{},{},{},{},{...BELTUP},], [{},{...BELTDOWN},{},{},{...BELTUP},], [{},{...BELTRIGHT},{...BELTRIGHT},{...BELTRIGHT},{...BELTUP},], [{},{},{},{},{},],] var LEVEL2 = [ [{},{},{},{},{},], [{},{...BELTRIGHT},{},{},{},], //BLUE [{},{...BRANCHRIGHT},{},{},{},], [{},{...BELTRIGHT},{},{},{},], //RED [{},{},{},{},{},],] var LEVEL3 = [ [{},{},{},{},{},], [{},{...WRITERUP,overwrite:true},{},{},{},], [{},{...WRITERDOWN},{},{},{},], [{},{...BELTRIGHT},{},{},{},], [{},{},{},{},{},],] var LEVEL4 = [ [{},{},{},{},{},], [{},{},{},{},{},], [{},{...BELTRIGHT,direction2:DOWN},{},{},{},], [{},{},{...BELTUP,direction2:LEFT},{},{},], [{},{},{},{},{},],] var STAGE1 = { name:"The First Stage!", objective:"Accept all bots", level:createGrid(5,5,4,2), start:{x:0,y:2}, accept:(tape)=>true} var STAGE2 = { name:"Blue Blue", objective:"Accept only Blue Bots", level:createGrid(5,5,4,2), start:{x:0,y:2}, accept:(tape)=>{ for (var i=0;i0) { var mask = remainingVal&1 if (mask===1) { tape="B"+tape } else { tape="R"+tape } remainingVal=remainingVal>>>1 } return tape; } function runBot(testing) { //console.log(new Date().getTime()) if (lastGameUpdate=button.x && getMousePos(e).x<=button.x+button.w && getMousePos(e).y>=button.y && getMousePos(e).y<=button.y+button.h) } function clickEvent(e) { //console.log(MENU.buttons) if (MENU.visible) { for (var button of MENU.buttons) { if (mouseOverButton(canvas,e,button)) { if (button.submenu_buttons&&button.submenu_buttons.length>0) { BUTTON_SELECTED=button; SUBMENU.visible=true; SUBMENU.buttons=[] var index = 0; for (var button2 of BUTTON_SELECTED.submenu_buttons) { var buttonX = ((index%3)*48)+16 var buttonY = canvas.height*0.8-(Math.floor(index/3)*48)-40 SUBMENU.buttons.push({def:button2,img:button2.img,x:buttonX,y:buttonY,w:32,h:32}) index++; } } ITEM_SELECTED=button.lastselected //console.log(button) return } } } //console.log(getGridCoords(getMousePos(e))) if (ITEM_SELECTED!==undefined) { var clickCoords = getGridCoords(getMousePos(e)) if (clickCoords.x>=0&&clickCoords.y>=0&&clickCoords.y=buttonX&&LAST_MOUSE_X<=buttonX+32&&LAST_MOUSE_Y>=buttonY&&LAST_MOUSE_Y<=buttonY+32)?"#555555":"#b5b5b5") index++; } } } function RenderMenu(ctx) { if (MENU.visible) { ctx.fillStyle="#20424a" ctx.fillRect(0,canvas.height*0.8,canvas.width,canvas.height*0.2) var buttonX = 16 var buttonY = canvas.height*0.8+16 for (var button of MENU.buttons) { if (button.lastselected) { RenderIcon(buttonX,buttonY,ctx,button.lastselected,0,"#b5b5b5") } else { AddButton(button.img,buttonX,buttonY,ctx,button,0) } button.x=buttonX button.y=buttonY button.w=32 button.h=32 buttonX+=48 } } } function AddButton(img,x,y,ctx,button,rot=0) { ctx.fillStyle="#b5b5b5" ctx.fillRect(x,y,32,32) drawImage(x+16,y+16,img,ctx,rot) } function ConsumeTape() { BOT_TAPE=BOT_TAPE.substring(1) } function AppendTape(col) { BOT_TAPE+=col } function OverwriteTape(col) { BOT_TAPE=col+BOT_TAPE.substring(1) } function LeftOf(dir) { return (dir+1)%4 } function RightOf(dir) { if (dir===0) {dir=4} return (dir-1)%4 }