shithub: musw

Download patch

ref: 826fce49c5319d0965c603dc950623cc07996376
parent: 00f7d8dd3dc47ed3cfa951325e809a92c37341b7
author: rodri <rgl@antares-labs.eu>
date: Sun Apr 23 10:33:14 EDT 2023

only send/recv the fired bullets on simstate packets.

--- a/musw.c
+++ b/musw.c
@@ -327,6 +327,7 @@
 threadnetppu(void *)
 {
 	int i, j;
+	int nfired[2], bi;
 	uchar *bufp;
 	Frame *frame, *newf;
 
@@ -384,11 +385,20 @@
 					&universe->ships[1].p, &universe->ships[1].θ,
 					&universe->star.p);
 
-					/* TODO: only recv the fired ones */
-					for(i = 0; i < nelem(universe->ships); i++)
-						for(j = 0; j < nelem(universe->ships[i].rounds); j++)
-							bufp += unpack(bufp, frame->len - (bufp-frame->data), "Pd",
-								&universe->ships[i].rounds[j].p, &universe->ships[i].rounds[j].θ);
+				bufp += unpack(bufp, frame->len - (bufp-frame->data), "bb", &nfired[0], &nfired[1]);
+
+				if(debug)
+					fprint(2, "nfired0 %d nfired1 %d\n", nfired[0], nfired[1]);
+
+				for(i = 0; i < nelem(universe->ships); i++)
+					for(j = 0; j < nfired[i]; j++){
+						bufp += unpack(bufp, frame->len - (bufp-frame->data), "b",
+							&bi);
+						if(debug)
+							fprint(2, "bi %d\n", bi);
+						bufp += unpack(bufp, frame->len - (bufp-frame->data), "Pd",
+							&universe->ships[i].rounds[bi].p, &universe->ships[i].rounds[bi].θ);
+					}
 				break;
 			case NSnudge:
 				newf = newframe(nil, NCnudge, frame->seq+1, frame->seq, 0, nil);
--- a/muswd.c
+++ b/muswd.c
@@ -279,6 +279,7 @@
 broadcaststate(void)
 {
 	int i, j, k;
+	int nfired[2];
 	uchar *bufp;
 	Frame *frame;
 	NetConn *pnc;
@@ -296,11 +297,19 @@
 				p->u->ships[1].p, p->u->ships[1].θ,
 				p->u->star.p);
 
-			/* TODO: only send the fired ones */
+			nfired[0] = nfired[1] = 0;
 			for(j = 0; j < nelem(p->u->ships); j++)
 				for(k = 0; k < nelem(p->u->ships[j].rounds); k++)
-					bufp += pack(bufp, frame->len - (bufp-frame->data), "Pd",
-						p->u->ships[j].rounds[k].p, p->u->ships[j].rounds[k].θ);
+					if(p->u->ships[j].rounds[k].fired)
+						nfired[j]++;
+
+			bufp += pack(bufp, frame->len - (bufp-frame->data), "bb", nfired[0], nfired[1]);
+
+			for(j = 0; j < nelem(p->u->ships); j++)
+				for(k = 0; k < nelem(p->u->ships[j].rounds); k++)
+					if(p->u->ships[j].rounds[k].fired)
+						bufp += pack(bufp, frame->len - (bufp-frame->data), "bPd",
+							k, p->u->ships[j].rounds[k].p, p->u->ships[j].rounds[k].θ);
 
 			signframe(frame, pnc->dh.priv);
 
--- a/pack.c
+++ b/pack.c
@@ -45,6 +45,14 @@
 		switch(*fmt++){
 		case '\0':
 			return p - p0;
+		case 'b':
+			k = va_arg(a, ulong);
+
+			if(p+1 > e)
+				goto err;
+
+			*p++ = k;
+			break;
 		case 'd':
 			d.x = va_arg(a, double);
 
@@ -119,6 +127,12 @@
 		switch(*fmt++){
 		case '\0':
 			return p - p0;
+		case 'b':
+			if(p+1 > e)
+				goto err;
+
+			*va_arg(a, ulong*) = *p++;
+			break;
 		case 'd':
 			if(p+8 > e)
 				goto err;
--- a/todo
+++ b/todo
@@ -13,6 +13,7 @@
 	[✓] waiting for a player
 	[✓] main game
 [ ] reduce the amount of data sent on every NSsimstate packet
+	[✓] only send the fired bullets
 [?] the client must try to connect continously
 	> there's an error in the udp stack that doesn't allow the client to receive packets if run before the server is up.
 [ ] more realistic DEC Type 30 CRT emulation