]> icculus.org git repositories - btb/d2x.git/blob - main/netmisc.c
remove rcs tags
[btb/d2x.git] / main / netmisc.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Misc routines for network.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "inferno.h"
28 #include "pstypes.h"
29 #include "mono.h"
30
31 #ifdef WORDS_BIGENDIAN
32
33 #include "byteswap.h"
34 #include "segment.h"
35 #include "gameseg.h"
36
37 // routine to calculate the checksum of the segments.  We add these specialized routines
38 // since the current way is byte order dependent.
39
40 void mac_do_checksum_calc(ubyte *b, int len, unsigned int *s1, unsigned int *s2)
41 {
42
43         while(len--) {
44                 *s1 += *b++;
45                 if (*s1 >= 255) *s1 -= 255;
46                 *s2 += *s1;
47         }
48 }
49
50 ushort mac_calc_segment_checksum()
51 {
52         int i, j, k;
53         unsigned int sum1,sum2;
54         short s;
55         int t;
56
57         sum1 = sum2 = 0;
58         for (i = 0; i < Highest_segment_index + 1; i++) {
59                 for (j = 0; j < MAX_SIDES_PER_SEGMENT; j++) {
60                         mac_do_checksum_calc((unsigned char *)&(Segments[i].sides[j].type), 1, &sum1, &sum2);
61                         mac_do_checksum_calc(&(Segments[i].sides[j].pad), 1, &sum1, &sum2);
62                         s = INTEL_SHORT(Segments[i].sides[j].wall_num);
63                         mac_do_checksum_calc((ubyte *)&s, 2, &sum1, &sum2);
64                         s = INTEL_SHORT(Segments[i].sides[j].tmap_num);
65                         mac_do_checksum_calc((ubyte *)&s, 2, &sum1, &sum2);
66                         s = INTEL_SHORT(Segments[i].sides[j].tmap_num2);
67                         mac_do_checksum_calc((ubyte *)&s, 2, &sum1, &sum2);
68                         for (k = 0; k < 4; k++) {
69                                 t = INTEL_INT(((int)Segments[i].sides[j].uvls[k].u));
70                                 mac_do_checksum_calc((ubyte *)&t, 4, &sum1, &sum2);
71                                 t = INTEL_INT(((int)Segments[i].sides[j].uvls[k].v));
72                                 mac_do_checksum_calc((ubyte *)&t, 4, &sum1, &sum2);
73                                 t = INTEL_INT(((int)Segments[i].sides[j].uvls[k].l));
74                                 mac_do_checksum_calc((ubyte *)&t, 4, &sum1, &sum2);
75                         }
76                         for (k = 0; k < 2; k++) {
77                                 t = INTEL_INT(((int)Segments[i].sides[j].normals[k].x));
78                                 mac_do_checksum_calc((ubyte *)&t, 4, &sum1, &sum2);
79                                 t = INTEL_INT(((int)Segments[i].sides[j].normals[k].y));
80                                 mac_do_checksum_calc((ubyte *)&t, 4, &sum1, &sum2);
81                                 t = INTEL_INT(((int)Segments[i].sides[j].normals[k].z));
82                                 mac_do_checksum_calc((ubyte *)&t, 4, &sum1, &sum2);
83                         }
84                 }
85                 for (j = 0; j < MAX_SIDES_PER_SEGMENT; j++) {
86                         s = INTEL_SHORT(Segments[i].children[j]);
87                         mac_do_checksum_calc((ubyte *)&s, 2, &sum1, &sum2);
88                 }
89                 for (j = 0; j < MAX_VERTICES_PER_SEGMENT; j++) {
90                         s = INTEL_SHORT(Segments[i].verts[j]);
91                         mac_do_checksum_calc((ubyte *)&s, 2, &sum1, &sum2);
92                 }
93                 t = INTEL_INT(Segments[i].objects);
94                 mac_do_checksum_calc((ubyte *)&t, 4, &sum1, &sum2);
95         }
96         sum2 %= 255;
97         return ((sum1<<8)+ sum2);
98 }
99
100 // this routine totally and completely relies on the fact that the network
101 //  checksum must be calculated on the segments!!!!!
102
103 ushort netmisc_calc_checksum(void * vptr, int len)
104 {
105         vptr = vptr;
106         len = len;
107         return mac_calc_segment_checksum();
108 }
109
110 // following are routine for macintosh only that will swap the elements of
111 // structures send through the networking code.  The structures and
112 // this code must be kept in total sync
113
114 #include "ipx.h"
115 #include "multi.h"
116 #ifdef NETWORK
117 #include "network.h"
118 #endif
119 #include "object.h"
120 #include "powerup.h"
121 #include "error.h"
122
123 sbyte out_buffer[IPX_MAX_DATA_SIZE];    // used for tmp netgame packets as well as sending object data
124
125 void receive_netplayer_info(ubyte *data, netplayer_info *info)
126 {
127         int loc = 0;
128
129         memcpy(info->callsign, &(data[loc]), CALLSIGN_LEN+1);       loc += CALLSIGN_LEN+1;
130         memcpy(&(info->network.ipx.server), &(data[loc]), 4);       loc += 4;
131         memcpy(&(info->network.ipx.node), &(data[loc]), 6);         loc += 6;
132         info->version_major = data[loc];                            loc++;
133         info->version_minor = data[loc];                            loc++;
134         memcpy(&(info->computer_type), &(data[loc]), 1);            loc++;      // memcpy to avoid compile time warning about enum
135         info->connected = data[loc];                                loc++;
136         memcpy(&(info->socket), &(data[loc]), 2);                   loc += 2;
137         memcpy (&(info->rank),&(data[loc]),1);                      loc++;
138         // MWA don't think we need to swap this because we need it in high
139         // order  info->socket = INTEL_SHORT(info->socket);
140 }
141
142 void send_netplayers_packet(ubyte *server, ubyte *node)
143 {
144         int i, tmpi;
145         int loc = 0;
146         short tmps;
147
148         memset(out_buffer, 0, sizeof(out_buffer));
149         out_buffer[0] = NetPlayers.type;                            loc++;
150         tmpi = INTEL_INT(NetPlayers.Security);
151         memcpy(&(out_buffer[loc]), &tmpi, 4);                       loc += 4;
152         for (i = 0; i < MAX_PLAYERS+4; i++) {
153                 memcpy(&(out_buffer[loc]), NetPlayers.players[i].callsign, CALLSIGN_LEN+1); loc += CALLSIGN_LEN+1;
154                 memcpy(&(out_buffer[loc]), NetPlayers.players[i].network.ipx.server, 4);    loc += 4;
155                 memcpy(&(out_buffer[loc]), NetPlayers.players[i].network.ipx.node, 6);      loc += 6;
156                 memcpy(&(out_buffer[loc]), &(NetPlayers.players[i].version_major), 1);      loc++;
157                 memcpy(&(out_buffer[loc]), &(NetPlayers.players[i].version_minor), 1);      loc++;
158                 memcpy(&(out_buffer[loc]), &(NetPlayers.players[i].computer_type), 1);      loc++;
159                 memcpy(&(out_buffer[loc]), &(NetPlayers.players[i].connected), 1);          loc++;
160                 tmps = INTEL_SHORT(NetPlayers.players[i].socket);
161                 memcpy(&(out_buffer[loc]), &tmps, 2);                                       loc += 2;
162                 memcpy(&(out_buffer[loc]), &(NetPlayers.players[i].rank), 1);               loc++;
163         }
164
165         if ((server == NULL) && (node == NULL))
166                 ipx_send_broadcast_packet_data((unsigned char *)out_buffer, loc);
167         else
168                 ipx_send_internetwork_packet_data((unsigned char *)out_buffer, loc, server, node);
169
170 }
171
172 void receive_netplayers_packet(ubyte *data, AllNetPlayers_info *pinfo)
173 {
174         int i, loc = 0;
175
176         pinfo->type = data[loc];                            loc++;
177         memcpy(&(pinfo->Security), &(data[loc]), 4);        loc += 4;
178         pinfo->Security = INTEL_INT(pinfo->Security);
179         for (i = 0; i < MAX_PLAYERS+4; i++) {
180                 receive_netplayer_info(&(data[loc]), &(pinfo->players[i]));
181                 loc += 26;          // sizeof(netplayer_info) on the PC
182         }
183 }
184
185 void send_sequence_packet(sequence_packet seq, ubyte *server, ubyte *node, ubyte *net_address)
186 {
187         short tmps;
188         int loc, tmpi;
189
190         loc = 0;
191         memset(out_buffer, 0, sizeof(out_buffer));
192         out_buffer[0] = seq.type;                                       loc++;
193         tmpi = INTEL_INT(seq.Security);
194         memcpy(&(out_buffer[loc]), &tmpi, 4);                           loc += 4;       loc += 3;
195         memcpy(&(out_buffer[loc]), seq.player.callsign, CALLSIGN_LEN+1);loc += CALLSIGN_LEN+1;
196         memcpy(&(out_buffer[loc]), seq.player.network.ipx.server, 4);   loc += 4;
197         memcpy(&(out_buffer[loc]), seq.player.network.ipx.node, 6);     loc += 6;
198         out_buffer[loc] = seq.player.version_major;                     loc++;
199         out_buffer[loc] = seq.player.version_minor;                     loc++;
200         out_buffer[loc] = seq.player.computer_type;                     loc++;
201         out_buffer[loc] = seq.player.connected;                         loc++;
202         tmps = INTEL_SHORT(seq.player.socket);
203         memcpy(&(out_buffer[loc]), &tmps, 2);                           loc += 2;
204         out_buffer[loc]=seq.player.rank;                                loc++;      // for pad byte
205         if (net_address != NULL)
206                 ipx_send_packet_data((unsigned char *)out_buffer, loc, server, node, net_address);
207         else if ((server == NULL) && (node == NULL))
208                 ipx_send_broadcast_packet_data((unsigned char *)out_buffer, loc);
209         else
210                 ipx_send_internetwork_packet_data((unsigned char *)out_buffer, loc, server, node);
211 }
212
213 void receive_sequence_packet(ubyte *data, sequence_packet *seq)
214 {
215         int loc = 0;
216
217         seq->type = data[0];                        loc++;
218         memcpy(&(seq->Security), &(data[loc]), 4);  loc += 4;   loc += 3;   // +3 for pad byte
219         seq->Security = INTEL_INT(seq->Security);
220         receive_netplayer_info(&(data[loc]), &(seq->player));
221 }
222
223 void send_netgame_packet(ubyte *server, ubyte *node, ubyte *net_address, int lite_flag)     // lite says shorter netgame packets
224 {
225         uint tmpi;
226         ushort tmps; // p;
227         int i, j;
228         int loc = 0;
229
230         memset(out_buffer, 0, IPX_MAX_DATA_SIZE);
231         memcpy(&(out_buffer[loc]), &(Netgame.type), 1);                 loc++;
232         tmpi = INTEL_INT(Netgame.Security);
233         memcpy(&(out_buffer[loc]), &tmpi, 4);                           loc += 4;
234         memcpy(&(out_buffer[loc]), Netgame.game_name, NETGAME_NAME_LEN+1);  loc += (NETGAME_NAME_LEN+1);
235         memcpy(&(out_buffer[loc]), Netgame.mission_title, MISSION_NAME_LEN+1);  loc += (MISSION_NAME_LEN+1);
236         memcpy(&(out_buffer[loc]), Netgame.mission_name, 9);            loc += 9;
237         tmpi = INTEL_INT(Netgame.levelnum);
238         memcpy(&(out_buffer[loc]), &tmpi, 4);                           loc += 4;
239         memcpy(&(out_buffer[loc]), &(Netgame.gamemode), 1);             loc++;
240         memcpy(&(out_buffer[loc]), &(Netgame.RefusePlayers), 1);        loc++;
241         memcpy(&(out_buffer[loc]), &(Netgame.difficulty), 1);           loc++;
242         memcpy(&(out_buffer[loc]), &(Netgame.game_status), 1);          loc++;
243         memcpy(&(out_buffer[loc]), &(Netgame.numplayers), 1);           loc++;
244         memcpy(&(out_buffer[loc]), &(Netgame.max_numplayers), 1);       loc++;
245         memcpy(&(out_buffer[loc]), &(Netgame.numconnected), 1);         loc++;
246         memcpy(&(out_buffer[loc]), &(Netgame.game_flags), 1);           loc++;
247         memcpy(&(out_buffer[loc]), &(Netgame.protocol_version), 1);     loc++;
248         memcpy(&(out_buffer[loc]), &(Netgame.version_major), 1);        loc++;
249         memcpy(&(out_buffer[loc]), &(Netgame.version_minor), 1);        loc++;
250         memcpy(&(out_buffer[loc]), &(Netgame.team_vector), 1);          loc++;
251
252         if (lite_flag)
253                 goto do_send;
254
255 // will this work -- damn bitfields -- totally bogus when trying to do
256 // this type of stuff
257 // Watcom makes bitfields from left to right.  CW7 on the mac goes
258 // from right to left.  then they are endian swapped
259
260         tmps = *(ushort *)((ubyte *)(&Netgame.team_vector) + 1);    // get the values for the first short bitfield
261         tmps = INTEL_SHORT(tmps);
262         memcpy(&(out_buffer[loc]), &tmps, 2);                           loc += 2;
263
264         tmps = *(ushort *)((ubyte *)(&Netgame.team_vector) + 3);    // get the values for the second short bitfield
265         tmps = INTEL_SHORT(tmps);
266         memcpy(&(out_buffer[loc]), &tmps, 2);                           loc += 2;
267
268 #if 0       // removed since I reordered bitfields on mac
269         p = *(ushort *)((ubyte *)(&Netgame.team_vector) + 1);       // get the values for the first short bitfield
270         tmps = 0;
271         for (i = 15; i >= 0; i--) {
272                 if (p & (1 << i))
273                         tmps |= (1 << (15 - i));
274         }
275         tmps = INTEL_SHORT(tmps);
276         memcpy(&(out_buffer[loc]), &tmps, 2);                           loc += 2;
277         p = *(ushort *)((ubyte *)(&Netgame.team_vector) + 3);       // get the values for the second short bitfield
278         tmps = 0;
279         for (i = 15; i >= 0; i--) {
280                 if (p & (1 << i))
281                         tmps |= (1 << (15 - i));
282         }
283         tmps = INTEL_SHORT(tmps);
284         memcpy(&(out_buffer[loc]), &tmps, 2);                           loc += 2;
285 #endif
286
287         memcpy(&(out_buffer[loc]), Netgame.team_name, 2*(CALLSIGN_LEN+1)); loc += 2*(CALLSIGN_LEN+1);
288         for (i = 0; i < MAX_PLAYERS; i++) {
289                 tmpi = INTEL_INT(Netgame.locations[i]);
290                 memcpy(&(out_buffer[loc]), &tmpi, 4);       loc += 4;   // SWAP HERE!!!
291         }
292
293         for (i = 0; i < MAX_PLAYERS; i++) {
294                 for (j = 0; j < MAX_PLAYERS; j++) {
295                         tmps = INTEL_SHORT(Netgame.kills[i][j]);
296                         memcpy(&(out_buffer[loc]), &tmps, 2);   loc += 2;   // SWAP HERE!!!
297                 }
298         }
299
300         tmps = INTEL_SHORT(Netgame.segments_checksum);
301         memcpy(&(out_buffer[loc]), &tmps, 2);           loc += 2;   // SWAP_HERE
302         tmps = INTEL_SHORT(Netgame.team_kills[0]);
303         memcpy(&(out_buffer[loc]), &tmps, 2);           loc += 2;   // SWAP_HERE
304         tmps = INTEL_SHORT(Netgame.team_kills[1]);
305         memcpy(&(out_buffer[loc]), &tmps, 2);           loc += 2;   // SWAP_HERE
306         for (i = 0; i < MAX_PLAYERS; i++) {
307                 tmps = INTEL_SHORT(Netgame.killed[i]);
308                 memcpy(&(out_buffer[loc]), &tmps, 2);       loc += 2;   // SWAP HERE!!!
309         }
310         for (i = 0; i < MAX_PLAYERS; i++) {
311                 tmps = INTEL_SHORT(Netgame.player_kills[i]);
312                 memcpy(&(out_buffer[loc]), &tmps, 2);       loc += 2;   // SWAP HERE!!!
313         }
314
315         tmpi = INTEL_INT(Netgame.KillGoal);
316         memcpy(&(out_buffer[loc]), &tmpi, 4);           loc += 4;   // SWAP_HERE
317         tmpi = INTEL_INT(Netgame.PlayTimeAllowed);
318         memcpy(&(out_buffer[loc]), &tmpi, 4);           loc += 4;   // SWAP_HERE
319         tmpi = INTEL_INT(Netgame.level_time);
320         memcpy(&(out_buffer[loc]), &tmpi, 4);           loc += 4;   // SWAP_HERE
321         tmpi = INTEL_INT(Netgame.control_invul_time);
322         memcpy(&(out_buffer[loc]), &tmpi, 4);           loc += 4;   // SWAP_HERE
323         tmpi = INTEL_INT(Netgame.monitor_vector);
324         memcpy(&(out_buffer[loc]), &tmpi, 4);           loc += 4;   // SWAP_HERE
325         for (i = 0; i < MAX_PLAYERS; i++) {
326                 tmpi = INTEL_INT(Netgame.player_score[i]);
327                 memcpy(&(out_buffer[loc]), &tmpi, 4);       loc += 4;   // SWAP_HERE
328         }
329         for (i = 0; i < MAX_PLAYERS; i++) {
330                 memcpy(&(out_buffer[loc]), &(Netgame.player_flags[i]), 1); loc++;
331         }
332         tmps = INTEL_SHORT(Netgame.PacketsPerSec);
333         memcpy(&(out_buffer[loc]), &tmps, 2);                   loc += 2;
334         memcpy(&(out_buffer[loc]), &(Netgame.ShortPackets), 1); loc ++;
335
336 do_send:
337         if (net_address != NULL)
338                 ipx_send_packet_data((unsigned char *)out_buffer, loc, server, node, net_address);
339         else if ((server == NULL) && (node == NULL))
340                 ipx_send_broadcast_packet_data((unsigned char *)out_buffer, loc);
341         else
342                 ipx_send_internetwork_packet_data((unsigned char *)out_buffer, loc, server, node);
343 }
344
345 void receive_netgame_packet(ubyte *data, netgame_info *netgame, int lite_flag)
346 {
347         int i, j;
348         int loc = 0;
349         short bitfield; // new_field;
350
351         memcpy(&(netgame->type), &(data[loc]), 1);                      loc++;
352         memcpy(&(netgame->Security), &(data[loc]), 4);                  loc += 4;
353         netgame->Security = INTEL_INT(netgame->Security);
354         memcpy(netgame->game_name, &(data[loc]), NETGAME_NAME_LEN+1);   loc += (NETGAME_NAME_LEN+1);
355         memcpy(netgame->mission_title, &(data[loc]), MISSION_NAME_LEN+1); loc += (MISSION_NAME_LEN+1);
356         memcpy(netgame->mission_name, &(data[loc]), 9);                 loc += 9;
357         memcpy(&(netgame->levelnum), &(data[loc]), 4);                  loc += 4;
358         netgame->levelnum = INTEL_INT(netgame->levelnum);
359         memcpy(&(netgame->gamemode), &(data[loc]), 1);                  loc++;
360         memcpy(&(netgame->RefusePlayers), &(data[loc]), 1);             loc++;
361         memcpy(&(netgame->difficulty), &(data[loc]), 1);                loc++;
362         memcpy(&(netgame->game_status), &(data[loc]), 1);               loc++;
363         memcpy(&(netgame->numplayers), &(data[loc]), 1);                loc++;
364         memcpy(&(netgame->max_numplayers), &(data[loc]), 1);            loc++;
365         memcpy(&(netgame->numconnected), &(data[loc]), 1);              loc++;
366         memcpy(&(netgame->game_flags), &(data[loc]), 1);                loc++;
367         memcpy(&(netgame->protocol_version), &(data[loc]), 1);          loc++;
368         memcpy(&(netgame->version_major), &(data[loc]), 1);             loc++;
369         memcpy(&(netgame->version_minor), &(data[loc]), 1);             loc++;
370         memcpy(&(netgame->team_vector), &(data[loc]), 1);               loc++;
371
372         if (lite_flag)
373                 return;
374
375         memcpy(&bitfield, &(data[loc]), 2);                             loc += 2;
376         bitfield = INTEL_SHORT(bitfield);
377         memcpy(((ubyte *)(&netgame->team_vector) + 1), &bitfield, 2);
378
379         memcpy(&bitfield, &(data[loc]), 2);                             loc += 2;
380         bitfield = INTEL_SHORT(bitfield);
381         memcpy(((ubyte *)(&netgame->team_vector) + 3), &bitfield, 2);
382
383 #if 0       // not used since reordering mac bitfields
384         memcpy(&bitfield, &(data[loc]), 2);                             loc += 2;
385         new_field = 0;
386         for (i = 15; i >= 0; i--) {
387                 if (bitfield & (1 << i))
388                         new_field |= (1 << (15 - i));
389         }
390         new_field = INTEL_SHORT(new_field);
391         memcpy(((ubyte *)(&netgame->team_vector) + 1), &new_field, 2);
392
393         memcpy(&bitfield, &(data[loc]), 2);                             loc += 2;
394         new_field = 0;
395         for (i = 15; i >= 0; i--) {
396                 if (bitfield & (1 << i))
397                         new_field |= (1 << (15 - i));
398         }
399         new_field = INTEL_SHORT(new_field);
400         memcpy(((ubyte *)(&netgame->team_vector) + 3), &new_field, 2);
401 #endif
402
403         memcpy(netgame->team_name, &(data[loc]), 2*(CALLSIGN_LEN+1));   loc += 2*(CALLSIGN_LEN+1);
404         for (i = 0; i < MAX_PLAYERS; i++) {
405                 memcpy(&(netgame->locations[i]), &(data[loc]), 4);          loc += 4;
406                 netgame->locations[i] = INTEL_INT(netgame->locations[i]);
407         }
408
409         for (i = 0; i < MAX_PLAYERS; i++) {
410                 for (j = 0; j < MAX_PLAYERS; j++) {
411                         memcpy(&(netgame->kills[i][j]), &(data[loc]), 2);       loc += 2;
412                         netgame->kills[i][j] = INTEL_SHORT(netgame->kills[i][j]);
413                 }
414         }
415
416         memcpy(&(netgame->segments_checksum), &(data[loc]), 2);         loc += 2;
417         netgame->segments_checksum = INTEL_SHORT(netgame->segments_checksum);
418         memcpy(&(netgame->team_kills[0]), &(data[loc]), 2);             loc += 2;
419         netgame->team_kills[0] = INTEL_SHORT(netgame->team_kills[0]);
420         memcpy(&(netgame->team_kills[1]), &(data[loc]), 2);             loc += 2;
421         netgame->team_kills[1] = INTEL_SHORT(netgame->team_kills[1]);
422         for (i = 0; i < MAX_PLAYERS; i++) {
423                 memcpy(&(netgame->killed[i]), &(data[loc]), 2);             loc += 2;
424                 netgame->killed[i] = INTEL_SHORT(netgame->killed[i]);
425         }
426         for (i = 0; i < MAX_PLAYERS; i++) {
427                 memcpy(&(netgame->player_kills[i]), &(data[loc]), 2);       loc += 2;
428                 netgame->player_kills[i] = INTEL_SHORT(netgame->player_kills[i]);
429         }
430         memcpy(&(netgame->KillGoal), &(data[loc]), 4);                  loc += 4;
431         netgame->KillGoal = INTEL_INT(netgame->KillGoal);
432         memcpy(&(netgame->PlayTimeAllowed), &(data[loc]), 4);           loc += 4;
433         netgame->PlayTimeAllowed = INTEL_INT(netgame->PlayTimeAllowed);
434
435         memcpy(&(netgame->level_time), &(data[loc]), 4);                loc += 4;
436         netgame->level_time = INTEL_INT(netgame->level_time);
437         memcpy(&(netgame->control_invul_time), &(data[loc]), 4);        loc += 4;
438         netgame->control_invul_time = INTEL_INT(netgame->control_invul_time);
439         memcpy(&(netgame->monitor_vector), &(data[loc]), 4);            loc += 4;
440         netgame->monitor_vector = INTEL_INT(netgame->monitor_vector);
441         for (i = 0; i < MAX_PLAYERS; i++) {
442                 memcpy(&(netgame->player_score[i]), &(data[loc]), 4);       loc += 4;
443                 netgame->player_score[i] = INTEL_INT(netgame->player_score[i]);
444         }
445         for (i = 0; i < MAX_PLAYERS; i++) {
446                 memcpy(&(netgame->player_flags[i]), &(data[loc]), 1);       loc++;
447         }
448         memcpy(&(netgame->PacketsPerSec), &(data[loc]), 2);             loc += 2;
449         netgame->PacketsPerSec = INTEL_SHORT(netgame->PacketsPerSec);
450         memcpy(&(netgame->ShortPackets), &(data[loc]), 1);              loc ++;
451
452 }
453
454 void swap_object(object *obj)
455 {
456         // swap the short and int entries for this object
457         obj->signature     = INTEL_INT(obj->signature);
458         obj->next          = INTEL_SHORT(obj->next);
459         obj->prev          = INTEL_SHORT(obj->prev);
460         obj->segnum        = INTEL_SHORT(obj->segnum);
461         obj->pos.x         = INTEL_INT(obj->pos.x);
462         obj->pos.y         = INTEL_INT(obj->pos.y);
463         obj->pos.z         = INTEL_INT(obj->pos.z);
464
465         obj->orient.rvec.x = INTEL_INT(obj->orient.rvec.x);
466         obj->orient.rvec.y = INTEL_INT(obj->orient.rvec.y);
467         obj->orient.rvec.z = INTEL_INT(obj->orient.rvec.z);
468         obj->orient.fvec.x = INTEL_INT(obj->orient.fvec.x);
469         obj->orient.fvec.y = INTEL_INT(obj->orient.fvec.y);
470         obj->orient.fvec.z = INTEL_INT(obj->orient.fvec.z);
471         obj->orient.uvec.x = INTEL_INT(obj->orient.uvec.x);
472         obj->orient.uvec.y = INTEL_INT(obj->orient.uvec.y);
473         obj->orient.uvec.z = INTEL_INT(obj->orient.uvec.z);
474
475         obj->size          = INTEL_INT(obj->size);
476         obj->shields       = INTEL_INT(obj->shields);
477
478         obj->last_pos.x    = INTEL_INT(obj->last_pos.x);
479         obj->last_pos.y    = INTEL_INT(obj->last_pos.y);
480         obj->last_pos.z    = INTEL_INT(obj->last_pos.z);
481
482         obj->lifeleft      = INTEL_INT(obj->lifeleft);
483
484         switch (obj->movement_type) {
485
486         case MT_PHYSICS:
487
488                 obj->mtype.phys_info.velocity.x = INTEL_INT(obj->mtype.phys_info.velocity.x);
489                 obj->mtype.phys_info.velocity.y = INTEL_INT(obj->mtype.phys_info.velocity.y);
490                 obj->mtype.phys_info.velocity.z = INTEL_INT(obj->mtype.phys_info.velocity.z);
491
492                 obj->mtype.phys_info.thrust.x   = INTEL_INT(obj->mtype.phys_info.thrust.x);
493                 obj->mtype.phys_info.thrust.y   = INTEL_INT(obj->mtype.phys_info.thrust.y);
494                 obj->mtype.phys_info.thrust.z   = INTEL_INT(obj->mtype.phys_info.thrust.z);
495
496                 obj->mtype.phys_info.mass       = INTEL_INT(obj->mtype.phys_info.mass);
497                 obj->mtype.phys_info.drag       = INTEL_INT(obj->mtype.phys_info.drag);
498                 obj->mtype.phys_info.brakes     = INTEL_INT(obj->mtype.phys_info.brakes);
499
500                 obj->mtype.phys_info.rotvel.x   = INTEL_INT(obj->mtype.phys_info.rotvel.x);
501                 obj->mtype.phys_info.rotvel.y   = INTEL_INT(obj->mtype.phys_info.rotvel.y);
502                 obj->mtype.phys_info.rotvel.z   = INTEL_INT(obj->mtype.phys_info.rotvel.z);
503
504                 obj->mtype.phys_info.rotthrust.x = INTEL_INT(obj->mtype.phys_info.rotthrust.x);
505                 obj->mtype.phys_info.rotthrust.y = INTEL_INT(obj->mtype.phys_info.rotthrust.y);
506                 obj->mtype.phys_info.rotthrust.z = INTEL_INT(obj->mtype.phys_info.rotthrust.z);
507
508                 obj->mtype.phys_info.turnroll   = INTEL_INT(obj->mtype.phys_info.turnroll);
509                 obj->mtype.phys_info.flags      = INTEL_SHORT(obj->mtype.phys_info.flags);
510
511                 break;
512
513         case MT_SPINNING:
514
515                 obj->mtype.spin_rate.x = INTEL_INT(obj->mtype.spin_rate.x);
516                 obj->mtype.spin_rate.y = INTEL_INT(obj->mtype.spin_rate.y);
517                 obj->mtype.spin_rate.z = INTEL_INT(obj->mtype.spin_rate.z);
518                 break;
519         }
520
521         switch (obj->control_type) {
522
523         case CT_WEAPON:
524                 obj->ctype.laser_info.parent_type       = INTEL_SHORT(obj->ctype.laser_info.parent_type);
525                 obj->ctype.laser_info.parent_num        = INTEL_SHORT(obj->ctype.laser_info.parent_num);
526                 obj->ctype.laser_info.parent_signature  = INTEL_INT(obj->ctype.laser_info.parent_signature);
527                 obj->ctype.laser_info.creation_time     = INTEL_INT(obj->ctype.laser_info.creation_time);
528                 obj->ctype.laser_info.last_hitobj       = INTEL_SHORT(obj->ctype.laser_info.last_hitobj);
529                 obj->ctype.laser_info.track_goal        = INTEL_SHORT(obj->ctype.laser_info.track_goal);
530                 obj->ctype.laser_info.multiplier        = INTEL_INT(obj->ctype.laser_info.multiplier);
531                 break;
532
533         case CT_EXPLOSION:
534                 obj->ctype.expl_info.spawn_time     = INTEL_INT(obj->ctype.expl_info.spawn_time);
535                 obj->ctype.expl_info.delete_time    = INTEL_INT(obj->ctype.expl_info.delete_time);
536                 obj->ctype.expl_info.delete_objnum  = INTEL_SHORT(obj->ctype.expl_info.delete_objnum);
537                 obj->ctype.expl_info.attach_parent  = INTEL_SHORT(obj->ctype.expl_info.attach_parent);
538                 obj->ctype.expl_info.prev_attach    = INTEL_SHORT(obj->ctype.expl_info.prev_attach);
539                 obj->ctype.expl_info.next_attach    = INTEL_SHORT(obj->ctype.expl_info.next_attach);
540                 break;
541
542         case CT_AI:
543                 obj->ctype.ai_info.hide_segment         = INTEL_SHORT(obj->ctype.ai_info.hide_segment);
544                 obj->ctype.ai_info.hide_index           = INTEL_SHORT(obj->ctype.ai_info.hide_index);
545                 obj->ctype.ai_info.path_length          = INTEL_SHORT(obj->ctype.ai_info.path_length);
546                 obj->ctype.ai_info.danger_laser_num     = INTEL_SHORT(obj->ctype.ai_info.danger_laser_num);
547                 obj->ctype.ai_info.danger_laser_signature = INTEL_INT(obj->ctype.ai_info.danger_laser_signature);
548                 obj->ctype.ai_info.dying_start_time     = INTEL_INT(obj->ctype.ai_info.dying_start_time);
549                 break;
550
551         case CT_LIGHT:
552                 obj->ctype.light_info.intensity = INTEL_INT(obj->ctype.light_info.intensity);
553                 break;
554
555         case CT_POWERUP:
556                 obj->ctype.powerup_info.count = INTEL_INT(obj->ctype.powerup_info.count);
557                 obj->ctype.powerup_info.creation_time = INTEL_INT(obj->ctype.powerup_info.creation_time);
558                 // Below commented out 5/2/96 by Matt.  I asked Allender why it was
559                 // here, and he didn't know, and it looks like it doesn't belong.
560                 // if (obj->id == POW_VULCAN_WEAPON)
561                 // obj->ctype.powerup_info.count = VULCAN_WEAPON_AMMO_AMOUNT;
562                 break;
563
564         }
565
566         switch (obj->render_type) {
567
568         case RT_MORPH:
569         case RT_POLYOBJ: {
570                 int i;
571
572                 obj->rtype.pobj_info.model_num      = INTEL_INT(obj->rtype.pobj_info.model_num);
573
574                 for (i=0;i<MAX_SUBMODELS;i++) {
575                         obj->rtype.pobj_info.anim_angles[i].p = INTEL_INT(obj->rtype.pobj_info.anim_angles[i].p);
576                         obj->rtype.pobj_info.anim_angles[i].b = INTEL_INT(obj->rtype.pobj_info.anim_angles[i].b);
577                         obj->rtype.pobj_info.anim_angles[i].h = INTEL_INT(obj->rtype.pobj_info.anim_angles[i].h);
578                 }
579
580                 obj->rtype.pobj_info.subobj_flags   = INTEL_INT(obj->rtype.pobj_info.subobj_flags);
581                 obj->rtype.pobj_info.tmap_override  = INTEL_INT(obj->rtype.pobj_info.tmap_override);
582                 obj->rtype.pobj_info.alt_textures   = INTEL_INT(obj->rtype.pobj_info.alt_textures);
583                 break;
584         }
585
586         case RT_WEAPON_VCLIP:
587         case RT_HOSTAGE:
588         case RT_POWERUP:
589         case RT_FIREBALL:
590                 obj->rtype.vclip_info.vclip_num = INTEL_INT(obj->rtype.vclip_info.vclip_num);
591                 obj->rtype.vclip_info.frametime = INTEL_INT(obj->rtype.vclip_info.frametime);
592                 break;
593
594         case RT_LASER:
595                 break;
596
597         }
598 //  END OF SWAPPING OBJECT STRUCTURE
599
600 }
601
602 #else /* !WORDS_BIGENDIAN */
603
604
605 // Calculates the checksum of a block of memory.
606 ushort netmisc_calc_checksum(void * vptr, int len)
607 {
608         ubyte *ptr = (ubyte *)vptr;
609         unsigned int sum1,sum2;
610
611         sum1 = sum2 = 0;
612
613         while(len--) {
614                 sum1 += *ptr++;
615                 if (sum1 >= 255) sum1 -= 255;
616                 sum2 += sum1;
617         }
618         sum2 %= 255;
619
620         return ((sum1<<8)+ sum2);
621 }
622
623 #endif /* WORDS_BIGENDIAN */
624
625 //--unused-- //Finds the difference between block1 and block2.  Fills in diff_buffer and
626 //--unused-- //returns the size of diff_buffer.
627 //--unused-- int netmisc_find_diff(void *block1, void *block2, int block_size, void *diff_buffer)
628 //--unused-- {
629 //--unused--    int mode;
630 //--unused--    ushort *c1, *c2, *diff_start, *c3;
631 //--unused--    int i, j, size, diff, n , same;
632 //--unused--
633 //--unused--    size=(block_size+1)/sizeof(ushort);
634 //--unused--    c1 = (ushort *)block1;
635 //--unused--    c2 = (ushort *)block2;
636 //--unused--    c3 = (ushort *)diff_buffer;
637 //--unused--
638 //--unused--    mode = same = diff = n = 0;
639 //--unused--
640 //--unused--    //mprintf(0, "=================================\n");
641 //--unused--
642 //--unused--    for (i=0; i<size; i++, c1++, c2++) {
643 //--unused--            if (*c1 != *c2) {
644 //--unused--                    if (mode==0) {
645 //--unused--                            mode = 1;
646 //--unused--                            //mprintf(0, "%ds ", same);
647 //--unused--                            c3[n++] = same;
648 //--unused--                            same=0; diff=0;
649 //--unused--                            diff_start = c2;
650 //--unused--                    }
651 //--unused--                    *c1 = *c2;
652 //--unused--                    diff++;
653 //--unused--                    if (diff==65535) {
654 //--unused--                            mode = 0;
655 //--unused--                            // send how many diff ones.
656 //--unused--                            //mprintf(0, "%dd ", diff);
657 //--unused--                            c3[n++]=diff;
658 //--unused--                            // send all the diff ones.
659 //--unused--                            for (j=0; j<diff; j++)
660 //--unused--                                    c3[n++] = diff_start[j];
661 //--unused--                            same=0; diff=0;
662 //--unused--                            diff_start = c2;
663 //--unused--                    }
664 //--unused--            } else {
665 //--unused--                    if (mode==1) {
666 //--unused--                            mode=0;
667 //--unused--                            // send how many diff ones.
668 //--unused--                            //mprintf(0, "%dd ", diff);
669 //--unused--                            c3[n++]=diff;
670 //--unused--                            // send all the diff ones.
671 //--unused--                            for (j=0; j<diff; j++)
672 //--unused--                                    c3[n++] = diff_start[j];
673 //--unused--                            same=0; diff=0;
674 //--unused--                            diff_start = c2;
675 //--unused--                    }
676 //--unused--                    same++;
677 //--unused--                    if (same==65535) {
678 //--unused--                            mode=1;
679 //--unused--                            // send how many the same
680 //--unused--                            //mprintf(0, "%ds ", same);
681 //--unused--                            c3[n++] = same;
682 //--unused--                            same=0; diff=0;
683 //--unused--                            diff_start = c2;
684 //--unused--                    }
685 //--unused--            }
686 //--unused--
687 //--unused--    }
688 //--unused--    if (mode==0) {
689 //--unused--            // send how many the same
690 //--unused--            //mprintf(0, "%ds ", same);
691 //--unused--            c3[n++] = same;
692 //--unused--    } else {
693 //--unused--            // send how many diff ones.
694 //--unused--            //mprintf(0, "%dd ", diff);
695 //--unused--            c3[n++]=diff;
696 //--unused--            // send all the diff ones.
697 //--unused--            for (j=0; j<diff; j++)
698 //--unused--                    c3[n++] = diff_start[j];
699 //--unused--    }
700 //--unused--
701 //--unused--    //mprintf(0, "=================================\n");
702 //--unused--
703 //--unused--    return n*2;
704 //--unused-- }
705
706 //--unused-- //Applies diff_buffer to block1 to create a new block1.  Returns the final
707 //--unused-- //size of block1.
708 //--unused-- int netmisc_apply_diff(void *block1, void *diff_buffer, int diff_size)
709 //--unused-- {
710 //--unused--    unsigned int i, j, n, size;
711 //--unused--    ushort *c1, *c2;
712 //--unused--
713 //--unused--    //mprintf(0, "=================================\n");
714 //--unused--    c1 = (ushort *)diff_buffer;
715 //--unused--    c2 = (ushort *)block1;
716 //--unused--
717 //--unused--    size = diff_size/2;
718 //--unused--
719 //--unused--    i=j=0;
720 //--unused--    while (1) {
721 //--unused--            j += c1[i];         // Same
722 //--unused--            //mprintf(0, "%ds ", c1[i]);
723 //--unused--            i++;
724 //--unused--            if (i>=size) break;
725 //--unused--            n = c1[i];          // ndiff
726 //--unused--            //mprintf(0, "%dd ", c1[i]);
727 //--unused--            i++;
728 //--unused--            if (n>0) {
729 //--unused--                    //Assert(n* < 256);
730 //--unused--                    memcpy(&c2[j], &c1[i], n*2);
731 //--unused--                    i += n;
732 //--unused--                    j += n;
733 //--unused--            }
734 //--unused--            if (i>=size) break;
735 //--unused--    }
736 //--unused--    //mprintf(0, "=================================\n");
737 //--unused--
738 //--unused--    return j*2;
739 //--unused-- }