aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/shared/ipredictionsystem.h
blob: e9738bc90a24a13ed4e39ad4c72e6d3e01cfcc05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#ifndef IPREDICTIONSYSTEM_H
#define IPREDICTIONSYSTEM_H
#ifdef _WIN32
#pragma once
#endif

#include "predictable_entity.h"

class CBaseEntity;

//-----------------------------------------------------------------------------
// Purpose: Interfaces derived from this are able to filter out the local player
//  when doing prediction on the client, this includes not sending network data to
//  the local player from the server if needed.
//-----------------------------------------------------------------------------
class IPredictionSystem
{
public:
	IPredictionSystem()
	{
		m_pNextSystem = g_pPredictionSystems;
		g_pPredictionSystems = this;

		m_bSuppressEvent = false;
		m_pSuppressHost = NULL;

		m_nStatusPushed = 0;
	};

	virtual ~IPredictionSystem() {};

	IPredictionSystem *GetNext()
	{
		return m_pNextSystem;
	}

	void SetSuppressEvent( bool state )
	{
		m_bSuppressEvent = state;
	}

	void SetSuppressHost( CBaseEntity *host )
	{
		m_pSuppressHost = host;
	}

	CBaseEntity const *GetSuppressHost( void )
	{
		if ( DisableFiltering() )
		{
			return NULL;
		}

		return m_pSuppressHost;
	}

	bool CanPredict( void ) const
	{
		if ( DisableFiltering() )
		{
			return false;
		}

		return !m_bSuppressEvent;
	}

	static IPredictionSystem *g_pPredictionSystems;

	static void SuppressEvents( bool state )
	{
		IPredictionSystem *sys = g_pPredictionSystems;
		while ( sys )
		{
			sys->SetSuppressEvent( state );
			sys = sys->GetNext();
		}
	}

	static void SuppressHostEvents( CBaseEntity *host )
	{
		IPredictionSystem *sys = g_pPredictionSystems;
		while ( sys )
		{
			sys->SetSuppressHost( host );
			sys = sys->GetNext();
		}
	}

private:

	static void Push( void )
	{
		IPredictionSystem *sys = g_pPredictionSystems;
		while ( sys )
		{
			sys->_Push();
			sys = sys->GetNext();
		}
	}

	static void Pop( void )
	{
		IPredictionSystem *sys = g_pPredictionSystems;
		while ( sys )
		{
			sys->_Pop();
			sys = sys->GetNext();
		}
	}

	void _Push( void )
	{
		++m_nStatusPushed;
	}
	void _Pop( void )
	{
		--m_nStatusPushed;
	}

	bool DisableFiltering( void ) const
	{
		return ( m_nStatusPushed > 0  ) ? true : false;
	}

	IPredictionSystem	*m_pNextSystem;
	bool				m_bSuppressEvent;
	CBaseEntity			*m_pSuppressHost;

	int					m_nStatusPushed;

	friend class CDisablePredictionFiltering;
};

class CDisablePredictionFiltering
{
public:
	CDisablePredictionFiltering( bool disable = true )
	{
		m_bDisabled = disable;
		if ( m_bDisabled )
		{
			IPredictionSystem::Push();
		}
	}

	~CDisablePredictionFiltering( void )
	{
		if ( m_bDisabled )
		{
			IPredictionSystem::Pop();
		}
	}
private:
	bool	m_bDisabled;
};

#endif // IPREDICTIONSYSTEM_H