ddnet/src/game/client/components/scoreboard.cpp

330 lines
9.3 KiB
C++
Raw Normal View History

2010-05-29 07:25:38 +00:00
#include <engine/graphics.h>
#include <engine/textrender.h>
#include <engine/shared/config.h>
#include <game/generated/protocol.h>
#include <game/generated/client_data.h>
#include <game/client/gameclient.h>
#include <game/client/animstate.h>
#include <game/client/render.h>
#include <game/client/components/motd.h>
#include <game/localization.h>
#include "scoreboard.h"
CScoreboard::CScoreboard()
2008-08-27 19:50:33 +00:00
{
2010-05-29 07:25:38 +00:00
OnReset();
2008-08-27 19:50:33 +00:00
}
2010-05-29 07:25:38 +00:00
void CScoreboard::ConKeyScoreboard(IConsole::IResult *pResult, void *pUserData)
2008-08-27 19:50:33 +00:00
{
2010-05-29 07:25:38 +00:00
((CScoreboard *)pUserData)->m_Active = pResult->GetInteger(0) != 0;
2008-08-27 19:50:33 +00:00
}
2010-05-29 07:25:38 +00:00
void CScoreboard::OnReset()
2008-08-27 19:50:33 +00:00
{
2010-05-29 07:25:38 +00:00
m_Active = false;
2008-08-27 19:50:33 +00:00
}
2010-05-29 07:25:38 +00:00
void CScoreboard::OnConsoleInit()
2008-08-27 19:50:33 +00:00
{
2010-05-29 07:25:38 +00:00
Console()->Register("+scoreboard", "", CFGFLAG_CLIENT, ConKeyScoreboard, this, "Show scoreboard");
2008-08-27 19:50:33 +00:00
}
2010-05-29 07:25:38 +00:00
void CScoreboard::RenderGoals(float x, float y, float w)
{
float h = 50.0f;
2009-10-27 14:38:53 +00:00
Graphics()->BlendNormal();
Graphics()->TextureSet(-1);
Graphics()->QuadsBegin();
Graphics()->SetColor(0,0,0,0.5f);
2010-05-29 07:25:38 +00:00
RenderTools()->DrawRoundRect(x-10.f, y-10.f, w, h, 10.0f);
2009-10-27 14:38:53 +00:00
Graphics()->QuadsEnd();
// render goals
//y = ystart+h-54;
2008-09-07 15:07:08 +00:00
float tw = 0.0f;
2010-05-29 07:25:38 +00:00
if(m_pClient->m_Snap.m_pGameobj)
2008-09-07 15:07:08 +00:00
{
2010-05-29 07:25:38 +00:00
if(m_pClient->m_Snap.m_pGameobj->m_ScoreLimit)
{
char aBuf[64];
str_format(aBuf, sizeof(aBuf), "%s: %d", Localize("Score limit"), m_pClient->m_Snap.m_pGameobj->m_ScoreLimit);
TextRender()->Text(0, x+20.0f, y, 22.0f, aBuf, -1);
tw += TextRender()->TextWidth(0, 22.0f, aBuf, -1);
}
if(m_pClient->m_Snap.m_pGameobj->m_TimeLimit)
{
char aBuf[64];
str_format(aBuf, sizeof(aBuf), "%s: %d min", Localize("Time limit"), m_pClient->m_Snap.m_pGameobj->m_TimeLimit);
TextRender()->Text(0, x+220.0f, y, 22.0f, aBuf, -1);
tw += TextRender()->TextWidth(0, 22.0f, aBuf, -1);
}
if(m_pClient->m_Snap.m_pGameobj->m_RoundNum && m_pClient->m_Snap.m_pGameobj->m_RoundCurrent)
{
char aBuf[64];
str_format(aBuf, sizeof(aBuf), "%s %d/%d", Localize("Round"), m_pClient->m_Snap.m_pGameobj->m_RoundCurrent, m_pClient->m_Snap.m_pGameobj->m_RoundNum);
TextRender()->Text(0, x+450.0f, y, 22.0f, aBuf, -1);
/*[48c3fd4c][game/scoreboard]: timelimit x:219.428558
[48c3fd4c][game/scoreboard]: round x:453.142822*/
}
}
}
2010-05-29 07:25:38 +00:00
void CScoreboard::RenderSpectators(float x, float y, float w)
{
2010-05-29 07:25:38 +00:00
char aBuffer[1024*4];
int Count = 0;
float h = 120.0f;
2010-05-29 07:25:38 +00:00
str_format(aBuffer, sizeof(aBuffer), "%s: ", Localize("Spectators"));
2009-10-27 14:38:53 +00:00
Graphics()->BlendNormal();
Graphics()->TextureSet(-1);
Graphics()->QuadsBegin();
Graphics()->SetColor(0,0,0,0.5f);
2010-05-29 07:25:38 +00:00
RenderTools()->DrawRoundRect(x-10.f, y-10.f, w, h, 10.0f);
2009-10-27 14:38:53 +00:00
Graphics()->QuadsEnd();
2010-05-29 07:25:38 +00:00
for(int i = 0; i < Client()->SnapNumItems(IClient::SNAP_CURRENT); i++)
{
2010-05-29 07:25:38 +00:00
IClient::CSnapItem Item;
const void *pData = Client()->SnapGetItem(IClient::SNAP_CURRENT, i, &Item);
2010-05-29 07:25:38 +00:00
if(Item.m_Type == NETOBJTYPE_PLAYERINFO)
{
2010-05-29 07:25:38 +00:00
const CNetObj_PlayerInfo *pInfo = (const CNetObj_PlayerInfo *)pData;
if(pInfo->m_Team == -1)
{
2010-05-29 07:25:38 +00:00
if(Count)
str_append(aBuffer, ", ", sizeof(aBuffer));
str_append(aBuffer, m_pClient->m_aClients[pInfo->m_ClientId].m_aName, sizeof(aBuffer));
Count++;
}
}
}
2010-05-29 07:25:38 +00:00
TextRender()->Text(0, x+10, y, 32, aBuffer, (int)w-20);
}
2010-05-29 07:25:38 +00:00
void CScoreboard::RenderScoreboard(float x, float y, float w, int Team, const char *pTitle)
{
//float ystart = y;
float h = 750.0f;
2009-10-27 14:38:53 +00:00
Graphics()->BlendNormal();
Graphics()->TextureSet(-1);
Graphics()->QuadsBegin();
Graphics()->SetColor(0,0,0,0.5f);
2010-05-29 07:25:38 +00:00
RenderTools()->DrawRoundRect(x-10.f, y-10.f, w, h, 17.0f);
2009-10-27 14:38:53 +00:00
Graphics()->QuadsEnd();
// render title
2010-05-29 07:25:38 +00:00
if(!pTitle)
{
2010-05-29 07:25:38 +00:00
if(m_pClient->m_Snap.m_pGameobj->m_GameOver)
pTitle = Localize("Game over");
else
2010-05-29 07:25:38 +00:00
pTitle = Localize("Score board");
}
2010-05-29 07:25:38 +00:00
float tw = TextRender()->TextWidth(0, 48, pTitle, -1);
2010-05-29 07:25:38 +00:00
if(Team == -1)
{
2010-05-29 07:25:38 +00:00
TextRender()->Text(0, x+w/2-tw/2, y, 48, pTitle, -1);
}
else
{
2010-05-29 07:25:38 +00:00
TextRender()->Text(0, x+10, y, 48, pTitle, -1);
2010-05-29 07:25:38 +00:00
if(m_pClient->m_Snap.m_pGameobj)
{
2010-05-29 07:25:38 +00:00
char aBuf[128];
int Score = Team ? m_pClient->m_Snap.m_pGameobj->m_TeamscoreBlue : m_pClient->m_Snap.m_pGameobj->m_TeamscoreRed;
str_format(aBuf, sizeof(aBuf), "%d", Score);
tw = TextRender()->TextWidth(0, 48, aBuf, -1);
TextRender()->Text(0, x+w-tw-30, y, 48, aBuf, -1);
}
}
y += 54.0f;
// find players
2010-05-29 07:25:38 +00:00
const CNetObj_PlayerInfo *paPlayers[MAX_CLIENTS] = {0};
int NumPlayers = 0;
for(int i = 0; i < Client()->SnapNumItems(IClient::SNAP_CURRENT); i++)
{
2010-05-29 07:25:38 +00:00
IClient::CSnapItem Item;
const void *pData = Client()->SnapGetItem(IClient::SNAP_CURRENT, i, &Item);
2010-05-29 07:25:38 +00:00
if(Item.m_Type == NETOBJTYPE_PLAYERINFO)
{
2010-05-29 07:25:38 +00:00
const CNetObj_PlayerInfo *pInfo = (const CNetObj_PlayerInfo *)pData;
if(pInfo->m_Team == Team)
2009-05-31 09:44:20 +00:00
{
2010-05-29 07:25:38 +00:00
paPlayers[NumPlayers] = pInfo;
if(++NumPlayers == MAX_CLIENTS)
break;
2009-05-31 09:44:20 +00:00
}
}
}
// sort players
for(int k = 0; k < NumPlayers-1; k++) // ffs, bubblesort
{
2010-05-29 07:25:38 +00:00
for(int i = 0; i < NumPlayers-k-1; i++)
{
2010-05-29 07:25:38 +00:00
if(paPlayers[i]->m_Score < paPlayers[i+1]->m_Score)
{
2010-05-29 07:25:38 +00:00
const CNetObj_PlayerInfo *pTmp = paPlayers[i];
paPlayers[i] = paPlayers[i+1];
paPlayers[i+1] = pTmp;
}
}
}
// render headlines
2010-05-29 07:25:38 +00:00
TextRender()->Text(0, x+10, y, 24.0f, Localize("Score"), -1);
TextRender()->Text(0, x+125, y, 24.0f, Localize("Name"), -1);
TextRender()->Text(0, x+w-70, y, 24.0f, Localize("Ping"), -1);
y += 29.0f;
2010-05-29 07:25:38 +00:00
float FontSize = 35.0f;
float LineHeight = 50.0f;
float TeeSizeMod = 1.0f;
float TeeOffset = 0.0f;
2009-05-31 09:44:20 +00:00
2010-05-29 07:25:38 +00:00
if(NumPlayers > 13)
2009-05-31 09:44:20 +00:00
{
2010-05-29 07:25:38 +00:00
FontSize = 30.0f;
LineHeight = 40.0f;
TeeSizeMod = 0.8f;
TeeOffset = -5.0f;
2009-05-31 09:44:20 +00:00
}
// render player scores
2010-05-29 07:25:38 +00:00
for(int i = 0; i < NumPlayers; i++)
{
2010-05-29 07:25:38 +00:00
const CNetObj_PlayerInfo *pInfo = paPlayers[i];
// make sure that we render the correct team
2010-05-29 07:25:38 +00:00
char aBuf[128];
if(pInfo->m_Local)
{
// background so it's easy to find the local player
2009-10-27 14:38:53 +00:00
Graphics()->TextureSet(-1);
Graphics()->QuadsBegin();
Graphics()->SetColor(1,1,1,0.25f);
2010-05-29 07:25:38 +00:00
RenderTools()->DrawRoundRect(x, y, w-20, LineHeight*0.95f, 17.0f);
2009-10-27 14:38:53 +00:00
Graphics()->QuadsEnd();
}
2010-05-29 07:25:38 +00:00
str_format(aBuf, sizeof(aBuf), "%4d", pInfo->m_Score);
TextRender()->Text(0, x+60-TextRender()->TextWidth(0, FontSize,aBuf,-1), y, FontSize, aBuf, -1);
float FontSizeName = FontSize;
while(TextRender()->TextWidth(0, FontSizeName, m_pClient->m_aClients[pInfo->m_ClientId].m_aName, -1) > w-200)
--FontSizeName;
TextRender()->Text(0, x+128, y+(FontSize-FontSizeName)/2, FontSizeName, m_pClient->m_aClients[pInfo->m_ClientId].m_aName, -1);
2010-05-29 07:25:38 +00:00
str_format(aBuf, sizeof(aBuf), "%4d", pInfo->m_Latency);
float tw = TextRender()->TextWidth(0, FontSize, aBuf, -1);
TextRender()->Text(0, x+w-tw-35, y, FontSize, aBuf, -1);
// render avatar
2010-05-29 07:25:38 +00:00
if((m_pClient->m_Snap.m_paFlags[0] && m_pClient->m_Snap.m_paFlags[0]->m_CarriedBy == pInfo->m_ClientId) ||
(m_pClient->m_Snap.m_paFlags[1] && m_pClient->m_Snap.m_paFlags[1]->m_CarriedBy == pInfo->m_ClientId))
{
2009-10-27 14:38:53 +00:00
Graphics()->BlendNormal();
2010-05-29 07:25:38 +00:00
Graphics()->TextureSet(g_pData->m_aImages[IMAGE_GAME].m_Id);
2009-10-27 14:38:53 +00:00
Graphics()->QuadsBegin();
2010-05-29 07:25:38 +00:00
if(pInfo->m_Team == 0) RenderTools()->SelectSprite(SPRITE_FLAG_BLUE, SPRITE_FLAG_FLIP_X);
else RenderTools()->SelectSprite(SPRITE_FLAG_RED, SPRITE_FLAG_FLIP_X);
float size = 64.0f;
2010-05-29 07:25:38 +00:00
IGraphics::CQuadItem QuadItem(x+55, y-15, size/2, size);
Graphics()->QuadsDrawTL(&QuadItem, 1);
2009-10-27 14:38:53 +00:00
Graphics()->QuadsEnd();
}
2010-05-29 07:25:38 +00:00
CTeeRenderInfo TeeInfo = m_pClient->m_aClients[pInfo->m_ClientId].m_RenderInfo;
TeeInfo.m_Size *= TeeSizeMod;
RenderTools()->RenderTee(CAnimState::GetIdle(), &TeeInfo, EMOTE_NORMAL, vec2(1,0), vec2(x+90, y+28+TeeOffset));
2010-05-29 07:25:38 +00:00
y += LineHeight;
}
}
2010-05-29 07:25:38 +00:00
void CScoreboard::OnRender()
{
2010-05-29 07:25:38 +00:00
bool DoScoreBoard = false;
// if we activly wanna look on the scoreboard
2010-05-29 07:25:38 +00:00
if(m_Active)
DoScoreBoard = true;
2010-05-29 07:25:38 +00:00
if(m_pClient->m_Snap.m_pLocalInfo && m_pClient->m_Snap.m_pLocalInfo->m_Team != -1)
{
// we are not a spectator, check if we are ead
2010-05-29 07:25:38 +00:00
if(!m_pClient->m_Snap.m_pLocalCharacter || m_pClient->m_Snap.m_pLocalCharacter->m_Health < 0)
DoScoreBoard = true;
}
// if we the game is over
2010-05-29 07:25:38 +00:00
if(m_pClient->m_Snap.m_pGameobj && m_pClient->m_Snap.m_pGameobj->m_GameOver)
DoScoreBoard = true;
2010-05-29 07:25:38 +00:00
if(!DoScoreBoard)
return;
// if the score board is active, then we should clear the motd message aswell
if(m_pClient->m_pMotd->IsActive())
2010-05-29 07:25:38 +00:00
m_pClient->m_pMotd->Clear();
2010-05-29 07:25:38 +00:00
float Width = 400*3.0f*Graphics()->ScreenAspect();
float Height = 400*3.0f;
2010-05-29 07:25:38 +00:00
Graphics()->MapScreen(0, 0, Width, Height);
float w = 650.0f;
2010-05-29 07:25:38 +00:00
if(m_pClient->m_Snap.m_pGameobj && !(m_pClient->m_Snap.m_pGameobj->m_Flags&GAMEFLAG_TEAMS))
{
2010-05-29 07:25:38 +00:00
RenderScoreboard(Width/2-w/2, 150.0f, w, 0, 0);
//render_scoreboard(gameobj, 0, 0, -1, 0);
}
else
{
2010-05-29 07:25:38 +00:00
if(m_pClient->m_Snap.m_pGameobj && m_pClient->m_Snap.m_pGameobj->m_GameOver)
{
2010-05-29 07:25:38 +00:00
const char *pText = Localize("Draw!");
if(m_pClient->m_Snap.m_pGameobj->m_TeamscoreRed > m_pClient->m_Snap.m_pGameobj->m_TeamscoreBlue)
pText = Localize("Red team wins!");
else if(m_pClient->m_Snap.m_pGameobj->m_TeamscoreBlue > m_pClient->m_Snap.m_pGameobj->m_TeamscoreRed)
pText = Localize("Blue team wins!");
2010-05-29 07:25:38 +00:00
float w = TextRender()->TextWidth(0, 92.0f, pText, -1);
TextRender()->Text(0, Width/2-w/2, 45, 92.0f, pText, -1);
}
2010-05-29 07:25:38 +00:00
RenderScoreboard(Width/2-w-20, 150.0f, w, 0, Localize("Red team"));
RenderScoreboard(Width/2 + 20, 150.0f, w, 1, Localize("Blue team"));
}
2010-05-29 07:25:38 +00:00
RenderGoals(Width/2-w/2, 150+750+25, w);
RenderSpectators(Width/2-w/2, 150+750+25+50+25, w);
}
bool CScoreboard::Active()
{
return m_Active | (m_pClient->m_Snap.m_pGameobj && m_pClient->m_Snap.m_pGameobj->m_GameOver);
}