/**
 * Fills the world with specified number of people and zombies.
 * The people are of different age; zombies are "brand new".
 */
void randomDistribution(WorldPtr world, int people, int zombies, simClock clock) {
	world->stats.clock = clock;
	world->stats.infectedFemales = 0;
	world->stats.infectedMales = 0;

	for (int i = 0; i < people;) {
		int x = randomInt(world->xStart, world->xEnd);
		int y = randomInt(world->yStart, world->yEnd);
		CellPtr cellPtr = GET_CELL_PTR(world, x, y);
		if (cellPtr->type != NONE) {
			continue;
		}

		newHuman(cellPtr, clock);
		if (cellPtr->gender == FEMALE) {
			world->stats.humanFemales++;
		} else {
			world->stats.humanMales++;
		}

		i++;
	}

	for (int i = 0; i < zombies;) {
		int x = randomInt(world->xStart, world->xEnd);
		int y = randomInt(world->yStart, world->yEnd);
		CellPtr cellPtr = GET_CELL_PTR(world, x, y);
		if (cellPtr->type != NONE) {
			continue;
		}

		newZombie(cellPtr, clock);
		world->stats.zombies++;

		i++;
	}
}
Exemple #2
0
void queuesimulator(FILE *queuefile){
	double queuetimer = 0.00;
	double arrivaltime;
	double servicetime;
	double epsilon = 0.0001;
	human inwaiting;

	int queuebuffer = 0;
	int thislength = 0;
	int maxlength = 0;
	double maxwait = 0.00;
	double thiswait;

	q queue = newQueue();
	
	do{
		//Decrements service time of first person, dequeue if finished
		if (queue->front != NULL){
			queue->front->containedperson->servicetime -= 0.01;
			if (queue->front->containedperson->servicetime < epsilon){
				human finished = dequeue(queue);
				//printf("Person arrived at %lf dequeued, waited %lf\n", finished->arrivaltime, finished->waitedtime);
				thiswait = finished->waitedtime;
				if (thiswait > maxwait)
					maxwait = thiswait;
				freeHuman(finished);
			}			
		}

		//Grab a person from the data ether, and make him wait until his time has come.
		if (queuebuffer == 0){
			if (!feof(queuefile)){
				fscanf(queuefile, "%lf %lf", &arrivaltime, &servicetime);
				inwaiting = newHuman(arrivaltime, servicetime);
				//printf("%lf %lf\n", arrivaltime, servicetime);
			}
			queuebuffer = 1;
		}
		//The etheral person's time has come! Place him in the queue.
		if (!feof(queuefile)){
			if (abs(queuetimer - inwaiting->arrivaltime) < epsilon){
				enqueue(queue, clone(inwaiting));
				freeHuman(inwaiting);
				queuebuffer = 0;
			}
		}
		//Increment waiting times for each cycle, and determine queue's length.
		//We start from hasasbehind to not count service time as waiting time.
		if(queue->front != NULL && queue->front->hasasbehind != NULL){
			for(qpos queuepos = queue->front->hasasbehind; queuepos != NULL; queuepos = queuepos->hasasbehind){
				queuepos->containedperson->waitedtime += 0.01;
				thislength += 1;
			}
			if (thislength + 1 > maxlength){
				maxlength = thislength + 1;
			}
			thislength = 0;

		}
		queuetimer += 0.01;
	} while (!feof(queuefile) || queue->front != NULL);
	printf("Maximum wait: %lf \nMaximum length: %d\n", maxwait, maxlength);
}