Subversion Repositories shark

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <drivers/glib.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "include/simcity.h"
#include "include/misc.h"
#include "include/proc.h"

int add() {
  if(maxc>0 && maxc<MAX_CAR-1) {
    busy_n->next=free_o;
    busy_n=free_o;
    free_o=free_o->next;
    busy_n->next=NULL;
  }
  if(maxc==0) {
    busy_n=busy_o=free_o;
    free_o=free_o->next;
    busy_n->next=NULL;
  }
  if(maxc==MAX_CAR-1) {
    busy_n->next=free_o;
    busy_n=free_o;
    free_o=free_n=NULL;
    busy_n->next=NULL;
  }
  if(maxc<MAX_CAR) {
    maxc++;
    return(busy_n->number);
  } else
    return(-1);
}

void del(int num) {
  car_data *d,*db;

  db=d=busy_o;
  if(maxc>1) {
    while((d->number)!=num) {
      db=d;
      d=d->next;
    }
    if(d==busy_n) {
      db->next=NULL;
      busy_n=db;
    } else if(d==busy_o) {
      busy_o=busy_o->next;
    } else {
      db->next=d->next;
    }
    if(maxc!=MAX_CAR) {
      free_n->next=d;
      free_n=d;
      free_n->next=NULL;
    }
    else {
      free_n=free_o=d;
    }
  }
  else {
    free_n->next=d;
    free_n=d;
    free_n->next=NULL;
    busy_n=busy_o=NULL;
  }
  maxc--;
}

void init_struct() {
  int i;

  free_o=&(car_data_array[0]);
  free_n=&(car_data_array[MAX_CAR-1]);
  busy_n=busy_o=NULL;
  for(i=0;i<MAX_CAR;i++)
    car_data_array[i].number=i;
  for(i=0;i<MAX_CAR;i++) {
    if(i<MAX_CAR-1)
      car_data_array[i].next=&(car_data_array[i+1]);
    else
      car_data_array[i].next=NULL;
  }
  maxc=0;
}

int del_o() {

  if(maxc<MAX_CAR && maxc>1) {
    free_n->next=busy_o;
    free_n=busy_o;
    busy_o=busy_o->next;
    free_n->next=NULL;
  } else
  if(maxc==MAX_CAR) {
    free_n=free_o=busy_o;
    busy_o=busy_o->next;
    free_n->next=NULL;
  } else
  if(maxc==1) {
    free_n->next=busy_o;
    free_n=busy_o;
    free_n->next=NULL;
    busy_o=busy_n=NULL;
  }
  if(maxc>0) {
    maxc--;
    return(free_n->number);
  } else
    return -1;
}

void gauge_create(char *name,int num) {
  void *par;
  PID pid;
  SOFT_TASK_MODEL stm;

  soft_task_default_model(stm);
  soft_task_def_level(stm,1);
  soft_task_def_ctrl_jet(stm);
  par=(void *) num;
  soft_task_def_arg(stm,par);
  soft_task_def_met(stm,GAUGE_WCET);
  soft_task_def_period(stm,PERIOD_CAR);
  soft_task_def_usemath(stm);
  pid = task_create(name, gauge_c, &stm, NULL);
  if (pid == NIL) {
    grx_close();
    perror("stl_create():Could not create task <Gauge_c>");
    sys_abort(1);
  }
  g_table[num]=pid;
  task_activate(pid);
}


void arrow_create(char *name,int num) {
  void *par;
  PID pid;
  SOFT_TASK_MODEL stm;

  soft_task_default_model(stm);
  soft_task_def_level(stm,1);
  soft_task_def_ctrl_jet(stm);
  par=(void *) num;
  soft_task_def_arg(stm,par);
  soft_task_def_met(stm,ARROW_WCET);
  soft_task_def_period(stm,PERIOD_CAR*4);
  pid = task_create(name,blink_arrow, &stm, NULL);
  if (pid == NULL) {
    grx_close();
    perror("stl_create():Could not create task <arrow>");
    sys_abort(1);
  }
  a_table[num]=pid;
  task_activate(pid);
}

