Debian Codegraph @

netgames/ctf/ctf.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/ctf/ctf.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003/08/26 03:56:47 $
23 * $Author: kevinb $
24 *
25 * <insert description of file here>
26 *
27 * $Log: ctf.cpp,v $
28 * Revision 1.1.1.1 2003/08/26 03:56:47 kevinb
29 * initial 1.5 import
30 *
31 *
32 * 140 10/03/01 1:05p Matt
33 * Made team_name buffer large enough to hold the team number *plus* the
34 * number of players on the team.
35 *
36 * 139 9/24/01 2:28p Matt
37 * Allowed room for longer team name on results screen.
38 *
39 * 138 9/20/01 12:58p Matt
40 * Added a team member list to the stats file.
41 *
42 * 137 10/21/99 9:27p Jeff
43 * B.A. Macintosh code merge
44 *
45 * 136 9/03/99 5:30p Jeff
46 * use the flag's roomnumber instead of the player's roomnum when checking
47 * for a score (in case of a really thin room that the flag is in)
48 *
49 * 135 8/17/99 5:53p Jeff
50 * track ranks on PXO
51 *
52 * 134 7/15/99 1:17a Jeff
53 * fixed up $scores
54 *
55 * 133 7/13/99 12:10p Jeff
56 * added some specific text taunt token decoding
57 *
58 * 132 7/12/99 2:27p Jeff
59 * fixed PLR to only display the team label for the disconnected list if
60 * there are people in the list
61 *
62 * 131 7/11/99 6:54p Jeff
63 * fixed PLR so it doesn't go off the screen on long lists and active
64 * players in the game are shown first
65 *
66 * 130 6/10/99 5:21p Jeff
67 * fixed crash in french version, due to local array being to small
68 *
69 * 129 5/26/99 4:38a Jeff
70 * fixed ctf bad scoring bugs
71 *
72 * 128 5/23/99 3:04a Jason
73 * fixed bug with player rankings not being updated correctly
74 *
75 * 127 5/20/99 7:54p 3dsmax
76 * changed number of attach points
77 *
78 * 126 5/12/99 11:04p Jeff
79 * dmfc and multiplayer games now have endian friendly packets (*whew*)
80 *
81 * 125 5/12/99 11:28a Jeff
82 * added sourcesafe comment block
83 *
84 * $NoKeywords: $
85 */
86
87 #include "gamedll_header.h"
88 #include <string.h>
89
90 #include "idmfc.h"
91 #include "ctf.h"
92 #include "CTFstr.h"
93
94 IDMFC *DMFCBase;
95 static IDmfcStats *dstat;
96
97 static object *dObjects;
98 static player *dPlayers;
99 static room *dRooms;
100 static netplayer *dNetPlayers;
101
102 #define SPID_GAMESTATE 0x01
103 #define SPID_ADDDELFLAG 0x02
104 #define SPID_COLLIDE 0x03
105
106 // Inventory flags
107 #define FLAGMASK_REDTEAM 0x01
108 #define FLAGMASK_BLUETEAM 0x02
109 #define FLAGMASK_GREENTEAM 0x04
110 #define FLAGMASK_YELLOWTEAM 0x08
111
112 // athome masks
113 #define REDAH 0x001
114 #define BLUEAH 0x002
115 #define GREENAH 0x004
116 #define YELLOWAH 0x008
117
118 #define FLAG_TIMEOUT_VALUE 120
119 // Sound name defines
120 #define SOUND_PICKUPFLAG_OWNTEAM "CTFPickupFlag1"
121 #define SOUND_PICKUPFLAG_OTHERTEAM "CTFPickupFlag1"
122 #define SOUND_SCORE_OWNTEAM "CTFScore1"
123 #define SOUND_SCORE_OTHERTEAM "CTFScore1"
124 #define SOUND_LOSEFLAG_OWNTEAM "CTFLostFlag1"
125 #define SOUND_LOSEFLAG_OTHERTEAM "CTFLostFlag1"
126 #define SOUND_FLAGRETURNED_OWNTEAM "CTFReturnedFlag1"
127 #define SOUND_FLAGRETURNED_OTHERTEAM "CTFReturnedFlag1"
128 #define SOUND_HATTRICK_FIRST "CTFHatTrick"
129 #define SOUND_HATTRICK_REGULAR "CTFHatTrick"
130 /*
131 $$TABLE_SOUND "CTFPickupFlag1"
132 $$TABLE_SOUND "CTFPickupFlag1"
133 $$TABLE_SOUND "CTFScore1"
134 $$TABLE_SOUND "CTFScore1"
135 $$TABLE_SOUND "CTFLostFlag1"
136 $$TABLE_SOUND "CTFLostFlag1"
137 $$TABLE_SOUND "CTFReturnedFlag1"
138 $$TABLE_SOUND "CTFReturnedFlag1"
139 $$TABLE_SOUND "CTFHatTrick"
140 $$TABLE_SOUND "CTFHatTrick"
141 */
142 static int snd_return_ownteam = -1;
143 static int snd_return_otherteam = -1;
144 static int snd_pickedup_otherteam = -1;
145 static int snd_pickedup_ownteam = -1;
146 static int snd_score_ownteam = -1;
147 static int snd_score_otherteam = -1;
148 static int snd_lose_ownteam = -1;
149 static int snd_lose_otherteam = -1;
150 static int snd_hattrick_first = -1;
151 static int snd_hattrick_reg = -1;
152
153 struct tPlayerStat { // Overall scores (throughout the game)
154 int Score[2];
155 };
156 static int pack_pstat(tPlayerStat *user_info, uint8_t *data);
157 static int unpack_pstat(tPlayerStat *user_info, uint8_t *data);
158 int pack_pstat(tPlayerStat *user_info, uint8_t *data) {
159 int count = 0;
160 MultiAddInt(user_info->Score[0], data, &count);
161 MultiAddInt(user_info->Score[1], data, &count);
162 return count;
163 }
164
165 int unpack_pstat(tPlayerStat *user_info, uint8_t *data) {
166 int count = 0;
167 user_info->Score[0] = MultiGetInt(data, &count);
168 user_info->Score[1] = MultiGetInt(data, &count);
169 return count;
170 }
171
172 static int SortedPlayers[MAX_PLAYER_RECORDS]; // sorted player nums
173 static int SortedPLRPlayers[DLLMAX_TEAMS][MAX_PLAYER_RECORDS];
174
175 static int TeamScores[DLLMAX_TEAMS]; // teams scores
176 static int OverallTeamScores[DLLMAX_TEAMS]; // overall scores per team
177 static int SortedTeams[DLLMAX_TEAMS]; // sorted team scores
178 static int Highlight_bmp = -1;
179 static bool DisplayScoreScreen;
180 static bool Someone_has_hattrick = false; // this is false if no one has had a hattrick this level
181 static bool First_game_frame = false;
182
183 static int FlagIDs[DLLMAX_TEAMS]; // Flag Object ID's
184 static int AFlagIDs[DLLMAX_TEAMS]; // Attached Flag Object ID's
185 static int GoalRooms[DLLMAX_TEAMS]; // Goal Rooms for Teams
186 static int FlagBmp[DLLMAX_TEAMS]; // Flag Bitmap handles
187 static int FlagAHBmp[DLLMAX_TEAMS]; // Flag At Home Bitmap handles
188 static int DimFlagAHBmp[DLLMAX_TEAMS]; // Dimmed versions of the Flag At Home Bitmaps
189 static bool FlagAtHome[DLLMAX_TEAMS]; // Flag At Home bools
190 static int HasFlag[DLLMAX_TEAMS]; // List of Playernums of who has what flag, -1 if no one does
191 static bool DisplayFlagBlink = true, DisplayScoreBlink = true;
192 static int WhoJustFlagged = -1, WhoJustFlaggedTimer = -1;
193 static int WhoJustScored = -1, WhoJustScoredTimer = -1;
194 static int CTFNumOfTeams = 2;
195 static int ChildFlags[DLLMAX_TEAMS]; // Object handles of attached flags as a player has em
196
197 static float Flag_timeout_timers[DLLMAX_TEAMS];
198 static bool display_my_welcome = false;
199
200 static void OnTimerScoreKill(void); // timer callback: when a score flash timer ends
201 static void OnTimerScore(void); // timer callback: on a score flash interval
202 static void OnTimer(void); // timer callback: when a flag taken flash timer ends
203 static void OnTimerKill(void); // timer callback: on a flag taken flash interval
204 static void DisplayWelcomeMessage(int player_num); // displays a welcome message to the player when he joins
205 static void SortTeamScores(int *sortedindex, int *scores); // sorts an array of team scores, filling in the sorted index
206 // numbers
207 static void DisplayHUDScores(struct tHUDItem *hitem); // callback when the HUD info is to be drawn
208 static void ReceiveGameState(uint8_t *data); // callback when a gamestate packet is received from the server
209 static void SendGameState(int playernum); // called when the server is to send gamestate packet to a client
210 static void SetColoredBalls(
211 int playernum,
212 bool reset = false); // sets the colored balls around a player (determined by what is in their inventory)
213 static void ChangeNumberOfTeams(int newsize); // called when the number of teams in the game is changed or to be changed
214 static void DoFlagReturnedHome(int team); // called to handle any events when a flag is returned home for a team
215 static void DoLoseFlag(int team); // called to handle any events when a team loses their flag
216 static void TellClientsToAddorDelFlag(int pnum, int team, int objnum, bool add);
217 static void ServerIsTellingMeToAddorDelAFlag(uint8_t *data);
218 static void OnGetTokenString(char *src, char *dest, int dest_size);
219 // returns the number of flags a player has, 0 if none, or an invalid pnum
220 static int GetFlagCountForPlayer(int pnum);
221 // returns the mask of which flags this player currently has
222 static uint8_t GetFlagMaskForPlayer(int pnum);
223 // adds a flag to a player, as a precaution, it will go through all the players and makes sure that no one
224 // has the flag that is being added. If they are adding the flag, than remove that flag from whoever we thought
225 // had it it will return false if it had to remove a flag from a player
226 static bool GivePlayerFlag(int pnum, uint8_t team);
227 // this function takes a flag away from the player, useful for when he scores, spews, disconnects, or observer modes
228 static void LoseFlagForPlayer(int pnum, uint8_t team, bool remove_from_inven = true);
229
230 ///////////////////////////////////////////////
231 // localization info/functions
232 static char **StringTable;
233 static int StringTableSize = 0;
234 static const char *_ErrorString = "Missing String";
235 const char *GetString(int d) {
236 if ((d < 0) || (d >= StringTableSize))
237 return _ErrorString;
238 else
239 return StringTable[d];
240 }
241 static void SaveStatsToFile(char *filename);
242 static void DetermineScore(int precord_num, int column_num, char *buffer, int buffer_size);
243 static void ShowStatBitmap(int precord_num, int column_num, int x, int y, int w, int h, uint8_t alpha_to_use);
244
245 // This function gets called by the game when it wants to learn some info about the game
246 void DLLFUNCCALL DLLGetGameInfo(tDLLOptions *options) {
247 options->flags = DOF_MAXTEAMS | DOF_MINTEAMS;
248 options->max_teams = 4;
249 options->min_teams = 2;
250 strcpy(options->game_name, TXT_CTF);
251 strcpy(options->requirements, "MINGOALS2,GOALPERTEAM");
252 }
253
254 void DetermineScore(int precord_num, int column_num, char *buffer, int buffer_size) {
255 player_record *pr = DMFCBase->GetPlayerRecord(precord_num);
256 if (!pr || pr->state == STATE_EMPTY) {
257 buffer[0] = '\0';
258 return;
259 }
260
261 tPlayerStat *stat = (tPlayerStat *)pr->user_info;
262 snprintf(buffer, buffer_size, "%d", (stat) ? stat->Score[DSTAT_LEVEL] : 0);
263 }
264
265 void TeamScoreCallback(int team, char *buffer, int buffer_size) {
266 ASSERT(team >= 0 && team < DLLMAX_TEAMS);
267
268 snprintf(buffer, buffer_size, " %d", TeamScores[team]);
269 }
270
271 void ShowStatBitmap(int precord_num, int column_num, int x, int y, int w, int h, uint8_t alpha_to_use) {
272 player_record *pr = DMFCBase->GetPlayerRecord(precord_num);
273 int flagcount, flagmask;
274
275 if (pr && pr->state == STATE_INGAME) {
276
277 flagcount = GetFlagCountForPlayer(pr->pnum);
278
279 if (!flagcount)
280 return;
281
282 flagmask = GetFlagMaskForPlayer(pr->pnum);
283
284 // Display the flags that this player has
285 x = x + w - (11 * flagcount);
286 for (int q = 0; q < CTFNumOfTeams; q++) {
287 // draw all the flags the player has in his inventory
288 if (flagmask & 0x01) {
289 // player has this flag
290 DLLRenderHUDQuad(x, y, 10, 10, 0, 0, 1, 1, FlagBmp[q], alpha_to_use, 0);
291 x += 11;
292 }
293
294 // rotate 1 bit for next flag
295 flagmask = flagmask >> 1;
296 }
297 }
298 }
299
300 ///////////////////////////////////////////////
301 // Initializes the game function pointers
302 void DLLFUNCCALL DLLGameInit(int *api_func, uint8_t *all_ok, int num_teams_to_use) {
303 *all_ok = 1;
304 DMFCBase = CreateDMFC();
305 if (!DMFCBase) {
306 *all_ok = 0;
307 return;
308 }
309
310 dstat = CreateDmfcStats();
311 if (!dstat) {
312 *all_ok = 0;
313 return;
314 }
315
316 DMFCBase->LoadFunctions(api_func);
317
318 // Setup event handlers
319 DMFCBase->Set_OnInterval(OnInterval);
320 DMFCBase->Set_OnHUDInterval(OnHUDInterval);
321 DMFCBase->Set_OnKeypress(OnKeypress);
322 DMFCBase->Set_OnServerGameCreated(OnServerGameCreated);
323 DMFCBase->Set_OnClientLevelStart(OnClientLevelStart);
324 DMFCBase->Set_OnClientLevelEnd(OnClientLevelEnd);
325 DMFCBase->Set_OnGameStateRequest(OnGameStateRequest);
326 DMFCBase->Set_OnPlayerConnect(OnPlayerConnect);
327 DMFCBase->Set_OnClientPlayerKilled(OnClientPlayerKilled);
328 DMFCBase->Set_OnServerCollide(OnServerCollide);
329 // DMFCBase->Set_OnClientCollide(OnClientCollide);
330 DMFCBase->Set_OnPLRInterval(OnPLRInterval);
331 DMFCBase->Set_OnPLRInit(OnPLRInit);
332 DMFCBase->Set_OnCanChangeTeam(OnCanChangeTeam);
333 DMFCBase->Set_OnSaveStatsToFile(OnSaveStatsToFile);
334 DMFCBase->Set_OnLevelEndSaveStatsToFile(OnLevelEndSaveStatsToFile);
335 DMFCBase->Set_OnDisconnectSaveStatsToFile(OnDisconnectSaveStatsToFile);
336 DMFCBase->Set_OnPrintScores(OnPrintScores);
337 DMFCBase->Set_OnPlayerEntersObserver(OnPlayerEntersObserver);
338 DMFCBase->Set_OnClientPlayerDisconnect(OnClientPlayerDisconnect);
339 DMFCBase->Set_OnPlayerChangeTeam(OnPlayerChangeTeam);
340 DMFCBase->Set_OnGetTokenString(OnGetTokenString);
341
342 // Setup arrays for easier to read code
343 dObjects = DMFCBase->GetObjects();
344 dPlayers = DMFCBase->GetPlayers();
345 dRooms = DMFCBase->GetRooms();
346 dNetPlayers = DMFCBase->GetNetPlayers();
347
348 netgame_info *Netgame = DMFCBase->GetNetgameInfo();
349 Netgame->flags |= (NF_TRACK_RANK);
350
351 CTFNumOfTeams = num_teams_to_use;
352
353 DMFCBase->GameInit(CTFNumOfTeams);
354 DLLCreateStringTable("CTF.str", &StringTable, &StringTableSize);
355 DLLmprintf(0, "%d strings loaded from string table\n", StringTableSize);
356 if (!StringTableSize) {
357 *all_ok = 0;
358 return;
359 }
360
361 ChangeNumberOfTeams(CTFNumOfTeams);
362
363 // add the death and suicide messages
364 DMFCBase->AddDeathMessage(TXT_DEATH1, true);
365 DMFCBase->AddDeathMessage(TXT_DEATH2, true);
366 DMFCBase->AddDeathMessage(TXT_DEATH3, false);
367 DMFCBase->AddDeathMessage(TXT_DEATH4, false);
368 DMFCBase->AddDeathMessage(TXT_DEATH5, true);
369 DMFCBase->AddDeathMessage(TXT_DEATH6, true);
370 DMFCBase->AddDeathMessage(TXT_DEATH7, false);
371 DMFCBase->AddDeathMessage(TXT_DEATH8, true);
372 DMFCBase->AddDeathMessage(TXT_DEATH9, true);
373 DMFCBase->AddDeathMessage(TXT_DEATH10, true);
374
375 DMFCBase->AddSuicideMessage(TXT_SUICIDE1);
376 DMFCBase->AddSuicideMessage(TXT_SUICIDE2);
377 DMFCBase->AddSuicideMessage(TXT_SUICIDE3);
378 DMFCBase->AddSuicideMessage(TXT_SUICIDE4);
379 DMFCBase->AddSuicideMessage(TXT_SUICIDE5);
380 DMFCBase->AddSuicideMessage(TXT_SUICIDE6);
381
382 // setup the Playerstats struct so DMFC can handle it automatically when a new player enters the game
383 DMFCBase->SetupPlayerRecord(sizeof(tPlayerStat), (int (*)(void *, uint8_t *))pack_pstat,
384 (int (*)(void *, uint8_t *))unpack_pstat);
385
386 DMFCBase->AddHUDItemCallback(HI_TEXT, DisplayHUDScores);
387
388 DMFCBase->RegisterPacketReceiver(SPID_GAMESTATE, ReceiveGameState);
389 DMFCBase->RegisterPacketReceiver(SPID_ADDDELFLAG, ServerIsTellingMeToAddorDelAFlag);
390 DMFCBase->RegisterPacketReceiver(SPID_COLLIDE, OnClientCollide);
391
392 // Load all the bitmaps
393 FlagBmp[RED_TEAM] = DLLbm_AllocLoadFileBitmap("RedFlagIcon.ogf", 0, BITMAP_FORMAT_1555);
394 FlagBmp[BLUE_TEAM] = DLLbm_AllocLoadFileBitmap("BlueFlagIcon.ogf", 0, BITMAP_FORMAT_1555);
395 FlagBmp[GREEN_TEAM] = DLLbm_AllocLoadFileBitmap("GreenFlagIcon.ogf", 0, BITMAP_FORMAT_1555);
396 FlagBmp[YELLOW_TEAM] = DLLbm_AllocLoadFileBitmap("YellowFlagIcon.ogf", 0, BITMAP_FORMAT_1555);
397 // athome bitmaps
398 FlagAHBmp[RED_TEAM] = DLLbm_AllocLoadFileBitmap("RedFlagIcon.ogf", 0, BITMAP_FORMAT_1555);
399 FlagAHBmp[BLUE_TEAM] = DLLbm_AllocLoadFileBitmap("BlueFlagIcon.ogf", 0, BITMAP_FORMAT_1555);
400 FlagAHBmp[GREEN_TEAM] = DLLbm_AllocLoadFileBitmap("GreenFlagIcon.ogf", 0, BITMAP_FORMAT_1555);
401 FlagAHBmp[YELLOW_TEAM] = DLLbm_AllocLoadFileBitmap("YellowFlagIcon.ogf", 0, BITMAP_FORMAT_1555);
402 DimFlagAHBmp[RED_TEAM] = DLLbm_AllocLoadFileBitmap("RedFlagIconDim.ogf", 0, BITMAP_FORMAT_1555);
403 DimFlagAHBmp[BLUE_TEAM] = DLLbm_AllocLoadFileBitmap("BlueFlagIconDim.ogf", 0, BITMAP_FORMAT_1555);
404 DimFlagAHBmp[GREEN_TEAM] = DLLbm_AllocLoadFileBitmap("GreenFlagIconDim.ogf", 0, BITMAP_FORMAT_1555);
405 DimFlagAHBmp[YELLOW_TEAM] = DLLbm_AllocLoadFileBitmap("YellowFlagIconDim.ogf", 0, BITMAP_FORMAT_1555);
406 // fill in the IDs
407
408 FlagIDs[RED_TEAM] = DLLFindObjectIDName("FlagRed");
409 FlagIDs[BLUE_TEAM] = DLLFindObjectIDName("Flagblue");
410 FlagIDs[GREEN_TEAM] = DLLFindObjectIDName("FlagGreen");
411 FlagIDs[YELLOW_TEAM] = DLLFindObjectIDName("FlagYellow");
412 AFlagIDs[RED_TEAM] = DLLFindObjectIDName("ShipRedFlag");
413 AFlagIDs[BLUE_TEAM] = DLLFindObjectIDName("ShipBlueFlag");
414 AFlagIDs[GREEN_TEAM] = DLLFindObjectIDName("ShipGreenFlag");
415 AFlagIDs[YELLOW_TEAM] = DLLFindObjectIDName("ShipYellowFlag");
416
417 // give initial values for the at home so they're set
418 FlagAtHome[RED_TEAM] = FlagAtHome[BLUE_TEAM] = FlagAtHome[GREEN_TEAM] = FlagAtHome[YELLOW_TEAM] = false;
419
420 // make sure all was init ok
421 for (int i = 0; i < DLLMAX_TEAMS; i++) {
422 if ((FlagBmp[i] <= BAD_BITMAP_HANDLE) || (FlagAHBmp[i] <= BAD_BITMAP_HANDLE) ||
423 (DimFlagAHBmp[i] <= BAD_BITMAP_HANDLE) || (FlagIDs[i] < 0) || (AFlagIDs[i] < 0)) {
424 *all_ok = 0;
425 return;
426 }
427 }
428
429 Highlight_bmp = DLLbm_AllocBitmap(32, 32, 0);
430 if (Highlight_bmp > BAD_BITMAP_HANDLE) {
431 uint16_t *data = DLLbm_data(Highlight_bmp, 0);
432 if (!data) {
433 // bail on out of here
434 *all_ok = 0;
435 return;
436 }
437 for (int x = 0; x < 32 * 32; x++) {
438 data[x] = GR_RGB16(50, 50, 50) | OPAQUE_FLAG;
439 }
440 }
441
442 // Load in and touch all the sounds so they're ready to rock
443 snd_score_ownteam = DLLFindSoundName(IGNORE_TABLE(SOUND_SCORE_OWNTEAM));
444 if (snd_score_ownteam != -1)
445 DLLTouchSound(snd_score_ownteam);
446 snd_score_otherteam = DLLFindSoundName(IGNORE_TABLE(SOUND_SCORE_OTHERTEAM));
447 if (snd_score_otherteam != -1)
448 DLLTouchSound(snd_score_otherteam);
449 snd_pickedup_otherteam = DLLFindSoundName(IGNORE_TABLE(SOUND_PICKUPFLAG_OWNTEAM));
450 if (snd_pickedup_otherteam != -1)
451 DLLTouchSound(snd_pickedup_otherteam);
452 snd_pickedup_ownteam = DLLFindSoundName(IGNORE_TABLE(SOUND_PICKUPFLAG_OTHERTEAM));
453 if (snd_pickedup_ownteam != -1)
454 DLLTouchSound(snd_pickedup_ownteam);
455 snd_return_ownteam = DLLFindSoundName(IGNORE_TABLE(SOUND_FLAGRETURNED_OWNTEAM));
456 if (snd_return_ownteam != -1)
457 DLLTouchSound(snd_return_ownteam);
458 snd_return_otherteam = DLLFindSoundName(IGNORE_TABLE(SOUND_FLAGRETURNED_OTHERTEAM));
459 if (snd_return_otherteam != -1)
460 DLLTouchSound(snd_return_otherteam);
461 snd_lose_ownteam = DLLFindSoundName(IGNORE_TABLE(SOUND_LOSEFLAG_OWNTEAM));
462 if (snd_lose_ownteam != -1)
463 DLLTouchSound(snd_lose_ownteam);
464 snd_lose_otherteam = DLLFindSoundName(IGNORE_TABLE(SOUND_LOSEFLAG_OTHERTEAM));
465 if (snd_lose_otherteam != -1)
466 DLLTouchSound(snd_lose_otherteam);
467 snd_hattrick_first = DLLFindSoundName(IGNORE_TABLE(SOUND_HATTRICK_FIRST));
468 if (snd_hattrick_first != -1)
469 DLLTouchSound(snd_hattrick_first);
470 snd_hattrick_reg = DLLFindSoundName(IGNORE_TABLE(SOUND_HATTRICK_REGULAR));
471 if (snd_hattrick_reg != -1)
472 DLLTouchSound(snd_hattrick_reg);
473
474 DisplayScoreScreen = false;
475
476 // Initialize the Stats Manager
477 // ----------------------------
478
479 tDmfcStatsInit tsi;
480 tDmfcStatsColumnInfo pl_col[7];
481 char gname[32];
482 strcpy(gname, TXT_CTF);
483
484 tsi.flags = DSIF_SHOW_PIC | DSIF_SHOW_OBSERVERICON | DSIF_SEPERATE_BY_TEAM;
485 tsi.cColumnCountDetailed = 0;
486 tsi.cColumnCountPlayerList = 7;
487 tsi.clbDetailedColumnBMP = NULL;
488 tsi.clbDetailedColumn = NULL;
489 tsi.clbPlayerColumn = DetermineScore;
490 tsi.clbPlayerColumnBMP = ShowStatBitmap;
491 tsi.DetailedColumns = NULL;
492 tsi.GameName = gname;
493 tsi.MaxNumberDisplayed = NULL;
494 tsi.PlayerListColumns = pl_col;
495 tsi.SortedPlayerRecords = SortedPlayers;
496 tsi.clTeamLine = TeamScoreCallback;
497
498 pl_col[0].color_type = DSCOLOR_TEAM;
499 pl_col[0].title[0] = '\0';
500 pl_col[0].type = DSCOL_BMP;
501 pl_col[0].width = 33;
502
503 pl_col[1].color_type = DSCOLOR_TEAM;
504 strcpy(pl_col[1].title, TXT_PILOT);
505 pl_col[1].type = DSCOL_PILOT_NAME;
506 pl_col[1].width = 120;
507
508 pl_col[2].color_type = DSCOLOR_TEAM;
509 strcpy(pl_col[2].title, TXT_POINTS);
510 pl_col[2].type = DSCOL_CUSTOM;
511 pl_col[2].width = 52;
512
513 pl_col[3].color_type = DSCOLOR_TEAM;
514 strcpy(pl_col[3].title, TXT_KILLS_SHORT);
515 pl_col[3].type = DSCOL_KILLS_LEVEL;
516 pl_col[3].width = 47;
517
518 pl_col[4].color_type = DSCOLOR_TEAM;
519 strcpy(pl_col[4].title, TXT_DEATHS_SHORT);
520 pl_col[4].type = DSCOL_DEATHS_LEVEL;
521 pl_col[4].width = 47;
522
523 pl_col[5].color_type = DSCOLOR_TEAM;
524 strcpy(pl_col[5].title, TXT_SUICIDES_SHORT);
525 pl_col[5].type = DSCOL_SUICIDES_LEVEL;
526 pl_col[5].width = 47;
527
528 pl_col[6].color_type = DSCOLOR_TEAM;
529 strcpy(pl_col[6].title, TXT_PING);
530 pl_col[6].type = DSCOL_PING;
531 pl_col[6].width = 40;
532
533 dstat->Initialize(&tsi);
534 }
535 // Called when the DLL is shutdown
536 void DLLFUNCCALL DLLGameClose() {
537 //@@SaveConfig();
538 //@@DMFCBase->CFGClose();
539
540 // Free all the bitmaps
541 DLLbm_FreeBitmap(FlagBmp[RED_TEAM]);
542 DLLbm_FreeBitmap(FlagBmp[BLUE_TEAM]);
543 DLLbm_FreeBitmap(FlagBmp[GREEN_TEAM]);
544 DLLbm_FreeBitmap(FlagBmp[YELLOW_TEAM]);
545 DLLbm_FreeBitmap(DimFlagAHBmp[RED_TEAM]);
546 DLLbm_FreeBitmap(DimFlagAHBmp[BLUE_TEAM]);
547 DLLbm_FreeBitmap(DimFlagAHBmp[GREEN_TEAM]);
548 DLLbm_FreeBitmap(DimFlagAHBmp[YELLOW_TEAM]);
549 DLLbm_FreeBitmap(FlagAHBmp[RED_TEAM]);
550 DLLbm_FreeBitmap(FlagAHBmp[BLUE_TEAM]);
551 DLLbm_FreeBitmap(FlagAHBmp[GREEN_TEAM]);
552 DLLbm_FreeBitmap(FlagAHBmp[YELLOW_TEAM]);
553
554 if (Highlight_bmp > BAD_BITMAP_HANDLE)
555 DLLbm_FreeBitmap(Highlight_bmp);
556
557 DLLDestroyStringTable(StringTable, StringTableSize);
558
559 if (dstat) {
560 dstat->DestroyPointer();
561 dstat = NULL;
562 }
563
564 if (DMFCBase) {
565 DMFCBase->GameClose();
566 DMFCBase->DestroyPointer();
567 DMFCBase = NULL;
568 }
569 }
570
571 /////////////////////////////////////////////////////////////
572 // DMFC Overrides
573
574 // OnHUDInterval
575 void OnHUDInterval(void) {
576 dstat->DoFrame();
577
578 DMFCBase->DisplayOutrageLogo();
579
580 DMFCBase->OnHUDInterval();
581 }
582
583 // OnInterval
584 void OnInterval(void) {
585 DMFCBase->GetSortedPlayerSlots(SortedPlayers, MAX_PLAYER_RECORDS);
586
587 SortTeamScores(SortedTeams, TeamScores);
588
589 // Determine if a flag has timed out yet
590 for (int i = 0; i < CTFNumOfTeams; i++) {
591 if ((HasFlag[i] == -1) && (!FlagAtHome[i])) {
592 // check the time out values
593 if (DMFCBase->GetGametime() >= Flag_timeout_timers[i]) {
594 // the timeout has expired, move home!
595 vector home_pos;
596 int home, objnum;
597
598 Flag_timeout_timers[i] = 0;
599 FlagAtHome[i] = true;
600 HasFlag[i] = -1;
601 home = GoalRooms[i];
602 objnum = -1;
603
604 // find the flag objnum
605 for (int o = 0; o < MAX_OBJECTS; o++) {
606 if (dObjects[o].type == OBJ_POWERUP && dObjects[o].id == FlagIDs[i]) {
607 // found it
608 objnum = o;
609 break;
610 }
611 }
612
613 if (objnum == -1) {
614 // hmm couldn't find the flag
615 // this is messed up...will create the flag on the server and all will be happy
616 if (DMFCBase->GetLocalRole() == LR_SERVER) {
617 int flagid = FlagIDs[i];
618 if ((flagid != -1) && (home >= 0) && (home <= DMFCBase->GetHighestRoomIndex()) &&
619 (!ROOMNUM_OUTSIDE(home)) && (dRooms[home].used)) {
620 // Safe to create the flag
621 DLLComputeRoomCenter(&home_pos, &dRooms[home]);
622 objnum = DLLObjCreate(OBJ_POWERUP, flagid, home, &home_pos, NULL, OBJECT_HANDLE_NONE);
623 DLLMultiSendObject(&dObjects[objnum], 0, true);
624 }
625 }
626 } else {
627 if ((home >= 0) && (home <= DMFCBase->GetHighestRoomIndex()) && (!ROOMNUM_OUTSIDE(home)) &&
628 (dRooms[home].used)) {
629 DLLComputeRoomCenter(&home_pos, &dRooms[home]);
630 DLLObjSetPos(&dObjects[objnum], &home_pos, home, NULL, false);
631 }
632 }
633 }
634 }
635 }
636
637 DMFCBase->OnInterval();
638
639 First_game_frame = true;
640 }
641
642 // OnKeypress
643 // What it needs to do:
644 // 1) Test for F7 keypress
645 void OnKeypress(int key) {
646 dllinfo *Data;
647 Data = DMFCBase->GetDLLInfoCallData();
648
649 switch (key) {
650 case K_F7:
651 DisplayScoreScreen = !DisplayScoreScreen;
652 DMFCBase->EnableOnScreenMenu(false);
653 dstat->Enable(DisplayScoreScreen);
654 break;
655 case K_PAGEDOWN:
656 if (DisplayScoreScreen) {
657 dstat->ScrollDown();
658 Data->iRet = 1;
659 }
660 break;
661 case K_PAGEUP:
662 if (DisplayScoreScreen) {
663 dstat->ScrollUp();
664 Data->iRet = 1;
665 }
666 break;
667 case K_F6:
668 DisplayScoreScreen = false;
669 dstat->Enable(false);
670 break;
671 case K_ESC:
672 if (DisplayScoreScreen) {
673 dstat->Enable(false);
674 DisplayScoreScreen = false;
675 Data->iRet = 1;
676 }
677 break;
678 }
679
680 DMFCBase->OnKeypress(key);
681 }
682
683 // OnServerGameCreated
684 // What it needs to do:
685 // 1) Zero out all the stats for every player
686 // 2) Zero out team scores
687 // 3) Display welcome message to server
688 void OnServerGameCreated(void) {
689 DMFCBase->OnServerGameCreated();
690 tPlayerStat *stat;
691 player_record *pr;
692 int i;
693
694 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
695 pr = DMFCBase->GetPlayerRecord(i);
696 if (pr) {
697 stat = (tPlayerStat *)pr->user_info;
698 if (stat) {
699 stat->Score[DSTAT_LEVEL] = 0;
700 stat->Score[DSTAT_OVERALL] = 0;
701 }
702 }
703 }
704 for (i = 0; i < CTFNumOfTeams; i++) {
705 TeamScores[i] = 0;
706 OverallTeamScores[i] = 0;
707 }
708 }
709
710 // OnClientLevelStart
711 // What it needs to do:
712 // 1) Zero out level scores for everyone
713 // 2) Reset colored balls
714 // 3) Zero out team scores
715 // 4) Set number of teams
716 // 5) If we are the server, create the flags and send them to the clients
717 void OnClientLevelStart(void) {
718 int i;
719
720 DMFCBase->OnClientLevelStart();
721
722 First_game_frame = false;
723
724 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
725 SortedPlayers[i] = -1;
726 }
727 for (i = 0; i < DLLMAX_TEAMS; i++) {
728
729 SortedTeams[i] = -1;
730 Flag_timeout_timers[i] = 0;
731 ChildFlags[i] = OBJECT_HANDLE_NONE;
732 }
733
734 Someone_has_hattrick = false;
735
736 tPlayerStat *stat;
737 player_record *pr;
738 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
739 pr = DMFCBase->GetPlayerRecord(i);
740 if (pr) {
741 stat = (tPlayerStat *)pr->user_info;
742 if (stat) {
743 stat->Score[DSTAT_LEVEL] = 0;
744 }
745 }
746 }
747 for (i = 0; i < DLLMAX_PLAYERS; i++) {
748 SetColoredBalls(i, true);
749 }
750
751 // fill in the goal room #'s
752 for (i = 0; i < DLLMAX_TEAMS; i++) {
753 GoalRooms[i] = DLLGetGoalRoomForTeam(i);
754 FlagAtHome[i] = true;
755 HasFlag[i] = -1;
756 TeamScores[i] = 0;
757 }
758
759 DMFCBase->SetNumberOfTeams(CTFNumOfTeams);
760 DLLMultiPaintGoalRooms(NULL);
761
762 // Bash the inventory since it doesn't get reset for the server
763 DLLInvReset(DMFCBase->GetPlayerNum(), true);
764
765 // Display our welcome message
766 display_my_welcome = true;
767
768 // If we are the server create the flags
769 if (DMFCBase->GetLocalRole() != LR_SERVER) {
770 // Get the game state
771 DMFCBase->RequestGameState();
772 return;
773 }
774
775 vector vpos;
776 int objnum;
777 int flagid, goalroom;
778
779 for (i = 0; i < CTFNumOfTeams; i++) {
780 flagid = FlagIDs[i];
781 goalroom = GoalRooms[i];
782 if ((flagid != -1) && (goalroom >= 0) && (goalroom <= DMFCBase->GetHighestRoomIndex()) &&
783 (!ROOMNUM_OUTSIDE(goalroom)) && (dRooms[goalroom].used)) {
784 // Safe to create the flag
785 DLLmprintf(0, "Creating %s Flag\n", DMFCBase->GetTeamString(i));
786 DLLComputeRoomCenter(&vpos, &dRooms[goalroom]);
787 objnum = DLLObjCreate(OBJ_POWERUP, flagid, goalroom, &vpos, NULL, OBJECT_HANDLE_NONE);
788 DLLMultiSendObject(&dObjects[objnum], 0, true);
789 }
790 }
791 }
792
793 // OnServerCollide
794 // What it needs to do:
795 // 1) See if the player has collided with a flag, if so pass to clients
796 void OnServerCollide(object *me_obj, object *it_obj) {
797 // see if we need to send this event to the clients
798 // only if me==player it==flag powerup
799 if (!me_obj || !it_obj)
800 return;
801 if ((me_obj->type == OBJ_PLAYER || me_obj->type == OBJ_GHOST) && (it_obj->type == OBJ_POWERUP))
802 if ((it_obj->id == FlagIDs[RED_TEAM]) || (it_obj->id == FlagIDs[BLUE_TEAM]) ||
803 (it_obj->id == FlagIDs[GREEN_TEAM]) || (it_obj->id == FlagIDs[YELLOW_TEAM])) {
804
805 // Start a packet for the collide
806 uint8_t data[MAX_GAME_DATA_SIZE];
807 int count = 0;
808 DMFCBase->StartPacket(data, SPID_COLLIDE, &count);
809 int start = count;
810
811 // we need me, it and me roomnum
812 MultiAddUshort((me_obj - dObjects), data, &count);
813 MultiAddUshort((it_obj - dObjects), data, &count);
814 MultiAddInt(it_obj->roomnum, data, &count);
815
816 DMFCBase->SendPacket(data, count, SP_ALL);
817 OnClientCollide(&data[start]);
818 }
819 }
820
821 // OnClientCollide
822 // What it needs to do:
823 // 1) See if Player collided with own flag
824 // 1.1) Check to see if in home goal
825 // 1.1.1) Reset colored balls
826 // 1.1.2) Remove all the flags from the inventory, send them back to their home goal
827 // 1.1.3) Add scores
828 // 1.1.4) Print out score
829 // 1.1.5) Do Kill Goal check
830 // 1,2) Move home flag to center of base
831 // 2) else collide with another team's flag
832 // 2.1) Add flag to inventory
833 // 2.2) Set rotating balls
834 // 2.3) Print out message
835 void OnClientCollide(uint8_t *data) {
836 object *me_obj, *it_obj;
837 int me_roomnum;
838 int count = 0;
839 int me_serv_objnum, it_serv_objnum, me_client_objnum, it_client_objnum;
840
841 me_serv_objnum = MultiGetUshort(data, &count);
842 it_serv_objnum = MultiGetUshort(data, &count);
843 me_roomnum = MultiGetInt(data, &count);
844
845 me_client_objnum = DMFCBase->ConvertServerToLocalObjnum(me_serv_objnum);
846 it_client_objnum = DMFCBase->ConvertServerToLocalObjnum(it_serv_objnum);
847
848 if (me_client_objnum == -1 || it_client_objnum == -1) {
849 Int3();
850 return;
851 }
852
853 me_obj = &dObjects[me_client_objnum];
854 it_obj = &dObjects[it_client_objnum];
855
856 // now process the collide
857
858 int fteam = -1; // flags team
859 int pnum = me_obj->id; // player number
860 int pteam = DMFCBase->GetPlayerTeam(pnum); // player's team
861 int i;
862 bool play_return_home_msg = false;
863
864 for (i = 0; i < CTFNumOfTeams; i++) {
865 if (it_obj->id == FlagIDs[i])
866 fteam = i;
867 }
868
869 if (fteam == -1) {
870 return; // something wrong, as none of the IDs are one of the flag's IDs
871 }
872
873 // Reset the timer for this flag since it's guaranteed someone owns it now
874 // or it has been sent home.
875 Flag_timeout_timers[fteam] = 0;
876
877 // Did player collide with his own team's flag?
878 if (fteam == pteam) {
879 int16_t flag_count = 0;
880 vector fpos;
881 int groom;
882 int flagcount;
883 int flagmask;
884
885 bool captured_flags[DLLMAX_TEAMS];
886 for (i = 0; i < DLLMAX_TEAMS; i++) {
887 captured_flags[i] = false;
888 }
889
890 // Check what room the player is in, if they're not in home base, then it's no score
891 if (GoalRooms[fteam] == me_roomnum) {
892 // ok so he is in his home base, now find out how many flags he has, score for each and return them home
893 flagcount = GetFlagCountForPlayer(pnum);
894 flagmask = GetFlagMaskForPlayer(pnum);
895
896 // now loop through the flags, check to see if we have them and handle it
897 for (i = 0; i < CTFNumOfTeams; i++) {
898 if (flagmask & 0x01) {
899 // we have the flag we're currently working on
900 captured_flags[i] = true;
901 groom = GoalRooms[i];
902 if ((groom >= 0) && (groom <= DMFCBase->GetHighestRoomIndex()) && (!ROOMNUM_OUTSIDE(groom)) &&
903 (dRooms[groom].used)) {
904 // the room is valid, so move it back to home
905
906 LoseFlagForPlayer(pnum, i);
907
908 if (DMFCBase->GetLocalRole() == LR_SERVER) {
909 // if we are the server create the object and send it on it's way to the player's
910 DLLComputeRoomCenter(&fpos, &dRooms[groom]);
911 int objnum = DLLObjCreate(OBJ_POWERUP, FlagIDs[i], groom, &fpos, NULL, OBJECT_HANDLE_NONE);
912 DLLMultiSendObject(&dObjects[objnum], 1, true);
913 } // end server create
914 } // end room ok
915
916 // set it's at home flag
917 FlagAtHome[i] = true;
918 } // end flag check
919 // rotate the flagmask over one to check for the next flag
920 flagmask = flagmask >> 1;
921 } // end for loop
922
923 // remove any rotating balls
924 SetColoredBalls(pnum, true);
925
926 // Now handle any scoring
927 int score = 0;
928 switch (flagcount) {
929 case 0:
930 break;
931 case 1:
932 score = 1;
933 break;
934 case 2:
935 score = 3;
936 break;
937 case 3:
938 score = 9;
939 break;
940 }
941
942 // print out the message and add to the scores
943
944 if (score) {
945 tPlayerStat *stat = (tPlayerStat *)DMFCBase->GetPlayerRecordData(pnum);
946 if (stat) {
947 stat->Score[DSTAT_LEVEL] += score;
948 stat->Score[DSTAT_OVERALL] += score;
949
950 if (stat->Score[DSTAT_LEVEL] == 3) {
951 if (Someone_has_hattrick) {
952 DLLAddHUDMessage(TXT_HATTRICK, dPlayers[pnum].callsign);
953 if (snd_hattrick_reg != -1)
954 DLLPlay2dSound(snd_hattrick_reg, MAX_GAME_VOLUME / 2);
955 } else {
956 DLLAddHUDMessage(TXT_HATTRICKFIRST, dPlayers[pnum].callsign);
957 if (snd_hattrick_first != -1)
958 DLLPlay2dSound(snd_hattrick_first, MAX_GAME_VOLUME / 2);
959 Someone_has_hattrick = true;
960 }
961 }
962 }
963
964 TeamScores[pteam] += score;
965 OverallTeamScores[pteam] += score;
966 int newscore = TeamScores[pteam];
967
968 switch (flagcount) {
969 case 1: {
970 char t[20];
971 t[0] = '\0';
972 for (int c = 0; c < DLLMAX_TEAMS; c++) {
973 if (captured_flags[c]) {
974 strcpy(t, DMFCBase->GetTeamString(c));
975 break;
976 }
977 }
978 DLLAddHUDMessage(TXT_CAPTURE, dPlayers[pnum].callsign, DMFCBase->GetTeamString(pteam), t);
979 } break;
980 case 2: {
981 char t[2][20];
982 t[0][0] = '\0';
983 t[1][0] = '\0';
984 int index = 0;
985 for (int c = 0; c < DLLMAX_TEAMS; c++) {
986 if (captured_flags[c]) {
987 strcpy(t[index], DMFCBase->GetTeamString(c));
988 index++;
989 if (index == 2)
990 break;
991 }
992 }
993 DLLAddHUDMessage(TXT_CAPTURETWO, dPlayers[pnum].callsign, DMFCBase->GetTeamString(pteam), t[0], t[1]);
994 } break;
995 case 3: {
996 char t[3][20];
997 t[0][0] = '\0';
998 t[1][0] = '\0';
999 t[2][0] = '\0';
1000
1001 int index = 0;
1002 for (int c = 0; c < DLLMAX_TEAMS; c++) {
1003 if (captured_flags[c]) {
1004 strcpy(t[index], DMFCBase->GetTeamString(c));
1005 index++;
1006 if (index == 3)
1007 break;
1008 }
1009 }
1010 DLLAddHUDMessage(TXT_CAPTURETHREE, dPlayers[pnum].callsign, DMFCBase->GetTeamString(pteam), t[0], t[1], t[2]);
1011 }
1012 }
1013
1014 if (dPlayers[DMFCBase->GetPlayerNum()].team == pteam) {
1015 if (snd_score_ownteam != -1)
1016 DLLPlay2dSound(snd_score_ownteam, MAX_GAME_VOLUME / 2);
1017 } else {
1018 if (snd_score_otherteam != -1)
1019 DLLPlay2dSound(snd_score_otherteam, MAX_GAME_VOLUME / 2);
1020 }
1021
1022 // Set a Timer to display
1023 if (WhoJustScoredTimer != -1)
1024 DMFCBase->KillTimer(WhoJustScoredTimer);
1025 WhoJustScoredTimer = DMFCBase->SetTimerInterval(OnTimerScore, 0.5f, 5.0f, OnTimerScoreKill);
1026 WhoJustScored = pteam;
1027 }
1028 // do the killgoal check
1029 int killgoal;
1030 if (DMFCBase->GetScoreLimit(&killgoal)) {
1031 if (TeamScores[pteam] >= killgoal) {
1032 // That's all she wrote for this level
1033 DLLmprintf(0, "OnClientCollide:Kill Goal Reached!\n");
1034 DMFCBase->EndLevel();
1035 }
1036 }
1037 } else {
1038 DoFlagReturnedHome(pteam);
1039 if (DMFCBase->CheckPlayerNum(pnum)) {
1040 char parm1[20], parm2[20];
1041 strcpy(parm1, DMFCBase->GetTeamString(pteam));
1042 strcpy(parm2, DMFCBase->GetTeamString(fteam));
1043 DLLAddHUDMessage(TXT_RETURN, dPlayers[pnum].callsign, parm1, parm2);
1044 }
1045 }
1046
1047 // since the player collided with his own team's flag we need to move the flag back home
1048 FlagAtHome[pteam] = true;
1049 HasFlag[pteam] = -1;
1050 vector home_pos;
1051 int home = GoalRooms[pteam];
1052
1053 if ((home >= 0) && (home <= DMFCBase->GetHighestRoomIndex()) && (!ROOMNUM_OUTSIDE(home)) && (dRooms[home].used)) {
1054 DLLComputeRoomCenter(&home_pos, &dRooms[home]);
1055 DLLObjSetPos(it_obj, &home_pos, home, NULL, false);
1056 }
1057
1058 } // end Player Collides with own team's flag
1059 else {
1060 // The Player collide with another team's flag
1061 FlagAtHome[fteam] = false;
1062
1063 // add the flag to the player
1064 GivePlayerFlag(pnum, fteam);
1065
1066 if (dPlayers[DMFCBase->GetPlayerNum()].team == dPlayers[pnum].team) {
1067 if (snd_pickedup_ownteam != -1)
1068 DLLPlay2dSound(snd_pickedup_ownteam, MAX_GAME_VOLUME / 2);
1069 } else {
1070 if (snd_pickedup_otherteam != -1)
1071 DLLPlay2dSound(snd_pickedup_otherteam, MAX_GAME_VOLUME / 2);
1072 }
1073
1074 // Set a Timer to display
1075 if (WhoJustFlaggedTimer != -1)
1076 DMFCBase->KillTimer(WhoJustFlaggedTimer);
1077 WhoJustFlaggedTimer = DMFCBase->SetTimerInterval(OnTimer, 0.5f, 5.0f, OnTimerKill);
1078 WhoJustFlagged = fteam;
1079
1080 char parm1[20], parm2[20];
1081 strcpy(parm1, DMFCBase->GetTeamString(pteam));
1082 strcpy(parm2, DMFCBase->GetTeamString(fteam));
1083 if (dObjects[dPlayers[pnum].objnum].roomnum == GoalRooms[fteam]) {
1084 DLLAddHUDMessage(TXT_PICKUPFLAG, dPlayers[pnum].callsign, parm1, parm2);
1085 } else {
1086 DLLAddHUDMessage(TXT_PICKUPFLAGSPEW, dPlayers[pnum].callsign, parm1, parm2);
1087 }
1088
1089 // remove the flag from the world
1090 if (DMFCBase->GetLocalRole() == LR_SERVER) {
1091 // tell the clients to remove this flag
1092 DLLSetObjectDeadFlag(it_obj, true, false);
1093 }
1094 }
1095 }
1096
1097 // OnGameStateRequest
1098 // 1) Send game state to new player
1099 void OnGameStateRequest(int player_num) {
1100 SendGameState(player_num);
1101 DMFCBase->OnGameStateRequest(player_num);
1102 }
1103
1104 void OnPlayerConnect(int player_num) {
1105 DMFCBase->OnPlayerConnect(player_num);
1106
1107 tPlayerStat *stat;
1108 stat = (tPlayerStat *)DMFCBase->GetPlayerRecordData(player_num);
1109 if (stat) {
1110 stat->Score[DSTAT_LEVEL] = 0;
1111 stat->Score[DSTAT_OVERALL] = 0;
1112 }
1113
1114 if (player_num != DMFCBase->GetPlayerNum()) {
1115 // announce the arrival of the player
1116 DisplayWelcomeMessage(player_num);
1117 }
1118 }
1119
1120 void OnClientLevelEnd(void) { DMFCBase->OnClientLevelEnd(); }
1121
1122 void OnPlayerChangeTeam(int player_num, int newteam, bool announce, bool spew_onrespawn) {
1123 DMFCBase->OnPlayerChangeTeam(player_num, newteam, announce, spew_onrespawn);
1124 player_record *pr = DMFCBase->GetPlayerRecordByPnum(player_num);
1125 }
1126
1127 // OnClientPlayerKilled
1128 // 1) If suicide add to suicides
1129 // 2) Else add to killer's kill, add to victim's deaths
1130 // 3) Reset colored balls
1131 // 4) If killed in a flag's home goal than put it in the center of the room
1132 void OnClientPlayerKilled(object *killer_obj, int victim_pnum) {
1133 int kpnum = -1;
1134
1135 if (killer_obj) {
1136 if ((killer_obj->type == OBJ_PLAYER) || (killer_obj->type == OBJ_GHOST) || (killer_obj->type == OBJ_OBSERVER))
1137 kpnum = killer_obj->id;
1138 else if (killer_obj->type == OBJ_ROBOT || killer_obj->type == OBJ_BUILDING) {
1139 // countermeasure kill
1140 kpnum = DMFCBase->GetCounterMeasureOwner(killer_obj);
1141 } else {
1142 kpnum = -1;
1143 }
1144 }
1145
1146 HandlePlayerSpew(victim_pnum);
1147 DMFCBase->OnClientPlayerKilled(killer_obj, victim_pnum);
1148 }
1149
1150 void OnClientPlayerDisconnect(int player_num) {
1151 HandlePlayerSpew(player_num);
1152 DMFCBase->OnClientPlayerDisconnect(player_num);
1153 }
1154
1155 void OnPlayerEntersObserver(int pnum, object *piggy) {
1156 HandlePlayerSpew(pnum);
1157 DMFCBase->OnPlayerEntersObserver(pnum, piggy);
1158 }
1159
1160 bool OnCanChangeTeam(int pnum, int newteam) {
1161 if (!DMFCBase->OnCanChangeTeam(pnum, newteam))
1162 return false;
1163
1164 if (GetFlagCountForPlayer(pnum)) {
1165 DMFCBase->AnnounceTeamChangeDeny(pnum);
1166 return false;
1167 }
1168
1169 return true;
1170 }
1171
1172 bool compare_slots(int a, int b) {
1173 int ascore, bscore;
1174 player_record *apr, *bpr;
1175 tPlayerStat *astat, *bstat;
1176
1177 apr = DMFCBase->GetPlayerRecord(a);
1178 bpr = DMFCBase->GetPlayerRecord(b);
1179 if (!apr)
1180 return true;
1181 if (!bpr)
1182 return false;
1183 if (apr->state == STATE_EMPTY)
1184 return true;
1185 if (bpr->state == STATE_EMPTY)
1186 return false;
1187 astat = (tPlayerStat *)apr->user_info;
1188 bstat = (tPlayerStat *)bpr->user_info;
1189
1190 if ((apr->state == STATE_INGAME) && (bpr->state == STATE_INGAME)) {
1191 // both players were in the game
1192 ascore = (astat) ? astat->Score[DSTAT_LEVEL] : 0;
1193 bscore = (bstat) ? bstat->Score[DSTAT_LEVEL] : 0;
1194 return (ascore < bscore);
1195 }
1196 if ((apr->state == STATE_INGAME) && (bpr->state == STATE_DISCONNECTED)) {
1197 // apr gets priority since he was in the game on exit
1198 return false;
1199 }
1200 if ((apr->state == STATE_DISCONNECTED) && (bpr->state == STATE_INGAME)) {
1201 // bpr gets priority since he was in the game on exit
1202 return true;
1203 }
1204 // if we got here then both players were disconnected
1205 ascore = (astat) ? astat->Score[DSTAT_LEVEL] : 0;
1206 bscore = (bstat) ? bstat->Score[DSTAT_LEVEL] : 0;
1207 return (ascore < bscore);
1208 }
1209
1210 void OnPLRInit(void) {
1211 int tempsort[MAX_PLAYER_RECORDS];
1212 int i, t, j;
1213
1214 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
1215 tempsort[i] = i;
1216 }
1217
1218 for (i = 1; i <= MAX_PLAYER_RECORDS - 1; i++) {
1219 t = tempsort[i];
1220 // Shift elements down until
1221 // insertion point found.
1222 for (j = i - 1; j >= 0 && compare_slots(tempsort[j], t); j--) {
1223 tempsort[j + 1] = tempsort[j];
1224 }
1225 // insert
1226 tempsort[j + 1] = t;
1227 }
1228
1229 // copy the array over
1230 memcpy(SortedPlayers, tempsort, MAX_PLAYER_RECORDS * sizeof(int));
1231
1232 // Now fill in the final structure of sorted names
1233 int TeamCount[DLLMAX_TEAMS];
1234 int team;
1235 for (i = 0; i < DLLMAX_TEAMS; i++)
1236 TeamCount[i] = 0;
1237 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
1238 int slot = SortedPlayers[i];
1239 player_record *pr = DMFCBase->GetPlayerRecord(slot);
1240 if (pr && pr->state != STATE_EMPTY) {
1241
1242 if (DMFCBase->IsPlayerDedicatedServer(pr))
1243 continue; // skip dedicated server
1244
1245 team = (pr->state == STATE_INGAME) ? dPlayers[pr->pnum].team : pr->team;
1246
1247 if (team >= CTFNumOfTeams)
1248 team = 0;
1249
1250 SortedPLRPlayers[team][TeamCount[team]] = slot;
1251
1252 TeamCount[team]++;
1253 }
1254 }
1255 for (i = 0; i < DLLMAX_TEAMS; i++) {
1256 if (TeamCount[i] < MAX_PLAYER_RECORDS)
1257 SortedPLRPlayers[i][TeamCount[i]] = -1;
1258 }
1259
1260 DMFCBase->OnPLRInit();
1261 }
1262
1263 void OnPLRInterval(void) {
1264 DMFCBase->OnPLRInterval();
1265
1266 int TeamCol = 35;
1267 int NameCol = 180;
1268 int PointsCol = 320;
1269 int KillsCol = 370;
1270 int DeathsCol = 410;
1271 int SuicidesCol = 450;
1272 int y = 40;
1273 int slot;
1274 bool had_members;
1275 player_record *pr;
1276 tPlayerStat *stat;
1277
1278 DLLgrtext_SetFont((DMFCBase->GetGameFontTranslateArray())[SMALL_UI_FONT_INDEX]);
1279
1280 int height = DLLgrfont_GetHeight((DMFCBase->GetGameFontTranslateArray())[SMALL_UI_FONT_INDEX]) + 1;
1281
1282 // print out header
1283 DLLgrtext_SetColor(GR_RGB(255, 255, 150));
1284 DLLgrtext_Printf(PointsCol, y, TXT_POINTS);
1285 DLLgrtext_Printf(NameCol, y, TXT_PILOT);
1286 DLLgrtext_Printf(KillsCol, y, TXT_KILLS_SHORT);
1287 DLLgrtext_Printf(DeathsCol, y, TXT_DEATHS_SHORT);
1288 DLLgrtext_Printf(SuicidesCol, y, TXT_SUICIDES_SHORT);
1289 y += height;
1290
1291 bool doing_connected = true;
1292
1293 do_disconnected_folk:
1294
1295 for (int team = 0; team < CTFNumOfTeams; 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_TEAMSCOREFORM, DMFCBase->GetTeamString(team), TeamScores[team],
1320 OverallTeamScores[team]);
1321 }
1322 had_members = false;
1323
1324 for (int index = 0; index < MAX_PLAYER_RECORDS; index++) {
1325 // get the player num
1326 slot = SortedPLRPlayers[team][index];
1327 if (slot == -1) // we are done with this team
1328 break;
1329
1330 pr = DMFCBase->GetPlayerRecord(slot);
1331 if (!pr || pr->state == STATE_EMPTY)
1332 continue;
1333
1334 if (DMFCBase->IsPlayerDedicatedServer(pr))
1335 continue;
1336
1337 if ((doing_connected && DMFCBase->WasPlayerInGameAtLevelEnd(slot) == -1) ||
1338 (!doing_connected && DMFCBase->WasPlayerInGameAtLevelEnd(slot) != -1))
1339 continue; // we're not handling them right now
1340
1341 stat = (tPlayerStat *)pr->user_info;
1342
1343 int pnum = DMFCBase->WasPlayerInGameAtLevelEnd(slot);
1344 if (pnum != -1) {
1345 DLLgrtext_SetColor(DMFCBase->GetTeamColor(team));
1346 } else {
1347 DLLgrtext_SetColor(GR_RGB(128, 128, 128));
1348 }
1349
1350 // valid player
1351 char temp[120];
1352 snprintf(temp, sizeof(temp), "%s", pr->callsign);
1353 DMFCBase->ClipString(PointsCol - NameCol - 10, temp, true);
1354
1355 DLLgrtext_Printf(NameCol, y, "%s", temp);
1356 DLLgrtext_Printf(PointsCol, y, "%d", (stat) ? stat->Score[DSTAT_LEVEL] : 0);
1357 DLLgrtext_Printf(KillsCol, y, "%d", pr->dstats.kills[DSTAT_LEVEL]);
1358 DLLgrtext_Printf(DeathsCol, y, "%d", pr->dstats.deaths[DSTAT_LEVEL]);
1359 DLLgrtext_Printf(SuicidesCol, y, "%d", pr->dstats.suicides[DSTAT_LEVEL]);
1360 y += height;
1361
1362 if (y >= 440)
1363 goto quick_exit;
1364
1365 had_members = true;
1366
1367 } // end for
1368
1369 if (!had_members) {
1370 // skip a line
1371 y += height;
1372
1373 if (y >= 440)
1374 goto quick_exit;
1375 }
1376 // on to the next team
1377 } // end for
1378
1379 if (doing_connected) {
1380 doing_connected = false;
1381 goto do_disconnected_folk;
1382 }
1383
1384 quick_exit:;
1385 }
1386
1387 void SaveStatsToFile(char *filename) {
1388 CFILE *file;
1389 DLLOpenCFILE(&file, filename, "wt");
1390 if (!file) {
1391 DLLmprintf(0, "Unable to open output file\n");
1392 return;
1393 }
1394
1395 // write out game stats
1396 #define BUFSIZE 150
1397 char buffer[BUFSIZE];
1398 char tempbuffer[25];
1399 int sortedslots[MAX_PLAYER_RECORDS];
1400 player_record *pr, *dpr;
1401 tPInfoStat stat;
1402 tPlayerStat *ps;
1403 int count, p;
1404 int team;
1405
1406 // sort the stats
1407 DMFCBase->GetSortedPlayerSlots(sortedslots, MAX_PLAYER_RECORDS);
1408
1409 int i, t, j;
1410
1411 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
1412 sortedslots[i] = i;
1413 }
1414
1415 for (i = 1; i <= MAX_PLAYER_RECORDS - 1; i++) {
1416 t = sortedslots[i];
1417 // Shift elements down until
1418 // insertion point found.
1419 for (j = i - 1; j >= 0 && compare_slots(sortedslots[j], t); j--) {
1420 sortedslots[j + 1] = sortedslots[j];
1421 }
1422 // insert
1423 sortedslots[j + 1] = t;
1424 }
1425
1426 count = 1;
1427
1428 snprintf(buffer, sizeof(buffer), TXT_STAT_HEADING, (DMFCBase->GetNetgameInfo())->name,
1429 (DMFCBase->GetCurrentMission())->cur_level);
1430 DLLcf_WriteString(file, buffer);
1431
1432 for (i = 0; i < DMFCBase->GetNumTeams(); i++) {
1433 int team = SortedTeams[i];
1434
1435 snprintf(buffer, sizeof(buffer), "%s: %d[%d]", DMFCBase->GetTeamString(team), TeamScores[team],
1436 OverallTeamScores[team]);
1437 DLLcf_WriteString(file, buffer);
1438 }
1439 strcpy(buffer, "\r\n");
1440 DLLcf_WriteString(file, buffer);
1441
1442 // Write team members
1443 DLLcf_WriteString(file, ""); // blank line
1444 for (t = 0; t < DMFCBase->GetNumTeams(); t++) {
1445 int team_i = SortedTeams[t];
1446
1447 snprintf(buffer, sizeof(buffer), "%s:", DMFCBase->GetTeamString(team_i));
1448 DLLcf_WriteString(file, buffer);
1449
1450 for (p = 0; p < MAX_PLAYER_RECORDS; p++) {
1451 pr = DMFCBase->GetPlayerRecord(sortedslots[p]);
1452 if (pr && pr->state != STATE_EMPTY) {
1453
1454 if (DMFCBase->IsPlayerDedicatedServer(pr))
1455 continue; // skip dedicated server
1456
1457 if (pr->team == team_i) { // Check if current team
1458 snprintf(buffer, sizeof(buffer), " %s", pr->callsign);
1459 DLLcf_WriteString(file, buffer);
1460 }
1461 }
1462 }
1463 }
1464 DLLcf_WriteString(file, ""); // blank line
1465
1466 snprintf(buffer, sizeof(buffer), "%s", TXT_STAT_HEADINGA);
1467 DLLcf_WriteString(file, buffer);
1468 strcpy(buffer, "--------------------------------------------------------------------------------------------------");
1469 DLLcf_WriteString(file, buffer);
1470
1471 for (int pslot = 0; pslot < MAX_PLAYER_RECORDS; pslot++) {
1472 int real_slot = sortedslots[pslot];
1473 pr = DMFCBase->GetPlayerRecord(real_slot);
1474 if (pr && pr->state != STATE_EMPTY) {
1475
1476 if (DMFCBase->IsPlayerDedicatedServer(pr))
1477 continue; // skip dedicated server
1478
1479 // we have a valid play
1480 ps = (tPlayerStat *)pr->user_info;
1481 memset(buffer, ' ', BUFSIZE);
1482
1483 team = pr->team;
1484
1485 snprintf(tempbuffer, sizeof(tempbuffer), "%d)", count);
1486 memcpy(&buffer[0], tempbuffer, strlen(tempbuffer));
1487
1488 snprintf(tempbuffer, sizeof(tempbuffer), "%.6s", DMFCBase->GetTeamString(team));
1489 memcpy(&buffer[5], tempbuffer, strlen(tempbuffer));
1490
1491 snprintf(tempbuffer, sizeof(tempbuffer), "%s%s", (pr->state == STATE_INGAME) ? "" : "*", pr->callsign);
1492 memcpy(&buffer[12], tempbuffer, strlen(tempbuffer));
1493
1494 if (ps) {
1495 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", ps->Score[DSTAT_LEVEL], ps->Score[DSTAT_OVERALL]);
1496 memcpy(&buffer[41], tempbuffer, strlen(tempbuffer));
1497 }
1498
1499 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", pr->dstats.kills[DSTAT_LEVEL],
1500 pr->dstats.kills[DSTAT_OVERALL]);
1501 memcpy(&buffer[53], tempbuffer, strlen(tempbuffer));
1502
1503 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", pr->dstats.deaths[DSTAT_LEVEL],
1504 pr->dstats.deaths[DSTAT_OVERALL]);
1505 memcpy(&buffer[65], tempbuffer, strlen(tempbuffer));
1506
1507 snprintf(tempbuffer, sizeof(tempbuffer), "%d[%d]", pr->dstats.suicides[DSTAT_LEVEL],
1508 pr->dstats.suicides[DSTAT_OVERALL]);
1509 memcpy(&buffer[76], tempbuffer, strlen(tempbuffer));
1510
1511 snprintf(tempbuffer, sizeof(tempbuffer), "%s", DMFCBase->GetTimeString(DMFCBase->GetTimeInGame(real_slot)));
1512 memcpy(&buffer[86], tempbuffer, strlen(tempbuffer));
1513
1514 size_t pos = 86 + strlen(tempbuffer) + 1;
1515 if (pos < BUFSIZE)
1516 buffer[pos] = '\0';
1517
1518 buffer[BUFSIZE - 1] = '\0';
1519 DLLcf_WriteString(file, buffer);
1520
1521 count++;
1522 }
1523 } // end for
1524
1525 DLLcf_WriteString(file, TXT_STAT_HEADINGB);
1526
1527 count = 1;
1528 for (int s = 0; s < MAX_PLAYER_RECORDS; s++) {
1529 p = sortedslots[s];
1530 pr = DMFCBase->GetPlayerRecord(p);
1531 if (pr && pr->state != STATE_EMPTY) {
1532
1533 if (DMFCBase->IsPlayerDedicatedServer(pr))
1534 continue; // skip dedicated server
1535
1536 // Write out header
1537 snprintf(buffer, sizeof(buffer), "%d) %s%s", count, (pr->state == STATE_INGAME) ? "" : "*", pr->callsign);
1538 DLLcf_WriteString(file, buffer);
1539 size_t length = strlen(buffer);
1540 memset(buffer, '=', length);
1541 buffer[length] = '\0';
1542 DLLcf_WriteString(file, buffer);
1543
1544 // time in game
1545 snprintf(buffer, sizeof(buffer), TXT_STAT_TIG, DMFCBase->GetTimeString(DMFCBase->GetTimeInGame(p)));
1546 DLLcf_WriteString(file, buffer);
1547
1548 if (DMFCBase->FindPInfoStatFirst(p, &stat)) {
1549 snprintf(buffer, sizeof(buffer), "%s", TXT_STAT_HEADINGC);
1550 DLLcf_WriteString(file, buffer);
1551
1552 if (stat.slot != p) {
1553 memset(buffer, ' ', BUFSIZE);
1554 dpr = DMFCBase->GetPlayerRecord(stat.slot);
1555
1556 if (dpr) {
1557 snprintf(tempbuffer, sizeof(tempbuffer), "%s", dpr->callsign);
1558 memcpy(buffer, tempbuffer, strlen(tempbuffer));
1559
1560 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.kills);
1561 memcpy(&buffer[30], tempbuffer, strlen(tempbuffer));
1562
1563 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.deaths);
1564 memcpy(&buffer[40], tempbuffer, strlen(tempbuffer));
1565
1566 size_t pos = 40 + strlen(tempbuffer) + 1;
1567 if (pos < BUFSIZE)
1568 buffer[pos] = '\0';
1569
1570 buffer[BUFSIZE - 1] = '\0';
1571 DLLcf_WriteString(file, buffer);
1572 }
1573 }
1574
1575 while (DMFCBase->FindPInfoStatNext(&stat)) {
1576 if (stat.slot != p) {
1577 memset(buffer, ' ', BUFSIZE);
1578 dpr = DMFCBase->GetPlayerRecord(stat.slot);
1579 if (dpr) {
1580 snprintf(tempbuffer, sizeof(tempbuffer), "%s", dpr->callsign);
1581 memcpy(buffer, tempbuffer, strlen(tempbuffer));
1582
1583 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.kills);
1584 memcpy(&buffer[30], tempbuffer, strlen(tempbuffer));
1585
1586 snprintf(tempbuffer, sizeof(tempbuffer), "%d", stat.deaths);
1587 memcpy(&buffer[40], tempbuffer, strlen(tempbuffer));
1588
1589 size_t pos = 40 + strlen(tempbuffer) + 1;
1590 if (pos < BUFSIZE)
1591 buffer[pos] = '\0';
1592
1593 buffer[BUFSIZE - 1] = '\0';
1594 DLLcf_WriteString(file, buffer);
1595 }
1596 }
1597 }
1598 }
1599 DMFCBase->FindPInfoStatClose();
1600 DLLcf_WriteString(file, ""); // skip a line
1601 count++;
1602 }
1603 }
1604
1605 // done writing stats
1606 DLLcfclose(file);
1607 DLLAddHUDMessage(TXT_STAT_SAVED);
1608 }
1609
1610 #define ROOTFILENAME "CTF"
1611 void OnSaveStatsToFile(void) {
1612 char filename[256];
1613 DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, false);
1614 SaveStatsToFile(filename);
1615 }
1616
1617 void OnLevelEndSaveStatsToFile(void) {
1618 char filename[256];
1619 DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, true);
1620 SaveStatsToFile(filename);
1621 }
1622
1623 void OnDisconnectSaveStatsToFile(void) {
1624 char filename[256];
1625 DMFCBase->GenerateStatFilename(filename, ROOTFILENAME, false);
1626 SaveStatsToFile(filename);
1627 }
1628
1629 void OnPrintScores(int level) {
1630 char buffer[256];
1631 char name[70];
1632 size_t t;
1633 size_t pos[6];
1634 size_t len[6];
1635
1636 for (int i = 0; i < CTFNumOfTeams; i++) {
1637 snprintf(buffer, sizeof(buffer), "%s:%d\n", DMFCBase->GetTeamString(i), TeamScores[i]);
1638 DPrintf(buffer);
1639 }
1640
1641 memset(buffer, ' ', 256);
1642 pos[0] = 0;
1643 t = len[0] = 30; // give ample room for pilot name
1644 pos[1] = pos[0] + t + 1;
1645 t = len[1] = strlen(TXT_POINTS);
1646 pos[2] = pos[1] + t + 1;
1647 t = len[2] = strlen(TXT_KILLS_SHORT);
1648 pos[3] = pos[2] + t + 1;
1649 t = len[3] = strlen(TXT_DEATHS_SHORT);
1650 pos[4] = pos[3] + t + 1;
1651 t = len[4] = strlen(TXT_SUICIDES_SHORT);
1652 pos[5] = pos[4] + t + 1;
1653 t = len[5] = strlen(TXT_PING);
1654
1655 memcpy(&buffer[pos[0]], TXT_PILOT, strlen(TXT_PILOT));
1656 memcpy(&buffer[pos[1]], TXT_POINTS, len[1]);
1657 memcpy(&buffer[pos[2]], TXT_KILLS_SHORT, len[2]);
1658 memcpy(&buffer[pos[3]], TXT_DEATHS_SHORT, len[3]);
1659 memcpy(&buffer[pos[4]], TXT_SUICIDES_SHORT, len[4]);
1660 memcpy(&buffer[pos[5]], TXT_PING, len[5]);
1661 buffer[pos[5] + len[5] + 1] = '\n';
1662 buffer[pos[5] + len[5] + 2] = '\0';
1663 DPrintf(buffer);
1664
1665 int slot;
1666 player_record *pr;
1667 int pcount;
1668
1669 if (level < 0 || level >= MAX_PLAYER_RECORDS)
1670 pcount = MAX_PLAYER_RECORDS;
1671 else
1672 pcount = level;
1673
1674 int sortedplayers[MAX_PLAYER_RECORDS];
1675 DMFCBase->GetSortedPlayerSlots(sortedplayers, MAX_PLAYER_RECORDS);
1676
1677 for (int i = 0; i < pcount; i++) {
1678 slot = sortedplayers[i];
1679 pr = DMFCBase->GetPlayerRecord(slot);
1680 if ((pr) && (pr->state != STATE_EMPTY)) {
1681
1682 if (DMFCBase->IsPlayerDedicatedServer(pr))
1683 continue; // skip dedicated server
1684
1685 snprintf(name, sizeof(name), "%s%s: %.8s", (pr->state == STATE_DISCONNECTED) ? "*" : "", pr->callsign,
1686 DMFCBase->GetTeamString(pr->team));
1687 name[29] = '\0';
1688
1689 tPlayerStat *stat;
1690 stat = (tPlayerStat *)pr->user_info;
1691
1692 memset(buffer, ' ', 256);
1693 t = strlen(name);
1694 memcpy(&buffer[pos[0]], name, (t < len[0]) ? t : len[0]);
1695 snprintf(name, sizeof(name), "%d", (stat) ? stat->Score[DSTAT_LEVEL] : 0);
1696 t = strlen(name);
1697 memcpy(&buffer[pos[1]], name, (t < len[1]) ? t : len[1]);
1698 snprintf(name, sizeof(name), "%d", pr->dstats.kills[DSTAT_LEVEL]);
1699 t = strlen(name);
1700 memcpy(&buffer[pos[2]], name, (t < len[2]) ? t : len[2]);
1701 snprintf(name, sizeof(name), "%d", pr->dstats.deaths[DSTAT_LEVEL]);
1702 t = strlen(name);
1703 memcpy(&buffer[pos[3]], name, (t < len[3]) ? t : len[3]);
1704 snprintf(name, sizeof(name), "%d", pr->dstats.suicides[DSTAT_LEVEL]);
1705 t = strlen(name);
1706 memcpy(&buffer[pos[4]], name, (t < len[4]) ? t : len[4]);
1707
1708 if (pr->state == STATE_INGAME)
1709 snprintf(name, sizeof(name), "%.0f", dNetPlayers[pr->pnum].ping_time * 1000.0f);
1710 else
1711 strcpy(name, "---");
1712 t = strlen(name);
1713 memcpy(&buffer[pos[5]], name, (t < len[5]) ? t : len[5]);
1714 buffer[pos[5] + len[5] + 1] = '\n';
1715 buffer[pos[5] + len[5] + 2] = '\0';
1716 DPrintf(buffer);
1717 }
1718 }
1719 }
1720
1721 // DMFCApp::HandlePlayerSpew
1722 //
1723 // If a player dies, becomes an observer or disconnects, it's possible that they
1724 // have a flag or more. This function will handle setting certain variables, timer
1725 // and other various settings related to a flag spew
1726 void HandlePlayerSpew(int pnum) {
1727 // Handle dropping the flag if the player had one
1728 uint8_t flaginfo = GetFlagCountForPlayer(pnum);
1729 uint8_t flagmask = GetFlagMaskForPlayer(pnum);
1730
1731 if (flaginfo == 0) {
1732 return;
1733 }
1734
1735 // Ok the player has flags, lets make sure everything that needs to happen, happens
1736 // 1) We need to clear flags and colored ball indicator
1737 // 2) If they are dying in a homebase of one of the flags they have, than set the
1738 // flag home.
1739 // 3) Start a timer if the flag spews outside of a base
1740
1741 // If we got here than the player has a flag
1742 vector fpos; // used to hold the center pos of the room
1743
1744 bool play_lose = false; // set true if the player lost a flag (ummm, this is guaranteed)
1745
1746 // loop through all the possible flags and check to see what they had
1747 for (int color = 0; color < DLLMAX_TEAMS; color++) {
1748 // check if the ID exists, and if this player had that color flag
1749 if ((FlagIDs[color] != -1) && (flagmask & 0x01)) {
1750
1751 play_lose = true;
1752
1753 // now check to see if they are in the flag's home goal
1754 if (GoalRooms[color] == dObjects[pnum].roomnum) {
1755 // we are!
1756 // so remove the flag from the inventory (so it don't get spewed)
1757 DLLInvRemove(pnum, OBJ_POWERUP, FlagIDs[color]);
1758 DoFlagReturnedHome(color);
1759
1760 // set the at home flag
1761 FlagAtHome[color] = true;
1762 if (DMFCBase->GetLocalRole() == LR_SERVER) {
1763 // if we are the server put the flag back in the center of the goal and tell all the players to do the same
1764 DLLComputeRoomCenter(&fpos, &dRooms[GoalRooms[color]]);
1765 int objnum = DLLObjCreate(OBJ_POWERUP, FlagIDs[color], GoalRooms[color], &fpos, NULL, OBJECT_HANDLE_NONE);
1766 DLLMultiSendObject(&dObjects[objnum], 1, true);
1767 }
1768 } else {
1769 // ok the flag is spewing out into the great beyond
1770 // Start the timer!
1771 Flag_timeout_timers[color] = DMFCBase->GetGametime() + FLAG_TIMEOUT_VALUE;
1772 }
1773
1774 // player has the flag
1775 LoseFlagForPlayer(pnum, color, false);
1776 }
1777 // rotate flagmask so it is pointing at the next bit
1778 flagmask = (flagmask >> 1);
1779 }
1780
1781 // Reset colored balls
1782 SetColoredBalls(pnum, true);
1783
1784 if (play_lose)
1785 DoLoseFlag(DMFCBase->GetPlayerTeam(pnum));
1786 }
1787
1788 /////////////////////////////////////////////////////////////
1789 // CTF Functions
1790 void DisplayHUDScores(struct tHUDItem *hitem) {
1791 if (display_my_welcome) {
1792 // announce our arrival to ourselves (as server)
1793 DisplayWelcomeMessage(DMFCBase->GetPlayerNum());
1794 display_my_welcome = false;
1795 }
1796
1797 if (!First_game_frame || DisplayScoreScreen) // interval hasn't been called yet or we are display the stats
1798 return;
1799
1800 uint8_t alpha = DMFCBase->ConvertHUDAlpha((uint8_t)((DisplayScoreScreen) ? 128 : 255));
1801
1802 int height = DLLgrfont_GetHeight((DMFCBase->GetGameFontTranslateArray())[HUD_FONT_INDEX]) + 3;
1803
1804 int y = 170;
1805 int x = 540;
1806 int team = DMFCBase->GetPlayerTeam(DMFCBase->GetPlayerNum());
1807 int myteam = team;
1808 int i;
1809
1810 // Flag bitmaps
1811 // if (HasFlag[team]!=-1) then draw a dimmed flag with the team flag of who has it on top
1812 // if (HasFlag[team]==-1) && (FlagAtHome[team]==true) then draw normal flag
1813 // if (HasFlag[team]==-1) && (FlagAtHome[team]==false) then draw dimmed flag
1814 int cx, cy;
1815 for (i = 0; i < CTFNumOfTeams; i++) {
1816 // first determine the x and y
1817 switch (i) {
1818 case RED_TEAM:
1819 cx = x;
1820 cy = height + 5;
1821 break;
1822 case BLUE_TEAM:
1823 cx = x + 33;
1824 cy = height + 5;
1825 break;
1826 case GREEN_TEAM:
1827 cx = x;
1828 cy = 2 * height + 41;
1829 break;
1830 case YELLOW_TEAM:
1831 cx = x + 33;
1832 cy = 2 * height + 41;
1833 break;
1834 }
1835
1836 if ((WhoJustFlagged != i) || (DisplayFlagBlink)) {
1837 if (HasFlag[i] != -1) {
1838 // draw dimmed flag with corner as the team who has the flag
1839 DLLRenderHUDQuad(cx, cy, DLLbm_w(DimFlagAHBmp[i], 0), DLLbm_h(DimFlagAHBmp[i], 0), 0, 0, 1, 1, DimFlagAHBmp[i],
1840 alpha, 0);
1841 DLLRenderHUDQuad(cx + 16, cy, 16, 16, 0, 0, 1, 1, FlagBmp[DMFCBase->GetPlayerTeam(HasFlag[i])], alpha, 0);
1842 } else {
1843 if (FlagAtHome[i]) {
1844 // draw normal flag
1845 DLLRenderHUDQuad(cx, cy, DLLbm_w(FlagAHBmp[i], 0), DLLbm_h(FlagAHBmp[i], 0), 0, 0, 1, 1, FlagAHBmp[i], alpha,
1846 0);
1847 } else {
1848 // draw dimmed flag
1849 int f_height = DLLbm_h(DimFlagAHBmp[i], 0);
1850 DLLRenderHUDQuad(cx, cy, DLLbm_w(DimFlagAHBmp[i], 0), f_height, 0, 0, 1, 1, DimFlagAHBmp[i], alpha, 0);
1851
1852 // Draw timeout time
1853 int time_left;
1854 time_left = (int)(Flag_timeout_timers[i] - DMFCBase->GetGametime());
1855 if (time_left > 0 && Flag_timeout_timers[i] != 0) {
1856 int minutes;
1857 minutes = time_left / 60;
1858 time_left = time_left % 60;
1859 DLLRenderHUDText(GR_GREEN, alpha, 0, cx, cy + 2 + f_height, "%01d:%02d", minutes, time_left);
1860 }
1861 }
1862 }
1863 }
1864 }
1865
1866 x = 520;
1867
1868 if (Highlight_bmp <= BAD_BITMAP_HANDLE) {
1869 // write the name of your team at the top of the screen since for some reason we don't a highlight bmp
1870 DLLRenderHUDText(DMFCBase->GetTeamColor(team), alpha, 0, x, y, TXT_TEAMFORM, DMFCBase->GetTeamString(team));
1871 y += height;
1872 }
1873
1874 // determine coordinates to use here
1875 // we'll use a virtual width of 85 pixels on a 640x480 screen
1876 // so first determine the new width
1877 int name_width = 85.0f * DMFCBase->GetHudAspectX();
1878 int score_width = DLLgrtext_GetTextLineWidth("888");
1879 int name_x = DMFCBase->GetGameWindowW() - name_width - score_width - 10;
1880 int score_x = DMFCBase->GetGameWindowW() - score_width - 5;
1881 y = (DMFCBase->GetGameWindowH() / 2) - ((height * DMFCBase->GetNumTeams()) / 2);
1882
1883 // draw the team scores
1884 for (team = 0; team < DLLMAX_TEAMS; team++) {
1885 i = SortedTeams[team];
1886
1887 if (i >= CTFNumOfTeams)
1888 continue;
1889
1890 if ((WhoJustScored != i) || (DisplayScoreBlink)) {
1891
1892 // determine the number of players on the team
1893 int num_players;
1894 num_players = 0;
1895 for (int w = 0; w < DLLMAX_PLAYERS; w++) {
1896 if (DMFCBase->CheckPlayerNum(w) && dPlayers[w].team == i && !DMFCBase->IsPlayerDedicatedServer(w))
1897 num_players++;
1898 }
1899
1900 if (i == myteam && Highlight_bmp > BAD_BITMAP_HANDLE) {
1901 // draw the highlite bar in the background
1902 DLLrend_SetAlphaValue(alpha * 0.50f);
1903 DLLrend_SetZBufferState(0);
1904 DLLrend_SetTextureType(TT_LINEAR);
1905 DLLrend_SetLighting(LS_NONE);
1906 DLLrend_SetAlphaType(AT_CONSTANT_TEXTURE);
1907
1908 DLLrend_DrawScaledBitmap(name_x - 2, y - 2, score_x + score_width + 2, y + height - 1, Highlight_bmp, 0, 0, 1,
1909 1, 1.0, BITMAP_FORMAT_1555, NULL);
1910 DLLrend_SetZBufferState(1);
1911 }
1912
1913 char team_name[MAX_TEAMNAME_LEN + 5];
1914 snprintf(team_name, sizeof(team_name), "[%d]%s", num_players, DMFCBase->GetTeamString(i));
1915 // DMFCBase->ClipString(615 - x - 10,team_name,true);
1916 // DLLRenderHUDText(DMFCBase->GetTeamColor(i),alpha,0,x,y,team_name);
1917 // DLLRenderHUDText(DMFCBase->GetTeamColor(i),alpha,0,615,y,"%d",TeamScores[i]);
1918 DMFCBase->ClipString(name_width, team_name, true);
1919
1920 DLLgrtext_SetAlpha(alpha);
1921 DLLgrtext_SetColor(DMFCBase->GetTeamColor(i));
1922 DLLgrtext_Printf(name_x, y, team_name);
1923 DLLgrtext_Printf(score_x, y, "%d", TeamScores[i]);
1924 }
1925 y += height;
1926 }
1927
1928 // draw the bitmap for any flags you have currently
1929 int flagcount;
1930 int flagmask;
1931
1932 flagcount = GetFlagCountForPlayer(DMFCBase->GetPlayerNum());
1933 flagmask = GetFlagMaskForPlayer(DMFCBase->GetPlayerNum());
1934
1935 if (flagcount == 0)
1936 return;
1937
1938 y += 5;
1939
1940 for (i = 0; i < CTFNumOfTeams; i++) {
1941 if (flagmask & 0x01) {
1942 // we have the flag
1943 DLLRenderHUDQuad(x, y, DLLbm_w(FlagBmp[i], 0) / 2, DLLbm_h(FlagBmp[i], 0) / 2, 0, 0, 1, 1, FlagBmp[i], alpha, 0);
1944 x += DLLbm_w(FlagBmp[i], 0) + 3;
1945 }
1946 // adjust the flagmask for the next flag
1947 flagmask = flagmask >> 1;
1948 }
1949 }
1950
1951 #define compGT(a, b) (a < b)
1952 void SortTeamScores(int *sortedindex, int *scores) {
1953 int t;
1954 int i, j;
1955
1956 // copy scores into scoreinfo array
1957 for (i = 0; i < DLLMAX_TEAMS; i++) {
1958 sortedindex[i] = i;
1959 }
1960
1961 for (i = 1; i <= DLLMAX_TEAMS - 1; i++) {
1962 t = sortedindex[i];
1963
1964 // Shift elements down until
1965 // insertion point found.
1966 for (j = i - 1; j >= 0 && compGT(scores[sortedindex[j]], scores[t]); j--) {
1967 sortedindex[j + 1] = sortedindex[j];
1968 }
1969 // insert
1970 sortedindex[j + 1] = t;
1971 }
1972 }
1973
1974 void DisplayWelcomeMessage(int player_num) {
1975 char name_buffer[64];
1976 strcpy(name_buffer, (DMFCBase->GetPlayers())[player_num].callsign);
1977
1978 if (player_num == DMFCBase->GetPlayerNum()) {
1979 DLLAddHUDMessage(TXT_WELCOME, name_buffer);
1980 } else {
1981 DLLAddHUDMessage(TXT_JOINED, name_buffer);
1982 }
1983 }
1984
1985 // adds a colored ball to a player (without removing what he has already)
1986 void SetColoredBalls(int playernum, bool reset) {
1987 float redb[4];
1988 float greenb[4];
1989 float blueb[4];
1990
1991 if (reset) {
1992 DLLPlayerSetRotatingBall(playernum, 0, 0, NULL, NULL, NULL);
1993 return;
1994 }
1995
1996 int flagcount;
1997 int flagmask;
1998
1999 bool red, blue, green, yellow;
2000 red = blue = green = yellow = false;
2001
2002 flagcount = GetFlagCountForPlayer(playernum);
2003 flagmask = GetFlagMaskForPlayer(playernum);
2004
2005 if (flagcount == 0) {
2006 DLLPlayerSetRotatingBall(playernum, 0, 0, NULL, NULL, NULL);
2007 return;
2008 }
2009
2010 if (flagmask & FLAGMASK_REDTEAM)
2011 red = true;
2012 if (flagmask & FLAGMASK_BLUETEAM)
2013 blue = true;
2014 if (flagmask & FLAGMASK_GREENTEAM)
2015 green = true;
2016 if (flagmask & FLAGMASK_YELLOWTEAM)
2017 yellow = true;
2018
2019 for (int i = 0; i < flagcount; i++) {
2020 if (red) {
2021 red = false;
2022 redb[i] = 1;
2023 greenb[i] = 0;
2024 blueb[i] = 0;
2025 } else if (blue) {
2026 blue = false;
2027 redb[i] = 0;
2028 greenb[i] = 0;
2029 blueb[i] = 1;
2030 } else if (green) {
2031 green = false;
2032 redb[i] = 0;
2033 greenb[i] = 1;
2034 blueb[i] = 0;
2035 } else if (yellow) {
2036 yellow = false;
2037 redb[i] = 1;
2038 greenb[i] = 1;
2039 blueb[i] = 0;
2040 }
2041 }
2042
2043 DLLPlayerSetRotatingBall(playernum, flagcount, 0, redb, greenb, blueb);
2044 }
2045
2046 // Allows for dynamic changing of the number of teams in the game
2047 void ChangeNumberOfTeams(int newsize) {
2048 CTFNumOfTeams = newsize;
2049 DMFCBase->SetNumberOfTeams(CTFNumOfTeams);
2050 }
2051
2052 void DoFlagReturnedHome(int team) {
2053 if (DMFCBase->GetPlayerTeam(DMFCBase->GetPlayerNum()) == team) {
2054 if (snd_return_ownteam != -1)
2055 DLLPlay2dSound(snd_return_ownteam, MAX_GAME_VOLUME / 2);
2056 } else {
2057 if (snd_return_otherteam != -1)
2058 DLLPlay2dSound(snd_return_otherteam, MAX_GAME_VOLUME / 2);
2059 }
2060 }
2061
2062 void DoLoseFlag(int team) {
2063 if (DMFCBase->GetPlayerTeam(DMFCBase->GetPlayerNum()) == team) {
2064 if (snd_lose_ownteam != -1)
2065 DLLPlay2dSound(snd_lose_ownteam, MAX_GAME_VOLUME / 2);
2066 } else {
2067 if (snd_lose_otherteam != -1)
2068 DLLPlay2dSound(snd_lose_otherteam, MAX_GAME_VOLUME / 2);
2069 }
2070 }
2071
2072 // VerifyFlagPosition
2073 // Call this at certain intervals to verify a flags position. It will look at the
2074 // flags room and makes sure that if it's in it's home room, the correct values
2075 // are set, and vice versa.
2076 void VerifyFlagPosition(void) {}
2077
2078 // AddFlagToPlayer
2079 // gives a player a flag
2080 bool AddFlagToPlayer(int pnum, int team, int flagobjnum) {
2081 object *pobj = &dObjects[dPlayers[pnum].objnum];
2082 bool ret = true;
2083
2084 if (DMFCBase->GetLocalRole() == LR_SERVER) {
2085 // We're the server, so we need to create the flag and tell the clients, and then attach it
2086 flagobjnum = DLLObjCreate(OBJ_POWERUP, AFlagIDs[team], pobj->roomnum, &pobj->pos, NULL, OBJECT_HANDLE_NONE);
2087
2088 if (flagobjnum != -1) {
2089 // tell the clients that the flag has been created and they should attach it
2090 TellClientsToAddorDelFlag(pnum, team, flagobjnum, true);
2091 }
2092
2093 // Attach the flag
2094
2095 // 1st figure out how many flags the guy already has
2096 // -------------------------------------------------
2097 int max_num_attach = 3; // based off chrishack for multiplayer
2098 int num_attached = 0;
2099
2100 if (!pobj->attach_children)
2101 ret = false;
2102 else {
2103 for (int ap = 0; ap < max_num_attach; ap++) {
2104 if (pobj->attach_children[ap] != OBJECT_HANDLE_NONE) {
2105 // attempt to make sure it's a valid object
2106 object *tobj;
2107 if (DLLObjGet(pobj->attach_children[ap], &tobj)) {
2108 num_attached++;
2109 }
2110 }
2111 }
2112
2113 // 2nd num_attached is the current number of flags this object has
2114 // ---------------------------------------------------------------
2115 switch (num_attached) {
2116 case 0: {
2117 // the easy case, nothing is attached, so just attach it to ap0
2118 ret = DLLAttachObject(pobj, 0, &dObjects[flagobjnum], 0, true);
2119 } break;
2120
2121 case 1: {
2122 bool retval;
2123 object *tobj;
2124
2125 // we should have a flag at ap0, move it to ap1
2126 ASSERT(pobj->attach_children[0] != OBJECT_HANDLE_NONE);
2127 int saved_ap0 = pobj->attach_children[0];
2128 retval = DLLUnattachChild(pobj, 0);
2129 ASSERT(retval);
2130
2131 retval = DLLObjGet(saved_ap0, &tobj);
2132 ASSERT(retval);
2133
2134 // attach to ap1
2135 retval = DLLAttachObject(pobj, 1, tobj, 0, true);
2136 ASSERT(retval);
2137
2138 // attach new flag to ap2
2139 retval = DLLAttachObject(pobj, 2, &dObjects[flagobjnum], 0, true);
2140 ASSERT(retval);
2141
2142 ret = retval;
2143
2144 } break;
2145
2146 case 2: {
2147 // we should have a flag at ap1 and ap2
2148 // so just add this to ap3
2149
2150 // attach new flag to ap3
2151 ret = DLLAttachObject(pobj, 0, &dObjects[flagobjnum], 0, true);
2152
2153 } break;
2154
2155 case 3:
2156 default: {
2157 Int3(); // Get Jeff
2158 ret = false;
2159 } break;
2160 }
2161 }
2162
2163 if (!ret) {
2164 // couldn't attach the flag
2165 mprintf(0, "CTF: COULDN'T ATTACH FLAG TO PLAYER, DELETING\n");
2166 // tell the clients to remove this flag
2167 DLLSetObjectDeadFlag(&dObjects[flagobjnum], true, false);
2168 }
2169 }
2170
2171 if (flagobjnum == -1) {
2172 // there was an error creating the flag...not good
2173 mprintf(0, "CTF: Couldn't create/unhash flag for attachment\n");
2174 DMFCBase->DisconnectMe();
2175 return false;
2176 }
2177
2178 ChildFlags[team] = dObjects[flagobjnum].handle;
2179
2180 return ret;
2181 }
2182
2183 // RemoveFlagFromPlayer
2184 // removes a flag from a player
2185 bool RemoveFlagFromPlayer(int pnum, int team) {
2186 int flagobjnum = -1;
2187
2188 if (DMFCBase->GetLocalRole() == LR_SERVER) {
2189
2190 if (ChildFlags[team] == OBJECT_HANDLE_NONE)
2191 return false;
2192
2193 object *fptr;
2194 if (!DLLObjGet(ChildFlags[team], &fptr)) {
2195 // the flag is already dead?!?
2196 return false;
2197 }
2198
2199 DLLSetObjectDeadFlag(fptr, true, false);
2200
2201 // tell the clients to remove the flag
2202 TellClientsToAddorDelFlag(pnum, team, -1, false);
2203 }
2204
2205 ChildFlags[team] = OBJECT_HANDLE_NONE;
2206
2207 return true;
2208 }
2209
2210 void TellClientsToAddorDelFlag(int pnum, int team, int objnum, bool add) {
2211 if (add) {
2212 // if we are adding a flag, than we need to send the object to them to make sure that
2213 // they have it for when we go to process it
2214 DLLMultiSendObject(&dObjects[objnum], false, true);
2215 }
2216
2217 uint8_t data[MAX_GAME_DATA_SIZE];
2218 int size = 0;
2219 DMFCBase->StartPacket(data, SPID_ADDDELFLAG, &size);
2220
2221 MultiAddByte(pnum, data, &size);
2222 MultiAddByte(team, data, &size);
2223 MultiAddByte(add, data, &size);
2224
2225 if (add) {
2226 // add the objnum of the flag and pack it into the packet
2227 MultiAddInt(objnum, data, &size);
2228 }
2229
2230 DMFCBase->SendPacket(data, size, SP_ALL);
2231 }
2232
2233 void ServerIsTellingMeToAddorDelAFlag(uint8_t *data) {
2234 int size = 0;
2235 int pnum, team;
2236 bool add;
2237
2238 pnum = MultiGetByte(data, &size);
2239 team = MultiGetByte(data, &size);
2240 add = (MultiGetByte(data, &size)) ? true : false;
2241
2242 // determine if we should add or delete a flag
2243 if (add) {
2244 int objnum;
2245 objnum = MultiGetInt(data, &size);
2246 objnum = DMFCBase->ConvertServerToLocalObjnum(objnum);
2247 if (objnum == -1) {
2248 // uh oh...corruption???
2249 FatalError("CTF: Server->Local Object Corruption\n");
2250 return;
2251 }
2252 AddFlagToPlayer(pnum, team, objnum);
2253 } else {
2254 RemoveFlagFromPlayer(pnum, team);
2255 }
2256 }
2257
2258 /////////////////////////////////
2259 // Timer event handlers
2260 void OnTimer(void) { DisplayFlagBlink = !DisplayFlagBlink; }
2261 void OnTimerKill(void) {
2262 DisplayFlagBlink = true;
2263 WhoJustFlagged = WhoJustFlaggedTimer = -1;
2264 }
2265
2266 void OnTimerScore(void) { DisplayScoreBlink = !DisplayScoreBlink; }
2267 void OnTimerScoreKill(void) {
2268 DisplayScoreBlink = true;
2269 WhoJustScored = WhoJustScoredTimer = -1;
2270 }
2271
2272 ///////////////////////////////////////////////////////////////////////////////////
2273 int PackByte(uint8_t byte, uint8_t *buffer, int pos) {
2274 buffer[pos] = byte;
2275 pos++;
2276 return pos;
2277 }
2278
2279 int UnPackByte(uint8_t *byte, uint8_t *buffer, int pos) {
2280 *byte = buffer[pos];
2281 pos++;
2282 return pos;
2283 }
2284
2285 int PackBytes(uint8_t *bytes, int count, uint8_t *buffer, int pos) {
2286 memcpy(&buffer[pos], bytes, count);
2287 pos += count;
2288 return pos;
2289 }
2290
2291 int UnPackBytes(uint8_t *bytes, int count, uint8_t *buffer, int pos) {
2292 memcpy(bytes, &buffer[pos], count);
2293 pos += count;
2294 return pos;
2295 }
2296
2297 int PackWord(uint16_t word, uint8_t *buffer, int pos) { return PackBytes((uint8_t *)&word, sizeof(uint16_t), buffer, pos); }
2298
2299 int UnPackWord(uint16_t *word, uint8_t *buffer, int pos) { return UnPackBytes((uint8_t *)word, sizeof(uint16_t), buffer, pos); }
2300
2301 int PackInt(int data, uint8_t *buffer, int pos) { return PackBytes((uint8_t *)&data, sizeof(int), buffer, pos); }
2302
2303 int UnPackInt(int *data, uint8_t *buffer, int pos) { return UnPackBytes((uint8_t *)data, sizeof(int), buffer, pos); }
2304
2305 int PackArray(uint8_t *data, int size, int count, uint8_t *buffer, int pos) {
2306 return PackBytes(data, size * count, buffer, pos);
2307 }
2308
2309 int UnPackArray(uint8_t *data, int size, int count, uint8_t *buffer, int pos) {
2310 return UnPackBytes(data, size * count, buffer, pos);
2311 }
2312
2313 void SendGameState(int pnum) {
2314 uint8_t data[MAX_GAME_DATA_SIZE];
2315 int count = 0;
2316 int i;
2317
2318 DMFCBase->StartPacket(data, SPID_GAMESTATE, &count);
2319
2320 // pack number of teams in this game
2321 MultiAddInt(CTFNumOfTeams, data, &count);
2322
2323 // pack flag whether someone has scored a hattrick
2324 MultiAddByte(Someone_has_hattrick, data, &count);
2325
2326 for (i = 0; i < DLLMAX_TEAMS; i++) {
2327 // Pack FlagAtHome
2328 MultiAddByte(FlagAtHome[i], data, &count);
2329
2330 // pack flag timeout timers
2331 MultiAddFloat(Flag_timeout_timers[i], data, &count);
2332
2333 // pack team scores
2334 MultiAddInt(TeamScores[i], data, &count);
2335
2336 // pack overall team scores
2337 MultiAddInt(OverallTeamScores[i], data, &count);
2338
2339 // pack who has flag data
2340 MultiAddInt(HasFlag[i], data, &count);
2341 }
2342
2343 uint16_t server_objnums[DLLMAX_TEAMS];
2344 for (i = 0; i < DLLMAX_TEAMS; i++) {
2345 if (ChildFlags[i] == OBJECT_HANDLE_NONE) {
2346 // slot not in use
2347 server_objnums[i] = 65535;
2348 } else {
2349 // should be a valid slot
2350 object *obj;
2351
2352 if (!DLLObjGet(ChildFlags[i], &obj)) {
2353 // ok it's not a valid slot...hrm
2354 ChildFlags[i] = OBJECT_HANDLE_NONE;
2355 server_objnums[i] = 65535;
2356 } else {
2357 // it's valid
2358 int objnum = (obj - dObjects);
2359 server_objnums[i] = objnum;
2360 }
2361 }
2362 }
2363
2364 // pack attached flag handles
2365 for (i = 0; i < DLLMAX_TEAMS; i++) {
2366 MultiAddUshort(server_objnums[i], data, &count);
2367 }
2368
2369 DMFCBase->SendPacket(data, count, pnum);
2370 }
2371
2372 void ReceiveGameState(uint8_t *data) {
2373 int count = 0;
2374 int num_teams;
2375 int i;
2376
2377 for (i = 0; i < DLLMAX_PLAYERS; i++) {
2378 // reset everything
2379 SetColoredBalls(i, true);
2380 for (int t = 0; t < DLLMAX_TEAMS; t++) {
2381 while (DLLInvCheckItem(i, OBJ_POWERUP, FlagIDs[t])) {
2382 DLLInvRemove(i, OBJ_POWERUP, FlagIDs[t]);
2383 }
2384 }
2385 }
2386
2387 // Read num teams
2388 num_teams = MultiGetInt(data, &count);
2389 ChangeNumberOfTeams(num_teams);
2390
2391 // see if someone has had a hattrick yet
2392 Someone_has_hattrick = (MultiGetByte(data, &count)) ? true : false;
2393
2394 for (i = 0; i < DLLMAX_TEAMS; i++) {
2395 // UnPack FlagAtHome
2396 FlagAtHome[i] = (MultiGetByte(data, &count)) ? true : false;
2397
2398 // unpack flag timeout timers
2399 Flag_timeout_timers[i] = MultiGetFloat(data, &count);
2400
2401 // unpack team scores
2402 TeamScores[i] = MultiGetInt(data, &count);
2403
2404 // unpack overall team scores
2405 OverallTeamScores[i] = MultiGetInt(data, &count);
2406
2407 // unpack who has flag data
2408 HasFlag[i] = MultiGetInt(data, &count);
2409 }
2410
2411 // go through the HasFlag array and put items in the inventory, and add colored balls
2412 for (i = 0; i < DLLMAX_TEAMS; i++) {
2413 if (HasFlag[i] != -1) {
2414 DLLInvAddTypeID(HasFlag[i], OBJ_POWERUP, FlagIDs[i], -1, -1, 0, NULL);
2415
2416 // put some balls on the guy
2417 SetColoredBalls(HasFlag[i]);
2418 }
2419 }
2420
2421 uint16_t server_objnums[DLLMAX_TEAMS];
2422 // unpack attached flag handles
2423 for (i = 0; i < DLLMAX_TEAMS; i++) {
2424 server_objnums[i] = MultiGetUshort(data, &count);
2425 }
2426
2427 // the final step, match the server_objnums->ChildFlags and Attach the flags
2428 for (i = 0; i < DLLMAX_TEAMS; i++) {
2429 if (server_objnums[i] != 65535) {
2430 // we should have a real object here
2431 uint16_t our_objnum = DMFCBase->ConvertServerToLocalObjnum(server_objnums[i]);
2432
2433 if (our_objnum == -1) {
2434 // fatal error
2435 mprintf(0, "CTF: Local Objnums don't match server objnums\n");
2436 ChildFlags[i] = OBJECT_HANDLE_NONE;
2437 DMFCBase->DisconnectMe();
2438 } else {
2439 // yeah! a valid objnum, attach it!!!
2440 if (HasFlag[i] != -1)
2441 AddFlagToPlayer(HasFlag[i], i, our_objnum);
2442 else {
2443 // hmm, HasFlag doesn't match ChildFlags
2444 mprintf(0, "CTF: HasFlag doesn't match ChildFlags!!!!\n");
2445 ChildFlags[i] = OBJECT_HANDLE_NONE;
2446 }
2447 }
2448 } else {
2449 // ok invalid...
2450 ChildFlags[i] = OBJECT_HANDLE_NONE;
2451 }
2452 }
2453 }
2454
2455 /*
2456 ********************************************************************************************
2457 */
2458
2459 // returns the number of flags a player has, 0 if none, or an invalid pnum
2460 int GetFlagCountForPlayer(int pnum) {
2461 // 1st check the pnum, make sure it is OK, is it isn't, return 0
2462 if (pnum < 0 || pnum >= DLLMAX_PLAYERS) {
2463 // invalid player number, return 0 flags
2464 mprintf(0, "CTF: Invalid PNUM passed to GetFlagCountForPlayer()\n");
2465 return 0;
2466 }
2467
2468 int flag_count = 0;
2469 // 2nd, go through all the team flags, and check the player's inventory, see if they have the ID,
2470 // if so, add it to their count
2471 for (int i = 0; i < DLLMAX_TEAMS; i++) {
2472 if (DLLInvCheckItem(pnum, OBJ_POWERUP, FlagIDs[i])) {
2473 // they have this flag
2474 flag_count++;
2475 }
2476 }
2477
2478 return flag_count;
2479 }
2480
2481 // returns the mask of which flags this player currently has
2482 uint8_t GetFlagMaskForPlayer(int pnum) {
2483 // 1st check the pnum, make sure it is OK, if it isn't, return 0, meaning no flags
2484 if (pnum < 0 || pnum >= DLLMAX_PLAYERS) {
2485 // invalid player number, return 0 flags
2486 mprintf(0, "CTF: Invalid PNUM passed to GetFlagMaskForPlayer()\n");
2487 return 0;
2488 }
2489
2490 int flag_mask = 0;
2491 uint8_t mask = 0x01;
2492
2493 // 2nd go through all the teams flags, and check the player's inventory, see if they have the ID,
2494 // if so, OR the current mask to the running flag_mask
2495 for (int i = 0; i < DLLMAX_TEAMS; i++) {
2496 if (DLLInvCheckItem(pnum, OBJ_POWERUP, FlagIDs[i])) {
2497 // the have this flag
2498 flag_mask |= mask;
2499 }
2500 // adjust the mask because we are moving to the next flag
2501 mask = mask << 1;
2502 }
2503
2504 return flag_mask;
2505 }
2506
2507 // adds a flag to a player, as a precaution, it will go through all the players and makes sure that no one
2508 // has the flag that is being added. If they are adding the flag, than remove that flag from whoever we thought
2509 // had it it will return false if it had to remove a flag from a player
2510 bool GivePlayerFlag(int pnum, uint8_t team) {
2511 // 1st check the player num, make sure it is valid
2512 if (!DMFCBase->CheckPlayerNum(pnum)) {
2513 // not a valid player
2514 mprintf(0, "CTF: Invalid pnum passed to GivePlayerFlag()\n");
2515 return false;
2516 }
2517
2518 // 2nd check to make sure the team given is valid, and not our own team
2519 if (team >= CTFNumOfTeams) {
2520 // not a valid team
2521 mprintf(0, "CTF: Invalid team passed to GivePlayerFlag() (team>=CTFNumOfTeams)\n");
2522 return false;
2523 }
2524 if (team == DMFCBase->GetPlayerTeam(pnum)) {
2525 // we can't add a flag of the same team to a player
2526 mprintf(0, "CTF: In GivePlayerFlag(), trying to add a player's home team flag\n");
2527 return false;
2528 }
2529
2530 // 3rd, make sure no one else currently has this flag
2531 // we'll check our HasFlags[] first
2532 if (HasFlag[team] != -1) {
2533 // hmm, we have someone listed as already having this flag...odd
2534 mprintf(0, "CTF: In GivePlayerFlag(), trying to add a flag, but we see someone else should already have it\n");
2535 int player = HasFlag[team];
2536 if (DMFCBase->CheckPlayerNum(player)) {
2537 // this player is in the game...
2538 // make sure this player doesn't have the flag in his inventory
2539 while (DLLInvCheckItem(player, OBJ_POWERUP, FlagIDs[team])) {
2540 // we have it listed that he does
2541 mprintf(0, "CTF: In GivePlayerFlag(), we detected the flag in someone elses inventory\n");
2542 // remove all the flags that this player has of this team...very weird
2543 DLLInvRemove(player, OBJ_POWERUP, FlagIDs[team]);
2544 SetColoredBalls(player, false);
2545 // check to see if the player had a flag attached to him
2546 if (ChildFlags[team] != OBJECT_HANDLE_NONE && DMFCBase->GetLocalRole() == LR_SERVER) {
2547 // he does have a flag attached to him...kill it
2548 RemoveFlagFromPlayer(player, team);
2549 }
2550 }
2551 }
2552 // reset this value of the array
2553 HasFlag[team] = -1;
2554 if (DMFCBase->GetLocalRole() != LR_SERVER) {
2555 mprintf(0, "CTF: Game must be out of sync, requesting game state\n");
2556 DMFCBase->RequestGameState();
2557 }
2558 }
2559
2560 // loop through all the players and make sure they don't have this flag
2561 for (int player = 0; player < DLLMAX_PLAYERS; player++) {
2562 // check to see if it's an active player
2563 if (!DMFCBase->CheckPlayerNum(player))
2564 continue;
2565
2566 // remove all the flags the player has
2567 while (DLLInvCheckItem(player, OBJ_POWERUP, FlagIDs[team])) {
2568 mprintf(0, "CTF: In GivePlayerFlag(), detected a flag in a stranger's inventory\n");
2569 DLLInvRemove(player, OBJ_POWERUP, FlagIDs[team]);
2570 SetColoredBalls(player, false);
2571 // check to see if the player had a flag attached to him
2572 if (ChildFlags[team] != OBJECT_HANDLE_NONE && DMFCBase->GetLocalRole() == LR_SERVER) {
2573 // he does have a flag attached to him...kill it
2574 RemoveFlagFromPlayer(player, team);
2575 }
2576 }
2577 }
2578
2579 // ok, when we get here everything should be ducky, just add the flag and do the necessary things
2580 DLLInvAddTypeID(pnum, OBJ_POWERUP, FlagIDs[team], -1, -1, 0, NULL);
2581 HasFlag[team] = pnum;
2582 SetColoredBalls(pnum);
2583
2584 if (DMFCBase->GetLocalRole() == LR_SERVER) {
2585 // so we got here and added a flag to the player, now we need to attach the flag to the player
2586 if (!AddFlagToPlayer(pnum, team)) {
2587 // there was an error adding the flag,,,,ack!
2588 mprintf(0, "CTF: In GivePlayerFlag(), couldn't attach the flag to the player\n");
2589 }
2590 }
2591
2592 return true;
2593 }
2594
2595 // this function takes a flag away from the player, useful for when he scores, spews, disconnects, or observer modes
2596 void LoseFlagForPlayer(int pnum, uint8_t team, bool remove_from_inven) {
2597 // 1st check the player number
2598 if (pnum < 0 || pnum >= DLLMAX_PLAYERS) {
2599 mprintf(0, "CTF:Invalid pnum passed to LoseFlagForPlayer()\n");
2600 return;
2601 }
2602
2603 // 2nd check the team number
2604 if (team >= CTFNumOfTeams) {
2605 mprintf(0, "CTF:Invalid team passed to LoseFlagForPlayer()\n");
2606 return;
2607 }
2608
2609 if (team == DMFCBase->GetPlayerTeam(pnum)) {
2610 mprintf(0, "CTF:Invalid team passed to LoseFlagForPlayer()...same team as player\n");
2611 return;
2612 }
2613
2614 // ok, we have it registered that the flag belongs to us
2615 while (remove_from_inven && DLLInvCheckItem(pnum, OBJ_POWERUP, FlagIDs[team])) {
2616 DLLInvRemove(pnum, OBJ_POWERUP, FlagIDs[team]);
2617 }
2618
2619 SetColoredBalls(pnum);
2620 HasFlag[team] = -1;
2621
2622 // check to see if the player had a flag attached to him
2623 if (ChildFlags[team] != OBJECT_HANDLE_NONE) {
2624 // he does have a flag attached to him...kill it
2625 if (DMFCBase->GetLocalRole() == LR_SERVER) {
2626 // remove this flag and tell the clients to do so, cause we're the server
2627 RemoveFlagFromPlayer(pnum, team);
2628 }
2629 }
2630 }
2631
2632 void OnGetTokenString(char *src, char *dest, int dest_size) {
2633 if (!stricmp(src, "flag")) {
2634 int mypnum = DMFCBase->GetPlayerNum();
2635 int flagcount = GetFlagCountForPlayer(mypnum);
2636 int flagmask = GetFlagMaskForPlayer(mypnum);
2637 int team;
2638 bool hasflag[DLLMAX_TEAMS];
2639
2640 for (team = 0; team < CTFNumOfTeams; team++) {
2641 // draw all the flags the player has in his inventory
2642 if (flagmask & 0x01) {
2643 // player has this flag
2644 hasflag[team] = true;
2645 } else {
2646 hasflag[team] = false;
2647 }
2648
2649 // rotate 1 bit for next flag
2650 flagmask = flagmask >> 1;
2651 }
2652 for (; team < DLLMAX_TEAMS; team++) {
2653 hasflag[team] = false;
2654 }
2655
2656 switch (flagcount) {
2657 case 1: {
2658 char t[64];
2659 for (team = 0; team < CTFNumOfTeams; team++) {
2660 if (hasflag[team]) {
2661 strcpy(t, DMFCBase->GetTeamString(team));
2662 break;
2663 }
2664 }
2665
2666 strncpy(dest, t, dest_size - 1);
2667 dest[dest_size - 1] = '\0';
2668 return;
2669 } break;
2670 case 2: {
2671 char t[2][64];
2672 int found = 0;
2673 for (team = 0; team < CTFNumOfTeams; team++) {
2674 if (hasflag[team]) {
2675 strcpy(t[found], DMFCBase->GetTeamString(team));
2676 found++;
2677 if (found == flagcount)
2678 break;
2679 }
2680 }
2681
2682 char buffer[256];
2683 snprintf(buffer, sizeof(buffer), "%s and %s", t[0], t[1]);
2684 strncpy(dest, buffer, dest_size - 1);
2685 dest[dest_size - 1] = '\0';
2686 return;
2687 } break;
2688 case 3: {
2689 char t[3][64];
2690 int found = 0;
2691 for (team = 0; team < CTFNumOfTeams; team++) {
2692 if (hasflag[team]) {
2693 strcpy(t[found], DMFCBase->GetTeamString(team));
2694 found++;
2695 if (found == flagcount)
2696 break;
2697 }
2698 }
2699
2700 char buffer[256];
2701 snprintf(buffer, sizeof(buffer), "%s, %s and %s", t[0], t[1], t[2]);
2702 strncpy(dest, buffer, dest_size - 1);
2703 dest[dest_size - 1] = '\0';
2704 return;
2705
2706 } break;
2707 default:
2708 strncpy(dest, "No", dest_size - 1);
2709 dest[dest_size - 1] = '\0';
2710 return;
2711 break;
2712 }
2713 }
2714
2715 DMFCBase->OnGetTokenString(src, dest, dest_size);
2716 }