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