hedgewars/uAI.pas
changeset 15386 fcdb6e3a9d36
parent 14690 fd22b6d474e7
child 15466 6031c0cfec89
equal deleted inserted replaced
15385:114b036522a6 15386:fcdb6e3a9d36
    36 
    36 
    37 var BestActions: TActions;
    37 var BestActions: TActions;
    38     CanUseAmmo: array [TAmmoType] of boolean;
    38     CanUseAmmo: array [TAmmoType] of boolean;
    39     StopThinking: boolean;
    39     StopThinking: boolean;
    40     StartTicks: Longword;
    40     StartTicks: Longword;
    41     ThinkThread: PSDL_Thread;
    41     ThreadSem: PSDL_Sem;
    42     ThreadLock: PSDL_Mutex;
       
    43 
    42 
    44 procedure FreeActionsList;
    43 procedure FreeActionsList;
    45 begin
    44 begin
    46     AddFileLog('FreeActionsList called');
    45     AddFileLog('FreeActionsList called');
    47     if (ThinkThread <> nil) then
    46 
    48         begin
    47     StopThinking:= true;
    49         StopThinking:= true;
    48     SDL_SemWait(ThreadSem);
    50         SDL_WaitThread(ThinkThread, nil);
    49     SDL_SemPost(ThreadSem);
    51         end;
       
    52     SDL_LockMutex(ThreadLock);
       
    53     ThinkThread:= nil;
       
    54     SDL_UnlockMutex(ThreadLock);
       
    55 
    50 
    56     if CurrentHedgehog <> nil then
    51     if CurrentHedgehog <> nil then
    57         with CurrentHedgehog^ do
    52         with CurrentHedgehog^ do
    58             if Gear <> nil then
    53             if Gear <> nil then
    59                 if BotLevel <> 0 then
    54                 if BotLevel <> 0 then
   520             SDL_Delay(100)
   515             SDL_Delay(100)
   521         end
   516         end
   522     end;
   517     end;
   523 
   518 
   524 Me^.State:= Me^.State and (not gstHHThinking);
   519 Me^.State:= Me^.State and (not gstHHThinking);
   525 SDL_LockMutex(ThreadLock);
       
   526 ThinkThread:= nil;
       
   527 SDL_UnlockMutex(ThreadLock);
       
   528 Think:= 0;
   520 Think:= 0;
       
   521 SDL_SemPost(ThreadSem);
   529 end;
   522 end;
   530 
   523 
   531 procedure StartThink(Me: PGear);
   524 procedure StartThink(Me: PGear);
       
   525 var ThinkThread: PSDL_Thread;
   532 begin
   526 begin
   533 if ((Me^.State and (gstAttacking or gstHHJumping or gstMoving)) <> 0)
   527 if ((Me^.State and (gstAttacking or gstHHJumping or gstMoving)) <> 0)
   534 or isInMultiShoot then
   528 or isInMultiShoot then
   535     exit;
   529     exit;
   536 
   530 
       
   531 SDL_SemWait(ThreadSem);
   537 //DeleteCI(Me); // this will break demo/netplay
   532 //DeleteCI(Me); // this will break demo/netplay
   538 
   533 
   539 Me^.State:= Me^.State or gstHHThinking;
   534 Me^.State:= Me^.State or gstHHThinking;
   540 Me^.Message:= 0;
   535 Me^.Message:= 0;
   541 
   536 
   554     exit
   549     exit
   555     end;
   550     end;
   556 
   551 
   557 FillBonuses(((Me^.State and gstAttacked) <> 0) and (not isInMultiShoot) and ((GameFlags and gfInfAttack) = 0));
   552 FillBonuses(((Me^.State and gstAttacked) <> 0) and (not isInMultiShoot) and ((GameFlags and gfInfAttack) = 0));
   558 
   553 
   559 SDL_LockMutex(ThreadLock);
       
   560 ThinkThread:= SDL_CreateThread(@Think, PChar('think'), Me);
   554 ThinkThread:= SDL_CreateThread(@Think, PChar('think'), Me);
   561 SDL_UnlockMutex(ThreadLock);
   555 SDL_DetachThread(ThinkThread);
   562 end;
   556 end;
   563 
   557 
   564 {$IFDEF DEBUGAI}
   558 {$IFDEF DEBUGAI}
   565 var scoreShown: boolean = false;
   559 var scoreShown: boolean = false;
   566 {$ENDIF}
   560 {$ENDIF}
   608 end;
   602 end;
   609 
   603 
   610 procedure initModule;
   604 procedure initModule;
   611 begin
   605 begin
   612     StartTicks:= 0;
   606     StartTicks:= 0;
   613     ThinkThread:= nil;
   607     ThreadSem:= SDL_CreateSemaphore(1);
   614     ThreadLock:= SDL_CreateMutex();
       
   615 end;
   608 end;
   616 
   609 
   617 procedure freeModule;
   610 procedure freeModule;
   618 begin
   611 begin
   619     FreeActionsList();
   612     FreeActionsList();
   620     SDL_DestroyMutex(ThreadLock);
   613     SDL_DestroySemaphore(ThreadSem);
   621 end;
   614 end;
   622 
   615 
   623 end.
   616 end.