shithub: musw

Download patch

ref: 650a361f5a04e4fc39ade90459d280d227875cb1
parent: 9c20abc1ab976cab00040a32299896dc8ea71e6c
author: rodri <rgl@antares-labs.eu>
date: Mon Jun 5 17:21:56 EDT 2023

use a queue for player input to dampen race conditions between the netcode and the sims.

--- a/dat.h
+++ b/dat.h
@@ -213,6 +213,7 @@
 {
 	char *name;
 	NetConn *conn;
+	Channel *inputq;
 	ulong oldkdown, kdown;
 	Player *next;
 };
--- a/muswd.c
+++ b/muswd.c
@@ -233,7 +233,7 @@
 				if(debug)
 					fprint(2, "\t%.*lub\n", sizeof(kdown)*8, kdown);
 
-				nc->player->kdown = kdown;
+				nbsendul(nc->player->inputq, kdown);
 
 				break;
 			case NCbuhbye:
@@ -333,6 +333,7 @@
 threadsim(void *)
 {
 	int i;
+	ulong kdown;
 	uvlong then, now;
 	double frametime, Δt;
 	Ioproc *io;
@@ -358,6 +359,9 @@
 			for(i = 0; i < nelem(p->players); i++){
 				player = p->players[i];
 				ship = &p->u->ships[i];
+
+				if(nbrecv(player->inputq, &kdown) != 0)
+					player->kdown = kdown;
 
 				if((player->kdown & 1<<Kquit) != 0){
 					np = p->next;
--- a/pack.c
+++ b/pack.c
@@ -3,6 +3,7 @@
 #include <ip.h>
 #include <mp.h>
 #include <libsec.h>
+#include <thread.h>
 #include <draw.h>
 #include <geometry.h>
 #include "dat.h"
--- a/party.c
+++ b/party.c
@@ -3,6 +3,7 @@
 #include <ip.h>
 #include <mp.h>
 #include <libsec.h>
+#include <thread.h>
 #include <draw.h>
 #include <geometry.h>
 #include "dat.h"
--- a/physics.c
+++ b/physics.c
@@ -3,6 +3,7 @@
 #include <ip.h>
 #include <mp.h>
 #include <libsec.h>
+#include <thread.h>
 #include <draw.h>
 #include <geometry.h>
 #include "dat.h"
--- a/player.c
+++ b/player.c
@@ -3,6 +3,7 @@
 #include <ip.h>
 #include <mp.h>
 #include <libsec.h>
+#include <thread.h>
 #include <draw.h>
 #include <geometry.h>
 #include "dat.h"
@@ -16,6 +17,7 @@
 	p = emalloc(sizeof(Player));
 	p->name = name? strdup(name): nil;
 	p->conn = nc;
+	p->inputq = chancreate(sizeof(ulong), 20);
 	p->oldkdown = p->kdown = 0;
 	p->next = nil;
 
@@ -26,6 +28,7 @@
 delplayer(Player *p)
 {
 	free(p->name);
+	chanclose(p->inputq);
 	free(p);
 }
 
--- a/universe.c
+++ b/universe.c
@@ -3,6 +3,7 @@
 #include <ip.h>
 #include <mp.h>
 #include <libsec.h>
+#include <thread.h>
 #include <draw.h>
 #include <geometry.h>
 #include "dat.h"