Re-enabled PDB files.
3 * =============================================================================
5 * Copyright (C) 2004-2009 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
34 #include "MenuVoting.h"
35 #include "PlayerManager.h"
36 #include "sourcemm_api.h"
37 #include <sourcemod.h>
39 #include <HalfLife2.h>
42 #include <Translator.h>
44 float g_next_vote = 0.0f;
46 #define VOTE_NOT_VOTING -2
47 #define VOTE_PENDING -1
49 ConVar sm_vote_hintbox("sm_vote_progress_hintbox",
52 "Show current vote progress in a hint box",
58 ConVar sm_vote_chat("sm_vote_progress_chat",
61 "Show current vote progress as chat messages",
67 ConVar sm_vote_console("sm_vote_progress_console",
70 "Show current vote progress as console messages",
76 ConVar sm_vote_client_console("sm_vote_progress_client_console",
79 "Show current vote progress as console messages to clients",
86 #if SOURCE_ENGINE >= SE_ORANGEBOX
87 void OnVoteDelayChange(IConVar *cvar, const char *value, float flOldValue);
89 void OnVoteDelayChange(ConVar *cvar, const char *value);
91 ConVar sm_vote_delay("sm_vote_delay",
94 "Sets the recommended time in between public votes",
101 #if SOURCE_ENGINE >= SE_ORANGEBOX
102 void OnVoteDelayChange(IConVar *cvar, const char *value, float flOldValue)
104 void OnVoteDelayChange(ConVar *cvar, const char *value)
107 /* See if the new vote delay isn't something we need to account for */
108 if (sm_vote_delay.GetFloat() < 1.0f)
114 /* If there was never a last vote, ignore this change */
115 if (g_next_vote < 0.1f)
120 /* Subtract the original value, then add the new one. */
121 g_next_vote -= (float)atof(value);
122 g_next_vote += sm_vote_delay.GetFloat();
125 unsigned int VoteMenuHandler::GetRemainingVoteDelay()
127 if (g_next_vote <= gpGlobals->curtime)
132 return (unsigned int)(g_next_vote - gpGlobals->curtime);
135 void VoteMenuHandler::OnSourceModAllInitialized()
137 g_Players.AddClientListener(this);
140 void VoteMenuHandler::OnSourceModShutdown()
142 g_Players.RemoveClientListener(this);
145 void VoteMenuHandler::OnSourceModLevelChange(const char *mapName)
150 unsigned int VoteMenuHandler::GetMenuAPIVersion2()
152 return m_pHandler->GetMenuAPIVersion2();
155 void VoteMenuHandler::OnClientDisconnected(int client)
157 if (!IsVoteInProgress())
162 /* Wipe out their vote if they had one. We have to make sure the the the
163 * newly connected client is not allowed to vote.
166 if ((item = m_ClientVotes[client]) >= VOTE_PENDING)
170 assert((unsigned)item < m_Items);
171 assert(m_Votes[item] > 0);
174 m_ClientVotes[client] = VOTE_NOT_VOTING;
178 bool VoteMenuHandler::IsVoteInProgress()
180 return (m_pCurMenu != NULL);
183 bool VoteMenuHandler::StartVote(IBaseMenu *menu,
184 unsigned int num_clients,
186 unsigned int max_time,
187 unsigned int flags/* =0 */)
189 if (!InitializeVoting(menu, menu->GetHandler(), max_time, flags))
194 /* Note: we can use game time and not universal time because
195 * if we're voting then players are in-game.
198 float fVoteDelay = sm_vote_delay.GetFloat();
199 if (fVoteDelay < 1.0)
205 /* This little trick breaks for infinite votes!
206 * However, we just ignore that since those 1) shouldn't exist and
207 * 2) people must be checking IsVoteInProgress() beforehand anyway.
209 g_next_vote = gpGlobals->curtime + fVoteDelay + (float)max_time;
212 m_fStartTime = gpGlobals->curtime;
213 m_nMenuTime = max_time;
215 for (unsigned int i=0; i<num_clients; i++)
217 if (clients[i] < 1 || clients[i] > 256)
221 menu->Display(clients[i], max_time, this);
229 bool VoteMenuHandler::IsClientInVotePool(int client)
232 || client > g_Players.MaxClients()
233 || m_pCurMenu == NULL)
238 return (m_ClientVotes[client] > VOTE_NOT_VOTING);
241 bool VoteMenuHandler::GetClientVoteChoice(int client, unsigned int *pItem)
243 if (!IsClientInVotePool(client)
244 || m_ClientVotes[client] == VOTE_PENDING)
249 *pItem = m_ClientVotes[client];
254 bool VoteMenuHandler::RedrawToClient(int client, bool revotes)
256 unsigned int time_limit;
258 if (!IsClientInVotePool(client))
263 if (m_ClientVotes[client] >= 0)
265 if ((m_VoteFlags & VOTEFLAG_NO_REVOTES) == VOTEFLAG_NO_REVOTES || !revotes)
269 assert((unsigned)m_ClientVotes[client] < m_Items);
270 assert(m_Votes[m_ClientVotes[client]] > 0);
271 m_Votes[m_ClientVotes[client]]--;
272 m_ClientVotes[client] = VOTE_PENDING;
273 m_Revoting[client] = true;
277 if (m_nMenuTime == MENU_TIME_FOREVER)
279 time_limit = m_nMenuTime;
283 time_limit = (int)((float)m_nMenuTime - (gpGlobals->curtime - m_fStartTime));
285 /* Make sure this doesn't round to zero */
286 if (time_limit == MENU_TIME_FOREVER)
292 return m_pCurMenu->Display(client, time_limit, this);
295 bool VoteMenuHandler::InitializeVoting(IBaseMenu *menu,
296 IMenuHandler *handler,
300 if (IsVoteInProgress())
307 /* Mark all clients as not voting */
308 for (int i=1; i<=gpGlobals->maxClients; i++)
310 m_ClientVotes[i] = VOTE_NOT_VOTING;
311 m_Revoting[i] = false;
314 m_Items = menu->GetItemCount();
316 if (m_Votes.size() < (size_t)m_Items)
318 /* Only clear the items we need to... */
319 size_t size = m_Votes.size();
320 for (size_t i=0; i<size; i++)
324 m_Votes.resize(m_Items, 0);
328 for (unsigned int i=0; i<m_Items; i++)
337 m_pHandler = handler;
339 m_pHandler->OnMenuStart(m_pCurMenu);
344 void VoteMenuHandler::StartVoting()
353 m_pHandler->OnMenuVoteStart(m_pCurMenu);
355 m_displayTimer = g_Timers.CreateTimer(this, 1.0, NULL, TIMER_FLAG_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
357 /* By now we know how many clients were set.
358 * If there are none, we should end IMMEDIATELY.
365 m_TotalClients = m_Clients;
368 void VoteMenuHandler::DecrementPlayerCount()
370 assert(m_Clients > 0);
374 if (m_bStarted && m_Clients == 0)
380 int SortVoteItems(const void *item1, const void *item2)
382 return ((menu_vote_result_t::menu_item_vote_t *)item2)->count
383 - ((menu_vote_result_t::menu_item_vote_t *)item1)->count;
386 void VoteMenuHandler::EndVoting()
388 /* Set when the next delay ends. We ignore cancellation because a menu
389 * was, at one point, displayed, which is all that counts. However, we
390 * do re-calculate the time just in case the menu had no time limit.
392 float fVoteDelay = sm_vote_delay.GetFloat();
393 if (fVoteDelay < 1.0)
399 g_next_vote = gpGlobals->curtime + fVoteDelay;
404 g_Timers.KillTimer(m_displayTimer);
409 /* If we were cancelled, don't bother tabulating anything.
410 * Reset just in case someone tries to redraw, which means
411 * we need to save our states.
413 IBaseMenu *menu = m_pCurMenu;
414 IMenuHandler *handler = m_pHandler;
416 handler->OnMenuVoteCancel(menu, VoteCancel_Generic);
417 handler->OnMenuEnd(menu, MenuEnd_VotingCancelled);
421 menu_vote_result_t vote;
422 menu_vote_result_t::menu_client_vote_t client_vote[256];
423 menu_vote_result_t::menu_item_vote_t item_vote[256];
425 memset(&vote, 0, sizeof(vote));
427 /* Build the item list */
428 for (unsigned int i=0; i<m_Items; i++)
432 item_vote[vote.num_items].count = m_Votes[i];
433 item_vote[vote.num_items].item = i;
434 vote.num_votes += m_Votes[i];
438 vote.item_list = item_vote;
442 IBaseMenu *menu = m_pCurMenu;
443 IMenuHandler *handler = m_pHandler;
445 handler->OnMenuVoteCancel(menu, VoteCancel_NoVotes);
446 handler->OnMenuEnd(menu, MenuEnd_VotingCancelled);
450 /* Build the client list */
451 for (int i=1; i<=gpGlobals->maxClients; i++)
453 if (m_ClientVotes[i] >= VOTE_PENDING)
455 client_vote[vote.num_clients].client = i;
456 client_vote[vote.num_clients].item = m_ClientVotes[i];
460 vote.client_list = client_vote;
462 /* Sort the item list descending like we promised */
465 sizeof(menu_vote_result_t::menu_item_vote_t),
468 /* Save states, then clear what we've saved.
469 * This makes us re-entrant, which is always the safe way to go.
471 IBaseMenu *menu = m_pCurMenu;
472 IMenuHandler *handler = m_pHandler;
476 handler->OnMenuVoteResults(menu, &vote);
477 handler->OnMenuEnd(menu, MenuEnd_VotingDone);
480 void VoteMenuHandler::OnMenuStart(IBaseMenu *menu)
485 void VoteMenuHandler::OnMenuEnd(IBaseMenu *menu, MenuEndReason reason)
487 DecrementPlayerCount();
490 void VoteMenuHandler::OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
492 m_pHandler->OnMenuCancel(menu, client, reason);
495 void VoteMenuHandler::OnMenuDisplay(IBaseMenu *menu, int client, IMenuPanel *display)
497 m_ClientVotes[client] = VOTE_PENDING;
498 m_pHandler->OnMenuDisplay(menu, client, display);
501 unsigned int VoteMenuHandler::OnMenuDisplayItem(IBaseMenu *menu, int client, IMenuPanel *panel, unsigned int item, const ItemDrawInfo &dr)
503 return m_pHandler->OnMenuDisplayItem(menu, client, panel, item, dr);
506 void VoteMenuHandler::OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style)
508 m_pHandler->OnMenuDrawItem(menu, client, item, style);
511 void VoteMenuHandler::OnMenuSelect(IBaseMenu *menu, int client, unsigned int item)
513 /* Check by our item count, NOT the vote array size */
516 m_ClientVotes[client] = item;
520 if (sm_vote_chat.GetBool() || sm_vote_console.GetBool())
522 static char buffer[1024];
524 menu->GetItemInfo(item, &dr);
526 if (sm_vote_console.GetBool())
528 int target = SOURCEMOD_SERVER_LANGUAGE;
529 CoreTranslate(buffer, sizeof(buffer), "[SM] %T", 4, NULL, "Voted For", &target, g_Players.GetPlayerByIndex(client)->GetName(), dr.display);
530 Engine_LogPrintWrapper(buffer);
533 if (sm_vote_chat.GetBool() || sm_vote_client_console.GetBool())
535 int maxclients = g_Players.GetMaxClients();
536 for (int i=1; i<=maxclients; i++)
538 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
541 if (pPlayer->IsInGame())
543 if (m_Revoting[client])
545 CoreTranslate(buffer, sizeof(buffer), "[SM] %T", 4, NULL, "Changed Vote", &i, g_Players.GetPlayerByIndex(client)->GetName(), dr.display);
549 CoreTranslate(buffer, sizeof(buffer), "[SM] %T", 4, NULL, "Voted For", &i, g_Players.GetPlayerByIndex(client)->GetName(), dr.display);
552 if (sm_vote_chat.GetBool())
554 g_HL2.TextMsg(i, HUD_PRINTTALK, buffer);
557 if (sm_vote_client_console.GetBool())
559 engine->ClientPrintf(pPlayer->GetEdict(), buffer);
570 m_pHandler->OnMenuSelect(menu, client, item);
573 void VoteMenuHandler::OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page)
575 if (m_pHandler->GetMenuAPIVersion2() >= 13)
577 m_pHandler->OnMenuSelect2(menu, client, item, item_on_page);
581 void VoteMenuHandler::InternalReset()
588 m_bCancelled = false;
590 m_leaderList[0] = '\0';
591 m_displayTimer = NULL;
595 void VoteMenuHandler::CancelVoting()
597 if (m_bCancelled || !m_pCurMenu)
602 m_pCurMenu->Cancel();
605 IBaseMenu *VoteMenuHandler::GetCurrentMenu()
610 bool VoteMenuHandler::IsCancelling()
615 void VoteMenuHandler::DrawHintProgress()
617 if (!sm_vote_hintbox.GetBool())
622 static char buffer[1024];
624 float timeRemaining = (m_fStartTime + m_nMenuTime) - gpGlobals->curtime;
625 if (timeRemaining < 0)
630 int iTimeRemaining = RoundFloatToInt(timeRemaining);
632 int maxclients = g_Players.GetMaxClients();
633 for (int i=1; i<=maxclients; i++)
635 if (g_Players.GetPlayerByIndex(i)->IsInGame())
637 CoreTranslate(buffer, sizeof(buffer), "%T%s", 6, NULL, "Vote Count", &i, &m_NumVotes, &m_TotalClients, &iTimeRemaining, &m_leaderList);
638 g_HL2.HintTextMsg(i, buffer);
643 void VoteMenuHandler::BuildVoteLeaders()
645 if (m_NumVotes == 0 || !sm_vote_hintbox.GetBool())
650 menu_vote_result_t vote;
651 menu_vote_result_t::menu_item_vote_t item_vote[256];
653 memset(&vote, 0, sizeof(vote));
655 /* Build the item list */
656 for (unsigned int i=0; i<m_Items; i++)
660 item_vote[vote.num_items].count = m_Votes[i];
661 item_vote[vote.num_items].item = i;
662 vote.num_votes += m_Votes[i];
666 vote.item_list = item_vote;
667 assert(vote.num_votes);
669 /* Sort the item list descending */
672 sizeof(menu_vote_result_t::menu_item_vote_t),
675 /* Take the top 3 (if applicable) and draw them */
677 for (unsigned int i=0; i<vote.num_items && i<3; i++)
679 int curItem = vote.item_list[i].item;
681 m_pCurMenu->GetItemInfo(curItem, &dr);
682 len += g_SourceMod.Format(m_leaderList + len, sizeof(m_leaderList) - len, "\n%i. %s: (%i)", i+1, dr.display, vote.item_list[i].count);
686 SourceMod::ResultType VoteMenuHandler::OnTimer(ITimer *pTimer, void *pData)
693 void VoteMenuHandler::OnTimerEnd(ITimer *pTimer, void *pData)
695 m_displayTimer = NULL;