aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/_networkvars.gnut
blob: 14990a152e99f6fb47b207834529f0f1914dc0d4 (plain)
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
165
166
167
168
169
untyped


global function SetEntityVar
global function SetServerVar
global function SetNetworkVar
global function SyncServerVars
global function SyncEntityVars


function SetEntityVar( entity ent, varName, value )
{
	Assert( IsServer() )
	Assert( varName in _entityClassVars[ent.GetClassName()], "Entity " + ent + " does not have remote var " + varName )
	Assert( varName in _entityClassVarsIsEnts[ent.GetClassName()] )
	Assert( varName in _entityClassVarsSyncToAllClients[ent.GetClassName()] )
	Assert( typeof value != "string" )

	Assert( "_entityVars" in ent )

	if ( ent._entityVars[varName] == value )
		return

	ent._entityVars[varName] = value

	if ( _entityClassVarsIsEnts[ent.GetClassName()][varName] && value != null )
	{
		//printl( "SET NETWORK ENTITY VAR TO AN ENTITY. GETTING EHANDLE" )
		value = value.GetEncodedEHandle()
	}

	local syncToAllPlayers = _entityClassVarsSyncToAllClients[ent.GetClassName()][varName]

	// only sync "player" variables to that player
	if ( ent.IsPlayer() && !ent.IsBot() && !syncToAllPlayers )
	{
		if ( !ent.p.clientScriptInitialized )
			return

		Remote_CallFunction_NonReplay( ent, "ServerCallback_SetEntityVar", ent.GetEncodedEHandle(), _entityClassVarHandles[varName], value )
	}
	else
	{
		array<entity> players = GetPlayerArray()
		foreach ( player in players )
		{
			if ( player.IsBot() )
				continue

			if ( !player.p.clientScriptInitialized )
				continue

			Remote_CallFunction_NonReplay( player, "ServerCallback_SetEntityVar", ent.GetEncodedEHandle(), _entityClassVarHandles[varName], value )
		}
	}
}

function SetServerVar( varName, value )
{
	Assert( IsServer() )
	Assert( varName in _serverVars )
	Assert( typeof value != "string" )
	expect string( varName )

	if ( _serverVars[varName] == value )
		return

	_serverVars[varName] = value

	if ( varName in _serverEntityVars && value != null )
	{
		if ( IsValid( value ) )
			value = value.GetEncodedEHandle()
		else
			value = null
	}

	// Run server script change callback if one exists
	thread ServerVarChangedCallbacks( varName )

	// Update the var on all clients
	array<entity> players = GetPlayerArray()
	foreach ( player in players )
	{
		if ( !player.p.clientScriptInitialized )
			continue

		Remote_CallFunction_NonReplay( player, "ServerCallback_SetServerVar", _serverVarHandles[varName], value )
	}
}

function SetNetworkVar( obj, varName, value )
{
	if ( obj == level )
	{
		return SetServerVar( varName, value )
	}
	else
	{
		expect entity( obj )
		return SetEntityVar( obj, varName, value )
	}
}

function SyncServerVars( entity player )
{
	Assert( IsServer() )

	foreach ( varName, value in _serverVars )
	{
		if ( varName in _serverEntityVars && value != null )
		{
			if ( IsValid( value ) )
				value = value.GetEncodedEHandle()
			else
				value = null
		}

		Remote_CallFunction_NonReplay( player, "ServerCallback_SetServerVar", _serverVarHandles[varName], value )
	}
}

function SyncEntityVars( entity player )
{
	Assert( IsServer() )

	foreach ( className, _ in _entityClassVars )
	{
		array<entity> entities
		if ( className == "player" )
			entities = GetPlayerArray()
		else
			entities = GetNPCArrayByClass( className )

		foreach ( ent in entities )
		{
			if ( !IsValid( ent ) )
				continue

			foreach( varName, value in _entityClassVars[className] )
			{
				local entValue = ent._entityVars[varName]
				if ( entValue == value )
					continue

				if ( !_entityClassVarsSyncToAllClients[className][varName] && ent != player )
				{
					Assert( className == "player" )
					continue
				}
				//if ( className == "player" && !_entityClassVarsSyncToAllClients[className][varName] )
				//	continue
                //
				if ( _entityClassVarsIsEnts[className][varName] )
				{
					if ( !IsValid( entValue ) )
						continue
					// if this is an entity var, change over to e-handle
					entValue = entValue.GetEncodedEHandle()
				}

				Assert( player.p.clientScriptInitialized )

				Remote_CallFunction_NonReplay( player, "ServerCallback_SetEntityVar", ent.GetEncodedEHandle(), _entityClassVarHandles[varName], entValue )
			}
		}
	}
}