void killer_create() {
  void *par;
  PID pid;
  SOFT_TASK_MODEL stm;

  soft_task_default_model(stm);
  soft_task_def_level(stm,1);
  soft_task_def_ctrl_jet(stm);
  par=(void *) 0;
  soft_task_def_arg(stm,par);
  soft_task_def_met(stm,1000);
  soft_task_def_period(stm,5*SECOND);
  pid = task_create("killer", killer, &stm, NULL);
  if (pid == NULL) {
    grx_close();
    perror("ref_create():Could not create task <killer>");
    sys_abort(1);
  }
  task_activate(pid);
}


void ref_create() {
  void *par;
  PID pid;
  SOFT_TASK_MODEL stm;

  soft_task_default_model(stm);
  soft_task_def_level(stm,1);
  soft_task_def_ctrl_jet(stm);
  par=(void *) 0;
  soft_task_def_arg(stm,par);
  soft_task_def_met(stm,3000);
  soft_task_def_period(stm,4*SECOND);
  pid = task_create("Refresher", refresher, &stm, NULL);
  if (pid == NULL) {
    grx_close();
    perror("ref_create():Could not create task <refresher>");
    sys_abort(1);
  }
  task_activate(pid);
}

void cam_create(char *name,int num) {
  void *par;
  PID pid;
  SOFT_TASK_MODEL stm;

  soft_task_default_model(stm);
  soft_task_def_level(stm,1);
  soft_task_def_ctrl_jet(stm);
  par=(void *) num;
  soft_task_def_arg(stm,par);
  soft_task_def_met(stm,CAM_WCET);
  soft_task_def_period(stm,PERIOD_CAR);
  pid = task_create(name, camera, &stm, NULL);
  if (pid == NULL) {
    grx_close();
    perror("stl_create():Could not create task <Camera>");
    sys_abort(1);
  }
  c_table[num]=pid;
  task_activate(pid);
}

void stl_create(char *name,int num) {
  void *par;
  PID pid;
  SOFT_TASK_MODEL stm;

  soft_task_default_model(stm);
  soft_task_def_level(stm,1);
  soft_task_def_ctrl_jet(stm);
  strcpy(tl_data_array[num].tl_name,name);
  par=(void *) &(tl_data_array[num]);
  soft_task_def_arg(stm,par);
  soft_task_def_met(stm,TL_WCET);
  soft_task_def_period(stm,tl_data_array[num].period);
  pid = task_create(name, traffic_light, &stm, NULL);
  if (pid == NULL) {
    grx_close();
    perror("stl_create():Could not create task <Traffic Light>");
    sys_abort(1);
  }
  tl_data_array[num].pid=pid;
  task_activate(pid);
}

void h_create(char *name,int num) {
  void *par;
  PID pid;
  int start_point=0;
  short flag=0;
  HARD_TASK_MODEL htm;
  car_data cd,*d;

#ifdef POS_RAND
  start_point=rand()%S_POINT;
  if(busy_o!=NULL)
    do {
      flag=0;
      d=busy_o;
      do {
        if(d->number!=num)
          if(module(starting_set_array[start_point].xpos,d->xp,starting_set_array[start_point].ypos,d->yp)<256) {
            start_point=(start_point+1)%S_POINT;
            flag=1;
            break;
          }
        d=d->next;
      } while(d!=NULL);
    } while(flag);
#endif
#ifndef POS_RAND
  start_point=OBL_START;
#endif

  cd.number=num;
  cd.xpos=cd.xp=starting_set_array[start_point].xpos;
  cd.ypos=cd.yp=starting_set_array[start_point].ypos;
  cd.angle=starting_set_array[start_point].angles;
  cd.speed=MIN_SPEED;
  cd.dist_obs=MAX_DIST;
  cd.collision=cd.boom=cd.correggi=cd.somma=0;
  cd.dir=STRAIGHT;
  car_data_array[num]=cd;
  hard_task_default_model(htm);
  hard_task_def_ctrl_jet(htm);
  par=(void *) &(car_data_array[num]);
  hard_task_def_arg(htm,par);
  hard_task_def_wcet(htm, CAR_WCET);
  hard_task_def_mit(htm,PERIOD_CAR);
  hard_task_def_usemath(htm);
  pid = task_create(name, car, &htm, NULL);
  if (pid == NULL) {
    grx_close();
    perror("h_create():Could not create task <hard car>");
    sys_abort(1);
  }
  else {
    p_table[num]=pid;
    sem_wait(&(kill_mutex[num]));
    kill_flag[num]=0;
    car_data_array[num].running=1;
    sem_post(&(kill_mutex[num]));
    task_activate(pid);
  }
}

