Choose default options and press start, or set the amount of players, width and size, press start. Click to drag map, tile info is shown on hover. Reset a search by pressing x.
xxxxxxxxxx
//path_array//wall order = []
var reset_toggle = false;
var grid = [];
var grids = [];
var total_players = 20;
var bank = false;
var x_padding = -0.1;
var x_Padding;
var rand = [];
var yoffset = 0;
var y_padding = 0;
var y_Padding;
var pathStart;
var mousedown = false;
var mousecounter = 340;
var menuxy = [805,50];
var xmap = 0;
var ymap = 0;
var offset = 0;
var offsetx;
var xx = 0;
var yy = 0;
var wallbackup = [];
var gridbackup = [];
var primewall = [];
var len = 40;
var a = 0;
var ylen = 33;
var b = 1;
var co = [];
var width = 1100;
var pathfinder = [];
var pathbackup = [];
var pathhist = []
var pathreset;
var nodesindex = [];
var nodestar = [];
var backupstar = [];
var starhist = [];
var key_Node = [];
var key_Node2 = [];
var nodestack = [];
var nodehist = [];
var stackstar = [];
var cluster = [];
var trackbuilder = [];
var trackstack = [];
var trackbackup = [];
var resetNodes;
var stack2 = [];
var buttons = [];
var sliders = [];
// buttons and menus
var extract_circuit;
var back;
var set_target;
var set_pathfinder;
var slidersminimize;
var slidersdrag;
var cluster;
var toggles;
var turn;
var info;
var switches;
var completed;
var game;
var start;
var paths;
var searching;
var return_path;
var returned;
var menu;
var renode;
var menudrag;
var target;
var targetbackup;
var pathfind;
var pathfind2;
var findnodes;
var load;
var minimize;
var sliderPlayers;
var slidersMenu;
var length;
var height;
var v = width/(len);
var u = width -v/5;
var w ;
var toggle = 0;
var end = false;
var moves = 0;
var sides = 6;
var theta = 360/sides;
var nodes = [];
var current = [];
var stack = [];
var hist = [];
var start_array = [];
var start_backup = [];
var scores = [];
var plants = [];
var rocks = [];
var animals = [];
var ratio;
//important variables
var grid_Cleared = false;
var grid_count = 0;
var count = 0;
var nodes_counted = false;
var nodes_count = 0;
var nodes_connected = false;
var nodescreated = 0;
var calculate_distance = false;
var neighbours_connected = false;
var nodes_array_Created = false;
var paths_explored = false;
var paths_evaluated = 0;
var paths_completed = false;
var find_node_count = 0;
var paths_count = 0;
var total_paths = 0;
var node_check = 0;
var node_check_complete = false;
var target_set;
var pathfinder_set;
var cluster_complete;
var cluster_total = 0;
var cluster_count = 0;
var total_cluster = 0;
var track_count = 0;
function setup(){
createCanvas(1100,660);
angleMode(DEGREES);
function createGrid(){
w = width/(len);
ratio = w/2 * sin(theta);
var wx = 1100/(ratio*2);
var ww = wx/len/2;
var ku = 660/ylen;
var kuu = ku/ylen/2;
for (var j=0;j<ylen;j++){
for(var i=0;i<len;i++){
var k = pow(-1,j);
var x = (ratio*1.5)+(ratio*2-ww+x_padding)*i+ratio/2*k;
var y = (w/2)+(ku-kuu-y_padding)*j;
var id = i+j*(len);
grid.push(new Cell(x,y,id,j,i));
gridbackup.push(new Cell(x,y,id,j,i));
}}
for(var i=0;i<grid.length;i++){
grid[i].init();
}
};
function createPlayers(a){
var c = total_players;
if (a.length!==total_players) {
for (var i=0;i<a;i++){
var start = round(random(0,grid.length-1));
var x = round(random(0,len-1));
var y = round(random(0,ylen-1))
if(y===9&&(x>4&&x<15)){
y=y-1;
}
var yy = y;
var b = current.includes(grid[x+yy*len],0);
if(!b){
current.push(grid[x+yy*len]);
clusters.push([]);
start_array.push(grid[x+yy*len]);
}
//trackbuilder[i] = [];
//trackbackup[i] = [];
//trackstack[i] = [];
stack[i]=[];
hist[i]=[];
scores[i]=[];
}}
for(var i=0;i<current.length;i++){
var node_info = round(random(0,255));
var c2 = round(random(0,255));
var c3 = round(random(0,255));
var col = color(node_info,c2,c3);
current[i].pcol = col;
current[i].currentid = i;
start_array[i].pcol = col;
}
};
function initialize(a){
createGrid();
createPlayers(a);
};
function home(x,y,w){
for(var i=0;i<w;i++){
var a = grid[x+i+y*len];
a.visited = true;
a.id = 0.01;
for(var j=0;j<a.walls.length;j++){
}
}
};
function pathfinders(a,target_tile,pathtile){
for(var i=0;i<grid.length;i++){
grid[i].depth = 0;
}
if(!target_tile){
var r = round(random(0,grid.length-1));
target = grid[r];
targetbackup = grid[r];
}
completed = false;
if(!pathtile){
var r = round(random(0,grid.length-1));
for( var i=0;i< a;i++){
pathfinder.push(grid[r]);
pathbackup.push(grid[r]);
//pathbackup[i]
stack2[i] = [];
pathhist[i] = [];
}}
for( var i=0;i< pathfinder.length;i++){
var c1 = round(random(0,255));
var c2 = round(random(0,255));
var c3 = round(random(0,255));
var col = color(c1,c2,c3);
//pathfinder[i].visited = true;
pathfinder[i].pcol2 = col;
pathfinder[i].depth = 0;
//pathfinder[i].walls = [false,false,false,false,false,false];
}
}
function Nodes(array,bool){
// trackbuilder = [];
// trackstack = [];
for(var i=0;i<array.length;i++){
array[i].pathconnected = false;
array[i].node=undefined;
}
for(var i=0;i<array.length;i++){
if(array[i].total!==2){
if(!bool){
nodestar.push(grid[i]);
stackstar.push([]) ;
backupstar.push(grid[i]);
starhist.push([]);
}
nodesindex.push(grid[i]);
key_Node.push(grid[i]);
nodehist.push([]);
nodestack.push([]);
nodescreated ++;
}}
if(bool){
for(var i=0;i<bool;i++){
nodestar.push(nodesindex[i]);
stackstar.push([]) ;
backupstar.push(nodesindex[i]);
starhist.push([]);
}}
for(var i=0;i<nodesindex.length;i++){
nodesindex[i].node_to_node_dist[nodesindex[i].original_id] = 0;
nodesindex[i].path_ref = {};
nodesindex[i].path_ref[nodesindex[i].original_id] = [nodesindex[i]];
nodesindex[i].best_paths = [];
nodesindex[i].temp_paths = [];
}
};
function randomizeMap(){
for(var i=0;i<grid.length-1;i++){
var r = round(random(0,grid.length-1));
}
};
function resetSketch(){
paths_explored = false;
nodescreated = 0;
neighbours_connected = false;
grid_Cleared = false;
grid_count = 0;
nodes_counted = false;
nodes_count = 0;
nodes_connected = false;
calculate_distance = false;
count = 0;
paths_completed = false;
nodes_array_Created = false;
paths_evaluated = 0;
nodes_count = 0;
find_node_count = 0;
paths_count = 0;
total_paths = 0;
node_check = 0;
node_check_complete = false;
cluster_count = 0;
cluster_complete = false;
total_cluster = 0;
cluster_total = 0;
xmap = 0;
ymap = 0;
toggles = 0;
turn = 0;
current = [];
grid = [];
hist = [];
stack = [];
start_array = [];
nodesindex = [];
stackstar = [];
nodestar = [];
backupstar = [];
starhist = [];
key_Node = [];
key_Node2 = [];
nodestack = [];
nodehist = [];
stack2 = [];
pathfinder = [];
pathbackup = [];
pathhist = [];
trackbuilder = [];
clusters = [];
trackbackup = [];
target;
completed = false;
createGrid();
createPlayers(total_players);
if(bank){
//home(5,9,10);
}
pathfinders(1)
};
function resetPath(a,target_tile,path_tile){
if(target_set||resetPath.toggle===1){
target.target = undefined;
target.visited2 = undefined;
target.pcol2 = null;
target.completed = undefined;
}
if(pathfinder_set||resetPath.toggle===1){
pathbackup[0].visited2 = undefined;
pathbackup[0].pcol2 = null;
pathbackup[0].completed = undefined;
}
return_path = null;
returned = null;
completed = false;
stack2 = [];
pathfinder = [];
pathfinder2 = [];
pathbackup = [];
pathhist = [];
//target = targetbackup;
target ;
grid = gridbackup;
pathfinders(a,target_tile,path_tile);
for(var i=0;i<grid.length;i++){
grid[i].visited2 = undefined;
grid[i].verified = undefined;
grid[i].visited3 = undefined;
grid[i].depth = 0;
grid[i].completed = undefined;
//grid[i].pcol2 = grid[i].pcol;
}
};
function noded(){
if(calculate_distance&&!nodes_array_Created){
nodesindex = [];
key_Node = []
key_Node2 = [];
nodehist = [];
nodestack = [];
Nodes(grid);
nodes_array_Created = true;
}
};
function re_node(){
if(renode.toggle===1&&!return_path){
target.target = false;
pathfinder = [target];
pathStart = pathfinder[0];
pathbackup[1] = pathfinder[0];
stack2 = []
stack2[0] = [];
target = pathbackup[0];
//pathbackup[0] = [target];
}
return_path = true;
renode.toggle = 0;
};
resetSketch();
function amend(){
// total_players = sliderPlayers.value;
if(sliderPlayers.value&&sliderPlayers.toggle===1){
total_players = sliderPlayers.value;
}
if(height.value&&height.toggle===1){
ylen = height.value;
}
if(length.value&&length.toggle===1){
len = length.value;
}
//grid = [];
resetSketch();
}
textSize(12);
//randomizeMap();
menu = new button({
x:menuxy[0]+xx,
y:menuxy[1]+yy-10,
width:190+12,
height:30*10,
id:"menu",
col:color(255,255,255,20),
label:"",
onClick: function(){
setups();
}
});
menudrag = new button({
x:menuxy[0]+xx,
y:menuxy[1]+yy-10,
width:190,
height:10,
col:color(255,255,255,20),
label:"",
onClick: function(){
//setups();
},
id : 1
});
game = new button({
x:menuxy[0]+xx,
y:menuxy[1]+30*0+yy,
width:190,
height:25,
col:color(255,255,255,20),
label:"Map",
onClick: function(){
setups();
}
});
start = new button({
x:menuxy[0]+xx,
y:menuxy[1]+30*1+yy,
width:190,
height:25,
col:color(255,255,255,20),
label:"Start",
onClick: function(){
turns();
}
});
paths = new button({
x:menuxy[0]+xx,
y:menuxy[1]+30*2+yy,
width:95,
height:25,
col:color(255,255,255,20),
label:"Paths",
onClick: function(){
cellNeighbours();
}
});
switches = new button({
x:menuxy[0]+95+xx,
y:menuxy[1]+30*2+yy,
width:95,
height:25,
col:color(255,255,255,20),
label:"Walls On/Off",
onClick: function(){
//setups();
}
});
pathreset = new button({
x:menuxy[0]+160+xx,
y:menuxy[1]+30*3+yy,
width:30,
height:25,
col:color(255,255,255,20),
label:"X",
onClick: function(){
resetPath(1);
}
});
pathfind = new button({
x:menuxy[0]+xx,
y:menuxy[1]+30*3+yy,
width:160,
height:25,
col:color(255,255,255,20),
label:"Djikstra",
onClick: function(){
//mapPath();
}
});
pathfind2 = new button({
x:menuxy[0]+xx,
y:menuxy[1]+30*4+yy,
width:160,
height:25,
col:color(255,255,255,20),
label:"Djikstra Node",
onClick: function(){
//mapPath();
}
});
pathfind3 = new button({
x:menuxy[0]+xx,
y:menuxy[1]+30*5+yy,
width:160,
height:25,
col:color(255,255,255,20),
label:"Super Pathfinder",
onClick: function(){
//mapPath3();
}
});
pathfind4 = new button({
x:menuxy[0]+xx,
y:menuxy[1]+30*6+yy,
width:160,
height:25,
col:color(255,255,255,20),
label:"Multi Pathfinder",
onClick: function(){
//cellNeighbours();
}
});
cluster = new button({
x:menuxy[0]+xx,
y:menuxy[1]+30*7+yy,
width:190,
height:25,
col:color(255,255,255,20),
label:"Cluster",
onClick: function(){
//resetSketch();
}
});
resetNode = new button({
x:menuxy[0]+160+xx,
y:menuxy[1]+30*4+yy,
width:30,
height:25,
col:color(255,255,255,20),
label:"X",
onClick: function(){
noded(grid);
}
});
generateNodes = new button({
x:menuxy[0]+xx,
y:menuxy[1]+30*5+yy,
width:190,
height:25,
col:color(255,255,255,20),
label:"Generate Nodes",
onClick: function(){
noded(grid);
}
});
findnodes = new button({
x:menuxy[0]+xx,
y:menuxy[1]+30*6+yy,
width:190,
height:25,
col:color(255,255,255,20),
label:"Findnodes",
onClick: function(){
//findnode();
}
});
set_target = new button({
x:menuxy[0]+xx,
y:menuxy[1]+30*8+yy,
width:95,
height:25,
col:color(255,255,255,20),
label:"Target",
onClick: function(){
if(target_set){
if(!pathfinder){
resetPath(1,target,pathfinder);
}
else{
resetPath(1,target,pathfinder);
}}}
});
set_pathfinder = new button({
x:menuxy[0]+xx+95,
y:menuxy[1]+30*8+yy,
width:95,
height:25,
col:color(255,255,255,20),
label:"Finder",
onClick: function(){
if(pathfinder_set){
if(!target){
pathfinders(1,null,pathfinder);
}
else{
pathfinders(1,target,pathfinder);
}
}}
});
extract_circuit = new button({
x:menuxy[0]+xx,
y:menuxy[1]+30*9+yy,
width:95,
height:25,
col:color(255,255,255,20),
label:"Extract",
onClick: function(){
//resetSketch();
}
});
back = new button({
x:menuxy[0]+xx+95,
y:menuxy[1]+30*9+yy,
width:95,
height:25,
col:color(255,255,255,20),
label:"Back",
onClick: function(){
//resetSketch();
}
});
reset = new button({
x:menuxy[0]+xx,
y:menuxy[1]+30*10+yy,
width:190,
height:25,
col:color(255,255,255,20),
label:"Reset",
onClick: function(){
resetSketch();
}
});
load = new button({
x:menuxy[0]+xx+4,
y:menuxy[1]+30*8+yy,
width:190,
height:25,
col:color(255,255,255,20),
label:"Load",
onClick: function(){
setups();
}
});
minimize = new button({
x:menuxy[0]+xx+190+2,
y:menuxy[1]+yy-10,
width:10,
height:10,
col:color(255,255,255,20),
label:"-",
onClick: function(){
hideMenu();
},
id: 1
});
var y = 560
var x = 90
var sw =6;
sliderPlayers = new button({
x:x,
y:y,
width:190,
height:sw,
col:color(255,255,255,20),
label:"No of Players",
value:1,
onClick: function(){
amend();
}
});
length = new button({
x:x,
y:y+14*1,
width:190,
height:sw,
col:color(255,255,255,20),
label:"XLen",
onClick: function(){
amend();
}
});
slidersMenu = new button({
x:x,
y:y-10,
width:190,
height:sw,
col:color(255,255,255,20),
label:"",
onClick: function(){
//setups();
}
});
height = new button({
x:x,
y:y+14*3,
width:190,
height:sw,
col:color(255,255,255,20),
label:"Ylen",
onClick: function(){
amend();
}
});
x_Padding = new button({
x:x,
y:y+14*4,
width:190,
height:sw,
col:color(255,255,255,20),
label:"X Padding",
onClick: function(){
amend();
}
});
slidersminimize = new button({
x:x,
y:y+14*4,
width:190,
height:sw,
col:color(255,255,255,20),
label:"-",
onClick: function(){
amend();
}
});
slidersdrag = new button({
x:x,
y:y+14*4,
width:190,
height:sw,
col:color(255,255,255,20),
label:"-",
onClick: function(){
amend();
}
});
y_Padding = new button({
x:x,
y:y+14*5,
width:190,
height:sw,
col:color(255,255,255,20),
label:"Y Padding",
onClick: function(){
amend();
}
});
renode = new button({
x:menuxy[0]+xx,
y:menuxy[1]+yy-10,
width:190+12,
height:320,
col:color(255,255,255,20),
label:"",
onClick: function(){
re_node();
}
});
buttons = [menu,game,start,paths,reset,pathfind,pathfind2,pathfind3,pathfind4,pathreset,generateNodes,findnodes,switches,load,sliderPlayers,minimize,menudrag,slidersMenu,slidersdrag,slidersminimize,cluster,extract_circuit,back,set_target,set_pathfinder];
sliders = [sliderPlayers,length,height,x_Padding,y_Padding];
for(var i=0;i<sliders.length;i++){
if(sliders[i].toggle ===1){
//resetSketch();
}}
};
function button(config){
this.x = config.x;
this.y = config.y;
this.w = config.width;
this.h = config.height;
this.col = config.col;
this.id = config.id;
this.col2 = color(255);
this.label = config.label;
this.onClick = config.onClick||function(){};
//this.value = config.value;
this.toggle = 0;
this.toggle2 = 0;
this.bw = 6;
this.bh = 6;
this.bxo = 0;
this.bxo1 = this.x;
this.stroke = 4;
this.textSize = 12;
this.visible = true;
this.slide = function(){
if(this.buttonPos&&mouseIsPressed&&mouseX<this.x+this.w&&mouseX>this.x){
this.value = abs(round(map(mouseX,this.x,this.x+this.w,0,40)));
}
}
this.draw = function(){
if(this.y+this.h<menu.h+menu.y+4+10&&this.y+4>menu.y){
if(this.toggle===1){
this.col =color(255,255,255,200);
this.stroke = 0;
this.col2 = color(0,0,0,175);
}
if(this.toggle===0){
this.stroke = 0;
this.col2 = color(255,255,255,175)
this.col =(255,255,255,80);
}
fill(this.col);
stroke(this.col2);
strokeWeight(this.stroke);
rect(this.x+xx,this.y+yy,this.w,this.h);
fill(this.col2);
strokeWeight(0.1);
textSize(this.textSize);
text(this.label,this.x+20+xx,this.y+17+yy);
//text(this.toggle,this.x+170,this.y+20)
strokeWeight(0);
}
};
this.draw2 = function(){
if(this.y+this.h<menu.h+menu.y+4&&this.y>menu.y-10){
if(this.toggle===1){
this.col =color(255,255,255,200);
this.stroke = 0;
this.col2 = color(0,0,0,120);
}
if(this.toggle===0){
this.stroke = 0;
this.col2 = color(0)
this.col =(255,255,255,80);
}
fill(this.col);
stroke(this.col2);
strokeWeight(this.stroke);
rect(this.x+xx,this.y+yy,this.w,this.h);
fill(255);
//text(toggle,10,10);
fill(this.col2);
strokeWeight(1);
textSize(this.textSize);
text(this.label,this.x+this.w/2-5+xx,this.y+20+yy-3);
//text(this.toggle,this.x+170,this.y+20)
strokeWeight(0);
}
};
this.draw3 = function(){
if(this.toggle===1){
this.col =color(255,255,255,200);
this.stroke = 0;
this.col2 = color(0,0,0,120);
}
if(this.toggle===0){
this.stroke = 0;
this.col2 = color(0)
this.col =(255,255,255,80);
}
fill(this.col);
stroke(this.col2);
strokeWeight(this.stroke);
rect(this.x+xx,this.y+yy,this.w,this.h);
fill(255);
//text(toggle,10,10);
fill(0);
strokeWeight(2);
textSize(this.textSize);
text(this.label,this.x+this.w/2-5+xx+2,this.y+20+yy-10);
//text(this.toggle,this.x+170,this.y+20)
strokeWeight(0);
};
this.draw4 = function(){
if(this.toggle===1){
this.col =color(255,255,255,200);
this.stroke = 0;
this.col2 = color(0,0,0,120);
}
if(this.toggle===0){
this.stroke = 0;
this.col2 = color(0)
this.col =(255,255,255,80);
}
fill(this.col);
stroke(this.col2);
strokeWeight(this.stroke);
rect(this.x,this.y,this.w,this.h);
//this.bxo1 = this.x;
fill(255)
textSize(12);
text(this.label,this.x-80,this.y+5)
if(this.toggle===1){
rect(this.bxo,this.y-1,this.bw+2,this.bh+2);
this.bxo1 = this.bxo;
}
else{
rect(this.bxo1,this.y-1,this.bw,this.bh);
}
fill(255);
//if(this.toggle===1){
var k = round(map(this.bxo1,this.x,this.x+this.w,0,40));
text(k,this.x+this.w,this.y);
//}
fill(0);
strokeWeight(2);
strokeWeight(0);
};
this.checkmpos = function(){
if(this.id===1&&!switches.checkmpos()){
return mouseX>this.x+xx && mouseX <(this.x + this.w+xx) && mouseY>this.y+yy && mouseY <(this.y + this.h+yy);
}else{
if(this.y+this.h<menu.h+menu.y+10&&this.y+4>menu.y){
return mouseX>this.x+xx && mouseX <(this.x + this.w+xx) && mouseY>this.y+yy && mouseY <(this.y + this.h+yy);
}}
};
this.hover = function(){
if(this.checkmpos2()){
this.textSize += 0.5;
this.col2 = color(255);
this.col = color(255);
}
else{
this.textSize = 12;
}
if(this.textSize>=14){
this.textSize = 14;
}
return true;
};
this.checkmpos2 = function(){
return mouseX>this.x+xx && mouseX <(this.x + this.w+xx) && mouseY>this.y+yy && mouseY <(this.y + this.h+yy);
};
this.mouseClick = function(){
if(this.y+this.h<menu.h+menu.y+4+10&&this.y+4>menu.y){
if(this.checkmpos()&&mouseClicked){
if(this.toggle===0){
this.toggle=1;
this.onClick();
this.col =color(255,255,255,200);
this.stroke = 5;
this.col2 = color(0,0,0,120);
}
else{
this.toggle=0;
this.stroke = 4;
this.col2 = color(0)
this.col =(255,255,255,80);
}
}
}
};
this.mouseClick2 = function(){
if(this.y+this.h<menu.h+menu.y+10&&this.y+4>menu.y){
if(this.checkmpos()&&mouseIsPressed){
this.toggle=1;
this.stroke = 5;
}
else if(this.checkmpos()&&mouseReleased&&this.toggle===1||!this.checkmpos()&&this.toggle===1){
this.toggle=0;
this.onClick();
this.stroke = 4;
}}
};
this.mouseClick3 = function(){
if(this.y+this.h<menu.h+menu.y+4+10&&this.y+4>menu.y){
if(this.checkmpos()&&mouseIsPressed&&!mousedown){
this.toggle=1;
}
else if(mouseReleased){
this.toggle=0;
}
}
};
this.mouseClick4 = function(){
if(this.checkmpos()&&mouseClicked){
if(this.toggle===0){
this.toggle = 1;
}
else{
this.toggle=0;
menu.toggle2=0;
}}
};
this.mouseClick5 = function(){
if(this.checkmpos2()&&mouseIsPressed){
this.toggle=1;
this.stroke = 5;
}
else if(this.checkmpos2()&&mouseReleased&&this.toggle===1||!this.checkmpos2()&&this.toggle===1){
this.toggle=0;
this.stroke = 4;
}
};
this.buttonPos = function(){
var a;
for(var i=0;i<sliders.length;i++){
if(sliders[i].toggle===1&&sliders[i]!==this){
a = 1;
}
}
if(mouseX>this.x&&mouseX<this.x+this.w&&mouseY>this.y-2&&mouseY<this.y+this.h+2&&a!==1){
return true;
}
}
this.buttonClick = function(){
if(this.buttonPos()&&mouseIsPressed){
this.toggle=1;
mousedown = true;
}
if(this.toggle ===1&&mouseDragged&&mouseIsPressed){
//mousedown = false;
this.onClick();
//reset_toggle = true;
if(this.toggle===1&&mouseX<this.x+this.w&&mouseX>this.x){
this.bxo = mouseX
}
this.bxo = mouseX;
if(mouseX> this.x+this.w){
this.bxo = this.x+this.w;
}
if(mouseX< this.x){
this.bxo = this.x;
}}
//resetSketch()
};
if(this.toggle===0){
reset_toggle = false;
mousedown = false;
}
this.hidden = function(){
if(this.toggle==1){
this.onClick();
}
}
};
function index (i,j){
if(i<0||i>len-1||j<0||j>ylen-1){
return undefined;
}
else{
return i+j*len;
}
};
function lShape (x,y,colour,array,object){
for(var i=0;i<array.length;i++){
if(array[i]===true){
var thetab = theta * i;
var thetac = theta * i+theta;
stroke(colour);
strokeWeight(2);
line(x+w/2*sin(thetab),y+w/2*cos(thetab),x+w/2*sin(thetac),y+w/2*cos(thetac));
}else{
//noStroke();
}
}
};
function lShape2 (x,y,colour,array,object){
for(var i=0;i<array[c].length;i++){
//for (var j=0;j<array[c].)
//var a =
if(array[c][i]===true){
var thetab = theta * i+a;
var thetac = theta * i+theta+a;
stroke(colour);
strokeWeight(2);
line(x+w/2*sin(thetab),y+w/2*cos(thetab),x+w/2*sin(thetac),y+w/2*cos(thetac));
}else{
noStroke();
}
}
};
function hideMenu(){
var t = 3;
var height = menu.h;
if(minimize.toggle===1){
t++;
menu.h+=-60;
if(menu.h<=0){
menu.h = 0;
menu.toggle2=1;
}
}
if(minimize.toggle===0){
t++;
menu.h+=60;
if(menu.h>=30*11){
menu.h = 30*11;
menu.toggle2=0;
}
}
noStroke();
fill(51)
//rect(menuxy[0],menuxy[1],menuxy[0]+190,menuxy[1]+400);
};
function Cell (x,y,id,ypos,xpos,col){
this.original_id = id;
this.currentid = null;
this.x = x+xmap;
this.y = y+ymap;
this.z = 0;
this.xpos = xpos;
this.ypos = ypos;
this.id = id;
this.nid = id;
this.walls = [];
this.walls2 = [];
this.wallsbackup = [];
this.edge = [];
this.fpos = [];
this.neighbour_cluster = [];
this.neighbour_cluster2 = [];
this.col = color(255, 255, 234);
this.pcol = this.col;
this.col2 = color(0, 0, 0);
this.vertex = [];
this.length = 0;
this.t = 0;
this.n1 = -1;
this.n2 = -1;
this.neighbours = [];
this.neighbours2 = [];
this.cluster_neighbour = [];
this.total = 0;
this.path = {};
this.node = false;
this.parent = [];
this.node_to_node_dist = {};
this.node_info = [];
this.myfootprint = {};
this.connections = [];
this.cons = [];
this.steps = 1;
this.child = [];
this.dist = 0;
this.found = [];
this.connections_to_Node = [];
this.addresstable = [];
this.pathtable = {};
this.pathtable_ref = {};
this.connectedNodes = {};
this.pathconnected = false;
this.imprinted_by = {};
this.nodesconnected = {};
this.score2 = 0;
this.nodes = 0;
this.ref_pos = 0;
this.outline_pos = 0;
this.init = function(){
var theta = 360/sides;
for (var i=0;i<total_players;i++){
//this.walls[i]=[true,true,true,true,true,true];
}
for (var i=0;i<sides;i++){
var thetab = theta * i;
var thetac = theta * i+theta;
this.walls.push(true);
this.walls2.push(true);
this.wallsbackup.push(true);
this.node_info.push([null])
this.edge.push([this.x+w/2*sin(thetab),this.y+w/2*cos(thetab),this.x+w/2*sin(thetac),this.y+w/2*cos(thetac)]);
this.vertex[i]= [this.x+w/2*sin(thetac),this.y+(w/2)*cos(thetac)];
this.length = dist(this.x+w/2*sin(thetab),this.y+w/2*cos(thetab),this.x+w/2*sin(thetac),this.y+w/2*cos(thetac));
}
};
this.showid = function(){
strokeWeight(0);
};
this.draw = function() {
// for(var i=0;i<this.walls.length;i++){
// for(var j=0;j<this.walls[i].length;j++){
// if(this.walls[i][j]!==false){
// //this.walls[i][j]=true;
// }
// }
// }
if(cluster.toggle===1){
lShape(this.x,this.y,this.col2,this.walls2,grid[this.original_id]);
if(this.t===1){
//this.visited = true;
}
if(this.visited ===true){
fill(255, 0, 0);
}
strokeWeight(0);
}else {
lShape(this.x,this.y,this.col2,this.walls,grid[this.original_id]);
if(this.t===1){
//this.visited = true;
}
if(this.visited ===true){
fill(255, 0, 0);
}
strokeWeight(0);
}
};
this.draw2 = function(c) {
lShape(this.x,this.y,this.col2,this.walls[0],grid[this.original_id]);
if(this.t===1){
this.visited = true;
}
if(this.visited ===true){
fill(255, 0, 0);
}
var b = this.walls;
strokeWeight(0);
};
this.fill = function(){
var mx = mouseX;
var pmx = pmouseX;
var my = mouseY;
var pmy = pmouseY;
var dx = mx-pmx;
var dy = my-pmy;
//noLoop();
if(dx===0&&dy===0){
//mousecounter--;
}
else{
mousecounter = 340;
}
if(mousecounter<=0){
mousecounter = 0;
//noLoop();
}
else{
//loop();
}
fill(51)
noStroke();
//rect(0,0,1100,660);
beginShape();
for(var k=0;k<sides;k++){
var thetab = theta * k;
var thetac = theta * k+theta;
fill(this.col);
if (this.visited) {
fill(this.pcol);
}
if(this.visited2||this.target&&(nodes_array_Created)){
fill(this.pcol2);
}
noStroke();
vertex(this.vertex[k][0],this.vertex[k][1]);
}
endShape();
};
this.highlight = function(){
beginShape();
for(var k=0;k<sides;k++){
noStroke();
var thetab = theta * k;
var thetac = theta * k+theta;
fill(0, 0, 255, 100);
vertex(this.x+w/2*sin(thetac),this.y+w/2*cos(thetac));
}
endShape();
};
this.highlight2 = function(){
//this.pcol.levels[4] = 50
// var a = color(this.pcol.levels[0]);
// var b = color(this.pcol.levels[1]);
// var c = color(this.pcol.levels[2]);
//
// if (a+20<255) {
// a = color(this.pcol.levels[0]+20);
// }
// else {
// a = color(this.pcol.levels[0]-20);
// }
// if (b+20<255) {
// b = color(this.pcol.levels[1]+20);
// }
// else {
// b = color(this.pcol.levels[1]+20);
// }
// if (c+20<255) {
// c = color(this.pcol.levels[2]+20);
// }
// else {
// c = color(this.pcol.levels[2]-20);
// }
//var d
var a = color(this.pcol.levels[0],this.pcol.levels[1],this.pcol.levels[1],125)
// var d = color(a,b,c,150);
beginShape();
for(var k=0;k<sides;k++){
noStroke();
var thetab = theta * k;
var thetac = theta * k+theta;
if(this.outer_tile){
fill(a);
vertex(this.x+w/2*sin(thetac),this.y+w/2*cos(thetac));
}}
endShape();
};
this.checkNeighbors = function(){
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
var n = neighbours.length;
fill(255, 0, 0);
if(right){
//text(right.id,this.x,this.y);
}
if(topL&&!topL.visited&&topL.id!==100000){
topL.rpos = 3;
neighbours.push(topL);
}
if(topR&&!topR.visited&&topR.id!==100000){
topR.rpos = 2;
neighbours.push(topR);
}
if(right&&!right.visited&&right.id!==100000){
right.rpos = 1;
neighbours.push(right);
}
if(btmR&&!btmR.visited&&btmR.id!==100000){
btmR.rpos = 0;
neighbours.push(btmR);
}
if(btmL&&!btmL.visited&&btmL.id!==100000){
btmL.rpos = 5;
neighbours.push(btmL);
}
if(left&&!left.visited&&left.id!==100000){
left.rpos = 4;
neighbours.push(left);
}
var n = neighbours.length-1;
if(neighbours.length>0){
var r = round(random(0,n));
return neighbours[r];
}
else{
return undefined;
}
};
this.checkNeighbors2 = function(a){
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
var n = neighbours.length;
fill(255, 0, 0);
if(right){
//text(right.id,this.x,this.y);
}
if(topL&&!topL.array2[a]&&topL.id!==100000){
topL.rpos = 3;
neighbours.push(topL);
}
if(topR&&!topR.array2[a]&&topR.id!==100000){
topR.rpos = 2;
neighbours.push(topR);
}
if(right&&!right.array2[a]&&right.id!==100000){
right.rpos = 1;
neighbours.push(right);
}
if(btmR&&!btmR.array2[a]&&btmR.id!==100000){
btmR.rpos = 0;
neighbours.push(btmR);
}
if(btmL&&!btmL.array2[a]&&btmL.id!==100000){
btmL.rpos = 5;
neighbours.push(btmL);
}
if(left&&!left.array2[a]&&left.id!==100000){
left.rpos = 4;
neighbours.push(left);
}
var n = neighbours.length-1;
if(neighbours.length>0){
var r = round(random(0,n));
if(neighbours.length>1){
var s = round(random(0,n));
}
return neighbours[r];
}
else{
return undefined;
}
};
this.nthPass = function(){
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
var n = neighbours.length;
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
if(topL && this.id!==topL.id&&topL.id!==100000&&topL.score!==1&&this.score!==1&&topL.id!==this.neighbour_cluster[0]&&topL.score2<3&&this.score2<3){
topL.rpos = 3
neighbours.push(topL);
}
if(topR && this.id!==topR.id&&topR.id!==100000&&topR.score!==1&&this.score!==1&&topR.id!==this.neighbour_cluster[0]&&topR.score2<3&&this.score2<3){
topR.rpos = 2;
neighbours.push(topR);
}
if(right && this.id!==right.id&&right.id!==100000&&right.score!==1&&this.score!==1&&right.id!==this.neighbour_cluster[0]&&right.score2<3&&this.score2<3){
right.rpos = 1;
neighbours.push(right);
}
if(btmR && this.id!==btmR.id&&btmR.id!==100000&&btmR.score!==1&&this.score!==1&&btmR.id!==this.neighbour_cluster[0]&&btmR.score2<3&&this.score2<3){
btmR.rpos = 0;
neighbours.push(btmR);
}
if(btmL && this.id!==btmL.id&&btmL.id!==100000&&btmL.score!==1&&this.score!==1&&btmL.id!==this.neighbour_cluster[0]&&btmL.score2<3&&this.score2<3){
btmL.rpos = 5;
neighbours.push(btmL);
}
if(left && this.id!==left.id&&left.id!==100000&&left.score!==1&&this.score!==1&&left.id!==this.neighbour_cluster[0]&&left.score2<3&&this.score2<3){
left.rpos = 4;
neighbours.push(left);
}
if(neighbours.length>0){
var r = round(random(0,neighbours.length-1));
return neighbours[r];
}
else{
return undefined;
}
};
this.nthPass2 = function(){
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
var n = neighbours.length;
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
// if(toggle){
if(!topL&&!topR){
neighbours.push(10000);
}
if(!right){
neighbours.push(20000);
}
if(!btmL&&!btmR){
neighbours.push(30000);
}
if(!left){
neighbours.push(40000);
}
// }
if(topL && (this.id!==topL.id&&topL.id!==100000)){
neighbours.push(topL);
}
if(topR && (this.id!==topR.id&&topR.id!==100000)){
neighbours.push(topR);
}
if(right && (this.id!==right.id&&right.id!==100000)){
neighbours.push(right);
}
if(btmR && (this.id!==btmR.id&&btmR.id!==100000)){
neighbours.push(btmR);
}
if(btmL && (this.id!==btmL.id&&btmL.id!==100000)){
neighbours.push(btmL);
}
if(left && (this.id!==left.id&&left.id!==100000)){
neighbours.push(left);
}
if(neighbours.length>0){
return neighbours;
}else{
return undefined;
}
};
this.fill_neighbour_cluster = function(){
var neighbour = this.nthPass2();
if(neighbour){
for (var i=0;i<neighbour.length;i++){
this.cluster_neighbour[i] = neighbour[i];
}
for(var i=0;i<neighbour.length;i++){
var c = neighbour[i];
if(isNaN(c)){
if(c.id!==this.id){
var a = this.neighbour_cluster.includes(c.id,0);
var b = c.neighbour_cluster.includes(this.id,0);
var d = this.neighbour_cluster2.includes(c.id,0);
var e = c.neighbour_cluster2.includes(this.id,0);
if(!a){
this.neighbour_cluster.push(c.id);
this.neighbouringCluster(this);
}
if(!b){
c.neighbour_cluster.push(this.id);
this.neighbouringCluster(c);
}
if(!d){
this.neighbour_cluster2.push(c.id);
}
if(!e){
c.neighbour_cluster2.push(this.id);
}}}
else{
var a = this.neighbour_cluster2.includes(c,0);
var b = this.neighbour_cluster.includes(c,0);
if(!a){
this.neighbour_cluster2.push(c);
}
if(!b){
this.neighbour_cluster.push(c);
}
}}}
};
this.neighbouringCluster = function(a){
for(var i=0;i<grid.length;i++){
if(grid[i].id===a.id){
grid[i].neighbour_cluster = a.neighbour_cluster;
grid[i].score2 = a.score2;
}
}
}
this.drawpath = function(){
if(paths_explored||paths.toggle===1){
var x = this.xpos;
var y = this.ypos;
var a = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
if(topL&&topL.walls[0]===false&&this.walls[3]===false){
a.push(topL);
}
if(topR&&topR.walls[5]===false&&this.walls[2]===false){
a.push(topR);
}
if(right&&right.walls[4]===false&&this.walls[1]===false){
a.push(right);
}
if(btmR&&btmR.walls[3]===false&&this.walls[0]===false){
a.push(btmR);
}
if(btmL&&btmL.walls[2]===false&&this.walls[5]===false){
a.push(btmL);
}
if(left&&left.walls[1]===false&&this.walls[4]===false){
a.push(left);
}
if (a.length > 0) {
this.total = a.length;
this.connect(a);
}
}
strokeWeight(0);
};
this.connect = function(a){
if(this.total !==2&&paths.toggle===1){
strokeWeight(w/6+len/w);
stroke(0, 0, 0);
point(this.x,this.y);
}
for(var i=0;i<a.length;i++){
stroke(0);
strokeWeight(2);
if(paths_explored){
this.neighbours[i] = a[i];
}
if(this.pathconnected===true&&findnodes.toggle===1){
stroke(255);
}
if(paths.toggle===1){
line(this.x,this.y,a[i].x,a[i].y);
}
}
if(paths_explored){
this.connected = true;
}
};
this.find = function (b,toggle,stack_array){
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
var a = 0;
if(topL&&topL.walls[0]===false&&this.walls[3]===false&&!topL.visited2){
neighbours.push(topL);
}
if(topR&&topR.walls[5]===false&&this.walls[2]===false&&!topR.visited2){
neighbours.push(topR);
}
if(right&&right.walls[4]===false&&this.walls[1]===false&&!right.visited2){
neighbours.push(right);
}
if(btmR&&btmR.walls[3]===false&&this.walls[0]===false&&!btmR.visited2){
neighbours.push(btmR);
}
if(btmL&&btmL.walls[2]===false&&this.walls[5]===false&&!btmL.visited2){
neighbours.push(btmL);
}
if(left&&left.walls[1]===false&&this.walls[4]===false&&!left.visited2){
neighbours.push(left);
}
if(neighbours.length>0){
var targeta = this.forward(neighbours,b,toggle,stack_array);
var targetb = this.nearest(neighbours,b,toggle,stack_array);
if(targeta){
line(this.x,this.y,b.x,b.y);
return targeta;
}
else{
line(this.x,this.y,b.x,b.y);
return targetb;
}}
};
this.findstart = function (){
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
for(var i=0;i<this.neighbours.length;i++){
var a = this.neighbours[i];
if(a&&a.visited2&&a.depth<=this.depth){
neighbours.push(a);
this.pcol2 = color(255,0,255,50);
}
}
for(var i=0;i<this.node_info.length;i++){
var a = this.node_info[i];
if(a[0]&&a[1].visited2&&a[1].depth<=this.depth){
neighbours.push(a[1]);
//this.pcol2 = color(255,0,255,50);
}
}
if(this ===pathbackup[0]){
return true;
}
else if(neighbours.length>0){
return neighbours.length;
}
};
this.findend = function (){
var next;
for(var i=0;i<this.neighbours.length;i++){
var a = this.neighbours[i];
if(a&&a.visited2&&a.depth>=this.depth){
//this.pcol2 = color(255,255,0,50);
next = true;
}
}
for(var i=0;i<this.node_info.length;i++){
var a = this.node_info[i];
if(a[0]&&a[1].visited2&&a[1].depth>=this.depth){
next = true;
}
}
if(this === target||next){
return true;
}
};
this.check_path_complete = function (){
var next;
for(var i=0;i<this.neighbours.length;i++){
var a = this.neighbours[i];
if(a.visited2&&a.completed){
//this.pcol2 = color(255,0,0,50);
this.completed = true;
next = true;
}
}
for(var i=0;i<this.node_info.length;i++){
var a = this.node_info[i];
if(a[0]&&a[1].completed&&a[1].depth>=this.depth){
this.completed = true;
next = true;
}
}
if(next){
return true;
}
};
this.forward = function(a,k,toggle,stack_array){
var b = this;
var c;
for(var i=0;i<a.length;i++){
c = a[i];
var d1 = dist(b.x,b.y,k.x,k.y);
var d2 = dist(a[i].x,a[i].y,k.x,k.y);
if(d1===d2){
if(toggle){
c.depth = this.depth;
pathfinder.push(c);
stack2.push(stack_array);
pathhist.push(stack_array);
}
}else{
if(d1>d2){
b = c;
}
else{
if(toggle===1){
c.depth = this.depth;
pathfinder.push(c);
stack2.push(stack_array);
pathhist.push(stack_array);
}
c = false;
}
}
}
return c;
};
this.nearest = function(a,k,toggle,stack_array){
var ta = 0;
var tb = 0;
var b = a[0];
var c;
for(var i=0;i<a.length-1;i++){
c = a[i+1];
var d1 = dist(b.x,b.y,k.x,k.y);
var d2 = dist(c.x,c.y,k.x,k.y);
if(d1===d2){
if(toggle){
c.depth = this.depth;
pathfinder.push(c);
stack2.push(stack_array);
pathhist.push(stack_array);
}
}else{
if(d1>d2){
if(toggle){
b.depth = this.depth;
pathfinder.push(b);
stack2.push(stack_array);
pathhist.push(stack_array);
}
b = c;
}
else{
if(toggle){
c.depth = this.depth;
pathfinder.push(c);
stack2.push(stack_array);
pathhist.push(stack_array);
//c = b;
}
}}
}
return b;
};
this.findnode = function(){
if((nodes_connected)){
// if(!nodesindex[0]){
// fill(255)
// text("please generate nodes",30, 10)
// }
// else{
if(this.id!==100000){
var x = this.xpos;
var y = this.ypos;
var a = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
if(!this.node){
if(topL&&topL.walls[0]===false&&this.walls[3]===false){
if(topL.node&&!this.node){
if(this.parent[0]&&this.parent[0]!==topL){
this.parent[1] = topL
}
else{
this.parent[0] = topL;
}
}
}
if(topR&&topR.walls[5]===false&&this.walls[2]===false){
if(topR.node&&!this.node){
if(this.parent[0]&&this.parent[0]!==topR){
this.parent[1] = topR
}
else{
this.parent[0] = topR;
}
}
}
if(right&&right.walls[4]===false&&this.walls[1]===false){
if(right.node&&!this.node){
if(this.parent[0]&&this.parent[0]!==right){
this.parent[1] = right;
}
else{
this.parent[0] = right;
}}
}
if(btmR&&btmR.walls[3]===false&&this.walls[0]===false){
if(btmR.node&&!this.node){
if(this.parent[0]&&this.parent[0]!==btmR){
this.parent[1] = btmR;
}
else{
this.parent[0] = btmR;
}}
}
if(btmL&&btmL.walls[2]===false&&this.walls[5]===false){
if(btmL.node&&!this.node){
if(this.parent[0]&&this.parent[0]!==btmL){
this.parent[1] = btmL;
}
else{
this.parent[0] = btmL;
}}
}
if(left&&left.walls[1]===false&&this.walls[4]===false){
if(left.node&&!this.node){
if(this.parent[0]&&this.parent[0]!==left){
this.parent[1] = left;
}
else{
this.parent[0] = left;
}}
}
}
if(this.node){
this.parents = [];
}
if(this.parent&&!this.parent[0]){
if(!this.node&&!this.neighbours[0].node&&!this.parent[0]&&this.neighbours[0].parent[0]){
this.parent[0] = this.neighbours[0].parent[0];
}
if(!this.node&&!this.neighbours[1].node&&!this.parent[0]&&this.neighbours[1].parent[0]){
this.parent[0] = this.neighbours[1].parent[0];
this.step = 1;
}
}
if(this.parent[0]&&!this.parent[1]){
if(!this.node&&!this.neighbours[0].node&&this.parent[0]&&!this.parent[1]&&this.neighbours[0].parent[0]&&this.neighbours[0].parent[0]!==this.parent[0]){
this.parent[1] = this.neighbours[0].parent[0];
}
if(!this.node&&!this.neighbours[0].node&&this.parent[0]&&!this.parent[1]&&this.neighbours[0].parent[1]&&this.neighbours[0].parent[1]!==this.parent[0]){
this.parent[1] = this.neighbours[0].parent[1];
}
if(!this.node&&!this.neighbours[1].node&&this.parent[0]&&!this.parent[1]&&this.neighbours[1].parent[0]&&this.neighbours[1].parent[0]!==this.parent[0]){
this.parent[1] = this.neighbours[1].parent[0];
}
if(!this.node&&!this.neighbours[1].node&&this.parent[0]&&!this.parent[1]&&this.neighbours[1].parent[1]&&this.neighbours[1].parent[1]!==this.parent[0]){
this.parent[1] = this.neighbours[1].parent[1];
}
}
}
//}
}
strokeWeight(0);
};
this.histScore = function(){
//this.score = 0;
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
var a = 0;
if(topL&&topL.walls[0]===false&&this.walls[3]===false&&this.id!==topL.id){
topL.rpos = 3;
neighbours.push(topL);
a ++;
}
if(topR&&topR.walls[5]===false&&this.walls[2]===false&&this.id!==topR.id){
topR.rpos = 2;
neighbours.push(topR);
a ++;
}
if(right&&right.walls[4]===false&&this.walls[1]===false&&this.id!==right.id){
right.rpos = 1;
neighbours.push(right);
a ++;
}
if(btmR&&btmR.walls[3]===false&&this.walls[0]===false&&this.id!==btmR.id){
btmR.rpos = 0;
neighbours.push(btmR);
a ++;
}
if(btmL&&btmL.walls[2]===false&&this.walls[5]===false&&this.id!==btmL.id){
btmL.rpos = 5;
neighbours.push(btmL);
a ++ ;
}
if(left&&left.walls[1]===false&&this.walls[4]===false&&this.id!==left.id){
left.rpos = 4;
neighbours.push(left);
a ++;
}
var n = neighbours.length;
if (n > 0) {
fill(255, 0, 0);
this.score = 1;
}else{
}
};
this.hover = function(){
//redraw()
var x = mouseX-xmap;var y = mouseY-ymap;
var a = this.edge;
for(var i=0;i<a.length;i++){
var x1 = a[i][0];
var y1 = a[i][1];
var x2 = a[i][2];
var y2 = a[i][3];
var d1 = dist(x,y,x1,y1);
var d2 = dist(x,y,x2,y2);
var d3 = dist(x1,y1,x2,y2);
var d4 = d1+d2;
var d5 = dist(this.x,this.y,x,y);
var dy = y1-y2;
var dx = x1-x2;
var dy2 = y2-y1;
var dx2 = x2-x1;
var dmx = x-x1;
var dmy = y-y1;
var dmx2 = x-x2;
var dmy2 = y-y2;
//fill(255, 0, 0);
var thetaXY1 = atan2(dy,dx);
var thetaXY2= atan2(dy2,dx2);
var thetaMXY2 = atan2(dmy,dmx);
var thetaMXY1 = atan2(dmy2,dmx2);
if((thetaXY1>=0&&thetaMXY1>=thetaXY1&&thetaMXY1>=0&&d1<=this.length&&d2<=this.length)||(thetaXY1<=0&&thetaMXY1<=0&&thetaMXY1>=thetaXY1&&d1<=this.length&&d2<=this.length)||(thetaXY2>=0&&thetaMXY2>=0&&thetaMXY2<=thetaXY2&&d1<=this.length&&d2<=this.length)||(thetaXY2<=0&&thetaXY2<=0&&thetaMXY2<=thetaXY2&&d1<=this.length&&d2<=this.length)||d5<=this.length/1.5){
//text("hello",this.x,this.y);
return true;
}
}
};
this.hover_cluster = function(){
if(cluster_complete&&(game.toggle===1||paths.toggle===1||cluster.toggle===1||switches.toggle===1)){
if(this.hover()){
this.highlight();
for(var i=0;i<clusters[this.currentid].length;i++){
if(clusters[this.currentid][i].id===this.id){
clusters[this.currentid][i].highlight2();
}
}}}
};
this.ppos = function(){
};
this.checkp = function(){
if(this.hover()&&mouseOver){
this.highlight();
this.col = color(29, 131, 240);
this.col2 = color(212, 8, 8);
}
else{
this.col = color(0, 204, 255);
this.col2 = color(162, 0, 255);
}
};
this.toggle = function(){
if(this.hover()){
this.hovered = true;
this.highlight();
}
else{
this.hovered = false;
}
if(this.hover()&&mouseClicked&&mousedown){
if(set_target.toggle===0&&set_pathfinder===0){
this.t++;
if(this.t===2){this.t=0;}
}
if(set_target.toggle===1){
target = this;
target_set = true;
//this.target = true;
set_target.toggle=0
}
if(set_pathfinder.toggle===1){
pathfinder[0] = this;
pathbackup[0] = this;
//pathsta
pathfinder_set = true;
set_pathfinder.toggle = 0;
stack2 = [[]];
pathhist = [[]];
}
}
if(this.t===1){
this.visited = false;
}
if(this.t===0){
//this.visited = true;
}
};
this.info = function(){
if(xmap<-200){
xmap = -200;
}
if(ymap<-200){
ymap = -200;
}
if(xmap>500){
xmap = 500;
}
if(ymap>350){
ymap = 350;
}
var a = 800-xmap;
var b = 410-ymap;
if(!menu.checkmpos()&&(this.hover()&&mousePressed&&(game.toggle===1||paths.toggle===1||start.toggle===1||findnodes.toggle===1)||this.toggle===1)&&!mousedown){
//strokeWeight(1)
fill(255)
stroke(0)
rect(a-2,b-10,190,250)
fill(0)
text(this.walls,a,b);
text("Total" +" " + this.total,a,b+20);
text("Node" +" " + this.node,a,b+20*2);
text("Node" +" " + this.nodeNode,a+90,b+20*2);
text("Id" +" " +this.original_id,a,b+20*3);
text("Cluster " +" " +this.id,a,b+20*4);
text("Neighbours" +" " +this.neighbours.length,a,b+20*5);
for(var i=0;i<this.neighbours.length;i++){
text(this.neighbours[i].original_id,a+30*i,b+20*6)
}
text("Toggle " +" " +this.t,a,b+20*7);
text("Visited:" + " " + this.visited,a,b+20*8);
//}
text("Path connected:" +" " + this.pathconnected,a,b+20*9);
text("Parents :",a,b+20*10);
if(this.parent[0]){
for(var i=0;i<2;i++){
stroke(51);
strokeWeight(0)
if(this.parent[i]){
text(this.parent[i].original_id,60 + a+30*i,b+20*10);
}}}
text("Connections :",a,b+20*11);
var d = -1;
for(var i=0;i<this.node_info.length;i++){
d ++;
if(this.node_info[i][1]){
text("[ "+this.node_info[i][1].original_id+ " :"+ this.node_info[i][0]+"], ",80+a+60*d,b+20*11);
}
else{
d --;
}
}
if(this.node){
text("Children :", a,b+20*12);
for(var i=0;i<this.child.length;i++){
text(this.child[i][1].original_id +", ", a+50+25*i,b+20*12);
}}}
strokeWeight(0)
};
this.neighbour_Node = function(a,steps){
var neighbours = []
for(var i=0;i<this.neighbours.length;i++){
var b = this.neighbours[i];
if(!b.connectedNodes[a]){
neighbours.push(b);
}
if(b.connectedNodes[a]&&b.connectedNodes[a]<steps){
//neighbours.push(b);
}
}
if(neighbours.length>0){
var r = round(random(0,neighbours.length-1));
return neighbours[r];
}
else{
return undefined;
}
};
this.path_to_node = function(){
if(this.neighbours.length===2){
this.connection = [Object.keys(this.connectedNodes)];
}
if(this.neighbours.length===2){
for(var i=0;i<this.connection[0].length;i++){
this.connections[i] = grid[parseInt(this.connection[0][i],10)];
}}
};
this.connectallnodes = function(objName,obj){
this.connections_to_Node = [Object.keys(this.nodesconnected),Object.values(this.nodesconnected)];
for(var i=0;i<this.connections_to_Node[0].length;i++){
this.addresstable[i] = [grid[parseInt(this.connections_to_Node[0][i])],this.connections_to_Node[1][i]]
}
this.temp_paths = [Object.keys(this.pathtable),Object.values(this.pathtable)];
for(var i=0;i<this.temp_paths[0].length;i++){
this.best_paths[i] = [grid[parseInt(this.temp_paths[0][i])],this.temp_paths[1][i]];
}
for(var i=0;i<this.temp_paths[0].length;i++){
this.best_paths[i] = [grid[parseInt(this.temp_paths[0][i])],this.temp_paths[1][i]];
}
};
this.flattenDeep = function (arr1) {
return arr1.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), []);
};
this.neighbourNode = function(toggle,b,c){
var neighbour_Node = [];
//console.log(b);
for(var i=0;i<this.node_info.length;i++){
var a = this.node_info[i];
if(a[0]){
var k = a[1].node_to_node_dist[b];
if(a[1].imprinted_by[b]){
if(k>this.node_to_node_dist[b] + a[0]){
neighbour_Node.push(a);
}
}
else{
neighbour_Node.push(a);
}}
}
if(neighbour_Node.length>1){
var a = this.shortestPath(neighbour_Node,c,toggle,starhist);
if(a){
//var a = neighbour_Node[0]
return a;
}}
else if(neighbour_Node.length===1){
return neighbour_Node[0];
}
};
this.shortestPath = function(a,c,toggle){
//console.log(c.original_id);
var neighbour_Node = a[0];
for(var i=1;i<a.length;i++){
if(neighbour_Node[0]>a[i][0] ){
neighbour_Node = a[i];
}
else{
if(toggle){
a[i][1].node_to_node_dist[c.original_id] = this.node_to_node_dist[c.original_id] + a[i][0];
c.nodesconnected[a[i][1].original_id] = a[i][1].node_to_node_dist[c.original_id];
if(!this.path_ref[c.original_id]){
this.path_ref[c.original_id] = [this]
}
a[i][1].path_ref[c.original_id] = [this.path_ref[c.original_id],a[i][1]];
a[i][1].path_ref[c.original_id] = a[i][1].path_ref[c.original_id].flat();
c.pathtable[a[i][1].original_id] = [a[i][1].path_ref[c.original_id]];
backupstar.push(c);
nodestar.push(a[i][1]);
stackstar.push([]);
starhist.push([]);
}
}
}
return neighbour_Node;
};
this.findnextNode = function(toggle,stack_array,path_array){
var a = this.node_info[0];
var b = this.node_info[1];
var step1 = a[0];
var step2 = b[0];
if(a[0]&&b[0]){
if(return_path){
if(a[1].visited2&&a[1].depth<this.depth&&a[1].total!==1&&!b.visited2){
this.verified = true;
stack_array.push(this);
for(var i=0;i<a[1].child.length;i++){
var e = a[1].child[i][1];
var d = e.node_info[0];
var c = e.node_info[1];
if(!e.visited2){
e.visited2;
}
if(e.depth<this.depth&&(d[1]===a[1]&&d[0]<a[0]&&c[1]===b[1]||c[1]===a[1]&&c[0]<a[0]&&d[1]===b[1])){
e.verified = true;
e.pcol2 = color(255,255,255,150);
stack_array.push(b);
}}
a[1].verified = true;
a[1].pcol2 = color(255,255,255,150);
stack_array.push(a[1]);
return a[1];
}
else if(b[0]&&b[1].depth<this.depth&&b[1].total!==1){
this.verified = true;
for(var i=0;i<b[1].child.length;i++){
var e = b[1].child[i][1];
var d = e.node_info[0];
var c = e.node_info[1];
if(e.depth<this.depth&&(d[1]===b[1]&&d[0]<b[0]&&c[1]===a[1]||c[1]===b[1]&&c[0]<b[0]&&d[1]===a[1])){
e.verified = true;
e.pcol2 = color(255,255,255,150);
stack_array.push(c);
}}
b[1].verified = true;
b[1].pcol2 = color(255,255,255,150);
stack_array.push(b[1]);
return b[1];
}
}
else{
if(a[1]===b[1]&&!a[1].visited2&&!b[1].visited2){
if(toggle){
stack_array.push(this);
path_array.push(this);
for(var i=0;i<b[1].child.length;i++){
var k = b[1].child[i];
var d = k[1].node_info[0];
var c = k[1].node_info[1];
if((d[1]===b[1]&&c[1]===a[1]&&d[0]<step2||c[1]===b[1]&&d[1]===a[1]&&c[0]<step2)){
k[1].pcol2 = color(0,0,0,150);
k[1].depth = this.depth+k[0];
k[1].visited2 = true;
stack_array.push(k[1]);
path_array.puch(k[1]);
b[1].pcol2 = color(0,0,0,150);
b[1].depth = this.depth+b[0];
b[1].visited2 = true;
stack_array.push(b[1]);
path_array.push(b[1]);
}}}
return b[1];
}
var d1 = dist(a[1].x,a[1].y,target.x,target.y)
var d2 = dist(b[1].x,b[1].y,target.x,target.y)
if(b[1].visited2&&a[1].visited2){
if(!toggle&&!a){
//this.pcol2 = color(0,0,0,0);
//this.visited3 = true;
}
return false;
}
else if(d1<d2&&!a[1].visited2){
if(toggle){
stack_array.push(this);
path_array.push(this);
for(var i=0;i<a[1].child.length;i++){
var k = a[1].child[i];
var d = k[1].node_info[0];
var c = k[1].node_info[1];
if((d[1]===a[1]&&c[1]===b[1]&&d[0]<step1||c[1]===a[1]&&d[1]===b[1]&&c[0]<step1)){
k[1].pcol2 = color(0,0,0,150);
k[1].depth = this.depth+k[0];
k[1].visited2 = true;
stack_array.push(k[1]);
path_array.push(k[1]);
a[1].pcol2 = color(0,0,0,150);
a[1].depth = this.depth+a[0];
a[1].visited2 = true;
stack_array.push(a[1]);
path_array.push(a[1]);
}}}
return a[1];
}
else if(!b[1].visited2){
if(toggle){
stack_array.push(this);
path_array.push(this);
for(var i=0;i<b[1].child.length;i++){
var k = b[1].child[i];
var d = k[1].node_info[0];
var c = k[1].node_info[1];
if((d[1]===b[1]&&c[1]===a[1]&&d[0]<step2||c[1]===b[1]&&d[1]===a[1]&&c[0]<step2)){
k[1].pcol2 = color(0,0,0,150);
k[1].depth = this.depth+k[0];
k[1].visited2 = true;
stack_array.push(k[1]);
path_array.push(k[1]);
b[1].pcol2 = color(0,0,0,150);
b[1].depth = this.depth+b[0];
b[1].visited2 = true;
stack_array.push(b[1]);
path_array.push(b[1]);
}}}
return b[1];
}}}
else{
return undefined;
}
};
this.nextNode = function(toggle,toggle2,stack_array,path_array){
var neighbour_Node = [];
var child;
for(var i=0;i<this.child.length;i++){
var a = this.child[i];
if(a[1] === target){
child = a;
}}
if(!child){
for(var i=0;i<this.node_info.length;i++){
var a = this.node_info[i];
if(a[0]){
if(return_path){
if(a[1].depth<this.depth&&a[1].visited2){
neighbour_Node.push(a);
}}
else{
if(a[1].total===1&&a[1]!==target){
}else if(!a[1].visited2){
neighbour_Node.push(a);
}}}}
if(neighbour_Node.length>1){
if(return_path){
var a = this.shortestPath2(neighbour_Node,false,stack_array);
a[1].verified = true;
if(a[1].child.length>0){
for(var i=0;i<this.child.length;i++){
var e = this.child[i][1];
var c = e.node_info[0][1]
var d = e.node_info[1][1]
if((this===c&&a[1]===d&&d.visited2)||(this===d&&a[1]===c&&c.visited2)){
e.visited2 = true
e.verified = true;
e.pcol2 = color(255,255,255,150);
}}}
a[1].verified = true;
a[1].pcol2 = color(255,255,255,150);
stack_array.push(a[1]);
return a[1];
}
else{
var a = this.shortestPath2(neighbour_Node,toggle2,stack_array,path_array);
if(a[0]){
if(toggle){
//this.depth = this.depth+1;
this.visited2 = true;
this.pcol2 = color(0,0,0,150);
stack_array.push(this);
path_array.push(this);
if(this.child.length>0){
for(var i=0;i<this.child.length;i++){
var k = this.child[i];
var b = k[1].parent[0];
var c = k[1].parent[1];
if((this===b&&a[1]===c&&!c.visited2)||(this===c&&a[1]===b&&!b.visited2)){
k[1].pcol2 = color(0,0,0,150);
k[1].depth = this.depth + k[0];
k[1].visited2 = true;
stack_array.push(k[1]);
path_array.push(k[1]);
}}}
a[1].depth = this.depth+a[0];
a[1].visited2 = true;
a[1].pcol2 = color(0,0,0,150);
stack_array.push(a[1]);
path_array.push(a[1]);
}
return a[1];
}
}}
else if(neighbour_Node.length===1){
var a = neighbour_Node[0];
if(return_path){
stack_array.push(this);
if(a[1].child.length>0){
for(var i=0;i<this.child.length;i++){
var b = this.child[i][1];
var c = b.node_info[0][1]
var d = b.node_info[1][1]
if(c===this&&d===a[1]||d===this&&c===a[1]&&b.depth<this.depth){
b.verified = true;
b.visited2 = true;
b.pcol2 = color(255,255,255,150);
stack_array.push(b);
}}}
a[1].verified = true;
a[1].pcol2 = color(255,255,255,150);
stack_array.push(a[1]);
pathbackup[1].visited2 = true;
pathbackup[1].pcol2 = color(255,255,255,255);
return a[1];
}
else{
if(toggle){
//this.depth = this.depth+1;
this.visited2 = true;
this.pcol2 = color(0,0,0,150);
stack_array.push(this);
path_array.push(this);
if(this.child.length>0){
for(var i=0;i<this.child.length;i++){
var k = this.child[i];
var b = k[1].parent[0];
var c = k[1].parent[1];
if((this===b&&a[1]===c&&!c.visited2)||(this===c&&a[1]===b&&!b.visited2)){
k[1].pcol2 = color(0,0,0,150);
k[1].depth = this.depth + k[0];
k[1].visited2 = true;
stack_array.push(k[1]);
path_array.push(k[1]);
}}}
a[1].depth = this.depth+a[0];
a[1].visited2 = true;
a[1].pcol2 = color(0,0,0,150);
stack_array.push(a[1]);
path_array.push(a[1]);
}
return a[1];
}}
else{
return undefined;
}}
else{
this.node_tochild(toggle,stack_array,path_array);
if(return_path){
target.veried = true;
}
//target.visited2 = true;
if(toggle){
//target.depth = this.depth+1;
this.completed = true;
target.completed = true;
stack_array[0].completed = true;
stack_array.push(target);
path_array.push(target);
}
target.highlight();
return target;
}
};
this.node_tochild = function(toggle,stack_array,path_array,toggle3){
var step;
var depth;
for(var i=0;i<this.child.length;i++){
var a = this.child[i];
if(a[1] === target){
step = a[0];
depth = a[1].depth
}}
var x = target.node_info;
var a = x[0];
var b = x[1]
if(return_path){
if(a[1]===this){
this.pcol2 = color(255,255,255,150);
this.verified = true;
stack_array.push(this);
path_array.push(this);
for(var i=0;i<a[1].child.length;i++){
var k = a[1].child[i];
var d = k[1].node_info[0];
var c = k[1].node_info[1];
if(target.depth<this.depth){
if((d[1]===a[1]&&c[1]===b[1]&&k[1].depth>target.depth||c[1]===a[1]&&d[1]===b[1]&&k[1].depth>target.depth)){
k[1].pcol2 = color(255,255,255,150);
k[1].verified = true;
stack_array.push(k[1]);
}}
if(target.depth>this.depth){
if((d[1]===a[1]&&c[1]===b[1]&&k[1].depth<target.depth||c[1]===a[1]&&d[1]===b[1]&&k[1].depth<target.depth)){
k[1].pcol2 = color(255,255,255,150);
k[1].verified = true;
stack_array.push(k[1]);
}}}
pathbackup[1].pcol2 = color(255,255,255,255);
return target;
}
if(b[1]===this){
this.pcol2 = color(255,255,255,150);
this.verified = true;
stack_array.push(this);
path_array.push(this);
for(var i=0;i<b[1].child.length;i++){
var k = b[1].child[i];
var d = k[1].node_info[0];
var c = k[1].node_info[1];
if(target.depth<this.depth){
if((d[1]===b[1]&&c[1]===a[1]&&k[1].depth>target.depth||c[1]===b[1]&&d[1]===a[1]&&k[1].depth>target.depth)){
k[1].pcol2 = color(255,255,255,150);
k[1].verified = true;
stack_array.push(k[1]);
}}
if(target.depth>this.depth){
if((d[1]===b[1]&&c[1]===a[1]&&k[1].depth<target.depth||c[1]===b[1]&&d[1]===a[1]&&k[1].depth<target.depth)){
k[1].pcol2 = color(255,255,255,150);
k[1].verified = true;
stack_array.push(k[1]);
}}}
return target;
}
}
else{
if(a[1]===this){
if(toggle){
this.pcol2 = color(0,0,0,150);
this.visited2 = true;
stack_array.push(this);
path_array.push(this);
for(var i=0;i<a[1].child.length;i++){
var k = a[1].child[i];
var d = k[1].node_info[0];
var c = k[1].node_info[1];
if((d[1]===a[1]&&c[1]===b[1]&&d[0]<step||c[1]===a[1]&&d[1]===b[1]&&c[0]<step)&&!d.visited2&&!c.visited2){
k[1].pcol2 = color(0,0,0,150);
k[1].depth = this.depth+k[0];
k[1].visited2 = true;
stack_array.push(k[1]);
path_array.push(k[1]);
}}}
target.visited2 = true;
target.depth = this.depth+ a[0];
return target;
}
if(b[1]===this){
if(toggle){
this.pcol2 = color(0,0,0,150);
this.visited2 = true;
stack_array.push(this);
path_array.push(this);
for(var i=0;i<b[1].child.length;i++){
var k = b[1].child[i];
var d = k[1].node_info[0];
var c = k[1].node_info[1];
if((d[1]===b[1]&&c[1]===a[1]&&d[0]<step||c[1]===b[1]&&d[1]===a[1]&&c[0]<step)&&!d.visited2&&!c.visited2){
k[1].pcol2 = color(0,0,0,150);
k[1].depth = this.depth+k[0];
k[1].visited2 = true;
stack_array.push(k[1]);
path_array.push(k[1]);
}}}
target.visited2 = true;
target.depth = this.depth + b[0];
return target;
}
}
};
this.shortestPath2 = function(a,toggle2,stack_array,path_array){
var node = a[0];
var b = this;
if(return_path){
for(var i=1;i<a.length;i++){
var d1 = dist(node[1].x,node[1].y,this.x,this.y);
var d2 = dist(a[i][1].x,a[i][1].y,this.x,this.y);
if(a[i][1].depth<node.depth&&a[i][1].visited2){
if(d2<d1){
a[i][1].verified = true;
node = a[i];
}}
else{
}
return node;
}}
else{
//if(toggle2){
for(var i=1;i<a.length;i++){
if(a[i][1]===target){
return a[i]
}
else if(a[i][1].total!==1){
var d1 = dist(node[1].x,node[1].y,target.x,target.y);
var d2 = dist(a[i][1].x,a[i][1].y,target.x,target.y);
var d3 = dist(node[1].x,node[1].y,pathbackup[0].x,pathbackup[0].y);
var d4 = dist(a[i][1].x,a[i][1].y,pathbackup[0].x,pathbackup[0].y);
var d5 = d1*5+d3;
var d6 = d2*5+d4;
if(d5>d6){
if(toggle2){
//this.depth = this.depth+1;
this.pcol2 = color(0,0,0,150);
this.visited2 = true;
pathfinder.push(this);
stack2.push([]);
pathhist.push([]);
}
node = a[i];
}
else{
if(toggle2){
//this.depth = this.depth+1;
this.pcol2 = color(0,0,0,150);
this.visited2 = true;
pathfinder.push(this);
stack2.push([]);
pathhist.push([]);
}
}}}
return node;
}
//}
};
this.check_node_info = function(){
var temp = 0;
for(var i=0;i<this.node_info.length;i++){
if(this.node_info[i][0]){
temp ++;
}
}
this.nodes = temp;
};
this.dd = function(){
fill(0)
//text(this.outline_pos,this.x-5,this.y+5);
};
this.move = function(){
for (var i=0;i<sides;i++){
var thetab = theta * i;
var thetac = theta * i+theta;
this.vertex[i]= [this.x+w/2*sin(thetac),this.y+(w/2)*cos(thetac)];
}
this.x += random(-1,1);
this.y += random(-1,1);
};
this.track = function(){
if(grid_Cleared){
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
var neighbours1 = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
if(!topL&&!topR){
neighbours.push(10000);
}
if(topL&&topL.id!==this.id){
neighbours.push(0);
}
if(topR&&topR.id!==this.id){
neighbours.push(0);
}
if(!right){
neighbours.push(20000);
}
if(right&&right.id!==this.id){
neighbours.push(0);
}
if(!btmR&&!btmL){
neighbours.push(30000);
}
if(btmR&&btmR.id!==this.id){
neighbours.push(0);
}
if(btmL&&btmL.id!==this.id){
neighbours.push(0);
}
if(!left){
neighbours.push(40000);
}
if(left&&left.id!==this.id){
neighbours.push(0);
}
if(neighbours.length>0){
this.outer_tile = true;
}
else {
this.outer_tile = false;
}}
};
this.draw_track = function(toggle){
if(cluster.toggle===1&&pathfind.toggle!==1&&pathfind2.toggle!==1&&pathfind3.toggle!==1&&pathfind4.toggle!==1){
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
var neighbours1 = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
var n = neighbours.length;
if(topL&&topL.id===this.id&&topL.outer_tile&&!topL.tracked){
neighbours.push(topL);
}
if(topR&&topR.id===this.id&&topR.outer_tile&&!topR.tracked){
topR.rpos =
neighbours.push(topR);
}
if(right&&right.id===this.id&&right.outer_tile&&!right.tracked){
neighbours.push(right);
}
if(btmR&&btmR.id===this.id&&btmR.outer_tile&&!btmR.tracked){
neighbours.push(btmR);
}
if(btmL&&btmL.id===this.id&&btmL.outer_tile&&!btmL.tracked){
neighbours.push(btmL);
}
if(left&&left.id===this.id&&left.outer_tile&&!left.tracked){
neighbours.push(left);
}
if(!toggle){
return true;
}
else {
if(neighbours.length>0){
for(var k=0;k<this.neighbour_cluster2.length;k++){
var b = this.neighbour_cluster2[k];
for(var i=0;i<neighbours.length;i++){
for(var j=0;j<neighbours[i].neighbour_cluster2.length;j++){
var next = this.neighbour_cluster2.includes(b);
if(!next){
neighbours.splice(i);
i--;
}
}}}
return this.closest(neighbours);
}
else if(neighbours.length===0){
return neighbours[0];
}
else {
return undefined;
}}}
};
this.closest = function(a){
var k = a[0]
var temp = [];
for(var i=0;i<a.length;i++){
temp.push(0);
}
for(var i=0;i<a.length;i++){
for(var j=0;j<a[i].neighbour_cluster2.length;j++){
var b = a[i].neighbour_cluster2[j];
var next = this.neighbour_cluster2.includes(b);
if(next){
temp[i] +=1;
}
}}
var index = 0;
var first = temp[0];
for(var i=1;i<temp.length;i++){
var b = a[i-1].cluster_neighbour.length;
var c = a[i].cluster_neighbour.length;
if(first===temp[i]){
if(b===c){
this.pcol2 = color(0)
}
else if(b<c){
first = temp[i];
index = i;
}}
else if(first<temp[i]){
//if((a[i-1].cluster_neighbour.length<a[i].cluster_neighbour.length&&first!==0||first<temp[i])){
first = temp[i];
index = i;
}
}
if(index>0){
}
return a[index];
};
this.complete_track = function(){
if(cluster.toggle===1&&pathfind.toggle!==1&&pathfind2.toggle!==1&&pathfind3.toggle!==1&&pathfind4.toggle!==1){
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
var neighbours1 = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
fill(255);
//text(this.outline_pos,this.x,this.y)
if(topL&&topL.id===this.id&&topL.outer_tile&&this.outer_tile&&(topL.outline_pos===this.outline_pos+1||this.end)){
neighbours.push(topL);
}
if(topR&&topR.id===this.id&&topR.outer_tile&&this.outer_tile&&(topR.outline_pos===this.outline_pos+1||this.end)){
neighbours.push(topR);
}
if(right&&right.id===this.id&&right.outer_tile&&this.outer_tile&&(right.outline_pos===this.outline_pos+1||this.end)){
neighbours.push(right);
}
if(btmR&&btmR.id===this.id&&btmR.outer_tile&&this.outer_tile&&(btmR.outline_pos===this.outline_pos+1||this.end)){
neighbours.push(btmR);
}
if(btmL&&btmL.id===this.id&&btmL.outer_tile&&this.outer_tile&&(btmL.outline_pos===this.outline_pos+1||this.end)){
neighbours.push(btmL);
}
if(left&&left.id===this.id&&left.outer_tile&&this.outer_tile&&(left.outline_pos===this.outline_pos+1||this.end)){
neighbours.push(left);
}
if(neighbours.length>0){
this.connect2(neighbours);
}}
};
this.connect2 = function(a){
for(var i=0;i<a.length;i++){
stroke(0);
strokeWeight(2);
line(this.x,this.y,a[i].x,a[i].y);
if(clusters.toggle===1){
stroke(0);
}}
};
};
function shortestPath(a){
};
function check (a, b) {
return (a.length !== b.length) ? false :
!(a.some(function(row, index) {
return a[index] !== b[index];
}));
};
// this function has been amended from its previous iteration, the neighbor/neighbour function now updates its target cel with a reference position this.rpos which can be called to more easily locate the required walls to remove. Reference position will return a position from 0-n according to the number of sides, for the hexagon it returns 0-5 with 0 being the bottom right line and moving counter clockwise. 0 is paired with 3, 1 with 4, 2 with 5, 3 with 0, 4 with 1, and 5 with 2.
function removeWalls (a, b) {
if (b.rpos === 0) {
a.walls[0] = false;
b.walls[3] = false;
} else if (b.rpos === 1) {
a.walls[1] = false;
b.walls[4] = false;
} else if (b.rpos === 2) {
a.walls[2] = false;
b.walls[5] = false;
} else if (b.rpos === 3) {
a.walls[3] = false;
b.walls[0] = false;
} else if (b.rpos === 4) {
a.walls[4] = false;
b.walls[1] = false;
} else if (b.rpos === 5) {
a.walls[5] = false;
b.walls[2] = false;
}
};
function rebootPlayers(){
current = [];
if(len>20){
for (var i=0;i<current.length;i++){
start_array[i]= current.slice(i,1);
start_array[i].pcol = current[i].pcol;
}
}
else{
for (var i=0;i<start_array.length;i++){
start_array[i]= current.slice(i,1);
start_array[i].pcol = current[i].pcol;
}
}
};
function setups(){
if(!nodes_array_Created){
}
for(var i=0;i<grid.length;i++){
if(grid[i].total!==2&&grid[i].neighbours.length>0){
grid[i].node = true;
}
if(grid[i].total===2&&grid[i].neighbours.length>0){
grid[i].node = false;
}}
if(game.toggle===1&&cluster.toggle!==1){
if(reset.toggle===1){
start.toggle=0
switches.toggle=0
game.toggle=0;
reset.toggle=0;
//paths.toggle=0;
pathfind.toggle=0;
}
for(var i=0;i<grid.length;i++){
grid[i].fill();
grid[i].dd()
grid[i].hover();
}}
if(!cluster_complete){
for(var i=0;i<grid.length;i++){
grid[i].drawpath();
}}
if(cluster_complete&&paths.toggle===1){
for(var i=0;i<grid.length;i++){
grid[i].drawpath();
}}
if(cluster.toggle===1&&pathfind.toggle!==1&&pathfind2.toggle!==1&&pathfind3.toggle!==1&&pathfind4.toggle!==1){
for(var i=0;i<grid.length;i++){
grid[i].track();
if(grid[i].outer_tile){
grid[i].fill();
grid[i].fill_neighbour_cluster();
}}}
for(var i=0;i<current.length;i++){
current[i].visited = true;
}
// if(cluster.toggle===1&&pathfind.toggle!==1&&pathfind2.toggle!==1&&pathfind3.toggle!==1&&pathfind4.toggle!==1){
// for(var i=0;i<grid.length;i++){
// grid[i].track();
// if(grid[i].outer_tile){
// grid[i].fill();
// }}}
};
// grid.histscore is called before and populates each cell with a score. The function checks if a neighbouring cell contains the same id, and if that neighbouring cell has an open wall with the cell in question. If yes the cell score is updated to 1. A binary figure is the only thing thats required because here were just checking how many open ends each path has. On toggle 2 the program will select a random wall shared with a neighbour with a different id, remember that as a "current" cell moves it imprints its own id onto the new cell. "Note if you wanted yo know the original id this is stored in this.original_id". So now some "currents" once returned to their starting position have an open wall, we now need another open wall to guarantee that the maze is completable. To accomplish this we step through the history array. A 2d array which stores all the histories of the "currents" and total the scores in each path. For a path to be open it should have a score of at least 2, meaning its connected to two other paths of different id's. If the score therefore is less than two we return true.
function checkhist(a){
var score =0;
var score2 = [];
var result = true;
for(var i=0;i<a.length;i++){
if(a[i].score2 >=2){
result = false;
}
}
return result;
};
// This function is used to check if neighbouring cells have the same id. It takes an array and returns the neighbouring cell along with the original cell which produced the neighbour, this information can then be passed into the remove wall function.
function randomNth2(a){
var t = [];
for(var i=0;i<a.length;i++){
var next = a[i].nthPass();
if(next){
t.push([a[i],next]);
}
}
if(t.length>0){
var r = round(random(0,t.length-1));
t[r][0].nthPass();
t[r][0].score = 1;
t[r][1].score = 1;
// t[r][0].score2 ++;
// t[r][1].score2 ++;
var a = t[r][0].neighbour_cluster.includes(t[r][1].id);
var b = t[r][1].neighbour_cluster.includes(t[r][0].id);
if(!a||t[r][0].neighbour_cluster.length===0){
t[r][0].neighbour_cluster.push(t[r][1].id)
t[r][1].score2 = 2;
}
//}
//for(var i=0;i<t[r][1].neighbour_cluster.length;i++){
if(!b||t[r][1].neighbour_cluster.length===0){
t[r][1].neighbour_cluster.push(t[r][0].id)
t[r][0].score2 = 2;
}
//}
neighbouringCluster(t[r][0]);
neighbouringCluster(t[r][1]);
return t[r];
}else{return false;}
};
function randomNth(a){
var t = [];
for(var i=0;i<a.length;i++){
var next = a[i].nthPass();
if(next){
t.push([a[i],next]);
}
}
if(t.length>0){
var r = round(random(0,t.length-1));
t[r][0].nthPass();
t[r][0].score = 1;
t[r][1].score = 1;
// t[r][0].score2 ++;
// t[r][1].score2 ++;
if(!t[r][0].neighbour_cluster[0]){
t[r][0].neighbour_cluster[0] = t[r][1].id;
if(!t[r][1].neighbour_cluster[0]){
t[r][1].neighbour_cluster[0] = t[r][0].id;
}
else{
t[r][1].neighbour_cluster[1] = t[r][0].id;
t[r][1].score2 = 2;
}
}
else{
t[r][0].neighbour_cluster[1] = t[r][1].id;
t[r][0].score2 = 2;
if(!t[r][1].neighbour_cluster[0]){
t[r][1].neighbour_cluster[0] = t[r][0].id;
}
else{
t[r][1].neighbour_cluster[1] = t[r][0].id;
t[r][1].score2 = 2;
}
}
neighbouringCluster(t[r][0]);
neighbouringCluster(t[r][1]);
return t[r];
}else{return false;}
};
function neighbouringCluster(a){
for(var i=0;i<grid.length;i++){
if(grid[i].id===a.id){
grid[i].neighbour_cluster = a.neighbour_cluster;
grid[i].score2 = a.score2;
}
}
};
function trimStack(a,b){
for(var i=a.length-1;i>-1;i--){
var next = a[i].checkNeighbors();
if(!next&&a[i]!==b){
a.splice(i,1);
}}
};
function trimStackStar(a,b){
for(var i=a.length-1;i>-1;i--){
var next = a[i].neighbourNode(false,b);
if(!next){
a.splice(i,1);
}}
};
function loadinga(){
var a=0,b=50;
if(start.toggle===1&&!!!paths_explored){
for (var i = 0; i < grid.length; i++) {
if(grid[i].visited){
a++;
}
}
t +=10;
b+=sin(t)/5;
fill(255);
textSize(50);
text(round(a/grid.length*100)/4*3+"%",1160/2+100,600/2);
a = map(a,0,grid.length,0,150);
fill(0)
rect(1160/2-150,600/2,300,10);
fill(255)
rect(1160/2-150,600/2,a,10);
textSize(b);
fill(255,0,255,30);
stroke(0);
strokeWeight(10);
noStroke();
rect(1160/2-490,600/2-200,800,400);
fill(0,0,0,150);
stroke(0);
strokeWeight(5);
text("Loading...",1160/2-190,600/2);
textSize(12);
}
};
function turns(){
//frameRate(60);
if(reset.toggle===1){
findnodes.toggle = 0;
start.toggle=0
switches.toggle=0
game.toggle=0;
generateNodes.toggle = 0;
pathfind.toggle=0;
}
if(start.toggle===1&&!!!paths_explored){
loadinga();
for (var i = 0; i < current.length; i++) {
var a = start_array[i].original_id;
var next = current[i].checkNeighbors();
current[i].visited = true;
current[i].currentid = start_array[i].currentid;
if (next) {
//current[i].highlight();
next.id=current[i].id;
next.currentid = start_array[i].currentid;
next.pcol=current[i].pcol;
next.visited = true;
//next.myfootprint[a] = true;
// STEP 2
stack[i].push(current[i]);
hist[i].push(current[i]);
// STEP 3
removeWalls(current[i], next);
// STEP 4
current[i] = next;
trimStack(stack[i],start_array[i]);
}
else if (stack[i].length > 0) {
current[i] = stack[i].pop();
}
}
}
//Histscore assigns a score to each cell based on its id. if it has an open wall shared with a neighbour of a different id, then it assigns a score of one. As the history is a two dimentional array checkhist is used to check each checking cell history path. It totals the scores of the history and if the score is less than 2 it returns true. If score equals 0, then that section of the maze is completely self contained and therefore inaccessible. if it is equal to one it means that it only has one open path. If there are to many sections with only one open path they can create sections of the maze which is shared by another with only one open path and again becaome self contained and inaccessible. So we need a score of 2 or more.
if(grid_Cleared){
var temp =[];
for (var i=hist.length-1;i>-1;i--) {
var a = checkhist(hist[i]);
if(a){
temp.push(hist[i]);
//hist[i].pop();
}}
// The checking cell histories with a score of less than 2 are pushed into the temp array. RandomNth is used to pick a random cell with a neighbour of a different id, and removeWalls is used to effect the removal of the wall. Temp is cleared to disable a loop.
for (var i=temp.length-1;i>-1; i--){
var pickCell = randomNth(temp[i]);
if(pickCell){
removeWalls(pickCell[0],pickCell[1]);
}
else{
temp.pop();
}
}
if(temp.length===0){
paths_explored = true;
}
}
gridbackup = grid;
};
function cellNeighbours (){
if(reset.toggle===1){
start.toggle=0;
switches.toggle=0;
paths.toggle=0;
game.toggle=0;
pathfind.toggle=0;
pathfind2.toggle=0;
pathfind3.toggle=0;
pathfind4.toggle=0;
cluster.toggle=0;
}
if(nodes_array_Created&&!cluster_complete){
for (var i = 0; i < current.length; i++) {
if(current[i].id!==current[i].original_id){
current[i].id = current[i].nid;
current[i].original_id = current[i].nid;
}
total_cluster++
trackstack[i] = [];
for (var j = 0; j < grid.length; j++) {
if(grid[j].id===current[i].id){
cluster_total ++;
clusters[i].push(grid[j]);
grid[j].clustered = true;
// fill(255)
// text("hello",10,10);
if(grid[j].outer_tile&&grid[j].total===1){
trackbuilder[i] = grid[j];
trackbackup[i] = grid[j];
}}}}
for(var i=0;i<trackbuilder.length;i++){
if(!trackbuilder[i]){
trackbuilder[i] = current[i];
trackbackup[i] = current[i];
}}
}
if(grid_count===grid.length){
grid_Cleared = true;
}
if(nodes_connected){
calculate_distance = true;
}
if(paths_evaluated >= total_paths&&nodes_array_Created){
paths_completed = true;
}
if(nodes_count===grid.length){
nodes_connected = true;
}
if(calculate_distance&&!nodes_array_Created){
generateNodes.toggle=1;
}
for (var i = 0; i < grid.length; i++) {
if(grid[i].clustered&&!cluster_complete){
cluster_count++;
if(cluster_count>=grid.length&&total_cluster>=current.length){
cluster_complete = true;
}}
if(pathfind2.toggle!==1&&pathfind.toggle!==1){
grid[i].findnode();
grid[i].path_to_node();
grid[i].connectallnodes();
}
if(grid[i].visited2||grid[i].verified&&!grid[i].visited3){
grid[i].fill();
grid[i].dd();
}
if(grid_Cleared){
grid[i].histScore();
}
if(start.toggle===1||game.toggle===1||switches.toggle===1){
grid[i].info();
grid[i].hover();
grid[i].toggle();
}
if((grid_Cleared&&start.toggle===1||game.toggle===1||switches.toggle===1)&&!nodes_array_Created){
grid[i].check_node_info();
grid[i].track();
}
if(grid_Cleared&&start.toggle===1||game.toggle===1||switches.toggle===1){
grid[i].complete_track();
grid[i].hover_cluster();
}
if(switches.toggle===1){
grid[i].draw(sides);
}
if(grid[i].node&&grid[i].parent.length>0){
grid[i].parent = [];
}
if(grid[i].visited&&!grid[i].counted){
grid_count++;
grid[i].counted = true;
}
if(grid[i].connected&&!grid[i].neighbour_counted&&grid_Cleared){
nodes_count++;
grid[i].neighbour_counted = true;
}
if(switches.toggle===1){
grid[i].draw(sides);
}}
if(x_Padding.value&&x_Padding.toggle===1){
x_padding = x_Padding.value;
}
if(y_Padding.value&&y_Padding.toggle===1){
y_padding = y_Padding.value;
}
};
function trimpathStack(a,b){
for(var i=a.length-1;i>-1;i--){
var next = a[i].find(target);
if(next){
a[i].pcol2 = color(0,0,0,150)
return;
}
else if(!next&&a.length>1){
a[i].visited3 = true;
a[i].pcol2 = color(0,0,0,0);
a.splice(i,1);
}}
};
function djikstra(){
if(pathfind.toggle===1&&target&&pathfinder[0]){
target.visited = true;
target.pcol2 = color(255,0,0);
target.fill();
for( var i=pathfinder.length-1;i>-1;i--){
pathbackup[0].pcol2 = color(0);
pathfinder[i].pcol2 = color(0,0,0,150);
stroke(0);
//strokeWeight(1);
//line(pathfinder[i].x,pathfinder[i].y,target.x,target.y);
strokeWeight(0);
if(pathfinder[i]&&(pathfinder[i].y!==target.y||pathfinder[i].x!==target.x)&&!completed){
searching = true;
var next = pathfinder[i].find(target,false);
pathfinder[i].visited2 = true;
if(next){
next.pcol2 = color(0,0,0,150);
next.depth = pathfinder[i].depth;
pathfinder[i].highlight();
pathfinder[i].visited3 = false;
stack2[i].push(pathfinder[i]);
pathhist[i].push(pathfinder[i])
pathfinder[i] = next;
}
else if(stack2[i].length>0){
var next1 = pathfinder[i].find(target,false);
if(next1){
if(!pathfinder[i].visited3){
pathfinder[i].pcol2 = color(0,0,0,150);
}
else{
pathfinder[i].pcol2 = pathfinder[i].pcol;
}}
else {
pathfinder[i].visited3 = true;
pathfinder[i].pcol2 = color(0,0,0,0);
}
trimpathStack(stack2[i]);
pathfinder[i] = stack2[i].pop();
}
}
else{
fill(255);
text("success",10,10);
searching = false;
pathfinder[i].completed = true;
completed = true;
}
if(pathfinder[i].completed){
}
if(completed&&!pathfinder[i].completed){
}
}}
};
function trimpathStack2(a){
for(var i=a.length-1;i>-1;i--){
var next = false;
if(a[i].node){
next = a[i].nextNode();
}
if(!a[i].node){
next = a[i].findnextNode(false);
}
if(next){
a[i].pcol2 = color(0,0,0,150)
return;
}
else if(!next&&a.length>1){
a[i].visited3 = true;
a[i].pcol2 = color(0,0,0,0);
a.splice(i,1);
}}
};
function mapPath2(){
if(pathfind2.toggle===1){
searching = true;
target.verified = true;
target.pcol2 = color(255,0,0);
target.fill();
for( var i=pathfinder.length-1;i>-1;i--){
pathbackup[0].pcol2 = color(0);
pathfinder[i].pcol2 = color(255,255,255,150);
stroke(0);
strokeWeight(1);
//line(pathfinder[i].x,pathfinder[i].y,target.x,target.y);
strokeWeight(0);
var next;
if(pathfinder[i]!==target&&!completed){
if(!pathfinder[i].node){
next = pathfinder[i].findnextNode(true,stack2[i],pathhist[i]);
}
else if(pathfinder[i].node){
next = pathfinder[i].nextNode(true,false,stack2[i],pathhist[i]);
}
pathfinder[i].visited2 = true;
if(next){
next.pcol2 = color(0,0,0,150);
next.depth = pathfinder[i].depth;
pathfinder[i].highlight();
pathfinder[i] = next;
}
else if(stack2[i].length>0){
var next;
if(!pathfinder[i].node){
next1 = pathfinder[i].findnextNode(false);
}
else if(pathfinder[i].node){
next1 = pathfinder[i].nextNode();
}
if(next1){
if(!pathfinder[i].visited3){
pathfinder[i].pcol2 = color(0,0,0,0);
}
else{
pathfinder[i].pcol2 = pathfinder[i].pcol;
}}
else {
pathfinder[i].visited3 = true;
pathfinder[i].pcol2 = color(0,0,0,150);
}
trimpathStack2(stack2[i]);
pathfinder[i] = stack2[i].pop();
}
}
else{
fill(255);
text("success",10,10);
pathfinder[i].completed = true;
searching = false;
completed = true;
}
if(pathfinder[i].completed){
}
if(completed&&!pathfinder[i].completed){
}
}}
};
function trimpathStack3(a){
for(var i=a.length-1;i>-1;i--){
var next;
if(a[i].node){
next = a[i].nextNode();
}
if(!a[i].node){
next = a[i].findnextNode();
}
if(next){
return;
}
if(!next&&a.length>1){
//a[i].visited3 = true;
//a[i].pcol2 = color(51)
a.splice(i,1);
}}
};
function trimpathStack4(a){
var start = a[0].findstart();
var next;
for(var i=a.length-1;i>-1;i--){
var end = a[i].findend();
var end2 = a[i].check_path_complete();
if((end2&&start)||a[i].completed||a[0].completed){
next = true;
//console.log(a.length);
}}
if(next){
a[0].completed = true;
a[0].pcol2 = color(255,255,255,150);
// a[0].highlight();
for(var i=a.length-1;i>-1;i--){
a[i].completed = true;
a[i].pcol2 = color(255,255,255,100);
//a.splice(i,1);
}}
for(var i=a.length-1;i>-1;i--){
var b = a[i].check_path_complete();
if(b){
//a[i].completed = true;
//a[i].pcol2 = color(255,255,255,0);
}
}
};
function mapPath4(){
if(pathfind4.toggle===1&&!return_path&&target&&pathfinder[0]){
target.pcol2 = color(255,0,0);
target.target = true;
target.fill();
var toggle = true;
for(var i=pathfinder.length-1;i>-1;i--){
stroke(0);
strokeWeight(1);
//line(pathfinder[i].x,pathfinder[i].y,target.x,target.y);
strokeWeight(0);
var next;
if(pathfinder[i]!==target&&!completed){
pathbackup[0].pcol2 = color(255);
pathfinder[i].pcol2 = color(0,0,0,150);
if(!pathfinder[i].node){
next = pathfinder[i].findnextNode(toggle,stack2[i],pathhist[i]);
}
else if(pathfinder[i].node){
next = pathfinder[i].nextNode(toggle,toggle,stack2[i],pathhist[i]);
}
pathfinder[i].visited2 = true;
if(next){
pathfinder[i].highlight();
pathfinder[i] = next;
if(!toggle){
stack2[i].push(pathfinder[i]);
pathhist[i].push(pathfinder[i]);
}
}
else if(stack2[i].length>0&&!completed){
trimpathStack3(stack2[i]);
pathfinder[i] = stack2[i].pop();
}
else if(stack2[i].length===0&&!completed){
pathhist.splice(i,1);
stack2.splice(i,1);
pathfinder.splice(i,1);
}}
else if(pathfinder[i]===target&&stack2[i].length>0){
fill(255);
text("Success!!",10,10);
completed = true;
}
if(completed){
renode.toggle = 1;
target.completed = true;
}}}
};
function trimtrackstack(a,b){
for(var i=a.length-1;i>-1;i--){
var next = a[i].draw_track(target);
if(next){
//b--;
return;
}
else if(!next&&a.length>1){
b.ref_pos--;
a.splice(i,1);
}}
};
function build_track(){
if(cluster.toggle===1&&cluster_complete){
for(var i=trackbuilder.length-1;i>-1;i--){
trackbuilder[i].tracked = true;
trackbuilder[i].outline_pos = trackbackup[i].ref_pos;
var next = trackbuilder[i].draw_track(true);
trackbuilder[i].highlight();
if(next){
//trackbackup[i].ref_pos ++;
trackbuilder[i].highlight();
next.tracked = true;
trackbackup[i].ref_pos++;
next.outline_pos = trackbackup[i].ref_pos;
trackstack[i].push(trackbuilder[i]);
trackbuilder[i] = next;
}
else if(trackstack[i].length>0){
trimtrackstack(trackstack[i],trackbackup[i]);
trackbackup[i].ref_pos--;
trackbuilder[i] = trackstack[i].pop();
}
else{
track_count++;
}
}}
};
function returnpath(){
if(pathfind4.toggle===1&&return_path&&!returned){
target.pcol2 = color(255,0,0,255);
target.verified = true;
if(pathfinder[i]!==target&&!returned){
toggle = false;
for( var i=pathfinder.length-1;i>-1;i--){
pathbackup[1].pcol2 = color(255);
stroke(0);
strokeWeight(1);
//line(pathfinder[i].x,pathfinder[i].y,target.x,target.y);
strokeWeight(0);
var next;
if(pathfinder[i]!==target&&completed){
//pathfinder[i].pcol2 = color(255,255,255,150);
pathbackup[1].pcol2 = color(255,255,255,255);
if(!pathfinder[i].node){
next = pathfinder[i].findnextNode(toggle,stack2[i],pathhist[i]);
}
else if(pathfinder[i].node){
next = pathfinder[i].nextNode(toggle,toggle,stack2[i],pathhist[i]);
}
pathfinder[i].verified = true;
if(next){
pathfinder[i].highlight();
//console.log(pathfinder[i])
pathfinder[i] = next;
//pathbackup[1].pcol2 = color(0,0,255,255);
if(!toggle){
stack2[i].push(pathfinder[i]);
pathhist[i].push(pathfinder[i]);
}
}
else if(stack2[i].length>0&&!completed){
pathfinder[i] = stack2[i].pop();
}}
else if(pathfinder[i]===target&&stack2[i].length>0){
fill(255);
text("Success!!",10,10);
searching = false;
returned = true;
}
}}
}
};
function super_path(){
var a = 0;
var c = ((nodesindex.length*nodesindex.length)-nodesindex.length);
//var c = stackstar.length;
if(pathfind3.toggle===1){
for(var i=0;i<starhist.length;i++){
a+=starhist[i].length;
}
fill(255);
text(round(a)+"%",315,17);
text(round(c)+"%",315,37);
text(round(a/c*100)+"%",335,57);
//a = map(a,0,c,0,300);
fill(0)
rect(10,10,300,10);
fill(255)
//rect(10,10,a,10);
//frameRate(5);
for(var i=nodestar.length-1;i>-1;i--){
var b = backupstar[i].original_id;
nodestar[i].imprinted_by[b] = true;
var next = nodestar[i].neighbourNode(true,b,backupstar[i]);
if(next){
//a++;
nodestar[i].highlight();
next[1].node_to_node_dist[b] = nodestar[i].node_to_node_dist[b] + next[0];
backupstar[i].nodesconnected[next[1].original_id] = next[1].node_to_node_dist[b];
if(!nodestar[i].path_ref[b]){
nodestar[i].path_ref[b] = [nodestar[i]]
}
next[1].path_ref[b] = [nodestar[i].path_ref[b],next[1]];
next[1].path_ref[b] = next[1].path_ref[b].flat();
backupstar[i].pathtable[next[1].original_id] = [next[1].path_ref[b]];
stackstar[i].push(nodestar[i]);
starhist[i].push(nodestar[i]);
trimStackStar(stackstar[i],b)
nodestar[i] = next[1];
}
else if(!next&&stackstar[i].length>0){
nodestar[i] = stackstar[i].pop();
}
else{
// backupstar.splice(i,1);
// nodestar.splice(i,1);
// stackstar.splice(i,1);
//i += -1;
}}}
};
function checktoggle (){
for(var i=0;i<sliders.length;i++){
if(sliders[i].toggle===1){
findnodes.toggle =0;
}
}
};
var t = 0;
function loadingb(){
var a = paths_evaluated;
var cc,ch2,cw,ch;
fill(255,255,255,255);
var b = 50;
t +=5;
b+=sin(t)/5;
if(a){
ch2 = map(a,0,total_paths,0,50)/4;
textSize(50-ch2);
text(round(a/total_paths*100)/4+75+"%",1160/2+100,600/2);
}
a = map(a,0,total_paths,0,75);
cw = map(a,0,total_paths,0,30);
ch = map(a,0,total_paths,0,400);
cc = map(a,0,total_paths,0,125);
fill(0)
rect(1160/2-150,600/2,300,10);
fill(255)
rect(1160/2+75,600/2,a,10);
rect(1160/2-150,600/2,225,10);
textSize(b-ch2);
fill(255,0,255,30-cw);
stroke(0);
strokeWeight(10);
rectMode(CENTER)
noStroke();
rect(1160/2-90,600/2,800,400-ch2*32);
fill(0,0,0,150);
stroke(0,0,0,255-cc);
strokeWeight(5);
text("Loading...",1160/2-190+ch2,600/2);
textSize(12);
rectMode(CORNER);
}
function findnode(){
if(nodes_array_Created&&!paths_completed&&pathfind2.toggle!==1&&pathfind.toggle!==1){
while(node_check<nodesindex.length){
total_paths += nodesindex[node_check].neighbours.length;
node_check++;
}
loadingb();
//frameRate(5);
for(var i=0;i<nodesindex.length;i++){
var steps = key_Node[i].steps;
var b = key_Node[i].original_id;
nodesindex[i].connectedNodes[b] = true;
var next = nodesindex[i].neighbour_Node(b,steps);
find_node_count ++;
if(key_Node[i].nodes<key_Node[i].neighbours.length){
if(next){
//nodesindex[i].highlight();
nodestack[i].push(nodesindex[i]);
if(next.node){
next.connectedNodes[b]=steps;
var a = nodesindex[i].parent[0]
var b = nodesindex[i].parent[1]
if((a&&!b)){
var node_info = [];
for(var j=0;j<key_Node[i].node_info.length;j++){
if(!key_Node[i].node_info[j][0]&&node_info.length<1){
node_info = key_Node[i].node_info[j];
}
if(node_info.length>0){
node_info[0] = steps;
node_info[1] = next;
node_info[2] = nodestack[i];
}}
}
if(a === key_Node[i]){
var node_info = [];
for(var j=0;j<key_Node[i].node_info.length;j++){
if(!key_Node[i].node_info[j][0]&&node_info.length<1){
node_info = key_Node[i].node_info[j];
}
if(node_info.length>0){
node_info[0] = steps;
node_info[1] = next;
//node_info[2] = nodestack[i];
}}
}
else if(b === key_Node[i]){
var node_info = [];
for(var j=0;j<key_Node[i].node_info.length;j++){
if(!key_Node[i].node_info[j][0]&&node_info.length<1){
node_info = key_Node[i].node_info[j];
}
if(node_info.length>0){
node_info[0] = steps;
node_info[1] = next;
node_info[2] = nodestack[i];
}}
}
if(nodesindex[i].node){
key_Node[i].steps = 1;
var node_info= [];
for(var j=0;j<next.node_info.length;j++){
if(!key_Node[i].node_info[j][0]&&node_info.length<1){
node_info = key_Node[i].node_info[j];
}
if(node_info.length>0){
node_info[0] = steps;
node_info[1] = next;
//node_info[2] = nodestack[i];
}}}
paths_evaluated++;
key_Node[i].nodes ++;
nodestack[i] = [];
}
else {
key_Node[i].steps ++;
if(!next.node_info[0][1] ){
next.node_info[0][0] = steps;
next.node_info[0][1] = key_Node[i];
//next.node_info[0][2] = nodestack[i];
}
else{
next.node_info[1][0] = steps;
next.node_info[1][1] = key_Node[i];
//next.node_info[1][2] = nodestack[i];
}
key_Node[i].child.push([steps,next]);
nodesindex[i] = next;
//console.log(steps,b,nodestack[i])
}
}
else {
key_Node[i].steps = 1;
nodestack[i] = [];
nodesindex[i] = key_Node[i];
if(count>0){
key_Node[i].steps = 1;
}}}}}
};
var count;
function shortestPath(a,b){
var starting_Node ;
var finishing_Node
if(!a.node){
if(a.connections[0]<a.connections[1]){
starting_Node = a.connections[0];
}
else{
starting_Node = a.connections[1];
}}
if(!b.node){
if(b.connections[0]<b.connections[1]){
finishing_Node = b.connections[0];
}
else{
finishing_Node = b.connections[1];
}}
for(var i=0;i<nodesindex.length;i++){
}
};
function checkToggles(){
for(var i=0;i<sliders.length;i++){
if(sliders[i].toggle===1)
return true;
}
}
function draw(){
//if(mouseX>0&&mouseX<500){
checktoggle();
push();
translate(xmap,ymap)
background(51);
setups();
turns();
findnode();
cellNeighbours();
djikstra();
mapPath2();
super_path();
mapPath4();
build_track();
returnpath();
pop();
//----------------------------------------
minimize.draw3();
hideMenu();
game.draw();
start.draw();
paths.draw();
reset.draw();
pathfind.draw();
pathfind2.draw();
pathfind3.draw();
pathfind4.draw();
pathreset.draw2();
set_target.draw();
set_pathfinder.draw();
back.draw();
cluster.draw();
extract_circuit.draw();
switches.draw();
if(!mousedown){
reset.mouseClick2();
pathreset.mouseClick2();
generateNodes.mouseClick2();
menudrag.mouseClick2();
}
menu.mouseClick3();
menudrag.draw3();
renode.hidden();
if(!mousedown){
menudrag.mouseClick5();
}
for(var i=0;i<sliders.length;i++){
sliders[i].draw4();
sliders[i].slide();
sliders[i].buttonClick();
}
for(var i=0;i<buttons.length;i++){
if(!mousedown){
buttons[i].hover();
}}
//}
};
function mouseClicked(){
if(game.toggle===1||paths.toggle===1){
for(var i=0;i<grid.length;i++){
grid[i].toggle();
}}
};
function mouseOver(){
};
function mouseMoved(){
//loop()
};
function mouseIsPressed(){
};
function keyIsPressed(){
if(keyCode===16){
mousedown = true;
}
};
function mouseReleased(){
mousedown = false;
reset_toggle = false;
for(var i=0;i<sliders.length;i++){
sliders[i].toggle = 0;
}
};
function mouseDragged(){
if(mousedown&&(menudrag.toggle!==1&&slidersdrag.toggle!==1)&&!checkToggles()){
xmap += mouseX-pmouseX;
ymap += mouseY-pmouseY;
}
if(menudrag.toggle===1){
mousedown = true;
xx = mouseX-menuxy[0]-190/2;
yy = mouseY-menuxy[1];
}
if(slidersdrag.toggle===1){
mousedown = true;
xx = mouseX-menuxy[0]-190/2;
yy = mouseY-menuxy[1];
}
};
function mousePressed(){
if(!menu.checkmpos()){
mousedown = true;
}
game.mouseClick();
start.mouseClick();
paths.mouseClick();
//findnodes.mouseClick();
pathfind.mouseClick();
pathfind2.mouseClick();
pathfind3.mouseClick();
pathfind4.mouseClick();
set_target.mouseClick();
set_pathfinder.mouseClick();
//resetNode.mouseClick();
switches.mouseClick();
cluster.mouseClick();
extract_circuit.mouseClick();
back.mouseClick();
minimize.mouseClick4();
slidersminimize.mouseClick4();
//menudrag.mouseClick5();
for(var i=0;i<sliders.length;i++){
//sliders[i].buttonClick();
}
if(!mousedown){
reset.mouseClick2();
pathreset.mouseClick2();
//generateNodes.mouseClick2();
}
};