void Ocean::initCells(void) { addEmptyCells(); addObstacles(); addPredators(); addPrey(); displayStats(-1); displayBorder(); Ocean1 = this; }
// Draw the content of the scene void Scene::draw(bwindow& win) { int i,j; // Total number of agents unsigned int N = nb_prey+nb_hunt; // Preys eaten this step preys_eaten = 0; // Draw borders for(i = 0; i < nb_borders; i++) { int* bordPoints = new int [4]; bordPoints = borders[i].get_points(); win.draw_line(bordPoints[0], bordPoints[1], bordPoints[2], bordPoints[3], borders[i].get_color()); delete bordPoints; } // Draw obstacles for(j=0; j<nb_obstacles; j++) { for(i=0; i<360; i += round(300/obstacles[j].r)) { win.draw_point(round(obstacles[j].x +(obstacles[j].r)*cos(i*0.017453)), round(obstacles[j].y + (obstacles[j].r)*sin(i*0.017453)), 0x4D9E3A); } win.draw_point(obstacles[j].x, obstacles[j].y, 0x4D9E3A); } int count_deads = 0; // Compute new positions and count for(j=0; j<(N); j++) { // State of the agent (has eaten, is alive or dead) int state = agents[j]->get_state(); // If the agent is not dead if(state < 1) { if(state >= 0) { // Move the agent agents[j]->move(borders, nb_borders, agents,j,N, obstacles, nb_obstacles); } else { // Wait for the time to be spent agents[j]->wait(); } } else { if(state == 1) count_deads++; } } // Update position and draw for(j=0; j<(N); j++) { // State of the agent (has eaten, is alive or dead) int state = agents[j]->get_state(); // If the agent is not dead if(state < 1) { if(state >= 0) { // Move the agent agents[j]->update_pos(); } // Draw the new position win.draw_fsquare(round(agents[j]->get_x()-1),round(agents[j]->get_y()-1),round(agents[j]->get_x()+1),round(agents[j]->get_y()+1),agents[j]->get_color()); } } // Counters and texts // Enough to hold all numbers up to 64-bits char numstr[21]; sprintf(numstr, "%d", count_deads); std::string count_string = " preys eaten."; count_string = numstr + count_string; win.draw_text(40,40,0x0,count_string.c_str(),count_string.size()); sprintf(numstr, "%d", (int)round(hunt_count)); count_string = " dead hunters."; count_string = numstr + count_string; win.draw_text(DefVal::WINDOW_WIDTH-140,40,0x0,count_string.c_str(),count_string.size()); if((nb_hunt == (int)round(hunt_count)) || (nb_hunt == DefVal::NB_MAX_HUNT) || (nb_prey == DefVal::NB_MAX_PREY)) win.draw_text(DefVal::WINDOW_WIDTH-95, DefVal::WINDOW_HEIGHT-40,0xFF0000, "THE END!", strlen("THE END!")); // Prey generation double birthrate = (nb_prey-count_deads)*DefVal::MU*(1 - ((nb_prey-count_deads)/DefVal::NB_LIM_PREY)); int dn = round(prey_count+birthrate) - round(prey_count); if(dn) { for(i=0; i<dn; i++) addPrey(); } prey_count += birthrate; // Hunter decay double deathrate = DefVal::MU2*(nb_hunt - round(hunt_count)); if (deathrate < 0) deathrate = 0; int dp = round(hunt_count+deathrate) - round(hunt_count); if(dp) { for(i=0; i<dn; i++); { hunters_killed++; dead_hunters++; } } hunt_count += deathrate; // Hunter generation for(i = 0; i< preys_eaten; i++) { addHunter(); } }