shithub: musw

Download patch

ref: 5052a27ece26976e3bec769896bbdc65d9e5ede2
parent: d40305baf8a0722cf6128520a5b792fc173c5a93
author: rodri <rgl@antares-labs.eu>
date: Thu Feb 23 11:31:16 EST 2023

(inefficiently) send bullets state and paint them.

--- a/musw.c
+++ b/musw.c
@@ -111,6 +111,28 @@
 }
 
 void
+drawbullets(Ship *ship, Image *dst)
+{
+	int i;
+	Bullet *b;
+	Point2 v;
+
+	for(i = 0; i < nelem(ship->rounds); i++){
+		b = &ship->rounds[i];
+		v = Vec2(-1,0); /* it's pointing backwards to paint the tail */
+		Matrix R = {
+			cos(b->θ), -sin(b->θ), 0,
+			sin(b->θ),  cos(b->θ), 0,
+			0, 0, 1,
+		};
+
+		v = xform(v, R);
+		line(dst, toscreen(b->p), toscreen(addpt2(b->p, mulpt2(v, 10))), 0, 0, 0, display->white, ZP);
+		fillellipse(dst, toscreen(b->p), 1, 1, display->white, ZP);
+	}
+}
+
+void
 drawship(Ship *ship, Image *dst)
 {
 	int i;
@@ -144,6 +166,8 @@
 			p += 3;
 			break;
 		}
+
+	drawbullets(ship, dst);
 }
 
 void
@@ -254,6 +278,8 @@
 void
 threadnetppu(void *)
 {
+	int i, j;
+	uchar *bufp;
 	Frame *frame, *newf;
 
 	threadsetname("threadnetppu");
@@ -302,10 +328,17 @@
 
 			switch(frame->type){
 			case NSsimstate:
-				unpack(frame->data, frame->len, "PdPdP",
+				bufp = frame->data;
+				bufp += unpack(bufp, frame->len, "PdPdP",
 					&universe->ships[0].p, &universe->ships[0].θ,
 					&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].θ);
 				break;
 			case NSnudge:
 				newf = newframe(nil, NCnudge, frame->seq+1, frame->seq, 0, nil);
@@ -398,9 +431,26 @@
 void
 drawconnecting(void)
 {
-	draw(screen, screen->r, display->black, nil, ZP);
-	string(screen, addpt(screen->r.min, Pt(100,300)), display->white, ZP, font, "connecting...");
-	flushimage(display, 1);
+	static double t0;
+	static Point p = {100,300};
+	Point np;
+	int i;
+
+	if(t0 == 0)
+		t0 = nanosec();
+
+	if(nanosec()-t0 >= 5e9){ /* every five seconds */
+		p = Pt(ntruerand(SCRW-2*100)+100,ntruerand(SCRH-100)+100);
+		t0 = nanosec();
+	}
+
+	draw(screen, screen->r, skymap, nil, ZP);
+	np = string(screen, addpt(screen->r.min, p), display->white, ZP, font, "connecting");
+
+	for(i = 1; i < 3+1; i++){
+		if(nanosec()-t0 > i*1e9)
+			np = string(screen, np, display->white, ZP, font, ".");
+	}
 }
 
 void
@@ -410,10 +460,15 @@
 
 	draw(screen, screen->r, skymap, nil, ZP);
 
-	drawship(&universe->ships[0], screen);
-	drawship(&universe->ships[1], screen);
-	universe->star.spr->draw(universe->star.spr, screen, subpt(toscreen(universe->star.p), Pt(16,16)));
+	if(netconn.state == NCSConnecting)
+		drawconnecting();
 
+	if(netconn.state == NCSConnected){
+		drawship(&universe->ships[0], screen);
+		drawship(&universe->ships[1], screen);
+		universe->star.spr->draw(universe->star.spr, screen, subpt(toscreen(universe->star.p), Pt(16,16)));
+	}
+
 	flushimage(display, 1);
 	unlockdisplay(display);
 }
@@ -494,6 +549,7 @@
 
 	/* TODO: implement this properly with screens and iodial(2) */
 	drawconnecting();
+	flushimage(display, 1);
 	fd = dial(server, nil, nil, nil);
 	if(fd < 0)
 		sysfatal("dial: %r");
--- a/muswd.c
+++ b/muswd.c
@@ -271,7 +271,8 @@
 void
 broadcaststate(void)
 {
-	int i;
+	int i, j, k;
+	uchar *bufp;
 	Frame *frame;
 	NetConn *pnc;
 	Party *p;
@@ -280,11 +281,20 @@
 		for(i = 0; i < nelem(p->players); i++){
 			pnc = p->players[i]->conn;
 
-			frame = newframe(&pnc->udp, NSsimstate, pnc->lastseq+1, 0, 2*(3*8+8)+3*8, nil);
-			pack(frame->data, frame->len, "PdPdP",
+			frame = newframe(&pnc->udp, NSsimstate, pnc->lastseq+1, 0, 2*(3*8+8)+3*8+20*(3*8+8), nil);
+
+			bufp = frame->data;
+			bufp += pack(bufp, frame->len, "PdPdP",
 				p->u->ships[0].p, p->u->ships[0].θ,
 				p->u->ships[1].p, p->u->ships[1].θ,
 				p->u->star.p);
+
+			/* TODO: only send the fired ones */
+			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].θ);
+
 			signframe(frame, pnc->dh.priv);
 
 			sendp(egress, frame);
@@ -488,5 +498,5 @@
 	threadcreate(threadnetppu, nil, mainstacksize);
 	threadcreate(threadnetsend, &adfd, mainstacksize);
 	threadcreate(threadsim, nil, mainstacksize);
-	threadexits(nil);
+	yield();
 }
--- a/universe.c
+++ b/universe.c
@@ -48,10 +48,11 @@
 		0, 0, 1,
 	};
 
-	bv = mulpt2(xform(Vec2(1,0), R), 10*THRUST);
+	bv = mulpt2(xform(Vec2(1,0), R), THRUST);
 
 	for(i = 0; i < nelem(s->rounds); i++)
 		if(!s->rounds[i].fired){
+			s->rounds[i].p = s->p;
 			s->rounds[i].v = addpt2(s->v, bv);
 			s->rounds[i].θ = s->θ;
 			s->rounds[i].fired++;