ref: d40305baf8a0722cf6128520a5b792fc173c5a93
parent: ecc7d34e9229524a8d4340f0e127f1421029141c
author: rodri <rgl@antares-labs.eu>
date: Wed Feb 22 16:40:54 EST 2023
take player input into account when stepping the simulations.
--- a/dat.h
+++ b/dat.h
@@ -51,6 +51,10 @@
ConnTimeout = 10000 /* in ms */
};
+enum {
+ THRUST = 100
+};
+
typedef struct VModel VModel;
typedef struct Sprite Sprite;
typedef struct Particle Particle;
@@ -117,6 +121,11 @@
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 @@
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
--- a/muswd.c
+++ b/muswd.c
@@ -42,26 +42,29 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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<<Kquit) != 0){
+ popconn(player->conn);
+ goto partywalk;
+ }
+ if((player->kdown & 1<<K↑) != 0)
+ ship->forward(ship, Δt);
+ if((player->kdown & 1<<K↺) != 0)
+ ship->rotate(ship, 1, Δt);
+ if((player->kdown & 1<<K↻) != 0)
+ ship->rotate(ship, -1, Δt);
+ if((player->kdown & 1<<Khyper) != 0)
+ ship->hyperjump(ship);
+ if((player->kdown & 1<<Kfire) != 0)
+ ship->fire(ship);
}
+
+ while(p->u->timeacc >= Δt)
+ p->u->step(p->u, Δt);
}
broadcaststate();
--- a/party.c
+++ b/party.c
@@ -113,7 +113,7 @@
Player *np;
if(pq->head == p){
- pq->pop(pq);
+ pq->get(pq);
return;
}
--- 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 @@
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 *