Example 3 might look like this:
class snapshot [
vector pos, vel;
uint frame; // This shouldn't be needed, but I'm including it in case I've overlooked something that will show up later.
snapshot() {
frame = C.frame;
}
snapshot (vector p, vector v, uint f) {
pos = 1.0 * p;
vel = 1.0 * v;
frame = f;
}
}
snapshot@[] history;
snapshot@ history_add (vector p, vector v, uint f=-1) {
if (f == -1) f=C.frame;
snapshot ret (p, v, f);
history.insert_at(0, ret);
// We don't want to waste RAM or allow rewinding too far, so keep the history from growing too long:
if(history.length() > 10) history.resize(10);
return ret;
}
// The following goes in the ball class:
bool revert(uint f) {
int df = C.frame-f;
if(df<0) {
// We don't want to skip ahead, or at most we might skip a single frame.
// In this case, I think the correct response is to send a message to the other player's game, since the general rule is that the slower peer takes priority.
return false;
}
else if(df >= history.length() ) {
// In this case, the other player is too far behind to adjust for with this method.
// I'd suggest sending a network event telling the other player's game to skip ahead to the latest frame you can revert to.
// I'd suggest going with df=history.length()-1, since being this far behind suggests there might be more lag to come, and you don't want to stretch things out by the maximum in that case, otherwise you'll be constantly falling into this case.
return false;
}
pos = history[df].pos;
vel = history[df].vel;
C.frame = f;
while(df>0) {
history.remove_at(0);
df--;
}
return true;
}
I lef! out the two out-ofbounds responses because they depend on how you're using the network. Those cases return false, so you could just respond to that return value by sending the "way too out of sync" message. However, you'd still need to revert in the too-far-behind case. I left out the implementation there because that's the part you'd probably wind up adjusting based on how it goes in tests.
A forced jump forward would be better executing the skipped frames in sequence, just skipping the Ctick.
I have not compiled or tested the above example; it's intended to illustrate the concept. You can try to use it as written, but it will inevitably need adjusting for details on your end I'm not fully aware of.
hth
看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
George... Don't do that.