Subversion Repositories shark

Rev

Rev 1655 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1655 giacomo 1
#include <drivers/glib.h>
2
#include <math.h>
3
#include <stdlib.h>
4
#include <string.h>
5
#include "include/simcity.h"
6
#include "include/misc.h"
7
#include "include/proc.h"
8
 
9
int add() {
10
  if(maxc>0 && maxc<MAX_CAR-1) {
11
    busy_n->next=free_o;
12
    busy_n=free_o;
13
    free_o=free_o->next;
14
    busy_n->next=NULL;
15
  }
16
  if(maxc==0) {
17
    busy_n=busy_o=free_o;
18
    free_o=free_o->next;
19
    busy_n->next=NULL;
20
  }
21
  if(maxc==MAX_CAR-1) {
22
    busy_n->next=free_o;
23
    busy_n=free_o;
24
    free_o=free_n=NULL;
25
    busy_n->next=NULL;
26
  }
27
  if(maxc<MAX_CAR) {
28
    maxc++;
29
    return(busy_n->number);
30
  } else
31
    return(-1);
32
}
33
 
34
void del(int num) {
35
  car_data *d,*db;
36
 
37
  db=d=busy_o;
38
  if(maxc>1) {
39
    while((d->number)!=num) {
40
      db=d;
41
      d=d->next;
42
    }
43
    if(d==busy_n) {
44
      db->next=NULL;
45
      busy_n=db;
46
    } else if(d==busy_o) {
47
      busy_o=busy_o->next;
48
    } else {
49
      db->next=d->next;
50
    }
51
    if(maxc!=MAX_CAR) {
52
      free_n->next=d;
53
      free_n=d;
54
      free_n->next=NULL;
55
    }
56
    else {
57
      free_n=free_o=d;
58
    }
59
  }
60
  else {
61
    free_n->next=d;
62
    free_n=d;
63
    free_n->next=NULL;
64
    busy_n=busy_o=NULL;
65
  }
66
  maxc--;
67
}
68
 
69
void init_struct() {
70
  int i;
71
 
72
  free_o=&(car_data_array[0]);
73
  free_n=&(car_data_array[MAX_CAR-1]);
74
  busy_n=busy_o=NULL;
75
  for(i=0;i<MAX_CAR;i++)
76
    car_data_array[i].number=i;
77
  for(i=0;i<MAX_CAR;i++) {
78
    if(i<MAX_CAR-1)
79
      car_data_array[i].next=&(car_data_array[i+1]);
80
    else
81
      car_data_array[i].next=NULL;
82
  }
83
  maxc=0;
84
}
85
 
86
int del_o() {
87
 
88
  if(maxc<MAX_CAR && maxc>1) {
89
    free_n->next=busy_o;
90
    free_n=busy_o;
91
    busy_o=busy_o->next;
92
    free_n->next=NULL;
93
  } else
94
  if(maxc==MAX_CAR) {
95
    free_n=free_o=busy_o;
96
    busy_o=busy_o->next;
97
    free_n->next=NULL;
98
  } else
99
  if(maxc==1) {
100
    free_n->next=busy_o;
101
    free_n=busy_o;
102
    free_n->next=NULL;
103
    busy_o=busy_n=NULL;
104
  }
105
  if(maxc>0) {
106
    maxc--;
107
    return(free_n->number);
108
  } else
109
    return -1;
110
}
111
 
112
void gauge_create(char *name,int num) {
113
  void *par;
114
  PID pid;
115
  SOFT_TASK_MODEL stm;
116
 
117
  soft_task_default_model(stm);
118
  soft_task_def_level(stm,1);
119
  soft_task_def_ctrl_jet(stm);
120
  par=(void *) num;
121
  soft_task_def_arg(stm,par);
122
  soft_task_def_met(stm,GAUGE_WCET);
123
  soft_task_def_period(stm,PERIOD_CAR);
124
  soft_task_def_usemath(stm);
125
  pid = task_create(name, gauge_c, &stm, NULL);
126
  if (pid == NIL) {
127
    grx_close();
128
    perror("stl_create():Could not create task <Gauge_c>");
129
    sys_abort(1);
130
  }
131
  g_table[num]=pid;
132
  task_activate(pid);
133
}
134
 
135
 
136
void arrow_create(char *name,int num) {
137
  void *par;
138
  PID pid;
139
  SOFT_TASK_MODEL stm;
140
 
141
  soft_task_default_model(stm);
142
  soft_task_def_level(stm,1);
143
  soft_task_def_ctrl_jet(stm);
144
  par=(void *) num;
145
  soft_task_def_arg(stm,par);
146
  soft_task_def_met(stm,ARROW_WCET);
147
  soft_task_def_period(stm,PERIOD_CAR*4);
148
  pid = task_create(name,blink_arrow, &stm, NULL);
149
  if (pid == NULL) {
150
    grx_close();
151
    perror("stl_create():Could not create task <arrow>");
152
    sys_abort(1);
153
  }
154
  a_table[num]=pid;
155
  task_activate(pid);
156
}
157
 
