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 #if SOURCE_ENGINE == SE_CSGO
41 #include <game/shared/csgo/protobuf/cstrike15_usermessages.pb.h>
44 #define MAX_HUD_CHANNELS 6
50 int player_channels[256 + 1];
53 struct player_chaninfo_t
55 double chan_times[MAX_HUD_CHANNELS];
56 hud_syncobj_t *chan_syncobjs[MAX_HUD_CHANNELS];
75 public IHandleTypeDispatch,
76 public IClientListener
81 return (g_HudMsgNum != -1);
84 virtual void OnSourceModAllInitialized_Post()
88 key = g_pGameConf->GetKeyValue("HudTextMsg");
91 g_HudMsgNum = g_UserMsgs.GetMessageIndex(key);
101 m_PlayerHuds = new player_chaninfo_t[256+1];
102 m_hHudSyncObj = g_HandleSys.CreateType("HudSyncObj", this, 0, NULL, NULL, g_pCoreIdent, NULL);
104 g_Players.AddClientListener(this);
107 virtual void OnSourceModShutdown()
114 delete [] m_PlayerHuds;
115 g_HandleSys.RemoveType(m_hHudSyncObj, g_pCoreIdent);
117 g_Players.RemoveClientListener(this);
120 virtual void OnHandleDestroy(HandleType_t type, void *object)
122 hud_syncobj_t *obj = (hud_syncobj_t *)object;
127 virtual bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
129 *pSize = sizeof(unsigned int) * g_Players.GetMaxClients();
133 virtual void OnClientConnected(int client)
135 player_chaninfo_t *player;
137 player = &m_PlayerHuds[client];
138 memset(player->chan_syncobjs, 0, sizeof(player->chan_syncobjs));
139 memset(player->chan_times, 0, sizeof(player->chan_times));
142 Handle_t CreateHudSyncObj(IdentityToken_t *pIdent)
149 obj = new hud_syncobj_t;
151 memset(obj->player_channels, 0, sizeof(obj->player_channels));
153 sec = HandleSecurity(pIdent, g_pCoreIdent);
155 if ((hndl = g_HandleSys.CreateHandleEx(m_hHudSyncObj, obj, &sec, NULL, &err))
164 HandleError ReadHudSyncObj(Handle_t hndl,
165 IdentityToken_t *pOwner,
166 hud_syncobj_t **pObj)
168 HandleSecurity sec(pOwner, g_pCoreIdent);
169 return g_HandleSys.ReadHandle(hndl, m_hHudSyncObj, &sec, (void **)pObj);
172 unsigned int AutoSelectChannel(unsigned int client)
175 player_chaninfo_t *player;
177 player = &m_PlayerHuds[client];
180 for (unsigned int i = 1; i < MAX_HUD_CHANNELS; i++)
182 if (player->chan_times[i] < player->chan_times[last_channel])
188 ManualSelectChannel(client, last_channel);
193 int TryReuseLastChannel(unsigned int client, hud_syncobj_t *obj)
196 player_chaninfo_t *player;
198 player = &m_PlayerHuds[client];
200 /* First, see if we can re-use the previous channel. */
201 last_channel = obj->player_channels[client];
203 if (player->chan_syncobjs[last_channel] == obj)
205 player->chan_times[last_channel] = *g_pUniversalTime;
212 int AutoSelectChannel(unsigned int client, hud_syncobj_t *obj)
215 player_chaninfo_t *player;
217 player = &m_PlayerHuds[client];
219 /* First, see if we can re-use the previous channel. */
220 last_channel = obj->player_channels[client];
222 if (player->chan_syncobjs[last_channel] == obj)
224 player->chan_times[last_channel] = *g_pUniversalTime;
229 for (unsigned int i = 1; i < MAX_HUD_CHANNELS; i++)
231 if (player->chan_times[i] < player->chan_times[last_channel])
237 obj->player_channels[client] = last_channel;
238 player->chan_syncobjs[last_channel] = obj;
239 player->chan_times[last_channel] = *g_pUniversalTime;
244 int ManualSelectChannel(unsigned int client, int channel)
246 player_chaninfo_t *player;
248 player = &m_PlayerHuds[client];
249 player->chan_times[channel] = *g_pUniversalTime;
250 player->chan_syncobjs[channel] = NULL;
255 HandleType_t m_hHudSyncObj;
256 player_chaninfo_t *m_PlayerHuds;
259 hud_text_parms g_hud_params;
261 static cell_t CreateHudSynchronizer(IPluginContext *pContext, const cell_t *params)
263 return s_HudMsgHelpers.CreateHudSyncObj(pContext->GetIdentity());
266 static cell_t SetHudTextParams(IPluginContext *pContext, const cell_t *params)
268 g_hud_params.x = sp_ctof(params[1]);
269 g_hud_params.y = sp_ctof(params[2]);
270 g_hud_params.holdTime = sp_ctof(params[3]);
271 g_hud_params.r1 = static_cast<byte>(params[4]);
272 g_hud_params.g1 = static_cast<byte>(params[5]);
273 g_hud_params.b1 = static_cast<byte>(params[6]);
274 g_hud_params.a1 = static_cast<byte>(params[7]);
275 g_hud_params.effect = params[8];
276 g_hud_params.fxTime = sp_ctof(params[9]);
277 g_hud_params.fadeinTime = sp_ctof(params[10]);
278 g_hud_params.fadeoutTime = sp_ctof(params[11]);
279 g_hud_params.r2 = 255;
280 g_hud_params.g2 = 255;
281 g_hud_params.b2 = 250;
287 static cell_t SetHudTextParamsEx(IPluginContext *pContext, const cell_t *params)
289 cell_t *color1, *color2;
291 pContext->LocalToPhysAddr(params[4], &color1);
292 pContext->LocalToPhysAddr(params[5], &color2);
294 g_hud_params.x = sp_ctof(params[1]);
295 g_hud_params.y = sp_ctof(params[2]);
296 g_hud_params.holdTime = sp_ctof(params[3]);
297 g_hud_params.r1 = static_cast<byte>(color1[0]);
298 g_hud_params.g1 = static_cast<byte>(color1[1]);
299 g_hud_params.b1 = static_cast<byte>(color1[2]);
300 g_hud_params.a1 = static_cast<byte>(color1[3]);
301 g_hud_params.effect = params[6];
302 g_hud_params.fxTime = sp_ctof(params[7]);
303 g_hud_params.fadeinTime = sp_ctof(params[8]);
304 g_hud_params.fadeoutTime = sp_ctof(params[9]);
305 g_hud_params.r2 = static_cast<byte>(color2[0]);
306 g_hud_params.g2 = static_cast<byte>(color2[1]);
307 g_hud_params.b2 = static_cast<byte>(color2[2]);
308 g_hud_params.a2 = static_cast<byte>(color2[3]);
313 void UTIL_SendHudText(int client, const hud_text_parms &textparms, const char *pMessage)
319 #if SOURCE_ENGINE == SE_CSGO
320 // If or when we need to support multiple games per engine with this, we can switch to reflection
321 CCSUsrMsg_HudMsg *msg = (CCSUsrMsg_HudMsg *)g_UserMsgs.StartProtobufMessage(g_HudMsgNum, players, 1, 0);
322 msg->set_channel(textparms.channel & 0xFF);
324 CMsgVector2D *pos = msg->mutable_pos();
325 pos->set_x(textparms.x);
326 pos->set_y(textparms.y);
328 CMsgRGBA *color1 = msg->mutable_clr1();
329 color1->set_r(textparms.r1);
330 color1->set_g(textparms.g1);
331 color1->set_b(textparms.b1);
332 color1->set_a(textparms.a1);
334 CMsgRGBA *color2 = msg->mutable_clr2();
335 color2->set_r(textparms.r2);
336 color2->set_g(textparms.g2);
337 color2->set_b(textparms.b2);
338 color2->set_a(textparms.a2);
340 msg->set_effect(textparms.effect);
341 msg->set_fade_in_time(textparms.fadeinTime);
342 msg->set_fade_out_time(textparms.fadeoutTime);
343 msg->set_hold_time(textparms.holdTime);
344 msg->set_fx_time(textparms.fxTime);
345 msg->set_text(pMessage);
347 bf_write *bf = g_UserMsgs.StartBitBufMessage(g_HudMsgNum, players, 1, 0);
348 bf->WriteByte(textparms.channel & 0xFF );
349 bf->WriteFloat(textparms.x);
350 bf->WriteFloat(textparms.y);
351 bf->WriteByte(textparms.r1);
352 bf->WriteByte(textparms.g1);
353 bf->WriteByte(textparms.b1);
354 bf->WriteByte(textparms.a1);
355 bf->WriteByte(textparms.r2);
356 bf->WriteByte(textparms.g2);
357 bf->WriteByte(textparms.b2);
358 bf->WriteByte(textparms.a2);
359 bf->WriteByte(textparms.effect);
360 bf->WriteFloat(textparms.fadeinTime);
361 bf->WriteFloat(textparms.fadeoutTime);
362 bf->WriteFloat(textparms.holdTime);
363 bf->WriteFloat(textparms.fxTime);
364 bf->WriteString(pMessage);
366 g_UserMsgs.EndMessage();
369 static cell_t ShowSyncHudText(IPluginContext *pContext, const cell_t *params)
375 char message_buffer[255-36];
377 if (!s_HudMsgHelpers.IsSupported())
382 if ((err = s_HudMsgHelpers.ReadHudSyncObj(params[2], pContext->GetIdentity(), &obj)) != HandleError_None)
384 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
388 if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
390 return pContext->ThrowNativeError("Invalid client index %d", client);
392 else if (!pPlayer->IsInGame())
394 return pContext->ThrowNativeError("Client %d is not in-game", client);
397 g_SourceMod.FormatString(message_buffer, sizeof(message_buffer), pContext, params, 3);
398 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
403 g_hud_params.channel = s_HudMsgHelpers.AutoSelectChannel(client, obj);
404 UTIL_SendHudText(client, g_hud_params, message_buffer);
409 static cell_t ClearSyncHud(IPluginContext *pContext, const cell_t *params)
417 if (!s_HudMsgHelpers.IsSupported())
422 if ((err = s_HudMsgHelpers.ReadHudSyncObj(params[2], pContext->GetIdentity(), &obj)) != HandleError_None)
424 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
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 if ((channel = s_HudMsgHelpers.TryReuseLastChannel(client, obj)) == -1)
442 g_hud_params.channel = channel;
443 UTIL_SendHudText(client, g_hud_params, "");
445 return g_hud_params.channel;
448 static cell_t ShowHudText(IPluginContext *pContext, const cell_t *params)
452 char message_buffer[255-36];
454 if (!s_HudMsgHelpers.IsSupported())
460 if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
462 return pContext->ThrowNativeError("Invalid client index %d", client);
464 else if (!pPlayer->IsInGame())
466 return pContext->ThrowNativeError("Client %d is not in-game", client);
469 g_SourceMod.FormatString(message_buffer, sizeof(message_buffer), pContext, params, 3);
470 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
477 g_hud_params.channel = s_HudMsgHelpers.AutoSelectChannel(client);
481 g_hud_params.channel = params[2] % MAX_HUD_CHANNELS;
482 s_HudMsgHelpers.ManualSelectChannel(client, g_hud_params.channel);
485 UTIL_SendHudText(client, g_hud_params, message_buffer);
487 return g_hud_params.channel;
490 REGISTER_NATIVES(hudNatives)
492 {"ClearSyncHud", ClearSyncHud},
493 {"CreateHudSynchronizer", CreateHudSynchronizer},
494 {"SetHudTextParams", SetHudTextParams},
495 {"SetHudTextParamsEx", SetHudTextParamsEx},
496 {"ShowHudText", ShowHudText},
497 {"ShowSyncHudText", ShowSyncHudText},