Diff markup
001 001
002 002
003 003
004 004
005 005
006 006
007 007
008 008
009 009
010 010
011 011
012 012
013 013
014 014
015 015
016 016
017 017
018 018
019 019
020 020
021 021
022 022
023 023
024 024
025 025
026 026
027 027
028 #include <sos/assert.h> 028 #include <sos/assert.h>
029 #include <sos/klibc.h> 029 #include <sos/klibc.h>
030 #include <sos/thread.h> 030 #include <sos/thread.h>
031 #include <sos/ksynch.h> 031 #include <sos/ksynch.h>
032 #include <sos/kmalloc.h> 032 #include <sos/kmalloc.h>
033 #include <drivers/x86_videomem.h> 033 #include <drivers/x86_videomem.h>
034 034
035 035
036 036
037 037
038 038
039 039
040 040
041 041
042 042
043 #define MAP_X 76 043 #define MAP_X 76
044 #define MAP_Y 12 044 #define MAP_Y 12
045 #define MAP_SIZE MAP_X * MAP_Y 045 #define MAP_SIZE MAP_X * MAP_Y
046 046
047 #define MOUSE 0x01 047 #define MOUSE 0x01
048 #define CHEESE 0x02 048 #define CHEESE 0x02
049 #define OBSTACLE 0x04 049 #define OBSTACLE 0x04
050 #define INPUT 0x08 050 #define INPUT 0x08
051 #define OUTPUT 0x10 051 #define OUTPUT 0x10
052 052
053 #define OBSTACLE_COUNT 100 053 #define OBSTACLE_COUNT 100
054 #define CHEESE_COUNT 650 054 #define CHEESE_COUNT 650
055 055
056 #define MOUSE_FULL 0x01 056 #define MOUSE_FULL 0x01
057 #define MOUSE_EMPTY 0x02 057 #define MOUSE_EMPTY 0x02
058 #define CHEESE_FOUND 0x04 058 #define CHEESE_FOUND 0x04
059 #define MOUSE_EXITED 0x08 059 #define MOUSE_EXITED 0x08
060 060
061 #define MOUSE_SPEED_MAX 1000 061 #define MOUSE_SPEED_MAX 1000
062 #define MOUSE_SPEED_MIN 4 062 #define MOUSE_SPEED_MIN 4
063 063
064 typedef unsigned int Color_t; 064 typedef unsigned int Color_t;
065 065
066 struct Point{ 066 struct Point{
067 int X; 067 int X;
068 int Y; 068 int Y;
069 }; 069 };
070 070
071 typedef struct Point Point_t; 071 typedef struct Point Point_t;
072 072
073 #define Set(Reg, Flag) Reg = (Reg | F 073 #define Set(Reg, Flag) Reg = (Reg | Flag)
074 #define Reset(Reg, Flag) Reg = (Reg &(~ 074 #define Reset(Reg, Flag) Reg = (Reg &(~Flag))
075 #define IsSet(Reg, Flag) (Reg & Flag) 075 #define IsSet(Reg, Flag) (Reg & Flag)
076 076
077 077
078 078
079 079
080 080
081 struct Element{ 081 struct Element{
082 sos_ui32_t Type; 082 sos_ui32_t Type;
083 sos_ui32_t Status; 083 sos_ui32_t Status;
084 Color_t Color; 084 Color_t Color;
085 Point_t P; 085 Point_t P;
086 struct sos_thread * ThreadID; 086 struct sos_thread * ThreadID;
087 int Way; 087 int Way;
088 }; 088 };
089 089
090 typedef struct Element Element_t; 090 typedef struct Element Element_t;
091 091
092 092
093 093
094 094
>> 095 static void MouseCommander(void);
095 static void DrawMap(void); 096 static void DrawMap(void);
096 static sos_ret_t CreateMap(void); 097 static sos_ret_t CreateMap(void);
097 static sos_ret_t InitMapInput(Element_t * * pM 098 static sos_ret_t InitMapInput(Element_t * * pMap);
098 static sos_ret_t InitMapOutput(Element_t * * p 099 static sos_ret_t InitMapOutput(Element_t * * pMap);
099 static sos_ret_t ElementInit(Element_t * * pMa 100 static sos_ret_t ElementInit(Element_t * * pMap, unsigned int Type);
100 static void Mouse(unsigned long Param); 101 static void Mouse(unsigned long Param);
101 static void MouseMove(Point_t * P); 102 static void MouseMove(Point_t * P);
102 static Point_t ChoosePosition(Element_t * pMou 103 static Point_t ChoosePosition(Element_t * pMouse, int Positions[], int Count);
103 static int EvaluatePositions(Point_t Org, int 104 static int EvaluatePositions(Point_t Org, int Positions[], Point_t * Cheese);
104 static sos_bool_t IsCollision(Point_t Org, Poi 105 static sos_bool_t IsCollision(Point_t Org, Point_t p, Point_t *Cheese);
105 static sos_bool_t AffectMovement(Point_t Org, 106 static sos_bool_t AffectMovement(Point_t Org, Point_t p);
106 static void MouseCreator(void); 107 static void MouseCreator(void);
107 static sos_ret_t CreateMouse(void); 108 static sos_ret_t CreateMouse(void);
108 109
109 110
110 111
111 112
112 113
113 static Element_t * * pMap; 114 static Element_t * * pMap;
114 static struct sos_ksema SemMap; 115 static struct sos_ksema SemMap;
115 static struct sos_ksema SemMouse; 116 static struct sos_ksema SemMouse;
116 static int MouseCount = 0; 117 static int MouseCount = 0;
117 static int CheeseCount = 0; 118 static int CheeseCount = 0;
118 static int ObstacleCount = 0; 119 static int ObstacleCount = 0;
119 static int MouseSpeed = 100; 120 static int MouseSpeed = 100;
120 121
121 122
122 123
123 124
124 void DrawPixel(int x, int y, Color_t color) 125 void DrawPixel(int x, int y, Color_t color)
125 { 126 {
126 sos_x86_videomem_putchar(y+3, x+2, color, 21 127 sos_x86_videomem_putchar(y+3, x+2, color, 219);
127 } 128 }
128 129
129 130
130 131
131 132
132 133
133 134
134 void MouseSim(void) 135 void MouseSim(void)
135 { 136 {
136 137
137 SOS_ASSERT_FATAL(SOS_OK == sos_ksema_init(& !! 138 SOS_ASSERT_FATAL(SOS_OK == sos_ksema_init(& SemMap, "SemMap", 1));
138 SO <<
139 139
140 140
141 SOS_ASSERT_FATAL(SOS_OK == sos_ksema_init(& !! 141 SOS_ASSERT_FATAL(SOS_OK == sos_ksema_init(& SemMouse, "SemMouse", 2));
142 SO <<
143 142
144 143
145 SOS_ASSERT_FATAL(SOS_OK == CreateMap()); 144 SOS_ASSERT_FATAL(SOS_OK == CreateMap());
146 145
147 146
148 SOS_ASSERT_FATAL(sos_create_kernel_thread("M 147 SOS_ASSERT_FATAL(sos_create_kernel_thread("MouseCreator",
149 (s 148 (sos_kernel_thread_start_routine_t)MouseCreator,
150 0, !! 149 0) != NULL);
151 150
152 } 151 }
153 152
154 153
155 154
156 155
157 156
158 157
159 158
160 static sos_ret_t CreateMap(void) 159 static sos_ret_t CreateMap(void)
161 { 160 {
162 pMap = (Element_t * *)sos_kmalloc(MAP_ 161 pMap = (Element_t * *)sos_kmalloc(MAP_SIZE * sizeof(Element_t *), 0);
163 if(pMap == NULL) return -SOS_ENOMEM; 162 if(pMap == NULL) return -SOS_ENOMEM;
164 163
165 164
166 memset(pMap, 0, MAP_SIZE * sizeof(Elem 165 memset(pMap, 0, MAP_SIZE * sizeof(Element_t *));
167 166
168 167
169 if(SOS_OK != InitMapInput(pMap)) 168 if(SOS_OK != InitMapInput(pMap))
170 { 169 {
171 return -SOS_EFATAL; 170 return -SOS_EFATAL;
172 } 171 }
173 172
174 173
175 if(InitMapOutput(pMap) != SOS_OK) 174 if(InitMapOutput(pMap) != SOS_OK)
176 { 175 {
177 return -SOS_EFATAL; 176 return -SOS_EFATAL;
178 } 177 }
179 178
180 179
181 if(ElementInit(pMap, CHEESE) != SOS_OK 180 if(ElementInit(pMap, CHEESE) != SOS_OK)
182 { 181 {
183 return -SOS_EFATAL; 182 return -SOS_EFATAL;
184 } 183 }
185 184
186 185
187 if(ElementInit(pMap, OBSTACLE) != SOS_ 186 if(ElementInit(pMap, OBSTACLE) != SOS_OK)
188 { 187 {
189 return -SOS_EFATAL; 188 return -SOS_EFATAL;
190 } 189 }
191 190
192 DrawMap(); 191 DrawMap();
193 192
194 return SOS_OK; 193 return SOS_OK;
195 } 194 }
196 195
197 196
198 197
199 198
200 199
201 200
202 static void DrawMap(void) 201 static void DrawMap(void)
203 { 202 {
204 unsigned int I; 203 unsigned int I;
205 204
206 for(I = 0; I < MAP_SIZE; I++) 205 for(I = 0; I < MAP_SIZE; I++)
207 { 206 {
208 if(pMap[I] != NULL) 207 if(pMap[I] != NULL)
209 { 208 {
210 DrawPixel(I % MAP_X, I 209 DrawPixel(I % MAP_X, I/MAP_X, pMap[I]->Color);
211 } 210 }
212 else DrawPixel(I % MAP_X, I/MA 211 else DrawPixel(I % MAP_X, I/MAP_X, SOS_X86_VIDEO_FG_BLACK);
213 } 212 }
214 sos_x86_videomem_printf(23, 0, SOS_X86 213 sos_x86_videomem_printf(23, 0, SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
215 "Souris = %d; 214 "Souris = %d; Fromages = %d; Obstacles = %d ",
216 MouseCount, Ch 215 MouseCount, CheeseCount, ObstacleCount);
217 } 216 }
218 217
219 218
220 219
221 220
222 221
223 222
224 223
225 static sos_ret_t InitMapInput(Element_t * * pM 224 static sos_ret_t InitMapInput(Element_t * * pMap)
226 { 225 {
227 Element_t * pElement; 226 Element_t * pElement;
228 227
229 228
230 pElement = (Element_t *)sos_kmalloc(si 229 pElement = (Element_t *)sos_kmalloc(sizeof(Element_t), 0);
231 if(pElement == NULL) return -SOS_ENOME 230 if(pElement == NULL) return -SOS_ENOMEM;
232 231
233 232
234 pElement->Type = INPUT; 233 pElement->Type = INPUT;
235 pElement->Status = 0; 234 pElement->Status = 0;
236 pElement->Color = SOS_X86_VIDEO_FG_YEL 235 pElement->Color = SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE;
237 pElement->P.X = 0; 236 pElement->P.X = 0;
238 pElement->P.Y = MAP_Y / 2; 237 pElement->P.Y = MAP_Y / 2;
239 pElement->ThreadID = 0; 238 pElement->ThreadID = 0;
240 239
241 pMap[(pElement->P.Y * MAP_X) + pElemen 240 pMap[(pElement->P.Y * MAP_X) + pElement->P.X] = pElement;
242 241
243 return SOS_OK; 242 return SOS_OK;
244 } 243 }
245 244
246 245
247 246
248 247
249 248
250 249
251 250
252 static sos_ret_t InitMapOutput(Element_t * * p 251 static sos_ret_t InitMapOutput(Element_t * * pMap)
253 { 252 {
254 Element_t * pElement; 253 Element_t * pElement;
255 254
256 255
257 pElement = (Element_t *)sos_kmalloc(si 256 pElement = (Element_t *)sos_kmalloc(sizeof(Element_t), 0);
258 if(pElement == NULL) return -SOS_ENOME 257 if(pElement == NULL) return -SOS_ENOMEM;
259 258
260 259
261 pElement->Type = OUTPUT; 260 pElement->Type = OUTPUT;
262 pElement->Status = 0; 261 pElement->Status = 0;
263 pElement->Color = SOS_X86_VIDEO_FG_LTB 262 pElement->Color = SOS_X86_VIDEO_FG_LTBLUE;
264 pElement->P.X = MAP_X - 1; 263 pElement->P.X = MAP_X - 1;
265 pElement->P.Y = MAP_Y / 2; 264 pElement->P.Y = MAP_Y / 2;
266 pElement->ThreadID = 0; 265 pElement->ThreadID = 0;
267 266
268 pMap[(pElement->P.Y * MAP_X) + pElemen 267 pMap[(pElement->P.Y * MAP_X) + pElement->P.X] = pElement;
269 268
270 return SOS_OK; 269 return SOS_OK;
271 } 270 }
272 271
273 272
274 273
275 274
276 275
277 276
278 277
279 278
280 static sos_ret_t ElementInit(Element_t * * pMa 279 static sos_ret_t ElementInit(Element_t * * pMap, unsigned int Type)
281 { 280 {
282 unsigned int I, J; 281 unsigned int I, J;
283 unsigned int Max; 282 unsigned int Max;
284 Color_t Color; 283 Color_t Color;
285 284
286 if(Type == CHEESE) 285 if(Type == CHEESE)
287 { 286 {
288 Max = CHEESE_COUNT; 287 Max = CHEESE_COUNT;
289 Color = SOS_X86_VIDEO_FG_YELLO 288 Color = SOS_X86_VIDEO_FG_YELLOW;
290 } 289 }
291 else if(Type == OBSTACLE) 290 else if(Type == OBSTACLE)
292 { 291 {
293 Max = OBSTACLE_COUNT; 292 Max = OBSTACLE_COUNT;
294 Color = SOS_X86_VIDEO_FG_GREEN 293 Color = SOS_X86_VIDEO_FG_GREEN;
295 } 294 }
296 else 295 else
297 { 296 {
298 return -SOS_EINVAL; 297 return -SOS_EINVAL;
299 } 298 }
300 299
301 for(I = 0; I < Max; I++) 300 for(I = 0; I < Max; I++)
302 { 301 {
303 J = random(); 302 J = random();
304 J += random(); 303 J += random();
305 J %= MAP_SIZE; 304 J %= MAP_SIZE;
306 if(pMap[J] == NULL) 305 if(pMap[J] == NULL)
307 { 306 {
308 pMap[J] = (Element_t * 307 pMap[J] = (Element_t *)sos_kmalloc(sizeof(Element_t),
309 308 0);
310 if(pMap[J] == NULL) re 309 if(pMap[J] == NULL) return -SOS_ENOMEM;
311 310
312 pMap[J]->Type = Type; 311 pMap[J]->Type = Type;
313 312
314 if(Type == CHEESE) 313 if(Type == CHEESE)
315 { 314 {
316 CheeseCount++; 315 CheeseCount++;
317 } 316 }
318 else if(Type == OBSTAC 317 else if(Type == OBSTACLE)
319 { 318 {
320 ObstacleCount+ 319 ObstacleCount++;
321 } 320 }
322 321
323 pMap[J]->Color = Color 322 pMap[J]->Color = Color;
324 pMap[J]->Status = 0; 323 pMap[J]->Status = 0;
325 pMap[J]->Color = Color 324 pMap[J]->Color = Color;
326 pMap[J]->P.X = J % MAP 325 pMap[J]->P.X = J % MAP_X;
327 pMap[J]->P.Y = J / MAP 326 pMap[J]->P.Y = J / MAP_X;
328 pMap[J]->ThreadID = 0; 327 pMap[J]->ThreadID = 0;
329 } 328 }
330 } 329 }
331 330
332 return SOS_OK; 331 return SOS_OK;
333 } 332 }
334 333
335 334
336 335
337 336
338 337
339 338
340 339
341 340
342 341
343 342
344 343
345 344
346 static void Mouse(unsigned long Param) 345 static void Mouse(unsigned long Param)
347 { 346 {
348 Element_t * pMouse = (Element_t *)Para 347 Element_t * pMouse = (Element_t *)Param;
349 Point_t P; 348 Point_t P;
350 349
351 SOS_ASSERT_FATAL(pMouse != NULL); 350 SOS_ASSERT_FATAL(pMouse != NULL);
352 351
353 352
354 P = pMouse->P; 353 P = pMouse->P;
355 P = pMouse->P; 354 P = pMouse->P;
356 355
357 while(1) 356 while(1)
358 { 357 {
359 int delay_ms; 358 int delay_ms;
360 struct sos_time delay; 359 struct sos_time delay;
361 360
362 361
363 sos_ksema_down(& SemMap, NULL); 362 sos_ksema_down(& SemMap, NULL);
364 363
365 MouseMove(&P); 364 MouseMove(&P);
366 365
367 sos_ksema_up(& SemMap); 366 sos_ksema_up(& SemMap);
368 367
369 368
370 if (IsSet(pMouse->Status, MOUSE_EXIT 369 if (IsSet(pMouse->Status, MOUSE_EXITED))
371 370
372 break; 371 break;
373 372
374 373
375 delay_ms = MOUSE_SPEED_MIN + (random 374 delay_ms = MOUSE_SPEED_MIN + (random() % MouseSpeed);
376 delay.sec = delay_ms / 1000; 375 delay.sec = delay_ms / 1000;
377 delay.nanosec = (delay_ms % 1000) * 376 delay.nanosec = (delay_ms % 1000) * 1000000;
378 sos_thread_sleep(& delay); 377 sos_thread_sleep(& delay);
379 } 378 }
380 379
381 380
382 sos_kfree((sos_vaddr_t)pMouse); 381 sos_kfree((sos_vaddr_t)pMouse);
383 } 382 }
384 383
385 384
386 385
387 386
388 387
389 388
390 389
391 390
392 static void MouseMove(Point_t * P) 391 static void MouseMove(Point_t * P)
393 { 392 {
394 Point_t Org; 393 Point_t Org;
395 Point_t p; 394 Point_t p;
396 Point_t Cheese; 395 Point_t Cheese;
397 int Positions[8]; 396 int Positions[8];
398 int Count = 0; 397 int Count = 0;
399 Element_t * pMouse; 398 Element_t * pMouse;
400 399
401 Org = *P; 400 Org = *P;
402 401
403 pMouse = pMap[Org.X + (Org.Y * MAP_X)] 402 pMouse = pMap[Org.X + (Org.Y * MAP_X)];
404 403
405 Count = EvaluatePositions(Org, Positio 404 Count = EvaluatePositions(Org, Positions, &Cheese);
406 405
407 if(Count == 0) return; 406 if(Count == 0) return;
408 407
409 p = Org; 408 p = Org;
410 409
411 if(IsSet(pMouse->Status, CHEESE_FOUND) 410 if(IsSet(pMouse->Status, CHEESE_FOUND))
412 { 411 {
413 Reset(pMouse->Status, CHEESE_F 412 Reset(pMouse->Status, CHEESE_FOUND);
414 p = Cheese; 413 p = Cheese;
415 } 414 }
416 else 415 else
417 { 416 {
418 p = ChoosePosition(pMouse, Pos 417 p = ChoosePosition(pMouse, Positions, Count);
419 } 418 }
420 if(AffectMovement(Org, p) == FALSE) re 419 if(AffectMovement(Org, p) == FALSE) return;
421 420
422 pMap[Org.X + (Org.Y * MAP_X)] = NULL; 421 pMap[Org.X + (Org.Y * MAP_X)] = NULL;
423 pMap[p.X + (p.Y * MAP_X)] = pMouse; 422 pMap[p.X + (p.Y * MAP_X)] = pMouse;
424 pMouse->P = p; 423 pMouse->P = p;
425 424
426 DrawPixel(Org.X, Org.Y, SOS_X86_VIDEO_ 425 DrawPixel(Org.X, Org.Y, SOS_X86_VIDEO_FG_BLACK);
427 DrawPixel(p.X, p.Y, pMouse->Color); 426 DrawPixel(p.X, p.Y, pMouse->Color);
428 sos_x86_videomem_printf( 23,0, SOS_X86 427 sos_x86_videomem_printf( 23,0, SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE, "Souris = %d; Fromages = %d; Obstacles = %d ", MouseCount, CheeseCount, ObstacleCount);
429 428
430 *P = p; 429 *P = p;
431 } 430 }
432 431
433 432
434 433
435 434
436 435
437 436
438 437
439 438
440 439
441 440
442 static Point_t ChoosePosition(Element_t * pMou 441 static Point_t ChoosePosition(Element_t * pMouse, int Positions[], int Count)
443 { 442 {
444 int I, J; 443 int I, J;
445 Point_t p; 444 Point_t p;
446 445
447 for(J = 0; J < Count; J++) 446 for(J = 0; J < Count; J++)
448 { 447 {
449 I = Positions[J]; 448 I = Positions[J];
450 if(I == pMouse->Way) 449 if(I == pMouse->Way)
451 { 450 {
452 p = pMouse->P; 451 p = pMouse->P;
453 switch(I) 452 switch(I)
454 { 453 {
455 case 0: 454 case 0:
456 p.Y++; 455 p.Y++;
457 break; 456 break;
458 case 1: 457 case 1:
459 p.X++; 458 p.X++;
460 p.Y++; 459 p.Y++;
461 break; 460 break;
462 case 2: 461 case 2:
463 p.X++; 462 p.X++;
464 break; 463 break;
465 case 3: 464 case 3:
466 p.Y--; 465 p.Y--;
467 p.X++; 466 p.X++;
468 break; 467 break;
469 case 4: 468 case 4:
470 p.Y--; 469 p.Y--;
471 break; 470 break;
472 case 5: 471 case 5:
473 p.Y--; 472 p.Y--;
474 p.X--; 473 p.X--;
475 break; 474 break;
476 case 6: 475 case 6:
477 p.X--; 476 p.X--;
478 break; 477 break;
479 case 7: 478 case 7:
480 p.X--; 479 p.X--;
481 p.Y++; 480 p.Y++;
482 break; 481 break;
483 } 482 }
484 return p; 483 return p;
485 } 484 }
486 } 485 }
487 486
488 J = random() % Count; 487 J = random() % Count;
489 I = Positions[J]; 488 I = Positions[J];
490 if(((I + 4) % 8) == pMouse->Way) 489 if(((I + 4) % 8) == pMouse->Way)
491 { 490 {
492 J = (J + 1) % Count; 491 J = (J + 1) % Count;
493 I = Positions[J]; 492 I = Positions[J];
494 } 493 }
495 494
496 p = pMouse->P; 495 p = pMouse->P;
497 switch(I) 496 switch(I)
498 { 497 {
499 case 0: 498 case 0:
500 p.Y++; 499 p.Y++;
501 break; 500 break;
502 case 1: 501 case 1:
503 p.X++; 502 p.X++;
504 p.Y++; 503 p.Y++;
505 break; 504 break;
506 case 2: 505 case 2:
507 p.X++; 506 p.X++;
508 break; 507 break;
509 case 3: 508 case 3:
510 p.Y--; 509 p.Y--;
511 p.X++; 510 p.X++;
512 break; 511 break;
513 case 4: 512 case 4:
514 p.Y--; 513 p.Y--;
515 break; 514 break;
516 case 5: 515 case 5:
517 p.Y--; 516 p.Y--;
518 p.X--; 517 p.X--;
519 break; 518 break;
520 case 6: 519 case 6:
521 p.X--; 520 p.X--;
522 break; 521 break;
523 case 7: 522 case 7:
524 p.X--; 523 p.X--;
525 p.Y++; 524 p.Y++;
526 break; 525 break;
527 } 526 }
528 527
529 pMouse->Way = I; 528 pMouse->Way = I;
530 529
531 return p; 530 return p;
532 } 531 }
533 532
534 533
535 534
536 535
537 536
538 537
539 538
540 539
541 540
542 541
543 542
544 543
545 544
546 static int EvaluatePositions(Point_t Org, int 545 static int EvaluatePositions(Point_t Org, int Positions[], Point_t * Cheese)
547 { 546 {
548 int I; 547 int I;
549 int Count = 0; 548 int Count = 0;
550 Point_t p; 549 Point_t p;
551 Point_t CheesePos; 550 Point_t CheesePos;
552 551
553 for(I = 0; I < 8; I++) 552 for(I = 0; I < 8; I++)
554 { 553 {
555 p = Org; 554 p = Org;
556 switch(I) 555 switch(I)
557 { 556 {
558 case 0: 557 case 0:
559 p.Y++; 558 p.Y++;
560 break; 559 break;
561 case 1: 560 case 1:
562 p.X++; 561 p.X++;
563 p.Y++; 562 p.Y++;
564 break; 563 break;
565 case 2: 564 case 2:
566 p.X++; 565 p.X++;
567 break; 566 break;
568 case 3: 567 case 3:
569 p.Y--; 568 p.Y--;
570 p.X++; 569 p.X++;
571 break; 570 break;
572 case 4: 571 case 4:
573 p.Y--; 572 p.Y--;
574 break; 573 break;
575 case 5: 574 case 5:
576 p.Y--; 575 p.Y--;
577 p.X--; 576 p.X--;
578 break; 577 break;
579 case 6: 578 case 6:
580 p.X--; 579 p.X--;
581 break; 580 break;
582 case 7: 581 case 7:
583 p.X--; 582 p.X--;
584 p.Y++; 583 p.Y++;
585 break; 584 break;
586 } 585 }
587 586
588 if(IsCollision(Org, p, &Cheese 587 if(IsCollision(Org, p, &CheesePos) == FALSE)
589 { 588 {
590 Positions[Count] = I; 589 Positions[Count] = I;
591 Count++; 590 Count++;
592 } 591 }
593 } 592 }
594 593
595 *Cheese = CheesePos; 594 *Cheese = CheesePos;
596 595
597 return Count; 596 return Count;
598 } 597 }
599 598
600 599
601 600
602 601
603 602
604 603
605 604
606 605
607 static sos_bool_t AffectMovement(Point_t Org, 606 static sos_bool_t AffectMovement(Point_t Org, Point_t p)
608 { 607 {
609 Element_t * pMouse = pMap[Org.X + (Org 608 Element_t * pMouse = pMap[Org.X + (Org.Y * MAP_X)];
610 Element_t * pElement; 609 Element_t * pElement;
611 610
612 pElement = pMap[p.X + (p.Y * MAP_X)]; 611 pElement = pMap[p.X + (p.Y * MAP_X)];
613 612
614 613
615 if(pElement == NULL) return TRUE; 614 if(pElement == NULL) return TRUE;
616 615
617 switch(pElement->Type) 616 switch(pElement->Type)
618 { 617 {
619 case CHEESE: 618 case CHEESE:
620 619
621 sos_kfree((sos_vaddr_t 620 sos_kfree((sos_vaddr_t)pElement);
622 pMap[p.X + (p.Y * MAP_ 621 pMap[p.X + (p.Y * MAP_X)] = NULL;
623 622
624 623
625 Set(pMouse->Status, MO 624 Set(pMouse->Status, MOUSE_FULL);
626 Reset(pMouse->Status, 625 Reset(pMouse->Status, MOUSE_EMPTY);
627 pMouse->Color = SOS_X8 626 pMouse->Color = SOS_X86_VIDEO_FG_MAGENTA;
628 CheeseCount--; 627 CheeseCount--;
629 return TRUE; 628 return TRUE;
630 case OUTPUT: 629 case OUTPUT:
631 630
632 pMap[Org.X + (Org.Y * 631 pMap[Org.X + (Org.Y * MAP_X)] = NULL;
633 MouseCount--; 632 MouseCount--;
634 DrawPixel(Org.X, Org.Y 633 DrawPixel(Org.X, Org.Y, SOS_X86_VIDEO_FG_BLACK);
635 sos_x86_videomem_print 634 sos_x86_videomem_printf( 23,0, SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
636 635 "Souris = %d; Fromages = %d; Obstacles = %d ",
637 636 MouseCount, CheeseCount,
638 637 ObstacleCount);
639 Set(pMouse->Status, MO 638 Set(pMouse->Status, MOUSE_EXITED);
640 return FALSE; 639 return FALSE;
641 default : 640 default :
642 return FALSE; 641 return FALSE;
643 } 642 }
644 643
645 return FALSE; 644 return FALSE;
646 } 645 }
647 646
648 647
649 648
650 649
651 650
652 651
653 652
654 653
655 654
656 655
657 static sos_bool_t IsCollision(Point_t Org, Poi 656 static sos_bool_t IsCollision(Point_t Org, Point_t p, Point_t *Cheese)
658 { 657 {
659 Element_t * pMouse = pMap[Org.X + (Org 658 Element_t * pMouse = pMap[Org.X + (Org.Y * MAP_X)];
660 Element_t * pElement; 659 Element_t * pElement;
661 660
662 661
663 if((p.X < 0)||(p.Y < 0)) return TRUE; 662 if((p.X < 0)||(p.Y < 0)) return TRUE;
664 663
665 if((p.Y >= MAP_Y)||(p.X >= MAP_X)) ret 664 if((p.Y >= MAP_Y)||(p.X >= MAP_X)) return TRUE;
666 665
667 pElement = pMap[p.X + (p.Y * MAP_X)]; 666 pElement = pMap[p.X + (p.Y * MAP_X)];
668 667
669 668
670 if(pElement == NULL) return FALSE; 669 if(pElement == NULL) return FALSE;
671 670
672 671
673 if(IsSet(pMouse->Status, CHEESE_FOUND) 672 if(IsSet(pMouse->Status, CHEESE_FOUND)) return FALSE;
674 673
675 switch(pElement->Type) 674 switch(pElement->Type)
676 { 675 {
677 case CHEESE: 676 case CHEESE:
678 if(IsSet(pMouse->Statu 677 if(IsSet(pMouse->Status, MOUSE_FULL)) return TRUE;
679 678
680 Set(pMouse->Status, CH 679 Set(pMouse->Status, CHEESE_FOUND);
681 680
682 (*Cheese).X = p.X; 681 (*Cheese).X = p.X;
683 (*Cheese).Y = p.Y; 682 (*Cheese).Y = p.Y;
684 break; 683 break;
685 case INPUT: 684 case INPUT:
686 if(IsSet(pMouse->Statu 685 if(IsSet(pMouse->Status, MOUSE_EMPTY)) return TRUE;
687 686
688 Set(pMouse->Status, MO 687 Set(pMouse->Status, MOUSE_EMPTY);
689 Reset(pMouse->Status, 688 Reset(pMouse->Status, MOUSE_FULL);
690 pMouse->Color = SOS_X8 689 pMouse->Color = SOS_X86_VIDEO_FG_LTRED;
691 690
692 sos_ksema_up(& SemMous 691 sos_ksema_up(& SemMouse);
693 return TRUE; 692 return TRUE;
694 case OUTPUT: 693 case OUTPUT:
695 break; 694 break;
696 default : 695 default :
697 return TRUE; 696 return TRUE;
698 } 697 }
699 698
700 return FALSE; 699 return FALSE;
701 } 700 }
702 701
703 702
704 703
705 704
706 static void MouseCreator(void) 705 static void MouseCreator(void)
707 { 706 {
708 while(1) 707 while(1)
709 { 708 {
710 sos_ksema_down(& SemMouse, NUL 709 sos_ksema_down(& SemMouse, NULL);
711 sos_ksema_down(& SemMap, NULL) 710 sos_ksema_down(& SemMap, NULL);
712 CreateMouse(); 711 CreateMouse();
713 sos_ksema_up(& SemMap); 712 sos_ksema_up(& SemMap);
714 } 713 }
715 } 714 }
716 715
717 716
718 717
719 718
720 719
721 720
722 721
723 static sos_ret_t CreateMouse(void) 722 static sos_ret_t CreateMouse(void)
724 { 723 {
725 Element_t * pElement; 724 Element_t * pElement;
726 unsigned int I; 725 unsigned int I;
727 726
728 Point_t p; 727 Point_t p;
729 728
730 for(I = 0; I < 8; I++) 729 for(I = 0; I < 8; I++)
731 { 730 {
732 p.X = 0; 731 p.X = 0;
733 p.Y = MAP_Y / 2; 732 p.Y = MAP_Y / 2;
734 switch(I) 733 switch(I)
735 { 734 {
736 case 0: 735 case 0:
737 p.Y++; 736 p.Y++;
738 break; 737 break;
739 case 1: 738 case 1:
740 p.X++; 739 p.X++;
741 p.Y++; 740 p.Y++;
742 break; 741 break;
743 case 2: 742 case 2:
744 p.X++; 743 p.X++;
745 break; 744 break;
746 case 3: 745 case 3:
747 p.Y--; 746 p.Y--;
748 p.X++; 747 p.X++;
749 break; 748 break;
750 case 4: 749 case 4:
751 p.Y--; 750 p.Y--;
752 break; 751 break;
753 case 5: 752 case 5:
754 p.Y--; 753 p.Y--;
755 p.X--; 754 p.X--;
756 break; 755 break;
757 case 6: 756 case 6:
758 p.X--; 757 p.X--;
759 break; 758 break;
760 case 7: 759 case 7:
761 p.X--; 760 p.X--;
762 p.Y++; 761 p.Y++;
763 break; 762 break;
764 } 763 }
765 if((p.X >= 0)&&(p.Y >= 0)&&(p. 764 if((p.X >= 0)&&(p.Y >= 0)&&(p.X < MAP_X)&&(p.Y < MAP_Y))
766 { 765 {
767 pElement = pMap[p.X + 766 pElement = pMap[p.X + (p.Y * MAP_X)];
768 if(pElement == NULL) 767 if(pElement == NULL)
769 { 768 {
770 pElement = (El 769 pElement = (Element_t *)sos_kmalloc(sizeof(Element_t), 0);
771 if(pElement != 770 if(pElement != NULL)
772 { 771 {
773 pEleme 772 pElement->Type = MOUSE;
774 Set(pE 773 Set(pElement->Status, MOUSE_EMPTY);
775 pEleme 774 pElement->Color = SOS_X86_VIDEO_FG_LTRED;
776 pEleme 775 pElement->P = p;
777 pEleme 776 pElement->Way = 0;
778 pEleme 777 pElement->ThreadID
779 = so 778 = sos_create_kernel_thread("Mouse",
780 779 (sos_kernel_thread_start_routine_t)Mouse,
781 !! 780 pElement);
782 if(pEl 781 if(pElement->ThreadID == 0)
783 { 782 {
784 783 sos_kfree((sos_vaddr_t)pElement);
785 784 pElement = NULL;
786 785 return -SOS_ENOMEM;
787 } 786 }
788 pMap[p 787 pMap[p.X + (p.Y * MAP_X)] = pElement;
789 MouseC 788 MouseCount++;
790 789
791 DrawPi 790 DrawPixel(p.X, p.Y, pElement->Color);
792 sos_x8 791 sos_x86_videomem_printf(23, 0, SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE, "Souris = %d; Fromages = %d; Obstacles = %d ", MouseCount, CheeseCount, ObstacleCount);
793 792
794 return 793 return SOS_OK;
795 } 794 }
796 } 795 }
797 } 796 }
798 } 797 }
799 return -SOS_EBUSY; 798 return -SOS_EBUSY;
800 } 799 }
801 800
802 801
803 802
804 803
805 804