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