Skip to content

Commit fe011dd

Browse files
authored
fix: Limit rollback ticks (#324)
Closes #323
1 parent 42536a7 commit fe011dd

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

addons/netfox.extras/plugin.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
name="netfox.extras"
44
description="Game-specific utilities for Netfox"
55
author="Tamas Galffy"
6-
version="1.11.0"
6+
version="1.11.1"
77
script="netfox-extras.gd"

addons/netfox.internals/plugin.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
name="netfox.internals"
44
description="Shared internals for netfox addons"
55
author="Tamas Galffy"
6-
version="1.11.0"
6+
version="1.11.1"
77
script="plugin.gd"

addons/netfox.noray/plugin.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
name="netfox.noray"
44
description="Bulletproof your connectivity with noray integration for netfox"
55
author="Tamas Galffy"
6-
version="1.11.0"
6+
version="1.11.1"
77
script="netfox-noray.gd"

addons/netfox/plugin.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
name="netfox"
44
description="Shared internals for netfox addons"
55
author="Tamas Galffy"
6-
version="1.11.0"
6+
version="1.11.1"
77
script="netfox.gd"

addons/netfox/rollback/network-rollback.gd

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
extends Node
2+
class_name _NetworkRollback
23

34
## Whether rollback is enabled.
45
var enabled: bool = ProjectSettings.get_setting("netfox/rollback/enabled", true)
@@ -7,7 +8,9 @@ var enabled: bool = ProjectSettings.get_setting("netfox/rollback/enabled", true)
78
##
89
## The larger the history limit, the further we can roll back into the past,
910
## thus the more latency we can manage.
10-
##
11+
## [br][br]
12+
## Rollback won't go further than this limit, regardless of inputs received.
13+
## [br][br]
1114
## [i]read-only[/i], you can change this in the project settings
1215
var history_limit: int:
1316
get:
@@ -21,7 +24,7 @@ var history_limit: int:
2124
## state of the game, but let's say the state two frames ago ( offset = 2 ).
2225
## This can help with hiding latency, by giving more time for an up-to-date
2326
## state to arrive before we try to display it.
24-
##
27+
## [br][br]
2528
## [i]read-only[/i], you can change this in the project settings
2629
var display_offset: int:
2730
get:
@@ -33,7 +36,7 @@ var display_offset: int:
3336
##
3437
## With UDP - packets may be lost, arrive late or out of order.
3538
## To mitigate this, we send the current and previous n ticks of input data.
36-
##
39+
## [br][br]
3740
## [i]read-only[/i], you can change this in the project settings
3841
var input_redundancy: int:
3942
get:
@@ -78,6 +81,8 @@ var _resim_from: int
7881
var _is_rollback: bool = false
7982
var _simulated_nodes: Dictionary = {}
8083

84+
static var _logger: _NetfoxLogger = _NetfoxLogger.for_netfox("NetworkRollback")
85+
8186
## Submit the resimulation start tick for the current loop.
8287
##
8388
## This is used to determine the resimulation range during each loop.
@@ -97,7 +102,7 @@ func notify_simulated(node: Node):
97102
##
98103
## This is used mostly internally by [RollbackSynchronizer]. The idea is to
99104
## submit each affected node while preparing the tick, and then use
100-
## [code]is_simulated[/code] to run only the nodes that need to be resimulated.
105+
## [member is_simulated] to run only the nodes that need to be resimulated.
101106
func is_simulated(node: Node):
102107
return _simulated_nodes.has(node)
103108

@@ -117,10 +122,10 @@ func is_rollback_aware(what: Object) -> bool:
117122
## simulation for the given rollback tick.
118123
##
119124
## This is used by [RollbackSynchronizer] to resimulate ticks during rollback.
120-
## While the _rollback_tick method could be called directly as well, this method
121-
## exists to future-proof the code a bit, so the method name is not repeated all
122-
## over the place.
123-
##
125+
## While the [code]_rollback_tick[/code] method could be called directly as
126+
## well, this method exists to future-proof the code a bit, so the method name
127+
## is not repeated all over the place.
128+
## [br][br]
124129
## [i]Note:[/i] Make sure to check if the target is rollback-aware, because if
125130
## it's not, this method will run into an error.
126131
func process_rollback(target: Object, delta: float, p_tick: int, is_fresh: bool):
@@ -144,7 +149,15 @@ func _rollback():
144149

145150
# to = Current tick
146151
var to = NetworkTime.tick
147-
152+
153+
# Limit number of rollback ticks
154+
if to - from > history_limit:
155+
_logger.warning(
156+
"Trying to run rollback for ticks %d to %d, past the history limit of %d" %
157+
[from, to, history_limit]
158+
)
159+
from = NetworkTime.tick - history_limit
160+
148161
# for tick in from .. to:
149162
for tick in range(from, to):
150163
_tick = tick

addons/netfox/rollback/rollback-synchronizer.gd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ func _submit_input(input: Dictionary, tick: int):
251251
for property in sanitized:
252252
for i in range(0, sanitized[property].size()):
253253
var t = tick - i
254+
if t < NetworkTime.tick - NetworkRollback.history_limit:
255+
# Input too old
256+
_logger.error("Received input for %s, rejecting because older than %s frames" % [t, NetworkRollback.history_limit])
257+
continue
258+
254259
var old_input = _inputs.get(t, {}).get(property)
255260
var new_input = sanitized[property][i]
256261

0 commit comments

Comments
 (0)