@@ -1340,11 +1340,12 @@ int wss_get_mode(int from_slot, int from_list, int to_slot, int to_list, int wl_
13401340}
13411341
13421342// store all the unit data and pool data
1343- int store_wss_data (ubyte *block, int max_size, interface_snd_id sound,int player_index)
1343+ int store_wss_data (ubyte *data, __UNUSED const unsigned int max_size, interface_snd_id sound, int player_index)
13441344{
1345- int j, i,offset=0 ;
1346- short player_id;
1347- short ishort;
1345+ int j, i, packet_size = 0 ;
1346+ ubyte val;
1347+ short player_id;
1348+ ushort pool_size;
13481349
13491350 // this function assumes that the data is going to be used over the network
13501351 // so make a non-network version of this function if needed
@@ -1354,72 +1355,64 @@ int store_wss_data(ubyte *block, int max_size, interface_snd_id sound,int player
13541355 if ( !(Game_mode & GM_MULTIPLAYER) )
13551356 return 0 ;
13561357
1357-
1358- // write the ship pool
1359- for ( i = 0 ; i < ship_info_size (); i++ ) {
1360- if ( Ss_pool[i] > 0 ) {
1361- block[offset++] = (ubyte)i;
1362- Assert ( Ss_pool[i] < UCHAR_MAX );
1363-
1364- // take care of sign issues
1365- if (Ss_pool[i] == -1 ){
1366- block[offset++] = 0xff ;
1367- } else {
1368- block[offset++] = (ubyte)Ss_pool[i];
1369- }
1358+ // write the ship pool
1359+ pool_size = 0 ;
1360+ for (i = 0 ; i < ship_info_size (); i++) {
1361+ if (Ss_pool[i] > 0 ) {
1362+ ++pool_size;
13701363 }
13711364 }
13721365
1373- block[offset++] = 0xff ; // signals start of weapons pool
1366+ ADD_USHORT (pool_size);
1367+
1368+ Assertion ((((sizeof (short )+sizeof (short )) * pool_size) + packet_size) < max_size, " Size of ship pool exceeds max data size!" );
1369+
1370+ for (i = 0 ; i < ship_info_size (); i++) {
1371+ if (Ss_pool[i] > 0 ) {
1372+ ADD_SHORT (static_cast <short >(i));
1373+ ADD_SHORT (static_cast <short >(Ss_pool[i]));
1374+ }
1375+ }
13741376
13751377 // write the weapon pool
1376- for ( i = 0 ; i < weapon_info_size (); i++ ) {
1377- if ( Wl_pool[i] > 0 ) {
1378- block[offset++] = (ubyte)i;
1379- ishort = INTEL_SHORT ( (short )Wl_pool[i] );
1380- memcpy (block+offset, &ishort, sizeof (short ));
1381- offset += sizeof (short );
1378+ pool_size = 0 ;
1379+ for (i = 0 ; i < weapon_info_size (); i++) {
1380+ if (Wl_pool[i] > 0 ) {
1381+ ++pool_size;
13821382 }
13831383 }
13841384
1385- // write the unit data
1385+ ADD_USHORT (pool_size);
13861386
1387- block[offset++] = 0xff ; // signals start of unit data
1387+ Assertion (((( sizeof ( short )+ sizeof ( short )) * pool_size) + packet_size) < max_size, " Size of weapon pool exceeds max data size! " );
13881388
1389- for ( i=0 ; i<MAX_WSS_SLOTS; i++ ) {
1390- Assert ( Wss_slots[i].ship_class < UCHAR_MAX );
1391- if (Wss_slots[i].ship_class == -1 ){
1392- block[offset++] = 0xff ;
1393- } else {
1394- block[offset++] = (ubyte)(Wss_slots[i].ship_class );
1389+ for (i = 0 ; i < weapon_info_size (); i++) {
1390+ if (Wl_pool[i] > 0 ) {
1391+ ADD_SHORT (static_cast <short >(i));
1392+ ADD_SHORT (static_cast <short >(Wl_pool[i]));
13951393 }
1396- for ( j = 0 ; j < MAX_SHIP_WEAPONS; j++ ) {
1397- // take care of sign issues
1398- Assert ( Wss_slots[i].wep [j] < UCHAR_MAX );
1399- if (Wss_slots[i].wep [j] == -1 ){
1400- block[offset++] = 0xff ;
1401- } else {
1402- block[offset++] = (ubyte)(Wss_slots[i].wep [j]);
1403- }
1394+ }
14041395
1405- Assert ( Wss_slots[i].wep_count [j] < SHRT_MAX );
1406- ishort = INTEL_SHORT ( (short )Wss_slots[i].wep_count [j] );
1396+ // write the unit data
1397+ val = MAX_WSS_SLOTS;
1398+ ADD_DATA (val);
1399+ val = MAX_SHIP_WEAPONS;
1400+ ADD_DATA (val);
14071401
1408- memcpy (&(block[offset]), &(ishort), sizeof (short ) );
1409- offset += sizeof (short );
1410- }
1402+ Assertion ((((sizeof (short ) + ((sizeof (short )+sizeof (short )) * MAX_SHIP_WEAPONS)) * MAX_WSS_SLOTS) + packet_size) < max_size, " Size of wss data exceeds max data size!" );
14111403
1412- // mwa -- old way below -- too much space
1413- // memcpy(block+offset, &Wss_slots[i], sizeof(wss_unit));
1414- // offset += sizeof(wss_unit);
1404+ for (i = 0 ; i < MAX_WSS_SLOTS; i++) {
1405+ ADD_SHORT (static_cast <short >(Wss_slots[i].ship_class ));
1406+
1407+ for (j = 0 ; j < MAX_SHIP_WEAPONS; j++) {
1408+ ADD_SHORT (static_cast <short >(Wss_slots[i].wep [j]));
1409+ Assert (Wss_slots[i].wep_count [j] < SHRT_MAX);
1410+ ADD_SHORT (static_cast <short >(Wss_slots[i].wep_count [j]));
1411+ }
14151412 }
14161413
14171414 // any sound index
1418- if (!sound.isValid ()){
1419- block[offset++] = 0xff ;
1420- } else {
1421- block[offset++] = (ubyte)sound.value ();
1422- }
1415+ ADD_SHORT (static_cast <short >(sound.value ()));
14231416
14241417 // add a netplayer address to identify who should play the sound
14251418 player_id = -1 ;
@@ -1428,20 +1421,19 @@ int store_wss_data(ubyte *block, int max_size, interface_snd_id sound,int player
14281421 player_id = Net_players[player_index].player_id ;
14291422 }
14301423
1431- player_id = INTEL_SHORT ( player_id );
1432- memcpy (block+offset,&player_id,sizeof (player_id));
1433- offset += sizeof (player_id);
1424+ ADD_SHORT (player_id);
14341425
1435- Assert ( offset < max_size );
1436- return offset ;
1426+ Assert ( packet_size < static_cast < int >( max_size) );
1427+ return packet_size ;
14371428}
14381429
1439- int restore_wss_data (ubyte *block )
1430+ int restore_wss_data (ubyte *data )
14401431{
1441- int i, j, sanity, offset=0 ;
1442- ubyte b1, b2,sound;
1443- short ishort;
1444- short player_id;
1432+ int i, j, offset = 0 ;
1433+ ubyte num_slots, num_weapons;
1434+ short b1, b2;
1435+ short player_id, sound;
1436+ ushort pool_size;
14451437
14461438 // this function assumes that the data is going to be used over the network
14471439 // so make a non-network version of this function if needed
@@ -1452,86 +1444,73 @@ int restore_wss_data(ubyte *block)
14521444 return 0 ;
14531445
14541446 // restore ship pool
1455- sanity=0 ;
14561447 memset (Ss_pool, 0 , MAX_SHIP_CLASSES*sizeof (int ));
1457- for (;;) {
1458- if ( sanity++ > MAX_SHIP_CLASSES ) {
1459- Int3 ();
1460- break ;
1461- }
1448+ GET_USHORT (pool_size);
14621449
1463- b1 = block[offset++];
1464- if ( b1 == 0xff ) {
1465- break ;
1466- }
1467-
1468- // take care of sign issues
1469- b2 = block[offset++];
1470- if (b2 == 0xff ){
1471- Ss_pool[b1] = -1 ;
1472- } else {
1450+ for (i = 0 ; i < pool_size; i++) {
1451+ GET_SHORT (b1);
1452+ GET_SHORT (b2);
1453+
1454+ if (b1 < MAX_SHIP_CLASSES) {
14731455 Ss_pool[b1] = b2;
14741456 }
14751457 }
14761458
14771459 // restore weapons pool
1478- sanity=0 ;
14791460 memset (Wl_pool, 0 , MAX_WEAPON_TYPES*sizeof (int ));
1480- for (;;) {
1481- if ( sanity++ > MAX_WEAPON_TYPES ) {
1482- Int3 ();
1483- break ;
1461+ GET_USHORT (pool_size);
1462+
1463+ for (i = 0 ; i < pool_size; i++) {
1464+ GET_SHORT (b1);
1465+ GET_SHORT (b2);
1466+
1467+ if (b1 < MAX_SHIP_CLASSES) {
1468+ Wl_pool[b1] = b2;
14841469 }
1470+ }
14851471
1486- b1 = block[offset++];
1487- if ( b1 == 0xff ) {
1488- break ;
1472+
1473+ // restore unit data
1474+ for (i = 0 ; i < MAX_WSS_SLOTS; i++) {
1475+ Wss_slots[i].ship_class = -1 ;
1476+
1477+ for (j = 0 ; j < MAX_SHIP_WEAPONS; j++) {
1478+ Wss_slots[i].wep [j] = -1 ;
1479+ Wss_slots[i].wep_count [j] = 0 ;
14891480 }
1490-
1491- memcpy (&ishort, block+offset, sizeof (short ));
1492- offset += sizeof (short );
1493- Wl_pool[b1] = INTEL_SHORT ( ishort );
14941481 }
14951482
1496- for ( i=0 ; i<MAX_WSS_SLOTS; i++ ) {
1497- if (block[offset] == 0xff ){
1498- Wss_slots[i].ship_class = -1 ;
1499- } else {
1500- Wss_slots[i].ship_class = block[offset];
1483+ GET_DATA (num_slots);
1484+ GET_DATA (num_weapons);
1485+
1486+ for (i = 0 ; i < num_slots; i++) {
1487+ GET_SHORT (b1);
1488+
1489+ if (i < MAX_WSS_SLOTS) {
1490+ Wss_slots[i].ship_class = b1;
15011491 }
1502- offset++;
1503- for ( j = 0 ; j < MAX_SHIP_WEAPONS ; j++ ) {
1504- // take care of sign issues
1505- if (block[offset] == 0xff ){
1506- Wss_slots[i]. wep [j] = - 1 ;
1507- offset++;
1508- } else {
1509- Wss_slots[i].wep [j] = ( int )(block[offset++]) ;
1492+
1493+ for (j = 0 ; j < num_weapons ; j++) {
1494+ GET_SHORT (b1);
1495+ GET_SHORT (b2);
1496+
1497+ if ( (i < MAX_WSS_SLOTS) && (j < MAX_SHIP_WEAPONS) ) {
1498+ Wss_slots[i]. wep [j] = b1;
1499+ Wss_slots[i].wep_count [j] = b2 ;
15101500 }
1511-
1512- memcpy ( &ishort, &(block[offset]), sizeof (short ) );
1513- ishort = INTEL_SHORT ( ishort );
1514- Wss_slots[i].wep_count [j] = (int )ishort;
1515- offset += sizeof (short );
15161501 }
1517-
1518- // mwa -- old way below
1519- // memcpy(&Wss_slots[i], block+offset, sizeof(wss_unit));
1520- // offset += sizeof(wss_unit);
15211502 }
15221503
15231504 // read in the sound data
1524- sound = block[offset++]; // the sound index
1505+ GET_SHORT ( sound);
15251506
15261507 // read in the player address
1527- memcpy (&player_id,block+offset,sizeof (player_id));
1528- player_id = INTEL_SHORT ( player_id );
1529- offset += sizeof (short );
1508+ GET_SHORT (player_id);
15301509
15311510 // determine if I'm the guy who should be playing the sound
15321511 if ((Net_player != NULL ) && (Net_player->player_id == player_id)){
15331512 // play the sound
1534- if (sound != 0xff ) {
1513+ if (sound >= 0 ) {
15351514 gamesnd_play_iface (static_cast <InterfaceSounds>(sound));
15361515 }
15371516 }
0 commit comments