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