ref: 9c20abc1ab976cab00040a32299896dc8ea71e6c
parent: 0625897dd0194986b02988eb5a9c2969e24adf0c
author: rodri <rgl@antares-labs.eu>
date: Mon Jun 5 16:23:26 EDT 2023
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.
--- a/mkfile
+++ b/mkfile
@@ -12,6 +12,7 @@
nanosec.$O\
pack.$O\
party.$O\
+ player.$O\
universe.$O\
sprite.$O\
vfx.$O\
--- a/musw.c
+++ b/musw.c
@@ -657,7 +657,7 @@
State *connecting_δ(State *s, void*)
{
- if(netconn.state != NCSConnecting)
+ if(netconn.state == NCSConnected)
return &gamestates[GSMatching];
return s;
}
@@ -664,7 +664,9 @@
State *matching_δ(State *s, void*)
{
- if(netconn.state == NCSConnected && weplaying)
+ if(netconn.state != NCSConnected)
+ return &gamestates[GSConnecting];
+ if(weplaying)
return &gamestates[GSPlaying];
return s;
}
--- a/muswd.c
+++ b/muswd.c
@@ -336,7 +336,7 @@
uvlong then, now;
double frametime, Δt;
Ioproc *io;
- Party *p;
+ Party *p, *np;
Player *player;
Ship *ship;
@@ -352,8 +352,7 @@
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 @@
ship = &p->u->ships[i];
if((player->kdown & 1<<Kquit) != 0){
+ np = p->next;
popconn(player->conn);
- goto partywalk;
+ goto partydone;
}
if((player->kdown & 1<<K↑) != 0)
ship->forward(ship, Δt);
@@ -384,6 +384,8 @@
p->u->step(p->u, Δt);
p->u->collide(p->u);
}
+ np = p->next;
+partydone:;
}
broadcaststate();
--- 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)
{
@@ -49,88 +47,4 @@
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;
}
--- /dev/null
+++ b/player.c
@@ -1,0 +1,91 @@
+#include <u.h>
+#include <libc.h>
+#include <ip.h>
+#include <mp.h>
+#include <libsec.h>
+#include <draw.h>
+#include <geometry.h>
+#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