void s_create(char *name,int num) {
  void *par;
  PID pid;
  int start_point=0;
  SOFT_TASK_MODEL stm;
  car_data cd,*d;
  short flag=0;

#ifdef POS_RAND
  start_point=rand()%S_POINT;
  if(busy_o!=NULL)
    do {
      flag=0;
      d=busy_o;
      do {
        if(d->number!=num)
          if(module(starting_set_array[start_point].xpos,d->xp,starting_set_array[start_point].ypos,d->yp)<256) {
            start_point=(start_point+1)%S_POINT;
            flag=1;
            break;
          }
        d=d->next;
      } while(d!=NULL);
    } while(flag);
#endif
#ifndef POS_RAND
  start_point=OBL_START;
#endif

  cd.number=num;
  cd.xpos=cd.xp=starting_set_array[start_point].xpos;
  cd.ypos=cd.yp=starting_set_array[start_point].ypos;
  cd.angle=starting_set_array[start_point].angles;
  cd.speed=MIN_SPEED;
  cd.dist_obs=MAX_DIST;
  cd.collision=cd.boom=cd.correggi=cd.somma=0;
  cd.dir=STRAIGHT;
  car_data_array[num]=cd;
  soft_task_default_model(stm);
  soft_task_def_level(stm,1);
  soft_task_def_ctrl_jet(stm);
  par=(void *) &(car_data_array[num]);;
  soft_task_def_arg(stm,par);
  soft_task_def_met(stm,SCAR_WCET);
  soft_task_def_period(stm,SPERIOD_CAR);
  soft_task_def_usemath(stm);
  pid = task_create(name, car, &stm, NULL);
  if (pid == NULL) {
    grx_close();
    perror("h_create():Could not create task <soft car>");
    sys_abort(1);
  }
  else {
    p_table[num]=pid;
    sem_wait(&(kill_mutex[num]));
    kill_flag[num]=0;
    car_data_array[num].running=1;
    sem_post(&(kill_mutex[num]));
    task_activate(pid);
  }
}

void killing(int num) {
  PID car_to_kill,cam_to_kill,gauge_to_kill,a_to_kill;
  int c;

  c=car_data_array[num].number*46;
  car_to_kill=p_table[num];
  cam_to_kill=c_table[num];
  gauge_to_kill=g_table[num];
  a_to_kill=a_table[num];
  sem_wait(&(kill_mutex[num]));
  kill_flag[num]=1;
  car_data_array[num].running=0;
  sem_post(&(kill_mutex[num]));
  task_kill(car_to_kill);
  task_kill(cam_to_kill);
  task_kill(gauge_to_kill);
  task_kill(a_to_kill);
  sem_wait(&mutex);
  grx_putimage(car_data_array[num].xp-15,car_data_array[num].yp-15,car_data_array[num].xp+14,car_data_array[num].yp+14,vbuf[car_data_array[num].number]);
  grx_putimage(CAMX-1,CAMY-1+c,CAMX+30,CAMY+30+c,clrcam);
  grx_putimage(CAMX+40-1,CAMY-1+c,CAMX+40+30,CAMY+c+30,clrcam);
  grx_putimage(CAMX+80-1,CAMY-1+c,CAMX+80+30,CAMY+c+30,clrcam);
  grx_text("    ",CAMX,CAMY+c+30,black,black);
  sem_post(&mutex);
}