1 // ==============================================================
2 //	This file is part of Glest (www.glest.org)
3 //
4 //	Copyright (C) 2001-2008 Martiño Figueroa
5 //
6 //	You can redistribute this code and/or modify it under
7 //	the terms of the GNU General Public License as published
8 //	by the Free Software Foundation; either version 2 of the
9 //	License, or (at your option) any later version
10 // ==============================================================
11 
12 #ifndef _GLEST_GAME_COMMANDER_H_
13 #define _GLEST_GAME_COMMANDER_H_
14 
15 #ifdef WIN32
16     #include <winsock2.h>
17     #include <winsock.h>
18 #endif
19 
20 #include <vector>
21 #include "vec.h"
22 #include "selection.h"
23 #include "command_type.h"
24 #include "platform_util.h"
25 #include "base_thread.h"
26 #include "leak_dumper.h"
27 
28 using std::vector;
29 
30 namespace Glest{ namespace Game{
31 
32 using Shared::Graphics::Vec2i;
33 using Shared::PlatformCommon::Chrono;
34 
35 class World;
36 class Unit;
37 class Command;
38 class CommandType;
39 class NetworkCommand;
40 class Game;
41 class SwitchTeamVote;
42 
43 // =====================================================
44 // 	class Commander
45 //
46 ///	Gives commands to the units
47 // =====================================================
48 class Commander {
49 private:
50 	typedef vector<std::pair<CommandResult,string> > CommandResultContainer;
51 
52 private:
53     World *world;
54 	Chrono perfTimer;
55 
56 	std::vector<std::pair<int,NetworkCommand> > replayCommandList;
57 
58 	bool pauseNetworkCommands;
59 
60 public:
61     Commander();
62     virtual ~Commander();
63 
getPauseNetworkCommands()64     bool getPauseNetworkCommands() const { return this->pauseNetworkCommands; }
setPauseNetworkCommands(bool pause)65     void setPauseNetworkCommands(bool pause) { this->pauseNetworkCommands = pause; }
66 
67     void signalNetworkUpdate(Game *game);
68     void init(World *world);
69 	void updateNetwork(Game *game);
70 
71 	void addToReplayCommandList(NetworkCommand &command,int worldFrameCount);
72 	bool getReplayCommandListForFrame(int worldFrameCount);
73 	bool hasReplayCommandListForFrame() const;
74 	int getReplayCommandListForFrameCount() const;
75 
76 	std::pair<CommandResult,string> tryGiveCommand(const Selection *selection, const CommandType *commandType,
77 										const Vec2i &pos, const UnitType* unitType,
78 										CardinalDir facing, bool tryQueue,Unit *targetUnit=NULL) const;
79 
80 	std::pair<CommandResult,string> tryGiveCommand(const Unit* unit, const CommandType *commandType, const Vec2i &pos, const UnitType* unitType, CardinalDir facing, bool tryQueue = false,Unit *targetUnit=NULL,int unitGroupCommandId=-1) const;
81 	std::pair<CommandResult,string> tryGiveCommand(const Selection *selection, CommandClass commandClass, const Vec2i &pos= Vec2i(0), const Unit *targetUnit= NULL, bool tryQueue = false) const;
82 	std::pair<CommandResult,string> tryGiveCommand(const Selection *selection, const CommandType *commandType, const Vec2i &pos= Vec2i(0), const Unit *targetUnit= NULL, bool tryQueue = false) const;
83 	std::pair<CommandResult,string> tryGiveCommand(const Selection *selection, const Vec2i &pos, const Unit *targetUnit= NULL, bool tryQueue = false, int unitCommandGroupId = -1) const;
84 	CommandResult tryCancelCommand(const Selection *selection) const;
85 	void trySetMeetingPoint(const Unit* unit, const Vec2i &pos) const;
86 	void trySwitchTeam(const Faction* faction, int teamIndex) const;
87 	void trySwitchTeamVote(const Faction* faction, SwitchTeamVote *vote) const;
88 	void tryDisconnectNetworkPlayer(const Faction* faction, int playerIndex) const;
89 
90 	void tryPauseGame(bool joinNetworkGame, bool clearCaches) const;
91 	void tryResumeGame(bool joinNetworkGame, bool clearCaches) const;
92 
93 	void tryNetworkPlayerDisconnected(int factionIndex) const;
94 
95 	Command* buildCommand(const NetworkCommand* networkCommand) const;
96 
97 private:
98 	std::pair<CommandResult,string> pushNetworkCommand(const NetworkCommand* networkCommand) const;
99 	std::pair<CommandResult,string> computeResult(const CommandResultContainer &results) const;
100 	void giveNetworkCommand(NetworkCommand* networkCommand) const;
101 	bool canSubmitCommandType(const Unit *unit, const CommandType *commandType) const;
102 };
103 
104 }} //end namespace
105 
106 #endif
107