Debian Codegraph @

netgames/entropy/EntropyBase.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/entropy/EntropyBase.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003/08/26 03:57:41 $
23 * $Author: kevinb $
24 *
25 * <insert description of file here>
26 *
27 * $Log: EntropyBase.cpp,v $
28 * Revision 1.1.1.1 2003/08/26 03:57:41 kevinb
29 * initial 1.5 import
30 *
31 *
32 * 59 9/24/01 2:28p Matt
33 * Allowed room for longer team name on results screen.
34 *
35 * 58 9/13/01 5:31p Matt
36 * Added a team member list to the stats display.
37 * Fixed a non-terminated string bug.
38 *
39 * 57 10/21/99 9:27p Jeff
40 * B.A. Macintosh code merge
41 *
42 * 56 9/03/99 5:27p Jeff
43 * fixed energy bug (going into an energy center with >100 energy)
44 *
45 * 55 8/17/99 5:53p Jeff
46 * track ranks on PXO
47 *
48 * 54 7/23/99 2:31p Jeff
49 * fixed bug where if the killer is not known, a player would still be
50 * able to carry virus after dying.
51 *
52 * 53 7/15/99 1:18a Jeff
53 * fixed up $scores
54 *
55 * 52 7/15/99 1:01a Jeff
56 * added $scores support
57 *
58 * 51 7/13/99 12:11p Jeff
59 * added some specific text taunt token decoding
60 *
61 * 50 7/12/99 2:27p Jeff
62 * fixed PLR to only display the team label for the disconnected list if
63 * there are people in the list. Play fanfare and print out a message is
64 * someone takes over the last base
65 *
66 * 49 7/11/99 6:54p Jeff
67 * fixed PLR so it doesn't go off the screen on long lists and active
68 * players in the game are shown first
69 *
70 * 48 6/08/99 7:08p Jeff
71 * begining to comment for SDK release
72 *
73 * 47 5/23/99 3:04a Jason
74 * fixed bug with player rankings not being updated correctly
75 *
76 * 46 5/23/99 2:22a Jeff
77 * added countdown. fixed takeover time...off by .5 seconds
78 *
79 * 45 5/22/99 1:12a Jeff
80 * play a sound on the server when picking up a virus
81 *
82 * 44 5/21/99 10:30p Jason
83 * made some rule changes to make more fun
84 *
85 * 43 5/21/99 10:03a Jeff
86 * hopefully filled in any remaining gaps related to have more than you
87 * can carry virus loads
88 *
89 * 42 5/12/99 11:04p Jeff
90 * dmfc and multiplayer games now have endian friendly packets (*whew*)
91 *
92 * 41 5/12/99 11:28a Jeff
93 * when a player scores, dont remove all virii, just what they used
94 *
95 * $NoKeywords: $
96 */
97
98 #include "gamedll_header.h" //included by all mods, it includes all needed headers, etc.
99 #include <string.h>
100 #include "idmfc.h" //dmfc! (required)
101 #include "Entropy.h"
102 #include "Entropystr.h" //our string table for Entropy
103 #include "EntropyAux.h"
104
105 // the DMFC object, used throughout, and required by all mods
106 IDMFC *DMFCBase = NULL;
107 // our DmfcStats object, used for the F7 screen
108 static IDmfcStats *dstat = NULL;
109 // these are some helper pointers, so we don't have to constantly query
110 // DMFC for these arrays, we can query once and just save it for just future use.
111 object *dObjects;
112 static player *dPlayers;
113 room *dRooms;
114
115 ////////////////////////////////////////////////////
116 // Customization flags
117 // These are 'the rules' to Entropy basically
118
119 // scoring
120 #define SCORE_PLYR_TAKEOVER_ROOM 5 // num points for a player when he/she takes over a room
121 #define SCORE_TEAM_WINSGAME 10 // num points for a team when they win a round
122 #define SCORE_TEAM_TAKEOVER_ROOM 3 // num points for a team when a teammate of their's takes over a room
123
124 // rates
125 #define REPAIR_RATE 5.0f // units/sec of the repair room
126 #define ENERGY_RATE 5.0f // units/sec of the energy room
127 #define DAMAGE_RATE 5.0f // units/sec of damage when in an enemy room
128 #define ENERGY_CAP 100.0f // max energy given to a player via an Energy room
129 #define SHIELD_CAP 100.0f // max shields given to a player via a Repair room
130 #define VIRUS_SPEW 20.0f // How long it takes for a lab to spew a virus
131 #define VIRUS_PER_KILL 2.0f // How many virii a player can carry per kill-in-a-row.
132
133 #define MINIMUM_VIRUS_COUNT 5 // Minimum number of virii needed to take over a room
134 #define TAKEOVER_TIME 3.0f // How long a player must stand still to take over a room
135
136 ////////////////////////////////////////////////////
137 struct tPlayerPos {
138 float total_time;
139 vector last_pos;
140 };
141
142 struct tPlayerStat { // Overall scores (throughout the game)
143 int Score[2];
144 };
145 static int pack_pstat(tPlayerStat *user_info, uint8_t *data);
146 static int unpack_pstat(tPlayerStat *user_info, uint8_t *data);
147 int pack_pstat(tPlayerStat *user_info, uint8_t *data) {
148 int count = 0;
149 MultiAddInt(user_info->Score[0], data, &count);
150 MultiAddInt(user_info->Score[1], data, &count);
151 return count;
152 }
153
154 int unpack_pstat(tPlayerStat *user_info, uint8_t *data) {
155 int count = 0;
156 user_info->Score[0] = MultiGetInt(data, &count);
157 user_info->Score[1] = MultiGetInt(data, &count);
158 return count;
159 }
160
161 int TeamOwnedRooms[NUM_TEAMS];
162 static int TeamVirii[NUM_TEAMS][MAX_VIRII];
163 int NumberOfKillsSinceLastDeath[DLLMAX_PLAYERS];
164 static int SortedPLRPlayers[NUM_TEAMS][MAX_PLAYER_RECORDS];
165 int TeamScore[NUM_TEAMS];
166 static int SortedTeams[NUM_TEAMS];
167 static int SortedPlayers[MAX_PLAYER_RECORDS];
168 int EnergyText[NUM_TEAMS];
169 int RepairText[NUM_TEAMS];
170 int LabText[NUM_TEAMS];
171 static tPlayerPos TimeInRoom[DLLMAX_PLAYERS];
172 static bool DisplayScoreScreen;
173 int virus_id = -1;
174 int *RoomList = NULL;
175 int RoomCount;
176 static int Highlight_bmp = -1;
177 static bool display_my_welcome = false;
178 static float Server_last_play_damage_sound = 0;
179 static int Player_who_took_over_last_base = -1;
180
181 static int snd_repair_center = -1;
182 static int snd_energy_center = -1;
183 static int snd_score = -1;
184 static int snd_virus_destroy = -1;
185 static int snd_damage = -1;
186 static int snd_virus_pickup = -1;
187
188 // void DisplayScores(void);
189 static void DisplayHUDScores(struct tHUDItem *hitem);
190 static void DisplayWelcomeMessage(int player_num);
191 static void SortTeamScores(int *sortedindex, int *scores);
192 static void SaveStatsToFile(char *filename);
193 static void OnLabSpewTimer(void);
194 void RemoveVirusFromPlayer(int player_num, bool remove_all);
195 static bool ScanForLaboratory(int team, int *newlab);
196 static bool CompareDistanceTravel(vector *curr_pos, vector *last_pos);
197 static void OnDisconnectSaveStatsToFile(void);
198 static void OnLevelEndSaveStatsToFile(void);
199 static void OnGetTokenString(char *src, char *dest, int dest_size);
200 // Player in special room functions
201 static void DoIntervalPlayerFrame(void);
202 static void DoPlayerInLab(int pnum, float time);
203 static void DoPlayerInEnergy(int pnum, float time);
204 static void DoPlayerInRepair(int pnum, float time);
205 static void DoPlayerInEnemy(int pnum, float time);
206
207 extern void DoServerConfigureDialog(void);
208 static void OnPrintScores(int level);
209
210 ///////////////////////////////////////////////
211 // localization info
212 static char **StringTable;
213 static int StringTableSize = 0;
214 static const char *_ErrorString = "Missing String";
215 const char *GetStringFromTable(int d) {
216 if ((d < 0) || (d >= StringTableSize))
217 return _ErrorString;
218 else
219 return StringTable[d];
220 }
221 ///////////////////////////////////////////////
222
223 // This function gets called by the game when it wants to learn some info about the game
224 void DLLFUNCCALL DLLGetGameInfo(tDLLOptions *options) {
225 options->flags = DOF_MAXTEAMS | DOF_MINTEAMS;
226 options->max_teams = NUM_TEAMS;
227 options->min_teams = NUM_TEAMS;
228 strcpy(options->game_name, TXT_GAMENAME);
229 strcpy(options->requirements, "ENTROPY");
230 }
231
232 void DetermineScore(int precord_num, int column_num, char *buffer, int buffer_size) {
233 player_record *pr = DMFCBase->GetPlayerRecord(precord_num);
234 if (!pr || pr->state == STATE_EMPTY) {
235 buffer[0] = '\0';
236 return;
237 }
238
239 tPlayerStat *stat = (tPlayerStat *)pr->user_info;
240 snprintf(buffer, buffer_size, "%d[%d]", (stat) ? stat->Score[DSTAT_LEVEL] : 0,
241 (stat) ? stat->Score[DSTAT_OVERALL] : 0);
242 }
243
244 void TeamScoreCallback(int team, char *buffer, int buffer_size) {
245 ASSERT(team >= 0 && team < NUM_TEAMS);
246
247 snprintf(buffer, buffer_size, " %d", TeamScore[team]);
248 }
249
250 // Initializes the game function pointers
251 void DLLFUNCCALL DLLGameInit(int *api_func, uint8_t *all_ok, int num_teams_to_use) {
252 *all_ok = 1;
253 DMFCBase = CreateDMFC();
254 if (!DMFCBase) {
255 *all_ok = 0;
256 return;
257 }
258
259 dstat = CreateDmfcStats();
260 if (!dstat) {
261 *all_ok = 0;
262 return;
263 }
264
265 DMFCBase->LoadFunctions(api_func);
266
267 // Setup event handlers
268 DMFCBase->Set_OnGameStateRequest(OnGameStateRequest);
269 DMFCBase->Set_OnClientPlayerKilled(OnClientPlayerKilled);
270 DMFCBase->Set_OnServerGameCreated(OnServerGameCreated);
271 DMFCBase->Set_OnClientLevelStart(OnClientLevelStart);
272 DMFCBase->Set_OnClientLevelEnd(OnClientLevelEnd);
273 DMFCBase->Set_OnClientPlayerEntersGame(OnClientPlayerEntersGame);
274 DMFCBase->Set_OnInterval(OnInterval);
275 DMFCBase->Set_OnHUDInterval(OnHUDInterval);
276 DMFCBase->Set_OnKeypress(OnKeypress);
277 DMFCBase->Set_OnPLRInterval(OnPLRInterval);
278 DMFCBase->Set_OnPLRInit(OnPLRInit);
279 DMFCBase->Set_OnSaveStatsToFile(OnSaveStatsToFile);
280 DMFCBase->Set_OnLevelEndSaveStatsToFile(OnLevelEndSaveStatsToFile);
281 DMFCBase->Set_OnDisconnectSaveStatsToFile(OnDisconnectSaveStatsToFile);
282 DMFCBase->Set_OnClientPlayerDisconnect(OnClientPlayerDisconnect);
283 DMFCBase->Set_OnServerPlayerChangeSegment(OnServerPlayerChangeSegment);
284 DMFCBase->Set_OnClientPlayerChangeSegment(OnClientPlayerChangeSegment);
285 DMFCBase->Set_OnServerCollide(OnServerCollide);
286 DMFCBase->Set_OnControlMessage(OnControlMessage);
287 DMFCBase->Set_OnClientShowUI(OnClientShowUI);
288 DMFCBase->Set_OnGetTokenString(OnGetTokenString);
289 DMFCBase->Set_OnPrintScores(OnPrintScores);
290
291 // Setup arrays for easier to read code
292 dObjects = DMFCBase->GetObjects();
293 dPlayers = DMFCBase->GetPlayers();
294 dRooms = DMFCBase->GetRooms();
295
296 netgame_info *Netgame = DMFCBase->GetNetgameInfo();
297 Netgame->flags |= (NF_TRACK_RANK);
298
299 DMFCBase->GameInit(NUM_TEAMS);
300 DLLCreateStringTable("entropy.str", &StringTable, &StringTableSize);
301 DLLmprintf(0, "%d strings loaded from string table\n", StringTableSize);
302 if (!StringTableSize) {
303 *all_ok = 0;
304 return;
305 }
306
307 // add the death and suicide messages
308 DMFCBase->AddDeathMessage(TXT_KILLA, true);
309
310 DMFCBase->AddSuicideMessage(TXT_SUICIDEA);
311
312 // register special packet receivers
313 DMFCBase->RegisterPacketReceiver(SPID_NEWPLAYER, GetGameStartPacket);
314 DMFCBase->RegisterPacketReceiver(SPID_TAKEOVER, ReceiveTakeOverPacket);
315 DMFCBase->RegisterPacketReceiver(SPID_ROOMINFO, ReceiveRoomInfo);
316 DMFCBase->RegisterPacketReceiver(SPID_PICKUPVIRUS, ReceivePickupVirus);
317 DMFCBase->RegisterPacketReceiver(SPID_VIRUSCREATE, DoVirusCreate);
318
319 DMFCBase->SetNumberOfTeams(NUM_TEAMS);
320
321 DMFCBase->AddHUDItemCallback(HI_TEXT, DisplayHUDScores);
322
323 EnergyText[RED_TEAM] = DLLFindTextureName("RedEnergy");
324 EnergyText[BLUE_TEAM] = DLLFindTextureName("BlueEnergy");
325 RepairText[RED_TEAM] = DLLFindTextureName("RedRepair");
326 RepairText[BLUE_TEAM] = DLLFindTextureName("BlueRepair");
327 LabText[RED_TEAM] = DLLFindTextureName("RedLab");
328 LabText[BLUE_TEAM] = DLLFindTextureName("BlueLab");
329
330 snd_repair_center = DLLFindSoundName("EntropyRepair");
331 snd_energy_center = DLLFindSoundName("EntropyEnergy");
332 snd_score = DLLFindSoundName("EntropyScore");
333 snd_virus_destroy = DLLFindSoundName("EntropyDestroy");
334 snd_damage = DLLFindSoundName("HitEnergy");
335 snd_virus_pickup = DLLFindSoundName("Powerup pickup");
336
337 DMFCBase->SetupPlayerRecord(sizeof(tPlayerStat), (int (*)(void *, uint8_t *))pack_pstat,
338 (int (*)(void *, uint8_t *))unpack_pstat);
339
340 virus_id = DLLFindObjectIDName("EntropyVirus");
341
342 DisplayScoreScreen = false;
343
344 Highlight_bmp = DLLbm_AllocBitmap(32, 32, 0);
345 if (Highlight_bmp > BAD_BITMAP_HANDLE) {
346 uint16_t *data = DLLbm_data(Highlight_bmp, 0);
347 if (!data) {
348 // bail on out of here
349 *all_ok = 0;
350 return;
351 }
352 for (int x = 0; x < 32 * 32; x++) {
353 data[x] = GR_RGB16(50, 50, 50) | OPAQUE_FLAG;
354 }
355 }
356
357 // Initialize the Stats Manager
358 // ----------------------------
359 tDmfcStatsInit tsi;
360 tDmfcStatsColumnInfo pl_col[6];
361 char gname[20];
362 strcpy(gname, TXT_STATSGAMENAME);
363
364 tsi.flags = DSIF_SHOW_PIC | DSIF_SHOW_OBSERVERICON | DSIF_SEPERATE_BY_TEAM;
365 tsi.cColumnCountDetailed = 0;
366 tsi.cColumnCountPlayerList = 6;
367 tsi.clbDetailedColumnBMP = NULL;
368 tsi.clbDetailedColumn = NULL;
369 tsi.clbPlayerColumn = DetermineScore;
370 tsi.clbPlayerColumnBMP = NULL;
371 tsi.DetailedColumns = NULL;
372 tsi.GameName = gname;
373 tsi.MaxNumberDisplayed = NULL;
374 tsi.PlayerListColumns = pl_col;
375 tsi.SortedPlayerRecords = SortedPlayers;
376 tsi.clTeamLine = TeamScoreCallback;
377
378 pl_col[0].color_type = DSCOLOR_TEAM;
379 strcpy(pl_col[0].title, TXT_PILOT);
380 pl_col[0].type = DSCOL_PILOT_NAME;
381 pl_col[0].width = 120;
382
383 pl_col[1].color_type = DSCOLOR_TEAM;
384 strcpy(pl_col[1].title, TXT_SCORE);
385 pl_col[1].type = DSCOL_CUSTOM;
386 pl_col[1].width = 47;
387
388 pl_col[2].color_type = DSCOLOR_TEAM;
389 strcpy(pl_col[2].title, TXT_KILLS);
390 pl_col[2].type = DSCOL_KILLS_LEVEL;
391 pl_col[2].width = 47;
392
393 pl_col[3].color_type = DSCOLOR_TEAM;
394 strcpy(pl_col[3].title, TXT_DEATHS);
395 pl_col[3].type = DSCOL_DEATHS_LEVEL;
396 pl_col[3].width = 57;
397
398 pl_col[4].color_type = DSCOLOR_TEAM;
399 strcpy(pl_col[4].title, TXT_SUICIDES);
400 pl_col[4].type = DSCOL_SUICIDES_LEVEL;
401 pl_col[4].width = 62;
402
403 pl_col[5].color_type = DSCOLOR_TEAM;
404 strcpy(pl_col[5].title, TXT_PING);
405 pl_col[5].type = DSCOL_PING;
406 pl_col[5].width = 40;
407
408 dstat->Initialize(&tsi);
409 }
410 // Called when the DLL is shutdown
411 void DLLFUNCCALL DLLGameClose() {
412 if (Highlight_bmp > BAD_BITMAP_HANDLE)
413 DLLbm_FreeBitmap(Highlight_bmp);
414
415 if (RoomList) {
416 free(RoomList);
417 RoomList = NULL;
418 }
419
420 DLLDestroyStringTable(StringTable, StringTableSize);
421
422 if (dstat) {
423 dstat->DestroyPointer();
424 dstat = NULL;
425 }
426
427 if (DMFCBase) {
428 DMFCBase->GameClose();
429 DMFCBase->DestroyPointer();
430 DMFCBase = NULL;
431 }
432 }
433
434 // The server has just started, so clear out all the stats and game info
435 void OnServerGameCreated(void) {
436 DMFCBase->OnServerGameCreated();
437
438 tPlayerStat *stat;
439 player_record *pr;
440 int i;
441
442 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
443 pr = DMFCBase->GetPlayerRecord(i);
444 if (pr) {
445 stat = (tPlayerStat *)pr->user_info;
446 if (stat) {
447 stat->Score[DSTAT_LEVEL] = 0;
448 stat->Score[DSTAT_OVERALL] = 0;
449 }
450 }
451 }
452 for (i = 0; i < NUM_TEAMS; i++) {
453 TeamScore[i] = 0;
454 }
455 }
456
457 // The server has started a new level, so clear out level scores
458 void OnClientLevelStart(void) {
459 DMFCBase->OnClientLevelStart();
460 int i;
461
462 for (i = 0; i < NUM_TEAMS; i++) {
463 TeamOwnedRooms[i] = 0;
464 for (int v = 0; v < MAX_VIRII; v++)
465 TeamVirii[i][v] = -1;
466 }
467
468 tPlayerStat *stat;
469 player_record *pr;
470 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
471 pr = DMFCBase->GetPlayerRecord(i);
472 if (pr) {
473 stat = (tPlayerStat *)pr->user_info;
474 if (stat) {
475 stat->Score[DSTAT_LEVEL] = 0;
476 }
477 }
478 }
479
480 // reset damage timer
481 Server_last_play_damage_sound = 0;
482
483 // reset the id to -1, so we are ready for the new level
484 Player_who_took_over_last_base = -1;
485
486 if (DMFCBase->GetLocalRole() == LR_SERVER) {
487 DMFCBase->SetTimerInterval(OnLabSpewTimer, VIRUS_SPEW, -1.0f);
488 }
489
490 if (RoomList) {
491 free(RoomList);
492 RoomList = NULL;
493 }
494 RoomCount = 0;
495 int r;
496 for (r = 0; r <= DMFCBase->GetHighestRoomIndex(); r++) {
497 if (dRooms[r].used) {
498 int flags = dRooms[r].flags;
499 if (flags & RF_SPECIAL1 || flags & RF_SPECIAL2 || flags & RF_SPECIAL3 || flags & RF_SPECIAL4 ||
500 flags & RF_SPECIAL5 || flags & RF_SPECIAL6) {
501 RoomCount++;
502 }
503 }
504 }
505
506 if (RoomCount == 0) {
507 FatalError("No Special Rooms Defined");
508 return;
509 }
510
511 RoomList = (int *)malloc(sizeof(int) * RoomCount);
512 if (!RoomList) {
513 FatalError("Out of memory");
514 return;
515 }
516
517 bool room_used_already;
518 int special_room_index = 0;
519
520 for (r = 0; r <= DMFCBase->GetHighestRoomIndex(); r++) {
521 if (dRooms[r].used) {
522 if (dRooms[r].flags & RF_FUELCEN) {
523 dRooms[r].flags &= ~RF_FUELCEN;
524 dRooms[r].room_change_flags |= RCF_GOALSPECIAL_FLAGS;
525 }
526
527 room_used_already = false;
528 if (dRooms[r].flags & RF_SPECIAL1) { // Red Team Laboratory
529 TeamOwnedRooms[RED_TEAM]++;
530 room_used_already = true;
531 PaintRoomWithTexture(LabText[RED_TEAM], r);
532 RoomList[special_room_index] = r;
533 special_room_index++;
534 }
535 if (dRooms[r].flags & RF_SPECIAL4) { // Blue Team Laboratory
536 if (room_used_already) {
537 dRooms[r].flags &= ~RF_SPECIAL4;
538 dRooms[r].room_change_flags |= RCF_GOALSPECIAL_FLAGS;
539 } else {
540 TeamOwnedRooms[BLUE_TEAM]++;
541 room_used_already = true;
542 PaintRoomWithTexture(LabText[BLUE_TEAM], r);
543 RoomList[special_room_index] = r;
544 special_room_index++;
545 }
546 }
547 if (dRooms[r].flags & RF_SPECIAL2) { // Red Team Energy Room
548 if (room_used_already) {
549 dRooms[r].flags &= ~RF_SPECIAL2;
550 dRooms[r].room_change_flags |= RCF_GOALSPECIAL_FLAGS;
551 } else {
552 TeamOwnedRooms[RED_TEAM]++;
553 room_used_already = true;
554 PaintRoomWithTexture(EnergyText[RED_TEAM], r);
555 RoomList[special_room_index] = r;
556 special_room_index++;
557 }
558 }
559 if (dRooms[r].flags & RF_SPECIAL5) { // Blue Team Energy Room
560 if (room_used_already) {
561 dRooms[r].flags &= ~RF_SPECIAL5;
562 dRooms[r].room_change_flags |= RCF_GOALSPECIAL_FLAGS;
563 } else {
564 TeamOwnedRooms[BLUE_TEAM]++;
565 room_used_already = true;
566 PaintRoomWithTexture(EnergyText[BLUE_TEAM], r);
567 RoomList[special_room_index] = r;
568 special_room_index++;
569 }
570 }
571 if (dRooms[r].flags & RF_SPECIAL3) { // Red Team Repair Room
572 if (room_used_already) {
573 dRooms[r].flags &= ~RF_SPECIAL3;
574 dRooms[r].room_change_flags |= RCF_GOALSPECIAL_FLAGS;
575 } else {
576 TeamOwnedRooms[RED_TEAM]++;
577 room_used_already = true;
578 PaintRoomWithTexture(RepairText[RED_TEAM], r);
579 RoomList[special_room_index] = r;
580 special_room_index++;
581 }
582 }
583 if (dRooms[r].flags & RF_SPECIAL6) { // Blue Team Repair Room
584 if (room_used_already) {
585 dRooms[r].flags &= ~RF_SPECIAL6;
586 dRooms[r].room_change_flags |= RCF_GOALSPECIAL_FLAGS;
587 } else {
588 TeamOwnedRooms[BLUE_TEAM]++;
589 room_used_already = true;
590 PaintRoomWithTexture(RepairText[BLUE_TEAM], r);
591 RoomList[special_room_index] = r;
592 special_room_index++;
593 }
594 }
595 }
596 }
597
598 // zero out other arrays
599 for (i = 0; i < DLLMAX_PLAYERS; i++) {
600 NumberOfKillsSinceLastDeath[i] = 0;
601 TimeInRoom[i].total_time = 0;
602 RemoveVirusFromPlayer(i, true);
603 }
604
605 if (DMFCBase->GetLocalRole() == LR_SERVER) {
606 // spew 3 virii in each lab
607 for (i = 0; i < 3; i++)
608 OnLabSpewTimer();
609 }
610
611 DMFCBase->RequestGameState();
612 }
613
614 void OnClientLevelEnd(void) { DMFCBase->OnClientLevelEnd(); }
615
616 // A New Player has entered the game, so we want to send him a game status packet that
617 // has information about the game
618 void OnGameStateRequest(int player_num) {
619 SendGameStartPacket(player_num);
620 DMFCBase->OnGameStateRequest(player_num);
621 }
622
623 // A new player has entered the game, zero there stats out
624 void OnClientPlayerEntersGame(int player_num) {
625 DMFCBase->OnClientPlayerEntersGame(player_num);
626 if (player_num != DMFCBase->GetPlayerNum())
627 DisplayWelcomeMessage(player_num);
628 else
629 display_my_welcome = true;
630
631 if (player_num != -1) {
632 NumberOfKillsSinceLastDeath[player_num] = 0;
633 }
634 TimeInRoom[player_num].total_time = 0;
635 RemoveVirusFromPlayer(player_num, true);
636 }
637
638 void OnServerPlayerChangeSegment(int player_num, int newseg, int oldseg) {
639 if (player_num == -1) {
640 DMFCBase->OnServerPlayerChangeSegment(player_num, newseg, oldseg);
641 return;
642 }
643
644 // check to make sure it is an 'indoor' room and not terrain
645 if (!ROOMNUM_OUTSIDE(newseg)) {
646 // Check the room flags, see if we entered a special room
647 room *rp = &dRooms[newseg];
648 if (rp->flags & RF_EXTERNAL) {
649 DMFCBase->OnServerPlayerChangeSegment(player_num, newseg, oldseg);
650 return;
651 }
652
653 if ((rp->flags & RF_SPECIAL1) || (rp->flags & RF_SPECIAL2) || (rp->flags & RF_SPECIAL3) ||
654 (rp->flags & RF_SPECIAL4) || (rp->flags & RF_SPECIAL5) || (rp->flags & RF_SPECIAL6)) {
655 DMFCBase->CallClientEvent(EVT_CLIENT_GAMEPLAYERCHANGESEG, DMFCBase->GetMeObjNum(), DMFCBase->GetItObjNum(), -1);
656 DMFCBase->CallOnClientPlayerChangeSegment(player_num, newseg, oldseg);
657 }
658 }
659
660 DMFCBase->OnServerPlayerChangeSegment(player_num, newseg, oldseg);
661 }
662
663 void OnClientPlayerChangeSegment(int player_num, int newseg, int oldseg) {
664 // someone has entered a special room
665 TimeInRoom[player_num].total_time = 0;
666
667 DMFCBase->OnClientPlayerChangeSegment(player_num, newseg, oldseg);
668 }
669
670 // We need to adjust the scores
671 void OnClientPlayerKilled(object *killer_obj, int victim_pnum) {
672 int kpnum;
673
674 if (victim_pnum != -1)
675 RemoveVirusFromPlayer(victim_pnum, true);
676
677 if (killer_obj) {
678 if ((killer_obj->type == OBJ_PLAYER) || (killer_obj->type == OBJ_GHOST))
679 kpnum = killer_obj->id;
680 else if (killer_obj->type == OBJ_ROBOT || (killer_obj->type == OBJ_BUILDING && killer_obj->ai_info)) {
681 // countermeasure kill
682 kpnum = DMFCBase->GetCounterMeasureOwner(killer_obj);
683 } else {
684 kpnum = -1;
685 }
686 } else
687 kpnum = -1;
688
689 if (kpnum != -1) {
690 if (kpnum == victim_pnum) {
691 NumberOfKillsSinceLastDeath[kpnum] = 0;
692 } else {
693 NumberOfKillsSinceLastDeath[kpnum]++;
694
695 if (victim_pnum != -1)
696 NumberOfKillsSinceLastDeath[victim_pnum] = 0;
697 }
698 } else {
699 // this would be the case if the room killed a player
700 if (victim_pnum != -1)
701 NumberOfKillsSinceLastDeath[victim_pnum] = 0;
702 }
703
704 // TODO: Handle a kill if killed by a room
705 DMFCBase->OnClientPlayerKilled(killer_obj, victim_pnum);
706 }
707
708 void OnHUDInterval(void) {
709 dstat->DoFrame();
710 DMFCBase->DisplayOutrageLogo();
711
712 DMFCBase->OnHUDInterval();
713 }
714
715 void OnInterval(void) {
716 SortTeamScores(SortedTeams, TeamScore);
717 // TODO: Correct Sorting based on score
718 DMFCBase->GetSortedPlayerSlots(SortedPlayers, MAX_PLAYER_RECORDS);
719 DoIntervalPlayerFrame();
720 DMFCBase->OnInterval();
721 }
722
723 void OnClientPlayerDisconnect(int pnum) {
724 if (pnum != -1) {
725 NumberOfKillsSinceLastDeath[pnum] = 0;
726 }
727 DMFCBase->OnClientPlayerDisconnect(pnum);
728 }
729
730 void OnKeypress(int key) {
731 dllinfo *Data = DMFCBase->GetDLLInfoCallData();
732 switch (key) {
733 case K_F7:
734 DisplayScoreScreen = !DisplayScoreScreen;
735 DMFCBase->EnableOnScreenMenu(false);
736 dstat->Enable(DisplayScoreScreen);
737 break;
738 case K_PAGEDOWN:
739 if (DisplayScoreScreen) {
740 dstat->ScrollDown();
741 Data->iRet = 1;
742 }
743 break;
744 case K_PAGEUP:
745 if (DisplayScoreScreen) {
746 dstat->ScrollUp();
747 Data->iRet = 1;
748 }
749 break;
750 case K_F6:
751 DisplayScoreScreen = false;
752 dstat->Enable(false);
753 break;
754 case K_ESC:
755 if (DisplayScoreScreen) {
756 dstat->Enable(false);
757 DisplayScoreScreen = false;
758 Data->iRet = 1;
759 }
760 break;
761 }
762
763 // if(key==K_E && DMFCBase->GetLocalRole()==LR_SERVER){
764 // DMFCBase->StartUIWindow(1);
765 //}
766
767 DMFCBase->OnKeypress(key);
768 }
769
770 void OnClientShowUI(int id, void *user_data) {
771 if (id == 1)
772 DoServerConfigureDialog();
773 DMFCBase->OnClientShowUI(id, user_data);
774 }
775
776 void OnServerCollide(object *me_obj, object *it_obj) {
777 if (!me_obj || !it_obj)
778 return;
779
780 if (virus_id == -1) {
781 FatalError("Unable to find Virus Object");
782 return;
783 }
784
785 if ((me_obj->type == OBJ_PLAYER) && (it_obj->type == OBJ_POWERUP) && (it_obj->id == virus_id)) {
786 // determine what to do with this collision
787 int virus_objnum = it_obj - dObjects;
788 int virus_team = -1;
789 int virus_index = -1;
790 int player_num = me_obj->id;
791
792 // find which team the virus belongs to
793 for (int v = 0; v < MAX_VIRII; v++) {
794 if (virus_objnum == TeamVirii[RED_TEAM][v]) {
795 virus_team = RED_TEAM;
796 virus_index = v;
797 break;
798 }
799 if (virus_objnum == TeamVirii[BLUE_TEAM][v]) {
800 virus_team = BLUE_TEAM;
801 virus_index = v;
802 break;
803 }
804 }
805 if (virus_team == -1) {
806 // hey! we hit a virus that doesn't belong to any team!!
807 DLLmprintf(0, "Virus (%d) doesn't belong to any team, removing...\n", virus_objnum);
808 DMFCBase->OnServerCollide(me_obj, it_obj);
809 DLLSetObjectDeadFlag(it_obj, true, false);
810 return;
811 }
812
813 int curr_count = DLLInvGetTypeIDCount(player_num, OBJ_POWERUP, it_obj->id);
814 if (dPlayers[player_num].team == virus_team) {
815 // a player has collided with a virus from his team
816 // see if the player has enough kills to pick up this virus
817 int max_carry = NumberOfKillsSinceLastDeath[player_num] * VIRUS_PER_KILL;
818 if (curr_count + 1 > max_carry) {
819 // the player can't carry another virii yet
820 if (player_num == DMFCBase->GetPlayerNum()) {
821 DLLAddHUDMessage(TXT_CANTCARRY);
822 } else {
823 DMFCBase->SendControlMessageToPlayer(player_num, VIRUS_NOTENOUGHKILLS);
824 }
825 } else {
826 // pick this guy up
827 TeamVirii[virus_team][virus_index] = -1;
828 SendClientPickupVirus(player_num);
829 DLLSetObjectDeadFlag(it_obj, true, true);
830 if (snd_virus_pickup != -1) {
831 DLLPlay2dSound(snd_virus_pickup, MAX_GAME_VOLUME);
832 }
833 }
834 } else {
835 // a player has collided with a virus from another team
836 // kill this object
837 TeamVirii[virus_team][virus_index] = -1;
838 DLLSetObjectDeadFlag(it_obj, true, false);
839 if (player_num == DMFCBase->GetPlayerNum()) {
840 DLLAddHUDMessage(TXT_VIRUSDESTROYED);
841 if (snd_virus_destroy != -1) {
842 DLLPlay2dSound(snd_virus_destroy, MAX_GAME_VOLUME);
843 }
844 } else {
845 DMFCBase->SendControlMessageToPlayer(player_num, VIRUS_DESTROYED);
846 }
847 }
848 }
849
850 DMFCBase->OnServerCollide(me_obj, it_obj);
851 }
852
853 void TakeOverRoom(int newteam, int oldteam, int roomnum, int victor) {
854 if (ROOMNUM_OUTSIDE(roomnum)) {
855 Int3();
856 return;
857 }
858
859 bool server = false;
860 bool success = false;
861 if (DMFCBase->GetLocalRole() == LR_SERVER)
862 server = true;
863
864 TeamOwnedRooms[newteam]++;
865 TeamOwnedRooms[oldteam]--;
866
867 tPlayerStat *stat = (tPlayerStat *)DMFCBase->GetPlayerRecordData(victor);
868 if (stat) {
869 stat->Score[DSTAT_LEVEL] += SCORE_PLYR_TAKEOVER_ROOM;
870 stat->Score[DSTAT_OVERALL] += SCORE_PLYR_TAKEOVER_ROOM;
871 TeamScore[DMFCBase->GetPlayerTeam(victor)] += SCORE_TEAM_TAKEOVER_ROOM;
872
873 if (snd_score != -1) {
874 DLLPlay2dSound(snd_score, MAX_GAME_VOLUME);
875 }
876 }
877
878 char room_buf[40];
879
880 room *rp = &dRooms[roomnum];
881
882 if (oldteam == RED_TEAM) {
883 if (rp->flags & RF_SPECIAL1) {
884 rp->flags &= ~RF_SPECIAL1;
885 rp->flags |= RF_SPECIAL4;
886 rp->room_change_flags |= RCF_GOALSPECIAL_FLAGS;
887 PaintRoomWithTexture(LabText[BLUE_TEAM], roomnum);
888 if (server)
889 SendTakeOverPacket(newteam, oldteam, roomnum, victor);
890 strcpy(room_buf, "Laboratory");
891 success = true;
892 }
893 if (rp->flags & RF_SPECIAL2) {
894 rp->flags &= ~RF_SPECIAL2;
895 rp->flags |= RF_SPECIAL5;
896 rp->room_change_flags |= RCF_GOALSPECIAL_FLAGS;
897 PaintRoomWithTexture(EnergyText[BLUE_TEAM], roomnum);
898 if (server)
899 SendTakeOverPacket(newteam, oldteam, roomnum, victor);
900 strcpy(room_buf, "Energy");
901 success = true;
902 }
903 if (rp->flags & RF_SPECIAL3) {
904 rp->flags &= ~RF_SPECIAL3;
905 rp->flags |= RF_SPECIAL6;
906 rp->room_change_flags |= RCF_GOALSPECIAL_FLAGS;
907 PaintRoomWithTexture(RepairText[BLUE_TEAM], roomnum);
908 if (server)
909 SendTakeOverPacket(newteam, oldteam, roomnum, victor);
910 strcpy(room_buf, "Repair");
911 success = true;
912 }
913 } else {
914 if (rp->flags & RF_SPECIAL4) {
915 rp->flags &= ~RF_SPECIAL4;
916 rp->flags |= RF_SPECIAL1;
917 rp->room_change_flags |= RCF_GOALSPECIAL_FLAGS;
918 PaintRoomWithTexture(LabText[RED_TEAM], roomnum);
919 if (server)
920 SendTakeOverPacket(newteam, oldteam, roomnum, victor);
921 strcpy(room_buf, "Laboratory");
922 success = true;
923 }
924 if (rp->flags & RF_SPECIAL5) {
925 rp->flags &= ~RF_SPECIAL5;
926 rp->flags |= RF_SPECIAL2;
927 rp->room_change_flags |= RCF_GOALSPECIAL_FLAGS;
928 PaintRoomWithTexture(EnergyText[RED_TEAM], roomnum);
929 if (server)
930 SendTakeOverPacket(newteam, oldteam, roomnum, victor);
931 strcpy(room_buf, "Energy");
932 success = true;
933 }
934 if (rp->flags & RF_SPECIAL6) {
935 rp->flags &= ~RF_SPECIAL6;
936 rp->flags |= RF_SPECIAL3;
937 rp->room_change_flags |= RCF_GOALSPECIAL_FLAGS;
938 PaintRoomWithTexture(RepairText[RED_TEAM], roomnum);
939 if (server)
940 SendTakeOverPacket(newteam, oldteam, roomnum, victor);
941 strcpy(room_buf, "Repair");
942 success = true;
943 }
944 }
945
946 if (!success) {
947 DLLmprintf(0, "Invalid Takeover!!!!!!!\n");
948 return;
949 }
950
951 // print out hud message (and sound?)
952 char buffer[256];
953 DLLmprintf(0, "old=%d new=%d\n", oldteam, newteam);
954 snprintf(buffer, sizeof(buffer), TXT_TAKEOVER, (victor != -1) ? dPlayers[victor].callsign : TXT_NONAME,
955 DMFCBase->GetTeamString(oldteam), room_buf);
956 DLLAddHUDMessage(buffer);
957 if (victor != -1)
958 RemoveVirusFromPlayer(victor, false);
959
960 if (TeamOwnedRooms[oldteam] <= 0) {
961 TeamScore[newteam] += SCORE_TEAM_WINSGAME;
962 Player_who_took_over_last_base = victor;
963 if (server) {
964 DMFCBase->EndLevel();
965 }
966 }
967
968 int newlab;
969 if (!ScanForLaboratory(oldteam, &newlab)) {
970 // we need to turn newlab into a new laboratory
971 int flag = 0;
972 room *rp = &dRooms[newlab];
973 if (oldteam == RED_TEAM) {
974 rp->flags &= ~RF_SPECIAL2;
975 rp->flags &= ~RF_SPECIAL3;
976 flag = RF_SPECIAL1;
977 } else {
978 rp->flags &= ~RF_SPECIAL5;
979 rp->flags &= ~RF_SPECIAL6;
980 flag = RF_SPECIAL4;
981 }
982 rp->flags |= flag;
983 rp->room_change_flags |= RCF_GOALSPECIAL_FLAGS;
984 PaintRoomWithTexture(LabText[oldteam], newlab);
985 }
986 }
987
988 void DoPlayerInLab(int pnum, float time) {
989 // nothing to do in here
990 }
991
992 void DoPlayerInEnergy(int pnum, float time) {
993 // restore player's energy
994 float amount = REPAIR_RATE * time;
995
996 if (dPlayers[pnum].energy >= ENERGY_CAP)
997 return;
998
999 if (dPlayers[pnum].energy + amount > ENERGY_CAP)
1000 dPlayers[pnum].energy = ENERGY_CAP;
1001 else
1002 dPlayers[pnum].energy += amount;
1003
1004 if (dPlayers[pnum].energy < ENERGY_CAP && pnum == DMFCBase->GetPlayerNum() && snd_energy_center >= 0) {
1005 DLLPlay2dSound(snd_energy_center, 1);
1006 }
1007 }
1008
1009 void DoPlayerInRepair(int pnum, float time) {
1010 // restore player's shields, give at a rate of 3 per second
1011 float amount = REPAIR_RATE * time;
1012 float curr_shields = dObjects[dPlayers[pnum].objnum].shields;
1013 float max_amount = SHIELD_CAP - curr_shields;
1014 if (amount > max_amount)
1015 amount = max_amount;
1016 if (curr_shields >= SHIELD_CAP)
1017 return;
1018
1019 DMFCBase->DoDamageToPlayer(pnum, PD_ENERGY_WEAPON, -amount);
1020
1021 if (amount > 0 && pnum == DMFCBase->GetPlayerNum() && snd_repair_center >= 0) {
1022 DLLPlay2dSound(snd_repair_center, MAX_GAME_VOLUME * 0.75f);
1023 }
1024 }
1025
1026 void DoPlayerInEnemy(int pnum, float time) {
1027 float damage_amount = DAMAGE_RATE * time;
1028
1029 if (dPlayers[pnum].flags & PLAYER_FLAGS_DYING || dPlayers[pnum].flags & PLAYER_FLAGS_DEAD)
1030 return;
1031
1032 // only play sound if player is a client, else do our own sound
1033 // so it isn't framerate dependant
1034 if (DMFCBase->GetLocalRole() == LR_SERVER) {
1035 if (pnum != DMFCBase->GetPlayerNum()) {
1036 DMFCBase->DoDamageToPlayer(pnum, PD_ENERGY_WEAPON, damage_amount, true);
1037 } else {
1038 DMFCBase->DoDamageToPlayer(pnum, PD_ENERGY_WEAPON, damage_amount, false);
1039 // time to play sound?
1040 float gt = DMFCBase->GetGametime();
1041 if (gt - Server_last_play_damage_sound > (1.0f / 5.0f)) // fake 5pps
1042 {
1043 // time to play sound
1044 Server_last_play_damage_sound = gt;
1045 if (snd_damage != -1) {
1046 DLLPlay2dSound(snd_damage, 1);
1047 }
1048 }
1049 }
1050 }
1051
1052 // check to see if the player has 5 virii in his inventory, if he does, he's
1053 // trying to take over this room
1054 if (DLLInvGetTypeIDCount(pnum, OBJ_POWERUP, virus_id) < MINIMUM_VIRUS_COUNT)
1055 return;
1056
1057 if (TimeInRoom[pnum].total_time == 0) {
1058 // update player position
1059 memcpy(&TimeInRoom[pnum].last_pos, &dObjects[dPlayers[pnum].objnum].pos, sizeof(vector));
1060 }
1061
1062 vector curr_pos;
1063 memcpy(&curr_pos, &dObjects[dPlayers[pnum].objnum].pos, sizeof(vector));
1064
1065 if (!CompareDistanceTravel(&curr_pos, &TimeInRoom[pnum].last_pos)) {
1066 // the player has moved!!!
1067 TimeInRoom[pnum].total_time = 0;
1068 } else {
1069 // still standing still
1070 TimeInRoom[pnum].total_time += time;
1071 }
1072
1073 float time_remaining = (TAKEOVER_TIME - TimeInRoom[pnum].total_time);
1074
1075 if (time_remaining <= 0) {
1076 // Take over the room!!!!
1077 if (DMFCBase->GetLocalRole() == LR_SERVER) {
1078 int newteam = DMFCBase->GetPlayerTeam(pnum);
1079 int oldteam = (newteam == RED_TEAM) ? BLUE_TEAM : RED_TEAM;
1080 int room = dObjects[dPlayers[pnum].objnum].roomnum;
1081 TakeOverRoom(newteam, oldteam, room, pnum);
1082 }
1083 }
1084 }
1085
1086 void DoIntervalPlayerFrame(void) {
1087 static float last_frametime = 0;
1088 static bool update_this_frame = false;
1089 float frametime = 0;
1090
1091 if (update_this_frame) {
1092 frametime = last_frametime + DMFCBase->GetFrametime();
1093 last_frametime = 0;
1094 update_this_frame = false;
1095 } else {
1096 last_frametime = DMFCBase->GetFrametime();
1097 update_this_frame = true;
1098 return;
1099 }
1100
1101 for (int i = 0; i < DLLMAX_PLAYERS; i++) {
1102 if (DMFCBase->CheckPlayerNum(i)) {
1103 if (i == DMFCBase->GetPlayerNum() || DMFCBase->GetLocalRole() == LR_SERVER) {
1104
1105 if (dObjects[dPlayers[i].objnum].type != OBJ_PLAYER)
1106 continue; // only work with OBJ_PLAYERS...this automatically handles observers,dedicated server
1107
1108 // only need to be here if we are the player that has to do with this
1109 // or we are the server
1110
1111 // first check to see if this player is in a special room
1112 if (ROOMNUM_OUTSIDE(dObjects[dPlayers[i].objnum].roomnum))
1113 continue;
1114
1115 int team = DMFCBase->GetPlayerTeam(i);
1116 room *rp = &dRooms[dObjects[dPlayers[i].objnum].roomnum];
1117 if (rp->flags & RF_SPECIAL1) {
1118 if (team == RED_TEAM)
1119 DoPlayerInLab(i, frametime);
1120 else
1121 DoPlayerInEnemy(i, frametime);
1122 }
1123 if (rp->flags & RF_SPECIAL2) {
1124 if (team == RED_TEAM)
1125 DoPlayerInEnergy(i, frametime);
1126 else
1127 DoPlayerInEnemy(i, frametime);
1128 }
1129 if (rp->flags & RF_SPECIAL3) {
1130 if (team == RED_TEAM)
1131 DoPlayerInRepair(i, frametime);
1132 else
1133 DoPlayerInEnemy(i, frametime);
1134 }
1135 if (rp->flags & RF_SPECIAL4) {
1136 if (team == BLUE_TEAM)
1137 DoPlayerInLab(i, frametime);
1138 else
1139 DoPlayerInEnemy(i, frametime);
1140 }
1141 if (rp->flags & RF_SPECIAL5) {
1142 if (team == BLUE_TEAM)
1143 DoPlayerInEnergy(i, frametime);
1144 else
1145 DoPlayerInEnemy(i, frametime);
1146 }
1147 if (rp->flags & RF_SPECIAL6) {
1148 if (team == BLUE_TEAM)
1149 DoPlayerInRepair(i, frametime);
1150 else
1151 DoPlayerInEnemy(i, frametime);
1152 }
1153 }
1154 }
1155 }
1156 }
1157
1158 bool compare_slots(int a, int b) {
1159 int ascore, bscore;
1160 player_record *apr, *bpr;
1161 tPlayerStat *as, *bs;
1162
1163 apr = DMFCBase->GetPlayerRecord(a);
1164 bpr = DMFCBase->GetPlayerRecord(b);
1165 if (!apr)
1166 return true;
1167 if (!bpr)
1168 return false;
1169 as = (tPlayerStat *)apr->user_info;
1170 bs = (tPlayerStat *)bpr->user_info;
1171 if (!as)
1172 return true;
1173 if (!bs)
1174 return false;
1175
1176 if (apr->state == STATE_EMPTY)
1177 return true;
1178 if (bpr->state == STATE_EMPTY)
1179 return false;
1180 if ((apr->state == STATE_INGAME) && (bpr->state == STATE_INGAME)) {
1181 // both players were in the game
1182 ascore = as->Score[DSTAT_LEVEL];
1183 bscore = bs->Score[DSTAT_LEVEL];
1184 return (ascore < bscore);
1185 }
1186 if ((apr->state == STATE_INGAME) && (bpr->state == STATE_DISCONNECTED)) {
1187 // apr gets priority since he was in the game on exit
1188 return false;
1189 }
1190 if ((apr->state == STATE_DISCONNECTED) && (bpr->state == STATE_INGAME)) {
1191 // bpr gets priority since he was in the game on exit
1192 return true;
1193 }
1194 // if we got here then both players were disconnected
1195 ascore = as->Score[DSTAT_LEVEL];
1196 bscore = bs->Score[DSTAT_LEVEL];
1197 return (ascore < bscore);
1198 }
1199
1200 void OnPLRInit(void) {
1201 int tempsort[MAX_PLAYER_RECORDS];
1202 int i, t, j;
1203
1204 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
1205 tempsort[i] = i;
1206 }
1207
1208 for (i = 1; i <= MAX_PLAYER_RECORDS - 1; i++) {
1209 t = tempsort[i];
1210 // Shift elements down until
1211 // insertion point found.
1212 for (j = i - 1; j >= 0 && compare_slots(tempsort[j], t); j--) {
1213 tempsort[j + 1] = tempsort[j];
1214 }
1215 // insert
1216 tempsort[j + 1] = t;
1217 }
1218
1219 // copy the array over
1220 memcpy(SortedPlayers, tempsort, MAX_PLAYER_RECORDS * sizeof(int));
1221
1222 // Now fill in the final structure of sorted names
1223 int TeamCount[NUM_TEAMS];
1224 int team;
1225 for (i = 0; i < NUM_TEAMS; i++)
1226 TeamCount[i] = 0;
1227 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
1228 int slot = SortedPlayers[i];
1229 player_record *pr = DMFCBase->GetPlayerRecord(slot);
1230 if (pr && pr->state != STATE_EMPTY) {
1231
1232 if (DMFCBase->IsPlayerDedicatedServer(pr))
1233 continue; // skip dedicated server
1234
1235 team = (pr->state == STATE_INGAME) ? dPlayers[pr->pnum].team : pr->team;
1236 SortedPLRPlayers[team][TeamCount[team]] = slot;
1237 TeamCount[team]++;
1238 }
1239 }
1240 for (i = 0; i < NUM_TEAMS; i++) {
1241 if (TeamCount[i] < MAX_PLAYER_RECORDS)
1242 SortedPLRPlayers[i][TeamCount[i]] = -1;
1243 }
1244
1245 if (Player_who_took_over_last_base != -1) {
1246 // play a fanfare!
1247 if (snd_score != -1) {
1248 DLLPlay2dSound(snd_score, MAX_GAME_VOLUME);
1249 }
1250 }
1251
1252 DMFCBase->OnPLRInit();
1253 }
1254
1255 void OnPLRInterval(void) {
1256 DMFCBase->OnPLRInterval();
1257
1258 int TeamCol = 35;
1259 int NameCol = 165;
1260 int KillsCol = 335;
1261 int DeathsCol = 375;
1262 int SuicidesCol = 425;
1263 int y = 40;
1264 int slot;
1265 player_record *pr;
1266
1267 DLLgrtext_SetFont((DMFCBase->GetGameFontTranslateArray())[SMALL_UI_FONT_INDEX]);
1268 int height = DLLgrfont_GetHeight((DMFCBase->GetGameFontTranslateArray())[SMALL_UI_FONT_INDEX]) + 1;
1269
1270 if (Player_who_took_over_last_base != -1) {
1271 // print out at the top who took over the last room
1272 int team = (DMFCBase->GetPlayerTeam(Player_who_took_over_last_base) == RED_TEAM) ? BLUE_TEAM : RED_TEAM;
1273 DLLgrtext_SetColor(GR_RGB(40, 255, 40));
1274 char message[384];
1275 snprintf(message, sizeof(message), "%s successfully took over %s team's last base!",
1276 dPlayers[Player_who_took_over_last_base].callsign, DMFCBase->GetTeamString(team));
1277 DLLgrtext_CenteredPrintf(0, y, message);
1278 y += height;
1279 }
1280
1281 // print out header
1282 DLLgrtext_SetColor(GR_RGB(255, 255, 150));
1283 DLLgrtext_Printf(NameCol, y, TXT_PILOT);
1284 DLLgrtext_Printf(KillsCol, y, TXT_KILLS);
1285 DLLgrtext_Printf(DeathsCol, y, TXT_DEATHS);
1286 DLLgrtext_Printf(SuicidesCol, y, TXT_SUICIDES);
1287 y += height;
1288
1289 tPlayerStat *stat;
1290 bool has_members;
1291 bool doing_connected = true;
1292
1293 do_disconnected_folk:
1294
1295 for (int team = 0; team < NUM_TEAMS; team++) {
1296 // process this team
1297 bool show_team_label;
1298 show_team_label = false;
1299
1300 if (!doing_connected) {
1301 int temp_idx;
1302 temp_idx = 0;
1303
1304 while (SortedPLRPlayers[team][temp_idx] != -1) {
1305 int pnum = DMFCBase->WasPlayerInGameAtLevelEnd(SortedPLRPlayers[team][temp_idx]);
1306 if (pnum == -1) {
1307 show_team_label = true;
1308 break;
1309 }
1310 temp_idx++;
1311 }
1312 } else {
1313 show_team_label = true;
1314 }
1315
1316 if (show_team_label) {
1317 // is there anyone on this team?
1318 DLLgrtext_SetColor(DMFCBase->GetTeamColor(team));
1319 DLLgrtext_Printf(TeamCol, y, TXT_HUDDISPLAY, DMFCBase->GetTeamString(team), TeamScore[team]);
1320 }
1321 has_members = false;
1322
1323 for (int index = 0; index < MAX_PLAYER_RECORDS; index++) {
1324 // get the player num
1325 slot = SortedPLRPlayers[team][index];
1326 if (slot == -1) // we are done with this team
1327 break;
1328 pr = DMFCBase->GetPlayerRecord(slot);
1329 if (pr && pr->state != STATE_EMPTY) {
1330
1331 if (DMFCBase->IsPlayerDedicatedServer(pr))
1332 continue; // dedicated server
1333
1334 int pnum = DMFCBase->WasPlayerInGameAtLevelEnd(slot);
1335
1336 if ((doing_connected && pnum == -1) || (!doing_connected && pnum != -1))
1337 continue; // we're not handling them right now
1338
1339 if (pnum != -1) {
1340 DLLgrtext_SetColor(DMFCBase->GetTeamColor(team));
1341 } else {
1342 DLLgrtext_SetColor(GR_RGB(128, 128, 128));
1343 }
1344
1345 stat = (tPlayerStat *)pr->user_info;
1346 // valid player
1347 DLLgrtext_Printf(NameCol, y, "%s %d[%d]", pr->callsign, (stat) ? stat->Score[DSTAT_LEVEL] : 0,
1348 (stat) ? stat->Score[DSTAT_OVERALL] : 0);
1349 DLLgrtext_Printf(KillsCol, y, "%d[%d]", pr->dstats.kills[DSTAT_LEVEL], pr->dstats.kills[DSTAT_OVERALL]);
1350 DLLgrtext_Printf(DeathsCol, y, "%d[%d]", pr->dstats.deaths[DSTAT_LEVEL], pr->dstats.deaths[DSTAT_OVERALL]);
1351 DLLgrtext_Printf(SuicidesCol, y, "%d[%d]", pr->dstats.suicides[DSTAT_LEVEL],
1352 pr->dstats.suicides[DSTAT_OVERALL]);
1353 y += height;
1354 has_members = true;
1355
1356 if (y >= 440)
1357 goto quick_exit;
1358 }
1359 } // end for
1360 // on to the next team
1361
1362 if (!has_members)
1363 y += height; // on to the next line
1364
1365 if (y >= 440)
1366 goto quick_exit;
1367 } // end for
1368
1369 if (doing_connected) {
1370 doing_connected = false;
1371 goto do_disconnected_folk;
1372 }
1373
1374 quick_exit:;
1375 }
1376
1377 void OnControlMessage(uint8_t msg, int from_pnum) {
1378 switch (msg) {
1379 case VIRUS_NOTENOUGHKILLS:
1380 DLLAddHUDMessage(TXT_CANTCARRY);
1381 break;
1382 case VIRUS_CANTKILL:
1383 DLLAddHUDMessage(TXT_CANTKILLVIRUS);
1384 break;
1385 case VIRUS_DESTROYED:
1386 DLLAddHUDMessage(TXT_VIRUSDESTROYED);
1387 if (snd_virus_destroy != -1) {
1388 DLLPlay2dSound(snd_virus_destroy, MAX_GAME_VOLUME);
1389 }
1390 break;
1391 default:
1392 DMFCBase->OnControlMessage(msg, from_pnum);
1393 break;
1394 }
1395 }
1396
1397 void SaveStatsToFile(char *filename) {
1398 CFILE *file;
1399 DLLOpenCFILE(&file, filename, "wt");
1400 if (!file) {
1401 DLLmprintf(0, "Unable to open output file\n");
1402 return;
1403 }
1404
1405 // write out game stats
1406 #define BUFSIZE 150
1407 char buffer[BUFSIZE];
1408 char tempbuffer[25];
1409 int sortedslots[MAX_PLAYER_RECORDS];
1410 player_record *pr, *dpr;
1411 tPInfoStat stat;
1412 tPlayerStat *st;
1413 int count, p;
1414
1415 // sort the stats
1416 DMFCBase->GetSortedPlayerSlots(sortedslots, MAX_PLAYER_RECORDS);
1417 SortTeamScores(SortedTeams, TeamScore);
1418 count = 1;
1419
1420 snprintf(buffer, sizeof(buffer), TXT_SAVESTATSA, (DMFCBase->GetNetgameInfo())->name,
1421 (DMFCBase->GetCurrentMission())->cur_level);
1422 DLLcf_WriteString(file, buffer);
1423
1424 for (p = 0; p < NUM_TEAMS; p++) {
1425 int team_i = SortedTeams[p];
1426 memset(buffer, ' ', BUFSIZE);
1427 snprintf(tempbuffer, sizeof(tempbuffer), TXT_TEAMINFO, DMFCBase->GetTeamString(team_i));
1428 memcpy(&buffer[0], tempbuffer, strlen(tempbuffer));
1429 snprintf(tempbuffer, sizeof(tempbuffer), "[%d]", TeamScore[team_i]);
1430 memcpy(&buffer[20], tempbuffer, strlen(tempbuffer));
1431 buffer[20 + strlen(tempbuffer)] = '\0';
1432 DLLcf_WriteString(file, buffer);
1433 }
1434
1435 // Write team members
1436 DLLcf_WriteString(file, ""); // blank line
1437 for (int t = 0; t < NUM_TEAMS; t++) {
1438 int team_i = SortedTeams[t];
1439
1440 snprintf(buffer, sizeof(buffer), TXT_TEAMINFO, DMFCBase->GetTeamString(team_i));
1441 strncat(buffer, ":", sizeof(buffer) - strlen(buffer) - 1);
1442 DLLcf_WriteString(file, buffer);
1443
1444 for (p = 0; p < MAX_PLAYER_RECORDS; p++) {
1445 pr = DMFCBase->GetPlayerRecord(sortedslots[p]);
1446 if (pr && pr->state != STATE_EMPTY) {
1447
1448 if (DMFCBase->IsPlayerDedicatedServer(pr))
1449 continue; // skip dedicated server
1450
1451 if (pr->team == team_i) { // Check if current team
1452 snprintf(buffer, sizeof(buffer), " %s", pr->callsign);
1453 DLLcf_WriteString(file, buffer);
1454 }
1455 }
1456 }
1457 }
1458 DLLcf_WriteString(file, ""); // blank line
1459
1460 snprintf(buffer, sizeof(buffer), "%s", TXT_CURRRANKINGS);
1461 DLLcf_WriteString(file, buffer);
1462
1463 snprintf(buffer, sizeof(buffer), "%s", TXT_SAVESTATSB);
1464 DLLcf_WriteString(file, buffer);
1465 strcpy(buffer, "-----------------------------------------------------------------------------");
1466 DLLcf_WriteString(file, buffer);
1467
1468 for (p = 0; p < MAX_PLAYER_RECORDS; p++) {
1469 pr = DMFCBase->GetPlayerRecord(sortedslots[p]);
1470 if (pr && pr->state != STATE_EMPTY) {
1471
1472 if (DMFCBase->IsPlayerDedicatedServer(pr))
1473 continue; // skip dedicated server
1474
1475 st = (tPlayerStat *)pr->user_info;
1476 memset(buffer, ' ', BUFSIZE);
1477
1478 snprintf(tempbuffer, sizeof(tempbuffer), "%d)", count);
1479 memcpy(&buffer[0], tempbuffer, strlen(tempbuffer));
1480
1481 snprintf(tempbuffer, sizeof(tempbuffer), "%s%s", (pr->state == STATE_INGAME) ? "" : "*", pr->callsign);
1482 memcpy(&buffer[5], tempbuffer, strlen(tempbuffer));
1483
1484 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", (st) ? st->Score[DSTAT_LEVEL] : 0,
1485 (st) ? st->Score[DSTAT_OVERALL] : 0);
1486 memcpy(&buffer[34], tempbuffer, strlen(tempbuffer));
1487
1488 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", pr->dstats.kills[DSTAT_LEVEL],
1489 pr->dstats.kills[DSTAT_OVERALL]);
1490 memcpy(&buffer[46], tempbuffer, strlen(tempbuffer));
1491
1492 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", pr->dstats.deaths[DSTAT_LEVEL],
1493 pr->dstats.deaths[DSTAT_OVERALL]);
1494 memcpy(&buffer[58], tempbuffer, strlen(tempbuffer));
1495
1496 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", pr->dstats.suicides[DSTAT_LEVEL],
1497 pr->dstats.suicides[DSTAT_OVERALL]);
1498 memcpy(&buffer[69], tempbuffer, strlen(tempbuffer));
1499
1500 size_t pos = 69 + strlen(tempbuffer) + 1;
1501 if (pos < BUFSIZE)
1502 buffer[pos] = '\0';
1503
1504 buffer[BUFSIZE - 1] = '\0';
1505 DLLcf_WriteString(file, buffer);
1506 count++;
1507 }
1508 }
1509
1510 DLLcf_WriteString(file, TXT_SAVESTATSC);
1511
1512 count = 1;
1513 for (p = 0; p < MAX_PLAYER_RECORDS; p++) {
1514 pr = DMFCBase->GetPlayerRecord(p);
1515 if (pr && pr->state != STATE_EMPTY) {
1516
1517 if (DMFCBase->IsPlayerDedicatedServer(pr))
1518 continue; // skip dedicated server
1519
1520 // Write out header
1521 snprintf(buffer, sizeof(buffer), "%d) %s%s", count, (pr->state == STATE_INGAME) ? "" : "*", pr->callsign);
1522 DLLcf_WriteString(file, buffer);
1523 size_t length = strlen(buffer);
1524 memset(buffer, '=', length);
1525 buffer[length] = '\0';
1526 DLLcf_WriteString(file, buffer);
1527
1528 // time in game
1529 snprintf(buffer, sizeof(buffer), TXT_TIMEINGAME, DMFCBase->GetTimeString(DMFCBase->GetTimeInGame(p)));
1530 DLLcf_WriteString(file, buffer);
1531
1532 if (DMFCBase->FindPInfoStatFirst(p, &stat)) {
1533 snprintf(buffer, sizeof(buffer), "%s", TXT_SAVESTATSD);
1534 DLLcf_WriteString(file, buffer);
1535
1536 if (stat.slot != p) {
1537 memset(buffer, ' ', BUFSIZE);
1538 dpr = DMFCBase->GetPlayerRecord(stat.slot);
1539
1540 if (dpr) {
1541 snprintf(tempbuffer, sizeof(tempbuffer), "%s", dpr->callsign);
1542 memcpy(buffer, tempbuffer, strlen(tempbuffer));
1543
1544 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.kills);
1545 memcpy(&buffer[30], tempbuffer, strlen(tempbuffer));
1546
1547 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.deaths);
1548 memcpy(&buffer[40], tempbuffer, strlen(tempbuffer));
1549
1550 size_t pos = 40 + strlen(tempbuffer) + 1;
1551 if (pos < BUFSIZE)
1552 buffer[pos] = '\0';
1553
1554 buffer[BUFSIZE - 1] = '\0';
1555 DLLcf_WriteString(file, buffer);
1556 }
1557 }
1558
1559 while (DMFCBase->FindPInfoStatNext(&stat)) {
1560 if (stat.slot != p) {
1561 memset(buffer, ' ', BUFSIZE);
1562 dpr = DMFCBase->GetPlayerRecord(stat.slot);
1563
1564 if (dpr) {
1565 snprintf(tempbuffer, sizeof(tempbuffer), "%s", dpr->callsign);
1566 memcpy(buffer, tempbuffer, strlen(tempbuffer));
1567
1568 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.kills);
1569 memcpy(&buffer[30], tempbuffer, strlen(tempbuffer));
1570
1571 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.deaths);
1572 memcpy(&buffer[40], tempbuffer, strlen(tempbuffer));
1573
1574 size_t pos = 40 + strlen(tempbuffer) + 1;
1575 if (pos < BUFSIZE)
1576 buffer[pos] = '\0';
1577
1578 buffer[BUFSIZE - 1] = '\0';
1579 DLLcf_WriteString(file, buffer);
1580 }
1581 }
1582 }
1583 }
1584 DMFCBase->FindPInfoStatClose();
1585 DLLcf_WriteString(file, ""); // skip a line
1586 count++;
1587 }
1588 }
1589
1590 // done writing stats
1591 DLLcfclose(file);
1592 DLLAddHUDMessage(TXT_SAVED);
1593 }
1594
1595 #define ROOTFILENAME "Entropy"
1596 void OnSaveStatsToFile(void) {
1597 char filename[256];
1598 DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, false);
1599 SaveStatsToFile(filename);
1600 }
1601
1602 void OnLevelEndSaveStatsToFile(void) {
1603 char filename[256];
1604 DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, true);
1605 SaveStatsToFile(filename);
1606 }
1607
1608 void OnDisconnectSaveStatsToFile(void) {
1609 char filename[256];
1610 DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, false);
1611 SaveStatsToFile(filename);
1612 }
1613
1614 void DisplayHUDScores(struct tHUDItem *hitem) {
1615 if (display_my_welcome) {
1616 DisplayWelcomeMessage(DMFCBase->GetPlayerNum());
1617 display_my_welcome = false;
1618 }
1619
1620 if (DisplayScoreScreen)
1621 return;
1622
1623 // Display the countdown if there should be one
1624 if (IsPlayerInEnemy(DMFCBase->GetPlayerNum()) &&
1625 DLLInvGetTypeIDCount(DMFCBase->GetPlayerNum(), OBJ_POWERUP, virus_id) >= MINIMUM_VIRUS_COUNT) {
1626 float time_remaining = (TAKEOVER_TIME - TimeInRoom[DMFCBase->GetPlayerNum()].total_time);
1627
1628 int font = (DMFCBase->GetGameFontTranslateArray())[BIG_FONT_INDEX];
1629 DLLgrtext_SetFont(font);
1630 int fontheight = DLLgrfont_GetHeight(font);
1631
1632 char buffer[255];
1633 int max_w = DMFCBase->GetGameWindowW();
1634 snprintf(buffer, sizeof(buffer), "T - %.1f", (time_remaining >= 0) ? time_remaining : 0);
1635 int lwidth = DLLgrtext_GetTextLineWidth(buffer);
1636 DLLgrtext_SetColor(GR_RGB(40, 255, 40));
1637 DLLgrtext_SetAlpha(255);
1638 DLLgrtext_Printf((max_w / 2) - (lwidth / 2), fontheight * 5, buffer);
1639 }
1640
1641 int font = (DMFCBase->GetGameFontTranslateArray())[HUD_FONT_INDEX];
1642 int height = DLLgrfont_GetHeight(font) + 3;
1643 DLLgrtext_SetFont(font);
1644
1645 int y = (DMFCBase->GetGameWindowH() / 2) - ((height * 3) / 2);
1646 int x = 520;
1647 int team;
1648 uint8_t alpha = DMFCBase->ConvertHUDAlpha((uint8_t)((DisplayScoreScreen) ? 128 : 255));
1649
1650 team = DMFCBase->GetMyTeam();
1651 DLLRenderHUDText(DMFCBase->GetTeamColor(team), alpha, 0, x, 0, TXT_TEAMINFO, DMFCBase->GetTeamString(team));
1652
1653 // determine coordinates to use here
1654 // we'll use a virtual width of 85 pixels on a 640x480 screen
1655 // so first determine the new width
1656 int name_width = 100.0f * DMFCBase->GetHudAspectX();
1657 int score_width = DLLgrtext_GetTextLineWidth("888");
1658 int name_x = DMFCBase->GetGameWindowW() - name_width - score_width - 10;
1659 int score_x = DMFCBase->GetGameWindowW() - score_width - 5;
1660 char buffer[256];
1661
1662 for (int i = 0; i < NUM_TEAMS; i++) {
1663 team = SortedTeams[i];
1664
1665 if (team == DMFCBase->GetMyTeam()) {
1666 if (Highlight_bmp > BAD_BITMAP_HANDLE) {
1667 // draw the highlite bar in the background
1668 DLLrend_SetAlphaValue(alpha * 0.50f);
1669 DLLrend_SetZBufferState(0);
1670 DLLrend_SetTextureType(TT_LINEAR);
1671 DLLrend_SetLighting(LS_NONE);
1672 DLLrend_SetAlphaType(AT_CONSTANT_TEXTURE);
1673 DLLrend_DrawScaledBitmap(name_x - 2, y - 2, score_x + score_width + 2, y + height - 1, Highlight_bmp, 0, 0, 1,
1674 1, 1.0, -1, NULL);
1675 DLLrend_SetZBufferState(1);
1676 }
1677 }
1678
1679 snprintf(buffer, sizeof(buffer), TXT_HUDSCORE, DMFCBase->GetTeamString(team), TeamOwnedRooms[team]);
1680 DMFCBase->ClipString(name_width, buffer, true);
1681
1682 DLLgrtext_SetAlpha(alpha);
1683 DLLgrtext_SetColor(DMFCBase->GetTeamColor(team));
1684 DLLgrtext_Printf(name_x, y, buffer);
1685 DLLgrtext_Printf(score_x, y, "[%d]", TeamScore[team]);
1686
1687 y += height;
1688 }
1689 DLLgrtext_SetColor(GR_WHITE);
1690 DLLgrtext_Printf(name_x, y, TXT_VIRUSLOAD, DLLInvGetTypeIDCount(DMFCBase->GetPlayerNum(), OBJ_POWERUP, virus_id),
1691 (int)(NumberOfKillsSinceLastDeath[DMFCBase->GetPlayerNum()] * VIRUS_PER_KILL));
1692 y += height;
1693 }
1694
1695 // insert sort
1696 #define compGT(a, b) (a < b)
1697 void SortTeamScores(int *sortedindex, int *scores) {
1698 int t;
1699 int i, j;
1700
1701 // copy scores into scoreinfo array
1702 for (i = 0; i < NUM_TEAMS; i++) {
1703 sortedindex[i] = i;
1704 }
1705
1706 for (i = 1; i <= NUM_TEAMS - 1; i++) {
1707 t = sortedindex[i];
1708
1709 /* Shift elements down until */
1710 /* insertion point found. */
1711 for (j = i - 1; j >= 0 && compGT(scores[sortedindex[j]], scores[t]); j--) {
1712 sortedindex[j + 1] = sortedindex[j];
1713 }
1714
1715 /* insert */
1716 sortedindex[j + 1] = t;
1717 }
1718 }
1719
1720 void DisplayWelcomeMessage(int player_num) {
1721 char name_buffer[64];
1722 strcpy(name_buffer, (DMFCBase->GetPlayers())[player_num].callsign);
1723
1724 if (player_num == DMFCBase->GetPlayerNum()) {
1725 int team = DMFCBase->GetMyTeam();
1726 if (team == -1)
1727 return;
1728
1729 DLLAddHUDMessage(TXT_WELCOME, name_buffer);
1730 DLLAddColoredHUDMessage(DMFCBase->GetTeamColor(team), TXT_TEAMJOIN, DMFCBase->GetTeamString(team));
1731 } else {
1732 int team = dPlayers[player_num].team;
1733 if (team == -1)
1734 return;
1735
1736 DLLAddColoredHUDMessage(DMFCBase->GetTeamColor(team), TXT_JOIN, name_buffer, DMFCBase->GetTeamString(team));
1737 }
1738 }
1739
1740 void OnLabSpewTimer(void) {
1741 int max_rooms = DMFCBase->GetHighestRoomIndex() + 1;
1742 int objnum;
1743 int index;
1744 int virus_count;
1745
1746 for (int r = 0; r < RoomCount; r++) {
1747 if (dRooms[RoomList[r]].flags & RF_SPECIAL1) {
1748 index = -1;
1749 virus_count = 0;
1750 for (int v = 0; v < MAX_VIRII; v++) {
1751 if (TeamVirii[RED_TEAM][v] == -1) {
1752 // insert here
1753 index = v;
1754 } else {
1755 if (dObjects[TeamVirii[RED_TEAM][v]].roomnum == RoomList[r])
1756 virus_count++;
1757 }
1758 }
1759
1760 // only create virus if we found a slot and there are less than MAX_VIRII_PER_ROOM
1761 // in the room
1762 if (virus_count < MAX_VIRII_PER_ROOM && index != -1) {
1763 objnum = SpewObjectInRoom(OBJ_POWERUP, virus_id, RoomList[r]);
1764 TeamVirii[RED_TEAM][index] = objnum;
1765 }
1766 }
1767 if (dRooms[RoomList[r]].flags & RF_SPECIAL4) {
1768 index = -1;
1769 virus_count = 0;
1770 for (int v = 0; v < MAX_VIRII; v++) {
1771 if (TeamVirii[BLUE_TEAM][v] == -1) {
1772 // insert here
1773 index = v;
1774 } else {
1775 if (dObjects[TeamVirii[BLUE_TEAM][v]].roomnum == RoomList[r])
1776 virus_count++;
1777 }
1778 }
1779 // only create virus if we found a slot and there are less than MAX_VIRII_PER_ROOM
1780 // in the room
1781 if (virus_count < MAX_VIRII_PER_ROOM && index != -1) {
1782 objnum = SpewObjectInRoom(OBJ_POWERUP, virus_id, RoomList[r]);
1783 TeamVirii[BLUE_TEAM][index] = objnum;
1784 }
1785 }
1786 }
1787 }
1788
1789 void RemoveVirusFromPlayer(int player_num, bool remove_all) {
1790 int inv_count = DLLInvGetTypeIDCount(player_num, OBJ_POWERUP, virus_id);
1791
1792 if (!remove_all) {
1793 ASSERT(inv_count >= MINIMUM_VIRUS_COUNT);
1794 inv_count = MINIMUM_VIRUS_COUNT;
1795 }
1796
1797 for (int i = 0; i < inv_count; i++) {
1798 DLLInvRemove(player_num, OBJ_POWERUP, virus_id);
1799 }
1800 }
1801
1802 // returns true if there is at least 1 lab, else returns the room which sould be a lab in newlab
1803 bool ScanForLaboratory(int team, int *newlab) {
1804 *newlab = 0;
1805 int flag;
1806 int ef, rf;
1807 bool found = false;
1808
1809 flag = (team == RED_TEAM) ? RF_SPECIAL1 : RF_SPECIAL4;
1810 ef = (team == RED_TEAM) ? RF_SPECIAL2 : RF_SPECIAL5;
1811 rf = (team == RED_TEAM) ? RF_SPECIAL3 : RF_SPECIAL6;
1812
1813 for (int r = 0; r < RoomCount; r++) {
1814 room *rp = &dRooms[RoomList[r]];
1815 if (rp->flags & flag) {
1816 // we found a lab
1817 return true;
1818 }
1819 if ((rp->flags & ef) && (!found)) {
1820 found = true;
1821 *newlab = RoomList[r];
1822 }
1823 if ((rp->flags & rf) && (!found)) {
1824 found = true;
1825 *newlab = RoomList[r];
1826 }
1827 }
1828 return false;
1829 }
1830
1831 bool CompareDistanceTravel(vector *curr_pos, vector *last_pos) {
1832 vector result;
1833 float a, b, c, bc, dist;
1834
1835 result.x = curr_pos->x - last_pos->x;
1836 result.y = curr_pos->y - last_pos->y;
1837 result.z = curr_pos->z - last_pos->z;
1838
1839 a = fabs(result.x);
1840 b = fabs(result.y);
1841 c = fabs(result.z);
1842
1843 if (a < b) {
1844 float t = a;
1845 a = b;
1846 b = t;
1847 }
1848
1849 if (b < c) {
1850 float t = b;
1851 b = c;
1852 c = t;
1853
1854 if (a < b) {
1855 float t = a;
1856 a = b;
1857 b = t;
1858 }
1859 }
1860
1861 bc = (b / 4) + (c / 8);
1862
1863 dist = a + bc + (bc / 2);
1864
1865 if (dist > 5) {
1866 memcpy(last_pos, curr_pos, sizeof(vector));
1867 return false;
1868 } else {
1869 return true;
1870 }
1871 }
1872
1873 bool IsPlayerInEnemy(int pnum) {
1874 if (!DMFCBase->CheckPlayerNum(pnum))
1875 return false;
1876
1877 if (dObjects[dPlayers[pnum].objnum].type != OBJ_PLAYER)
1878 return false; // only work with OBJ_PLAYERS...this automatically handles observers,dedicated server
1879
1880 // first check to see if this player is in a special room
1881 if (ROOMNUM_OUTSIDE(dObjects[dPlayers[pnum].objnum].roomnum))
1882 return false;
1883
1884 int team = DMFCBase->GetPlayerTeam(pnum);
1885 room *rp = &dRooms[dObjects[dPlayers[pnum].objnum].roomnum];
1886
1887 if (team != RED_TEAM) {
1888 if ((rp->flags & RF_SPECIAL1) || (rp->flags & RF_SPECIAL2) || (rp->flags & RF_SPECIAL3)) {
1889 return true;
1890 }
1891 }
1892
1893 if (team != BLUE_TEAM) {
1894 if ((rp->flags & RF_SPECIAL4) || (rp->flags & RF_SPECIAL5) || (rp->flags & RF_SPECIAL6)) {
1895 return true;
1896 }
1897 }
1898
1899 return false;
1900 }
1901
1902 void OnGetTokenString(char *src, char *dest, int dest_size) {
1903 if (!stricmp(src, "virus")) {
1904 int count = DLLInvGetTypeIDCount(DMFCBase->GetPlayerNum(), OBJ_POWERUP, virus_id);
1905 char buffer[100];
1906 snprintf(buffer, sizeof(buffer), "%d", count);
1907 strncpy(dest, buffer, dest_size - 1);
1908 dest[dest_size - 1] = '\0';
1909 return;
1910 }
1911
1912 DMFCBase->OnGetTokenString(src, dest, dest_size);
1913 }
1914
1915 void OnPrintScores(int level) {
1916 char buffer[256];
1917 char name[70];
1918 size_t t;
1919 size_t pos[6];
1920 size_t len[6];
1921
1922 netplayer *dNetPlayers = DMFCBase->GetNetPlayers();
1923
1924 for (int i = 0; i < NUM_TEAMS; i++) {
1925 snprintf(buffer, sizeof(buffer), "%s:%d\n", DMFCBase->GetTeamString(i), TeamScore[i]);
1926 DPrintf(buffer);
1927 }
1928
1929 memset(buffer, ' ', 256);
1930
1931 pos[0] = 0;
1932 t = len[0] = 30; // give ample room for pilot name
1933 pos[1] = pos[0] + t + 1;
1934 t = len[1] = strlen(TXT_SCORE);
1935 pos[2] = pos[1] + t + 1;
1936 t = len[2] = strlen(TXT_KILLS_SHORT);
1937 pos[3] = pos[2] + t + 1;
1938 t = len[3] = strlen(TXT_DEATHS_SHORT);
1939 pos[4] = pos[3] + t + 1;
1940 t = len[4] = strlen(TXT_SUICIDES_SHORT);
1941 pos[5] = pos[4] + t + 1;
1942 t = len[5] = strlen(TXT_PING);
1943
1944 memcpy(&buffer[pos[0]], TXT_PILOT, strlen(TXT_PILOT));
1945 memcpy(&buffer[pos[1]], TXT_SCORE, len[1]);
1946 memcpy(&buffer[pos[2]], TXT_KILLS_SHORT, len[2]);
1947 memcpy(&buffer[pos[3]], TXT_DEATHS_SHORT, len[3]);
1948 memcpy(&buffer[pos[4]], TXT_SUICIDES_SHORT, len[4]);
1949 memcpy(&buffer[pos[5]], TXT_PING, len[5]);
1950 buffer[pos[5] + len[5] + 1] = '\n';
1951 buffer[pos[5] + len[5] + 2] = '\0';
1952 DPrintf(buffer);
1953
1954 int slot;
1955 player_record *pr;
1956 int pcount;
1957
1958 if (level < 0 || level >= MAX_PLAYER_RECORDS)
1959 pcount = MAX_PLAYER_RECORDS;
1960 else
1961 pcount = level;
1962
1963 int sortedplayers[MAX_PLAYER_RECORDS];
1964
1965 DMFCBase->GetSortedPlayerSlots(sortedplayers, MAX_PLAYER_RECORDS);
1966
1967 for (int i = 0; i < pcount; i++) {
1968 slot = sortedplayers[i];
1969 pr = DMFCBase->GetPlayerRecord(slot);
1970 if ((pr) && (pr->state != STATE_EMPTY)) {
1971
1972 if (DMFCBase->IsPlayerDedicatedServer(pr))
1973 continue; // skip dedicated server
1974
1975 snprintf(name, sizeof(name), "%s%s: %.8s", (pr->state == STATE_DISCONNECTED) ? "*" : "", pr->callsign,
1976 DMFCBase->GetTeamString(pr->team));
1977 name[29] = '\0';
1978
1979 tPlayerStat *stat;
1980 stat = (tPlayerStat *)pr->user_info;
1981
1982 memset(buffer, ' ', 256);
1983 t = strlen(name);
1984 memcpy(&buffer[pos[0]], name, (t < len[0]) ? t : len[0]);
1985 snprintf(name, sizeof(name), "%d", (stat) ? stat->Score[DSTAT_LEVEL] : 0);
1986 t = strlen(name);
1987 memcpy(&buffer[pos[1]], name, (t < len[1]) ? t : len[1]);
1988 snprintf(name, sizeof(name), "%d", pr->dstats.kills[DSTAT_LEVEL]);
1989 t = strlen(name);
1990 memcpy(&buffer[pos[2]], name, (t < len[2]) ? t : len[2]);
1991 snprintf(name, sizeof(name), "%d", pr->dstats.deaths[DSTAT_LEVEL]);
1992 t = strlen(name);
1993 memcpy(&buffer[pos[3]], name, (t < len[3]) ? t : len[3]);
1994 snprintf(name, sizeof(name), "%d", pr->dstats.suicides[DSTAT_LEVEL]);
1995 t = strlen(name);
1996 memcpy(&buffer[pos[4]], name, (t < len[4]) ? t : len[4]);
1997
1998 if (pr->state == STATE_INGAME)
1999 snprintf(name, sizeof(name), "%.0f", dNetPlayers[pr->pnum].ping_time * 1000.0f);
2000 else
2001 strcpy(name, "---");
2002 t = strlen(name);
2003 memcpy(&buffer[pos[5]], name, (t < len[5]) ? t : len[5]);
2004 buffer[pos[5] + len[5] + 1] = '\n';
2005 buffer[pos[5] + len[5] + 2] = '\0';
2006 DPrintf(buffer);
2007 }
2008 }
2009 }