My "ai" keeps going to the bottom right

So last year I had a course in p5.js and I’m still experimenting with it this year, thought I’m a complete noob. I’ve made this little simulation which is meant to be part of a bigger project I’m working on in my free time. The purpose of it is to compare the differences between groups by giving them various traits and having them fight eachother to see which traits work better. Its inspired by a sebastian lague video. So far the teams balance their will to fight eachother or reproduce with their teamates to multiply their population.

The problem i’m facing is that after a few frames, all of the dots rush towards the bottom right and get stuck there. I’m wondering why this could be? again, I’m a total noob at this. Thanks!

let people=[];
let fr=0;
let pops=[{}]
let toGen=50

function preload() {
	redHeart=loadImage("redHeart.png")
	blueHeart=loadImage("blueheart.png")
	blueSword=loadImage("bluesword.png");
	redSword=loadImage("Sword.png");
}

function setup() {
	imageMode(CENTER);
	createCanvas(400, 400);
	background(100);
	frameRate(2);
	for (let i = 0; i < toGen; i = i + 1) {
		if (round(i/2)==i/2) { 
			people.length=people.length+1;
			people[people.length-1]={team:1,x:random(0,400),y:random(0,400),bloodlust:0,reprodurge:3, goal:-1, frmt:round(i/5)}
		} else {
			people.length=people.length+1;
			people[people.length-1]={team:2,x:random(0,400),y:random(0,400),bloodlust:0,reprodurge:3, goal:-1, frmt:round(i/5)}
		}
		
	}
	
				textAlign(CENTER);

}

