Pie Noon
An open source project by FPL.
 All Classes Pages
multiplayer_director.h
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef MULTIPLAYER_DIRECTOR_H_
16 #define MULTIPLAYER_DIRECTOR_H_
17 
18 #include <vector>
19 #include "common.h"
20 #include "controller.h"
21 #include "game_state.h"
22 #include "multiplayer_controller.h"
23 #include "multiplayer_generated.h"
24 #include "pie_noon_game.h"
25 
26 #ifdef PIE_NOON_USES_GOOGLE_PLAY_GAMES
27 #include "gpg_multiplayer.h"
28 #endif
29 
30 namespace fpl {
31 namespace pie_noon {
32 
33 // MultiplayerDirector is used for the multiscreen version of Pie Noon.
34 //
35 // It is responsible for managing the timings of all of the turns, receiving the
36 // messages from clients saying what actions they wish to perform, and for the
37 // formatting and sending of multiplayer messages between the host and client.
38 //
39 // When you create MultiplayerDirector, you must register multiple
40 // MultiplayerControllers (one for each player), which is how
41 // MultiplayerDirector can direct what the characters do each turn.
43  public:
45 
46  // Give the multiplayer director everything it will need.
47  void Initialize(GameState *gamestate_ptr, const Config *config);
48 #ifdef PIE_NOON_USES_GOOGLE_PLAY_GAMES
49  // Register a pointer to GPGMultiplayer, so we can send multiplayer messages.
50  void RegisterGPGMultiplayer(GPGMultiplayer *gpg_multiplayer) {
51  gpg_multiplayer_ = gpg_multiplayer;
52  }
53 #endif
54  // Register one MultiplayerController assigned to each player.
55  void RegisterController(MultiplayerController *);
56 
57  // Call this each frame if multiplayer gameplay is going on.
58  void AdvanceFrame(WorldTime delta_time);
59 
60  // Start a new multi-screen game.
61  void StartGame();
62 
63  // End the multi-screen game.
64  void EndGame();
65 
66  // If testing on PC, pass in your PC keyboard input system to use debug
67  // keys for testing turn-based timings.
68  void SetDebugInputSystem(fplbase::InputSystem *input) {
69  debug_input_system_ = input;
70  }
71 
72 #ifdef PIE_NOON_USES_GOOGLE_PLAY_GAMES
73  // Tell one of your connected players what his player number is.
74  void SendPlayerAssignmentMsg(const std::string &instance, CharacterId id);
75  // Broadcast start-of-turn to the players.
76  void SendStartTurnMsg(unsigned int turn_seconds);
77  // Broadcast end-of-game message to the players.
78  void SendEndGameMsg();
79  // Broadcast player health to the players.
80  void SendPlayerStatusMsg();
81 #endif
82 
83  // Takes effect when the next turn starts.
84  void set_seconds_per_turn(unsigned int seconds) {
85  seconds_per_turn_ = seconds;
86  }
87 
88  unsigned int seconds_per_turn() { return seconds_per_turn_; }
89 
90  // First turn is numbered 1, second turn numbered 2, etc.
91  // Is 0 before the first turn starts.
92  unsigned int turn_number() { return turn_number_; }
93 
94  // Tell the multiplayer director about a player's input.
95  void InputPlayerCommand(CharacterId id,
96  const multiplayer::PlayerCommand &command);
97 
98  // Internally, call this when a player has been hit by a pie. The multiplayer
99  // director will decide whether that player should be "stunned" by the hit
100  // and have one or more of his buttons locked for a turn.
101  void TriggerPlayerHitByPie(CharacterId player, int damage);
102 
103  // Is this an AI player or a human player?
104  bool IsAIPlayer(CharacterId player);
105 
106  // How long until the current turn ends? 0 if outside a turn.
107  WorldTime turn_timer() { return turn_timer_; }
108  // How long until the next turn starts? 0 if in a turn.
109  WorldTime start_turn_timer() { return start_turn_timer_; }
110 
111  // Set the number of AI players. The last N players are AIs.
112  void set_num_ai_players(unsigned int n) { num_ai_players_ = n; }
113  unsigned int num_ai_players() const { return num_ai_players_; }
114 
115  private:
116  struct Command {
117  CharacterId aim_at;
118  bool is_firing;
119  bool is_blocking;
120  Command() : aim_at(-1), is_firing(false), is_blocking(false) {}
121  };
122 
123  void TriggerStartOfTurn();
124  void TriggerEndOfTurn();
125  unsigned int CalculateSecondsPerTurn(unsigned int turn_number);
126 
127  // Get all the players' healths so we can send them in an update
128  std::vector<uint8_t> ReadPlayerHealth();
129 
130  // Tell the multiplayer director to choose AI commands for this player.
131  void ChooseAICommand(CharacterId id);
132 
133  void DebugInput(fplbase::InputSystem *input);
134 
135  // Get all the players' onscreen splats to send in an update
136  std::vector<uint8_t> ReadPlayerSplats();
137 
138  GameState *gamestate_; // Pointer to the gamestate object
139  const Config *config_; // Pointer to the config structure
140 
141  std::vector<MultiplayerController *> controllers_;
142  std::vector<uint8_t> character_splats_;
143  // How long the current turn lasts.
144  WorldTime turn_timer_;
145  // In how long to start the next turn.
146  WorldTime start_turn_timer_;
147  unsigned int seconds_per_turn_;
148  unsigned int turn_number_;
149 
150  unsigned int num_ai_players_; // the last N players are AI.
151 
152  fplbase::InputSystem *debug_input_system_;
153 
154  std::vector<Command> commands_;
155 
156 #ifdef PIE_NOON_USES_GOOGLE_PLAY_GAMES
157  GPGMultiplayer *gpg_multiplayer_ = nullptr;
158 #endif
159 
160  bool game_running_;
161 };
162 
163 } // pie_noon
164 } // fpl
165 
166 #endif // MULTIPLAYER_DIRECTOR_H_
Definition: multiplayer_controller.h:32
Definition: game_state.h:56
Definition: gpg_multiplayer.h:63
Definition: multiplayer_director.h:42