core/smn_hudtext.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Feb 08 19:57:20 2010 -0800 (2010-02-08)
changeset 2914 bf5f9aa53abe
parent 2816 4e853c011546
child 3010 8ea042c00c80
permissions -rw-r--r--
Fixed FindEntityByClassname crash in Synergy (bug 4235, r=dvander).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "sm_globals.h"
    33 #include "GameConfigs.h"
    34 #include "UserMessages.h"
    35 #include "TimerSys.h"
    36 #include "PlayerManager.h"
    37 #include "HandleSys.h"
    38 
    39 #define MAX_HUD_CHANNELS		6
    40 
    41 int g_HudMsgNum = -1;
    42 
    43 struct hud_syncobj_t
    44 {
    45 	int player_channels[256 + 1];
    46 };
    47 
    48 struct player_chaninfo_t
    49 {
    50 	double chan_times[MAX_HUD_CHANNELS];
    51 	hud_syncobj_t *chan_syncobjs[MAX_HUD_CHANNELS];
    52 };
    53 
    54 struct hud_text_parms
    55 {
    56 	float       x;
    57 	float       y;
    58 	int         effect;
    59 	byte        r1, g1, b1, a1;
    60 	byte        r2, g2, b2, a2;
    61 	float       fadeinTime;
    62 	float       fadeoutTime;
    63 	float       holdTime;
    64 	float       fxTime;
    65 	int         channel;
    66 };
    67 
    68 class HudMsgHelpers : 
    69 	public SMGlobalClass,
    70 	public IHandleTypeDispatch,
    71 	public IClientListener
    72 {
    73 public:
    74 	bool IsSupported()
    75 	{
    76 		return (g_HudMsgNum != -1);
    77 	}
    78 
    79 	virtual void OnSourceModAllInitialized_Post()
    80 	{
    81 		const char *key;
    82 
    83 		key = g_pGameConf->GetKeyValue("HudTextMsg");
    84 		if (key != NULL)
    85 		{
    86 			g_HudMsgNum = g_UserMsgs.GetMessageIndex(key);
    87 		}
    88 
    89 		if (!IsSupported())
    90 		{
    91 			m_hHudSyncObj = 0;
    92 			m_PlayerHuds = NULL;
    93 			return;
    94 		}
    95 
    96 		m_PlayerHuds = new player_chaninfo_t[256+1];
    97 		m_hHudSyncObj = g_HandleSys.CreateType("HudSyncObj", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    98 
    99 		g_Players.AddClientListener(this);
   100 	}
   101 
   102 	virtual void OnSourceModShutdown()
   103 	{
   104 		if (!IsSupported())
   105 		{
   106 			return;
   107 		}
   108 
   109 		delete [] m_PlayerHuds;
   110 		g_HandleSys.RemoveType(m_hHudSyncObj, g_pCoreIdent);
   111 
   112 		g_Players.RemoveClientListener(this);
   113 	}
   114 
   115 	virtual void OnHandleDestroy(HandleType_t type, void *object)
   116 	{
   117 		hud_syncobj_t *obj = (hud_syncobj_t *)object;
   118 
   119 		delete obj;
   120 	}
   121 
   122 	virtual bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
   123 	{
   124 		*pSize = sizeof(unsigned int) * g_Players.GetMaxClients();
   125 		return true;
   126 	}
   127 
   128 	virtual void OnClientConnected(int client)
   129 	{
   130 		player_chaninfo_t *player;
   131 
   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));
   135 	}
   136 
   137 	Handle_t CreateHudSyncObj(IdentityToken_t *pIdent)
   138 	{
   139 		Handle_t hndl;
   140 		HandleError err;
   141 		hud_syncobj_t *obj;
   142 		HandleSecurity sec;
   143 
   144 		obj = new hud_syncobj_t;
   145 
   146 		memset(obj->player_channels, 0, sizeof(obj->player_channels));
   147 
   148 		sec = HandleSecurity(pIdent, g_pCoreIdent);
   149 		
   150 		if ((hndl = g_HandleSys.CreateHandleEx(m_hHudSyncObj, obj, &sec, NULL, &err))
   151 			== BAD_HANDLE)
   152 		{
   153 			delete obj;
   154 		}
   155 
   156 		return hndl;
   157 	}
   158 
   159 	HandleError ReadHudSyncObj(Handle_t hndl, 
   160 		IdentityToken_t *pOwner, 
   161 		hud_syncobj_t **pObj)
   162 	{
   163 		HandleSecurity sec(pOwner, g_pCoreIdent);
   164 		return g_HandleSys.ReadHandle(hndl, m_hHudSyncObj, &sec, (void **)pObj);
   165 	}
   166 
   167 	unsigned int AutoSelectChannel(unsigned int client)
   168 	{
   169 		int last_channel;
   170 		player_chaninfo_t *player;
   171 
   172 		player = &m_PlayerHuds[client];
   173 
   174 		last_channel = 0;
   175 		for (unsigned int i = 1; i < MAX_HUD_CHANNELS; i++)
   176 		{
   177 			if (player->chan_times[i] < player->chan_times[last_channel])
   178 			{
   179 				last_channel = i;
   180 			}
   181 		}
   182 
   183 		ManualSelectChannel(client, last_channel);
   184 
   185 		return last_channel;
   186 	}
   187 
   188 	int TryReuseLastChannel(unsigned int client, hud_syncobj_t *obj)
   189 	{
   190 		int last_channel;
   191 		player_chaninfo_t *player;
   192 
   193 		player = &m_PlayerHuds[client];
   194 
   195 		/* First, see if we can re-use the previous channel. */
   196 		last_channel = obj->player_channels[client];
   197 
   198 		if (player->chan_syncobjs[last_channel] == obj)
   199 		{
   200 			player->chan_times[last_channel] = *g_pUniversalTime;
   201 			return last_channel;
   202 		}
   203 
   204 		return -1;
   205 	}
   206 
   207 	int AutoSelectChannel(unsigned int client, hud_syncobj_t *obj)
   208 	{
   209 		int last_channel;
   210 		player_chaninfo_t *player;
   211 
   212 		player = &m_PlayerHuds[client];
   213 		
   214 		/* First, see if we can re-use the previous channel. */
   215 		last_channel = obj->player_channels[client];
   216 
   217 		if (player->chan_syncobjs[last_channel] == obj)
   218 		{
   219 			player->chan_times[last_channel] = *g_pUniversalTime;
   220 			return last_channel;
   221 		}
   222 
   223 		last_channel = 0;
   224 		for (unsigned int i = 1; i < MAX_HUD_CHANNELS; i++)
   225 		{
   226 			if (player->chan_times[i] < player->chan_times[last_channel])
   227 			{
   228 				last_channel = i;
   229 			}
   230 		}
   231 
   232 		obj->player_channels[client] = last_channel;
   233 		player->chan_syncobjs[last_channel] = obj;
   234 		player->chan_times[last_channel] = *g_pUniversalTime;
   235 
   236 		return last_channel;
   237 	}
   238 
   239 	int ManualSelectChannel(unsigned int client, int channel)
   240 	{
   241 		player_chaninfo_t *player;
   242 
   243 		player = &m_PlayerHuds[client];
   244 		player->chan_times[channel] = *g_pUniversalTime;
   245 		player->chan_syncobjs[channel] = NULL;
   246 
   247 		return channel;
   248 	}
   249 private:
   250 	HandleType_t m_hHudSyncObj;
   251 	player_chaninfo_t *m_PlayerHuds;
   252 } s_HudMsgHelpers;
   253 
   254 hud_text_parms g_hud_params;
   255 
   256 static cell_t CreateHudSynchronizer(IPluginContext *pContext, const cell_t *params)
   257 {
   258 	return s_HudMsgHelpers.CreateHudSyncObj(pContext->GetIdentity());
   259 }
   260 
   261 static cell_t SetHudTextParams(IPluginContext *pContext, const cell_t *params)
   262 {
   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;
   277 	g_hud_params.a2 = 0;
   278 
   279 	return 1;
   280 }
   281 
   282 static cell_t SetHudTextParamsEx(IPluginContext *pContext, const cell_t *params)
   283 {
   284 	cell_t *color1, *color2;
   285 
   286 	pContext->LocalToPhysAddr(params[4], &color1);
   287 	pContext->LocalToPhysAddr(params[5], &color2);
   288 
   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]);
   304 
   305 	return 1;
   306 }
   307 
   308 void UTIL_SendHudText(int client, const hud_text_parms &textparms, const char *pMessage)
   309 {
   310 	bf_write *bf;
   311 	cell_t players[1];
   312 
   313 	players[0] = client;
   314 
   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();
   334 }
   335 
   336 static cell_t ShowSyncHudText(IPluginContext *pContext, const cell_t *params)
   337 {
   338 	int client;
   339 	Handle_t err;
   340 	CPlayer *pPlayer;
   341 	hud_syncobj_t *obj;
   342 	char message_buffer[255-36];
   343 
   344 	if (!s_HudMsgHelpers.IsSupported())
   345 	{
   346 		return -1;
   347 	}
   348 
   349 	if ((err = s_HudMsgHelpers.ReadHudSyncObj(params[2], pContext->GetIdentity(), &obj)) != HandleError_None)
   350 	{
   351 		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
   352 	}
   353 
   354 	client = params[1];
   355 	if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
   356 	{
   357 		return pContext->ThrowNativeError("Invalid client index %d", client);
   358 	}
   359 	else if (!pPlayer->IsInGame())
   360 	{
   361 		return pContext->ThrowNativeError("Client %d is not in-game", client);
   362 	}
   363 
   364 	g_SourceMod.FormatString(message_buffer, sizeof(message_buffer), pContext, params, 3);
   365 	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
   366 	{
   367 		return 0;
   368 	}
   369 
   370 	g_hud_params.channel = s_HudMsgHelpers.AutoSelectChannel(client, obj);
   371 	UTIL_SendHudText(client, g_hud_params, message_buffer);
   372 
   373 	return 1;
   374 }
   375 
   376 static cell_t ClearSyncHud(IPluginContext *pContext, const cell_t *params)
   377 {
   378 	int client;
   379 	int channel;
   380 	Handle_t err;
   381 	CPlayer *pPlayer;
   382 	hud_syncobj_t *obj;
   383 
   384 	if (!s_HudMsgHelpers.IsSupported())
   385 	{
   386 		return -1;
   387 	}
   388 
   389 	if ((err = s_HudMsgHelpers.ReadHudSyncObj(params[2], pContext->GetIdentity(), &obj)) != HandleError_None)
   390 	{
   391 		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
   392 	}
   393 
   394 	client = params[1];
   395 	if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
   396 	{
   397 		return pContext->ThrowNativeError("Invalid client index %d", client);
   398 	}
   399 	else if (!pPlayer->IsInGame())
   400 	{
   401 		return pContext->ThrowNativeError("Client %d is not in-game", client);
   402 	}
   403 
   404 	if ((channel = s_HudMsgHelpers.TryReuseLastChannel(client, obj)) == -1)
   405 	{
   406 		return -1;
   407 	}
   408 
   409 	g_hud_params.channel = channel;
   410 	UTIL_SendHudText(client, g_hud_params, "");
   411 
   412 	return g_hud_params.channel;
   413 }
   414 
   415 static cell_t ShowHudText(IPluginContext *pContext, const cell_t *params)
   416 {
   417 	int client;
   418 	CPlayer *pPlayer;
   419 	char message_buffer[255-36];
   420 
   421 	if (!s_HudMsgHelpers.IsSupported())
   422 	{
   423 		return -1;
   424 	}
   425 
   426 	client = params[1];
   427 	if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
   428 	{
   429 		return pContext->ThrowNativeError("Invalid client index %d", client);
   430 	}
   431 	else if (!pPlayer->IsInGame())
   432 	{
   433 		return pContext->ThrowNativeError("Client %d is not in-game", client);
   434 	}
   435 
   436 	g_SourceMod.FormatString(message_buffer, sizeof(message_buffer), pContext, params, 3);
   437 	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
   438 	{
   439 		return 0;
   440 	}
   441 
   442 	if (params[2] == -1)
   443 	{
   444 		g_hud_params.channel = s_HudMsgHelpers.AutoSelectChannel(client);
   445 	}
   446 	else
   447 	{
   448 		g_hud_params.channel = params[2] % MAX_HUD_CHANNELS;
   449 		s_HudMsgHelpers.ManualSelectChannel(client, g_hud_params.channel);
   450 	}
   451 
   452 	UTIL_SendHudText(client, g_hud_params, message_buffer);
   453 
   454 	return g_hud_params.channel;
   455 }
   456 
   457 REGISTER_NATIVES(hudNatives)
   458 {
   459 	{"ClearSyncHud",				ClearSyncHud},
   460 	{"CreateHudSynchronizer",		CreateHudSynchronizer},
   461 	{"SetHudTextParams",			SetHudTextParams},
   462 	{"SetHudTextParamsEx",			SetHudTextParamsEx},
   463 	{"ShowHudText",					ShowHudText},
   464 	{"ShowSyncHudText",				ShowSyncHudText},
   465 	{NULL,							NULL},
   466 };