Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
234 changes: 151 additions & 83 deletions addons/keh_network/entityinfo.gd

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions addons/keh_network/eventinfo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ extends Reference
class_name NetEventInfo

# Those are just shortcuts
const CTYPE_UINT: int = SnapEntityBase.CTYPE_UINT
const CTYPE_USHORT: int = SnapEntityBase.CTYPE_USHORT
const CTYPE_BYTE: int = SnapEntityBase.CTYPE_BYTE
const CTYPE_UINT: int = 65538
const CTYPE_USHORT: int = 131074
const CTYPE_BYTE: int = 196610

# Type ID of events described by this
var _type_id: int
Expand Down
28 changes: 19 additions & 9 deletions addons/keh_network/network.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
extends Node
class_name Network

const CTYPE_UINT: int = 65538
const CTYPE_BYTE: int = 131074
const CTYPE_USHORT: int = 196610

# TODO:
# - Method to "cull" objects from snapshots which may lead to *some* bandwidth
# savings. One such case would be to eliminate every object that is relatively
Expand Down Expand Up @@ -762,29 +766,35 @@ func get_snap_building_signature() -> int:


# Create an instance of the given snapshot entity class.
func create_snap_entity(eclass: Script, uid: int, class_hash: int) -> SnapEntityBase:
func create_snap_entity(eclass: Script, uid: int, class_hash: int) -> Array:
return snapshot_data._instantiate_snap_entity(eclass, uid, class_hash)


# Add the provided entity into the snapshot that is being built
func snapshot_entity(entity: SnapEntityBase) -> void:
func snapshot_entity(node: Node) -> void:
assert(_update_control.snap)
assert(snapshot_data._entity_name.has(entity.get_script()))
assert(snapshot_data._entity_name.has(node.get_script()))

var ehash: int = snapshot_data._entity_name.get(entity.get_script()).hash
_update_control.snap.add_entity(ehash, entity)
add_node_to_snapshot(node,_update_control.snap)



func correct_in_snapshot(entity: SnapEntityBase, input: InputData) -> void:
assert(snapshot_data._entity_name.has(entity.get_script()))
func correct_in_snapshot(node: Node, input: InputData) -> void:
assert(snapshot_data._entity_name.has(node.get_script()))

var snap: NetSnapshot = snapshot_data.get_snapshot_by_input(input.signature)
if (snap):
var ehash: int = snapshot_data._entity_name.get(entity.get_script()).hash
snap.add_entity(ehash, entity)
add_node_to_snapshot(node,snap)

func add_node_to_snapshot(node: Node, snapshot: NetSnapshot) -> void:
var ehash: int = get_node_ehash(node)
snapshot.add_entity(ehash, get_node_properties(ehash, node))

func get_node_properties(ehash: int, node: Node) -> Array:
return snapshot_data._entity_info[ehash].get_properties_from_node(node)

func get_node_ehash(node: Node) -> int:
return snapshot_data._entity_name.get(node.get_script()).hash

# This function will be automatically called whenever the snapshot is actually finished,
# through the deferred call. This function is meant to iterate through connected players
Expand Down
94 changes: 0 additions & 94 deletions addons/keh_network/snapentity.gd

This file was deleted.

12 changes: 7 additions & 5 deletions addons/keh_network/snapshot.gd
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,21 @@ func get_entity_count(nhash: int) -> int:
assert(_entity_data.has(nhash))
return _entity_data[nhash].size()

func add_entity(nhash: int, entity: SnapEntityBase) -> void:
func add_entity(nhash: int, entity: Array) -> void:
assert(_entity_data.has(nhash))
_entity_data[nhash][entity.id] = entity
_entity_data[nhash][entity[EntityInfo.UID]] = entity

func remove_entity(nhash: int, uid: int) -> void:
assert(_entity_data.has(nhash))
if (_entity_data[nhash].has(uid)):
_entity_data[nhash].erase(uid)

func get_entity(nhash: int, uid: int) -> SnapEntityBase:
func get_entity(nhash: int, uid: int) -> Array:
assert(_entity_data.has(nhash))
return _entity_data[nhash].get(uid)
var ret = _entity_data[nhash].get(uid)
if ret:
return ret
return []


