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 <IGameConfigs.h>
34 #include "UserMessages.h"
36 #include "PlayerManager.h"
37 #include "HandleSys.h"
38 #include "logic_bridge.h"
40 #define MAX_HUD_CHANNELS 6
46 int player_channels[256 + 1];
49 struct player_chaninfo_t
51 double chan_times[MAX_HUD_CHANNELS];
52 hud_syncobj_t *chan_syncobjs[MAX_HUD_CHANNELS];
71 public IHandleTypeDispatch,
72 public IClientListener
77 return (g_HudMsgNum != -1);
80 virtual void OnSourceModAllInitialized_Post()
84 key = g_pGameConf->GetKeyValue("HudTextMsg");
87 g_HudMsgNum = g_UserMsgs.GetMessageIndex(key);
97 m_PlayerHuds = new player_chaninfo_t[256+1];
98 m_hHudSyncObj = g_HandleSys.CreateType("HudSyncObj", this, 0, NULL, NULL, g_pCoreIdent, NULL);
100 g_Players.AddClientListener(this);
103 virtual void OnSourceModShutdown()
110 delete [] m_PlayerHuds;
111 g_HandleSys.RemoveType(m_hHudSyncObj, g_pCoreIdent);
113 g_Players.RemoveClientListener(this);
116 virtual void OnHandleDestroy(HandleType_t type, void *object)
118 hud_syncobj_t *obj = (hud_syncobj_t *)object;
123 virtual bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
125 *pSize = sizeof(unsigned int) * g_Players.GetMaxClients();
129 virtual void OnClientConnected(int client)
131 player_chaninfo_t *player;
133 player = &m_PlayerHuds[client];
134 memset(player->chan_syncobjs, 0, sizeof(player->chan_syncobjs));
135 memset(player->chan_times, 0, sizeof(player->chan_times));
138 Handle_t CreateHudSyncObj(IdentityToken_t *pIdent)
145 obj = new hud_syncobj_t;
147 memset(obj->player_channels, 0, sizeof(obj->player_channels));
149 sec = HandleSecurity(pIdent, g_pCoreIdent);
151 if ((hndl = g_HandleSys.CreateHandleEx(m_hHudSyncObj, obj, &sec, NULL, &err))
160 HandleError ReadHudSyncObj(Handle_t hndl,
161 IdentityToken_t *pOwner,
162 hud_syncobj_t **pObj)
164 HandleSecurity sec(pOwner, g_pCoreIdent);
165 return g_HandleSys.ReadHandle(hndl, m_hHudSyncObj, &sec, (void **)pObj);
168 unsigned int AutoSelectChannel(unsigned int client)
171 player_chaninfo_t *player;
173 player = &m_PlayerHuds[client];
176 for (unsigned int i = 1; i < MAX_HUD_CHANNELS; i++)
178 if (player->chan_times[i] < player->chan_times[last_channel])
184 ManualSelectChannel(client, last_channel);
189 int TryReuseLastChannel(unsigned int client, hud_syncobj_t *obj)
192 player_chaninfo_t *player;
194 player = &m_PlayerHuds[client];
196 /* First, see if we can re-use the previous channel. */
197 last_channel = obj->player_channels[client];
199 if (player->chan_syncobjs[last_channel] == obj)
201 player->chan_times[last_channel] = *g_pUniversalTime;
208 int AutoSelectChannel(unsigned int client, hud_syncobj_t *obj)
211 player_chaninfo_t *player;
213 player = &m_PlayerHuds[client];
215 /* First, see if we can re-use the previous channel. */
216 last_channel = obj->player_channels[client];
218 if (player->chan_syncobjs[last_channel] == obj)
220 player->chan_times[last_channel] = *g_pUniversalTime;
225 for (unsigned int i = 1; i < MAX_HUD_CHANNELS; i++)
227 if (player->chan_times[i] < player->chan_times[last_channel])
233 obj->player_channels[client] = last_channel;
234 player->chan_syncobjs[last_channel] = obj;
235 player->chan_times[last_channel] = *g_pUniversalTime;
240 int ManualSelectChannel(unsigned int client, int channel)
242 player_chaninfo_t *player;
244 player = &m_PlayerHuds[client];
245 player->chan_times[channel] = *g_pUniversalTime;
246 player->chan_syncobjs[channel] = NULL;
251 HandleType_t m_hHudSyncObj;
252 player_chaninfo_t *m_PlayerHuds;
255 hud_text_parms g_hud_params;
257 static cell_t CreateHudSynchronizer(IPluginContext *pContext, const cell_t *params)
259 return s_HudMsgHelpers.CreateHudSyncObj(pContext->GetIdentity());
262 static cell_t SetHudTextParams(IPluginContext *pContext, const cell_t *params)
264 g_hud_params.x = sp_ctof(params[1]);
265 g_hud_params.y = sp_ctof(params[2]);
266 g_hud_params.holdTime = sp_ctof(params[3]);
267 g_hud_params.r1 = static_cast<byte>(params[4]);
268 g_hud_params.g1 = static_cast<byte>(params[5]);
269 g_hud_params.b1 = static_cast<byte>(params[6]);
270 g_hud_params.a1 = static_cast<byte>(params[7]);
271 g_hud_params.effect = params[8];
272 g_hud_params.fxTime = sp_ctof(params[9]);
273 g_hud_params.fadeinTime = sp_ctof(params[10]);
274 g_hud_params.fadeoutTime = sp_ctof(params[11]);
275 g_hud_params.r2 = 255;
276 g_hud_params.g2 = 255;
277 g_hud_params.b2 = 250;
283 static cell_t SetHudTextParamsEx(IPluginContext *pContext, const cell_t *params)
285 cell_t *color1, *color2;
287 pContext->LocalToPhysAddr(params[4], &color1);
288 pContext->LocalToPhysAddr(params[5], &color2);
290 g_hud_params.x = sp_ctof(params[1]);
291 g_hud_params.y = sp_ctof(params[2]);
292 g_hud_params.holdTime = sp_ctof(params[3]);
293 g_hud_params.r1 = static_cast<byte>(color1[0]);
294 g_hud_params.g1 = static_cast<byte>(color1[1]);
295 g_hud_params.b1 = static_cast<byte>(color1[2]);
296 g_hud_params.a1 = static_cast<byte>(color1[3]);
297 g_hud_params.effect = params[6];
298 g_hud_params.fxTime = sp_ctof(params[7]);
299 g_hud_params.fadeinTime = sp_ctof(params[8]);
300 g_hud_params.fadeoutTime = sp_ctof(params[9]);
301 g_hud_params.r2 = static_cast<byte>(color2[0]);
302 g_hud_params.g2 = static_cast<byte>(color2[1]);
303 g_hud_params.b2 = static_cast<byte>(color2[2]);
304 g_hud_params.a2 = static_cast<byte>(color2[3]);
309 void UTIL_SendHudText(int client, const hud_text_parms &textparms, const char *pMessage)
316 bf = g_UserMsgs.StartMessage(g_HudMsgNum, players, 1, 0);
317 bf->WriteByte(textparms.channel & 0xFF );
318 bf->WriteFloat(textparms.x);
319 bf->WriteFloat(textparms.y);
320 bf->WriteByte(textparms.r1);
321 bf->WriteByte(textparms.g1);
322 bf->WriteByte(textparms.b1);
323 bf->WriteByte(textparms.a1);
324 bf->WriteByte(textparms.r2);
325 bf->WriteByte(textparms.g2);
326 bf->WriteByte(textparms.b2);
327 bf->WriteByte(textparms.a2);
328 bf->WriteByte(textparms.effect);
329 bf->WriteFloat(textparms.fadeinTime);
330 bf->WriteFloat(textparms.fadeoutTime);
331 bf->WriteFloat(textparms.holdTime);
332 bf->WriteFloat(textparms.fxTime);
333 bf->WriteString(pMessage);
334 g_UserMsgs.EndMessage();
337 static cell_t ShowSyncHudText(IPluginContext *pContext, const cell_t *params)
343 char message_buffer[255-36];
345 if (!s_HudMsgHelpers.IsSupported())
350 if ((err = s_HudMsgHelpers.ReadHudSyncObj(params[2], pContext->GetIdentity(), &obj)) != HandleError_None)
352 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
356 if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
358 return pContext->ThrowNativeError("Invalid client index %d", client);
360 else if (!pPlayer->IsInGame())
362 return pContext->ThrowNativeError("Client %d is not in-game", client);
365 g_SourceMod.FormatString(message_buffer, sizeof(message_buffer), pContext, params, 3);
366 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
371 g_hud_params.channel = s_HudMsgHelpers.AutoSelectChannel(client, obj);
372 UTIL_SendHudText(client, g_hud_params, message_buffer);
377 static cell_t ClearSyncHud(IPluginContext *pContext, const cell_t *params)
385 if (!s_HudMsgHelpers.IsSupported())
390 if ((err = s_HudMsgHelpers.ReadHudSyncObj(params[2], pContext->GetIdentity(), &obj)) != HandleError_None)
392 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
396 if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
398 return pContext->ThrowNativeError("Invalid client index %d", client);
400 else if (!pPlayer->IsInGame())
402 return pContext->ThrowNativeError("Client %d is not in-game", client);
405 if ((channel = s_HudMsgHelpers.TryReuseLastChannel(client, obj)) == -1)
410 g_hud_params.channel = channel;
411 UTIL_SendHudText(client, g_hud_params, "");
413 return g_hud_params.channel;
416 static cell_t ShowHudText(IPluginContext *pContext, const cell_t *params)
420 char message_buffer[255-36];
422 if (!s_HudMsgHelpers.IsSupported())
428 if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
430 return pContext->ThrowNativeError("Invalid client index %d", client);
432 else if (!pPlayer->IsInGame())
434 return pContext->ThrowNativeError("Client %d is not in-game", client);
437 g_SourceMod.FormatString(message_buffer, sizeof(message_buffer), pContext, params, 3);
438 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
445 g_hud_params.channel = s_HudMsgHelpers.AutoSelectChannel(client);
449 g_hud_params.channel = params[2] % MAX_HUD_CHANNELS;
450 s_HudMsgHelpers.ManualSelectChannel(client, g_hud_params.channel);
453 UTIL_SendHudText(client, g_hud_params, message_buffer);
455 return g_hud_params.channel;
458 REGISTER_NATIVES(hudNatives)
460 {"ClearSyncHud", ClearSyncHud},
461 {"CreateHudSynchronizer", CreateHudSynchronizer},
462 {"SetHudTextParams", SetHudTextParams},
463 {"SetHudTextParamsEx", SetHudTextParamsEx},
464 {"ShowHudText", ShowHudText},
465 {"ShowSyncHudText", ShowSyncHudText},