core/smn_hudtext.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed Aug 14 14:20:57 2013 -0400 (2013-08-14)
changeset 4008 490b01aa0683
parent 3872 4c93f74cae51
child 4060 b4484bd36c22
permissions -rw-r--r--
Updated some CS:GO gamedata.
     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 "logic_bridge.h"
    38 #include "sourcemod.h"
    39 
    40 #if SOURCE_ENGINE == SE_DOTA
    41 #include <game/shared/protobuf/usermessages.pb.h>
    42 #elif SOURCE_ENGINE == SE_CSGO
    43 #include <game/shared/csgo/protobuf/cstrike15_usermessages.pb.h>
    44 #endif
    45 
    46 #define MAX_HUD_CHANNELS		6
    47 
    48 int g_HudMsgNum = -1;
    49 
    50 struct hud_syncobj_t
    51 {
    52 	int player_channels[256 + 1];
    53 };
    54 
    55 struct player_chaninfo_t
    56 {
    57 	double chan_times[MAX_HUD_CHANNELS];
    58 	hud_syncobj_t *chan_syncobjs[MAX_HUD_CHANNELS];
    59 };
    60 
    61 struct hud_text_parms
    62 {
    63 	float       x;
    64 	float       y;
    65 	int         effect;
    66 	byte        r1, g1, b1, a1;
    67 	byte        r2, g2, b2, a2;
    68 	float       fadeinTime;
    69 	float       fadeoutTime;
    70 	float       holdTime;
    71 	float       fxTime;
    72 	int         channel;
    73 };
    74 
    75 class HudMsgHelpers : 
    76 	public SMGlobalClass,
    77 	public IHandleTypeDispatch,
    78 	public IClientListener
    79 {
    80 public:
    81 	bool IsSupported()
    82 	{
    83 		return (g_HudMsgNum != -1);
    84 	}
    85 
    86 	virtual void OnSourceModAllInitialized_Post()
    87 	{
    88 		const char *key;
    89 
    90 		key = g_pGameConf->GetKeyValue("HudTextMsg");
    91 		if (key != NULL)
    92 		{
    93 			g_HudMsgNum = g_UserMsgs.GetMessageIndex(key);
    94 		}
    95 
    96 		if (!IsSupported())
    97 		{
    98 			m_hHudSyncObj = 0;
    99 			m_PlayerHuds = NULL;
   100 			return;
   101 		}
   102 
   103 		m_PlayerHuds = new player_chaninfo_t[256+1];
   104 		m_hHudSyncObj = handlesys->CreateType("HudSyncObj", this, 0, NULL, NULL, g_pCoreIdent, NULL);
   105 
   106 		g_Players.AddClientListener(this);
   107 	}
   108 
   109 	virtual void OnSourceModShutdown()
   110 	{
   111 		if (!IsSupported())
   112 		{
   113 			return;
   114 		}
   115 
   116 		delete [] m_PlayerHuds;
   117 		handlesys->RemoveType(m_hHudSyncObj, g_pCoreIdent);
   118 
   119 		g_Players.RemoveClientListener(this);
   120 	}
   121 
   122 	virtual void OnHandleDestroy(HandleType_t type, void *object)
   123 	{
   124 		hud_syncobj_t *obj = (hud_syncobj_t *)object;
   125 
   126 		delete obj;
   127 	}
   128 
   129 	virtual bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
   130 	{
   131 		*pSize = sizeof(unsigned int) * g_Players.GetMaxClients();
   132 		return true;
   133 	}
   134 
   135 	virtual void OnClientConnected(int client)
   136 	{
   137 		player_chaninfo_t *player;
   138 
   139 		player = &m_PlayerHuds[client];
   140 		memset(player->chan_syncobjs, 0, sizeof(player->chan_syncobjs));
   141 		memset(player->chan_times, 0, sizeof(player->chan_times));
   142 	}
   143 
   144 	Handle_t CreateHudSyncObj(IdentityToken_t *pIdent)
   145 	{
   146 		Handle_t hndl;
   147 		HandleError err;
   148 		hud_syncobj_t *obj;
   149 		HandleSecurity sec;
   150 
   151 		obj = new hud_syncobj_t;
   152 
   153 		memset(obj->player_channels, 0, sizeof(obj->player_channels));
   154 
   155 		sec = HandleSecurity(pIdent, g_pCoreIdent);
   156 		
   157 		if ((hndl = handlesys->CreateHandleEx(m_hHudSyncObj, obj, &sec, NULL, &err))
   158 			== BAD_HANDLE)
   159 		{
   160 			delete obj;
   161 		}
   162 
   163 		return hndl;
   164 	}
   165 
   166 	HandleError ReadHudSyncObj(Handle_t hndl, 
   167 		IdentityToken_t *pOwner, 
   168 		hud_syncobj_t **pObj)
   169 	{
   170 		HandleSecurity sec(pOwner, g_pCoreIdent);
   171 		return handlesys->ReadHandle(hndl, m_hHudSyncObj, &sec, (void **)pObj);
   172 	}
   173 
   174 	unsigned int AutoSelectChannel(unsigned int client)
   175 	{
   176 		int last_channel;
   177 		player_chaninfo_t *player;
   178 
   179 		player = &m_PlayerHuds[client];
   180 
   181 		last_channel = 0;
   182 		for (unsigned int i = 1; i < MAX_HUD_CHANNELS; i++)
   183 		{
   184 			if (player->chan_times[i] < player->chan_times[last_channel])
   185 			{
   186 				last_channel = i;
   187 			}
   188 		}
   189 
   190 		ManualSelectChannel(client, last_channel);
   191 
   192 		return last_channel;
   193 	}
   194 
   195 	int TryReuseLastChannel(unsigned int client, hud_syncobj_t *obj)
   196 	{
   197 		int last_channel;
   198 		player_chaninfo_t *player;
   199 
   200 		player = &m_PlayerHuds[client];
   201 
   202 		/* First, see if we can re-use the previous channel. */
   203 		last_channel = obj->player_channels[client];
   204 
   205 		if (player->chan_syncobjs[last_channel] == obj)
   206 		{
   207 			player->chan_times[last_channel] = *g_pUniversalTime;
   208 			return last_channel;
   209 		}
   210 
   211 		return -1;
   212 	}
   213 
   214 	int AutoSelectChannel(unsigned int client, hud_syncobj_t *obj)
   215 	{
   216 		int last_channel;
   217 		player_chaninfo_t *player;
   218 
   219 		player = &m_PlayerHuds[client];
   220 		
   221 		/* First, see if we can re-use the previous channel. */
   222 		last_channel = obj->player_channels[client];
   223 
   224 		if (player->chan_syncobjs[last_channel] == obj)
   225 		{
   226 			player->chan_times[last_channel] = *g_pUniversalTime;
   227 			return last_channel;
   228 		}
   229 
   230 		last_channel = 0;
   231 		for (unsigned int i = 1; i < MAX_HUD_CHANNELS; i++)
   232 		{
   233 			if (player->chan_times[i] < player->chan_times[last_channel])
   234 			{
   235 				last_channel = i;
   236 			}
   237 		}
   238 
   239 		obj->player_channels[client] = last_channel;
   240 		player->chan_syncobjs[last_channel] = obj;
   241 		player->chan_times[last_channel] = *g_pUniversalTime;
   242 
   243 		return last_channel;
   244 	}
   245 
   246 	int ManualSelectChannel(unsigned int client, int channel)
   247 	{
   248 		player_chaninfo_t *player;
   249 
   250 		player = &m_PlayerHuds[client];
   251 		player->chan_times[channel] = *g_pUniversalTime;
   252 		player->chan_syncobjs[channel] = NULL;
   253 
   254 		return channel;
   255 	}
   256 private:
   257 	HandleType_t m_hHudSyncObj;
   258 	player_chaninfo_t *m_PlayerHuds;
   259 } s_HudMsgHelpers;
   260 
   261 hud_text_parms g_hud_params;
   262 
   263 static cell_t CreateHudSynchronizer(IPluginContext *pContext, const cell_t *params)
   264 {
   265 	return s_HudMsgHelpers.CreateHudSyncObj(pContext->GetIdentity());
   266 }
   267 
   268 static cell_t SetHudTextParams(IPluginContext *pContext, const cell_t *params)
   269 {
   270 	g_hud_params.x = sp_ctof(params[1]);
   271 	g_hud_params.y = sp_ctof(params[2]);
   272 	g_hud_params.holdTime = sp_ctof(params[3]);
   273 	g_hud_params.r1 = static_cast<byte>(params[4]);
   274 	g_hud_params.g1 = static_cast<byte>(params[5]);
   275 	g_hud_params.b1 = static_cast<byte>(params[6]);
   276 	g_hud_params.a1 = static_cast<byte>(params[7]);
   277 	g_hud_params.effect = params[8];
   278 	g_hud_params.fxTime = sp_ctof(params[9]);
   279 	g_hud_params.fadeinTime = sp_ctof(params[10]);
   280 	g_hud_params.fadeoutTime = sp_ctof(params[11]);
   281 	g_hud_params.r2 = 255;
   282 	g_hud_params.g2 = 255;
   283 	g_hud_params.b2 = 250;
   284 	g_hud_params.a2 = 0;
   285 
   286 	return 1;
   287 }
   288 
   289 static cell_t SetHudTextParamsEx(IPluginContext *pContext, const cell_t *params)
   290 {
   291 	cell_t *color1, *color2;
   292 
   293 	pContext->LocalToPhysAddr(params[4], &color1);
   294 	pContext->LocalToPhysAddr(params[5], &color2);
   295 
   296 	g_hud_params.x = sp_ctof(params[1]);
   297 	g_hud_params.y = sp_ctof(params[2]);
   298 	g_hud_params.holdTime = sp_ctof(params[3]);
   299 	g_hud_params.r1 = static_cast<byte>(color1[0]);
   300 	g_hud_params.g1 = static_cast<byte>(color1[1]);
   301 	g_hud_params.b1 = static_cast<byte>(color1[2]);
   302 	g_hud_params.a1 = static_cast<byte>(color1[3]);
   303 	g_hud_params.effect = params[6];
   304 	g_hud_params.fxTime = sp_ctof(params[7]);
   305 	g_hud_params.fadeinTime = sp_ctof(params[8]);
   306 	g_hud_params.fadeoutTime = sp_ctof(params[9]);
   307 	g_hud_params.r2 = static_cast<byte>(color2[0]);
   308 	g_hud_params.g2 = static_cast<byte>(color2[1]);
   309 	g_hud_params.b2 = static_cast<byte>(color2[2]);
   310 	g_hud_params.a2 = static_cast<byte>(color2[3]);
   311 
   312 	return 1;
   313 }
   314 
   315 void UTIL_SendHudText(int client, const hud_text_parms &textparms, const char *pMessage)
   316 {
   317 	cell_t players[1];
   318 
   319 	players[0] = client;
   320 
   321 #if SOURCE_ENGINE == SE_DOTA
   322 	CUserMsg_HudMsg *msg = (CUserMsg_HudMsg *)g_UserMsgs.StartProtobufMessage(g_HudMsgNum, players, 1, 0);
   323 	msg->set_channel(textparms.channel & 0xFF);
   324 
   325 	msg->set_x(textparms.x);
   326 	msg->set_y(textparms.y);
   327 
   328 	Color c1(textparms.r1, textparms.g1, textparms.b1, textparms.a1);
   329 	msg->set_color1(c1.GetRawColor());
   330 
   331 	Color c2(textparms.r2, textparms.g2, textparms.b2, textparms.a2);
   332 	msg->set_color2(c2.GetRawColor());
   333 
   334 	msg->set_effect(textparms.effect);
   335 	msg->set_fade_in_time(textparms.fadeinTime);
   336 	msg->set_fade_out_time(textparms.fadeoutTime);
   337 	msg->set_hold_time(textparms.holdTime);
   338 	msg->set_fx_time(textparms.fxTime);
   339 	msg->set_message(pMessage);
   340 #elif SOURCE_ENGINE == SE_CSGO
   341 	CCSUsrMsg_HudMsg *msg = (CCSUsrMsg_HudMsg *)g_UserMsgs.StartProtobufMessage(g_HudMsgNum, players, 1, 0);
   342 	msg->set_channel(textparms.channel & 0xFF);
   343 
   344 	CMsgVector2D *pos = msg->mutable_pos();
   345 	pos->set_x(textparms.x);
   346 	pos->set_y(textparms.y);
   347 
   348 	CMsgRGBA *color1 = msg->mutable_clr1();
   349 	color1->set_r(textparms.r1);
   350 	color1->set_g(textparms.g1);
   351 	color1->set_b(textparms.b1);
   352 	color1->set_a(textparms.a1);
   353 
   354 	CMsgRGBA *color2 = msg->mutable_clr2();
   355 	color2->set_r(textparms.r2);
   356 	color2->set_g(textparms.g2);
   357 	color2->set_b(textparms.b2);
   358 	color2->set_a(textparms.a2);
   359 
   360 	msg->set_effect(textparms.effect);
   361 	msg->set_fade_in_time(textparms.fadeinTime);
   362 	msg->set_fade_out_time(textparms.fadeoutTime);
   363 	msg->set_hold_time(textparms.holdTime);
   364 	msg->set_fx_time(textparms.fxTime);
   365 	msg->set_text(pMessage);
   366 #else
   367 	bf_write *bf = g_UserMsgs.StartBitBufMessage(g_HudMsgNum, players, 1, 0);
   368 	bf->WriteByte(textparms.channel & 0xFF );
   369 	bf->WriteFloat(textparms.x);
   370 	bf->WriteFloat(textparms.y);
   371 	bf->WriteByte(textparms.r1);
   372 	bf->WriteByte(textparms.g1);
   373 	bf->WriteByte(textparms.b1);
   374 	bf->WriteByte(textparms.a1);
   375 	bf->WriteByte(textparms.r2);
   376 	bf->WriteByte(textparms.g2);
   377 	bf->WriteByte(textparms.b2);
   378 	bf->WriteByte(textparms.a2);
   379 	bf->WriteByte(textparms.effect);
   380 	bf->WriteFloat(textparms.fadeinTime);
   381 	bf->WriteFloat(textparms.fadeoutTime);
   382 	bf->WriteFloat(textparms.holdTime);
   383 	bf->WriteFloat(textparms.fxTime);
   384 	bf->WriteString(pMessage);
   385 #endif
   386 	g_UserMsgs.EndMessage();
   387 }
   388 
   389 static cell_t ShowSyncHudText(IPluginContext *pContext, const cell_t *params)
   390 {
   391 	int client;
   392 	Handle_t err;
   393 	CPlayer *pPlayer;
   394 	hud_syncobj_t *obj;
   395 	char message_buffer[255-36];
   396 
   397 	if (!s_HudMsgHelpers.IsSupported())
   398 	{
   399 		return -1;
   400 	}
   401 
   402 	if ((err = s_HudMsgHelpers.ReadHudSyncObj(params[2], pContext->GetIdentity(), &obj)) != HandleError_None)
   403 	{
   404 		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
   405 	}
   406 
   407 	client = params[1];
   408 	if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
   409 	{
   410 		return pContext->ThrowNativeError("Invalid client index %d", client);
   411 	}
   412 	else if (!pPlayer->IsInGame())
   413 	{
   414 		return pContext->ThrowNativeError("Client %d is not in-game", client);
   415 	}
   416 
   417 	g_SourceMod.FormatString(message_buffer, sizeof(message_buffer), pContext, params, 3);
   418 	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
   419 	{
   420 		return 0;
   421 	}
   422 
   423 	g_hud_params.channel = s_HudMsgHelpers.AutoSelectChannel(client, obj);
   424 	UTIL_SendHudText(client, g_hud_params, message_buffer);
   425 
   426 	return 1;
   427 }
   428 
   429 static cell_t ClearSyncHud(IPluginContext *pContext, const cell_t *params)
   430 {
   431 	int client;
   432 	int channel;
   433 	Handle_t err;
   434 	CPlayer *pPlayer;
   435 	hud_syncobj_t *obj;
   436 
   437 	if (!s_HudMsgHelpers.IsSupported())
   438 	{
   439 		return -1;
   440 	}
   441 
   442 	if ((err = s_HudMsgHelpers.ReadHudSyncObj(params[2], pContext->GetIdentity(), &obj)) != HandleError_None)
   443 	{
   444 		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
   445 	}
   446 
   447 	client = params[1];
   448 	if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
   449 	{
   450 		return pContext->ThrowNativeError("Invalid client index %d", client);
   451 	}
   452 	else if (!pPlayer->IsInGame())
   453 	{
   454 		return pContext->ThrowNativeError("Client %d is not in-game", client);
   455 	}
   456 
   457 	if ((channel = s_HudMsgHelpers.TryReuseLastChannel(client, obj)) == -1)
   458 	{
   459 		return -1;
   460 	}
   461 
   462 	g_hud_params.channel = channel;
   463 	UTIL_SendHudText(client, g_hud_params, "");
   464 
   465 	return g_hud_params.channel;
   466 }
   467 
   468 static cell_t ShowHudText(IPluginContext *pContext, const cell_t *params)
   469 {
   470 	int client;
   471 	CPlayer *pPlayer;
   472 	char message_buffer[255-36];
   473 
   474 	if (!s_HudMsgHelpers.IsSupported())
   475 	{
   476 		return -1;
   477 	}
   478 
   479 	client = params[1];
   480 	if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
   481 	{
   482 		return pContext->ThrowNativeError("Invalid client index %d", client);
   483 	}
   484 	else if (!pPlayer->IsInGame())
   485 	{
   486 		return pContext->ThrowNativeError("Client %d is not in-game", client);
   487 	}
   488 
   489 	g_SourceMod.FormatString(message_buffer, sizeof(message_buffer), pContext, params, 3);
   490 	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
   491 	{
   492 		return 0;
   493 	}
   494 
   495 	if (params[2] == -1)
   496 	{
   497 		g_hud_params.channel = s_HudMsgHelpers.AutoSelectChannel(client);
   498 	}
   499 	else
   500 	{
   501 		g_hud_params.channel = params[2] % MAX_HUD_CHANNELS;
   502 		s_HudMsgHelpers.ManualSelectChannel(client, g_hud_params.channel);
   503 	}
   504 
   505 	UTIL_SendHudText(client, g_hud_params, message_buffer);
   506 
   507 	return g_hud_params.channel;
   508 }
   509 
   510 REGISTER_NATIVES(hudNatives)
   511 {
   512 	{"ClearSyncHud",				ClearSyncHud},
   513 	{"CreateHudSynchronizer",		CreateHudSynchronizer},
   514 	{"SetHudTextParams",			SetHudTextParams},
   515 	{"SetHudTextParamsEx",			SetHudTextParamsEx},
   516 	{"ShowHudText",					ShowHudText},
   517 	{"ShowSyncHudText",				ShowSyncHudText},
   518 	{NULL,							NULL},
   519 };