func build_tracker() -> Dictionary:
Expand All @@ -76,4 +79,3 @@ func build_tracker() -> Dictionary:
ret[ehash] = _entity_data[ehash].keys()

return ret

65 changes: 40 additions & 25 deletions addons/keh_network/snapshotdata.gd
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func register_entity_types() -> void:
var pdebug: bool = ProjectSettings.get_setting("keh_addons/network/print_debug_info") if ProjectSettings.has_setting("keh_addons/network/print_debug_info") else false

for c in clist:
# Only interested in classes derived from SnapEntityBase
if (c.base == "SnapEntityBase"):
var edata: EntityInfo = EntityInfo.new(c.class, c.path)
var script: Script = load(c.path)
if script_has_net_vars(script):
var edata: EntityInfo = EntityInfo.new(script,c.class)

if (edata.error.length() > 0):
var msg: String = "Skipping registration of class %s (%d). Reason: %s"
Expand All @@ -91,7 +91,16 @@ func register_entity_types() -> void:
"hash": edata._name_hash,
}

static func extract_class_name(path: String) -> String:
return path.get_file().rstrip(path.get_extension())

static func script_has_net_vars(script: Script) -> bool:
for property in script.get_script_property_list():
# prints(property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE, property.name, property.name.begins_with("net_"))
if property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE and property.name.begins_with("net_"):
# print(property.name)
return true
return false

# Spawners are the main mean to create game nodes in association with the various
# classes derived from SnapEntityBase
Expand Down Expand Up @@ -186,8 +195,8 @@ func reset() -> void:
_isig_to_snap.clear()


func _instantiate_snap_entity(eclass: Script, uid: int, chash: int) -> SnapEntityBase:
var ret: SnapEntityBase = null
func _instantiate_snap_entity(eclass: Script, uid: int, chash: int) -> Array:
var ret: Array

var einfo: EntityInfo = _get_entity_info(eclass)
if (einfo):
Expand Down Expand Up @@ -225,7 +234,7 @@ func encode_full(snap: NetSnapshot, into: EncDecBuffer, isig: int) -> void:
# Encode the entities of this type
for uid in snap._entity_data[ehash]:
# Get the entity in order to encode the properties
var entity: SnapEntityBase = snap.get_entity(ehash, uid)
var entity: Array = snap.get_entity(ehash, uid)

einfo.encode_full_entity(entity, into)

Expand Down Expand Up @@ -273,7 +282,7 @@ func decode_full(from: EncDecBuffer) -> NetSnapshot:
var count: int = from.read_uint()

for _i in count:
var entity: SnapEntityBase = einfo.decode_full_entity(from)
var entity: Array = einfo.decode_full_entity(from)
if (entity):
retval.add_entity(ehash, entity)