function draw() {
background(100);
	fr=fr+1;
	if (fr>round(toGen/5)) {
		fr=0;
	}
	print(fr)
	for (let i = 0; i < people.length; i = i + 1) {
		if (people[i]!=null) {
			strokeWeight(10);
			if (people[i].team==1) {
				stroke(255,0,0);
				people[i].bloodlust=people[i].bloodlust+0.05;
				people[i].reprodurge=people[i].reprodurge+0.05;
			} else {
				stroke(0,0,255);
				people[i].bloodlust=people[i].bloodlust+0.05;
				people[i].reprodurge=people[i].reprodurge+0.05;
			}
			if (people[i]!=null && people[i].goal != -1 && people[people[i].goal]!=null) {
				if (people[i].team==1) {
				//red team
				if (people[people[i].goal].team != people[i].team) {
					//enemy
					image(redSword, people[i].x,people[i].y)
				} else {
					//ally
					image(redHeart, people[i].x,people[i].y)
				}
			} else {
				//blue team
				if (people[people[i].goal].team != people[i].team) {
					//enemy
					image(blueSword, people[i].x,people[i].y)
				} else {
					//ally
					image(blueHeart, people[i].x,people[i].y)
				}
			}
			} else if (people[i]!=null){
				if (people[i].team==1) {
					image(redHeart, people[i].x,people[i].y)
				} else {
					image(blueHeart, people[i].x,people[i].y)
				}
			}
			
			
			noStroke();
			textSize(10);
			fill(0);
			text("r: " + round(people[i].reprodurge), people[i].x,people[i].y-20)
			text("b: " + round(people[i].bloodlust), people[i].x,people[i].y-10)
			
			if (people[i].goal==-1 || fr==people[i].frmt) {
				let curClosest=0;
							for (let q = 0; q < people.length; q = q + 1) {
								if (people[curClosest]==null && people[q]!=null)
								{
									curClosest=q
									q=people.length
								}
							}
				if (people[i].reprodurge > people[i].bloodlust) {
					for (let z = 0; z < people.length; z = z + 1) {
						if (people[z]!=null) {
							if (people[i].team==people[z].team) {
								if (dist(people[curClosest].x, people[curClosest].y, people[i].x, people[i].y) > dist(people[z].x,people[z].y,people[i].x,people[i].y)) {
									curClosest=z
								}
							}
						}
					}
				} else {
					for (let z = 0; z < people.length; z = z + 1) {
						if (people[z]!=null) {
							if (people[i].team!=people[z].team) {
								if (dist(people[curClosest].x, people[curClosest].y, people[i].x, people[i].y) < dist(people[z].x,people[z].y,people[i].x,people[i].y)) {
									curClosest=z
								}
							}
						}
					}
				}
				people[i].goal=curClosest
			}
			
			if (people[people[i].goal] != null && people[i].x > people[people[i].goal].x) {
				if (people[i].x-5 >0)
				people[i].x=people[i].x-5;
			} else {
				if (people[i].x+5 <400)
				people[i].x=people[i].x+5;
			}
			
			if (people[people[i].goal] != null && people[i].y > people[people[i].goal].y) {
				if (people[i].x-5 >0)
				people[i].y=people[i].y-5;
			} else {
				if (people[i].y+5 <400)
				people[i].y=people[i].y+5;
			}
			
			if (people[people[i].goal] != null && dist(people[i].x,people[i].y,people[people[i].goal].x, people[people[i].goal].y) < 20) {
				if (people[people[i].goal].team == people[i].team) {
					//if reprod
					people.length=people.length+1;
					people[people.length-1]={team:people[i].team,x:people[i].x+10,y:people[i].y+10,bloodlust:0,reprodurge:0, goal:-1}
					people[i].reprodurge=0;
					people[i].goal=-1
				} else {
					//if bld
					people[people[i].goal]=null
					people[i].bloodlust=0;
					people[i].goal=-1;
				}
			}
			
		}
		
	//	if (people[i] != null && people[people[i].goal] !=null) {
	//		stroke(255,255,255);
	//		strokeWeight(1);
	//		line(people[i].x,people[i].y,people[people[i].goal].x,people[people[i].goal].y)
	//	}
		
		
	}
	let redPop=0;
	let bluePop=0;
	for (let i = 0; i < people.length; i = i + 1) {
		if (people[i]!=null) {
			if (people[i].team==1) {
				redPop=redPop+1;
			} else {
				bluePop=bluePop+1;
			}
		}
	}
	pops.length=pops.length+1;
	pops[pops.length-1]={red:redPop,blue:bluePop}
	for (let i = 1; i < pops.length; i = i + 1) {
			stroke(255,0,0)
			strokeWeight(1);
			line(i*3,pops[i-1].red*(-0.0068965517241379*toGen + 3.0689655172414),i*3+3,pops[i].red*(-0.0068965517241379*toGen + 3.0689655172414))
					 
					 stroke(0,0,255)
			strokeWeight(1);
			line(i*3,pops[i-1].blue*(-0.0068965517241379*toGen + 3.0689655172414),i*3+3,pops[i].blue*(-0.0068965517241379*toGen + 3.0689655172414))
		}
	
	print (redPop + ":r, b:" + bluePop);
	if (bluePop==2 || bluePop==1 || bluePop == 0) {
		textSize(64);
		fill(255,0,0);
		noStroke();
		text("Red Wins", 200, 200)
		
		noLoop();
	}
	if (redPop==2 || redPop==1 || redPop == 0) {
		textSize(64);
		fill(255,0,0);
		noStroke();
		text("Blue Wins", 200, 200)
		noLoop();
	}
	
	fill(255)
	rect(20,20,20,20);
}```

Missing { here …?

After )

Hi @Voleia,

Guess the second if should be y-5 not x…

Cheers
— mnse

1 Like

I tried it now

(I had to get rid of the images because I don’t have them)

I tried the section with and without else (to make it else if or just if),
with else if it is slightly better, but of course all items wander top left corner.

Question is what do you want to achieve in this section? Just keep items on screen? Then maybe plus + and minus - are swapped.

  • Tested with swapped plus + and minus -, didn’t work. See below
let people = [];
let fr = 0;
let pops = [{}];
let toGen = 50;

function preload() {}

function setup() {
  imageMode(CENTER);
  createCanvas(400, 400);
  background(100);
  frameRate(2);
  for (let i = 0; i < toGen; i = i + 1) {
    if (round(i / 2) == i / 2) {
      people.length = people.length + 1;
      people[people.length - 1] = {
        team: 1,
        x: random(0, 400),
        y: random(0, 400),
        bloodlust: 0,
        reprodurge: 3,
        goal: -1,
        frmt: round(i / 5),
      };
    } else {
      people.length = people.length + 1;
      people[people.length - 1] = {
        team: 2,
        x: random(0, 400),
        y: random(0, 400),
        bloodlust: 0,
        reprodurge: 3,
        goal: -1,
        frmt: round(i / 5),
      };
    }
  }

  textAlign(CENTER);
}

function draw() {
  background(100);
  fr = fr + 1;
  if (fr > round(toGen / 5)) {
    fr = 0;
  }
  print(fr);
  for (let i = 0; i < people.length; i = i + 1) {
    if (people[i] != null) {
      strokeWeight(10);
      if (people[i].team == 1) {
        stroke(255, 0, 0);
        people[i].bloodlust = people[i].bloodlust + 0.05;
        people[i].reprodurge = people[i].reprodurge + 0.05;
      } else {
        stroke(0, 0, 255);
        people[i].bloodlust = people[i].bloodlust + 0.05;
        people[i].reprodurge = people[i].reprodurge + 0.05;
      }
      if (
        people[i] != null &&
        people[i].goal != -1 &&
        people[people[i].goal] != null
      ) {
        if (people[i].team == 1) {
          //red team
          if (people[people[i].goal].team != people[i].team) {
            //enemy
            text("rs", people[i].x, people[i].y);
          } else {
            //ally
            text("rh", people[i].x, people[i].y);
          }
        } else {
          //blue team
          if (people[people[i].goal].team != people[i].team) {
            //enemy
            text("bs", people[i].x, people[i].y);
          } else {
            //ally
            text("bh", people[i].x, people[i].y);
          }
        }
      } else if (people[i] != null) {
        if (people[i].team == 1) {
          text("red", people[i].x, people[i].y);
        } else {
          text("bl", people[i].x, people[i].y);
        }
      }

      noStroke();
      textSize(10);
      fill(0);
      text("r: " + round(people[i].reprodurge), people[i].x, people[i].y - 20);
      text("b: " + round(people[i].bloodlust), people[i].x, people[i].y - 10);

      if (people[i].goal == -1 || fr == people[i].frmt) {
        let curClosest = 0;
        for (let q = 0; q < people.length; q = q + 1) {
          if (people[curClosest] == null && people[q] != null) {
            curClosest = q;
            q = people.length;
          }
        }
        if (people[i].reprodurge > people[i].bloodlust) {
          for (let z = 0; z < people.length; z = z + 1) {
            if (people[z] != null) {
              if (people[i].team == people[z].team) {
                if (
                  dist(
                    people[curClosest].x,
                    people[curClosest].y,
                    people[i].x,
                    people[i].y
                  ) > dist(people[z].x, people[z].y, people[i].x, people[i].y)
                ) {
                  curClosest = z;
                }
              }
            }
          }
        } else {
          for (let z = 0; z < people.length; z = z + 1) {
            if (people[z] != null) {
              if (people[i].team != people[z].team) {
                if (
                  dist(
                    people[curClosest].x,
                    people[curClosest].y,
                    people[i].x,
                    people[i].y
                  ) < dist(people[z].x, people[z].y, people[i].x, people[i].y)
                ) {
                  curClosest = z;
                }
              }
            }
          }
        }
        people[i].goal = curClosest;
      }

      // check X
      if (
        people[people[i].goal] != null &&
        people[i].x > people[people[i].goal].x
      ) {
        if (people[i].x - 5 > 0) people[i].x -= 5;
        else if (people[i].x + 5 < 400) people[i].x = people[i].x + 5;
      } // outer if

      // check Y
      if (
        people[people[i].goal] != null &&
        people[i].y > people[people[i].goal].y
      ) {
        if (people[i].y - 5 > 0) {
          people[i].y = people[i].y - 5;
        } else if (people[i].y + 5 < 400) {
          people[i].y = people[i].y + 5;
        }
      } //outer if

      if (
        people[people[i].goal] != null &&
        dist(
          people[i].x,
          people[i].y,
          people[people[i].goal].x,
          people[people[i].goal].y
        ) < 20
      ) {
        if (people[people[i].goal].team == people[i].team) {
          //if reprod
          people.length = people.length + 1;
          people[people.length - 1] = {
            team: people[i].team,
            x: people[i].x + 10,
            y: people[i].y + 10,
            bloodlust: 0,
            reprodurge: 0,
            goal: -1,
          };
          people[i].reprodurge = 0;
          people[i].goal = -1;
        } else {
          //if bld
          people[people[i].goal] = null;
          people[i].bloodlust = 0;
          people[i].goal = -1;
        }
      }
    }

    //  if (people[i] != null && people[people[i].goal] !=null) {
    //    stroke(255,255,255);
    //    strokeWeight(1);
    //    line(people[i].x,people[i].y,people[people[i].goal].x,people[people[i].goal].y)
    //  }
  }
  let redPop = 0;
  let bluePop = 0;
  for (let i = 0; i < people.length; i = i + 1) {
    if (people[i] != null) {
      if (people[i].team == 1) {
        redPop = redPop + 1;
      } else {
        bluePop = bluePop + 1;
      }
    }
  }
  pops.length = pops.length + 1;
  pops[pops.length - 1] = { red: redPop, blue: bluePop };
  for (let i = 1; i < pops.length; i = i + 1) {
    stroke(255, 0, 0);
    strokeWeight(1);
    line(
      i * 3,
      pops[i - 1].red * (-0.0068965517241379 * toGen + 3.0689655172414),
      i * 3 + 3,
      pops[i].red * (-0.0068965517241379 * toGen + 3.0689655172414)
    );

    stroke(0, 0, 255);
    strokeWeight(1);
    line(
      i * 3,
      pops[i - 1].blue * (-0.0068965517241379 * toGen + 3.0689655172414),
      i * 3 + 3,
      pops[i].blue * (-0.0068965517241379 * toGen + 3.0689655172414)
    );
  }

  print(redPop + ":r, b:" + bluePop);
  if (bluePop == 2 || bluePop == 1 || bluePop == 0) {
    textSize(64);
    fill(255, 0, 0);
    noStroke();
    text("Red Wins", 200, 200);

    noLoop();
  }
  if (redPop == 2 || redPop == 1 || redPop == 0) {
    textSize(64);
    fill(255, 0, 0);
    noStroke();
    text("Blue Wins", 200, 200);
    noLoop();
  }

  fill(255);
  rect(20, 20, 20, 20);
}
//


Tested with swapped plus + and minus -, didn’t work.

let people = [];
let fr = 0;
let pops = [{}];
let toGen = 50;

function preload() {}

function setup() {
  imageMode(CENTER);
  createCanvas(400, 400);
  background(100);
  frameRate(2);
  for (let i = 0; i < toGen; i = i + 1) {
    if (round(i / 2) == i / 2) {
      people.length = people.length + 1;
      people[people.length - 1] = {
        team: 1,
        x: random(0, 400),
        y: random(0, 400),
        bloodlust: 0,
        reprodurge: 3,
        goal: -1,
        frmt: round(i / 5),
      };
    } else {
      people.length = people.length + 1;
      people[people.length - 1] = {
        team: 2,
        x: random(0, 400),
        y: random(0, 400),
        bloodlust: 0,
        reprodurge: 3,
        goal: -1,
        frmt: round(i / 5),
      };
    }
  }

  textAlign(CENTER);
}

function draw() {
  background(100);
  fr = fr + 1;
  if (fr > round(toGen / 5)) {
    fr = 0;
  }
  print(fr);
  for (let i = 0; i < people.length; i = i + 1) {
    if (people[i] != null) {
      strokeWeight(10);
      if (people[i].team == 1) {
        stroke(255, 0, 0);
        people[i].bloodlust = people[i].bloodlust + 0.05;
        people[i].reprodurge = people[i].reprodurge + 0.05;
      } else {
        stroke(0, 0, 255);
        people[i].bloodlust = people[i].bloodlust + 0.05;
        people[i].reprodurge = people[i].reprodurge + 0.05;
      }
      if (
        people[i] != null &&
        people[i].goal != -1 &&
        people[people[i].goal] != null
      ) {
        if (people[i].team == 1) {
          //red team
          if (people[people[i].goal].team != people[i].team) {
            //enemy
            text("rs", people[i].x, people[i].y);
          } else {
            //ally
            text("rh", people[i].x, people[i].y);
          }
        } else {
          //blue team
          if (people[people[i].goal].team != people[i].team) {
            //enemy
            text("bs", people[i].x, people[i].y);
          } else {
            //ally
            text("bh", people[i].x, people[i].y);
          }
        }
      } else if (people[i] != null) {
        if (people[i].team == 1) {
          text("red", people[i].x, people[i].y);
        } else {
          text("bl", people[i].x, people[i].y);
        }
      }

      noStroke();
      textSize(10);
      fill(0);
      text("r: " + round(people[i].reprodurge), people[i].x, people[i].y - 20);
      text("b: " + round(people[i].bloodlust), people[i].x, people[i].y - 10);

      if (people[i].goal == -1 || fr == people[i].frmt) {
        let curClosest = 0;
        for (let q = 0; q < people.length; q = q + 1) {
          if (people[curClosest] == null && people[q] != null) {
            curClosest = q;
            q = people.length;
          }
        }
        if (people[i].reprodurge > people[i].bloodlust) {
          for (let z = 0; z < people.length; z = z + 1) {
            if (people[z] != null) {
              if (people[i].team == people[z].team) {
                if (
                  dist(
                    people[curClosest].x,
                    people[curClosest].y,
                    people[i].x,
                    people[i].y
                  ) > dist(people[z].x, people[z].y, people[i].x, people[i].y)
                ) {
                  curClosest = z;
                }
              }
            }
          }
        } else {
          for (let z = 0; z < people.length; z = z + 1) {
            if (people[z] != null) {
              if (people[i].team != people[z].team) {
                if (
                  dist(
                    people[curClosest].x,
                    people[curClosest].y,
                    people[i].x,
                    people[i].y
                  ) < dist(people[z].x, people[z].y, people[i].x, people[i].y)
                ) {
                  curClosest = z;
                }
              }
            }
          }
        }
        people[i].goal = curClosest;
      }

      /*
      // check X
      if (
        people[people[i].goal] != null &&
        people[i].x > people[people[i].goal].x
      ) {
        if (people[i].x - 5 < 5) people[i].x += 5;
        else if (people[i].x + 5 > 395) people[i].x -= 5;
      } // outer if

      // check Y
      if (
        people[people[i].goal] != null &&
        people[i].y > people[people[i].goal].y
      ) {
        if (people[i].y - 5 < 5) {
          people[i].y += 5;
        } else if (people[i].y + 5 > 395) {
          people[i].y -= 5;
        }
      } //outer if
*/

      if (
        people[people[i].goal] != null &&
        dist(
          people[i].x,
          people[i].y,
          people[people[i].goal].x,
          people[people[i].goal].y
        ) < 20
      ) {
        if (people[people[i].goal].team == people[i].team) {
          //if reprod
          people.length = people.length + 1;
          people[people.length - 1] = {
            team: people[i].team,
            x: people[i].x + 10,
            y: people[i].y + 10,
            bloodlust: 0,
            reprodurge: 0,
            goal: -1,
          };
          people[i].reprodurge = 0;
          people[i].goal = -1;
        } else {
          //if bld
          people[people[i].goal] = null;
          people[i].bloodlust = 0;
          people[i].goal = -1;
        }
      }
    }

    //  if (people[i] != null && people[people[i].goal] !=null) {
    //    stroke(255,255,255);
    //    strokeWeight(1);
    //    line(people[i].x,people[i].y,people[people[i].goal].x,people[people[i].goal].y)
    //  }
  }
  let redPop = 0;
  let bluePop = 0;
  for (let i = 0; i < people.length; i = i + 1) {
    if (people[i] != null) {
      if (people[i].team == 1) {
        redPop = redPop + 1;
      } else {
        bluePop = bluePop + 1;
      }
    }
  }
  pops.length = pops.length + 1;
  pops[pops.length - 1] = { red: redPop, blue: bluePop };
  for (let i = 1; i < pops.length; i = i + 1) {
    stroke(255, 0, 0);
    strokeWeight(1);
    line(
      i * 3,
      pops[i - 1].red * (-0.0068965517241379 * toGen + 3.0689655172414),
      i * 3 + 3,
      pops[i].red * (-0.0068965517241379 * toGen + 3.0689655172414)
    );

    stroke(0, 0, 255);
    strokeWeight(1);
    line(
      i * 3,
      pops[i - 1].blue * (-0.0068965517241379 * toGen + 3.0689655172414),
      i * 3 + 3,
      pops[i].blue * (-0.0068965517241379 * toGen + 3.0689655172414)
    );
  }

  print(redPop + ":r, b:" + bluePop);
  if (bluePop == 2 || bluePop == 1 || bluePop == 0) {
    textSize(64);
    fill(255, 0, 0);
    noStroke();
    text("Red Wins", 200, 200);

    noLoop();
  }
  if (redPop == 2 || redPop == 1 || redPop == 0) {
    textSize(64);
    fill(255, 0, 0);
    noStroke();
    text("Blue Wins", 200, 200);
    noLoop();
  }

  fill(255);
  rect(20, 20, 20, 20);
}
//