From d40305baf8a0722cf6128520a5b792fc173c5a93 Mon Sep 17 00:00:00 2001 From: rodri Date: Wed, 22 Feb 2023 21:40:54 +0000 Subject: take player input into account when stepping the simulations. --- dat.h | 10 ++++++++++ muswd.c | 54 +++++++++++++++++++++++++++++++++++++++++------------- party.c | 2 +- universe.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 14 deletions(-) diff --git a/dat.h b/dat.h index 5820845..5e984bb 100644 --- a/dat.h +++ b/dat.h @@ -51,6 +51,10 @@ enum { ConnTimeout = 10000 /* in ms */ }; +enum { + THRUST = 100 +}; + typedef struct VModel VModel; typedef struct Sprite Sprite; typedef struct Particle Particle; @@ -117,6 +121,11 @@ struct Ship Bullet rounds[10]; VModel *mdl; Matrix mdlxform; + + void (*forward)(Ship*, double); + void (*rotate)(Ship*, int, double); + void (*hyperjump)(Ship*); + void (*fire)(Ship*); }; struct Star @@ -167,6 +176,7 @@ struct NetConn u32int lastack; ulong lastrecvts; /* last time a packet was received (in ms) */ ulong lastnudgets; /* last time a nudge was sent (in ms) */ + Player *player; }; struct Player diff --git a/muswd.c b/muswd.c index 602223b..cd268ca 100644 --- a/muswd.c +++ b/muswd.c @@ -42,26 +42,29 @@ getconn(Frame *f) return nil; } -/* TODO: this is an ugly hack. nc should probably reference the player back */ void -dissolveparty(NetConn *nc) +dissolveparty(Player *player) { int i; Party *p; - Player *player; + /* + * kick the player and put their adversary back in the + * queue, then dissolve the party + */ for(p = theparty.next; p != &theparty; p = p->next) for(i = 0; i < nelem(p->players); i++) - if(p->players[i]->conn == nc){ + if(p->players[i] == player){ delplayer(p->players[i]); players.put(&players, p->players[i^1]); delparty(p); } - /* also clean the player queue */ - for(player = players.head; player != nil; player = player->next) - if(player->conn == nc) - players.del(&players, player); + /* + * also clean the player queue + * TODO: has nothing to do with the party + */ + players.del(&players, player); } int @@ -75,7 +78,7 @@ popconn(NetConn *nc) if(*ncp == nc){ memmove(ncp, ncp+1, sizeof(NetConn*)*(ncpe-ncp-1)); nconns--; - dissolveparty(nc); /* TODO: ugly hack. */ + dissolveparty(nc->player); delnetconn(nc); return 0; } @@ -179,6 +182,7 @@ threadnetppu(void *) nc->state = NCSConnected; players.put(&players, newplayer(nil, nc)); + nc->player = players.tail; if(debug) fprint(2, "\trcvd pubkey %ld\n", nc->dh.pub); @@ -210,6 +214,9 @@ threadnetppu(void *) if(debug) fprint(2, "\t%.*lub\n", sizeof(kdown)*8, kdown); + nc->player->oldkdown = nc->player->kdown; + nc->player->kdown = kdown; + break; case NCbuhbye: popconn(nc); @@ -288,10 +295,13 @@ broadcaststate(void) void threadsim(void *) { + int i; uvlong then, now; double frametime, Δt; Ioproc *io; Party *p; + Player *player; + Ship *ship; Δt = 0.01; then = nanosec(); @@ -305,14 +315,32 @@ threadsim(void *) frametime = now - then; then = now; +partywalk: for(p = theparty.next; p != &theparty; p = p->next){ p->u->timeacc += frametime/1e9; - while(p->u->timeacc >= Δt){ - p->u->step(p->u, Δt); - p->u->timeacc -= Δt; - p->u->t += Δt; + for(i = 0; i < nelem(p->players); i++){ + player = p->players[i]; + ship = &p->u->ships[i]; + + if((player->kdown & 1<conn); + goto partywalk; + } + if((player->kdown & 1<forward(ship, Δt); + if((player->kdown & 1<rotate(ship, 1, Δt); + if((player->kdown & 1<rotate(ship, -1, Δt); + if((player->kdown & 1<hyperjump(ship); + if((player->kdown & 1<fire(ship); } + + while(p->u->timeacc >= Δt) + p->u->step(p->u, Δt); } broadcaststate(); diff --git a/party.c b/party.c index 87cf1f8..1360875 100644 --- a/party.c +++ b/party.c @@ -113,7 +113,7 @@ playerq_del(Playerq *pq, Player *p) Player *np; if(pq->head == p){ - pq->pop(pq); + pq->get(pq); return; } diff --git a/universe.c b/universe.c index eea95a8..be481c9 100644 --- a/universe.c +++ b/universe.c @@ -8,10 +8,65 @@ #include "dat.h" #include "fns.h" +/* Ship */ + +static void +ship_forward(Ship *s, double Δt) +{ + Point2 v; + Matrix R = { + cos(s->θ), -sin(s->θ), 0, + sin(s->θ), cos(s->θ), 0, + 0, 0, 1, + }; + + v = mulpt2(xform(Vec2(1,0), R), THRUST*Δt); + s->v = addpt2(s->v, v); +} + +static void +ship_rotate(Ship *s, int dir, double Δt) +{ + s->θ += PI*dir*Δt; +} + +static void +ship_hyperjump(Ship *s) +{ + USED(s); + return; +} + +static void +ship_fire(Ship *s) +{ + int i; + Point2 bv; + Matrix R = { + cos(s->θ), -sin(s->θ), 0, + sin(s->θ), cos(s->θ), 0, + 0, 0, 1, + }; + + bv = mulpt2(xform(Vec2(1,0), R), 10*THRUST); + + for(i = 0; i < nelem(s->rounds); i++) + if(!s->rounds[i].fired){ + s->rounds[i].v = addpt2(s->v, bv); + s->rounds[i].θ = s->θ; + s->rounds[i].fired++; + break; + } +} + +/* Universe */ + static void universe_step(Universe *u, double Δt) { integrate(u, u->t, Δt); + u->timeacc -= Δt; + u->t += Δt; } static void @@ -59,6 +114,11 @@ inituniverse(Universe *u) u->ships[1].mass = 40e3; /* 40 tons */ u->ships[1].kind = WEDGE; u->ships[1].fuel = 200; + + u->ships[0].forward = u->ships[1].forward = ship_forward; + u->ships[0].rotate = u->ships[1].rotate = ship_rotate; + u->ships[0].hyperjump = u->ships[1].hyperjump = ship_hyperjump; + u->ships[0].fire = u->ships[1].fire = ship_fire; } Universe * -- cgit v1.2.3