| 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/coop/coop.cpp $ |
| 21 | * $Revision: 1.1.1.1 $ |
| 22 | * $Date: 2003/08/26 03:56:42 $ |
| 23 | * $Author: kevinb $ |
| 24 | * |
| 25 | * Co-Op Play |
| 26 | * |
| 27 | * $Log: coop.cpp,v $ |
| 28 | * Revision 1.1.1.1 2003/08/26 03:56:42 kevinb |
| 29 | * initial 1.5 import |
| 30 | * |
| 31 | * |
| 32 | * 23 10/21/99 9:27p Jeff |
| 33 | * B.A. Macintosh code merge |
| 34 | * |
| 35 | * 22 7/12/99 1:17p Jeff |
| 36 | * added netgame flag NF_COOP |
| 37 | * |
| 38 | * 21 5/23/99 3:04a Jason |
| 39 | * fixed bug with player rankings not being updated correctly |
| 40 | * |
| 41 | * 20 5/19/99 3:53p Jeff |
| 42 | * fixed possible (???) bug with displaying hud names |
| 43 | * |
| 44 | * 19 5/12/99 11:04p Jeff |
| 45 | * dmfc and multiplayer games now have endian friendly packets (*whew*) |
| 46 | * |
| 47 | * 18 5/09/99 6:20a Jeff |
| 48 | * improved Entropy (added sounds, max virii per room). Fixed rendering |
| 49 | * bugs for other multiplayer dlls. |
| 50 | * |
| 51 | * 17 5/07/99 12:52p Jeff |
| 52 | * audio taunt icon is ppic if available. coop has hard max team set of 4 |
| 53 | * |
| 54 | * 16 5/02/99 8:39a Jeff |
| 55 | * made quick & dirty PLR for coop |
| 56 | * |
| 57 | * 15 4/23/99 6:15p Jeff |
| 58 | * fixed double calls to GameClose |
| 59 | * |
| 60 | * 14 4/23/99 12:43p Jeff |
| 61 | * forgot to call CloseGame |
| 62 | * |
| 63 | * 13 4/21/99 5:34p Jeff |
| 64 | * added requirements |
| 65 | * |
| 66 | * 12 4/19/99 6:07p Jeff |
| 67 | * compile for Linux |
| 68 | * |
| 69 | * 11 3/27/99 4:53p Jeff |
| 70 | * player rankings in multiplayer games implemented. Fixed join message |
| 71 | * so it doesn't get cut off |
| 72 | * |
| 73 | * 10 3/19/99 12:54p Jeff |
| 74 | * base support for requesting the number of teams for a multiplayer game |
| 75 | * |
| 76 | * 9 3/18/99 8:40p Jeff |
| 77 | * hi res font fixes |
| 78 | * |
| 79 | * 8 3/17/99 12:23p Jeff |
| 80 | * converted DMFC to be COM interface |
| 81 | * |
| 82 | * 7 3/01/99 4:17p Jeff |
| 83 | * added net flag whether buddy bot allowed |
| 84 | * |
| 85 | * 6 2/11/99 3:09p Jeff |
| 86 | * localized |
| 87 | * |
| 88 | * 5 2/11/99 12:49a Jeff |
| 89 | * changed names of exported variables |
| 90 | * |
| 91 | * 3 2/07/99 2:06a Jeff |
| 92 | * updated coop...fixed bug when getting countermeasure owner, if owner is |
| 93 | * observer |
| 94 | * |
| 95 | * 2 2/03/99 7:22p Jeff |
| 96 | * |
| 97 | * 1 2/03/99 7:19p Jeff |
| 98 | * |
| 99 | * $NoKeywords: $ |
| 100 | */ |
| 101 | |
| 102 | #include <string.h> |
| 103 | #include <stdio.h> |
| 104 | #include <stdlib.h> |
| 105 | #include "idmfc.h" |
| 106 | #include "coop.h" |
| 107 | #include "coopstr.h" |
| 108 | |
| 109 | #include <algorithm> |
| 110 | |
| 111 | IDMFC *DMFCBase = NULL; |
| 112 | static IDmfcStats *dstat = NULL; |
| 113 | static player *dPlayers; |
| 114 | |
| 115 | struct tPlayerStat { |
| 116 | int Score[2]; |
| 117 | }; |
| 118 | |
| 119 | static int pack_pstat(tPlayerStat *user_info, uint8_t *data); |
| 120 | static int unpack_pstat(tPlayerStat *user_info, uint8_t *data); |
| 121 | |
| 122 | int pack_pstat(tPlayerStat *user_info, uint8_t *data) { |
| 123 | int count = 0; |
| 124 | MultiAddInt(user_info->Score[0], data, &count); |
| 125 | MultiAddInt(user_info->Score[1], data, &count); |
| 126 | return count; |
| 127 | } |
| 128 | |
| 129 | int unpack_pstat(tPlayerStat *user_info, uint8_t *data) { |
| 130 | int count = 0; |
| 131 | user_info->Score[0] = MultiGetInt(data, &count); |
| 132 | user_info->Score[1] = MultiGetInt(data, &count); |
| 133 | return count; |
| 134 | } |
| 135 | |
| 136 | /////////////////////////////////////////////// |
| 137 | // localization info |
| 138 | static char **StringTable; |
| 139 | static int StringTableSize = 0; |
| 140 | static const char *_ErrorString = "Missing String"; |
| 141 | const char *GetStringFromTable(int d) { |
| 142 | if ((d < 0) || (d >= StringTableSize)) |
| 143 | return _ErrorString; |
| 144 | else |
| 145 | return StringTable[d]; |
| 146 | } |
| 147 | /////////////////////////////////////////////// |
| 148 | static int SortedPlayers[MAX_PLAYER_RECORDS]; |
| 149 | static bool DisplayScoreScreen = false; |
| 150 | static bool FirstFrame = false; |
| 151 | static bool display_my_welcome = false; |
| 152 | |
| 153 | static void DisplayWelcomeMessage(int player_num); |
| 154 | static void DisplayHUDScores(struct tHUDItem *hitem); |
| 155 | static void DetermineScore(int precord_num, int column_num, char *buffer, int buffer_size); |
| 156 | static bool compare_slots(int a, int b); |
| 157 | static int Highlight_bmp = -1; |
| 158 | |
| 159 | static void OnPLRInterval(void); |
| 160 | static void OnPLRInit(void); |
| 161 | |
| 162 | void DetermineScore(int precord_num, int column_num, char *buffer, int buffer_size) { |
| 163 | player_record *pr = DMFCBase->GetPlayerRecord(precord_num); |
| 164 | if (!pr || pr->state == STATE_EMPTY) { |
| 165 | buffer[0] = '\0'; |
| 166 | return; |
| 167 | } |
| 168 | tPlayerStat *stat = (tPlayerStat *)pr->user_info; |
| 169 | |
| 170 | snprintf(buffer, buffer_size, "%d", stat->Score[DSTAT_LEVEL]); |
| 171 | } |
| 172 | |
| 173 | // This function gets called by the game when it wants to learn some info about the game |
| 174 | void DLLFUNCCALL DLLGetGameInfo(tDLLOptions *options) { |
| 175 | options->flags = DOF_MAXTEAMS; |
| 176 | options->max_teams = 1; |
| 177 | strcpy(options->game_name, TXT_COOP); |
| 178 | strcpy(options->requirements, "COOP"); |
| 179 | } |
| 180 | |
| 181 | // Initializes the game function pointers |
| 182 | void DLLFUNCCALL DLLGameInit(int *api_func, uint8_t *all_ok, int num_teams_to_use) { |
| 183 | *all_ok = 1; |
| 184 | DMFCBase = CreateDMFC(); |
| 185 | if (!DMFCBase) { |
| 186 | *all_ok = 0; |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | dstat = CreateDmfcStats(); |
| 191 | if (!dstat) { |
| 192 | *all_ok = 0; |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | DMFCBase->LoadFunctions(api_func); |
| 197 | |
| 198 | // Setup event handlers |
| 199 | DMFCBase->Set_OnHUDInterval(OnHUDInterval); |
| 200 | DMFCBase->Set_OnInterval(OnInterval); |
| 201 | DMFCBase->Set_OnKeypress(OnKeypress); |
| 202 | DMFCBase->Set_OnClientPlayerEntersGame(OnClientPlayerEntersGame); |
| 203 | DMFCBase->Set_OnServerObjectKilled(OnServerObjectKilled); |
| 204 | DMFCBase->Set_OnClientObjectKilled(OnClientObjectKilled); |
| 205 | DMFCBase->Set_OnPlayerConnect(OnPlayerConnect); |
| 206 | DMFCBase->Set_OnServerGameCreated(OnServerGameCreated); |
| 207 | DMFCBase->Set_OnClientLevelStart(OnClientLevelStart); |
| 208 | DMFCBase->Set_OnGameStateRequest(OnGameStateRequest); |
| 209 | DMFCBase->Set_OnSaveStatsToFile(OnSaveStatsToFile); |
| 210 | DMFCBase->Set_OnLevelEndSaveStatsToFile(OnLevelEndSaveStatsToFile); |
| 211 | DMFCBase->Set_OnDisconnectSaveStatsToFile(OnDisconnectSaveStatsToFile); |
| 212 | DMFCBase->Set_OnPrintScores(OnPrintScores); |
| 213 | DMFCBase->Set_OnPLRInterval(OnPLRInterval); |
| 214 | DMFCBase->Set_OnPLRInit(OnPLRInit); |
| 215 | |
| 216 | dPlayers = DMFCBase->GetPlayers(); |
| 217 | |
| 218 | DLLCreateStringTable("Coop.str", &StringTable, &StringTableSize); |
| 219 | mprintf(0, "%d strings loaded from string table\n", StringTableSize); |
| 220 | if (!StringTableSize) { |
| 221 | *all_ok = 0; |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | DMFCBase->GameInit(1); |
| 226 | |
| 227 | Highlight_bmp = DLLbm_AllocBitmap(32, 32, 0); |
| 228 | if (Highlight_bmp > BAD_BITMAP_HANDLE) { |
| 229 | uint16_t *data = DLLbm_data(Highlight_bmp, 0); |
| 230 | if (!data) { |
| 231 | // bail on out of here |
| 232 | *all_ok = 0; |
| 233 | return; |
| 234 | } |
| 235 | for (int x = 0; x < 32 * 32; x++) { |
| 236 | data[x] = GR_RGB16(50, 50, 50) | OPAQUE_FLAG; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | DMFCBase->AddHUDItemCallback(HI_TEXT, DisplayHUDScores); |
| 241 | DMFCBase->SetupPlayerRecord(sizeof(tPlayerStat), (int (*)(void *, uint8_t *))pack_pstat, |
| 242 | (int (*)(void *, uint8_t *))unpack_pstat); |
| 243 | DMFCBase->SetNumberOfTeams(1); |
| 244 | |
| 245 | netgame_info *netgameinfo = DMFCBase->GetNetgameInfo(); |
| 246 | netgameinfo->flags |= (NF_USE_ROBOTS | NF_RESPAWN_WAYPOINT | NF_ALLOWGUIDEBOT | NF_COOP); |
| 247 | netgameinfo->max_players = std::min(4, netgameinfo->max_players); |
| 248 | DMFCBase->SetMaxPlayerHardLimit(4); |
| 249 | |
| 250 | // Initialize the Stats Manager |
| 251 | // ---------------------------- |
| 252 | tDmfcStatsInit tsi; |
| 253 | tDmfcStatsColumnInfo pl_col[5]; |
| 254 | char gname[20]; |
| 255 | strcpy(gname, TXT_STATGAMENAME); |
| 256 | |
| 257 | tsi.flags = DSIF_SHOW_PIC | DSIF_SHOW_OBSERVERICON | DSIF_NOLASTKILLER | DSIF_NOLASTVICTIM | DSIF_NODETAILEDINFO | |
| 258 | DSIF_SHOWPLAYERVIEW; |
| 259 | tsi.cColumnCountDetailed = 0; |
| 260 | tsi.cColumnCountPlayerList = 5; |
| 261 | tsi.clbDetailedColumn = NULL; |
| 262 | tsi.clbDetailedColumnBMP = NULL; |
| 263 | tsi.clbPlayerColumn = DetermineScore; |
| 264 | tsi.clbPlayerColumnBMP = NULL; |
| 265 | tsi.DetailedColumns = NULL; |
| 266 | tsi.GameName = gname; |
| 267 | tsi.MaxNumberDisplayed = NULL; |
| 268 | tsi.PlayerListColumns = pl_col; |
| 269 | tsi.SortedPlayerRecords = SortedPlayers; |
| 270 | |
| 271 | pl_col[0].color_type = DSCOLOR_SHIPCOLOR; |
| 272 | strcpy(pl_col[0].title, TXT_PILOT); |
| 273 | pl_col[0].type = DSCOL_PILOT_NAME; |
| 274 | pl_col[0].width = 84; |
| 275 | |
| 276 | pl_col[1].color_type = DSCOLOR_SHIPCOLOR; |
| 277 | strcpy(pl_col[1].title, TXT_KILLS); |
| 278 | pl_col[1].type = DSCOL_CUSTOM; |
| 279 | pl_col[1].width = 51; |
| 280 | |
| 281 | pl_col[2].color_type = DSCOLOR_SHIPCOLOR; |
| 282 | strcpy(pl_col[2].title, TXT_DEATHS); |
| 283 | pl_col[2].type = DSCOL_DEATHS_LEVEL; |
| 284 | pl_col[2].width = 64; |
| 285 | |
| 286 | pl_col[3].color_type = DSCOLOR_SHIPCOLOR; |
| 287 | strcpy(pl_col[3].title, TXT_SUICIDES); |
| 288 | pl_col[3].type = DSCOL_SUICIDES_LEVEL; |
| 289 | pl_col[3].width = 69; |
| 290 | |
| 291 | pl_col[4].color_type = DSCOLOR_SHIPCOLOR; |
| 292 | strcpy(pl_col[4].title, TXT_PING); |
| 293 | pl_col[4].type = DSCOL_PING; |
| 294 | pl_col[4].width = 40; |
| 295 | |
| 296 | dstat->Initialize(&tsi); |
| 297 | } |
| 298 | // Called when the DLL is shutdown |
| 299 | void DLLFUNCCALL DLLGameClose() { |
| 300 | if (Highlight_bmp > BAD_BITMAP_HANDLE) |
| 301 | DLLbm_FreeBitmap(Highlight_bmp); |
| 302 | |
| 303 | DLLDestroyStringTable(StringTable, StringTableSize); |
| 304 | |
| 305 | if (dstat) { |
| 306 | dstat->DestroyPointer(); |
| 307 | dstat = NULL; |
| 308 | } |
| 309 | |
| 310 | if (DMFCBase) { |
| 311 | DMFCBase->GameClose(); |
| 312 | DMFCBase->DestroyPointer(); |
| 313 | DMFCBase = NULL; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void OnHUDInterval(void) { |
| 318 | dstat->DoFrame(); |
| 319 | DMFCBase->DisplayOutrageLogo(); |
| 320 | DMFCBase->OnHUDInterval(); |
| 321 | } |
| 322 | |
| 323 | bool compare_slots(int a, int b) { |
| 324 | int ascore, bscore; |
| 325 | player_record *apr, *bpr; |
| 326 | tPlayerStat *astat, *bstat; |
| 327 | |
| 328 | apr = DMFCBase->GetPlayerRecord(a); |
| 329 | bpr = DMFCBase->GetPlayerRecord(b); |
| 330 | if (!apr) |
| 331 | return true; |
| 332 | if (!bpr) |
| 333 | return false; |
| 334 | if (apr->state == STATE_EMPTY) |
| 335 | return true; |
| 336 | if (bpr->state == STATE_EMPTY) |
| 337 | return false; |
| 338 | astat = (tPlayerStat *)apr->user_info; |
| 339 | bstat = (tPlayerStat *)bpr->user_info; |
| 340 | |
| 341 | if ((apr->state == STATE_INGAME) && (bpr->state == STATE_INGAME)) { |
| 342 | // both players were in the game |
| 343 | ascore = (astat) ? astat->Score[DSTAT_LEVEL] : 0; |
| 344 | bscore = (bstat) ? bstat->Score[DSTAT_LEVEL] : 0; |
| 345 | return (ascore < bscore); |
| 346 | } |
| 347 | if ((apr->state == STATE_INGAME) && (bpr->state == STATE_DISCONNECTED)) { |
| 348 | // apr gets priority since he was in the game on exit |
| 349 | return false; |
| 350 | } |
| 351 | if ((apr->state == STATE_DISCONNECTED) && (bpr->state == STATE_INGAME)) { |
| 352 | // bpr gets priority since he was in the game on exit |
| 353 | return true; |
| 354 | } |
| 355 | // if we got here then both players were disconnected |
| 356 | ascore = (astat) ? astat->Score[DSTAT_LEVEL] : 0; |
| 357 | bscore = (bstat) ? bstat->Score[DSTAT_LEVEL] : 0; |
| 358 | return (ascore < bscore); |
| 359 | } |
| 360 | |
| 361 | void OnInterval(void) { |
| 362 | // sort players |
| 363 | int i, t, j; |
| 364 | |
| 365 | for (i = 0; i < MAX_PLAYER_RECORDS; i++) { |
| 366 | SortedPlayers[i] = i; |
| 367 | } |
| 368 | |
| 369 | for (i = 1; i <= MAX_PLAYER_RECORDS - 1; i++) { |
| 370 | t = SortedPlayers[i]; |
| 371 | // Shift elements down until |
| 372 | // insertion point found. |
| 373 | for (j = i - 1; j >= 0 && compare_slots(SortedPlayers[j], t); j--) { |
| 374 | SortedPlayers[j + 1] = SortedPlayers[j]; |
| 375 | } |
| 376 | // insert |
| 377 | SortedPlayers[j + 1] = t; |
| 378 | } |
| 379 | |
| 380 | FirstFrame = true; |
| 381 | DMFCBase->OnInterval(); |
| 382 | } |
| 383 | |
| 384 | void OnKeypress(int key) { |
| 385 | dllinfo *Data = DMFCBase->GetDLLInfoCallData(); |
| 386 | |
| 387 | switch (key) { |
| 388 | case K_F7: |
| 389 | DisplayScoreScreen = !DisplayScoreScreen; |
| 390 | DMFCBase->EnableOnScreenMenu(false); |
| 391 | dstat->Enable(DisplayScoreScreen); |
| 392 | break; |
| 393 | case K_PAGEDOWN: |
| 394 | if (DisplayScoreScreen) { |
| 395 | dstat->ScrollDown(); |
| 396 | Data->iRet = 1; |
| 397 | } |
| 398 | break; |
| 399 | case K_PAGEUP: |
| 400 | if (DisplayScoreScreen) { |
| 401 | dstat->ScrollUp(); |
| 402 | Data->iRet = 1; |
| 403 | } |
| 404 | break; |
| 405 | case K_F6: |
| 406 | DisplayScoreScreen = false; |
| 407 | dstat->Enable(false); |
| 408 | break; |
| 409 | case K_ESC: |
| 410 | if (DisplayScoreScreen) { |
| 411 | dstat->Enable(false); |
| 412 | DisplayScoreScreen = false; |
| 413 | Data->iRet = 1; |
| 414 | } |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | DMFCBase->OnKeypress(key); |
| 419 | } |
| 420 | |
| 421 | void OnClientPlayerEntersGame(int player_num) { |
| 422 | if (player_num == DMFCBase->GetPlayerNum()) { |
| 423 | FirstFrame = false; |
| 424 | DisplayScoreScreen = false; |
| 425 | } |
| 426 | |
| 427 | DMFCBase->OnClientPlayerEntersGame(player_num); |
| 428 | |
| 429 | if (player_num != DMFCBase->GetPlayerNum()) |
| 430 | DisplayWelcomeMessage(player_num); |
| 431 | else |
| 432 | display_my_welcome = true; |
| 433 | } |
| 434 | |
| 435 | // A new player has entered the game, zero there stats out |
| 436 | void OnPlayerConnect(int player_num) { |
| 437 | DMFCBase->OnPlayerConnect(player_num); |
| 438 | |
| 439 | tPlayerStat *stat = (tPlayerStat *)DMFCBase->GetPlayerRecordData(player_num); |
| 440 | if (stat) { |
| 441 | stat->Score[DSTAT_LEVEL] = 0; |
| 442 | stat->Score[DSTAT_OVERALL] = 0; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // The server has just started, so clear out all the stats and game info |
| 447 | void OnServerGameCreated(void) { |
| 448 | DMFCBase->OnServerGameCreated(); |
| 449 | player_record *pr; |
| 450 | tPlayerStat *stat; |
| 451 | for (int i = 0; i < MAX_PLAYER_RECORDS; i++) { |
| 452 | pr = DMFCBase->GetPlayerRecord(i); |
| 453 | if (pr) |
| 454 | stat = (tPlayerStat *)pr->user_info; |
| 455 | else |
| 456 | stat = NULL; |
| 457 | if (stat) { |
| 458 | stat->Score[DSTAT_LEVEL] = 0; |
| 459 | stat->Score[DSTAT_OVERALL] = 0; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // The server has started a new level, so clear out level scores |
| 465 | void OnClientLevelStart(void) { |
| 466 | DMFCBase->OnClientLevelStart(); |
| 467 | player_record *pr; |
| 468 | tPlayerStat *stat; |
| 469 | for (int i = 0; i < MAX_PLAYER_RECORDS; i++) { |
| 470 | pr = DMFCBase->GetPlayerRecord(i); |
| 471 | if (pr) |
| 472 | stat = (tPlayerStat *)pr->user_info; |
| 473 | else |
| 474 | stat = NULL; |
| 475 | |
| 476 | if (stat) { |
| 477 | stat->Score[DSTAT_LEVEL] = 0; |
| 478 | } |
| 479 | } |
| 480 | DMFCBase->RequestGameState(); |
| 481 | } |
| 482 | |
| 483 | // We need to send all the inventory info out to the new player |
| 484 | void OnGameStateRequest(int player_num) { DMFCBase->OnGameStateRequest(player_num); } |
| 485 | |
| 486 | void DisplayWelcomeMessage(int player_num) { |
| 487 | char name_buffer[64]; |
| 488 | strcpy(name_buffer, (DMFCBase->GetPlayers())[player_num].callsign); |
| 489 | |
| 490 | if (player_num == DMFCBase->GetPlayerNum()) { |
| 491 | DLLAddHUDMessage(TXT_WELCOME, name_buffer); |
| 492 | } else { |
| 493 | DLLAddHUDMessage(TXT_JOINED, name_buffer); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | void OnServerObjectKilled(object *obj, object *killer) { |
| 498 | if (!killer) |
| 499 | return; // can't handle this |
| 500 | |
| 501 | if (obj->type == OBJ_ROBOT || (obj->type == OBJ_BUILDING && obj->ai_info)) { |
| 502 | |
| 503 | // check to see if the killer is a player |
| 504 | bool ok_to_call = false; |
| 505 | |
| 506 | if (killer->type == OBJ_PLAYER) { |
| 507 | ok_to_call = true; |
| 508 | } else if (killer->type == OBJ_ROBOT) { |
| 509 | if (DMFCBase->GetCounterMeasureOwner(killer) != -1) { |
| 510 | ok_to_call = true; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | if (ok_to_call) { |
| 515 | DMFCBase->CallClientEvent(EVT_CLIENT_GAMEOBJKILLED, DMFCBase->GetMeObjNum(), DMFCBase->GetItObjNum(), -1); |
| 516 | DMFCBase->CallOnClientObjectKilled(obj, killer); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | DMFCBase->OnServerObjectKilled(obj, killer); |
| 521 | } |
| 522 | |
| 523 | void OnClientObjectKilled(object *obj, object *killer) { |
| 524 | object *parent; |
| 525 | DLLGetUltimateParentForObject(&parent, killer); |
| 526 | |
| 527 | if ((parent->type != OBJ_PLAYER) && (parent->type != OBJ_OBSERVER)) { |
| 528 | mprintf(0, "Robot killed wasn't by a OBJ_PLAYER or OBJ_OBSERVER (%d)\n", parent->type); |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | int killer_pnum = parent->id; |
| 533 | |
| 534 | player_record *pr = DMFCBase->GetPlayerRecordByPnum(killer_pnum); |
| 535 | if (!pr) { |
| 536 | mprintf(0, "Invalid player record!\n"); |
| 537 | Int3(); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | tPlayerStat *stat = (tPlayerStat *)pr->user_info; |
| 542 | |
| 543 | stat->Score[DSTAT_LEVEL]++; |
| 544 | stat->Score[DSTAT_OVERALL]++; |
| 545 | |
| 546 | mprintf(0, "%s's score is now %d\n", dPlayers[killer_pnum].callsign, stat->Score[DSTAT_LEVEL]); |
| 547 | |
| 548 | DMFCBase->OnClientObjectKilled(obj, killer); |
| 549 | } |
| 550 | |
| 551 | void DisplayHUDScores(struct tHUDItem *hitem) { |
| 552 | if (!FirstFrame) |
| 553 | return; |
| 554 | |
| 555 | if (display_my_welcome) { |
| 556 | DisplayWelcomeMessage(DMFCBase->GetPlayerNum()); |
| 557 | display_my_welcome = false; |
| 558 | } |
| 559 | |
| 560 | if (DisplayScoreScreen) |
| 561 | return; |
| 562 | |
| 563 | if (DMFCBase->IAmDedicatedServer()) |
| 564 | return; |
| 565 | |
| 566 | int old_font = DLLgrtext_GetFont(); |
| 567 | int start_y; |
| 568 | int i; |
| 569 | |
| 570 | DLLgrtext_SetFont((DMFCBase->GetGameFontTranslateArray())[HUD_FONT_INDEX]); |
| 571 | int height = DLLRenderHUDGetTextHeight("X") + 1; |
| 572 | int players_in_game = 0; |
| 573 | |
| 574 | for (i = 0; i < DLLMAX_PLAYERS; i++) { |
| 575 | if (DMFCBase->CheckPlayerNum(i) && !DMFCBase->IsPlayerDedicatedServer(i)) { |
| 576 | players_in_game++; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | if (players_in_game <= 0) |
| 581 | return; |
| 582 | |
| 583 | players_in_game = std::min(players_in_game, 8); |
| 584 | |
| 585 | start_y = (DMFCBase->GetGameWindowH() / 2) - ((players_in_game * 5) / 2); |
| 586 | // determine coordinates to use here |
| 587 | // we'll use a virtual width of 85 pixels on a 640x480 screen |
| 588 | // so first determine the new width |
| 589 | int name_width = 85.0f * DMFCBase->GetHudAspectX(); |
| 590 | int name_x = DMFCBase->GetGameWindowW() - name_width - 10; |
| 591 | |
| 592 | int pnum, index = 0; |
| 593 | uint8_t alpha = DMFCBase->ConvertHUDAlpha((uint8_t)((DisplayScoreScreen) ? 128 : 255)); |
| 594 | |
| 595 | DLLgrtext_SetAlpha(alpha); |
| 596 | char name[64]; |
| 597 | |
| 598 | // Display only the players in the game |
| 599 | while ((players_in_game > 0) && (index < MAX_PLAYER_RECORDS)) { |
| 600 | pnum = SortedPlayers[index]; |
| 601 | |
| 602 | if (DMFCBase->CheckPlayerNum(pnum) && !DMFCBase->IsPlayerDedicatedServer(pnum)) { |
| 603 | // valid player |
| 604 | if (pnum == DMFCBase->GetPlayerNum()) { |
| 605 | if (Highlight_bmp > BAD_BITMAP_HANDLE) { |
| 606 | // draw the highlite bar in the background |
| 607 | DLLrend_SetAlphaValue(alpha * 0.50f); |
| 608 | DLLrend_SetZBufferState(0); |
| 609 | DLLrend_SetTextureType(TT_LINEAR); |
| 610 | DLLrend_SetLighting(LS_NONE); |
| 611 | DLLrend_SetAlphaType(AT_CONSTANT_TEXTURE); |
| 612 | |
| 613 | DLLrend_DrawScaledBitmap(name_x - 2, start_y - 2, DMFCBase->GetGameWindowW() - 8, start_y + height + 1, |
| 614 | Highlight_bmp, 0, 0, 1, 1, 1.0, -1, NULL); |
| 615 | DLLrend_SetZBufferState(1); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | strcpy(name, dPlayers[pnum].callsign); |
| 620 | DMFCBase->ClipString(name_width, name, true); |
| 621 | |
| 622 | DLLgrtext_SetColor((DMFCBase->GetPlayerColors())[pnum]); |
| 623 | DLLgrtext_Printf(name_x, start_y, name); |
| 624 | |
| 625 | start_y += height; |
| 626 | players_in_game--; |
| 627 | } |
| 628 | index++; |
| 629 | } |
| 630 | |
| 631 | DLLgrtext_SetFont(old_font); |
| 632 | } |
| 633 | |
| 634 | void SaveStatsToFile(char *filename) { |
| 635 | /* |
| 636 | CFILE *file; |
| 637 | DLLOpenCFILE(&file,filename,"wt"); |
| 638 | if(!file){ |
| 639 | mprintf(0,"Unable to open output file\n"); |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | //write out game stats |
| 644 | #define BUFSIZE 150 |
| 645 | char buffer[BUFSIZE]; |
| 646 | char tempbuffer[25]; |
| 647 | int sortedslots[MAX_PLAYER_RECORDS]; |
| 648 | player_record *pr,*dpr; |
| 649 | tPInfoStat stat; |
| 650 | int count,length,p; |
| 651 | |
| 652 | count = 1; |
| 653 | |
| 654 | sprintf(buffer,TXT_SAVE_HEADER,Netgame->name,Current_mission->cur_level); |
| 655 | DLLcf_WriteString(file,buffer); |
| 656 | |
| 657 | sprintf(buffer,TXT_SAVE_HEADERB); |
| 658 | DLLcf_WriteString(file,buffer); |
| 659 | strcpy(buffer,"-----------------------------------------------------------------------------------"); |
| 660 | DLLcf_WriteString(file,buffer); |
| 661 | |
| 662 | |
| 663 | tPlayerStat *st; |
| 664 | for(int s=0;s<MAX_PLAYER_RECORDS;s++){ |
| 665 | p = SortedPlayers[s]; |
| 666 | pr = GetPlayerRecord(p); |
| 667 | if( pr && pr->state!=STATE_EMPTY) { |
| 668 | |
| 669 | if(IsPlayerDedicatedServer(pr)) |
| 670 | continue; //skip dedicated server |
| 671 | |
| 672 | st = (tPlayerStat *)pr->user_info; |
| 673 | |
| 674 | memset(buffer,' ',BUFSIZE); |
| 675 | |
| 676 | sprintf(tempbuffer,"%d)",count); |
| 677 | memcpy(&buffer[0],tempbuffer,strlen(tempbuffer)); |
| 678 | |
| 679 | sprintf(tempbuffer,"%s%s",(pr->state==STATE_INGAME)?"":"*",pr->callsign); |
| 680 | memcpy(&buffer[7],tempbuffer,strlen(tempbuffer)); |
| 681 | |
| 682 | sprintf(tempbuffer,"%d[%d]",st->Score[DSTAT_LEVEL],st->Score[DSTAT_OVERALL]); |
| 683 | memcpy(&buffer[36],tempbuffer,strlen(tempbuffer)); |
| 684 | |
| 685 | sprintf(tempbuffer,"%d[%d]",pr->dstats.deaths[DSTAT_LEVEL],pr->dstats.deaths[DSTAT_OVERALL]); |
| 686 | memcpy(&buffer[48],tempbuffer,strlen(tempbuffer)); |
| 687 | |
| 688 | sprintf(tempbuffer,"%d[%d]",pr->dstats.suicides[DSTAT_LEVEL],pr->dstats.suicides[DSTAT_OVERALL]); |
| 689 | memcpy(&buffer[60],tempbuffer,strlen(tempbuffer)); |
| 690 | |
| 691 | sprintf(tempbuffer,"%s",GetTimeString(GetTimeInGame(p))); |
| 692 | memcpy(&buffer[71],tempbuffer,strlen(tempbuffer)); |
| 693 | |
| 694 | int pos; |
| 695 | pos = 71 + strlen(tempbuffer) + 1; |
| 696 | if(pos<BUFSIZE) |
| 697 | buffer[pos] = '\0'; |
| 698 | |
| 699 | buffer[BUFSIZE-1] = '\0'; |
| 700 | DLLcf_WriteString(file,buffer); |
| 701 | count++; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | //done writing stats |
| 706 | DLLcfclose(file); |
| 707 | DLLAddHUDMessage(TXT_MSG_SAVESTATS); |
| 708 | */ |
| 709 | } |
| 710 | |
| 711 | #define ROOTFILENAME "D3Coop" |
| 712 | void OnSaveStatsToFile(void) { |
| 713 | char filename[256]; |
| 714 | DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, false); |
| 715 | SaveStatsToFile(filename); |
| 716 | } |
| 717 | |
| 718 | void OnLevelEndSaveStatsToFile(void) { |
| 719 | char filename[256]; |
| 720 | DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, true); |
| 721 | SaveStatsToFile(filename); |
| 722 | } |
| 723 | |
| 724 | void OnDisconnectSaveStatsToFile(void) { |
| 725 | char filename[256]; |
| 726 | DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, false); |
| 727 | SaveStatsToFile(filename); |
| 728 | } |
| 729 | |
| 730 | void OnPrintScores(int level) { |
| 731 | /* |
| 732 | char buffer[256]; |
| 733 | char name[50]; |
| 734 | memset(buffer,' ',256); |
| 735 | |
| 736 | int t; |
| 737 | int pos[5]; |
| 738 | int len[5]; |
| 739 | pos[0] = 0; t = len[0] = 20; //give ample room for pilot name |
| 740 | pos[1] = pos[0] + t + 1; t = len[1] = strlen(TXT_KILLS_SHORT); |
| 741 | pos[2] = pos[1] + t + 1; t = len[2] = strlen(TXT_DEATHS_SHORT); |
| 742 | pos[3] = pos[2] + t + 1; t = len[3] = strlen(TXT_SUICIDES_SHORT); |
| 743 | pos[4] = pos[3] + t + 1; t = len[4] = strlen(TXT_PING); |
| 744 | |
| 745 | memcpy(&buffer[pos[0]],TXT_PILOT,strlen(TXT_PILOT)); |
| 746 | memcpy(&buffer[pos[1]],TXT_KILLS_SHORT,len[1]); |
| 747 | memcpy(&buffer[pos[2]],TXT_DEATHS_SHORT,len[2]); |
| 748 | memcpy(&buffer[pos[3]],TXT_SUICIDES_SHORT,len[3]); |
| 749 | memcpy(&buffer[pos[4]],TXT_PING,len[4]); |
| 750 | buffer[pos[4]+len[4]+1] = '\n'; |
| 751 | buffer[pos[4]+len[4]+2] = '\0'; |
| 752 | DPrintf(buffer); |
| 753 | |
| 754 | int slot; |
| 755 | player_record *pr; |
| 756 | int pcount; |
| 757 | |
| 758 | if(level<0 || level>=MAX_PLAYER_RECORDS) |
| 759 | pcount = MAX_PLAYER_RECORDS; |
| 760 | else |
| 761 | pcount = level; |
| 762 | |
| 763 | tPlayerStat *st; |
| 764 | |
| 765 | for(int i=0;i<pcount;i++){ |
| 766 | slot = SortedPlayers[i]; |
| 767 | pr = GetPlayerRecord(slot); |
| 768 | if((pr)&&(pr->state!=STATE_EMPTY)){ |
| 769 | |
| 770 | if(IsPlayerDedicatedServer(pr)) |
| 771 | continue; //skip dedicated server |
| 772 | |
| 773 | st = (tPlayerStat *)pr->user_info; |
| 774 | |
| 775 | sprintf(name,"%s%s:",(pr->state==STATE_DISCONNECTED)?"*":"",pr->callsign); |
| 776 | name[19] = '\0'; |
| 777 | |
| 778 | memset(buffer,' ',256); |
| 779 | t = strlen(name); memcpy(&buffer[pos[0]],name,(t<len[0])?t:len[0]); |
| 780 | sprintf(name,"%d",st->Score[DSTAT_LEVEL]); |
| 781 | t = strlen(name); memcpy(&buffer[pos[1]],name,(t<len[1])?t:len[1]); |
| 782 | sprintf(name,"%d",pr->dstats.deaths[DSTAT_LEVEL]); |
| 783 | t = strlen(name); memcpy(&buffer[pos[2]],name,(t<len[2])?t:len[2]); |
| 784 | sprintf(name,"%d",pr->dstats.suicides[DSTAT_LEVEL]); |
| 785 | t = strlen(name); memcpy(&buffer[pos[3]],name,(t<len[3])?t:len[3]); |
| 786 | |
| 787 | if(pr->state==STATE_INGAME) |
| 788 | sprintf(name,"%.0f",NetPlayers[pr->pnum].ping_time*1000.0f); |
| 789 | else |
| 790 | strcpy(name,"---"); |
| 791 | t = strlen(name); memcpy(&buffer[pos[4]],name,(t<len[4])?t:len[4]); |
| 792 | buffer[pos[4]+len[4]+1] = '\n'; |
| 793 | buffer[pos[4]+len[4]+2] = '\0'; |
| 794 | DPrintf(buffer); |
| 795 | } |
| 796 | } |
| 797 | */ |
| 798 | } |
| 799 | |
| 800 | static float PLRElapsedtime = 0.0f; |
| 801 | static float PLRLasttime = 0.0f; |
| 802 | |
| 803 | void OnPLRInit(void) { |
| 804 | PLRElapsedtime = 0.0f; |
| 805 | PLRLasttime = DMFCBase->GetGametime(); |
| 806 | } |
| 807 | |
| 808 | void OnPLRInterval(void) { |
| 809 | level_info *li = DMFCBase->GetLevelInfo(); |
| 810 | |
| 811 | int height = DLLgrfont_GetHeight((DMFCBase->GetGameFontTranslateArray())[BIG_FONT_INDEX]); |
| 812 | DLLgrtext_SetFont((DMFCBase->GetGameFontTranslateArray())[BIG_FONT_INDEX]); |
| 813 | |
| 814 | int y = 240; |
| 815 | |
| 816 | float ft, newtime; |
| 817 | newtime = DMFCBase->GetGametime(); |
| 818 | ft = newtime - PLRLasttime; |
| 819 | PLRLasttime = newtime; |
| 820 | PLRElapsedtime += ft; |
| 821 | |
| 822 | float alpha; |
| 823 | |
| 824 | if (PLRElapsedtime > 5.0f) { |
| 825 | alpha = 1.0f; |
| 826 | } else { |
| 827 | alpha = (5.0f - PLRElapsedtime) / 5.0f; |
| 828 | } |
| 829 | |
| 830 | DLLrend_ClearScreen(GR_BLACK); |
| 831 | DLLgrtext_SetColor(GR_RGB(40, 40, 255)); |
| 832 | DLLgrtext_CenteredPrintf(0, y - height - 1, TXT_LEVELCOMPLETE); |
| 833 | |
| 834 | if (li) { |
| 835 | DLLgrtext_SetAlpha((uint8_t)(alpha * 255.0f)); |
| 836 | DLLgrtext_SetColor(GR_RGB(255, 255, 255)); |
| 837 | DLLgrtext_CenteredPrintf(0, y + 1, li->name); |
| 838 | } |
| 839 | |
| 840 | DLLgrtext_Flush(); |
| 841 | } |