3 * =============================================================================
5 * Copyright (C) 2004-2008 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>.
32 #include "sm_globals.h"
33 #include "GameConfigs.h"
34 #include "UserMessages.h"
36 #include "PlayerManager.h"
37 #include "HandleSys.h"
39 #define MAX_HUD_CHANNELS 6
45 int player_channels[256 + 1];
48 struct player_chaninfo_t
50 double chan_times[MAX_HUD_CHANNELS];
51 hud_syncobj_t *chan_syncobjs[MAX_HUD_CHANNELS];
70 public IHandleTypeDispatch,
71 public IClientListener
76 return (g_HudMsgNum != -1);
79 virtual void OnSourceModAllInitialized_Post()
83 key = g_pGameConf->GetKeyValue("HudTextMsg");
86 g_HudMsgNum = g_UserMsgs.GetMessageIndex(key);
96 m_PlayerHuds = new player_chaninfo_t[256+1];
97 m_hHudSyncObj = g_HandleSys.CreateType("HudSyncObj", this, 0, NULL, NULL, g_pCoreIdent, NULL);
99 g_Players.AddClientListener(this);
102 virtual void OnSourceModShutdown()
109 delete [] m_PlayerHuds;
110 g_HandleSys.RemoveType(m_hHudSyncObj, g_pCoreIdent);
112 g_Players.RemoveClientListener(this);
115 virtual void OnHandleDestroy(HandleType_t type, void *object)
117 hud_syncobj_t *obj = (hud_syncobj_t *)object;
122 virtual bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
124 *pSize = sizeof(unsigned int) * g_Players.GetMaxClients();
128 virtual void OnClientConnected(int client)
130 player_chaninfo_t *player;
132 player = &m_PlayerHuds[client];
133 memset(player->chan_syncobjs, 0, sizeof(player->chan_syncobjs));
134 memset(player->chan_times, 0, sizeof(player->chan_times));
137 Handle_t CreateHudSyncObj(IdentityToken_t *pIdent)
144 obj = new hud_syncobj_t;
146 memset(obj->player_channels, 0, sizeof(obj->player_channels));
148 sec = HandleSecurity(pIdent, g_pCoreIdent);
150 if ((hndl = g_HandleSys.CreateHandleEx(m_hHudSyncObj, obj, &sec, NULL, &err))
159 HandleError ReadHudSyncObj(Handle_t hndl,
160 IdentityToken_t *pOwner,
161 hud_syncobj_t **pObj)
163 HandleSecurity sec(pOwner, g_pCoreIdent);
164 return g_HandleSys.ReadHandle(hndl, m_hHudSyncObj, &sec, (void **)pObj);
167 unsigned int AutoSelectChannel(unsigned int client)
170 player_chaninfo_t *player;
172 player = &m_PlayerHuds[client];
175 for (unsigned int i = 1; i < MAX_HUD_CHANNELS; i++)
177 if (player->chan_times[i] < player->chan_times[last_channel])
183 ManualSelectChannel(client, last_channel);
188 int TryReuseLastChannel(unsigned int client, hud_syncobj_t *obj)
191 player_chaninfo_t *player;
193 player = &m_PlayerHuds[client];
195 /* First, see if we can re-use the previous channel. */
196 last_channel = obj->player_channels[client];
198 if (player->chan_syncobjs[last_channel] == obj)
200 player->chan_times[last_channel] = *g_pUniversalTime;
207 int AutoSelectChannel(unsigned int client, hud_syncobj_t *obj)
210 player_chaninfo_t *player;
212 player = &m_PlayerHuds[client];
214 /* First, see if we can re-use the previous channel. */
215 last_channel = obj->player_channels[client];
217 if (player->chan_syncobjs[last_channel] == obj)
219 player->chan_times[last_channel] = *g_pUniversalTime;
224 for (unsigned int i = 1; i < MAX_HUD_CHANNELS; i++)
226 if (player->chan_times[i] < player->chan_times[last_channel])
232 obj->player_channels[client] = last_channel;
233 player->chan_syncobjs[last_channel] = obj;
234 player->chan_times[last_channel] = *g_pUniversalTime;
239 int ManualSelectChannel(unsigned int client, int channel)
241 player_chaninfo_t *player;
243 player = &m_PlayerHuds[client];
244 player->chan_times[channel] = *g_pUniversalTime;
245 player->chan_syncobjs[channel] = NULL;
250 HandleType_t m_hHudSyncObj;
251 player_chaninfo_t *m_PlayerHuds;
254 hud_text_parms g_hud_params;
256 static cell_t CreateHudSynchronizer(IPluginContext *pContext, const cell_t *params)
258 return s_HudMsgHelpers.CreateHudSyncObj(pContext->GetIdentity());
261 static cell_t SetHudTextParams(IPluginContext *pContext, const cell_t *params)
263 g_hud_params.x = sp_ctof(params[1]);
264 g_hud_params.y = sp_ctof(params[2]);
265 g_hud_params.holdTime = sp_ctof(params[3]);
266 g_hud_params.r1 = static_cast<byte>(params[4]);
267 g_hud_params.g1 = static_cast<byte>(params[5]);
268 g_hud_params.b1 = static_cast<byte>(params[6]);
269 g_hud_params.a1 = static_cast<byte>(params[7]);
270 g_hud_params.effect = params[8];
271 g_hud_params.fxTime = sp_ctof(params[9]);
272 g_hud_params.fadeinTime = sp_ctof(params[10]);
273 g_hud_params.fadeoutTime = sp_ctof(params[11]);
274 g_hud_params.r2 = 255;
275 g_hud_params.g2 = 255;
276 g_hud_params.b2 = 250;
282 static cell_t SetHudTextParamsEx(IPluginContext *pContext, const cell_t *params)
284 cell_t *color1, *color2;
286 pContext->LocalToPhysAddr(params[4], &color1);
287 pContext->LocalToPhysAddr(params[5], &color2);
289 g_hud_params.x = sp_ctof(params[1]);
290 g_hud_params.y = sp_ctof(params[2]);
291 g_hud_params.holdTime = sp_ctof(params[3]);
292 g_hud_params.r1 = static_cast<byte>(color1[0]);
293 g_hud_params.g1 = static_cast<byte>(color1[1]);
294 g_hud_params.b1 = static_cast<byte>(color1[2]);
295 g_hud_params.a1 = static_cast<byte>(color1[3]);
296 g_hud_params.effect = params[6];
297 g_hud_params.fxTime = sp_ctof(params[7]);
298 g_hud_params.fadeinTime = sp_ctof(params[8]);
299 g_hud_params.fadeoutTime = sp_ctof(params[9]);
300 g_hud_params.r2 = static_cast<byte>(color2[0]);
301 g_hud_params.g2 = static_cast<byte>(color2[1]);
302 g_hud_params.b2 = static_cast<byte>(color2[2]);
303 g_hud_params.a2 = static_cast<byte>(color2[3]);
308 void UTIL_SendHudText(int client, const hud_text_parms &textparms, const char *pMessage)
315 bf = g_UserMsgs.StartMessage(g_HudMsgNum, players, 1, 0);
316 bf->WriteByte(textparms.channel & 0xFF );
317 bf->WriteFloat(textparms.x);
318 bf->WriteFloat(textparms.y);
319 bf->WriteByte(textparms.r1);
320 bf->WriteByte(textparms.g1);
321 bf->WriteByte(textparms.b1);
322 bf->WriteByte(textparms.a1);
323 bf->WriteByte(textparms.r2);
324 bf->WriteByte(textparms.g2);
325 bf->WriteByte(textparms.b2);
326 bf->WriteByte(textparms.a2);
327 bf->WriteByte(textparms.effect);
328 bf->WriteFloat(textparms.fadeinTime);
329 bf->WriteFloat(textparms.fadeoutTime);
330 bf->WriteFloat(textparms.holdTime);
331 bf->WriteFloat(textparms.fxTime);
332 bf->WriteString(pMessage);
333 g_UserMsgs.EndMessage();
336 static cell_t ShowSyncHudText(IPluginContext *pContext, const cell_t *params)
342 char message_buffer[255-36];
344 if (!s_HudMsgHelpers.IsSupported())
349 if ((err = s_HudMsgHelpers.ReadHudSyncObj(params[2], pContext->GetIdentity(), &obj)) != HandleError_None)
351 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
355 if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
357 return pContext->ThrowNativeError("Invalid client index %d", client);
359 else if (!pPlayer->IsInGame())
361 return pContext->ThrowNativeError("Client %d is not in-game", client);
364 g_SourceMod.FormatString(message_buffer, sizeof(message_buffer), pContext, params, 3);
365 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
370 g_hud_params.channel = s_HudMsgHelpers.AutoSelectChannel(client, obj);
371 UTIL_SendHudText(client, g_hud_params, message_buffer);
376 static cell_t ClearSyncHud(IPluginContext *pContext, const cell_t *params)
384 if (!s_HudMsgHelpers.IsSupported())
389 if ((err = s_HudMsgHelpers.ReadHudSyncObj(params[2], pContext->GetIdentity(), &obj)) != HandleError_None)
391 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
395 if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
397 return pContext->ThrowNativeError("Invalid client index %d", client);
399 else if (!pPlayer->IsInGame())
401 return pContext->ThrowNativeError("Client %d is not in-game", client);
404 if ((channel = s_HudMsgHelpers.TryReuseLastChannel(client, obj)) == -1)
409 g_hud_params.channel = channel;
410 UTIL_SendHudText(client, g_hud_params, "");
412 return g_hud_params.channel;
415 static cell_t ShowHudText(IPluginContext *pContext, const cell_t *params)
419 char message_buffer[255-36];
421 if (!s_HudMsgHelpers.IsSupported())
427 if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
429 return pContext->ThrowNativeError("Invalid client index %d", client);
431 else if (!pPlayer->IsInGame())
433 return pContext->ThrowNativeError("Client %d is not in-game", client);
436 g_SourceMod.FormatString(message_buffer, sizeof(message_buffer), pContext, params, 3);
437 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
444 g_hud_params.channel = s_HudMsgHelpers.AutoSelectChannel(client);
448 g_hud_params.channel = params[2] % MAX_HUD_CHANNELS;
449 s_HudMsgHelpers.ManualSelectChannel(client, g_hud_params.channel);
452 UTIL_SendHudText(client, g_hud_params, message_buffer);
454 return g_hud_params.channel;
457 REGISTER_NATIVES(hudNatives)
459 {"ClearSyncHud", ClearSyncHud},
460 {"CreateHudSynchronizer", CreateHudSynchronizer},
461 {"SetHudTextParams", SetHudTextParams},
462 {"SetHudTextParamsEx", SetHudTextParamsEx},
463 {"ShowHudText", ShowHudText},
464 {"ShowSyncHudText", ShowSyncHudText},