158
void killer_create() {
159
  void *par;
160
  PID pid;
161
  SOFT_TASK_MODEL stm;
162
 
163
  soft_task_default_model(stm);
164
  soft_task_def_level(stm,1);
165
  soft_task_def_ctrl_jet(stm);
166
  par=(void *) 0;
167
  soft_task_def_arg(stm,par);
168
  soft_task_def_met(stm,1000);
169
  soft_task_def_period(stm,5*SECOND);
170
  pid = task_create("killer", killer, &stm, NULL);
171
  if (pid == NULL) {
172
    grx_close();
173
    perror("ref_create():Could not create task <killer>");
174
    sys_abort(1);
175
  }
176
  task_activate(pid);
177
}
178
 
179
 
180
void ref_create() {
181
  void *par;
182
  PID pid;
183
  SOFT_TASK_MODEL stm;
184
 
185
  soft_task_default_model(stm);
186
  soft_task_def_level(stm,1);
187
  soft_task_def_ctrl_jet(stm);
188
  par=(void *) 0;
189
  soft_task_def_arg(stm,par);
190
  soft_task_def_met(stm,3000);
191
  soft_task_def_period(stm,4*SECOND);
192
  pid = task_create("Refresher", refresher, &stm, NULL);
193
  if (pid == NULL) {
194
    grx_close();
195
    perror("ref_create():Could not create task <refresher>");
196
    sys_abort(1);
197
  }
198
  task_activate(pid);
199
}
200
 
201
void cam_create(char *name,int num) {
202
  void *par;
203
  PID pid;
204
  SOFT_TASK_MODEL stm;
205
 
206
  soft_task_default_model(stm);
207
  soft_task_def_level(stm,1);
208
  soft_task_def_ctrl_jet(stm);
209
  par=(void *) num;
210
  soft_task_def_arg(stm,par);
211
  soft_task_def_met(stm,CAM_WCET);
212
  soft_task_def_period(stm,PERIOD_CAR);
213
  pid = task_create(name, camera, &stm, NULL);
214
  if (pid == NULL) {
215
    grx_close();
216
    perror("stl_create():Could not create task <Camera>");
217
    sys_abort(1);
218
  }
219
  c_table[num]=pid;
220
  task_activate(pid);
221
}
222
 
223
void stl_create(char *name,int num) {
224
  void *par;
225
  PID pid;
226
  SOFT_TASK_MODEL stm;
227
 
228
  soft_task_default_model(stm);
229
  soft_task_def_level(stm,1);
230
  soft_task_def_ctrl_jet(stm);
231
  strcpy(tl_data_array[num].tl_name,name);
232
  par=(void *) &(tl_data_array[num]);
233
  soft_task_def_arg(stm,par);
234
  soft_task_def_met(stm,TL_WCET);
235
  soft_task_def_period(stm,tl_data_array[num].period);
236
  pid = task_create(name, traffic_light, &stm, NULL);
237
  if (pid == NULL) {
238
    grx_close();
239
    perror("stl_create():Could not create task <Traffic Light>");
240
    sys_abort(1);
241
  }
242
  tl_data_array[num].pid=pid;
243
  task_activate(pid);
244
}
245
 
246
void h_create(char *name,int num) {
247
  void *par;
248
  PID pid;
249
  int start_point=0;
250
  short flag=0;
251
  HARD_TASK_MODEL htm;
252
  car_data cd,*d;
253
 
254
#ifdef POS_RAND
255
  start_point=rand()%S_POINT;
256
  if(busy_o!=NULL)
257
    do {
258
      flag=0;
259
      d=busy_o;
260
      do {
261
        if(d->number!=num)
262
          if(module(starting_set_array[start_point].xpos,d->xp,starting_set_array[start_point].ypos,d->yp)<256) {
263
            start_point=(start_point+1)%S_POINT;
264
            flag=1;
265
            break;
266
          }
267
        d=d->next;
268
      } while(d!=NULL);
269
    } while(flag);
270
#endif
271
#ifndef POS_RAND
272
  start_point=OBL_START;
273
#endif
274
 
275
  cd.number=num;
276
  cd.xpos=cd.xp=starting_set_array[start_point].xpos;
277
  cd.ypos=cd.yp=starting_set_array[start_point].ypos;
278
  cd.angle=starting_set_array[start_point].angles;
279
  cd.speed=MIN_SPEED;
280
  cd.dist_obs=MAX_DIST;
281
  cd.collision=cd.boom=cd.correggi=cd.somma=0;
282
  cd.dir=STRAIGHT;
283
  car_data_array[num]=cd;
284
  hard_task_default_model(htm);
285
  hard_task_def_ctrl_jet(htm);
286
  par=(void *) &(car_data_array[num]);
287
  hard_task_def_arg(htm,par);
288
  hard_task_def_wcet(htm, CAR_WCET);
289
  hard_task_def_mit(htm,PERIOD_CAR);
290
  hard_task_def_usemath(htm);
291
  pid = task_create(name, car, &htm, NULL);
292
  if (pid == NULL) {
293
    grx_close();
294
    perror("h_create():Could not create task <hard car>");
295
    sys_abort(1);
296
  }
297
  else {
298
    p_table[num]=pid;
299
    sem_wait(&(kill_mutex[num]));
300
    kill_flag[num]=0;
301
    car_data_array[num].running=1;
302
    sem_post(&(kill_mutex[num]));
303
    task_activate(pid);
304
  }
305
}
306
 
