1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef GAME_SERVER_GAMECONTROLLER_H
4#define GAME_SERVER_GAMECONTROLLER_H
5
6#include <base/vmath.h>
7#include <engine/map.h>
8#include <engine/shared/protocol.h>
9#include <game/server/teams.h>
10
11struct CScoreLoadBestTimeResult;
12
13/*
14 Class: Game Controller
15 Controls the main game logic. Keeping track of team and player score,
16 winning conditions and specific game logic.
17*/
18class IGameController
19{
20 friend class CSaveTeam; // need access to GameServer() and Server()
21
22 std::vector<vec2> m_avSpawnPoints[3];
23
24 class CGameContext *m_pGameServer;
25 class CConfig *m_pConfig;
26 class IServer *m_pServer;
27
28 CGameTeams m_Teams;
29
30protected:
31 CGameContext *GameServer() const { return m_pGameServer; }
32 CConfig *Config() { return m_pConfig; }
33 IServer *Server() const { return m_pServer; }
34
35 void DoActivityCheck();
36
37 struct CSpawnEval
38 {
39 CSpawnEval()
40 {
41 m_Got = false;
42 m_FriendlyTeam = -1;
43 m_Pos = vec2(100, 100);
44 }
45
46 vec2 m_Pos;
47 bool m_Got;
48 int m_FriendlyTeam;
49 float m_Score;
50 };
51
52 float EvaluateSpawnPos(CSpawnEval *pEval, vec2 Pos, int DDTeam);
53 void EvaluateSpawnType(CSpawnEval *pEval, int Type, int DDTeam);
54
55 void ResetGame();
56
57 char m_aMapWish[MAX_MAP_LENGTH];
58
59 int m_RoundStartTick;
60 int m_GameOverTick;
61 int m_SuddenDeath;
62
63 int m_Warmup;
64 int m_RoundCount;
65
66 int m_GameFlags;
67 int m_UnbalancedTick;
68 bool m_ForceBalanced;
69
70public:
71 const char *m_pGameType;
72
73 IGameController(class CGameContext *pGameServer);
74 virtual ~IGameController();
75
76 // event
77 /*
78 Function: OnCharacterDeath
79 Called when a CCharacter in the world dies.
80
81 Arguments:
82 victim - The CCharacter that died.
83 killer - The player that killed it.
84 weapon - What weapon that killed it. Can be -1 for undefined
85 weapon when switching team or player suicides.
86 */
87 virtual int OnCharacterDeath(class CCharacter *pVictim, class CPlayer *pKiller, int Weapon);
88 /*
89 Function: OnCharacterSpawn
90 Called when a CCharacter spawns into the game world.
91
92 Arguments:
93 chr - The CCharacter that was spawned.
94 */
95 virtual void OnCharacterSpawn(class CCharacter *pChr);
96
97 virtual void HandleCharacterTiles(class CCharacter *pChr, int MapIndex);
98
99 /*
100 Function: OnEntity
101 Called when the map is loaded to process an entity
102 in the map.
103
104 Arguments:
105 index - Entity index.
106 pos - Where the entity is located in the world.
107
108 Returns:
109 bool?
110 */
111 virtual bool OnEntity(int Index, int x, int y, int Layer, int Flags, bool Initial, int Number = 0);
112
113 virtual void OnPlayerConnect(class CPlayer *pPlayer);
114 virtual void OnPlayerDisconnect(class CPlayer *pPlayer, const char *pReason);
115
116 virtual void OnReset();
117
118 // game
119 void DoWarmup(int Seconds);
120
121 void StartRound();
122 void EndRound();
123 void ChangeMap(const char *pToMap);
124
125 bool IsForceBalanced();
126
127 /*
128
129 */
130 virtual bool CanBeMovedOnBalance(int ClientId);
131
132 virtual void Tick();
133
134 virtual void Snap(int SnappingClient);
135
136 //spawn
137 virtual bool CanSpawn(int Team, vec2 *pOutPos, int DDTeam);
138
139 virtual void DoTeamChange(class CPlayer *pPlayer, int Team, bool DoChatMsg = true);
140 /*
141
142 */
143 virtual const char *GetTeamName(int Team);
144 virtual int GetAutoTeam(int NotThisId);
145 virtual bool CanJoinTeam(int Team, int NotThisId, char *pErrorReason, int ErrorReasonSize);
146 int ClampTeam(int Team);
147
148 CClientMask GetMaskForPlayerWorldEvent(int Asker, int ExceptID = -1);
149
150 bool IsTeamPlay() { return m_GameFlags & GAMEFLAG_TEAMS; }
151 // DDRace
152
153 float m_CurrentRecord;
154 CGameTeams &Teams() { return m_Teams; }
155 std::shared_ptr<CScoreLoadBestTimeResult> m_pLoadBestTimeResult;
156};
157
158#endif
159