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