penguins  1.0.0
bot_thread.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include "bot.h"
4 #include "game.h"
5 #include "gui/better_random.hh"
6 #include <memory>
7 #include <wx/defs.h>
8 #include <wx/thread.h>
9 #include <wx/version.h>
10 
11 class BotTurnController;
12 
13 // The implementation of this class is somewhat based on
14 // <https://github.com/wxWidgets/wxWidgets/blob/v3.2.2.1/src/unix/threadpsx.cpp#L576-L716>.
16 public:
18 
19  void notify_exit();
20  void wait_for_exit();
21 
22  bool exited = false;
23 
24 protected:
27 
29 };
30 
31 class BotThread : public wxThread {
32 public:
34  virtual ~BotThread();
35  void cancel();
36 
37  std::shared_ptr<BotThreadShared> shared{ new BotThreadShared() };
38 
39 protected:
40  virtual void OnExit() override;
41 
42 #if !wxCHECK_VERSION(3, 1, 6)
43  // Stub for when this method is not available.
44  bool SetName(const wxString& WXUNUSED(name)) {
45  return false;
46  }
47 #endif
48 
50  std::unique_ptr<Game, decltype(&game_free)> game{ nullptr, game_free };
51  std::shared_ptr<BotParameters> bot_params{ nullptr };
52  std::unique_ptr<BotState, decltype(&bot_state_free)> bot_state{ nullptr, bot_state_free };
54  volatile bool* cancelled_ptr = nullptr;
55 };
56 
57 class BotPlacementThread : public BotThread {
58 public:
60 
61 protected:
62  virtual ExitCode Entry() override;
63 };
64 
65 class BotMovementThread : public BotThread {
66 public:
68 
69 protected:
70  virtual ExitCode Entry() override;
71 };
void bot_state_free(BotState *self)
Recursively destroys a BotState and its substates (similarly to game_free).
Definition: bot.c:75
The bot algorithm.
BotMovementThread(BotTurnController *controller)
Definition: bot_thread.hh:67
virtual ExitCode Entry() override
Definition: bot_thread.cc:63
BotPlacementThread(BotTurnController *controller)
Definition: bot_thread.hh:59
virtual ExitCode Entry() override
Definition: bot_thread.cc:48
void wait_for_exit()
Definition: bot_thread.cc:10
void notify_exit()
Definition: bot_thread.cc:18
wxCondition condvar
Definition: bot_thread.hh:26
wxDECLARE_NO_COPY_CLASS(BotThreadShared)
bool SetName(const wxString &WXUNUSED(name))
Definition: bot_thread.hh:44
BotTurnController * controller
Definition: bot_thread.hh:49
virtual void OnExit() override
Definition: bot_thread.cc:40
std::shared_ptr< BotParameters > bot_params
Definition: bot_thread.hh:51
BetterRng rng
Definition: bot_thread.hh:53
std::unique_ptr< BotState, decltype(&bot_state_free)> bot_state
Definition: bot_thread.hh:52
void cancel()
Definition: bot_thread.cc:36
std::shared_ptr< BotThreadShared > shared
Definition: bot_thread.hh:37
std::unique_ptr< Game, decltype(&game_free)> game
Definition: bot_thread.hh:50
volatile bool * cancelled_ptr
Definition: bot_thread.hh:54
BotThread(BotTurnController *controller)
Definition: bot_thread.cc:25
virtual ~BotThread()
Definition: bot_thread.cc:32
void * ExitCode
void game_free(Game *self)
Destroys a Game, freeing the memory allocated for the struct itself and all associated internal lists...
Definition: game.c:68
The core of the unified game logic library, contains the Game struct.
An implementation of Rng for C++ using the standard functions from <random>.
Contains temporary data created during the evaluation of bot's moves.
Definition: bot.h:101
The central struct of the application, holds the game data and settings.
Definition: game.h:237