Debian Codegraph @

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