#include <stdio.h>
#include "csim.h"
#include <math.h>

FACILITY first_2person;
FACILITY xray1, xray2;
FACILITY last_2person;

int N, totalCustLeft, seed;
double TotalDelay;

/*prototypes*/
void passenger(int id);

sim(int argc, char *argv[]){
   N = 0;
   totalCustLeft = 0;
   TotalDelay = 0;
   if(argc != 2){
      printf("Usage: %s seed\n", argv[0]);
   }
   seed = atoi(argv[1]);
   create("sim");
   reseed(NIL, seed);
   first_2person = facility_ms("first_2person", 2);
   xray1 = facility("xray1");
   xray2 = facility("xray2");
   last_2person = facility_ms("last_2person", 2);
   while(simtime() <= (2.0 * 60 * 60)){
      N++;

      passenger(N);
      hold(normal(20.0, 3.0));
   }
   printf("Average response time = %lf\n\n", (TotalDelay/totalCustLeft));
}

void passenger(int id){
   double starttime;
   double endtime;
   create("passenger");
   starttime = simtime();
   use(first_2person, normal(12.0,1.0));
   if(qlength(xray1) < qlength(xray2)){
      use(xray1, normal(15.0,2.0));
   }
   else if(qlength(xray1) > qlength(xray2)){
      use(xray2, normal(15.0, 2.0));
   } else {
      if(uniform(0.0, 1.0) < 0.5){
         use(xray1, normal(15.0, 2.0));
      } else {
         use(xray2, normal(15.0, 2.0));
      }
   }
   if(uniform(0.0, 1.0) < 0.15){
      use(last_2person, normal(100.0, 10.0));
   }
   endtime = simtime();
   TotalDelay += endtime - starttime;
   totalCustLeft++;
}

