|
|
@@ -15,6 +15,7 @@
|
|
|
|
|
|
#include "game.h"
|
|
|
#include "networking.h"
|
|
|
+#include "util.h"
|
|
|
#include "GameState_net.h"
|
|
|
|
|
|
#define SERVER_HOST "127.0.0.1"
|
|
|
@@ -25,59 +26,22 @@ typedef enum {
|
|
|
SS_GAME
|
|
|
} ServerState;
|
|
|
|
|
|
-typedef struct {
|
|
|
- NetMsgType type;
|
|
|
- void *data;
|
|
|
-} OutgoingMsg;
|
|
|
-
|
|
|
-SCDARRAY_HEADER(OutgoingMsg, OutgoingMsg)
|
|
|
+SCDARRAY_HEADER(NetMsgType, NetMsgType)
|
|
|
|
|
|
static FILE *p1_conn = nullptr;
|
|
|
static FILE *p2_conn = nullptr;
|
|
|
static ServerState server_state = SS_LOBBY;
|
|
|
static GameState game_state;
|
|
|
-static SCDArray_OutgoingMsg out_msgs;
|
|
|
-
|
|
|
-SCDARRAY_IMPL(OutgoingMsg, OutgoingMsg)
|
|
|
-
|
|
|
-static OutgoingMsg OutgoingMsg_alloc(const NetMsgType type, void *const data) {
|
|
|
- const size_t data_size = NetMsgType_to_buf_size(type);
|
|
|
-
|
|
|
- OutgoingMsg msg;
|
|
|
- msg.type = type;
|
|
|
- if (data != nullptr) {
|
|
|
- msg.data = malloc(data_size);
|
|
|
- memcpy(msg.data, data, data_size);
|
|
|
- } else {
|
|
|
- msg.data = nullptr;
|
|
|
- }
|
|
|
- return msg;
|
|
|
-}
|
|
|
+static SCDArray_NetMsgType out_msgs;
|
|
|
|
|
|
-static void OutgoingMsg_free(OutgoingMsg *const msg) {
|
|
|
- if (msg->data != nullptr) {
|
|
|
- free(msg->data);
|
|
|
- msg->data = nullptr;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-static void push_out_msg(const NetMsgType type, void *const data) {
|
|
|
- const OutgoingMsg msg = OutgoingMsg_alloc(type, data);
|
|
|
- SCDArray_OutgoingMsg_push_back(&out_msgs, &msg);
|
|
|
-}
|
|
|
-
|
|
|
-static OutgoingMsg pop_out_msg() {
|
|
|
- OutgoingMsg *msg = SCDArray_OutgoingMsg_at(&out_msgs, out_msgs.len - 1);
|
|
|
- const OutgoingMsg ret = *msg;
|
|
|
- SCDArray_OutgoingMsg_remove(&out_msgs, out_msgs.len - 1);
|
|
|
- return ret;
|
|
|
-}
|
|
|
+SCDARRAY_IMPL(NetMsgType, NetMsgType)
|
|
|
|
|
|
static void send_out_msgs() {
|
|
|
for (size_t i = 0; i < out_msgs.len; i++) {
|
|
|
- OutgoingMsg out_msg = pop_out_msg();
|
|
|
- switch (out_msg.type) {
|
|
|
+ const NetMsgType *const type = SCDArray_NetMsgType_at(&out_msgs, i);
|
|
|
+ switch (*type) {
|
|
|
case NET_MSG_PADDLE_POS:
|
|
|
+ {
|
|
|
// p1
|
|
|
PaddleUpdateMsg msg = {
|
|
|
.l_pos_y = game_state.paddle_l.pos[1],
|
|
|
@@ -93,25 +57,54 @@ static void send_out_msgs() {
|
|
|
msg.r_dirv_y = game_state.paddle_l.dirv[1];
|
|
|
send_msg(p2_conn, NET_MSG_PADDLE_POS, (uint8_t *) &msg);
|
|
|
break;
|
|
|
+ }
|
|
|
case NET_MSG_PADDLE_CHANGE_DIR:
|
|
|
case NET_MSG_GAME_STATE:
|
|
|
case NET_MSG_GAME_START:
|
|
|
case NET_MSG_IS_PLAYER_1:
|
|
|
break;
|
|
|
+ case NET_MSG_BALL_UPDATE:
|
|
|
+ {
|
|
|
+ BallUpdateMsg msg = {
|
|
|
+ .ball_pos_x = game_state.ball.pos[0],
|
|
|
+ .ball_pos_y = game_state.ball.pos[1],
|
|
|
+ .ball_dirv_x = game_state.ball.dirv[0],
|
|
|
+ .ball_dirv_y = game_state.ball.dirv[1],
|
|
|
+ };
|
|
|
+ send_msg(p1_conn, NET_MSG_BALL_UPDATE, (uint8_t *) &msg);
|
|
|
+
|
|
|
+ msg.ball_pos_x = 1.0f - msg.ball_pos_x;
|
|
|
+ msg.ball_dirv_x *= -1.0f;
|
|
|
+ send_msg(p2_conn, NET_MSG_BALL_UPDATE, (uint8_t *) &msg);
|
|
|
+ break;
|
|
|
}
|
|
|
+ case NET_MSG_SCORE_UPDATE:
|
|
|
+ {
|
|
|
+ ScoreUpdateMsg msg = {
|
|
|
+ .l_score = game_state.score_l,
|
|
|
+ .r_score = game_state.score_r
|
|
|
+ };
|
|
|
+ send_msg(p1_conn, NET_MSG_SCORE_UPDATE, (uint8_t *) &msg);
|
|
|
|
|
|
- OutgoingMsg_free(&out_msg);
|
|
|
+ msg.l_score = game_state.score_r;
|
|
|
+ msg.r_score = game_state.score_l;
|
|
|
+ send_msg(p2_conn, NET_MSG_SCORE_UPDATE, (uint8_t *) &msg);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ SCDArray_NetMsgType_clear_no_resize(&out_msgs);
|
|
|
}
|
|
|
|
|
|
-static void send_game_state(FILE *conn) {
|
|
|
+static void send_game_state(FILE *conn, const bool p1) {
|
|
|
GameStateMsg msg;
|
|
|
- GameState_to_GameStateMsg(&game_state, &msg);
|
|
|
+ GameState_to_GameStateMsg(&game_state, &msg, p1);
|
|
|
send_msg(conn, NET_MSG_GAME_STATE, (uint8_t *) &msg);
|
|
|
}
|
|
|
|
|
|
static void server_lobby(const int sock) {
|
|
|
- SCDArray_OutgoingMsg_alloc(&out_msgs);
|
|
|
+ assert_scufcont_success(SCDArray_NetMsgType_alloc(&out_msgs));
|
|
|
game_state = GameState_new(game_default_params, false);
|
|
|
|
|
|
int player1 = accept(sock, nullptr, nullptr);
|
|
|
@@ -151,7 +144,7 @@ static void server_lobby(const int sock) {
|
|
|
send_msg(p2_conn, NET_MSG_IS_PLAYER_1, (uint8_t *) &p1_msg);
|
|
|
}
|
|
|
|
|
|
-static void handle_paddle_change_dir_msg(FILE* conn, const bool p1) {
|
|
|
+static void handle_paddle_change_dir_msg(FILE *conn, const bool p1) {
|
|
|
PaddleChangeDirMsg msg;
|
|
|
recv_msg(conn, NET_MSG_PADDLE_CHANGE_DIR, (uint8_t *) &msg);
|
|
|
SEDirection dir = (SEDirection) msg.dir;
|
|
|
@@ -160,7 +153,8 @@ static void handle_paddle_change_dir_msg(FILE* conn, const bool p1) {
|
|
|
} else {
|
|
|
GameState_paddle_r_change_dir(&game_state, dir);
|
|
|
}
|
|
|
- push_out_msg(NET_MSG_PADDLE_POS, nullptr);
|
|
|
+ const NetMsgType type = NET_MSG_PADDLE_POS;
|
|
|
+ assert_scufcont_success(SCDArray_NetMsgType_push_back(&out_msgs, &type));
|
|
|
}
|
|
|
|
|
|
static void handle_player_input(const bool p1) {
|
|
|
@@ -175,6 +169,8 @@ static void handle_player_input(const bool p1) {
|
|
|
case NET_MSG_GAME_STATE:
|
|
|
case NET_MSG_GAME_START:
|
|
|
case NET_MSG_IS_PLAYER_1:
|
|
|
+ case NET_MSG_BALL_UPDATE:
|
|
|
+ case NET_MSG_SCORE_UPDATE:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
@@ -185,8 +181,8 @@ static void server_game() {
|
|
|
|
|
|
send_msg(p1_conn, NET_MSG_GAME_START, nullptr);
|
|
|
send_msg(p2_conn, NET_MSG_GAME_START, nullptr);
|
|
|
- send_game_state(p1_conn);
|
|
|
- send_game_state(p2_conn);
|
|
|
+ send_game_state(p1_conn, true);
|
|
|
+ send_game_state(p2_conn, false);
|
|
|
|
|
|
double last_frame_time_s = 0.0;
|
|
|
while (true) {
|
|
|
@@ -200,7 +196,29 @@ static void server_game() {
|
|
|
handle_player_input(false);
|
|
|
|
|
|
// Tick
|
|
|
- GameState_update(&game_state, delta);
|
|
|
+ const GameStateEvent gs_event = GameState_update(&game_state, delta);
|
|
|
+ // if (gs_event != GSEVENT_NONE) {
|
|
|
+ // LOGF("GameStateEvent: %s", GameStateEvent_to_str(gs_event));
|
|
|
+ // }
|
|
|
+ switch (gs_event) {
|
|
|
+ case GSEVENT_BALL_DIR_CHANGE:
|
|
|
+ {
|
|
|
+ const NetMsgType type = NET_MSG_BALL_UPDATE;
|
|
|
+ assert_scufcont_success(SCDArray_NetMsgType_push_back(&out_msgs, &type));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case GSEVENT_NONE:
|
|
|
+ break;
|
|
|
+ case GSEVENT_L_SCORE:
|
|
|
+ case GSEVENT_R_SCORE:
|
|
|
+ {
|
|
|
+ NetMsgType type = NET_MSG_SCORE_UPDATE;
|
|
|
+ assert_scufcont_success(SCDArray_NetMsgType_push_back(&out_msgs, &type));
|
|
|
+ type = NET_MSG_BALL_UPDATE;
|
|
|
+ assert_scufcont_success(SCDArray_NetMsgType_push_back(&out_msgs, &type));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
// Player output
|
|
|
send_out_msgs();
|