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