Debian Codegraph @

netgames/roboanarchy/roboanarchy.cpp

raw symbols SCIP
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/roboanarchy/roboanarchy.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003/08/26 03:59:03 $
23 * $Author: kevinb $
24 *
25 * <insert description of file here>
26 *
27 * $Log: roboanarchy.cpp,v $
28 * Revision 1.1.1.1 2003/08/26 03:59:03 kevinb
29 * initial 1.5 import
30 *
31 *
32 * 43 10/21/99 3:20p Kevin
33 * Macintosh merge
34 *
35 * 42 7/12/99 1:22p Jeff
36 * updated for new netflags
37 *
38 * 41 7/11/99 6:45p Jeff
39 * cleaner plr if the list is too long
40 *
41 * 40 5/23/99 3:04a Jason
42 * fixed bug with player rankings not being updated correctly
43 *
44 * 39 5/12/99 11:28a Jeff
45 * added sourcesafe comment block
46 *
47 * $NoKeywords: $
48 */
49
50 #include <string.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include "idmfc.h"
54 #include "roboAnarchy.h"
55 #include "roboanarchystr.h"
56
57 #include <algorithm>
58
59 IDMFC *DMFCBase = NULL;
60 static IDmfcStats *dstat = NULL;
61
62 static int SortedPlayers[MAX_PLAYER_RECORDS]; // sorted player nums
63 static bool DisplayScoreScreen;
64 static int Highlight_bmp = -1;
65
66 #define AHD_NONE 0
67 #define AHD_SCORE 1
68 #define AHD_EFFICIENCY 2
69 #define ACM_PLAYERCOLOR 0
70 #define ACM_NORMAL 1
71 static uint8_t Anarchy_hud_display = AHD_SCORE;
72 static uint8_t HUD_color_model = ACM_PLAYERCOLOR;
73 static bool display_my_welcome = false;
74
75 static void DisplayHUDScores(struct tHUDItem *hitem);
76 static void DisplayScores(void);
77 static void DisplayWelcomeMessage(int player_num);
78 static void SaveStatsToFile(char *filename);
79 static void SwitchHUDColor(int i);
80 static void SwitchAnarchyScores(int i);
81
82 ///////////////////////////////////////////////
83 // localization info
84 static char **StringTable;
85 static int StringTableSize = 0;
86 static const char *_ErrorString = "Missing String";
87 const char *GetString(int d) {
88 if ((d < 0) || (d >= StringTableSize))
89 return _ErrorString;
90 else
91 return StringTable[d];
92 }
93 ///////////////////////////////////////////////
94
95 void SwitchAnarchyScores(int i) {
96 if (i < 0)
97 i = 0;
98 if (i > 2)
99 i = 2;
100
101 Anarchy_hud_display = i;
102
103 switch (i) {
104 case AHD_NONE:
105 DLLAddHUDMessage(TXT_HUDD_NONE);
106 break;
107 case AHD_SCORE:
108 DLLAddHUDMessage(TXT_HUDD_SCORES);
109 break;
110 case AHD_EFFICIENCY:
111 DLLAddHUDMessage(TXT_HUDD_EFFIC);
112 break;
113 };
114 }
115
116 // This function gets called by the game when it wants to learn some info about the game
117 void DLLFUNCCALL DLLGetGameInfo(tDLLOptions *options) {
118 options->flags = DOF_MAXTEAMS;
119 options->max_teams = 1;
120 strcpy(options->game_name, "Anarchy");
121 strcpy(options->requirements, "COOP");
122 }
123
124 // Initializes the game function pointers
125 void DLLFUNCCALL DLLGameInit(int *api_func, uint8_t *all_ok, int num_teams_to_use) {
126 *all_ok = 1;
127 DMFCBase = CreateDMFC();
128 if (!DMFCBase) {
129 *all_ok = 0;
130 return;
131 }
132
133 dstat = CreateDmfcStats();
134 if (!dstat) {
135 *all_ok = 0;
136 return;
137 }
138
139 DMFCBase->LoadFunctions(api_func);
140
141 // Setup the event handlers that we handle
142 DMFCBase->Set_OnKeypress(OnKeypress);
143 DMFCBase->Set_OnHUDInterval(OnHUDInterval);
144 DMFCBase->Set_OnInterval(OnInterval);
145 DMFCBase->Set_OnClientPlayerKilled(OnClientPlayerKilled);
146 DMFCBase->Set_OnClientPlayerEntersGame(OnClientPlayerEntersGame);
147 DMFCBase->Set_OnClientLevelStart(OnClientLevelStart);
148 DMFCBase->Set_OnClientLevelEnd(OnClientLevelEnd);
149 DMFCBase->Set_OnServerGameCreated(OnServerGameCreated);
150 DMFCBase->Set_OnPLRInterval(OnPLRInterval);
151 DMFCBase->Set_OnPLRInit(OnPLRInit);
152 DMFCBase->Set_OnSaveStatsToFile(OnSaveStatsToFile);
153 DMFCBase->Set_OnLevelEndSaveStatsToFile(OnLevelEndSaveStatsToFile);
154 DMFCBase->Set_OnDisconnectSaveStatsToFile(OnDisconnectSaveStatsToFile);
155 DMFCBase->Set_OnPrintScores(OnPrintScores);
156
157 DLLCreateStringTable("Anarchy.str", &StringTable, &StringTableSize);
158 mprintf(0, "%d strings loaded from string table\n", StringTableSize);
159 if (!StringTableSize) {
160 *all_ok = 0;
161 return;
162 }
163
164 AnarchyGameInit(1);
165
166 // add the death and suicide messages
167 DMFCBase->AddDeathMessage(TXT_DEATH1, true);
168 DMFCBase->AddDeathMessage(TXT_DEATH2, true);
169 DMFCBase->AddDeathMessage(TXT_DEATH3, false);
170 DMFCBase->AddDeathMessage(TXT_DEATH4, false);
171 DMFCBase->AddDeathMessage(TXT_DEATH5, true);
172 DMFCBase->AddDeathMessage(TXT_DEATH6, true);
173 DMFCBase->AddDeathMessage(TXT_DEATH7, false);
174 DMFCBase->AddDeathMessage(TXT_DEATH8, true);
175 DMFCBase->AddDeathMessage(TXT_DEATH9, true);
176 DMFCBase->AddDeathMessage(TXT_DEATH10, true);
177
178 DMFCBase->AddSuicideMessage(TXT_SUICIDE1);
179 DMFCBase->AddSuicideMessage(TXT_SUICIDE2);
180 DMFCBase->AddSuicideMessage(TXT_SUICIDE3);
181 DMFCBase->AddSuicideMessage(TXT_SUICIDE4);
182 DMFCBase->AddSuicideMessage(TXT_SUICIDE5);
183 DMFCBase->AddSuicideMessage(TXT_SUICIDE6);
184
185 DMFCBase->SetNumberOfTeams(1);
186
187 netgame_info *Netgame = DMFCBase->GetNetgameInfo();
188 Netgame->flags |= (NF_DAMAGE_FRIENDLY | NF_USE_ROBOTS | NF_TRACK_RANK);
189
190 Highlight_bmp = DLLbm_AllocBitmap(32, 32, 0);
191 if (Highlight_bmp > BAD_BITMAP_HANDLE) {
192 uint16_t *data = DLLbm_data(Highlight_bmp, 0);
193 if (!data) {
194 // bail on out of here
195 *all_ok = 0;
196 return;
197 }
198 for (int x = 0; x < 32 * 32; x++) {
199 data[x] = GR_RGB16(50, 50, 50) | OPAQUE_FLAG;
200 }
201 }
202
203 DMFCBase->AddHUDItemCallback(HI_TEXT, DisplayHUDScores);
204
205 DisplayScoreScreen = false;
206 }
207 // Called when the DLL is shutdown
208 void DLLFUNCCALL DLLGameClose() {
209 if (Highlight_bmp > BAD_BITMAP_HANDLE)
210 DLLbm_FreeBitmap(Highlight_bmp);
211
212 DLLDestroyStringTable(StringTable, StringTableSize);
213
214 if (dstat) {
215 dstat->DestroyPointer();
216 dstat = NULL;
217 }
218
219 if (DMFCBase) {
220 AnarchyGameClose();
221 DMFCBase->GameClose();
222 DMFCBase->DestroyPointer();
223 DMFCBase = NULL;
224 }
225 }
226
227 void DetermineScore(int precord_num, int column_num, char *buffer, int buffer_size) {
228 player_record *pr = DMFCBase->GetPlayerRecord(precord_num);
229 if (!pr || pr->state == STATE_EMPTY) {
230 buffer[0] = '\0';
231 return;
232 }
233
234 snprintf(buffer, buffer_size, "%d", pr->dstats.kills[DSTAT_LEVEL] - pr->dstats.suicides[DSTAT_LEVEL]);
235 }
236
237 void AnarchyGameClose(void) {}
238
239 // DMFCApp::GameInit
240 //
241 // Sets up all the DLL functions and pointers and preps the class for use. This ABSOLUTLY must be
242 // called, so if you override DMFCApp::GameInit, make sure that you put a call to this somewhere in
243 // the override.
244 void AnarchyGameInit(int teams) {
245 // add the anarchy menu/submenus
246 IMenuItem *lev1, *lev2;
247
248 lev1 = CreateMenuItemWArgs("Anarchy", MIT_NORMAL, 0, NULL);
249
250 lev2 = CreateMenuItemWArgs(TXT_MNU_HUDSTYLE, MIT_STATE, 0, SwitchAnarchyScores);
251 lev2->SetStateItemList(3, TXT_NONE, TXT_SCOREHD, TXT_EFFICIENCY);
252 lev2->SetState(1);
253 lev1->AddSubMenu(lev2);
254
255 lev2 = CreateMenuItemWArgs(TXT_MNU_HUDCOLOR, MIT_STATE, 0, SwitchHUDColor);
256 lev2->SetStateItemList(2, TXT_PLAYERCOLORS, TXT_NORMAL);
257 lev2->SetState(HUD_color_model);
258 lev1->AddSubMenu(lev2);
259
260 lev2 = DMFCBase->GetOnScreenMenu();
261 lev2->AddSubMenu(lev1);
262
263 DMFCBase->GameInit(teams);
264
265 // Initialize the Stats Manager
266 // ----------------------------
267
268 tDmfcStatsInit tsi;
269 tDmfcStatsColumnInfo pl_col[6];
270 char gname[20];
271 strcpy(gname, "Anarchy");
272
273 tsi.flags = DSIF_SHOW_PIC | DSIF_SHOW_OBSERVERICON;
274 tsi.cColumnCountDetailed = 0;
275 tsi.cColumnCountPlayerList = 6;
276 tsi.clbDetailedColumn = NULL;
277 tsi.clbDetailedColumnBMP = NULL;
278 tsi.clbPlayerColumn = DetermineScore;
279 tsi.clbPlayerColumnBMP = NULL;
280 tsi.DetailedColumns = NULL;
281 tsi.GameName = gname;
282 tsi.MaxNumberDisplayed = NULL;
283 tsi.PlayerListColumns = pl_col;
284 tsi.SortedPlayerRecords = SortedPlayers;
285
286 pl_col[0].color_type = DSCOLOR_SHIPCOLOR;
287 strcpy(pl_col[0].title, TXT_PILOT);
288 pl_col[0].type = DSCOL_PILOT_NAME;
289 pl_col[0].width = 120;
290
291 pl_col[1].color_type = DSCOLOR_SHIPCOLOR;
292 strcpy(pl_col[1].title, TXT_SCORE);
293 pl_col[1].type = DSCOL_CUSTOM;
294 pl_col[1].width = 50;
295
296 pl_col[2].color_type = DSCOLOR_SHIPCOLOR;
297 strcpy(pl_col[2].title, TXT_KILLS_SHORT);
298 pl_col[2].type = DSCOL_KILLS_LEVEL;
299 pl_col[2].width = 50;
300
301 pl_col[3].color_type = DSCOLOR_SHIPCOLOR;
302 strcpy(pl_col[3].title, TXT_DEATHS_SHORT);
303 pl_col[3].type = DSCOL_DEATHS_LEVEL;
304 pl_col[3].width = 60;
305
306 pl_col[4].color_type = DSCOLOR_SHIPCOLOR;
307 strcpy(pl_col[4].title, TXT_SUICIDES_SHORT);
308 pl_col[4].type = DSCOL_SUICIDES_LEVEL;
309 pl_col[4].width = 65;
310
311 pl_col[5].color_type = DSCOLOR_SHIPCOLOR;
312 strcpy(pl_col[5].title, TXT_PING);
313 pl_col[5].type = DSCOL_PING;
314 pl_col[5].width = 40;
315
316 dstat->Initialize(&tsi);
317 }
318
319 void OnHUDInterval(void) {
320 dstat->DoFrame();
321 DMFCBase->DisplayOutrageLogo();
322 DMFCBase->OnHUDInterval();
323 }
324
325 void OnInterval(void) {
326 DMFCBase->GetSortedPlayerSlots(SortedPlayers, MAX_PLAYER_RECORDS);
327 DMFCBase->OnInterval();
328 }
329
330 void OnKeypress(int key) {
331 dllinfo *Data = DMFCBase->GetDLLInfoCallData();
332
333 switch (key) {
334 case K_F7:
335 DisplayScoreScreen = !DisplayScoreScreen;
336 DMFCBase->EnableOnScreenMenu(false);
337 dstat->Enable(DisplayScoreScreen);
338 break;
339 case K_PAGEDOWN:
340 if (DisplayScoreScreen) {
341 dstat->ScrollDown();
342 Data->iRet = 1;
343 }
344 break;
345 case K_PAGEUP:
346 if (DisplayScoreScreen) {
347 dstat->ScrollUp();
348 Data->iRet = 1;
349 }
350 break;
351 case K_F6:
352 DisplayScoreScreen = false;
353 dstat->Enable(false);
354 break;
355 case K_ESC:
356 if (DisplayScoreScreen) {
357 dstat->Enable(false);
358 DisplayScoreScreen = false;
359 Data->iRet = 1;
360 }
361 break;
362 }
363
364 DMFCBase->OnKeypress(key);
365 }
366
367 // The server has just started, so clear out all the stats and game info
368 void OnServerGameCreated(void) { DMFCBase->OnServerGameCreated(); }
369
370 // The server has started a new level, so clear out any scores needed to be reset
371 void OnClientLevelStart(void) {
372 for (int i = 0; i < MAX_PLAYER_RECORDS; i++) {
373 SortedPlayers[i] = -1;
374 }
375
376 DMFCBase->OnClientLevelStart();
377 }
378
379 void OnClientLevelEnd(void) { DMFCBase->OnClientLevelEnd(); }
380
381 // A new player has entered the game, zero their stats out
382 void OnClientPlayerEntersGame(int player_num) {
383 DMFCBase->OnClientPlayerEntersGame(player_num);
384
385 if (player_num != DMFCBase->GetPlayerNum())
386 DisplayWelcomeMessage(player_num);
387 else
388 display_my_welcome = true;
389 }
390
391 // We need to adjust the scores
392 void OnClientPlayerKilled(object *killer_obj, int victim_pnum) {
393 DMFCBase->OnClientPlayerKilled(killer_obj, victim_pnum);
394 player_record *kpr;
395
396 int kpnum;
397
398 if (killer_obj) {
399 if ((killer_obj->type == OBJ_PLAYER) || (killer_obj->type == OBJ_GHOST))
400 kpnum = killer_obj->id;
401 else if (killer_obj->type == OBJ_ROBOT || killer_obj->type == OBJ_BUILDING) {
402 // countermeasure kill
403 kpnum = DMFCBase->GetCounterMeasureOwner(killer_obj);
404 } else {
405 kpnum = -1;
406 }
407 } else {
408 kpnum = -1;
409 }
410
411 kpr = DMFCBase->GetPlayerRecordByPnum(kpnum);
412 if (kpr) {
413 int goal;
414 if (DMFCBase->GetScoreLimit(&goal)) {
415 int score = kpr->dstats.kills[DSTAT_LEVEL] - kpr->dstats.suicides[DSTAT_LEVEL];
416 if (score >= goal) {
417 DMFCBase->EndLevel();
418 }
419 }
420 }
421 }
422
423 bool compare_slots(int a, int b) {
424 int ascore, bscore;
425 player_record *apr, *bpr;
426 apr = DMFCBase->GetPlayerRecord(a);
427 bpr = DMFCBase->GetPlayerRecord(b);
428 if (!apr)
429 return true;
430 if (!bpr)
431 return false;
432 if (apr->state == STATE_EMPTY)
433 return true;
434 if (bpr->state == STATE_EMPTY)
435 return false;
436 if ((apr->state == STATE_INGAME) && (bpr->state == STATE_INGAME)) {
437 // both players were in the game
438 ascore = apr->dstats.kills[DSTAT_LEVEL] - apr->dstats.suicides[DSTAT_LEVEL];
439 bscore = bpr->dstats.kills[DSTAT_LEVEL] - bpr->dstats.suicides[DSTAT_LEVEL];
440 return (ascore < bscore);
441 }
442 if ((apr->state == STATE_INGAME) && (bpr->state == STATE_DISCONNECTED)) {
443 // apr gets priority since he was in the game on exit
444 return false;
445 }
446 if ((apr->state == STATE_DISCONNECTED) && (bpr->state == STATE_INGAME)) {
447 // bpr gets priority since he was in the game on exit
448 return true;
449 }
450 // if we got here then both players were disconnected
451 ascore = apr->dstats.kills[DSTAT_LEVEL] - apr->dstats.suicides[DSTAT_LEVEL];
452 bscore = bpr->dstats.kills[DSTAT_LEVEL] - bpr->dstats.suicides[DSTAT_LEVEL];
453 return (ascore < bscore);
454 }
455
456 void OnPLRInit(void) {
457 int tempsort[MAX_PLAYER_RECORDS];
458 int i, t, j;
459
460 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
461 tempsort[i] = i;
462 }
463
464 for (i = 1; i <= MAX_PLAYER_RECORDS - 1; i++) {
465 t = tempsort[i];
466 // Shift elements down until
467 // insertion point found.
468 for (j = i - 1; j >= 0 && compare_slots(tempsort[j], t); j--) {
469 tempsort[j + 1] = tempsort[j];
470 }
471 // insert
472 tempsort[j + 1] = t;
473 }
474
475 // copy the array over
476 memcpy(SortedPlayers, tempsort, DLLMAX_PLAYERS * sizeof(int));
477
478 DMFCBase->OnPLRInit();
479 }
480
481 void OnPLRInterval(void) {
482 #define PLAYERS_COL 130
483 #define SCORE_COL 280
484 #define DEATHS_COL 330
485 #define SUICIDES_COL 390
486 #define TOTAL_COL 460
487
488 DMFCBase->OnPLRInterval();
489
490 int y = 40;
491
492 int height = DLLgrfont_GetHeight((DMFCBase->GetGameFontTranslateArray())[SMALL_UI_FONT_INDEX]) + 1;
493 DLLgrtext_SetFont((DMFCBase->GetGameFontTranslateArray())[SMALL_UI_FONT_INDEX]);
494
495 // print out header
496 DLLgrtext_SetColor(GR_RGB(255, 255, 150));
497 DLLgrtext_Printf(PLAYERS_COL, y, TXT_PILOT);
498 DLLgrtext_Printf(SCORE_COL, y, TXT_KILLS_SHORT);
499 DLLgrtext_Printf(DEATHS_COL, y, TXT_DEATHS_SHORT);
500 DLLgrtext_Printf(SUICIDES_COL, y, TXT_SUICIDES_SHORT);
501 DLLgrtext_Printf(TOTAL_COL, y, TXT_SCORE);
502 y += height;
503
504 // print out player stats
505 int rank = 1;
506 int slot, pnum;
507 player_record *pr;
508
509 for (int i = 0; i < DLLMAX_PLAYERS; i++) {
510 slot = SortedPlayers[i];
511 pr = DMFCBase->GetPlayerRecord(slot);
512 if ((pr) && (pr->state != STATE_EMPTY)) {
513
514 if (DMFCBase->IsPlayerDedicatedServer(pr))
515 continue; // skip a dedicated server
516
517 pnum = DMFCBase->WasPlayerInGameAtLevelEnd(slot);
518 if (pnum != -1) {
519 DLLgrtext_SetColor((DMFCBase->GetPlayerColors())[pnum]);
520 } else {
521 DLLgrtext_SetColor(GR_RGB(128, 128, 128));
522 }
523
524 char temp[100];
525 snprintf(temp, sizeof(temp), "%d)%s", rank, pr->callsign);
526 DMFCBase->ClipString(SCORE_COL - PLAYERS_COL - 10, temp, true);
527 DLLgrtext_Printf(PLAYERS_COL, y, "%s", temp);
528
529 DLLgrtext_Printf(SCORE_COL, y, "%d", pr->dstats.kills[DSTAT_LEVEL]);
530 DLLgrtext_Printf(DEATHS_COL, y, "%d", pr->dstats.deaths[DSTAT_LEVEL]);
531 DLLgrtext_Printf(SUICIDES_COL, y, "%d", pr->dstats.suicides[DSTAT_LEVEL]);
532 DLLgrtext_Printf(TOTAL_COL, y, "%d", pr->dstats.kills[DSTAT_LEVEL] - pr->dstats.suicides[DSTAT_LEVEL]);
533 y += height;
534 rank++;
535
536 if (y >= 440)
537 goto quick_exit;
538 }
539 }
540
541 quick_exit:;
542 }
543
544 void SaveStatsToFile(char *filename) {
545 CFILE *file;
546 DLLOpenCFILE(&file, filename, "wt");
547 if (!file) {
548 mprintf(0, "Unable to open output file\n");
549 return;
550 }
551
552 // write out game stats
553 #define BUFSIZE 150
554 char buffer[BUFSIZE];
555 char tempbuffer[25];
556 int sortedslots[MAX_PLAYER_RECORDS];
557 player_record *pr, *dpr;
558 tPInfoStat stat;
559 int count, p;
560
561 // sort the stats
562 DMFCBase->GetSortedPlayerSlots(sortedslots, MAX_PLAYER_RECORDS);
563 count = 1;
564
565 snprintf(buffer, sizeof(buffer), TXT_SAVE_HEADER, (DMFCBase->GetNetgameInfo())->name,
566 (DMFCBase->GetCurrentMission())->cur_level);
567 DLLcf_WriteString(file, buffer);
568
569 snprintf(buffer, sizeof(buffer), "%s", TXT_SAVE_HEADERB);
570 DLLcf_WriteString(file, buffer);
571 strcpy(buffer, "-----------------------------------------------------------------------------------------------");
572 DLLcf_WriteString(file, buffer);
573
574 for (int s = 0; s < MAX_PLAYER_RECORDS; s++) {
575 p = sortedslots[s];
576 pr = DMFCBase->GetPlayerRecord(p);
577 if (pr && pr->state != STATE_EMPTY) {
578
579 if (DMFCBase->IsPlayerDedicatedServer(pr))
580 continue; // skip dedicated server
581
582 memset(buffer, ' ', BUFSIZE);
583
584 snprintf(tempbuffer, sizeof(tempbuffer), "%d)", count);
585 memcpy(&buffer[0], tempbuffer, strlen(tempbuffer));
586
587 snprintf(tempbuffer, sizeof(tempbuffer), "%s%s", (pr->state == STATE_INGAME) ? "" : "*", pr->callsign);
588 memcpy(&buffer[7], tempbuffer, strlen(tempbuffer));
589
590 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]",
591 pr->dstats.kills[DSTAT_LEVEL] - pr->dstats.suicides[DSTAT_LEVEL],
592 pr->dstats.kills[DSTAT_OVERALL] - pr->dstats.suicides[DSTAT_OVERALL]);
593 memcpy(&buffer[36], tempbuffer, strlen(tempbuffer));
594
595 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", pr->dstats.kills[DSTAT_LEVEL],
596 pr->dstats.kills[DSTAT_OVERALL]);
597 memcpy(&buffer[48], tempbuffer, strlen(tempbuffer));
598
599 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", pr->dstats.deaths[DSTAT_LEVEL],
600 pr->dstats.deaths[DSTAT_OVERALL]);
601 memcpy(&buffer[60], tempbuffer, strlen(tempbuffer));
602
603 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", pr->dstats.suicides[DSTAT_LEVEL],
604 pr->dstats.suicides[DSTAT_OVERALL]);
605 memcpy(&buffer[71], tempbuffer, strlen(tempbuffer));
606
607 strcpy(tempbuffer, DMFCBase->GetTimeString(DMFCBase->GetTimeInGame(p)));
608 memcpy(&buffer[82], tempbuffer, strlen(tempbuffer));
609
610 size_t pos = 82 + strlen(tempbuffer) + 1;
611 if (pos < BUFSIZE)
612 buffer[pos] = '\0';
613
614 buffer[BUFSIZE - 1] = '\0';
615 DLLcf_WriteString(file, buffer);
616 count++;
617 }
618 }
619
620 DLLcf_WriteString(file, TXT_SAVE_HEADERC);
621
622 count = 1;
623 for (p = 0; p < MAX_PLAYER_RECORDS; p++) {
624 pr = DMFCBase->GetPlayerRecord(p);
625 if (pr && pr->state != STATE_EMPTY) {
626
627 if (DMFCBase->IsPlayerDedicatedServer(pr))
628 continue; // skip dedicated server
629
630 // Write out header
631 snprintf(buffer, sizeof(buffer), "%d) %s%s", count, (pr->state == STATE_INGAME) ? "" : "*", pr->callsign);
632 DLLcf_WriteString(file, buffer);
633 size_t length = strlen(buffer);
634 memset(buffer, '=', length);
635 buffer[length] = '\0';
636 DLLcf_WriteString(file, buffer);
637
638 // time in game
639 snprintf(buffer, sizeof(buffer), TXT_SAVE_TIMEINGAME, DMFCBase->GetTimeString(DMFCBase->GetTimeInGame(p)));
640 DLLcf_WriteString(file, buffer);
641
642 if (DMFCBase->FindPInfoStatFirst(p, &stat)) {
643 snprintf(buffer, sizeof(buffer), "%s", TXT_SAVE_KILLERLIST);
644 DLLcf_WriteString(file, buffer);
645
646 if (stat.slot != p) {
647 memset(buffer, ' ', BUFSIZE);
648 dpr = DMFCBase->GetPlayerRecord(stat.slot);
649
650 ASSERT(dpr != NULL);
651 if (dpr) {
652 strcpy(tempbuffer, dpr->callsign);
653 memcpy(buffer, tempbuffer, strlen(tempbuffer));
654
655 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.kills);
656 memcpy(&buffer[30], tempbuffer, strlen(tempbuffer));
657
658 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.deaths);
659 memcpy(&buffer[40], tempbuffer, strlen(tempbuffer));
660
661 size_t pos = 40 + strlen(tempbuffer) + 1;
662 if (pos < BUFSIZE)
663 buffer[pos] = '\0';
664
665 buffer[BUFSIZE - 1] = '\0';
666 DLLcf_WriteString(file, buffer);
667 }
668 }
669
670 while (DMFCBase->FindPInfoStatNext(&stat)) {
671 if (stat.slot != p) {
672 memset(buffer, ' ', BUFSIZE);
673 dpr = DMFCBase->GetPlayerRecord(stat.slot);
674
675 if (dpr) {
676 strcpy(tempbuffer, dpr->callsign);
677 memcpy(buffer, tempbuffer, strlen(tempbuffer));
678
679 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.kills);
680 memcpy(&buffer[30], tempbuffer, strlen(tempbuffer));
681
682 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.deaths);
683 memcpy(&buffer[40], tempbuffer, strlen(tempbuffer));
684
685 size_t pos = 40 + strlen(tempbuffer) + 1;
686 if (pos < BUFSIZE)
687 buffer[pos] = '\0';
688
689 buffer[BUFSIZE - 1] = '\0';
690 DLLcf_WriteString(file, buffer);
691 }
692 }
693 }
694 }
695 DMFCBase->FindPInfoStatClose();
696 DLLcf_WriteString(file, ""); // skip a line
697 count++;
698 }
699 }
700
701 // done writing stats
702 DLLcfclose(file);
703 DLLAddHUDMessage(TXT_MSG_SAVESTATS);
704 }
705
706 #define ROOTFILENAME "Anarchy"
707 void OnSaveStatsToFile(void) {
708 char filename[256];
709 DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, false);
710 SaveStatsToFile(filename);
711 }
712
713 void OnLevelEndSaveStatsToFile(void) {
714 char filename[256];
715 DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, true);
716 SaveStatsToFile(filename);
717 }
718
719 void OnDisconnectSaveStatsToFile(void) {
720 char filename[256];
721 DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, false);
722 SaveStatsToFile(filename);
723 }
724
725 void OnPrintScores(int level) {
726 char buffer[256];
727 char name[50];
728 memset(buffer, ' ', 256);
729
730 size_t t;
731 size_t pos[6];
732 size_t len[6];
733 pos[0] = 0;
734 t = len[0] = 20; // give ample room for pilot name
735 pos[1] = pos[0] + t + 1;
736 t = len[1] = strlen(TXT_POINTS);
737 pos[2] = pos[1] + t + 1;
738 t = len[2] = strlen(TXT_KILLS_SHORT);
739 pos[3] = pos[2] + t + 1;
740 t = len[3] = strlen(TXT_DEATHS_SHORT);
741 pos[4] = pos[3] + t + 1;
742 t = len[4] = strlen(TXT_SUICIDES_SHORT);
743 pos[5] = pos[4] + t + 1;
744 t = len[5] = strlen(TXT_PING);
745
746 memcpy(&buffer[pos[0]], TXT_PILOT, strlen(TXT_PILOT));
747 memcpy(&buffer[pos[1]], TXT_POINTS, len[1]);
748 memcpy(&buffer[pos[2]], TXT_KILLS_SHORT, len[2]);
749 memcpy(&buffer[pos[3]], TXT_DEATHS_SHORT, len[3]);
750 memcpy(&buffer[pos[4]], TXT_SUICIDES_SHORT, len[4]);
751 memcpy(&buffer[pos[5]], TXT_PING, len[5]);
752 buffer[pos[5] + len[5] + 1] = '\n';
753 buffer[pos[5] + len[5] + 2] = '\0';
754 DPrintf(buffer);
755
756 int slot;
757 player_record *pr;
758 int pcount;
759
760 if (level < 0 || level >= MAX_PLAYER_RECORDS)
761 pcount = MAX_PLAYER_RECORDS;
762 else
763 pcount = level;
764
765 int sortedplayers[MAX_PLAYER_RECORDS];
766 DMFCBase->GetSortedPlayerSlots(sortedplayers, MAX_PLAYER_RECORDS);
767
768 for (int i = 0; i < pcount; i++) {
769 slot = sortedplayers[i];
770 pr = DMFCBase->GetPlayerRecord(slot);
771 if ((pr) && (pr->state != STATE_EMPTY)) {
772
773 if (DMFCBase->IsPlayerDedicatedServer(pr))
774 continue; // skip dedicated server
775
776 snprintf(name, sizeof(name), "%s%s:", (pr->state == STATE_DISCONNECTED) ? "*" : "", pr->callsign);
777 name[19] = '\0';
778
779 memset(buffer, ' ', 256);
780 t = strlen(name);
781 memcpy(&buffer[pos[0]], name, (t < len[0]) ? t : len[0]);
782 snprintf(name, sizeof(name), "%d", pr->dstats.kills[DSTAT_LEVEL] - pr->dstats.suicides[DSTAT_LEVEL]);
783 t = strlen(name);
784 memcpy(&buffer[pos[1]], name, (t < len[1]) ? t : len[1]);
785 snprintf(name, sizeof(name), "%d", pr->dstats.kills[DSTAT_LEVEL]);
786 t = strlen(name);
787 memcpy(&buffer[pos[2]], name, (t < len[2]) ? t : len[2]);
788 snprintf(name, sizeof(name), "%d", pr->dstats.deaths[DSTAT_LEVEL]);
789 t = strlen(name);
790 memcpy(&buffer[pos[3]], name, (t < len[3]) ? t : len[3]);
791 snprintf(name, sizeof(name), "%d", pr->dstats.suicides[DSTAT_LEVEL]);
792 t = strlen(name);
793 memcpy(&buffer[pos[4]], name, (t < len[4]) ? t : len[4]);
794
795 if (pr->state == STATE_INGAME)
796 snprintf(name, sizeof(name), "%.0f", (DMFCBase->GetNetPlayers())[pr->pnum].ping_time * 1000.0f);
797 else
798 strcpy(name, "---");
799 t = strlen(name);
800 memcpy(&buffer[pos[5]], name, (t < len[5]) ? t : len[5]);
801 buffer[pos[5] + len[5] + 1] = '\n';
802 buffer[pos[5] + len[5] + 2] = '\0';
803 DPrintf(buffer);
804 }
805 }
806 }
807
808 void DisplayHUDScores(struct tHUDItem *hitem) {
809 if (display_my_welcome) {
810 DisplayWelcomeMessage(DMFCBase->GetPlayerNum());
811 display_my_welcome = false;
812 }
813
814 if (DisplayScoreScreen)
815 return;
816
817 int height = DLLgrfont_GetHeight((DMFCBase->GetGameFontTranslateArray())[HUD_FONT_INDEX]) + 3;
818 uint8_t alpha = DMFCBase->ConvertHUDAlpha((uint8_t)((DisplayScoreScreen) ? 128 : 255));
819 int y = (DMFCBase->GetGameWindowH() / 2) - ((height * 5) / 2);
820 int x = 520;
821 ddgr_color color;
822
823 int rank = 1;
824 player_record *pr;
825
826 // Display your Kills & Deaths on the top corners of the screen
827 pr = DMFCBase->GetPlayerRecordByPnum(DMFCBase->GetPlayerNum());
828 if (pr) {
829 int y = 25, x;
830 int lwidth;
831 char buffer[20];
832
833 int w_kill, w_death, max_w;
834 w_kill = DLLgrtext_GetTextLineWidth(TXT_KILLS);
835 w_death = DLLgrtext_GetTextLineWidth(TXT_DEATHS);
836 max_w = std::max(w_kill, w_death);
837
838 x = DMFCBase->GetGameWindowW() - DMFCBase->GetGameWindowW() * 0.0078125f;
839 DLLgrtext_SetColor(GR_GREEN);
840 DLLgrtext_SetAlpha(alpha);
841 DLLgrtext_Printf(x - (max_w / 2) - (w_kill / 2), y, TXT_KILLS);
842 y += height;
843
844 snprintf(buffer, sizeof(buffer), "%d", pr->dstats.kills[DSTAT_LEVEL]);
845 lwidth = DLLgrtext_GetTextLineWidth(buffer);
846 DLLgrtext_SetColor(GR_GREEN);
847 DLLgrtext_SetAlpha(alpha);
848 DLLgrtext_Printf(x - (max_w / 2) - (lwidth / 2), y, buffer);
849 y += height + 3;
850
851 DLLgrtext_SetColor(GR_GREEN);
852 DLLgrtext_SetAlpha(alpha);
853 DLLgrtext_Printf(x - (max_w / 2) - (w_death / 2), y, TXT_DEATHS);
854 y += height;
855
856 snprintf(buffer, sizeof(buffer), "%d", pr->dstats.deaths[DSTAT_LEVEL]);
857 lwidth = DLLgrtext_GetTextLineWidth(buffer);
858 DLLgrtext_SetColor(GR_GREEN);
859 DLLgrtext_SetAlpha(alpha);
860 DLLgrtext_Printf(x - (max_w / 2) - (lwidth / 2), y, buffer);
861 }
862
863 int ESortedPlayers[DLLMAX_PLAYERS] = {};
864
865 switch (Anarchy_hud_display) {
866 case AHD_NONE:
867 return;
868 break;
869 case AHD_EFFICIENCY:
870 DMFCBase->GetSortedPlayerSlots(ESortedPlayers, DLLMAX_PLAYERS);
871 break;
872 }
873
874 char name[30];
875
876 // determine coordinates to use here
877 // we'll use a virtual width of 85 pixels on a 640x480 screen
878 // so first determine the new width
879 int name_width = 85.0f * DMFCBase->GetHudAspectX();
880 int score_width = DLLgrtext_GetTextLineWidth("888");
881 int name_x = DMFCBase->GetGameWindowW() - name_width - score_width - 10;
882 int score_x = DMFCBase->GetGameWindowW() - score_width - 5;
883
884 for (int i = 0; i < DLLMAX_PLAYERS; i++) {
885 int slot;
886
887 if (Anarchy_hud_display == AHD_EFFICIENCY)
888 slot = ESortedPlayers[i];
889 else
890 slot = SortedPlayers[i];
891
892 pr = DMFCBase->GetPlayerRecord(slot);
893
894 if ((pr) && (pr->state != STATE_EMPTY)) {
895
896 if (DMFCBase->IsPlayerDedicatedServer(pr))
897 continue; // skip dedicated server
898
899 if ((pr->state == STATE_DISCONNECTED) || (pr->state == STATE_INGAME && !DMFCBase->IsPlayerObserver(pr->pnum))) {
900
901 if (pr->pnum == DMFCBase->GetPlayerNum()) {
902
903 switch (HUD_color_model) {
904 case ACM_PLAYERCOLOR:
905 color = (DMFCBase->GetPlayerColors())[pr->pnum];
906 break;
907 case ACM_NORMAL:
908 color = GR_RGB(40, 255, 40);
909 break;
910 };
911
912 if (Highlight_bmp > BAD_BITMAP_HANDLE) {
913 // draw the highlite bar in the background
914 DLLrend_SetAlphaValue(alpha * 0.50f);
915 DLLrend_SetZBufferState(0);
916 DLLrend_SetTextureType(TT_LINEAR);
917 DLLrend_SetLighting(LS_NONE);
918 DLLrend_SetAlphaType(AT_CONSTANT_TEXTURE);
919 DLLrend_DrawScaledBitmap(name_x - 2, y - 2, score_x + score_width + 2, y + height - 1, Highlight_bmp, 0, 0,
920 1, 1, 1.0, -1, NULL);
921 DLLrend_SetZBufferState(1);
922 }
923
924 strcpy(name, pr->callsign);
925 DMFCBase->ClipString(name_width, name, true);
926
927 DLLgrtext_SetAlpha(alpha);
928 DLLgrtext_SetColor(color);
929 DLLgrtext_Printf(name_x, y, "%s", name);
930
931 if (Anarchy_hud_display == AHD_EFFICIENCY) {
932 float t = pr->dstats.kills[DSTAT_LEVEL] + pr->dstats.suicides[DSTAT_LEVEL] + pr->dstats.deaths[DSTAT_LEVEL];
933 float value = (float)(pr->dstats.kills[DSTAT_LEVEL]) / ((t) ? t : 0.0000001f);
934 DLLgrtext_Printf(score_x, y, "%.1f", value);
935 } else {
936 DLLgrtext_Printf(score_x, y, "%d", pr->dstats.kills[DSTAT_LEVEL] - pr->dstats.suicides[DSTAT_LEVEL]);
937 }
938
939 y += height;
940 } else if (rank < 6) {
941
942 if (pr->state == STATE_DISCONNECTED) {
943 color = GR_GREY;
944 } else {
945 switch (HUD_color_model) {
946 case ACM_PLAYERCOLOR:
947 color = (DMFCBase->GetPlayerColors())[pr->pnum];
948 break;
949 case ACM_NORMAL:
950 color = GR_RGB(40, 255, 40);
951 break;
952 };
953 }
954 strcpy(name, pr->callsign);
955 DMFCBase->ClipString(name_width, name, true);
956
957 DLLgrtext_SetAlpha(alpha);
958 DLLgrtext_SetColor(color);
959 DLLgrtext_Printf(name_x, y, "%s", name);
960
961 if (Anarchy_hud_display == AHD_EFFICIENCY) {
962 float t = pr->dstats.kills[DSTAT_LEVEL] + pr->dstats.suicides[DSTAT_LEVEL] + pr->dstats.deaths[DSTAT_LEVEL];
963 float value = (float)(pr->dstats.kills[DSTAT_LEVEL]) / ((t) ? t : 0.0000001f);
964 DLLgrtext_Printf(score_x, y, "%.1f", value);
965 } else {
966 DLLgrtext_Printf(score_x, y, "%d", pr->dstats.kills[DSTAT_LEVEL] - pr->dstats.suicides[DSTAT_LEVEL]);
967 }
968
969 y += height;
970 }
971 rank++;
972 }
973 }
974 }
975 }
976
977 void DisplayWelcomeMessage(int player_num) {
978 char name_buffer[64];
979 strcpy(name_buffer, (DMFCBase->GetPlayers())[player_num].callsign);
980
981 if (player_num == DMFCBase->GetPlayerNum()) {
982 DLLAddHUDMessage(TXT_WELCOME, name_buffer);
983 } else {
984 DLLAddHUDMessage(TXT_JOINED, name_buffer);
985 }
986 }
987
988 void SwitchHUDColor(int i) {
989 if (i < 0 || i > 1)
990 return;
991 HUD_color_model = i;
992
993 switch (HUD_color_model) {
994 case ACM_PLAYERCOLOR:
995 DLLAddHUDMessage(TXT_MSG_COLORPLR);
996 break;
997 case ACM_NORMAL:
998 DLLAddHUDMessage(TXT_MSG_COLORNORM);
999 break;
1000 };
1001 }