Add support for marshalling GameState + packed values
[tennix.git] / network.c
blob0b5f27199fd1618a06addea863854d4630f1cf72
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007, 2008, 2009 Thomas Perl <thp@thpinfo.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
22 **/
24 #include "game.h"
25 #include "network.h"
27 #include <assert.h>
28 #include <SDL/SDL_net.h>
30 /* HELPER FUNCTIONS */
32 Uint32
33 pack_float(float v, float min, float max)
35 assert(v >= min && v < max);
36 return (Uint32)((1U<<31) * (v-min) / (max-min));
39 float
40 unpack_float(Uint32 v, float min, float max)
42 assert(v < (1U<<31));
43 return v * (max-min) / (1U<<31) + min;
47 void
48 net_serialize_ball(const Ball* src, NetworkBall* dest)
50 SDLNet_Write32(pack_float(src->x, -WIDTH, WIDTH), &(dest->x));
51 SDLNet_Write32(pack_float(src->y, -HEIGHT, HEIGHT), &(dest->y));
52 SDLNet_Write32(pack_float(src->z, -50, 50), &(dest->z));
53 SDLNet_Write32(pack_float(src->move_x, -50, 50), &(dest->move_x));
54 SDLNet_Write32(pack_float(src->move_y, -50, 50), &(dest->move_y));
55 SDLNet_Write32(pack_float(src->move_z, -50, 50), &(dest->move_z));
56 dest->ground_hit = src->ground_hit;
57 dest->last_hit_by = src->last_hit_by;
58 dest->inhibit_gravity = src->inhibit_gravity;
61 void
62 net_unserialize_ball(const NetworkBall* src, Ball* dest)
64 dest->x = unpack_float(SDLNet_Read32(&(src->x)), -WIDTH, WIDTH);
65 dest->y = unpack_float(SDLNet_Read32(&(src->y)), -HEIGHT, HEIGHT);
66 dest->z = unpack_float(SDLNet_Read32(&(src->z)), -50, 50);
67 dest->move_x = unpack_float(SDLNet_Read32(&(src->move_x)), -50, 50);
68 dest->move_y = unpack_float(SDLNet_Read32(&(src->move_y)), -50, 50);
69 dest->move_z = unpack_float(SDLNet_Read32(&(src->move_z)), -50, 50);
70 dest->ground_hit = src->ground_hit;
71 dest->last_hit_by = src->last_hit_by;
72 dest->inhibit_gravity = src->inhibit_gravity;
75 void
76 net_serialize_player(const Player* src, NetworkPlayer* dest)
78 SDLNet_Write32(pack_float(src->x, 0, WIDTH), &(dest->x));
79 SDLNet_Write32(pack_float(src->y, 0, HEIGHT), &(dest->y));
80 SDLNet_Write32(pack_float(src->power, 0, 110), &(dest->power));
81 dest->use_power = src->use_power;
82 dest->score = src->score;
83 dest->desire = src->desire;
84 dest->game = src->game;
85 memcpy(dest->sets, src->sets, sizeof(unsigned char)*(SETS_TO_WIN*2));
86 SDLNet_Write32(pack_float(src->accelerate, 0, 1), &(dest->accelerate));
89 void
90 net_unserialize_player(const NetworkPlayer* src, Player* dest)
92 dest->x = unpack_float(SDLNet_Read32(&(src->x)), 0, WIDTH);
93 dest->y = unpack_float(SDLNet_Read32(&(src->y)), 0, HEIGHT);
94 dest->power = unpack_float(SDLNet_Read32(&(src->power)), 0, 110);
95 dest->use_power = src->use_power;
96 dest->score = src->score;
97 dest->desire = src->desire;
98 dest->game = src->game;
99 memcpy(dest->sets, src->sets, sizeof(unsigned char)*(SETS_TO_WIN*2));
100 dest->accelerate = unpack_float(SDLNet_Read32(&(src->accelerate)), 0, 1);
103 void
104 net_serialize_gamestate(const GameState* src, NetworkGameState* dest)
106 int p;
108 net_serialize_ball(&(src->ball), &(dest->ball));
109 for (p=0; p<MAXPLAYERS; p++) {
110 net_serialize_player(&(src->players[p]), &(dest->players[p]));
112 dest->serving_player = src->serving_player;
113 dest->referee = src->referee;
114 dest->current_set = src->current_set;
115 dest->winner = src->winner;
116 dest->sound_events = src->sound_events;
117 dest->score_event = src->score_event;
118 dest->ec_game = src->ec_game;
119 dest->ec_sets = src->ec_sets;
120 dest->status_message = src->status_message;
122 fprintf(stderr, "size = %lu\n", sizeof(NetworkGameState));
125 void
126 net_unserialize_gamestate(const NetworkGameState* src, GameState* dest)
128 int p;
130 net_unserialize_ball(&(src->ball), &(dest->ball));
131 for (p=0; p<MAXPLAYERS; p++) {
132 net_unserialize_player(&(src->players[p]), &(dest->players[p]));
134 dest->serving_player = src->serving_player;
135 dest->referee = src->referee;
136 dest->current_set = src->current_set;
137 dest->winner = src->winner;
138 dest->sound_events = src->sound_events;
139 dest->score_event = src->score_event;
140 dest->ec_game = src->ec_game;
141 dest->ec_sets = src->ec_sets;
142 dest->status_message = src->status_message;