#include "field.h" #include #include //MY SORT function bool cellSort(Agent *p1, Agent *p2){ return (*p1).priority()>(*p2).priority(); }//end cellsort //HELPER FUNCTIONS Point Field::newLocation(int cur_X, int cur_Y, int moveTo){ Point result; if(moveTo == 0){//East result = Point(cur_X+1, cur_Y); }//end if else if(moveTo == 1){//NE result = Point(cur_X+1, cur_Y+1); }//end else if else if(moveTo == 2){//North result = Point(cur_X, cur_Y+1); }//end else if else if(moveTo == 3){//NW result = Point(cur_X-1, cur_Y+1); }//end else if else if(moveTo == 4){//West result = Point(cur_X-1, cur_Y); }//end else if else if(moveTo == 5){//SW result = Point(cur_X-1, cur_Y-1); }//end else if else if(moveTo == 6){//South result = Point(cur_X, cur_Y-1); }//end else if else if(moveTo == 7){//SE result = Point(cur_X+1, cur_Y-1); }//end else if else{ //error //cout << "Error moving to SAME place" < Field::getSurrounding(int x, int y){ //returns state of hight priority agent in each cell if no agent returns ' ' //if cell out of bounds return '*' //0 1 2 //3 4 //5 6 7 //using Dir //3 2 1 //4 0 //5 6 7 vector result(8); for(int z=0; z<8; z++){ if(z==0){ if(isInBound(newLocation(x,y, 3))){ Point pT = newLocation(x,y, 3); if(grid[pT.x][pT.y].size()==0){//can't call state on an empty space result[0]= ' '; }//end if else{ result[0] = grid[pT.x][pT.y][0]->state(); } }//end if else{ //must be out-of-bounds result[0] = '*'; }//end else }//end if else if(z==1){ if(isInBound(newLocation(x,y, 2))){ Point pT = newLocation(x,y, 2); if(grid[pT.x][pT.y].size()==0){//can't call state on an empty space result[1]= ' '; }//end if else{ result[1] = grid[pT.x][pT.y][0]->state(); } }//end if else{ //must be out-of-bounds result[1] = '*'; }//end else }//end else if else if(z==2){ if(isInBound(newLocation(x,y, 1))){ Point pT = newLocation(x,y, 1); if(grid[pT.x][pT.y].size()==0){//can't call state on an empty space result[2]= ' '; }//end if else{ result[2] = grid[pT.x][pT.y][0]->state(); } }//end if else{ //must be out-of-bounds result[2] = '*'; }//end else }//end else if else if(z==3){ if(isInBound(newLocation(x,y, 4))){ Point pT = newLocation(x,y, 4); if(grid[pT.x][pT.y].size()==0){//can't call state on an empty space result[3]= ' '; }//end if else{ result[3] = grid[pT.x][pT.y][0]->state(); } }//end if else{ //must be out-of-bounds result[3] = '*'; }//end else }//end else if else if(z==4){ if(isInBound(newLocation(x,y, 0))){ Point pT = newLocation(x,y, 0); if(grid[pT.x][pT.y].size()==0){//can't call state on an empty space result[4]= ' '; }//end if else{ result[4] = grid[pT.x][pT.y][0]->state(); } }//end if else{ //must be out-of-bounds result[4] = '*'; }//end else }//end else if else if(z==5){ if(isInBound(newLocation(x,y, 5))){ Point pT = newLocation(x,y, 5); if(grid[pT.x][pT.y].size()==0){//can't call state on an empty space result[5]= ' '; }//end if else{ result[5] = grid[pT.x][pT.y][0]->state(); } }//end if else{ //must be out-of-bounds result[5] = '*'; }//end else }//end else if else if(z==6){ if(isInBound(newLocation(x,y, 6))){ Point pT = newLocation(x,y, 6); if(grid[pT.x][pT.y].size()==0){//can't call state on an empty space result[6]= ' '; }//end if else{ result[6] = grid[pT.x][pT.y][0]->state(); } }//end if else{ //must be out-of-bounds result[6] = '*'; }//end else }//end else if else{ if(isInBound(newLocation(x,y, 7))){ Point pT = newLocation(x,y, 7); if(grid[pT.x][pT.y].size()==0){//can't call state on an empty space result[7]= ' '; }//end if else{ result[7] = grid[pT.x][pT.y][0]->state(); } }//end if else{ //must be out-of-bounds result[7] = '*'; }//end else }//end else }//end for return result; }//end getSurrounding vector Field::getLocalAgents(int x, int y){ //returns the states of all agents in the cell in order of priority vector result(grid[x][y].size()); for(int z=0; zstate(); }//end for return result; }//end getLocalAgents bool Field::isInBound(Point &p){ if(p.x>=0 && p.x=0 && p.y=0; y--){ for(int x=0; x(sizey)),gridSize(sizex, sizey){ // construct field with no agents of size sizex x sizey }//end field Field::~Field(){ // call delete on all pointers to agents stored in the field //loop through and delete all agents simalar to print code ecept deleting for(int y=gridSize.y-1; y>=0; y--){ for(int x=0; x=n_of_agents(p) return NULL pointer if( i<0 || i>=grid[p.x][p.y].size() ){ return NULL; //do noting }//end if else{ return grid[p.x][p.y][i]; }//end else }//end get_agent char Field::get_state(const Point &p) const{ // returns the state of agent with highest priority in cell p // or ' ' if there is no agent if(grid[p.x][p.y].size()==0){ return ' '; }//end if //assumn Cell is sorted by priority return grid[p.x][p.y][0]->state(); }//end get state long Field::n_of_agents() const{ // return the total number of agents currently on the field long counter = 0; for(int y=0; y=0 or i=0 && i > ALTgrid(gridSize.x, vector(gridSize.y)); vector to_del; vector actionData; //first int through the field and get needed stuff to call act() for(int y=0; y sur = getSurrounding(x,y); vector pis = getLocalAgents(x,y); actionData.push_back(ActData(sur,pis, zz, AG, Point(x,y))); }//end for }//end for }//end for for(int b=0; b sur,pos; // long index; // Agent* AG; // Point cur_loc; Point curLocal = actionData[b].cur_loc; Agent *AGNT = actionData[b].AG; ans = (*(actionData[b].AG)).act(actionData[b].sur, actionData[b].pos, actionData[b].index); action = ans.first; if(action==0){//STAY //add agent to the altField //altField.plant_agent(Point(x,y), AG); ALTgrid[curLocal.x][curLocal.y].push_back(AGNT); }//end if else if(action==1){//DIE to_del.push_back(toDelete(curLocal, actionData[b].index)); }//end else if else if(action==2){//MOVE //puts agent in it's new location returned by newLocation vector dirIn = ans.second; //altField.plant_agent(newLocation(x,y, dirIn[0]), AG); Point temp = newLocation(curLocal.x,curLocal.y, dirIn[0]); ALTgrid[temp.x][temp.y].push_back(AGNT); }//end else if else if(action==3){//CLONE vector dirIn = ans.second; //add all the clones to altField for(int zzz=0; zzz