From 9c20abc1ab976cab00040a32299896dc8ea71e6c Mon Sep 17 00:00:00 2001 From: rodri Date: Mon, 5 Jun 2023 20:23:26 +0000 Subject: put the player routines in their own unit. fixed an issue with the threadsim routine that would cause re-stepping of all the previous parties after dissolving one of them due to one of the players quitting. --- mkfile | 1 + musw.c | 6 +++-- muswd.c | 10 ++++--- party.c | 86 ------------------------------------------------------------ player.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+), 92 deletions(-) create mode 100644 player.c diff --git a/mkfile b/mkfile index 6973c0e..eb3c9a0 100644 --- a/mkfile +++ b/mkfile @@ -12,6 +12,7 @@ OFILES=\ nanosec.$O\ pack.$O\ party.$O\ + player.$O\ universe.$O\ sprite.$O\ vfx.$O\ diff --git a/musw.c b/musw.c index fd04db3..fd8c8ac 100644 --- a/musw.c +++ b/musw.c @@ -657,14 +657,16 @@ State *intro_δ(State *s, void *arg) State *connecting_δ(State *s, void*) { - if(netconn.state != NCSConnecting) + if(netconn.state == NCSConnected) return &gamestates[GSMatching]; return s; } State *matching_δ(State *s, void*) { - if(netconn.state == NCSConnected && weplaying) + if(netconn.state != NCSConnected) + return &gamestates[GSConnecting]; + if(weplaying) return &gamestates[GSPlaying]; return s; } diff --git a/muswd.c b/muswd.c index 4f871d8..1882e59 100644 --- a/muswd.c +++ b/muswd.c @@ -336,7 +336,7 @@ threadsim(void *) uvlong then, now; double frametime, Δt; Ioproc *io; - Party *p; + Party *p, *np; Player *player; Ship *ship; @@ -352,8 +352,7 @@ threadsim(void *) if(players.len >= 2) newparty(&theparty, players.get(&players), players.get(&players)); -partywalk: - for(p = theparty.next; p != &theparty; p = p->next){ + for(p = theparty.next; p != &theparty; p = np){ p->u->timeacc += frametime/1e9; for(i = 0; i < nelem(p->players); i++){ @@ -361,8 +360,9 @@ partywalk: ship = &p->u->ships[i]; if((player->kdown & 1<next; popconn(player->conn); - goto partywalk; + goto partydone; } if((player->kdown & 1<forward(ship, Δt); @@ -384,6 +384,8 @@ partywalk: p->u->step(p->u, Δt); p->u->collide(p->u); } + np = p->next; +partydone:; } broadcaststate(); diff --git a/party.c b/party.c index be0f4ed..031a4fa 100644 --- a/party.c +++ b/party.c @@ -8,8 +8,6 @@ #include "dat.h" #include "fns.h" -/* Party */ - Party * newparty(Party *p, Player *player0, Player *player1) { @@ -50,87 +48,3 @@ initparty(Party *p) { p->next = p->prev = p; } - -/* Player */ - -Player * -newplayer(char *name, NetConn *nc) -{ - Player *p; - - p = emalloc(sizeof(Player)); - p->name = name? strdup(name): nil; - p->conn = nc; - p->oldkdown = p->kdown = 0; - p->next = nil; - - return p; -} - -void -delplayer(Player *p) -{ - free(p->name); - free(p); -} - -/* Player queue */ - -static void -playerq_put(Playerq *pq, Player *p) -{ - if(pq->tail == nil) - pq->head = pq->tail = p; - else{ - pq->tail->next = p; - pq->tail = p; - } - pq->len++; -} - -static Player * -playerq_get(Playerq *pq) -{ - Player *p; - - if(pq->head == nil) - return nil; - - p = pq->head; - if(pq->head == pq->tail) - pq->head = pq->tail = nil; - else{ - pq->head = p->next; - p->next = nil; - } - pq->len--; - return p; -} - -static void -playerq_del(Playerq *pq, Player *p) -{ - Player *np; - - if(pq->head == p){ - pq->get(pq); - return; - } - - for(np = pq->head; np != nil && np->next != nil; np = np->next) - if(np->next == p){ - np->next = np->next->next; - p->next = nil; - pq->len--; - } -} - -void -initplayerq(Playerq *pq) -{ - pq->head = pq->tail = nil; - pq->len = 0; - pq->put = playerq_put; - pq->get = playerq_get; - pq->del = playerq_del; -} diff --git a/player.c b/player.c new file mode 100644 index 0000000..fc573b7 --- /dev/null +++ b/player.c @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include +#include +#include "dat.h" +#include "fns.h" + +Player * +newplayer(char *name, NetConn *nc) +{ + Player *p; + + p = emalloc(sizeof(Player)); + p->name = name? strdup(name): nil; + p->conn = nc; + p->oldkdown = p->kdown = 0; + p->next = nil; + + return p; +} + +void +delplayer(Player *p) +{ + free(p->name); + free(p); +} + +/* Player queue */ + +static void +playerq_put(Playerq *pq, Player *p) +{ + if(pq->tail == nil) + pq->head = pq->tail = p; + else{ + pq->tail->next = p; + pq->tail = p; + } + pq->len++; +} + +static Player * +playerq_get(Playerq *pq) +{ + Player *p; + + if(pq->head == nil) + return nil; + + p = pq->head; + if(pq->head == pq->tail) + pq->head = pq->tail = nil; + else{ + pq->head = p->next; + p->next = nil; + } + pq->len--; + return p; +} + +static void +playerq_del(Playerq *pq, Player *p) +{ + Player *np; + + if(pq->head == p){ + pq->get(pq); + return; + } + + for(np = pq->head; np != nil && np->next != nil; np = np->next) + if(np->next == p){ + np->next = np->next->next; + p->next = nil; + pq->len--; + } +} + +void +initplayerq(Playerq *pq) +{ + pq->head = pq->tail = nil; + pq->len = 0; + pq->put = playerq_put; + pq->get = playerq_get; + pq->del = playerq_del; +} \ No newline at end of file -- cgit v1.2.3