Expand Down Expand Up @@ -335,15 +344,15 @@ func encode_delta(snap: NetSnapshot, oldsnap: NetSnapshot, into: EncDecBuffer, i
# Check the entities
for uid in snap._entity_data[ehash]:
# Retrive old state of this entity - obviously if it exists (if not this will be null)
var eold: SnapEntityBase = oldsnap.get_entity(ehash, uid)
var eold: Array = oldsnap.get_entity(ehash, uid)
# Retrieve new state of this entity - it should exist as the iteration is based on the
# new snapshot.
var enew: SnapEntityBase = snap.get_entity(ehash, uid)
var enew: Array = snap.get_entity(ehash, uid)

# Assume the entity is new
var cmask: int = einfo.get_full_change_mask()

if (eold && enew):
if (!eold.empty() && !enew.empty()):
# Ok, entity exist on both snapshots so it's not new. Calculate the "real" change mask
cmask = einfo.calculate_change_mask(eold, enew)
# Remove this from the tracker so it isn't considered as a removed entity
Expand Down Expand Up @@ -377,7 +386,7 @@ func encode_delta(snap: NetSnapshot, oldsnap: NetSnapshot, into: EncDecBuffer, i
into.write_uint(ccount)
written_type_header = true

einfo.encode_delta_entity(uid, null, 0, into)
einfo.encode_delta_entity(uid, [], 0, into)
ccount += 1

if (ccount > 0):
Expand Down Expand Up @@ -435,34 +444,36 @@ func decode_delta(from: EncDecBuffer) -> NetSnapshot:
# Decode them
for _i in count:
var edata: Dictionary = einfo.decode_delta_entity(from)

var oldent: SnapEntityBase = _server_state.get_entity(ehash, edata.entity.id)
var entity: Array = edata.entity
var cmask: int = edata.cmask
var id: int = entity[EntityInfo.UID]
var oldent: Array = _server_state.get_entity(ehash, id)

if (oldent):
# The entity exists in the old state. Check if it's not marked for removal
if (edata.cmask > 0):
if (cmask > 0):
# It isn't. So, "match" the delta to make the data correct (that is, take unchanged)
# data from the old state and apply into the new one.
einfo.match_delta(edata.entity, oldent, edata.cmask)
einfo.match_delta(entity, oldent, cmask)
# Add the changed entity into the return value
retval.add_entity(ehash, edata.entity)
retval.add_entity(ehash, entity)

# This is a changed entity, so remove it from the tracker
tracker[ehash].erase(edata.entity.id)
tracker[ehash].erase(id)

else:
# Entity is not in the old state. Add the decoded data into the return value in the
# hopes it is holding the entire correct data (this can be checked by comparing the cmask though)
# Change mask can be 0 in this case, when the acknowledgement still didn't arrive theren when
# server dispatched a new data set.
if (edata.cmask > 0):
retval.add_entity(ehash, edata.entity)
if (cmask > 0):
retval.add_entity(ehash, entity)

# Check the tracker now
for ehash in tracker:
var einfo: EntityInfo = _entity_info.get(ehash)
for uid in tracker[ehash]:
var entity: SnapEntityBase = _server_state.get_entity(ehash, uid)
var entity: Array = _server_state.get_entity(ehash, uid)
retval.add_entity(ehash, einfo.clone_entity(entity))

return retval
Expand Down Expand Up @@ -538,11 +549,11 @@ func client_check_snapshot(snap: NetSnapshot) -> void:

# Iterate through entities of the server's snapshot
for uid in snap._entity_data[ehash]:
var rentity: SnapEntityBase = snap.get_entity(ehash, uid)
var lentity: SnapEntityBase = local.get_entity(ehash, uid)
var rentity: Array = snap.get_entity(ehash, uid)
var lentity: Array = local.get_entity(ehash, uid)
var node: Node = null

if (rentity && lentity):
if (!rentity.empty() && !lentity.empty()):
# Entity exists on both ends. First update the local_entity array because
# it's meant to hold entities that are present only in the local machine
local_entity.erase(uid)
Expand All @@ -558,12 +569,16 @@ func client_check_snapshot(snap: NetSnapshot) -> void:
# Entity exists only on the server's data. If necessary spawn the game node.
var n: Node = einfo.get_game_node(uid)
if (!n):
node = einfo.spawn_node(uid, rentity.class_hash)
node = einfo.spawn_node(uid, rentity[EntityInfo.CHASH])


if (node):
# If here, then it's necessary to apply the server's state into the node
rentity.apply_state(node)
# rentity.apply_state(node)
# new solution was orignally gonna be node.apply_state(rentity)
# could also be implemented as apply_properties_to_node and then calling just
# node.apply_state() for the node to execute different behaviors
einfo.apply_properties_to_node(node,rentity)

# "Propagate" the server's data into every snapshot in the local history
for s in _history:
Expand Down
1 change: 1 addition & 0 deletions demos/audiomaster/ear_left.png.import
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
Expand Down
1 change: 1 addition & 0 deletions demos/audiomaster/manBrown_stand.png.import
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
Expand Down
3 changes: 1 addition & 2 deletions demos/debughelper/maindbghelper.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,11 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 4.86177 )
[node name="point1" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -3, 2.5, 0 )
mesh = SubResource( 1 )
material/0 = null

[node name="point2" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 3, -2.5, 0 )
mesh = SubResource( 1 )
material/0 = null

[connection signal="pressed" from="canvas/pnl/bt_overlaytoggle" to="." method="_on_bt_overlaytoggle_pressed"]
[connection signal="pressed" from="canvas/pnl/bt_alignleft" to="." method="_on_bt_alignleft_pressed"]
[connection signal="pressed" from="canvas/pnl/bt_aligncenter" to="." method="_on_bt_aligncenter_pressed"]
Expand Down
Loading