aboutsummaryrefslogtreecommitdiff
path: root/muswd.c
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2021-07-25 20:59:58 +0000
committerrodri <rgl@antares-labs.eu>2021-07-25 20:59:58 +0000
commitbdaa63a1a4604ada3539aeb50e4af144da3ba6f0 (patch)
tree6aa3d68f94d7756d45f78cce10af1796bdb744d2 /muswd.c
parentf54a8666413d80db3bda3de6e17f795faf7790eb (diff)
downloadmusw-bdaa63a1a4604ada3539aeb50e4af144da3ba6f0.tar.gz
musw-bdaa63a1a4604ada3539aeb50e4af144da3ba6f0.tar.bz2
musw-bdaa63a1a4604ada3539aeb50e4af144da3ba6f0.zip
implemented parties to hold PvP match info.
started serializing game state through the wire. made some major changes to the Lobby struct, plus the ability to check the health of every connection. added some more debug statements, did some cleanup and put more checks.
Diffstat (limited to 'muswd.c')
-rw-r--r--muswd.c75
1 files changed, 55 insertions, 20 deletions
diff --git a/muswd.c b/muswd.c
index 249e516..b3667e2 100644
--- a/muswd.c
+++ b/muswd.c
@@ -55,15 +55,36 @@ threadlisten(void *arg)
continue;
}
- lobby->takeseat(lobby, dfd);
+ lobby->takeseat(lobby, ldir, lcfd, dfd);
if(debug)
fprint(2, "added conn for %lud conns at %lud max\n",
- lobby->seats.len, lobby->seats.cap);
+ lobby->nseats, lobby->cap);
}
}
void
+broadcaststate(void)
+{
+ int i, n;
+ uchar buf[256];
+ Party *p;
+
+ if(debug)
+ fprint(2, "state: x=%g v=%g\n", state.x, state.v);
+
+ for(p = theparty.next; p != &theparty; p = p->next)
+ for(i = 0; i < nelem(p->players); i++){
+ n = pack(buf, sizeof buf, "dd", state.x, state.v);
+ if(write(p->players[i].conn.data, buf, n) != n){
+ lobby->takeseat(lobby, p->players[i^1].conn.dir, p->players[i^1].conn.ctl, p->players[i^1].conn.data);
+ delparty(p);
+ }
+ }
+
+}
+
+void
resetsim(void)
{
t = 0;
@@ -74,10 +95,10 @@ resetsim(void)
void
threadsim(void *)
{
- int i;
uvlong then, now;
double frametime, timeacc;
Ioproc *io;
+ Player couple[2];
Δt = 0.01;
then = nanosec();
@@ -87,24 +108,30 @@ threadsim(void *)
resetsim();
for(;;){
- now = nanosec();
- frametime = now - then;
- then = now;
- timeacc += frametime/1e9;
+ lobby->healthcheck(lobby);
- for(i = 0; i < lobby->seats.len; i++)
- if(fprint(lobby->seats.fds[i], "state: x=%g v=%g\n", state.x, state.v) < 0){
- if(debug)
- fprint(2, "client #%d hanged up\n", i+1);
+ if(debug){
+ Party *p;
+ ulong nparties = 0;
- lobby->leaveseat(lobby, i);
+ for(p = theparty.next; p != &theparty; p = p->next)
+ nparties++;
- if(debug)
- fprint(2, "removed conn %d for %lud conns at %lud max\n",
- i, lobby->seats.len, lobby->seats.cap);
+ fprint(2, "lobby status: %lud conns at %lud cap\n",
+ lobby->nseats, lobby->cap);
+ fprint(2, "party status: %lud parties going on\n",
+ nparties);
+ }
- i--;
- }
+ if(lobby->getcouple(lobby, couple) != -1)
+ newparty(couple);
+
+ broadcaststate();
+
+ now = nanosec();
+ frametime = now - then;
+ then = now;
+ timeacc += frametime/1e9;
while(timeacc >= Δt){
integrate(&state, t, Δt);
@@ -119,7 +146,7 @@ threadsim(void *)
void
usage(void)
{
- fprint(2, "usage: %s [-d]\n", argv0);
+ fprint(2, "usage: %s [-d] [-a addr]\n", argv0);
threadexitsall("usage");
}
@@ -127,9 +154,13 @@ void
threadmain(int argc, char *argv[])
{
int acfd;
- char adir[40];
+ char adir[40], *addr;
+ addr = "udp!*!112";
ARGBEGIN{
+ case 'a':
+ addr = EARGF(usage());
+ break;
case 'd':
debug++;
break;
@@ -139,11 +170,15 @@ threadmain(int argc, char *argv[])
if(argc != 0)
usage();
- acfd = announce("tcp!*!112", adir);
+ acfd = announce(addr, adir);
if(acfd < 0)
sysfatal("announce: %r");
+ if(debug)
+ fprint(2, "listening on %s\n", addr);
+
lobby = newlobby();
+ inittheparty();
threadcreate(threadlisten, adir, 4096);
threadcreate(threadsim, nil, 4096);