307
void s_create(char *name,int num) {
308
  void *par;
309
  PID pid;
310
  int start_point=0;
311
  SOFT_TASK_MODEL stm;
312
  car_data cd,*d;
313
  short flag=0;
314
 
315
#ifdef POS_RAND
316
  start_point=rand()%S_POINT;
317
  if(busy_o!=NULL)
318
    do {
319
      flag=0;
320
      d=busy_o;
321
      do {
322
        if(d->number!=num)
323
          if(module(starting_set_array[start_point].xpos,d->xp,starting_set_array[start_point].ypos,d->yp)<256) {
324
            start_point=(start_point+1)%S_POINT;
325
            flag=1;
326
            break;
327
          }
328
        d=d->next;
329
      } while(d!=NULL);
330
    } while(flag);
331
#endif
332
#ifndef POS_RAND
333
  start_point=OBL_START;
334
#endif
335
 
336
  cd.number=num;
337
  cd.xpos=cd.xp=starting_set_array[start_point].xpos;
338
  cd.ypos=cd.yp=starting_set_array[start_point].ypos;
339
  cd.angle=starting_set_array[start_point].angles;
340
  cd.speed=MIN_SPEED;
341
  cd.dist_obs=MAX_DIST;
342
  cd.collision=cd.boom=cd.correggi=cd.somma=0;
343
  cd.dir=STRAIGHT;
344
  car_data_array[num]=cd;
345
  soft_task_default_model(stm);
346
  soft_task_def_level(stm,1);
347
  soft_task_def_ctrl_jet(stm);
348
  par=(void *) &(car_data_array[num]);;
349
  soft_task_def_arg(stm,par);
350
  soft_task_def_met(stm,SCAR_WCET);
351
  soft_task_def_period(stm,SPERIOD_CAR);
352
  soft_task_def_usemath(stm);
353
  pid = task_create(name, car, &stm, NULL);
354
  if (pid == NULL) {
355
    grx_close();
356
    perror("h_create():Could not create task <soft car>");
357
    sys_abort(1);
358
  }
359
  else {
360
    p_table[num]=pid;
361
    sem_wait(&(kill_mutex[num]));
362
    kill_flag[num]=0;
363
    car_data_array[num].running=1;
364
    sem_post(&(kill_mutex[num]));
365
    task_activate(pid);
366
  }
367
}
368
 
369
void killing(int num) {
370
  PID car_to_kill,cam_to_kill,gauge_to_kill,a_to_kill;
371
  int c;
372
 
373
  c=car_data_array[num].number*46;
374
  car_to_kill=p_table[num];
375
  cam_to_kill=c_table[num];
376
  gauge_to_kill=g_table[num];
377
  a_to_kill=a_table[num];
378
  sem_wait(&(kill_mutex[num]));
379
  kill_flag[num]=1;
380
  car_data_array[num].running=0;
381
  sem_post(&(kill_mutex[num]));
382
  task_kill(car_to_kill);
383
  task_kill(cam_to_kill);
384
  task_kill(gauge_to_kill);
385
  task_kill(a_to_kill);
386
  sem_wait(&mutex);
387
  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]);
388
  grx_putimage(CAMX-1,CAMY-1+c,CAMX+30,CAMY+30+c,clrcam);
389
  grx_putimage(CAMX+40-1,CAMY-1+c,CAMX+40+30,CAMY+c+30,clrcam);
390
  grx_putimage(CAMX+80-1,CAMY-1+c,CAMX+80+30,CAMY+c+30,clrcam);
391
  grx_text("    ",CAMX,CAMY+c+30,black,black);
392
  sem_post(&mutex);
393
}