| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/Dmfc/dmfcprecord.cpp $ |
| 21 | * $Revision: 1.1.1.1 $ |
| 22 | * $Date: 2003/08/26 03:57:21 $ |
| 23 | * $Author: kevinb $ |
| 24 | * |
| 25 | * DMFC Player record functions |
| 26 | * |
| 27 | * $Log: dmfcprecord.cpp,v $ |
| 28 | * Revision 1.1.1.1 2003/08/26 03:57:21 kevinb |
| 29 | * initial 1.5 import |
| 30 | * |
| 31 | * |
| 32 | * 18 10/21/99 9:28p Jeff |
| 33 | * B.A. Macintosh code merge |
| 34 | * |
| 35 | * 17 7/09/99 2:54p Jeff |
| 36 | * handle gametime better (pause it when needed) if the server is 'waiting |
| 37 | * for players' |
| 38 | * |
| 39 | * 16 5/21/99 10:44p Jeff |
| 40 | * fix PXO crash due to bytes being out of line when joining a game |
| 41 | * |
| 42 | * 15 5/13/99 4:55p Ardussi |
| 43 | * changes for compiling on the Mac |
| 44 | * |
| 45 | * 14 5/12/99 11:05p Jeff |
| 46 | * dmfc and multiplayer games now have endian friendly packets (*whew*) |
| 47 | * |
| 48 | * 13 3/17/99 12:25p Jeff |
| 49 | * converted DMFC to be COM interface |
| 50 | * |
| 51 | * 12 2/11/99 12:51a Jeff |
| 52 | * changed names of exported variables |
| 53 | * |
| 54 | * 11 12/08/98 4:47p Jeff |
| 55 | * umm, various changes, fixed pilot pics so that they work for everyone |
| 56 | * now |
| 57 | * |
| 58 | * 10 11/11/98 7:19p Jeff |
| 59 | * changes made so that a dedicated server's team is always -1 (team game |
| 60 | * or not) |
| 61 | * |
| 62 | * 9 10/18/98 7:59p Jeff |
| 63 | * functions added to dmfc for client->server objnum matching. Banning |
| 64 | * now uses tracker id when available. |
| 65 | * |
| 66 | * 8 10/17/98 7:30p Jeff |
| 67 | * network_address compares don't compare port on somethings |
| 68 | * |
| 69 | * 7 9/30/98 3:50p Jeff |
| 70 | * general improvements (many) |
| 71 | * |
| 72 | * 6 9/29/98 3:04p Jeff |
| 73 | * added time in game and start_time support |
| 74 | * |
| 75 | * 5 9/22/98 5:20p Jason |
| 76 | * fixed some player bugs |
| 77 | * |
| 78 | * 4 9/21/98 7:11p Jeff |
| 79 | * made InputCommand interface API and moved existing input commands to |
| 80 | * the interface. Changed mprintf/ASSERT so they are valid in DMFC |
| 81 | * |
| 82 | * $NoKeywords: $ |
| 83 | */ |
| 84 | |
| 85 | #include "gamedll_header.h" |
| 86 | #include "DMFC.h" |
| 87 | #include "dmfcinternal.h" |
| 88 | #include <stdlib.h> |
| 89 | #include <string.h> |
| 90 | |
| 91 | extern DMFCBase *basethis; |
| 92 | player_record Player_records[MAX_PLAYER_RECORDS]; |
| 93 | int Pnum_to_PRec[DLLMAX_PLAYERS]; |
| 94 | |
| 95 | // callback pointers for packet packing and unpacking |
| 96 | int (*Player_record_pack_cb)(void *user_info, uint8_t *data); |
| 97 | int (*Player_record_unpack_cb)(void *user_info, uint8_t *data); |
| 98 | |
| 99 | // Initializes the Player records |
| 100 | void PRec_Init(void) { |
| 101 | int i; |
| 102 | |
| 103 | for (i = 0; i < MAX_PLAYER_RECORDS; i++) { |
| 104 | memset(&Player_records[i], 0, sizeof(player_record)); |
| 105 | Player_records[i].state = STATE_EMPTY; |
| 106 | Player_records[i].user_info = NULL; |
| 107 | Player_records[i].pinfo = NULL; |
| 108 | Player_records[i].tracker_id = NULL; |
| 109 | Player_records[i].user_info_size = 0; |
| 110 | Player_records[i].total_time_in_game = 0; |
| 111 | } |
| 112 | for (i = 0; i < DLLMAX_PLAYERS; i++) |
| 113 | Pnum_to_PRec[i] = -1; |
| 114 | |
| 115 | Player_record_pack_cb = NULL; |
| 116 | Player_record_unpack_cb = NULL; |
| 117 | } |
| 118 | |
| 119 | // Frees up and closes the Player records |
| 120 | void PRec_Close(void) { |
| 121 | for (int i = 0; i < MAX_PLAYER_RECORDS; i++) { |
| 122 | if (Player_records[i].user_info) |
| 123 | free(Player_records[i].user_info); |
| 124 | |
| 125 | if (Player_records[i].tracker_id) |
| 126 | free(Player_records[i].tracker_id); |
| 127 | |
| 128 | if (Player_records[i].pinfo) { |
| 129 | delete Player_records[i].pinfo; |
| 130 | Player_records[i].pinfo = NULL; |
| 131 | } |
| 132 | } |
| 133 | Player_record_pack_cb = NULL; |
| 134 | Player_record_unpack_cb = NULL; |
| 135 | } |
| 136 | |
| 137 | // Sets up the Player records so they can be used for Multiplayer user defined stats |
| 138 | // sizeof_user_stats : size of the struct for each player's stats |
| 139 | // pack_callback : callback function called when data from the struct needs to be packed |
| 140 | // into a packet. It is SUPER important that this function packs the data |
| 141 | // in little endian format. This function gets a pointer to the struct that |
| 142 | // needs to be packed (user_info), and a buffer in which to pack it to. This |
| 143 | // function is to return the number of bytes it has packed. |
| 144 | // unpack_callback : callback function called when data from the struct needs to be unpacket |
| 145 | // from a packet. This data will be in little endian format. Returns the number of |
| 146 | // bytes unpacked. |
| 147 | // returns: 1 if size given was <=0 (if so all previous user stats will be removed) |
| 148 | // 0 all went ok |
| 149 | // -1 out of memory (all user stats memory will be freed) |
| 150 | // -2 invalid callbacks |
| 151 | int PRec_SetupUserPRec(int sizeof_user_stats, int (*pack_callback)(void *user_info, uint8_t *data), |
| 152 | int (*unpack_callback)(void *user_info, uint8_t *data)) { |
| 153 | ASSERT(pack_callback != NULL); |
| 154 | ASSERT(unpack_callback != NULL); |
| 155 | if (!pack_callback || !unpack_callback) |
| 156 | return -2; |
| 157 | |
| 158 | int i; |
| 159 | |
| 160 | // calculate to make sure they don't specify too much |
| 161 | int size = MAX_GAME_DATA_SIZE - (104 + 5 * MAX_PLAYER_RECORDS); |
| 162 | if (sizeof_user_stats > size) { |
| 163 | // the user mod specified too big of a size |
| 164 | mprintf(0, "DMFC Warning: In PRec_SetupUserPRec(), the size given is too large for a packet\n"); |
| 165 | Int3(); |
| 166 | goto user_stats_err; |
| 167 | } |
| 168 | |
| 169 | // check the first item to see whats set for it |
| 170 | if (Player_records[0].user_info) { |
| 171 | // we need to free up all the user_info's |
| 172 | for (i = 0; i < MAX_PLAYER_RECORDS; i++) { |
| 173 | if (Player_records[i].user_info) { |
| 174 | free(Player_records[i].user_info); |
| 175 | Player_records[i].user_info = NULL; |
| 176 | } |
| 177 | Player_records[i].user_info_size = 0; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (sizeof_user_stats <= 0) |
| 182 | return 1; |
| 183 | |
| 184 | for (i = 0; i < MAX_PLAYER_RECORDS; i++) { |
| 185 | Player_records[i].user_info = malloc(sizeof_user_stats); |
| 186 | if (!Player_records[i].user_info) |
| 187 | goto user_stats_err; |
| 188 | Player_records[i].user_info_size = sizeof_user_stats; |
| 189 | memset(Player_records[i].user_info, 0, sizeof_user_stats); |
| 190 | } |
| 191 | |
| 192 | Player_record_pack_cb = pack_callback; |
| 193 | Player_record_unpack_cb = unpack_callback; |
| 194 | |
| 195 | return 0; |
| 196 | |
| 197 | user_stats_err: |
| 198 | for (i = 0; i < MAX_PLAYER_RECORDS; i++) { |
| 199 | if (Player_records[i].user_info) { |
| 200 | free(Player_records[i].user_info); |
| 201 | Player_records[i].user_info = NULL; |
| 202 | } |
| 203 | Player_records[i].user_info_size = 0; |
| 204 | } |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | // Given a pnum it will return the PRec slot of that player, or -1 if it's not a valid player |
| 209 | int PRec_GetPlayerSlot(int pnum) { |
| 210 | if ((pnum < 0) || (pnum >= DLLMAX_PLAYERS)) |
| 211 | return -1; |
| 212 | return Pnum_to_PRec[pnum]; |
| 213 | } |
| 214 | |
| 215 | // Given a PRec slot, it returns a pointer to the user stats struct, NULL if error, or PRec_SetupUserPRec |
| 216 | // hasn't been called yet |
| 217 | void *PRec_GetUserStats(int slot) { |
| 218 | if ((slot < 0) || (slot >= MAX_PLAYER_RECORDS)) |
| 219 | return NULL; |
| 220 | return Player_records[slot].user_info; |
| 221 | } |
| 222 | |
| 223 | // Returns a pointer to a player_record for the requested slot, NULL if error |
| 224 | player_record *PRec_GetPRecord(int slot) { |
| 225 | if ((slot < 0) || (slot >= MAX_PLAYER_RECORDS)) |
| 226 | return NULL; |
| 227 | return &Player_records[slot]; |
| 228 | } |
| 229 | |
| 230 | // Returns a pointer to a player_record of a player, NULL if error |
| 231 | player_record *PRec_GetPRecordByPnum(int pnum) { |
| 232 | int slot = PRec_GetPlayerSlot(pnum); |
| 233 | return PRec_GetPRecord(slot); |
| 234 | } |
| 235 | |
| 236 | // Sets a disconnect time for a player |
| 237 | void PRec_SetDisconnectTime(int slot, float time) { |
| 238 | if ((slot < 0) || (slot >= MAX_PLAYER_RECORDS)) |
| 239 | return; |
| 240 | Player_records[slot].disconnect_time = time; |
| 241 | } |
| 242 | |
| 243 | // Returns the PRec slot of a player who matches the given info, -1 if none |
| 244 | // addr = network address (can be NULL) |
| 245 | // tracker_id = Mastertracker ID (can be NULL) |
| 246 | int PRec_FindPlayer(const char *callsign, network_address *addr, const char *tracker_id) { |
| 247 | if (!callsign) |
| 248 | return -1; |
| 249 | |
| 250 | bool good_to_use; |
| 251 | |
| 252 | for (int i = 0; i < MAX_PLAYER_RECORDS; i++) { |
| 253 | // check if the slot isn't empty |
| 254 | if (Player_records[i].state == STATE_DISCONNECTED) { |
| 255 | |
| 256 | if (!strcmp(Player_records[i].callsign, callsign)) { |
| 257 | // we have a possible match, the callsign matches |
| 258 | |
| 259 | good_to_use = true; |
| 260 | |
| 261 | if (addr) { |
| 262 | // we need to check the network address |
| 263 | if (!basethis->CompareNetworkAddress(&Player_records[i].net_addr, addr, false)) |
| 264 | good_to_use = false; // the network address doesn't match |
| 265 | } |
| 266 | |
| 267 | if (tracker_id && good_to_use) { |
| 268 | // we need to check the master tracker_id |
| 269 | if (Player_records[i].tracker_id) { |
| 270 | if (strcmp(Player_records[i].tracker_id, tracker_id)) { |
| 271 | good_to_use = false; // the tracker_id doesn't match |
| 272 | } |
| 273 | } else { |
| 274 | good_to_use = false; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (good_to_use) { |
| 279 | // we found the player based on the information passed in |
| 280 | return i; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | return -1; // no matches |
| 286 | } |
| 287 | |
| 288 | // Gets the first available free slot (STATE_EMPTY), if none are available it clears and returns |
| 289 | // the oldest disconnected player (STATE_DISCONNECTED) |
| 290 | int PRec_GetFreeSlot(void) { |
| 291 | int i, old_player = -1; |
| 292 | float old_time = 9999999.0f; |
| 293 | for (i = 0; i < MAX_PLAYER_RECORDS; i++) { |
| 294 | if (Player_records[i].state == STATE_EMPTY) |
| 295 | return i; // we found a free slot |
| 296 | if (Player_records[i].state == STATE_DISCONNECTED) { |
| 297 | if (old_player == -1) { |
| 298 | // we found our first, so make sure we mark it |
| 299 | old_player = i; |
| 300 | old_time = Player_records[i].disconnect_time; |
| 301 | } |
| 302 | // check to see if this player has been disconnected longer than any others |
| 303 | if (Player_records[i].disconnect_time < old_time) { |
| 304 | // he has |
| 305 | old_time = Player_records[i].disconnect_time; |
| 306 | old_player = i; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | ASSERT(old_player != -1); |
| 312 | |
| 313 | if (old_player != -1) { |
| 314 | // go through all the records and remove this guy as a victim |
| 315 | for (int p = 0; p < MAX_PLAYER_RECORDS; p++) { |
| 316 | if ((p != old_player) && (Player_records[p].pinfo)) { |
| 317 | Player_records[p].pinfo->RemoveKiller(old_player); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // clear out this player |
| 322 | if (Player_records[old_player].tracker_id) |
| 323 | free(Player_records[old_player].tracker_id); |
| 324 | void *save_ptr = Player_records[old_player].user_info; |
| 325 | int size = Player_records[old_player].user_info_size; |
| 326 | if (Player_records[old_player].pinfo) { |
| 327 | delete Player_records[old_player].pinfo; |
| 328 | Player_records[old_player].pinfo = NULL; |
| 329 | } |
| 330 | memset(&Player_records[old_player], 0, sizeof(player_record)); |
| 331 | Player_records[old_player].state = STATE_EMPTY; |
| 332 | Player_records[old_player].user_info = save_ptr; |
| 333 | Player_records[old_player].user_info_size = size; |
| 334 | Player_records[old_player].total_time_in_game = 0; |
| 335 | } |
| 336 | return old_player; |
| 337 | } |
| 338 | |
| 339 | // Assigns a player to a specified slot (fills in the info, and prepares the slot for action) |
| 340 | // Do Not use this for a reconnecting player |
| 341 | bool PRec_AssignPlayerToSlot(int pnum, int slot, player *players_array, netplayer *netplayers_array) { |
| 342 | if ((slot < 0) || (slot >= MAX_PLAYER_RECORDS)) |
| 343 | return false; |
| 344 | if ((pnum < 0) || (pnum >= DLLMAX_PLAYERS)) |
| 345 | return false; |
| 346 | if (!players_array) |
| 347 | return false; |
| 348 | if (!netplayers_array) |
| 349 | return false; |
| 350 | if (Player_records[slot].state != STATE_EMPTY) |
| 351 | return false; |
| 352 | |
| 353 | Pnum_to_PRec[pnum] = slot; |
| 354 | Player_records[slot].state = STATE_INGAME; |
| 355 | strcpy(Player_records[slot].callsign, players_array[pnum].callsign); |
| 356 | memcpy(&Player_records[slot].net_addr, &netplayers_array[pnum].addr, sizeof(network_address)); |
| 357 | Player_records[slot].pnum = pnum; |
| 358 | Player_records[slot].start_time = basethis->GetRealGametime(); |
| 359 | Player_records[slot].total_time_in_game = 0; |
| 360 | Player_records[slot].team = players_array[pnum].team; |
| 361 | |
| 362 | if (basethis->IsMasterTrackerGame() && (basethis->Players[pnum].tracker_id)) { |
| 363 | // we are in a master tracker game, so save the tracker ID |
| 364 | mprintf(0, "PREC: Got a PXO Player ID of %s\n", basethis->Players[pnum].tracker_id); |
| 365 | Player_records[slot].tracker_id = strdup(basethis->Players[pnum].tracker_id); |
| 366 | } else { |
| 367 | // either it's not a tracker game or the tracker_id member of players is void |
| 368 | Player_records[slot].tracker_id = NULL; |
| 369 | } |
| 370 | |
| 371 | memset(&Player_records[slot].dstats, 0, sizeof(t_dstat)); |
| 372 | if (Player_records[slot].pinfo) { |
| 373 | delete Player_records[slot].pinfo; |
| 374 | Player_records[slot].pinfo = NULL; |
| 375 | } |
| 376 | Player_records[slot].pinfo = new PInfo(slot); |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | // Reconnects a player to a STATE_DISCONNECTED slot |
| 381 | bool PRec_ReconnectPlayerToSlot(int pnum, int slot, player *players_array, netplayer *netplayers_array) { |
| 382 | if ((slot < 0) || (slot >= MAX_PLAYER_RECORDS)) |
| 383 | return false; |
| 384 | if ((pnum < 0) || (pnum >= DLLMAX_PLAYERS)) |
| 385 | return false; |
| 386 | if (!players_array) |
| 387 | return false; |
| 388 | if (!netplayers_array) |
| 389 | return false; |
| 390 | if (Player_records[slot].state != STATE_DISCONNECTED) |
| 391 | return false; |
| 392 | |
| 393 | Pnum_to_PRec[pnum] = slot; |
| 394 | Player_records[slot].state = STATE_INGAME; |
| 395 | strcpy(Player_records[slot].callsign, players_array[pnum].callsign); |
| 396 | memcpy(&Player_records[slot].net_addr, &netplayers_array[pnum].addr, sizeof(network_address)); |
| 397 | Player_records[slot].pnum = pnum; |
| 398 | Player_records[slot].start_time = basethis->GetRealGametime(); |
| 399 | |
| 400 | if (!Player_records[slot].pinfo) { |
| 401 | // try to create the pinfo now |
| 402 | Player_records[slot].pinfo = new PInfo(slot); |
| 403 | } |
| 404 | return true; |
| 405 | } |
| 406 | |
| 407 | // Disconnects a player from the Player records |
| 408 | bool PRec_DisconnectPlayer(int pnum) { |
| 409 | if ((pnum < 0) || (pnum >= DLLMAX_PLAYERS)) |
| 410 | return false; |
| 411 | |
| 412 | int slot = Pnum_to_PRec[pnum]; |
| 413 | if (slot == -1) |
| 414 | return false; // player was never connected |
| 415 | |
| 416 | Pnum_to_PRec[pnum] = -1; |
| 417 | Player_records[slot].state = STATE_DISCONNECTED; |
| 418 | Player_records[slot].pnum = -1; |
| 419 | |
| 420 | float curr_time = basethis->GetRealGametime(); |
| 421 | float time_in = curr_time - Player_records[slot].start_time; |
| 422 | if (time_in > 0) { |
| 423 | Player_records[slot].total_time_in_game += time_in; |
| 424 | } |
| 425 | |
| 426 | PRec_SetDisconnectTime(slot, curr_time); |
| 427 | return true; |
| 428 | } |
| 429 | |
| 430 | // Clear all the level volatile (things that reset with the change of levels) things |
| 431 | void PRec_LevelReset(void) { |
| 432 | t_dstat *stat; |
| 433 | for (int i = 0; i < MAX_PLAYER_RECORDS; i++) { |
| 434 | stat = &Player_records[i].dstats; |
| 435 | stat->kills[DSTAT_LEVEL] = 0; |
| 436 | stat->deaths[DSTAT_LEVEL] = 0; |
| 437 | stat->suicides[DSTAT_LEVEL] = 0; |
| 438 | if (Player_records[i].pinfo) |
| 439 | Player_records[i].pinfo->ResetAll(); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | // Gets the team of a player given the pnum |
| 444 | int PRec_GetPlayerTeam(int pnum) { |
| 445 | int slot = PRec_GetPlayerSlot(pnum); |
| 446 | if (slot == -1) |
| 447 | return 0; |
| 448 | return Player_records[slot].team; |
| 449 | } |
| 450 | |
| 451 | // Sets the team of a player given the pnum |
| 452 | void PRec_SetPlayerTeam(int pnum, int team) { |
| 453 | int slot = PRec_GetPlayerSlot(pnum); |
| 454 | if (slot == -1) |
| 455 | return; |
| 456 | Player_records[slot].team = team; |
| 457 | } |
| 458 | |
| 459 | void MultiPackNetworkAddress(network_address *addr, uint8_t *data, int *count) { |
| 460 | memcpy(&data[*count], addr->address, 6); |
| 461 | (*count) += 6; |
| 462 | MultiAddUshort(addr->port, data, count); |
| 463 | memcpy(&data[*count], addr->net_id, 4); |
| 464 | (*count) += 4; |
| 465 | MultiAddByte(addr->connection_type, data, count); |
| 466 | } |
| 467 | |
| 468 | void MultiUnpackNetworkAddress(network_address *addr, uint8_t *data, int *count) { |
| 469 | memcpy(addr->address, &data[*count], 6); |
| 470 | (*count) += 6; |
| 471 | addr->port = MultiGetUshort(data, count); |
| 472 | memcpy(addr->net_id, &data[*count], 4); |
| 473 | (*count) += 4; |
| 474 | addr->connection_type = (network_protocol)MultiGetByte(data, count); |
| 475 | } |
| 476 | |
| 477 | void MultiPackDStats(t_dstat *stat, uint8_t *data, int *count) { |
| 478 | MultiAddInt(stat->kills[0], data, count); |
| 479 | MultiAddInt(stat->kills[1], data, count); |
| 480 | MultiAddInt(stat->deaths[0], data, count); |
| 481 | MultiAddInt(stat->deaths[1], data, count); |
| 482 | MultiAddInt(stat->suicides[0], data, count); |
| 483 | MultiAddInt(stat->suicides[1], data, count); |
| 484 | } |
| 485 | |
| 486 | void MultiUnpackDStats(t_dstat *stat, uint8_t *data, int *count) { |
| 487 | stat->kills[0] = MultiGetInt(data, count); |
| 488 | stat->kills[1] = MultiGetInt(data, count); |
| 489 | stat->deaths[0] = MultiGetInt(data, count); |
| 490 | stat->deaths[1] = MultiGetInt(data, count); |
| 491 | stat->suicides[0] = MultiGetInt(data, count); |
| 492 | stat->suicides[1] = MultiGetInt(data, count); |
| 493 | } |
| 494 | |
| 495 | // Sends the Player records info to the given client |
| 496 | void PRec_SendPRecToPlayer(int pnum) { |
| 497 | if (basethis->GetLocalRole() != LR_SERVER) |
| 498 | return; |
| 499 | |
| 500 | uint8_t data[MAX_GAME_DATA_SIZE]; |
| 501 | int callsignlen, totalsize, totalcount; |
| 502 | totalsize = totalcount = 0; |
| 503 | int pinfo_size, count; |
| 504 | |
| 505 | for (int i = 0; i < MAX_PLAYER_RECORDS; i++) { |
| 506 | if (Player_records[i].state != STATE_EMPTY) { |
| 507 | callsignlen = strlen(Player_records[i].callsign); |
| 508 | |
| 509 | if (Player_records[i].pinfo) |
| 510 | pinfo_size = Player_records[i].pinfo->GetSizeOfData(); |
| 511 | else |
| 512 | pinfo_size = 0; |
| 513 | |
| 514 | uint8_t tracker_len = 0; |
| 515 | |
| 516 | if (Player_records[i].tracker_id) { |
| 517 | tracker_len = strlen(Player_records[i].tracker_id); |
| 518 | } |
| 519 | |
| 520 | count = 0; |
| 521 | basethis->StartPacket(data, SPID_PRECORD, &count); |
| 522 | |
| 523 | MultiAddByte(i, data, &count); // pack byte (record num) |
| 524 | MultiAddByte(Player_records[i].state, data, &count); // pack byte (state) |
| 525 | MultiAddByte(callsignlen, data, &count); // pack byte (strlen(callsign)) |
| 526 | memcpy(&data[count], Player_records[i].callsign, callsignlen); |
| 527 | count += callsignlen; // pack bytes (callsign) |
| 528 | MultiPackNetworkAddress(&Player_records[i].net_addr, data, &count); // pack bytes (network_address) |
| 529 | MultiAddByte(Player_records[i].pnum, data, &count); // pack byte (pnum) |
| 530 | MultiAddByte((Player_records[i].team == -1) ? 255 : Player_records[i].team, data, &count); // pack byte (team) |
| 531 | |
| 532 | if (basethis->IsMasterTrackerGame()) { |
| 533 | MultiAddByte(1, data, &count); |
| 534 | MultiAddByte(tracker_len, data, &count); // pack byte (tracker len) |
| 535 | if (tracker_len) { |
| 536 | memcpy(&data[count], Player_records[i].tracker_id, tracker_len); |
| 537 | count += tracker_len; // pack bytes (tracker_id) |
| 538 | } |
| 539 | } else { |
| 540 | MultiAddByte(0, data, &count); |
| 541 | } |
| 542 | |
| 543 | MultiAddFloat(Player_records[i].disconnect_time, data, &count); // pack float (disconnect_time/start_time) |
| 544 | MultiAddFloat(Player_records[i].total_time_in_game, data, &count); // pack float (total time in game) |
| 545 | |
| 546 | MultiPackDStats(&Player_records[i].dstats, data, &count); // pack bytes (dstats) |
| 547 | |
| 548 | MultiAddByte((Player_records[i].user_info_size > 0) ? 1 : 0, data, &count); // pack byte (there is user info) |
| 549 | if (Player_records[i].user_info_size > 0) // if(user_info_size>0) |
| 550 | { |
| 551 | if (Player_record_pack_cb) { |
| 552 | count += (*Player_record_pack_cb)(Player_records[i].user_info, &data[count]); |
| 553 | } else { |
| 554 | Int3(); |
| 555 | count += Player_records[i].user_info_size; // attempt to recover |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | // PInfo stuff |
| 560 | MultiAddInt(pinfo_size, data, &count); |
| 561 | if (pinfo_size > 0) { |
| 562 | Player_records[i].pinfo->PackData(&data[count]); |
| 563 | count += pinfo_size; |
| 564 | } |
| 565 | |
| 566 | totalsize += count; |
| 567 | totalcount++; |
| 568 | basethis->SendPacket(data, count, pnum); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | mprintf(0, "*Player Record: Send out %d packets of total %d bytes to %s\n", |
| 573 | totalcount, |
| 574 | totalsize, |
| 575 | basethis->Players[pnum].callsign); |
| 576 | } |
| 577 | |
| 578 | // Receives the Player records info from the server |
| 579 | void PRec_ReceivePRecFromServer(uint8_t *data) { |
| 580 | int count = 0; |
| 581 | int callsignlen; |
| 582 | int slot = MultiGetByte(data, &count); // unpack byte (record num) |
| 583 | int pinfo_size; |
| 584 | player_record *pr; |
| 585 | ASSERT((slot >= 0) && (slot < MAX_PLAYER_RECORDS)); |
| 586 | pr = &Player_records[slot]; |
| 587 | |
| 588 | pr->state = (slot_state)MultiGetByte(data, &count); // unpack byte (state) |
| 589 | callsignlen = MultiGetByte(data, &count); // unpack byte (callsign length) |
| 590 | memcpy(pr->callsign, &data[count], callsignlen); |
| 591 | count += callsignlen; // unpack bytes (callsign) |
| 592 | pr->callsign[callsignlen] = '\0'; |
| 593 | MultiUnpackNetworkAddress(&pr->net_addr, data, &count); // unpack bytes (network address) |
| 594 | pr->pnum = MultiGetByte(data, &count); // unpack byte (pnum) |
| 595 | pr->team = MultiGetByte(data, &count); |
| 596 | if (pr->team == 255) // unpack byte (team) |
| 597 | pr->team = -1; |
| 598 | |
| 599 | if (MultiGetByte(data, &count)) { |
| 600 | ASSERT(basethis->IsMasterTrackerGame() != 0); |
| 601 | // there is tracker_id data coming in |
| 602 | uint8_t len = MultiGetByte(data, &count); |
| 603 | pr->tracker_id = (char *)malloc(len + 1); |
| 604 | |
| 605 | if (pr->tracker_id) { |
| 606 | memcpy(pr->tracker_id, &data[count], len); |
| 607 | pr->tracker_id[len] = '\0'; |
| 608 | } else { |
| 609 | mprintf(0, "PREC: UH OH!!! OUT OF MEMORY\n"); |
| 610 | } |
| 611 | count += len; |
| 612 | } else { |
| 613 | ASSERT(basethis->IsMasterTrackerGame() == 0); |
| 614 | } |
| 615 | |
| 616 | pr->disconnect_time = MultiGetFloat(data, &count); // unpack float (disconnect time/start time) |
| 617 | pr->total_time_in_game = MultiGetFloat(data, &count); // unpack float (total time in game) |
| 618 | MultiUnpackDStats(&pr->dstats, data, &count); // unpack bytes (dstats) |
| 619 | |
| 620 | if (MultiGetByte(data, &count)) { |
| 621 | ASSERT(pr->user_info != NULL); |
| 622 | if (Player_record_unpack_cb) { |
| 623 | count += (*Player_record_unpack_cb)(pr->user_info, &data[count]); |
| 624 | } else { |
| 625 | Int3(); |
| 626 | count += pr->user_info_size; // attempt to recover |
| 627 | } |
| 628 | } else { |
| 629 | ASSERT(pr->user_info == NULL); |
| 630 | } |
| 631 | |
| 632 | if (pr->pinfo) { |
| 633 | delete pr->pinfo; |
| 634 | } |
| 635 | pr->pinfo = new PInfo(slot); |
| 636 | |
| 637 | // PInfo stuff |
| 638 | pinfo_size = MultiGetInt(data, &count); |
| 639 | if (pinfo_size > 0) { |
| 640 | if (pr->pinfo) |
| 641 | pr->pinfo->UnpackData(&data[count], pinfo_size); |
| 642 | count += pinfo_size; |
| 643 | } else { |
| 644 | if (pr->pinfo) |
| 645 | pr->pinfo->ResetAll(); |
| 646 | } |
| 647 | |
| 648 | if (pr->pnum != 255) { |
| 649 | Pnum_to_PRec[pr->pnum] = slot; |
| 650 | } |
| 651 | } |