Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cells on the edge of the grid causes a crash #13

Open
boydkr opened this issue Aug 18, 2020 · 3 comments
Open

Cells on the edge of the grid causes a crash #13

boydkr opened this issue Aug 18, 2020 · 3 comments

Comments

@boydkr
Copy link

boydkr commented Aug 18, 2020

java.lang.ArrayIndexOutOfBoundsException: 31
	at virus$Cell.pushOut(virus.java:1054)
	at virus$Cell.doAction(virus.java:911)
	at virus$Cell.iterate(virus.java:883)
	at virus.iterate(virus.java:185)
	at virus.draw(virus.java:116)
	at processing.core.PApplet.handleDraw(PApplet.java:2482)
	at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)

I was able to fix by setting all cells on the edge to empty (type 0) in getTypeFromXY

int getTypeFromXY(int preX, int preY){
  //set all cells on the edge to  emtpy
  if ( preX >= WORLD_SIZE - 1 || preY >= WORLD_SIZE - 1 ) {
    return 0;
  }
...
@HyperLan-git
Copy link

I mentionned that in my issue #11 the assumptions the program is making about the cell arrangements is unclear and makes experimenting with it 1000% harder.

@c00lum
Copy link

c00lum commented Aug 19, 2020

If this helps, I'm pretty sure this happens when running the "pushOut" function in Cell.pde, which ejects particles from inside the cell to outside, and is only ran when the cell runs the "REMOVE WASTE" or "REMOVE FOOD" codons. The cell checks if there are any obstacles in it's way before ejecting waste. These obstacles would be other cells or walls, and it checks them by looking in [x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1] indexes, which is the equivalent to right, left, up, and down. However, when a cell is against the wall and tries to eject a particle, it will check if a cell is in a position which is out of bounds of the cells array. This causes a ArrayIndexOutOfBoundsException error. A solution is to check if the x and y the cell is checking is bigger than the dimensions of the cells array, or the world size (or is negitive). Hopes this gets fixed!

@c00lum
Copy link

c00lum commented Aug 19, 2020

Just fixed it! Let me know if it doesn't work, thanks!

Replace pushOut function in Cell.pde with this,

public void pushOut(Particle waste){
    int[][] dire = {{0,1},{0,-1},{1,0},{-1,0}};
    boolean canPushOut = false;
    for(int i = 0; i < 4; i++) {
      if(!(y+dire[i][1] > WORLD_SIZE - 1 || y+dire[i][1] < 0 || x+dire[i][0] > WORLD_SIZE - 1 || x+dire[i][0] < 0 || cells[y+dire[i][1]][x+dire[i][0]].type != 0)) {
        canPushOut = true;
      }
    }
    if(canPushOut) {
      int chosen = -1;
      while(chosen == -1 || y+dire[chosen][1] > WORLD_SIZE - 1 || y+dire[chosen][1] < 0 || x+dire[chosen][0] > WORLD_SIZE - 1 || x+dire[chosen][0] < 0 || cells[y+dire[chosen][1]][x+dire[chosen][0]].type != 0){
        chosen = (int)random(0,4);
      }
    double[] oldCoor = waste.copyCoor();
    for(int dim = 0; dim < 2; dim++){
      if(dire[chosen][dim] == -1){
        waste.coor[dim] = Math.floor(waste.coor[dim])-EPS;
        waste.velo[dim] = -Math.abs(waste.velo[dim]);
      }else if(dire[chosen][dim] == 1){
        waste.coor[dim] = Math.ceil(waste.coor[dim])+EPS;
        waste.velo[dim] = Math.abs(waste.velo[dim]);
      }
      waste.loopCoor(dim);
    }
    Cell p_cell = getCellAt(oldCoor,true);
    p_cell.removeParticleFromCell(waste);
    Cell n_cell = getCellAt(waste.coor,true);
    n_cell.addParticleToCell(waste);
    laserT = frameCount;
    laserTarget = waste;
    }
    else{
      println("i'm stuck");
    }
  }

basti564 pushed a commit to basti564/VirusGame that referenced this issue Sep 11, 2020
- fixed pushOut function
- cells can now be placed anywere
- the game won't crash anymore if a cell is surrounded by other cells

bugfix by [c00lum](carykh#13 (comment))

Co-Authored-By: c00lum <69761240+c00lum@users.noreply.github.com>
j0912345 added a commit to j0912345/VirusGame that referenced this issue Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants