diff options
author | BobTheBob <32057864+BobTheBob9@users.noreply.github.com> | 2021-06-22 14:30:49 +0100 |
---|---|---|
committer | BobTheBob <32057864+BobTheBob9@users.noreply.github.com> | 2021-06-22 14:30:49 +0100 |
commit | 207facbc402f5639cbcd31f079214351ef605cf2 (patch) | |
tree | 4710b2a88dd64f3dfea1609d31a5de9141640951 /Northstar.CustomServers/scripts/vscripts/_networkvars.gnut | |
parent | c2d438568df6d98cf731807e30eaa7da31e5ea52 (diff) | |
download | NorthstarMods-207facbc402f5639cbcd31f079214351ef605cf2.tar.gz NorthstarMods-207facbc402f5639cbcd31f079214351ef605cf2.zip |
initial commit after moving to new repo
Diffstat (limited to 'Northstar.CustomServers/scripts/vscripts/_networkvars.gnut')
-rw-r--r-- | Northstar.CustomServers/scripts/vscripts/_networkvars.gnut | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/Northstar.CustomServers/scripts/vscripts/_networkvars.gnut b/Northstar.CustomServers/scripts/vscripts/_networkvars.gnut new file mode 100644 index 00000000..14990a15 --- /dev/null +++ b/Northstar.CustomServers/scripts/vscripts/_networkvars.gnut @@ -0,0 +1,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 ) + } + } + } +} + |