Debian Codegraph @

netgames/hyperanarchy/hyperanarchy.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/hyperanarchy/hyperanarchy.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003/08/26 03:57:56 $
23 * $Author: kevinb $
24 *
25 * <insert description of file here>
26 *
27 * $Log: hyperanarchy.cpp,v $
28 * Revision 1.1.1.1 2003/08/26 03:57:56 kevinb
29 * initial 1.5 import
30 *
31 *
32 * 68 10/21/99 9:27p Jeff
33 * B.A. Macintosh code merge
34 *
35 * 67 7/12/99 1:22p Jeff
36 * updated for new netflags
37 *
38 * 66 7/11/99 6:45p Jeff
39 * cleaner plr if the list is too long
40 *
41 * 65 5/23/99 3:04a Jason
42 * fixed bug with player rankings not being updated correctly
43 *
44 * 64 5/20/99 5:03p Jeff
45 * fixed HUD display bugs
46 *
47 * 63 5/12/99 11:04p Jeff
48 * dmfc and multiplayer games now have endian friendly packets (*whew*)
49 *
50 * 62 5/12/99 11:28a Jeff
51 * added sourcesafe comment block
52 *
53 * $NoKeywords: $
54 */
55
56 #include <stdlib.h>
57 #include <string.h>
58 #include "gamedll_header.h"
59 #include "idmfc.h"
60 #include "HyperAnarchy.h"
61 #include "hyperstr.h"
62 IDMFC *DMFCBase = NULL;
63 static IDmfcStats *dstat = NULL;
64 static object *dObjects;
65 static player *dPlayers;
66
67 #define SPID_HYPERINFO 0
68 #define SPID_HYPERPOS 1
69 #define HCM_PLAYERCOLOR 0
70 #define HCM_NORMAL 1
71
72 struct tPlayerStat {
73 int Score[2];
74 };
75
76 static int pack_pstat(tPlayerStat *user_info, uint8_t *data);
77 static int unpack_pstat(tPlayerStat *user_info, uint8_t *data);
78
79 int pack_pstat(tPlayerStat *user_info, uint8_t *data) {
80 int count = 0;
81 MultiAddInt(user_info->Score[0], data, &count);
82 MultiAddInt(user_info->Score[1], data, &count);
83 return count;
84 }
85
86 int unpack_pstat(tPlayerStat *user_info, uint8_t *data) {
87 int count = 0;
88 user_info->Score[0] = MultiGetInt(data, &count);
89 user_info->Score[1] = MultiGetInt(data, &count);
90 return count;
91 }
92
93 static int SortedPlayers[MAX_PLAYER_RECORDS];
94 static bool DisplayScoreScreen;
95 static int HyperOrbID = -1;
96 static int WhoHasOrb = -1;
97 static int KillCount = 0;
98 static int HyperOrbIcon = -1;
99 static bool DisplayFlagBlink = true;
100 static int WhoJustScoredTimer = -1;
101 static int HyperMoveTimer = -1;
102 static uint8_t HUD_color_model = HCM_PLAYERCOLOR;
103 static int Highlight_bmp = -1;
104 static bool display_my_welcome = false;
105
106 static void OnClientPlayerEntersGame(int player_num);
107
108 ///////////////////////////////////////////////
109 // localization info
110 static char **StringTable;
111 static int StringTableSize = 0;
112 static const char *_ErrorString = "Missing String";
113 const char *GetStringFromTable(int d) {
114 if ((d < 0) || (d >= StringTableSize))
115 return _ErrorString;
116 else
117 return StringTable[d];
118 }
119 ///////////////////////////////////////////////
120
121 static void SwitchHUDColor(int i);
122 // handles a Hyper Anarchy Game State packet
123 static void ReceiveHyperGameState(uint8_t *data);
124 // sends a Hyper Anarchy Game State packet
125 static void SendHyperGameState(int playernum);
126 // Displays HUD scores
127 static void DisplayHUDScores(struct tHUDItem *hitem);
128 // Displays the Stats screen
129 static void DisplayScores(void);
130 // Sorts based on score
131 static void SortPlayerScores(int *sortedindex, int size);
132 // Updates the effect on all the players
133 static void UpdateEffect(void);
134 // Displays the Welcome Screen
135 static void DisplayWelcomeScreen(int pnum);
136 // Handler for menu item for turning on/off blinking hud
137 static void SwitchBlinkingHud(int i);
138 // Generates a valid random room
139 static int GetRandomValidRoom(void);
140 // Moves the HyperOrb to the center of a random room
141 // if objnum is -1, than the HyperOrb is created and moved
142 static void MoveHyperOrbToRoom(int objnum);
143 // Given a room the HyperOrb is created and placed in the center of it
144 static void CreateHyperOrbInRoom(int room);
145 // Given the objnum and room it will move the HyperOrb to the center of that room. Objnum better be valid.
146 static void MoveHyperOrbToRoomCenter(int objnum, int room);
147 // handles a Hyper Anarchy Object Placement packet
148 static void ReceiveHyperPos(uint8_t *data);
149 // Searches through all the objects and looks for the HyperOrb, returns it's objnum. -1 if it doesn't exist
150 static int FindHyperObjectNum(void);
151 // Searches through all the player's inventory, returns the pnum of the player who has the HyperOrb, -1
152 // if no one does.
153 static int FindHyperOrbInInventory(void);
154 static void ResetTimer(void);
155 static void SaveStatsToFile(char *filename);
156
157 void DetermineScore(int precord_num, int column_num, char *buffer, int buffer_size) {
158 player_record *pr = DMFCBase->GetPlayerRecord(precord_num);
159 if (!pr || pr->state == STATE_EMPTY) {
160 buffer[0] = '\0';
161 return;
162 }
163
164 tPlayerStat *stat = (tPlayerStat *)pr->user_info;
165
166 snprintf(buffer, buffer_size, "%d[%d]", stat->Score[DSTAT_LEVEL], stat->Score[DSTAT_OVERALL]);
167 }
168
169 void ShowStatBitmap(int precord_num, int column_num, int x, int y, int w, int h, uint8_t alpha_to_use) {
170 player_record *pr = DMFCBase->GetPlayerRecord(precord_num);
171
172 if (pr && pr->state == STATE_INGAME) {
173 if (WhoHasOrb == pr->pnum) {
174 // draw hyper orb icon
175 DLLRenderHUDQuad(x + 3, y, 10, 10, 0, 0, 1, 1, HyperOrbIcon, alpha_to_use, 0);
176 }
177 }
178 }
179
180 // Timer handlers
181 void OnTimer(void);
182 void OnTimerKill(void);
183 void OnTimerFlagReturn(void);
184 void OnTimerFlagReturnKill(void);
185
186 // This function gets called by the game when it wants to learn some info about the game
187 void DLLFUNCCALL DLLGetGameInfo(tDLLOptions *options) {
188 options->flags = DOF_MAXTEAMS;
189 options->max_teams = 1;
190 strcpy(options->game_name, TXT_GAMENAME);
191 strcpy(options->requirements, "");
192 }
193
194 // Initializes the game function pointers
195 void DLLFUNCCALL DLLGameInit(int *api_func, uint8_t *all_ok, int num_teams_to_use) {
196 *all_ok = 1;
197 DMFCBase = CreateDMFC();
198 if (!DMFCBase) {
199 *all_ok = 0;
200 return;
201 }
202
203 dstat = CreateDmfcStats();
204 if (!dstat) {
205 *all_ok = 0;
206 return;
207 }
208
209 DMFCBase->LoadFunctions(api_func);
210
211 DMFCBase->Set_OnInterval(OnInterval);
212 DMFCBase->Set_OnHUDInterval(OnHUDInterval);
213 DMFCBase->Set_OnKeypress(OnKeypress);
214 DMFCBase->Set_OnServerGameCreated(OnServerGameCreated);
215 DMFCBase->Set_OnServerLevelStart(OnServerLevelStart);
216 DMFCBase->Set_OnClientLevelStart(OnClientLevelStart);
217 DMFCBase->Set_OnClientLevelEnd(OnClientLevelEnd);
218 DMFCBase->Set_OnGameStateRequest(OnGameStateRequest);
219 DMFCBase->Set_OnPlayerConnect(OnPlayerConnect);
220 DMFCBase->Set_OnServerCollide(OnServerCollide);
221 DMFCBase->Set_OnClientCollide(OnClientCollide);
222 DMFCBase->Set_OnClientPlayerKilled(OnClientPlayerKilled);
223 DMFCBase->Set_OnPLRInterval(OnPLRInterval);
224 DMFCBase->Set_OnPLRInit(OnPLRInit);
225 DMFCBase->Set_OnServerPlayerDisconnect(OnServerPlayerDisconnect);
226 DMFCBase->Set_OnClientPlayerDisconnect(OnClientPlayerDisconnect);
227 DMFCBase->Set_OnSaveStatsToFile(OnSaveStatsToFile);
228 DMFCBase->Set_OnLevelEndSaveStatsToFile(OnLevelEndSaveStatsToFile);
229 DMFCBase->Set_OnDisconnectSaveStatsToFile(OnDisconnectSaveStatsToFile);
230 DMFCBase->Set_OnPrintScores(OnPrintScores);
231 DMFCBase->Set_OnPlayerEntersObserver(OnPlayerEntersObserver);
232 DMFCBase->Set_OnClientPlayerEntersGame(OnClientPlayerEntersGame);
233
234 DLLCreateStringTable("hyper.str", &StringTable, &StringTableSize);
235 DLLmprintf(0, "%d strings loaded from string table\n", StringTableSize);
236 if (!StringTableSize) {
237 *all_ok = 0;
238 return;
239 }
240
241 // add the anarchy menu/submenus
242 IMenuItem *lev1, *lev2, *MainMenu;
243 lev1 = CreateMenuItemWArgs(TXT_GAMENAME, MIT_NORMAL, 0, NULL);
244
245 lev2 = CreateMenuItemWArgs(TXT_HUDSCORECOLORS, MIT_STATE, 0, SwitchHUDColor);
246 lev2->SetStateItemList(2, TXT_PLAYERCOLORS, TXT_NORMAL);
247 lev2->SetState(HUD_color_model);
248 lev1->AddSubMenu(lev2);
249
250 MainMenu = DMFCBase->GetOnScreenMenu();
251 MainMenu->AddSubMenu(lev1);
252
253 dObjects = DMFCBase->GetObjects();
254 dPlayers = DMFCBase->GetPlayers();
255
256 DMFCBase->GameInit(1);
257
258 // add the death and suicide messages
259 DMFCBase->AddDeathMessage(TXT_DEATH1, true);
260 DMFCBase->AddSuicideMessage(TXT_SUICIDE1);
261
262 HyperOrbID = DLLFindObjectIDName("Hyperorb");
263 if (HyperOrbID == -1) {
264 DLLmprintf(0, "Hyper Anarchy: BIG WARNING, COULDN'T FIND HyperOrb ID...YOUR GAME IS IN JEOPARDY!\n");
265 *all_ok = 0;
266 return;
267 }
268
269 // setup the Playerstats struct so DMFC can handle it automatically when a new player enters the game
270 DMFCBase->SetupPlayerRecord(sizeof(tPlayerStat), (int (*)(void *, uint8_t *))pack_pstat,
271 (int (*)(void *, uint8_t *))unpack_pstat);
272
273 DMFCBase->RegisterPacketReceiver(SPID_HYPERINFO, ReceiveHyperGameState);
274 DMFCBase->RegisterPacketReceiver(SPID_HYPERPOS, ReceiveHyperPos);
275
276 DMFCBase->SetNumberOfTeams(1);
277 (DMFCBase->GetNetgameInfo())->flags |= (NF_DAMAGE_FRIENDLY | NF_TRACK_RANK);
278
279 DMFCBase->AddHUDItemCallback(HI_TEXT, DisplayHUDScores);
280
281 DisplayScoreScreen = false;
282 WhoHasOrb = -1;
283 KillCount = 0;
284 HyperMoveTimer = -1;
285 HyperOrbIcon = DLLbm_AllocLoadFileBitmap("HyperOrbICON.ogf", 0, BITMAP_FORMAT_1555);
286 if (HyperOrbIcon == -1) {
287 *all_ok = 0;
288 return;
289 }
290
291 Highlight_bmp = DLLbm_AllocBitmap(32, 32, 0);
292 if (Highlight_bmp > BAD_BITMAP_HANDLE) {
293 uint16_t *data = DLLbm_data(Highlight_bmp, 0);
294 if (!data) {
295 // bail on out of here
296 *all_ok = 0;
297 return;
298 }
299 for (int x = 0; x < 32 * 32; x++) {
300 data[x] = GR_RGB16(50, 50, 50) | OPAQUE_FLAG;
301 }
302 }
303
304 // Initialize the Stats Manager
305 // ----------------------------
306
307 tDmfcStatsInit tsi;
308 tDmfcStatsColumnInfo pl_col[7];
309 char gname[20];
310 strcpy(gname, TXT_STATSGAMENAME);
311
312 tsi.flags = DSIF_SHOW_PIC | DSIF_SHOW_OBSERVERICON;
313 tsi.cColumnCountDetailed = 0;
314 tsi.cColumnCountPlayerList = 7;
315 tsi.clbDetailedColumn = NULL;
316 tsi.clbDetailedColumnBMP = NULL;
317 tsi.clbPlayerColumn = DetermineScore;
318 tsi.clbPlayerColumnBMP = ShowStatBitmap;
319 tsi.DetailedColumns = NULL;
320 tsi.GameName = gname;
321 tsi.MaxNumberDisplayed = NULL;
322 tsi.PlayerListColumns = pl_col;
323 tsi.SortedPlayerRecords = SortedPlayers;
324
325 pl_col[0].color_type = DSCOLOR_TEAM;
326 pl_col[0].title[0] = '\0';
327 pl_col[0].type = DSCOL_BMP;
328 pl_col[0].width = 15;
329
330 pl_col[1].color_type = DSCOLOR_SHIPCOLOR;
331 strcpy(pl_col[1].title, TXT_PILOT);
332 pl_col[1].type = DSCOL_PILOT_NAME;
333 pl_col[1].width = 120;
334
335 pl_col[2].color_type = DSCOLOR_SHIPCOLOR;
336 strcpy(pl_col[2].title, TXT_SCORE);
337 pl_col[2].type = DSCOL_CUSTOM;
338 pl_col[2].width = 60;
339
340 pl_col[3].color_type = DSCOLOR_SHIPCOLOR;
341 strcpy(pl_col[3].title, TXT_KILLS_SHORT);
342 pl_col[3].type = DSCOL_KILLS_LEVEL;
343 pl_col[3].width = 57;
344
345 pl_col[4].color_type = DSCOLOR_SHIPCOLOR;
346 strcpy(pl_col[4].title, TXT_DEATHS_SHORT);
347 pl_col[4].type = DSCOL_DEATHS_LEVEL;
348 pl_col[4].width = 57;
349
350 pl_col[5].color_type = DSCOLOR_SHIPCOLOR;
351 strcpy(pl_col[5].title, TXT_SUICIDES_SHORT);
352 pl_col[5].type = DSCOL_SUICIDES_LEVEL;
353 pl_col[5].width = 57;
354
355 pl_col[6].color_type = DSCOLOR_SHIPCOLOR;
356 strcpy(pl_col[6].title, TXT_PING);
357 pl_col[6].type = DSCOL_PING;
358 pl_col[6].width = 40;
359
360 dstat->Initialize(&tsi);
361 }
362
363 // Called when the DLL is shutdown
364 void DLLFUNCCALL DLLGameClose() {
365 if (Highlight_bmp > BAD_BITMAP_HANDLE)
366 DLLbm_FreeBitmap(Highlight_bmp);
367
368 DLLDestroyStringTable(StringTable, StringTableSize);
369 if (HyperOrbIcon > 0)
370 DLLbm_FreeBitmap(HyperOrbIcon);
371
372 if (dstat) {
373 dstat->DestroyPointer();
374 dstat = NULL;
375 }
376
377 if (DMFCBase) {
378 DMFCBase->GameClose();
379 DMFCBase->DestroyPointer();
380 DMFCBase = NULL;
381 }
382 }
383
384 //////////////////////////////////////////////////////////////
385 /////// Overrides ///////////////////////////////////////////
386
387 void OnHUDInterval(void) {
388 dstat->DoFrame();
389 DMFCBase->DisplayOutrageLogo();
390
391 DMFCBase->OnHUDInterval();
392 }
393
394 void OnInterval(void) {
395 // Set a Timer to display
396 if ((HyperMoveTimer == -1) && (WhoHasOrb == -1))
397 HyperMoveTimer = DMFCBase->SetTimerInterval(OnTimerFlagReturn, 30.0f, 30.0f);
398
399 SortPlayerScores(SortedPlayers, MAX_PLAYER_RECORDS);
400 DMFCBase->OnInterval();
401 }
402
403 void OnPlayerEntersObserver(int pnum, object *piggy) {
404 if (WhoHasOrb == pnum) {
405 WhoHasOrb = -1;
406 KillCount = 0;
407 HyperMoveTimer = -1;
408 DLLAddHUDMessage(TXT_LOSTORB, dPlayers[pnum].callsign);
409
410 UpdateEffect();
411 }
412
413 DMFCBase->OnPlayerEntersObserver(pnum, piggy);
414 }
415
416 void OnKeypress(int key) {
417 dllinfo *Data = DMFCBase->GetDLLInfoCallData();
418 switch (key) {
419 case K_F7:
420 DisplayScoreScreen = !DisplayScoreScreen;
421 DMFCBase->EnableOnScreenMenu(false);
422 dstat->Enable(DisplayScoreScreen);
423 break;
424 case K_PAGEDOWN:
425 if (DisplayScoreScreen) {
426 dstat->ScrollDown();
427 Data->iRet = 1;
428 }
429 break;
430 case K_PAGEUP:
431 if (DisplayScoreScreen) {
432 dstat->ScrollUp();
433 Data->iRet = 1;
434 }
435 break;
436 case K_F6:
437 DisplayScoreScreen = false;
438 dstat->Enable(false);
439 break;
440 case K_ESC:
441 if (DisplayScoreScreen) {
442 dstat->Enable(false);
443 DisplayScoreScreen = false;
444 Data->iRet = 1;
445 }
446 break;
447 }
448
449 DMFCBase->OnKeypress(key);
450 }
451
452 // The server has just started, so clear out all the stats and game info
453 void OnServerGameCreated(void) {
454 DMFCBase->OnServerGameCreated();
455 player_record *pr;
456 tPlayerStat *stat;
457 for (int i = 0; i < MAX_PLAYER_RECORDS; i++) {
458 pr = DMFCBase->GetPlayerRecord(i);
459 if (pr)
460 stat = (tPlayerStat *)pr->user_info;
461 else
462 stat = NULL;
463 if (stat) {
464 stat->Score[DSTAT_LEVEL] = 0;
465 stat->Score[DSTAT_OVERALL] = 0;
466 }
467 }
468 }
469
470 void OnServerLevelStart(void) {
471 // Now create a hyper orb in a random room -eek
472 if (HyperOrbID != -1) {
473 DLLmprintf(0, "Attempting to create HyperOrb in a random room\n");
474 int room = GetRandomValidRoom();
475 DLLmprintf(0, "Room %d selected\n", room);
476 // We got a good room
477 // Safe to create the flag
478 CreateHyperOrbInRoom(room);
479 }
480
481 KillCount = 0;
482 WhoHasOrb = -1;
483 HyperMoveTimer = -1;
484 UpdateEffect();
485
486 DMFCBase->OnServerLevelStart();
487 }
488
489 void OnClientLevelEnd(void) { DMFCBase->OnClientLevelEnd(); }
490
491 // The server has started a new level, so clear out level scores
492 void OnClientLevelStart(void) {
493 DMFCBase->OnClientLevelStart();
494
495 player_record *pr;
496 tPlayerStat *stat;
497 for (int i = 0; i < MAX_PLAYER_RECORDS; i++) {
498 pr = DMFCBase->GetPlayerRecord(i);
499 if (pr)
500 stat = (tPlayerStat *)pr->user_info;
501 else
502 stat = NULL;
503 if (stat) {
504 stat->Score[DSTAT_LEVEL] = 0;
505 }
506 }
507
508 DMFCBase->RequestGameState();
509 UpdateEffect();
510 }
511
512 // We need to send all the inventory info out to the new player
513 void OnGameStateRequest(int player_num) {
514 SendHyperGameState(player_num);
515 DMFCBase->OnGameStateRequest(player_num);
516 }
517
518 // A new player has entered the game, zero there stats out
519 void OnPlayerConnect(int player_num) {
520 tPlayerStat *stat = (tPlayerStat *)DMFCBase->GetPlayerRecordData(player_num);
521 if (stat) {
522 stat->Score[DSTAT_LEVEL] = 0;
523 stat->Score[DSTAT_OVERALL] = 0;
524 }
525
526 DMFCBase->OnPlayerConnect(player_num);
527 }
528
529 // A new player has entered the game, zero there stats out
530 void OnClientPlayerEntersGame(int player_num) {
531 DMFCBase->OnClientPlayerEntersGame(player_num);
532 if (player_num != DMFCBase->GetPlayerNum())
533 DisplayWelcomeScreen(player_num);
534 else
535 display_my_welcome = true;
536 }
537
538 void OnPrintScores(int level) {
539 char buffer[256];
540 char name[50];
541 memset(buffer, ' ', 256);
542
543 size_t t;
544 size_t pos[6];
545 size_t len[6];
546 pos[0] = 0;
547 t = len[0] = 20; // give ample room for pilot name
548 pos[1] = pos[0] + t + 1;
549 t = len[1] = strlen(TXT_SCORE);
550 pos[2] = pos[1] + t + 1;
551 t = len[2] = strlen(TXT_KILLS);
552 pos[3] = pos[2] + t + 1;
553 t = len[3] = strlen(TXT_DEATHS);
554 pos[4] = pos[3] + t + 1;
555 t = len[4] = strlen(TXT_SUICIDES);
556 pos[5] = pos[4] + t + 1;
557 t = len[5] = strlen(TXT_PING);
558
559 memcpy(&buffer[pos[0]], TXT_PILOT, strlen(TXT_PILOT));
560 memcpy(&buffer[pos[1]], TXT_SCORE, len[1]);
561 memcpy(&buffer[pos[2]], TXT_KILLS, len[2]);
562 memcpy(&buffer[pos[3]], TXT_DEATHS, len[3]);
563 memcpy(&buffer[pos[4]], TXT_SUICIDES, len[4]);
564 memcpy(&buffer[pos[5]], TXT_PING, len[5]);
565 buffer[pos[5] + len[5] + 1] = '\n';
566 buffer[pos[5] + len[5] + 2] = '\0';
567 DPrintf(buffer);
568
569 int slot;
570 player_record *pr;
571 int pcount;
572
573 if (level < 0 || level >= MAX_PLAYER_RECORDS)
574 pcount = MAX_PLAYER_RECORDS;
575 else
576 pcount = level;
577
578 int sortedplayers[MAX_PLAYER_RECORDS];
579 DMFCBase->GetSortedPlayerSlots(sortedplayers, MAX_PLAYER_RECORDS);
580
581 for (int i = 0; i < pcount; i++) {
582 slot = sortedplayers[i];
583 pr = DMFCBase->GetPlayerRecord(slot);
584 if ((pr) && (pr->state != STATE_EMPTY)) {
585
586 if (DMFCBase->IsPlayerDedicatedServer(pr))
587 continue; // skip dedicated server
588
589 snprintf(name, sizeof(name), "%s%s:", (pr->state == STATE_DISCONNECTED) ? "*" : "", pr->callsign);
590 name[19] = '\0';
591
592 tPlayerStat *stat = (tPlayerStat *)pr->user_info;
593
594 memset(buffer, ' ', 256);
595 t = strlen(name);
596 memcpy(&buffer[pos[0]], name, (t < len[0]) ? t : len[0]);
597 snprintf(name, sizeof(name), "%d[%d]", (stat) ? stat->Score[DSTAT_LEVEL] : 0,
598 (stat) ? stat->Score[DSTAT_OVERALL] : 0);
599 t = strlen(name);
600 memcpy(&buffer[pos[1]], name, (t < len[1]) ? t : len[1]);
601 snprintf(name, sizeof(name), "%d[%d]", pr->dstats.kills[DSTAT_LEVEL], pr->dstats.kills[DSTAT_OVERALL]);
602 t = strlen(name);
603 memcpy(&buffer[pos[2]], name, (t < len[2]) ? t : len[2]);
604 snprintf(name, sizeof(name), "%d[%d]", pr->dstats.deaths[DSTAT_LEVEL], pr->dstats.deaths[DSTAT_OVERALL]);
605 t = strlen(name);
606 memcpy(&buffer[pos[3]], name, (t < len[3]) ? t : len[3]);
607 snprintf(name, sizeof(name), "%d[%d]", pr->dstats.suicides[DSTAT_LEVEL], pr->dstats.suicides[DSTAT_OVERALL]);
608 t = strlen(name);
609 memcpy(&buffer[pos[4]], name, (t < len[4]) ? t : len[4]);
610
611 if (pr->state == STATE_INGAME)
612 snprintf(name, sizeof(name), "%.0f", (DMFCBase->GetNetPlayers())[pr->pnum].ping_time * 1000.0f);
613 else
614 strcpy(name, "---");
615 t = strlen(name);
616 memcpy(&buffer[pos[5]], name, (t < len[5]) ? t : len[5]);
617 buffer[pos[5] + len[5] + 1] = '\n';
618 buffer[pos[5] + len[5] + 2] = '\0';
619 DPrintf(buffer);
620 }
621 }
622 }
623
624 bool compare_slots(int a, int b) {
625 int ascore, bscore;
626 player_record *apr, *bpr;
627 tPlayerStat *astat, *bstat;
628
629 apr = DMFCBase->GetPlayerRecord(a);
630 bpr = DMFCBase->GetPlayerRecord(b);
631 if (!apr)
632 return true;
633 if (!bpr)
634 return false;
635 if (apr->state == STATE_EMPTY)
636 return true;
637 if (bpr->state == STATE_EMPTY)
638 return false;
639 astat = (tPlayerStat *)apr->user_info;
640 bstat = (tPlayerStat *)bpr->user_info;
641
642 if ((apr->state == STATE_INGAME) && (bpr->state == STATE_INGAME)) {
643 // both players were in the game
644 ascore = (astat) ? astat->Score[DSTAT_LEVEL] : 0;
645 bscore = (bstat) ? bstat->Score[DSTAT_LEVEL] : 0;
646 return (ascore < bscore);
647 }
648 if ((apr->state == STATE_INGAME) && (bpr->state == STATE_DISCONNECTED)) {
649 // apr gets priority since he was in the game on exit
650 return false;
651 }
652 if ((apr->state == STATE_DISCONNECTED) && (bpr->state == STATE_INGAME)) {
653 // bpr gets priority since he was in the game on exit
654 return true;
655 }
656 // if we got here then both players were disconnected
657 ascore = (astat) ? astat->Score[DSTAT_LEVEL] : 0;
658 bscore = (bstat) ? bstat->Score[DSTAT_LEVEL] : 0;
659 return (ascore < bscore);
660 }
661
662 void OnPLRInit(void) {
663 int i, t, j;
664
665 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
666 SortedPlayers[i] = i;
667 }
668
669 for (i = 1; i <= MAX_PLAYER_RECORDS - 1; i++) {
670 t = SortedPlayers[i];
671 // Shift elements down until
672 // insertion point found.
673 for (j = i - 1; j >= 0 && compare_slots(SortedPlayers[j], t); j--) {
674 SortedPlayers[j + 1] = SortedPlayers[j];
675 }
676 // insert
677 SortedPlayers[j + 1] = t;
678 }
679
680 DMFCBase->OnPLRInit();
681 }
682
683 void OnPLRInterval(void) {
684 #define PLAYERS_COL 110
685 #define SCORE_COL 240
686 #define KILLS_COL 300
687 #define DEATHS_COL 360
688 #define SUICIDES_COL 420
689
690 DMFCBase->OnPLRInterval();
691
692 int y = 40;
693 int height = DLLgrfont_GetHeight((DMFCBase->GetGameFontTranslateArray())[SMALL_UI_FONT_INDEX]) + 1;
694
695 DLLgrtext_SetFont((DMFCBase->GetGameFontTranslateArray())[SMALL_UI_FONT_INDEX]);
696
697 // print out header
698 DLLgrtext_SetColor(GR_RGB(255, 255, 150));
699 DLLgrtext_Printf(PLAYERS_COL, y, TXT_PILOT);
700 DLLgrtext_Printf(SCORE_COL, y, TXT_SCORE);
701 DLLgrtext_Printf(KILLS_COL, y, TXT_KILLS);
702 DLLgrtext_Printf(DEATHS_COL, y, TXT_DEATHS);
703 DLLgrtext_Printf(SUICIDES_COL, y, TXT_SUICIDES);
704 y += height;
705
706 // print out player stats
707 int rank = 1;
708 int slot;
709 tPlayerStat *stat;
710 player_record *pr;
711
712 for (int i = 0; i < MAX_PLAYER_RECORDS; i++) {
713 slot = SortedPlayers[i];
714 pr = DMFCBase->GetPlayerRecord(slot);
715 if (pr && pr->state != STATE_EMPTY) {
716
717 if (DMFCBase->IsPlayerDedicatedServer(pr))
718 continue; // skip dedicated server
719
720 stat = (tPlayerStat *)pr->user_info;
721
722 int pnum = DMFCBase->WasPlayerInGameAtLevelEnd(slot);
723 if (pnum != -1) {
724 DLLgrtext_SetColor((DMFCBase->GetPlayerColors())[pnum]);
725 } else {
726 DLLgrtext_SetColor(GR_RGB(128, 128, 128));
727 }
728
729 DLLgrtext_Printf(PLAYERS_COL, y, "%d)%s", rank, pr->callsign);
730 DLLgrtext_Printf(SCORE_COL, y, "%d[%d]", (stat) ? stat->Score[DSTAT_LEVEL] : 0,
731 (stat) ? stat->Score[DSTAT_OVERALL] : 0);
732 DLLgrtext_Printf(KILLS_COL, y, "%d[%d]", pr->dstats.kills[DSTAT_LEVEL], pr->dstats.kills[DSTAT_OVERALL]);
733 DLLgrtext_Printf(DEATHS_COL, y, "%d[%d]", pr->dstats.deaths[DSTAT_LEVEL], pr->dstats.deaths[DSTAT_OVERALL]);
734 DLLgrtext_Printf(SUICIDES_COL, y, "%d[%d]", pr->dstats.suicides[DSTAT_LEVEL], pr->dstats.suicides[DSTAT_OVERALL]);
735 y += height;
736 rank++;
737 if (y >= 440)
738 goto quick_exit;
739 }
740 }
741
742 quick_exit:;
743 }
744
745 void OnServerCollide(object *me_obj, object *it_obj) {
746 if (!me_obj || !it_obj)
747 return;
748
749 if (it_obj->type != OBJ_PLAYER) {
750 DMFCBase->OnServerCollide(me_obj, it_obj);
751 return;
752 }
753
754 if ((me_obj->type == OBJ_PLAYER) && (DLLInvCheckItem(it_obj->id, OBJ_POWERUP, HyperOrbID))) {
755 /*
756 //Two Players collided...one had orb
757 DLLmprintf(0,"Player2Player\n");
758 DMFCBase->CallClientEvent(EVT_CLIENT_GAMECOLLIDE,DMFCBase->GetMeObjNum(),DMFCBase->GetItObjNum(),-1);
759 DMFCBase->CallOnClientCollide(me_obj,it_obj);
760 */
761 } else if (me_obj->type == OBJ_POWERUP && me_obj->id == HyperOrbID) {
762 // A Player collided with the HyperOrb
763 // Sent this information to the clients to process
764 DMFCBase->CallClientEvent(EVT_CLIENT_GAMECOLLIDE, DMFCBase->GetMeObjNum(), DMFCBase->GetItObjNum(), -1);
765 DMFCBase->CallOnClientCollide(me_obj, it_obj);
766 }
767
768 DMFCBase->OnServerCollide(me_obj, it_obj);
769 }
770
771 void OnClientCollide(object *me_objp, object *it_objp) {
772 if (me_objp->type == OBJ_PLAYER) {
773 // spew this puppy
774 DLLPlayerSpewInventory(it_objp, false, false);
775 DLLAddHUDMessage(TXT_LOSTORB, dPlayers[it_objp->id].callsign);
776 WhoHasOrb = -1;
777 KillCount = 0;
778 HyperMoveTimer = -1;
779 UpdateEffect();
780 } else {
781 // We'll only get here if me_objp is HyperOrb and it_objp is the collider
782 WhoHasOrb = it_objp->id;
783 KillCount = 0; // reset this variable, since they just picked up the HyperOrb
784
785 // add the hyper orb to the players inventory
786 DLLInvAddTypeID(it_objp->id, me_objp->type, me_objp->id, -1, -1, 0, NULL);
787 // if we are the server, remove the orb from the world and tell the clients to do the same
788 if (DMFCBase->GetLocalRole() == LR_SERVER) {
789 DLLSetObjectDeadFlag(me_objp, true, 0);
790 }
791
792 DLLAddHUDMessage(TXT_GOTORB, dPlayers[it_objp->id].callsign);
793
794 // Play a sound
795 int id = DLLFindSoundName("PwrHyperOrb");
796 if (id != -1)
797 DLLPlay3dSound(id, &dObjects[dPlayers[DMFCBase->GetPlayerNum()].objnum], 1, 0);
798 UpdateEffect();
799
800 // Set a Timer to display
801 if (WhoJustScoredTimer != -1)
802 DMFCBase->KillTimer(WhoJustScoredTimer);
803 WhoJustScoredTimer = DMFCBase->SetTimerInterval(OnTimer, 0.5f, 5.0f, OnTimerKill);
804 }
805 DMFCBase->OnClientCollide(me_objp, it_objp);
806 }
807
808 void OnClientPlayerKilled(object *killer_obj, int victim_pnum) {
809 int score = 0;
810 int kpnum;
811
812 if (killer_obj) {
813 if ((killer_obj->type == OBJ_PLAYER) || (killer_obj->type == OBJ_GHOST))
814 kpnum = killer_obj->id;
815 else if (killer_obj->type == OBJ_ROBOT || (killer_obj->type == OBJ_BUILDING && killer_obj->ai_info)) {
816 // countermeasure kill
817 kpnum = DMFCBase->GetCounterMeasureOwner(killer_obj);
818 } else {
819 kpnum = -1;
820 }
821 } else
822 kpnum = -1;
823
824 bool suicide = (bool)(kpnum == victim_pnum);
825
826 if (victim_pnum == WhoHasOrb) {
827 // The guy with the score orb lost it!
828 KillCount++;
829 if (!suicide) {
830 score = KillCount + 1;
831 if (score > 5)
832 score = 5;
833 } else {
834 score = -(KillCount + 1);
835 if (score < -5)
836 score = -5;
837 }
838
839 WhoHasOrb = -1;
840 KillCount = 0;
841 HyperMoveTimer = -1;
842 DLLAddHUDMessage(TXT_LOSTORB, dPlayers[victim_pnum].callsign);
843
844 UpdateEffect();
845 } else {
846
847 if (kpnum != -1) {
848 // Was the killer the guy with the HyperOrb?
849 if (kpnum == WhoHasOrb) {
850 // Yup!
851 KillCount++;
852 score = KillCount + 1;
853 if (score > 5)
854 score = 5;
855 if (!suicide)
856 DLLAddHUDMessage(TXT_SCOREPOINTS, dPlayers[kpnum].callsign, score);
857 } else {
858 // Nope!
859 if (!suicide)
860 score = 1;
861 else
862 score = -1;
863 }
864 }
865 }
866
867 tPlayerStat *stat = (tPlayerStat *)DMFCBase->GetPlayerRecordData(kpnum);
868
869 if (kpnum != -1 && stat) {
870 stat->Score[DSTAT_LEVEL] += score;
871 stat->Score[DSTAT_OVERALL] += score;
872 }
873
874 if ((DMFCBase->GetLocalRole() == LR_SERVER) && (kpnum != -1) && (stat)) {
875 // check the score to see if we hit the limit
876 if ((DMFCBase->GetScoreLimit(&score))) {
877 if (score <= stat->Score[DSTAT_LEVEL]) {
878 DLLmprintf(0, "Score limit reached\n");
879 DMFCBase->EndLevel();
880 }
881 }
882 }
883
884 DMFCBase->OnClientPlayerKilled(killer_obj, victim_pnum);
885 }
886
887 void OnServerPlayerDisconnect(int player_num) {
888 DMFCBase->OnServerPlayerDisconnect(player_num);
889 if (player_num == WhoHasOrb) {
890 // send this event to the clients so they can handle it
891 DMFCBase->CallClientEvent(EVT_CLIENT_GAMEPLAYERDISCONNECT, DMFCBase->GetMeObjNum(), DMFCBase->GetItObjNum(), -1);
892 DMFCBase->CallOnClientPlayerDisconnect(player_num);
893 }
894 }
895
896 void OnClientPlayerDisconnect(int player_num) {
897 if (player_num == WhoHasOrb) {
898 WhoHasOrb = -1;
899 KillCount = 0;
900 HyperMoveTimer = -1;
901 ResetTimer();
902 }
903 DMFCBase->OnClientPlayerDisconnect(player_num);
904 }
905
906 void SaveStatsToFile(char *filename) {
907 CFILE *file;
908 DLLOpenCFILE(&file, filename, "wt");
909 if (!file) {
910 DLLmprintf(0, "Unable to open output file\n");
911 return;
912 }
913
914 // write out game stats
915 #define BUFSIZE 150
916 char buffer[BUFSIZE];
917 char tempbuffer[25];
918 int sortedslots[MAX_PLAYER_RECORDS];
919 player_record *pr, *dpr;
920 tPInfoStat stat;
921 tPlayerStat *ps;
922 int count, p;
923
924 // sort the stats
925 SortPlayerScores(sortedslots, MAX_PLAYER_RECORDS);
926 count = 1;
927
928 snprintf(buffer, sizeof(buffer), TXT_SAVESTATSA, (DMFCBase->GetNetgameInfo())->name,
929 (DMFCBase->GetCurrentMission())->cur_level);
930 DLLcf_WriteString(file, buffer);
931
932 snprintf(buffer, sizeof(buffer), "%s", TXT_SAVESTATSB);
933 DLLcf_WriteString(file, buffer);
934
935 snprintf(buffer, sizeof(buffer), "%s", TXT_SAVESTATSC);
936 DLLcf_WriteString(file, buffer);
937 strcpy(buffer, "----------------------------------------------------------------------------------------------");
938 DLLcf_WriteString(file, buffer);
939
940 for (p = 0; p < MAX_PLAYER_RECORDS; p++) {
941 pr = DMFCBase->GetPlayerRecord(sortedslots[p]);
942 if (pr && pr->state != STATE_EMPTY) {
943
944 if (DMFCBase->IsPlayerDedicatedServer(pr))
945 continue; // skip dedicated server
946
947 memset(buffer, ' ', BUFSIZE);
948
949 ps = (tPlayerStat *)pr->user_info;
950
951 snprintf(tempbuffer, sizeof(tempbuffer), "%d)", count);
952 memcpy(&buffer[0], tempbuffer, strlen(tempbuffer));
953
954 snprintf(tempbuffer, sizeof(tempbuffer), "%s%s", (pr->state == STATE_INGAME) ? "" : "*", pr->callsign);
955 memcpy(&buffer[7], tempbuffer, strlen(tempbuffer));
956
957 if (ps) {
958 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", ps->Score[DSTAT_LEVEL], ps->Score[DSTAT_OVERALL]);
959 memcpy(&buffer[35], tempbuffer, strlen(tempbuffer));
960 }
961
962 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", pr->dstats.kills[DSTAT_LEVEL],
963 pr->dstats.kills[DSTAT_OVERALL]);
964 memcpy(&buffer[47], tempbuffer, strlen(tempbuffer));
965
966 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", pr->dstats.deaths[DSTAT_LEVEL],
967 pr->dstats.deaths[DSTAT_OVERALL]);
968 memcpy(&buffer[59], tempbuffer, strlen(tempbuffer));
969
970 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", pr->dstats.suicides[DSTAT_LEVEL],
971 pr->dstats.suicides[DSTAT_OVERALL]);
972 memcpy(&buffer[70], tempbuffer, strlen(tempbuffer));
973
974 snprintf(tempbuffer, sizeof(tempbuffer), "%s", DMFCBase->GetTimeString(DMFCBase->GetTimeInGame(sortedslots[p])));
975 memcpy(&buffer[81], tempbuffer, strlen(tempbuffer));
976
977 size_t pos = 81 + strlen(tempbuffer) + 1;
978
979 if (pos < BUFSIZE)
980 buffer[pos] = '\0';
981
982 buffer[BUFSIZE - 1] = '\0';
983 DLLcf_WriteString(file, buffer);
984 count++;
985 }
986 }
987
988 DLLcf_WriteString(file, TXT_SAVESTATSD);
989
990 count = 1;
991 for (p = 0; p < MAX_PLAYER_RECORDS; p++) {
992 pr = DMFCBase->GetPlayerRecord(p);
993 if (pr && pr->state != STATE_EMPTY) {
994
995 if (DMFCBase->IsPlayerDedicatedServer(pr))
996 continue; // skip dedicated server
997
998 // Write out header
999 snprintf(buffer, sizeof(buffer), "%d) %s%s", count, (pr->state == STATE_INGAME) ? "" : "*", pr->callsign);
1000 DLLcf_WriteString(file, buffer);
1001 size_t length = strlen(buffer);
1002 memset(buffer, '=', length);
1003 buffer[length] = '\0';
1004 DLLcf_WriteString(file, buffer);
1005
1006 // time in game
1007 snprintf(buffer, sizeof(buffer), TXT_TIMEINGAME, DMFCBase->GetTimeString(DMFCBase->GetTimeInGame(p)));
1008 DLLcf_WriteString(file, buffer);
1009
1010 if (DMFCBase->FindPInfoStatFirst(p, &stat)) {
1011 snprintf(buffer, sizeof(buffer), "%s", TXT_SAVESTATSE);
1012 DLLcf_WriteString(file, buffer);
1013
1014 if (stat.slot != p) {
1015 memset(buffer, ' ', BUFSIZE);
1016 dpr = DMFCBase->GetPlayerRecord(stat.slot);
1017
1018 if (dpr) {
1019 snprintf(tempbuffer, sizeof(tempbuffer), "%s", dpr->callsign);
1020 memcpy(buffer, tempbuffer, strlen(tempbuffer));
1021
1022 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.kills);
1023 memcpy(&buffer[30], tempbuffer, strlen(tempbuffer));
1024
1025 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.deaths);
1026 memcpy(&buffer[40], tempbuffer, strlen(tempbuffer));
1027
1028 size_t pos = 40 + strlen(tempbuffer) + 1;
1029 if (pos < BUFSIZE)
1030 buffer[pos] = '\0';
1031
1032 buffer[BUFSIZE - 1] = '\0';
1033 DLLcf_WriteString(file, buffer);
1034 }
1035 }
1036
1037 while (DMFCBase->FindPInfoStatNext(&stat)) {
1038 if (stat.slot != p) {
1039 memset(buffer, ' ', BUFSIZE);
1040 dpr = DMFCBase->GetPlayerRecord(stat.slot);
1041
1042 if (dpr) {
1043 strncpy(tempbuffer, dpr->callsign, sizeof(tempbuffer));
1044 memcpy(buffer, tempbuffer, strlen(tempbuffer));
1045
1046 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.kills);
1047 memcpy(&buffer[30], tempbuffer, strlen(tempbuffer));
1048
1049 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.deaths);
1050 memcpy(&buffer[40], tempbuffer, strlen(tempbuffer));
1051
1052 size_t pos = 40 + strlen(tempbuffer) + 1;
1053 if (pos < BUFSIZE)
1054 buffer[pos] = '\0';
1055
1056 buffer[BUFSIZE - 1] = '\0';
1057 DLLcf_WriteString(file, buffer);
1058 }
1059 }
1060 }
1061 }
1062 DMFCBase->FindPInfoStatClose();
1063 DLLcf_WriteString(file, ""); // skip a line
1064 count++;
1065 }
1066 }
1067
1068 // done writing stats
1069 DLLcfclose(file);
1070 DLLAddHUDMessage(TXT_STATSAVED);
1071 }
1072
1073 #define ROOTFILENAME "Hyper"
1074 void OnSaveStatsToFile(void) {
1075 char filename[256];
1076 DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, false);
1077 SaveStatsToFile(filename);
1078 }
1079
1080 void OnLevelEndSaveStatsToFile(void) {
1081 char filename[256];
1082 DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, true);
1083 SaveStatsToFile(filename);
1084 }
1085
1086 void OnDisconnectSaveStatsToFile(void) {
1087 char filename[256];
1088 DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, false);
1089 SaveStatsToFile(filename);
1090 }
1091
1092 /////////////////////////////////////////////////////////////
1093 /////////////////////////////////////////////////////////////
1094
1095 // handles a Hyper Anarchy Game State packet
1096 void ReceiveHyperGameState(uint8_t *data) {
1097 // remove all the HyperOrbs from everyone's inventory
1098 for (int i = 0; i < DLLMAX_PLAYERS; i++)
1099 DLLInvRemove(i, OBJ_POWERUP, HyperOrbID);
1100
1101 int count = 0;
1102 WhoHasOrb = MultiGetInt(data, &count);
1103 KillCount = MultiGetInt(data, &count);
1104
1105 if (WhoHasOrb != -1) {
1106 DLLmprintf(0, "%s has the score orb!\n", dPlayers[WhoHasOrb].callsign);
1107 DLLInvAddTypeID(WhoHasOrb, OBJ_POWERUP, HyperOrbID, -1, -1, 0, NULL);
1108 }
1109 UpdateEffect();
1110 }
1111
1112 // sends a Hyper Anarchy Game State packet
1113 void SendHyperGameState(int playernum) {
1114 bool updateall = false;
1115 int count = 0;
1116 uint8_t data[MAX_GAME_DATA_SIZE];
1117 DMFCBase->StartPacket(data, SPID_HYPERINFO, &count);
1118
1119 if ((WhoHasOrb != -1) && (!DMFCBase->CheckPlayerNum(WhoHasOrb))) {
1120 WhoHasOrb = -1;
1121 KillCount = 0;
1122 HyperMoveTimer = -1;
1123 updateall = true;
1124 }
1125
1126 MultiAddInt(WhoHasOrb, data, &count);
1127 MultiAddInt(KillCount, data, &count);
1128
1129 DMFCBase->SendPacket(data, count, (updateall) ? SP_ALL : playernum);
1130
1131 if (updateall) {
1132 // remove all the HyperOrbs from everyone's inventory
1133 for (int i = 0; i < DLLMAX_PLAYERS; i++)
1134 DLLInvRemove(i, OBJ_POWERUP, HyperOrbID);
1135 UpdateEffect();
1136 }
1137 }
1138
1139 // Displays HUD scores
1140 void DisplayHUDScores(struct tHUDItem *hitem) {
1141 if (display_my_welcome) {
1142 DisplayWelcomeScreen(DMFCBase->GetPlayerNum());
1143 display_my_welcome = false;
1144 }
1145
1146 if (DisplayScoreScreen)
1147 return;
1148
1149 int height = DLLgrfont_GetHeight((DMFCBase->GetGameFontTranslateArray())[HUD_FONT_INDEX]) + 3;
1150 uint8_t alpha = DMFCBase->ConvertHUDAlpha((uint8_t)((DisplayScoreScreen) ? 128 : 255));
1151 int y = (DMFCBase->GetGameWindowH() / 2) - ((height * 5) / 2);
1152 int x = 510;
1153 int rank = 1;
1154 ddgr_color color = 0;
1155 bool hasorb;
1156 char temp[50];
1157 bool hdisp = false;
1158
1159 // determine coordinates to use here
1160 // we'll use a virtual width of 85 pixels on a 640x480 screen
1161 // so first determine the new width
1162 int name_width = 85.0f * DMFCBase->GetHudAspectX();
1163 int score_width = DLLgrtext_GetTextLineWidth("888");
1164 int name_x = DMFCBase->GetGameWindowW() - name_width - score_width - 10;
1165 int score_x = DMFCBase->GetGameWindowW() - score_width - 5;
1166 int icon_size = height - 3;
1167 int icon_x = name_x - 5 - icon_size;
1168 bool player_shown;
1169
1170 #define NUM_PLAYERS_TO_SHOW_PLUS_ONE 6
1171
1172 for (int i = 0; i < MAX_PLAYER_RECORDS; i++) {
1173 int slot = SortedPlayers[i];
1174 player_record *pr = DMFCBase->GetPlayerRecord(slot);
1175 player_shown = false;
1176
1177 if (pr && pr->state != STATE_EMPTY) {
1178 hasorb = false;
1179
1180 if (DMFCBase->IsPlayerDedicatedServer(pr))
1181 continue; // skip dedicated server
1182
1183 if ((pr->state == STATE_DISCONNECTED) || (pr->state == STATE_INGAME && !DMFCBase->IsPlayerObserver(pr->pnum))) {
1184 tPlayerStat *stat = (tPlayerStat *)pr->user_info;
1185
1186 if ((rank < NUM_PLAYERS_TO_SHOW_PLUS_ONE) ||
1187 (pr->state == STATE_INGAME && pr->pnum == DMFCBase->GetPlayerNum()))
1188 player_shown = true;
1189
1190 if (pr->state == STATE_INGAME && WhoHasOrb == pr->pnum) {
1191 hasorb = true;
1192 if (player_shown)
1193 hdisp = true;
1194 }
1195
1196 if (((pr->state == STATE_INGAME) && (WhoHasOrb != pr->pnum)) || (DisplayFlagBlink) ||
1197 pr->state == STATE_DISCONNECTED) {
1198 strcpy(temp, pr->callsign);
1199 DMFCBase->ClipString(name_width, temp, true);
1200
1201 if (pr->state == STATE_INGAME && pr->pnum == DMFCBase->GetPlayerNum()) {
1202
1203 switch (HUD_color_model) {
1204 case HCM_PLAYERCOLOR:
1205 color = (DMFCBase->GetPlayerColors())[pr->pnum];
1206 break;
1207 case HCM_NORMAL:
1208 color = GR_RGB(40, 255, 40);
1209 break;
1210 };
1211
1212 if (Highlight_bmp > BAD_BITMAP_HANDLE) {
1213 // draw the highlight bar in the background
1214 DLLrend_SetAlphaValue(alpha * 0.50f);
1215 DLLrend_SetZBufferState(0);
1216 DLLrend_SetTextureType(TT_LINEAR);
1217 DLLrend_SetLighting(LS_NONE);
1218 DLLrend_SetAlphaType(AT_CONSTANT_TEXTURE);
1219
1220 DLLrend_DrawScaledBitmap(icon_x - 2, y - 2, score_x + score_width + 2, y + height - 1, Highlight_bmp, 0,
1221 0, 1, 1, 1.0f, -1, NULL);
1222 DLLrend_SetZBufferState(1);
1223 }
1224
1225 DLLgrtext_SetAlpha(alpha);
1226 DLLgrtext_SetColor(color);
1227 DLLgrtext_Printf(name_x, y, temp);
1228 DLLgrtext_Printf(score_x, y, "%d", (stat) ? stat->Score[DSTAT_LEVEL] : 0);
1229
1230 } else if (rank < NUM_PLAYERS_TO_SHOW_PLUS_ONE) {
1231 if (pr->state == STATE_DISCONNECTED) {
1232 color = GR_GREY;
1233 } else {
1234 switch (HUD_color_model) {
1235 case HCM_PLAYERCOLOR:
1236 color = (DMFCBase->GetPlayerColors())[pr->pnum];
1237 break;
1238 case HCM_NORMAL:
1239 color = GR_RGB(40, 255, 40);
1240 break;
1241 };
1242 }
1243
1244 DLLgrtext_SetAlpha(alpha);
1245 DLLgrtext_SetColor(color);
1246 DLLgrtext_Printf(name_x, y, temp);
1247 DLLgrtext_Printf(score_x, y, "%d", (stat) ? stat->Score[DSTAT_LEVEL] : 0);
1248 }
1249 }
1250
1251 if (hasorb && DisplayFlagBlink) {
1252 DLLrend_SetZBufferState(0);
1253 DLLrend_SetTextureType(TT_LINEAR);
1254 DLLrend_SetAlphaValue(alpha);
1255 DLLrend_SetLighting(LS_NONE);
1256 DLLrend_SetWrapType(WT_CLAMP);
1257 DLLrend_SetAlphaType(AT_CONSTANT_TEXTURE);
1258 DLLrend_DrawScaledBitmap(icon_x, y, icon_x + icon_size, y + icon_size, HyperOrbIcon, 0, 0, 1, 1, 1.0f, -1,
1259 NULL);
1260 DLLrend_SetZBufferState(1);
1261 }
1262
1263 if (player_shown)
1264 y += height;
1265 rank++;
1266 }
1267 }
1268 }
1269
1270 if (WhoHasOrb != -1 && DisplayFlagBlink && !hdisp) {
1271 player_record *pr = DMFCBase->GetPlayerRecordByPnum(WhoHasOrb);
1272 if (pr && pr->state == STATE_INGAME) {
1273 // the player with the orb needs to be shown
1274 strcpy(temp, dPlayers[WhoHasOrb].callsign);
1275 DMFCBase->ClipString(name_width, temp, true);
1276
1277 DLLgrtext_SetAlpha(alpha);
1278 DLLgrtext_SetColor(GR_RGB(40, 255, 40));
1279 DLLgrtext_Printf(name_x, y, temp);
1280
1281 tPlayerStat *stat = (tPlayerStat *)pr->user_info;
1282 DLLgrtext_Printf(score_x, y, "%d", (stat) ? stat->Score[DSTAT_LEVEL] : 0);
1283
1284 DLLrend_SetZBufferState(0);
1285 DLLrend_SetTextureType(TT_LINEAR);
1286 DLLrend_SetAlphaValue(alpha);
1287 DLLrend_SetLighting(LS_NONE);
1288 DLLrend_SetWrapType(WT_CLAMP);
1289 DLLrend_SetAlphaType(AT_CONSTANT_TEXTURE);
1290 DLLrend_DrawScaledBitmap(icon_x, y, icon_x + icon_size, y + icon_size, HyperOrbIcon, 0, 0, 1, 1, 1.0f, -1, NULL);
1291 DLLrend_SetZBufferState(1);
1292 }
1293 }
1294
1295 // Display the HyperOrb in the corner if we have it
1296 if (DMFCBase->GetPlayerNum() == WhoHasOrb) {
1297 DLLrend_SetZBufferState(0);
1298 DLLrend_SetTextureType(TT_LINEAR);
1299 DLLrend_SetAlphaValue(alpha);
1300 DLLrend_SetLighting(LS_NONE);
1301 DLLrend_SetWrapType(WT_CLAMP);
1302 DLLrend_SetAlphaType(AT_CONSTANT_TEXTURE);
1303 int x1, x2, y1, y2;
1304 x1 = DMFCBase->GetGameWindowW() - 5 - DLLbm_w(HyperOrbIcon, 0);
1305 y1 = 25;
1306 x2 = x1 + DLLbm_w(HyperOrbIcon, 0);
1307 y2 = y1 + DLLbm_h(HyperOrbIcon, 0);
1308 DLLrend_DrawScaledBitmap(x1, y1, x2, y2, HyperOrbIcon, 0, 0, 1, 1, 1.0f, -1, NULL);
1309 DLLrend_SetZBufferState(1);
1310 }
1311 }
1312
1313 // Update Effect on the guy who has the Orb
1314 void UpdateEffect(void) {
1315 float redb[4];
1316 float greenb[4];
1317 float blueb[4];
1318 int i;
1319
1320 for (i = 0; i < 4; i++) {
1321 redb[i] = 0.83f;
1322 greenb[i] = 0.17f;
1323 blueb[i] = 0;
1324 }
1325
1326 for (i = 0; i < DLLMAX_PLAYERS; i++) {
1327 if (DMFCBase->CheckPlayerNum(i)) {
1328 // Player valid
1329 if (i == WhoHasOrb) {
1330 // Do the effect on this guy
1331 DLLPlayerSetRotatingBall(i, 3, 4, redb, greenb, blueb);
1332 } else {
1333 // Reset effect on this guy
1334 DLLPlayerSetRotatingBall(i, 0, 4, NULL, NULL, NULL);
1335 }
1336 }
1337 }
1338 }
1339
1340 // Displays the Welcome Screen
1341 void DisplayWelcomeScreen(int player_num) {
1342 char name_buffer[64];
1343 strcpy(name_buffer, (DMFCBase->GetPlayers())[player_num].callsign);
1344
1345 if (player_num == DMFCBase->GetPlayerNum()) {
1346 DLLAddHUDMessage(TXT_WELCOME, name_buffer);
1347 } else {
1348 DLLAddHUDMessage(TXT_JOINED, name_buffer);
1349 }
1350 }
1351
1352 void SortPlayerScores(int *sortedindex, int size) {
1353 int tempsort[MAX_PLAYER_RECORDS];
1354 int i, t, j;
1355
1356 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
1357 tempsort[i] = i;
1358 }
1359
1360 for (i = 1; i <= MAX_PLAYER_RECORDS - 1; i++) {
1361 t = tempsort[i];
1362 // Shift elements down until
1363 // insertion point found.
1364 for (j = i - 1; j >= 0 && compare_slots(tempsort[j], t); j--) {
1365 tempsort[j + 1] = tempsort[j];
1366 }
1367 // insert
1368 tempsort[j + 1] = t;
1369 }
1370
1371 // copy the array over
1372 memcpy(sortedindex, tempsort, ((size > MAX_PLAYER_RECORDS) ? MAX_PLAYER_RECORDS : size) * sizeof(int));
1373 }
1374
1375 // Generates a valid random room
1376 int GetRandomValidRoom(void) {
1377 while (1) {
1378 int room;
1379 room = rand() % (DMFCBase->GetHighestRoomIndex() + 1);
1380 if ((!ROOMNUM_OUTSIDE(room)) && ((DMFCBase->GetRooms())[room].used)) {
1381 if (!((DMFCBase->GetRooms())[room].flags & RF_EXTERNAL)) {
1382 DLLmprintf(0, "Room %d selected\n", room);
1383 return room;
1384 }
1385 }
1386 }
1387 }
1388
1389 // Moves the HyperOrb to the center of a random room
1390 // if objnum is -1, than the HyperOrb is created and moved
1391 void MoveHyperOrbToRoom(int objnum) {
1392 if ((objnum < 0) || (objnum >= MAX_OBJECTS))
1393 objnum = -1;
1394 else if (dObjects[objnum].type != OBJ_POWERUP)
1395 objnum = -1;
1396
1397 if (objnum == -1) {
1398 // we need to create it and place it somewhere in the level
1399 CreateHyperOrbInRoom(GetRandomValidRoom());
1400 } else {
1401 // we need to just move it
1402 MoveHyperOrbToRoomCenter(objnum, GetRandomValidRoom());
1403 }
1404 }
1405
1406 // Given a room the HyperOrb is created and placed in the center of it
1407 void CreateHyperOrbInRoom(int room) {
1408 if (DMFCBase->GetLocalRole() != LR_SERVER)
1409 return;
1410 vector vpos;
1411 int objnum;
1412 DLLComputeRoomCenter(&vpos, &((DMFCBase->GetRooms())[room]));
1413 objnum = DLLObjCreate(OBJ_POWERUP, HyperOrbID, room, &vpos, NULL, OBJECT_HANDLE_NONE);
1414 DLLMultiSendObject(&dObjects[objnum], 0, true);
1415 }
1416
1417 // Given the objnum and room it will move the HyperOrb to the center of that room. Objnum better be valid.
1418 void MoveHyperOrbToRoomCenter(int objnum, int room) {
1419 vector home_pos;
1420 DLLComputeRoomCenter(&home_pos, &((DMFCBase->GetRooms())[room]));
1421 DLLObjSetPos(&dObjects[objnum], &home_pos, room, NULL, false);
1422 if (DMFCBase->GetLocalRole() == LR_SERVER) { // tell the clients to move
1423 uint8_t data[MAX_GAME_DATA_SIZE];
1424 int count = 0;
1425
1426 DMFCBase->StartPacket(data, SPID_HYPERPOS, &count);
1427 MultiAddInt(room, data, &count);
1428 DMFCBase->SendPacket(data, count, SP_ALL);
1429 }
1430 }
1431
1432 // handles a Hyper Anarchy Object Placement packet
1433 void ReceiveHyperPos(uint8_t *data) {
1434 int count = 0;
1435 int room = MultiGetInt(data, &count);
1436
1437 // first find the Orb's number
1438 int objnum = FindHyperObjectNum();
1439 if (objnum == -1) {
1440 // THIS BETTER NOT HAPPEN!!!!!!!
1441 Int3();
1442 }
1443 MoveHyperOrbToRoomCenter(objnum, room);
1444 }
1445
1446 // Searches through all the objects and looks for the HyperOrb, returns it's objnum. -1 if it doesn't exist
1447 int FindHyperObjectNum(void) {
1448 // go through all the objects until we find it
1449 for (int i = 0; i < MAX_OBJECTS; i++) {
1450 if ((dObjects[i].type == OBJ_POWERUP) && (dObjects[i].id == HyperOrbID)) {
1451 // we found it
1452 return i;
1453 }
1454 }
1455 return -1;
1456 }
1457
1458 // Searches through all the player's inventory, returns the pnum of the player who has the HyperOrb, -1
1459 // if no one does.
1460 int FindHyperOrbInInventory(void) {
1461 // go through everyone's inventory looking for HyperOrb
1462 for (int pnum = 0; pnum < DLLMAX_PLAYERS; pnum++) {
1463 if (DMFCBase->CheckPlayerNum(pnum)) {
1464 if (DLLInvCheckItem(pnum, OBJ_POWERUP, HyperOrbID))
1465 return pnum;
1466 }
1467 }
1468 return -1;
1469 }
1470
1471 void ResetTimer(void) {
1472 if (HyperMoveTimer != -1)
1473 DMFCBase->KillTimer(HyperMoveTimer);
1474 HyperMoveTimer = -1;
1475 }
1476
1477 /////////////////////////////////
1478 // Timer event handlers
1479 void OnTimer(void) { DisplayFlagBlink = !DisplayFlagBlink; }
1480 void OnTimerKill(void) { DisplayFlagBlink = true; }
1481
1482 void OnTimerFlagReturn(void) {
1483 if (DMFCBase->GetLocalRole() != LR_SERVER)
1484 return;
1485 if (WhoHasOrb == -1) {
1486 // time to return move the hyperorb home
1487 int objnum = FindHyperObjectNum();
1488 if (objnum != -1) {
1489 // ok, so it's in some place, lets move it home
1490 MoveHyperOrbToRoom(objnum);
1491 } else {
1492 // did it disappear?, check all the players inventories
1493 WhoHasOrb = FindHyperOrbInInventory();
1494 if (WhoHasOrb == -1) { // no one has it, it's missing!, create it and place it in a room
1495 MoveHyperOrbToRoom(-1);
1496 }
1497 }
1498 }
1499 HyperMoveTimer = -1;
1500 }
1501
1502 void OnTimerFlagReturnKill(void) { HyperMoveTimer = -1; }
1503
1504 void SwitchHUDColor(int i) { HUD_color_model = i; }