import com.cycling74.max.*; public class gol extends MaxObject { private int[][] currentField = new int[600][600]; private int[][] futureField = new int[600][600]; private int R = 255; private int G = 0; private int B = 0; private int xpos = 0; private int ypos = 0; private int yprev; private int xprev; private int _xsize = 100; private int _ysize = 100; private int _squaresize = 8; private static final String[] INLET_ASSIST = new String[]{ "inlet 1 help" }; private static final String[] OUTLET_ASSIST = new String[]{ "outlet 1 help" }; public gol(int xsize, int ysize, int squaresize) { _xsize = xsize; _ysize = ysize; _squaresize = squaresize; declareInlets(new int[]{DataTypes.ALL,DataTypes.ALL,DataTypes.ALL,DataTypes.ALL,DataTypes.ALL,DataTypes.ALL}); declareOutlets(new int[]{DataTypes.ALL}); setInletAssist(new String[]{"Bang for next calc","Current R amount","Current G amount","Current B amount","XPOS of new cell","YPOS of new cell"}); setOutletAssist(OUTLET_ASSIST); } public void clear() { for(int i = 0; i < _ysize; i ++) { for(int j = 0; j < _xsize; j ++) { currentField[i][j] = 0; futureField[i][j] = 0; } } } public void bang() { for(int i = 0; i < _ysize; i ++) { for(int j = 0; j < _xsize; j ++) { futureField[i][j] = 0; } } // calculate a new field int neighbours = 0; for(int i = 0; i < (_ysize - 1); i ++) { for(int j = 0; j < (_xsize - 1); j ++) { neighbours = 0; if(i > 0 && j > 0) { if(currentField[i-1][j-1] > 0) {neighbours ++;} if(currentField[i-1][j] > 0) {neighbours ++;} if(currentField[i-1][j+1] > 0) {neighbours ++;} if(currentField[i][j-1] > 0) {neighbours ++;} if(currentField[i][j+1] > 0) {neighbours ++;} if(currentField[i+1][j-1] > 0) {neighbours ++;} } if(currentField[i+1][j] > 0) {neighbours ++;} if(currentField[i+1][j+1] > 0) {neighbours ++;} if(neighbours < 2) { futureField[i][j] = 0; if(currentField[i][j] != futureField [i][j]) { outlet(0,"setpixel",new Atom[]{ Atom.newAtom(j), Atom.newAtom(i), Atom.newAtom(0), Atom.newAtom(0), Atom.newAtom(0)});} } if(neighbours > 3) { futureField[i][j] = 0; if(currentField[i][j] != futureField [i][j]) { outlet(0,"setpixel",new Atom[]{ Atom.newAtom(j), Atom.newAtom(i), Atom.newAtom(0), Atom.newAtom(0), Atom.newAtom(0)});} } if((neighbours == 2 || neighbours == 3) && currentField[i][j] == 1) { futureField[i][j] = 1; if(currentField[i][j] != futureField [i][j]) { outlet(0,"setpixel",new Atom[]{ Atom.newAtom(j), Atom.newAtom(i), Atom.newAtom(R), Atom.newAtom(G), Atom.newAtom(B)});} } if(neighbours == 3 && currentField[i][j] == 0) { futureField[i][j] = 1; if(currentField[i][j] != futureField [i][j]) { outlet(0,"setpixel",new Atom[]{ Atom.newAtom(j), Atom.newAtom(i), Atom.newAtom(R), Atom.newAtom(G), Atom.newAtom(B)});} } } } for(int i = 0; i < _ysize; i ++) { for(int j = 0; j < _xsize; j ++) { currentField[i][j] = futureField[i][j]; } } } public void inlet(int i) { int inlet = 0; inlet = getInlet(); if(inlet == 1) {R = i;} if(inlet == 2) {G = i;} if(inlet == 3) {B = i;} if(inlet == 4) { if((xpos != xprev) || (ypos != yprev)) { xprev = xpos; xpos = i; if(currentField[ypos][xpos] == 0) { currentField[ypos][xpos] = 1; outlet(0,"setpixel",new Atom[]{ Atom.newAtom(xpos), Atom.newAtom(ypos), Atom.newAtom(R), Atom.newAtom(G), Atom.newAtom(B)}); } else if(currentField[ypos][xpos] == 1) { currentField[ypos][xpos] = 0; outlet(0,"setpixel",new Atom[]{ Atom.newAtom(xpos), Atom.newAtom(ypos), Atom.newAtom(0), Atom.newAtom(0), Atom.newAtom(0)}); } } } if(inlet == 5) { yprev = ypos; ypos = i; } } public void inlet(float f) { } public void list(Atom[] list) { } }